【发布时间】:2016-04-27 03:27:34
【问题描述】:
我想得到一个函数的地址。对于本地函数和 glibc 函数,使用函数名称可以在 x86 上获得正确的地址。
但在 ARM 上,本地函数地址是正确的,而 glibc 函数地址是错误的。
这是我的简单程序:
#include <stdio.h>
int sum(int a, int b)
{
return a + b;
}
int main(int argc, char *argv[])
{
char buffer[32] = { '\0' };
sprintf(buffer, "cat /proc/%d/maps", getpid());
printf("sum = %p\n", sum);
printf("fopen = %p\n", fopen);
system(buffer);
return 0;
}
# x-compile it to an ARM executable:
$ arm-linux-gnueabihf-4.9.1-gcc -g -o misc misc.c
# debug on ARM
/home # ./gdb ./misc
GNU gdb (GDB) 7.5.1
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "arm-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/misc...done.
(gdb) b 16
Breakpoint 1 at 0x8534: file misc.c, line 16.
(gdb) r
Starting program: /home/misc
sum = 0x8491
fopen = 0x835c
00008000-00009000 r-xp 00000000 00:13 1703976 /home/misc
00010000-00011000 rw-p 00000000 00:13 1703976 /home/misc
76ed9000-76fd0000 r-xp 00000000 1f:08 217 /lib/libc-2.19-2014.06.so
76fd0000-76fd7000 ---p 000f7000 1f:08 217 /lib/libc-2.19-2014.06.so
76fd7000-76fd9000 r--p 000f6000 1f:08 217 /lib/libc-2.19-2014.06.so
76fd9000-76fda000 rw-p 000f8000 1f:08 217 /lib/libc-2.19-2014.06.so
76fda000-76fdd000 rw-p 00000000 00:00 0
76fdd000-76ff7000 r-xp 00000000 1f:08 199 /lib/ld-2.19-2014.06.so
76ffb000-76ffe000 rw-p 00000000 00:00 0
76ffe000-76fff000 r--p 00019000 1f:08 199 /lib/ld-2.19-2014.06.so
76fff000-77000000 rw-p 0001a000 1f:08 199 /lib/ld-2.19-2014.06.so
7efdf000-7f000000 rw-p 00000000 00:00 0 [stack]
ffff0000-ffff1000 r-xp 00000000 00:00 0 [vectors]
Breakpoint 1, main (argc=1, argv=0x7efffe64) at misc.c:16
16 misc.c: No such file or directory.
(gdb) p fopen
$1 = {<text variable, no debug info>} 0x76f26a50 <fopen>
(gdb)
注意 glibc 文本段映射到地址 76ed9000,那么fopen 怎么可能是像 0x835c 这样的有线地址呢?
但是,下一行(gdb) p fopen,gdb 给出了正确的地址。
【问题讨论】:
-
我怀疑这与动态链接的实现方式有关。
-
地址空间布局随机化(ASLR)?