Skip to content

Commit 279898b

Browse files
author
George Rimar
committed
[llvm-objcopy] - Strip sections before symbols.
This is a fix for https://bugs.llvm.org/show_bug.cgi?id=40007. Idea is to swap the order of stripping. So that we strip sections before symbols what allows us to strip relocation sections without emitting the error about relative symbols. Differential revision: https://reviews.llvm.org/D59763 llvm-svn: 357017
1 parent d7aba76 commit 279898b

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# RUN: yaml2obj %s > %t
2+
3+
## Check we are able to strip all symbols and relocatable information at the same time.
4+
5+
# RUN: llvm-objcopy -S %t %t2
6+
# RUN: llvm-objdump --section-headers %t2 | FileCheck %s
7+
8+
# RUN: llvm-objcopy --strip-all-gnu %t %t2
9+
# RUN: llvm-objdump --section-headers %t2 | FileCheck %s
10+
11+
# CHECK-NOT: .symtab
12+
# CHECK-NOT: .rela.text
13+
14+
## Check we are able to strip the particular symbol if we
15+
## strip the corresponding relocation section at the same time.
16+
17+
# RUN: llvm-objcopy --strip-symbol=bar -R .rela.text %t %t2
18+
# RUN: llvm-readelf -s -S %t2 | FileCheck %s --check-prefix=STRIPSYM
19+
20+
# STRIPSYM-NOT: bar
21+
# STRIPSYM-NOT: .rela.text
22+
23+
# RUN: not llvm-objcopy --strip-symbol=bar %t %t2 2>&1 | FileCheck %s --check-prefix=ERR
24+
# ERR: not stripping symbol 'bar' because it is named in a relocation
25+
26+
--- !ELF
27+
FileHeader:
28+
Class: ELFCLASS64
29+
Data: ELFDATA2LSB
30+
Type: ET_REL
31+
Machine: EM_X86_64
32+
Sections:
33+
- Name: .text
34+
Type: SHT_PROGBITS
35+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
36+
Content: 00000000
37+
- Name: .rela.text
38+
Type: SHT_RELA
39+
Link: .symtab
40+
Info: .text
41+
Relocations:
42+
- Offset: 0x0000000000000000
43+
Symbol: bar
44+
Type: R_X86_64_32S
45+
Symbols:
46+
Global:
47+
- Name: bar
48+
Section: .text

llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,14 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
542542
Obj.OSABI = Config.OutputArch.getValue().OSABI;
543543
}
544544

545-
if (Error E = updateAndRemoveSymbols(Config, Obj))
545+
// It is important to remove the sections first. For example, we want to
546+
// remove the relocation sections before removing the symbols. That allows
547+
// us to avoid reporting the inappropriate errors about removing symbols
548+
// named in relocations.
549+
if (Error E = replaceAndRemoveSections(Config, Obj))
546550
return E;
547551

548-
if (Error E = replaceAndRemoveSections(Config, Obj))
552+
if (Error E = updateAndRemoveSymbols(Config, Obj))
549553
return E;
550554

551555
if (!Config.SectionsToRename.empty()) {

0 commit comments

Comments
 (0)