Link Error: using gcc 4.5.2 on RHEL 6.0 -- I do not understand - linux

Here is the command and the error.
gcc --shared \
-m64 \
-shared-libgcc \
-Wl,--whole-archive ./release64/*.a
/usr/lib64/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x1d): undefined reference to `__init_array_end'
/usr/bin/ld: /usr/lib64/libc_nonshared.a(elf-init.oS): relocation R_X86_64_PC32 against undefined hidden symbol `__init_array_end' can not be used when making a shared object
/usr/bin/ld: final link failed: Bad value
collect2: ld returned 1 exit status
make: *** [build] Error 1
What does this mean?

By leaving a "hanging" --whole-archive option, you trick GCC into trying to link code compiled without -fPIC (from libc_nonshared.a) into a shared library. Don't do that. Do this instead:
gcc -shared ... -Wl,--whole-archive release64/*.a -Wl,--no-whole-archive

Related

"can not be used when making a shared object" while building glibc

I am compiling glibc 2.19 on Debian 9.8 (x64), but I got the following error after running make. How to fix this?
gcc -B/usr/bin/ -nostdlib -nostartfiles -o /home/cyril/HME/Multcore_version/AHME/glibc-build/iconv/iconv_prog -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both /home/cyril/HME/Multcore_version/AHME/glibc-build/csu/crt1.o /home/cyril/HME/Multcore_version/AHME/glibc-build/csu/crti.o `gcc -B/usr/bin/ --print-file-name=crtbegin.o` /home/cyril/HME/Multcore_version/AHME/glibc-build/iconv/iconv_prog.o /home/cyril/HME/Multcore_version/AHME/glibc-build/iconv/iconv_charmap.o /home/cyril/HME/Multcore_version/AHME/glibc-build/iconv/charmap.o /home/cyril/HME/Multcore_version/AHME/glibc-build/iconv/charmap-dir.o /home/cyril/HME/Multcore_version/AHME/glibc-build/iconv/linereader.o /home/cyril/HME/Multcore_version/AHME/glibc-build/iconv/dummy-repertoire.o /home/cyril/HME/Multcore_version/AHME/glibc-build/iconv/simple-hash.o /home/cyril/HME/Multcore_version/AHME/glibc-build/iconv/xstrdup.o /home/cyril/HME/Multcore_version/AHME/glibc-build/iconv/xmalloc.o -Wl,-dynamic-linker=/lib64/ld-linux-x86-64.so.2 -Wl,-rpath-link=/home/cyril/HME/Multcore_version/AHME/glibc-build:/home/cyril/HME/Multcore_version/AHME/glibc-build/math:/home/cyril/HME/Multcore_version/AHME/glibc-build/elf:/home/cyril/HME/Multcore_version/AHME/glibc-build/dlfcn:/home/cyril/HME/Multcore_version/AHME/glibc-build/nss:/home/cyril/HME/Multcore_version/AHME/glibc-build/nis:/home/cyril/HME/Multcore_version/AHME/glibc-build/rt:/home/cyril/HME/Multcore_version/AHME/glibc-build/resolv:/home/cyril/HME/Multcore_version/AHME/glibc-build/crypt:/home/cyril/HME/Multcore_version/AHME/glibc-build/nptl /home/cyril/HME/Multcore_version/AHME/glibc-build/libc.so.6 /home/cyril/HME/Multcore_version/AHME/glibc-build/libc_nonshared.a -Wl,--as-needed /home/cyril/HME/Multcore_version/AHME/glibc-build/elf/ld.so -Wl,--no-as-needed -lgcc `gcc -B/usr/bin/ --print-file-name=crtend.o` /home/cyril/HME/Multcore_version/AHME/glibc-build/csu/crtn.o
/usr/bin/ld: /home/cyril/HME/Multcore_version/AHME/glibc-build/csu/crt1.o: relocation R_X86_64_32S against symbol `__libc_csu_fini' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/6/crtbegin.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
/usr/bin/ld: final link failed: Nonrepresentable section on output
/usr/bin/ld: /home/cyril/HME/Multcore_version/AHME/glibc-build/csu/crt1.o: relocation R_X86_64_32S against symbol `__libc_csu_fini' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/6/crtbegin.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
collect2: error: ld returned 1 exit status
Your GCC is configured to produce position-independent executables by default. But the Makefile for GLIBC-2.19 is not prepared for such configuration.
Step 1: verify that the PIE part is true:
echo "int main() { return 0; }" | gcc -xc - &&
file ./a.out
It should say "shared library" or "position independent executable".
Step 2: you can fix the GLIBC-2.19 built by adding -no-pie to LDFLAGS.

relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC

I'm trying to run the following makefile for JNI in Eclipse on Ubuntu 14.04
CLASS_PATH = ../bin
vpath %.class $(CLASS_PATH)
all : libSend.so
libSend.so : Send.o
gcc -fPIC -shared -o $# $<
Send.o : Send.c Send.h
gcc -fPIC -I"/usr/lib/jvm/java-8-openjdk-amd64/include" -I"/usr/lib/jvm/java-8-openjdk-amd64/include/linux" -c $< -o $#
Send.h : Send.class
javah -classpath $(CLASS_PATH) $*
clean :
rm Send.h Send.o libSend.so
When I try to build this makefile. I'm getting the follwing error:
make all
javah -classpath ../bin Send
gcc -fPIC -shared -o libSend.so Send.o
/usr/bin/ld: Send.o: relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
Send.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [libSend.so] Error 1
Please help me to resolve this issue.
My code worked when I followed #AndrewHenle's suggestion:
make clean; make all
Thank you #AndrewHenle.

Cross-compiling Snap7 for MIPS with OpenWrt as OS

Currently i'm working on a project to log data out of a Siemens PLC. To achieve this i'm using Snap7. Snap7 (http://snap7.sourceforge.net/) is a communication library.
I've managed to get it work on my linux desktop but when i try to cross-compile the snap7 library i get some errors
The error is generated when linking all of the object files.
g++ -shared -fPIC -o ../bin/mips-openwrt-linux-g++/libsnap7.so #"filelist.txt" -L. -lpthread -lrt -O3
/usr/bin/ld: ../temp/mips-openwrt-linux/sys_snap_msgsock.o: Relocations in generic ELF (EM: 8)
./temp/mips-openwrt-linux/sys_snap_msgsock.o: could not read symbols: File in wrong format
collect2: error: ld returned 1 exit status
make: *** [../bin/mips-openwrt-linux-g++/libsnap7.so] Error 1
If i'm correct i think it's using the wrong linker, it should use the linker of the toolchain located at: openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/
I've also tried to compile the software with mips-openwrt-linux-g++.
Now another error is given:
mips-openwrt-linux-g++ -shared -fPIC -o ../bin/mips-openwrt-linux-g++/libsnap7.so #"filelist.txt" -L. -lpthread -lrt -O3
openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib/gcc/mips-openwrt-linux-uclibc/4.8.3/../../../../mips-openwrt-linux-uclibc/bin/ld: ../temp/mips-openwrt-linux/sys_snap_msgsock.o: relocation R_MIPS_26 against `close' can not be used when making a shared object; recompile with -fPIC
../temp/mips-openwrt-linux/sys_snap_msgsock.o: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [../bin/mips-openwrt-linux-g++/libsnap7.so] Error 1
I hope someone is familiar with this error and can help me out.
Thanks in advance.
edit1:
There are some pre-compiled binaries. In the makefiles i changed the CC to my g++ directory. There is no LD flag so i cannot set my linker over there.
Can you cross compile and run a "hello world" program?
What is the output of file ../temp/mips-openwrt-linux/sys_snap_msgsock.o?

ld skipping incompatible library when g++ compiling with either -m32 or -m64

I'm on 64-bit Ubuntu and I'm trying to build with "libasmlibrary.so" with Eclipse. However, it kept telling me that the library is incompatible, either with "-m32" or "-m64". This is what is shown when I don't add either of the two options.
17:25:54 ** Incremental Build of configuration Debug for project ASM_TEST **
make all
Building target: ASM_TEST
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -L/home/leon/asmlibrary/linux -o "ASM_TEST" ./src/demo_fit.o ./src/video_camera.o ./src/vjfacedetect.o -lopencv_core -lasmlibrary -lopencv_highgui -lopencv_imgproc -lopencv_ml -lopencv_video -lopencv_objdetect
/usr/bin/ld: skipping incompatible /home/leon/asmlibrary/linux/libasmlibrary.so when searching for -lasmlibrary
/usr/bin/ld: cannot find -lasmlibrary
collect2: ld returned 1 exit status
make: * [ASM_TEST] Error 1
17:25:54 Build Finished (took 111ms)

/usr/bin/ld: client: hidden symbol `__dso_handle'

I am trying to link with a shared lib in my C++ program.
command I used: g++ -o client Client.cpp -L. -lprint
Following is the error:
/usr/bin/ld: client: hidden symbol `__dso_handle' in /usr/lib/gcc/i486-linux-gnu/4.4.3/crtbegin.o is referenced by DSO
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: ld returned 1 exit status
How can I resolve this error?
hidden symbol `__dso_handle' in /usr/lib/gcc/i486-linux-gnu/4.4.3/crtbegin.o is referenced by DSO
Presumably libprint.so is that referencing DSO. You can confirm with:
nm ./libprint.so | grep __dso_handle
If that produces a U __dso_handle output, your libprint.so was built incorrectly (most likely you used ld -shared to link it. Don't do that, use the compiler driver, e.g. g++ -shared ... instead).

Resources