Integrate cctools-751 changes
[striptease.git] / libstuff / swap_headers.c
blobbb197886b28ca8b4bdf9d09c336344cc5983e6c1
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 dylib_command *dl;
79 struct sub_framework_command *sub;
80 struct sub_umbrella_command *usub;
81 struct sub_library_command *lsub;
82 struct sub_client_command *csub;
83 struct prebound_dylib_command *pbdylib;
84 struct dylinker_command *dyld;
85 struct routines_command *rc;
86 struct routines_command_64 *rc64;
87 struct twolevel_hints_command *hints;
88 struct prebind_cksum_command *cs;
89 struct uuid_command *uuid;
90 struct linkedit_data_command *ld;
91 struct rpath_command *rpath;
92 struct encryption_info_command *ec;
93 struct dyld_info_command *dc;
94 uint32_t flavor, count;
95 unsigned long nflavor;
96 char *p, *state, *cmd_name;
98 magic = *((uint32_t *)mach_header);
99 if(magic == MH_MAGIC){
100 mh = (struct mach_header *)mach_header;
101 ncmds = mh->ncmds;
102 sizeofcmds = mh->sizeofcmds;
103 cputype = mh->cputype;
104 cpusubtype = mh->cpusubtype;
105 cmd_multiple = 4;
106 mh64 = NULL;
108 else{
109 mh64 = (struct mach_header_64 *)mach_header;
110 ncmds = mh64->ncmds;
111 sizeofcmds = mh64->sizeofcmds;
112 cputype = mh64->cputype;
113 cpusubtype = mh64->cpusubtype;
114 cmd_multiple = 8;
115 mh = NULL;
118 * Make a pass through the load commands checking them to the level
119 * that they can be parsed and then swapped.
121 for(i = 0, lc = load_commands; i < ncmds; i++){
122 l = *lc;
123 /* check load command size for a correct multiple size */
124 if(lc->cmdsize % cmd_multiple != 0){
125 error("in swap_object_headers(): malformed load command %lu "
126 "(cmdsize not a multiple of %u)", i, cmd_multiple);
127 return(FALSE);
129 /* check that load command does not extends past end of commands */
130 if((char *)lc + lc->cmdsize >
131 (char *)load_commands + sizeofcmds){
132 error("in swap_object_headers(): truncated or malformed load "
133 "command %lu (extends past the end of the all load "
134 "commands)", i);
135 return(FALSE);
137 /* check that the load command size is not zero */
138 if(lc->cmdsize == 0){
139 error("in swap_object_headers(): malformed load command %lu "
140 "(cmdsize is zero)", i);
141 return(FALSE);
143 switch(lc->cmd){
144 case LC_SEGMENT:
145 sg = (struct segment_command *)lc;
146 if(sg->cmdsize != sizeof(struct segment_command) +
147 sg->nsects * sizeof(struct section)){
148 error("in swap_object_headers(): malformed load command "
149 "(inconsistent cmdsize in LC_SEGMENT command %lu for "
150 "the number of sections)", i);
151 return(FALSE);
153 break;
155 case LC_SEGMENT_64:
156 sg64 = (struct segment_command_64 *)lc;
157 if(sg64->cmdsize != sizeof(struct segment_command_64) +
158 sg64->nsects * sizeof(struct section_64)){
159 error("in swap_object_headers(): malformed load command "
160 "(inconsistent cmdsize in LC_SEGMENT_64 command %lu "
161 "for the number of sections)", i);
162 return(FALSE);
164 break;
166 case LC_SYMTAB:
167 st = (struct symtab_command *)lc;
168 if(st->cmdsize != sizeof(struct symtab_command)){
169 error("in swap_object_headers(): malformed load commands "
170 "(LC_SYMTAB command %lu has incorrect cmdsize", i);
171 return(FALSE);
173 break;
175 case LC_DYSYMTAB:
176 dyst = (struct dysymtab_command *)lc;
177 if(dyst->cmdsize != sizeof(struct dysymtab_command)){
178 error("in swap_object_headers(): malformed load commands "
179 "(LC_DYSYMTAB command %lu has incorrect cmdsize", i);
180 return(FALSE);
182 break;
184 case LC_SYMSEG:
185 ss = (struct symseg_command *)lc;
186 if(ss->cmdsize != sizeof(struct symseg_command)){
187 error("in swap_object_headers(): malformed load command "
188 "(LC_SYMSEG command %lu has incorrect cmdsize", i);
189 return(FALSE);
191 break;
193 case LC_IDFVMLIB:
194 case LC_LOADFVMLIB:
195 fl = (struct fvmlib_command *)lc;
196 if(fl->cmdsize < sizeof(struct fvmlib_command)){
197 error("in swap_object_headers(): malformed load commands "
198 "(%s command %lu has too small cmdsize field)",
199 fl->cmd == LC_IDFVMLIB ? "LC_IDFVMLIB" :
200 "LC_LOADFVMLIB", i);
201 return(FALSE);
203 if(fl->fvmlib.name.offset >= fl->cmdsize){
204 error("in swap_object_headers(): truncated or malformed "
205 "load commands (name.offset field of %s command %lu "
206 "extends past the end of all load commands)",
207 fl->cmd == LC_IDFVMLIB ? "LC_IDFVMLIB" :
208 "LC_LOADFVMLIB", i);
209 return(FALSE);
211 break;
213 case LC_ID_DYLIB:
214 cmd_name = "LC_ID_DYLIB";
215 goto check_dylib_command;
216 case LC_LOAD_DYLIB:
217 cmd_name = "LC_LOAD_DYLIB";
218 goto check_dylib_command;
219 case LC_LOAD_WEAK_DYLIB:
220 cmd_name = "LC_LOAD_WEAK_DYLIB";
221 goto check_dylib_command;
222 case LC_REEXPORT_DYLIB:
223 cmd_name = "LC_REEXPORT_DYLIB";
224 goto check_dylib_command;
225 case LC_LAZY_LOAD_DYLIB:
226 cmd_name = "LC_LAZY_LOAD_DYLIB";
227 goto check_dylib_command;
228 check_dylib_command:
229 dl = (struct dylib_command *)lc;
230 if(dl->cmdsize < sizeof(struct dylib_command)){
231 error("in swap_object_headers(): malformed load commands "
232 "(%s command %lu has too small cmdsize field)",
233 cmd_name, i);
234 return(FALSE);
236 if(dl->dylib.name.offset >= dl->cmdsize){
237 error("in swap_object_headers(): truncated or malformed "
238 "load commands (name.offset field of %s command %lu "
239 "extends past the end of all load commands)",
240 cmd_name, i);
241 return(FALSE);
243 break;
245 case LC_SUB_FRAMEWORK:
246 sub = (struct sub_framework_command *)lc;
247 if(sub->cmdsize < sizeof(struct sub_framework_command)){
248 error("in swap_object_headers(): malformed load commands "
249 "(LC_SUB_FRAMEWORK command %lu has too small cmdsize "
250 "field)", i);
251 return(FALSE);
253 if(sub->umbrella.offset >= sub->cmdsize){
254 error("in swap_object_headers(): truncated or malformed "
255 "load commands (umbrella.offset field of "
256 "LC_SUB_FRAMEWORK command %lu extends past the end "
257 "of all load commands)", i);
258 return(FALSE);
260 break;
262 case LC_SUB_UMBRELLA:
263 usub = (struct sub_umbrella_command *)lc;
264 if(usub->cmdsize < sizeof(struct sub_umbrella_command)){
265 error("in swap_object_headers(): malformed load commands "
266 "(LC_SUB_UMBRELLA command %lu has too small cmdsize "
267 "field)", i);
268 return(FALSE);
270 if(usub->sub_umbrella.offset >= usub->cmdsize){
271 error("in swap_object_headers(): truncated or malformed "
272 "load commands (sub_umbrella.offset field of "
273 "LC_SUB_UMBRELLA command %lu extends past the end "
274 "of all load commands)", i);
275 return(FALSE);
277 break;
279 case LC_SUB_LIBRARY:
280 lsub = (struct sub_library_command *)lc;
281 if(lsub->cmdsize < sizeof(struct sub_library_command)){
282 error("in swap_object_headers(): malformed load commands "
283 "(LC_SUB_LIBRARY command %lu has too small cmdsize "
284 "field)", i);
285 return(FALSE);
287 if(lsub->sub_library.offset >= lsub->cmdsize){
288 error("in swap_object_headers(): truncated or malformed "
289 "load commands (sub_library.offset field of "
290 "LC_SUB_LIBRARY command %lu extends past the end "
291 "of all load commands)", i);
292 return(FALSE);
294 break;
296 case LC_SUB_CLIENT:
297 csub = (struct sub_client_command *)lc;
298 if(csub->cmdsize < sizeof(struct sub_client_command)){
299 error("in swap_object_headers(): malformed load commands "
300 "(LC_SUB_CLIENT command %lu has too small cmdsize "
301 "field)", i);
302 return(FALSE);
304 if(csub->client.offset >= csub->cmdsize){
305 error("in swap_object_headers(): truncated or malformed "
306 "load commands (client.offset field of "
307 "LC_SUB_CLIENT command %lu extends past the end "
308 "of all load commands)", i);
309 return(FALSE);
311 break;
313 case LC_PREBOUND_DYLIB:
314 pbdylib = (struct prebound_dylib_command *)lc;
315 if(pbdylib->cmdsize < sizeof(struct prebound_dylib_command)){
316 error("in swap_object_headers(): malformed load commands "
317 "(LC_PREBOUND_DYLIB command %lu has too small "
318 "cmdsize field)", i);
319 return(FALSE);
321 if(pbdylib->name.offset >= pbdylib->cmdsize){
322 error("in swap_object_headers(): truncated or malformed "
323 "load commands (name.offset field of "
324 "LC_PREBOUND_DYLIB command %lu extends past the end "
325 "of all load commands)", i);
326 return(FALSE);
328 if(pbdylib->linked_modules.offset >= pbdylib->cmdsize){
329 error("in swap_object_headers(): truncated or malformed "
330 "load commands (linked_modules.offset field of "
331 "LC_PREBOUND_DYLIB command %lu extends past the end "
332 "of all load commands)", i);
333 return(FALSE);
335 break;
337 case LC_ID_DYLINKER:
338 case LC_LOAD_DYLINKER:
339 dyld = (struct dylinker_command *)lc;
340 if(dyld->cmdsize < sizeof(struct dylinker_command)){
341 error("in swap_object_headers(): malformed load commands "
342 "(%s command %lu has too small cmdsize field)",
343 dyld->cmd == LC_ID_DYLINKER ? "LC_ID_DYLINKER" :
344 "LC_LOAD_DYLINKER", i);
345 return(FALSE);
347 if(dyld->name.offset >= dyld->cmdsize){
348 error("in swap_object_headers(): truncated or malformed "
349 "load commands (name.offset field of %s command %lu "
350 "extends past the end of all load commands)",
351 dyld->cmd == LC_ID_DYLINKER ? "LC_ID_DYLINKER" :
352 "LC_LOAD_DYLINKER", i);
353 return(FALSE);
355 break;
357 case LC_UNIXTHREAD:
358 case LC_THREAD:
359 ut = (struct thread_command *)lc;
360 state = (char *)ut + sizeof(struct thread_command);
362 if(cputype == CPU_TYPE_MC680x0){
363 struct m68k_thread_state_regs *cpu;
364 struct m68k_thread_state_68882 *fpu;
365 struct m68k_thread_state_user_reg *user_reg;
367 nflavor = 0;
368 p = (char *)ut + ut->cmdsize;
369 while(state < p){
370 flavor = *((uint32_t *)state);
371 state += sizeof(uint32_t);
372 count = *((uint32_t *)state);
373 state += sizeof(uint32_t);
374 switch(flavor){
375 case M68K_THREAD_STATE_REGS:
376 if(count != M68K_THREAD_STATE_REGS_COUNT){
377 error("in swap_object_headers(): malformed "
378 "load commands (count "
379 "not M68K_THREAD_STATE_REGS_COUNT for "
380 "flavor number %lu which is a M68K_THREAD_"
381 "STATE_REGS flavor in %s command %lu)",
382 nflavor, ut->cmd == LC_UNIXTHREAD ?
383 "LC_UNIXTHREAD" : "LC_THREAD", i);
384 return(FALSE);
386 cpu = (struct m68k_thread_state_regs *)state;
387 state += sizeof(struct m68k_thread_state_regs);
388 break;
389 case M68K_THREAD_STATE_68882:
390 if(count != M68K_THREAD_STATE_68882_COUNT){
391 error("in swap_object_headers(): malformed "
392 "load commands (count "
393 "not M68K_THREAD_STATE_68882_COUNT for "
394 "flavor number %lu which is a M68K_THREAD_"
395 "STATE_68882 flavor in %s command %lu)",
396 nflavor, ut->cmd == LC_UNIXTHREAD ?
397 "LC_UNIXTHREAD" : "LC_THREAD", i);
398 return(FALSE);
400 fpu = (struct m68k_thread_state_68882 *)state;
401 state += sizeof(struct m68k_thread_state_68882);
402 break;
403 case M68K_THREAD_STATE_USER_REG:
404 if(count != M68K_THREAD_STATE_USER_REG_COUNT){
405 error("in swap_object_headers(): malformed "
406 "load commands (count "
407 "not M68K_THREAD_STATE_USER_REG_COUNT for "
408 "flavor number %lu which is a M68K_THREAD_"
409 "STATE_USER_REG flavor in %s command %lu)",
410 nflavor, ut->cmd == LC_UNIXTHREAD ?
411 "LC_UNIXTHREAD" : "LC_THREAD", i);
412 return(FALSE);
414 user_reg =
415 (struct m68k_thread_state_user_reg *)state;
416 state += sizeof(struct m68k_thread_state_user_reg);
417 break;
418 default:
419 error("in swap_object_headers(): malformed "
420 "load commands (unknown "
421 "flavor %u for flavor number %lu in %s command"
422 " %lu can't byte swap it)", flavor, nflavor,
423 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
424 "LC_THREAD", i);
425 return(FALSE);
427 nflavor++;
429 break;
431 if(cputype == CPU_TYPE_POWERPC ||
432 cputype == CPU_TYPE_VEO ||
433 cputype == CPU_TYPE_POWERPC64){
434 ppc_thread_state_t *cpu;
435 ppc_float_state_t *fpu;
436 ppc_exception_state_t *except;
437 ppc_thread_state64_t *cpu64;
439 nflavor = 0;
440 p = (char *)ut + ut->cmdsize;
441 while(state < p){
442 flavor = *((uint32_t *)state);
443 state += sizeof(uint32_t);
444 count = *((uint32_t *)state);
445 state += sizeof(uint32_t);
446 switch(flavor){
447 case PPC_THREAD_STATE:
448 if(count != PPC_THREAD_STATE_COUNT){
449 error("in swap_object_headers(): malformed "
450 "load commands (count "
451 "not PPC_THREAD_STATE_COUNT for "
452 "flavor number %lu which is a PPC_THREAD_"
453 "STATE flavor in %s command %lu)",
454 nflavor, ut->cmd == LC_UNIXTHREAD ?
455 "LC_UNIXTHREAD" : "LC_THREAD", i);
456 return(FALSE);
458 cpu = (ppc_thread_state_t *)state;
459 state += sizeof(ppc_thread_state_t);
460 break;
461 case PPC_FLOAT_STATE:
462 if(count != PPC_FLOAT_STATE_COUNT){
463 error("in swap_object_headers(): malformed "
464 "load commands (count "
465 "not PPC_FLOAT_STATE_COUNT for "
466 "flavor number %lu which is a PPC_FLOAT_"
467 "STATE flavor in %s command %lu)",
468 nflavor, ut->cmd == LC_UNIXTHREAD ?
469 "LC_UNIXTHREAD" : "LC_THREAD", i);
470 return(FALSE);
472 fpu = (ppc_float_state_t *)state;
473 state += sizeof(ppc_float_state_t);
474 break;
475 case PPC_EXCEPTION_STATE:
476 if(count != PPC_EXCEPTION_STATE_COUNT){
477 error("in swap_object_headers(): malformed "
478 "load commands (count "
479 "not PPC_EXCEPTION_STATE_COUNT for "
480 "flavor number %lu which is a PPC_EXCEPT"
481 "ION_STATE flavor in %s command %lu)",
482 nflavor, ut->cmd == LC_UNIXTHREAD ?
483 "LC_UNIXTHREAD" : "LC_THREAD", i);
484 return(FALSE);
486 except = (ppc_exception_state_t *)state;
487 state += sizeof(ppc_exception_state_t);
488 break;
489 case PPC_THREAD_STATE64:
490 if(count != PPC_THREAD_STATE64_COUNT){
491 error("in swap_object_headers(): malformed "
492 "load commands (count "
493 "not PPC_THREAD_STATE64_COUNT for "
494 "flavor number %lu which is a PPC_THREAD_"
495 "STATE64 flavor in %s command %lu)",
496 nflavor, ut->cmd == LC_UNIXTHREAD ?
497 "LC_UNIXTHREAD" : "LC_THREAD", i);
498 return(FALSE);
500 cpu64 = (ppc_thread_state64_t *)state;
501 state += sizeof(ppc_thread_state64_t);
502 break;
503 default:
504 error("in swap_object_headers(): malformed "
505 "load commands (unknown "
506 "flavor %u for flavor number %lu in %s command"
507 " %lu can't byte swap it)", flavor, nflavor,
508 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
509 "LC_THREAD", i);
510 return(FALSE);
512 nflavor++;
514 break;
516 if(cputype == CPU_TYPE_MC88000){
517 m88k_thread_state_grf_t *cpu;
518 m88k_thread_state_xrf_t *fpu;
519 m88k_thread_state_user_t *user;
520 m88110_thread_state_impl_t *spu;
522 nflavor = 0;
523 p = (char *)ut + ut->cmdsize;
524 while(state < p){
525 flavor = *((uint32_t *)state);
526 state += sizeof(uint32_t);
527 count = *((uint32_t *)state);
528 state += sizeof(uint32_t);
529 switch(flavor){
530 case M88K_THREAD_STATE_GRF:
531 if(count != M88K_THREAD_STATE_GRF_COUNT){
532 error("in swap_object_headers(): malformed "
533 "load commands (count "
534 "not M88K_THREAD_STATE_GRF_COUNT for "
535 "flavor number %lu which is a M88K_THREAD_"
536 "STATE_GRF flavor in %s command %lu)",
537 nflavor, ut->cmd == LC_UNIXTHREAD ?
538 "LC_UNIXTHREAD" : "LC_THREAD", i);
539 return(FALSE);
541 cpu = (m88k_thread_state_grf_t *)state;
542 state += sizeof(m88k_thread_state_grf_t);
543 break;
544 case M88K_THREAD_STATE_XRF:
545 if(count != M88K_THREAD_STATE_XRF_COUNT){
546 error("in swap_object_headers(): malformed "
547 "load commands (count "
548 "not M88K_THREAD_STATE_XRF_COUNT for "
549 "flavor number %lu which is a M88K_THREAD_"
550 "STATE_XRF flavor in %s command %lu)",
551 nflavor, ut->cmd == LC_UNIXTHREAD ?
552 "LC_UNIXTHREAD" : "LC_THREAD", i);
553 return(FALSE);
555 fpu = (m88k_thread_state_xrf_t *)state;
556 state += sizeof(m88k_thread_state_xrf_t);
557 break;
558 case M88K_THREAD_STATE_USER:
559 if(count != M88K_THREAD_STATE_USER_COUNT){
560 error("in swap_object_headers(): malformed "
561 "load commands (count "
562 "not M88K_THREAD_STATE_USER_COUNT for "
563 "flavor number %lu which is a M88K_THREAD_"
564 "STATE_USER flavor in %s command %lu)",
565 nflavor, ut->cmd == LC_UNIXTHREAD ?
566 "LC_UNIXTHREAD" : "LC_THREAD", i);
567 return(FALSE);
569 user = (m88k_thread_state_user_t *)state;
570 state += sizeof(m88k_thread_state_user_t);
571 break;
572 case M88110_THREAD_STATE_IMPL:
573 if(count != M88110_THREAD_STATE_IMPL_COUNT){
574 error("in swap_object_headers(): malformed "
575 "load commands (count "
576 "not M88110_THREAD_STATE_IMPL_COUNT for "
577 "flavor number %lu which is a M88110_THREAD"
578 "_STATE_IMPL flavor in %s command %lu)",
579 nflavor, ut->cmd == LC_UNIXTHREAD ?
580 "LC_UNIXTHREAD" : "LC_THREAD", i);
581 return(FALSE);
583 spu = (m88110_thread_state_impl_t *)state;
584 state += sizeof(m88110_thread_state_impl_t);
585 break;
586 default:
587 error("in swap_object_headers(): malformed "
588 "load commands (unknown "
589 "flavor %u for flavor number %lu in %s command"
590 " %lu can't byte swap it)", flavor, nflavor,
591 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
592 "LC_THREAD", i);
593 return(FALSE);
595 nflavor++;
597 break;
599 if(cputype == CPU_TYPE_I860){
600 struct i860_thread_state_regs *cpu;
602 nflavor = 0;
603 p = (char *)ut + ut->cmdsize;
604 while(state < p){
605 flavor = *((uint32_t *)state);
606 state += sizeof(uint32_t);
607 count = *((uint32_t *)state);
608 state += sizeof(uint32_t);
609 switch(flavor){
610 case I860_THREAD_STATE_REGS:
611 if(count != I860_THREAD_STATE_REGS_COUNT){
612 error("in swap_object_headers(): malformed "
613 "load commands (count "
614 "not I860_THREAD_STATE_REGS_COUNT for "
615 "flavor number %lu which is a I860_THREAD_"
616 "STATE_REGS flavor in %s command %lu)",
617 nflavor, ut->cmd == LC_UNIXTHREAD ?
618 "LC_UNIXTHREAD" : "LC_THREAD", i);
619 return(FALSE);
621 cpu = (struct i860_thread_state_regs *)state;
622 state += sizeof(struct i860_thread_state_regs);
623 break;
624 default:
625 error("in swap_object_headers(): malformed "
626 "load commands (unknown "
627 "flavor %u for flavor number %lu in %s command"
628 " %lu can't byte swap it)", flavor, nflavor,
629 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
630 "LC_THREAD", i);
631 return(FALSE);
633 nflavor++;
635 break;
637 if(cputype == CPU_TYPE_I386
638 #ifdef x86_THREAD_STATE64
639 || cputype == CPU_TYPE_X86_64
640 #endif /* x86_THREAD_STATE64 */
642 i386_thread_state_t *cpu;
643 #ifdef x86_THREAD_STATE64
644 x86_thread_state64_t *cpu64;
645 #endif /* x86_THREAD_STATE64 */
646 /* current i386 thread states */
647 #if i386_THREAD_STATE == 1
648 struct i386_float_state *fpu;
649 i386_exception_state_t *exc;
650 #endif /* i386_THREAD_STATE == 1 */
652 /* i386 thread states on older releases */
653 #if i386_THREAD_STATE == -1
654 i386_thread_fpstate_t *fpu;
655 i386_thread_exceptstate_t *exc;
656 i386_thread_cthreadstate_t *user;
657 #endif /* i386_THREAD_STATE == -1 */
659 nflavor = 0;
660 p = (char *)ut + ut->cmdsize;
661 while(state < p){
662 flavor = *((uint32_t *)state);
663 state += sizeof(uint32_t);
664 count = *((uint32_t *)state);
665 state += sizeof(uint32_t);
666 switch(flavor){
667 case i386_THREAD_STATE:
668 /* current i386 thread states */
669 #if i386_THREAD_STATE == 1
670 case -1:
671 #endif /* i386_THREAD_STATE == 1 */
672 /* i386 thread states on older releases */
673 #if i386_THREAD_STATE == -1
674 case 1:
675 #endif /* i386_THREAD_STATE == -1 */
676 if(count != i386_THREAD_STATE_COUNT){
677 error("in swap_object_headers(): malformed "
678 "load commands (count "
679 "not i386_THREAD_STATE_COUNT for flavor "
680 "number %lu which is a i386_THREAD_STATE "
681 "flavor in %s command %lu)", nflavor,
682 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
683 "LC_THREAD", i);
684 return(FALSE);
686 cpu = (i386_thread_state_t *)state;
687 state += sizeof(i386_thread_state_t);
688 break;
689 /* current i386 thread states */
690 #if i386_THREAD_STATE == 1
691 case i386_FLOAT_STATE:
692 if(count != i386_FLOAT_STATE_COUNT){
693 error("in swap_object_headers(): malformed "
694 "load commands (count "
695 "not i386_FLOAT_STATE_COUNT for flavor "
696 "number %lu which is a i386_FLOAT_STATE "
697 "flavor in %s command %lu)", nflavor,
698 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
699 "LC_THREAD", i);
700 return(FALSE);
702 fpu = (struct i386_float_state *)state;
703 state += sizeof(struct i386_float_state);
704 break;
705 case i386_EXCEPTION_STATE:
706 if(count != I386_EXCEPTION_STATE_COUNT){
707 error("in swap_object_headers(): malformed "
708 "load commands (count "
709 "not I386_EXCEPTION_STATE_COUNT for "
710 "flavor number %lu which is a i386_"
711 "EXCEPTION_STATE flavor in %s command %lu)",
712 nflavor,
713 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
714 "LC_THREAD", i);
715 return(FALSE);
717 exc = (i386_exception_state_t *)state;
718 state += sizeof(i386_exception_state_t);
719 break;
720 #endif /* i386_THREAD_STATE == 1 */
722 /* i386 thread states on older releases */
723 #if i386_THREAD_STATE == -1
724 case i386_THREAD_FPSTATE:
725 if(count != i386_THREAD_FPSTATE_COUNT){
726 error("in swap_object_headers(): malformed "
727 "load commands (count "
728 "not i386_THREAD_FPSTATE_COUNT for flavor "
729 "number %lu which is a i386_THREAD_FPSTATE "
730 "flavor in %s command %lu)", nflavor,
731 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
732 "LC_THREAD", i);
733 return(FALSE);
735 fpu = (i386_thread_fpstate_t *)state;
736 state += sizeof(i386_thread_fpstate_t);
737 break;
738 case i386_THREAD_EXCEPTSTATE:
739 if(count != i386_THREAD_EXCEPTSTATE_COUNT){
740 error("in swap_object_headers(): malformed "
741 "load commands (count "
742 "not i386_THREAD_EXCEPTSTATE_COUNT for "
743 "flavor number %lu which is a i386_THREAD_"
744 "EXCEPTSTATE flavor in %s command %lu)",
745 nflavor,
746 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
747 "LC_THREAD", i);
748 return(FALSE);
750 exc = (i386_thread_exceptstate_t *)state;
751 state += sizeof(i386_thread_fpstate_t);
752 break;
753 case i386_THREAD_CTHREADSTATE:
754 if(count != i386_THREAD_CTHREADSTATE_COUNT){
755 error("in swap_object_headers(): malformed "
756 "load commands (count "
757 "not i386_THREAD_CTHREADSTATE_COUNT for "
758 "flavor number %lu which is a i386_THREAD_"
759 "CTHREADSTATE flavor in %s command %lu)",
760 nflavor,
761 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
762 "LC_THREAD", i);
763 return(FALSE);
765 user = (i386_thread_cthreadstate_t *)state;
766 state += sizeof(i386_thread_fpstate_t);
767 break;
768 #endif /* i386_THREAD_STATE == -1 */
769 #ifdef x86_THREAD_STATE64
770 case x86_THREAD_STATE64:
771 if(count != x86_THREAD_STATE64_COUNT){
772 error("in swap_object_headers(): malformed "
773 "load commands (count "
774 "not x86_THREAD_STATE64_COUNT for "
775 "flavor number %lu which is an x86_THREAD_"
776 "STATE64 flavor in %s command %lu)",
777 nflavor,
778 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
779 "LC_THREAD", i);
780 return(FALSE);
782 cpu64 = (x86_thread_state64_t *)state;
783 state += sizeof(x86_thread_state64_t);
784 break;
785 #endif /* x86_THREAD_STATE64 */
786 default:
787 error("in swap_object_headers(): malformed "
788 "load commands (unknown "
789 "flavor %u for flavor number %lu in %s command"
790 " %lu can't byte swap it)", flavor, nflavor,
791 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
792 "LC_THREAD", i);
793 return(FALSE);
795 nflavor++;
797 break;
799 if(cputype == CPU_TYPE_HPPA){
800 struct hp_pa_integer_thread_state *cpu;
801 struct hp_pa_frame_thread_state *frame;
802 struct hp_pa_fp_thread_state *fpu;
804 nflavor = 0;
805 p = (char *)ut + ut->cmdsize;
806 while(state < p){
807 flavor = *((uint32_t *)state);
808 state += sizeof(uint32_t);
809 count = *((uint32_t *)state);
810 state += sizeof(uint32_t);
811 switch(flavor){
812 case HPPA_INTEGER_THREAD_STATE:
813 if(count != HPPA_INTEGER_THREAD_STATE_COUNT){
814 error("in swap_object_headers(): malformed "
815 "load commands (count "
816 "not HPPA_INTEGER_THREAD_STATE_COUNT for "
817 "flavor number %lu which is a HPPA_INTEGER"
818 "_THREAD_STATE flavor in %s command %lu)",
819 nflavor, ut->cmd == LC_UNIXTHREAD ?
820 "LC_UNIXTHREAD" : "LC_THREAD", i);
821 return(FALSE);
823 cpu = (struct hp_pa_integer_thread_state *)state;
824 state += sizeof(struct hp_pa_integer_thread_state);
825 break;
826 case HPPA_FRAME_THREAD_STATE:
827 if(count != HPPA_FRAME_THREAD_STATE_COUNT){
828 error("in swap_object_headers(): malformed "
829 "load commands (count "
830 "not HPPA_FRAME_THREAD_STATE_COUNT for "
831 "flavor number %lu which is a HPPA_FRAME"
832 "_THREAD_STATE flavor in %s command %lu)",
833 nflavor, ut->cmd == LC_UNIXTHREAD ?
834 "LC_UNIXTHREAD" : "LC_THREAD", i);
835 return(FALSE);
837 frame = (struct hp_pa_frame_thread_state *)state;
838 state += sizeof(struct hp_pa_frame_thread_state);
839 break;
840 case HPPA_FP_THREAD_STATE:
841 if(count != HPPA_FP_THREAD_STATE_COUNT){
842 error("in swap_object_headers(): malformed "
843 "load commands (count "
844 "not HPPA_FP_THREAD_STATE_COUNT for "
845 "flavor number %lu which is a HPPA_FP"
846 "_THREAD_STATE flavor in %s command %lu)",
847 nflavor, ut->cmd == LC_UNIXTHREAD ?
848 "LC_UNIXTHREAD" : "LC_THREAD", i);
849 return(FALSE);
851 fpu = (struct hp_pa_fp_thread_state *)state;
852 state += sizeof(struct hp_pa_fp_thread_state);
853 break;
854 default:
855 error("in swap_object_headers(): malformed "
856 "load commands (unknown "
857 "flavor %u for flavor number %lu in %s command"
858 " %lu can't byte swap it)", flavor, nflavor,
859 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
860 "LC_THREAD", i);
861 return(FALSE);
863 nflavor++;
865 break;
867 if(cputype == CPU_TYPE_SPARC) {
868 struct sparc_thread_state_regs *cpu;
869 struct sparc_thread_state_fpu *fpu;
871 nflavor = 0;
872 p = (char *)ut + ut->cmdsize;
873 while (state < p) {
874 flavor = *((uint32_t *) state);
875 state += sizeof(uint32_t);
876 count = *((uint32_t *) state);
877 state += sizeof(uint32_t);
878 switch (flavor) {
879 case SPARC_THREAD_STATE_REGS:
880 if (count != SPARC_THREAD_STATE_REGS_COUNT) {
881 error("in swap_object_headers(): malformed "
882 "load commands (count "
883 "not SPARC_THREAD_STATE_REGS_COUNT for "
884 "flavor number %lu which is a SPARC_THREAD_"
885 "STATE_REGS flavor in %s command %lu)",
886 nflavor, ut->cmd == LC_UNIXTHREAD ?
887 "LC_UNIXTHREAD" : "LC_THREAD", i);
888 return(FALSE);
890 cpu = (struct sparc_thread_state_regs *) state;
891 state += sizeof(struct sparc_thread_state_regs);
892 break;
893 case SPARC_THREAD_STATE_FPU:
894 if (count != SPARC_THREAD_STATE_FPU_COUNT) {
895 error("in swap_object_headers(): malformed "
896 "load commands (count "
897 "not SPARC_THREAD_STATE_FPU_COUNT for "
898 "flavor number %lu which is a SPARC_THREAD_"
899 "STATE_FPU flavor in %s command %lu)",
900 nflavor, ut->cmd == LC_UNIXTHREAD ?
901 "LC_UNIXTHREAD" : "LC_THREAD", i);
902 return(FALSE);
904 fpu = (struct sparc_thread_state_fpu *) state;
905 state += sizeof(struct sparc_thread_state_fpu);
906 break;
909 break;
911 if(cputype == CPU_TYPE_ARM){
912 arm_thread_state_t *cpu;
914 nflavor = 0;
915 p = (char *)ut + ut->cmdsize;
916 while(state < p){
917 flavor = *((uint32_t *)state);
918 state += sizeof(uint32_t);
919 count = *((uint32_t *)state);
920 state += sizeof(uint32_t);
921 switch(flavor){
922 case ARM_THREAD_STATE:
923 if(count != ARM_THREAD_STATE_COUNT){
924 error("in swap_object_headers(): malformed "
925 "load commands (count "
926 "not ARM_THREAD_STATE_COUNT for "
927 "flavor number %lu which is a ARM_THREAD_"
928 "STATE flavor in %s command %lu)",
929 nflavor, ut->cmd == LC_UNIXTHREAD ?
930 "LC_UNIXTHREAD" : "LC_THREAD", i);
931 return(FALSE);
933 cpu = (arm_thread_state_t *)state;
934 state += sizeof(arm_thread_state_t);
935 break;
936 default:
937 error("in swap_object_headers(): malformed load "
938 "commands (unknown flavor for flavor number "
939 "%lu in %s command %lu can't byte swap it)",
940 nflavor, ut->cmd == LC_UNIXTHREAD ?
941 "LC_UNIXTHREAD" : "LC_THREAD", i);
942 return(FALSE);
944 nflavor++;
946 break;
948 error("in swap_object_headers(): malformed load commands "
949 "(unknown cputype (%d) and cpusubtype (%d) of object and "
950 "can't byte swap %s command %lu)", cputype,
951 cpusubtype, ut->cmd == LC_UNIXTHREAD ?
952 "LC_UNIXTHREAD" : "LC_THREAD", i);
953 return(FALSE);
954 case LC_IDENT:
955 id = (struct ident_command *)lc;
956 if((char *)id + id->cmdsize >
957 (char *)load_commands + sizeofcmds){
958 error("in swap_object_headers(): truncated or malformed "
959 "load commands (cmdsize field of LC_IDENT command %lu "
960 "extends past the end of the load commands)", i);
961 return(FALSE);
963 break;
965 case LC_ROUTINES:
966 rc = (struct routines_command *)lc;
967 if(rc->cmdsize != sizeof(struct routines_command)){
968 error("in swap_object_headers(): malformed load commands ("
969 "LC_ROUTINES command %lu has incorrect cmdsize",
971 return(FALSE);
973 break;
975 case LC_ROUTINES_64:
976 rc64 = (struct routines_command_64 *)lc;
977 if(rc64->cmdsize != sizeof(struct routines_command_64)){
978 error("in swap_object_headers(): malformed load commands ("
979 "LC_ROUTINES_64 command %lu has incorrect cmdsize",
981 return(FALSE);
983 break;
985 case LC_TWOLEVEL_HINTS:
986 hints = (struct twolevel_hints_command *)lc;
987 if(hints->cmdsize != sizeof(struct twolevel_hints_command)){
988 error("in swap_object_headers(): malformed load commands "
989 "(LC_TWOLEVEL_HINTS command %lu has incorrect "
990 "cmdsize", i);
991 return(FALSE);
993 break;
995 case LC_PREBIND_CKSUM:
996 cs = (struct prebind_cksum_command *)lc;
997 if(cs->cmdsize != sizeof(struct prebind_cksum_command)){
998 error("in swap_object_headers(): malformed load commands "
999 "(LC_PREBIND_CKSUM command %lu has incorrect cmdsize",
1001 return(FALSE);
1003 break;
1005 case LC_UUID:
1006 uuid = (struct uuid_command *)lc;
1007 if(uuid->cmdsize != sizeof(struct uuid_command)){
1008 error("in swap_object_headers(): malformed load commands "
1009 "(LC_UUID command %lu has incorrect cmdsize", i);
1010 return(FALSE);
1012 break;
1014 case LC_CODE_SIGNATURE:
1015 ld = (struct linkedit_data_command *)lc;
1016 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1017 error("in swap_object_headers(): malformed load commands "
1018 "(LC_CODE_SIGNATURE command %lu has incorrect "
1019 "cmdsize", i);
1020 return(FALSE);
1022 break;
1024 case LC_SEGMENT_SPLIT_INFO:
1025 ld = (struct linkedit_data_command *)lc;
1026 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1027 error("in swap_object_headers(): malformed load commands "
1028 "(LC_SEGMENT_SPLIT_INFO command %lu has incorrect "
1029 "cmdsize", i);
1030 return(FALSE);
1032 break;
1034 case LC_RPATH:
1035 rpath = (struct rpath_command *)lc;
1036 if(rpath->cmdsize < sizeof(struct rpath_command)){
1037 error("in swap_object_headers(): malformed load commands "
1038 "(LC_RPATH command %lu has too small cmdsize field)",
1040 return(FALSE);
1042 if(rpath->path.offset >= rpath->cmdsize){
1043 error("in swap_object_headers(): truncated or malformed "
1044 "load commands (path.offset field of LC_RPATH "
1045 "command %lu extends past the end of all load "
1046 "commands)", i);
1047 return(FALSE);
1049 break;
1051 case LC_ENCRYPTION_INFO:
1052 ec = (struct encryption_info_command *)lc;
1053 if(ec->cmdsize != sizeof(struct encryption_info_command)){
1054 error("in swap_object_headers(): malformed load commands "
1055 "(LC_ENCRYPTION_INFO command %lu has incorrect "
1056 "cmdsize", i);
1057 return(FALSE);
1059 break;
1061 case LC_DYLD_INFO:
1062 case LC_DYLD_INFO_ONLY:
1063 dc = (struct dyld_info_command *)lc;
1064 if(dc->cmdsize != sizeof(struct dyld_info_command)){
1065 error("in swap_object_headers(): malformed load commands "
1066 "(LC_DYLD_INFO command %lu has incorrect "
1067 "cmdsize", i);
1068 return(FALSE);
1070 break;
1072 default:
1073 error("in swap_object_headers(): malformed load commands "
1074 "(unknown load command %lu)", i);
1075 return(FALSE);
1078 lc = (struct load_command *)((char *)lc + l.cmdsize);
1079 /* check that next load command does not extends past the end */
1080 if((char *)lc > (char *)load_commands + sizeofcmds){
1081 error("in swap_object_headers(): truncated or malformed load "
1082 "commands (load command %lu extends past the end of all "
1083 "load commands)", i + 1);
1084 return(FALSE);
1087 /* check for an inconsistent size of the load commands */
1088 if((char *)load_commands + sizeofcmds != (char *)lc){
1089 error("in swap_object_headers(): malformed load commands "
1090 "(inconsistent sizeofcmds field in mach header)");
1091 return(FALSE);
1096 * Now knowing the load commands can be parsed swap them.
1098 target_byte_sex = get_host_byte_sex() == BIG_ENDIAN_BYTE_SEX ?
1099 LITTLE_ENDIAN_BYTE_SEX : BIG_ENDIAN_BYTE_SEX;
1100 for(i = 0, lc = load_commands; i < ncmds; i++){
1101 l = *lc;
1102 switch(lc->cmd){
1103 case LC_SEGMENT:
1104 sg = (struct segment_command *)lc;
1105 s = (struct section *)
1106 ((char *)sg + sizeof(struct segment_command));
1107 swap_section(s, sg->nsects, target_byte_sex);
1108 swap_segment_command(sg, target_byte_sex);
1109 break;
1111 case LC_SEGMENT_64:
1112 sg64 = (struct segment_command_64 *)lc;
1113 s64 = (struct section_64 *)
1114 ((char *)sg64 + sizeof(struct segment_command_64));
1115 swap_section_64(s64, sg64->nsects, target_byte_sex);
1116 swap_segment_command_64(sg64, target_byte_sex);
1117 break;
1119 case LC_SYMTAB:
1120 st = (struct symtab_command *)lc;
1121 swap_symtab_command(st, target_byte_sex);
1122 break;
1124 case LC_DYSYMTAB:
1125 dyst = (struct dysymtab_command *)lc;
1126 swap_dysymtab_command(dyst, target_byte_sex);
1127 break;
1129 case LC_SYMSEG:
1130 ss = (struct symseg_command *)lc;
1131 swap_symseg_command(ss, target_byte_sex);
1132 break;
1134 case LC_IDFVMLIB:
1135 case LC_LOADFVMLIB:
1136 fl = (struct fvmlib_command *)lc;
1137 swap_fvmlib_command(fl, target_byte_sex);
1138 break;
1140 case LC_ID_DYLIB:
1141 case LC_LOAD_DYLIB:
1142 case LC_LOAD_WEAK_DYLIB:
1143 case LC_REEXPORT_DYLIB:
1144 case LC_LAZY_LOAD_DYLIB:
1145 dl = (struct dylib_command *)lc;
1146 swap_dylib_command(dl, target_byte_sex);
1147 break;
1149 case LC_SUB_FRAMEWORK:
1150 sub = (struct sub_framework_command *)lc;
1151 swap_sub_framework_command(sub, target_byte_sex);
1152 break;
1154 case LC_SUB_UMBRELLA:
1155 usub = (struct sub_umbrella_command *)lc;
1156 swap_sub_umbrella_command(usub, target_byte_sex);
1157 break;
1159 case LC_SUB_LIBRARY:
1160 lsub = (struct sub_library_command *)lc;
1161 swap_sub_library_command(lsub, target_byte_sex);
1162 break;
1164 case LC_SUB_CLIENT:
1165 csub = (struct sub_client_command *)lc;
1166 swap_sub_client_command(csub, target_byte_sex);
1167 break;
1169 case LC_PREBOUND_DYLIB:
1170 pbdylib = (struct prebound_dylib_command *)lc;
1171 swap_prebound_dylib_command(pbdylib, target_byte_sex);
1172 break;
1174 case LC_ID_DYLINKER:
1175 case LC_LOAD_DYLINKER:
1176 dyld = (struct dylinker_command *)lc;
1177 swap_dylinker_command(dyld, target_byte_sex);
1178 break;
1180 case LC_UNIXTHREAD:
1181 case LC_THREAD:
1182 ut = (struct thread_command *)lc;
1183 state = (char *)ut + sizeof(struct thread_command);
1184 p = (char *)ut + ut->cmdsize;
1185 swap_thread_command(ut, target_byte_sex);
1187 if(cputype == CPU_TYPE_MC680x0){
1188 struct m68k_thread_state_regs *cpu;
1189 struct m68k_thread_state_68882 *fpu;
1190 struct m68k_thread_state_user_reg *user_reg;
1192 while(state < p){
1193 flavor = *((uint32_t *)state);
1194 *((uint32_t *)state) = SWAP_INT(flavor);
1195 state += sizeof(uint32_t);
1196 count = *((uint32_t *)state);
1197 *((uint32_t *)state) = SWAP_INT(count);
1198 state += sizeof(uint32_t);
1199 switch(flavor){
1200 case M68K_THREAD_STATE_REGS:
1201 cpu = (struct m68k_thread_state_regs *)state;
1202 swap_m68k_thread_state_regs(cpu, target_byte_sex);
1203 state += sizeof(struct m68k_thread_state_regs);
1204 break;
1205 case M68K_THREAD_STATE_68882:
1206 fpu = (struct m68k_thread_state_68882 *)state;
1207 swap_m68k_thread_state_68882(fpu, target_byte_sex);
1208 state += sizeof(struct m68k_thread_state_68882);
1209 break;
1210 case M68K_THREAD_STATE_USER_REG:
1211 user_reg =
1212 (struct m68k_thread_state_user_reg *)state;
1213 swap_m68k_thread_state_user_reg(user_reg,
1214 target_byte_sex);
1215 state += sizeof(struct m68k_thread_state_user_reg);
1216 break;
1219 break;
1221 if(cputype == CPU_TYPE_POWERPC ||
1222 cputype == CPU_TYPE_VEO ||
1223 cputype == CPU_TYPE_POWERPC64){
1224 ppc_thread_state_t *cpu;
1225 ppc_thread_state64_t *cpu64;
1226 ppc_float_state_t *fpu;
1227 ppc_exception_state_t *except;
1229 while(state < p){
1230 flavor = *((uint32_t *)state);
1231 *((uint32_t *)state) = SWAP_INT(flavor);
1232 state += sizeof(uint32_t);
1233 count = *((uint32_t *)state);
1234 *((uint32_t *)state) = SWAP_INT(count);
1235 state += sizeof(uint32_t);
1236 switch(flavor){
1237 case PPC_THREAD_STATE:
1238 cpu = (ppc_thread_state_t *)state;
1239 swap_ppc_thread_state_t(cpu, target_byte_sex);
1240 state += sizeof(ppc_thread_state_t);
1241 break;
1242 case PPC_THREAD_STATE64:
1243 cpu64 = (ppc_thread_state64_t *)state;
1244 swap_ppc_thread_state64_t(cpu64, target_byte_sex);
1245 state += sizeof(ppc_thread_state64_t);
1246 break;
1247 case PPC_FLOAT_STATE:
1248 fpu = (ppc_float_state_t *)state;
1249 swap_ppc_float_state_t(fpu, target_byte_sex);
1250 state += sizeof(ppc_float_state_t);
1251 case PPC_EXCEPTION_STATE:
1252 except = (ppc_exception_state_t *)state;
1253 swap_ppc_exception_state_t(except, target_byte_sex);
1254 state += sizeof(ppc_exception_state_t);
1255 break;
1258 break;
1260 if(cputype == CPU_TYPE_MC88000){
1261 m88k_thread_state_grf_t *cpu;
1262 m88k_thread_state_xrf_t *fpu;
1263 m88k_thread_state_user_t *user;
1264 m88110_thread_state_impl_t *spu;
1266 while(state < p){
1267 flavor = *((uint32_t *)state);
1268 *((uint32_t *)state) = SWAP_INT(flavor);
1269 state += sizeof(uint32_t);
1270 count = *((uint32_t *)state);
1271 *((uint32_t *)state) = SWAP_INT(count);
1272 state += sizeof(uint32_t);
1273 switch(flavor){
1274 case M88K_THREAD_STATE_GRF:
1275 cpu = (m88k_thread_state_grf_t *)state;
1276 swap_m88k_thread_state_grf_t(cpu,
1277 target_byte_sex);
1278 state += sizeof(m88k_thread_state_grf_t);
1279 break;
1280 case M88K_THREAD_STATE_XRF:
1281 fpu = (m88k_thread_state_xrf_t *)state;
1282 swap_m88k_thread_state_xrf_t(fpu,
1283 target_byte_sex);
1284 state += sizeof(m88k_thread_state_xrf_t);
1285 break;
1286 case M88K_THREAD_STATE_USER:
1287 user = (m88k_thread_state_user_t *)state;
1288 swap_m88k_thread_state_user_t(user,
1289 target_byte_sex);
1290 state += sizeof(m88k_thread_state_user_t);
1291 break;
1292 case M88110_THREAD_STATE_IMPL:
1293 spu = (m88110_thread_state_impl_t *)state;
1294 swap_m88110_thread_state_impl_t(spu,
1295 target_byte_sex);
1296 state += sizeof(m88110_thread_state_impl_t);
1297 break;
1300 break;
1302 if(cputype == CPU_TYPE_I860){
1303 struct i860_thread_state_regs *cpu;
1305 while(state < p){
1306 flavor = *((uint32_t *)state);
1307 *((uint32_t *)state) = SWAP_INT(flavor);
1308 state += sizeof(uint32_t);
1309 count = *((uint32_t *)state);
1310 *((uint32_t *)state) = SWAP_INT(count);
1311 state += sizeof(uint32_t);
1312 switch(flavor){
1313 case I860_THREAD_STATE_REGS:
1314 cpu = (struct i860_thread_state_regs *)state;
1315 swap_i860_thread_state_regs(cpu, target_byte_sex);
1316 state += sizeof(struct i860_thread_state_regs);
1317 break;
1320 break;
1322 if(cputype == CPU_TYPE_I386
1323 #ifdef x86_THREAD_STATE64
1324 || cputype == CPU_TYPE_X86_64
1325 #endif /* x86_THREAD_STATE64 */
1327 i386_thread_state_t *cpu;
1328 #ifdef x86_THREAD_STATE64
1329 x86_thread_state64_t *cpu64;
1330 #endif /* x86_THREAD_STATE64 */
1331 /* current i386 thread states */
1332 #if i386_THREAD_STATE == 1
1333 struct i386_float_state *fpu;
1334 i386_exception_state_t *exc;
1335 #endif /* i386_THREAD_STATE == 1 */
1337 /* i386 thread states on older releases */
1338 #if i386_THREAD_STATE == -1
1339 i386_thread_fpstate_t *fpu;
1340 i386_thread_exceptstate_t *exc;
1341 i386_thread_cthreadstate_t *user;
1342 #endif /* i386_THREAD_STATE == -1 */
1344 while(state < p){
1345 flavor = *((uint32_t *)state);
1346 *((uint32_t *)state) = SWAP_INT(flavor);
1347 state += sizeof(uint32_t);
1348 count = *((uint32_t *)state);
1349 *((uint32_t *)state) = SWAP_INT(count);
1350 state += sizeof(uint32_t);
1351 switch(flavor){
1352 case i386_THREAD_STATE:
1353 /* current i386 thread states */
1354 #if i386_THREAD_STATE == 1
1355 case -1:
1356 #endif /* i386_THREAD_STATE == 1 */
1357 /* i386 thread states on older releases */
1358 #if i386_THREAD_STATE == -1
1359 case 1:
1360 #endif /* i386_THREAD_STATE == -1 */
1361 cpu = (i386_thread_state_t *)state;
1362 swap_i386_thread_state(cpu, target_byte_sex);
1363 state += sizeof(i386_thread_state_t);
1364 break;
1365 /* current i386 thread states */
1366 #if i386_THREAD_STATE == 1
1367 case i386_FLOAT_STATE:
1368 fpu = (struct i386_float_state *)state;
1369 swap_i386_float_state(fpu, target_byte_sex);
1370 state += sizeof(struct i386_float_state);
1371 break;
1372 case i386_EXCEPTION_STATE:
1373 exc = (i386_exception_state_t *)state;
1374 swap_i386_exception_state(exc, target_byte_sex);
1375 state += sizeof(i386_exception_state_t);
1376 break;
1377 #endif /* i386_THREAD_STATE == 1 */
1379 /* i386 thread states on older releases */
1380 #if i386_THREAD_STATE == -1
1381 case i386_THREAD_FPSTATE:
1382 fpu = (i386_thread_fpstate_t *)state;
1383 swap_i386_thread_fpstate(fpu, target_byte_sex);
1384 state += sizeof(i386_thread_fpstate_t);
1385 break;
1386 case i386_THREAD_EXCEPTSTATE:
1387 exc = (i386_thread_exceptstate_t *)state;
1388 swap_i386_thread_exceptstate(exc, target_byte_sex);
1389 state += sizeof(i386_thread_exceptstate_t);
1390 break;
1391 case i386_THREAD_CTHREADSTATE:
1392 user = (i386_thread_cthreadstate_t *)state;
1393 swap_i386_thread_cthreadstate(user,target_byte_sex);
1394 state += sizeof(i386_thread_cthreadstate_t);
1395 break;
1396 #endif /* i386_THREAD_STATE == -1 */
1397 #ifdef x86_THREAD_STATE64
1398 case x86_THREAD_STATE64:
1399 cpu64 = (x86_thread_state64_t *)state;
1400 swap_x86_thread_state64(cpu64, target_byte_sex);
1401 state += sizeof(x86_thread_state64_t);
1402 break;
1403 #endif /* x86_THREAD_STATE64 */
1406 break;
1408 if(cputype == CPU_TYPE_HPPA){
1409 struct hp_pa_integer_thread_state *cpu;
1410 struct hp_pa_frame_thread_state *frame;
1411 struct hp_pa_fp_thread_state *fpu;
1413 while(state < p){
1414 flavor = *((uint32_t *)state);
1415 *((uint32_t *)state) = SWAP_INT(flavor);
1416 state += sizeof(uint32_t);
1417 count = *((uint32_t *)state);
1418 *((uint32_t *)state) = SWAP_INT(count);
1419 state += sizeof(uint32_t);
1420 switch(flavor){
1421 case HPPA_INTEGER_THREAD_STATE:
1422 cpu = (struct hp_pa_integer_thread_state *)state;
1423 swap_hppa_integer_thread_state(cpu,
1424 target_byte_sex);
1425 state += sizeof(struct hp_pa_integer_thread_state);
1426 break;
1427 case HPPA_FRAME_THREAD_STATE:
1428 frame = (struct hp_pa_frame_thread_state *)state;
1429 swap_hppa_frame_thread_state(frame,
1430 target_byte_sex);
1431 state += sizeof(struct hp_pa_frame_thread_state);
1432 break;
1433 case HPPA_FP_THREAD_STATE:
1434 fpu = (struct hp_pa_fp_thread_state *)state;
1435 swap_hppa_fp_thread_state(fpu,
1436 target_byte_sex);
1437 state += sizeof(struct hp_pa_fp_thread_state);
1438 break;
1441 break;
1444 if(cputype == CPU_TYPE_SPARC) {
1445 struct sparc_thread_state_regs *cpu;
1446 struct sparc_thread_state_fpu *fpu;
1448 while (state < p) {
1449 flavor = *((uint32_t *) state);
1450 *((uint32_t *) state) = SWAP_INT(flavor);
1451 state += sizeof(uint32_t);
1452 count = *((unsigned int *) state);
1453 *((unsigned int *) state) = SWAP_INT(count);
1454 state += sizeof(uint32_t);
1455 switch (flavor) {
1456 case SPARC_THREAD_STATE_REGS:
1457 cpu = (struct sparc_thread_state_regs *) state;
1458 swap_sparc_thread_state_regs(cpu, target_byte_sex);
1459 state += sizeof(struct sparc_thread_state_regs);
1460 break;
1461 case SPARC_THREAD_STATE_FPU:
1462 fpu = (struct sparc_thread_state_fpu *) state;
1463 swap_sparc_thread_state_fpu(fpu, target_byte_sex);
1464 state += sizeof(struct sparc_thread_state_fpu);
1465 break;
1468 break;
1470 if(cputype == CPU_TYPE_ARM){
1471 arm_thread_state_t *cpu;
1473 while(state < p){
1474 flavor = *((uint32_t *)state);
1475 *((uint32_t *)state) = SWAP_INT(flavor);
1476 state += sizeof(uint32_t);
1477 count = *((uint32_t *)state);
1478 *((uint32_t *)state) = SWAP_INT(count);
1479 state += sizeof(uint32_t);
1480 switch(flavor){
1481 case ARM_THREAD_STATE:
1482 cpu = (arm_thread_state_t *)state;
1483 swap_arm_thread_state_t(cpu, target_byte_sex);
1484 state += sizeof(arm_thread_state_t);
1485 break;
1488 break;
1490 break;
1492 case LC_IDENT:
1493 id = (struct ident_command *)lc;
1494 swap_ident_command(id, target_byte_sex);
1495 break;
1497 case LC_ROUTINES:
1498 rc = (struct routines_command *)lc;
1499 swap_routines_command(rc, target_byte_sex);
1500 break;
1502 case LC_ROUTINES_64:
1503 rc64 = (struct routines_command_64 *)lc;
1504 swap_routines_command_64(rc64, target_byte_sex);
1505 break;
1507 case LC_TWOLEVEL_HINTS:
1508 hints = (struct twolevel_hints_command *)lc;
1509 swap_twolevel_hints_command(hints, target_byte_sex);
1510 break;
1512 case LC_PREBIND_CKSUM:
1513 cs = (struct prebind_cksum_command *)lc;
1514 swap_prebind_cksum_command(cs, target_byte_sex);
1515 break;
1517 case LC_UUID:
1518 uuid = (struct uuid_command *)lc;
1519 swap_uuid_command(uuid, target_byte_sex);
1520 break;
1522 case LC_CODE_SIGNATURE:
1523 case LC_SEGMENT_SPLIT_INFO:
1524 ld = (struct linkedit_data_command *)lc;
1525 swap_linkedit_data_command(ld, target_byte_sex);
1526 break;
1528 case LC_RPATH:
1529 rpath = (struct rpath_command *)lc;
1530 swap_rpath_command(rpath, target_byte_sex);
1531 break;
1533 case LC_ENCRYPTION_INFO:
1534 ec = (struct encryption_info_command *)lc;
1535 swap_encryption_command(ec, target_byte_sex);
1536 break;
1538 case LC_DYLD_INFO:
1539 case LC_DYLD_INFO_ONLY:
1540 dc = (struct dyld_info_command *)lc;
1541 swap_dyld_info_command(dc, target_byte_sex);
1542 break;
1545 lc = (struct load_command *)((char *)lc + l.cmdsize);
1547 if(mh != NULL)
1548 swap_mach_header(mh, target_byte_sex);
1549 else
1550 swap_mach_header_64(mh64, target_byte_sex);
1552 return(TRUE);