When I'm using libbfd(bfd.h), I met some problems.
I wrote a simple file(test.cpp) and compiled it successfully. Then I ran 
'objdump test' and I got all infomation(etc. sections syms) correctly.
I installed binutils on my server, and built a check_test.cpp just like what 
objdump.c do. When I ran './check_test test', I got the section Info correctly 
but I coundn't get other info(even bfd_get_filename (abfd) and abfd->format are 
null).
I have checked my code many times and i cannot find bug, so I guess maybe my 
compiling option were not right.
I use 'g++ -g check_test.cpp /usr/lib64/libbfd.a /usr/lib64/libiberty.a -ldl 
-lz -o check_test' or 'g++ -g check_test.cpp -lbfd -liberty -ldl -lz -o 
check_test', and the problem was same as before.
So I want to know how to compile a cpp using bfd? What else should I know to 
use libbfd?
The attachment is my code.
Yours Sincerely.
#include <cstdio>
#include "config.h"
#include <bfd.h>
#include <linux/elf.h>
#include <malloc.h>

void
list_matching_formats (char **p)
{
        printf ("Matching formats:");
        while (*p)
                printf (" %s", *p++);
        printf("\n");
}

static void
dump_bfd (bfd *abfd) {
        asymbol **sy = NULL;
        long storage;
        int symcount;

        if(abfd->xvec == NULL) {
                printf("bfd hasn't xvec\n");
        }
        else {
                printf ("%s:     file format\n", bfd_get_filename (abfd));
        }
        if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
        {
                printf("bfd hasn't syms\n");
                printf("bfd->flags %x\n", abfd->flags);
                symcount = 0;
                //return;
        }
        storage = bfd_get_symtab_upper_bound (abfd);
        if(storage < 0) {
                printf("failed to read symbol table\n");
        }
        if (storage)
                sy = (asymbol **) malloc (storage);

        symcount = bfd_canonicalize_symtab (abfd, sy);
}

static void
display_object_bfd (bfd *abfd) {
        char **matching;

        if (bfd_check_format_matches (abfd, bfd_object, &matching))
        {
                printf("bfd object\n");
                dump_bfd (abfd);
                return;
        }
        if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
    {
        printf("bfd error file_ambiguously_recognized\n");
                list_matching_formats (matching);
                free (matching);
                return;
    }

        if (bfd_get_error () != bfd_error_file_not_recognized)
    {
        printf("bfd error not file_not_recognized\n");
        return;
    }

        if (bfd_check_format_matches (abfd, bfd_core, &matching))
    {
        printf("bfd core\n");
                dump_bfd (abfd);
                return;
    }

        if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
    {
        printf("bfd error file_ambiguously_recognized\n");
                list_matching_formats (matching);
                free (matching);
    }
}

int main(int argc, char *argv[]) {
  if (argc<2) return -1;
  char *filename = argv[1];
  char **matching;
  bfd_init();

  bfd *mybfd = bfd_openr(filename, NULL);
  if(mybfd == NULL) {
    printf("bfd NULL\n");
  }
  else {
        if (bfd_check_format (mybfd, bfd_archive)) {
                printf("bfd archive\n");
        }
        else {
                printf("bfd not archive\n");
                printf("bfd format: %s\n", mybfd->format);
                display_object_bfd(mybfd);
        }
    // if (!bfd_check_format_matches(mybfd, bfd_object, &matching)) {
    //   printf("bfd_check_format wrong\n");
    // }
    // else {
    //   printf("bfd format %s\n", bfd_get_format(mybfd));
    //   printf("bfd target %s\n", bfd_get_target(mybfd));
    //   int sections_count = bfd_count_sections(mybfd);
    //   printf("bfd section num: %d \n", sections_count);
      // {
      //        asection *psection = bfd_get_section_by_name(ibfd, 
".debug_info"); 
      //        printf("section name=%s; start_address=0x%llx; size=%d\n"  
      //    , psection->name  
      //    , psection->vma  
      //    , psection->size);
      // }
      // int syms_count = bfd_get_symcount(mybfd);
      // printf("bfd symbols num: %d \n", syms_count);
      // bfd_get_symtab_upper_bound(mybfd);
      // printf("bfd symbols storage: %ld \n", storage);
      // if(storage < 0) exit(0);
      // asymbol **syms;  
      // long number_of_symbols;
      // syms =  (asymbol **)malloc(storage);  
      // number_of_symbols =  bfd_canonicalize_symtab (ibfd, syms);
      // printf("Scanning %ld symbols\n", number_of_symbols);
    //}
  }
  printf("last line\n");
  fflush(stdout);
  bfd_close(mybfd);
  return 0;
}

//g++ -g check_toy.cpp /usr/lib64/libbfd.a /usr/lib64/libiberty.a -ldl -lz -o 
check_toy
_______________________________________________
bug-binutils mailing list
bug-binutils@gnu.org
https://lists.gnu.org/mailman/listinfo/bug-binutils

Reply via email to