Index: docs/CommandGuide/llvm-objcopy.rst =================================================================== --- docs/CommandGuide/llvm-objcopy.rst +++ docs/CommandGuide/llvm-objcopy.rst @@ -82,6 +82,11 @@ Remove the specified section from the output. Can be specified multiple times to remove multiple sections simultaneously. +.. option:: --set-section-alignment
= + + Set the alignment of section ``
`` to ```. 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 Index: test/tools/llvm-objcopy/ELF/binary-input.test =================================================================== --- test/tools/llvm-objcopy/ELF/binary-input.test +++ test/tools/llvm-objcopy/ELF/binary-input.test @@ -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-NOT: } +# ALIGN: AddressAlignment: 8 Index: test/tools/llvm-objcopy/ELF/set-section-alignment.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/ELF/set-section-alignment.test @@ -0,0 +1,39 @@ +# RUN: yaml2obj %s -o %t + +# RUN: llvm-objcopy --set-section-alignment .foo=4 --set-section-alignment=.bar=0x5 %t %t.2 +# RUN: llvm-readobj --sections %t.2 | FileCheck --check-prefix=CHECK %s + +# CHECK: Name: .foo +# CHECK-NOT: } +# CHECK: AddressAlignment: 4 +# CHECK: Name: .bar +# CHECK-NOT: } +# CHECK: AddressAlignment: 5 + +# 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=BAD-ALIGN %s +# BAD-ALIGN: error: bad format for --set-section-alignment: bad alignment: 'bar' + +# RUN: not llvm-objcopy --set-section-alignment .foo=4 --set-section-alignment=.foo=0 %t /dev/null 2>&1 | \ +# RUN: FileCheck --check-prefix=MULTIPLE %s +# MULTIPLE: error: --set-section-alignment set multiple times for section '.foo' + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_PROGBITS + - Name: .bar + Type: SHT_NOBITS Index: tools/llvm-objcopy/COFF/COFFObjcopy.cpp =================================================================== --- tools/llvm-objcopy/COFF/COFFObjcopy.cpp +++ tools/llvm-objcopy/COFF/COFFObjcopy.cpp @@ -208,10 +208,11 @@ !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, Index: tools/llvm-objcopy/CopyConfig.h =================================================================== --- tools/llvm-objcopy/CopyConfig.h +++ tools/llvm-objcopy/CopyConfig.h @@ -76,6 +76,11 @@ Optional NewFlags; }; +struct SectionAlignmentUpdate { + StringRef Name; + uint64_t NewAlign; +}; + struct SectionFlagsUpdate { StringRef Name; SectionFlag NewFlags; @@ -166,6 +171,7 @@ // Map options StringMap SectionsToRename; + StringMap SetSectionAlignment; StringMap SetSectionFlags; StringMap SymbolsToRename; Index: tools/llvm-objcopy/CopyConfig.cpp =================================================================== --- tools/llvm-objcopy/CopyConfig.cpp +++ tools/llvm-objcopy/CopyConfig.cpp @@ -155,6 +155,26 @@ return SR; } +static Expected> +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, + "bad format for --set-section-alignment: bad alignment: '%s'", + Split.second.str().c_str()); + return std::make_pair(Split.first, NewAlign); +} + static Expected parseSetSectionFlagValue(StringRef FlagValue) { if (!StringRef(FlagValue).contains('=')) @@ -581,6 +601,17 @@ "multiple renames of section '%s'", SR->OriginalName.str().c_str()); } + for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_alignment)) { + Expected> SAU = + parseSetSectionAlignment(Arg->getValue()); + if (!SAU) + return SAU.takeError(); + if (!Config.SetSectionAlignment.try_emplace(SAU->first, SAU->second).second) + return createStringError( + errc::invalid_argument, + "--set-section-alignment set multiple times for section '%s'", + SAU->first.str().c_str()); + } for (auto Arg : InputArgs.filtered(OBJCOPY_set_section_flags)) { Expected SFU = parseSetSectionFlagValue(Arg->getValue()); Index: tools/llvm-objcopy/ELF/ELFObjcopy.cpp =================================================================== --- tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -670,6 +670,14 @@ } } + 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); @@ -745,9 +753,13 @@ Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In, Buffer &Out) { + uint64_t DataAlign = 1; + auto It = Config.SetSectionAlignment.find(".data"); + if (It != Config.SetSectionAlignment.end()) + DataAlign = It->second; uint8_t NewSymbolVisibility = Config.NewSymbolVisibility.getValueOr((uint8_t)ELF::STV_DEFAULT); - BinaryReader Reader(&In, NewSymbolVisibility); + BinaryReader Reader(&In, DataAlign, NewSymbolVisibility); std::unique_ptr Obj = Reader.create(); // Prefer OutputArch (-O) if set, otherwise fallback to BinaryArch Index: tools/llvm-objcopy/ELF/Object.h =================================================================== --- tools/llvm-objcopy/ELF/Object.h +++ tools/llvm-objcopy/ELF/Object.h @@ -887,12 +887,14 @@ class BinaryELFBuilder : public BasicELFBuilder { MemoryBuffer *MemBuf; + uint64_t DataAlign; uint8_t NewSymbolVisibility; void addData(SymbolTableSection *SymTab); public: - BinaryELFBuilder(MemoryBuffer *MB, uint8_t NewSymbolVisibility) - : BasicELFBuilder(), MemBuf(MB), + BinaryELFBuilder(MemoryBuffer *MB, uint64_t DataAlign, + uint8_t NewSymbolVisibility) + : BasicELFBuilder(), MemBuf(MB), DataAlign(DataAlign), NewSymbolVisibility(NewSymbolVisibility) {} std::unique_ptr build(); @@ -941,11 +943,14 @@ class BinaryReader : public Reader { MemoryBuffer *MemBuf; + uint64_t DataAlign; uint8_t NewSymbolVisibility; public: - BinaryReader(MemoryBuffer *MB, const uint8_t NewSymbolVisibility) - : MemBuf(MB), NewSymbolVisibility(NewSymbolVisibility) {} + BinaryReader(MemoryBuffer *MB, uint64_t DataAlign, + uint8_t NewSymbolVisibility) + : MemBuf(MB), DataAlign(DataAlign), + NewSymbolVisibility(NewSymbolVisibility) {} std::unique_ptr create() const override; }; Index: tools/llvm-objcopy/ELF/Object.cpp =================================================================== --- tools/llvm-objcopy/ELF/Object.cpp +++ tools/llvm-objcopy/ELF/Object.cpp @@ -1153,6 +1153,7 @@ DataSection.Type = ELF::SHT_PROGBITS; DataSection.Size = Data.size(); DataSection.Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; + DataSection.Align = DataAlign; std::string SanitizedFilename = MemBuf->getBufferIdentifier().str(); std::replace_if(std::begin(SanitizedFilename), std::end(SanitizedFilename), @@ -1606,7 +1607,7 @@ Reader::~Reader() {} std::unique_ptr BinaryReader::create() const { - return BinaryELFBuilder(MemBuf, NewSymbolVisibility).build(); + return BinaryELFBuilder(MemBuf, DataAlign, NewSymbolVisibility).build(); } Expected> IHexReader::parse() const { Index: tools/llvm-objcopy/MachO/MachOObjcopy.cpp =================================================================== --- tools/llvm-objcopy/MachO/MachOObjcopy.cpp +++ tools/llvm-objcopy/MachO/MachOObjcopy.cpp @@ -31,13 +31,14 @@ !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"); } Index: tools/llvm-objcopy/ObjcopyOpts.td =================================================================== --- tools/llvm-objcopy/ObjcopyOpts.td +++ tools/llvm-objcopy/ObjcopyOpts.td @@ -75,6 +75,10 @@ "Make a section named
with the contents of .">, 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 "