Conditionals in Linker Scripts

Felix von Leitner
Tue Dec 11 11:57:00 GMT 2018


I have a small libc for Linux, for static linking only, and as I have
been adding features, I am growing more and more dismayed about how much
boilerplate code I need to add to and/or reference from the start code.

Currently, I allow removing features from the libc via #define at
compile time — of the libc. I would much rather do this at the compile
time of the application.

For example, my startup code has support for static PIE, but without, so the startup code has to do some ELF parsing to apply the
relocations. This adds a few KB of code that I would rather not force
upon non-PIE static binaries.

The other example is ctor/dtor and exception handling support.

I have been thinking about how do to this. I have a small wrapper
program around the compiler, so I could theoretically do the linking
step multiple times, but that is very inefficient.

The exception handling problem could be solved with a weak symbol for
_Unwind_RaiseException, but what about the startup code for ctor/dtor
and static PIE? Is it possible to make a linker script that will only
pull in the PIE startup code if the output format is "shared library"?
Is it possible to only pull in the ctor/dtor code if there are ctors or

Thanks for any help you can offer!


More information about the Binutils mailing list