[lld] [lld][ELF] Enable link script to support absolute path matching (PR #156353)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 22 04:04:28 PDT 2025


mykouHW wrote:

> I found the description difficult to follow.
> 
> > When input files use absolute paths, the matching results from mc lld do not meet expectations.
> 
> Can you say what your expectations are and why?
> 
> If I've understood your patch correctly, you expect lld to match the file part of `file.o(section)` with an object with a filename of `/path/to/file.o`
> 
> When I look at https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html I can't see anything that says that when a full path is given the linker is expected to extract the filename. I think the recommended pattern in this case is `*file.o(section)`
> 
> Trying to make a similar example with GNU ld, I got an error message: `ld: cannot find foo.o: No such file or directory`
> 
> So we definitely have a discrepancy between GNU ld and lld, but if we think it is important I'd prefer that we fix it to match GNU ld behaviour. I think there could be problems with automatically discarding path information, particularly when there are multiple object files with the same name, but different paths `path1/foo.o` `path2/foo.o`
> 
> As an aside mc lld, doesn't exist as a name, it is just lld.

Hello, I have noticed that when using `*file.o(section)`, if there are other files with the suffix of the object filename, issues may arise. Therefore, I made another modification, which restrict the condition of "extract the filename."

```
# REQUIRES: x86
# RUN: rm -rf %t && mkdir -p %t
# RUN: split-file %s %t && cd %t

# RUN: llvm-mc -filetype=obj -triple=x86_64 foo.s -o foo.o
# RUN: llvm-mc -filetype=obj -triple=x86_64 bar.s -o bar.o
# RUN: llvm-mc -filetype=obj -triple=x86_64 prefoo.s -o prefoo.o
# RUN: llvm-mc -filetype=obj -triple=x86_64 pre_bar.s -o pre_bar.o

## same path, filename is a suffix of another filename
# RUN: ld.lld %t/foo.o %t/bar.o %t/prefoo.o %t/pre_bar.o -T script_star.ld -o suffix_star.o -Map=suffix_star.map
# RUN: ld.lld %t/foo.o %t/bar.o %t/prefoo.o %t/pre_bar.o -T script_nostar.ld -o suffix_nostar.o -Map=suffix_nostar.map


# RUN: FileCheck %s < suffix_star.map
# CHECK: .goo
# CHECK-NOT: prefoo.o:(.text_foo) 
# CHECK-NOT: pre_bar.o:(.text_bar)

# RUN: FileCheck %s < suffix_nostar.map  --check-prefix=NOSTAR
# NOSTAR: .goo

#--- foo.s
    .section .text_foo, "ax", %progbits
    .globl	foo
    .p2align	4
    .type	foo, at function
foo:
    nop

#--- prefoo.s
    .section .text_foo, "ax", %progbits
    .globl	prefoo
    .p2align	4
    .type	foo, at function
foo:
    nop

#--- bar.s
    .section .text_bar, "ax", %progbits
    .globl	bar
    .p2align	4
    .type	bar, at function
bar:      
    nop

#--- pre_bar.s
    .section .text_bar, "ax", %progbits
    .globl	pre_bar
    .p2align	4
    .type	bar, at function
bar:      
    nop

#--- script_star.ld
SECTIONS {
  .text : { *(.text) }
  .goo : {
    *bar.o(.text_bar);
    *foo.o(.text_foo);
  }
}

#--- script_nostar.ld
SECTIONS {
  .text : { *(.text) }
  .goo : {
    bar.o(.text_bar);
    foo.o(.text_foo);
  }
}
```

In `suffix_star.map`, pre_bar.o and prefoo.o would be changed to .goo. 
```
             VMA              LMA     Size Align Out     In      Symbol
               0                0        0     4 .text
               0                0        0     4         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/foo.o:(.text)
               0                0        0     4         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/bar.o:(.text)
               0                0        0     4         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/prefoo.o:(.text)
               0                0        0     4         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/pre_bar.o:(.text)
               0                0       31    16 .goo
               0                0        1    16         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/bar.o:(.text_bar)
               0                0        0     1                 bar
              10               10        1    16         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/pre_bar.o:(.text_bar)
              10               10        0     1                 bar
              20               20        1    16         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/foo.o:(.text_foo)
              20               20        0     1                 foo
              30               30        1    16         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/prefoo.o:(.text_foo)
              30               30        0     1                 foo
               0                0        8     1 .comment
               0                0        8     1         <internal>:(.comment)
               0                0       a8     8 .symtab
               0                0       a8     8         <internal>:(.symtab)
               0                0       2f     1 .shstrtab
               0                0       2f     1         <internal>:(.shstrtab)
               0                0       20     1 .strtab
               0                0       20     1         <internal>:(.strtab)
```

In `suffix_nostar.map`, no .goo section will be generated.
```
             VMA              LMA     Size Align Out     In      Symbol
               0                0        0     4 .text
               0                0        0     4         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/foo.o:(.text)
               0                0        0     4         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/bar.o:(.text)
               0                0        0     4         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/prefoo.o:(.text)
               0                0        0     4         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/pre_bar.o:(.text)
               0                0       11    16 .text_foo
               0                0        1    16         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/foo.o:(.text_foo)
               0                0        0     1                 foo
              10               10        1    16         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/prefoo.o:(.text_foo)
              10               10        0     1                 foo
              20               20       11    16 .text_bar
              20               20        1    16         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/bar.o:(.text_bar)
              20               20        0     1                 bar
              30               30        1    16         /home/koumeiyuan/code/LLVM_fix/llvm-project/build/tools/lld/test/ELF/linkerscript/Output/abs-path-match-sub.s.tmp/pre_bar.o:(.text_bar)
              30               30        0     1                 bar
               0                0        8     1 .comment
               0                0        8     1         <internal>:(.comment)
               0                0       a8     8 .symtab
               0                0       a8     8         <internal>:(.symtab)
               0                0       3e     1 .shstrtab
               0                0       3e     1         <internal>:(.shstrtab)
               0                0       20     1 .strtab
               0                0       20     1         <internal>:(.strtab)
```


https://github.com/llvm/llvm-project/pull/156353


More information about the llvm-commits mailing list