【发布时间】:2019-09-04 20:51:52
【问题描述】:
以下程序:
#include <stdio.h>
int main(int argc, char *argv[])
{
for (int j = 0; j < argc; j++)
printf("%d: %s\n", j, argv[j]);
return 0;
}
内置于静态链接的 PIE:
gcc -g -fpie main.c -static-pie -o ld.so
工作正常:
$ ./ld.so foo bar
0: ./ld.so
1: foo
2: bar
但是当我将该程序用作另一个程序的ELF解释器时:
$ gcc -g main.c -Wl,-I./ld.so -o a.out
它像这样崩溃:
gdb -q ./a.out
(gdb) run
Starting program: /tmp/a.out
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7da84e2 in __ctype_init () at ctype-info.c:31
31 *bp = (const uint16_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_CLASS) + 128;
(gdb) bt
#0 0x00007ffff7da84e2 in __ctype_init () at ctype-info.c:31
#1 0x00007ffff7d9e3bf in __libc_init_first (argc=argc@entry=1, argv=argv@entry=0x7fffffffd728, envp=0x7fffffffd738) at ../csu/init-first.c:84
#2 0x00007ffff7d575cd in __libc_start_main (main=0x7ffff7d56e29 <main>, argc=1, argv=0x7fffffffd728, init=0x7ffff7d57ce0 <__libc_csu_init>, fini=0x7ffff7d57d70 <__libc_csu_fini>, rtld_fini=0x0,
stack_end=0x7fffffffd718) at ../csu/libc-start.c:244
#3 0x00007ffff7d56d6a in _start () at ../sysdeps/x86_64/start.S:120
这是为什么呢?
以上所有地址都在./ld.so 本身内,因此它在自己的初始化过程中崩溃。事实上,自从ld.so 退出后,控件将永远不会到达a.out。
【问题讨论】:
标签: gcc glibc elf dynamic-linking