Skip to content

Commit

Permalink
[llvm-objcopy][ELF] Disable huge section offset
Browse files Browse the repository at this point in the history
By default, GNU objcopy issues a warning for large offsets in ELF
files. To align with this behavior, we introduce an option to
define a maximum file offset for a section. This allows us to skip
creating excessively large files by specifying a threshold beyond
which the file offset will trigger an error.
  • Loading branch information
djtodoro authored and Djordje Todorovic committed Sep 10, 2024
1 parent b11a703 commit ef2c3ba
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 9 deletions.
1 change: 1 addition & 0 deletions llvm/include/llvm/ObjCopy/ELF/ELFConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct ELFConfig {
bool KeepFileSymbols = false;
bool LocalizeHidden = false;
bool VerifyNoteSections = true;
std::optional<uint64_t> MaxHugeSectionOffset;
};

} // namespace objcopy
Expand Down
15 changes: 8 additions & 7 deletions llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ static std::unique_ptr<Writer> createELFWriter(const CommonConfig &Config,
}

static std::unique_ptr<Writer> createWriter(const CommonConfig &Config,
const ELFConfig &ELFConfig,
Object &Obj, raw_ostream &Out,
ElfType OutputElfType) {
switch (Config.OutputFormat) {
case FileFormat::Binary:
return std::make_unique<BinaryWriter>(Obj, Out, Config);
return std::make_unique<BinaryWriter>(Obj, Out, Config, ELFConfig);
case FileFormat::IHex:
return std::make_unique<IHexWriter>(Obj, Out, Config.OutputFilename);
case FileFormat::SREC:
Expand Down Expand Up @@ -926,10 +927,10 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
return Error::success();
}

static Error writeOutput(const CommonConfig &Config, Object &Obj,
raw_ostream &Out, ElfType OutputElfType) {
static Error writeOutput(const CommonConfig &Config, const ELFConfig &ELFConfig,
Object &Obj, raw_ostream &Out, ElfType OutputElfType) {
std::unique_ptr<Writer> Writer =
createWriter(Config, Obj, Out, OutputElfType);
createWriter(Config, ELFConfig, Obj, Out, OutputElfType);
if (Error E = Writer->finalize())
return E;
return Writer->write();
Expand All @@ -947,7 +948,7 @@ Error objcopy::elf::executeObjcopyOnIHex(const CommonConfig &Config,
getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj))
return E;
return writeOutput(Config, **Obj, Out, OutputElfType);
return writeOutput(Config, ELFConfig, **Obj, Out, OutputElfType);
}

Error objcopy::elf::executeObjcopyOnRawBinary(const CommonConfig &Config,
Expand All @@ -965,7 +966,7 @@ Error objcopy::elf::executeObjcopyOnRawBinary(const CommonConfig &Config,
getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj))
return E;
return writeOutput(Config, **Obj, Out, OutputElfType);
return writeOutput(Config, ELFConfig, **Obj, Out, OutputElfType);
}

Error objcopy::elf::executeObjcopyOnBinary(const CommonConfig &Config,
Expand All @@ -985,7 +986,7 @@ Error objcopy::elf::executeObjcopyOnBinary(const CommonConfig &Config,
if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj))
return createFileError(Config.InputFilename, std::move(E));

if (Error E = writeOutput(Config, **Obj, Out, OutputElfType))
if (Error E = writeOutput(Config, ELFConfig, **Obj, Out, OutputElfType))
return createFileError(Config.InputFilename, std::move(E));

return Error::success();
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/ObjCopy/ELF/ELFObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,13 @@ Error BinaryWriter::finalize() {
if (Sec.Type != SHT_NOBITS && Sec.Size > 0) {
Sec.Offset = Sec.Addr - MinAddr;
TotalSize = std::max(TotalSize, Sec.Offset + Sec.Size);

if (MaxHugeSectionOffset) {
if (Sec.Offset > *MaxHugeSectionOffset)
return createStringError(errc::file_too_large,
"writing section " + Sec.Name +
" at huge file offset");
}
}

Buf = WritableMemoryBuffer::getNewMemBuffer(TotalSize);
Expand Down
9 changes: 7 additions & 2 deletions llvm/lib/ObjCopy/ELF/ELFObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/ObjCopy/CommonConfig.h"
#include "llvm/ObjCopy/ELF/ELFConfig.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileOutputBuffer.h"
Expand Down Expand Up @@ -366,12 +367,16 @@ class BinaryWriter : public Writer {

uint64_t TotalSize = 0;

std::optional<uint64_t> MaxHugeSectionOffset;

public:
~BinaryWriter() {}
Error finalize() override;
Error write() override;
BinaryWriter(Object &Obj, raw_ostream &Out, const CommonConfig &Config)
: Writer(Obj, Out), GapFill(Config.GapFill), PadTo(Config.PadTo) {}
BinaryWriter(Object &Obj, raw_ostream &Out, const CommonConfig &Config,
const ELFConfig &ELFConfig)
: Writer(Obj, Out), GapFill(Config.GapFill), PadTo(Config.PadTo),
MaxHugeSectionOffset(ELFConfig.MaxHugeSectionOffset) {}
};

// A base class for writing ascii hex formats such as srec and ihex.
Expand Down
27 changes: 27 additions & 0 deletions llvm/test/tools/llvm-objcopy/ELF/disable-huge-section-offset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# RUN: yaml2obj %s --docnum=1 -o %t
# RUN: not llvm-objcopy -O binary %t %t2 --set-max-huge-section-offset=2000000000 2>&1 | FileCheck %s

# CHECK: writing section .high_addr at huge file offset

--- !ELF
FileHeader:
Class: ELFCLASS32
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_MIPS
Sections:
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Address: 0x1000
Content: "00112233445566778899AABBCCDDEEFF"
- Name: .data
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_WRITE ]
Address: 0x2000
Content: "112233445566778899AABBCCDDEEFF00"
- Name: .high_addr
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_WRITE ]
Address: 0x80001000
Content: "2233445566778899AABBCCDDEEFF0011"
5 changes: 5 additions & 0 deletions llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,11 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
Config.ExtractMainPartition =
InputArgs.hasArg(OBJCOPY_extract_main_partition);
ELFConfig.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);

if (auto *Arg = InputArgs.getLastArg(OBJCOPY_set_max_huge_section_offset)) {
ELFConfig.MaxHugeSectionOffset = std::stoull(Arg->getValue());
}

Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
if (auto *Arg =
InputArgs.getLastArg(OBJCOPY_discard_all, OBJCOPY_discard_locals)) {
Expand Down
6 changes: 6 additions & 0 deletions llvm/tools/llvm-objcopy/ObjcopyOpts.td
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ def extract_main_partition
: Flag<["--"], "extract-main-partition">,
HelpText<"Extract main partition from the input file">;

defm set_max_huge_section_offset
: Eq<"set-max-huge-section-offset",
"Emit an error if input section has a file offset greater than the "
"specified `offset`">,
MetaVarName<"offset">;

def localize_hidden
: Flag<["--"], "localize-hidden">,
HelpText<
Expand Down

0 comments on commit ef2c3ba

Please sign in to comment.