我在bfd库(objdump -h
版本。现在,我在实施nm
时遇到了麻烦。我可以使用bfd
库获取二进制可执行文件的所有不同符号,但是如何使用asection / asymbol结构数据获取每个符号的(主要,等等)vma?这是我打印出每个符号名称的代码:
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <bfd.h>
int main(int argc,char *argv[])
{
bfd *ibfd = NULL;
if (!argv[1])
{
printf("Please supply a second argument\n");
return -1;
}
else
{
// initialize bfd so we can use it
bfd_init();
// open the supplied argument file
const char *str = argv[1];
ibfd = bfd_openr(str,"elf64-x86-64");
// if issue opening
if (!ibfd)
{
bfd_perror("open failure\n");
return -1;
}
// if file isnt elf binary file
if (!bfd_check_format(ibfd,bfd_object))
{
printf("not an object file\n");
return -1;
}
int spaceNeeded = bfd_get_symtab_upper_bound(ibfd);
if (spaceNeeded < 0)
{
return -1;
}
else if (spaceNeeded == 0)
{
return 1;
}
asymbol **symTable = malloc(spaceNeeded);
long numSyms = bfd_canonicalize_symtab(ibfd,symTable);
if (numSyms < 0)
return -1;
for (int i = 0,count = 0; i < numSyms; i++)
{
printf("%s\n",symTable[i]->name);
}
bfd_close(ibfd);
}
// success code
return 1;
}