Skip to content

Commit 7a66a26

Browse files
authored
[llvm-objcopy,ELF] --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 llvm#47468 (tcl has a use case that requires `strip -x tclStubLib.o` to strip local symbols not referenced by relocations.) Pull Request: llvm#130704
1 parent 1585db4 commit 7a66a26

File tree

6 files changed

+57
-42
lines changed

6 files changed

+57
-42
lines changed

llvm/docs/CommandGuide/llvm-objcopy.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ multiple file formats.
5757

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

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

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

@@ -386,7 +387,7 @@ them.
386387

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

389-
Remove local symbols starting with ".L" from the output.
390+
Remove local symbols starting with ".L" not referenced by relocations from the output.
390391

391392
.. option:: --extract-dwo
392393

llvm/docs/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Changes to the LLVM tools
160160

161161
* llvm-objcopy now supports the `--update-section` flag for intermediate Mach-O object files.
162162
* llvm-strip now supports continuing to process files on encountering an error.
163+
* In llvm-objcopy's ELF port, `--discard-locals` and `--discard-all` now allow and preserve symbols referenced by relocations.
163164

164165
Changes to LLDB
165166
---------------------------------

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

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

389-
if ((Config.DiscardMode == DiscardType::All ||
390-
(Config.DiscardMode == DiscardType::Locals &&
391-
StringRef(Sym.Name).starts_with(".L"))) &&
392-
Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
393-
Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
394-
return true;
395-
396389
if ((Config.StripUnneeded ||
397390
Config.UnneededSymbolsToRemove.matches(Sym.Name)) &&
398391
(!Obj.isRelocatable() || isUnneededSymbol(Sym)))
399392
return true;
400393

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

406407
return false;
407408
};

llvm/test/tools/llvm-objcopy/ELF/discard-all.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ Sections:
3232
Address: 0x1000
3333
AddressAlign: 0x0000000000000010
3434
Size: 64
35+
- Name: .rela.text
36+
Type: SHT_RELA
37+
Link: .symtab
38+
Info: .text
39+
Relocations:
40+
- Offset: 0
41+
Symbol: Referenced
42+
Type: R_X86_64_PC32
3543
Symbols:
3644
- Name: Local
3745
Type: STT_FUNC
@@ -49,6 +57,8 @@ Symbols:
4957
Section: .text
5058
Value: 0x1010
5159
Binding: STB_GLOBAL
60+
- Name: Referenced
61+
Section: .text
5262
- Name: Weak
5363
Type: STT_FUNC
5464
Size: 8
@@ -85,6 +95,15 @@ Symbols:
8595
#CHECK-NEXT: Section: Undefined
8696
#CHECK-NEXT: }
8797
#CHECK-NEXT: Symbol {
98+
#CHECK-NEXT: Name: Referenced
99+
#CHECK-NEXT: Value: 0x0
100+
#CHECK-NEXT: Size: 0
101+
#CHECK-NEXT: Binding: Local
102+
#CHECK-NEXT: Type: None
103+
#CHECK-NEXT: Other: 0
104+
#CHECK-NEXT: Section: .text
105+
#CHECK-NEXT: }
106+
#CHECK-NEXT: Symbol {
88107
#CHECK-NEXT: Name: Global
89108
#CHECK-NEXT: Value: 0x1010
90109
#CHECK-NEXT: Size: 8

llvm/test/tools/llvm-objcopy/ELF/discard-locals-rel.test

Lines changed: 0 additions & 26 deletions
This file was deleted.

llvm/test/tools/llvm-objcopy/ELF/discard-locals.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ FileHeader:
2929
Sections:
3030
- Name: .text
3131
Type: SHT_PROGBITS
32+
- Name: .rela.text
33+
Type: SHT_RELA
34+
Link: .symtab
35+
Info: .text
36+
Relocations:
37+
- Offset: 0
38+
Symbol: .L.referenced
39+
Type: R_X86_64_PC32
3240
- Name: .LLVM.Custom.Section
3341
Type: SHT_PROGBITS
3442
Symbols:
@@ -48,6 +56,8 @@ Symbols:
4856
- Name: .L.undefined
4957
- Name: .L.abs
5058
Index: SHN_ABS
59+
- Name: .L.referenced
60+
Section: .text
5161
- Name: .L.Global
5262
Type: STT_FUNC
5363
Section: .text
@@ -109,6 +119,15 @@ Symbols:
109119
# CHECK-NEXT: Section: Undefined
110120
# CHECK-NEXT: }
111121
# CHECK-NEXT: Symbol {
122+
# CHECK-NEXT: Name: .L.referenced
123+
# CHECK-NEXT: Value:
124+
# CHECK-NEXT: Size:
125+
# CHECK-NEXT: Binding: Local
126+
# CHECK-NEXT: Type: None
127+
# CHECK-NEXT: Other:
128+
# CHECK-NEXT: Section: .text
129+
# CHECK-NEXT: }
130+
# CHECK-NEXT: Symbol {
112131
# CHECK-NEXT: Name: .L.Global
113132
# CHECK-NEXT: Value:
114133
# CHECK-NEXT: Size:

0 commit comments

Comments
 (0)