Thread: gcc and -nostdlib

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    8

    gcc and -nostdlib

    How can I link files manually to gcc rather than use the ones installed in the OS ? The point is that the only the manually provided files should be used when executing the program: glibc, libgcc, linker, headers, and whatever other files are needed.

    This should be a really easy thing to do ? (I know about -static but I want to used shared libraries rather than using a huge single binary.) -nostdlib is the way to go but I cannot even get close to compiling a simple Hello World program.

    I tried something like this but I get errors: (the .so files are on my Desktop)

    myuser@linux:~/Desktop$ gcc -nostdlib libc-2.11.2.so ld-2.11.2.so libgcc_s.so.1 simple.c
    /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400390
    myuser@linux:~/Desktop$ ./a.out
    Hello World!
    Segmentation fault (core dumped)

    Can anybody provide a working example ?

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    8
    myuser@linux:~/Desktop$ gcc -nostdlib -Wl,-dynamic-linker,/home/myuser/Desktop/ld-linux-x86-64.so.2,-rpath,/home/myuser/Desktop libc.so.6 libgcc_s.so.1 simple.c
    /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400388
    myuser@linux:~/Desktop$ ./a.out
    Hello World!
    Segmentation fault (core dumped)
    myuser@linux:~/Desktop$ ldd a.out
    linux-vdso.so.1 => (0x00007fff4b0f8000)
    libc.so.6 => /home/myuser/Desktop/libc.so.6 (0x00007f6e9d82f000)
    libgcc_s.so.1 => /home/myuser/Desktop/libgcc_s.so.1 (0x00007f6e9d619000)
    /home/myuser/Desktop/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007f6e9db92000)



    In simple.c, I changed the function main to _start and the error disappears.
    It stops segfaulting by calling exit(0) at the end of the _start function.

    So, which one is it using: /home/myuser/Desktop/ld-linux-x86-64.so.2 or /lib64/ld-linux-x86-64.so.2 ?
    Last edited by pulio; 04-24-2011 at 04:57 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Before you start trying to drive the linker manually, study the output of
    gcc -v foo.c
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    8
    Quote Originally Posted by Salem View Post
    Before you start trying to drive the linker manually, study the output of
    gcc -v foo.c
    It contains a reference to: -dynamic-linker /lib64/ld-linux-x86-64.so.2

    Is it not being overrided when I call -dynamic-linker in my post above ?

Popular pages Recent additions subscribe to a feed

Tags for this Thread