This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Don't create empty output section for unreferenced PROVIDE symbol
ClosedPublic

Authored by jhenderson on Jun 29 2018, 6:49 AM.

Details

Summary

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).

Diff Detail

Repository
rL LLVM

Event Timeline

jhenderson created this revision.Jun 29 2018, 6:49 AM
grimar accepted this revision.Jun 29 2018, 8:12 AM

LGTM with few nits.

test/ELF/linkerscript/provide-empty-section.s
5

I would avoid using additional input file and -defsym and be more explicit
(Just echoing code to llvm-mc is the common way when there is a little amount of code I believe).

# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %tundefined.o
# RUN: echo "foo=42" | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %tdefined.o
# RUN: echo "call foo" | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %treference.o
11

Let's do it a single line. That is what we usually try to do if possible and also it avoids multiple echo calls.

# RUN: echo "SECTIONS { .bar : { PROVIDE(foo = .); } }" > %t.script

This revision is now accepted and ready to land.Jun 29 2018, 8:12 AM
grimar added inline comments.Jun 29 2018, 8:16 AM
ELF/LinkerScript.cpp
841

I also was wondered if we might want to abstract from Provide here and use something like

if (Cmd->Name != "." && !Cmd->Sym)
...

But I am not sure it worth doing.

Rather than rush this in before I go home today, I'll wait until Monday before putting it in, assuming there are no objections by then.

ELF/LinkerScript.cpp
841

My one concern with this change is that it relies on us setting Provide to false when we define a symbol because of it. I'll try with your suggested change on Monday before putting this in, as I think it feels a little safer. Are you happy with it going straight in if I make that change?

test/ELF/linkerscript/provide-empty-section.s
5

Ah, I didn't know about that technique. I'll make that change, no problem.

11

Will do.

grimar added inline comments.Jun 29 2018, 9:26 AM
ELF/LinkerScript.cpp
841

Yep.

This revision was automatically updated to reflect the committed changes.