Like the question says: We are building on Linux using the GNU linker, and on Solaris using the solaris ld. GNU ld supports the --export-dynamic flag, which:
When creating a dynamically linked executable, add all symbols to the dynamic
symbol table. The dynamic symbol table is the set of symbols which are visible
from dynamic objects at run time.
What is the equivalent to this flag using the solaris linker? Is there an equivalent?
The Sun Studio linker (ld), by default, exports all symbols.
You can find the complete reference for the Sun linker on docs.sun.com.
Search for the "Linker and Libraries Guide".
By "all symbols" you mean all global symbols, right? C file-static symbols
are not promoted to global right? I don't think that would work.
Related
In this article, I found this line: The GNU linker uses selective linking, which keeps other unreferenced functions out of the linker’s output image.
I am not sure what this means exactly. But what I think is, if I include stdio.h in my source code, and use only printf from that, then the resulting exe contains only code for printf extracted from stdio.c and other functions defined in that file are discarded.
Is what I say is correct? If not, what does selective linking mean? Also, in the above case, does compiler include entire file, or only the used functions?
The GNU linker uses selective linking, which keeps other unreferenced functions out of the linker’s output image.
That only applies when linking .a files. A .a file is a collection of .o files. That selective linking means it links in only a function rather than the entire .o where that function resides.
if I include stdio.h in my source code, and use only printf from that, then the resulting exe contains only code for printf extracted from stdio.c and other functions defined in that file are discarded.
C standard library functions normally reside in libc.so, unless you explicitly link statically. So, either you link libc.a and that copies printf function into your executable (selective linking), or you link libc.so and no copy of printf is made.
stdio.c is only ever used to build libc.a and libc.so.
As #JohannesSchaub-litb mentions in the comment:
-ffunction-sections is needed when compiling .o files to take advantage of selective linking linker feature:
Together with a linker garbage collection (linker --gc-sections option) these options may lead to smaller statically-linked executables (after stripping).
--gc-sections linker option enables selective linking:
--gc-sections decides which input sections are used by examining symbols and relocations. The section containing the entry symbol and all sections containing symbols undefined on the command-line will be kept, as will sections containing symbols referenced by dynamic objects. Note that when building shared libraries, the linker must assume that any visible symbol is referenced.
I have libA.so, libB.so, and an executable 'foo'. 'foo' needs libB.so which itself needs libA.so. During linking foo explicitly links with libB because it directly uses symbols from it. 'foo' does not directly use symbols from libA. When linking 'foo', ld wants to check it can resolve symbols references from libB in libA but it can't find libA. I can make it find libA by using -Wl,rpath-link=, or I can have the linker ignore libA using -Wl,--allow-shlib-undefined.
The problem is I shouldn't have to set either of these options because libB.so contains an rpath that tells the linker where to find libA.so and the linker uses this rpath at runtime to successfully find libA. So why doesn't it use it at link time? Forcing foo's build configuration to know where libA is seems completely unnecessary in this case?
I shouldn't have to set either of these options because libB.so contains an rpath that tells the linker where to find libA.so and the linker uses this rpath at runtime
You are mixing up the static linker ld and the runtime linker (aka loader) ld.so.
On Linux, these come from binutils and GLIBC respectively. They are completely different programs, maintained by different sets of people.
It would be possible for ld to implement the search path that current version of ld.so uses, but this is
nontrivial amount of code, that would need to be written from scratch and
will break as soon as ld.so search mechanism is changed
Update:
isn't the search of the rpath executed by the dynamic linker 'the standard'
There is no standard that defines this (that I know of).
In addition, on Linux and Solaris the search path that ld.so uses could contain dynamic tokens like $ORIGIN and $PLATFORM, which are unknown at (static) link time.
I am using CMake to build an executable binary for a Renesas processor using GNU toolchain. I changed from object to static libraries and had issues with the interrupt table being correctly linked. Thanks to Stackoverflow I found out about --whole-archive option.
My question is, is there a linker flag or way that shows me the objects that are linked from a library so that I know which objects (so these would be the objects without unresolved symbols) the linker is ignoring?
ld can create a map file that will show which objects are linked and for what reason (i.e. which object requested the symbol to be resolved):
gcc -Wl,-Map -Wl,mapfile ...
I would like to know how to link a pgc++ compiled code (blabla.a) with a main code compiled with c++ or g++ GNU compiler.
For the moment linking with default gnu c++ linker gives errors like:
undefined reference to `__pgio_initu'
As the previous person already pointed out, PGI supports G++ name mangling when using the pgc++ command. Judging from this output, I'm guessing that you're linking with g++ rather than pgc++. I've had the most success when using pgc++ as the linker so that it finds the PGI libraries. If that's not an option, you can link an executable with pgc++ -dryrun to get the full link line and past the -L and -l options from there to get the same libraries.
Different C++ compilers use different name-mangling conventions
to generate the names that they expose to the linker, so the member function
name int A::foo(int) will be emitted to to the linker by compiler A as one string
of goobledegook, and by compiler B as quite a different string of goobledegook,
and the linker has no way of knowing they refer to the same function. Hence
you can't link object files produced by different C++ compilers unless they
employ the same name-mangling convention (and quite possibly not even then: name-mangling
is just one aspect of ABI compatibility.)
That being said, according to this document,
PGC++ supported name-mangling compatibility with g++ 3-and-half years ago, provided that PGI C++ compiler was invoked with precisely the command pgc++ or pgcpp --gnu. It may be that the library you are dealing with was not built in that specific way, or perhaps was built with an older PGI C++ compiler.
Anyhow, if g++ compiles the headers of your blabla.a and emits different
symbols from the ones in blabla.a, you can't link g++ code with blabla.a.
You'd need to rebuild blabla.a with g++, which perhaps is not an option.
I need to include all symbols from a static library. "-force_load" is good when compiling with Xcode. But, for example, when using it under Ubuntu with gcc, "-force_load" is not recognized. I'm looking for alternative options that can be used under other operating systems. Thanks.
The GNU linker's option is called --whole-archive, but while -force_load applies to one library, --whole-archive applies to all libraries after it on the command line. So the usual thing is to do --whole-archive somelib.a --no-whole-archive.
Usually you don't use ld directly but instead invoke it via GCC, in which case you have to tell GCC to pass the options on to the linker: -Wl,--whole-archive,somelib.a,--no-whole-archive