ColinIanKing / stress-ng

This is the stress-ng upstream project git repository. stress-ng will stress test a computer system in various selectable ways. It was designed to exercise various physical subsystems of a computer as well as the various operating system kernel interfaces.
https://github.com/ColinIanKing/stress-ng
GNU General Public License v2.0
1.82k stars 290 forks source link

ARM64 Cross Compile Linker Error - implicit pthreads dependency in librt #358

Closed thallett-solidigm closed 9 months ago

thallett-solidigm commented 9 months ago

When Cross Compiling a Static Executable for ARM64, a linker error occured b/c of an implicit librt dependency on libpthread.

$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"

$ uname -r
5.4.0-170-generic

$ git lg -1
* 9656f6edc (HEAD -> master, origin/master, origin/HEAD) core-thrash: no need to snprintf a fixed path name, clean up code - Colin Ian King, 20 hours ago

$ STATIC=1 CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu--g++ make -j $(nproc)
...
LD stress-ng
/usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../aarch64-linux-gnu/bin/ld: /usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../aarch64-linux-gnu/lib/../lib/librt.a(shm_open.o): in function `shm_open':
(.text+0x40): undefined reference to `__shm_directory'
/usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../aarch64-linux-gnu/bin/ld: /usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../aarch64-linux-gnu/lib/../lib/librt.a(shm_unlink.o): in function `shm_unlink':
(.text+0x34): undefined reference to `__shm_directory'
collect2: error: ld returned 1 exit status
make: *** [Makefile:659: stress-ng] Error 1
...

Similar bug: https://github.com/pytorch/pytorch/issues/8110

Hack to workaround

diff --git a/Makefile b/Makefile
index 1ea5f52ad..06c76e9a6 100644
--- a/Makefile
+++ b/Makefile
@@ -656,7 +656,7 @@ stress-ng: config.h $(OBJS)
        $(PRE_Q)echo "LD $@"
        $(eval LINK_TOOL := $(shell if [ -n "$(shell grep '^#define HAVE_EIGEN' config.h)" ]; then echo $(CXX); else echo $(CC); fi))
        $(eval LDFLAGS_EXTRA := $(shell grep CONFIG_LDFLAGS config | sed 's/CONFIG_LDFLAGS +=//' | tr '\n' ' '))
-       $(PRE_V)$(LINK_TOOL) $(OBJS) -lm $(LDFLAGS) $(LDFLAGS_EXTRA) -o $@
+       $(PRE_V)$(LINK_TOOL) $(OBJS) -lm $(LDFLAGS) -pthread $(LDFLAGS_EXTRA) -o $@

 stress-eigen-ops.o: config.h
        @if grep -q '^#define HAVE_EIGEN' config.h; then \
ColinIanKing commented 9 months ago

The link ordering has now been fixed to resolve this issue. Thank you for the clear bug report, I was able to reproduce the issue on Ubuntu focal with the ARM64 tool chain and fix this.

thallett-solidigm commented 9 months ago

Thank you for the quick fix!