[ELF] Don't create empty output section for unreferenced PROVIDEs

LLD removes empty output sections otherwise specified in the linker
script. Prior to this change however, if section descriptions included
ANY kind of symbol assignment, then the consequent output section would
not be removed, even if the assignment was marked with PROVIDE and not
actually triggered (i.e. the symbol was never referenced). This change
modifies the isDiscarable function to ignore such directives when
determining whether a section should be discarded, in keeping with
bfd's behaviour. Symbol assignments that do result in a symbol
definition will continue to result in a kept section (this is not
actually the same as bfd's behaviour, but it is simpler, and probably
makes more sense).

Reviewed By: grimar

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

llvm-svn: 336184
This commit is contained in:
James Henderson
2018-07-03 09:23:25 +00:00
parent 3897ded691
commit b427d4eced
2 changed files with 38 additions and 1 deletions

View File

@@ -834,9 +834,16 @@ static bool isDiscardable(OutputSection &Sec) {
if (Sec.ExpressionsUseSymbols)
return false;
for (BaseCommand *Base : Sec.SectionCommands)
for (BaseCommand *Base : Sec.SectionCommands) {
if (auto Cmd = dyn_cast<SymbolAssignment>(Base))
// Don't create empty output sections just for unreferenced PROVIDE
// symbols.
if (Cmd->Name != "." && !Cmd->Sym)
continue;
if (!isa<InputSectionDescription>(*Base))
return false;
}
return true;
}