Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Difference between linking by GCC and LD?

Recently I was creating 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 discovered that foo.so.1 is larger than foo.so.2 by about 3KB, and

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

revealed that GCC added stuffs other than foo.c into 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 both foo.so.1 and foo.so.2 can be loaded via dlopen, I was wondering:

  1. What's the difference between these 2 linking methods?
  2. Do crtendS.o and crtn.o make any difference to functions in created libraries?
like image 236
Wei-Chung Wen Avatar asked Oct 02 '14 00:10

Wei-Chung Wen


1 Answers

There's no difference in principle. When you "link by gcc" it actually calls ld. If you get a message at the linking stage when "linking by gcc" you'll immediately see that it is actually from ld. If you want to pass some ld-specific command-line options to ld, gcc's command-line interface has features intended specifically for that purpose (-Xlinker and -Wl options).

As for the additional objects files... they probably contain global load-time library initialization/de-initialization code implicitly added by the compiler. (Requested by the standard library?) You can find some information about it here: https://gcc.gnu.org/onlinedocs/gccint/Initialization.html

like image 105
AnT Avatar answered Oct 18 '22 19:10

AnT