This is an alternative to D38321. The motivation, as described in that patch, is the same:
The size of an OutputSection is calculated early, to aid handling of compressed debug sections. However, subsequent to this point, unused synthetic sections are removed. In the event that an OutputSection, from which such an InputSection is removed, is still required (e.g. because it has a symbol assignment), and no longer has any InputSections, dot assignments, or BYTE()-family directives, the size member is never updated when processing the commands. If the removed InputSection had a non-zero size (such as a .got.plt section), the section ends up with the wrong size in the output.
In this version, I have stopped estimating the value of the OutputSection Size and InputSection OutSecOff members early. There were a couple of issues I had to resolve, as part of this change:
- Compressed debug sections still need the Size set early, because they are not processed in assignAddresses. The original behaviour of setting the size early is maintained for such sections.
- The order of input sections is important for SHF_LINK_ORDER sections. This was previously relying on OutSecOff being an approximation for determining the final order. By making OutSecOff and OutputSection Size clearly not be the offset and size respectively, and instead making them represent indexes and section count, it should be much harder to misuse them.