Skip to content

[llvm-objcopy] Add --gap-fill and --pad-to options #65815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/docs/CommandGuide/llvm-objcopy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ them.

Extract the named partition from the output.

.. option:: --gap-fill <value>

For binary outputs, fill the gaps between sections with ``<value>`` instead
of zero. The value must be an unsigned 8-bit integer.

.. option:: --globalize-symbol <symbol>

Mark any defined symbols named ``<symbol>`` as global symbols in the output.
Expand Down Expand Up @@ -411,6 +416,11 @@ them.
be the same as the value specified for :option:`--input-target` or the input
file's format if that option is also unspecified.

.. option:: --pad-to <address>

For binary outputs, pad the output to the load address ``<address>`` using a value
of zero or the value specified by :option:`--gap-fill`.

.. option:: --prefix-alloc-sections <prefix>

Add ``<prefix>`` to the front of the names of all allocatable sections in the
Expand Down
3 changes: 3 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ Changes to the LLVM tools

* llvm-symbolizer and llvm-addr2line now support addresses specified as symbol names.

* llvm-objcopy now supports ``--gap-fill`` and ``--pad-to`` options, for
ELF input and binary output files only.

Changes to LLDB
---------------------------------

Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/ObjCopy/CommonConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ struct CommonConfig {
// Cached gnu_debuglink's target CRC
uint32_t GnuDebugLinkCRC32;
std::optional<StringRef> ExtractPartition;
uint8_t GapFill = 0;
uint64_t PadTo = 0;
StringRef SplitDWO;
StringRef SymbolsPrefix;
StringRef AllocSectionsPrefix;
Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/ObjCopy/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Expected<const COFFConfig &> ConfigManager::getCOFFConfig() const {
Common.ExtractDWO || Common.PreserveDates || Common.StripDWO ||
Common.StripNonAlloc || Common.StripSections || Common.Weaken ||
Common.DecompressDebugSections ||
Common.DiscardMode == DiscardType::Locals || !Common.SymbolsToAdd.empty())
Common.DiscardMode == DiscardType::Locals ||
!Common.SymbolsToAdd.empty() || Common.GapFill != 0 || Common.PadTo != 0)
return createStringError(llvm::errc::invalid_argument,
"option is not supported for COFF");

Expand All @@ -42,7 +43,8 @@ Expected<const MachOConfig &> ConfigManager::getMachOConfig() const {
Common.PreserveDates || Common.StripAllGNU || Common.StripDWO ||
Common.StripNonAlloc || Common.StripSections ||
Common.DecompressDebugSections || Common.StripUnneeded ||
Common.DiscardMode == DiscardType::Locals || !Common.SymbolsToAdd.empty())
Common.DiscardMode == DiscardType::Locals ||
!Common.SymbolsToAdd.empty() || Common.GapFill != 0 || Common.PadTo != 0)
return createStringError(llvm::errc::invalid_argument,
"option is not supported for MachO");

Expand All @@ -60,7 +62,8 @@ Expected<const WasmConfig &> ConfigManager::getWasmConfig() const {
!Common.SymbolsToWeaken.empty() || !Common.SymbolsToKeepGlobal.empty() ||
!Common.SectionsToRename.empty() || !Common.SetSectionAlignment.empty() ||
!Common.SetSectionFlags.empty() || !Common.SetSectionType.empty() ||
!Common.SymbolsToRename.empty())
!Common.SymbolsToRename.empty() || Common.GapFill != 0 ||
Common.PadTo != 0)
return createStringError(llvm::errc::invalid_argument,
"only flags for section dumping, removal, and "
"addition are supported");
Expand All @@ -86,7 +89,8 @@ Expected<const XCOFFConfig &> ConfigManager::getXCOFFConfig() const {
Common.ExtractMainPartition || Common.OnlyKeepDebug ||
Common.PreserveDates || Common.StripAllGNU || Common.StripDWO ||
Common.StripDebug || Common.StripNonAlloc || Common.StripSections ||
Common.Weaken || Common.StripUnneeded || Common.DecompressDebugSections) {
Common.Weaken || Common.StripUnneeded || Common.DecompressDebugSections ||
Common.GapFill != 0 || Common.PadTo != 0) {
return createStringError(
llvm::errc::invalid_argument,
"no flags are supported yet, only basic copying is allowed");
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static std::unique_ptr<Writer> createWriter(const CommonConfig &Config,
ElfType OutputElfType) {
switch (Config.OutputFormat) {
case FileFormat::Binary:
return std::make_unique<BinaryWriter>(Obj, Out);
return std::make_unique<BinaryWriter>(Obj, Out, Config);
case FileFormat::IHex:
return std::make_unique<IHexWriter>(Obj, Out);
default:
Expand Down
31 changes: 29 additions & 2 deletions llvm/lib/ObjCopy/ELF/ELFObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2636,9 +2636,36 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
}

Error BinaryWriter::write() {
for (const SectionBase &Sec : Obj.allocSections())
SmallVector<const SectionBase *, 30> SectionsToWrite;
for (const SectionBase &Sec : Obj.allocSections()) {
if (Sec.Type != SHT_NOBITS)
SectionsToWrite.push_back(&Sec);
}

if (SectionsToWrite.empty())
return Error::success();

llvm::stable_sort(SectionsToWrite,
[](const SectionBase *LHS, const SectionBase *RHS) {
return LHS->Offset < RHS->Offset;
});

assert(SectionsToWrite.front()->Offset == 0);

for (size_t i = 0; i != SectionsToWrite.size(); ++i) {
const SectionBase &Sec = *SectionsToWrite[i];
if (Error Err = Sec.accept(*SecWriter))
return Err;
if (GapFill == 0)
continue;
uint64_t PadOffset = (i < SectionsToWrite.size() - 1)
? SectionsToWrite[i + 1]->Offset
: Buf->getBufferSize();
assert(PadOffset <= Buf->getBufferSize());
assert(Sec.Offset + Sec.Size <= PadOffset);
std::fill(Buf->getBufferStart() + Sec.Offset + Sec.Size,
Buf->getBufferStart() + PadOffset, GapFill);
}

// TODO: Implement direct writing to the output stream (without intermediate
// memory buffer Buf).
Expand All @@ -2664,7 +2691,7 @@ Error BinaryWriter::finalize() {
// file size. This might not be the same as the offset returned by
// layoutSections, because we want to truncate the last segment to the end of
// its last non-empty section, to match GNU objcopy's behaviour.
TotalSize = 0;
TotalSize = PadTo > MinAddr ? PadTo - MinAddr : 0;
for (SectionBase &Sec : Obj.allocSections())
if (Sec.Type != SHT_NOBITS && Sec.Size > 0) {
Sec.Offset = Sec.Addr - MinAddr;
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/ObjCopy/ELF/ELFObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ template <class ELFT> class ELFWriter : public Writer {

class BinaryWriter : public Writer {
private:
const uint8_t GapFill;
const uint64_t PadTo;
std::unique_ptr<BinarySectionWriter> SecWriter;

uint64_t TotalSize = 0;
Expand All @@ -365,7 +367,8 @@ class BinaryWriter : public Writer {
~BinaryWriter() {}
Error finalize() override;
Error write() override;
BinaryWriter(Object &Obj, raw_ostream &Out) : Writer(Obj, Out) {}
BinaryWriter(Object &Obj, raw_ostream &Out, const CommonConfig &Config)
: Writer(Obj, Out), GapFill(Config.GapFill), PadTo(Config.PadTo) {}
};

class IHexWriter : public Writer {
Expand Down
176 changes: 176 additions & 0 deletions llvm/test/tools/llvm-objcopy/ELF/gap-fill.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# RUN: yaml2obj --docnum=1 %s -o %t

# RUN: not llvm-objcopy --gap-fill 1 %t 2>&1 | FileCheck %s --check-prefix=NOT-BINARY
# NOT-BINARY: error: '--gap-fill' is only supported for binary output

# RUN: not llvm-objcopy -O binary --gap-fill= %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-FORMAT
# BAD-FORMAT: error: --gap-fill: bad number:

# RUN: not llvm-objcopy -O binary --gap-fill=x %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT
# BAD-INPUT: error: --gap-fill: bad number: x

# RUN: not llvm-objcopy -O binary --gap-fill=0x %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT2
# BAD-INPUT2: error: --gap-fill: bad number: 0x

# RUN: not llvm-objcopy -O binary --gap-fill=0x1G %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT3
# BAD-INPUT3: error: --gap-fill: bad number: 0x1G

# RUN: not llvm-objcopy -O binary --gap-fill=ff %t %t.bin 2>&1 | FileCheck %s --check-prefix=BAD-INPUT4
# BAD-INPUT4: error: --gap-fill: bad number: ff

# RUN: not llvm-objcopy -O binary --gap-fill=0x1122 %t %t-val16 2>&1 | FileCheck %s --check-prefix=TRUNCATED-ERR
# TRUNCATED-ERR: error: gap-fill value 0x1122 is out of range (0 to 0xff)

## Test no gap fill with all allocatable output sections.
# RUN: llvm-objcopy -O binary %t %t-default
# RUN: od -v -Ax -t x1 %t-default | FileCheck %s --check-prefix=DEFAULT --match-full-lines
# DEFAULT: 000000 ee ff 11 22 33 44 aa bb cc dd fe dc ba 00 a1 b2
# DEFAULT-NEXT: 000010 c3 d4 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: 000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: 000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: 000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: 000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: 000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: 000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# DEFAULT-NEXT: 000080 00 00 89 ab cd ef
# DEFAULT-NEXT: 000086

## Test gap fill with all allocatable output sections.
# RUN: llvm-objcopy -O binary --gap-fill=0xe9 %t %t-filled
# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=FULL --match-full-lines
# FULL: 000000 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 a1 b2
# FULL-NEXT: 000010 c3 d4 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: 000020 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: 000030 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: 000040 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: 000050 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: 000060 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: 000070 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# FULL-NEXT: 000080 e9 e9 89 ab cd ef
# FULL-NEXT: 000086

## Test gap fill with a decimal value.
# RUN: llvm-objcopy -O binary --gap-fill=99 %t %t-filled-decimal
# RUN: od -v -Ax -t x1 %t-filled-decimal | FileCheck %s --check-prefix=DEC --match-full-lines
# DEC: 000000 ee ff 11 22 33 44 aa bb cc dd fe dc ba 63 a1 b2
# DEC-NEXT: 000010 c3 d4 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: 000020 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: 000030 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: 000040 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: 000050 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: 000060 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: 000070 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
# DEC-NEXT: 000080 63 63 89 ab cd ef
# DEC-NEXT: 000086

## Test gap fill with the last section removed, should be truncated.
# RUN: llvm-objcopy -O binary --gap-fill=0xe9 --remove-section=.foo %t %t-filled
# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=REMOVE-LAST-SECTION --match-full-lines
# REMOVE-LAST-SECTION: 000000 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 a1 b2
# REMOVE-LAST-SECTION-NEXT: 000010 c3 d4
# REMOVE-LAST-SECTION-NEXT: 000012

## Test gap fill with the middle section removed, should be filled.
# RUN: llvm-objcopy -O binary --gap-fill=0xe9 --remove-section=.gap2 %t %t-filled
# RUN: od -v -Ax -t x1 %t-filled | FileCheck %s --check-prefix=REMOVE-MIDDLE-SECTION --match-full-lines
# REMOVE-MIDDLE-SECTION: 000000 ee ff 11 22 33 44 aa bb cc dd fe dc ba e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: 000010 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: 000020 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: 000030 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: 000040 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: 000050 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: 000060 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: 000070 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9 e9
# REMOVE-MIDDLE-SECTION-NEXT: 000080 e9 e9 89 ab cd ef
# REMOVE-MIDDLE-SECTION-NEXT: 000086

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64
Sections:
- Name: .space1
Type: Fill
Pattern: 'ABCD'
Size: 0x2
- Name: .nogap
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ]
Address: 0x0102
Size: 0x6
Content: 'EEFF11223344'
- Name: .gap1
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Address: 0x0108
Content: 'AABBCCDDFEDCBA'
- Name: .space2
Type: Fill
Pattern: 'DC'
Size: 1
- Name: .gap2
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ]
Address: 0x0110
Content: 'A1B2C3D4'
- Name: .space3
Type: Fill
Pattern: 'FE'
Size: 0x1
- Name: .nobit_tbss
Type: SHT_NOBITS
Flags: [ SHF_WRITE, SHF_ALLOC, SHF_TLS ]
Address: 0x0180
Size: 0x0018
- Name: .space4
Type: Fill
Pattern: '01234567'
Size: 0x4
- Name: .foo
Type: SHT_PROGBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
Address: 0x0184
Content: '89ABCDEF'
- Name: .nobit_bss
Type: SHT_NOBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
Address: 0x018A
Size: 0x0008
- Name: .comment
Type: SHT_PROGBITS
Flags: [ SHF_MERGE, SHF_STRINGS ]
EntSize: 0x0001
Content: 4743433A

## In this test, output sections are defined out of order with respect to their
## load addresses. Verify that gaps are still correctly filled.

# RUN: yaml2obj --docnum=2 %s -o %t.2
# RUN: llvm-objcopy -O binary --gap-fill=0xe9 %t.2 %t.2.filled
# RUN: od -v -Ax -t x1 %t.2.filled | FileCheck --match-full-lines %s
# CHECK: 000000 aa bb cc dd e9 e9 e9 e9 11 22 33 44

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64
Sections:
- Name: .bss
Type: SHT_NOBITS
Flags: [ SHF_ALLOC, SHF_WRITE ]
Address: 0x0104
Size: 4
- Name: .section1
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_WRITE ]
Address: 0x0108
Content: '11223344'
- Name: .section3
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_WRITE ]
Address: 0x0100
Content: 'AABBCCDD'
Loading