This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C Start-Up shared vs static


On Fri, 4 Dec 2009, Mike Frysinger wrote:

> 
> so see what the linker is doing.  it should parse the linker script and load 
> all of the libraries you need so you dont need to

Very good advice, indeed.  I tried the following

$ arm-none-linux-gcc -Wl,--verbose -v -o hello hello.o

and the interesting part of the output seems to be this

Using built-in specs.
Target: arm-none-linux
Configured with: ../gcc-4.3/configure --target=arm-none-linux --prefix=/Volumes/cross/usr --with-sysroot=/Volumes/cross/arm-none-linux --with-build-sysroot=/Volumes/cross/arm-none-linux --with-libs=/Volumes/cross/arm-none-linux/usr/lib --disable-libssp --disable-libgomp --disable-libmudflap --enable-languages=c --enable-symvers=gnu --with-gmp=/Volumes/cross/usr --with-mpfr=/Volumes/cross/usr
Thread model: posix
gcc version 4.3.3 (GCC) 
COMPILER_PATH=/Volumes/cross/usr/libexec/gcc/arm-none-linux/4.3.3/:/Volumes/cross/usr/libexec/gcc/arm-none-linux/4.3.3/:/Volumes/cross/usr/libexec/gcc/arm-none-linux/:/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/:/Volumes/cross/usr/lib/gcc/arm-none-linux/:/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/bin/
LIBRARY_PATH=/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/:/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/:/Volumes/cross/arm-none-linux/lib/:/Volumes/cross/arm-none-linux/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'hello'
 /Volumes/cross/usr/libexec/gcc/arm-none-linux/4.3.3/collect2 --sysroot=/Volumes/cross/arm-none-linux --eh-frame-hdr -dynamic-linker /lib/ld-linux.so.2 -X -m armelf_linux -p -o hello /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/crt1.o /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/crti.o /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/crtbegin.o -L/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3 -L/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib -L/Volumes/cross/arm-none-linux/lib -L/Volumes/cross/arm-none-linux/usr/lib --verbose hello.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/crtend.o /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/crtn.o
GNU ld (GNU Binutils) 2.19.51.20090205
  Supported emulations:
   armelf_linux
   armelf
   armelfb
   armelfb_linux
using internal linker script:
==================================================
/* Script for -z combreloc: combine and sort reloc sections */

[ ... snip out internal linker script ... ]

==================================================
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/crt1.o succeeded
/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/crt1.o
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/crti.o succeeded
/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/crti.o
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/crtbegin.o succeeded
/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/crtbegin.o
attempt to open hello.o succeeded
hello.o
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libgcc.so failed
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libgcc.a succeeded
(/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libgcc.a)_arm_addsubdf3.o
(/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libgcc.a)_arm_cmpdf2.o
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libgcc_s.so failed
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libgcc_s.a failed
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/libgcc_s.so succeeded
-lgcc_s (/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/libgcc_s.so)
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libc.so failed
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libc.a failed
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/libc.so succeeded
-lc (/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/libc.so)

[ This is the problem: why is ld using 
$sysroot/usr/arm-none-linux/lib/libc.so here instead 
of $sysroot/usr/lib/libc.so? ]

attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libgcc.so failed
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libgcc.a succeeded
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libgcc_s.so failed
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/libgcc_s.a failed
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/libgcc_s.so succeeded
-lgcc_s (/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/libgcc_s.so)
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/crtend.o succeeded
/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/crtend.o
attempt to open /Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/crtn.o succeeded
/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/crtn.o/Volumes/cross/usr/lib/gcc/arm-none-linux/4.3.3/../../../../arm-none-linux/lib/crt1.o: In function `_start':
init.c:(.text+0x24): undefined reference to `__libc_start_main'
init.c:(.text+0x28): undefined reference to `abort'
init.c:(.text+0x2c): undefined reference to `__libc_csu_fini'
init.c:(.text+0x34): undefined reference to `__libc_csu_init'
hello.o: In function `main':
hello.c:(.text+0x2c): undefined reference to `printf'
collect2: ld returned 1 exit status

Then I remembered what I had read in glibc-2.8/EGLIBC.cross-building, 
namely this

===

Finally, 'libgcc_s.so' requires a 'libc.so' to link against.  However,
since we will never actually execute its code, it doesn't matter what
it contains.  So, treating '/dev/null' as a C source file, we produce
a dummy 'libc.so' in one step:

    $ $tools/bin/$target-gcc -nostdlib -nostartfiles -shared -x c 
/dev/null \
    >                        -o $sysroot/usr/lib/libc.so

===

I believe the gcc build process must have copied that empty libc.so into 
$sysroot/usr/arm-none-linux/lib/libc.so where it remained to be linked 
against after the real libc.so was installed in $sysroot/lib and 
$sysroot/usr/lib.  We can see that this library is empty:

$ pwd
/Volumes/cross/usr/arm-none-linux/lib
$ arm-none-linux-objdump -j .text -s libc.so

libc.so:     file format elf32-littlearm

$ 

The solution, then, is to remove it thereby forcing ld to link against the 
real libc.so in $sysroot/usr/lib (which itself is actually fake, since 
it's really a linker script).

I'm posting this for the archives in case somebody else gets bitten by the 
same problem.

Thanks everyone for your time and advice.

-- 
Charles M. Coldwell, W1CMC
"Turn on, log in, tune out"
Winchester, Massachusetts, New England (FN42kk)

GPG ID:  852E052F
GPG FPR: 77E5 2B51 4907 F08A 7E92  DE80 AFA9 9A8F 852E 052F


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]