Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Errors using bfd.h on Linux

Tags:

linux

gcc

I am new to Linux programming and am attempting to use the BFD library. This is the current code I'm trying to compile:

#include <bfd.h>
#include <stdlib.h>

bfd *abfd;

int main() {
  bfd_init();
  abfd = bfd_openr("/home/mike/Desktop/testFunc/testProg", NULL);
  return 0;
}

I am using the following command line for compiling:

gcc readInfo.c /usr/lib/libbfd.a -o readInfo

And am getting the following errors:

gcc readInfo.c /usr/lib/libbfd.a -o readInfo /usr/lib/libbfd.a(elflink.o): In function elf_link_add_object_symbols': /build/buildd/binutils-2.21.53.20110810/builddir-single/bfd/../../bfd/elflink.c:4605: undefined reference toobjalloc_free_block' /build/buildd/binutils-2.21.53.20110810/builddir-single/bfd/../../bfd/elflink.c:4892: undefined reference to _sch_istable' /usr/lib/libbfd.a(elflink.o): In functionbfd_elf_size_dynamic_sections': /build/buildd/binutils-2.21.53.20110810/builddir-single/bfd/../../bfd/elflink.c:6048: undefined reference to lbasename' undefined reference to_sch_istable' collect2: ld returned 1 exit status make: * [all] Error 1

There are a lot more lines of errors, which you can view here. I'm sure there is a simple explanation for this but it has got me stumped for a while.

To summarise what I have done so far:

  • Installed clean build of Ubuntu
  • Installed binutils-dev package
like image 948
Mike Kwan Avatar asked Nov 04 '11 00:11

Mike Kwan


3 Answers

if you use ubuntu install binutils-dev

sudo apt-get install binutils-dev
like image 92
bitfree Avatar answered Nov 05 '22 19:11

bitfree


Do you need to statically link your program?

It compiles and runs without error if you dynamically link it instead:

gcc readInfo.c -o readInfo -lbfd

I've run into a new problem when trying to make it statically linked:

$ gcc readInfo.c /usr/lib/libbfd.a /usr/lib/x86_64-linux-gnu/libc.a -o readInfo
/usr/bin/ld.bfd.real: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality
    in `/usr/lib/x86_64-linux-gnu/libc.a(strcmp.o)' can not be used when making
    an executable; recompile with -fPIE and relink with -pie
collect2: ld returned 1 exit status
$ gcc -fPIE readInfo.c /usr/lib/libbfd.a /usr/lib/x86_64-linux-gnu/libc.a \
    -o readInfo
/usr/bin/ld.bfd.real: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality
    in `/usr/lib/x86_64-linux-gnu/libc.a(strcmp.o)' can not be used when making
    an executable; recompile with -fPIE and relink with -pie
collect2: ld returned 1 exit status
$ gcc -fPIE -pie readInfo.c /usr/lib/libbfd.a /usr/lib/x86_64-linux-gnu/libc.a \
  -o readInfo
/usr/bin/ld.bfd.real: /usr/lib/libbfd.a(opncls.o): relocation R_X86_64_32S
    against `.rodata' can not be used when making a shared object; recompile with
    -fPIC
/usr/lib/libbfd.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
$ gcc -fPIC -fPIE -pie readInfo.c /usr/lib/libbfd.a \
  /usr/lib/x86_64-linux-gnu/libc.a -o readInfo
/usr/bin/ld.bfd.real: /usr/lib/libbfd.a(opncls.o): relocation R_X86_64_32S
    against `.rodata' can not be used when making a shared object; recompile with
    -fPIC
/usr/lib/libbfd.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
like image 3
sarnold Avatar answered Nov 05 '22 18:11

sarnold


gcc -v main.c -o blah /usr/lib64/libbfd.a /usr/lib64/libiberty.a -ldl -lz

Looks like libbfd requires features from libiberty, dl and z - this on opensuse 13.1 x86_64 today with similar trivial test app.

like image 2
Amit Chaudhuri Avatar answered Nov 05 '22 19:11

Amit Chaudhuri