Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions llvm/docs/CommandGuide/llvm-objcopy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ multiple file formats.

.. option:: --discard-all, -x

Remove most local symbols from the output. Different file formats may limit
this to a subset of the local symbols. For example, file and section symbols in
ELF objects will not be discarded. Additionally, remove all debug sections.
Remove most local symbols not referenced by relocations from the output.
Different file formats may limit this to a subset of the local symbols. For
example, file and section symbols in ELF objects will not be discarded.
Additionally, remove all debug sections.

.. option:: --dump-section <section>=<file>

Expand Down Expand Up @@ -386,7 +387,7 @@ them.

.. option:: --discard-locals, -X

Remove local symbols starting with ".L" from the output.
Remove local symbols starting with ".L" not referenced by relocations from the output.

.. option:: --extract-dwo

Expand Down
25 changes: 13 additions & 12 deletions llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
// (like GroupSection or RelocationSection). This way, we know which
// symbols are still 'needed' and which are not.
if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() ||
!Config.OnlySection.empty()) {
!Config.OnlySection.empty() || Config.DiscardMode != DiscardType::None) {
for (SectionBase &Sec : Obj.sections())
Sec.markSymbols();
}
Expand All @@ -386,22 +386,23 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
if (Config.StripDebug && Sym.Type == STT_FILE)
return true;

if ((Config.DiscardMode == DiscardType::All ||
(Config.DiscardMode == DiscardType::Locals &&
StringRef(Sym.Name).starts_with(".L"))) &&
Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
return true;

if ((Config.StripUnneeded ||
Config.UnneededSymbolsToRemove.matches(Sym.Name)) &&
(!Obj.isRelocatable() || isUnneededSymbol(Sym)))
return true;

// We want to remove undefined symbols if all references have been stripped.
if (!Config.OnlySection.empty() && !Sym.Referenced &&
Sym.getShndx() == SHN_UNDEF)
return true;
if (!Sym.Referenced) {
if ((Config.DiscardMode == DiscardType::All ||
(Config.DiscardMode == DiscardType::Locals &&
StringRef(Sym.Name).starts_with(".L"))) &&
Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
return true;
// We want to remove undefined symbols if all references have been
// stripped.
if (!Config.OnlySection.empty() && Sym.getShndx() == SHN_UNDEF)
return true;
}

return false;
};
Expand Down
19 changes: 19 additions & 0 deletions llvm/test/tools/llvm-objcopy/ELF/discard-all.test
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ Sections:
Address: 0x1000
AddressAlign: 0x0000000000000010
Size: 64
- Name: .rela.text
Type: SHT_RELA
Link: .symtab
Info: .text
Relocations:
- Offset: 0
Symbol: Referenced
Type: R_X86_64_PC32
Symbols:
- Name: Local
Type: STT_FUNC
Expand All @@ -49,6 +57,8 @@ Symbols:
Section: .text
Value: 0x1010
Binding: STB_GLOBAL
- Name: Referenced
Section: .text
- Name: Weak
Type: STT_FUNC
Size: 8
Expand Down Expand Up @@ -85,6 +95,15 @@ Symbols:
#CHECK-NEXT: Section: Undefined
#CHECK-NEXT: }
#CHECK-NEXT: Symbol {
#CHECK-NEXT: Name: Referenced
#CHECK-NEXT: Value: 0x0
#CHECK-NEXT: Size: 0
#CHECK-NEXT: Binding: Local
#CHECK-NEXT: Type: None
#CHECK-NEXT: Other: 0
#CHECK-NEXT: Section: .text
#CHECK-NEXT: }
#CHECK-NEXT: Symbol {
#CHECK-NEXT: Name: Global
#CHECK-NEXT: Value: 0x1010
#CHECK-NEXT: Size: 8
Expand Down
26 changes: 0 additions & 26 deletions llvm/test/tools/llvm-objcopy/ELF/discard-locals-rel.test

This file was deleted.

19 changes: 19 additions & 0 deletions llvm/test/tools/llvm-objcopy/ELF/discard-locals.test
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ FileHeader:
Sections:
- Name: .text
Type: SHT_PROGBITS
- Name: .rela.text
Type: SHT_RELA
Link: .symtab
Info: .text
Relocations:
- Offset: 0
Symbol: .L.referenced
Type: R_X86_64_PC32
- Name: .LLVM.Custom.Section
Type: SHT_PROGBITS
Symbols:
Expand All @@ -48,6 +56,8 @@ Symbols:
- Name: .L.undefined
- Name: .L.abs
Index: SHN_ABS
- Name: .L.referenced
Section: .text
- Name: .L.Global
Type: STT_FUNC
Section: .text
Expand Down Expand Up @@ -109,6 +119,15 @@ Symbols:
# CHECK-NEXT: Section: Undefined
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: .L.referenced
# CHECK-NEXT: Value:
# CHECK-NEXT: Size:
# CHECK-NEXT: Binding: Local
# CHECK-NEXT: Type: None
# CHECK-NEXT: Other:
# CHECK-NEXT: Section: .text
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: .L.Global
# CHECK-NEXT: Value:
# CHECK-NEXT: Size:
Expand Down