Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 742c679

Browse files
committed
[llvm-objcopy] Add support for --rename-section flags from gnu objcopy
Summary: Add support for --rename-section flags from gnu objcopy. Not all flags appear to have an effect for ELF objects, but allowing them would allow easier drop-in replacement. Other unrecognized flags are rejected. This was only tested by comparing flags printed by "readelf -e <.o>" against the output of gnu vs llvm objcopy, it hasn't been tested to be valid beyond that. Reviewers: jakehehrlich, alexshap Subscribers: llvm-commits, paulsemel, alexshap Differential Revision: https://reviews.llvm.org/D49870 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@338582 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 9e6a27f commit 742c679

File tree

6 files changed

+361
-13
lines changed

6 files changed

+361
-13
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Test that cpu/processor-specific SHF_* flags are preserved.
2+
3+
# ===== x86_64 =====
4+
5+
# RUN: yaml2obj -docnum 1 %s > %t-x86_64.o
6+
# RUN: llvm-objcopy --rename-section=.foo=.bar,alloc %t-x86_64.o
7+
# RUN: llvm-readobj -sections %t-x86_64.o | FileCheck %s --check-prefix=X86_64
8+
9+
--- !ELF
10+
FileHeader:
11+
Class: ELFCLASS64
12+
Data: ELFDATA2LSB
13+
Type: ET_REL
14+
Machine: EM_X86_64
15+
Sections:
16+
- Name: .foo
17+
Type: SHT_PROGBITS
18+
Flags: [ SHF_X86_64_LARGE ]
19+
20+
# X86_64: Name: .bar
21+
# X86_64-NEXT: Type: SHT_PROGBITS
22+
# X86_64-NEXT: Flags [
23+
# X86_64-NEXT: SHF_ALLOC (0x2)
24+
# X86_64-NEXT: SHF_WRITE (0x1)
25+
# X86_64-NEXT: SHF_X86_64_LARGE (0x10000000)
26+
# X86_64-NEXT: ]
27+
28+
# ===== hex =====
29+
30+
# RUN: yaml2obj -docnum 2 %s > %t-hex.o
31+
# RUN: llvm-objcopy --rename-section=.foo=.bar,alloc %t-hex.o
32+
# RUN: llvm-readobj -sections %t-hex.o | FileCheck %s --check-prefix=HEX
33+
34+
--- !ELF
35+
FileHeader:
36+
Class: ELFCLASS64
37+
Data: ELFDATA2LSB
38+
Type: ET_REL
39+
Machine: EM_HEXAGON
40+
Sections:
41+
- Name: .foo
42+
Type: SHT_PROGBITS
43+
Flags: [ SHF_HEX_GPREL ]
44+
45+
# HEX: Name: .bar
46+
# HEX-NEXT: Type: SHT_PROGBITS
47+
# HEX-NEXT: Flags [
48+
# HEX-NEXT: SHF_ALLOC (0x2)
49+
# HEX-NEXT: SHF_HEX_GPREL (0x10000000)
50+
# HEX-NEXT: SHF_WRITE (0x1)
51+
# HEX-NEXT: ]
52+
53+
# ===== mips =====
54+
55+
# RUN: yaml2obj -docnum 3 %s > %t-mips.o
56+
# RUN: llvm-objcopy --rename-section=.foo=.bar,alloc %t-mips.o
57+
# RUN: llvm-readobj -sections %t-mips.o | FileCheck %s --check-prefix=MIPS
58+
59+
--- !ELF
60+
FileHeader:
61+
Class: ELFCLASS64
62+
Data: ELFDATA2LSB
63+
Type: ET_REL
64+
Machine: EM_MIPS
65+
Sections:
66+
- Name: .foo
67+
Type: SHT_PROGBITS
68+
Flags: [ SHF_MIPS_NODUPES, SHF_MIPS_NAMES, SHF_MIPS_LOCAL,
69+
SHF_MIPS_NOSTRIP, SHF_MIPS_GPREL, SHF_MIPS_MERGE,
70+
SHF_MIPS_ADDR, SHF_MIPS_STRING ]
71+
72+
# Note: llvm-readobj prints SHF_EXCLUDE/SHF_MASKPROC since specifying all
73+
# SHF_MIPS_* flags covers the same bitset.
74+
75+
# MIPS: Name: .bar
76+
# MIPS-NEXT: Type: SHT_PROGBITS
77+
# MIPS-NEXT: Flags [
78+
# MIPS-NEXT: SHF_ALLOC (0x2)
79+
# MIPS-NEXT: SHF_EXCLUDE (0x80000000)
80+
# MIPS-NEXT: SHF_MASKPROC (0xF0000000)
81+
# MIPS-NEXT: SHF_MIPS_ADDR (0x40000000)
82+
# MIPS-NEXT: SHF_MIPS_GPREL (0x10000000)
83+
# MIPS-NEXT: SHF_MIPS_LOCAL (0x4000000)
84+
# MIPS-NEXT: SHF_MIPS_MERGE (0x20000000)
85+
# MIPS-NEXT: SHF_MIPS_NAMES (0x2000000)
86+
# MIPS-NEXT: SHF_MIPS_NODUPES (0x1000000)
87+
# MIPS-NEXT: SHF_MIPS_NOSTRIP (0x8000000)
88+
# MIPS-NEXT: SHF_MIPS_STRING (0x80000000)
89+
# MIPS-NEXT: SHF_WRITE (0x1)
90+
# MIPS-NEXT: ]
91+
92+
# ===== arm =====
93+
94+
# RUN: yaml2obj -docnum 4 %s > %t-arm.o
95+
# RUN: llvm-objcopy --rename-section=.foo=.bar,alloc %t-arm.o
96+
# RUN: llvm-readobj -sections %t-arm.o | FileCheck %s --check-prefix=ARM
97+
98+
--- !ELF
99+
FileHeader:
100+
Class: ELFCLASS64
101+
Data: ELFDATA2LSB
102+
Type: ET_REL
103+
Machine: EM_ARM
104+
Sections:
105+
- Name: .foo
106+
Type: SHT_PROGBITS
107+
Flags: [ SHF_ARM_PURECODE ]
108+
109+
# ARM: Name: .bar
110+
# ARM-NEXT: Type: SHT_PROGBITS
111+
# ARM-NEXT: Flags [
112+
# ARM-NEXT: SHF_ALLOC (0x2)
113+
# ARM-NEXT: SHF_ARM_PURECODE (0x20000000)
114+
# ARM-NEXT: SHF_WRITE (0x1)
115+
# ARM-NEXT: ]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# RUN: yaml2obj %s > %t
2+
3+
# Single flags on a section with all flags:
4+
# RUN: llvm-objcopy --rename-section=.foo=.bar,alloc %t %t.alloc
5+
# RUN: llvm-readobj -sections %t.alloc | FileCheck %s --check-prefixes=CHECK,ALLOC,WRITE
6+
# RUN: llvm-objcopy --rename-section=.foo=.bar,load %t %t.load
7+
# RUN: llvm-readobj -sections %t.load | FileCheck %s --check-prefixes=CHECK,WRITE
8+
# RUN: llvm-objcopy --rename-section=.foo=.bar,noload %t %t.noload
9+
# RUN: llvm-readobj -sections %t.noload | FileCheck %s --check-prefixes=CHECK,WRITE
10+
# RUN: llvm-objcopy --rename-section=.foo=.bar,readonly %t %t.readonly
11+
# RUN: llvm-readobj -sections %t.readonly | FileCheck %s --check-prefixes=CHECK
12+
# RUN: llvm-objcopy --rename-section=.foo=.bar,debug %t %t.debug
13+
# RUN: llvm-readobj -sections %t.debug | FileCheck %s --check-prefixes=CHECK,WRITE
14+
# RUN: llvm-objcopy --rename-section=.foo=.bar,code %t %t.code
15+
# RUN: llvm-readobj -sections %t.code | FileCheck %s --check-prefixes=CHECK,EXEC,WRITE
16+
# RUN: llvm-objcopy --rename-section=.foo=.bar,data %t %t.data
17+
# RUN: llvm-readobj -sections %t.data | FileCheck %s --check-prefixes=CHECK,WRITE
18+
# RUN: llvm-objcopy --rename-section=.foo=.bar,rom %t %t.rom
19+
# RUN: llvm-readobj -sections %t.rom | FileCheck %s --check-prefixes=CHECK,WRITE
20+
# RUN: llvm-objcopy --rename-section=.foo=.bar,contents %t %t.contents
21+
# RUN: llvm-readobj -sections %t.contents | FileCheck %s --check-prefixes=CHECK,WRITE
22+
# RUN: llvm-objcopy --rename-section=.foo=.bar,merge %t %t.merge
23+
# RUN: llvm-readobj -sections %t.merge | FileCheck %s --check-prefixes=CHECK,MERGE,WRITE
24+
# RUN: llvm-objcopy --rename-section=.foo=.bar,strings %t %t.strings
25+
# RUN: llvm-readobj -sections %t.strings | FileCheck %s --check-prefixes=CHECK,STRINGS,WRITE
26+
# RUN: llvm-objcopy --rename-section=.foo=.bar,share %t %t.share
27+
# RUN: llvm-readobj -sections %t.share | FileCheck %s --check-prefixes=CHECK,WRITE
28+
29+
!ELF
30+
FileHeader:
31+
Class: ELFCLASS64
32+
Data: ELFDATA2LSB
33+
Type: ET_REL
34+
Machine: EM_X86_64
35+
Sections:
36+
- Name: .infolink
37+
Type: SHT_NOBITS
38+
- Name: .group
39+
Type: SHT_GROUP
40+
Link: .symtab
41+
Flags: [ ]
42+
Info: dummy
43+
Members:
44+
- SectionOrType: GRP_COMDAT
45+
- SectionOrType: .foo
46+
- Name: .foo
47+
Type: SHT_PROGBITS
48+
Link: .infolink
49+
Flags: [ SHF_ALLOC, SHF_COMPRESSED, SHF_EXCLUDE, SHF_EXECINSTR,
50+
SHF_GROUP, SHF_INFO_LINK, SHF_LINK_ORDER, SHF_MERGE,
51+
SHF_OS_NONCONFORMING, SHF_STRINGS, SHF_TLS, SHF_WRITE ]
52+
Content: "a4a4a4a4"
53+
Symbols:
54+
Local:
55+
- Name: dummy
56+
Section: .group
57+
58+
# CHECK: Name: .bar
59+
# CHECK-NEXT: Type: SHT_PROGBITS
60+
# CHECK-NEXT: Flags [
61+
# ALLOC-NEXT: SHF_ALLOC (0x2)
62+
# CHECK-NEXT: SHF_COMPRESSED (0x800)
63+
# CHECK-NEXT: SHF_EXCLUDE (0x80000000)
64+
# EXEC-NEXT: SHF_EXECINSTR (0x4)
65+
# CHECK-NEXT: SHF_GROUP (0x200)
66+
# CHECK-NEXT: SHF_INFO_LINK (0x40)
67+
# CHECK-NEXT: SHF_LINK_ORDER (0x80)
68+
# MERGE-NEXT: SHF_MERGE (0x10)
69+
# STRINGS-NEXT: SHF_STRINGS (0x20)
70+
# CHECK-NEXT: SHF_TLS (0x400)
71+
# WRITE-NEXT: SHF_WRITE (0x1)
72+
# CHECK-NEXT: ]
73+
74+
# BAD-FLAG: unrecognized section flag 'xyzzy'
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# RUN: yaml2obj %s > %t
2+
3+
# Single flags on a section with no flags:
4+
# RUN: llvm-objcopy --rename-section=.foo=.bar,alloc %t %t.alloc
5+
# RUN: llvm-readobj -sections %t.alloc | FileCheck %s --check-prefixes=CHECK,ALLOC,WRITE
6+
# RUN: llvm-objcopy --rename-section=.foo=.bar,load %t %t.load
7+
# RUN: llvm-readobj -sections %t.load | FileCheck %s --check-prefixes=CHECK,WRITE
8+
# RUN: llvm-objcopy --rename-section=.foo=.bar,noload %t %t.noload
9+
# RUN: llvm-readobj -sections %t.noload | FileCheck %s --check-prefixes=CHECK,WRITE
10+
# RUN: llvm-objcopy --rename-section=.foo=.bar,readonly %t %t.readonly
11+
# RUN: llvm-readobj -sections %t.readonly | FileCheck %s --check-prefixes=CHECK
12+
# RUN: llvm-objcopy --rename-section=.foo=.bar,debug %t %t.debug
13+
# RUN: llvm-readobj -sections %t.debug | FileCheck %s --check-prefixes=CHECK,WRITE
14+
# RUN: llvm-objcopy --rename-section=.foo=.bar,code %t %t.code
15+
# RUN: llvm-readobj -sections %t.code | FileCheck %s --check-prefixes=CHECK,EXEC,WRITE
16+
# RUN: llvm-objcopy --rename-section=.foo=.bar,data %t %t.data
17+
# RUN: llvm-readobj -sections %t.data | FileCheck %s --check-prefixes=CHECK,WRITE
18+
# RUN: llvm-objcopy --rename-section=.foo=.bar,rom %t %t.rom
19+
# RUN: llvm-readobj -sections %t.rom | FileCheck %s --check-prefixes=CHECK,WRITE
20+
# RUN: llvm-objcopy --rename-section=.foo=.bar,contents %t %t.contents
21+
# RUN: llvm-readobj -sections %t.contents | FileCheck %s --check-prefixes=CHECK,WRITE
22+
# RUN: llvm-objcopy --rename-section=.foo=.bar,merge %t %t.merge
23+
# RUN: llvm-readobj -sections %t.merge | FileCheck %s --check-prefixes=CHECK,MERGE,WRITE
24+
# RUN: llvm-objcopy --rename-section=.foo=.bar,strings %t %t.strings
25+
# RUN: llvm-readobj -sections %t.strings | FileCheck %s --check-prefixes=CHECK,STRINGS,WRITE
26+
# RUN: llvm-objcopy --rename-section=.foo=.bar,share %t %t.share
27+
# RUN: llvm-readobj -sections %t.share | FileCheck %s --check-prefixes=CHECK,WRITE
28+
29+
# Multiple flags:
30+
# RUN: llvm-objcopy --rename-section=.foo=.bar,alloc,readonly,strings %t %t.alloc_ro_strings
31+
# RUN: llvm-readobj -sections %t.alloc_ro_strings | FileCheck %s --check-prefixes=CHECK,ALLOC,STRINGS
32+
# RUN: llvm-objcopy --rename-section=.foo=.bar,alloc,code %t %t.alloc_code
33+
# RUN: llvm-readobj -sections %t.alloc_code | FileCheck %s --check-prefixes=CHECK,ALLOC,EXEC,WRITE
34+
35+
# Invalid flags:
36+
# RUN: not llvm-objcopy --rename-section=.foo=.bar,xyzzy %t %t.xyzzy 2>&1 | FileCheck %s --check-prefix=BAD-FLAG
37+
38+
!ELF
39+
FileHeader:
40+
Class: ELFCLASS64
41+
Data: ELFDATA2LSB
42+
Type: ET_REL
43+
Machine: EM_X86_64
44+
Sections:
45+
- Name: .foo
46+
Type: SHT_PROGBITS
47+
Flags: [ ]
48+
Content: "c3c3c3c3"
49+
50+
# CHECK: Name: .bar
51+
# CHECK-NEXT: Type: SHT_PROGBITS
52+
# CHECK-NEXT: Flags [
53+
# ALLOC-NEXT: SHF_ALLOC (0x2)
54+
# EXEC-NEXT: SHF_EXECINSTR (0x4)
55+
# MERGE-NEXT: SHF_MERGE (0x10)
56+
# STRINGS-NEXT: SHF_STRINGS (0x20)
57+
# WRITE-NEXT: SHF_WRITE (0x1)
58+
# CHECK-NEXT: ]
59+
60+
# BAD-FLAG: Unrecognized section flag 'xyzzy'. Flags supported for GNU compatibility: alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings.

test/tools/llvm-objcopy/rename-section.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ Sections:
2626
# CHECK: Name: .strtab
2727
# CHECK: Name: .shstrtab
2828

29-
#BAD-FORMAT: Bad format for --rename-section
30-
#MULTIPLE-RENAMES: Already have a section rename for .foo
29+
#BAD-FORMAT: Bad format for --rename-section: missing '='
30+
#MULTIPLE-RENAMES: Multiple renames of section .foo

tools/llvm-objcopy/ObjcopyOpts.td

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ 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">;
30+
defm rename_section
31+
: Eq<"rename-section">,
32+
MetaVarName<"old=new[,flag1,...]">,
33+
HelpText<
34+
"Renames a section from old to new, optionally with specified flags. "
35+
"Flags supported for GNU compatibility: alloc, load, noload, "
36+
"readonly, debug, code, data, rom, share, contents, merge, strings.">;
3337
defm redefine_symbol : Eq<"redefine-sym">,
3438
MetaVarName<"old=new">,
3539
HelpText<"Change the name of a symbol old to new">;

0 commit comments

Comments
 (0)