简体   繁体   中英

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 . 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? 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. You can read the source code for that function to get an idea as to the implementation.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM