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