Skip to content

Commit a0662ce

Browse files
authored
[objcopy][MachO] Revert special handling of encryptable binaries (#144058)
Code originally added in #120995 and later corrected in #130517 but apparently still not correct according to #141494 and rust-lang/rust#141913. Revert the special handling because the test written in #120995 and #130517 still passes without those changes. Kept the test and improved it with a `__DATA` section to keep the current behaviour checked in case other changes modify the behaviour and break this edge case.
1 parent a00b736 commit a0662ce

File tree

5 files changed

+106
-69
lines changed

5 files changed

+106
-69
lines changed

llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ uint64_t MachOLayoutBuilder::layoutSegments() {
116116
const bool IsObjectFile =
117117
O.Header.FileType == MachO::HeaderFileType::MH_OBJECT;
118118
uint64_t Offset = IsObjectFile ? (HeaderSize + O.Header.SizeOfCmds) : 0;
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();
123119
for (LoadCommand &LC : O.LoadCommands) {
124120
auto &MLC = LC.MachOLoadCommand;
125121
StringRef Segname;
@@ -173,10 +169,6 @@ uint64_t MachOLayoutBuilder::layoutSegments() {
173169
if (!Sec->hasValidOffset()) {
174170
Sec->Offset = 0;
175171
} else {
176-
if (RequiresFirstSectionOutsideFirstPage) {
177-
SectOffset = alignToPowerOf2(SectOffset, PageSize);
178-
RequiresFirstSectionOutsideFirstPage = false;
179-
}
180172
Sec->Offset = SegOffset + SectOffset;
181173
Sec->Size = Sec->Content.size();
182174
SegFileSize = std::max(SegFileSize, SectOffset + Sec->Size);

llvm/lib/ObjCopy/MachO/MachOObject.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ void Object::updateLoadCommandIndexes() {
9898
case MachO::LC_DYLD_EXPORTS_TRIE:
9999
ExportsTrieCommandIndex = Index;
100100
break;
101-
case MachO::LC_ENCRYPTION_INFO:
102-
case MachO::LC_ENCRYPTION_INFO_64:
103-
EncryptionInfoCommandIndex = Index;
104-
break;
105101
}
106102
}
107103
}

llvm/lib/ObjCopy/MachO/MachOObject.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,6 @@ struct Object {
341341
/// The index of the LC_SEGMENT or LC_SEGMENT_64 load command
342342
/// corresponding to the __TEXT segment.
343343
std::optional<size_t> TextSegmentCommandIndex;
344-
/// The index of the LC_ENCRYPTION_INFO or LC_ENCRYPTION_INFO_64 load command
345-
/// if present.
346-
std::optional<size_t> EncryptionInfoCommandIndex;
347344

348345
BumpPtrAllocator Alloc;
349346
StringSaver NewSectionsContents;

llvm/lib/ObjCopy/MachO/MachOReader.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,6 @@ Error MachOReader::readLoadCommands(Object &O) const {
184184
case MachO::LC_DYLD_CHAINED_FIXUPS:
185185
O.ChainedFixupsCommandIndex = O.LoadCommands.size();
186186
break;
187-
case MachO::LC_ENCRYPTION_INFO:
188-
case MachO::LC_ENCRYPTION_INFO_64:
189-
O.EncryptionInfoCommandIndex = O.LoadCommands.size();
190-
break;
191187
}
192188
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
193189
case MachO::LCName: \

llvm/test/tools/llvm-objcopy/MachO/strip-with-encryption-info.test

Lines changed: 106 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
# CHECK: fileoff: 0
1717

1818
# The YAML below is the following code
19+
# ```
20+
# static int foo = 12345;
21+
# int bar = 4567;
1922
# int main(int argc, char **argv) { return 0; }
23+
# ```
2024
# Compiled on macOS against the macOS SDK and passing `-Wl,-encryptable`
2125
# Contents are removed, since they are not important for the test. We need a
2226
# small text segment (smaller than a page).
@@ -26,8 +30,8 @@ FileHeader:
2630
cputype: 0x100000C
2731
cpusubtype: 0x0
2832
filetype: 0x2
29-
ncmds: 15
30-
sizeofcmds: 696
33+
ncmds: 18
34+
sizeofcmds: 920
3135
flags: 0x200085
3236
reserved: 0x0
3337
LoadCommands:
@@ -69,7 +73,7 @@ LoadCommands:
6973
- sectname: __unwind_info
7074
segname: __TEXT
7175
addr: 0x100004020
72-
size: 4152
76+
size: 88
7377
offset: 0x4020
7478
align: 2
7579
reloff: 0x0
@@ -79,37 +83,61 @@ LoadCommands:
7983
reserved2: 0x0
8084
reserved3: 0x0
8185
- cmd: LC_SEGMENT_64
82-
cmdsize: 72
83-
segname: __LINKEDIT
86+
cmdsize: 152
87+
segname: __DATA
8488
vmaddr: 4295000064
85-
vmsize: 592
89+
vmsize: 16384
8690
fileoff: 32768
87-
filesize: 592
91+
filesize: 16384
92+
maxprot: 3
93+
initprot: 3
94+
nsects: 1
95+
flags: 0
96+
Sections:
97+
- sectname: __data
98+
segname: __DATA
99+
addr: 0x100008000
100+
size: 4
101+
offset: 0x8000
102+
align: 2
103+
reloff: 0x0
104+
nreloc: 0
105+
flags: 0x0
106+
reserved1: 0x0
107+
reserved2: 0x0
108+
reserved3: 0x0
109+
- cmd: LC_SEGMENT_64
110+
cmdsize: 72
111+
segname: __LINKEDIT
112+
vmaddr: 4295016448
113+
vmsize: 16384
114+
fileoff: 49152
115+
filesize: 768
88116
maxprot: 1
89117
initprot: 1
90118
nsects: 0
91119
flags: 0
92120
- cmd: LC_DYLD_CHAINED_FIXUPS
93121
cmdsize: 16
94-
dataoff: 32768
95-
datasize: 48
122+
dataoff: 49152
123+
datasize: 56
96124
- cmd: LC_DYLD_EXPORTS_TRIE
97125
cmdsize: 16
98-
dataoff: 32816
99-
datasize: 48
126+
dataoff: 49208
127+
datasize: 64
100128
- cmd: LC_SYMTAB
101129
cmdsize: 24
102-
symoff: 32872
103-
nsyms: 2
104-
stroff: 32904
105-
strsize: 32
130+
symoff: 49280
131+
nsyms: 3
132+
stroff: 49328
133+
strsize: 40
106134
- cmd: LC_DYSYMTAB
107135
cmdsize: 80
108136
ilocalsym: 0
109137
nlocalsym: 0
110138
iextdefsym: 0
111-
nextdefsym: 2
112-
iundefsym: 2
139+
nextdefsym: 3
140+
iundefsym: 3
113141
nundefsym: 0
114142
tocoff: 0
115143
ntoc: 0
@@ -123,45 +151,57 @@ LoadCommands:
123151
nextrel: 0
124152
locreloff: 0
125153
nlocrel: 0
126-
- cmd: LC_ENCRYPTION_INFO_64
127-
cmdsize: 24
128-
cryptoff: 16384
129-
cryptsize: 16384
130-
cryptid: 0
131-
pad: 0
132154
- cmd: LC_LOAD_DYLINKER
133155
cmdsize: 32
134156
name: 12
135157
Content: '/usr/lib/dyld'
136158
ZeroPadBytes: 7
137159
- cmd: LC_UUID
138160
cmdsize: 24
139-
uuid: 4C4C4447-5555-3144-A18A-01E9EB7E7D92
161+
uuid: ADDA943C-657A-3A49-9580-168E17A40FFB
140162
- cmd: LC_BUILD_VERSION
141163
cmdsize: 32
142164
platform: 1
143165
minos: 983040
144-
sdk: 983552
166+
sdk: 984320
145167
ntools: 1
146168
Tools:
147-
- tool: 4
148-
version: 1310720
169+
- tool: 3
170+
version: 76481537
171+
- cmd: LC_SOURCE_VERSION
172+
cmdsize: 16
173+
version: 0
149174
- cmd: LC_MAIN
150175
cmdsize: 24
151176
entryoff: 16384
152177
stacksize: 0
178+
- cmd: LC_ENCRYPTION_INFO_64
179+
cmdsize: 24
180+
cryptoff: 16384
181+
cryptsize: 16384
182+
cryptid: 0
183+
pad: 0
184+
- cmd: LC_LOAD_DYLIB
185+
cmdsize: 56
186+
dylib:
187+
name: 24
188+
timestamp: 2
189+
current_version: 88539136
190+
compatibility_version: 65536
191+
Content: '/usr/lib/libSystem.B.dylib'
192+
ZeroPadBytes: 6
153193
- cmd: LC_FUNCTION_STARTS
154194
cmdsize: 16
155-
dataoff: 32864
195+
dataoff: 49272
156196
datasize: 8
157197
- cmd: LC_DATA_IN_CODE
158198
cmdsize: 16
159-
dataoff: 32872
199+
dataoff: 49280
160200
datasize: 0
161201
- cmd: LC_CODE_SIGNATURE
162202
cmdsize: 16
163-
dataoff: 32944
164-
datasize: 416
203+
dataoff: 49376
204+
datasize: 544
165205
LinkEditData:
166206
ExportTrie:
167207
TerminalSize: 0
@@ -173,51 +213,67 @@ LinkEditData:
173213
ImportName: ''
174214
Children:
175215
- TerminalSize: 0
176-
NodeOffset: 5
216+
NodeOffset: 25
177217
Name: _
178218
Flags: 0x0
179219
Address: 0x0
180220
Other: 0x0
181221
ImportName: ''
182222
Children:
223+
- TerminalSize: 2
224+
NodeOffset: 9
225+
Name: _mh_execute_header
226+
Flags: 0x0
227+
Address: 0x0
228+
Other: 0x0
229+
ImportName: ''
183230
- TerminalSize: 4
184-
NodeOffset: 33
185-
Name: main
231+
NodeOffset: 13
232+
Name: bar
186233
Flags: 0x0
187-
Address: 0x4000
234+
Address: 0x8000
188235
Other: 0x0
189236
ImportName: ''
190-
- TerminalSize: 2
191-
NodeOffset: 39
192-
Name: _mh_execute_header
237+
- TerminalSize: 4
238+
NodeOffset: 19
239+
Name: main
193240
Flags: 0x0
194-
Address: 0x0
241+
Address: 0x4000
195242
Other: 0x0
196243
ImportName: ''
197244
NameList:
198245
- n_strx: 2
199246
n_type: 0xF
200247
n_sect: 1
248+
n_desc: 16
249+
n_value: 4294967296
250+
- n_strx: 22
251+
n_type: 0xF
252+
n_sect: 3
201253
n_desc: 0
202-
n_value: 4294983680
203-
- n_strx: 8
254+
n_value: 4295000064
255+
- n_strx: 27
204256
n_type: 0xF
205257
n_sect: 1
206-
n_desc: 16
207-
n_value: 4294967296
258+
n_desc: 0
259+
n_value: 4294983680
208260
StringTable:
209261
- ' '
210-
- _main
211262
- __mh_execute_header
263+
- _bar
264+
- _main
265+
- ''
266+
- ''
267+
- ''
212268
- ''
213269
- ''
214270
- ''
215271
- ''
216272
FunctionStarts: [ 0x4000 ]
217-
ChainedFixups: [ 0x0, 0x0, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x30, 0x0,
218-
0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
219-
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
220-
0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
221-
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
273+
ChainedFixups: [ 0x0, 0x0, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x34, 0x0,
274+
0x0, 0x0, 0x34, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
275+
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
276+
0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
277+
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
278+
0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
222279
...
223-

0 commit comments

Comments
 (0)