What is the difference between GCC and LD links?

I recently created a loadable module and found that both

gcc -fPIC --shared -o foo.so.1 foo.c 

and

 gcc -fPIC --shared -c foo.c ld --shared -o foo.so.2 foo.o 

can achieve the same effect.

I also found that foo.so.1 is larger than foo.so.2 by about 3KB, and

 gcc -### -fPIC --shared -o foo.so.1 foo.c 

showed that GCC added stuff other than foo.c to foo.so.1 (e.g. crtendS.o and crtn.o):

 /usr/lib/gcc/x86_64-linux-gnu/4.7/collect2 "--sysroot=/" --build-id --no-add-needed --eh-frame-hdr -m elf_x86_64 "--hash-style=both" -shared -o foo.so.1 /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.7/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/4.7 -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../.. /tmp/cc3JBdCJ.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.7/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crtn.o 

Since foo.so.1 and foo.so.2 can be loaded via dlopen, I was wondering:

  • What is the difference between the two linking methods?
  • Do crtendS.o and crtn.o provide any values ​​for functions in the generated libraries?
+7
gcc shared-libraries ld
source share
1 answer

In principle, there is no difference. When you "bind gcc", it actually calls ld. If you get a message at the linking stage when "linking by gcc" you will immediately see that it is actually from ld. If you want to pass some ld command-line options to ld, the gcc command-line interface has functions designed specifically for this purpose ( -Xlinker and -Wl options).

As for the additional object files ... they probably contain the global initialization / deionization code of the initialization of the load-time library, implicitly added by the compiler. (Requested by the standard library?) Here you can find information about this: https://gcc.gnu.org/onlinedocs/gccint/Initialization.html

+6
source share

All Articles