Skip to content

lld looks for libraries given via relative paths at wrong place (relative to system paths) #67779

Closed
llvm/llvm-project-release-prs
#719
@kalibera

Description

@kalibera

When libraries are passed to the linker as relative paths (e.g. "../../../../lib/libLLVMSupport.a"), they are sometimes looked for relative to the system installation path (e.g. "/something/usr/x86_64-pc-linux-gnu/lib/clang/17/lib/../../../../lib/libLLVMSupport.a"). Instead, they should be looked up always relative to the current working directory.

I've ran into this when cross-compiling a native LLVM compiler (to run on Windows/aarch64 and produce code for Windows/aarch64), using a cross-compiler running on Linux/x86_64. Linking failed due to errors like "ld.lld: error: unknown file type: CommandLine.cpp.o". The correct "libLLVMSupport.a" library has been available relatively to the current working directory in "../../../../lib/libLLVMSupport.a", but the linker instead used an incompatible version (for Linux/x86_64) which happened to be located in "/something/usr/x86_64-pc-linux-gnu/lib/clang/17/lib/../../../../lib/libLLVMSupport.a". When I deleted that incompatible library from that location, the linking passed as it should.

I'm attaching a narrowed-down example based on this problem. On my system, with that "incompatible" library in place,

cd test/sub/sub/sub/sub/
./test1

does

/home/tomas/toolchain/clang/toolchain_libs/mxe/usr/x86_64-pc-linux-gnu/bin/aarch64-w64-mingw32.static.posix-clang++ \
TableGen.cpp.obj \
../../../../lib/libLLVMSupport.a

and outputs

ld.lld: error: unknown file type: CommandLine.cpp.o
ld.lld: error: unknown file type: ManagedStatic.cpp.o
ld.lld: error: unknown file type: PrettyStackTrace.cpp.o
ld.lld: error: unknown file type: raw_ostream.cpp.o
ld.lld: error: unknown file type: Signals.cpp.o
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

With the "incompatible" library not in place, the output includes only complaints about undefined symbols, as it should, because I've narrowed down the example. The archive includes also outputs from strace, which make it clear that lld opens the "incompatible" library at the wrong place:

openat(AT_FDCWD, "/home/tomas/toolchain/clang/toolchain_libs/mxe/usr/x86_64-pc-linux-gnu/lib/clang/17/lib/../../../../lib/libLLVMSupport.a", O_RDONLY|O_CLOEXEC) = 4

The archive also includes the "incompatible" version of the library one could place in the corresponding place to reproduce the problem.

test.zip

@mstorsjo
@tru @rnk @zmodem

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions