[llvm-objcopy] Apply encryptable offset to first segment, not section… · llvm/llvm-project@cb50aaf (original) (raw)

Original file line number Diff line number Diff line change
@@ -116,11 +116,10 @@ uint64_t MachOLayoutBuilder::layoutSegments() {
116 116 const bool IsObjectFile =
117 117 O.Header.FileType == MachO::HeaderFileType::MH_OBJECT;
118 118 uint64_t Offset = IsObjectFile ? (HeaderSize + O.Header.SizeOfCmds) : 0;
119 -if (O.EncryptionInfoCommandIndex) {
120 -// If we are emitting an encryptable binary, our load commands must have a
121 -// separate (non-encrypted) page to themselves.
122 - Offset = alignToPowerOf2(HeaderSize + O.Header.SizeOfCmds, PageSize);
123 - }
119 +// If we are emitting an encryptable binary, our load commands must have a
120 +// separate (non-encrypted) page to themselves.
121 +bool RequiresFirstSectionOutsideFirstPage =
122 + O.EncryptionInfoCommandIndex.has_value();
124 123 for (LoadCommand &LC : O.LoadCommands) {
125 124 auto &MLC = LC.MachOLoadCommand;
126 125 StringRef Segname;
@@ -174,6 +173,10 @@ uint64_t MachOLayoutBuilder::layoutSegments() {
174 173 if (!Sec->hasValidOffset()) {
175 174 Sec->Offset = 0;
176 175 } else {
176 +if (RequiresFirstSectionOutsideFirstPage) {
177 + SectOffset = alignToPowerOf2(SectOffset, PageSize);
178 + RequiresFirstSectionOutsideFirstPage = false;
179 + }
177 180 Sec->Offset = SegOffset + SectOffset;
178 181 Sec->Size = Sec->Content.size();
179 182 SegFileSize = std::max(SegFileSize, SectOffset + Sec->Size);