[ELF] Do not ICF two sections with different output sections (by SECTIONS commands)
authorFangrui Song <maskray@google.com>
Mon, 2 Sep 2019 10:33:58 +0000 (10:33 +0000)
committerFangrui Song <maskray@google.com>
Mon, 2 Sep 2019 10:33:58 +0000 (10:33 +0000)
commitd8bc6a48eaa9111b1fc232aa678695a57ae25ec6
treefd8b66ad4be55416be86dbf4101b4f33817f8b48
parent252a584cbd018e21b0ab4d9ee89eab31f4c84ee2
[ELF] Do not ICF two sections with different output sections (by SECTIONS commands)

Fixes PR39418. Complements D47241 (the non-linker-script case).

processSectionCommands() assigns input sections to output sections.
ICF is called before it, so .text.foo and .text.bar may be folded even if
their output sections are made different by SECTIONS commands.

```
markLive<ELFT>()
doIcf<ELFT>()                      // During ICF, we don't know the output sections
writeResult()
  combineEhSections<ELFT>()
  script->processSectionCommands() // InputSection -> OutputSection assignment
```

This patch splits processSectionCommands() into processSectionCommands() and
processSymbolAssignments(), and moves processSectionCommands() before ICF:

```
markLive<ELFT>()
combineEhSections<ELFT>()
script->processSectionCommands()
doIcf<ELFT>()                      // should remove folded input sections
writeResult()
  script->processSymbolAssignments()
```

An alternative approach is to unfold a section `sec` in
processSectionCommands() when we find `sec` and `sec->repl` belong to
different output sections. I feel this patch is superior because this
can fold more sections and the decouple of
SectionCommand/SymbolAssignment gives flexibility:

* An ExprValue can't be evaluated before its section is assigned to an
  output section -> we can delete getOutputSectionVA and simplify
  another place where we had to check if the output section is null.
  Moreover, a case in linkerscript/early-assign-symbol.s can be handled
  now.
* processSectionCommands/processSymbolAssignments can be freely moved
  around.

Reviewed By: ruiu

Differential Revision: https://reviews.llvm.org/D66717

llvm-svn: 370635
lld/ELF/Driver.cpp
lld/ELF/ICF.cpp
lld/ELF/LinkerScript.cpp
lld/ELF/LinkerScript.h
lld/ELF/Writer.cpp
lld/ELF/Writer.h
lld/test/ELF/linkerscript/early-assign-symbol.s
lld/test/ELF/linkerscript/icf-output-sections.s [new file with mode: 0644]
lld/test/ELF/linkerscript/subalign.s