[英]getting virtual memory address(vma) of linker symbols
I'm playing around the bfd library (<bfd.h>), and I was able to implement my own version of objdump -h
on binary files by printing out sections, their vmas, size, etc. Now, I'm having trouble implementing nm
.我正在研究 bfd 库 (<bfd.h>),我能够通过打印部分、它们的 vmas、大小等在二进制文件上实现我自己的
objdump -h
版本。现在,我有实施nm
时遇到麻烦。 I'm able to use the bfd
library to obtain all the different symbols of a binary executable file, but how can I get each symbol's (main, etc) vma using asection/asymbol struct data?我可以使用
bfd
库获取二进制可执行文件的所有不同符号,但是如何使用 asection/asymbol 结构数据获取每个符号(主等)的 vma? Here's the code I have that prints out every symbol name:这是我打印出每个符号名称的代码:
#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;
}
nm uses the function bfd_symbol_info to fetch the virtual memory addresses of the symbols. nm 使用 function bfd_symbol_info 来获取符号的虚拟 memory 地址。 You can read the source code for that function to get an idea as to the implementation.
您可以阅读该 function 的源代码以了解实现。
void
bfd_symbol_info (symbol, ret)
asymbol *symbol;
symbol_info *ret;
{
ret->type = bfd_decode_symclass (symbol);
if (bfd_is_undefined_symclass (ret->type))
ret->value = 0;
else
ret->value = symbol->value + symbol->section->vma;
ret->name = symbol->name;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.