<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/67779>67779</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            lld looks for libraries given via relative paths at wrong place (relative to system paths)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          kalibera
      </td>
    </tr>
</table>

<pre>
    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](https://github.com/llvm/llvm-project/files/12763224/test.zip)

@mstorsjo
@tru @rnk @zmodem 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V91u47wRfRr6ZmDBoeW_C18kzrpYINsUTdC0V8FInFhcU6RAUna8T18MJTvOZjeLBO0HBFIskfNzZs4ZCkPQG0u0FJMrMbkeYBsr55dbNLogj4PCqcPyoSILRhcevaYA6AkaDIEURAexIjDabskDBvBkMOodL4hVACHnlG0yEFJmmZDrny5GF9315uZf3-7apnE-ZiikFHIh5IptH5K74GqKuqYAxrktKXhy_sVXH0U4hEg1aBsiGn7lbArjPAoh18lWpe1GyHUbvJDr5_n0cZoPm3JotG2fhxvbnoIrDaaVF7PTo49mksFXGyKhOqUUKtcaBQUd02kbQLPHQ3iTVNl6TzbC3vmtthtQ2lMZnT9kYnQtRpfd9auQsx2BRwvapp06wJ7LVnoXwrB0daMN70ewnX0OFLrn5Bmi6MC3FpyFB22V2wch14i-rKY5oFXQeKfakqB0ihL-b5b1VWtD5-jcNXk2bvmFs3DDMJ-Az_h3Su4JtSEFqk3pk_fOBzB6S1w6ozJjlBhfdi_4n9ZurdtbeNKGIB4a4ocrV9do1Y22lJVNkzluPrhnLJ1n8JKxt5XqO_wAFQYoiCzgDrXBwtCpKubwx7qAtp9o9xUUbTznku5aBlqmWSorA4lRczg78oG7W8g5F-I1nEIuYF_psoIKm4ZsR9PUayVGUn2AfzkTMkg68hUUGeI4YoXxdV7HCjx5V3evU8za2Z46CR2Gu9cfDKBjT6c3hKgBY8SyOra9925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