1/*
2 * Copyright (c) 2004, Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1.  Redistributions of source code must retain the above copyright
8 *     notice, this list of conditions and the following disclaimer.
9 * 2.  Redistributions in binary form must reproduce the above copyright
10 *     notice, this list of conditions and the following disclaimer in the
11 *     documentation and/or other materials provided with the distribution.
12 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
13 *     its contributors may be used to endorse or promote products derived
14 *     from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28/* byte_sex.c */
29
30#define __cr cr
31#define __ctr ctr
32#define __dar dar
33#define __dsisr dsisr
34#define __exception exception
35#define __fpregs fpregs
36#define __fpscr fpscr
37#define __fpscr_pad fpscr_pad
38#define __lr lr
39#define __mq mq
40#define __pad0 pad0
41#define __pad1 pad1
42#define __r0 r0
43#define __r1 r1
44#define __r10 r10
45#define __r11 r11
46#define __r12 r12
47#define __r13 r13
48#define __r14 r14
49#define __r15 r15
50#define __r16 r16
51#define __r17 r17
52#define __r18 r18
53#define __r19 r19
54#define __r2 r2
55#define __r20 r20
56#define __r21 r21
57#define __r22 r22
58#define __r23 r23
59#define __r24 r24
60#define __r25 r25
61#define __r26 r26
62#define __r27 r27
63#define __r28 r28
64#define __r29 r29
65#define __r3 r3
66#define __r30 r30
67#define __r31 r31
68#define __r4 r4
69#define __r5 r5
70#define __r6 r6
71#define __r7 r7
72#define __r8 r8
73#define __r9 r9
74#define __srr0 srr0
75#define __srr1 srr1
76#define __vrsave vrsave
77#define __xer xer
78
79#define __darwin_i386_exception_state i386_exception_state
80#define __darwin_i386_float_state i386_float_state
81#define __darwin_i386_thread_state i386_thread_state
82#define __busy busy
83#define __c0 c0
84#define __c1 c1
85#define __c2 c2
86#define __c3 c3
87#define __cs cs
88#define __darwin_fp_control fp_control
89#define __darwin_fp_status fp_status
90#define __darwin_mmst_reg mmst_reg
91#define __darwin_xmm_reg xmm_reg
92#define __denorm denorm
93#define __ds ds
94#define __eax eax
95#define __ebp ebp
96#define __ebx ebx
97#define __ecx ecx
98#define __edi edi
99#define __edx edx
100#define __eflags eflags
101#define __eip eip
102#define __err err
103#define __errsumm errsumm
104#define __es es
105#define __esi esi
106#define __esp esp
107#define __faultvaddr faultvaddr
108#define __fpu_cs fpu_cs
109#define __fpu_dp fpu_dp
110#define __fpu_ds fpu_ds
111#define __fpu_fcw fpu_fcw
112#define __fpu_fop fpu_fop
113#define __fpu_fsw fpu_fsw
114#define __fpu_ftw fpu_ftw
115#define __fpu_ip fpu_ip
116#define __fpu_mxcsr fpu_mxcsr
117#define __fpu_mxcsrmask fpu_mxcsrmask
118#define __fpu_reserved fpu_reserved
119#define __fpu_reserved1 fpu_reserved1
120#define __fpu_rsrv1 fpu_rsrv1
121#define __fpu_rsrv2 fpu_rsrv2
122#define __fpu_rsrv3 fpu_rsrv3
123#define __fpu_rsrv4 fpu_rsrv4
124#define __fpu_stmm0 fpu_stmm0
125#define __fpu_stmm1 fpu_stmm1
126#define __fpu_stmm2 fpu_stmm2
127#define __fpu_stmm3 fpu_stmm3
128#define __fpu_stmm4 fpu_stmm4
129#define __fpu_stmm5 fpu_stmm5
130#define __fpu_stmm6 fpu_stmm6
131#define __fpu_stmm7 fpu_stmm7
132#define __fpu_xmm0 fpu_xmm0
133#define __fpu_xmm1 fpu_xmm1
134#define __fpu_xmm2 fpu_xmm2
135#define __fpu_xmm3 fpu_xmm3
136#define __fpu_xmm4 fpu_xmm4
137#define __fpu_xmm5 fpu_xmm5
138#define __fpu_xmm6 fpu_xmm6
139#define __fpu_xmm7 fpu_xmm7
140#define __fs fs
141#define __gs gs
142#define __invalid invalid
143#define __mmst_reg mmst_reg
144#define __mmst_rsrv mmst_rsrv
145#define __ovrfl ovrfl
146#define __pc pc
147#define __precis precis
148#define __rc rc
149#define __ss ss
150#define __stkflt stkflt
151#define __tos tos
152#define __trapno trapno
153#define __undfl undfl
154#define __xmm_reg xmm_reg
155#define __zdiv zdiv
156
157#define __rax rax
158#define __rbx rbx
159#define __rcx rcx
160#define __rdx rdx
161#define __rdi rdi
162#define __rsi rsi
163#define __rbp rbp
164#define __rsp rsp
165#define __r8 r8
166#define __r9 r9
167#define __r10 r10
168#define __r11 r11
169#define __r12 r12
170#define __r13 r13
171#define __r14 r14
172#define __r15 r15
173#define __rip rip
174#define __rflags rflags
175
176#define __dr0 dr0
177#define __dr1 dr1
178#define __dr2 dr2
179#define __dr3 dr3
180#define __dr4 dr4
181#define __dr5 dr5
182#define __dr6 dr6
183#define __dr7 dr7
184
185#include <string.h>
186#include <mach-o/fat.h>
187#include <mach-o/loader.h>
188#include <mach/m68k/thread_status.h>
189#undef MACHINE_THREAD_STATE	/* need to undef these to avoid warnings */
190#undef MACHINE_THREAD_STATE_COUNT
191#undef THREAD_STATE_NONE
192#undef VALID_THREAD_STATE_FLAVOR
193#include <mach/ppc/thread_status.h>
194#undef MACHINE_THREAD_STATE	/* need to undef these to avoid warnings */
195#undef MACHINE_THREAD_STATE_COUNT
196#undef THREAD_STATE_NONE
197#undef VALID_THREAD_STATE_FLAVOR
198#include <mach/m88k/thread_status.h>
199#include <mach/i860/thread_status.h>
200#include <mach/i386/thread_status.h>
201#include <mach/hppa/thread_status.h>
202#include <mach/sparc/thread_status.h>
203#include <mach/arm/thread_status.h>
204#include <mach-o/nlist.h>
205#include <mach-o/reloc.h>
206#include <mach-o/ranlib.h>
207#include "stuff/bool.h"
208#include "stuff/bytesex.h"
209
210__private_extern__
211long long
212SWAP_LONG_LONG(
213long long ll)
214{
215	union {
216	    char c[8];
217	    long long ll;
218	} in, out;
219	in.ll = ll;
220	out.c[0] = in.c[7];
221	out.c[1] = in.c[6];
222	out.c[2] = in.c[5];
223	out.c[3] = in.c[4];
224	out.c[4] = in.c[3];
225	out.c[5] = in.c[2];
226	out.c[6] = in.c[1];
227	out.c[7] = in.c[0];
228	return(out.ll);
229}
230
231__private_extern__
232double
233SWAP_DOUBLE(
234double d)
235{
236	union {
237	    char c[8];
238	    double d;
239	} in, out;
240	in.d = d;
241	out.c[0] = in.c[7];
242	out.c[1] = in.c[6];
243	out.c[2] = in.c[5];
244	out.c[3] = in.c[4];
245	out.c[4] = in.c[3];
246	out.c[5] = in.c[2];
247	out.c[6] = in.c[1];
248	out.c[7] = in.c[0];
249	return(out.d);
250}
251
252__private_extern__
253float
254SWAP_FLOAT(
255float f)
256{
257	union {
258	    char c[7];
259	    float f;
260	} in, out;
261	in.f = f;
262	out.c[0] = in.c[3];
263	out.c[1] = in.c[2];
264	out.c[2] = in.c[1];
265	out.c[3] = in.c[0];
266	return(out.f);
267}
268
269/*
270 * get_host_byte_sex() returns the enum constant for the byte sex of the host
271 * it is running on.
272 */
273__private_extern__
274enum byte_sex
275get_host_byte_sex(
276void)
277{
278    uint32_t s;
279
280	s = (BIG_ENDIAN_BYTE_SEX << 24) | LITTLE_ENDIAN_BYTE_SEX;
281	return((enum byte_sex)*((char *)&s));
282}
283
284__private_extern__
285void
286swap_fat_header(
287struct fat_header *fat_header,
288enum byte_sex target_byte_sex)
289{
290#ifdef __MWERKS__
291    enum byte_sex dummy;
292        dummy = target_byte_sex;
293#endif
294
295	fat_header->magic     = SWAP_INT(fat_header->magic);
296	fat_header->nfat_arch = SWAP_INT(fat_header->nfat_arch);
297}
298
299__private_extern__
300void
301swap_fat_arch(
302struct fat_arch *fat_archs,
303uint32_t nfat_arch,
304enum byte_sex target_byte_sex)
305{
306    uint32_t i;
307#ifdef __MWERKS__
308    enum byte_sex dummy;
309        dummy = target_byte_sex;
310#endif
311
312	for(i = 0; i < nfat_arch; i++){
313	    fat_archs[i].cputype    = SWAP_INT(fat_archs[i].cputype);
314	    fat_archs[i].cpusubtype = SWAP_INT(fat_archs[i].cpusubtype);
315	    fat_archs[i].offset     = SWAP_INT(fat_archs[i].offset);
316	    fat_archs[i].size       = SWAP_INT(fat_archs[i].size);
317	    fat_archs[i].align      = SWAP_INT(fat_archs[i].align);
318	}
319}
320
321__private_extern__
322void
323swap_mach_header(
324struct mach_header *mh,
325enum byte_sex target_byte_sex)
326{
327#ifdef __MWERKS__
328    enum byte_sex dummy;
329        dummy = target_byte_sex;
330#endif
331	mh->magic = SWAP_INT(mh->magic);
332	mh->cputype = SWAP_INT(mh->cputype);
333	mh->cpusubtype = SWAP_INT(mh->cpusubtype);
334	mh->filetype = SWAP_INT(mh->filetype);
335	mh->ncmds = SWAP_INT(mh->ncmds);
336	mh->sizeofcmds = SWAP_INT(mh->sizeofcmds);
337	mh->flags = SWAP_INT(mh->flags);
338}
339
340__private_extern__
341void
342swap_mach_header_64(
343struct mach_header_64 *mh,
344enum byte_sex target_byte_sex)
345{
346#ifdef __MWERKS__
347    enum byte_sex dummy;
348        dummy = target_byte_sex;
349#endif
350	mh->magic = SWAP_INT(mh->magic);
351	mh->cputype = SWAP_INT(mh->cputype);
352	mh->cpusubtype = SWAP_INT(mh->cpusubtype);
353	mh->filetype = SWAP_INT(mh->filetype);
354	mh->ncmds = SWAP_INT(mh->ncmds);
355	mh->sizeofcmds = SWAP_INT(mh->sizeofcmds);
356	mh->flags = SWAP_INT(mh->flags);
357	mh->reserved = SWAP_INT(mh->reserved);
358}
359
360__private_extern__
361void
362swap_load_command(
363struct load_command *lc,
364enum byte_sex target_byte_sex)
365{
366#ifdef __MWERKS__
367    enum byte_sex dummy;
368        dummy = target_byte_sex;
369#endif
370	lc->cmd = SWAP_INT(lc->cmd);
371	lc->cmdsize = SWAP_INT(lc->cmdsize);
372}
373
374__private_extern__
375void
376swap_segment_command(
377struct segment_command *sg,
378enum byte_sex target_byte_sex)
379{
380#ifdef __MWERKS__
381    enum byte_sex dummy;
382        dummy = target_byte_sex;
383#endif
384	/* segname[16] */
385	sg->cmd = SWAP_INT(sg->cmd);
386	sg->cmdsize = SWAP_INT(sg->cmdsize);
387	sg->vmaddr = SWAP_INT(sg->vmaddr);
388	sg->vmsize = SWAP_INT(sg->vmsize);
389	sg->fileoff = SWAP_INT(sg->fileoff);
390	sg->filesize = SWAP_INT(sg->filesize);
391	sg->maxprot = SWAP_INT(sg->maxprot);
392	sg->initprot = SWAP_INT(sg->initprot);
393	sg->nsects = SWAP_INT(sg->nsects);
394	sg->flags = SWAP_INT(sg->flags);
395}
396
397__private_extern__
398void
399swap_segment_command_64(
400struct segment_command_64 *sg,
401enum byte_sex target_byte_sex)
402{
403#ifdef __MWERKS__
404    enum byte_sex dummy;
405        dummy = target_byte_sex;
406#endif
407	/* segname[16] */
408	sg->cmd = SWAP_INT(sg->cmd);
409	sg->cmdsize = SWAP_INT(sg->cmdsize);
410	sg->vmaddr = SWAP_LONG_LONG(sg->vmaddr);
411	sg->vmsize = SWAP_LONG_LONG(sg->vmsize);
412	sg->fileoff = SWAP_LONG_LONG(sg->fileoff);
413	sg->filesize = SWAP_LONG_LONG(sg->filesize);
414	sg->maxprot = SWAP_INT(sg->maxprot);
415	sg->initprot = SWAP_INT(sg->initprot);
416	sg->nsects = SWAP_INT(sg->nsects);
417	sg->flags = SWAP_INT(sg->flags);
418}
419
420__private_extern__
421void
422swap_section(
423struct section *s,
424uint32_t nsects,
425enum byte_sex target_byte_sex)
426{
427    uint32_t i;
428#ifdef __MWERKS__
429    enum byte_sex dummy;
430        dummy = target_byte_sex;
431#endif
432
433	for(i = 0; i < nsects; i++){
434	    /* sectname[16] */
435	    /* segname[16] */
436	    s[i].addr = SWAP_INT(s[i].addr);
437	    s[i].size = SWAP_INT(s[i].size);
438	    s[i].offset = SWAP_INT(s[i].offset);
439	    s[i].align = SWAP_INT(s[i].align);
440	    s[i].reloff = SWAP_INT(s[i].reloff);
441	    s[i].nreloc = SWAP_INT(s[i].nreloc);
442	    s[i].flags = SWAP_INT(s[i].flags);
443	    s[i].reserved1 = SWAP_INT(s[i].reserved1);
444	    s[i].reserved2 = SWAP_INT(s[i].reserved2);
445	}
446}
447
448__private_extern__
449void
450swap_section_64(
451struct section_64 *s,
452uint32_t nsects,
453enum byte_sex target_byte_sex)
454{
455    uint32_t i;
456#ifdef __MWERKS__
457    enum byte_sex dummy;
458        dummy = target_byte_sex;
459#endif
460
461	for(i = 0; i < nsects; i++){
462	    /* sectname[16] */
463	    /* segname[16] */
464	    s[i].addr = SWAP_LONG_LONG(s[i].addr);
465	    s[i].size = SWAP_LONG_LONG(s[i].size);
466	    s[i].offset = SWAP_INT(s[i].offset);
467	    s[i].align = SWAP_INT(s[i].align);
468	    s[i].reloff = SWAP_INT(s[i].reloff);
469	    s[i].nreloc = SWAP_INT(s[i].nreloc);
470	    s[i].flags = SWAP_INT(s[i].flags);
471	    s[i].reserved1 = SWAP_INT(s[i].reserved1);
472	    s[i].reserved2 = SWAP_INT(s[i].reserved2);
473	}
474}
475
476__private_extern__
477void
478swap_symtab_command(
479struct symtab_command *st,
480enum byte_sex target_byte_sex)
481{
482#ifdef __MWERKS__
483    enum byte_sex dummy;
484        dummy = target_byte_sex;
485#endif
486	st->cmd = SWAP_INT(st->cmd);
487	st->cmdsize = SWAP_INT(st->cmdsize);
488	st->symoff = SWAP_INT(st->symoff);
489	st->nsyms = SWAP_INT(st->nsyms);
490	st->stroff = SWAP_INT(st->stroff);
491	st->strsize = SWAP_INT(st->strsize);
492}
493
494__private_extern__
495void
496swap_dysymtab_command(
497struct dysymtab_command *dyst,
498enum byte_sex target_byte_sex)
499{
500#ifdef __MWERKS__
501    enum byte_sex dummy;
502        dummy = target_byte_sex;
503#endif
504	dyst->cmd = SWAP_INT(dyst->cmd);
505	dyst->cmdsize = SWAP_INT(dyst->cmdsize);
506	dyst->ilocalsym = SWAP_INT(dyst->ilocalsym);
507	dyst->nlocalsym = SWAP_INT(dyst->nlocalsym);
508	dyst->iextdefsym = SWAP_INT(dyst->iextdefsym);
509	dyst->nextdefsym = SWAP_INT(dyst->nextdefsym);
510	dyst->iundefsym = SWAP_INT(dyst->iundefsym);
511	dyst->nundefsym = SWAP_INT(dyst->nundefsym);
512	dyst->tocoff = SWAP_INT(dyst->tocoff);
513	dyst->ntoc = SWAP_INT(dyst->ntoc);
514	dyst->modtaboff = SWAP_INT(dyst->modtaboff);
515	dyst->nmodtab = SWAP_INT(dyst->nmodtab);
516	dyst->extrefsymoff = SWAP_INT(dyst->extrefsymoff);
517	dyst->nextrefsyms = SWAP_INT(dyst->nextrefsyms);
518	dyst->indirectsymoff = SWAP_INT(dyst->indirectsymoff);
519	dyst->nindirectsyms = SWAP_INT(dyst->nindirectsyms);
520	dyst->extreloff = SWAP_INT(dyst->extreloff);
521	dyst->nextrel = SWAP_INT(dyst->nextrel);
522	dyst->locreloff = SWAP_INT(dyst->locreloff);
523	dyst->nlocrel = SWAP_INT(dyst->nlocrel);
524}
525
526__private_extern__
527void
528swap_symseg_command(
529struct symseg_command *ss,
530enum byte_sex target_byte_sex)
531{
532#ifdef __MWERKS__
533    enum byte_sex dummy;
534        dummy = target_byte_sex;
535#endif
536	ss->cmd = SWAP_INT(ss->cmd);
537	ss->cmdsize = SWAP_INT(ss->cmdsize);
538	ss->offset = SWAP_INT(ss->offset);
539	ss->size = SWAP_INT(ss->size);
540}
541
542__private_extern__
543void
544swap_fvmlib_command(
545struct fvmlib_command *fl,
546enum byte_sex target_byte_sex)
547{
548#ifdef __MWERKS__
549    enum byte_sex dummy;
550        dummy = target_byte_sex;
551#endif
552	fl->cmd = SWAP_INT(fl->cmd);
553	fl->cmdsize = SWAP_INT(fl->cmdsize);
554	fl->fvmlib.name.offset = SWAP_INT(fl->fvmlib.name.offset);
555	fl->fvmlib.minor_version = SWAP_INT(fl->fvmlib.minor_version);
556	fl->fvmlib.header_addr = SWAP_INT(fl->fvmlib.header_addr);
557}
558
559__private_extern__
560void
561swap_dylib_command(
562struct dylib_command *dl,
563enum byte_sex target_byte_sex)
564{
565#ifdef __MWERKS__
566    enum byte_sex dummy;
567        dummy = target_byte_sex;
568#endif
569	dl->cmd = SWAP_INT(dl->cmd);
570	dl->cmdsize = SWAP_INT(dl->cmdsize);
571	dl->dylib.name.offset = SWAP_INT(dl->dylib.name.offset);
572	dl->dylib.timestamp = SWAP_INT(dl->dylib.timestamp);
573	dl->dylib.current_version = SWAP_INT(dl->dylib.current_version);
574	dl->dylib.compatibility_version =
575				SWAP_INT(dl->dylib.compatibility_version);
576}
577
578__private_extern__
579void
580swap_sub_framework_command(
581struct sub_framework_command *sub,
582enum byte_sex target_byte_sex)
583{
584#ifdef __MWERKS__
585    enum byte_sex dummy;
586        dummy = target_byte_sex;
587#endif
588	sub->cmd = SWAP_INT(sub->cmd);
589	sub->cmdsize = SWAP_INT(sub->cmdsize);
590	sub->umbrella.offset = SWAP_INT(sub->umbrella.offset);
591}
592
593__private_extern__
594void
595swap_sub_umbrella_command(
596struct sub_umbrella_command *usub,
597enum byte_sex target_byte_sex)
598{
599#ifdef __MWERKS__
600    enum byte_sex dummy;
601        dummy = target_byte_sex;
602#endif
603	usub->cmd = SWAP_INT(usub->cmd);
604	usub->cmdsize = SWAP_INT(usub->cmdsize);
605	usub->sub_umbrella.offset = SWAP_INT(usub->sub_umbrella.offset);
606}
607
608__private_extern__
609void
610swap_sub_library_command(
611struct sub_library_command *lsub,
612enum byte_sex target_byte_sex)
613{
614#ifdef __MWERKS__
615    enum byte_sex dummy;
616        dummy = target_byte_sex;
617#endif
618	lsub->cmd = SWAP_INT(lsub->cmd);
619	lsub->cmdsize = SWAP_INT(lsub->cmdsize);
620	lsub->sub_library.offset = SWAP_INT(lsub->sub_library.offset);
621}
622
623__private_extern__
624void
625swap_sub_client_command(
626struct sub_client_command *csub,
627enum byte_sex target_byte_sex)
628{
629#ifdef __MWERKS__
630    enum byte_sex dummy;
631        dummy = target_byte_sex;
632#endif
633	csub->cmd = SWAP_INT(csub->cmd);
634	csub->cmdsize = SWAP_INT(csub->cmdsize);
635	csub->client.offset = SWAP_INT(csub->client.offset);
636}
637
638__private_extern__
639void
640swap_prebound_dylib_command(
641struct prebound_dylib_command *pbdylib,
642enum byte_sex target_byte_sex)
643{
644#ifdef __MWERKS__
645    enum byte_sex dummy;
646        dummy = target_byte_sex;
647#endif
648	pbdylib->cmd = SWAP_INT(pbdylib->cmd);
649	pbdylib->cmdsize = SWAP_INT(pbdylib->cmdsize);
650	pbdylib->name.offset = SWAP_INT(pbdylib->name.offset);
651	pbdylib->nmodules = SWAP_INT(pbdylib->nmodules);
652	pbdylib->linked_modules.offset =
653		SWAP_INT(pbdylib->linked_modules.offset);
654}
655
656__private_extern__
657void
658swap_dylinker_command(
659struct dylinker_command *dyld,
660enum byte_sex target_byte_sex)
661{
662#ifdef __MWERKS__
663    enum byte_sex dummy;
664        dummy = target_byte_sex;
665#endif
666	dyld->cmd = SWAP_INT(dyld->cmd);
667	dyld->cmdsize = SWAP_INT(dyld->cmdsize);
668	dyld->name.offset = SWAP_INT(dyld->name.offset);
669}
670
671__private_extern__
672void
673swap_fvmfile_command(
674struct fvmfile_command *ff,
675enum byte_sex target_byte_sex)
676{
677#ifdef __MWERKS__
678    enum byte_sex dummy;
679        dummy = target_byte_sex;
680#endif
681	ff->cmd = SWAP_INT(ff->cmd);
682	ff->cmdsize = SWAP_INT(ff->cmdsize);
683	ff->name.offset = SWAP_INT(ff->name.offset);
684	ff->header_addr = SWAP_INT(ff->header_addr);
685}
686
687
688__private_extern__
689void
690swap_thread_command(
691struct thread_command *ut,
692enum byte_sex target_byte_sex)
693{
694#ifdef __MWERKS__
695    enum byte_sex dummy;
696        dummy = target_byte_sex;
697#endif
698	ut->cmd = SWAP_INT(ut->cmd);
699	ut->cmdsize = SWAP_INT(ut->cmdsize);
700}
701
702__private_extern__
703void
704swap_m68k_thread_state_regs(
705struct m68k_thread_state_regs *cpu,
706enum byte_sex target_byte_sex)
707{
708    uint32_t i;
709#ifdef __MWERKS__
710    enum byte_sex dummy;
711        dummy = target_byte_sex;
712#endif
713
714	for(i = 0; i < 8; i++)
715	    cpu->dreg[i] = SWAP_INT(cpu->dreg[i]);
716	for(i = 0; i < 8; i++)
717	    cpu->areg[i] = SWAP_INT(cpu->areg[i]);
718	cpu->pad0 = SWAP_SHORT(cpu->pad0);
719	cpu->sr = SWAP_SHORT(cpu->sr);
720	cpu->pc = SWAP_INT(cpu->pc);
721}
722
723__private_extern__
724void
725swap_m68k_thread_state_68882(
726struct m68k_thread_state_68882 *fpu,
727enum byte_sex target_byte_sex)
728{
729    uint32_t i, tmp;
730#ifdef __MWERKS__
731    enum byte_sex dummy;
732        dummy = target_byte_sex;
733#endif
734
735	for(i = 0; i < 8; i++){
736	                   tmp = SWAP_INT(fpu->regs[i].fp[0]);
737	    fpu->regs[i].fp[1] = SWAP_INT(fpu->regs[i].fp[1]);
738	    fpu->regs[i].fp[0] = SWAP_INT(fpu->regs[i].fp[2]);
739	    fpu->regs[i].fp[2] = tmp;
740	}
741	fpu->cr = SWAP_INT(fpu->cr);
742	fpu->sr = SWAP_INT(fpu->sr);
743	fpu->iar = SWAP_INT(fpu->iar);
744	fpu->state = SWAP_INT(fpu->state);
745}
746
747__private_extern__
748void
749swap_m68k_thread_state_user_reg(
750struct m68k_thread_state_user_reg *user_reg,
751enum byte_sex target_byte_sex)
752{
753#ifdef __MWERKS__
754    enum byte_sex dummy;
755        dummy = target_byte_sex;
756#endif
757	user_reg->user_reg = SWAP_INT(user_reg->user_reg);
758}
759
760__private_extern__
761void
762swap_ppc_thread_state_t(
763ppc_thread_state_t *cpu,
764enum byte_sex target_byte_sex)
765{
766	cpu->srr0 = SWAP_INT(cpu->srr0);
767	cpu->srr1 = SWAP_INT(cpu->srr1);
768	cpu->r0 = SWAP_INT(cpu->r0);
769	cpu->r1 = SWAP_INT(cpu->r1);
770	cpu->r2 = SWAP_INT(cpu->r2);
771	cpu->r3 = SWAP_INT(cpu->r3);
772	cpu->r4 = SWAP_INT(cpu->r4);
773	cpu->r5 = SWAP_INT(cpu->r5);
774	cpu->r6 = SWAP_INT(cpu->r6);
775	cpu->r7 = SWAP_INT(cpu->r7);
776	cpu->r8 = SWAP_INT(cpu->r8);
777	cpu->r9 = SWAP_INT(cpu->r9);
778	cpu->r10 = SWAP_INT(cpu->r10);
779	cpu->r11 = SWAP_INT(cpu->r11);
780	cpu->r12 = SWAP_INT(cpu->r12);
781	cpu->r13 = SWAP_INT(cpu->r13);
782	cpu->r14 = SWAP_INT(cpu->r14);
783	cpu->r15 = SWAP_INT(cpu->r15);
784	cpu->r16 = SWAP_INT(cpu->r16);
785	cpu->r17 = SWAP_INT(cpu->r17);
786	cpu->r18 = SWAP_INT(cpu->r18);
787	cpu->r19 = SWAP_INT(cpu->r19);
788	cpu->r20 = SWAP_INT(cpu->r20);
789	cpu->r21 = SWAP_INT(cpu->r21);
790	cpu->r22 = SWAP_INT(cpu->r22);
791	cpu->r23 = SWAP_INT(cpu->r23);
792	cpu->r24 = SWAP_INT(cpu->r24);
793	cpu->r25 = SWAP_INT(cpu->r25);
794	cpu->r26 = SWAP_INT(cpu->r26);
795	cpu->r27 = SWAP_INT(cpu->r27);
796	cpu->r28 = SWAP_INT(cpu->r28);
797	cpu->r29 = SWAP_INT(cpu->r29);
798	cpu->r30 = SWAP_INT(cpu->r30);
799	cpu->r31 = SWAP_INT(cpu->r31);
800	cpu->cr  = SWAP_INT(cpu->cr);
801	cpu->xer = SWAP_INT(cpu->xer);
802	cpu->lr  = SWAP_INT(cpu->lr);
803	cpu->ctr = SWAP_INT(cpu->ctr);
804	cpu->mq =  SWAP_INT(cpu->mq);
805	cpu->vrsave = SWAP_INT(cpu->vrsave);
806}
807
808__private_extern__
809void
810swap_ppc_thread_state64_t(
811ppc_thread_state64_t *cpu,
812enum byte_sex target_byte_sex)
813{
814	cpu->srr0 = SWAP_LONG_LONG(cpu->srr0);
815	cpu->srr1 = SWAP_LONG_LONG(cpu->srr1);
816	cpu->r0 = SWAP_LONG_LONG(cpu->r0);
817	cpu->r1 = SWAP_LONG_LONG(cpu->r1);
818	cpu->r2 = SWAP_LONG_LONG(cpu->r2);
819	cpu->r3 = SWAP_LONG_LONG(cpu->r3);
820	cpu->r4 = SWAP_LONG_LONG(cpu->r4);
821	cpu->r5 = SWAP_LONG_LONG(cpu->r5);
822	cpu->r6 = SWAP_LONG_LONG(cpu->r6);
823	cpu->r7 = SWAP_LONG_LONG(cpu->r7);
824	cpu->r8 = SWAP_LONG_LONG(cpu->r8);
825	cpu->r9 = SWAP_LONG_LONG(cpu->r9);
826	cpu->r10 = SWAP_LONG_LONG(cpu->r10);
827	cpu->r11 = SWAP_LONG_LONG(cpu->r11);
828	cpu->r12 = SWAP_LONG_LONG(cpu->r12);
829	cpu->r13 = SWAP_LONG_LONG(cpu->r13);
830	cpu->r14 = SWAP_LONG_LONG(cpu->r14);
831	cpu->r15 = SWAP_LONG_LONG(cpu->r15);
832	cpu->r16 = SWAP_LONG_LONG(cpu->r16);
833	cpu->r17 = SWAP_LONG_LONG(cpu->r17);
834	cpu->r18 = SWAP_LONG_LONG(cpu->r18);
835	cpu->r19 = SWAP_LONG_LONG(cpu->r19);
836	cpu->r20 = SWAP_LONG_LONG(cpu->r20);
837	cpu->r21 = SWAP_LONG_LONG(cpu->r21);
838	cpu->r22 = SWAP_LONG_LONG(cpu->r22);
839	cpu->r23 = SWAP_LONG_LONG(cpu->r23);
840	cpu->r24 = SWAP_LONG_LONG(cpu->r24);
841	cpu->r25 = SWAP_LONG_LONG(cpu->r25);
842	cpu->r26 = SWAP_LONG_LONG(cpu->r26);
843	cpu->r27 = SWAP_LONG_LONG(cpu->r27);
844	cpu->r28 = SWAP_LONG_LONG(cpu->r28);
845	cpu->r29 = SWAP_LONG_LONG(cpu->r29);
846	cpu->r30 = SWAP_LONG_LONG(cpu->r30);
847	cpu->r31 = SWAP_LONG_LONG(cpu->r31);
848	cpu->cr  = SWAP_INT(cpu->cr);
849	cpu->xer = SWAP_LONG_LONG(cpu->xer);
850	cpu->lr  = SWAP_LONG_LONG(cpu->lr);
851	cpu->ctr = SWAP_LONG_LONG(cpu->ctr);
852	cpu->vrsave =  SWAP_INT(cpu->vrsave);
853}
854
855__private_extern__
856void
857swap_ppc_float_state_t(
858ppc_float_state_t *fpu,
859enum byte_sex target_byte_sex)
860{
861    uint32_t i;
862#ifdef __MWERKS__
863    enum byte_sex dummy;
864        dummy = target_byte_sex;
865#endif
866
867	for(i = 0; i < 32; i++)
868	    fpu->fpregs[i] = SWAP_DOUBLE(fpu->fpregs[i]);
869
870	fpu->fpscr_pad = SWAP_INT(fpu->fpscr_pad);
871	fpu->fpscr = SWAP_INT(fpu->fpscr);
872}
873
874__private_extern__
875void
876swap_ppc_exception_state_t(
877ppc_exception_state_t *state,
878enum byte_sex target_byte_sex)
879{
880    uint32_t i;
881#ifdef __MWERKS__
882    enum byte_sex dummy;
883        dummy = target_byte_sex;
884#endif
885
886	state->dar = SWAP_INT(state->dar);
887	state->dsisr = SWAP_INT(state->dsisr);
888	state->exception = SWAP_INT(state->exception);
889	state->pad0 = SWAP_INT(state->pad0);
890
891	for(i = 0; i < 4; i++)
892	    state->pad1[i] = SWAP_INT(state->pad1[i]);
893}
894
895__private_extern__
896void
897swap_m88k_thread_state_grf_t(
898m88k_thread_state_grf_t *cpu,
899enum byte_sex target_byte_sex)
900{
901#ifdef __MWERKS__
902    enum byte_sex dummy;
903        dummy = target_byte_sex;
904#endif
905	cpu->r1 = SWAP_INT(cpu->r1);
906	cpu->r2 = SWAP_INT(cpu->r2);
907	cpu->r3 = SWAP_INT(cpu->r3);
908	cpu->r4 = SWAP_INT(cpu->r4);
909	cpu->r5 = SWAP_INT(cpu->r5);
910	cpu->r6 = SWAP_INT(cpu->r6);
911	cpu->r7 = SWAP_INT(cpu->r7);
912	cpu->r8 = SWAP_INT(cpu->r8);
913	cpu->r9 = SWAP_INT(cpu->r9);
914	cpu->r10 = SWAP_INT(cpu->r10);
915	cpu->r11 = SWAP_INT(cpu->r11);
916	cpu->r12 = SWAP_INT(cpu->r12);
917	cpu->r13 = SWAP_INT(cpu->r13);
918	cpu->r14 = SWAP_INT(cpu->r14);
919	cpu->r15 = SWAP_INT(cpu->r15);
920	cpu->r16 = SWAP_INT(cpu->r16);
921	cpu->r17 = SWAP_INT(cpu->r17);
922	cpu->r18 = SWAP_INT(cpu->r18);
923	cpu->r19 = SWAP_INT(cpu->r19);
924	cpu->r20 = SWAP_INT(cpu->r20);
925	cpu->r21 = SWAP_INT(cpu->r21);
926	cpu->r22 = SWAP_INT(cpu->r22);
927	cpu->r23 = SWAP_INT(cpu->r23);
928	cpu->r24 = SWAP_INT(cpu->r24);
929	cpu->r25 = SWAP_INT(cpu->r25);
930	cpu->r26 = SWAP_INT(cpu->r26);
931	cpu->r27 = SWAP_INT(cpu->r27);
932	cpu->r28 = SWAP_INT(cpu->r28);
933	cpu->r29 = SWAP_INT(cpu->r29);
934	cpu->r30 = SWAP_INT(cpu->r30);
935	cpu->r31 = SWAP_INT(cpu->r31);
936	cpu->xip = SWAP_INT(cpu->xip);
937	cpu->xip_in_bd = SWAP_INT(cpu->xip_in_bd);
938	cpu->nip = SWAP_INT(cpu->nip);
939}
940
941__private_extern__
942void
943swap_m88k_thread_state_xrf_t(
944m88k_thread_state_xrf_t *fpu,
945enum byte_sex target_byte_sex)
946{
947    enum byte_sex host_byte_sex;
948
949    struct swapped_m88k_fpsr {
950	union {
951	    struct {
952		unsigned	afinx:BIT_WIDTH(0);
953		unsigned	afovf:BIT_WIDTH(1);
954		unsigned	afunf:BIT_WIDTH(2);
955		unsigned	afdvz:BIT_WIDTH(3);
956		unsigned	afinv:BIT_WIDTH(4);
957		unsigned	:BITS_WIDTH(15,5);
958		unsigned	xmod:BIT_WIDTH(16);
959		unsigned	:BITS_WIDTH(31,17);
960	    } fields;
961	    uint32_t word;
962	} u;
963    } ssr;
964    struct swapped_m88k_fpcr {
965	union {
966	    struct {
967		unsigned	efinx:BIT_WIDTH(0);
968		unsigned	efovf:BIT_WIDTH(1);
969		unsigned	efunf:BIT_WIDTH(2);
970		unsigned	efdvz:BIT_WIDTH(3);
971		unsigned	efinv:BIT_WIDTH(4);
972		unsigned	:BITS_WIDTH(13,5);
973		m88k_fpcr_rm_t	rm:BITS_WIDTH(15,14);
974		unsigned	:BITS_WIDTH(31,16);
975	    } fields;
976	    uint32_t word;
977	} u;
978    } scr;
979
980	host_byte_sex = get_host_byte_sex();
981
982	fpu->x1.x[0] = SWAP_INT(fpu->x1.x[0]);
983	fpu->x1.x[1] = SWAP_INT(fpu->x1.x[1]);
984	fpu->x1.x[2] = SWAP_INT(fpu->x1.x[2]);
985	fpu->x1.x[3] = SWAP_INT(fpu->x1.x[3]);
986	fpu->x2.x[0] = SWAP_INT(fpu->x2.x[0]);
987	fpu->x2.x[1] = SWAP_INT(fpu->x2.x[1]);
988	fpu->x2.x[2] = SWAP_INT(fpu->x2.x[2]);
989	fpu->x2.x[3] = SWAP_INT(fpu->x2.x[3]);
990	fpu->x3.x[0] = SWAP_INT(fpu->x3.x[0]);
991	fpu->x3.x[1] = SWAP_INT(fpu->x3.x[1]);
992	fpu->x3.x[2] = SWAP_INT(fpu->x3.x[2]);
993	fpu->x3.x[3] = SWAP_INT(fpu->x3.x[3]);
994	fpu->x4.x[0] = SWAP_INT(fpu->x4.x[0]);
995	fpu->x4.x[1] = SWAP_INT(fpu->x4.x[1]);
996	fpu->x4.x[2] = SWAP_INT(fpu->x4.x[2]);
997	fpu->x4.x[3] = SWAP_INT(fpu->x4.x[3]);
998	fpu->x5.x[0] = SWAP_INT(fpu->x5.x[0]);
999	fpu->x5.x[1] = SWAP_INT(fpu->x5.x[1]);
1000	fpu->x5.x[2] = SWAP_INT(fpu->x5.x[2]);
1001	fpu->x5.x[3] = SWAP_INT(fpu->x5.x[3]);
1002	fpu->x6.x[0] = SWAP_INT(fpu->x6.x[0]);
1003	fpu->x6.x[1] = SWAP_INT(fpu->x6.x[1]);
1004	fpu->x6.x[2] = SWAP_INT(fpu->x6.x[2]);
1005	fpu->x6.x[3] = SWAP_INT(fpu->x6.x[3]);
1006	fpu->x7.x[0] = SWAP_INT(fpu->x7.x[0]);
1007	fpu->x7.x[1] = SWAP_INT(fpu->x7.x[1]);
1008	fpu->x7.x[2] = SWAP_INT(fpu->x7.x[2]);
1009	fpu->x7.x[3] = SWAP_INT(fpu->x7.x[3]);
1010	fpu->x8.x[0] = SWAP_INT(fpu->x8.x[0]);
1011	fpu->x8.x[1] = SWAP_INT(fpu->x8.x[1]);
1012	fpu->x8.x[2] = SWAP_INT(fpu->x8.x[2]);
1013	fpu->x8.x[3] = SWAP_INT(fpu->x8.x[3]);
1014	fpu->x9.x[0] = SWAP_INT(fpu->x9.x[0]);
1015	fpu->x9.x[1] = SWAP_INT(fpu->x9.x[1]);
1016	fpu->x9.x[2] = SWAP_INT(fpu->x9.x[2]);
1017	fpu->x9.x[3] = SWAP_INT(fpu->x9.x[3]);
1018	fpu->x10.x[0] = SWAP_INT(fpu->x10.x[0]);
1019	fpu->x10.x[1] = SWAP_INT(fpu->x10.x[1]);
1020	fpu->x10.x[2] = SWAP_INT(fpu->x10.x[2]);
1021	fpu->x10.x[3] = SWAP_INT(fpu->x10.x[3]);
1022	fpu->x11.x[0] = SWAP_INT(fpu->x11.x[0]);
1023	fpu->x11.x[1] = SWAP_INT(fpu->x11.x[1]);
1024	fpu->x11.x[2] = SWAP_INT(fpu->x11.x[2]);
1025	fpu->x11.x[3] = SWAP_INT(fpu->x11.x[3]);
1026	fpu->x12.x[0] = SWAP_INT(fpu->x12.x[0]);
1027	fpu->x12.x[1] = SWAP_INT(fpu->x12.x[1]);
1028	fpu->x12.x[2] = SWAP_INT(fpu->x12.x[2]);
1029	fpu->x12.x[3] = SWAP_INT(fpu->x12.x[3]);
1030	fpu->x13.x[0] = SWAP_INT(fpu->x13.x[0]);
1031	fpu->x13.x[1] = SWAP_INT(fpu->x13.x[1]);
1032	fpu->x13.x[2] = SWAP_INT(fpu->x13.x[2]);
1033	fpu->x13.x[3] = SWAP_INT(fpu->x13.x[3]);
1034	fpu->x14.x[0] = SWAP_INT(fpu->x14.x[0]);
1035	fpu->x14.x[1] = SWAP_INT(fpu->x14.x[1]);
1036	fpu->x14.x[2] = SWAP_INT(fpu->x14.x[2]);
1037	fpu->x14.x[3] = SWAP_INT(fpu->x14.x[3]);
1038	fpu->x15.x[0] = SWAP_INT(fpu->x15.x[0]);
1039	fpu->x15.x[1] = SWAP_INT(fpu->x15.x[1]);
1040	fpu->x15.x[2] = SWAP_INT(fpu->x15.x[2]);
1041	fpu->x15.x[3] = SWAP_INT(fpu->x15.x[3]);
1042	fpu->x16.x[0] = SWAP_INT(fpu->x16.x[0]);
1043	fpu->x16.x[1] = SWAP_INT(fpu->x16.x[1]);
1044	fpu->x16.x[2] = SWAP_INT(fpu->x16.x[2]);
1045	fpu->x16.x[3] = SWAP_INT(fpu->x16.x[3]);
1046	fpu->x17.x[0] = SWAP_INT(fpu->x17.x[0]);
1047	fpu->x17.x[1] = SWAP_INT(fpu->x17.x[1]);
1048	fpu->x17.x[2] = SWAP_INT(fpu->x17.x[2]);
1049	fpu->x17.x[3] = SWAP_INT(fpu->x17.x[3]);
1050	fpu->x18.x[0] = SWAP_INT(fpu->x18.x[0]);
1051	fpu->x18.x[1] = SWAP_INT(fpu->x18.x[1]);
1052	fpu->x18.x[2] = SWAP_INT(fpu->x18.x[2]);
1053	fpu->x18.x[3] = SWAP_INT(fpu->x18.x[3]);
1054	fpu->x19.x[0] = SWAP_INT(fpu->x19.x[0]);
1055	fpu->x19.x[1] = SWAP_INT(fpu->x19.x[1]);
1056	fpu->x19.x[2] = SWAP_INT(fpu->x19.x[2]);
1057	fpu->x19.x[3] = SWAP_INT(fpu->x19.x[3]);
1058	fpu->x20.x[0] = SWAP_INT(fpu->x20.x[0]);
1059	fpu->x20.x[1] = SWAP_INT(fpu->x20.x[1]);
1060	fpu->x20.x[2] = SWAP_INT(fpu->x20.x[2]);
1061	fpu->x20.x[3] = SWAP_INT(fpu->x20.x[3]);
1062	fpu->x21.x[0] = SWAP_INT(fpu->x21.x[0]);
1063	fpu->x21.x[1] = SWAP_INT(fpu->x21.x[1]);
1064	fpu->x21.x[2] = SWAP_INT(fpu->x21.x[2]);
1065	fpu->x21.x[3] = SWAP_INT(fpu->x21.x[3]);
1066	fpu->x22.x[0] = SWAP_INT(fpu->x22.x[0]);
1067	fpu->x22.x[1] = SWAP_INT(fpu->x22.x[1]);
1068	fpu->x22.x[2] = SWAP_INT(fpu->x22.x[2]);
1069	fpu->x22.x[3] = SWAP_INT(fpu->x22.x[3]);
1070	fpu->x23.x[0] = SWAP_INT(fpu->x23.x[0]);
1071	fpu->x23.x[1] = SWAP_INT(fpu->x23.x[1]);
1072	fpu->x23.x[2] = SWAP_INT(fpu->x23.x[2]);
1073	fpu->x23.x[3] = SWAP_INT(fpu->x23.x[3]);
1074	fpu->x24.x[0] = SWAP_INT(fpu->x24.x[0]);
1075	fpu->x24.x[1] = SWAP_INT(fpu->x24.x[1]);
1076	fpu->x24.x[2] = SWAP_INT(fpu->x24.x[2]);
1077	fpu->x24.x[3] = SWAP_INT(fpu->x24.x[3]);
1078	fpu->x25.x[0] = SWAP_INT(fpu->x25.x[0]);
1079	fpu->x25.x[1] = SWAP_INT(fpu->x25.x[1]);
1080	fpu->x25.x[2] = SWAP_INT(fpu->x25.x[2]);
1081	fpu->x25.x[3] = SWAP_INT(fpu->x25.x[3]);
1082	fpu->x26.x[0] = SWAP_INT(fpu->x26.x[0]);
1083	fpu->x26.x[1] = SWAP_INT(fpu->x26.x[1]);
1084	fpu->x26.x[2] = SWAP_INT(fpu->x26.x[2]);
1085	fpu->x26.x[3] = SWAP_INT(fpu->x26.x[3]);
1086	fpu->x27.x[0] = SWAP_INT(fpu->x27.x[0]);
1087	fpu->x27.x[1] = SWAP_INT(fpu->x27.x[1]);
1088	fpu->x27.x[2] = SWAP_INT(fpu->x27.x[2]);
1089	fpu->x27.x[3] = SWAP_INT(fpu->x27.x[3]);
1090	fpu->x28.x[0] = SWAP_INT(fpu->x28.x[0]);
1091	fpu->x28.x[1] = SWAP_INT(fpu->x28.x[1]);
1092	fpu->x28.x[2] = SWAP_INT(fpu->x28.x[2]);
1093	fpu->x28.x[3] = SWAP_INT(fpu->x28.x[3]);
1094	fpu->x29.x[0] = SWAP_INT(fpu->x29.x[0]);
1095	fpu->x29.x[1] = SWAP_INT(fpu->x29.x[1]);
1096	fpu->x29.x[2] = SWAP_INT(fpu->x29.x[2]);
1097	fpu->x29.x[3] = SWAP_INT(fpu->x29.x[3]);
1098	fpu->x30.x[0] = SWAP_INT(fpu->x30.x[0]);
1099	fpu->x30.x[1] = SWAP_INT(fpu->x30.x[1]);
1100	fpu->x30.x[2] = SWAP_INT(fpu->x30.x[2]);
1101	fpu->x30.x[3] = SWAP_INT(fpu->x30.x[3]);
1102	fpu->x31.x[0] = SWAP_INT(fpu->x31.x[0]);
1103	fpu->x31.x[1] = SWAP_INT(fpu->x31.x[1]);
1104	fpu->x31.x[2] = SWAP_INT(fpu->x31.x[2]);
1105	fpu->x31.x[3] = SWAP_INT(fpu->x31.x[3]);
1106
1107	if(target_byte_sex == host_byte_sex){
1108	    memcpy(&ssr, &(fpu->fpsr), sizeof(struct swapped_m88k_fpsr));
1109	    ssr.u.word = SWAP_INT(ssr.u.word);
1110	    fpu->fpsr.afinx = ssr.u.fields.afinx;
1111	    fpu->fpsr.afovf = ssr.u.fields.afovf;
1112	    fpu->fpsr.afunf = ssr.u.fields.afunf;
1113	    fpu->fpsr.afdvz = ssr.u.fields.afdvz;
1114	    fpu->fpsr.afinv = ssr.u.fields.afinv;
1115	    fpu->fpsr.xmod = ssr.u.fields.xmod;
1116
1117	    memcpy(&scr, &(fpu->fpcr), sizeof(struct swapped_m88k_fpcr));
1118	    scr.u.word = SWAP_INT(scr.u.word);
1119	    fpu->fpcr.efinx = scr.u.fields.efinx;
1120	    fpu->fpcr.efovf = scr.u.fields.efovf;
1121	    fpu->fpcr.efunf = scr.u.fields.efunf;
1122	    fpu->fpcr.efdvz = scr.u.fields.efdvz;
1123	    fpu->fpcr.efinv = scr.u.fields.efinv;
1124	    fpu->fpcr.rm = scr.u.fields.rm;
1125	}
1126	else{
1127	    ssr.u.fields.afinx = fpu->fpsr.afinx;
1128	    ssr.u.fields.afovf = fpu->fpsr.afovf;
1129	    ssr.u.fields.afunf = fpu->fpsr.afunf;
1130	    ssr.u.fields.afdvz = fpu->fpsr.afdvz;
1131	    ssr.u.fields.afinv = fpu->fpsr.afinv;
1132	    ssr.u.fields.xmod = fpu->fpsr.xmod;
1133	    ssr.u.word = SWAP_INT(ssr.u.word);
1134	    memcpy(&(fpu->fpsr), &ssr, sizeof(struct swapped_m88k_fpsr));
1135
1136	    scr.u.fields.efinx = fpu->fpcr.efinx;
1137	    scr.u.fields.efovf = fpu->fpcr.efovf;
1138	    scr.u.fields.efunf = fpu->fpcr.efunf;
1139	    scr.u.fields.efdvz = fpu->fpcr.efdvz;
1140	    scr.u.fields.efinv = fpu->fpcr.efinv;
1141	    scr.u.fields.rm = fpu->fpcr.rm;
1142	    scr.u.word = SWAP_INT(scr.u.word);
1143	    memcpy(&(fpu->fpcr), &scr, sizeof(struct swapped_m88k_fpcr));
1144	}
1145}
1146
1147__private_extern__
1148void
1149swap_m88k_thread_state_user_t(
1150m88k_thread_state_user_t *user,
1151enum byte_sex target_byte_sex)
1152{
1153#ifdef __MWERKS__
1154    enum byte_sex dummy;
1155        dummy = target_byte_sex;
1156#endif
1157	user->user = SWAP_INT(user->user);
1158}
1159
1160__private_extern__
1161void
1162swap_m88110_thread_state_impl_t(
1163m88110_thread_state_impl_t *spu,
1164enum byte_sex target_byte_sex)
1165{
1166    uint32_t i;
1167    enum byte_sex host_byte_sex;
1168
1169    struct swapped_m88110_bp_ctrl {
1170	union {
1171	    struct {
1172		unsigned	v:BIT_WIDTH(0);
1173		m88110_match_t	addr_match:BITS_WIDTH(12,1);
1174		unsigned	:BITS_WIDTH(26,13);
1175		unsigned	rwm:BIT_WIDTH(27);
1176		unsigned	rw:BIT_WIDTH(28);
1177		unsigned	:BITS_WIDTH(31,29);
1178	    } fields;
1179	    uint32_t word;
1180	} u;
1181    } sbpc;
1182
1183    struct swap_m88110_psr {
1184	union {
1185	    struct {
1186		unsigned	:BITS_WIDTH(1,0);
1187		unsigned	mxm_dis:BIT_WIDTH(2);
1188		unsigned	sfu1dis:BIT_WIDTH(3);
1189		unsigned	:BITS_WIDTH(22,4);
1190		unsigned	trace:BIT_WIDTH(23);
1191		unsigned	:BIT_WIDTH(24);
1192		unsigned	sm:BIT_WIDTH(25);
1193		unsigned	sgn_imd:BIT_WIDTH(26);
1194		unsigned	:BIT_WIDTH(27);
1195		unsigned	c:BIT_WIDTH(28);
1196		unsigned	se:BIT_WIDTH(29);
1197		unsigned	le:BIT_WIDTH(30);
1198		unsigned	supr:BIT_WIDTH(31);
1199	    } fields;
1200	    uint32_t word;
1201	} u;
1202    } spsr;
1203
1204    struct swapped_m88110_fp_trap_status {
1205	union {
1206	    struct {
1207		unsigned	efinx:BIT_WIDTH(0);
1208		unsigned	efovf:BIT_WIDTH(1);
1209		unsigned	efunf:BIT_WIDTH(2);
1210		unsigned	efdvz:BIT_WIDTH(3);
1211		unsigned	efinv:BIT_WIDTH(4);
1212		unsigned	priv:BIT_WIDTH(5);
1213		unsigned	unimp:BIT_WIDTH(6);
1214		unsigned	int:BIT_WIDTH(7);
1215		unsigned	sfu1_disabled:BIT_WIDTH(8);
1216		unsigned	:BITS_WIDTH(13,9);
1217		m88110_iresult_size_t	iresult_size:BITS_WIDTH(15,14);
1218		unsigned	:BITS_WIDTH(31,16);
1219	    } fields;
1220	    uint32_t word;
1221	} u;
1222    } sfps;
1223
1224	host_byte_sex = get_host_byte_sex();
1225
1226	if(target_byte_sex == host_byte_sex){
1227	    for(i = 0; i < M88110_N_DATA_BP; i++){
1228		spu->data_bp[i].addr = SWAP_INT(spu->data_bp[i].addr);
1229		memcpy(&sbpc, &(spu->data_bp[i].ctrl),
1230		       sizeof(struct swapped_m88110_bp_ctrl));
1231		sbpc.u.word = SWAP_INT(sbpc.u.word);
1232		spu->data_bp[i].ctrl.v = sbpc.u.fields.v;
1233		spu->data_bp[i].ctrl.addr_match = sbpc.u.fields.addr_match;
1234		spu->data_bp[i].ctrl.rwm = sbpc.u.fields.rwm;
1235		spu->data_bp[i].ctrl.rw = sbpc.u.fields.rw;
1236	    }
1237
1238	    memcpy(&spsr, &(spu->psr), sizeof(struct swap_m88110_psr));
1239	    spsr.u.word = SWAP_INT(spsr.u.word);
1240	    spu->psr.mxm_dis = spsr.u.fields.mxm_dis;
1241	    spu->psr.sfu1dis = spsr.u.fields.sfu1dis;
1242	    spu->psr.trace = spsr.u.fields.trace;
1243	    spu->psr.sm = spsr.u.fields.sm;
1244	    spu->psr.sgn_imd = spsr.u.fields.sgn_imd;
1245	    spu->psr.c = spsr.u.fields.c;
1246	    spu->psr.se = spsr.u.fields.se;
1247	    spu->psr.le = spsr.u.fields.le;
1248	    spu->psr.supr = spsr.u.fields.supr;
1249
1250	    memcpy(&sfps, &(spu->fp_trap_status),
1251		   sizeof(struct swapped_m88110_fp_trap_status));
1252	    sfps.u.word = SWAP_INT(sfps.u.word);
1253	    spu->fp_trap_status.efinx = sfps.u.fields.efinx;
1254	    spu->fp_trap_status.efovf = sfps.u.fields.efovf;
1255	    spu->fp_trap_status.efunf = sfps.u.fields.efunf;
1256	    spu->fp_trap_status.efdvz = sfps.u.fields.efdvz;
1257	    spu->fp_trap_status.efinv = sfps.u.fields.efinv;
1258	    spu->fp_trap_status.priv = sfps.u.fields.priv;
1259	    spu->fp_trap_status.unimp = sfps.u.fields.unimp;
1260	    spu->fp_trap_status.sfu1_disabled = sfps.u.fields.sfu1_disabled;
1261	    spu->fp_trap_status.iresult_size = sfps.u.fields.iresult_size;
1262	}
1263	else{
1264	    for(i = 0; i < M88110_N_DATA_BP; i++){
1265		spu->data_bp[i].addr = SWAP_INT(spu->data_bp[i].addr);
1266		sbpc.u.fields.v = spu->data_bp[i].ctrl.v;
1267		sbpc.u.fields.addr_match = spu->data_bp[i].ctrl.addr_match;
1268		sbpc.u.fields.rwm = spu->data_bp[i].ctrl.rwm;
1269		sbpc.u.fields.rw = spu->data_bp[i].ctrl.rw;
1270		sbpc.u.word = SWAP_INT(sbpc.u.word);
1271		memcpy(&(spu->data_bp[i].ctrl), &sbpc,
1272		       sizeof(struct swapped_m88110_bp_ctrl));
1273	    }
1274
1275	    spsr.u.fields.mxm_dis = spu->psr.mxm_dis;
1276	    spsr.u.fields.sfu1dis = spu->psr.sfu1dis;
1277	    spsr.u.fields.trace = spu->psr.trace;
1278	    spsr.u.fields.sm = spu->psr.sm;
1279	    spsr.u.fields.sgn_imd = spu->psr.sgn_imd;
1280	    spsr.u.fields.c = spu->psr.c;
1281	    spsr.u.fields.se = spu->psr.se;
1282	    spsr.u.fields.le = spu->psr.le;
1283	    spsr.u.fields.supr = spu->psr.supr;
1284	    spsr.u.word = SWAP_INT(spsr.u.word);
1285	    memcpy(&(spu->psr), &spsr, sizeof(struct swap_m88110_psr));
1286
1287	    sfps.u.fields.efinx = spu->fp_trap_status.efinx;
1288	    sfps.u.fields.efovf = spu->fp_trap_status.efovf;
1289	    sfps.u.fields.efunf = spu->fp_trap_status.efunf;
1290	    sfps.u.fields.efdvz = spu->fp_trap_status.efdvz;
1291	    sfps.u.fields.efinv = spu->fp_trap_status.efinv;
1292	    sfps.u.fields.priv = spu->fp_trap_status.priv;
1293	    sfps.u.fields.unimp = spu->fp_trap_status.unimp;
1294	    sfps.u.fields.sfu1_disabled = spu->fp_trap_status.sfu1_disabled;
1295	    sfps.u.fields.iresult_size = spu->fp_trap_status.iresult_size;
1296	    sfps.u.word = SWAP_INT(sfps.u.word);
1297	    memcpy(&(spu->fp_trap_status), &sfps,
1298		   sizeof(struct swapped_m88110_fp_trap_status));
1299	}
1300	spu->intermediate_result.x[0] =
1301	    SWAP_INT(spu->intermediate_result.x[0]);
1302	spu->intermediate_result.x[1] =
1303	    SWAP_INT(spu->intermediate_result.x[1]);
1304	spu->intermediate_result.x[2] =
1305	    SWAP_INT(spu->intermediate_result.x[2]);
1306	spu->intermediate_result.x[3] =
1307	    SWAP_INT(spu->intermediate_result.x[3]);
1308}
1309
1310__private_extern__
1311void
1312swap_i860_thread_state_regs(
1313struct i860_thread_state_regs *cpu,
1314enum byte_sex target_byte_sex)
1315{
1316    uint32_t i;
1317#ifdef __MWERKS__
1318    enum byte_sex dummy;
1319        dummy = target_byte_sex;
1320#endif
1321
1322	for(i = 0; i < 31; i++)
1323	    cpu->ireg[i] = SWAP_INT(cpu->ireg[i]);
1324	for(i = 0; i < 30; i++)
1325	    cpu->freg[i] = SWAP_INT(cpu->freg[i]);
1326	cpu->psr = SWAP_INT(cpu->psr);
1327	cpu->epsr = SWAP_INT(cpu->epsr);
1328	cpu->db = SWAP_INT(cpu->db);
1329	cpu->pc = SWAP_INT(cpu->pc);
1330	cpu->_padding_ = SWAP_INT(cpu->_padding_);
1331	cpu->Mres3 = SWAP_DOUBLE(cpu->Mres3);
1332	cpu->Ares3 = SWAP_DOUBLE(cpu->Ares3);
1333	cpu->Mres2 = SWAP_DOUBLE(cpu->Mres2);
1334	cpu->Ares2 = SWAP_DOUBLE(cpu->Ares2);
1335	cpu->Mres1 = SWAP_DOUBLE(cpu->Mres1);
1336	cpu->Ares1 = SWAP_DOUBLE(cpu->Ares1);
1337	cpu->Ires1 = SWAP_DOUBLE(cpu->Ires1);
1338	cpu->Lres3m = SWAP_DOUBLE(cpu->Lres3m);
1339	cpu->Lres2m = SWAP_DOUBLE(cpu->Lres2m);
1340	cpu->Lres1m = SWAP_DOUBLE(cpu->Lres1m);
1341	cpu->KR = SWAP_DOUBLE(cpu->KR);
1342	cpu->KI = SWAP_DOUBLE(cpu->KI);
1343	cpu->T = SWAP_DOUBLE(cpu->T);
1344	cpu->Fsr3 = SWAP_INT(cpu->Fsr3);
1345	cpu->Fsr2 = SWAP_INT(cpu->Fsr2);
1346	cpu->Fsr1 = SWAP_INT(cpu->Fsr1);
1347	cpu->Mergelo32 = SWAP_INT(cpu->Mergelo32);
1348	cpu->Mergehi32 = SWAP_INT(cpu->Mergehi32);
1349}
1350
1351__private_extern__
1352void
1353swap_i386_thread_state(
1354i386_thread_state_t *cpu,
1355enum byte_sex target_byte_sex)
1356{
1357#ifdef __MWERKS__
1358    enum byte_sex dummy;
1359        dummy = target_byte_sex;
1360#endif
1361	cpu->eax = SWAP_INT(cpu->eax);
1362	cpu->ebx = SWAP_INT(cpu->ebx);
1363	cpu->ecx = SWAP_INT(cpu->ecx);
1364	cpu->edx = SWAP_INT(cpu->edx);
1365	cpu->edi = SWAP_INT(cpu->edi);
1366	cpu->esi = SWAP_INT(cpu->esi);
1367	cpu->ebp = SWAP_INT(cpu->ebp);
1368	cpu->esp = SWAP_INT(cpu->esp);
1369	cpu->ss = SWAP_INT(cpu->ss);
1370	cpu->eflags = SWAP_INT(cpu->eflags);
1371	cpu->eip = SWAP_INT(cpu->eip);
1372	cpu->cs = SWAP_INT(cpu->cs);
1373	cpu->ds = SWAP_INT(cpu->ds);
1374	cpu->es = SWAP_INT(cpu->es);
1375	cpu->fs = SWAP_INT(cpu->fs);
1376	cpu->gs = SWAP_INT(cpu->gs);
1377}
1378
1379#ifdef x86_THREAD_STATE64
1380__private_extern__
1381void
1382swap_x86_thread_state64(
1383x86_thread_state64_t *cpu,
1384enum byte_sex target_byte_sex)
1385{
1386	cpu->rax = SWAP_LONG_LONG(cpu->rax);
1387	cpu->rbx = SWAP_LONG_LONG(cpu->rbx);
1388	cpu->rcx = SWAP_LONG_LONG(cpu->rcx);
1389	cpu->rdx = SWAP_LONG_LONG(cpu->rdx);
1390	cpu->rdi = SWAP_LONG_LONG(cpu->rdi);
1391	cpu->rsi = SWAP_LONG_LONG(cpu->rsi);
1392	cpu->rbp = SWAP_LONG_LONG(cpu->rbp);
1393	cpu->rsp = SWAP_LONG_LONG(cpu->rsp);
1394	cpu->rflags = SWAP_LONG_LONG(cpu->rflags);
1395	cpu->rip = SWAP_LONG_LONG(cpu->rip);
1396	cpu->r8 = SWAP_LONG_LONG(cpu->r8);
1397	cpu->r9 = SWAP_LONG_LONG(cpu->r9);
1398	cpu->r10 = SWAP_LONG_LONG(cpu->r10);
1399	cpu->r11 = SWAP_LONG_LONG(cpu->r11);
1400	cpu->r12 = SWAP_LONG_LONG(cpu->r12);
1401	cpu->r13 = SWAP_LONG_LONG(cpu->r13);
1402	cpu->r14 = SWAP_LONG_LONG(cpu->r14);
1403	cpu->r15 = SWAP_LONG_LONG(cpu->r15);
1404	cpu->cs = SWAP_LONG_LONG(cpu->cs);
1405	cpu->fs = SWAP_LONG_LONG(cpu->fs);
1406	cpu->gs = SWAP_LONG_LONG(cpu->gs);
1407}
1408#endif /* x86_THREAD_STATE64 */
1409
1410/* current i386 thread states */
1411#if i386_THREAD_STATE == 1
1412__private_extern__
1413void
1414swap_i386_float_state(
1415struct i386_float_state *fpu,
1416enum byte_sex target_byte_sex)
1417{
1418#ifndef i386_EXCEPTION_STATE_COUNT
1419    /* this routine does nothing as their are currently no non-byte fields */
1420#else /* defined(i386_EXCEPTION_STATE_COUNT) */
1421    struct swapped_fp_control {
1422	union {
1423	    struct {
1424		unsigned short
1425			    :3,
1426		    /*inf*/ :1,
1427		    rc	    :2,
1428		    pc	    :2,
1429			    :2,
1430		    precis  :1,
1431		    undfl   :1,
1432		    ovrfl   :1,
1433		    zdiv    :1,
1434		    denorm  :1,
1435		    invalid :1;
1436	    } fields;
1437	    unsigned short half;
1438	} u;
1439    } sfpc;
1440
1441    struct swapped_fp_status {
1442	union {
1443	    struct {
1444		unsigned short
1445		    busy    :1,
1446		    c3	    :1,
1447		    tos	    :3,
1448		    c2	    :1,
1449		    c1	    :1,
1450		    c0	    :1,
1451		    errsumm :1,
1452		    stkflt  :1,
1453		    precis  :1,
1454		    undfl   :1,
1455		    ovrfl   :1,
1456		    zdiv    :1,
1457		    denorm  :1,
1458		    invalid :1;
1459	    } fields;
1460	    unsigned short half;
1461	} u;
1462    } sfps;
1463
1464    enum byte_sex host_byte_sex;
1465
1466	host_byte_sex = get_host_byte_sex();
1467
1468	fpu->fpu_reserved[0] = SWAP_INT(fpu->fpu_reserved[0]);
1469	fpu->fpu_reserved[1] = SWAP_INT(fpu->fpu_reserved[1]);
1470
1471	if(target_byte_sex == host_byte_sex){
1472	    memcpy(&sfpc, &(fpu->fpu_fcw),
1473		   sizeof(struct swapped_fp_control));
1474	    sfpc.u.half = SWAP_SHORT(sfpc.u.half);
1475	    fpu->fpu_fcw.rc = sfpc.u.fields.rc;
1476	    fpu->fpu_fcw.pc = sfpc.u.fields.pc;
1477	    fpu->fpu_fcw.precis = sfpc.u.fields.precis;
1478	    fpu->fpu_fcw.undfl = sfpc.u.fields.undfl;
1479	    fpu->fpu_fcw.ovrfl = sfpc.u.fields.ovrfl;
1480	    fpu->fpu_fcw.zdiv = sfpc.u.fields.zdiv;
1481	    fpu->fpu_fcw.denorm = sfpc.u.fields.denorm;
1482	    fpu->fpu_fcw.invalid = sfpc.u.fields.invalid;
1483
1484	    memcpy(&sfps, &(fpu->fpu_fsw),
1485		   sizeof(struct swapped_fp_status));
1486	    sfps.u.half = SWAP_SHORT(sfps.u.half);
1487	    fpu->fpu_fsw.busy = sfps.u.fields.busy;
1488	    fpu->fpu_fsw.c3 = sfps.u.fields.c3;
1489	    fpu->fpu_fsw.tos = sfps.u.fields.tos;
1490	    fpu->fpu_fsw.c2 = sfps.u.fields.c2;
1491	    fpu->fpu_fsw.c1 = sfps.u.fields.c1;
1492	    fpu->fpu_fsw.c0 = sfps.u.fields.c0;
1493	    fpu->fpu_fsw.errsumm = sfps.u.fields.errsumm;
1494	    fpu->fpu_fsw.stkflt = sfps.u.fields.stkflt;
1495	    fpu->fpu_fsw.precis = sfps.u.fields.precis;
1496	    fpu->fpu_fsw.undfl = sfps.u.fields.undfl;
1497	    fpu->fpu_fsw.ovrfl = sfps.u.fields.ovrfl;
1498	    fpu->fpu_fsw.zdiv = sfps.u.fields.zdiv;
1499	    fpu->fpu_fsw.denorm = sfps.u.fields.denorm;
1500	    fpu->fpu_fsw.invalid = sfps.u.fields.invalid;
1501	}
1502	else{
1503	    sfpc.u.fields.rc = fpu->fpu_fcw.rc;
1504	    sfpc.u.fields.pc = fpu->fpu_fcw.pc;
1505	    sfpc.u.fields.precis = fpu->fpu_fcw.precis;
1506	    sfpc.u.fields.undfl = fpu->fpu_fcw.undfl;
1507	    sfpc.u.fields.ovrfl = fpu->fpu_fcw.ovrfl;
1508	    sfpc.u.fields.zdiv = fpu->fpu_fcw.zdiv;
1509	    sfpc.u.fields.denorm = fpu->fpu_fcw.denorm;
1510	    sfpc.u.fields.invalid = fpu->fpu_fcw.invalid;
1511	    sfpc.u.half = SWAP_SHORT(sfpc.u.half);
1512	    memcpy(&(fpu->fpu_fcw), &sfpc,
1513		   sizeof(struct swapped_fp_control));
1514
1515	    sfps.u.fields.busy = fpu->fpu_fsw.busy;
1516	    sfps.u.fields.c3 = fpu->fpu_fsw.c3;
1517	    sfps.u.fields.tos = fpu->fpu_fsw.tos;
1518	    sfps.u.fields.c2 = fpu->fpu_fsw.c2;
1519	    sfps.u.fields.c1 = fpu->fpu_fsw.c1;
1520	    sfps.u.fields.c0 = fpu->fpu_fsw.c0;
1521	    sfps.u.fields.errsumm = fpu->fpu_fsw.errsumm;
1522	    sfps.u.fields.stkflt = fpu->fpu_fsw.stkflt;
1523	    sfps.u.fields.precis = fpu->fpu_fsw.precis;
1524	    sfps.u.fields.undfl = fpu->fpu_fsw.undfl;
1525	    sfps.u.fields.ovrfl = fpu->fpu_fsw.ovrfl;
1526	    sfps.u.fields.zdiv = fpu->fpu_fsw.zdiv;
1527	    sfps.u.fields.denorm = fpu->fpu_fsw.denorm;
1528	    sfps.u.fields.invalid = fpu->fpu_fsw.invalid;
1529	    sfps.u.half = SWAP_SHORT(sfps.u.half);
1530	    memcpy(&(fpu->fpu_fsw), &sfps,
1531		   sizeof(struct swapped_fp_status));
1532	}
1533	fpu->fpu_fop = SWAP_SHORT(fpu->fpu_fop);
1534	fpu->fpu_ip = SWAP_INT(fpu->fpu_ip);
1535	fpu->fpu_cs = SWAP_SHORT(fpu->fpu_cs);
1536	fpu->fpu_rsrv2 = SWAP_SHORT(fpu->fpu_rsrv2);
1537	fpu->fpu_dp = SWAP_INT(fpu->fpu_dp);
1538	fpu->fpu_ds = SWAP_SHORT(fpu->fpu_ds);
1539	fpu->fpu_rsrv3 = SWAP_SHORT(fpu->fpu_rsrv3);
1540	fpu->fpu_mxcsr = SWAP_INT(fpu->fpu_mxcsr);
1541	fpu->fpu_mxcsrmask = SWAP_INT(fpu->fpu_mxcsrmask);
1542	fpu->fpu_reserved1 = SWAP_INT(fpu->fpu_reserved1);
1543
1544#endif /* defined(i386_EXCEPTION_STATE_COUNT) */
1545}
1546
1547__private_extern__
1548void
1549swap_i386_exception_state(
1550i386_exception_state_t *exc,
1551enum byte_sex target_byte_sex)
1552{
1553	exc->trapno = SWAP_INT(exc->trapno);
1554	exc->err = SWAP_INT(exc->err);
1555    	exc->faultvaddr = SWAP_INT(exc->faultvaddr);
1556}
1557
1558#ifdef x86_THREAD_STATE64
1559
1560__private_extern__
1561void
1562swap_x86_float_state64(
1563x86_float_state64_t *fpu,
1564enum byte_sex target_byte_sex)
1565{
1566    struct swapped_fp_control {
1567	union {
1568	    struct {
1569		unsigned short
1570			    :3,
1571		    /*inf*/ :1,
1572		    rc	    :2,
1573		    pc	    :2,
1574			    :2,
1575		    precis  :1,
1576		    undfl   :1,
1577		    ovrfl   :1,
1578		    zdiv    :1,
1579		    denorm  :1,
1580		    invalid :1;
1581	    } fields;
1582	    unsigned short half;
1583	} u;
1584    } sfpc;
1585
1586    struct swapped_fp_status {
1587	union {
1588	    struct {
1589		unsigned short
1590		    busy    :1,
1591		    c3	    :1,
1592		    tos	    :3,
1593		    c2	    :1,
1594		    c1	    :1,
1595		    c0	    :1,
1596		    errsumm :1,
1597		    stkflt  :1,
1598		    precis  :1,
1599		    undfl   :1,
1600		    ovrfl   :1,
1601		    zdiv    :1,
1602		    denorm  :1,
1603		    invalid :1;
1604	    } fields;
1605	    unsigned short half;
1606	} u;
1607    } sfps;
1608
1609    enum byte_sex host_byte_sex;
1610
1611	host_byte_sex = get_host_byte_sex();
1612
1613	fpu->fpu_reserved[0] = SWAP_INT(fpu->fpu_reserved[0]);
1614	fpu->fpu_reserved[1] = SWAP_INT(fpu->fpu_reserved[1]);
1615
1616	if(target_byte_sex == host_byte_sex){
1617	    memcpy(&sfpc, &(fpu->fpu_fcw),
1618		   sizeof(struct swapped_fp_control));
1619	    sfpc.u.half = SWAP_SHORT(sfpc.u.half);
1620	    fpu->fpu_fcw.rc = sfpc.u.fields.rc;
1621	    fpu->fpu_fcw.pc = sfpc.u.fields.pc;
1622	    fpu->fpu_fcw.precis = sfpc.u.fields.precis;
1623	    fpu->fpu_fcw.undfl = sfpc.u.fields.undfl;
1624	    fpu->fpu_fcw.ovrfl = sfpc.u.fields.ovrfl;
1625	    fpu->fpu_fcw.zdiv = sfpc.u.fields.zdiv;
1626	    fpu->fpu_fcw.denorm = sfpc.u.fields.denorm;
1627	    fpu->fpu_fcw.invalid = sfpc.u.fields.invalid;
1628
1629	    memcpy(&sfps, &(fpu->fpu_fsw),
1630		   sizeof(struct swapped_fp_status));
1631	    sfps.u.half = SWAP_SHORT(sfps.u.half);
1632	    fpu->fpu_fsw.busy = sfps.u.fields.busy;
1633	    fpu->fpu_fsw.c3 = sfps.u.fields.c3;
1634	    fpu->fpu_fsw.tos = sfps.u.fields.tos;
1635	    fpu->fpu_fsw.c2 = sfps.u.fields.c2;
1636	    fpu->fpu_fsw.c1 = sfps.u.fields.c1;
1637	    fpu->fpu_fsw.c0 = sfps.u.fields.c0;
1638	    fpu->fpu_fsw.errsumm = sfps.u.fields.errsumm;
1639	    fpu->fpu_fsw.stkflt = sfps.u.fields.stkflt;
1640	    fpu->fpu_fsw.precis = sfps.u.fields.precis;
1641	    fpu->fpu_fsw.undfl = sfps.u.fields.undfl;
1642	    fpu->fpu_fsw.ovrfl = sfps.u.fields.ovrfl;
1643	    fpu->fpu_fsw.zdiv = sfps.u.fields.zdiv;
1644	    fpu->fpu_fsw.denorm = sfps.u.fields.denorm;
1645	    fpu->fpu_fsw.invalid = sfps.u.fields.invalid;
1646	}
1647	else{
1648	    sfpc.u.fields.rc = fpu->fpu_fcw.rc;
1649	    sfpc.u.fields.pc = fpu->fpu_fcw.pc;
1650	    sfpc.u.fields.precis = fpu->fpu_fcw.precis;
1651	    sfpc.u.fields.undfl = fpu->fpu_fcw.undfl;
1652	    sfpc.u.fields.ovrfl = fpu->fpu_fcw.ovrfl;
1653	    sfpc.u.fields.zdiv = fpu->fpu_fcw.zdiv;
1654	    sfpc.u.fields.denorm = fpu->fpu_fcw.denorm;
1655	    sfpc.u.fields.invalid = fpu->fpu_fcw.invalid;
1656	    sfpc.u.half = SWAP_SHORT(sfpc.u.half);
1657	    memcpy(&(fpu->fpu_fcw), &sfpc,
1658		   sizeof(struct swapped_fp_control));
1659
1660	    sfps.u.fields.busy = fpu->fpu_fsw.busy;
1661	    sfps.u.fields.c3 = fpu->fpu_fsw.c3;
1662	    sfps.u.fields.tos = fpu->fpu_fsw.tos;
1663	    sfps.u.fields.c2 = fpu->fpu_fsw.c2;
1664	    sfps.u.fields.c1 = fpu->fpu_fsw.c1;
1665	    sfps.u.fields.c0 = fpu->fpu_fsw.c0;
1666	    sfps.u.fields.errsumm = fpu->fpu_fsw.errsumm;
1667	    sfps.u.fields.stkflt = fpu->fpu_fsw.stkflt;
1668	    sfps.u.fields.precis = fpu->fpu_fsw.precis;
1669	    sfps.u.fields.undfl = fpu->fpu_fsw.undfl;
1670	    sfps.u.fields.ovrfl = fpu->fpu_fsw.ovrfl;
1671	    sfps.u.fields.zdiv = fpu->fpu_fsw.zdiv;
1672	    sfps.u.fields.denorm = fpu->fpu_fsw.denorm;
1673	    sfps.u.fields.invalid = fpu->fpu_fsw.invalid;
1674	    sfps.u.half = SWAP_SHORT(sfps.u.half);
1675	    memcpy(&(fpu->fpu_fsw), &sfps,
1676		   sizeof(struct swapped_fp_status));
1677	}
1678	fpu->fpu_fop = SWAP_SHORT(fpu->fpu_fop);
1679	fpu->fpu_ip = SWAP_INT(fpu->fpu_ip);
1680	fpu->fpu_cs = SWAP_SHORT(fpu->fpu_cs);
1681	fpu->fpu_rsrv2 = SWAP_SHORT(fpu->fpu_rsrv2);
1682	fpu->fpu_dp = SWAP_INT(fpu->fpu_dp);
1683	fpu->fpu_ds = SWAP_SHORT(fpu->fpu_ds);
1684	fpu->fpu_rsrv3 = SWAP_SHORT(fpu->fpu_rsrv3);
1685	fpu->fpu_mxcsr = SWAP_INT(fpu->fpu_mxcsr);
1686	fpu->fpu_mxcsrmask = SWAP_INT(fpu->fpu_mxcsrmask);
1687	fpu->fpu_reserved1 = SWAP_INT(fpu->fpu_reserved1);
1688}
1689
1690__private_extern__
1691void
1692swap_x86_exception_state64(
1693x86_exception_state64_t *exc,
1694enum byte_sex target_byte_sex)
1695{
1696	exc->trapno = SWAP_INT(exc->trapno);
1697	exc->err = SWAP_INT(exc->err);
1698    	exc->faultvaddr = SWAP_LONG_LONG(exc->faultvaddr);
1699}
1700
1701__private_extern__
1702void
1703swap_x86_debug_state32(
1704x86_debug_state32_t *debug,
1705enum byte_sex target_byte_sex)
1706{
1707	debug->dr0 = SWAP_INT(debug->dr0);
1708	debug->dr1 = SWAP_INT(debug->dr1);
1709	debug->dr2 = SWAP_INT(debug->dr2);
1710	debug->dr3 = SWAP_INT(debug->dr3);
1711	debug->dr4 = SWAP_INT(debug->dr4);
1712	debug->dr5 = SWAP_INT(debug->dr5);
1713	debug->dr6 = SWAP_INT(debug->dr6);
1714	debug->dr7 = SWAP_INT(debug->dr7);
1715}
1716
1717__private_extern__
1718void
1719swap_x86_debug_state64(
1720x86_debug_state64_t *debug,
1721enum byte_sex target_byte_sex)
1722{
1723	debug->dr0 = SWAP_LONG_LONG(debug->dr0);
1724	debug->dr1 = SWAP_LONG_LONG(debug->dr1);
1725	debug->dr2 = SWAP_LONG_LONG(debug->dr2);
1726	debug->dr3 = SWAP_LONG_LONG(debug->dr3);
1727	debug->dr4 = SWAP_LONG_LONG(debug->dr4);
1728	debug->dr5 = SWAP_LONG_LONG(debug->dr5);
1729	debug->dr6 = SWAP_LONG_LONG(debug->dr6);
1730	debug->dr7 = SWAP_LONG_LONG(debug->dr7);
1731}
1732
1733__private_extern__
1734void
1735swap_x86_state_hdr(
1736struct x86_state_hdr *hdr,
1737enum byte_sex target_byte_sex)
1738{
1739	hdr->flavor = SWAP_INT(hdr->flavor);
1740	hdr->count = SWAP_INT(hdr->count);
1741}
1742#endif /* x86_THREAD_STATE64 */
1743
1744#endif /* i386_THREAD_STATE == 1 */
1745
1746/* i386 thread states on older releases */
1747#if i386_THREAD_STATE == -1
1748__private_extern__
1749void
1750swap_i386_thread_fpstate(
1751i386_thread_fpstate_t *fpu,
1752enum byte_sex target_byte_sex)
1753{
1754    struct swapped_fp_control {
1755	union {
1756	    struct {
1757		unsigned short
1758			    :3,
1759		    /*inf*/ :1,
1760		    rc	    :2,
1761		    pc	    :2,
1762			    :2,
1763		    precis  :1,
1764		    undfl   :1,
1765		    ovrfl   :1,
1766		    zdiv    :1,
1767		    denorm  :1,
1768		    invalid :1;
1769	    } fields;
1770	    unsigned short half;
1771	} u;
1772    } sfpc;
1773
1774    struct swapped_fp_status {
1775	union {
1776	    struct {
1777		unsigned short
1778		    busy    :1,
1779		    c3	    :1,
1780		    tos	    :3,
1781		    c2	    :1,
1782		    c1	    :1,
1783		    c0	    :1,
1784		    errsumm :1,
1785		    stkflt  :1,
1786		    precis  :1,
1787		    undfl   :1,
1788		    ovrfl   :1,
1789		    zdiv    :1,
1790		    denorm  :1,
1791		    invalid :1;
1792	    } fields;
1793	    unsigned short half;
1794	} u;
1795    } sfps;
1796
1797    struct swapped_fp_tag {
1798	union {
1799	    struct {
1800		unsigned short
1801		    tag7 :2,
1802		    tag6 :2,
1803		    tag5 :2,
1804		    tag4 :2,
1805		    tag3 :2,
1806		    tag2 :2,
1807		    tag1 :2,
1808		    tag0 :2;
1809	    } fields;
1810	    unsigned short half;
1811	} u;
1812    } sfpt;
1813
1814    struct swapped_fp_data_reg {
1815	unsigned short mant;
1816	unsigned short mant1 :16,
1817		       mant2 :16,
1818		       mant3 :16;
1819	union {
1820	    struct {
1821		unsigned short sign :1,
1822			       exp  :15;
1823	    } fields;
1824	    unsigned short half;
1825	} u;
1826    } sfpd;
1827
1828    struct swapped_sel {
1829	union {
1830	    struct {
1831    		unsigned short
1832		    index :13,
1833		    ti    :1,
1834		    rpl   :2;
1835	    } fields;
1836	    unsigned short half;
1837	} u;
1838    } ss;
1839
1840    enum byte_sex host_byte_sex;
1841    uint32_t i;
1842
1843	host_byte_sex = get_host_byte_sex();
1844
1845	fpu->environ.ip = SWAP_INT(fpu->environ.ip);
1846	fpu->environ.opcode = SWAP_SHORT(fpu->environ.opcode);
1847	fpu->environ.dp = SWAP_INT(fpu->environ.dp);
1848
1849	if(target_byte_sex == host_byte_sex){
1850	    memcpy(&sfpc, &(fpu->environ.control),
1851		   sizeof(struct swapped_fp_control));
1852	    sfpc.u.half = SWAP_SHORT(sfpc.u.half);
1853	    fpu->environ.control.rc = sfpc.u.fields.rc;
1854	    fpu->environ.control.pc = sfpc.u.fields.pc;
1855	    fpu->environ.control.precis = sfpc.u.fields.precis;
1856	    fpu->environ.control.undfl = sfpc.u.fields.undfl;
1857	    fpu->environ.control.ovrfl = sfpc.u.fields.ovrfl;
1858	    fpu->environ.control.zdiv = sfpc.u.fields.zdiv;
1859	    fpu->environ.control.denorm = sfpc.u.fields.denorm;
1860	    fpu->environ.control.invalid = sfpc.u.fields.invalid;
1861
1862	    memcpy(&sfps, &(fpu->environ.status),
1863		   sizeof(struct swapped_fp_status));
1864	    sfps.u.half = SWAP_SHORT(sfps.u.half);
1865	    fpu->environ.status.busy = sfps.u.fields.busy;
1866	    fpu->environ.status.c3 = sfps.u.fields.c3;
1867	    fpu->environ.status.tos = sfps.u.fields.tos;
1868	    fpu->environ.status.c2 = sfps.u.fields.c2;
1869	    fpu->environ.status.c1 = sfps.u.fields.c1;
1870	    fpu->environ.status.c0 = sfps.u.fields.c0;
1871	    fpu->environ.status.errsumm = sfps.u.fields.errsumm;
1872	    fpu->environ.status.stkflt = sfps.u.fields.stkflt;
1873	    fpu->environ.status.precis = sfps.u.fields.precis;
1874	    fpu->environ.status.undfl = sfps.u.fields.undfl;
1875	    fpu->environ.status.ovrfl = sfps.u.fields.ovrfl;
1876	    fpu->environ.status.zdiv = sfps.u.fields.zdiv;
1877	    fpu->environ.status.denorm = sfps.u.fields.denorm;
1878	    fpu->environ.status.invalid = sfps.u.fields.invalid;
1879
1880	    memcpy(&sfpt, &(fpu->environ.tag),
1881		   sizeof(struct swapped_fp_tag));
1882	    sfpt.u.half = SWAP_SHORT(sfpt.u.half);
1883	    fpu->environ.tag.tag7 = sfpt.u.fields.tag7;
1884	    fpu->environ.tag.tag6 = sfpt.u.fields.tag6;
1885	    fpu->environ.tag.tag5 = sfpt.u.fields.tag5;
1886	    fpu->environ.tag.tag4 = sfpt.u.fields.tag4;
1887	    fpu->environ.tag.tag3 = sfpt.u.fields.tag3;
1888	    fpu->environ.tag.tag2 = sfpt.u.fields.tag2;
1889	    fpu->environ.tag.tag1 = sfpt.u.fields.tag1;
1890	    fpu->environ.tag.tag0 = sfpt.u.fields.tag0;
1891
1892	    memcpy(&ss, &(fpu->environ.cs),
1893		   sizeof(struct swapped_sel));
1894	    ss.u.half = SWAP_SHORT(ss.u.half);
1895	    fpu->environ.cs.index = ss.u.fields.index;
1896	    fpu->environ.cs.ti = ss.u.fields.ti;
1897	    fpu->environ.cs.rpl = ss.u.fields.rpl;
1898
1899	    memcpy(&ss, &(fpu->environ.ds),
1900		   sizeof(struct swapped_sel));
1901	    ss.u.half = SWAP_SHORT(ss.u.half);
1902	    fpu->environ.ds.index = ss.u.fields.index;
1903	    fpu->environ.ds.ti = ss.u.fields.ti;
1904	    fpu->environ.ds.rpl = ss.u.fields.rpl;
1905
1906	    for(i = 0; i < 8; i++){
1907		memcpy(&sfpd, &(fpu->stack.ST[i]),
1908		       sizeof(struct swapped_fp_data_reg));
1909		fpu->stack.ST[i].mant = SWAP_SHORT(sfpd.mant);
1910		fpu->stack.ST[i].mant1 = SWAP_SHORT(sfpd.mant1);
1911		fpu->stack.ST[i].mant2 = SWAP_SHORT(sfpd.mant2);
1912		fpu->stack.ST[i].mant3 = SWAP_SHORT(sfpd.mant3);
1913		sfpd.u.half = SWAP_SHORT(sfpd.u.half);
1914		fpu->stack.ST[i].exp = sfpd.u.fields.exp;
1915		fpu->stack.ST[i].sign = sfpd.u.fields.sign;
1916	    }
1917	}
1918	else{
1919	    sfpc.u.fields.rc = fpu->environ.control.rc;
1920	    sfpc.u.fields.pc = fpu->environ.control.pc;
1921	    sfpc.u.fields.precis = fpu->environ.control.precis;
1922	    sfpc.u.fields.undfl = fpu->environ.control.undfl;
1923	    sfpc.u.fields.ovrfl = fpu->environ.control.ovrfl;
1924	    sfpc.u.fields.zdiv = fpu->environ.control.zdiv;
1925	    sfpc.u.fields.denorm = fpu->environ.control.denorm;
1926	    sfpc.u.fields.invalid = fpu->environ.control.invalid;
1927	    sfpc.u.half = SWAP_SHORT(sfpc.u.half);
1928	    memcpy(&(fpu->environ.control), &sfpc,
1929		   sizeof(struct swapped_fp_control));
1930
1931	    sfps.u.fields.busy = fpu->environ.status.busy;
1932	    sfps.u.fields.c3 = fpu->environ.status.c3;
1933	    sfps.u.fields.tos = fpu->environ.status.tos;
1934	    sfps.u.fields.c2 = fpu->environ.status.c2;
1935	    sfps.u.fields.c1 = fpu->environ.status.c1;
1936	    sfps.u.fields.c0 = fpu->environ.status.c0;
1937	    sfps.u.fields.errsumm = fpu->environ.status.errsumm;
1938	    sfps.u.fields.stkflt = fpu->environ.status.stkflt;
1939	    sfps.u.fields.precis = fpu->environ.status.precis;
1940	    sfps.u.fields.undfl = fpu->environ.status.undfl;
1941	    sfps.u.fields.ovrfl = fpu->environ.status.ovrfl;
1942	    sfps.u.fields.zdiv = fpu->environ.status.zdiv;
1943	    sfps.u.fields.denorm = fpu->environ.status.denorm;
1944	    sfps.u.fields.invalid = fpu->environ.status.invalid;
1945	    sfps.u.half = SWAP_SHORT(sfps.u.half);
1946	    memcpy(&(fpu->environ.status), &sfps,
1947		   sizeof(struct swapped_fp_status));
1948
1949	    sfpt.u.fields.tag7 = fpu->environ.tag.tag7;
1950	    sfpt.u.fields.tag6 = fpu->environ.tag.tag6;
1951	    sfpt.u.fields.tag5 = fpu->environ.tag.tag5;
1952	    sfpt.u.fields.tag4 = fpu->environ.tag.tag4;
1953	    sfpt.u.fields.tag3 = fpu->environ.tag.tag3;
1954	    sfpt.u.fields.tag2 = fpu->environ.tag.tag2;
1955	    sfpt.u.fields.tag1 = fpu->environ.tag.tag1;
1956	    sfpt.u.fields.tag0 = fpu->environ.tag.tag0;
1957	    sfpt.u.half = SWAP_SHORT(sfpt.u.half);
1958	    memcpy(&(fpu->environ.tag), &sfpt,
1959		   sizeof(struct swapped_fp_tag));
1960
1961	    ss.u.fields.index = fpu->environ.cs.index;
1962	    ss.u.fields.ti = fpu->environ.cs.ti;
1963	    ss.u.fields.rpl = fpu->environ.cs.rpl;
1964	    ss.u.half = SWAP_SHORT(ss.u.half);
1965	    memcpy(&(fpu->environ.cs), &ss,
1966		   sizeof(struct swapped_sel));
1967
1968	    ss.u.fields.index = fpu->environ.ds.index;
1969	    ss.u.fields.ti = fpu->environ.ds.ti;
1970	    ss.u.fields.rpl = fpu->environ.ds.rpl;
1971	    ss.u.half = SWAP_SHORT(ss.u.half);
1972	    memcpy(&(fpu->environ.cs), &ss,
1973		   sizeof(struct swapped_sel));
1974
1975	    for(i = 0; i < 8; i++){
1976		sfpd.mant = SWAP_SHORT(fpu->stack.ST[i].mant);
1977		sfpd.mant1 = SWAP_SHORT(fpu->stack.ST[i].mant1);
1978		sfpd.mant2 = SWAP_SHORT(fpu->stack.ST[i].mant2);
1979		sfpd.mant3 = SWAP_SHORT(fpu->stack.ST[i].mant3);
1980		sfpd.u.fields.exp = fpu->stack.ST[i].exp;
1981		sfpd.u.fields.sign = fpu->stack.ST[i].sign;
1982		sfpd.u.half = SWAP_SHORT(sfpd.u.half);
1983		memcpy(&(fpu->stack.ST[i]), &sfpd,
1984		       sizeof(struct swapped_fp_data_reg));
1985	    }
1986	}
1987}
1988
1989__private_extern__
1990void
1991swap_i386_thread_exceptstate(
1992i386_thread_exceptstate_t *exc,
1993enum byte_sex target_byte_sex)
1994{
1995    struct swapped_err_code {
1996	union {
1997	    struct err_code_normal {
1998		unsigned int		:16,
1999				index	:13,
2000				tbl	:2,
2001				ext	:1;
2002	    } normal;
2003	    struct err_code_pgfault {
2004		unsigned int		:29,
2005				user	:1,
2006				wrtflt	:1,
2007				prot	:1;
2008	    } pgfault;
2009	    uint32_t word;
2010	} u;
2011    } sec;
2012    uint32_t word;
2013    enum byte_sex host_byte_sex;
2014
2015	host_byte_sex = get_host_byte_sex();
2016
2017	exc->trapno = SWAP_INT(exc->trapno);
2018	if(exc->trapno == 14){
2019	    if(target_byte_sex == host_byte_sex){
2020		memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code));
2021		sec.u.word = SWAP_INT(sec.u.word);
2022		exc->err.pgfault.user   = sec.u.pgfault.user;
2023		exc->err.pgfault.wrtflt = sec.u.pgfault.wrtflt;
2024		exc->err.pgfault.prot   = sec.u.pgfault.prot;
2025	    }
2026	    else{
2027		sec.u.pgfault.prot   = exc->err.pgfault.prot;
2028		sec.u.pgfault.wrtflt = exc->err.pgfault.wrtflt;
2029		sec.u.pgfault.user   = exc->err.pgfault.user;
2030		sec.u.word = SWAP_INT(sec.u.word);
2031		memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code));
2032	    }
2033	}
2034	else{
2035	    if(target_byte_sex == host_byte_sex){
2036		memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code));
2037		sec.u.word = SWAP_INT(sec.u.word);
2038		word = sec.u.normal.index;
2039		exc->err.normal.index = SWAP_INT(word);
2040		exc->err.normal.tbl   = sec.u.normal.tbl;
2041		exc->err.normal.ext   = sec.u.normal.ext;
2042	    }
2043	    else{
2044		sec.u.normal.ext   = exc->err.normal.ext;
2045		sec.u.normal.tbl   = exc->err.normal.tbl;
2046		word = exc->err.normal.index;
2047		sec.u.normal.index = SWAP_INT(word);
2048		sec.u.word = SWAP_INT(sec.u.word);
2049		memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code));
2050	    }
2051	}
2052}
2053
2054__private_extern__
2055void
2056swap_i386_thread_cthreadstate(
2057i386_thread_cthreadstate_t *user,
2058enum byte_sex target_byte_sex)
2059{
2060#ifdef __MWERKS__
2061    enum byte_sex dummy;
2062        dummy = target_byte_sex;
2063#endif
2064	user->self = SWAP_INT(user->self);
2065}
2066#endif /* i386_THREAD_STATE == -1 */
2067
2068__private_extern__
2069void
2070swap_hppa_integer_thread_state(
2071struct hp_pa_integer_thread_state *regs,
2072enum byte_sex target_byte_sex)
2073{
2074#ifdef __MWERKS__
2075    enum byte_sex dummy;
2076        dummy = target_byte_sex;
2077#endif
2078	regs->ts_gr1 = SWAP_INT(regs->ts_gr1);
2079	regs->ts_gr2 = SWAP_INT(regs->ts_gr2);
2080	regs->ts_gr3 = SWAP_INT(regs->ts_gr3);
2081	regs->ts_gr4 = SWAP_INT(regs->ts_gr4);
2082	regs->ts_gr5 = SWAP_INT(regs->ts_gr5);
2083	regs->ts_gr6 = SWAP_INT(regs->ts_gr6);
2084	regs->ts_gr7 = SWAP_INT(regs->ts_gr7);
2085	regs->ts_gr8 = SWAP_INT(regs->ts_gr8);
2086	regs->ts_gr9 = SWAP_INT(regs->ts_gr9);
2087	regs->ts_gr10 = SWAP_INT(regs->ts_gr10);
2088	regs->ts_gr11 = SWAP_INT(regs->ts_gr11);
2089	regs->ts_gr12 = SWAP_INT(regs->ts_gr12);
2090	regs->ts_gr13 = SWAP_INT(regs->ts_gr13);
2091	regs->ts_gr14 = SWAP_INT(regs->ts_gr14);
2092	regs->ts_gr15 = SWAP_INT(regs->ts_gr15);
2093	regs->ts_gr16 = SWAP_INT(regs->ts_gr16);
2094	regs->ts_gr17 = SWAP_INT(regs->ts_gr17);
2095	regs->ts_gr18 = SWAP_INT(regs->ts_gr18);
2096	regs->ts_gr19 = SWAP_INT(regs->ts_gr19);
2097	regs->ts_gr20 = SWAP_INT(regs->ts_gr20);
2098	regs->ts_gr21 = SWAP_INT(regs->ts_gr21);
2099	regs->ts_gr22 = SWAP_INT(regs->ts_gr22);
2100	regs->ts_gr23 = SWAP_INT(regs->ts_gr23);
2101	regs->ts_gr24 = SWAP_INT(regs->ts_gr24);
2102	regs->ts_gr25 = SWAP_INT(regs->ts_gr25);
2103	regs->ts_gr26 = SWAP_INT(regs->ts_gr26);
2104	regs->ts_gr27 = SWAP_INT(regs->ts_gr27);
2105	regs->ts_gr28 = SWAP_INT(regs->ts_gr28);
2106	regs->ts_gr29 = SWAP_INT(regs->ts_gr29);
2107	regs->ts_gr30 = SWAP_INT(regs->ts_gr30);
2108	regs->ts_gr31 = SWAP_INT(regs->ts_gr31);
2109	regs->ts_sr0 = SWAP_INT(regs->ts_sr0);
2110	regs->ts_sr1 = SWAP_INT(regs->ts_sr1);
2111	regs->ts_sr2 = SWAP_INT(regs->ts_sr2);
2112	regs->ts_sr3 = SWAP_INT(regs->ts_sr3);
2113	regs->ts_sar = SWAP_INT(regs->ts_sar);
2114}
2115
2116__private_extern__
2117void swap_hppa_frame_thread_state(
2118struct hp_pa_frame_thread_state *frame,
2119enum byte_sex target_byte_sex)
2120{
2121#ifdef __MWERKS__
2122    enum byte_sex dummy;
2123        dummy = target_byte_sex;
2124#endif
2125	frame->ts_pcsq_front = SWAP_INT(frame->ts_pcsq_front);
2126	frame->ts_pcsq_back = SWAP_INT(frame->ts_pcsq_back);
2127	frame->ts_pcoq_front = SWAP_INT(frame->ts_pcoq_front);
2128	frame->ts_pcoq_back = SWAP_INT(frame->ts_pcoq_back);
2129	frame->ts_psw = SWAP_INT(frame->ts_psw);
2130	frame->ts_unaligned_faults = SWAP_INT(frame->ts_unaligned_faults);
2131	frame->ts_fault_address = SWAP_INT(frame->ts_fault_address);
2132	frame->ts_step_range_start = SWAP_INT(frame->ts_step_range_start);
2133	frame->ts_step_range_stop = SWAP_INT(frame->ts_step_range_stop);
2134}
2135
2136__private_extern__
2137void swap_hppa_fp_thread_state(
2138struct hp_pa_fp_thread_state *fp,
2139enum byte_sex target_byte_sex)
2140{
2141#ifdef __MWERKS__
2142    enum byte_sex dummy;
2143        dummy = target_byte_sex;
2144#endif
2145	fp->ts_fp0 = SWAP_DOUBLE(fp->ts_fp0);
2146	fp->ts_fp1 = SWAP_DOUBLE(fp->ts_fp1);
2147	fp->ts_fp2 = SWAP_DOUBLE(fp->ts_fp2);
2148	fp->ts_fp3 = SWAP_DOUBLE(fp->ts_fp3);
2149	fp->ts_fp4 = SWAP_DOUBLE(fp->ts_fp4);
2150	fp->ts_fp5 = SWAP_DOUBLE(fp->ts_fp5);
2151	fp->ts_fp6 = SWAP_DOUBLE(fp->ts_fp6);
2152	fp->ts_fp7 = SWAP_DOUBLE(fp->ts_fp7);
2153	fp->ts_fp8 = SWAP_DOUBLE(fp->ts_fp8);
2154	fp->ts_fp9 = SWAP_DOUBLE(fp->ts_fp9);
2155	fp->ts_fp10 = SWAP_DOUBLE(fp->ts_fp10);
2156	fp->ts_fp11 = SWAP_DOUBLE(fp->ts_fp11);
2157	fp->ts_fp12 = SWAP_DOUBLE(fp->ts_fp12);
2158	fp->ts_fp13 = SWAP_DOUBLE(fp->ts_fp13);
2159	fp->ts_fp14 = SWAP_DOUBLE(fp->ts_fp14);
2160	fp->ts_fp15 = SWAP_DOUBLE(fp->ts_fp15);
2161	fp->ts_fp16 = SWAP_DOUBLE(fp->ts_fp16);
2162	fp->ts_fp17 = SWAP_DOUBLE(fp->ts_fp17);
2163	fp->ts_fp18 = SWAP_DOUBLE(fp->ts_fp18);
2164	fp->ts_fp19 = SWAP_DOUBLE(fp->ts_fp19);
2165	fp->ts_fp20 = SWAP_DOUBLE(fp->ts_fp20);
2166	fp->ts_fp21 = SWAP_DOUBLE(fp->ts_fp21);
2167	fp->ts_fp22 = SWAP_DOUBLE(fp->ts_fp22);
2168	fp->ts_fp23 = SWAP_DOUBLE(fp->ts_fp23);
2169	fp->ts_fp24 = SWAP_DOUBLE(fp->ts_fp24);
2170	fp->ts_fp25 = SWAP_DOUBLE(fp->ts_fp25);
2171	fp->ts_fp26 = SWAP_DOUBLE(fp->ts_fp26);
2172	fp->ts_fp27 = SWAP_DOUBLE(fp->ts_fp27);
2173	fp->ts_fp28 = SWAP_DOUBLE(fp->ts_fp28);
2174	fp->ts_fp29 = SWAP_DOUBLE(fp->ts_fp29);
2175	fp->ts_fp30 = SWAP_DOUBLE(fp->ts_fp30);
2176	fp->ts_fp31 = SWAP_DOUBLE(fp->ts_fp31);
2177}
2178
2179__private_extern__
2180void
2181swap_sparc_thread_state_regs(
2182struct sparc_thread_state_regs *cpu,
2183enum byte_sex target_byte_sex)
2184{
2185    struct swapped_psr {
2186	union {
2187	    struct {
2188		unsigned int
2189		cwp:BITS_WIDTH(4,0),
2190		et:BIT_WIDTH(5),
2191		ps:BIT_WIDTH(6),
2192		s:BIT_WIDTH(7),
2193		pil:BITS_WIDTH(11,8),
2194		ef:BIT_WIDTH(12),
2195		ec:BIT_WIDTH(13),
2196		reserved:BITS_WIDTH(19,14),
2197		icc:BITS_WIDTH(23,20),
2198		ver:BITS_WIDTH(27,24),
2199		impl:BITS_WIDTH(31,28);
2200	    } fields;
2201	    unsigned int word;
2202	} u;
2203    } spsr;
2204    struct p_status *pr_status;
2205    enum byte_sex host_byte_sex;
2206
2207	host_byte_sex = get_host_byte_sex();
2208
2209	cpu->regs.r_pc = SWAP_INT(cpu->regs.r_pc);
2210	cpu->regs.r_npc = SWAP_INT(cpu->regs.r_npc);
2211	cpu->regs.r_y = SWAP_INT(cpu->regs.r_y);
2212	cpu->regs.r_g1 = SWAP_INT(cpu->regs.r_g1);
2213	cpu->regs.r_g2 = SWAP_INT(cpu->regs.r_g2);
2214	cpu->regs.r_g3 = SWAP_INT(cpu->regs.r_g3);
2215	cpu->regs.r_g4 = SWAP_INT(cpu->regs.r_g4);
2216	cpu->regs.r_g5 = SWAP_INT(cpu->regs.r_g5);
2217	cpu->regs.r_g6 = SWAP_INT(cpu->regs.r_g6);
2218	cpu->regs.r_g7 = SWAP_INT(cpu->regs.r_g7);
2219	cpu->regs.r_o0 = SWAP_INT(cpu->regs.r_o0);
2220	cpu->regs.r_o1 = SWAP_INT(cpu->regs.r_o1);
2221	cpu->regs.r_o2 = SWAP_INT(cpu->regs.r_o2);
2222	cpu->regs.r_o3 = SWAP_INT(cpu->regs.r_o3);
2223	cpu->regs.r_o4 = SWAP_INT(cpu->regs.r_o4);
2224	cpu->regs.r_o5 = SWAP_INT(cpu->regs.r_o5);
2225	cpu->regs.r_o6 = SWAP_INT(cpu->regs.r_o6);
2226	cpu->regs.r_o7 = SWAP_INT(cpu->regs.r_o7);
2227
2228    	pr_status = (struct p_status *) &(cpu->regs.r_psr);
2229	if(target_byte_sex == host_byte_sex){
2230	    memcpy(&spsr, &(cpu->regs.r_psr), sizeof(struct swapped_psr));
2231	    spsr.u.word = SWAP_INT(spsr.u.word);
2232	    pr_status->PSRREG.psr_bits.cwp = spsr.u.fields.cwp;
2233	    pr_status->PSRREG.psr_bits.ps = spsr.u.fields.ps;
2234	    pr_status->PSRREG.psr_bits.s = spsr.u.fields.s;
2235	    pr_status->PSRREG.psr_bits.pil = spsr.u.fields.pil;
2236	    pr_status->PSRREG.psr_bits.ef = spsr.u.fields.ef;
2237	    pr_status->PSRREG.psr_bits.ec = spsr.u.fields.ec;
2238	    pr_status->PSRREG.psr_bits.reserved = spsr.u.fields.reserved;
2239	    pr_status->PSRREG.psr_bits.icc = spsr.u.fields.icc;
2240	    pr_status->PSRREG.psr_bits.et = spsr.u.fields.ver;
2241	    pr_status->PSRREG.psr_bits.impl = spsr.u.fields.impl;
2242	}
2243	else{
2244	    spsr.u.fields.cwp = pr_status->PSRREG.psr_bits.cwp;
2245	    spsr.u.fields.ps = pr_status->PSRREG.psr_bits.ps;
2246	    spsr.u.fields.s = pr_status->PSRREG.psr_bits.s;
2247	    spsr.u.fields.pil = pr_status->PSRREG.psr_bits.pil;
2248	    spsr.u.fields.ef = pr_status->PSRREG.psr_bits.ef;
2249	    spsr.u.fields.ec = pr_status->PSRREG.psr_bits.ec;
2250	    spsr.u.fields.reserved = pr_status->PSRREG.psr_bits.reserved;
2251	    spsr.u.fields.icc = pr_status->PSRREG.psr_bits.icc;
2252	    spsr.u.fields.ver = pr_status->PSRREG.psr_bits.et;
2253	    spsr.u.fields.impl = pr_status->PSRREG.psr_bits.impl;
2254	    spsr.u.word = SWAP_INT(spsr.u.word);
2255	    memcpy(&(cpu->regs.r_psr), &spsr, sizeof(struct swapped_psr));
2256	}
2257}
2258
2259__private_extern__
2260void
2261swap_sparc_thread_state_fpu(
2262struct sparc_thread_state_fpu *fpu,
2263enum byte_sex target_byte_sex)
2264{
2265    struct swapped_fsr {
2266	union {
2267	    struct {
2268		unsigned int
2269		cexc:BITS_WIDTH(4,0),
2270		aexc:BITS_WIDTH(9,5),
2271		fcc:BITS_WIDTH(11,10),
2272		pr:BIT_WIDTH(12),
2273		qne:BIT_WIDTH(13),
2274		ftt:BITS_WIDTH(16,14),
2275		res:BITS_WIDTH(22,17),
2276		tem:BITS_WIDTH(27,23),
2277		rp:BITS_WIDTH(29,28),
2278		rd:BITS_WIDTH(31,30);
2279	    } fields;
2280	    unsigned int word;
2281	} u;
2282    } sfsr;
2283    uint32_t i;
2284    struct f_status *fpu_status;
2285    enum byte_sex host_byte_sex;
2286
2287	host_byte_sex = get_host_byte_sex();
2288
2289
2290	/* floating point registers */
2291	for(i = 0; i < 16; i++)		/* 16 doubles */
2292	    fpu->fpu.fpu_fr.Fpu_dregs[i] =
2293		SWAP_DOUBLE(fpu->fpu.fpu_fr.Fpu_dregs[i]);
2294
2295	fpu->fpu.Fpu_q[0].FQu.whole = SWAP_DOUBLE(fpu->fpu.Fpu_q[0].FQu.whole);
2296	fpu->fpu.Fpu_q[1].FQu.whole = SWAP_DOUBLE(fpu->fpu.Fpu_q[1].FQu.whole);
2297	fpu->fpu.Fpu_flags = SWAP_INT(fpu->fpu.Fpu_flags);
2298	fpu->fpu.Fpu_extra = SWAP_INT(fpu->fpu.Fpu_extra);
2299	fpu->fpu.Fpu_qcnt = SWAP_INT(fpu->fpu.Fpu_qcnt);
2300
2301	fpu_status = (struct f_status *) &(fpu->fpu.Fpu_fsr);
2302	if(target_byte_sex == host_byte_sex){
2303	    memcpy(&sfsr, &(fpu->fpu.Fpu_fsr), sizeof(unsigned int));
2304	    sfsr.u.word = SWAP_INT(sfsr.u.word);
2305	    fpu_status->FPUREG.Fpu_fsr_bits.rd = sfsr.u.fields.rd;
2306	    fpu_status->FPUREG.Fpu_fsr_bits.rp = sfsr.u.fields.rp;
2307	    fpu_status->FPUREG.Fpu_fsr_bits.tem = sfsr.u.fields.tem;
2308	    fpu_status->FPUREG.Fpu_fsr_bits.res = sfsr.u.fields.res;
2309	    fpu_status->FPUREG.Fpu_fsr_bits.ftt = sfsr.u.fields.ftt;
2310	    fpu_status->FPUREG.Fpu_fsr_bits.qne = sfsr.u.fields.qne;
2311	    fpu_status->FPUREG.Fpu_fsr_bits.pr = sfsr.u.fields.pr;
2312	    fpu_status->FPUREG.Fpu_fsr_bits.fcc = sfsr.u.fields.fcc;
2313	    fpu_status->FPUREG.Fpu_fsr_bits.aexc = sfsr.u.fields.aexc;
2314	    fpu_status->FPUREG.Fpu_fsr_bits.cexc = sfsr.u.fields.cexc;
2315	}
2316	else{
2317	    sfsr.u.fields.rd = fpu_status->FPUREG.Fpu_fsr_bits.rd;
2318	    sfsr.u.fields.rp = fpu_status->FPUREG.Fpu_fsr_bits.rp;
2319	    sfsr.u.fields.tem = fpu_status->FPUREG.Fpu_fsr_bits.tem;
2320	    sfsr.u.fields.res = fpu_status->FPUREG.Fpu_fsr_bits.res;
2321	    sfsr.u.fields.ftt = fpu_status->FPUREG.Fpu_fsr_bits.ftt;
2322	    sfsr.u.fields.qne = fpu_status->FPUREG.Fpu_fsr_bits.qne;
2323	    sfsr.u.fields.pr = fpu_status->FPUREG.Fpu_fsr_bits.pr;
2324	    sfsr.u.fields.fcc = fpu_status->FPUREG.Fpu_fsr_bits.fcc;
2325	    sfsr.u.fields.aexc = fpu_status->FPUREG.Fpu_fsr_bits.aexc;
2326	    sfsr.u.fields.cexc = fpu_status->FPUREG.Fpu_fsr_bits.cexc;
2327	    sfsr.u.word = SWAP_INT(sfsr.u.word);
2328	    memcpy(&(fpu->fpu.Fpu_fsr), &sfsr, sizeof(struct swapped_fsr));
2329	}
2330}
2331
2332__private_extern__
2333void
2334swap_arm_thread_state_t(
2335arm_thread_state_t *cpu,
2336enum byte_sex target_byte_sex)
2337{
2338    int i;
2339
2340	for(i = 0; i < 13; i++)
2341	    cpu->__r[i] = SWAP_INT(cpu->__r[i]);
2342	cpu->__sp = SWAP_INT(cpu->__sp);
2343	cpu->__lr = SWAP_INT(cpu->__lr);
2344	cpu->__pc = SWAP_INT(cpu->__pc);
2345	cpu->__cpsr = SWAP_INT(cpu->__cpsr);
2346}
2347
2348__private_extern__
2349void
2350swap_ident_command(
2351struct ident_command *id_cmd,
2352enum byte_sex target_byte_sex)
2353{
2354#ifdef __MWERKS__
2355    enum byte_sex dummy;
2356        dummy = target_byte_sex;
2357#endif
2358	id_cmd->cmd = SWAP_INT(id_cmd->cmd);
2359	id_cmd->cmdsize = SWAP_INT(id_cmd->cmdsize);
2360}
2361
2362__private_extern__
2363void
2364swap_routines_command(
2365struct routines_command *r_cmd,
2366enum byte_sex target_byte_sex)
2367{
2368#ifdef __MWERKS__
2369    enum byte_sex dummy;
2370        dummy = target_byte_sex;
2371#endif
2372	r_cmd->cmd = SWAP_INT(r_cmd->cmd);
2373	r_cmd->cmdsize = SWAP_INT(r_cmd->cmdsize);
2374	r_cmd->init_address = SWAP_INT(r_cmd->init_address);
2375	r_cmd->init_module = SWAP_INT(r_cmd->init_module);
2376	r_cmd->reserved1 = SWAP_INT(r_cmd->reserved1);
2377	r_cmd->reserved2 = SWAP_INT(r_cmd->reserved2);
2378	r_cmd->reserved3 = SWAP_INT(r_cmd->reserved3);
2379	r_cmd->reserved4 = SWAP_INT(r_cmd->reserved4);
2380	r_cmd->reserved5 = SWAP_INT(r_cmd->reserved5);
2381	r_cmd->reserved6 = SWAP_INT(r_cmd->reserved6);
2382}
2383
2384__private_extern__
2385void
2386swap_routines_command_64(
2387struct routines_command_64 *r_cmd,
2388enum byte_sex target_byte_sex)
2389{
2390#ifdef __MWERKS__
2391    enum byte_sex dummy;
2392        dummy = target_byte_sex;
2393#endif
2394	r_cmd->cmd = SWAP_INT(r_cmd->cmd);
2395	r_cmd->cmdsize = SWAP_INT(r_cmd->cmdsize);
2396	r_cmd->init_address = SWAP_LONG_LONG(r_cmd->init_address);
2397	r_cmd->init_module = SWAP_LONG_LONG(r_cmd->init_module);
2398	r_cmd->reserved1 = SWAP_LONG_LONG(r_cmd->reserved1);
2399	r_cmd->reserved2 = SWAP_LONG_LONG(r_cmd->reserved2);
2400	r_cmd->reserved3 = SWAP_LONG_LONG(r_cmd->reserved3);
2401	r_cmd->reserved4 = SWAP_LONG_LONG(r_cmd->reserved4);
2402	r_cmd->reserved5 = SWAP_LONG_LONG(r_cmd->reserved5);
2403	r_cmd->reserved6 = SWAP_LONG_LONG(r_cmd->reserved6);
2404}
2405
2406__private_extern__
2407void
2408swap_twolevel_hints_command(
2409struct twolevel_hints_command *hints_cmd,
2410enum byte_sex target_byte_sex)
2411{
2412#ifdef __MWERKS__
2413    enum byte_sex dummy;
2414        dummy = target_byte_sex;
2415#endif
2416	hints_cmd->cmd = SWAP_INT(hints_cmd->cmd);
2417	hints_cmd->cmdsize = SWAP_INT(hints_cmd->cmdsize);
2418	hints_cmd->offset = SWAP_INT(hints_cmd->offset);
2419	hints_cmd->nhints = SWAP_INT(hints_cmd->nhints);
2420}
2421
2422__private_extern__
2423void
2424swap_twolevel_hint(
2425struct twolevel_hint *hints,
2426uint32_t nhints,
2427enum byte_sex target_byte_sex)
2428{
2429    struct swapped_twolevel_hint {
2430	union {
2431	    struct {
2432		uint32_t
2433		    itoc:24,
2434		    isub_image:8;
2435	    } fields;
2436	    uint32_t word;
2437	} u;
2438    } shint;
2439
2440    uint32_t i;
2441    enum byte_sex host_byte_sex;
2442
2443	host_byte_sex = get_host_byte_sex();
2444
2445	for(i = 0; i < nhints; i++){
2446	    if(target_byte_sex == host_byte_sex){
2447		memcpy(&shint, hints + i, sizeof(struct swapped_twolevel_hint));
2448		shint.u.word = SWAP_INT(shint.u.word);
2449		hints[i].itoc = shint.u.fields.itoc;
2450		hints[i].isub_image = shint.u.fields.isub_image;
2451	    }
2452	    else{
2453		shint.u.fields.isub_image = hints[i].isub_image;
2454		shint.u.fields.itoc = hints[i].itoc;
2455		shint.u.word = SWAP_INT(shint.u.word);
2456		memcpy(hints + i, &shint, sizeof(struct swapped_twolevel_hint));
2457	    }
2458	}
2459}
2460
2461__private_extern__
2462void
2463swap_data_in_code_entry(
2464struct data_in_code_entry *dices,
2465uint32_t ndices,
2466enum byte_sex target_byte_sex)
2467{
2468    uint32_t i;
2469
2470	for(i = 0; i < ndices; i++){
2471	    dices[i].offset = SWAP_INT(dices[i].offset);
2472	    dices[i].length = SWAP_INT(dices[i].length);
2473	    dices[i].kind = SWAP_INT(dices[i].kind);
2474	}
2475}
2476
2477__private_extern__
2478void
2479swap_prebind_cksum_command(
2480struct prebind_cksum_command *cksum_cmd,
2481enum byte_sex target_byte_sex)
2482{
2483#ifdef __MWERKS__
2484    enum byte_sex dummy;
2485        dummy = target_byte_sex;
2486#endif
2487	cksum_cmd->cmd = SWAP_INT(cksum_cmd->cmd);
2488	cksum_cmd->cmdsize = SWAP_INT(cksum_cmd->cmdsize);
2489	cksum_cmd->cksum = SWAP_INT(cksum_cmd->cksum);
2490}
2491
2492__private_extern__
2493void
2494swap_uuid_command(
2495struct uuid_command *uuid_cmd,
2496enum byte_sex target_byte_sex)
2497{
2498	uuid_cmd->cmd = SWAP_INT(uuid_cmd->cmd);
2499	uuid_cmd->cmdsize = SWAP_INT(uuid_cmd->cmdsize);
2500}
2501
2502__private_extern__
2503void
2504swap_linkedit_data_command(
2505struct linkedit_data_command *ld,
2506enum byte_sex target_byte_sex)
2507{
2508	ld->cmd = SWAP_INT(ld->cmd);
2509	ld->cmdsize = SWAP_INT(ld->cmdsize);
2510	ld->dataoff = SWAP_INT(ld->dataoff);
2511	ld->datasize = SWAP_INT(ld->datasize);
2512}
2513
2514__private_extern__
2515void
2516swap_version_min_command(
2517struct version_min_command *ver_cmd,
2518enum byte_sex target_byte_sex)
2519{
2520	ver_cmd->cmd = SWAP_INT(ver_cmd->cmd);
2521	ver_cmd->cmdsize = SWAP_INT(ver_cmd->cmdsize);
2522	ver_cmd->version = SWAP_INT(ver_cmd->version);
2523}
2524
2525__private_extern__
2526void swap_rpath_command(
2527struct rpath_command *rpath_cmd,
2528enum byte_sex target_byte_sex)
2529{
2530	rpath_cmd->cmd = SWAP_INT(rpath_cmd->cmd);
2531	rpath_cmd->cmdsize = SWAP_INT(rpath_cmd->cmdsize);
2532	rpath_cmd->path.offset = SWAP_INT(rpath_cmd->path.offset);
2533}
2534
2535__private_extern__
2536 void
2537swap_encryption_command(
2538struct encryption_info_command *ec,
2539enum byte_sex target_byte_sex)
2540{
2541	ec->cmd = SWAP_INT(ec->cmd);
2542	ec->cmdsize = SWAP_INT(ec->cmdsize);
2543	ec->cryptoff = SWAP_INT(ec->cryptoff);
2544	ec->cryptsize = SWAP_INT(ec->cryptsize);
2545	ec->cryptid = SWAP_INT(ec->cryptid);
2546}
2547
2548__private_extern__
2549 void
2550swap_encryption_command_64(
2551struct encryption_info_command_64 *ec,
2552enum byte_sex target_byte_sex)
2553{
2554	ec->cmd = SWAP_INT(ec->cmd);
2555	ec->cmdsize = SWAP_INT(ec->cmdsize);
2556	ec->cryptoff = SWAP_INT(ec->cryptoff);
2557	ec->cryptsize = SWAP_INT(ec->cryptsize);
2558	ec->cryptid = SWAP_INT(ec->cryptid);
2559	ec->cryptid = SWAP_INT(ec->pad);
2560}
2561
2562__private_extern__
2563 void
2564swap_linker_option_command(
2565struct linker_option_command *lo,
2566enum byte_sex target_byte_sex)
2567{
2568	lo->cmd = SWAP_INT(lo->cmd);
2569	lo->cmdsize = SWAP_INT(lo->cmdsize);
2570	lo->count = SWAP_INT(lo->count);
2571}
2572
2573__private_extern__
2574 void
2575swap_dyld_info_command(
2576struct dyld_info_command *ed,
2577enum byte_sex target_byte_sex)
2578{
2579	ed->cmd = SWAP_INT(ed->cmd);
2580	ed->cmdsize = SWAP_INT(ed->cmdsize);
2581	ed->rebase_off = SWAP_INT(ed->rebase_off);
2582	ed->rebase_size = SWAP_INT(ed->rebase_size);
2583	ed->bind_off = SWAP_INT(ed->bind_off);
2584	ed->bind_size = SWAP_INT(ed->bind_size);
2585	ed->weak_bind_off = SWAP_INT(ed->weak_bind_off);
2586	ed->weak_bind_size = SWAP_INT(ed->weak_bind_size);
2587	ed->lazy_bind_off = SWAP_INT(ed->lazy_bind_off);
2588	ed->lazy_bind_size = SWAP_INT(ed->lazy_bind_size);
2589	ed->export_off = SWAP_INT(ed->export_off);
2590	ed->export_size = SWAP_INT(ed->export_size);
2591}
2592
2593__private_extern__
2594void
2595swap_entry_point_command(
2596struct entry_point_command *ep,
2597enum byte_sex target_byte_sex)
2598{
2599	ep->cmd = SWAP_INT(ep->cmd);
2600	ep->cmdsize = SWAP_INT(ep->cmdsize);
2601	ep->entryoff = SWAP_LONG_LONG(ep->entryoff);
2602	ep->stacksize = SWAP_LONG_LONG(ep->stacksize);
2603}
2604
2605__private_extern__
2606void
2607swap_source_version_command(
2608struct source_version_command *sv,
2609enum byte_sex target_byte_sex)
2610{
2611	sv->cmd = SWAP_INT(sv->cmd);
2612	sv->cmdsize = SWAP_INT(sv->cmdsize);
2613	sv->version = SWAP_LONG_LONG(sv->version);
2614}
2615
2616__private_extern__
2617void
2618swap_nlist(
2619struct nlist *symbols,
2620uint32_t nsymbols,
2621enum byte_sex target_byte_sex)
2622{
2623    uint32_t i;
2624#ifdef __MWERKS__
2625    enum byte_sex dummy;
2626        dummy = target_byte_sex;
2627#endif
2628
2629	for(i = 0; i < nsymbols; i++){
2630	    symbols[i].n_un.n_strx = SWAP_INT(symbols[i].n_un.n_strx);
2631	    /* n_type */
2632	    /* n_sect */
2633	    symbols[i].n_desc = SWAP_SHORT(symbols[i].n_desc);
2634	    symbols[i].n_value = SWAP_INT(symbols[i].n_value);
2635	}
2636}
2637
2638__private_extern__
2639void
2640swap_nlist_64(
2641struct nlist_64 *symbols,
2642uint32_t nsymbols,
2643enum byte_sex target_byte_sex)
2644{
2645    uint32_t i;
2646#ifdef __MWERKS__
2647    enum byte_sex dummy;
2648        dummy = target_byte_sex;
2649#endif
2650
2651	for(i = 0; i < nsymbols; i++){
2652	    symbols[i].n_un.n_strx = SWAP_INT(symbols[i].n_un.n_strx);
2653	    /* n_type */
2654	    /* n_sect */
2655	    symbols[i].n_desc = SWAP_SHORT(symbols[i].n_desc);
2656	    symbols[i].n_value = SWAP_LONG_LONG(symbols[i].n_value);
2657	}
2658}
2659
2660__private_extern__
2661void
2662swap_ranlib(
2663struct ranlib *ranlibs,
2664uint32_t nranlibs,
2665enum byte_sex target_byte_sex)
2666{
2667    uint32_t i;
2668#ifdef __MWERKS__
2669    enum byte_sex dummy;
2670        dummy = target_byte_sex;
2671#endif
2672
2673	for(i = 0; i < nranlibs; i++){
2674	    ranlibs[i].ran_un.ran_strx = SWAP_INT(ranlibs[i].ran_un.ran_strx);
2675	    ranlibs[i].ran_off = SWAP_INT(ranlibs[i].ran_off);
2676	}
2677}
2678
2679__private_extern__
2680void
2681swap_relocation_info(
2682struct relocation_info *relocs,
2683uint32_t nrelocs,
2684enum byte_sex target_byte_sex)
2685{
2686    uint32_t i;
2687    enum byte_sex host_byte_sex;
2688    enum bool to_host_byte_sex, scattered;
2689
2690    struct swapped_relocation_info {
2691	int32_t r_address;
2692	union {
2693	    struct {
2694		unsigned int
2695		    r_type:4,
2696		    r_extern:1,
2697		    r_length:2,
2698		    r_pcrel:1,
2699		    r_symbolnum:24;
2700	    } fields;
2701	    uint32_t word;
2702	} u;
2703    } sr;
2704
2705    struct swapped_scattered_relocation_info {
2706	uint32_t word;
2707	int32_t	r_value;
2708    } *ssr;
2709
2710	host_byte_sex = get_host_byte_sex();
2711	to_host_byte_sex = (enum bool)(target_byte_sex == host_byte_sex);
2712
2713	for(i = 0; i < nrelocs; i++){
2714	    if(to_host_byte_sex)
2715		scattered = (enum bool)(
2716			(SWAP_INT(relocs[i].r_address) & R_SCATTERED) != 0);
2717	    else
2718		scattered = (enum bool)
2719			(((relocs[i].r_address) & R_SCATTERED) != 0);
2720	    if(scattered == FALSE){
2721		if(to_host_byte_sex){
2722		    memcpy(&sr, relocs + i, sizeof(struct relocation_info));
2723		    sr.r_address = SWAP_INT(sr.r_address);
2724		    sr.u.word = SWAP_INT(sr.u.word);
2725		    relocs[i].r_address = sr.r_address;
2726		    relocs[i].r_symbolnum = sr.u.fields.r_symbolnum;
2727		    relocs[i].r_pcrel = sr.u.fields.r_pcrel;
2728		    relocs[i].r_length = sr.u.fields.r_length;
2729		    relocs[i].r_extern = sr.u.fields.r_extern;
2730		    relocs[i].r_type = sr.u.fields.r_type;
2731		}
2732		else{
2733		    sr.r_address = relocs[i].r_address;
2734		    sr.u.fields.r_symbolnum = relocs[i].r_symbolnum;
2735		    sr.u.fields.r_length = relocs[i].r_length;
2736		    sr.u.fields.r_pcrel = relocs[i].r_pcrel;
2737		    sr.u.fields.r_extern = relocs[i].r_extern;
2738		    sr.u.fields.r_type = relocs[i].r_type;
2739		    sr.r_address = SWAP_INT(sr.r_address);
2740		    sr.u.word = SWAP_INT(sr.u.word);
2741		    memcpy(relocs + i, &sr, sizeof(struct relocation_info));
2742		}
2743	    }
2744	    else{
2745		ssr = (struct swapped_scattered_relocation_info *)(relocs + i);
2746		ssr->word = SWAP_INT(ssr->word);
2747		ssr->r_value = SWAP_INT(ssr->r_value);
2748	    }
2749	}
2750}
2751
2752__private_extern__
2753void
2754swap_indirect_symbols(
2755uint32_t *indirect_symbols,
2756uint32_t nindirect_symbols,
2757enum byte_sex target_byte_sex)
2758{
2759    uint32_t i;
2760#ifdef __MWERKS__
2761    enum byte_sex dummy;
2762        dummy = target_byte_sex;
2763#endif
2764
2765	for(i = 0; i < nindirect_symbols; i++)
2766	    indirect_symbols[i] = SWAP_INT(indirect_symbols[i]);
2767}
2768
2769__private_extern__
2770void
2771swap_dylib_reference(
2772struct dylib_reference *refs,
2773uint32_t nrefs,
2774enum byte_sex target_byte_sex)
2775{
2776    struct swapped_dylib_reference {
2777	union {
2778	    struct {
2779		uint32_t
2780		    flags:8,
2781		    isym:24;
2782	    } fields;
2783	    uint32_t word;
2784	} u;
2785    } sref;
2786
2787    uint32_t i;
2788    enum byte_sex host_byte_sex;
2789
2790	host_byte_sex = get_host_byte_sex();
2791
2792	for(i = 0; i < nrefs; i++){
2793	    if(target_byte_sex == host_byte_sex){
2794		memcpy(&sref, refs + i, sizeof(struct swapped_dylib_reference));
2795		sref.u.word = SWAP_INT(sref.u.word);
2796		refs[i].flags = sref.u.fields.flags;
2797		refs[i].isym = sref.u.fields.isym;
2798	    }
2799	    else{
2800		sref.u.fields.isym = refs[i].isym;
2801		sref.u.fields.flags = refs[i].flags;
2802		sref.u.word = SWAP_INT(sref.u.word);
2803		memcpy(refs + i, &sref, sizeof(struct swapped_dylib_reference));
2804	    }
2805	}
2806
2807}
2808
2809__private_extern__
2810void
2811swap_dylib_module(
2812struct dylib_module *mods,
2813uint32_t nmods,
2814enum byte_sex target_byte_sex)
2815{
2816    uint32_t i;
2817#ifdef __MWERKS__
2818    enum byte_sex dummy;
2819        dummy = target_byte_sex;
2820#endif
2821
2822	for(i = 0; i < nmods; i++){
2823	    mods[i].module_name = SWAP_INT(mods[i].module_name);
2824	    mods[i].iextdefsym  = SWAP_INT(mods[i].iextdefsym);
2825	    mods[i].nextdefsym  = SWAP_INT(mods[i].nextdefsym);
2826	    mods[i].irefsym     = SWAP_INT(mods[i].irefsym);
2827	    mods[i].nrefsym     = SWAP_INT(mods[i].nrefsym);
2828	    mods[i].ilocalsym   = SWAP_INT(mods[i].ilocalsym);
2829	    mods[i].nlocalsym   = SWAP_INT(mods[i].nlocalsym);
2830	    mods[i].iextrel     = SWAP_INT(mods[i].iextrel);
2831	    mods[i].nextrel     = SWAP_INT(mods[i].nextrel);
2832	    mods[i].iinit_iterm = SWAP_INT(mods[i].iinit_iterm);
2833	    mods[i].ninit_nterm = SWAP_INT(mods[i].ninit_nterm);
2834	    mods[i].objc_module_info_addr =
2835				  SWAP_INT(mods[i].objc_module_info_addr);
2836	    mods[i].objc_module_info_size =
2837				  SWAP_INT(mods[i].objc_module_info_size);
2838	}
2839}
2840
2841__private_extern__
2842void
2843swap_dylib_module_64(
2844struct dylib_module_64 *mods,
2845uint32_t nmods,
2846enum byte_sex target_byte_sex)
2847{
2848    uint32_t i;
2849#ifdef __MWERKS__
2850    enum byte_sex dummy;
2851        dummy = target_byte_sex;
2852#endif
2853
2854	for(i = 0; i < nmods; i++){
2855	    mods[i].module_name = SWAP_INT(mods[i].module_name);
2856	    mods[i].iextdefsym  = SWAP_INT(mods[i].iextdefsym);
2857	    mods[i].nextdefsym  = SWAP_INT(mods[i].nextdefsym);
2858	    mods[i].irefsym     = SWAP_INT(mods[i].irefsym);
2859	    mods[i].nrefsym     = SWAP_INT(mods[i].nrefsym);
2860	    mods[i].ilocalsym   = SWAP_INT(mods[i].ilocalsym);
2861	    mods[i].nlocalsym   = SWAP_INT(mods[i].nlocalsym);
2862	    mods[i].iextrel     = SWAP_INT(mods[i].iextrel);
2863	    mods[i].nextrel     = SWAP_INT(mods[i].nextrel);
2864	    mods[i].iinit_iterm = SWAP_INT(mods[i].iinit_iterm);
2865	    mods[i].ninit_nterm = SWAP_INT(mods[i].ninit_nterm);
2866	    mods[i].objc_module_info_addr =
2867				  SWAP_LONG_LONG(mods[i].objc_module_info_addr);
2868	    mods[i].objc_module_info_size =
2869				  SWAP_LONG_LONG(mods[i].objc_module_info_size);
2870	}
2871}
2872
2873__private_extern__
2874void
2875swap_dylib_table_of_contents(
2876struct dylib_table_of_contents *tocs,
2877uint32_t ntocs,
2878enum byte_sex target_byte_sex)
2879{
2880    uint32_t i;
2881#ifdef __MWERKS__
2882    enum byte_sex dummy;
2883        dummy = target_byte_sex;
2884#endif
2885
2886	for(i = 0; i < ntocs; i++){
2887	    tocs[i].symbol_index = SWAP_INT(tocs[i].symbol_index);
2888	    tocs[i].module_index = SWAP_INT(tocs[i].module_index);
2889	}
2890}
2891