Skip to content

[llvm-objcopy] Add change-section-lma *+/-offset #95431

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 8 commits into from
Jul 8, 2024
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
4 changes: 4 additions & 0 deletions llvm/docs/CommandGuide/llvm-objcopy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ them.
Allow :program:`llvm-objcopy` to remove sections even if it would leave invalid
section references. Any invalid sh_link fields will be set to zero.

.. option:: --change-section-lma \*{+-}<val>

Shift LMA of non-zero-sized segments by ``<val>``.

.. option:: --change-start <incr>, --adjust-start

Add ``<incr>`` to the program's start address. Can be specified multiple
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/ObjCopy/CommonConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ struct CommonConfig {
// Symbol info specified by --add-symbol option.
SmallVector<NewSymbolInfo, 0> SymbolsToAdd;

// Integer options
int64_t ChangeSectionLMAValAll = 0;

// Boolean options
bool DeterministicArchives = true;
bool ExtractDWO = false;
Expand Down
11 changes: 7 additions & 4 deletions llvm/lib/ObjCopy/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Expected<const COFFConfig &> ConfigManager::getCOFFConfig() const {
Common.StripNonAlloc || Common.StripSections || Common.Weaken ||
Common.DecompressDebugSections ||
Common.DiscardMode == DiscardType::Locals ||
!Common.SymbolsToAdd.empty() || Common.GapFill != 0 || Common.PadTo != 0)
!Common.SymbolsToAdd.empty() || Common.GapFill != 0 ||
Common.PadTo != 0 || Common.ChangeSectionLMAValAll != 0)
return createStringError(llvm::errc::invalid_argument,
"option is not supported for COFF");

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

Expand All @@ -66,7 +68,7 @@ Expected<const WasmConfig &> ConfigManager::getWasmConfig() const {
!Common.SectionsToRename.empty() || !Common.SetSectionAlignment.empty() ||
!Common.SetSectionFlags.empty() || !Common.SetSectionType.empty() ||
!Common.SymbolsToRename.empty() || Common.GapFill != 0 ||
Common.PadTo != 0)
Common.PadTo != 0 || Common.ChangeSectionLMAValAll != 0)
return createStringError(llvm::errc::invalid_argument,
"only flags for section dumping, removal, and "
"addition are supported");
Expand Down Expand Up @@ -94,7 +96,8 @@ Expected<const XCOFFConfig &> ConfigManager::getXCOFFConfig() const {
Common.PreserveDates || Common.StripAllGNU || Common.StripDWO ||
Common.StripDebug || Common.StripNonAlloc || Common.StripSections ||
Common.Weaken || Common.StripUnneeded || Common.DecompressDebugSections ||
Common.GapFill != 0 || Common.PadTo != 0) {
Common.GapFill != 0 || Common.PadTo != 0 ||
Common.ChangeSectionLMAValAll != 0) {
return createStringError(
llvm::errc::invalid_argument,
"no flags are supported yet, only basic copying is allowed");
Expand Down
27 changes: 27 additions & 0 deletions llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,33 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
}
}

if (Config.ChangeSectionLMAValAll != 0) {
for (Segment &Seg : Obj.segments()) {
if (Seg.FileSize > 0) {
if (Config.ChangeSectionLMAValAll > 0 &&
Seg.PAddr > std::numeric_limits<uint64_t>::max() -
Config.ChangeSectionLMAValAll) {
return createStringError(
errc::invalid_argument,
"address 0x" + Twine::utohexstr(Seg.PAddr) +
" cannot be increased by 0x" +
Twine::utohexstr(Config.ChangeSectionLMAValAll) +
". The result would overflow");
} else if (Config.ChangeSectionLMAValAll < 0 &&
Seg.PAddr < std::numeric_limits<uint64_t>::min() -
Config.ChangeSectionLMAValAll) {
return createStringError(
errc::invalid_argument,
"address 0x" + Twine::utohexstr(Seg.PAddr) +
" cannot be decreased by 0x" +
Twine::utohexstr(std::abs(Config.ChangeSectionLMAValAll)) +
". The result would underflow");
}
Seg.PAddr += Config.ChangeSectionLMAValAll;
}
}
}

if (Config.OnlyKeepDebug)
for (auto &Sec : Obj.sections())
if (Sec.Flags & SHF_ALLOC && Sec.Type != SHT_NOTE)
Expand Down
79 changes: 79 additions & 0 deletions llvm/test/tools/llvm-objcopy/ELF/change-section-lma.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# RUN: yaml2obj %s -o %t
# RUN: llvm-objcopy --change-section-lma *+0x20 %t %t2
# RUN: llvm-readelf --program-headers %t2 | FileCheck %s --check-prefix=CHECK-PLUS-PROGRAMS
# RUN: llvm-readelf --section-headers %t2 | FileCheck %s --check-prefix=CHECK-PLUS-SECTIONS
Comment on lines +3 to +4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could simplify this and the similar below down to a single call with both options specified.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the original version had them in one command but I "simplified" it by having two separate commands. I think it makes it clearly to see what is being checked.

# RUN: llvm-objcopy --change-section-lma *-0x30 %t %t3
# RUN: llvm-readelf --program-headers %t3 | FileCheck %s --check-prefix=CHECK-MINUS-PROGRAMS
# RUN: llvm-readelf --section-headers %t3 | FileCheck %s --check-prefix=CHECK-MINUS-SECTIONS
# RUN: not llvm-objcopy --change-section-lma .text3=0x5000 %t 2>&1 | FileCheck %s --check-prefix=ERR-SET-ADDRESS
# RUN: not llvm-objcopy --change-section-lma .text3+0x30 %t 2>&1 | FileCheck %s --check-prefix=ERR-SPECIFIC-SEC
# RUN: not llvm-objcopy --change-section-lma *+0c50 %t 2>&1 | FileCheck %s --check-prefix=ERR-INVALID-VAL
# RUN: not llvm-objcopy --change-section-lma 0 %t 2>&1 | FileCheck %s --check-prefix=ERR-MISSING-OFFSET
# RUN: not llvm-objcopy --change-section-lma *-0x2000 %t 2>&1 | FileCheck %s --check-prefix=ERR-UNDERFLOW
# RUN: not llvm-objcopy --change-section-lma *+0x100000000 %t 2>&1 | FileCheck %s --check-prefix=ERR-OVERFLOW

# CHECK-PLUS-PROGRAMS: Type Offset VirtAddr PhysAddr FileSiz MemSiz
# CHECK-PLUS-PROGRAMS: PHDR 0x000002 0x0000000000001102 0x0000000000001122 0x000038 0x000000
# CHECK-PLUS-PROGRAMS: LOAD 0x000000 0x0000000000001100 0x0000000000001120 0x000258 0x000258
# CHECK-PLUS-PROGRAMS: LOAD 0x000258 0xffffffff00005100 0xffffffff00006120 0x000100 0x000100
# CHECK-PLUS-PROGRAMS: NOTE 0x000358 0x0000000000001200 0x0000000000001220 0x000010 0x000000
# CHECK-PLUS-PROGRAMS: NOTE 0x000368 0x0000000000000000 0x0000000000000000 0x000000 0x000000

# CHECK-MINUS-PROGRAMS: PHDR 0x000002 0x0000000000001102 0x00000000000010d2 0x000038 0x000000
# CHECK-MINUS-PROGRAMS: LOAD 0x000000 0x0000000000001100 0x00000000000010d0 0x000258 0x000258
# CHECK-MINUS-PROGRAMS: LOAD 0x000258 0xffffffff00005100 0xffffffff000060d0 0x000100 0x000100
# CHECK-MINUS-PROGRAMS: NOTE 0x000358 0x0000000000001200 0x00000000000011d0 0x000010 0x000000
# CHECK-MINUS-PROGRAMS: NOTE 0x000368 0x0000000000000000 0x0000000000000000 0x000000 0x000000

# CHECK-PLUS-SECTIONS: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
# CHECK-PLUS-SECTIONS: .text1
# CHECK-PLUS-SECTIONS-SAME: 0000000000000000
# CHECK-PLUS-SECTIONS: .text2
# CHECK-PLUS-SECTIONS-SAME: 0000000000000000

# CHECK-MINUS-SECTIONS: .text1
# CHECK-MINUS-SECTIONS-SAME: 0000000000000000
# CHECK-MINUS-SECTIONS: .text2
# CHECK-MINUS-SECTIONS-SAME: 0000000000000000

# ERR-SET-ADDRESS: error: bad format for --change-section-lma: changing LMA to a specific value is not supported. Use *+val or *-val instead
# ERR-SPECIFIC-SEC: error: bad format for --change-section-lma: changing a specific section LMA is not supported. Use *+val or *-val instead
# ERR-INVALID-VAL: error: bad format for --change-section-lma: value after *+ is 0c50 when it should be an integer
# ERR-MISSING-OFFSET: error: bad format for --change-section-lma: missing LMA offset
# ERR-UNDERFLOW: : address 0x1102 cannot be decreased by 0x2000. The result would underflow
# ERR-OVERFLOW: address 0xffffffff00006100 cannot be increased by 0x100000000. The result would overflow

!ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Sections:
- Name: .text1
Type: SHT_PROGBITS
Size: 0x100
- Name: .text2
Type: SHT_PROGBITS
Size: 0x100
ProgramHeaders:
- Type: PT_PHDR
FileSize: 0x38
Offset: 0x2
VAddr: 0x1102
- Type: PT_LOAD
Offset: 0x0
VAddr: 0x1100
FirstSec: .text1
LastSec: .text1
- Type: PT_LOAD
VAddr: 0xFFFFFFFF00005100
PAddr: 0xFFFFFFFF00006100
FirstSec: .text2
LastSec: .text2
- Type: PT_NOTE
FileSize: 0x10
VAddr: 0x1200
Offset: 0x358
- Type: PT_NOTE
FileSize: 0x0
Offset: 0x368
40 changes: 40 additions & 0 deletions llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,38 @@ static Error loadNewSectionData(StringRef ArgValue, StringRef OptionName,
return Error::success();
}

static Expected<int64_t> parseChangeSectionLMA(StringRef ArgValue,
StringRef OptionName) {
StringRef StringValue;
if (ArgValue.starts_with("*+")) {
StringValue = ArgValue.slice(2, StringRef::npos);
} else if (ArgValue.starts_with("*-")) {
StringValue = ArgValue.slice(1, StringRef::npos);
} else if (ArgValue.contains("=")) {
return createStringError(errc::invalid_argument,
"bad format for " + OptionName +
": changing LMA to a specific value is not "
"supported. Use *+val or *-val instead");
} else if (ArgValue.contains("+") || ArgValue.contains("-")) {
return createStringError(errc::invalid_argument,
"bad format for " + OptionName +
": changing a specific section LMA is not "
"supported. Use *+val or *-val instead");
}
if (StringValue.empty())
return createStringError(errc::invalid_argument,
"bad format for " + OptionName +
": missing LMA offset");

auto LMAValue = getAsInteger<int64_t>(StringValue);
if (!LMAValue)
return createStringError(LMAValue.getError(),
"bad format for " + OptionName + ": value after " +
ArgValue.slice(0, 2) + " is " + StringValue +
" when it should be an integer");
return *LMAValue;
}

// parseObjcopyOptions returns the config and sets the input arguments. If a
// help flag is set then parseObjcopyOptions will print the help messege and
// exit.
Expand Down Expand Up @@ -833,6 +865,14 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
Config.PadTo = *Addr;
}

if (const auto *Arg = InputArgs.getLastArg(OBJCOPY_change_section_lma)) {
Expected<int64_t> LMAValue =
parseChangeSectionLMA(Arg->getValue(), Arg->getSpelling());
if (!LMAValue)
return LMAValue.takeError();
Config.ChangeSectionLMAValAll = *LMAValue;
}

for (auto *Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
if (!StringRef(Arg->getValue()).contains('='))
return createStringError(errc::invalid_argument,
Expand Down
4 changes: 4 additions & 0 deletions llvm/tools/llvm-objcopy/ObjcopyOpts.td
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ def adjust_start : JoinedOrSeparate<["--"], "adjust-start">,
Alias<change_start>,
HelpText<"Alias for --change-start">;

defm change_section_lma
: Eq<"change-section-lma", "Shift LMA of non-zero-sized sections in the program header by <val>">,
MetaVarName<"*{+|-}val">;

defm add_symbol
: Eq<"add-symbol", "Add new symbol <name> to .symtab. Accepted flags: "
"global, local, weak, default, hidden, protected, file, section, object, "
Expand Down
Loading