Skip to content

Commit 959806e

Browse files
committed
Merge commit 7a66a26658f4 from llvm git (by Fangrui Song):
--discard-locals/--discard-all: allow and keep symbols referenced by relocations In GNU objcopy, symbols referenced by relocations are retained. Our COFF (https://reviews.llvm.org/D56480) and Mach-O (https://reviews.llvm.org/D75104) ports port the behavior, but the ELF port doesn't. This PR implements the behavior for ELF. Close #47468 (tcl has a use case that requires `strip -x tclStubLib.o` to strip local symbols not referenced by relocations.) Pull Request: llvm/llvm-project#130704 PR: 258820 Approved by: dim Differential Revision: https://reviews.freebsd.org/D52198
1 parent e6253ea commit 959806e

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

contrib/llvm-project/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
368368
// (like GroupSection or RelocationSection). This way, we know which
369369
// symbols are still 'needed' and which are not.
370370
if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() ||
371-
!Config.OnlySection.empty()) {
371+
!Config.OnlySection.empty() || Config.DiscardMode != DiscardType::None) {
372372
for (SectionBase &Sec : Obj.sections())
373373
Sec.markSymbols();
374374
}
@@ -390,22 +390,23 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
390390
if (Config.StripDebug && Sym.Type == STT_FILE)
391391
return true;
392392

393-
if ((Config.DiscardMode == DiscardType::All ||
394-
(Config.DiscardMode == DiscardType::Locals &&
395-
StringRef(Sym.Name).starts_with(".L"))) &&
396-
Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
397-
Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
398-
return true;
399-
400393
if ((Config.StripUnneeded ||
401394
Config.UnneededSymbolsToRemove.matches(Sym.Name)) &&
402395
(!Obj.isRelocatable() || isUnneededSymbol(Sym)))
403396
return true;
404397

405-
// We want to remove undefined symbols if all references have been stripped.
406-
if (!Config.OnlySection.empty() && !Sym.Referenced &&
407-
Sym.getShndx() == SHN_UNDEF)
408-
return true;
398+
if (!Sym.Referenced) {
399+
if ((Config.DiscardMode == DiscardType::All ||
400+
(Config.DiscardMode == DiscardType::Locals &&
401+
StringRef(Sym.Name).starts_with(".L"))) &&
402+
Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
403+
Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
404+
return true;
405+
// We want to remove undefined symbols if all references have been
406+
// stripped.
407+
if (!Config.OnlySection.empty() && Sym.getShndx() == SHN_UNDEF)
408+
return true;
409+
}
409410

410411
return false;
411412
};

0 commit comments

Comments
 (0)