Skip to content

[llvm-objcopy] MachO: Fix section finding policy for object files #127604

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 6 commits into from
Feb 23, 2025
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
2 changes: 2 additions & 0 deletions llvm/docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ Changes to the Debug Info
Changes to the LLVM tools
---------------------------------

* llvm-objcopy now supports the `--update-section` flag for intermediate Mach-O object files.

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

Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,24 @@ static Error addSection(const NewSectionInfo &NewSection, Object &Obj) {
static Expected<Section &> findSection(StringRef SecName, Object &O) {
StringRef SegName;
std::tie(SegName, SecName) = SecName.split(",");
// For compactness, intermediate object files (MH_OBJECT) contain
// only one segment in which all sections are placed.
// The static linker places each section in the named segment when building
// the final product (any file that is not of type MH_OBJECT).
//
// Source:
// https://math-atlas.sourceforge.net/devel/assembly/MachORuntime.pdf
// page 57
if (O.Header.FileType == MachO::HeaderFileType::MH_OBJECT) {
for (const auto& LC : O.LoadCommands)
for (const auto& Sec : LC.Sections)
if (Sec->Segname == SegName && Sec->Sectname == SecName)
return *Sec;

StringRef ErrMsg = "could not find section with name '%s' in '%s' segment";
return createStringError(errc::invalid_argument, ErrMsg.str().c_str(),
SecName.str().c_str(), SegName.str().c_str());
}
auto FoundSeg =
llvm::find_if(O.LoadCommands, [SegName](const LoadCommand &LC) {
return LC.getSegmentName() == SegName;
Expand Down
4 changes: 4 additions & 0 deletions llvm/test/tools/llvm-objcopy/MachO/Inputs/macho_sections.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.section __TEXT,__text
.space 64
.section __DATA,__storage
.space 128
47 changes: 47 additions & 0 deletions llvm/test/tools/llvm-objcopy/MachO/update-section-object.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# REQUIRES: aarch64-registered-target

# RUN: llvm-mc -assemble -triple=arm64-apple-macos11 -filetype=obj %p/Inputs/macho_sections.s -o %t.o
# RUN: llvm-otool -l %t.o | FileCheck %s --check-prefix=ORIG


# RUN: llvm-objcopy %t.o --update-section __DATA,__storage=%p/Inputs/macho_sections.s %t.new.o
# RUN: llvm-otool -l %t.new.o | FileCheck %s --check-prefix=UPDATED

# ORIG: cmd LC_SEGMENT_64
# ORIG-NEXT: cmdsize 232
# ORIG-NEXT: segname
# ORIG-NEXT: vmaddr 0x0000000000000000
# ORIG-NEXT: vmsize 0x00000000000000c0
# ORIG-NEXT: fileoff 392
# ORIG-NEXT: filesize 192
# ORIG-NEXT: maxprot 0x00000007
# ORIG-NEXT: initprot 0x00000007
# ORIG-NEXT: nsects 2
# ORIG-NEXT: flags 0x0

# ORIG: Section
# ORIG: sectname __storage
# ORIG-NEXT: segname __DATA
# ORIG-NEXT: addr 0x0000000000000040
# ORIG-NEXT: size 0x0000000000000080


### Make sure the file size and segment size have changed
# UPDATED: cmd LC_SEGMENT_64
# UPDATED-NEXT: cmdsize 232
# UPDATED-NEXT: segname
# UPDATED-NEXT: vmaddr 0x0000000000000000
# UPDATED-NEXT: vmsize 0x0000000000000090
# UPDATED-NEXT: fileoff 392
# UPDATED-NEXT: filesize 144
# UPDATED-NEXT: maxprot 0x00000007
# UPDATED-NEXT: initprot 0x00000007
# UPDATED-NEXT: nsects 2
# UPDATED-NEXT: flags 0x0

# UPDATED: Section
# UPDATED: sectname __storage
# UPDATED-NEXT: segname __DATA
# UPDATED-NEXT: addr 0x0000000000000040
# UPDATED-NEXT: size 0x0000000000000050

2 changes: 1 addition & 1 deletion llvm/test/tools/llvm-objcopy/MachO/update-section.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# RUN: not llvm-objcopy --update-section __TEXT,__text=%t.noexist %t /dev/null

# RUN: not llvm-objcopy --update-section __NOEXIST,__text=%t.diff %t /dev/null 2>&1 | FileCheck %s --check-prefix=NO-SEGMENT
# NO-SEGMENT: error: {{.*}}could not find segment with name '__NOEXIST'
# NO-SEGMENT: error: {{.*}}could not find section with name '__text' in '__NOEXIST' segment

# RUN: not llvm-objcopy --update-section __TEXT,__noexist=%t.diff %t /dev/null 2>&1 | FileCheck %s --check-prefix=NO-SECTION
# NO-SECTION: error: {{.*}}could not find section with name '__noexist'
Expand Down