Integrate cctools-822 changes
[striptease.git] / libstuff / swap_headers.c
blobba2ff6b29b20c107f027e4ee28201b2ffc2615b1
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #define __darwin_i386_exception_state i386_exception_state
24 #define __darwin_i386_float_state i386_float_state
25 #define __darwin_i386_thread_state i386_thread_state
27 #include <mach-o/loader.h>
28 #include <mach/m68k/thread_status.h>
29 #undef MACHINE_THREAD_STATE /* need to undef these to avoid warnings */
30 #undef MACHINE_THREAD_STATE_COUNT
31 #undef THREAD_STATE_NONE
32 #undef VALID_THREAD_STATE_FLAVOR
33 #include <mach/ppc/thread_status.h>
34 #undef MACHINE_THREAD_STATE /* need to undef these to avoid warnings */
35 #undef MACHINE_THREAD_STATE_COUNT
36 #undef THREAD_STATE_NONE
37 #undef VALID_THREAD_STATE_FLAVOR
38 #include <mach/m88k/thread_status.h>
39 #include <mach/i860/thread_status.h>
40 #include <mach/i386/thread_status.h>
41 #include <mach/hppa/thread_status.h>
42 #include <mach/sparc/thread_status.h>
43 #include <mach/arm/thread_status.h>
44 #include "stuff/bool.h"
45 #include "stuff/bytesex.h"
46 #include "stuff/errors.h"
49 * swap_object_headers() swaps the object file headers from the host byte sex
50 * into the non-host byte sex. It returns TRUE if it can and did swap the
51 * headers else returns FALSE and does not touch the headers and prints an error
52 * using the error() routine.
54 __private_extern__
55 enum bool
56 swap_object_headers(
57 void *mach_header,
58 struct load_command *load_commands)
60 unsigned long i;
61 uint32_t magic, ncmds, sizeofcmds, cmd_multiple;
62 cpu_type_t cputype;
63 cpu_subtype_t cpusubtype;
64 struct mach_header *mh;
65 struct mach_header_64 *mh64;
66 enum byte_sex target_byte_sex;
67 struct load_command *lc, l;
68 struct segment_command *sg;
69 struct segment_command_64 *sg64;
70 struct section *s;
71 struct section_64 *s64;
72 struct symtab_command *st;
73 struct dysymtab_command *dyst;
74 struct symseg_command *ss;
75 struct fvmlib_command *fl;
76 struct thread_command *ut;
77 struct ident_command *id;
78 struct entry_point_command *ep;
79 struct source_version_command *sv;
80 struct dylib_command *dl;
81 struct sub_framework_command *sub;
82 struct sub_umbrella_command *usub;
83 struct sub_library_command *lsub;
84 struct sub_client_command *csub;
85 struct prebound_dylib_command *pbdylib;
86 struct dylinker_command *dyld;
87 struct routines_command *rc;
88 struct routines_command_64 *rc64;
89 struct twolevel_hints_command *hints;
90 struct prebind_cksum_command *cs;
91 struct uuid_command *uuid;
92 struct linkedit_data_command *ld;
93 struct rpath_command *rpath;
94 struct encryption_info_command *ec;
95 struct dyld_info_command *dc;
96 struct version_min_command *vc;
97 uint32_t flavor, count;
98 unsigned long nflavor;
99 char *p, *state, *cmd_name;
101 magic = *((uint32_t *)mach_header);
102 if(magic == MH_MAGIC){
103 mh = (struct mach_header *)mach_header;
104 ncmds = mh->ncmds;
105 sizeofcmds = mh->sizeofcmds;
106 cputype = mh->cputype;
107 cpusubtype = mh->cpusubtype;
108 cmd_multiple = 4;
109 mh64 = NULL;
111 else{
112 mh64 = (struct mach_header_64 *)mach_header;
113 ncmds = mh64->ncmds;
114 sizeofcmds = mh64->sizeofcmds;
115 cputype = mh64->cputype;
116 cpusubtype = mh64->cpusubtype;
117 cmd_multiple = 8;
118 mh = NULL;
121 * Make a pass through the load commands checking them to the level
122 * that they can be parsed and then swapped.
124 for(i = 0, lc = load_commands; i < ncmds; i++){
125 l = *lc;
126 /* check load command size for a correct multiple size */
127 if(lc->cmdsize % cmd_multiple != 0){
128 error("in swap_object_headers(): malformed load command %lu "
129 "(cmdsize not a multiple of %u)", i, cmd_multiple);
130 return(FALSE);
132 /* check that load command does not extends past end of commands */
133 if((char *)lc + lc->cmdsize >
134 (char *)load_commands + sizeofcmds){
135 error("in swap_object_headers(): truncated or malformed load "
136 "command %lu (extends past the end of the all load "
137 "commands)", i);
138 return(FALSE);
140 /* check that the load command size is not zero */
141 if(lc->cmdsize == 0){
142 error("in swap_object_headers(): malformed load command %lu "
143 "(cmdsize is zero)", i);
144 return(FALSE);
146 switch(lc->cmd){
147 case LC_SEGMENT:
148 sg = (struct segment_command *)lc;
149 if(sg->cmdsize != sizeof(struct segment_command) +
150 sg->nsects * sizeof(struct section)){
151 error("in swap_object_headers(): malformed load command "
152 "(inconsistent cmdsize in LC_SEGMENT command %lu for "
153 "the number of sections)", i);
154 return(FALSE);
156 break;
158 case LC_SEGMENT_64:
159 sg64 = (struct segment_command_64 *)lc;
160 if(sg64->cmdsize != sizeof(struct segment_command_64) +
161 sg64->nsects * sizeof(struct section_64)){
162 error("in swap_object_headers(): malformed load command "
163 "(inconsistent cmdsize in LC_SEGMENT_64 command %lu "
164 "for the number of sections)", i);
165 return(FALSE);
167 break;
169 case LC_SYMTAB:
170 st = (struct symtab_command *)lc;
171 if(st->cmdsize != sizeof(struct symtab_command)){
172 error("in swap_object_headers(): malformed load commands "
173 "(LC_SYMTAB command %lu has incorrect cmdsize", i);
174 return(FALSE);
176 break;
178 case LC_DYSYMTAB:
179 dyst = (struct dysymtab_command *)lc;
180 if(dyst->cmdsize != sizeof(struct dysymtab_command)){
181 error("in swap_object_headers(): malformed load commands "
182 "(LC_DYSYMTAB command %lu has incorrect cmdsize", i);
183 return(FALSE);
185 break;
187 case LC_SYMSEG:
188 ss = (struct symseg_command *)lc;
189 if(ss->cmdsize != sizeof(struct symseg_command)){
190 error("in swap_object_headers(): malformed load command "
191 "(LC_SYMSEG command %lu has incorrect cmdsize", i);
192 return(FALSE);
194 break;
196 case LC_IDFVMLIB:
197 case LC_LOADFVMLIB:
198 fl = (struct fvmlib_command *)lc;
199 if(fl->cmdsize < sizeof(struct fvmlib_command)){
200 error("in swap_object_headers(): malformed load commands "
201 "(%s command %lu has too small cmdsize field)",
202 fl->cmd == LC_IDFVMLIB ? "LC_IDFVMLIB" :
203 "LC_LOADFVMLIB", i);
204 return(FALSE);
206 if(fl->fvmlib.name.offset >= fl->cmdsize){
207 error("in swap_object_headers(): truncated or malformed "
208 "load commands (name.offset field of %s command %lu "
209 "extends past the end of all load commands)",
210 fl->cmd == LC_IDFVMLIB ? "LC_IDFVMLIB" :
211 "LC_LOADFVMLIB", i);
212 return(FALSE);
214 break;
216 case LC_ID_DYLIB:
217 cmd_name = "LC_ID_DYLIB";
218 goto check_dylib_command;
219 case LC_LOAD_DYLIB:
220 cmd_name = "LC_LOAD_DYLIB";
221 goto check_dylib_command;
222 case LC_LOAD_WEAK_DYLIB:
223 cmd_name = "LC_LOAD_WEAK_DYLIB";
224 goto check_dylib_command;
225 case LC_REEXPORT_DYLIB:
226 cmd_name = "LC_REEXPORT_DYLIB";
227 goto check_dylib_command;
228 case LC_LOAD_UPWARD_DYLIB:
229 cmd_name = "LC_LOAD_UPWARD_DYLIB";
230 goto check_dylib_command;
231 case LC_LAZY_LOAD_DYLIB:
232 cmd_name = "LC_LAZY_LOAD_DYLIB";
233 goto check_dylib_command;
234 check_dylib_command:
235 dl = (struct dylib_command *)lc;
236 if(dl->cmdsize < sizeof(struct dylib_command)){
237 error("in swap_object_headers(): malformed load commands "
238 "(%s command %lu has too small cmdsize field)",
239 cmd_name, i);
240 return(FALSE);
242 if(dl->dylib.name.offset >= dl->cmdsize){
243 error("in swap_object_headers(): truncated or malformed "
244 "load commands (name.offset field of %s command %lu "
245 "extends past the end of all load commands)",
246 cmd_name, i);
247 return(FALSE);
249 break;
251 case LC_SUB_FRAMEWORK:
252 sub = (struct sub_framework_command *)lc;
253 if(sub->cmdsize < sizeof(struct sub_framework_command)){
254 error("in swap_object_headers(): malformed load commands "
255 "(LC_SUB_FRAMEWORK command %lu has too small cmdsize "
256 "field)", i);
257 return(FALSE);
259 if(sub->umbrella.offset >= sub->cmdsize){
260 error("in swap_object_headers(): truncated or malformed "
261 "load commands (umbrella.offset field of "
262 "LC_SUB_FRAMEWORK command %lu extends past the end "
263 "of all load commands)", i);
264 return(FALSE);
266 break;
268 case LC_SUB_UMBRELLA:
269 usub = (struct sub_umbrella_command *)lc;
270 if(usub->cmdsize < sizeof(struct sub_umbrella_command)){
271 error("in swap_object_headers(): malformed load commands "
272 "(LC_SUB_UMBRELLA command %lu has too small cmdsize "
273 "field)", i);
274 return(FALSE);
276 if(usub->sub_umbrella.offset >= usub->cmdsize){
277 error("in swap_object_headers(): truncated or malformed "
278 "load commands (sub_umbrella.offset field of "
279 "LC_SUB_UMBRELLA command %lu extends past the end "
280 "of all load commands)", i);
281 return(FALSE);
283 break;
285 case LC_SUB_LIBRARY:
286 lsub = (struct sub_library_command *)lc;
287 if(lsub->cmdsize < sizeof(struct sub_library_command)){
288 error("in swap_object_headers(): malformed load commands "
289 "(LC_SUB_LIBRARY command %lu has too small cmdsize "
290 "field)", i);
291 return(FALSE);
293 if(lsub->sub_library.offset >= lsub->cmdsize){
294 error("in swap_object_headers(): truncated or malformed "
295 "load commands (sub_library.offset field of "
296 "LC_SUB_LIBRARY command %lu extends past the end "
297 "of all load commands)", i);
298 return(FALSE);
300 break;
302 case LC_SUB_CLIENT:
303 csub = (struct sub_client_command *)lc;
304 if(csub->cmdsize < sizeof(struct sub_client_command)){
305 error("in swap_object_headers(): malformed load commands "
306 "(LC_SUB_CLIENT command %lu has too small cmdsize "
307 "field)", i);
308 return(FALSE);
310 if(csub->client.offset >= csub->cmdsize){
311 error("in swap_object_headers(): truncated or malformed "
312 "load commands (client.offset field of "
313 "LC_SUB_CLIENT command %lu extends past the end "
314 "of all load commands)", i);
315 return(FALSE);
317 break;
319 case LC_PREBOUND_DYLIB:
320 pbdylib = (struct prebound_dylib_command *)lc;
321 if(pbdylib->cmdsize < sizeof(struct prebound_dylib_command)){
322 error("in swap_object_headers(): malformed load commands "
323 "(LC_PREBOUND_DYLIB command %lu has too small "
324 "cmdsize field)", i);
325 return(FALSE);
327 if(pbdylib->name.offset >= pbdylib->cmdsize){
328 error("in swap_object_headers(): truncated or malformed "
329 "load commands (name.offset field of "
330 "LC_PREBOUND_DYLIB command %lu extends past the end "
331 "of all load commands)", i);
332 return(FALSE);
334 if(pbdylib->linked_modules.offset >= pbdylib->cmdsize){
335 error("in swap_object_headers(): truncated or malformed "
336 "load commands (linked_modules.offset field of "
337 "LC_PREBOUND_DYLIB command %lu extends past the end "
338 "of all load commands)", i);
339 return(FALSE);
341 break;
343 case LC_ID_DYLINKER:
344 cmd_name = "LC_ID_DYLINKER";
345 goto check_dylinker_command;
346 case LC_LOAD_DYLINKER:
347 cmd_name = "LC_LOAD_DYLINKER";
348 goto check_dylinker_command;
349 case LC_DYLD_ENVIRONMENT:
350 cmd_name = "LC_DYLD_ENVIRONMENT";
351 goto check_dylinker_command;
352 check_dylinker_command:
353 dyld = (struct dylinker_command *)lc;
354 if(dyld->cmdsize < sizeof(struct dylinker_command)){
355 error("in swap_object_headers(): malformed load commands "
356 "(%s command %lu has too small cmdsize field)",
357 cmd_name, i);
358 return(FALSE);
360 if(dyld->name.offset >= dyld->cmdsize){
361 error("in swap_object_headers(): truncated or malformed "
362 "load commands (name.offset field of %s command %lu "
363 "extends past the end of all load commands)",
364 cmd_name, i);
365 return(FALSE);
367 break;
369 case LC_UNIXTHREAD:
370 case LC_THREAD:
371 ut = (struct thread_command *)lc;
372 state = (char *)ut + sizeof(struct thread_command);
374 if(cputype == CPU_TYPE_MC680x0){
375 struct m68k_thread_state_regs *cpu;
376 struct m68k_thread_state_68882 *fpu;
377 struct m68k_thread_state_user_reg *user_reg;
379 nflavor = 0;
380 p = (char *)ut + ut->cmdsize;
381 while(state < p){
382 flavor = *((uint32_t *)state);
383 state += sizeof(uint32_t);
384 count = *((uint32_t *)state);
385 state += sizeof(uint32_t);
386 switch(flavor){
387 case M68K_THREAD_STATE_REGS:
388 if(count != M68K_THREAD_STATE_REGS_COUNT){
389 error("in swap_object_headers(): malformed "
390 "load commands (count "
391 "not M68K_THREAD_STATE_REGS_COUNT for "
392 "flavor number %lu which is a M68K_THREAD_"
393 "STATE_REGS flavor in %s command %lu)",
394 nflavor, ut->cmd == LC_UNIXTHREAD ?
395 "LC_UNIXTHREAD" : "LC_THREAD", i);
396 return(FALSE);
398 cpu = (struct m68k_thread_state_regs *)state;
399 state += sizeof(struct m68k_thread_state_regs);
400 break;
401 case M68K_THREAD_STATE_68882:
402 if(count != M68K_THREAD_STATE_68882_COUNT){
403 error("in swap_object_headers(): malformed "
404 "load commands (count "
405 "not M68K_THREAD_STATE_68882_COUNT for "
406 "flavor number %lu which is a M68K_THREAD_"
407 "STATE_68882 flavor in %s command %lu)",
408 nflavor, ut->cmd == LC_UNIXTHREAD ?
409 "LC_UNIXTHREAD" : "LC_THREAD", i);
410 return(FALSE);
412 fpu = (struct m68k_thread_state_68882 *)state;
413 state += sizeof(struct m68k_thread_state_68882);
414 break;
415 case M68K_THREAD_STATE_USER_REG:
416 if(count != M68K_THREAD_STATE_USER_REG_COUNT){
417 error("in swap_object_headers(): malformed "
418 "load commands (count "
419 "not M68K_THREAD_STATE_USER_REG_COUNT for "
420 "flavor number %lu which is a M68K_THREAD_"
421 "STATE_USER_REG flavor in %s command %lu)",
422 nflavor, ut->cmd == LC_UNIXTHREAD ?
423 "LC_UNIXTHREAD" : "LC_THREAD", i);
424 return(FALSE);
426 user_reg =
427 (struct m68k_thread_state_user_reg *)state;
428 state += sizeof(struct m68k_thread_state_user_reg);
429 break;
430 default:
431 error("in swap_object_headers(): malformed "
432 "load commands (unknown "
433 "flavor %u for flavor number %lu in %s command"
434 " %lu can't byte swap it)", flavor, nflavor,
435 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
436 "LC_THREAD", i);
437 return(FALSE);
439 nflavor++;
441 break;
443 if(cputype == CPU_TYPE_POWERPC ||
444 cputype == CPU_TYPE_VEO ||
445 cputype == CPU_TYPE_POWERPC64){
446 ppc_thread_state_t *cpu;
447 ppc_float_state_t *fpu;
448 ppc_exception_state_t *except;
449 ppc_thread_state64_t *cpu64;
451 nflavor = 0;
452 p = (char *)ut + ut->cmdsize;
453 while(state < p){
454 flavor = *((uint32_t *)state);
455 state += sizeof(uint32_t);
456 count = *((uint32_t *)state);
457 state += sizeof(uint32_t);
458 switch(flavor){
459 case PPC_THREAD_STATE:
460 if(count != PPC_THREAD_STATE_COUNT){
461 error("in swap_object_headers(): malformed "
462 "load commands (count "
463 "not PPC_THREAD_STATE_COUNT for "
464 "flavor number %lu which is a PPC_THREAD_"
465 "STATE flavor in %s command %lu)",
466 nflavor, ut->cmd == LC_UNIXTHREAD ?
467 "LC_UNIXTHREAD" : "LC_THREAD", i);
468 return(FALSE);
470 cpu = (ppc_thread_state_t *)state;
471 state += sizeof(ppc_thread_state_t);
472 break;
473 case PPC_FLOAT_STATE:
474 if(count != PPC_FLOAT_STATE_COUNT){
475 error("in swap_object_headers(): malformed "
476 "load commands (count "
477 "not PPC_FLOAT_STATE_COUNT for "
478 "flavor number %lu which is a PPC_FLOAT_"
479 "STATE flavor in %s command %lu)",
480 nflavor, ut->cmd == LC_UNIXTHREAD ?
481 "LC_UNIXTHREAD" : "LC_THREAD", i);
482 return(FALSE);
484 fpu = (ppc_float_state_t *)state;
485 state += sizeof(ppc_float_state_t);
486 break;
487 case PPC_EXCEPTION_STATE:
488 if(count != PPC_EXCEPTION_STATE_COUNT){
489 error("in swap_object_headers(): malformed "
490 "load commands (count "
491 "not PPC_EXCEPTION_STATE_COUNT for "
492 "flavor number %lu which is a PPC_EXCEPT"
493 "ION_STATE flavor in %s command %lu)",
494 nflavor, ut->cmd == LC_UNIXTHREAD ?
495 "LC_UNIXTHREAD" : "LC_THREAD", i);
496 return(FALSE);
498 except = (ppc_exception_state_t *)state;
499 state += sizeof(ppc_exception_state_t);
500 break;
501 case PPC_THREAD_STATE64:
502 if(count != PPC_THREAD_STATE64_COUNT){
503 error("in swap_object_headers(): malformed "
504 "load commands (count "
505 "not PPC_THREAD_STATE64_COUNT for "
506 "flavor number %lu which is a PPC_THREAD_"
507 "STATE64 flavor in %s command %lu)",
508 nflavor, ut->cmd == LC_UNIXTHREAD ?
509 "LC_UNIXTHREAD" : "LC_THREAD", i);
510 return(FALSE);
512 cpu64 = (ppc_thread_state64_t *)state;
513 state += sizeof(ppc_thread_state64_t);
514 break;
515 default:
516 error("in swap_object_headers(): malformed "
517 "load commands (unknown "
518 "flavor %u for flavor number %lu in %s command"
519 " %lu can't byte swap it)", flavor, nflavor,
520 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
521 "LC_THREAD", i);
522 return(FALSE);
524 nflavor++;
526 break;
528 if(cputype == CPU_TYPE_MC88000){
529 m88k_thread_state_grf_t *cpu;
530 m88k_thread_state_xrf_t *fpu;
531 m88k_thread_state_user_t *user;
532 m88110_thread_state_impl_t *spu;
534 nflavor = 0;
535 p = (char *)ut + ut->cmdsize;
536 while(state < p){
537 flavor = *((uint32_t *)state);
538 state += sizeof(uint32_t);
539 count = *((uint32_t *)state);
540 state += sizeof(uint32_t);
541 switch(flavor){
542 case M88K_THREAD_STATE_GRF:
543 if(count != M88K_THREAD_STATE_GRF_COUNT){
544 error("in swap_object_headers(): malformed "
545 "load commands (count "
546 "not M88K_THREAD_STATE_GRF_COUNT for "
547 "flavor number %lu which is a M88K_THREAD_"
548 "STATE_GRF flavor in %s command %lu)",
549 nflavor, ut->cmd == LC_UNIXTHREAD ?
550 "LC_UNIXTHREAD" : "LC_THREAD", i);
551 return(FALSE);
553 cpu = (m88k_thread_state_grf_t *)state;
554 state += sizeof(m88k_thread_state_grf_t);
555 break;
556 case M88K_THREAD_STATE_XRF:
557 if(count != M88K_THREAD_STATE_XRF_COUNT){
558 error("in swap_object_headers(): malformed "
559 "load commands (count "
560 "not M88K_THREAD_STATE_XRF_COUNT for "
561 "flavor number %lu which is a M88K_THREAD_"
562 "STATE_XRF flavor in %s command %lu)",
563 nflavor, ut->cmd == LC_UNIXTHREAD ?
564 "LC_UNIXTHREAD" : "LC_THREAD", i);
565 return(FALSE);
567 fpu = (m88k_thread_state_xrf_t *)state;
568 state += sizeof(m88k_thread_state_xrf_t);
569 break;
570 case M88K_THREAD_STATE_USER:
571 if(count != M88K_THREAD_STATE_USER_COUNT){
572 error("in swap_object_headers(): malformed "
573 "load commands (count "
574 "not M88K_THREAD_STATE_USER_COUNT for "
575 "flavor number %lu which is a M88K_THREAD_"
576 "STATE_USER flavor in %s command %lu)",
577 nflavor, ut->cmd == LC_UNIXTHREAD ?
578 "LC_UNIXTHREAD" : "LC_THREAD", i);
579 return(FALSE);
581 user = (m88k_thread_state_user_t *)state;
582 state += sizeof(m88k_thread_state_user_t);
583 break;
584 case M88110_THREAD_STATE_IMPL:
585 if(count != M88110_THREAD_STATE_IMPL_COUNT){
586 error("in swap_object_headers(): malformed "
587 "load commands (count "
588 "not M88110_THREAD_STATE_IMPL_COUNT for "
589 "flavor number %lu which is a M88110_THREAD"
590 "_STATE_IMPL flavor in %s command %lu)",
591 nflavor, ut->cmd == LC_UNIXTHREAD ?
592 "LC_UNIXTHREAD" : "LC_THREAD", i);
593 return(FALSE);
595 spu = (m88110_thread_state_impl_t *)state;
596 state += sizeof(m88110_thread_state_impl_t);
597 break;
598 default:
599 error("in swap_object_headers(): malformed "
600 "load commands (unknown "
601 "flavor %u for flavor number %lu in %s command"
602 " %lu can't byte swap it)", flavor, nflavor,
603 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
604 "LC_THREAD", i);
605 return(FALSE);
607 nflavor++;
609 break;
611 if(cputype == CPU_TYPE_I860){
612 struct i860_thread_state_regs *cpu;
614 nflavor = 0;
615 p = (char *)ut + ut->cmdsize;
616 while(state < p){
617 flavor = *((uint32_t *)state);
618 state += sizeof(uint32_t);
619 count = *((uint32_t *)state);
620 state += sizeof(uint32_t);
621 switch(flavor){
622 case I860_THREAD_STATE_REGS:
623 if(count != I860_THREAD_STATE_REGS_COUNT){
624 error("in swap_object_headers(): malformed "
625 "load commands (count "
626 "not I860_THREAD_STATE_REGS_COUNT for "
627 "flavor number %lu which is a I860_THREAD_"
628 "STATE_REGS flavor in %s command %lu)",
629 nflavor, ut->cmd == LC_UNIXTHREAD ?
630 "LC_UNIXTHREAD" : "LC_THREAD", i);
631 return(FALSE);
633 cpu = (struct i860_thread_state_regs *)state;
634 state += sizeof(struct i860_thread_state_regs);
635 break;
636 default:
637 error("in swap_object_headers(): malformed "
638 "load commands (unknown "
639 "flavor %u for flavor number %lu in %s command"
640 " %lu can't byte swap it)", flavor, nflavor,
641 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
642 "LC_THREAD", i);
643 return(FALSE);
645 nflavor++;
647 break;
649 if(cputype == CPU_TYPE_I386
650 #ifdef x86_THREAD_STATE64
651 || cputype == CPU_TYPE_X86_64
652 #endif /* x86_THREAD_STATE64 */
654 i386_thread_state_t *cpu;
655 #ifdef x86_THREAD_STATE64
656 x86_thread_state64_t *cpu64;
657 #endif /* x86_THREAD_STATE64 */
658 /* current i386 thread states */
659 #if i386_THREAD_STATE == 1
660 struct i386_float_state *fpu;
661 i386_exception_state_t *exc;
662 #endif /* i386_THREAD_STATE == 1 */
664 /* i386 thread states on older releases */
665 #if i386_THREAD_STATE == -1
666 i386_thread_fpstate_t *fpu;
667 i386_thread_exceptstate_t *exc;
668 i386_thread_cthreadstate_t *user;
669 #endif /* i386_THREAD_STATE == -1 */
671 nflavor = 0;
672 p = (char *)ut + ut->cmdsize;
673 while(state < p){
674 flavor = *((uint32_t *)state);
675 state += sizeof(uint32_t);
676 count = *((uint32_t *)state);
677 state += sizeof(uint32_t);
678 switch((int)flavor){
679 case i386_THREAD_STATE:
680 /* current i386 thread states */
681 #if i386_THREAD_STATE == 1
682 case -1:
683 #endif /* i386_THREAD_STATE == 1 */
684 /* i386 thread states on older releases */
685 #if i386_THREAD_STATE == -1
686 case 1:
687 #endif /* i386_THREAD_STATE == -1 */
688 if(count != i386_THREAD_STATE_COUNT){
689 error("in swap_object_headers(): malformed "
690 "load commands (count "
691 "not i386_THREAD_STATE_COUNT for flavor "
692 "number %lu which is a i386_THREAD_STATE "
693 "flavor in %s command %lu)", nflavor,
694 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
695 "LC_THREAD", i);
696 return(FALSE);
698 cpu = (i386_thread_state_t *)state;
699 state += sizeof(i386_thread_state_t);
700 break;
701 /* current i386 thread states */
702 #if i386_THREAD_STATE == 1
703 case i386_FLOAT_STATE:
704 if(count != i386_FLOAT_STATE_COUNT){
705 error("in swap_object_headers(): malformed "
706 "load commands (count "
707 "not i386_FLOAT_STATE_COUNT for flavor "
708 "number %lu which is a i386_FLOAT_STATE "
709 "flavor in %s command %lu)", nflavor,
710 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
711 "LC_THREAD", i);
712 return(FALSE);
714 fpu = (struct i386_float_state *)state;
715 state += sizeof(struct i386_float_state);
716 break;
717 case i386_EXCEPTION_STATE:
718 if(count != I386_EXCEPTION_STATE_COUNT){
719 error("in swap_object_headers(): malformed "
720 "load commands (count "
721 "not I386_EXCEPTION_STATE_COUNT for "
722 "flavor number %lu which is a i386_"
723 "EXCEPTION_STATE flavor in %s command %lu)",
724 nflavor,
725 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
726 "LC_THREAD", i);
727 return(FALSE);
729 exc = (i386_exception_state_t *)state;
730 state += sizeof(i386_exception_state_t);
731 break;
732 #endif /* i386_THREAD_STATE == 1 */
734 /* i386 thread states on older releases */
735 #if i386_THREAD_STATE == -1
736 case i386_THREAD_FPSTATE:
737 if(count != i386_THREAD_FPSTATE_COUNT){
738 error("in swap_object_headers(): malformed "
739 "load commands (count "
740 "not i386_THREAD_FPSTATE_COUNT for flavor "
741 "number %lu which is a i386_THREAD_FPSTATE "
742 "flavor in %s command %lu)", nflavor,
743 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
744 "LC_THREAD", i);
745 return(FALSE);
747 fpu = (i386_thread_fpstate_t *)state;
748 state += sizeof(i386_thread_fpstate_t);
749 break;
750 case i386_THREAD_EXCEPTSTATE:
751 if(count != i386_THREAD_EXCEPTSTATE_COUNT){
752 error("in swap_object_headers(): malformed "
753 "load commands (count "
754 "not i386_THREAD_EXCEPTSTATE_COUNT for "
755 "flavor number %lu which is a i386_THREAD_"
756 "EXCEPTSTATE flavor in %s command %lu)",
757 nflavor,
758 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
759 "LC_THREAD", i);
760 return(FALSE);
762 exc = (i386_thread_exceptstate_t *)state;
763 state += sizeof(i386_thread_fpstate_t);
764 break;
765 case i386_THREAD_CTHREADSTATE:
766 if(count != i386_THREAD_CTHREADSTATE_COUNT){
767 error("in swap_object_headers(): malformed "
768 "load commands (count "
769 "not i386_THREAD_CTHREADSTATE_COUNT for "
770 "flavor number %lu which is a i386_THREAD_"
771 "CTHREADSTATE flavor in %s command %lu)",
772 nflavor,
773 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
774 "LC_THREAD", i);
775 return(FALSE);
777 user = (i386_thread_cthreadstate_t *)state;
778 state += sizeof(i386_thread_fpstate_t);
779 break;
780 #endif /* i386_THREAD_STATE == -1 */
781 #ifdef x86_THREAD_STATE64
782 case x86_THREAD_STATE64:
783 if(count != x86_THREAD_STATE64_COUNT){
784 error("in swap_object_headers(): malformed "
785 "load commands (count "
786 "not x86_THREAD_STATE64_COUNT for "
787 "flavor number %lu which is an x86_THREAD_"
788 "STATE64 flavor in %s command %lu)",
789 nflavor,
790 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
791 "LC_THREAD", i);
792 return(FALSE);
794 cpu64 = (x86_thread_state64_t *)state;
795 state += sizeof(x86_thread_state64_t);
796 break;
797 #endif /* x86_THREAD_STATE64 */
798 default:
799 error("in swap_object_headers(): malformed "
800 "load commands (unknown "
801 "flavor %u for flavor number %lu in %s command"
802 " %lu can't byte swap it)", flavor, nflavor,
803 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
804 "LC_THREAD", i);
805 return(FALSE);
807 nflavor++;
809 break;
811 if(cputype == CPU_TYPE_HPPA){
812 struct hp_pa_integer_thread_state *cpu;
813 struct hp_pa_frame_thread_state *frame;
814 struct hp_pa_fp_thread_state *fpu;
816 nflavor = 0;
817 p = (char *)ut + ut->cmdsize;
818 while(state < p){
819 flavor = *((uint32_t *)state);
820 state += sizeof(uint32_t);
821 count = *((uint32_t *)state);
822 state += sizeof(uint32_t);
823 switch(flavor){
824 case HPPA_INTEGER_THREAD_STATE:
825 if(count != HPPA_INTEGER_THREAD_STATE_COUNT){
826 error("in swap_object_headers(): malformed "
827 "load commands (count "
828 "not HPPA_INTEGER_THREAD_STATE_COUNT for "
829 "flavor number %lu which is a HPPA_INTEGER"
830 "_THREAD_STATE flavor in %s command %lu)",
831 nflavor, ut->cmd == LC_UNIXTHREAD ?
832 "LC_UNIXTHREAD" : "LC_THREAD", i);
833 return(FALSE);
835 cpu = (struct hp_pa_integer_thread_state *)state;
836 state += sizeof(struct hp_pa_integer_thread_state);
837 break;
838 case HPPA_FRAME_THREAD_STATE:
839 if(count != HPPA_FRAME_THREAD_STATE_COUNT){
840 error("in swap_object_headers(): malformed "
841 "load commands (count "
842 "not HPPA_FRAME_THREAD_STATE_COUNT for "
843 "flavor number %lu which is a HPPA_FRAME"
844 "_THREAD_STATE flavor in %s command %lu)",
845 nflavor, ut->cmd == LC_UNIXTHREAD ?
846 "LC_UNIXTHREAD" : "LC_THREAD", i);
847 return(FALSE);
849 frame = (struct hp_pa_frame_thread_state *)state;
850 state += sizeof(struct hp_pa_frame_thread_state);
851 break;
852 case HPPA_FP_THREAD_STATE:
853 if(count != HPPA_FP_THREAD_STATE_COUNT){
854 error("in swap_object_headers(): malformed "
855 "load commands (count "
856 "not HPPA_FP_THREAD_STATE_COUNT for "
857 "flavor number %lu which is a HPPA_FP"
858 "_THREAD_STATE flavor in %s command %lu)",
859 nflavor, ut->cmd == LC_UNIXTHREAD ?
860 "LC_UNIXTHREAD" : "LC_THREAD", i);
861 return(FALSE);
863 fpu = (struct hp_pa_fp_thread_state *)state;
864 state += sizeof(struct hp_pa_fp_thread_state);
865 break;
866 default:
867 error("in swap_object_headers(): malformed "
868 "load commands (unknown "
869 "flavor %u for flavor number %lu in %s command"
870 " %lu can't byte swap it)", flavor, nflavor,
871 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
872 "LC_THREAD", i);
873 return(FALSE);
875 nflavor++;
877 break;
879 if(cputype == CPU_TYPE_SPARC) {
880 struct sparc_thread_state_regs *cpu;
881 struct sparc_thread_state_fpu *fpu;
883 nflavor = 0;
884 p = (char *)ut + ut->cmdsize;
885 while (state < p) {
886 flavor = *((uint32_t *) state);
887 state += sizeof(uint32_t);
888 count = *((uint32_t *) state);
889 state += sizeof(uint32_t);
890 switch (flavor) {
891 case SPARC_THREAD_STATE_REGS:
892 if (count != SPARC_THREAD_STATE_REGS_COUNT) {
893 error("in swap_object_headers(): malformed "
894 "load commands (count "
895 "not SPARC_THREAD_STATE_REGS_COUNT for "
896 "flavor number %lu which is a SPARC_THREAD_"
897 "STATE_REGS flavor in %s command %lu)",
898 nflavor, ut->cmd == LC_UNIXTHREAD ?
899 "LC_UNIXTHREAD" : "LC_THREAD", i);
900 return(FALSE);
902 cpu = (struct sparc_thread_state_regs *) state;
903 state += sizeof(struct sparc_thread_state_regs);
904 break;
905 case SPARC_THREAD_STATE_FPU:
906 if (count != SPARC_THREAD_STATE_FPU_COUNT) {
907 error("in swap_object_headers(): malformed "
908 "load commands (count "
909 "not SPARC_THREAD_STATE_FPU_COUNT for "
910 "flavor number %lu which is a SPARC_THREAD_"
911 "STATE_FPU flavor in %s command %lu)",
912 nflavor, ut->cmd == LC_UNIXTHREAD ?
913 "LC_UNIXTHREAD" : "LC_THREAD", i);
914 return(FALSE);
916 fpu = (struct sparc_thread_state_fpu *) state;
917 state += sizeof(struct sparc_thread_state_fpu);
918 break;
921 break;
923 if(cputype == CPU_TYPE_ARM){
924 arm_thread_state_t *cpu;
926 nflavor = 0;
927 p = (char *)ut + ut->cmdsize;
928 while(state < p){
929 flavor = *((uint32_t *)state);
930 state += sizeof(uint32_t);
931 count = *((uint32_t *)state);
932 state += sizeof(uint32_t);
933 switch(flavor){
934 case ARM_THREAD_STATE:
935 if(count != ARM_THREAD_STATE_COUNT){
936 error("in swap_object_headers(): malformed "
937 "load commands (count "
938 "not ARM_THREAD_STATE_COUNT for "
939 "flavor number %lu which is a ARM_THREAD_"
940 "STATE flavor in %s command %lu)",
941 nflavor, ut->cmd == LC_UNIXTHREAD ?
942 "LC_UNIXTHREAD" : "LC_THREAD", i);
943 return(FALSE);
945 cpu = (arm_thread_state_t *)state;
946 state += sizeof(arm_thread_state_t);
947 break;
948 default:
949 error("in swap_object_headers(): malformed load "
950 "commands (unknown flavor for flavor number "
951 "%lu in %s command %lu can't byte swap it)",
952 nflavor, ut->cmd == LC_UNIXTHREAD ?
953 "LC_UNIXTHREAD" : "LC_THREAD", i);
954 return(FALSE);
956 nflavor++;
958 break;
960 error("in swap_object_headers(): malformed load commands "
961 "(unknown cputype (%d) and cpusubtype (%d) of object and "
962 "can't byte swap %s command %lu)", cputype,
963 cpusubtype, ut->cmd == LC_UNIXTHREAD ?
964 "LC_UNIXTHREAD" : "LC_THREAD", i);
965 return(FALSE);
967 case LC_MAIN:
968 ep = (struct entry_point_command *)lc;
969 if((char *)ep + ep->cmdsize >
970 (char *)load_commands + sizeofcmds){
971 error("in swap_object_headers(): truncated or malformed "
972 "load commands (cmdsize field of LC_MAIN command %lu "
973 "extends past the end of the load commands)", i);
974 return(FALSE);
976 break;
978 case LC_SOURCE_VERSION:
979 sv = (struct source_version_command *)lc;
980 if((char *)sv + sv->cmdsize >
981 (char *)load_commands + sizeofcmds){
982 error("in swap_object_headers(): truncated or malformed "
983 "load commands (cmdsize field of LC_SOURCE_VERSION "
984 "command %lu extends past the end of the load "
985 "commands)", i);
986 return(FALSE);
988 break;
990 case LC_IDENT:
991 id = (struct ident_command *)lc;
992 if((char *)id + id->cmdsize >
993 (char *)load_commands + sizeofcmds){
994 error("in swap_object_headers(): truncated or malformed "
995 "load commands (cmdsize field of LC_IDENT command %lu "
996 "extends past the end of the load commands)", i);
997 return(FALSE);
999 break;
1001 case LC_ROUTINES:
1002 rc = (struct routines_command *)lc;
1003 if(rc->cmdsize != sizeof(struct routines_command)){
1004 error("in swap_object_headers(): malformed load commands ("
1005 "LC_ROUTINES command %lu has incorrect cmdsize",
1007 return(FALSE);
1009 break;
1011 case LC_ROUTINES_64:
1012 rc64 = (struct routines_command_64 *)lc;
1013 if(rc64->cmdsize != sizeof(struct routines_command_64)){
1014 error("in swap_object_headers(): malformed load commands ("
1015 "LC_ROUTINES_64 command %lu has incorrect cmdsize",
1017 return(FALSE);
1019 break;
1021 case LC_TWOLEVEL_HINTS:
1022 hints = (struct twolevel_hints_command *)lc;
1023 if(hints->cmdsize != sizeof(struct twolevel_hints_command)){
1024 error("in swap_object_headers(): malformed load commands "
1025 "(LC_TWOLEVEL_HINTS command %lu has incorrect "
1026 "cmdsize", i);
1027 return(FALSE);
1029 break;
1031 case LC_PREBIND_CKSUM:
1032 cs = (struct prebind_cksum_command *)lc;
1033 if(cs->cmdsize != sizeof(struct prebind_cksum_command)){
1034 error("in swap_object_headers(): malformed load commands "
1035 "(LC_PREBIND_CKSUM command %lu has incorrect cmdsize",
1037 return(FALSE);
1039 break;
1041 case LC_UUID:
1042 uuid = (struct uuid_command *)lc;
1043 if(uuid->cmdsize != sizeof(struct uuid_command)){
1044 error("in swap_object_headers(): malformed load commands "
1045 "(LC_UUID command %lu has incorrect cmdsize", i);
1046 return(FALSE);
1048 break;
1050 case LC_CODE_SIGNATURE:
1051 ld = (struct linkedit_data_command *)lc;
1052 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1053 error("in swap_object_headers(): malformed load commands "
1054 "(LC_CODE_SIGNATURE command %lu has incorrect "
1055 "cmdsize", i);
1056 return(FALSE);
1058 break;
1060 case LC_SEGMENT_SPLIT_INFO:
1061 ld = (struct linkedit_data_command *)lc;
1062 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1063 error("in swap_object_headers(): malformed load commands "
1064 "(LC_SEGMENT_SPLIT_INFO command %lu has incorrect "
1065 "cmdsize", i);
1066 return(FALSE);
1068 break;
1070 case LC_FUNCTION_STARTS:
1071 ld = (struct linkedit_data_command *)lc;
1072 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1073 error("in swap_object_headers(): malformed load commands "
1074 "(LC_FUNCTION_STARTS command %lu has incorrect "
1075 "cmdsize", i);
1076 return(FALSE);
1078 break;
1080 case LC_DATA_IN_CODE:
1081 ld = (struct linkedit_data_command *)lc;
1082 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1083 error("in swap_object_headers(): malformed load commands "
1084 "(LC_DATA_IN_CODE command %lu has incorrect "
1085 "cmdsize", i);
1086 return(FALSE);
1088 break;
1090 case LC_DYLIB_CODE_SIGN_DRS:
1091 ld = (struct linkedit_data_command *)lc;
1092 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1093 error("in swap_object_headers(): malformed load commands "
1094 "(LC_DYLIB_CODE_SIGN_DRS command %lu has incorrect "
1095 "cmdsize", i);
1096 return(FALSE);
1098 break;
1100 case LC_VERSION_MIN_MACOSX:
1101 vc = (struct version_min_command *)lc;
1102 if(vc->cmdsize != sizeof(struct version_min_command)){
1103 error("in swap_object_headers(): malformed load commands "
1104 "(LC_VERSION_MIN_MACOSX command %lu has incorrect "
1105 "cmdsize", i);
1106 return(FALSE);
1108 break;
1110 case LC_VERSION_MIN_IPHONEOS:
1111 vc = (struct version_min_command *)lc;
1112 if(vc->cmdsize != sizeof(struct version_min_command)){
1113 error("in swap_object_headers(): malformed load commands "
1114 "(LC_VERSION_MIN_IPHONEOS command %lu has incorrect "
1115 "cmdsize", i);
1116 return(FALSE);
1118 break;
1120 case LC_RPATH:
1121 rpath = (struct rpath_command *)lc;
1122 if(rpath->cmdsize < sizeof(struct rpath_command)){
1123 error("in swap_object_headers(): malformed load commands "
1124 "(LC_RPATH command %lu has too small cmdsize field)",
1126 return(FALSE);
1128 if(rpath->path.offset >= rpath->cmdsize){
1129 error("in swap_object_headers(): truncated or malformed "
1130 "load commands (path.offset field of LC_RPATH "
1131 "command %lu extends past the end of all load "
1132 "commands)", i);
1133 return(FALSE);
1135 break;
1137 case LC_ENCRYPTION_INFO:
1138 ec = (struct encryption_info_command *)lc;
1139 if(ec->cmdsize != sizeof(struct encryption_info_command)){
1140 error("in swap_object_headers(): malformed load commands "
1141 "(LC_ENCRYPTION_INFO command %lu has incorrect "
1142 "cmdsize", i);
1143 return(FALSE);
1145 break;
1147 case LC_DYLD_INFO:
1148 case LC_DYLD_INFO_ONLY:
1149 dc = (struct dyld_info_command *)lc;
1150 if(dc->cmdsize != sizeof(struct dyld_info_command)){
1151 error("in swap_object_headers(): malformed load commands "
1152 "(LC_DYLD_INFO command %lu has incorrect "
1153 "cmdsize", i);
1154 return(FALSE);
1156 break;
1158 default:
1159 error("in swap_object_headers(): malformed load commands "
1160 "(unknown load command %lu)", i);
1161 return(FALSE);
1164 lc = (struct load_command *)((char *)lc + l.cmdsize);
1165 /* check that next load command does not extends past the end */
1166 if((char *)lc > (char *)load_commands + sizeofcmds){
1167 error("in swap_object_headers(): truncated or malformed load "
1168 "commands (load command %lu extends past the end of all "
1169 "load commands)", i + 1);
1170 return(FALSE);
1173 /* check for an inconsistent size of the load commands */
1174 if((char *)load_commands + sizeofcmds != (char *)lc){
1175 error("in swap_object_headers(): malformed load commands "
1176 "(inconsistent sizeofcmds field in mach header)");
1177 return(FALSE);
1182 * Now knowing the load commands can be parsed swap them.
1184 target_byte_sex = get_host_byte_sex() == BIG_ENDIAN_BYTE_SEX ?
1185 LITTLE_ENDIAN_BYTE_SEX : BIG_ENDIAN_BYTE_SEX;
1186 for(i = 0, lc = load_commands; i < ncmds; i++){
1187 l = *lc;
1188 switch(lc->cmd){
1189 case LC_SEGMENT:
1190 sg = (struct segment_command *)lc;
1191 s = (struct section *)
1192 ((char *)sg + sizeof(struct segment_command));
1193 swap_section(s, sg->nsects, target_byte_sex);
1194 swap_segment_command(sg, target_byte_sex);
1195 break;
1197 case LC_SEGMENT_64:
1198 sg64 = (struct segment_command_64 *)lc;
1199 s64 = (struct section_64 *)
1200 ((char *)sg64 + sizeof(struct segment_command_64));
1201 swap_section_64(s64, sg64->nsects, target_byte_sex);
1202 swap_segment_command_64(sg64, target_byte_sex);
1203 break;
1205 case LC_SYMTAB:
1206 st = (struct symtab_command *)lc;
1207 swap_symtab_command(st, target_byte_sex);
1208 break;
1210 case LC_DYSYMTAB:
1211 dyst = (struct dysymtab_command *)lc;
1212 swap_dysymtab_command(dyst, target_byte_sex);
1213 break;
1215 case LC_SYMSEG:
1216 ss = (struct symseg_command *)lc;
1217 swap_symseg_command(ss, target_byte_sex);
1218 break;
1220 case LC_IDFVMLIB:
1221 case LC_LOADFVMLIB:
1222 fl = (struct fvmlib_command *)lc;
1223 swap_fvmlib_command(fl, target_byte_sex);
1224 break;
1226 case LC_ID_DYLIB:
1227 case LC_LOAD_DYLIB:
1228 case LC_LOAD_WEAK_DYLIB:
1229 case LC_REEXPORT_DYLIB:
1230 case LC_LOAD_UPWARD_DYLIB:
1231 case LC_LAZY_LOAD_DYLIB:
1232 dl = (struct dylib_command *)lc;
1233 swap_dylib_command(dl, target_byte_sex);
1234 break;
1236 case LC_SUB_FRAMEWORK:
1237 sub = (struct sub_framework_command *)lc;
1238 swap_sub_framework_command(sub, target_byte_sex);
1239 break;
1241 case LC_SUB_UMBRELLA:
1242 usub = (struct sub_umbrella_command *)lc;
1243 swap_sub_umbrella_command(usub, target_byte_sex);
1244 break;
1246 case LC_SUB_LIBRARY:
1247 lsub = (struct sub_library_command *)lc;
1248 swap_sub_library_command(lsub, target_byte_sex);
1249 break;
1251 case LC_SUB_CLIENT:
1252 csub = (struct sub_client_command *)lc;
1253 swap_sub_client_command(csub, target_byte_sex);
1254 break;
1256 case LC_PREBOUND_DYLIB:
1257 pbdylib = (struct prebound_dylib_command *)lc;
1258 swap_prebound_dylib_command(pbdylib, target_byte_sex);
1259 break;
1261 case LC_ID_DYLINKER:
1262 case LC_LOAD_DYLINKER:
1263 case LC_DYLD_ENVIRONMENT:
1264 dyld = (struct dylinker_command *)lc;
1265 swap_dylinker_command(dyld, target_byte_sex);
1266 break;
1268 case LC_UNIXTHREAD:
1269 case LC_THREAD:
1270 ut = (struct thread_command *)lc;
1271 state = (char *)ut + sizeof(struct thread_command);
1272 p = (char *)ut + ut->cmdsize;
1273 swap_thread_command(ut, target_byte_sex);
1275 if(cputype == CPU_TYPE_MC680x0){
1276 struct m68k_thread_state_regs *cpu;
1277 struct m68k_thread_state_68882 *fpu;
1278 struct m68k_thread_state_user_reg *user_reg;
1280 while(state < p){
1281 flavor = *((uint32_t *)state);
1282 *((uint32_t *)state) = SWAP_INT(flavor);
1283 state += sizeof(uint32_t);
1284 count = *((uint32_t *)state);
1285 *((uint32_t *)state) = SWAP_INT(count);
1286 state += sizeof(uint32_t);
1287 switch(flavor){
1288 case M68K_THREAD_STATE_REGS:
1289 cpu = (struct m68k_thread_state_regs *)state;
1290 swap_m68k_thread_state_regs(cpu, target_byte_sex);
1291 state += sizeof(struct m68k_thread_state_regs);
1292 break;
1293 case M68K_THREAD_STATE_68882:
1294 fpu = (struct m68k_thread_state_68882 *)state;
1295 swap_m68k_thread_state_68882(fpu, target_byte_sex);
1296 state += sizeof(struct m68k_thread_state_68882);
1297 break;
1298 case M68K_THREAD_STATE_USER_REG:
1299 user_reg =
1300 (struct m68k_thread_state_user_reg *)state;
1301 swap_m68k_thread_state_user_reg(user_reg,
1302 target_byte_sex);
1303 state += sizeof(struct m68k_thread_state_user_reg);
1304 break;
1307 break;
1309 if(cputype == CPU_TYPE_POWERPC ||
1310 cputype == CPU_TYPE_VEO ||
1311 cputype == CPU_TYPE_POWERPC64){
1312 ppc_thread_state_t *cpu;
1313 ppc_thread_state64_t *cpu64;
1314 ppc_float_state_t *fpu;
1315 ppc_exception_state_t *except;
1317 while(state < p){
1318 flavor = *((uint32_t *)state);
1319 *((uint32_t *)state) = SWAP_INT(flavor);
1320 state += sizeof(uint32_t);
1321 count = *((uint32_t *)state);
1322 *((uint32_t *)state) = SWAP_INT(count);
1323 state += sizeof(uint32_t);
1324 switch(flavor){
1325 case PPC_THREAD_STATE:
1326 cpu = (ppc_thread_state_t *)state;
1327 swap_ppc_thread_state_t(cpu, target_byte_sex);
1328 state += sizeof(ppc_thread_state_t);
1329 break;
1330 case PPC_THREAD_STATE64:
1331 cpu64 = (ppc_thread_state64_t *)state;
1332 swap_ppc_thread_state64_t(cpu64, target_byte_sex);
1333 state += sizeof(ppc_thread_state64_t);
1334 break;
1335 case PPC_FLOAT_STATE:
1336 fpu = (ppc_float_state_t *)state;
1337 swap_ppc_float_state_t(fpu, target_byte_sex);
1338 state += sizeof(ppc_float_state_t);
1339 case PPC_EXCEPTION_STATE:
1340 except = (ppc_exception_state_t *)state;
1341 swap_ppc_exception_state_t(except, target_byte_sex);
1342 state += sizeof(ppc_exception_state_t);
1343 break;
1346 break;
1348 if(cputype == CPU_TYPE_MC88000){
1349 m88k_thread_state_grf_t *cpu;
1350 m88k_thread_state_xrf_t *fpu;
1351 m88k_thread_state_user_t *user;
1352 m88110_thread_state_impl_t *spu;
1354 while(state < p){
1355 flavor = *((uint32_t *)state);
1356 *((uint32_t *)state) = SWAP_INT(flavor);
1357 state += sizeof(uint32_t);
1358 count = *((uint32_t *)state);
1359 *((uint32_t *)state) = SWAP_INT(count);
1360 state += sizeof(uint32_t);
1361 switch(flavor){
1362 case M88K_THREAD_STATE_GRF:
1363 cpu = (m88k_thread_state_grf_t *)state;
1364 swap_m88k_thread_state_grf_t(cpu,
1365 target_byte_sex);
1366 state += sizeof(m88k_thread_state_grf_t);
1367 break;
1368 case M88K_THREAD_STATE_XRF:
1369 fpu = (m88k_thread_state_xrf_t *)state;
1370 swap_m88k_thread_state_xrf_t(fpu,
1371 target_byte_sex);
1372 state += sizeof(m88k_thread_state_xrf_t);
1373 break;
1374 case M88K_THREAD_STATE_USER:
1375 user = (m88k_thread_state_user_t *)state;
1376 swap_m88k_thread_state_user_t(user,
1377 target_byte_sex);
1378 state += sizeof(m88k_thread_state_user_t);
1379 break;
1380 case M88110_THREAD_STATE_IMPL:
1381 spu = (m88110_thread_state_impl_t *)state;
1382 swap_m88110_thread_state_impl_t(spu,
1383 target_byte_sex);
1384 state += sizeof(m88110_thread_state_impl_t);
1385 break;
1388 break;
1390 if(cputype == CPU_TYPE_I860){
1391 struct i860_thread_state_regs *cpu;
1393 while(state < p){
1394 flavor = *((uint32_t *)state);
1395 *((uint32_t *)state) = SWAP_INT(flavor);
1396 state += sizeof(uint32_t);
1397 count = *((uint32_t *)state);
1398 *((uint32_t *)state) = SWAP_INT(count);
1399 state += sizeof(uint32_t);
1400 switch(flavor){
1401 case I860_THREAD_STATE_REGS:
1402 cpu = (struct i860_thread_state_regs *)state;
1403 swap_i860_thread_state_regs(cpu, target_byte_sex);
1404 state += sizeof(struct i860_thread_state_regs);
1405 break;
1408 break;
1410 if(cputype == CPU_TYPE_I386
1411 #ifdef x86_THREAD_STATE64
1412 || cputype == CPU_TYPE_X86_64
1413 #endif /* x86_THREAD_STATE64 */
1415 i386_thread_state_t *cpu;
1416 #ifdef x86_THREAD_STATE64
1417 x86_thread_state64_t *cpu64;
1418 #endif /* x86_THREAD_STATE64 */
1419 /* current i386 thread states */
1420 #if i386_THREAD_STATE == 1
1421 struct i386_float_state *fpu;
1422 i386_exception_state_t *exc;
1423 #endif /* i386_THREAD_STATE == 1 */
1425 /* i386 thread states on older releases */
1426 #if i386_THREAD_STATE == -1
1427 i386_thread_fpstate_t *fpu;
1428 i386_thread_exceptstate_t *exc;
1429 i386_thread_cthreadstate_t *user;
1430 #endif /* i386_THREAD_STATE == -1 */
1432 while(state < p){
1433 flavor = *((uint32_t *)state);
1434 *((uint32_t *)state) = SWAP_INT(flavor);
1435 state += sizeof(uint32_t);
1436 count = *((uint32_t *)state);
1437 *((uint32_t *)state) = SWAP_INT(count);
1438 state += sizeof(uint32_t);
1439 switch((int)flavor){
1440 case i386_THREAD_STATE:
1441 /* current i386 thread states */
1442 #if i386_THREAD_STATE == 1
1443 case -1:
1444 #endif /* i386_THREAD_STATE == 1 */
1445 /* i386 thread states on older releases */
1446 #if i386_THREAD_STATE == -1
1447 case 1:
1448 #endif /* i386_THREAD_STATE == -1 */
1449 cpu = (i386_thread_state_t *)state;
1450 swap_i386_thread_state(cpu, target_byte_sex);
1451 state += sizeof(i386_thread_state_t);
1452 break;
1453 /* current i386 thread states */
1454 #if i386_THREAD_STATE == 1
1455 case i386_FLOAT_STATE:
1456 fpu = (struct i386_float_state *)state;
1457 swap_i386_float_state(fpu, target_byte_sex);
1458 state += sizeof(struct i386_float_state);
1459 break;
1460 case i386_EXCEPTION_STATE:
1461 exc = (i386_exception_state_t *)state;
1462 swap_i386_exception_state(exc, target_byte_sex);
1463 state += sizeof(i386_exception_state_t);
1464 break;
1465 #endif /* i386_THREAD_STATE == 1 */
1467 /* i386 thread states on older releases */
1468 #if i386_THREAD_STATE == -1
1469 case i386_THREAD_FPSTATE:
1470 fpu = (i386_thread_fpstate_t *)state;
1471 swap_i386_thread_fpstate(fpu, target_byte_sex);
1472 state += sizeof(i386_thread_fpstate_t);
1473 break;
1474 case i386_THREAD_EXCEPTSTATE:
1475 exc = (i386_thread_exceptstate_t *)state;
1476 swap_i386_thread_exceptstate(exc, target_byte_sex);
1477 state += sizeof(i386_thread_exceptstate_t);
1478 break;
1479 case i386_THREAD_CTHREADSTATE:
1480 user = (i386_thread_cthreadstate_t *)state;
1481 swap_i386_thread_cthreadstate(user,target_byte_sex);
1482 state += sizeof(i386_thread_cthreadstate_t);
1483 break;
1484 #endif /* i386_THREAD_STATE == -1 */
1485 #ifdef x86_THREAD_STATE64
1486 case x86_THREAD_STATE64:
1487 cpu64 = (x86_thread_state64_t *)state;
1488 swap_x86_thread_state64(cpu64, target_byte_sex);
1489 state += sizeof(x86_thread_state64_t);
1490 break;
1491 #endif /* x86_THREAD_STATE64 */
1494 break;
1496 if(cputype == CPU_TYPE_HPPA){
1497 struct hp_pa_integer_thread_state *cpu;
1498 struct hp_pa_frame_thread_state *frame;
1499 struct hp_pa_fp_thread_state *fpu;
1501 while(state < p){
1502 flavor = *((uint32_t *)state);
1503 *((uint32_t *)state) = SWAP_INT(flavor);
1504 state += sizeof(uint32_t);
1505 count = *((uint32_t *)state);
1506 *((uint32_t *)state) = SWAP_INT(count);
1507 state += sizeof(uint32_t);
1508 switch(flavor){
1509 case HPPA_INTEGER_THREAD_STATE:
1510 cpu = (struct hp_pa_integer_thread_state *)state;
1511 swap_hppa_integer_thread_state(cpu,
1512 target_byte_sex);
1513 state += sizeof(struct hp_pa_integer_thread_state);
1514 break;
1515 case HPPA_FRAME_THREAD_STATE:
1516 frame = (struct hp_pa_frame_thread_state *)state;
1517 swap_hppa_frame_thread_state(frame,
1518 target_byte_sex);
1519 state += sizeof(struct hp_pa_frame_thread_state);
1520 break;
1521 case HPPA_FP_THREAD_STATE:
1522 fpu = (struct hp_pa_fp_thread_state *)state;
1523 swap_hppa_fp_thread_state(fpu,
1524 target_byte_sex);
1525 state += sizeof(struct hp_pa_fp_thread_state);
1526 break;
1529 break;
1532 if(cputype == CPU_TYPE_SPARC) {
1533 struct sparc_thread_state_regs *cpu;
1534 struct sparc_thread_state_fpu *fpu;
1536 while (state < p) {
1537 flavor = *((uint32_t *) state);
1538 *((uint32_t *) state) = SWAP_INT(flavor);
1539 state += sizeof(uint32_t);
1540 count = *((unsigned int *) state);
1541 *((unsigned int *) state) = SWAP_INT(count);
1542 state += sizeof(uint32_t);
1543 switch (flavor) {
1544 case SPARC_THREAD_STATE_REGS:
1545 cpu = (struct sparc_thread_state_regs *) state;
1546 swap_sparc_thread_state_regs(cpu, target_byte_sex);
1547 state += sizeof(struct sparc_thread_state_regs);
1548 break;
1549 case SPARC_THREAD_STATE_FPU:
1550 fpu = (struct sparc_thread_state_fpu *) state;
1551 swap_sparc_thread_state_fpu(fpu, target_byte_sex);
1552 state += sizeof(struct sparc_thread_state_fpu);
1553 break;
1556 break;
1558 if(cputype == CPU_TYPE_ARM){
1559 arm_thread_state_t *cpu;
1561 while(state < p){
1562 flavor = *((uint32_t *)state);
1563 *((uint32_t *)state) = SWAP_INT(flavor);
1564 state += sizeof(uint32_t);
1565 count = *((uint32_t *)state);
1566 *((uint32_t *)state) = SWAP_INT(count);
1567 state += sizeof(uint32_t);
1568 switch(flavor){
1569 case ARM_THREAD_STATE:
1570 cpu = (arm_thread_state_t *)state;
1571 swap_arm_thread_state_t(cpu, target_byte_sex);
1572 state += sizeof(arm_thread_state_t);
1573 break;
1576 break;
1578 break;
1580 case LC_MAIN:
1581 ep = (struct entry_point_command *)lc;
1582 swap_entry_point_command(ep, target_byte_sex);
1583 break;
1585 case LC_SOURCE_VERSION:
1586 sv = (struct source_version_command *)lc;
1587 swap_source_version_command(sv, target_byte_sex);
1588 break;
1590 case LC_IDENT:
1591 id = (struct ident_command *)lc;
1592 swap_ident_command(id, target_byte_sex);
1593 break;
1595 case LC_ROUTINES:
1596 rc = (struct routines_command *)lc;
1597 swap_routines_command(rc, target_byte_sex);
1598 break;
1600 case LC_ROUTINES_64:
1601 rc64 = (struct routines_command_64 *)lc;
1602 swap_routines_command_64(rc64, target_byte_sex);
1603 break;
1605 case LC_TWOLEVEL_HINTS:
1606 hints = (struct twolevel_hints_command *)lc;
1607 swap_twolevel_hints_command(hints, target_byte_sex);
1608 break;
1610 case LC_PREBIND_CKSUM:
1611 cs = (struct prebind_cksum_command *)lc;
1612 swap_prebind_cksum_command(cs, target_byte_sex);
1613 break;
1615 case LC_UUID:
1616 uuid = (struct uuid_command *)lc;
1617 swap_uuid_command(uuid, target_byte_sex);
1618 break;
1620 case LC_CODE_SIGNATURE:
1621 case LC_SEGMENT_SPLIT_INFO:
1622 case LC_FUNCTION_STARTS:
1623 case LC_DATA_IN_CODE:
1624 case LC_DYLIB_CODE_SIGN_DRS:
1625 ld = (struct linkedit_data_command *)lc;
1626 swap_linkedit_data_command(ld, target_byte_sex);
1627 break;
1629 case LC_RPATH:
1630 rpath = (struct rpath_command *)lc;
1631 swap_rpath_command(rpath, target_byte_sex);
1632 break;
1634 case LC_ENCRYPTION_INFO:
1635 ec = (struct encryption_info_command *)lc;
1636 swap_encryption_command(ec, target_byte_sex);
1637 break;
1639 case LC_DYLD_INFO:
1640 case LC_DYLD_INFO_ONLY:
1641 dc = (struct dyld_info_command *)lc;
1642 swap_dyld_info_command(dc, target_byte_sex);
1643 break;
1645 case LC_VERSION_MIN_MACOSX:
1646 case LC_VERSION_MIN_IPHONEOS:
1647 vc = (struct version_min_command *)lc;
1648 swap_version_min_command(vc, target_byte_sex);
1649 break;
1652 lc = (struct load_command *)((char *)lc + l.cmdsize);
1654 if(mh != NULL)
1655 swap_mach_header(mh, target_byte_sex);
1656 else
1657 swap_mach_header_64(mh64, target_byte_sex);
1659 return(TRUE);