This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Keep local symbols when both --emit-relocs and --discard-all are specified
ClosedPublic

Authored by MaskRay on Apr 17 2020, 11:06 AM.

Details

Summary

This fixes a bug as exposed by D77807.

Add tests for {--emit-relocs,-r} x {--discard-locals,--discard-all}. They add coverage for previously undertested cases:

  • STT_SECTION associated to GCed sections (gc)
  • STT_SECTION associated to retained sections (text)
  • STT_SECTION associated to non-SHF_ALLOC sections (.comment)
  • STB_LOCAL in GCed sections (unused_gc)

Event Timeline

MaskRay created this revision.Apr 17 2020, 11:06 AM
ikudrin added inline comments.Apr 17 2020, 8:39 PM
lld/ELF/Writer.cpp
646–647

For the purpose of this fix, it would be simpler to extend the condition at the beginning of getDiscard() in Driver.cpp and remove this condition. The other changes are not required.

MaskRay marked an inline comment as done.Apr 17 2020, 9:14 PM
MaskRay added inline comments.
lld/ELF/Writer.cpp
646–647

Do you mean

-  if (args.hasArg(OPT_relocatable))
+ if (args.hasArg(OPT_relocatable) || config->emitRelocs)
    return DiscardPolicy::None;

? That would increase the amount of differences for D77807.

ikudrin added inline comments.Apr 18 2020, 2:05 AM
lld/ELF/Writer.cpp
646–647

Yes, that is true, but I believe that every patch should solve its issue in the right way and the change in Driver.cpp looks like that. We emit relocations in two cases, -r and --emit-relocs, and both of them should be handled similarly.

MaskRay marked an inline comment as done.Apr 19 2020, 11:27 PM
MaskRay added inline comments.
lld/ELF/Writer.cpp
646–647

I have a different opinion, though. I prefer the current version despite the -r --emit-relocs similarity.

-  if (config->discard != DiscardPolicy::All)
    copyLocalSymbols();

This removes the logic from the caller and makes the intricate logic local to the body of copyLocalSymbols.

ikudrin added inline comments.Apr 20 2020, 2:20 AM
lld/ELF/Writer.cpp
646–647

My main concern is the unjustified difference in handling the -r and --emit-relocs cases. What about removing the condition in getDiscard() in Driver.cpp and changing config->emitRelocs to config->copyRelocs here?

ikudrin added inline comments.Apr 20 2020, 2:26 AM
lld/test/ELF/emit-relocs-discard-locals.s
3

overrides?

grimar added inline comments.Apr 20 2020, 3:53 AM
lld/ELF/Writer.cpp
646–647

I think I am slightly in favor of keeping it here, because the argument about hiding "the intricate logic" probably makes sense to me.

650

Can this be moved closer to the first config->discard check to group them?

if (config->discard == DiscardPolicy::None)
  return true;

(Seems if (config->discard == DiscardPolicy::None) can be moved here).

lld/test/ELF/emit-relocs-discard-locals.s
3

overrides

8

NM_NOGC -> NM-NOGC (we don't use underbars in tests I think)

10

Why it is important to test --gc-sections/--no-gc-sections too?

18

The emit-relocs-discard-locals.s test case name reflects the --emit-relocs, but not -r.
I think this test could be splitted into two perhaps. Or renamed properly.

MaskRay updated this revision to Diff 258757.Apr 20 2020, 8:25 AM
MaskRay marked 6 inline comments as done.

Address comments

lld/test/ELF/emit-relocs-discard-locals.s
8

I've seen underscores in a few places. Thinking more, underscores may not be bad because it does not conflict with -NOT -DAG ... or possible future extensions..

MaskRay updated this revision to Diff 258774.Apr 20 2020, 9:40 AM

Improve tests (check STT_SECTION)
Localize config->discard logic

MaskRay marked 5 inline comments as done.Apr 20 2020, 9:41 AM
grimar accepted this revision.Apr 21 2020, 2:13 AM

LGTM with a nit.

lld/test/ELF/emit-relocs-discard-locals.s
10

This comment was forgotten it seems. Please add a comment, I guess the reason to test "-gc-sections" is "just in case"?

This revision is now accepted and ready to land.Apr 21 2020, 2:13 AM
ikudrin accepted this revision.Apr 21 2020, 4:31 AM

LGTM with a couple of nits.

lld/ELF/Writer.cpp
646

You may use config->copyRelocs, it is the same.

lld/test/ELF/emit-relocs-discard-locals.s
27–28

Do we really need to check the symbols which are not important to the fix? Unnecessary details make the test fragile.

31–33

The same thoughts about these 0xFFFFFFFFFFFFFFFC values.

MaskRay marked 3 inline comments as done.Apr 21 2020, 8:05 AM
MaskRay added inline comments.
lld/test/ELF/emit-relocs-discard-locals.s
27–28

The STT_SECTION symbols (also STB_LOCAL) are actually an interesting cast which was neglected. I add them for coverage. I am careful and will not intentionally add useless tests.

31–33

These values don't make tests fragile and have minimum cost (they don't even introduce new lines).

Previously we simply dropped the symbols from relocation records. 0xFFFFFFFFFFFFFFFC is added just in case the addend is replaced by 0.

ikudrin added inline comments.Apr 21 2020, 8:14 AM
lld/test/ELF/emit-relocs-discard-locals.s
27–28

I meant .text, .comment and _start, not text and unreferenced. And I would have probably removed the section indexes for all symbols.

MaskRay updated this revision to Diff 259006.Apr 21 2020, 8:16 AM
MaskRay marked 4 inline comments as done.

Address comments

MaskRay updated this revision to Diff 259007.Apr 21 2020, 8:21 AM
MaskRay marked an inline comment as done.

Drop section indices from llvm-readelf -S output

lld/test/ELF/emit-relocs-discard-locals.s
27–28

unreferenced demonstrates that --discard-locals does not discard the STT_SECTION symbol associated with a GCed section.--gc-sections can discard such symbols.

text represents a subset of STB_LOCAL symbols (STT_SECTION).

MaskRay updated this revision to Diff 259010.Apr 21 2020, 8:25 AM

Rename unreferenced to gc

MaskRay edited the summary of this revision. (Show Details)Apr 21 2020, 8:25 AM
This revision was automatically updated to reflect the committed changes.
Harbormaster failed remote builds in B54100: Diff 259007!
Harbormaster failed remote builds in B54099: Diff 259006!