我试图从.symtab部分读取符号,但似乎我没有以正确的方式理解这个部分。我已经在网上阅读了规范和搜索了一段时间。
为什么symtabi.st_name对每个条目都读取0?我正在读取的可执行文件包含符号。
Elf64_Half shdr_tab_size = ehdr->e_shentsize * ehdr->e_shnum;
Elf64_Shdr shdr_tab[shdr_tab_size];
rewind(infos.fp);
fseek(infos.fp, ehdr->e_shoff, SEEK_SET);
fread(&shdr_tab, 1, ehdr->e_shentsize * ehdr->e_shnum, infos.fp);
char *shdr_names;
shdr_names = malloc(shdr_tab[ehdr->e_shstrndx].sh_size);
if (shdr_names != NULL) {
fseek(infos.fp, shdr_tab[ehdr->e_shstrndx].sh_offset, SEEK_SET);
fread(shdr_names, 1, shdr_tab[ehdr->e_shstrndx].sh_size, infos.fp);
}
Elf64_Shdr shdr_strtab = get_section_hdr(".strtab", shdr_tab, shdr_names, ehdr->e_shnum);
Elf64_Shdr shdr_symtab = get_section_hdr(".symtab", shdr_tab, shdr_names, ehdr->e_shnum);
if (shdr_symtab.sh_type == SHT_SYMTAB) {
Elf64_Sym *symtab = malloc(shdr_symtab.sh_size);
fseek(infos.fp, shdr_symtab.sh_offset, SEEK_SET);
fread(symtab, 1, shdr_symtab.sh_size, infos.fp);
char *strtab = malloc(shdr_strtab.sh_size);
fseek(infos.fp, shdr_tab[shdr_symtab.sh_link].sh_offset, SEEK_SET);
fread(strtab, 1, shdr_strtab.sh_size, infos.fp);
// 10 is arbitrary, I did not figure out how to get the number of entries yet
for (int i = 0; i < 10; i++)
printf("%s\n", strtab + symtab[i].st_name); // .stname reads 0
}编辑:
对于每个symtab[i],这就是我得到的结果。似乎我在什么地方弄错了..。
我已经使用nm和其他工具进行了验证,以确保符号和名称定义良好。

发布于 2021-03-11 16:21:58
如果st_name为0,则该条目没有手册中所述的名称
st_name
An index into the object file's symbol string table, which holds the character
representations of the symbol names. If the value is nonzero, it represents a string table
index that gives the symbol name. Otherwise, the symbol table entry has no name.在表的开头,某些条目为0是正常的。
如果您确定有带名称的符号,请尝试读取所有条目,并查看是否正确找到它们。条目数为shdr_symtab.sh_size / sizeof(Elf64_Sym)
另外,将shdr_tab[shdr_symtab.sh_link].sh_offset替换为shdr_strtab.sh_offset,如下所示:
char *strtab = malloc(shdr_strtab.sh_size);
fseek(infos.fp, shdr_strtab.sh_offset, SEEK_SET);
fread(strtab, 1, shdr_strtab.sh_size, infos.fp);https://stackoverflow.com/questions/66578471
复制相似问题