2

Suppose I have a table in .rodata which would store procedure offsets, so I can use:

call [table + index]

in order to call the procedure defined in my code. Is it possible to achieve in PIC? How can I find these offsets and create such a table?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Funny
  • 193
  • 8
  • 3
    Store relative offsets and add them to RIP. Related: [Jump table implementation in MASM x64?](https://stackoverflow.com/q/57571027) although my answer there has extra levels of indirection (because I think the question did) so it's weird and over-complicated. And it's assuming that you can have 64-bit absolute addresses in static data, which you normally can with runtime fixups; `.quad target1, target2` etc. should just work with normal toolchains, although it does require runtime relocation. – Peter Cordes Apr 23 '22 at 01:24
  • 1
    See [GCC Jump Table initialization code generating movsxd and add?](https://stackoverflow.com/q/52190313) for how GCC does it in x86-64 PIE executables (position-independent). You can write it yourself with `foo: dd target1 - foo, target2 - foo` and so on, where `foo` is the anchor point you use for a RIP-relative LEA. (Or in 32-bit PIC code, use whatever anchor point you generate with a call/pop.) – Peter Cordes Apr 23 '22 at 01:28

0 Answers0