1

I want to link libc.a and test.c but don't go well

#test.c  
#include<stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
 char *ptr;
 ptr = malloc(1024);
 free(ptr);
 return 0;
}

GCC linking libc static

gcc test.c mystrcmp.c ./libc.a
ldd ./a.out
linux-gate.so.1 =>  (0xb7767000)
        libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xb7737000)
        libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb758e000)
        /lib/ld-linux.so.2 (0xb7768000)

this method is site(GCC linking libc static and some other library dynamically, revisited?).

mystrcmp.c is defined strcmp function because it does not work without mystrcmp.c.

execute this file

./a.out 
Segmentation fault

abort is at __libc_start_main

(gdb) r

Program received signal SIGSEGV, Segmentation fault.
0x0804d5a7 in __libc_start_main ()
(gdb)disas
Dump of assembler code for function __libc_start_main:
   0x0804d560 <+0>:     push   %ebp
   0x0804d561 <+1>:     mov    $0x0,%eax
   0x0804d566 <+6>:     push   %edi
   0x0804d567 <+7>:     push   %esi
   0x0804d568 <+8>:     push   %ebx
   0x0804d569 <+9>:     sub    $0x4c,%esp
   0x0804d56c <+12>:    test   %eax,%eax
   0x0804d56e <+14>:    mov    0x6c(%esp),%ebp
   0x0804d572 <+18>:    mov    0x70(%esp),%edi
   0x0804d576 <+22>:    mov    0x74(%esp),%esi
   0x0804d57a <+26>:    je     0x804d6fa <__libc_start_main+410>
   0x0804d580 <+32>:    mov    0x0,%ecx
   0x0804d586 <+38>:    xor    %eax,%eax
   0x0804d588 <+40>:    test   %ecx,%ecx
   0x0804d58a <+42>:    sete   %al
   0x0804d58d <+45>:    mov    0x64(%esp),%edx
   0x0804d591 <+49>:    mov    0x68(%esp),%ecx
   0x0804d595 <+53>:    mov    %eax,0x80ee5c4
   0x0804d59a <+58>:    lea    0x4(%ecx,%edx,4),%eax
   0x0804d59e <+62>:    mov    0x78(%esp),%edx
   0x0804d5a2 <+66>:    mov    %eax,0x80ef5a8
=> 0x0804d5a7 <+71>:    mov    %edx,0x80eded4

aborting locate's mappings.

(gdb) shell pmap 15147
15147:   /home/watanabe/dlmalloc/test
08048000    660K r-x--  /home/a.out
080ed000      4K r----  /home/a.out
080ee000      4K rw---  /home/a.out
080ef000      8K rw---    [ anon ]
b7e02000      8K rw---    [ anon ]
b7e04000   1676K r-x--  /lib/i386-linux-gnu/libc-2.15.so
b7fa7000      8K r----  /lib/i386-linux-gnu/libc-2.15.so
b7fa9000      4K rw---  /lib/i386-linux-gnu/libc-2.15.so

0x80eded4 is read only so program is abort;

when gcc -static test.c, a.out mappings.

_libc_start_main
   0x08048f7e <+62>:    mov    0x78(%esp),%edx
   0x08048f82 <+66>:    mov    %eax,0x80ef5a8
   0x08048f87 <+71>:    mov    %edx,0x80edfc0

(gdb) shell pmap 15199
15199:   /home/watanabe/dlmalloc/a.out
08048000    660K r-x--  /home/a.out
080ed000      8K rw---  /home/a.out
080ef000    144K rw---    [ anon ]
b7fff000      4K r-x--    [ anon ]

0x80edfc0 is read and write, so go well

How it can go well if you do?

Community
  • 1
  • 1
  • 4
    You are not linking statically. Try `gcc -nostdlib -static test.c mystrcmp.c ./libc.a` instead. Please note that this includes all of `libc.a` instead of just the parts you need. – fuz Dec 31 '15 at 16:11
  • 3
    @FUZxxl: When you link with a static library (`.a`), only the object files from that library that the program needs are included in the executable (which may be more than you expected, but isn't automatically everything). By contrast, when you use a shared library, all the parts of the shared library are automatically available (but not linked into the executable, of course), but the program will still only use the parts of the shared library that your code calls, directly or indirectly. – Jonathan Leffler Jan 01 '16 at 04:23
  • @JonathanLeffler he is linking against libc.a directly instead of through a -l operand. The difference is that --as-needed will not be set for that operand. – fuz Jan 01 '16 at 10:03
  • 1
    That matters not the slightest. With a static archive, the linker only ever links the object files that are needed, which is seldom all the object files in the library. I think `--as-needed` only affects shared libraries anyway. – Jonathan Leffler Jan 01 '16 at 10:05
  • this line: `#test.c` will cause the compiler to raise a error because 'test' is not a valid preprocessor directive. Suggest using `// test.c` 2) the compiler will raise warnings about the unused parameters: `argc` and `argv`. Suggest using the `int main( void )` signature for the main() function. – user3629249 Jan 01 '16 at 14:59
  • Which `./libc.a` are you using? – Armali Feb 20 '18 at 12:16

0 Answers0