Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit ff83113

Browse files
committed
[llvm-objcopy] Add basic support for --rename-section
Summary: Add basic support for --rename-section=old=new to llvm-objcopy. A full replacement for GNU objcopy requires also modifying flags (i.e. --rename-section=old=new,flag1,flag2); I'd like to keep that in a separate change to keep this simple. Reviewers: jakehehrlich, alexshap Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D49576 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337604 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 502e1bf commit ff83113

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# RUN: yaml2obj %s > %t
2+
# RUN: llvm-objcopy --rename-section=.test1=.test2 --rename-section=.test3=.test4 --rename-section=.test5=.test6 %t %t2
3+
# RUN: llvm-readobj -file-headers -sections -section-data %t2 | FileCheck %s
4+
5+
!ELF
6+
FileHeader:
7+
Class: ELFCLASS64
8+
Data: ELFDATA2LSB
9+
Type: ET_REL
10+
Machine: EM_X86_64
11+
Sections:
12+
- Name: .test1
13+
Type: SHT_PROGBITS
14+
Flags: [ SHF_ALLOC ]
15+
Content: "c3c3c3c3"
16+
- Name: .test3
17+
Type: SHT_PROGBITS
18+
Flags: [ SHF_ALLOC ]
19+
Content: "abababab"
20+
- Name: .test7
21+
Type: SHT_PROGBITS
22+
Flags: [ SHF_ALLOC ]
23+
Content: "37373737"
24+
25+
# CHECK: SectionHeaderCount: 7
26+
27+
# CHECK: Name: .test2
28+
# CHECK: SectionData (
29+
# CHECK-NEXT: 0000: C3C3C3C3
30+
# CHECK-NEXT: )
31+
# CHECK: Name: .test4
32+
# CHECK: SectionData (
33+
# CHECK-NEXT: 0000: ABABABAB
34+
# CHECK-NEXT: )
35+
# CHECK: Name: .test7
36+
# CHECK: SectionData (
37+
# CHECK-NEXT: 0000: 37373737
38+
# CHECK-NEXT: )
39+
# CHECK: Name: .symtab
40+
# CHECK: Name: .strtab
41+
# CHECK: Name: .shstrtab
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# RUN: yaml2obj %s > %t
2+
# RUN: llvm-objcopy --rename-section=.foo=.bar %t %t2
3+
# RUN: llvm-readobj -file-headers -sections -section-data %t2 | FileCheck %s
4+
# RUN: not llvm-objcopy --rename-section=.foo.bar --rename-section=.foo=.other %t %t2 2>&1 | FileCheck %s --check-prefix=BAD-FORMAT
5+
# RUN: not llvm-objcopy --rename-section=.foo=.bar --rename-section=.foo=.other %t %t2 2>&1 | FileCheck %s --check-prefix=MULTIPLE-RENAMES
6+
7+
!ELF
8+
FileHeader:
9+
Class: ELFCLASS64
10+
Data: ELFDATA2LSB
11+
Type: ET_REL
12+
Machine: EM_X86_64
13+
Sections:
14+
- Name: .foo
15+
Type: SHT_PROGBITS
16+
Flags: [ SHF_ALLOC ]
17+
Content: "c3c3c3c3"
18+
19+
# CHECK: SectionHeaderCount: 5
20+
21+
# CHECK: Name: .bar
22+
# CHECK: SectionData (
23+
# CHECK-NEXT: 0000: C3C3C3C3
24+
# CHECK-NEXT: )
25+
# CHECK: Name: .symtab
26+
# CHECK: Name: .strtab
27+
# CHECK: Name: .shstrtab
28+
29+
#BAD-FORMAT: Bad format for --rename-section
30+
#MULTIPLE-RENAMES: Already have a section rename for .foo

tools/llvm-objcopy/ObjcopyOpts.td

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ defm add_gnu_debuglink : Eq<"add-gnu-debuglink">,
2727
defm remove_section : Eq<"remove-section">,
2828
MetaVarName<"section">,
2929
HelpText<"Remove <section>">;
30+
defm rename_section : Eq<"rename-section">,
31+
MetaVarName<"old=new">,
32+
HelpText<"Renames a section from old to new">;
3033
defm redefine_symbol : Eq<"redefine-sym">,
3134
MetaVarName<"old=new">,
3235
HelpText<"Change the name of a symbol old to new">;

tools/llvm-objcopy/llvm-objcopy.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ struct CopyConfig {
133133
std::vector<StringRef> SymbolsToWeaken;
134134
std::vector<StringRef> SymbolsToRemove;
135135
std::vector<StringRef> SymbolsToKeep;
136+
StringMap<StringRef> SectionsToRename;
136137
StringMap<StringRef> SymbolsToRename;
137138
bool StripAll = false;
138139
bool StripAllGNU = false;
@@ -430,6 +431,14 @@ static void HandleArgs(const CopyConfig &Config, Object &Obj,
430431

431432
Obj.removeSections(RemovePred);
432433

434+
if (!Config.SectionsToRename.empty()) {
435+
for (auto &Sec : Obj.sections()) {
436+
const auto Iter = Config.SectionsToRename.find(Sec.Name);
437+
if (Iter != Config.SectionsToRename.end())
438+
Sec.Name = Iter->second;
439+
}
440+
}
441+
433442
if (!Config.AddSection.empty()) {
434443
for (const auto &Flag : Config.AddSection) {
435444
auto SecPair = Flag.split("=");
@@ -587,6 +596,14 @@ static CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
587596
error("Multiple redefinition of symbol " + Old2New.first);
588597
}
589598

599+
for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) {
600+
if (!StringRef(Arg->getValue()).contains('='))
601+
error("Bad format for --rename-section");
602+
auto Old2New = StringRef(Arg->getValue()).split('=');
603+
if (!Config.SectionsToRename.insert(Old2New).second)
604+
error("Already have a section rename for " + Old2New.first);
605+
}
606+
590607
for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
591608
Config.ToRemove.push_back(Arg->getValue());
592609
for (auto Arg : InputArgs.filtered(OBJCOPY_keep))

0 commit comments

Comments
 (0)