1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
3 * Copyright (c) 2014 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
27 #include <sys/errno.h>
28 #include <sys/fcntl.h>
29 #include <mach-o/loader.h>
30 #include <mach-o/fat.h>
36 #include <unordered_map>
37 #include <unordered_set>
39 #include "CacheBuilder.h"
40 #include "Diagnostics.h"
41 #include "DyldSharedCache.h"
43 #include "MachOFileAbstraction.hpp"
46 #ifndef EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
47 #define EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE 0x02
55 Adjustor(DyldSharedCache
* cacheBuffer
, macho_header
<P
>* mh
, const std::vector
<CacheBuilder::SegmentMappingInfo
>& mappingInfo
, Diagnostics
& diag
);
56 void adjustImageForNewSegmentLocations(std::vector
<void*>& pointersForASLR
);
59 void adjustReferencesUsingInfoV2(std::vector
<void*>& pointersForASLR
);
60 void adjustReference(uint32_t kind
, uint8_t* mappedAddr
, uint64_t fromNewAddress
, uint64_t toNewAddress
, int64_t adjust
, int64_t targetSlide
,
61 uint64_t imageStartAddress
, uint64_t imageEndAddress
, std::vector
<void*>& pointersForASLR
, uint32_t*& lastMappedAddr32
,
62 uint32_t& lastKind
, uint64_t& lastToNewAddress
);
63 void adjustDataPointers(std::vector
<void*>& pointersForASLR
);
64 void slidePointer(int segIndex
, uint64_t segOffset
, uint8_t type
, std::vector
<void*>& pointersForASLR
);
65 void adjustSymbolTable();
66 void adjustExportsTrie(std::vector
<uint8_t>& newTrieBytes
);
67 void rebuildLinkEdit();
69 void adjustInstruction(uint8_t kind
, uint64_t cacheOffset
, uint64_t codeToDataDelta
);
70 void rebuildLinkEditAndLoadCommands();
71 uint64_t slideForOrigAddress(uint64_t addr
);
73 typedef typename
P::uint_t pint_t
;
74 typedef typename
P::E E
;
76 DyldSharedCache
* _cacheBuffer
;
78 Diagnostics
& _diagnostics
;
79 const uint8_t* _linkeditBias
= nullptr;
80 int64_t _linkeditAdjust
= 0;
81 unsigned _linkeditSegIndex
= 0;
82 bool _maskPointers
= false;
83 bool _splitSegInfoV2
= false;
84 const char* _installName
= nullptr;
85 macho_symtab_command
<P
>* _symTabCmd
= nullptr;
86 macho_dysymtab_command
<P
>* _dynSymTabCmd
= nullptr;
87 macho_dyld_info_command
<P
>* _dyldInfo
= nullptr;
88 macho_linkedit_data_command
<P
>* _splitSegInfoCmd
= nullptr;
89 macho_linkedit_data_command
<P
>* _functionStartsCmd
= nullptr;
90 macho_linkedit_data_command
<P
>* _dataInCodeCmd
= nullptr;
91 std::vector
<uint64_t> _segOrigStartAddresses
;
92 std::vector
<uint64_t> _segSlides
;
93 std::vector
<macho_segment_command
<P
>*> _segCmds
;
94 const std::vector
<CacheBuilder::SegmentMappingInfo
>& _mappingInfo
;
98 Adjustor
<P
>::Adjustor(DyldSharedCache
* cacheBuffer
, macho_header
<P
>* mh
, const std::vector
<CacheBuilder::SegmentMappingInfo
>& mappingInfo
, Diagnostics
& diag
)
99 : _cacheBuffer(cacheBuffer
), _mh(mh
), _diagnostics(diag
), _mappingInfo(mappingInfo
)
101 assert((mh
->magic() == MH_MAGIC
) || (mh
->magic() == MH_MAGIC_64
));
102 macho_segment_command
<P
>* segCmd
;
103 const macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((uint8_t*)mh
+ sizeof(macho_header
<P
>));
104 const uint32_t cmd_count
= mh
->ncmds();
105 const macho_load_command
<P
>* cmd
= cmds
;
106 unsigned segIndex
= 0;
107 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
108 switch (cmd
->cmd()) {
110 _installName
= ((macho_dylib_command
<P
>*)cmd
)->name();
113 _symTabCmd
= (macho_symtab_command
<P
>*)cmd
;
116 _dynSymTabCmd
= (macho_dysymtab_command
<P
>*)cmd
;
119 case LC_DYLD_INFO_ONLY
:
120 _dyldInfo
= (macho_dyld_info_command
<P
>*)cmd
;
122 case LC_SEGMENT_SPLIT_INFO
:
123 _splitSegInfoCmd
= (macho_linkedit_data_command
<P
>*)cmd
;
125 case LC_FUNCTION_STARTS
:
126 _functionStartsCmd
= (macho_linkedit_data_command
<P
>*)cmd
;
128 case LC_DATA_IN_CODE
:
129 _dataInCodeCmd
= (macho_linkedit_data_command
<P
>*)cmd
;
131 case macho_segment_command
<P
>::CMD
:
132 segCmd
= (macho_segment_command
<P
>*)cmd
;
133 _segCmds
.push_back(segCmd
);
134 _segOrigStartAddresses
.push_back(segCmd
->vmaddr());
135 _segSlides
.push_back(_mappingInfo
[segIndex
].dstCacheAddress
- segCmd
->vmaddr());
136 if ( strcmp(segCmd
->segname(), "__LINKEDIT") == 0 ) {
137 _linkeditAdjust
= _mappingInfo
[segIndex
].dstCacheOffset
- segCmd
->fileoff();
138 _linkeditBias
= (uint8_t*)cacheBuffer
+ _linkeditAdjust
;
139 _linkeditSegIndex
= segIndex
;
144 cmd
= (const macho_load_command
<P
>*)(((uint8_t*)cmd
)+cmd
->cmdsize());
146 _maskPointers
= (P::E::get32(mh
->cputype()) == CPU_TYPE_ARM64
);
147 if ( _splitSegInfoCmd
!= NULL
) {
148 const uint8_t* infoStart
= &_linkeditBias
[_splitSegInfoCmd
->dataoff()];
149 _splitSegInfoV2
= (*infoStart
== DYLD_CACHE_ADJ_V2_FORMAT
);
152 _diagnostics
.error("missing LC_SEGMENT_SPLIT_INFO in %s", _installName
);
156 template <typename P
>
157 void Adjustor
<P
>::adjustImageForNewSegmentLocations(std::vector
<void*>& pointersForASLR
)
159 if ( _diagnostics
.hasError() )
161 if ( _splitSegInfoV2
) {
162 adjustReferencesUsingInfoV2(pointersForASLR
);
165 adjustDataPointers(pointersForASLR
);
168 if ( _diagnostics
.hasError() )
171 if ( _diagnostics
.hasError() )
173 rebuildLinkEditAndLoadCommands();
176 template <typename P
>
177 uint64_t Adjustor
<P
>::slideForOrigAddress(uint64_t addr
)
179 for (unsigned i
=0; i
< _segOrigStartAddresses
.size(); ++i
) {
180 if ( (_segOrigStartAddresses
[i
] <= addr
) && (addr
< (_segOrigStartAddresses
[i
]+_segCmds
[i
]->vmsize())) )
181 return _segSlides
[i
];
183 // On arm64, high nibble of pointers can have extra bits
184 if ( _maskPointers
&& (addr
& 0xF000000000000000) ) {
185 return slideForOrigAddress(addr
& 0x0FFFFFFFFFFFFFFF);
187 _diagnostics
.error("slide not known for dylib address 0x%llX in %s", addr
, _installName
);
191 template <typename P
>
192 void Adjustor
<P
>::rebuildLinkEditAndLoadCommands()
194 // Exports trie is only data structure in LINKEDIT that might grow
195 std::vector
<uint8_t> newTrieBytes
;
196 adjustExportsTrie(newTrieBytes
);
198 // Remove: code signature, rebase info, code-sign-dirs, split seg info
199 uint32_t bindOffset
= 0;
200 uint32_t bindSize
= _dyldInfo
->bind_size();
201 uint32_t lazyBindOffset
= bindOffset
+ bindSize
;
202 uint32_t lazyBindSize
= _dyldInfo
->lazy_bind_size();
203 uint32_t weakBindOffset
= lazyBindOffset
+ lazyBindSize
;
204 uint32_t weakBindSize
= _dyldInfo
->weak_bind_size();
205 uint32_t exportOffset
= weakBindOffset
+ weakBindSize
;
206 uint32_t exportSize
= (uint32_t)newTrieBytes
.size();
207 uint32_t splitSegInfoOffset
= exportOffset
+ exportSize
;
208 uint32_t splitSegInfosSize
= (_splitSegInfoCmd
? _splitSegInfoCmd
->datasize() : 0);
209 uint32_t funcStartsOffset
= splitSegInfoOffset
+ splitSegInfosSize
;
210 uint32_t funcStartsSize
= (_functionStartsCmd
? _functionStartsCmd
->datasize() : 0);
211 uint32_t dataInCodeOffset
= funcStartsOffset
+ funcStartsSize
;
212 uint32_t dataInCodeSize
= (_dataInCodeCmd
? _dataInCodeCmd
->datasize() : 0);
213 uint32_t symbolTableOffset
= dataInCodeOffset
+ dataInCodeSize
;
214 uint32_t symbolTableSize
= _symTabCmd
->nsyms() * sizeof(macho_nlist
<P
>);
215 uint32_t indirectTableOffset
= symbolTableOffset
+ symbolTableSize
;
216 uint32_t indirectTableSize
= _dynSymTabCmd
->nindirectsyms() * sizeof(uint32_t);
217 uint32_t symbolStringsOffset
= indirectTableOffset
+ indirectTableSize
;
218 uint32_t symbolStringsSize
= _symTabCmd
->strsize();
219 uint32_t newLinkEditSize
= symbolStringsOffset
+ symbolStringsSize
;
221 size_t linkeditBufferSize
= align(_segCmds
[_linkeditSegIndex
]->vmsize(), 12);
222 if ( linkeditBufferSize
< newLinkEditSize
) {
223 _diagnostics
.error("LINKEDIT overflow in %s", _installName
);
227 uint32_t linkeditStartOffset
= (uint32_t)_mappingInfo
[_linkeditSegIndex
].dstCacheOffset
;
228 uint8_t* newLinkeditBufer
= (uint8_t*)::calloc(linkeditBufferSize
, 1);
230 memcpy(&newLinkeditBufer
[bindOffset
], &_linkeditBias
[_dyldInfo
->bind_off()], bindSize
);
232 memcpy(&newLinkeditBufer
[lazyBindOffset
], &_linkeditBias
[_dyldInfo
->lazy_bind_off()], lazyBindSize
);
234 memcpy(&newLinkeditBufer
[weakBindOffset
], &_linkeditBias
[_dyldInfo
->weak_bind_off()], weakBindSize
);
236 memcpy(&newLinkeditBufer
[exportOffset
], &newTrieBytes
[0], exportSize
);
237 if ( splitSegInfosSize
)
238 memcpy(&newLinkeditBufer
[splitSegInfoOffset
], &_linkeditBias
[_splitSegInfoCmd
->dataoff()], splitSegInfosSize
);
239 if ( funcStartsSize
)
240 memcpy(&newLinkeditBufer
[funcStartsOffset
], &_linkeditBias
[_functionStartsCmd
->dataoff()], funcStartsSize
);
241 if ( dataInCodeSize
)
242 memcpy(&newLinkeditBufer
[dataInCodeOffset
], &_linkeditBias
[_dataInCodeCmd
->dataoff()], dataInCodeSize
);
243 if ( symbolTableSize
)
244 memcpy(&newLinkeditBufer
[symbolTableOffset
], &_linkeditBias
[_symTabCmd
->symoff()], symbolTableSize
);
245 if ( indirectTableSize
)
246 memcpy(&newLinkeditBufer
[indirectTableOffset
], &_linkeditBias
[_dynSymTabCmd
->indirectsymoff()], indirectTableSize
);
247 if ( symbolStringsSize
)
248 memcpy(&newLinkeditBufer
[symbolStringsOffset
], &_linkeditBias
[_symTabCmd
->stroff()], symbolStringsSize
);
250 memcpy((uint8_t*)_cacheBuffer
+linkeditStartOffset
, newLinkeditBufer
, newLinkEditSize
);
251 ::bzero((uint8_t*)_cacheBuffer
+linkeditStartOffset
+newLinkEditSize
, linkeditBufferSize
-newLinkEditSize
);
252 ::free(newLinkeditBufer
);
254 // updates load commands and removed ones no longer needed
255 macho_load_command
<P
>* const cmds
= (macho_load_command
<P
>*)((uint8_t*)_mh
+ sizeof(macho_header
<P
>));
256 uint32_t cmd_count
= _mh
->ncmds();
257 const macho_load_command
<P
>* cmd
= cmds
;
258 const unsigned origLoadCommandsSize
= _mh
->sizeofcmds();
259 unsigned bytesRemaining
= origLoadCommandsSize
;
260 unsigned removedCount
= 0;
261 unsigned segIndex
= 0;
262 for (uint32_t i
= 0; i
< cmd_count
; ++i
) {
263 macho_symtab_command
<P
>* symTabCmd
;
264 macho_dysymtab_command
<P
>* dynSymTabCmd
;
265 macho_dyld_info_command
<P
>* dyldInfo
;
266 macho_linkedit_data_command
<P
>* functionStartsCmd
;
267 macho_linkedit_data_command
<P
>* dataInCodeCmd
;
268 macho_linkedit_data_command
<P
>* splitSegInfoCmd
;
269 macho_segment_command
<P
>* segCmd
;
270 macho_routines_command
<P
>* routinesCmd
;
271 macho_dylib_command
<P
>* dylibIDCmd
;
272 uint32_t cmdSize
= cmd
->cmdsize();
273 int32_t segFileOffsetDelta
;
275 switch ( cmd
->cmd() ) {
277 dylibIDCmd
= (macho_dylib_command
<P
>*)cmd
;
278 dylibIDCmd
->set_timestamp(2); // match what static linker sets in LC_LOAD_DYLIB
281 symTabCmd
= (macho_symtab_command
<P
>*)cmd
;
282 symTabCmd
->set_symoff(linkeditStartOffset
+symbolTableOffset
);
283 symTabCmd
->set_stroff(linkeditStartOffset
+symbolStringsOffset
);
286 dynSymTabCmd
= (macho_dysymtab_command
<P
>*)cmd
;
287 dynSymTabCmd
->set_indirectsymoff(linkeditStartOffset
+indirectTableOffset
);
290 case LC_DYLD_INFO_ONLY
:
291 dyldInfo
= (macho_dyld_info_command
<P
>*)cmd
;
292 dyldInfo
->set_rebase_off(0);
293 dyldInfo
->set_rebase_size(0);
294 dyldInfo
->set_bind_off(bindSize
? linkeditStartOffset
+bindOffset
: 0);
295 dyldInfo
->set_bind_size(bindSize
);
296 dyldInfo
->set_weak_bind_off(weakBindSize
? linkeditStartOffset
+weakBindOffset
: 0);
297 dyldInfo
->set_weak_bind_size(weakBindSize
);
298 dyldInfo
->set_lazy_bind_off(lazyBindSize
? linkeditStartOffset
+lazyBindOffset
: 0);
299 dyldInfo
->set_lazy_bind_size(lazyBindSize
);
300 dyldInfo
->set_export_off(exportSize
? linkeditStartOffset
+exportOffset
: 0);
301 dyldInfo
->set_export_size(exportSize
);
303 case LC_FUNCTION_STARTS
:
304 functionStartsCmd
= (macho_linkedit_data_command
<P
>*)cmd
;
305 functionStartsCmd
->set_dataoff(linkeditStartOffset
+funcStartsOffset
);
307 case LC_DATA_IN_CODE
:
308 dataInCodeCmd
= (macho_linkedit_data_command
<P
>*)cmd
;
309 dataInCodeCmd
->set_dataoff(linkeditStartOffset
+dataInCodeOffset
);
311 case macho_routines_command
<P
>::CMD
:
312 routinesCmd
= (macho_routines_command
<P
>*)cmd
;
313 routinesCmd
->set_init_address(routinesCmd
->init_address()+slideForOrigAddress(routinesCmd
->init_address()));
315 case macho_segment_command
<P
>::CMD
:
316 segCmd
= (macho_segment_command
<P
>*)cmd
;
317 segFileOffsetDelta
= (int32_t)(_mappingInfo
[segIndex
].dstCacheOffset
- segCmd
->fileoff());
318 segCmd
->set_vmaddr(_mappingInfo
[segIndex
].dstCacheAddress
);
319 segCmd
->set_vmsize(_mappingInfo
[segIndex
].dstCacheSegmentSize
);
320 segCmd
->set_fileoff(_mappingInfo
[segIndex
].dstCacheOffset
);
321 segCmd
->set_filesize(_mappingInfo
[segIndex
].copySegmentSize
);
322 if ( strcmp(segCmd
->segname(), "__LINKEDIT") == 0 )
323 segCmd
->set_vmsize(linkeditBufferSize
);
324 if ( segCmd
->nsects() > 0 ) {
325 macho_section
<P
>* const sectionsStart
= (macho_section
<P
>*)((uint8_t*)segCmd
+ sizeof(macho_segment_command
<P
>));
326 macho_section
<P
>* const sectionsEnd
= §ionsStart
[segCmd
->nsects()];
327 for (macho_section
<P
>* sect
=sectionsStart
; sect
< sectionsEnd
; ++sect
) {
328 sect
->set_addr(sect
->addr() + _segSlides
[segIndex
]);
329 if ( sect
->offset() != 0 )
330 sect
->set_offset(sect
->offset() + segFileOffsetDelta
);
336 _diagnostics
.warning("dyld shared cache does not support LC_RPATH found in %s", _installName
);
339 case LC_SEGMENT_SPLIT_INFO
:
340 splitSegInfoCmd
= (macho_linkedit_data_command
<P
>*)cmd
;
341 splitSegInfoCmd
->set_dataoff(linkeditStartOffset
+splitSegInfoOffset
);
343 case LC_CODE_SIGNATURE
:
344 case LC_DYLIB_CODE_SIGN_DRS
:
350 macho_load_command
<P
>* nextCmd
= (macho_load_command
<P
>*)(((uint8_t*)cmd
)+cmdSize
);
352 ::memmove((void*)cmd
, (void*)nextCmd
, bytesRemaining
);
356 bytesRemaining
-= cmdSize
;
360 // zero out stuff removed
361 ::bzero((void*)cmd
, bytesRemaining
);
363 _mh
->set_ncmds(cmd_count
-removedCount
);
364 _mh
->set_sizeofcmds(origLoadCommandsSize
-bytesRemaining
);
365 _mh
->set_flags(_mh
->flags() | 0x80000000);
369 template <typename P
>
370 void Adjustor
<P
>::adjustSymbolTable()
372 macho_nlist
<P
>* symbolTable
= (macho_nlist
<P
>*)&_linkeditBias
[_symTabCmd
->symoff()];
374 // adjust global symbol table entries
375 macho_nlist
<P
>* lastExport
= &symbolTable
[_dynSymTabCmd
->iextdefsym()+_dynSymTabCmd
->nextdefsym()];
376 for (macho_nlist
<P
>* entry
= &symbolTable
[_dynSymTabCmd
->iextdefsym()]; entry
< lastExport
; ++entry
) {
377 if ( (entry
->n_type() & N_TYPE
) == N_SECT
)
378 entry
->set_n_value(entry
->n_value() + slideForOrigAddress(entry
->n_value()));
381 // adjust local symbol table entries
382 macho_nlist
<P
>* lastLocal
= &symbolTable
[_dynSymTabCmd
->ilocalsym()+_dynSymTabCmd
->nlocalsym()];
383 for (macho_nlist
<P
>* entry
= &symbolTable
[_dynSymTabCmd
->ilocalsym()]; entry
< lastLocal
; ++entry
) {
384 if ( (entry
->n_sect() != NO_SECT
) && ((entry
->n_type() & N_STAB
) == 0) )
385 entry
->set_n_value(entry
->n_value() + slideForOrigAddress(entry
->n_value()));
389 template <typename P
>
390 void Adjustor
<P
>::slidePointer(int segIndex
, uint64_t segOffset
, uint8_t type
, std::vector
<void*>& pointersForASLR
)
392 pint_t
* mappedAddrP
= (pint_t
*)((uint8_t*)_cacheBuffer
+ _mappingInfo
[segIndex
].dstCacheOffset
+ segOffset
);
393 uint32_t* mappedAddr32
= (uint32_t*)mappedAddrP
;
397 case REBASE_TYPE_POINTER
:
398 valueP
= (pint_t
)P::getP(*mappedAddrP
);
399 P::setP(*mappedAddrP
, valueP
+ slideForOrigAddress(valueP
));
400 pointersForASLR
.push_back(mappedAddrP
);
403 case REBASE_TYPE_TEXT_ABSOLUTE32
:
404 value32
= P::E::get32(*mappedAddr32
);
405 P::E::set32(*mappedAddr32
, value32
+ (uint32_t)slideForOrigAddress(value32
));
408 case REBASE_TYPE_TEXT_PCREL32
:
409 // general text relocs not support
411 _diagnostics
.error("unknown rebase type 0x%02X in %s", type
, _installName
);
416 static bool isThumbMovw(uint32_t instruction
)
418 return ( (instruction
& 0x8000FBF0) == 0x0000F240 );
421 static bool isThumbMovt(uint32_t instruction
)
423 return ( (instruction
& 0x8000FBF0) == 0x0000F2C0 );
426 static uint16_t getThumbWord(uint32_t instruction
)
428 uint32_t i
= ((instruction
& 0x00000400) >> 10);
429 uint32_t imm4
= (instruction
& 0x0000000F);
430 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
431 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
432 return ((imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
);
435 static uint32_t setThumbWord(uint32_t instruction
, uint16_t word
) {
436 uint32_t imm4
= (word
& 0xF000) >> 12;
437 uint32_t i
= (word
& 0x0800) >> 11;
438 uint32_t imm3
= (word
& 0x0700) >> 8;
439 uint32_t imm8
= word
& 0x00FF;
440 return (instruction
& 0x8F00FBF0) | imm4
| (i
<< 10) | (imm3
<< 28) | (imm8
<< 16);
443 static bool isArmMovw(uint32_t instruction
)
445 return (instruction
& 0x0FF00000) == 0x03000000;
448 static bool isArmMovt(uint32_t instruction
)
450 return (instruction
& 0x0FF00000) == 0x03400000;
453 static uint16_t getArmWord(uint32_t instruction
)
455 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
456 uint32_t imm12
= (instruction
& 0x00000FFF);
457 return (imm4
<< 12) | imm12
;
460 static uint32_t setArmWord(uint32_t instruction
, uint16_t word
) {
461 uint32_t imm4
= (word
& 0xF000) >> 12;
462 uint32_t imm12
= word
& 0x0FFF;
463 return (instruction
& 0xFFF0F000) | (imm4
<< 16) | imm12
;
466 template <typename P
>
467 void Adjustor
<P
>::adjustReference(uint32_t kind
, uint8_t* mappedAddr
, uint64_t fromNewAddress
, uint64_t toNewAddress
,
468 int64_t adjust
, int64_t targetSlide
, uint64_t imageStartAddress
, uint64_t imageEndAddress
,
469 std::vector
<void*>& pointersForASLR
, uint32_t*& lastMappedAddr32
, uint32_t& lastKind
, uint64_t& lastToNewAddress
)
472 uint64_t* mappedAddr64
= 0;
474 uint32_t* mappedAddr32
= 0;
475 uint32_t instruction
;
476 int64_t offsetAdjust
;
479 case DYLD_CACHE_ADJ_V2_DELTA_32
:
480 mappedAddr32
= (uint32_t*)mappedAddr
;
481 value32
= P::E::get32(*mappedAddr32
);
482 delta
= (int32_t)value32
;
484 if ( (delta
> 0x80000000) || (-delta
> 0x80000000) ) {
485 _diagnostics
.error("DYLD_CACHE_ADJ_V2_DELTA_32 can't be adjust by 0x%016llX in %s", adjust
, _installName
);
488 P::E::set32(*mappedAddr32
, (int32_t)delta
);
490 case DYLD_CACHE_ADJ_V2_POINTER_32
:
491 mappedAddr32
= (uint32_t*)mappedAddr
;
492 if ( toNewAddress
!= (E::get32(*mappedAddr32
) + targetSlide
) ) {
493 _diagnostics
.error("bad DYLD_CACHE_ADJ_V2_POINTER_32 value not as expected at address 0x%llX in %s", fromNewAddress
, _installName
);
496 E::set32(*mappedAddr32
, (uint32_t)toNewAddress
);
497 pointersForASLR
.push_back(mappedAddr
);
499 case DYLD_CACHE_ADJ_V2_POINTER_64
:
500 mappedAddr64
= (uint64_t*)mappedAddr
;
501 if ( toNewAddress
!= (E::get64(*mappedAddr64
) + targetSlide
) ) {
502 _diagnostics
.error("bad DYLD_CACHE_ADJ_V2_POINTER_64 value not as expected at address 0x%llX in %s", fromNewAddress
, _installName
);
505 E::set64(*mappedAddr64
, toNewAddress
);
506 pointersForASLR
.push_back(mappedAddr
);
508 case DYLD_CACHE_ADJ_V2_DELTA_64
:
509 mappedAddr64
= (uint64_t*)mappedAddr
;
510 value64
= P::E::get64(*mappedAddr64
);
511 E::set64(*mappedAddr64
, value64
+ adjust
);
513 case DYLD_CACHE_ADJ_V2_IMAGE_OFF_32
:
516 mappedAddr32
= (uint32_t*)mappedAddr
;
517 value32
= P::E::get32(*mappedAddr32
);
518 value64
= toNewAddress
- imageStartAddress
;
519 if ( value64
> imageEndAddress
) {
520 _diagnostics
.error("DYLD_CACHE_ADJ_V2_IMAGE_OFF_32 can't be adjust to 0x%016llX in %s", toNewAddress
, _installName
);
523 P::E::set32(*mappedAddr32
, (uint32_t)value64
);
525 case DYLD_CACHE_ADJ_V2_ARM64_ADRP
:
526 mappedAddr32
= (uint32_t*)mappedAddr
;
527 instruction
= P::E::get32(*mappedAddr32
);
528 if ( (instruction
& 0x9F000000) == 0x90000000 ) {
529 int64_t pageDistance
= ((toNewAddress
& ~0xFFF) - (fromNewAddress
& ~0xFFF));
530 int64_t newPage21
= pageDistance
>> 12;
531 if ( (newPage21
> 2097151) || (newPage21
< -2097151) ) {
532 _diagnostics
.error("DYLD_CACHE_ADJ_V2_ARM64_ADRP can't be adjusted that far in %s", _installName
);
535 instruction
= (instruction
& 0x9F00001F) | ((newPage21
<< 29) & 0x60000000) | ((newPage21
<< 3) & 0x00FFFFE0);
536 P::E::set32(*mappedAddr32
, instruction
);
539 // ADRP instructions are sometimes optimized to other instructions (e.g. ADR) after the split-seg-info is generated
542 case DYLD_CACHE_ADJ_V2_ARM64_OFF12
:
543 mappedAddr32
= (uint32_t*)mappedAddr
;
544 instruction
= P::E::get32(*mappedAddr32
);
545 offsetAdjust
= (adjust
& 0xFFF);
546 if ( offsetAdjust
== 0 )
548 if ( (instruction
& 0x3B000000) == 0x39000000 ) {
550 if ( offsetAdjust
!= 0 ) {
551 uint32_t encodedAddend
= ((instruction
& 0x003FFC00) >> 10);
552 uint32_t newAddend
= 0;
553 switch ( instruction
& 0xC0000000 ) {
555 if ( (instruction
& 0x04800000) == 0x04800000 ) {
556 if ( offsetAdjust
& 0xF ) {
557 _diagnostics
.error("can't adjust off12 scale=16 instruction by %lld bytes at mapped address=%p in %s", offsetAdjust
, mappedAddr
, _installName
);
560 if ( encodedAddend
*16 >= 4096 ) {
561 _diagnostics
.error("off12 scale=16 instruction points outside its page at mapped address=%p in %s", mappedAddr
, _installName
);
563 newAddend
= (encodedAddend
+ offsetAdjust
/16) % 256;
567 newAddend
= (encodedAddend
+ (int32_t)offsetAdjust
) % 4096;
571 if ( offsetAdjust
& 1 ) {
572 _diagnostics
.error("can't adjust off12 scale=2 instruction by %lld bytes at mapped address=%p in %s", offsetAdjust
, mappedAddr
, _installName
);
575 if ( encodedAddend
*2 >= 4096 ) {
576 _diagnostics
.error("off12 scale=2 instruction points outside its page at mapped address=%p in %s", mappedAddr
, _installName
);
579 newAddend
= (encodedAddend
+ offsetAdjust
/2) % 2048;
582 if ( offsetAdjust
& 3 ) {
583 _diagnostics
.error("can't adjust off12 scale=4 instruction by %lld bytes at mapped address=%p in %s", offsetAdjust
, mappedAddr
, _installName
);
586 if ( encodedAddend
*4 >= 4096 ) {
587 _diagnostics
.error("off12 scale=4 instruction points outside its page at mapped address=%p in %s", mappedAddr
, _installName
);
590 newAddend
= (encodedAddend
+ offsetAdjust
/4) % 1024;
593 if ( offsetAdjust
& 7 ) {
594 _diagnostics
.error("can't adjust off12 scale=8 instruction by %lld bytes at mapped address=%p in %s", offsetAdjust
, mappedAddr
, _installName
);
597 if ( encodedAddend
*8 >= 4096 ) {
598 _diagnostics
.error("off12 scale=8 instruction points outside its page at mapped address=%p in %s", mappedAddr
, _installName
);
601 newAddend
= (encodedAddend
+ offsetAdjust
/8) % 512;
604 uint32_t newInstruction
= (instruction
& 0xFFC003FF) | (newAddend
<< 10);
605 P::E::set32(*mappedAddr32
, newInstruction
);
608 else if ( (instruction
& 0xFFC00000) == 0x91000000 ) {
610 if ( instruction
& 0x00C00000 ) {
611 _diagnostics
.error("ADD off12 uses shift at mapped address=%p in %s", mappedAddr
, _installName
);
614 uint32_t encodedAddend
= ((instruction
& 0x003FFC00) >> 10);
615 uint32_t newAddend
= (encodedAddend
+ offsetAdjust
) & 0xFFF;
616 uint32_t newInstruction
= (instruction
& 0xFFC003FF) | (newAddend
<< 10);
617 P::E::set32(*mappedAddr32
, newInstruction
);
619 else if ( instruction
!= 0xD503201F ) {
620 // ignore imm12 instructions optimized into a NOP, but warn about others
621 _diagnostics
.error("unknown off12 instruction 0x%08X at 0x%0llX in %s", instruction
, fromNewAddress
, _installName
);
625 case DYLD_CACHE_ADJ_V2_THUMB_MOVW_MOVT
:
626 mappedAddr32
= (uint32_t*)mappedAddr
;
627 // to update a movw/movt pair we need to extract the 32-bit they will make,
628 // add the adjust and write back the new movw/movt pair.
629 if ( lastKind
== kind
) {
630 if ( lastToNewAddress
== toNewAddress
) {
631 uint32_t instruction1
= P::E::get32(*lastMappedAddr32
);
632 uint32_t instruction2
= P::E::get32(*mappedAddr32
);
633 if ( isThumbMovw(instruction1
) && isThumbMovt(instruction2
) ) {
634 uint16_t high
= getThumbWord(instruction2
);
635 uint16_t low
= getThumbWord(instruction1
);
636 uint32_t full
= high
<< 16 | low
;
638 instruction1
= setThumbWord(instruction1
, full
& 0xFFFF);
639 instruction2
= setThumbWord(instruction2
, full
>> 16);
641 else if ( isThumbMovt(instruction1
) && isThumbMovw(instruction2
) ) {
642 uint16_t high
= getThumbWord(instruction1
);
643 uint16_t low
= getThumbWord(instruction2
);
644 uint32_t full
= high
<< 16 | low
;
646 instruction2
= setThumbWord(instruction2
, full
& 0xFFFF);
647 instruction1
= setThumbWord(instruction1
, full
>> 16);
650 _diagnostics
.error("two DYLD_CACHE_ADJ_V2_THUMB_MOVW_MOVT in a row but not paried in %s", _installName
);
653 P::E::set32(*lastMappedAddr32
, instruction1
);
654 P::E::set32(*mappedAddr32
, instruction2
);
658 _diagnostics
.error("two DYLD_CACHE_ADJ_V2_THUMB_MOVW_MOVT in a row but target different addresses in %s", _installName
);
663 case DYLD_CACHE_ADJ_V2_ARM_MOVW_MOVT
:
664 mappedAddr32
= (uint32_t*)mappedAddr
;
665 // to update a movw/movt pair we need to extract the 32-bit they will make,
666 // add the adjust and write back the new movw/movt pair.
667 if ( lastKind
== kind
) {
668 if ( lastToNewAddress
== toNewAddress
) {
669 uint32_t instruction1
= P::E::get32(*lastMappedAddr32
);
670 uint32_t instruction2
= P::E::get32(*mappedAddr32
);
671 if ( isArmMovw(instruction1
) && isArmMovt(instruction2
) ) {
672 uint16_t high
= getArmWord(instruction2
);
673 uint16_t low
= getArmWord(instruction1
);
674 uint32_t full
= high
<< 16 | low
;
676 instruction1
= setArmWord(instruction1
, full
& 0xFFFF);
677 instruction2
= setArmWord(instruction2
, full
>> 16);
679 else if ( isArmMovt(instruction1
) && isArmMovw(instruction2
) ) {
680 uint16_t high
= getArmWord(instruction1
);
681 uint16_t low
= getArmWord(instruction2
);
682 uint32_t full
= high
<< 16 | low
;
684 instruction2
= setArmWord(instruction2
, full
& 0xFFFF);
685 instruction1
= setArmWord(instruction1
, full
>> 16);
688 _diagnostics
.error("two DYLD_CACHE_ADJ_V2_ARM_MOVW_MOVT in a row but not paired in %s", _installName
);
691 P::E::set32(*lastMappedAddr32
, instruction1
);
692 P::E::set32(*mappedAddr32
, instruction2
);
696 _diagnostics
.error("two DYLD_CACHE_ADJ_V2_ARM_MOVW_MOVT in a row but target different addresses in %s", _installName
);
701 case DYLD_CACHE_ADJ_V2_ARM64_BR26
:
702 case DYLD_CACHE_ADJ_V2_THUMB_BR22
:
703 case DYLD_CACHE_ADJ_V2_ARM_BR24
:
704 // nothing to do with calls to stubs
707 _diagnostics
.error("unknown split seg kind=%d in %s", kind
, _installName
);
711 lastToNewAddress
= toNewAddress
;
712 lastMappedAddr32
= mappedAddr32
;
715 template <typename P
>
716 void Adjustor
<P
>::adjustReferencesUsingInfoV2(std::vector
<void*>& pointersForASLR
)
718 static const bool log
= false;
720 const uint8_t* infoStart
= &_linkeditBias
[_splitSegInfoCmd
->dataoff()];
721 const uint8_t* infoEnd
= &infoStart
[_splitSegInfoCmd
->datasize()];
722 if ( *infoStart
++ != DYLD_CACHE_ADJ_V2_FORMAT
) {
723 _diagnostics
.error("malformed split seg info in %s", _installName
);
726 // build section arrays of slide and mapped address for each section
727 std::vector
<uint64_t> sectionSlides
;
728 std::vector
<uint64_t> sectionNewAddress
;
729 std::vector
<uint8_t*> sectionMappedAddress
;
730 sectionSlides
.reserve(16);
731 sectionNewAddress
.reserve(16);
732 sectionMappedAddress
.reserve(16);
733 // section index 0 refers to mach_header
734 sectionMappedAddress
.push_back((uint8_t*)_cacheBuffer
+ _mappingInfo
[0].dstCacheOffset
);
735 sectionSlides
.push_back(_segSlides
[0]);
736 sectionNewAddress
.push_back(_mappingInfo
[0].dstCacheAddress
);
737 // section 1 and later refer to real sections
738 unsigned sectionIndex
= 0;
739 for (unsigned segmentIndex
=0; segmentIndex
< _segCmds
.size(); ++segmentIndex
) {
740 macho_segment_command
<P
>* segCmd
= _segCmds
[segmentIndex
];
741 macho_section
<P
>* const sectionsStart
= (macho_section
<P
>*)((char*)segCmd
+ sizeof(macho_segment_command
<P
>));
742 macho_section
<P
>* const sectionsEnd
= §ionsStart
[segCmd
->nsects()];
743 for(macho_section
<P
>* sect
= sectionsStart
; sect
< sectionsEnd
; ++sect
) {
744 sectionMappedAddress
.push_back((uint8_t*)_cacheBuffer
+ _mappingInfo
[segmentIndex
].dstCacheOffset
+ sect
->addr() - segCmd
->vmaddr());
745 sectionSlides
.push_back(_segSlides
[segmentIndex
]);
746 sectionNewAddress
.push_back(_mappingInfo
[segmentIndex
].dstCacheAddress
+ sect
->addr() - segCmd
->vmaddr());
748 fprintf(stderr
, " %s/%s, sectIndex=%d, mapped at=%p\n",
749 sect
->segname(), sect
->sectname(), sectionIndex
, sectionMappedAddress
.back());
755 // Whole :== <count> FromToSection+
756 // FromToSection :== <from-sect-index> <to-sect-index> <count> ToOffset+
757 // ToOffset :== <to-sect-offset-delta> <count> FromOffset+
758 // FromOffset :== <kind> <count> <from-sect-offset-delta>
759 const uint8_t* p
= infoStart
;
760 uint64_t sectionCount
= read_uleb128(p
, infoEnd
);
761 for (uint64_t i
=0; i
< sectionCount
; ++i
) {
762 uint32_t* lastMappedAddr32
= NULL
;
763 uint32_t lastKind
= 0;
764 uint64_t lastToNewAddress
= 0;
765 uint64_t fromSectionIndex
= read_uleb128(p
, infoEnd
);
766 uint64_t toSectionIndex
= read_uleb128(p
, infoEnd
);
767 uint64_t toOffsetCount
= read_uleb128(p
, infoEnd
);
768 uint64_t fromSectionSlide
= sectionSlides
[fromSectionIndex
];
769 uint64_t fromSectionNewAddress
= sectionNewAddress
[fromSectionIndex
];
770 uint8_t* fromSectionMappedAddress
= sectionMappedAddress
[fromSectionIndex
];
771 uint64_t toSectionSlide
= sectionSlides
[toSectionIndex
];
772 uint64_t toSectionNewAddress
= sectionNewAddress
[toSectionIndex
];
773 if (log
) printf(" from sect=%lld (mapped=%p), to sect=%lld (new addr=0x%llX):\n", fromSectionIndex
, fromSectionMappedAddress
, toSectionIndex
, toSectionNewAddress
);
774 uint64_t toSectionOffset
= 0;
775 for (uint64_t j
=0; j
< toOffsetCount
; ++j
) {
776 uint64_t toSectionDelta
= read_uleb128(p
, infoEnd
);
777 uint64_t fromOffsetCount
= read_uleb128(p
, infoEnd
);
778 toSectionOffset
+= toSectionDelta
;
779 for (uint64_t k
=0; k
< fromOffsetCount
; ++k
) {
780 uint64_t kind
= read_uleb128(p
, infoEnd
);
782 _diagnostics
.error("bad kind value (%llu) in %s", kind
, _installName
);
785 uint64_t fromSectDeltaCount
= read_uleb128(p
, infoEnd
);
786 uint64_t fromSectionOffset
= 0;
787 for (uint64_t l
=0; l
< fromSectDeltaCount
; ++l
) {
788 uint64_t delta
= read_uleb128(p
, infoEnd
);
789 fromSectionOffset
+= delta
;
790 int64_t deltaAdjust
= toSectionSlide
- fromSectionSlide
;
791 //if (log) printf(" kind=%lld, from offset=0x%0llX, to offset=0x%0llX, adjust=0x%llX, targetSlide=0x%llX\n", kind, fromSectionOffset, toSectionOffset, deltaAdjust, toSectionSlide);
792 uint8_t* fromMappedAddr
= fromSectionMappedAddress
+ fromSectionOffset
;
793 uint64_t toNewAddress
= toSectionNewAddress
+ toSectionOffset
;
794 uint64_t fromNewAddress
= fromSectionNewAddress
+ fromSectionOffset
;
795 uint64_t imageStartAddress
= sectionNewAddress
.front();
796 uint64_t imageEndAddress
= sectionNewAddress
.back();
797 if ( toSectionIndex
!= 255 )
798 adjustReference((uint32_t)kind
, fromMappedAddr
, fromNewAddress
, toNewAddress
, deltaAdjust
, toSectionSlide
, imageStartAddress
, imageEndAddress
, pointersForASLR
, lastMappedAddr32
, lastKind
, lastToNewAddress
);
799 if ( _diagnostics
.hasError() )
808 template <typename P
>
809 void Adjustor
<P
>::adjustDataPointers(std::vector
<void*>& pointersForASLR
)
811 const uint8_t* p
= &_linkeditBias
[_dyldInfo
->rebase_off()];
812 const uint8_t* end
= &p
[_dyldInfo
->rebase_size()];
816 uint64_t segOffset
= 0;
820 while ( !done
&& (p
< end
) ) {
821 uint8_t immediate
= *p
& REBASE_IMMEDIATE_MASK
;
822 uint8_t opcode
= *p
& REBASE_OPCODE_MASK
;
825 case REBASE_OPCODE_DONE
:
828 case REBASE_OPCODE_SET_TYPE_IMM
:
831 case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
:
832 segIndex
= immediate
;
833 segOffset
= read_uleb128(p
, end
);
835 case REBASE_OPCODE_ADD_ADDR_ULEB
:
836 segOffset
+= read_uleb128(p
, end
);
838 case REBASE_OPCODE_ADD_ADDR_IMM_SCALED
:
839 segOffset
+= immediate
*sizeof(pint_t
);
841 case REBASE_OPCODE_DO_REBASE_IMM_TIMES
:
842 for (int i
=0; i
< immediate
; ++i
) {
843 slidePointer(segIndex
, segOffset
, type
, pointersForASLR
);
844 segOffset
+= sizeof(pint_t
);
847 case REBASE_OPCODE_DO_REBASE_ULEB_TIMES
:
848 count
= read_uleb128(p
, end
);
849 for (uint32_t i
=0; i
< count
; ++i
) {
850 slidePointer(segIndex
, segOffset
, type
, pointersForASLR
);
851 segOffset
+= sizeof(pint_t
);
854 case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB
:
855 slidePointer(segIndex
, segOffset
, type
, pointersForASLR
);
856 segOffset
+= read_uleb128(p
, end
) + sizeof(pint_t
);
858 case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB
:
859 count
= read_uleb128(p
, end
);
860 skip
= read_uleb128(p
, end
);
861 for (uint32_t i
=0; i
< count
; ++i
) {
862 slidePointer(segIndex
, segOffset
, type
, pointersForASLR
);
863 segOffset
+= skip
+ sizeof(pint_t
);
867 _diagnostics
.error("unknown rebase opcode 0x%02X in %s", opcode
, _installName
);
875 template <typename P
>
876 void Adjustor
<P
>::adjustInstruction(uint8_t kind
, uint64_t cacheOffset
, uint64_t codeToDataDelta
)
878 uint8_t* fixupLoc
= (uint8_t*)_cacheBuffer
+ cacheOffset
;
879 uint32_t* fixupLoc32
= (uint32_t*)fixupLoc
;
880 uint64_t* fixupLoc64
= (uint64_t*)fixupLoc
;
881 uint32_t instruction
;
886 case 1: // 32-bit pointer (including x86_64 RIP-rel)
887 value32
= P::E::get32(*fixupLoc32
);
888 value32
+= codeToDataDelta
;
889 P::E::set32(*fixupLoc32
, value32
);
891 case 2: // 64-bit pointer
892 value64
= P::E::get64(*fixupLoc64
);
893 value64
+= codeToDataDelta
;
894 P::E::set64(*fixupLoc64
, value64
);
896 case 4: // only used for i386, a reference to something in the IMPORT segment
898 case 5: // used by thumb2 movw
899 instruction
= P::E::get32(*fixupLoc32
);
900 // slide is always a multiple of 4096, so only top 4 bits of lo16 will ever need adjusting
901 value32
= (instruction
& 0x0000000F) + ((uint32_t)codeToDataDelta
>> 12);
902 instruction
= (instruction
& 0xFFFFFFF0) | (value32
& 0x0000000F);
903 P::E::set32(*fixupLoc32
, instruction
);
905 case 6: // used by ARM movw
906 instruction
= P::E::get32(*fixupLoc32
);
907 // slide is always a multiple of 4096, so only top 4 bits of lo16 will ever need adjusting
908 value32
= ((instruction
& 0x000F0000) >> 16) + ((uint32_t)codeToDataDelta
>> 12);
909 instruction
= (instruction
& 0xFFF0FFFF) | ((value32
<<16) & 0x000F0000);
910 P::E::set32(*fixupLoc32
, instruction
);
928 // used by thumb2 movt (low nibble of kind is high 4-bits of paired movw)
930 instruction
= P::E::get32(*fixupLoc32
);
931 assert((instruction
& 0x8000FBF0) == 0x0000F2C0);
932 // extract 16-bit value from instruction
933 uint32_t i
= ((instruction
& 0x00000400) >> 10);
934 uint32_t imm4
= (instruction
& 0x0000000F);
935 uint32_t imm3
= ((instruction
& 0x70000000) >> 28);
936 uint32_t imm8
= ((instruction
& 0x00FF0000) >> 16);
937 uint32_t imm16
= (imm4
<< 12) | (i
<< 11) | (imm3
<< 8) | imm8
;
938 // combine with codeToDataDelta and kind nibble
939 uint32_t targetValue
= (imm16
<< 16) | ((kind
& 0xF) << 12);
940 uint32_t newTargetValue
= targetValue
+ (uint32_t)codeToDataDelta
;
941 // construct new bits slices
942 uint32_t imm4_
= (newTargetValue
& 0xF0000000) >> 28;
943 uint32_t i_
= (newTargetValue
& 0x08000000) >> 27;
944 uint32_t imm3_
= (newTargetValue
& 0x07000000) >> 24;
945 uint32_t imm8_
= (newTargetValue
& 0x00FF0000) >> 16;
946 // update instruction to match codeToDataDelta
947 uint32_t newInstruction
= (instruction
& 0x8F00FBF0) | imm4_
| (i_
<< 10) | (imm3_
<< 28) | (imm8_
<< 16);
948 P::E::set32(*fixupLoc32
, newInstruction
);
967 // used by arm movt (low nibble of kind is high 4-bits of paired movw)
969 instruction
= P::E::get32(*fixupLoc32
);
970 // extract 16-bit value from instruction
971 uint32_t imm4
= ((instruction
& 0x000F0000) >> 16);
972 uint32_t imm12
= (instruction
& 0x00000FFF);
973 uint32_t imm16
= (imm4
<< 12) | imm12
;
974 // combine with codeToDataDelta and kind nibble
975 uint32_t targetValue
= (imm16
<< 16) | ((kind
& 0xF) << 12);
976 uint32_t newTargetValue
= targetValue
+ (uint32_t)codeToDataDelta
;
977 // construct new bits slices
978 uint32_t imm4_
= (newTargetValue
& 0xF0000000) >> 28;
979 uint32_t imm12_
= (newTargetValue
& 0x0FFF0000) >> 16;
980 // update instruction to match codeToDataDelta
981 uint32_t newInstruction
= (instruction
& 0xFFF0F000) | (imm4_
<< 16) | imm12_
;
982 P::E::set32(*fixupLoc32
, newInstruction
);
985 case 3: // used for arm64 ADRP
986 instruction
= P::E::get32(*fixupLoc32
);
987 if ( (instruction
& 0x9F000000) == 0x90000000 ) {
988 // codeToDataDelta is always a multiple of 4096, so only top 4 bits of lo16 will ever need adjusting
989 value64
= ((instruction
& 0x60000000) >> 17) | ((instruction
& 0x00FFFFE0) << 9);
990 value64
+= codeToDataDelta
;
991 instruction
= (instruction
& 0x9F00001F) | ((value64
<< 17) & 0x60000000) | ((value64
>> 9) & 0x00FFFFE0);
992 P::E::set32(*fixupLoc32
, instruction
);
1000 template <typename P
>
1001 void Adjustor
<P
>::adjustCode()
1003 // find compressed info on how code needs to be updated
1004 const uint8_t* infoStart
= &_linkeditBias
[_splitSegInfoCmd
->dataoff()];
1005 const uint8_t* infoEnd
= &infoStart
[_splitSegInfoCmd
->datasize()];;
1007 // This encoding only works if all data segments slide by the same amount
1008 uint64_t codeToDataDelta
= _segSlides
[1] - _segSlides
[0];
1010 // compressed data is: [ <kind> [uleb128-delta]+ <0> ] + <0>
1011 for (const uint8_t* p
= infoStart
; (*p
!= 0) && (p
< infoEnd
);) {
1012 uint8_t kind
= *p
++;
1013 uint64_t cacheOffset
= _mappingInfo
[0].dstCacheOffset
;
1014 while (uint64_t delta
= read_uleb128(p
, infoEnd
)) {
1015 cacheOffset
+= delta
;
1016 adjustInstruction(kind
, cacheOffset
, codeToDataDelta
);
1022 template <typename P
>
1023 void Adjustor
<P
>::adjustExportsTrie(std::vector
<uint8_t>& newTrieBytes
)
1025 // if no export info, nothing to adjust
1026 if ( _dyldInfo
->export_size() == 0 )
1029 // since export info addresses are offsets from mach_header, everything in __TEXT is fine
1030 // only __DATA addresses need to be updated
1031 const uint8_t* start
= &_linkeditBias
[_dyldInfo
->export_off()];
1032 const uint8_t* end
= &start
[_dyldInfo
->export_size()];
1033 std::vector
<ExportInfoTrie::Entry
> originalExports
;
1034 if ( !ExportInfoTrie::parseTrie(start
, end
, originalExports
) ) {
1035 _diagnostics
.error("malformed exports trie in %s", _installName
);
1039 std::vector
<ExportInfoTrie::Entry
> newExports
;
1040 newExports
.reserve(originalExports
.size());
1041 uint64_t baseAddress
= _segOrigStartAddresses
[0];
1042 uint64_t baseAddressSlide
= slideForOrigAddress(baseAddress
);
1043 for (auto& entry
: originalExports
) {
1044 // remove symbols used by the static linker only
1045 if ( (strncmp(entry
.name
.c_str(), "$ld$", 4) == 0)
1046 || (strncmp(entry
.name
.c_str(), ".objc_class_name",16) == 0)
1047 || (strncmp(entry
.name
.c_str(), ".objc_category_name",19) == 0) ) {
1050 // adjust symbols in slid segments
1051 if ( (entry
.info
.flags
& EXPORT_SYMBOL_FLAGS_KIND_MASK
) != EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
)
1052 entry
.info
.address
+= (slideForOrigAddress(entry
.info
.address
+ baseAddress
) - baseAddressSlide
);
1053 newExports
.push_back(entry
);
1056 // rebuild export trie
1057 newTrieBytes
.reserve(_dyldInfo
->export_size());
1059 ExportInfoTrie(newExports
).emit(newTrieBytes
);
1061 while ( (newTrieBytes
.size() % sizeof(pint_t
)) != 0 )
1062 newTrieBytes
.push_back(0);
1066 } // anonymous namespace
1069 void adjustDylibSegments(DyldSharedCache
* cache
, bool is64
, mach_header
* mhInCache
, const std::vector
<CacheBuilder::SegmentMappingInfo
>& mappingInfo
, std::vector
<void*>& pointersForASLR
, Diagnostics
& diag
)
1072 Adjustor
<Pointer64
<LittleEndian
>> adjustor64(cache
, (macho_header
<Pointer64
<LittleEndian
>>*)mhInCache
, mappingInfo
, diag
);
1073 adjustor64
.adjustImageForNewSegmentLocations(pointersForASLR
);
1076 Adjustor
<Pointer32
<LittleEndian
>> adjustor32(cache
, (macho_header
<Pointer32
<LittleEndian
>>*)mhInCache
, mappingInfo
, diag
);
1077 adjustor32
.adjustImageForNewSegmentLocations(pointersForASLR
);