2

I'm trying to track down an issues that a handful of users are reporting. I cannot reproduce it at the moment, but I suspect the issue is related to the use of PIC and inline assembly.

PIC uses the Global Offset Table (GOT), and the inline assembly must preserve EBX and RBX according to the ABI. I've audited the code, and it appears to preserve EBX and RBX as required. But that does not mean the generated code is consistent with expectations because GCC will interleave instructions as it sees fit. All GCC guarantees is consecutiveness (i.e., my ASM will not be reordered).

I want to instrument debug builds with code similar to the following:

volatile void* got1 = GlobalOffsetTablePointer();

// Call a routine that uses inline assembly

volatile void* got2 = GlobalOffsetTablePointer();

assert(got1 = got2);

The problem I am experiencing is I cannot locate the function GlobalOffsetTablePointer. I already have a suspicion for bad interactions with inline assembly, so I am trying to avoid more inline assembly to fetch the GOT pointer.

Is the Global Offset Table (GOT) pointer available from C/C++? If so, how do I access it?

jww
  • 97,681
  • 90
  • 411
  • 885
  • 2
    Can you share your asm? Perhaps it's something obvious. You know, like using `=` when you meant `==`. – David Wohlferd Oct 22 '15 at 08:01
  • @David - good point. I'm going to leave that there. You can find the project at [www.cryptopp.com/](https://www.cryptopp.com/) (be sure to enable PIC by modifying `CXXFLAGS` in the `GNUmakefile`). Or use `curl https://www.cryptopp.com/cryptopp563rc5.zip -o cryptopp563rc5.zip`. 5.6.3 enables PIC on amd64. – jww Oct 22 '15 at 13:04
  • cryptopp has dozens of asm statements. Can you identify the specific asm statement(s) that are giving you problems? – David Wohlferd Oct 23 '15 at 00:02
  • And if it's using some of those AS1 ASJ macros, can you post the expanded code? Maybe build with `-S`? – David Wohlferd Oct 23 '15 at 00:10
  • @David - *"Crypto++ has dozens of asm statements..."* - tell me about it :) *"Can you identify the specific asm statement(s) that are giving you problems?"* - I'm trying to locate that now :) – jww Oct 23 '15 at 08:15
  • *All GCC guarantees is consecutiveness (i.e., my ASM will not be reordered*. That is not fully guaranteed either. `asm volatile` may help (but I'm not sure even that's guaranteed for Extended asm), but don't do that unless you need to because it can prevent optimizations like loop unrolling. – Peter Cordes Mar 14 '18 at 05:57
  • x86-64 PIC doesn't keep a GOT pointer in `rbx`. RIP-relative addressing is used instead. `rbx` is still call-preserved in the ABI, but it's best to let the compiler worry about saving/restoring if it wasn't already, because you can't safely use the stack from inline asm in x86-64 code: [you'll step on the red-zone](https://stackoverflow.com/questions/34520013/using-base-pointer-register-in-c-inline-asm) and corrupt the compiler's data on the stack (e.g. the caller's saved register state). – Peter Cordes Mar 14 '18 at 06:01

0 Answers0