]> git.saurik.com Git - apple/ld64.git/blob - src/other/rebase.cpp
d1e319408f31d38de08c05be100c8494424f2870
[apple/ld64.git] / src / other / rebase.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2006-2008 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
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
12 * file.
13 *
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.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include <mach/mach.h>
29 #include <limits.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <vector>
36 #include <set>
37
38
39 #include "MachOFileAbstraction.hpp"
40 #include "Architectures.hpp"
41
42 static bool verbose = false;
43
44 __attribute__((noreturn))
45 void throwf(const char* format, ...)
46 {
47 va_list list;
48 char* p;
49 va_start(list, format);
50 vasprintf(&p, format, list);
51 va_end(list);
52
53 const char* t = p;
54 throw t;
55 }
56
57
58 class AbstractRebaser
59 {
60 public:
61 virtual cpu_type_t getArchitecture() const = 0;
62 virtual uint64_t getBaseAddress() const = 0;
63 virtual uint64_t getVMSize() const = 0;
64 virtual void setBaseAddress(uint64_t) = 0;
65 };
66
67
68 template <typename A>
69 class Rebaser : public AbstractRebaser
70 {
71 public:
72 Rebaser(const void* machHeader);
73 virtual ~Rebaser() {}
74
75 virtual cpu_type_t getArchitecture() const;
76 virtual uint64_t getBaseAddress() const;
77 virtual uint64_t getVMSize() const;
78 virtual void setBaseAddress(uint64_t);
79
80 private:
81 typedef typename A::P P;
82 typedef typename A::P::E E;
83 typedef typename A::P::uint_t pint_t;
84
85 struct vmmap { pint_t vmaddr; pint_t vmsize; pint_t fileoff; };
86
87 void setRelocBase();
88 void buildSectionTable();
89 void adjustLoadCommands();
90 void adjustSymbolTable();
91 void adjustDATA();
92 void doLocalRelocation(const macho_relocation_info<P>* reloc);
93 pint_t* mappedAddressForVMAddress(uint32_t vmaddress);
94 void rebaseAt(int segIndex, uint64_t offset, uint8_t type);
95
96 const macho_header<P>* fHeader;
97 pint_t fOrignalVMRelocBaseAddress;
98 pint_t fSlide;
99 std::vector<vmmap> fVMMApping;
100 };
101
102
103
104 class MultiArchRebaser
105 {
106 public:
107 MultiArchRebaser(const char* path, bool writable=false);
108 ~MultiArchRebaser();
109
110 const std::vector<AbstractRebaser*>& getArchs() const { return fRebasers; }
111 void commit();
112
113 private:
114 std::vector<AbstractRebaser*> fRebasers;
115 void* fMappingAddress;
116 uint64_t fFileSize;
117 };
118
119
120
121 MultiArchRebaser::MultiArchRebaser(const char* path, bool writable)
122 : fMappingAddress(0), fFileSize(0)
123 {
124 // map in whole file
125 int fd = ::open(path, (writable ? O_RDWR : O_RDONLY), 0);
126 if ( fd == -1 )
127 throwf("can't open file %s, errno=%d", path, errno);
128 struct stat stat_buf;
129 if ( fstat(fd, &stat_buf) == -1)
130 throwf("can't stat open file %s, errno=%d", path, errno);
131 if ( stat_buf.st_size < 20 )
132 throwf("file too small %s", path);
133 const int prot = writable ? (PROT_READ | PROT_WRITE) : PROT_READ;
134 const int flags = writable ? (MAP_FILE | MAP_SHARED) : (MAP_FILE | MAP_PRIVATE);
135 uint8_t* p = (uint8_t*)::mmap(NULL, stat_buf.st_size, prot, flags, fd, 0);
136 if ( p == (uint8_t*)(-1) )
137 throwf("can't map file %s, errno=%d", path, errno);
138 ::close(fd);
139
140 // if fat file, process each architecture
141 const fat_header* fh = (fat_header*)p;
142 const mach_header* mh = (mach_header*)p;
143 if ( fh->magic == OSSwapBigToHostInt32(FAT_MAGIC) ) {
144 // Fat header is always big-endian
145 const struct fat_arch* archs = (struct fat_arch*)(p + sizeof(struct fat_header));
146 for (unsigned long i=0; i < OSSwapBigToHostInt32(fh->nfat_arch); ++i) {
147 uint32_t fileOffset = OSSwapBigToHostInt32(archs[i].offset);
148 try {
149 switch ( OSSwapBigToHostInt32(archs[i].cputype) ) {
150 case CPU_TYPE_POWERPC:
151 fRebasers.push_back(new Rebaser<ppc>(&p[fileOffset]));
152 break;
153 case CPU_TYPE_POWERPC64:
154 fRebasers.push_back(new Rebaser<ppc64>(&p[fileOffset]));
155 break;
156 case CPU_TYPE_I386:
157 fRebasers.push_back(new Rebaser<x86>(&p[fileOffset]));
158 break;
159 case CPU_TYPE_X86_64:
160 fRebasers.push_back(new Rebaser<x86_64>(&p[fileOffset]));
161 break;
162 case CPU_TYPE_ARM:
163 fRebasers.push_back(new Rebaser<arm>(&p[fileOffset]));
164 break;
165 default:
166 throw "unknown file format";
167 }
168 }
169 catch (const char* msg) {
170 fprintf(stderr, "rebase warning: %s for %s\n", msg, path);
171 }
172 }
173 }
174 else {
175 try {
176 if ( (OSSwapBigToHostInt32(mh->magic) == MH_MAGIC) && (OSSwapBigToHostInt32(mh->cputype) == CPU_TYPE_POWERPC)) {
177 fRebasers.push_back(new Rebaser<ppc>(mh));
178 }
179 else if ( (OSSwapBigToHostInt32(mh->magic) == MH_MAGIC_64) && (OSSwapBigToHostInt32(mh->cputype) == CPU_TYPE_POWERPC64)) {
180 fRebasers.push_back(new Rebaser<ppc64>(mh));
181 }
182 else if ( (OSSwapLittleToHostInt32(mh->magic) == MH_MAGIC) && (OSSwapLittleToHostInt32(mh->cputype) == CPU_TYPE_I386)) {
183 fRebasers.push_back(new Rebaser<x86>(mh));
184 }
185 else if ( (OSSwapLittleToHostInt32(mh->magic) == MH_MAGIC_64) && (OSSwapLittleToHostInt32(mh->cputype) == CPU_TYPE_X86_64)) {
186 fRebasers.push_back(new Rebaser<x86_64>(mh));
187 }
188 else if ( (OSSwapLittleToHostInt32(mh->magic) == MH_MAGIC) && (OSSwapLittleToHostInt32(mh->cputype) == CPU_TYPE_ARM)) {
189 fRebasers.push_back(new Rebaser<arm>(mh));
190 }
191 else {
192 throw "unknown file format";
193 }
194 }
195 catch (const char* msg) {
196 fprintf(stderr, "rebase warning: %s for %s\n", msg, path);
197 }
198 }
199
200 fMappingAddress = p;
201 fFileSize = stat_buf.st_size;
202 }
203
204
205 MultiArchRebaser::~MultiArchRebaser()
206 {
207 ::munmap(fMappingAddress, fFileSize);
208 }
209
210 void MultiArchRebaser::commit()
211 {
212 ::msync(fMappingAddress, fFileSize, MS_ASYNC);
213 }
214
215
216
217 template <typename A>
218 Rebaser<A>::Rebaser(const void* machHeader)
219 : fHeader((const macho_header<P>*)machHeader)
220 {
221 switch ( fHeader->filetype() ) {
222 case MH_DYLIB:
223 if ( (fHeader->flags() & MH_SPLIT_SEGS) != 0 )
224 throw "split-seg dylibs cannot be rebased";
225 break;
226 case MH_BUNDLE:
227 break;
228 default:
229 throw "file is not a dylib or bundle";
230 }
231
232 }
233
234 template <> cpu_type_t Rebaser<ppc>::getArchitecture() const { return CPU_TYPE_POWERPC; }
235 template <> cpu_type_t Rebaser<ppc64>::getArchitecture() const { return CPU_TYPE_POWERPC64; }
236 template <> cpu_type_t Rebaser<x86>::getArchitecture() const { return CPU_TYPE_I386; }
237 template <> cpu_type_t Rebaser<x86_64>::getArchitecture() const { return CPU_TYPE_X86_64; }
238 template <> cpu_type_t Rebaser<arm>::getArchitecture() const { return CPU_TYPE_ARM; }
239
240 template <typename A>
241 uint64_t Rebaser<A>::getBaseAddress() const
242 {
243 uint64_t lowestSegmentAddress = LLONG_MAX;
244 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>));
245 const uint32_t cmd_count = fHeader->ncmds();
246 const macho_load_command<P>* cmd = cmds;
247 for (uint32_t i = 0; i < cmd_count; ++i) {
248 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
249 const macho_segment_command<P>* segCmd = (const macho_segment_command<P>*)cmd;
250 if ( segCmd->vmaddr() < lowestSegmentAddress ) {
251 lowestSegmentAddress = segCmd->vmaddr();
252 }
253 }
254 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
255 }
256 return lowestSegmentAddress;
257 }
258
259 template <typename A>
260 uint64_t Rebaser<A>::getVMSize() const
261 {
262 const macho_segment_command<P>* highestSegmentCmd = NULL;
263 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>));
264 const uint32_t cmd_count = fHeader->ncmds();
265 const macho_load_command<P>* cmd = cmds;
266 for (uint32_t i = 0; i < cmd_count; ++i) {
267 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
268 const macho_segment_command<P>* segCmd = (const macho_segment_command<P>*)cmd;
269 if ( (highestSegmentCmd == NULL) || (segCmd->vmaddr() > highestSegmentCmd->vmaddr()) ) {
270 highestSegmentCmd = segCmd;
271 }
272 }
273 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
274 }
275
276 return ((highestSegmentCmd->vmaddr() + highestSegmentCmd->vmsize() - this->getBaseAddress() + 4095) & (-4096));
277 }
278
279
280 template <typename A>
281 void Rebaser<A>::setBaseAddress(uint64_t addr)
282 {
283 // calculate slide
284 fSlide = addr - this->getBaseAddress();
285
286 // compute base address for relocations
287 this->setRelocBase();
288
289 // build cache of section index to section
290 this->buildSectionTable();
291
292 // update load commands
293 this->adjustLoadCommands();
294
295 // update symbol table
296 this->adjustSymbolTable();
297
298 // update writable segments that have internal pointers
299 this->adjustDATA();
300 }
301
302 template <typename A>
303 void Rebaser<A>::adjustLoadCommands()
304 {
305 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>));
306 const uint32_t cmd_count = fHeader->ncmds();
307 const macho_load_command<P>* cmd = cmds;
308 for (uint32_t i = 0; i < cmd_count; ++i) {
309 switch ( cmd->cmd() ) {
310 case LC_ID_DYLIB:
311 if ( (fHeader->flags() & MH_PREBOUND) != 0 ) {
312 // clear timestamp so that any prebound clients are invalidated
313 macho_dylib_command<P>* dylib = (macho_dylib_command<P>*)cmd;
314 dylib->set_timestamp(1);
315 }
316 break;
317 case LC_LOAD_DYLIB:
318 case LC_LOAD_WEAK_DYLIB:
319 case LC_REEXPORT_DYLIB:
320 case LC_LOAD_UPWARD_DYLIB:
321 if ( (fHeader->flags() & MH_PREBOUND) != 0 ) {
322 // clear expected timestamps so that this image will load with invalid prebinding
323 macho_dylib_command<P>* dylib = (macho_dylib_command<P>*)cmd;
324 dylib->set_timestamp(2);
325 }
326 break;
327 case macho_routines_command<P>::CMD:
328 // update -init command
329 {
330 macho_routines_command<P>* routines = (macho_routines_command<P>*)cmd;
331 routines->set_init_address(routines->init_address() + fSlide);
332 }
333 break;
334 case macho_segment_command<P>::CMD:
335 // update segment commands
336 {
337 macho_segment_command<P>* seg = (macho_segment_command<P>*)cmd;
338 seg->set_vmaddr(seg->vmaddr() + fSlide);
339 macho_section<P>* const sectionsStart = (macho_section<P>*)((char*)seg + sizeof(macho_segment_command<P>));
340 macho_section<P>* const sectionsEnd = &sectionsStart[seg->nsects()];
341 for(macho_section<P>* sect = sectionsStart; sect < sectionsEnd; ++sect) {
342 sect->set_addr(sect->addr() + fSlide);
343 }
344 }
345 break;
346 }
347 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
348 }
349 }
350
351
352 template <typename A>
353 void Rebaser<A>::buildSectionTable()
354 {
355 // build vector of sections
356 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>));
357 const uint32_t cmd_count = fHeader->ncmds();
358 const macho_load_command<P>* cmd = cmds;
359 for (uint32_t i = 0; i < cmd_count; ++i) {
360 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
361 const macho_segment_command<P>* seg = (macho_segment_command<P>*)cmd;
362 vmmap mapping;
363 mapping.vmaddr = seg->vmaddr();
364 mapping.vmsize = seg->vmsize();
365 mapping.fileoff = seg->fileoff();
366 fVMMApping.push_back(mapping);
367 }
368 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
369 }
370 }
371
372
373 template <typename A>
374 void Rebaser<A>::adjustSymbolTable()
375 {
376 const macho_dysymtab_command<P>* dysymtab = NULL;
377 macho_nlist<P>* symbolTable = NULL;
378 const char* strings = NULL;
379
380 // get symbol table info
381 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>));
382 const uint32_t cmd_count = fHeader->ncmds();
383 const macho_load_command<P>* cmd = cmds;
384 for (uint32_t i = 0; i < cmd_count; ++i) {
385 switch (cmd->cmd()) {
386 case LC_SYMTAB:
387 {
388 const macho_symtab_command<P>* symtab = (macho_symtab_command<P>*)cmd;
389 symbolTable = (macho_nlist<P>*)(((uint8_t*)fHeader) + symtab->symoff());
390 strings = (char*)(((uint8_t*)fHeader) + symtab->stroff());
391 }
392 break;
393 case LC_DYSYMTAB:
394 dysymtab = (macho_dysymtab_command<P>*)cmd;
395 break;
396 }
397 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
398 }
399
400 // walk all exports and slide their n_value
401 macho_nlist<P>* lastExport = &symbolTable[dysymtab->iextdefsym()+dysymtab->nextdefsym()];
402 for (macho_nlist<P>* entry = &symbolTable[dysymtab->iextdefsym()]; entry < lastExport; ++entry) {
403 if ( (entry->n_type() & N_TYPE) == N_SECT )
404 entry->set_n_value(entry->n_value() + fSlide);
405 }
406
407 // walk all local symbols and slide their n_value
408 macho_nlist<P>* lastLocal = &symbolTable[dysymtab->ilocalsym()+dysymtab->nlocalsym()];
409 for (macho_nlist<P>* entry = &symbolTable[dysymtab->ilocalsym()]; entry < lastLocal; ++entry) {
410 if ( ((entry->n_type() & N_STAB) == 0) && ((entry->n_type() & N_TYPE) == N_SECT) ) {
411 entry->set_n_value(entry->n_value() + fSlide);
412 }
413 else if ( entry->n_type() & N_STAB ) {
414 // some stabs need to be slid too
415 switch ( entry->n_type() ) {
416 case N_FUN:
417 // don't slide end-of-function FUN which is FUN with no string
418 if ( (entry->n_strx() == 0) || (strings[entry->n_strx()] == '\0') )
419 break;
420 case N_BNSYM:
421 case N_STSYM:
422 case N_LCSYM:
423 entry->set_n_value(entry->n_value() + fSlide);
424 break;
425 }
426 }
427 }
428
429 // FIXME ¥¥¥ adjust dylib_module if it exists
430 }
431
432 static uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end)
433 {
434 uint64_t result = 0;
435 int bit = 0;
436 do {
437 if (p == end)
438 throwf("malformed uleb128");
439
440 uint64_t slice = *p & 0x7f;
441
442 if (bit >= 64 || slice << bit >> bit != slice)
443 throwf("uleb128 too big");
444 else {
445 result |= (slice << bit);
446 bit += 7;
447 }
448 }
449 while (*p++ & 0x80);
450 return result;
451 }
452
453 template <typename A>
454 void Rebaser<A>::rebaseAt(int segIndex, uint64_t offset, uint8_t type)
455 {
456 //fprintf(stderr, "rebaseAt(seg=%d, offset=0x%08llX, type=%d\n", segIndex, offset, type);
457 static int lastSegIndex = -1;
458 static uint8_t* lastSegMappedStart = NULL;
459 if ( segIndex != lastSegIndex ) {
460 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>));
461 const uint32_t cmd_count = fHeader->ncmds();
462 const macho_load_command<P>* cmd = cmds;
463 int segCount = 0;
464 for (uint32_t i = 0; i < cmd_count; ++i) {
465 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
466 if ( segIndex == segCount ) {
467 const macho_segment_command<P>* seg = (macho_segment_command<P>*)cmd;
468 lastSegMappedStart = (uint8_t*)fHeader + seg->fileoff();
469 lastSegIndex = segCount;
470 break;
471 }
472 ++segCount;
473 }
474 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
475 }
476 }
477
478 pint_t* locationToFix = (pint_t*)(lastSegMappedStart+offset);
479 uint32_t* locationToFix32 = (uint32_t*)(lastSegMappedStart+offset);
480 switch (type) {
481 case REBASE_TYPE_POINTER:
482 P::setP(*locationToFix, A::P::getP(*locationToFix) + fSlide);
483 break;
484 case REBASE_TYPE_TEXT_ABSOLUTE32:
485 E::set32(*locationToFix32, E::get32(*locationToFix32) + fSlide);
486 break;
487 default:
488 throwf("bad rebase type %d", type);
489 }
490 }
491
492
493 template <typename A>
494 void Rebaser<A>::adjustDATA()
495 {
496 const macho_dysymtab_command<P>* dysymtab = NULL;
497 const macho_dyld_info_command<P>* dyldInfo = NULL;
498
499 // get symbol table info
500 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>));
501 const uint32_t cmd_count = fHeader->ncmds();
502 const macho_load_command<P>* cmd = cmds;
503 for (uint32_t i = 0; i < cmd_count; ++i) {
504 switch (cmd->cmd()) {
505 case LC_DYSYMTAB:
506 dysymtab = (macho_dysymtab_command<P>*)cmd;
507 break;
508 case LC_DYLD_INFO:
509 case LC_DYLD_INFO_ONLY:
510 dyldInfo = (macho_dyld_info_command<P>*)cmd;
511 break;
512 }
513 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
514 }
515
516 // use new encoding of rebase info if present
517 if ( dyldInfo != NULL ) {
518 if ( dyldInfo->rebase_size() != 0 ) {
519 const uint8_t* p = (uint8_t*)fHeader + dyldInfo->rebase_off();
520 const uint8_t* end = &p[dyldInfo->rebase_size()];
521
522 uint8_t type = 0;
523 uint64_t offset = 0;
524 uint32_t count;
525 uint32_t skip;
526 int segIndex;
527 bool done = false;
528 while ( !done && (p < end) ) {
529 uint8_t immediate = *p & REBASE_IMMEDIATE_MASK;
530 uint8_t opcode = *p & REBASE_OPCODE_MASK;
531 ++p;
532 switch (opcode) {
533 case REBASE_OPCODE_DONE:
534 done = true;
535 break;
536 case REBASE_OPCODE_SET_TYPE_IMM:
537 type = immediate;
538 break;
539 case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
540 segIndex = immediate;
541 offset = read_uleb128(p, end);
542 break;
543 case REBASE_OPCODE_ADD_ADDR_ULEB:
544 offset += read_uleb128(p, end);
545 break;
546 case REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
547 offset += immediate*sizeof(pint_t);
548 break;
549 case REBASE_OPCODE_DO_REBASE_IMM_TIMES:
550 for (int i=0; i < immediate; ++i) {
551 rebaseAt(segIndex, offset, type);
552 offset += sizeof(pint_t);
553 }
554 break;
555 case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
556 count = read_uleb128(p, end);
557 for (uint32_t i=0; i < count; ++i) {
558 rebaseAt(segIndex, offset, type);
559 offset += sizeof(pint_t);
560 }
561 break;
562 case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
563 rebaseAt(segIndex, offset, type);
564 offset += read_uleb128(p, end) + sizeof(pint_t);
565 break;
566 case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
567 count = read_uleb128(p, end);
568 skip = read_uleb128(p, end);
569 for (uint32_t i=0; i < count; ++i) {
570 rebaseAt(segIndex, offset, type);
571 offset += skip + sizeof(pint_t);
572 }
573 break;
574 default:
575 throwf("bad rebase opcode %d", *p);
576 }
577 }
578
579
580
581 }
582 }
583 else {
584 // walk all local relocations and slide every pointer
585 const macho_relocation_info<P>* const relocsStart = (macho_relocation_info<P>*)(((uint8_t*)fHeader) + dysymtab->locreloff());
586 const macho_relocation_info<P>* const relocsEnd = &relocsStart[dysymtab->nlocrel()];
587 for (const macho_relocation_info<P>* reloc=relocsStart; reloc < relocsEnd; ++reloc) {
588 this->doLocalRelocation(reloc);
589 }
590
591 // walk non-lazy-pointers and slide the ones that are LOCAL
592 cmd = cmds;
593 for (uint32_t i = 0; i < cmd_count; ++i) {
594 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
595 const macho_segment_command<P>* seg = (macho_segment_command<P>*)cmd;
596 const macho_section<P>* const sectionsStart = (macho_section<P>*)((char*)seg + sizeof(macho_segment_command<P>));
597 const macho_section<P>* const sectionsEnd = &sectionsStart[seg->nsects()];
598 const uint32_t* const indirectTable = (uint32_t*)(((uint8_t*)fHeader) + dysymtab->indirectsymoff());
599 for(const macho_section<P>* sect = sectionsStart; sect < sectionsEnd; ++sect) {
600 if ( (sect->flags() & SECTION_TYPE) == S_NON_LAZY_SYMBOL_POINTERS ) {
601 const uint32_t indirectTableOffset = sect->reserved1();
602 uint32_t pointerCount = sect->size() / sizeof(pint_t);
603 pint_t* nonLazyPointer = (pint_t*)(((uint8_t*)fHeader) + sect->offset());
604 for (uint32_t i=0; i < pointerCount; ++i, ++nonLazyPointer) {
605 if ( E::get32(indirectTable[indirectTableOffset + i]) == INDIRECT_SYMBOL_LOCAL ) {
606 P::setP(*nonLazyPointer, A::P::getP(*nonLazyPointer) + fSlide);
607 }
608 }
609 }
610 }
611 }
612 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
613 }
614 }
615 }
616
617
618 template <typename A>
619 typename A::P::uint_t* Rebaser<A>::mappedAddressForVMAddress(uint32_t vmaddress)
620 {
621 for(typename std::vector<vmmap>::iterator it = fVMMApping.begin(); it != fVMMApping.end(); ++it) {
622 //fprintf(stderr, "vmaddr=0x%08lX, vmsize=0x%08lX\n", it->vmaddr, it->vmsize);
623 if ( (vmaddress >= it->vmaddr) && (vmaddress < (it->vmaddr+it->vmsize)) ) {
624 return (pint_t*)((vmaddress - it->vmaddr) + it->fileoff + (uint8_t*)fHeader);
625 }
626 }
627 throwf("reloc address 0x%08X not found", vmaddress);
628 }
629
630
631 template <>
632 void Rebaser<x86_64>::doLocalRelocation(const macho_relocation_info<x86_64::P>* reloc)
633 {
634 if ( reloc->r_type() == X86_64_RELOC_UNSIGNED ) {
635 pint_t* addr = mappedAddressForVMAddress(reloc->r_address() + fOrignalVMRelocBaseAddress);
636 P::setP(*addr, P::getP(*addr) + fSlide);
637 }
638 else {
639 throw "invalid relocation type";
640 }
641 }
642
643 template <>
644 void Rebaser<ppc>::doLocalRelocation(const macho_relocation_info<P>* reloc)
645 {
646 if ( (reloc->r_address() & R_SCATTERED) == 0 ) {
647 if ( reloc->r_type() == GENERIC_RELOC_VANILLA ) {
648 pint_t* addr = mappedAddressForVMAddress(reloc->r_address() + fOrignalVMRelocBaseAddress);
649 P::setP(*addr, P::getP(*addr) + fSlide);
650 }
651 }
652 else {
653 throw "cannot rebase final linked image with scattered relocations";
654 }
655 }
656
657 template <>
658 void Rebaser<x86>::doLocalRelocation(const macho_relocation_info<P>* reloc)
659 {
660 if ( (reloc->r_address() & R_SCATTERED) == 0 ) {
661 if ( reloc->r_type() == GENERIC_RELOC_VANILLA ) {
662 pint_t* addr = mappedAddressForVMAddress(reloc->r_address() + fOrignalVMRelocBaseAddress);
663 P::setP(*addr, P::getP(*addr) + fSlide);
664 }
665 }
666 else {
667 macho_scattered_relocation_info<P>* sreloc = (macho_scattered_relocation_info<P>*)reloc;
668 if ( sreloc->r_type() == GENERIC_RELOC_PB_LA_PTR ) {
669 sreloc->set_r_value( sreloc->r_value() + fSlide );
670 }
671 else {
672 throw "cannot rebase final linked image with scattered relocations";
673 }
674 }
675 }
676
677 #if SUPPORT_ARCH_arm_any
678 template <>
679 void Rebaser<arm>::doLocalRelocation(const macho_relocation_info<P>* reloc)
680 {
681 if ( (reloc->r_address() & R_SCATTERED) == 0 ) {
682 if ( reloc->r_type() == ARM_RELOC_VANILLA ) {
683 pint_t* addr = mappedAddressForVMAddress(reloc->r_address() + fOrignalVMRelocBaseAddress);
684 P::setP(*addr, P::getP(*addr) + fSlide);
685 }
686 }
687 else {
688 macho_scattered_relocation_info<P>* sreloc = (macho_scattered_relocation_info<P>*)reloc;
689 if ( sreloc->r_type() == ARM_RELOC_PB_LA_PTR ) {
690 sreloc->set_r_value( sreloc->r_value() + fSlide );
691 }
692 else {
693 throw "cannot rebase final linked image with scattered relocations";
694 }
695 }
696 }
697 #endif
698
699 template <typename A>
700 void Rebaser<A>::doLocalRelocation(const macho_relocation_info<P>* reloc)
701 {
702 if ( (reloc->r_address() & R_SCATTERED) == 0 ) {
703 if ( reloc->r_type() == GENERIC_RELOC_VANILLA ) {
704 pint_t* addr = mappedAddressForVMAddress(reloc->r_address() + fOrignalVMRelocBaseAddress);
705 P::setP(*addr, P::getP(*addr) + fSlide);
706 }
707 }
708 else {
709 throw "cannot rebase final linked image with scattered relocations";
710 }
711 }
712
713
714 template <typename A>
715 void Rebaser<A>::setRelocBase()
716 {
717 // reloc addresses are from the start of the mapped file (base address)
718 fOrignalVMRelocBaseAddress = this->getBaseAddress();
719 //fprintf(stderr, "fOrignalVMRelocBaseAddress=0x%08X\n", fOrignalVMRelocBaseAddress);
720 }
721
722 template <>
723 void Rebaser<ppc64>::setRelocBase()
724 {
725 // reloc addresses either:
726 // 1) from the base address if no writable segment is > 4GB from base address
727 // 2) from start of first writable segment
728 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>));
729 const uint32_t cmd_count = fHeader->ncmds();
730 const macho_load_command<P>* cmd = cmds;
731 for (uint32_t i = 0; i < cmd_count; ++i) {
732 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
733 const macho_segment_command<P>* segCmd = (const macho_segment_command<P>*)cmd;
734 if ( segCmd->initprot() & VM_PROT_WRITE ) {
735 if ( (segCmd->vmaddr() + segCmd->vmsize() - this->getBaseAddress()) > 0x100000000ULL ) {
736 // found writable segment with address > 4GB past base address
737 fOrignalVMRelocBaseAddress = segCmd->vmaddr();
738 return;
739 }
740 }
741 }
742 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
743 }
744 // just use base address
745 fOrignalVMRelocBaseAddress = this->getBaseAddress();
746 }
747
748 template <>
749 void Rebaser<x86_64>::setRelocBase()
750 {
751 // reloc addresses are always based from the start of the first writable segment
752 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>));
753 const uint32_t cmd_count = fHeader->ncmds();
754 const macho_load_command<P>* cmd = cmds;
755 for (uint32_t i = 0; i < cmd_count; ++i) {
756 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
757 const macho_segment_command<P>* segCmd = (const macho_segment_command<P>*)cmd;
758 if ( segCmd->initprot() & VM_PROT_WRITE ) {
759 fOrignalVMRelocBaseAddress = segCmd->vmaddr();
760 return;
761 }
762 }
763 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
764 }
765 throw "no writable segment";
766 }
767
768
769 static void copyFile(const char* srcFile, const char* dstFile)
770 {
771 // open files
772 int src = open(srcFile, O_RDONLY);
773 if ( src == -1 )
774 throwf("can't open file %s, errno=%d", srcFile, errno);
775 struct stat stat_buf;
776 if ( fstat(src, &stat_buf) == -1)
777 throwf("can't stat open file %s, errno=%d", srcFile, errno);
778
779 // create new file with all same permissions to hold copy of dylib
780 ::unlink(dstFile);
781 int dst = open(dstFile, O_CREAT | O_RDWR | O_TRUNC, stat_buf.st_mode);
782 if ( dst == -1 )
783 throwf("can't create temp file %s, errnor=%d", dstFile, errno);
784
785 // mark source as "don't cache"
786 (void)fcntl(src, F_NOCACHE, 1);
787 // we want to cache the dst because we are about to map it in and modify it
788
789 // copy permission bits
790 if ( chmod(dstFile, stat_buf.st_mode & 07777) == -1 )
791 throwf("can't chmod temp file %s, errno=%d", dstFile, errno);
792 if ( chown(dstFile, stat_buf.st_uid, stat_buf.st_gid) == -1)
793 throwf("can't chown temp file %s, errno=%d", dstFile, errno);
794
795 // copy contents
796 ssize_t len;
797 const uint32_t kBufferSize = 128*1024;
798 static uint8_t* buffer = NULL;
799 if ( buffer == NULL ) {
800 vm_address_t addr = 0;
801 if ( vm_allocate(mach_task_self(), &addr, kBufferSize, true /*find range*/) == KERN_SUCCESS )
802 buffer = (uint8_t*)addr;
803 else
804 throw "can't allcoate copy buffer";
805 }
806 while ( (len = read(src, buffer, kBufferSize)) > 0 ) {
807 if ( write(dst, buffer, len) == -1 )
808 throwf("write failure copying feil %s, errno=%d", dstFile, errno);
809 }
810
811 // close files
812 int result1 = close(dst);
813 int result2 = close(src);
814 if ( (result1 != 0) || (result2 != 0) )
815 throw "can't close file";
816 }
817
818
819 // scan dylibs and collect size info
820 // calculate new base address for each dylib
821 // rebase each file
822 // copy to temp and mmap
823 // update content
824 // unmap/flush
825 // rename
826
827 struct archInfo {
828 cpu_type_t arch;
829 uint64_t vmSize;
830 uint64_t orgBase;
831 uint64_t newBase;
832 };
833
834 struct fileInfo
835 {
836 fileInfo(const char* p) : path(p) {}
837
838 const char* path;
839 std::vector<archInfo> archs;
840 };
841
842 //
843 // add archInfos to fileInfo for every slice of a fat file
844 // for ppc, there may be duplicate architectures (with different sub-types)
845 //
846 static void setSizes(fileInfo& info, const std::set<cpu_type_t>& onlyArchs)
847 {
848 const MultiArchRebaser mar(info.path);
849 const std::vector<AbstractRebaser*>& rebasers = mar.getArchs();
850 for(std::set<cpu_type_t>::iterator ait=onlyArchs.begin(); ait != onlyArchs.end(); ++ait) {
851 for(std::vector<AbstractRebaser*>::const_iterator rit=rebasers.begin(); rit != rebasers.end(); ++rit) {
852 AbstractRebaser* rebaser = *rit;
853 if ( rebaser->getArchitecture() == *ait ) {
854 archInfo ai;
855 ai.arch = *ait;
856 ai.vmSize = rebaser->getVMSize();
857 ai.orgBase = rebaser->getBaseAddress();
858 ai.newBase = 0;
859 //fprintf(stderr, "base=0x%llX, size=0x%llX\n", ai.orgBase, ai.vmSize);
860 info.archs.push_back(ai);
861 }
862 }
863 }
864 }
865
866 static const char* nameForArch(cpu_type_t arch)
867 {
868 switch( arch ) {
869 case CPU_TYPE_POWERPC:
870 return "ppc";
871 case CPU_TYPE_POWERPC64:
872 return "ppca64";
873 case CPU_TYPE_I386:
874 return "i386";
875 case CPU_TYPE_X86_64:
876 return "x86_64";
877 case CPU_TYPE_ARM:
878 return "arm";
879 }
880 return "unknown";
881 }
882
883 static void rebase(const fileInfo& info)
884 {
885 // generate temp file name
886 char realFilePath[PATH_MAX];
887 if ( realpath(info.path, realFilePath) == NULL ) {
888 throwf("realpath() failed on %s, errno=%d", info.path, errno);
889 }
890 const char* tempPath;
891 asprintf((char**)&tempPath, "%s_rebase", realFilePath);
892
893 // copy whole file to temp file
894 copyFile(info.path, tempPath);
895
896 try {
897 // rebase temp file
898 MultiArchRebaser mar(tempPath, true);
899 const std::vector<AbstractRebaser*>& rebasers = mar.getArchs();
900 for(std::vector<archInfo>::const_iterator fait=info.archs.begin(); fait != info.archs.end(); ++fait) {
901 for(std::vector<AbstractRebaser*>::const_iterator rit=rebasers.begin(); rit != rebasers.end(); ++rit) {
902 if ( (*rit)->getArchitecture() == fait->arch ) {
903 (*rit)->setBaseAddress(fait->newBase);
904 if ( verbose )
905 printf("%8s 0x%0llX -> 0x%0llX %s\n", nameForArch(fait->arch), fait->orgBase, fait->newBase, info.path);
906 }
907 }
908 }
909
910 // flush temp file out to disk
911 mar.commit();
912
913 // rename
914 int result = rename(tempPath, info.path);
915 if ( result != 0 ) {
916 throwf("can't swap temporary rebased file: rename(%s,%s) returned errno=%d", tempPath, info.path, errno);
917 }
918
919 // make sure every really gets out to disk
920 ::sync();
921 }
922 catch (const char* msg) {
923 // delete temp file
924 ::unlink(tempPath);
925
926 // throw exception with file name added
927 const char* newMsg;
928 asprintf((char**)&newMsg, "%s for file %s", msg, info.path);
929 throw newMsg;
930 }
931 }
932
933 static uint64_t totalVMSize(cpu_type_t arch, std::vector<fileInfo>& files)
934 {
935 uint64_t totalSize = 0;
936 for(std::vector<fileInfo>::iterator fit=files.begin(); fit != files.end(); ++fit) {
937 fileInfo& fi = *fit;
938 for(std::vector<archInfo>::iterator fait=fi.archs.begin(); fait != fi.archs.end(); ++fait) {
939 if ( fait->arch == arch )
940 totalSize += fait->vmSize;
941 }
942 }
943 return totalSize;
944 }
945
946 static uint64_t startAddress(cpu_type_t arch, std::vector<fileInfo>& files, uint64_t lowAddress, uint64_t highAddress)
947 {
948 if ( lowAddress != 0 )
949 return lowAddress;
950 else if ( highAddress != 0 ) {
951 uint64_t totalSize = totalVMSize(arch, files);
952 if ( highAddress < totalSize )
953 throwf("cannot use -high_address 0x%X because total size of images is greater: 0x%X", highAddress, totalSize);
954 return highAddress - totalSize;
955 }
956 else {
957 if ( (arch == CPU_TYPE_I386) || (arch == CPU_TYPE_POWERPC) ) {
958 // place dylibs below dyld
959 uint64_t topAddr = 0x8FE00000;
960 uint64_t totalSize = totalVMSize(arch, files);
961 if ( totalSize > topAddr )
962 throwf("total size of images (0x%X) does not fit below 0x8FE00000", totalSize);
963 return topAddr - totalSize;
964 }
965 else if ( arch == CPU_TYPE_POWERPC64 ) {
966 return 0x200000000ULL;
967 }
968 else if ( arch == CPU_TYPE_X86_64 ) {
969 return 0x200000000ULL;
970 }
971 else if ( arch == CPU_TYPE_ARM ) {
972 // place dylibs below dyld
973 uint64_t topAddr = 0x2FE00000;
974 uint64_t totalSize = totalVMSize(arch, files);
975 if ( totalSize > topAddr )
976 throwf("total size of images (0x%X) does not fit below 0x2FE00000", totalSize);
977 return topAddr - totalSize;
978 }
979 else
980 throw "unknown architecture";
981 }
982 }
983
984 static void usage()
985 {
986 fprintf(stderr, "rebase [-low_address] [-high_address] [-v] [-arch <arch>] files...\n");
987 }
988
989
990 int main(int argc, const char* argv[])
991 {
992 std::vector<fileInfo> files;
993 std::set<cpu_type_t> onlyArchs;
994 uint64_t lowAddress = 0;
995 uint64_t highAddress = 0;
996
997 try {
998 // parse command line options
999 char* endptr;
1000 for(int i=1; i < argc; ++i) {
1001 const char* arg = argv[i];
1002 if ( arg[0] == '-' ) {
1003 if ( strcmp(arg, "-v") == 0 ) {
1004 verbose = true;
1005 }
1006 else if ( strcmp(arg, "-low_address") == 0 ) {
1007 lowAddress = strtoull(argv[++i], &endptr, 16);
1008 }
1009 else if ( strcmp(arg, "-high_address") == 0 ) {
1010 highAddress = strtoull(argv[++i], &endptr, 16);
1011 }
1012 else if ( strcmp(arg, "-arch") == 0 ) {
1013 const char* archName = argv[++i];
1014 if ( archName == NULL )
1015 throw "-arch missing architecture name";
1016 bool found = false;
1017 for (const ArchInfo* t=archInfoArray; t->archName != NULL; ++t) {
1018 if ( strcmp(t->archName,archName) == 0 ) {
1019 onlyArchs.insert(t->cpuType);
1020 found = true;
1021 }
1022 }
1023 if ( !found )
1024 throwf("unknown architecture %s", archName);
1025 }
1026 else {
1027 usage();
1028 throwf("unknown option: %s\n", arg);
1029 }
1030 }
1031 else {
1032 files.push_back(fileInfo(arg));
1033 }
1034 }
1035
1036 if ( files.size() == 0 )
1037 throw "no files specified";
1038
1039 // use all architectures if no restrictions specified
1040 if ( onlyArchs.size() == 0 ) {
1041 onlyArchs.insert(CPU_TYPE_POWERPC);
1042 onlyArchs.insert(CPU_TYPE_POWERPC64);
1043 onlyArchs.insert(CPU_TYPE_I386);
1044 onlyArchs.insert(CPU_TYPE_X86_64);
1045 onlyArchs.insert(CPU_TYPE_ARM);
1046 }
1047
1048 // scan files and collect sizes
1049 for(std::vector<fileInfo>::iterator it=files.begin(); it != files.end(); ++it) {
1050 setSizes(*it, onlyArchs);
1051 }
1052
1053 // assign new base address for each arch
1054 for(std::set<cpu_type_t>::iterator ait=onlyArchs.begin(); ait != onlyArchs.end(); ++ait) {
1055 cpu_type_t arch = *ait;
1056 uint64_t baseAddress = startAddress(arch, files, lowAddress, highAddress);
1057 for(std::vector<fileInfo>::iterator fit=files.begin(); fit != files.end(); ++fit) {
1058 fileInfo& fi = *fit;
1059 for(std::vector<archInfo>::iterator fait=fi.archs.begin(); fait != fi.archs.end(); ++fait) {
1060 if ( fait->arch == arch ) {
1061 fait->newBase = baseAddress;
1062 baseAddress += fait->vmSize;
1063 baseAddress = (baseAddress + 4095) & (-4096); // page align
1064 }
1065 }
1066 }
1067 }
1068
1069 // rebase each file if it contains something rebaseable
1070 for(std::vector<fileInfo>::iterator it=files.begin(); it != files.end(); ++it) {
1071 fileInfo& fi = *it;
1072 if ( fi.archs.size() > 0 )
1073 rebase(fi);
1074 }
1075
1076 }
1077 catch (const char* msg) {
1078 fprintf(stderr, "rebase failed: %s\n", msg);
1079 return 1;
1080 }
1081
1082 return 0;
1083 }
1084
1085
1086