Linker error (undefined symbol) switching from g++ to clang++ - g++

I'm trying to switch my project from using g++ to clang++, but I am running into undefined reference errors. The command I'm running is the following:
clang++ -v <long list of .o files> -I$(SRC_DIR) -I$(SRC_DIR)/third_party/gemmlowp -I$(SRC_DIR)/third_party/flatbuffers/include -I$(SRC_DIR)/third_party/ruy -Isrc/third_party/kissfft -I$(SOC_SOFTWARE_DIR)/include -I$(SOC_SOFTWARE_DIR)/libc -I$(LIBC_DIR)/include -I$(LITEX_HW_DIR) -I(VEX_SRC_DIR) -ffunction-sections -fdata-sections -fno-common -fomit-frame-pointer -ffreestanding -Wno-error -Wsign-compare -Wdouble-promotion -Wshadow -Wunused-variable -Wno-missing-field-initializers -Wunused-function -Wno-uninitialized -Wno-deprecated-register -Wno-format -Wswitch -Wvla -DTF_LITE_STATIC_MEMORY -DTF_LITE_USE_GLOBAL_CMATH_FUNCTIONS -DTF_LITE_USE_GLOBAL_MIN -DTF_LITE_USE_GLOBAL_MAX -DTF_LITE_DISABLE_X86_NEON -g -O3 -fno-builtin --target=riscv32 --sysroot=/opt/riscv32/riscv32-unknown-elf --gcc-toolchain=/opt/riscv32 -menable-experimental-extensions -fuse-ld=lld -std=c++11 -fstrict-aliasing -fno-rtti -fno-exceptions -fno-threadsafe-statics -fmessage-length=0 -Wall -Wextra -Wstrict-aliasing -Wno-unused-parameter -L$(BUILD_DIR)/ld -L$(SOC_SOFTWARE_DIR)/include/generated -L$(SOC_SOFTWARE_DIR)/libbase -lbase -L$(SOC_SOFTWARE_DIR)/libc -lc -L$(SOC_SOFTWARE_DIR)/libc/newlib -lm -L$(SOC_SOFTWARE_DIR)/libcompiler_rt -lcompiler_rt -nostartfiles -Wl,--gc-sections -Wl,--fatal-warnings -Wl,--no-warn-mismatch -Wl,--script=$(BUILD_DIR)/ld/linker.ld -Wl,--build-id=none -Wl,-Map=software.map -o software.elf
When I run the above with g++ everything compiles and runs as expected, however when I run with clang++ I get:
>>> referenced by base.c:34 (<BUILD_DIR>/src/base.c:34)
>>> src/base.o:(isr)
ld.lld: error: undefined symbol: uart_init
>>> referenced by irq.h:19 (<VEX_SRC_DIR>/irq.h:19)
>>> src/base.o:(init_runtime)
ld.lld: error: undefined symbol: getpid
>>> referenced by signal.c:159 (<LIBC_DIR>/signal/signal.c:159)
>>> libc_signal_signal.c.o:(raise) in archive <SOC_SOFTWARE_DIR>/libc/libc.a
ld.lld: error: undefined symbol: kill
>>> referenced by signal.c:160 (<LIBC_DIR>/signal/signal.c:160)
>>> libc_signal_signal.c.o:(raise) in archive <SOC_SOFTWARE_DIR>/libc/libc.a
ld.lld: error: undefined symbol: stdin
>>> referenced by getchar.c:40 (<LIBC_DIR>/tinystdio/getchar.c:40)
>>> libc_tinystdio_getchar.c.o:(getchar) in archive <SOC_SOFTWARE_DIR>/libc/libc.a
>>> referenced by getchar.c:40 (<LIBC_DIR>/tinystdio/getchar.c:40)
>>> libc_tinystdio_getchar.c.o:(getchar) in archive <SOC_SOFTWARE_DIR>/libc/libc.a
ld.lld: error: undefined symbol: stdout
>>> referenced by printf.c:42 (<LIBC_DIR>/tinystdio/printf.c:42)
>>> libc_tinystdio_printf.c.o:(printf) in archive <SOC_SOFTWARE_DIR>/libc/libc.a
>>> referenced by printf.c:37 (<LIBC_DIR>/tinystdio/printf.c:37)
>>> libc_tinystdio_printf.c.o:(printf) in archive <SOC_SOFTWARE_DIR>/libc/libc.a
>>> referenced by putchar.c:39 (<LIBC_DIR>/tinystdio/putchar.c:39)
>>> libc_tinystdio_putchar.c.o:(putchar) in archive <SOC_SOFTWARE_DIR>/libc/libc.a
>>> referenced 3 more times
ld.lld: error: undefined symbol: __errno
>>> referenced by sys_exit.c
>>> sys_exit.o:(_exit) in archive /opt/riscv32/riscv32-unknown-elf/lib/libgloss.a
ld.lld: error: section .eh_frame load address range overlaps with .data
>>> .eh_frame range is [0x400A29F0, 0x400A31F3]
>>> .data range is [0x400A29F0, 0x400A318F]
clang-15: error: ld command failed with exit code 1 (use -v to see invocation)```
Is there something that needs to be done differently when building libraries and linking with clang++ vs. g++ that I'm missing?

Related

Contrafold-se install error: error: unknown FP unit 'sse'

I am trying to install contrafold-se (link to github here: https://github.com/csfoo/contrafold-se), and I am getting this error when I run 'make' in src:
g++ -O3 -mfpmath=sse -msse -msse2 -msse3 -DEVIDENCE_SR -DEVIDENCE_PARS -DNDEBUG -W -pipe -Wundef -Winline --param large-function-growth=100000 -Wall -c Contrafold.cpp
clang: warning: argument unused during compilation: '-msse' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-msse2' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-msse3' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '--param large-function-growth=100000' [-Wunused-command-line-argument]
error: unknown FP unit 'sse'
error message
I see that contrafold-se installation has been tested on certain versions of g++ but I am using a macbook, and when I run g++ --version, I get:
Apple clang version 13.1.6 (clang-1316.0.21.2.5)
Target: arm64-apple-darwin21.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Thanks for your help!

cmake error when building a C library as BPF

I would like to use a rust library that is a C bindings in Solana.
First Solana is calling the cmake of the h3 library, I am replicating the process here.
I am cloning the repo and creating a build folder
git clone --recurse-submodules https://github.com/nmandery/h3ron.git
mkdir h3ron/build
cd h3ron/build
I export the path to lld and run the cmake command very similar to solana's command (I added -v -fuse-ld=lld ) why is solana missing this one argument? it seems it needs solana's lld and not the system ld.
export PATH="/home/pc/.local/share/solana/install/releases/1.9.4/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/:$PATH"
"cmake" "../h3ron-h3-sys/libh3" \
"-DCMAKE_INSTALL_PREFIX=./h3ron-h3-sys-df478ba73015eab9/out" \
"-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC --target=bpfel-unknown-none -v -fuse-ld=lld" \
"-DCMAKE_C_COMPILER=/home/pc/.local/share/solana/install/releases/1.9.4/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/clang" \
"-DCMAKE_CXX_FLAGS= -ffunction-sections -fdata-sections -fPIC -v" \
"-DCMAKE_CXX_COMPILER=/usr/bin/c++" \
"-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC --target=bpfel-unknown-unknown -v" \
"-DCMAKE_ASM_COMPILER=/home/pc/.local/share/solana/install/releases/1.9.4/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/clang" \
"-DCMAKE_BUILD_TYPE=Release"
The error:
Target: bpfel-unknown-none
Thread model: posix
InstalledDir: /home/pc/.local/share/solana/install/releases/1.9.4/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin
"/usr/bin/gcc" -fuse-ld=lld -rdynamic -o cmTC_26933 CMakeFiles/cmTC_26933.dir/testCCompiler.c.o
ld.lld: error: CMakeFiles/cmTC_26933.dir/testCCompiler.c.o is incompatible with elf64-x86-64
collect2: error: ld returned 1 exit status
Note: The system ld would generate this error /usr/bin/ld: unknown architecture of input file CMakeFiles/cmTC_5c450.dir/testCCompiler.c.o' is incompatible with i386:x86-64 output
any idea?

relocation R_X86_64_PC32 against symbol can not be used when making a shared object when fPIC was already used

I have looked at numerous posts regarding resolving this type of linker error, in most cases, people just forgot to compile with -fPIC, sometimes people had trouble with inline functions, etc. that is not the case here. I am trying to wrap a c++ library for python using Pybind11. In this process, I want to link some static libraries (one of them is newmat11) into a .so file.
I build the newmat11 library using an automake system with -fPIC (here's some output...)
...
g++ -DHAVE_CONFIG_H -I. -g -O2 -MT newmat8.lo -MD -MP -MF .deps/newmat8.Tpo -c newmat8.cpp -fPIC -DPIC -o newmat8.o
...
ar cru libnewmat11.a bandmat.o cholesky.o evalue.o fft.o jacobi.o hholder.o myexcept.o newfft.o newmat1.o newmat2.o newmat3.o newmat4.o newmat5.o newmat6.o newmat7.o newmat8.o newmat9.o newmatex.o newmatrm.o solution.o sort.o submat.o svd.o
Indeed, when I run readelf --relocs libnewmat11.a, I see plenty of relocated symbols. Here's one that's giving me trouble:
$readelf --relocs libnewmat11.a | grep ZTIN6NEWMAT17Sing
000000001d20 013c00000002 R_X86_64_PC32 0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x21280 contains 3 entries:
000000005b0b 013c00000001 R_X86_64_64 0000000000000000 _ZTIN6NEWMAT17Singular + 0
0000000009fc 008d00000002 R_X86_64_PC32 0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x20b38 contains 3 entries:
000000008280 008d00000001 R_X86_64_64 0000000000000000 _ZTIN6NEWMAT17Singular + 0
...
Everything seems ok so far, but when I run python3 setup.py build I get this error:
running build
running build_py
running build_ext
building 'mytest' extension
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I../.. -I../../mhfe -I/usr/include/python3.8 -I/usr/local/include/newmat11/include -c src/my_python_binding.cpp -o build/temp.linux-x86_64-3.8/src/my_python_binding.o -std=c++11 -DPYBIND11_PYTHON_VERSION=3.8
...
x86_64-linux-gnu-g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.8/src/my_python_binding.o -L/usr/lib64python3.8 -o build/lib.linux-x86_64-3.8/mytest.cpython-38-x86_64-linux-gnu.so -fPIC -Wl,--whole-archive /usr/local/lib/newmat11/lib/libnewmat11.a -Wl,--no-whole-archive
/usr/bin/ld: /usr/local/lib/newmat11/lib/libnewmat11.a(newmat8.o): relocation R_X86_64_PC32 against symbol `_ZTIN6NEWMAT17SingularExceptionE' can not be used when making a shared object; recompile with -fPIC
As noted above, I'm building the static lib with -fPIC and I can see the relocation symbols in the .a file. Is there something about R_X86_64_PC32 that makes it incompatible with shared objects? I think I would need to have it produce R_X86_64_PLT32 relocation symbols instead, but I'm really not sure. I can see other R_X86_64_PLT32 relocation symbols in the library, but not for the symbol in question. Also, for the record, I'm not familiar with all of the flags that setuptools is adding to the build, perhaps one of them is giving me trouble?
All help appreciated.
I found a solution.
Although my build was using -fPIC for the dependent library, I noticed that I wasn't using --with-pic in my configure for automake. I added it just to see what the difference would be.
readelf --relocs now shows:
000000001a90 01390000002a R_X86_64_REX_GOTP 0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x20f30 contains 3 entries:
000000005a54 013900000001 R_X86_64_64 0000000000000000 _ZTIN6NEWMAT17Singular + 0
0000000009fc 008c0000002a R_X86_64_REX_GOTP 0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x20830 contains 3 entries:
0000000082b6 008c00000001 R_X86_64_64 0000000000000000 _ZTIN6NEWMAT17Singular + 0
This has R_X86_64_REX_GOTP relocation symbol types. I also found that my linker error has been resolved.
I can't see any difference in the compile flags after adding --with-pic, so at the compiler level, I'm not sure what the difference is. In any case, hopefully this will help somebody with a similar problem.

How to fix /usr/bin/ld: cannot find -lboost_python

I just installed boost.python following the instructions on https://www.boost.org/doc/libs/1_69_0/libs/python/doc/html/building/no_install_quickstart.html
but when I try to run the code
bjam toolset=gcc --verbose-test test
it throws:
...found 23 targets...
...updating 6 targets...
gcc.link.dll extending.so
/usr/bin/ld: cannot find -lboost_python
collect2: error: ld returned 1 exit status
"g++" -o "extending.so" -Wl,-h -Wl,extending.so -shared -Wl,--start-group "extending.o" -Wl,-Bstatic -Wl,-Bdynamic -lboost_python -ldl -lpthread -lutil -Wl,--end-group -fPIC -g
...failed gcc.link.dll extending.so...
...skipped <p.>test_ext for lack of <p.>extending.so...
gcc.link test_embed
/usr/bin/ld: cannot find -lboost_python
collect2: error: ld returned 1 exit status
"g++" -L"/usr/lib" -L"/usr/lib/python2.7/config" -Wl,-rpath -Wl,"/usr/lib" -Wl,-rpath -Wl,"/usr/lib/python2.7/config" -o "test_embed" -Wl,--start-group "embedding.o" -Wl,-Bstatic -Wl,-Bdynamic -lboost_python -ldl -lpthread -lutil -lpython2.7 -Wl,--end-group -fPIC -g
...failed gcc.link test_embed...
...skipped <p.>test_embed.run for lack of <p.>test_embed...
...failed updating 2 targets...
...skipped 4 targets...
Anyone knows how to solve it? I only want boost to use boost.python.
Thanks
It's a linker error. Probably the lib file is under a different name, so have to create a symbolic link manually.
Search and locate the "libboost_pythonXX.so" file in the usr/lib directory
XX will match the python version with which you configured boost while building, From the exception thrown you probably configured it with python2.7, so the file probably will be named as libboost_python27.so
and then create a symbolic link :
sudo ln -s "libboost_pythonXX.so" libboost_python.so
You can use this link for reference.
https://github.com/BVLC/caffe/issues/4843
It helped me to solve the error for me.

nettle-3.0 and gmp-6.0.0 - undefined symbols "gmpz_limbs_write, gmpz_limbs_read..."

I am trying to compile nettle 3.0 with gmp 6.0.0 and I kept getting missing symbols:
Making all in tools
gcc -g -O2 -ggdb3 -Wno-pointer-sign -Wall -W -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes -Wpointer-arith -Wbad-function-cast -Wnested-externs -L.. pkcs1-conv.o misc.o ../getopt.o ../getopt1.o -lhogweed -lnettle -lgmp -o pkcs1-conv
ld: warning: relocation error: R_386_32: file ../getopt.o: symbol optarg: external symbolic relocation against non-allocatable section .debug_info; cannot be processed at runtime: relocation ignored
Undefined first referenced
symbol in file
__gmpz_limbs_write ../libhogweed.so
__gmpz_limbs_finish ../libhogweed.so
__gmpz_limbs_modify ../libhogweed.so
__gmpn_zero ../libhogweed.so
__gmpz_roinit_n ../libhogweed.so
__gmpn_cnd_sub_n ../libhogweed.so
__gmpn_cnd_add_n ../libhogweed.so
__gmpz_limbs_read ../libhogweed.so
ld: fatal: symbol referencing errors
collect2: error: ld returned 1 exit status
*** Error code 1
make: Fatal error: Command failed for target `pkcs1-conv'
Current working directory /root/nettle-3.0/tools
I installed gmp 6.0.0 to /usr/local, then - in the nettle directory -, I ran ./configure --prefix=/usr/local, then make and got the error above.
It looks nettle is finding an older version first. Have you tried?
./configure --prefix=/usr/local --with-include-path=/usr/local/include --with-lib-path=/usr/local/lib
I found the options by using ./configure --help.