问题
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?
来源:https://stackoverflow.com/questions/34548856/gcc-linking-libc-static