private Function getReferencedFunction(boolean autoThunkOK, Program program,
			TaskMonitor monitor) {

		Listing listing = program.getListing();

		if (referencedSymbol != null) {
			Object obj = referencedSymbol.getObject();
			if (obj instanceof Function) {
				return (Function) obj;
			}
			if (obj instanceof ExternalLocation) {
				return ((ExternalLocation) obj).createFunction();
			}
			referencedFunctionAddr = referencedSymbol.getAddress();
		}
		else if ((referencedFunctionAddr == null || referencedFunctionAddr == Address.NO_ADDRESS) &&
			autoThunkOK) {
			// first try to get the address that is already there
			referencedFunctionAddr = getThunkedExternalFunctionAddress(program, entry);
			if (referencedFunctionAddr == null) {
				referencedFunctionAddr = getThunkedAddr(program, entry, checkForSideEffects);
			}
			// if can't get an address, try to calculate it
			if (referencedFunctionAddr == null || referencedFunctionAddr == Address.NO_ADDRESS) {
				try {
					if (resolveComputableFlow(program, entry, monitor)) {
						referencedFunctionAddr =
							getThunkedAddr(program, entry, checkForSideEffects);
					}
				}
				catch (CancelledException e) {
					// TODO: Is this the right thing to do on a canceled exception?
					return null;
				}
			}

			// if still no thunkAddr, grab the first basic block and the call/jump at the end of the block
			if (referencedFunctionAddr == null || referencedFunctionAddr == Address.NO_ADDRESS) {
				referencedFunctionAddr = getFirstBlockJumpCall(program, monitor);
			}
		}
		else if (referencedFunctionAddr != null) {
			// Ignore low-bit for certain languages (e.g., Thumb)
			referencedFunctionAddr =
				PseudoDisassembler.getNormalizedDisassemblyAddress(program, referencedFunctionAddr);
		}

		if (referencedFunctionAddr == null) {
			setStatusMsg(
				"Failed to create thunk at " + entry + ": unable to find thunked function");
			return null;
		}

		Function f = listing.getFunctionAt(referencedFunctionAddr);
		if (f == null) {
			// If referencedFunctionAddr contained within EXTERNAL block attempt to 
			// create a thunk function for it
			if (program.getMemory().isExternalBlockAddress(referencedFunctionAddr)) {
				CreateThunkFunctionCmd extThunkCmd =
					new CreateThunkFunctionCmd(referencedFunctionAddr, false);
				if (extThunkCmd.applyTo(program)) {
					f = extThunkCmd.getThunkFunction();
				}
			}
		}
		if (f != null) {
			// no circular thunking allowed
			if (f.getEntryPoint().equals(entry)) {
				setStatusMsg("Invalid referenced function: circular reference");
				return null;
			}
			return f;
		}

		if (referencedFunctionAddr.isExternalAddress()) {
			Symbol s = program.getSymbolTable().getPrimarySymbol(referencedFunctionAddr);
			if (s != null) {
				ExternalLocation extLoc = (ExternalLocation) s.getObject();
				Msg.trace(this,
					"Converting external location to function as a result of thunk at: " + entry);
				return extLoc.createFunction();
			}
			return null;
		}

		if (!referencedFunctionAddr.isMemoryAddress()) {
			setStatusMsg("Referenced address/symbol is not a valid memory location");
			return null;
		}

		if (!program.getMemory().contains(referencedFunctionAddr)) {
			return getExternalFunction(program);
		}

		f = listing.getFunctionContaining(referencedFunctionAddr);
		if (f != null || listing.getInstructionAt(referencedFunctionAddr) == null) {
			setStatusMsg("Invalid referenced function entry address");
			return null;
		}

		if (referringThunkAddresses.contains(referencedFunctionAddr)) {
			setStatusMsg("Invalid referenced function: circular reference");
			return null;
		}

		CreateFunctionCmd funcCmd =
			new CreateFunctionCmd(referencedFunctionAddr, referringThunkAddresses);
		if (funcCmd.applyTo(program)) {
			return funcCmd.getFunction();
		}
		setStatusMsg("Failed to create thunk at " + entry +
			": unable to create thunked-function at " + referencedFunctionAddr);
		return null;
	}
Get the thunkee function the thunking function refers to.

@param autoThunkOK if true, discover the thunkee function
@param program program
@param monitor for canceling
@return referenced function
Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java