Bug 249049 - devel/binutils: ld.bfd searches amd64 paths when linking i386
Summary: devel/binutils: ld.bfd searches amd64 paths when linking i386
Status: Open
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: amd64 Any
: --- Affects Only Me
Assignee: Baptiste Daroussin
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-09-01 11:16 UTC by Alex Richardson
Modified: 2020-09-01 11:23 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Richardson freebsd_committer 2020-09-01 11:16:21 UTC
I was trying to run the llvm compiler-rt testsuite and noticed that I was getting lots of linker failures like this: 

/usr/local/bin/x86_64-unknown-freebsd12.1-ld.bfd: warning: libelf.so.2, needed by /usr/lib32/libexecinfo.so, not found (try using -rpath or -rpath-link)
/usr/local/bin/x86_64-unknown-freebsd12.1-ld.bfd: /usr/lib32/libexecinfo.so: undefined reference to `gelf_getshdr@R1.0'
/usr/local/bin/x86_64-unknown-freebsd12.1-ld.bfd: /usr/lib32/libexecinfo.so: undefined reference to `gelf_getsym@R1.0'
/usr/local/bin/x86_64-unknown-freebsd12.1-ld.bfd: /usr/lib32/libexecinfo.so: undefined reference to `elf_errmsg@R1.0'
/usr/local/bin/x86_64-unknown-freebsd12.1-ld.bfd: /usr/lib32/libexecinfo.so: undefined reference to `elf_nextscn@R1.0'
/usr/local/bin/x86_64-unknown-freebsd12.1-ld.bfd: /usr/lib32/libexecinfo.so: undefined reference to `elf_end@R1.0'
/usr/local/bin/x86_64-unknown-freebsd12.1-ld.bfd: /usr/lib32/libexecinfo.so: undefined reference to `elf_strptr@R1.0'
/usr/local/bin/x86_64-unknown-freebsd12.1-ld.bfd: /usr/lib32/libexecinfo.so: undefined reference to `elf_begin@R1.0'
/usr/local/bin/x86_64-unknown-freebsd12.1-ld.bfd: /usr/lib32/libexecinfo.so: undefined reference to `elf_errno@R1.0'
/usr/local/bin/x86_64-unknown-freebsd12.1-ld.bfd: /usr/lib32/libexecinfo.so: undefined reference to `elf_version@R1.0'
/usr/local/bin/x86_64-unknown-freebsd12.1-ld.bfd: /usr/lib32/libexecinfo.so: undefined reference to `elf_getdata@R1.0'



Linking with --verbose gives the following interesting output:
attempt to open /usr/lib32/crtn.o succeeded
/usr/lib32/crtn.o
libelf.so.2 needed by /usr/lib32/libexecinfo.so
attempt to open /usr/lib/libelf.so.2 failed
attempt to open /usr/lib/compat/libelf.so.2 failed
attempt to open /usr/local/lib/libelf.so.2 failed
attempt to open /usr/local/lib/compat/pkg/libelf.so.2 failed
attempt to open /usr/local/lib/compat/libelf.so.2 failed
attempt to open /usr/local/lib/gcc9/libelf.so.2 failed
attempt to open /usr/local/lib/perl5/5.28/mach/CORE/libelf.so.2 failed
attempt to open /usr/local/lib/perl5/5.32/mach/CORE/libelf.so.2 failed
attempt to open /usr/local/lib/samba4/libelf.so.2 failed
attempt to open /usr/local/llvm-devel/lib/libelf.so.2 failed
attempt to open /usr/local/llvm10/lib/libelf.so.2 failed
attempt to open /usr/local/llvm80/lib/libelf.so.2 failed
attempt to open /usr/local/llvm90/lib/libelf.so.2 failed
attempt to open /usr/local/i386-unknown-freebsd12.1/lib/libelf.so.2 failed
attempt to open /usr/lib/libelf.so.2 failed
attempt to open /usr/local/lib/libelf.so.2 failed
attempt to open /usr/lib/libelf.so.2 failed
attempt to open /usr/lib/compat/libelf.so.2 failed
attempt to open /usr/local/lib/libelf.so.2 failed
attempt to open /usr/local/lib/compat/pkg/libelf.so.2 failed
attempt to open /usr/local/lib/compat/libelf.so.2 failed
attempt to open /usr/local/lib/gcc9/libelf.so.2 failed
attempt to open /usr/local/lib/perl5/5.28/mach/CORE/libelf.so.2 failed
attempt to open /usr/local/lib/perl5/5.32/mach/CORE/libelf.so.2 failed
attempt to open /usr/local/lib/samba4/libelf.so.2 failed
attempt to open /usr/local/llvm-devel/lib/libelf.so.2 failed
attempt to open /usr/local/llvm10/lib/libelf.so.2 failed
attempt to open /usr/local/llvm80/lib/libelf.so.2 failed
attempt to open /usr/local/llvm90/lib/libelf.so.2 failed
attempt to open /usr/local/i386-unknown-freebsd12.1/lib/libelf.so.2 failed
attempt to open /usr/lib/libelf.so.2 failed
attempt to open /usr/local/lib/libelf.so.2 failed

Running ktrace+kdump showed me that ld.bfd was reading /var/run/ld-elf.so.hints instead of /var/run/ld-elf32.so.hints when resolving DT_NEEDED entries, which explains this rather odd path list that does not include the -L directories.
Adding -lelf works around this linker error but is IMO not the correct fix since it also affects all other implicit dependencies.

BFD should be reading /var/run/ld-elf32.so.hints for i386 targets on amd64 and/or also looking in the SEARCH_DIR() entries.

Example command:
/home/alr48/obj/build/upstream-llvm-project-build/./bin/clang -gline-tables-only -fsanitize=undefined -m32 /exports/users/alr48/sources/upstream-llvm-project/compiler-rt/test/sanitizer_common/TestCases/wcrtomb.c -o /home/alr48/obj/build/upstream-llvm-project-build/projects/compiler-rt/test/sanitizer_common/ubsan-i386-FreeBSD/Output/wcrtomb.c.tmp -v
Comment 1 Alex Richardson freebsd_committer 2020-09-01 11:18:17 UTC
The other option would be to add -rpath-link=/usr/lib32 but again this is a workaround and not a fix.
Comment 2 Li-Wen Hsu freebsd_committer 2020-09-01 11:23:00 UTC
Over to maintainer