Skip to content

[llvm-objcopy] Apply encryptable offset to first segment, not section #130517

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 3 commits into from
Mar 14, 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
13 changes: 8 additions & 5 deletions llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,10 @@ uint64_t MachOLayoutBuilder::layoutSegments() {
const bool IsObjectFile =
O.Header.FileType == MachO::HeaderFileType::MH_OBJECT;
uint64_t Offset = IsObjectFile ? (HeaderSize + O.Header.SizeOfCmds) : 0;
if (O.EncryptionInfoCommandIndex) {
// If we are emitting an encryptable binary, our load commands must have a
// separate (non-encrypted) page to themselves.
Offset = alignToPowerOf2(HeaderSize + O.Header.SizeOfCmds, PageSize);
}
// If we are emitting an encryptable binary, our load commands must have a
// separate (non-encrypted) page to themselves.
bool RequiresFirstSectionOutsideFirstPage =
O.EncryptionInfoCommandIndex.has_value();
for (LoadCommand &LC : O.LoadCommands) {
auto &MLC = LC.MachOLoadCommand;
StringRef Segname;
Expand Down Expand Up @@ -174,6 +173,10 @@ uint64_t MachOLayoutBuilder::layoutSegments() {
if (!Sec->hasValidOffset()) {
Sec->Offset = 0;
} else {
if (RequiresFirstSectionOutsideFirstPage) {
SectOffset = alignToPowerOf2(SectOffset, PageSize);
RequiresFirstSectionOutsideFirstPage = false;
}
Sec->Offset = SegOffset + SectOffset;
Sec->Size = Sec->Content.size();
SegFileSize = std::max(SegFileSize, SectOffset + Sec->Size);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# RUN: rm -rf %t && mkdir %t
# RUN: yaml2obj %s -o %t/original
# RUN: llvm-strip --strip-all %t/original -o %t/stripped
# RUN: llvm-readobj --macho-segment %t/stripped | FileCheck %s
# RUN: llvm-readobj --macho-segment --section-headers %t/stripped | FileCheck %s

# CHECK-LABEL: Sections [
# CHECK: Index: 0
# CHECK-NEXT: Name: __text
# CHECK-NEXT: Segment: __TEXT
# CHECK: Offset: 16384

# CHECK-LABEL: Name: __PAGEZERO
# CHECK: fileoff: 16384
# CHECK: fileoff: 0

# CHECK-LABEL: Name: __TEXT
# CHECK: fileoff: 16384
# CHECK: fileoff: 0

# The YAML below is the following code
# int main(int argc, char **argv) { return 0; }
Expand Down
Loading