[all-commits] [llvm/llvm-project] fdd319: [ELF] Make start/stop symbols retain associated di...

Fangrui Song via All-commits all-commits at lists.llvm.org
Tue Jul 2 10:58:45 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: fdd319655359b005889abf40d1d8a54fbd56059e
      https://github.com/llvm/llvm-project/commit/fdd319655359b005889abf40d1d8a54fbd56059e
  Author: Fangrui Song <i at maskray.me>
  Date:   2024-07-02 (Tue, 02 Jul 2024)

  Changed paths:
    M lld/ELF/LinkerScript.cpp
    M lld/ELF/LinkerScript.h
    M lld/ELF/Writer.cpp
    A lld/test/ELF/linkerscript/empty-preinit-array-start-stop.test
    A lld/test/ELF/linkerscript/empty-section-start-stop.test
    R lld/test/ELF/linkerscript/preinit-array-empty.test
    M lld/test/ELF/linkerscript/sections-gc2.s
    M lld/test/ELF/pre_init_fini_array_missing.s

  Log Message:
  -----------
  [ELF] Make start/stop symbols retain associated discardable output sections

An empty output section specified in the `SECTIONS` command (e.g.
`empty : { *(empty) }`) may be discarded. Due to phase ordering, we
might define `__start_empty`/`__stop_empty` symbols with incorrect
section indexes (usually benign, but could go out of bounds and cause
`readelf -s` to print `BAD`).

```
finalizeSections
  addStartStopSymbols     // __start_empty is defined
  // __start_empty is added to .symtab
  sortSections
    adjustOutputSections  // `empty` is discarded
writeSections
  // __start_empty is Defined with an invalid section index
```

Loaders use `st_value` members of the start/stop symbols and expect no
"undefined symbol" linker error, but do not particularly care whether
the symbols are defined or undefined. Let's retain the associated empty
output section so that start/stop symbols will have correct section
indexes.

The approach allows us to remove `LinkerScript::isDiscarded`
(https://reviews.llvm.org/D114179). Also delete the
`findSection(".text")` special case from https://reviews.llvm.org/D46200,
which is unnecessary even before this patch (`elfHeader` would be fine
even with very large executables).

Note: we should be careful not to unnecessarily retain .ARM.exidx, which
would create an empty PT_ARM_EXIDX. ~40 tests would need to be updated.

---

An alternative is to discard the empty output section and keep the
start/stop symbols undefined. This approach needs more code and requires
`LinkerScript::isDiscarded` before we discard empty sections in
``adjustOutputSections`.

Pull Request: https://github.com/llvm/llvm-project/pull/96343



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list