github.com -> github.io
[striptease.git] / libstuff / swap_headers.c
blobbed9c3a7a6af13ded3c22b108f6a6d13d4c948d9
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 #undef MACHINE_THREAD_STATE /* need to undef these to avoid warnings */
44 #undef MACHINE_THREAD_STATE_COUNT
45 #undef THREAD_STATE_NONE
46 #undef VALID_THREAD_STATE_FLAVOR
47 #include <mach/arm/thread_status.h>
48 #include "stuff/bool.h"
49 #include "stuff/bytesex.h"
50 #include "stuff/errors.h"
53 * swap_object_headers() swaps the object file headers from the host byte sex
54 * into the non-host byte sex. It returns TRUE if it can and did swap the
55 * headers else returns FALSE and does not touch the headers and prints an error
56 * using the error() routine.
58 __private_extern__
59 enum bool
60 swap_object_headers(
61 void *mach_header,
62 struct load_command *load_commands)
64 unsigned long i;
65 uint32_t magic, ncmds, sizeofcmds, cmd_multiple;
66 cpu_type_t cputype;
67 cpu_subtype_t cpusubtype;
68 struct mach_header *mh;
69 struct mach_header_64 *mh64;
70 enum byte_sex target_byte_sex;
71 struct load_command *lc, l;
72 struct segment_command *sg;
73 struct segment_command_64 *sg64;
74 struct section *s;
75 struct section_64 *s64;
76 struct symtab_command *st;
77 struct dysymtab_command *dyst;
78 struct symseg_command *ss;
79 struct fvmlib_command *fl;
80 struct thread_command *ut;
81 struct ident_command *id;
82 struct entry_point_command *ep;
83 struct source_version_command *sv;
84 struct dylib_command *dl;
85 struct sub_framework_command *sub;
86 struct sub_umbrella_command *usub;
87 struct sub_library_command *lsub;
88 struct sub_client_command *csub;
89 struct prebound_dylib_command *pbdylib;
90 struct dylinker_command *dyld;
91 struct routines_command *rc;
92 struct routines_command_64 *rc64;
93 struct twolevel_hints_command *hints;
94 struct prebind_cksum_command *cs;
95 struct uuid_command *uuid;
96 struct linkedit_data_command *ld;
97 struct rpath_command *rpath;
98 struct encryption_info_command *ec;
99 struct dyld_info_command *dc;
100 struct version_min_command *vc;
101 uint32_t flavor, count;
102 unsigned long nflavor;
103 char *p, *state, *cmd_name;
105 magic = *((uint32_t *)mach_header);
106 if(magic == MH_MAGIC){
107 mh = (struct mach_header *)mach_header;
108 ncmds = mh->ncmds;
109 sizeofcmds = mh->sizeofcmds;
110 cputype = mh->cputype;
111 cpusubtype = mh->cpusubtype;
112 cmd_multiple = 4;
113 mh64 = NULL;
115 else{
116 mh64 = (struct mach_header_64 *)mach_header;
117 ncmds = mh64->ncmds;
118 sizeofcmds = mh64->sizeofcmds;
119 cputype = mh64->cputype;
120 cpusubtype = mh64->cpusubtype;
121 cmd_multiple = 8;
122 mh = NULL;
125 * Make a pass through the load commands checking them to the level
126 * that they can be parsed and then swapped.
128 for(i = 0, lc = load_commands; i < ncmds; i++){
129 l = *lc;
130 /* check load command size for a correct multiple size */
131 if(lc->cmdsize % cmd_multiple != 0){
132 error("in swap_object_headers(): malformed load command %lu "
133 "(cmdsize not a multiple of %u)", i, cmd_multiple);
134 return(FALSE);
136 /* check that load command does not extends past end of commands */
137 if((char *)lc + lc->cmdsize >
138 (char *)load_commands + sizeofcmds){
139 error("in swap_object_headers(): truncated or malformed load "
140 "command %lu (extends past the end of the all load "
141 "commands)", i);
142 return(FALSE);
144 /* check that the load command size is not zero */
145 if(lc->cmdsize == 0){
146 error("in swap_object_headers(): malformed load command %lu "
147 "(cmdsize is zero)", i);
148 return(FALSE);
150 switch(lc->cmd){
151 case LC_SEGMENT:
152 sg = (struct segment_command *)lc;
153 if(sg->cmdsize != sizeof(struct segment_command) +
154 sg->nsects * sizeof(struct section)){
155 error("in swap_object_headers(): malformed load command "
156 "(inconsistent cmdsize in LC_SEGMENT command %lu for "
157 "the number of sections)", i);
158 return(FALSE);
160 break;
162 case LC_SEGMENT_64:
163 sg64 = (struct segment_command_64 *)lc;
164 if(sg64->cmdsize != sizeof(struct segment_command_64) +
165 sg64->nsects * sizeof(struct section_64)){
166 error("in swap_object_headers(): malformed load command "
167 "(inconsistent cmdsize in LC_SEGMENT_64 command %lu "
168 "for the number of sections)", i);
169 return(FALSE);
171 break;
173 case LC_SYMTAB:
174 st = (struct symtab_command *)lc;
175 if(st->cmdsize != sizeof(struct symtab_command)){
176 error("in swap_object_headers(): malformed load commands "
177 "(LC_SYMTAB command %lu has incorrect cmdsize", i);
178 return(FALSE);
180 break;
182 case LC_DYSYMTAB:
183 dyst = (struct dysymtab_command *)lc;
184 if(dyst->cmdsize != sizeof(struct dysymtab_command)){
185 error("in swap_object_headers(): malformed load commands "
186 "(LC_DYSYMTAB command %lu has incorrect cmdsize", i);
187 return(FALSE);
189 break;
191 case LC_SYMSEG:
192 ss = (struct symseg_command *)lc;
193 if(ss->cmdsize != sizeof(struct symseg_command)){
194 error("in swap_object_headers(): malformed load command "
195 "(LC_SYMSEG command %lu has incorrect cmdsize", i);
196 return(FALSE);
198 break;
200 case LC_IDFVMLIB:
201 case LC_LOADFVMLIB:
202 fl = (struct fvmlib_command *)lc;
203 if(fl->cmdsize < sizeof(struct fvmlib_command)){
204 error("in swap_object_headers(): malformed load commands "
205 "(%s command %lu has too small cmdsize field)",
206 fl->cmd == LC_IDFVMLIB ? "LC_IDFVMLIB" :
207 "LC_LOADFVMLIB", i);
208 return(FALSE);
210 if(fl->fvmlib.name.offset >= fl->cmdsize){
211 error("in swap_object_headers(): truncated or malformed "
212 "load commands (name.offset field of %s command %lu "
213 "extends past the end of all load commands)",
214 fl->cmd == LC_IDFVMLIB ? "LC_IDFVMLIB" :
215 "LC_LOADFVMLIB", i);
216 return(FALSE);
218 break;
220 case LC_ID_DYLIB:
221 cmd_name = "LC_ID_DYLIB";
222 goto check_dylib_command;
223 case LC_LOAD_DYLIB:
224 cmd_name = "LC_LOAD_DYLIB";
225 goto check_dylib_command;
226 case LC_LOAD_WEAK_DYLIB:
227 cmd_name = "LC_LOAD_WEAK_DYLIB";
228 goto check_dylib_command;
229 case LC_REEXPORT_DYLIB:
230 cmd_name = "LC_REEXPORT_DYLIB";
231 goto check_dylib_command;
232 case LC_LOAD_UPWARD_DYLIB:
233 cmd_name = "LC_LOAD_UPWARD_DYLIB";
234 goto check_dylib_command;
235 case LC_LAZY_LOAD_DYLIB:
236 cmd_name = "LC_LAZY_LOAD_DYLIB";
237 goto check_dylib_command;
238 check_dylib_command:
239 dl = (struct dylib_command *)lc;
240 if(dl->cmdsize < sizeof(struct dylib_command)){
241 error("in swap_object_headers(): malformed load commands "
242 "(%s command %lu has too small cmdsize field)",
243 cmd_name, i);
244 return(FALSE);
246 if(dl->dylib.name.offset >= dl->cmdsize){
247 error("in swap_object_headers(): truncated or malformed "
248 "load commands (name.offset field of %s command %lu "
249 "extends past the end of all load commands)",
250 cmd_name, i);
251 return(FALSE);
253 break;
255 case LC_SUB_FRAMEWORK:
256 sub = (struct sub_framework_command *)lc;
257 if(sub->cmdsize < sizeof(struct sub_framework_command)){
258 error("in swap_object_headers(): malformed load commands "
259 "(LC_SUB_FRAMEWORK command %lu has too small cmdsize "
260 "field)", i);
261 return(FALSE);
263 if(sub->umbrella.offset >= sub->cmdsize){
264 error("in swap_object_headers(): truncated or malformed "
265 "load commands (umbrella.offset field of "
266 "LC_SUB_FRAMEWORK command %lu extends past the end "
267 "of all load commands)", i);
268 return(FALSE);
270 break;
272 case LC_SUB_UMBRELLA:
273 usub = (struct sub_umbrella_command *)lc;
274 if(usub->cmdsize < sizeof(struct sub_umbrella_command)){
275 error("in swap_object_headers(): malformed load commands "
276 "(LC_SUB_UMBRELLA command %lu has too small cmdsize "
277 "field)", i);
278 return(FALSE);
280 if(usub->sub_umbrella.offset >= usub->cmdsize){
281 error("in swap_object_headers(): truncated or malformed "
282 "load commands (sub_umbrella.offset field of "
283 "LC_SUB_UMBRELLA command %lu extends past the end "
284 "of all load commands)", i);
285 return(FALSE);
287 break;
289 case LC_SUB_LIBRARY:
290 lsub = (struct sub_library_command *)lc;
291 if(lsub->cmdsize < sizeof(struct sub_library_command)){
292 error("in swap_object_headers(): malformed load commands "
293 "(LC_SUB_LIBRARY command %lu has too small cmdsize "
294 "field)", i);
295 return(FALSE);
297 if(lsub->sub_library.offset >= lsub->cmdsize){
298 error("in swap_object_headers(): truncated or malformed "
299 "load commands (sub_library.offset field of "
300 "LC_SUB_LIBRARY command %lu extends past the end "
301 "of all load commands)", i);
302 return(FALSE);
304 break;
306 case LC_SUB_CLIENT:
307 csub = (struct sub_client_command *)lc;
308 if(csub->cmdsize < sizeof(struct sub_client_command)){
309 error("in swap_object_headers(): malformed load commands "
310 "(LC_SUB_CLIENT command %lu has too small cmdsize "
311 "field)", i);
312 return(FALSE);
314 if(csub->client.offset >= csub->cmdsize){
315 error("in swap_object_headers(): truncated or malformed "
316 "load commands (client.offset field of "
317 "LC_SUB_CLIENT command %lu extends past the end "
318 "of all load commands)", i);
319 return(FALSE);
321 break;
323 case LC_PREBOUND_DYLIB:
324 pbdylib = (struct prebound_dylib_command *)lc;
325 if(pbdylib->cmdsize < sizeof(struct prebound_dylib_command)){
326 error("in swap_object_headers(): malformed load commands "
327 "(LC_PREBOUND_DYLIB command %lu has too small "
328 "cmdsize field)", i);
329 return(FALSE);
331 if(pbdylib->name.offset >= pbdylib->cmdsize){
332 error("in swap_object_headers(): truncated or malformed "
333 "load commands (name.offset field of "
334 "LC_PREBOUND_DYLIB command %lu extends past the end "
335 "of all load commands)", i);
336 return(FALSE);
338 if(pbdylib->linked_modules.offset >= pbdylib->cmdsize){
339 error("in swap_object_headers(): truncated or malformed "
340 "load commands (linked_modules.offset field of "
341 "LC_PREBOUND_DYLIB command %lu extends past the end "
342 "of all load commands)", i);
343 return(FALSE);
345 break;
347 case LC_ID_DYLINKER:
348 cmd_name = "LC_ID_DYLINKER";
349 goto check_dylinker_command;
350 case LC_LOAD_DYLINKER:
351 cmd_name = "LC_LOAD_DYLINKER";
352 goto check_dylinker_command;
353 case LC_DYLD_ENVIRONMENT:
354 cmd_name = "LC_DYLD_ENVIRONMENT";
355 goto check_dylinker_command;
356 check_dylinker_command:
357 dyld = (struct dylinker_command *)lc;
358 if(dyld->cmdsize < sizeof(struct dylinker_command)){
359 error("in swap_object_headers(): malformed load commands "
360 "(%s command %lu has too small cmdsize field)",
361 cmd_name, i);
362 return(FALSE);
364 if(dyld->name.offset >= dyld->cmdsize){
365 error("in swap_object_headers(): truncated or malformed "
366 "load commands (name.offset field of %s command %lu "
367 "extends past the end of all load commands)",
368 cmd_name, i);
369 return(FALSE);
371 break;
373 case LC_UNIXTHREAD:
374 case LC_THREAD:
375 ut = (struct thread_command *)lc;
376 state = (char *)ut + sizeof(struct thread_command);
378 if(cputype == CPU_TYPE_MC680x0){
379 struct m68k_thread_state_regs *cpu;
380 struct m68k_thread_state_68882 *fpu;
381 struct m68k_thread_state_user_reg *user_reg;
383 nflavor = 0;
384 p = (char *)ut + ut->cmdsize;
385 while(state < p){
386 flavor = *((uint32_t *)state);
387 state += sizeof(uint32_t);
388 count = *((uint32_t *)state);
389 state += sizeof(uint32_t);
390 switch(flavor){
391 case M68K_THREAD_STATE_REGS:
392 if(count != M68K_THREAD_STATE_REGS_COUNT){
393 error("in swap_object_headers(): malformed "
394 "load commands (count "
395 "not M68K_THREAD_STATE_REGS_COUNT for "
396 "flavor number %lu which is a M68K_THREAD_"
397 "STATE_REGS flavor in %s command %lu)",
398 nflavor, ut->cmd == LC_UNIXTHREAD ?
399 "LC_UNIXTHREAD" : "LC_THREAD", i);
400 return(FALSE);
402 cpu = (struct m68k_thread_state_regs *)state;
403 state += sizeof(struct m68k_thread_state_regs);
404 break;
405 case M68K_THREAD_STATE_68882:
406 if(count != M68K_THREAD_STATE_68882_COUNT){
407 error("in swap_object_headers(): malformed "
408 "load commands (count "
409 "not M68K_THREAD_STATE_68882_COUNT for "
410 "flavor number %lu which is a M68K_THREAD_"
411 "STATE_68882 flavor in %s command %lu)",
412 nflavor, ut->cmd == LC_UNIXTHREAD ?
413 "LC_UNIXTHREAD" : "LC_THREAD", i);
414 return(FALSE);
416 fpu = (struct m68k_thread_state_68882 *)state;
417 state += sizeof(struct m68k_thread_state_68882);
418 break;
419 case M68K_THREAD_STATE_USER_REG:
420 if(count != M68K_THREAD_STATE_USER_REG_COUNT){
421 error("in swap_object_headers(): malformed "
422 "load commands (count "
423 "not M68K_THREAD_STATE_USER_REG_COUNT for "
424 "flavor number %lu which is a M68K_THREAD_"
425 "STATE_USER_REG flavor in %s command %lu)",
426 nflavor, ut->cmd == LC_UNIXTHREAD ?
427 "LC_UNIXTHREAD" : "LC_THREAD", i);
428 return(FALSE);
430 user_reg =
431 (struct m68k_thread_state_user_reg *)state;
432 state += sizeof(struct m68k_thread_state_user_reg);
433 break;
434 default:
435 error("in swap_object_headers(): malformed "
436 "load commands (unknown "
437 "flavor %u for flavor number %lu in %s command"
438 " %lu can't byte swap it)", flavor, nflavor,
439 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
440 "LC_THREAD", i);
441 return(FALSE);
443 nflavor++;
445 break;
447 if(cputype == CPU_TYPE_POWERPC ||
448 cputype == CPU_TYPE_VEO ||
449 cputype == CPU_TYPE_POWERPC64){
450 ppc_thread_state_t *cpu;
451 ppc_float_state_t *fpu;
452 ppc_exception_state_t *except;
453 ppc_thread_state64_t *cpu64;
455 nflavor = 0;
456 p = (char *)ut + ut->cmdsize;
457 while(state < p){
458 flavor = *((uint32_t *)state);
459 state += sizeof(uint32_t);
460 count = *((uint32_t *)state);
461 state += sizeof(uint32_t);
462 switch(flavor){
463 case PPC_THREAD_STATE:
464 if(count != PPC_THREAD_STATE_COUNT){
465 error("in swap_object_headers(): malformed "
466 "load commands (count "
467 "not PPC_THREAD_STATE_COUNT for "
468 "flavor number %lu which is a PPC_THREAD_"
469 "STATE flavor in %s command %lu)",
470 nflavor, ut->cmd == LC_UNIXTHREAD ?
471 "LC_UNIXTHREAD" : "LC_THREAD", i);
472 return(FALSE);
474 cpu = (ppc_thread_state_t *)state;
475 state += sizeof(ppc_thread_state_t);
476 break;
477 case PPC_FLOAT_STATE:
478 if(count != PPC_FLOAT_STATE_COUNT){
479 error("in swap_object_headers(): malformed "
480 "load commands (count "
481 "not PPC_FLOAT_STATE_COUNT for "
482 "flavor number %lu which is a PPC_FLOAT_"
483 "STATE flavor in %s command %lu)",
484 nflavor, ut->cmd == LC_UNIXTHREAD ?
485 "LC_UNIXTHREAD" : "LC_THREAD", i);
486 return(FALSE);
488 fpu = (ppc_float_state_t *)state;
489 state += sizeof(ppc_float_state_t);
490 break;
491 case PPC_EXCEPTION_STATE:
492 if(count != PPC_EXCEPTION_STATE_COUNT){
493 error("in swap_object_headers(): malformed "
494 "load commands (count "
495 "not PPC_EXCEPTION_STATE_COUNT for "
496 "flavor number %lu which is a PPC_EXCEPT"
497 "ION_STATE flavor in %s command %lu)",
498 nflavor, ut->cmd == LC_UNIXTHREAD ?
499 "LC_UNIXTHREAD" : "LC_THREAD", i);
500 return(FALSE);
502 except = (ppc_exception_state_t *)state;
503 state += sizeof(ppc_exception_state_t);
504 break;
505 case PPC_THREAD_STATE64:
506 if(count != PPC_THREAD_STATE64_COUNT){
507 error("in swap_object_headers(): malformed "
508 "load commands (count "
509 "not PPC_THREAD_STATE64_COUNT for "
510 "flavor number %lu which is a PPC_THREAD_"
511 "STATE64 flavor in %s command %lu)",
512 nflavor, ut->cmd == LC_UNIXTHREAD ?
513 "LC_UNIXTHREAD" : "LC_THREAD", i);
514 return(FALSE);
516 cpu64 = (ppc_thread_state64_t *)state;
517 state += sizeof(ppc_thread_state64_t);
518 break;
519 default:
520 error("in swap_object_headers(): malformed "
521 "load commands (unknown "
522 "flavor %u for flavor number %lu in %s command"
523 " %lu can't byte swap it)", flavor, nflavor,
524 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
525 "LC_THREAD", i);
526 return(FALSE);
528 nflavor++;
530 break;
532 if(cputype == CPU_TYPE_MC88000){
533 m88k_thread_state_grf_t *cpu;
534 m88k_thread_state_xrf_t *fpu;
535 m88k_thread_state_user_t *user;
536 m88110_thread_state_impl_t *spu;
538 nflavor = 0;
539 p = (char *)ut + ut->cmdsize;
540 while(state < p){
541 flavor = *((uint32_t *)state);
542 state += sizeof(uint32_t);
543 count = *((uint32_t *)state);
544 state += sizeof(uint32_t);
545 switch(flavor){
546 case M88K_THREAD_STATE_GRF:
547 if(count != M88K_THREAD_STATE_GRF_COUNT){
548 error("in swap_object_headers(): malformed "
549 "load commands (count "
550 "not M88K_THREAD_STATE_GRF_COUNT for "
551 "flavor number %lu which is a M88K_THREAD_"
552 "STATE_GRF flavor in %s command %lu)",
553 nflavor, ut->cmd == LC_UNIXTHREAD ?
554 "LC_UNIXTHREAD" : "LC_THREAD", i);
555 return(FALSE);
557 cpu = (m88k_thread_state_grf_t *)state;
558 state += sizeof(m88k_thread_state_grf_t);
559 break;
560 case M88K_THREAD_STATE_XRF:
561 if(count != M88K_THREAD_STATE_XRF_COUNT){
562 error("in swap_object_headers(): malformed "
563 "load commands (count "
564 "not M88K_THREAD_STATE_XRF_COUNT for "
565 "flavor number %lu which is a M88K_THREAD_"
566 "STATE_XRF flavor in %s command %lu)",
567 nflavor, ut->cmd == LC_UNIXTHREAD ?
568 "LC_UNIXTHREAD" : "LC_THREAD", i);
569 return(FALSE);
571 fpu = (m88k_thread_state_xrf_t *)state;
572 state += sizeof(m88k_thread_state_xrf_t);
573 break;
574 case M88K_THREAD_STATE_USER:
575 if(count != M88K_THREAD_STATE_USER_COUNT){
576 error("in swap_object_headers(): malformed "
577 "load commands (count "
578 "not M88K_THREAD_STATE_USER_COUNT for "
579 "flavor number %lu which is a M88K_THREAD_"
580 "STATE_USER flavor in %s command %lu)",
581 nflavor, ut->cmd == LC_UNIXTHREAD ?
582 "LC_UNIXTHREAD" : "LC_THREAD", i);
583 return(FALSE);
585 user = (m88k_thread_state_user_t *)state;
586 state += sizeof(m88k_thread_state_user_t);
587 break;
588 case M88110_THREAD_STATE_IMPL:
589 if(count != M88110_THREAD_STATE_IMPL_COUNT){
590 error("in swap_object_headers(): malformed "
591 "load commands (count "
592 "not M88110_THREAD_STATE_IMPL_COUNT for "
593 "flavor number %lu which is a M88110_THREAD"
594 "_STATE_IMPL flavor in %s command %lu)",
595 nflavor, ut->cmd == LC_UNIXTHREAD ?
596 "LC_UNIXTHREAD" : "LC_THREAD", i);
597 return(FALSE);
599 spu = (m88110_thread_state_impl_t *)state;
600 state += sizeof(m88110_thread_state_impl_t);
601 break;
602 default:
603 error("in swap_object_headers(): malformed "
604 "load commands (unknown "
605 "flavor %u for flavor number %lu in %s command"
606 " %lu can't byte swap it)", flavor, nflavor,
607 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
608 "LC_THREAD", i);
609 return(FALSE);
611 nflavor++;
613 break;
615 if(cputype == CPU_TYPE_I860){
616 struct i860_thread_state_regs *cpu;
618 nflavor = 0;
619 p = (char *)ut + ut->cmdsize;
620 while(state < p){
621 flavor = *((uint32_t *)state);
622 state += sizeof(uint32_t);
623 count = *((uint32_t *)state);
624 state += sizeof(uint32_t);
625 switch(flavor){
626 case I860_THREAD_STATE_REGS:
627 if(count != I860_THREAD_STATE_REGS_COUNT){
628 error("in swap_object_headers(): malformed "
629 "load commands (count "
630 "not I860_THREAD_STATE_REGS_COUNT for "
631 "flavor number %lu which is a I860_THREAD_"
632 "STATE_REGS flavor in %s command %lu)",
633 nflavor, ut->cmd == LC_UNIXTHREAD ?
634 "LC_UNIXTHREAD" : "LC_THREAD", i);
635 return(FALSE);
637 cpu = (struct i860_thread_state_regs *)state;
638 state += sizeof(struct i860_thread_state_regs);
639 break;
640 default:
641 error("in swap_object_headers(): malformed "
642 "load commands (unknown "
643 "flavor %u for flavor number %lu in %s command"
644 " %lu can't byte swap it)", flavor, nflavor,
645 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
646 "LC_THREAD", i);
647 return(FALSE);
649 nflavor++;
651 break;
653 if(cputype == CPU_TYPE_I386
654 #ifdef x86_THREAD_STATE64
655 || cputype == CPU_TYPE_X86_64
656 #endif /* x86_THREAD_STATE64 */
658 i386_thread_state_t *cpu;
659 #ifdef x86_THREAD_STATE64
660 x86_thread_state64_t *cpu64;
661 #endif /* x86_THREAD_STATE64 */
662 /* current i386 thread states */
663 #if i386_THREAD_STATE == 1
664 struct i386_float_state *fpu;
665 i386_exception_state_t *exc;
666 #endif /* i386_THREAD_STATE == 1 */
668 /* i386 thread states on older releases */
669 #if i386_THREAD_STATE == -1
670 i386_thread_fpstate_t *fpu;
671 i386_thread_exceptstate_t *exc;
672 i386_thread_cthreadstate_t *user;
673 #endif /* i386_THREAD_STATE == -1 */
675 nflavor = 0;
676 p = (char *)ut + ut->cmdsize;
677 while(state < p){
678 flavor = *((uint32_t *)state);
679 state += sizeof(uint32_t);
680 count = *((uint32_t *)state);
681 state += sizeof(uint32_t);
682 switch((int)flavor){
683 case i386_THREAD_STATE:
684 /* current i386 thread states */
685 #if i386_THREAD_STATE == 1
686 case -1:
687 #endif /* i386_THREAD_STATE == 1 */
688 /* i386 thread states on older releases */
689 #if i386_THREAD_STATE == -1
690 case 1:
691 #endif /* i386_THREAD_STATE == -1 */
692 if(count != i386_THREAD_STATE_COUNT){
693 error("in swap_object_headers(): malformed "
694 "load commands (count "
695 "not i386_THREAD_STATE_COUNT for flavor "
696 "number %lu which is a i386_THREAD_STATE "
697 "flavor in %s command %lu)", nflavor,
698 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
699 "LC_THREAD", i);
700 return(FALSE);
702 cpu = (i386_thread_state_t *)state;
703 state += sizeof(i386_thread_state_t);
704 break;
705 /* current i386 thread states */
706 #if i386_THREAD_STATE == 1
707 case i386_FLOAT_STATE:
708 if(count != i386_FLOAT_STATE_COUNT){
709 error("in swap_object_headers(): malformed "
710 "load commands (count "
711 "not i386_FLOAT_STATE_COUNT for flavor "
712 "number %lu which is a i386_FLOAT_STATE "
713 "flavor in %s command %lu)", nflavor,
714 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
715 "LC_THREAD", i);
716 return(FALSE);
718 fpu = (struct i386_float_state *)state;
719 state += sizeof(struct i386_float_state);
720 break;
721 case i386_EXCEPTION_STATE:
722 if(count != I386_EXCEPTION_STATE_COUNT){
723 error("in swap_object_headers(): malformed "
724 "load commands (count "
725 "not I386_EXCEPTION_STATE_COUNT for "
726 "flavor number %lu which is a i386_"
727 "EXCEPTION_STATE flavor in %s command %lu)",
728 nflavor,
729 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
730 "LC_THREAD", i);
731 return(FALSE);
733 exc = (i386_exception_state_t *)state;
734 state += sizeof(i386_exception_state_t);
735 break;
736 #endif /* i386_THREAD_STATE == 1 */
738 /* i386 thread states on older releases */
739 #if i386_THREAD_STATE == -1
740 case i386_THREAD_FPSTATE:
741 if(count != i386_THREAD_FPSTATE_COUNT){
742 error("in swap_object_headers(): malformed "
743 "load commands (count "
744 "not i386_THREAD_FPSTATE_COUNT for flavor "
745 "number %lu which is a i386_THREAD_FPSTATE "
746 "flavor in %s command %lu)", nflavor,
747 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
748 "LC_THREAD", i);
749 return(FALSE);
751 fpu = (i386_thread_fpstate_t *)state;
752 state += sizeof(i386_thread_fpstate_t);
753 break;
754 case i386_THREAD_EXCEPTSTATE:
755 if(count != i386_THREAD_EXCEPTSTATE_COUNT){
756 error("in swap_object_headers(): malformed "
757 "load commands (count "
758 "not i386_THREAD_EXCEPTSTATE_COUNT for "
759 "flavor number %lu which is a i386_THREAD_"
760 "EXCEPTSTATE flavor in %s command %lu)",
761 nflavor,
762 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
763 "LC_THREAD", i);
764 return(FALSE);
766 exc = (i386_thread_exceptstate_t *)state;
767 state += sizeof(i386_thread_fpstate_t);
768 break;
769 case i386_THREAD_CTHREADSTATE:
770 if(count != i386_THREAD_CTHREADSTATE_COUNT){
771 error("in swap_object_headers(): malformed "
772 "load commands (count "
773 "not i386_THREAD_CTHREADSTATE_COUNT for "
774 "flavor number %lu which is a i386_THREAD_"
775 "CTHREADSTATE flavor in %s command %lu)",
776 nflavor,
777 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
778 "LC_THREAD", i);
779 return(FALSE);
781 user = (i386_thread_cthreadstate_t *)state;
782 state += sizeof(i386_thread_fpstate_t);
783 break;
784 #endif /* i386_THREAD_STATE == -1 */
785 #ifdef x86_THREAD_STATE64
786 case x86_THREAD_STATE64:
787 if(count != x86_THREAD_STATE64_COUNT){
788 error("in swap_object_headers(): malformed "
789 "load commands (count "
790 "not x86_THREAD_STATE64_COUNT for "
791 "flavor number %lu which is an x86_THREAD_"
792 "STATE64 flavor in %s command %lu)",
793 nflavor,
794 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
795 "LC_THREAD", i);
796 return(FALSE);
798 cpu64 = (x86_thread_state64_t *)state;
799 state += sizeof(x86_thread_state64_t);
800 break;
801 #endif /* x86_THREAD_STATE64 */
802 default:
803 error("in swap_object_headers(): malformed "
804 "load commands (unknown "
805 "flavor %u for flavor number %lu in %s command"
806 " %lu can't byte swap it)", flavor, nflavor,
807 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
808 "LC_THREAD", i);
809 return(FALSE);
811 nflavor++;
813 break;
815 if(cputype == CPU_TYPE_HPPA){
816 struct hp_pa_integer_thread_state *cpu;
817 struct hp_pa_frame_thread_state *frame;
818 struct hp_pa_fp_thread_state *fpu;
820 nflavor = 0;
821 p = (char *)ut + ut->cmdsize;
822 while(state < p){
823 flavor = *((uint32_t *)state);
824 state += sizeof(uint32_t);
825 count = *((uint32_t *)state);
826 state += sizeof(uint32_t);
827 switch(flavor){
828 case HPPA_INTEGER_THREAD_STATE:
829 if(count != HPPA_INTEGER_THREAD_STATE_COUNT){
830 error("in swap_object_headers(): malformed "
831 "load commands (count "
832 "not HPPA_INTEGER_THREAD_STATE_COUNT for "
833 "flavor number %lu which is a HPPA_INTEGER"
834 "_THREAD_STATE flavor in %s command %lu)",
835 nflavor, ut->cmd == LC_UNIXTHREAD ?
836 "LC_UNIXTHREAD" : "LC_THREAD", i);
837 return(FALSE);
839 cpu = (struct hp_pa_integer_thread_state *)state;
840 state += sizeof(struct hp_pa_integer_thread_state);
841 break;
842 case HPPA_FRAME_THREAD_STATE:
843 if(count != HPPA_FRAME_THREAD_STATE_COUNT){
844 error("in swap_object_headers(): malformed "
845 "load commands (count "
846 "not HPPA_FRAME_THREAD_STATE_COUNT for "
847 "flavor number %lu which is a HPPA_FRAME"
848 "_THREAD_STATE flavor in %s command %lu)",
849 nflavor, ut->cmd == LC_UNIXTHREAD ?
850 "LC_UNIXTHREAD" : "LC_THREAD", i);
851 return(FALSE);
853 frame = (struct hp_pa_frame_thread_state *)state;
854 state += sizeof(struct hp_pa_frame_thread_state);
855 break;
856 case HPPA_FP_THREAD_STATE:
857 if(count != HPPA_FP_THREAD_STATE_COUNT){
858 error("in swap_object_headers(): malformed "
859 "load commands (count "
860 "not HPPA_FP_THREAD_STATE_COUNT for "
861 "flavor number %lu which is a HPPA_FP"
862 "_THREAD_STATE flavor in %s command %lu)",
863 nflavor, ut->cmd == LC_UNIXTHREAD ?
864 "LC_UNIXTHREAD" : "LC_THREAD", i);
865 return(FALSE);
867 fpu = (struct hp_pa_fp_thread_state *)state;
868 state += sizeof(struct hp_pa_fp_thread_state);
869 break;
870 default:
871 error("in swap_object_headers(): malformed "
872 "load commands (unknown "
873 "flavor %u for flavor number %lu in %s command"
874 " %lu can't byte swap it)", flavor, nflavor,
875 ut->cmd == LC_UNIXTHREAD ? "LC_UNIXTHREAD" :
876 "LC_THREAD", i);
877 return(FALSE);
879 nflavor++;
881 break;
883 if(cputype == CPU_TYPE_SPARC) {
884 struct sparc_thread_state_regs *cpu;
885 struct sparc_thread_state_fpu *fpu;
887 nflavor = 0;
888 p = (char *)ut + ut->cmdsize;
889 while (state < p) {
890 flavor = *((uint32_t *) state);
891 state += sizeof(uint32_t);
892 count = *((uint32_t *) state);
893 state += sizeof(uint32_t);
894 switch (flavor) {
895 case SPARC_THREAD_STATE_REGS:
896 if (count != SPARC_THREAD_STATE_REGS_COUNT) {
897 error("in swap_object_headers(): malformed "
898 "load commands (count "
899 "not SPARC_THREAD_STATE_REGS_COUNT for "
900 "flavor number %lu which is a SPARC_THREAD_"
901 "STATE_REGS flavor in %s command %lu)",
902 nflavor, ut->cmd == LC_UNIXTHREAD ?
903 "LC_UNIXTHREAD" : "LC_THREAD", i);
904 return(FALSE);
906 cpu = (struct sparc_thread_state_regs *) state;
907 state += sizeof(struct sparc_thread_state_regs);
908 break;
909 case SPARC_THREAD_STATE_FPU:
910 if (count != SPARC_THREAD_STATE_FPU_COUNT) {
911 error("in swap_object_headers(): malformed "
912 "load commands (count "
913 "not SPARC_THREAD_STATE_FPU_COUNT for "
914 "flavor number %lu which is a SPARC_THREAD_"
915 "STATE_FPU flavor in %s command %lu)",
916 nflavor, ut->cmd == LC_UNIXTHREAD ?
917 "LC_UNIXTHREAD" : "LC_THREAD", i);
918 return(FALSE);
920 fpu = (struct sparc_thread_state_fpu *) state;
921 state += sizeof(struct sparc_thread_state_fpu);
922 break;
925 break;
927 if(cputype == CPU_TYPE_ARM){
928 arm_thread_state_t *cpu;
930 nflavor = 0;
931 p = (char *)ut + ut->cmdsize;
932 while(state < p){
933 flavor = *((uint32_t *)state);
934 state += sizeof(uint32_t);
935 count = *((uint32_t *)state);
936 state += sizeof(uint32_t);
937 switch(flavor){
938 case ARM_THREAD_STATE:
939 if(count != ARM_THREAD_STATE_COUNT){
940 error("in swap_object_headers(): malformed "
941 "load commands (count "
942 "not ARM_THREAD_STATE_COUNT for "
943 "flavor number %lu which is a ARM_THREAD_"
944 "STATE flavor in %s command %lu)",
945 nflavor, ut->cmd == LC_UNIXTHREAD ?
946 "LC_UNIXTHREAD" : "LC_THREAD", i);
947 return(FALSE);
949 cpu = (arm_thread_state_t *)state;
950 state += sizeof(arm_thread_state_t);
951 break;
952 default:
953 error("in swap_object_headers(): malformed load "
954 "commands (unknown flavor for flavor number "
955 "%lu in %s command %lu can't byte swap it)",
956 nflavor, ut->cmd == LC_UNIXTHREAD ?
957 "LC_UNIXTHREAD" : "LC_THREAD", i);
958 return(FALSE);
960 nflavor++;
962 break;
964 error("in swap_object_headers(): malformed load commands "
965 "(unknown cputype (%d) and cpusubtype (%d) of object and "
966 "can't byte swap %s command %lu)", cputype,
967 cpusubtype, ut->cmd == LC_UNIXTHREAD ?
968 "LC_UNIXTHREAD" : "LC_THREAD", i);
969 return(FALSE);
971 case LC_MAIN:
972 ep = (struct entry_point_command *)lc;
973 if((char *)ep + ep->cmdsize >
974 (char *)load_commands + sizeofcmds){
975 error("in swap_object_headers(): truncated or malformed "
976 "load commands (cmdsize field of LC_MAIN command %lu "
977 "extends past the end of the load commands)", i);
978 return(FALSE);
980 break;
982 case LC_SOURCE_VERSION:
983 sv = (struct source_version_command *)lc;
984 if((char *)sv + sv->cmdsize >
985 (char *)load_commands + sizeofcmds){
986 error("in swap_object_headers(): truncated or malformed "
987 "load commands (cmdsize field of LC_SOURCE_VERSION "
988 "command %lu extends past the end of the load "
989 "commands)", i);
990 return(FALSE);
992 break;
994 case LC_IDENT:
995 id = (struct ident_command *)lc;
996 if((char *)id + id->cmdsize >
997 (char *)load_commands + sizeofcmds){
998 error("in swap_object_headers(): truncated or malformed "
999 "load commands (cmdsize field of LC_IDENT command %lu "
1000 "extends past the end of the load commands)", i);
1001 return(FALSE);
1003 break;
1005 case LC_ROUTINES:
1006 rc = (struct routines_command *)lc;
1007 if(rc->cmdsize != sizeof(struct routines_command)){
1008 error("in swap_object_headers(): malformed load commands ("
1009 "LC_ROUTINES command %lu has incorrect cmdsize",
1011 return(FALSE);
1013 break;
1015 case LC_ROUTINES_64:
1016 rc64 = (struct routines_command_64 *)lc;
1017 if(rc64->cmdsize != sizeof(struct routines_command_64)){
1018 error("in swap_object_headers(): malformed load commands ("
1019 "LC_ROUTINES_64 command %lu has incorrect cmdsize",
1021 return(FALSE);
1023 break;
1025 case LC_TWOLEVEL_HINTS:
1026 hints = (struct twolevel_hints_command *)lc;
1027 if(hints->cmdsize != sizeof(struct twolevel_hints_command)){
1028 error("in swap_object_headers(): malformed load commands "
1029 "(LC_TWOLEVEL_HINTS command %lu has incorrect "
1030 "cmdsize", i);
1031 return(FALSE);
1033 break;
1035 case LC_PREBIND_CKSUM:
1036 cs = (struct prebind_cksum_command *)lc;
1037 if(cs->cmdsize != sizeof(struct prebind_cksum_command)){
1038 error("in swap_object_headers(): malformed load commands "
1039 "(LC_PREBIND_CKSUM command %lu has incorrect cmdsize",
1041 return(FALSE);
1043 break;
1045 case LC_UUID:
1046 uuid = (struct uuid_command *)lc;
1047 if(uuid->cmdsize != sizeof(struct uuid_command)){
1048 error("in swap_object_headers(): malformed load commands "
1049 "(LC_UUID command %lu has incorrect cmdsize", i);
1050 return(FALSE);
1052 break;
1054 case LC_CODE_SIGNATURE:
1055 ld = (struct linkedit_data_command *)lc;
1056 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1057 error("in swap_object_headers(): malformed load commands "
1058 "(LC_CODE_SIGNATURE command %lu has incorrect "
1059 "cmdsize", i);
1060 return(FALSE);
1062 break;
1064 case LC_SEGMENT_SPLIT_INFO:
1065 ld = (struct linkedit_data_command *)lc;
1066 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1067 error("in swap_object_headers(): malformed load commands "
1068 "(LC_SEGMENT_SPLIT_INFO command %lu has incorrect "
1069 "cmdsize", i);
1070 return(FALSE);
1072 break;
1074 case LC_FUNCTION_STARTS:
1075 ld = (struct linkedit_data_command *)lc;
1076 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1077 error("in swap_object_headers(): malformed load commands "
1078 "(LC_FUNCTION_STARTS command %lu has incorrect "
1079 "cmdsize", i);
1080 return(FALSE);
1082 break;
1084 case LC_DATA_IN_CODE:
1085 ld = (struct linkedit_data_command *)lc;
1086 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1087 error("in swap_object_headers(): malformed load commands "
1088 "(LC_DATA_IN_CODE command %lu has incorrect "
1089 "cmdsize", i);
1090 return(FALSE);
1092 break;
1094 case LC_DYLIB_CODE_SIGN_DRS:
1095 ld = (struct linkedit_data_command *)lc;
1096 if(ld->cmdsize != sizeof(struct linkedit_data_command)){
1097 error("in swap_object_headers(): malformed load commands "
1098 "(LC_DYLIB_CODE_SIGN_DRS command %lu has incorrect "
1099 "cmdsize", i);
1100 return(FALSE);
1102 break;
1104 case LC_VERSION_MIN_MACOSX:
1105 vc = (struct version_min_command *)lc;
1106 if(vc->cmdsize != sizeof(struct version_min_command)){
1107 error("in swap_object_headers(): malformed load commands "
1108 "(LC_VERSION_MIN_MACOSX command %lu has incorrect "
1109 "cmdsize", i);
1110 return(FALSE);
1112 break;
1114 case LC_VERSION_MIN_IPHONEOS:
1115 vc = (struct version_min_command *)lc;
1116 if(vc->cmdsize != sizeof(struct version_min_command)){
1117 error("in swap_object_headers(): malformed load commands "
1118 "(LC_VERSION_MIN_IPHONEOS command %lu has incorrect "
1119 "cmdsize", i);
1120 return(FALSE);
1122 break;
1124 case LC_RPATH:
1125 rpath = (struct rpath_command *)lc;
1126 if(rpath->cmdsize < sizeof(struct rpath_command)){
1127 error("in swap_object_headers(): malformed load commands "
1128 "(LC_RPATH command %lu has too small cmdsize field)",
1130 return(FALSE);
1132 if(rpath->path.offset >= rpath->cmdsize){
1133 error("in swap_object_headers(): truncated or malformed "
1134 "load commands (path.offset field of LC_RPATH "
1135 "command %lu extends past the end of all load "
1136 "commands)", i);
1137 return(FALSE);
1139 break;
1141 case LC_ENCRYPTION_INFO:
1142 ec = (struct encryption_info_command *)lc;
1143 if(ec->cmdsize != sizeof(struct encryption_info_command)){
1144 error("in swap_object_headers(): malformed load commands "
1145 "(LC_ENCRYPTION_INFO command %lu has incorrect "
1146 "cmdsize", i);
1147 return(FALSE);
1149 break;
1151 case LC_DYLD_INFO:
1152 case LC_DYLD_INFO_ONLY:
1153 dc = (struct dyld_info_command *)lc;
1154 if(dc->cmdsize != sizeof(struct dyld_info_command)){
1155 error("in swap_object_headers(): malformed load commands "
1156 "(LC_DYLD_INFO command %lu has incorrect "
1157 "cmdsize", i);
1158 return(FALSE);
1160 break;
1162 default:
1163 error("in swap_object_headers(): malformed load commands "
1164 "(unknown load command %lu)", i);
1165 return(FALSE);
1168 lc = (struct load_command *)((char *)lc + l.cmdsize);
1169 /* check that next load command does not extends past the end */
1170 if((char *)lc > (char *)load_commands + sizeofcmds){
1171 error("in swap_object_headers(): truncated or malformed load "
1172 "commands (load command %lu extends past the end of all "
1173 "load commands)", i + 1);
1174 return(FALSE);
1177 /* check for an inconsistent size of the load commands */
1178 if((char *)load_commands + sizeofcmds != (char *)lc){
1179 error("in swap_object_headers(): malformed load commands "
1180 "(inconsistent sizeofcmds field in mach header)");
1181 return(FALSE);
1186 * Now knowing the load commands can be parsed swap them.
1188 target_byte_sex = get_host_byte_sex() == BIG_ENDIAN_BYTE_SEX ?
1189 LITTLE_ENDIAN_BYTE_SEX : BIG_ENDIAN_BYTE_SEX;
1190 for(i = 0, lc = load_commands; i < ncmds; i++){
1191 l = *lc;
1192 switch(lc->cmd){
1193 case LC_SEGMENT:
1194 sg = (struct segment_command *)lc;
1195 s = (struct section *)
1196 ((char *)sg + sizeof(struct segment_command));
1197 swap_section(s, sg->nsects, target_byte_sex);
1198 swap_segment_command(sg, target_byte_sex);
1199 break;
1201 case LC_SEGMENT_64:
1202 sg64 = (struct segment_command_64 *)lc;
1203 s64 = (struct section_64 *)
1204 ((char *)sg64 + sizeof(struct segment_command_64));
1205 swap_section_64(s64, sg64->nsects, target_byte_sex);
1206 swap_segment_command_64(sg64, target_byte_sex);
1207 break;
1209 case LC_SYMTAB:
1210 st = (struct symtab_command *)lc;
1211 swap_symtab_command(st, target_byte_sex);
1212 break;
1214 case LC_DYSYMTAB:
1215 dyst = (struct dysymtab_command *)lc;
1216 swap_dysymtab_command(dyst, target_byte_sex);
1217 break;
1219 case LC_SYMSEG:
1220 ss = (struct symseg_command *)lc;
1221 swap_symseg_command(ss, target_byte_sex);
1222 break;
1224 case LC_IDFVMLIB:
1225 case LC_LOADFVMLIB:
1226 fl = (struct fvmlib_command *)lc;
1227 swap_fvmlib_command(fl, target_byte_sex);
1228 break;
1230 case LC_ID_DYLIB:
1231 case LC_LOAD_DYLIB:
1232 case LC_LOAD_WEAK_DYLIB:
1233 case LC_REEXPORT_DYLIB:
1234 case LC_LOAD_UPWARD_DYLIB:
1235 case LC_LAZY_LOAD_DYLIB:
1236 dl = (struct dylib_command *)lc;
1237 swap_dylib_command(dl, target_byte_sex);
1238 break;
1240 case LC_SUB_FRAMEWORK:
1241 sub = (struct sub_framework_command *)lc;
1242 swap_sub_framework_command(sub, target_byte_sex);
1243 break;
1245 case LC_SUB_UMBRELLA:
1246 usub = (struct sub_umbrella_command *)lc;
1247 swap_sub_umbrella_command(usub, target_byte_sex);
1248 break;
1250 case LC_SUB_LIBRARY:
1251 lsub = (struct sub_library_command *)lc;
1252 swap_sub_library_command(lsub, target_byte_sex);
1253 break;
1255 case LC_SUB_CLIENT:
1256 csub = (struct sub_client_command *)lc;
1257 swap_sub_client_command(csub, target_byte_sex);
1258 break;
1260 case LC_PREBOUND_DYLIB:
1261 pbdylib = (struct prebound_dylib_command *)lc;
1262 swap_prebound_dylib_command(pbdylib, target_byte_sex);
1263 break;
1265 case LC_ID_DYLINKER:
1266 case LC_LOAD_DYLINKER:
1267 case LC_DYLD_ENVIRONMENT:
1268 dyld = (struct dylinker_command *)lc;
1269 swap_dylinker_command(dyld, target_byte_sex);
1270 break;
1272 case LC_UNIXTHREAD:
1273 case LC_THREAD:
1274 ut = (struct thread_command *)lc;
1275 state = (char *)ut + sizeof(struct thread_command);
1276 p = (char *)ut + ut->cmdsize;
1277 swap_thread_command(ut, target_byte_sex);
1279 if(cputype == CPU_TYPE_MC680x0){
1280 struct m68k_thread_state_regs *cpu;
1281 struct m68k_thread_state_68882 *fpu;
1282 struct m68k_thread_state_user_reg *user_reg;
1284 while(state < p){
1285 flavor = *((uint32_t *)state);
1286 *((uint32_t *)state) = SWAP_INT(flavor);
1287 state += sizeof(uint32_t);
1288 count = *((uint32_t *)state);
1289 *((uint32_t *)state) = SWAP_INT(count);
1290 state += sizeof(uint32_t);
1291 switch(flavor){
1292 case M68K_THREAD_STATE_REGS:
1293 cpu = (struct m68k_thread_state_regs *)state;
1294 swap_m68k_thread_state_regs(cpu, target_byte_sex);
1295 state += sizeof(struct m68k_thread_state_regs);
1296 break;
1297 case M68K_THREAD_STATE_68882:
1298 fpu = (struct m68k_thread_state_68882 *)state;
1299 swap_m68k_thread_state_68882(fpu, target_byte_sex);
1300 state += sizeof(struct m68k_thread_state_68882);
1301 break;
1302 case M68K_THREAD_STATE_USER_REG:
1303 user_reg =
1304 (struct m68k_thread_state_user_reg *)state;
1305 swap_m68k_thread_state_user_reg(user_reg,
1306 target_byte_sex);
1307 state += sizeof(struct m68k_thread_state_user_reg);
1308 break;
1311 break;
1313 if(cputype == CPU_TYPE_POWERPC ||
1314 cputype == CPU_TYPE_VEO ||
1315 cputype == CPU_TYPE_POWERPC64){
1316 ppc_thread_state_t *cpu;
1317 ppc_thread_state64_t *cpu64;
1318 ppc_float_state_t *fpu;
1319 ppc_exception_state_t *except;
1321 while(state < p){
1322 flavor = *((uint32_t *)state);
1323 *((uint32_t *)state) = SWAP_INT(flavor);
1324 state += sizeof(uint32_t);
1325 count = *((uint32_t *)state);
1326 *((uint32_t *)state) = SWAP_INT(count);
1327 state += sizeof(uint32_t);
1328 switch(flavor){
1329 case PPC_THREAD_STATE:
1330 cpu = (ppc_thread_state_t *)state;
1331 swap_ppc_thread_state_t(cpu, target_byte_sex);
1332 state += sizeof(ppc_thread_state_t);
1333 break;
1334 case PPC_THREAD_STATE64:
1335 cpu64 = (ppc_thread_state64_t *)state;
1336 swap_ppc_thread_state64_t(cpu64, target_byte_sex);
1337 state += sizeof(ppc_thread_state64_t);
1338 break;
1339 case PPC_FLOAT_STATE:
1340 fpu = (ppc_float_state_t *)state;
1341 swap_ppc_float_state_t(fpu, target_byte_sex);
1342 state += sizeof(ppc_float_state_t);
1343 case PPC_EXCEPTION_STATE:
1344 except = (ppc_exception_state_t *)state;
1345 swap_ppc_exception_state_t(except, target_byte_sex);
1346 state += sizeof(ppc_exception_state_t);
1347 break;
1350 break;
1352 if(cputype == CPU_TYPE_MC88000){
1353 m88k_thread_state_grf_t *cpu;
1354 m88k_thread_state_xrf_t *fpu;
1355 m88k_thread_state_user_t *user;
1356 m88110_thread_state_impl_t *spu;
1358 while(state < p){
1359 flavor = *((uint32_t *)state);
1360 *((uint32_t *)state) = SWAP_INT(flavor);
1361 state += sizeof(uint32_t);
1362 count = *((uint32_t *)state);
1363 *((uint32_t *)state) = SWAP_INT(count);
1364 state += sizeof(uint32_t);
1365 switch(flavor){
1366 case M88K_THREAD_STATE_GRF:
1367 cpu = (m88k_thread_state_grf_t *)state;
1368 swap_m88k_thread_state_grf_t(cpu,
1369 target_byte_sex);
1370 state += sizeof(m88k_thread_state_grf_t);
1371 break;
1372 case M88K_THREAD_STATE_XRF:
1373 fpu = (m88k_thread_state_xrf_t *)state;
1374 swap_m88k_thread_state_xrf_t(fpu,
1375 target_byte_sex);
1376 state += sizeof(m88k_thread_state_xrf_t);
1377 break;
1378 case M88K_THREAD_STATE_USER:
1379 user = (m88k_thread_state_user_t *)state;
1380 swap_m88k_thread_state_user_t(user,
1381 target_byte_sex);
1382 state += sizeof(m88k_thread_state_user_t);
1383 break;
1384 case M88110_THREAD_STATE_IMPL:
1385 spu = (m88110_thread_state_impl_t *)state;
1386 swap_m88110_thread_state_impl_t(spu,
1387 target_byte_sex);
1388 state += sizeof(m88110_thread_state_impl_t);
1389 break;
1392 break;
1394 if(cputype == CPU_TYPE_I860){
1395 struct i860_thread_state_regs *cpu;
1397 while(state < p){
1398 flavor = *((uint32_t *)state);
1399 *((uint32_t *)state) = SWAP_INT(flavor);
1400 state += sizeof(uint32_t);
1401 count = *((uint32_t *)state);
1402 *((uint32_t *)state) = SWAP_INT(count);
1403 state += sizeof(uint32_t);
1404 switch(flavor){
1405 case I860_THREAD_STATE_REGS:
1406 cpu = (struct i860_thread_state_regs *)state;
1407 swap_i860_thread_state_regs(cpu, target_byte_sex);
1408 state += sizeof(struct i860_thread_state_regs);
1409 break;
1412 break;
1414 if(cputype == CPU_TYPE_I386
1415 #ifdef x86_THREAD_STATE64
1416 || cputype == CPU_TYPE_X86_64
1417 #endif /* x86_THREAD_STATE64 */
1419 i386_thread_state_t *cpu;
1420 #ifdef x86_THREAD_STATE64
1421 x86_thread_state64_t *cpu64;
1422 #endif /* x86_THREAD_STATE64 */
1423 /* current i386 thread states */
1424 #if i386_THREAD_STATE == 1
1425 struct i386_float_state *fpu;
1426 i386_exception_state_t *exc;
1427 #endif /* i386_THREAD_STATE == 1 */
1429 /* i386 thread states on older releases */
1430 #if i386_THREAD_STATE == -1
1431 i386_thread_fpstate_t *fpu;
1432 i386_thread_exceptstate_t *exc;
1433 i386_thread_cthreadstate_t *user;
1434 #endif /* i386_THREAD_STATE == -1 */
1436 while(state < p){
1437 flavor = *((uint32_t *)state);
1438 *((uint32_t *)state) = SWAP_INT(flavor);
1439 state += sizeof(uint32_t);
1440 count = *((uint32_t *)state);
1441 *((uint32_t *)state) = SWAP_INT(count);
1442 state += sizeof(uint32_t);
1443 switch((int)flavor){
1444 case i386_THREAD_STATE:
1445 /* current i386 thread states */
1446 #if i386_THREAD_STATE == 1
1447 case -1:
1448 #endif /* i386_THREAD_STATE == 1 */
1449 /* i386 thread states on older releases */
1450 #if i386_THREAD_STATE == -1
1451 case 1:
1452 #endif /* i386_THREAD_STATE == -1 */
1453 cpu = (i386_thread_state_t *)state;
1454 swap_i386_thread_state(cpu, target_byte_sex);
1455 state += sizeof(i386_thread_state_t);
1456 break;
1457 /* current i386 thread states */
1458 #if i386_THREAD_STATE == 1
1459 case i386_FLOAT_STATE:
1460 fpu = (struct i386_float_state *)state;
1461 swap_i386_float_state(fpu, target_byte_sex);
1462 state += sizeof(struct i386_float_state);
1463 break;
1464 case i386_EXCEPTION_STATE:
1465 exc = (i386_exception_state_t *)state;
1466 swap_i386_exception_state(exc, target_byte_sex);
1467 state += sizeof(i386_exception_state_t);
1468 break;
1469 #endif /* i386_THREAD_STATE == 1 */
1471 /* i386 thread states on older releases */
1472 #if i386_THREAD_STATE == -1
1473 case i386_THREAD_FPSTATE:
1474 fpu = (i386_thread_fpstate_t *)state;
1475 swap_i386_thread_fpstate(fpu, target_byte_sex);
1476 state += sizeof(i386_thread_fpstate_t);
1477 break;
1478 case i386_THREAD_EXCEPTSTATE:
1479 exc = (i386_thread_exceptstate_t *)state;
1480 swap_i386_thread_exceptstate(exc, target_byte_sex);
1481 state += sizeof(i386_thread_exceptstate_t);
1482 break;
1483 case i386_THREAD_CTHREADSTATE:
1484 user = (i386_thread_cthreadstate_t *)state;
1485 swap_i386_thread_cthreadstate(user,target_byte_sex);
1486 state += sizeof(i386_thread_cthreadstate_t);
1487 break;
1488 #endif /* i386_THREAD_STATE == -1 */
1489 #ifdef x86_THREAD_STATE64
1490 case x86_THREAD_STATE64:
1491 cpu64 = (x86_thread_state64_t *)state;
1492 swap_x86_thread_state64(cpu64, target_byte_sex);
1493 state += sizeof(x86_thread_state64_t);
1494 break;
1495 #endif /* x86_THREAD_STATE64 */
1498 break;
1500 if(cputype == CPU_TYPE_HPPA){
1501 struct hp_pa_integer_thread_state *cpu;
1502 struct hp_pa_frame_thread_state *frame;
1503 struct hp_pa_fp_thread_state *fpu;
1505 while(state < p){
1506 flavor = *((uint32_t *)state);
1507 *((uint32_t *)state) = SWAP_INT(flavor);
1508 state += sizeof(uint32_t);
1509 count = *((uint32_t *)state);
1510 *((uint32_t *)state) = SWAP_INT(count);
1511 state += sizeof(uint32_t);
1512 switch(flavor){
1513 case HPPA_INTEGER_THREAD_STATE:
1514 cpu = (struct hp_pa_integer_thread_state *)state;
1515 swap_hppa_integer_thread_state(cpu,
1516 target_byte_sex);
1517 state += sizeof(struct hp_pa_integer_thread_state);
1518 break;
1519 case HPPA_FRAME_THREAD_STATE:
1520 frame = (struct hp_pa_frame_thread_state *)state;
1521 swap_hppa_frame_thread_state(frame,
1522 target_byte_sex);
1523 state += sizeof(struct hp_pa_frame_thread_state);
1524 break;
1525 case HPPA_FP_THREAD_STATE:
1526 fpu = (struct hp_pa_fp_thread_state *)state;
1527 swap_hppa_fp_thread_state(fpu,
1528 target_byte_sex);
1529 state += sizeof(struct hp_pa_fp_thread_state);
1530 break;
1533 break;
1536 if(cputype == CPU_TYPE_SPARC) {
1537 struct sparc_thread_state_regs *cpu;
1538 struct sparc_thread_state_fpu *fpu;
1540 while (state < p) {
1541 flavor = *((uint32_t *) state);
1542 *((uint32_t *) state) = SWAP_INT(flavor);
1543 state += sizeof(uint32_t);
1544 count = *((unsigned int *) state);
1545 *((unsigned int *) state) = SWAP_INT(count);
1546 state += sizeof(uint32_t);
1547 switch (flavor) {
1548 case SPARC_THREAD_STATE_REGS:
1549 cpu = (struct sparc_thread_state_regs *) state;
1550 swap_sparc_thread_state_regs(cpu, target_byte_sex);
1551 state += sizeof(struct sparc_thread_state_regs);
1552 break;
1553 case SPARC_THREAD_STATE_FPU:
1554 fpu = (struct sparc_thread_state_fpu *) state;
1555 swap_sparc_thread_state_fpu(fpu, target_byte_sex);
1556 state += sizeof(struct sparc_thread_state_fpu);
1557 break;
1560 break;
1562 if(cputype == CPU_TYPE_ARM){
1563 arm_thread_state_t *cpu;
1565 while(state < p){
1566 flavor = *((uint32_t *)state);
1567 *((uint32_t *)state) = SWAP_INT(flavor);
1568 state += sizeof(uint32_t);
1569 count = *((uint32_t *)state);
1570 *((uint32_t *)state) = SWAP_INT(count);
1571 state += sizeof(uint32_t);
1572 switch(flavor){
1573 case ARM_THREAD_STATE:
1574 cpu = (arm_thread_state_t *)state;
1575 swap_arm_thread_state_t(cpu, target_byte_sex);
1576 state += sizeof(arm_thread_state_t);
1577 break;
1580 break;
1582 break;
1584 case LC_MAIN:
1585 ep = (struct entry_point_command *)lc;
1586 swap_entry_point_command(ep, target_byte_sex);
1587 break;
1589 case LC_SOURCE_VERSION:
1590 sv = (struct source_version_command *)lc;
1591 swap_source_version_command(sv, target_byte_sex);
1592 break;
1594 case LC_IDENT:
1595 id = (struct ident_command *)lc;
1596 swap_ident_command(id, target_byte_sex);
1597 break;
1599 case LC_ROUTINES:
1600 rc = (struct routines_command *)lc;
1601 swap_routines_command(rc, target_byte_sex);
1602 break;
1604 case LC_ROUTINES_64:
1605 rc64 = (struct routines_command_64 *)lc;
1606 swap_routines_command_64(rc64, target_byte_sex);
1607 break;
1609 case LC_TWOLEVEL_HINTS:
1610 hints = (struct twolevel_hints_command *)lc;
1611 swap_twolevel_hints_command(hints, target_byte_sex);
1612 break;
1614 case LC_PREBIND_CKSUM:
1615 cs = (struct prebind_cksum_command *)lc;
1616 swap_prebind_cksum_command(cs, target_byte_sex);
1617 break;
1619 case LC_UUID:
1620 uuid = (struct uuid_command *)lc;
1621 swap_uuid_command(uuid, target_byte_sex);
1622 break;
1624 case LC_CODE_SIGNATURE:
1625 case LC_SEGMENT_SPLIT_INFO:
1626 case LC_FUNCTION_STARTS:
1627 case LC_DATA_IN_CODE:
1628 case LC_DYLIB_CODE_SIGN_DRS:
1629 ld = (struct linkedit_data_command *)lc;
1630 swap_linkedit_data_command(ld, target_byte_sex);
1631 break;
1633 case LC_RPATH:
1634 rpath = (struct rpath_command *)lc;
1635 swap_rpath_command(rpath, target_byte_sex);
1636 break;
1638 case LC_ENCRYPTION_INFO:
1639 ec = (struct encryption_info_command *)lc;
1640 swap_encryption_command(ec, target_byte_sex);
1641 break;
1643 case LC_DYLD_INFO:
1644 case LC_DYLD_INFO_ONLY:
1645 dc = (struct dyld_info_command *)lc;
1646 swap_dyld_info_command(dc, target_byte_sex);
1647 break;
1649 case LC_VERSION_MIN_MACOSX:
1650 case LC_VERSION_MIN_IPHONEOS:
1651 vc = (struct version_min_command *)lc;
1652 swap_version_min_command(vc, target_byte_sex);
1653 break;
1656 lc = (struct load_command *)((char *)lc + l.cmdsize);
1658 if(mh != NULL)
1659 swap_mach_header(mh, target_byte_sex);
1660 else
1661 swap_mach_header_64(mh64, target_byte_sex);
1663 return(TRUE);