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 to
objalloc_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 function
bfd_elf_size_dynamic_sections': /build/buildd/binutils-2.21.53.20110810/builddir-single/bfd/../../bfd/elflink.c:6048: undefined reference tolbasename' 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:
if you use ubuntu install binutils-dev
sudo apt-get install binutils-dev
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With