Search code examples
clinkershared-librariesembedded-linux

How to get the base address of a loaded .so in memory?


I am working on an embedded Linux program. The scenario is: There're thousands of configuration items which are compiled statically in the final executable, and the address of the items is defined in the linker script, e.g.,

firstConfigItem = .;
*(.SEGM_CONFIG)
lastConfigItem = .;

so that the variables firstConfigItem and lastConfigItem are used directly in source code.

Now the trouble is, even there's an insignificant modification in only one config item, the executable should be re-compiled.

So my idea is, to compile all the config items as an .so file then load it dynamically.

Is that feasible? If yes, how can I initialize firstConfigItem and lastConfigItem? Is there any api in Linux that can get the loaded addr of a specified so?

Or, can anyone recommend a easier way?

Thanks in advance for any help.


Solution

  • Yes, you can do this. Should work just fine:

    1. open the shared object with dlopen
    2. obtain the address of the desired symbol using dlsym
    3. write that address into a variable for later use

    What's important is that you pass the handle of the object to dlsym so you get the symbol from the shared object and not from some other component that might define it.

    You can also produce a dummy configuration object and link against it at build time. At runtime before starting the program, you place the correct configuration object into the rpath, allowing your program to automatically find it. Then you don't need to do anything at runtime, it's just a link with a shared library.