我一直在尝试使用elfutils包的libelf通过程序获取节的地址。我节目的摘录是:
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
printf("Error: file %s cannot be opened\n", argv[1]);
goto out_ret;
}
elf_version(EV_CURRENT);
elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
if (!elf){
printf("Error: elf_begin\n");
goto out_close;
}
if (gelf_getehdr(elf, &ehdr) == NULL) {
printf("Error : gelf_getehdr\n");
goto out_close;
}
if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
printf("Error : elf_getshdrstrndx\n");
goto out_close;
}
while ((scn = elf_nextscn(elf, scn)) != NULL) {
gelf_getshdr(scn, &shdr);
name = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
printf("name = %s\n, addr = 0x%x\n", name, (unsigned)shdr.sh_addr);
}
但是,当我使用/lib64/libc.so.6或其他libpthread.so等调用此程序时,我将其作为输出:
name = .note.gnu.build-id, addr = 0x47a00270
name = .note.ABI-tag, addr = 0x47a00294
name = .gnu.hash, addr = 0x47a002b8
name = .dynsym, addr = 0x47a03cd8
name = .dynstr, addr = 0x47a10b48
name = .gnu.version, addr = 0x47a16338
name = .gnu.version_d, addr = 0x47a17470
name = .gnu.version_r, addr = 0x47a17758
name = .rela.dyn, addr = 0x47a17788
name = .rela.plt, addr = 0x47a1efd0
name = .plt, addr = 0x47a1f0e0
name = .text, addr = 0x47a1f1a0
name = __libc_freeres_fn, addr = 0x47b60960
name = __libc_thread_freeres_fn, addr = 0x47b62000
name = .rodata, addr = 0x47b62300
name = .stapsdt.base, addr = 0x47b7cc10
name = .interp, addr = 0x47b7cc20
name = .eh_frame_hdr, addr = 0x47b7cc3c
name = .eh_frame, addr = 0x47b83478
name = .gcc_except_table, addr = 0x47ba97fc
name = .hash, addr = 0x47ba9bc0
name = .tdata, addr = 0x47dad6f0
name = .tbss, addr = 0x47dad700
name = .init_array, addr = 0x47dad700
当我使用readelf查找这些部分的地址时,我发现地址有所不同:
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .note.gnu.build-i NOTE 0000003b47a00270 00000270
0000000000000024 0000000000000000 A 0 0 4
[ 2] .note.ABI-tag NOTE 0000003b47a00294 00000294
0000000000000020 0000000000000000 A 0 0 4
[ 3] .gnu.hash GNU_HASH 0000003b47a002b8 000002b8
0000000000003a20 0000000000000000 A 4 0 8
[ 4] .dynsym DYNSYM 0000003b47a03cd8 00003cd8
000000000000ce70 0000000000000018 A 5 3 8
[ 5] .dynstr STRTAB 0000003b47a10b48 00010b48
00000000000057ef 0000000000000000 A 0 0 1
[ 6] .gnu.version VERSYM 0000003b47a16338 00016338
0000000000001134 0000000000000002 A 4 0 2
[ 7] .gnu.version_d VERDEF 0000003b47a17470 00017470
00000000000002e4 0000000000000000 A 5 21 8
[ 8] .gnu.version_r VERNEED 0000003b47a17758 00017758
0000000000000030 0000000000000000 A 5 1 8
[ 9] .rela.dyn RELA 0000003b47a17788 00017788
0000000000007848 0000000000000018 A 4 0 8
[10] .rela.plt RELA 0000003b47a1efd0 0001efd0
0000000000000108 0000000000000018 A 4 11 8
[11] .plt PROGBITS 0000003b47a1f0e0 0001f0e0
00000000000000c0 0000000000000010 AX 0 0 16
地址不同。例如,对于例如.note.gnu.build-id,addr = 0x47a00270,如果是我的输出,并且在readelf的情况下具有地址3b47a00270。我使用的是fedora 18,我的m / c是64位。我不明白原因。任何人都可以解释我的原因。提前谢谢。
答案 0 :(得分:2)
当您转换为无符号时,您只打印出一个32位的值,并且您运行的是64位二进制文件 - 请尝试%p
获取地址的格式字符串 - 它应该允许您打印指针正确。
虽然现在我写的是,我并不完全使用通用的elf库,所以如果你需要同时支持32位和64位代码,那么你需要有条件地打印出这些值。精灵代码的位模式,即如果它是32位代码然后打印32位地址,如果是64位代码则打印64位地址。
要仅支持64位代码,并假设您在64位模式下构建代码,则需要更改打印代码:
while ((scn = elf_nextscn(elf, scn)) != NULL) {
gelf_getshdr(scn, &shdr);
name = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
printf("name = %s\n, addr = 0x%p\n", name, (void *)shdr.sh_addr);
}
如果您是以32位程序运行,则需要明确转换为uint64_t
并使用PRIx64
中的inttypes.h
进行打印:
printf("name = %s\n, addr = 0x" PRIx64 "\n", name, (uint64_t)shdr.sh_addr);
我们之间的懒惰会转向
unsigned long long
并将其打印为%llx