diff --git a/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp b/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp index d4eb6a9b9fc0b..8ecd669e67178 100644 --- a/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp +++ b/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp @@ -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; @@ -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); diff --git a/llvm/test/tools/llvm-objcopy/MachO/strip-with-encryption-info.test b/llvm/test/tools/llvm-objcopy/MachO/strip-with-encryption-info.test index 19b06b1ec02c8..2b2bd670613de 100644 --- a/llvm/test/tools/llvm-objcopy/MachO/strip-with-encryption-info.test +++ b/llvm/test/tools/llvm-objcopy/MachO/strip-with-encryption-info.test @@ -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; }