Skip to content

Commit 671fb34

Browse files
committedOct 2, 2019
[llvm-objcopy] Add --set-section-alignment
Fixes PR43181. This option was recently added to GNU objcopy (binutils PR24942). `llvm-objcopy -I binary -O elf64-x86-64 --set-section-alignment .data=8` can set the alignment of .data. Reviewed By: grimar, jhenderson, rupprecht Differential Revision: https://reviews.llvm.org/D67656 llvm-svn: 373461
1 parent ecbfb85 commit 671fb34

File tree

9 files changed

+119
-11
lines changed

9 files changed

+119
-11
lines changed
 

‎llvm/docs/CommandGuide/llvm-objcopy.rst

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ multiple file formats.
8282
Remove the specified section from the output. Can be specified multiple times
8383
to remove multiple sections simultaneously.
8484

85+
.. option:: --set-section-alignment <section>=<align>
86+
87+
Set the alignment of section ``<section>`` to `<align>``. Can be specified
88+
multiple times to update multiple sections.
89+
8590
.. option:: --strip-all-gnu
8691

8792
Remove all symbols, debug sections and relocations from the output. This option

‎llvm/test/tools/llvm-objcopy/ELF/binary-input.test

+8
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,11 @@
110110
# CHECK-NEXT: Section: Absolute
111111
# CHECK-NEXT: }
112112
# CHECK-NEXT: ]
113+
114+
## The alignment can be changed by --set-section-alignment.
115+
# RUN: llvm-objcopy -I binary -O elf64-x86-64 --set-section-alignment .data=8 %t.x-txt %t2.o
116+
# RUN: llvm-readobj --sections %t2.o | FileCheck --check-prefix=ALIGN %s
117+
118+
# ALIGN: Name: .data
119+
# ALIGN: AddressAlignment:
120+
# ALIGN-SAME: 8{{$}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# RUN: yaml2obj %s -o %t
2+
3+
# RUN: llvm-objcopy --set-section-alignment .foo=4 --set-section-alignment .bar=0x5 \
4+
# RUN: --set-section-alignment .baz=0 %t %t.2
5+
# RUN: llvm-readobj --sections %t.2 | FileCheck --check-prefix=CHECK %s
6+
7+
# CHECK: Name: .foo
8+
# CHECK: AddressAlignment:
9+
# CHECK-SAME: 4{{$}}
10+
# CHECK: Name: .bar
11+
# CHECK: AddressAlignment:
12+
# CHECK-SAME: 5{{$}}
13+
# CHECK: Name: .baz
14+
# CHECK: AddressAlignment:
15+
# CHECK-SAME: 0{{$}}
16+
17+
## If a section is specified multiple times, the last wins.
18+
# RUN: llvm-objcopy --set-section-alignment .foo=4 --set-section-alignment=.foo=7 %t %t.3
19+
# RUN: llvm-readobj --sections %t.3 | FileCheck --check-prefix=MULTI %s
20+
21+
# MULTI: Name: .foo
22+
# MULTI: AddressAlignment:
23+
# MULTI-SAME: 7{{$}}
24+
25+
## Ignore the option if the section does not exist.
26+
# RUN: llvm-objcopy --set-section-alignment .not_exist=4 %t.3 %t.4
27+
# RUN: cmp %t.3 %t.4
28+
29+
# RUN: not llvm-objcopy --set-section-alignment=.foo %t /dev/null 2>&1 | \
30+
# RUN: FileCheck --check-prefix=MISSING-EQUAL %s
31+
# MISSING-EQUAL: error: bad format for --set-section-alignment: missing '='
32+
33+
# RUN: not llvm-objcopy --set-section-alignment==4 %t /dev/null 2>&1 | \
34+
# RUN: FileCheck --check-prefix=MISSING-SECTION %s
35+
# MISSING-SECTION: error: bad format for --set-section-alignment: missing section name
36+
37+
# RUN: not llvm-objcopy --set-section-alignment=.foo=bar %t /dev/null 2>&1 | \
38+
# RUN: FileCheck --check-prefix=INVALID-ALIGN %s
39+
# INVALID-ALIGN: error: invalid alignment for --set-section-alignment: 'bar'
40+
41+
!ELF
42+
FileHeader:
43+
Class: ELFCLASS64
44+
Data: ELFDATA2LSB
45+
Type: ET_REL
46+
Machine: EM_X86_64
47+
Sections:
48+
- Name: .foo
49+
Type: SHT_PROGBITS
50+
- Name: .bar
51+
Type: SHT_NOBITS
52+
- Name: .baz
53+
Type: SHT_NOTE
54+
AddressAlign: 4

‎llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,11 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
208208
!Config.SymbolsToGlobalize.empty() || !Config.SymbolsToKeep.empty() ||
209209
!Config.SymbolsToLocalize.empty() || !Config.SymbolsToWeaken.empty() ||
210210
!Config.SymbolsToKeepGlobal.empty() || !Config.SectionsToRename.empty() ||
211-
!Config.SetSectionFlags.empty() || !Config.SymbolsToRename.empty() ||
212-
Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden ||
213-
Config.PreserveDates || Config.StripDWO || Config.StripNonAlloc ||
214-
Config.StripSections || Config.Weaken || Config.DecompressDebugSections ||
211+
!Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() ||
212+
!Config.SymbolsToRename.empty() || Config.ExtractDWO ||
213+
Config.KeepFileSymbols || Config.LocalizeHidden || Config.PreserveDates ||
214+
Config.StripDWO || Config.StripNonAlloc || Config.StripSections ||
215+
Config.Weaken || Config.DecompressDebugSections ||
215216
Config.DiscardMode == DiscardType::Locals ||
216217
!Config.SymbolsToAdd.empty() || Config.EntryExpr) {
217218
return createStringError(llvm::errc::invalid_argument,

‎llvm/tools/llvm-objcopy/CopyConfig.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) {
155155
return SR;
156156
}
157157

158+
static Expected<std::pair<StringRef, uint64_t>>
159+
parseSetSectionAlignment(StringRef FlagValue) {
160+
if (!FlagValue.contains('='))
161+
return createStringError(
162+
errc::invalid_argument,
163+
"bad format for --set-section-alignment: missing '='");
164+
auto Split = StringRef(FlagValue).split('=');
165+
if (Split.first.empty())
166+
return createStringError(
167+
errc::invalid_argument,
168+
"bad format for --set-section-alignment: missing section name");
169+
uint64_t NewAlign;
170+
if (Split.second.getAsInteger(0, NewAlign))
171+
return createStringError(errc::invalid_argument,
172+
"invalid alignment for --set-section-alignment: '%s'",
173+
Split.second.str().c_str());
174+
return std::make_pair(Split.first, NewAlign);
175+
}
176+
158177
static Expected<SectionFlagsUpdate>
159178
parseSetSectionFlagValue(StringRef FlagValue) {
160179
if (!StringRef(FlagValue).contains('='))
@@ -489,6 +508,13 @@ Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
489508
"multiple renames of section '%s'",
490509
SR->OriginalName.str().c_str());
491510
}
511+
for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_alignment)) {
512+
Expected<std::pair<StringRef, uint64_t>> NameAndAlign =
513+
parseSetSectionAlignment(Arg->getValue());
514+
if (!NameAndAlign)
515+
return NameAndAlign.takeError();
516+
Config.SetSectionAlignment[NameAndAlign->first] = NameAndAlign->second;
517+
}
492518
for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) {
493519
Expected<SectionFlagsUpdate> SFU =
494520
parseSetSectionFlagValue(Arg->getValue());

‎llvm/tools/llvm-objcopy/CopyConfig.h

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ struct CopyConfig {
161161

162162
// Map options
163163
StringMap<SectionRename> SectionsToRename;
164+
StringMap<uint64_t> SetSectionAlignment;
164165
StringMap<SectionFlagsUpdate> SetSectionFlags;
165166
StringMap<StringRef> SymbolsToRename;
166167

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

+8
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,14 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
670670
}
671671
}
672672

673+
if (!Config.SetSectionAlignment.empty()) {
674+
for (SectionBase &Sec : Obj.sections()) {
675+
auto I = Config.SetSectionAlignment.find(Sec.Name);
676+
if (I != Config.SetSectionAlignment.end())
677+
Sec.Align = I->second;
678+
}
679+
}
680+
673681
if (!Config.SetSectionFlags.empty()) {
674682
for (auto &Sec : Obj.sections()) {
675683
const auto Iter = Config.SetSectionFlags.find(Sec.Name);

‎llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
3131
!Config.SymbolsToKeepGlobal.empty() || !Config.SectionsToRename.empty() ||
3232
!Config.SymbolsToRename.empty() ||
3333
!Config.UnneededSymbolsToRemove.empty() ||
34-
!Config.SetSectionFlags.empty() || !Config.ToRemove.empty() ||
35-
Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden ||
36-
Config.PreserveDates || Config.StripDWO || Config.StripNonAlloc ||
37-
Config.StripSections || Config.Weaken || Config.DecompressDebugSections ||
38-
Config.StripDebug || Config.StripNonAlloc || Config.StripSections ||
39-
Config.StripUnneeded || Config.DiscardMode != DiscardType::None ||
40-
!Config.SymbolsToAdd.empty() || Config.EntryExpr) {
34+
!Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() ||
35+
!Config.ToRemove.empty() || Config.ExtractDWO || Config.KeepFileSymbols ||
36+
Config.LocalizeHidden || Config.PreserveDates || Config.StripDWO ||
37+
Config.StripNonAlloc || Config.StripSections || Config.Weaken ||
38+
Config.DecompressDebugSections || Config.StripDebug ||
39+
Config.StripNonAlloc || Config.StripSections || Config.StripUnneeded ||
40+
Config.DiscardMode != DiscardType::None || !Config.SymbolsToAdd.empty() ||
41+
Config.EntryExpr) {
4142
return createStringError(llvm::errc::invalid_argument,
4243
"option not supported by llvm-objcopy for MachO");
4344
}

‎llvm/tools/llvm-objcopy/ObjcopyOpts.td

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ defm add_section
7575
"Make a section named <section> with the contents of <file>.">,
7676
MetaVarName<"section=file">;
7777

78+
defm set_section_alignment
79+
: Eq<"set-section-alignment", "Set alignment for a given section.">,
80+
MetaVarName<"section=align">;
81+
7882
defm set_section_flags
7983
: Eq<"set-section-flags",
8084
"Set section flags for a given section. Flags supported for GNU "

0 commit comments

Comments
 (0)
Please sign in to comment.