This is an archive of the discontinued LLVM Phabricator instance.

[LLD][ELF] Implement --keep-unique option
ClosedPublic

Authored by peter.smith on May 11 2018, 6:59 AM.

Details

Summary

The --keep-unique <symbol> option is taken from gold. The intention is that <symbol> will be prevented from being folded by ICF. Although not specifically mentioned in the documentation <symbol> only matches global symbols. In gold the option "unfolds" sections that have been folded due to ICF. For LLD I've decided to make sections defining a global symbol specified by --keep-unique inelligible for ICF.

This option is used within the Android ART runtime for jit_debug_register_code and dex_debug_register_code. Supporting it will allow ART to be linked with LLD. I think it could also be used as a basis for --icf=safe (find unsafe symbols and exclude the sections that define them).

Diff Detail

Repository
rLLD LLVM Linker

Event Timeline

peter.smith created this revision.May 11 2018, 6:59 AM
ruiu added a comment.May 11 2018, 10:16 AM

I agree that we should support this option.

ELF/ICF.cpp
422–423

I think that looking up a hash function for each input section is slow, so I'd avoid doing this. Instead, I believe you can add a KeepUnique bit to the InputSection and set that bit in the driver.

grimar added inline comments.May 12 2018, 1:16 AM
ELF/ICF.cpp
422–423

I think adding KeepUnique will not change the size of SectionBase, so that is probably ok.

But at the same time, I just wonder why do you think it should be slow? I do not expect to see more than ... well, 50? sections in a set. That probably should not be slow here. We could avoid adding the flag then.

test/ELF/icf-keep-unique.s
9

This contains absolute path and will fail in a different environment I think.

29

can these 3 lines be a nop?

grimar added inline comments.May 12 2018, 1:23 AM
ELF/ICF.cpp
427

We usually start warnings and errors from the lower case character.

Thanks very much for the comments. I've made the following changes:

  • Used a bitfield for KeepUnique
  • moved findKeepUniqueSections to the driver
  • Simplified test case with nops, and removed absolute paths.
ruiu accepted this revision.May 14 2018, 3:50 PM

LGTM with these changes.

ELF/Driver.cpp
1168–1169

I'd think you don't need Config->KeepUnique if you pass Args to this function. If you do that, you can directly consume --keep-unique arguments like this

for (auto *Arg : Args.filtered(OPT_keep_unique)) {
  StringRef Name = Arg->getValue();
  ...
1170–1171

nit: you could do this

if (auto *Sym = dyn_cast_or_null<Defined>(Symtab->Find(Name))
This revision is now accepted and ready to land.May 14 2018, 3:50 PM
This revision was automatically updated to reflect the committed changes.

Thank you. I've made the suggested changes and committed under r332332