[llvm-dev] Figuring out return address of a call

Mathias Payer via llvm-dev llvm-dev at lists.llvm.org
Sat Mar 4 10:16:39 PST 2017


Hi folks,

I'm trying to figure out the return address of a function in an LLVM
pass, i.e., the byte address right after the end of the call instruction
(so that I can initialize a global variable with the return address of a
function for a sanity check). Due to some other constraints, I have to
run this pass in somewhere in the midend.

At a high level, I want to find the address after a call instruction (my
main target is x86_64 for now) at runtime, see the two examples below:

100: e8 ff ff ff ff callq func
105: .marker

100: ff d0          callq *%rax
102: .marker

My approach is to find call addresses through a function pass, split the
basic block *after* the call instruction, then generate a BlockAddress
as follows:

if (auto CL = dyn_cast<CallInst>(&*I)) {
  BasicBlock *callblock = (*CL)->getParent();
  BasicBlock *postblock =
    callblock->splitBasicBlock((*CL)->getNextNode());
  BlockAddress *retaddr = BlockAddress::get(postblock);
  ...
}

This works well except that the BlockAddress is slightly off. I run into
the problem that during code generation, my BlockAddress is moved past
the instructions that store arguments. E.g., if the function returns an
argument, %rax is first spilled somewhere and my BlockAddress points to
the end of, e.g., the movq instruction.

Is there a better way to retrieve the address right after the call
instruction (i.e., before the return value is stored)?

Thanks,
Mathias

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170304/e77578fb/attachment.sig>


More information about the llvm-dev mailing list