static + dynamic linking problem

Chris Johns chrisj at
Fri May 12 21:56:01 UTC 2017

On 13/5/17 4:41 am, Mathew Benson wrote:
> I have an application using the RTL dynamic loader.  The loader seems to
> work just fine, but two of the dynamic libraries that I'm loading
> require math functions in libm.a.  Correct me if I'm wrong, but I can't
> link those into the dynamic libraries without symbols colliding. 
> However, if I put the -lm in the main executable, the symbols get
> stripped out even though I use the "-Wl,--export-dynamic" switch.  If I
> use a function in the main executable, that function is exported
> correctly.  But all unused functions get stripped out.  It would appear
> the --export-dynamic only applies to the .o's and not to any .a's.

This is a classic problem when linking with libraries like libm as well
as libc. It becomes more complex if you have other modules you want to
load that also reference the same library.

> I think there are multiple ways to solve this, but I haven't found a
> clean solution yet.  One working, but ugly solution is to use "ar x
> libm.a" to extract the .o's, then link the .o's with mine.  That
> combined with the --export-dynamic exports all the symbols.

Yes there are a number of ways this can be solved. One that is available
now is to use rtems-ld and the RAP format. It will pull into the RAP
file the referenced object files not in the base image. This works for a
single executable. If you create more than one executable that
references the same object file it will be copied into both, the first
executable loaded will load that object file and the second executable
load should not load the same object as the symbols referenced will be

The rtems-ld tool is part of the RTEMS Tools Project and should be
installed when build that package.

The only documentation on rtems-ld is I do
apologize there is nothing more.

Note, the RAP format has not been updated to handle the C++ exception
fixes the ELF format recently got.

> I'm thinking this can probably be solved with a toolchain switch.  Is
> there any way I can tell the linker to also export all the symbols from
> the .a's linked in?  Can I specify specific symbols to export?

I would like rtems-ld to build and maintain an archive of object files
referenced by executables rtems-ld links. I am planning to add support
to the target link editor to search archives on the target and load
required object files. I think this will simplify executable building
and management. I cannot say when this work will be done.


More information about the users mailing list