符号查找错误:./ libbjdata.so:未定义符号:bfd_map_over_sections

时间:2015-09-30 01:31:31

标签: c

当我编译getsections_dl.c文件时,它总是输出此错误,这是否意味着我没有链接库?

我的编译命令是:

gcc getsections_dl.c -L. -ldl -lbfd -o getsections_dl

我的getsections_dl.c:

#include <stdlib.h>
#include <string.h>
#include <bfd.h>
#include <unistd.h>
#include <dlfcn.h>

extern void dump_sections(bfd *abfd);
#define rdtsc(x)    __asm__ __volatile__("rdtsc \n\t" : "=A" (*(x)))



// the following function is a tweaked version of the itoa functions
// that can be found in either objsym.c or objsect.c
char* lltoa(long long int val, int base) {
  if (val == 0x0) {
   static char zero[] = "0";
   return &zero[0];
  }
  static char buf[64] = {0};
  int i = 60;
  for(; val && i ; --i, val /= base)
    buf[i] = "0123456789abcdef"[val % base];
  return &buf[i+1];
}


int main(int argc, char *argv[]) {
  bfd *obj;
  void *handle;
  unsigned long long start, finish;  
  void (*func)(bfd *);

  bfd_init();

  obj = bfd_openr(argv[1], "elf32-i386");
  if (!obj) {
    bfd_perror("open failure\n");
    return 0;
  }

  rdtsc(&start);

  if (!strcmp(argv[2], "RTLD_LAZY"))
    handle = dlopen("./libobjdata.so", RTLD_LAZY);
  else
    handle = dlopen("./libobjdata.so", RTLD_NOW);

  rdtsc(&finish);

  bfd_check_format(obj, bfd_object);


  //compute the cycles
  write(1, "time: ", strlen("time: "));
  int t = (finish-start)/2793;
  write(1, lltoa(t, 10), strlen(lltoa(t, 10)));
  write(1, " cycles/MHz\n\n", strlen(" cycles/MHz\n\n"));



  func = dlsym(handle, "dump_sections");
  // bfd_map_over_sections(obj, func2, NULL);
  dump_sections(obj);
  dlclose(handle);

  return 0;
}

我的库C文件objsect.c(这是做objdump()剂量的事情):

#include <bfd.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

void dump_section(bfd *abfd, asection *section, void *obj)
{
    // Number of octets per target byte
    //unsigned int opb = bfd_octets_per_byte(abfd); 


char* itoa(int val, int base) {
  if (val == 0x0) {
   static char zero[] = "0";
   return &zero[0];
  }
  static char buf[32] = {0};
  int i = 30;
  for(; val && i ; --i, val /= base)
    buf[i] = "0123456789abcdef"[val % base];
  return &buf[i+1];
}



//write(1, "Section:", strlen("Section:"));
write(1, section->name, strlen(section->name));
write(1, " \t", strlen(" \t"));
write(1, itoa(section->vma, 16), strlen(itoa(section->vma, 16)));
write(1, " \t", strlen(" \t"));
write(1, itoa(section->rawsize, 16), strlen(itoa(section->rawsize, 10)));
write(1, " \t", strlen(" \t"));
write(1, itoa(section->size, 16), strlen(itoa(section->size, 10)));
write(1, " \t", strlen(" \t"));
write(1, itoa(section->filepos, 16), strlen(itoa(section->filepos, 16)));
write(1, " \n", strlen(" \n"));
}

void dump_sections(bfd *abfd)
{

    write(1, "Name\t", strlen("Name\t"));

    write(1, "Size\t", strlen("Size\t"));

    write(1, "VMA\tt", strlen("VMA\t"));
    write(1, "LMA\t", strlen("LMA\t"));
    write(1, "File off\t", strlen("File off\t"));
    write(1, "Algn\n", strlen("Algn\n"));

// For each section in abfd, call dump_section_headers()
    bfd_map_over_sections(abfd, dump_section, NULL);
}

0 个答案:

没有答案