Linking symbols to fixed addresses on Linux

后端 未结 3 360
南笙
南笙 2020-12-10 04:45

How would one go about linking (some) symbols to specific fixed addresses using GNU ld so that the binary could still be executed as normal in Linux (x86)? There will not be

相关标签:
3条回答
  • 2020-12-10 05:31

    The suggestion by litb to use --defsym symbol=address does work, but is a bit cumbersome when you have a few dozen such instances to map. However, --just-symbols=symbolfile does just the trick. It took me a while to find out the syntax of the symbolfile, which is

    symbolname1 = address;
    symbolname2 = address;
    ...
    

    The spaces seem to be required, as otherwise ld reports file format not recognized; treating as linker script.

    0 讨论(0)
  • 2020-12-10 05:32

    I'll give you the hot tip... GNU LD can do this (assuming the system libs don't need the address you want). You just need to build your own linker script instead of using the compiler's autogenerated one. Read the man page for ld. Also, building a linker script for a complex piece of software is no easy task when you involve the GLIBC too.

    0 讨论(0)
  • 2020-12-10 05:45

    Try it with

    --defsym symbol=expression
    

    As with this:

    gcc -Wl,--defsym,foobar=0x76543210 file.c
    

    And make foobar in your code an extern declaration:

    extern struct FooBar foobar;
    

    This looks promising. However, it's a bad idea to do such a thing (unless you really know what you do). Why do you need it?

    0 讨论(0)
提交回复
热议问题