[lld] [ELF] Orphan placement: remove hasInputSections condition (PR #93761)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 9 12:36:52 PDT 2024


MaskRay wrote:

> > Since output sections with `hasInputSections==false` always have A or WA flags. The "similar section's rank <= orphan's rank" condition almost always kicks in, with a few exceptions: ambiguous cases that we don't particularly care about. These test updates are documented in the description.
> 
> Maybe I'm missing something, but the latest revision of this patch with my example still places synthetic sections in an executable segment. Prioritizing the last section of the same similarity (#94099) does little to address the core problem I was trying to demonstrate: output sections without input sections have no reliable flags, and thus when used as potential anchors for orphan sections, the output can be unexpected; another example can easily be conjured up where such sections occur in the last segment, attracting orphans there. So for me, this patch favors simplification over correctness, and I would stick with the latter, especially since the proposed simplification does not seem critical.

I think the new behavior is expected. Use the example at https://github.com/llvm/llvm-project/pull/93761#issuecomment-2141073582.
Given 

```
PHDRS { text PT_LOAD; rodata PT_LOAD; }
SECTIONS {
  .text1 : { *(.text1) } : text
  #.other : { foo = .; *(.other) }
  .other : { LONG(0); *(.other) }
  .text2 : { *(.text2) }
  .rodata : { *(.rodata) } : rodata
}
```

the section layout after this patch is:
```
Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .text1            PROGBITS        0000000000000000 001000 000001 00  AX  0   0  1
  [ 2] .other            PROGBITS        0000000000000001 001001 000004 00   A  0   0  1
  [ 3] .dynsym           DYNSYM          0000000000000008 001008 000018 18   A  6   1  8
  [ 4] .gnu.hash         GNU_HASH        0000000000000020 001020 00001c 00   A  3   0  8
  [ 5] .hash             HASH            000000000000003c 00103c 000010 04   A  3   0  4
  [ 6] .dynstr           STRTAB          000000000000004c 00104c 000001 00   A  0   0  1
  [ 7] .text2            PROGBITS        000000000000004d 00104d 000001 00  AX  0   0  1
  [ 8] .text             PROGBITS        0000000000000050 001050 000000 00  AX  0   0  4
  [ 9] .rodata           PROGBITS        0000000000000050 001050 000004 00   A  0   0  1
**** after .rodata
  [10] .orphan           PROGBITS        0000000000000054 001054 000004 00   A  0   0  1
  [11] .dynamic          DYNAMIC         0000000000000058 001058 000070 10  WA  6   0  8
  [12] .comment          PROGBITS        0000000000000000 0010c8 000013 01  MS  0   0  1
  [13] .symtab           SYMTAB          0000000000000000 0010e0 000030 18     15   2  8
  [14] .shstrtab         STRTAB          0000000000000000 001110 000078 00      0   0  1
  [15] .strtab           STRTAB          0000000000000000 001188 00000a 00      0   0  1
```

If `.rodata` (A) is removed,

```
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .text1            PROGBITS        0000000000000000 001000 000001 00  AX  0   0  1
  [ 2] .other            PROGBITS        0000000000000001 001001 000004 00   A  0   0  1
***** after .other
  [ 3] .rodata           PROGBITS        0000000000000005 001005 000004 00   A  0   0  1
  [ 4] .orphan           PROGBITS        0000000000000009 001009 000004 00   A  0   0  1
  [ 5] .dynsym           DYNSYM          0000000000000010 001010 000018 18   A  8   1  8
  [ 6] .gnu.hash         GNU_HASH        0000000000000028 001028 00001c 00   A  5   0  8
  [ 7] .hash             HASH            0000000000000044 001044 000010 04   A  5   0  4
  [ 8] .dynstr           STRTAB          0000000000000054 001054 000001 00   A  0   0  1
  [ 9] .text2            PROGBITS        0000000000000055 001055 000001 00  AX  0   0  1
  [10] .text             PROGBITS        0000000000000058 001058 000000 00  AX  0   0  4
  [11] .dynamic          DYNAMIC         0000000000000058 001058 000070 10  WA  8   0  8
  [12] .comment          PROGBITS        0000000000000000 0010c8 000013 01  MS  0   0  1
  [13] .symtab           SYMTAB          0000000000000000 0010e0 000030 18     15   2  8
  [14] .shstrtab         STRTAB          0000000000000000 001110 000078 00      0   0  1
  [15] .strtab           STRTAB          0000000000000000 001188 00000a 00      0   0  1
```



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


More information about the llvm-commits mailing list