[llvm] r373461 - [llvm-objcopy] Add --set-section-alignment
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 2 05:41:25 PDT 2019
Author: maskray
Date: Wed Oct 2 05:41:25 2019
New Revision: 373461
URL: http://llvm.org/viewvc/llvm-project?rev=373461&view=rev
Log:
[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
Added:
llvm/trunk/test/tools/llvm-objcopy/ELF/set-section-alignment.test
Modified:
llvm/trunk/docs/CommandGuide/llvm-objcopy.rst
llvm/trunk/test/tools/llvm-objcopy/ELF/binary-input.test
llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp
llvm/trunk/tools/llvm-objcopy/CopyConfig.h
llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
llvm/trunk/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td
Modified: llvm/trunk/docs/CommandGuide/llvm-objcopy.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/CommandGuide/llvm-objcopy.rst?rev=373461&r1=373460&r2=373461&view=diff
==============================================================================
--- llvm/trunk/docs/CommandGuide/llvm-objcopy.rst (original)
+++ llvm/trunk/docs/CommandGuide/llvm-objcopy.rst Wed Oct 2 05:41:25 2019
@@ -82,6 +82,11 @@ multiple file formats.
Remove the specified section from the output. Can be specified multiple times
to remove multiple sections simultaneously.
+.. option:: --set-section-alignment <section>=<align>
+
+ Set the alignment of section ``<section>`` to `<align>``. Can be specified
+ multiple times to update multiple sections.
+
.. option:: --strip-all-gnu
Remove all symbols, debug sections and relocations from the output. This option
Modified: llvm/trunk/test/tools/llvm-objcopy/ELF/binary-input.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/ELF/binary-input.test?rev=373461&r1=373460&r2=373461&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/ELF/binary-input.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/ELF/binary-input.test Wed Oct 2 05:41:25 2019
@@ -110,3 +110,11 @@
# CHECK-NEXT: Section: Absolute
# CHECK-NEXT: }
# CHECK-NEXT: ]
+
+## The alignment can be changed by --set-section-alignment.
+# RUN: llvm-objcopy -I binary -O elf64-x86-64 --set-section-alignment .data=8 %t.x-txt %t2.o
+# RUN: llvm-readobj --sections %t2.o | FileCheck --check-prefix=ALIGN %s
+
+# ALIGN: Name: .data
+# ALIGN: AddressAlignment:
+# ALIGN-SAME: 8{{$}}
Added: llvm/trunk/test/tools/llvm-objcopy/ELF/set-section-alignment.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/ELF/set-section-alignment.test?rev=373461&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/ELF/set-section-alignment.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/ELF/set-section-alignment.test Wed Oct 2 05:41:25 2019
@@ -0,0 +1,54 @@
+# RUN: yaml2obj %s -o %t
+
+# RUN: llvm-objcopy --set-section-alignment .foo=4 --set-section-alignment .bar=0x5 \
+# RUN: --set-section-alignment .baz=0 %t %t.2
+# RUN: llvm-readobj --sections %t.2 | FileCheck --check-prefix=CHECK %s
+
+# CHECK: Name: .foo
+# CHECK: AddressAlignment:
+# CHECK-SAME: 4{{$}}
+# CHECK: Name: .bar
+# CHECK: AddressAlignment:
+# CHECK-SAME: 5{{$}}
+# CHECK: Name: .baz
+# CHECK: AddressAlignment:
+# CHECK-SAME: 0{{$}}
+
+## If a section is specified multiple times, the last wins.
+# RUN: llvm-objcopy --set-section-alignment .foo=4 --set-section-alignment=.foo=7 %t %t.3
+# RUN: llvm-readobj --sections %t.3 | FileCheck --check-prefix=MULTI %s
+
+# MULTI: Name: .foo
+# MULTI: AddressAlignment:
+# MULTI-SAME: 7{{$}}
+
+## Ignore the option if the section does not exist.
+# RUN: llvm-objcopy --set-section-alignment .not_exist=4 %t.3 %t.4
+# RUN: cmp %t.3 %t.4
+
+# RUN: not llvm-objcopy --set-section-alignment=.foo %t /dev/null 2>&1 | \
+# RUN: FileCheck --check-prefix=MISSING-EQUAL %s
+# MISSING-EQUAL: error: bad format for --set-section-alignment: missing '='
+
+# RUN: not llvm-objcopy --set-section-alignment==4 %t /dev/null 2>&1 | \
+# RUN: FileCheck --check-prefix=MISSING-SECTION %s
+# MISSING-SECTION: error: bad format for --set-section-alignment: missing section name
+
+# RUN: not llvm-objcopy --set-section-alignment=.foo=bar %t /dev/null 2>&1 | \
+# RUN: FileCheck --check-prefix=INVALID-ALIGN %s
+# INVALID-ALIGN: error: invalid alignment for --set-section-alignment: 'bar'
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .foo
+ Type: SHT_PROGBITS
+ - Name: .bar
+ Type: SHT_NOBITS
+ - Name: .baz
+ Type: SHT_NOTE
+ AddressAlign: 4
Modified: llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp?rev=373461&r1=373460&r2=373461&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp Wed Oct 2 05:41:25 2019
@@ -208,10 +208,11 @@ static Error handleArgs(const CopyConfig
!Config.SymbolsToGlobalize.empty() || !Config.SymbolsToKeep.empty() ||
!Config.SymbolsToLocalize.empty() || !Config.SymbolsToWeaken.empty() ||
!Config.SymbolsToKeepGlobal.empty() || !Config.SectionsToRename.empty() ||
- !Config.SetSectionFlags.empty() || !Config.SymbolsToRename.empty() ||
- Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden ||
- Config.PreserveDates || Config.StripDWO || Config.StripNonAlloc ||
- Config.StripSections || Config.Weaken || Config.DecompressDebugSections ||
+ !Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() ||
+ !Config.SymbolsToRename.empty() || Config.ExtractDWO ||
+ Config.KeepFileSymbols || Config.LocalizeHidden || Config.PreserveDates ||
+ Config.StripDWO || Config.StripNonAlloc || Config.StripSections ||
+ Config.Weaken || Config.DecompressDebugSections ||
Config.DiscardMode == DiscardType::Locals ||
!Config.SymbolsToAdd.empty() || Config.EntryExpr) {
return createStringError(llvm::errc::invalid_argument,
Modified: llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp?rev=373461&r1=373460&r2=373461&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp Wed Oct 2 05:41:25 2019
@@ -155,6 +155,25 @@ static Expected<SectionRename> parseRena
return SR;
}
+static Expected<std::pair<StringRef, uint64_t>>
+parseSetSectionAlignment(StringRef FlagValue) {
+ if (!FlagValue.contains('='))
+ return createStringError(
+ errc::invalid_argument,
+ "bad format for --set-section-alignment: missing '='");
+ auto Split = StringRef(FlagValue).split('=');
+ if (Split.first.empty())
+ return createStringError(
+ errc::invalid_argument,
+ "bad format for --set-section-alignment: missing section name");
+ uint64_t NewAlign;
+ if (Split.second.getAsInteger(0, NewAlign))
+ return createStringError(errc::invalid_argument,
+ "invalid alignment for --set-section-alignment: '%s'",
+ Split.second.str().c_str());
+ return std::make_pair(Split.first, NewAlign);
+}
+
static Expected<SectionFlagsUpdate>
parseSetSectionFlagValue(StringRef FlagValue) {
if (!StringRef(FlagValue).contains('='))
@@ -489,6 +508,13 @@ Expected<DriverConfig> parseObjcopyOptio
"multiple renames of section '%s'",
SR->OriginalName.str().c_str());
}
+ for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_alignment)) {
+ Expected<std::pair<StringRef, uint64_t>> NameAndAlign =
+ parseSetSectionAlignment(Arg->getValue());
+ if (!NameAndAlign)
+ return NameAndAlign.takeError();
+ Config.SetSectionAlignment[NameAndAlign->first] = NameAndAlign->second;
+ }
for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) {
Expected<SectionFlagsUpdate> SFU =
parseSetSectionFlagValue(Arg->getValue());
Modified: llvm/trunk/tools/llvm-objcopy/CopyConfig.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/CopyConfig.h?rev=373461&r1=373460&r2=373461&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/CopyConfig.h (original)
+++ llvm/trunk/tools/llvm-objcopy/CopyConfig.h Wed Oct 2 05:41:25 2019
@@ -161,6 +161,7 @@ struct CopyConfig {
// Map options
StringMap<SectionRename> SectionsToRename;
+ StringMap<uint64_t> SetSectionAlignment;
StringMap<SectionFlagsUpdate> SetSectionFlags;
StringMap<StringRef> SymbolsToRename;
Modified: llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp?rev=373461&r1=373460&r2=373461&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp Wed Oct 2 05:41:25 2019
@@ -670,6 +670,14 @@ static Error handleArgs(const CopyConfig
}
}
+ if (!Config.SetSectionAlignment.empty()) {
+ for (SectionBase &Sec : Obj.sections()) {
+ auto I = Config.SetSectionAlignment.find(Sec.Name);
+ if (I != Config.SetSectionAlignment.end())
+ Sec.Align = I->second;
+ }
+ }
+
if (!Config.SetSectionFlags.empty()) {
for (auto &Sec : Obj.sections()) {
const auto Iter = Config.SetSectionFlags.find(Sec.Name);
Modified: llvm/trunk/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/MachO/MachOObjcopy.cpp?rev=373461&r1=373460&r2=373461&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/MachO/MachOObjcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/MachO/MachOObjcopy.cpp Wed Oct 2 05:41:25 2019
@@ -31,13 +31,14 @@ static Error handleArgs(const CopyConfig
!Config.SymbolsToKeepGlobal.empty() || !Config.SectionsToRename.empty() ||
!Config.SymbolsToRename.empty() ||
!Config.UnneededSymbolsToRemove.empty() ||
- !Config.SetSectionFlags.empty() || !Config.ToRemove.empty() ||
- Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden ||
- Config.PreserveDates || Config.StripDWO || Config.StripNonAlloc ||
- Config.StripSections || Config.Weaken || Config.DecompressDebugSections ||
- Config.StripDebug || Config.StripNonAlloc || Config.StripSections ||
- Config.StripUnneeded || Config.DiscardMode != DiscardType::None ||
- !Config.SymbolsToAdd.empty() || Config.EntryExpr) {
+ !Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() ||
+ !Config.ToRemove.empty() || Config.ExtractDWO || Config.KeepFileSymbols ||
+ Config.LocalizeHidden || Config.PreserveDates || Config.StripDWO ||
+ Config.StripNonAlloc || Config.StripSections || Config.Weaken ||
+ Config.DecompressDebugSections || Config.StripDebug ||
+ Config.StripNonAlloc || Config.StripSections || Config.StripUnneeded ||
+ Config.DiscardMode != DiscardType::None || !Config.SymbolsToAdd.empty() ||
+ Config.EntryExpr) {
return createStringError(llvm::errc::invalid_argument,
"option not supported by llvm-objcopy for MachO");
}
Modified: llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td?rev=373461&r1=373460&r2=373461&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td (original)
+++ llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td Wed Oct 2 05:41:25 2019
@@ -75,6 +75,10 @@ defm add_section
"Make a section named <section> with the contents of <file>.">,
MetaVarName<"section=file">;
+defm set_section_alignment
+ : Eq<"set-section-alignment", "Set alignment for a given section.">,
+ MetaVarName<"section=align">;
+
defm set_section_flags
: Eq<"set-section-flags",
"Set section flags for a given section. Flags supported for GNU "
More information about the llvm-commits
mailing list