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.
Yes, you can do this. Should work just fine:
dlopen
dlsym
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.