home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / cctools / libstuff / bytesex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-21  |  52.5 KB  |  1,757 lines

  1. /* byte_sex.c */
  2. #include <mach-o/fat.h>
  3. #include <mach-o/loader.h>
  4. #import <mach/m68k/thread_status.h>
  5. #import <mach/m98k/thread_status.h>
  6. #import <mach/m88k/thread_status.h>
  7. #import <mach/i860/thread_status.h>
  8. #import <mach/i386/thread_status.h>
  9. #import <mach/hppa/thread_status.h>
  10. #import <mach/sparc/thread_status.h>
  11. #include <mach-o/nlist.h>
  12. #include <mach-o/reloc.h>
  13. #include <bsd/ranlib.h>
  14. #include "stuff/bool.h"
  15. #include "stuff/bytesex.h"
  16.  
  17. __private_extern__
  18. double
  19. SWAP_DOUBLE(
  20. double d)
  21. {
  22.     union {
  23.         char c[8];
  24.         double d;
  25.     } in, out;
  26.     in.d = d;
  27.     out.c[0] = in.c[7];
  28.     out.c[1] = in.c[6];
  29.     out.c[2] = in.c[5];
  30.     out.c[3] = in.c[4];
  31.     out.c[4] = in.c[3];
  32.     out.c[5] = in.c[2];
  33.     out.c[6] = in.c[1];
  34.     out.c[7] = in.c[0];
  35.     return(out.d);
  36. }
  37.  
  38. __private_extern__
  39. float
  40. SWAP_FLOAT(
  41. float f)
  42. {
  43.     union {
  44.         char c[7];
  45.         float f;
  46.     } in, out;
  47.     in.f = f;
  48.     out.c[0] = in.c[3];
  49.     out.c[1] = in.c[2];
  50.     out.c[2] = in.c[1];
  51.     out.c[3] = in.c[0];
  52.     return(out.f);
  53. }
  54.  
  55. /*
  56.  * get_host_byte_sex() returns the enum constant for the byte sex of the host
  57.  * it is running on.
  58.  */
  59. __private_extern__
  60. enum byte_sex
  61. get_host_byte_sex(
  62. void)
  63. {
  64.     unsigned long s;
  65.  
  66.     s = (BIG_ENDIAN_BYTE_SEX << 24) | LITTLE_ENDIAN_BYTE_SEX;
  67.     return((enum byte_sex)*((char *)&s));
  68. }
  69.  
  70. __private_extern__
  71. void
  72. swap_fat_header(
  73. struct fat_header *fat_header,
  74. enum byte_sex target_byte_sex)
  75. {
  76.     fat_header->magic     = SWAP_LONG(fat_header->magic);
  77.     fat_header->nfat_arch = SWAP_LONG(fat_header->nfat_arch);
  78. }
  79.  
  80. __private_extern__
  81. void
  82. swap_fat_arch(
  83. struct fat_arch *fat_archs,
  84. unsigned long nfat_arch,
  85. enum byte_sex target_byte_sex)
  86. {
  87.     unsigned long i;
  88.  
  89.     for(i = 0; i < nfat_arch; i++){
  90.         fat_archs[i].cputype    = SWAP_LONG(fat_archs[i].cputype);
  91.         fat_archs[i].cpusubtype = SWAP_LONG(fat_archs[i].cpusubtype);
  92.         fat_archs[i].offset     = SWAP_LONG(fat_archs[i].offset);
  93.         fat_archs[i].size       = SWAP_LONG(fat_archs[i].size);
  94.         fat_archs[i].align      = SWAP_LONG(fat_archs[i].align);
  95.     }
  96. }
  97.  
  98. __private_extern__
  99. void
  100. swap_mach_header(
  101. struct mach_header *mh,
  102. enum byte_sex target_byte_sex)
  103. {
  104.     mh->magic = SWAP_LONG(mh->magic);
  105.     mh->cputype = SWAP_LONG(mh->cputype);
  106.     mh->cpusubtype = SWAP_LONG(mh->cpusubtype);
  107.     mh->filetype = SWAP_LONG(mh->filetype);
  108.     mh->ncmds = SWAP_LONG(mh->ncmds);
  109.     mh->sizeofcmds = SWAP_LONG(mh->sizeofcmds);
  110.     mh->flags = SWAP_LONG(mh->flags);
  111. }
  112.  
  113. __private_extern__
  114. void
  115. swap_load_command(
  116. struct load_command *lc,
  117. enum byte_sex target_byte_sex)
  118. {
  119.     lc->cmd = SWAP_LONG(lc->cmd);
  120.     lc->cmdsize = SWAP_LONG(lc->cmdsize);
  121. }
  122.  
  123. __private_extern__
  124. void
  125. swap_segment_command(
  126. struct segment_command *sg,
  127. enum byte_sex target_byte_sex)
  128. {
  129.     /* segname[16] */
  130.     sg->cmd = SWAP_LONG(sg->cmd);
  131.     sg->cmdsize = SWAP_LONG(sg->cmdsize);
  132.     sg->vmaddr = SWAP_LONG(sg->vmaddr);
  133.     sg->vmsize = SWAP_LONG(sg->vmsize);
  134.     sg->fileoff = SWAP_LONG(sg->fileoff);
  135.     sg->filesize = SWAP_LONG(sg->filesize);
  136.     sg->maxprot = SWAP_LONG(sg->maxprot);
  137.     sg->initprot = SWAP_LONG(sg->initprot);
  138.     sg->nsects = SWAP_LONG(sg->nsects);
  139.     sg->flags = SWAP_LONG(sg->flags);
  140. }
  141.  
  142. __private_extern__
  143. void
  144. swap_section(
  145. struct section *s,
  146. unsigned long nsects,
  147. enum byte_sex target_byte_sex)
  148. {
  149.     unsigned long i;
  150.  
  151.     for(i = 0; i < nsects; i++){
  152.         /* sectname[16] */
  153.         /* segname[16] */
  154.         s[i].addr = SWAP_LONG(s[i].addr);
  155.         s[i].size = SWAP_LONG(s[i].size);
  156.         s[i].offset = SWAP_LONG(s[i].offset);
  157.         s[i].align = SWAP_LONG(s[i].align);
  158.         s[i].reloff = SWAP_LONG(s[i].reloff);
  159.         s[i].nreloc = SWAP_LONG(s[i].nreloc);
  160.         s[i].flags = SWAP_LONG(s[i].flags);
  161.         s[i].reserved1 = SWAP_LONG(s[i].reserved1);
  162.         s[i].reserved2 = SWAP_LONG(s[i].reserved2);
  163.     }
  164. }
  165.  
  166. __private_extern__
  167. void
  168. swap_symtab_command(
  169. struct symtab_command *st,
  170. enum byte_sex target_byte_sex)
  171. {
  172.     st->cmd = SWAP_LONG(st->cmd);
  173.     st->cmdsize = SWAP_LONG(st->cmdsize);
  174.     st->symoff = SWAP_LONG(st->symoff);
  175.     st->nsyms = SWAP_LONG(st->nsyms);
  176.     st->stroff = SWAP_LONG(st->stroff);
  177.     st->strsize = SWAP_LONG(st->strsize);
  178. }
  179.  
  180. __private_extern__
  181. void
  182. swap_dysymtab_command(
  183. struct dysymtab_command *dyst,
  184. enum byte_sex target_byte_sex)
  185. {
  186.     dyst->cmd = SWAP_LONG(dyst->cmd);
  187.     dyst->cmdsize = SWAP_LONG(dyst->cmdsize);
  188.     dyst->ilocalsym = SWAP_LONG(dyst->ilocalsym);
  189.     dyst->nlocalsym = SWAP_LONG(dyst->nlocalsym);
  190.     dyst->iextdefsym = SWAP_LONG(dyst->iextdefsym);
  191.     dyst->nextdefsym = SWAP_LONG(dyst->nextdefsym);
  192.     dyst->iundefsym = SWAP_LONG(dyst->iundefsym);
  193.     dyst->nundefsym = SWAP_LONG(dyst->nundefsym);
  194.     dyst->tocoff = SWAP_LONG(dyst->tocoff);
  195.     dyst->ntoc = SWAP_LONG(dyst->ntoc);
  196.     dyst->modtaboff = SWAP_LONG(dyst->modtaboff);
  197.     dyst->nmodtab = SWAP_LONG(dyst->nmodtab);
  198.     dyst->extrefsymoff = SWAP_LONG(dyst->extrefsymoff);
  199.     dyst->nextrefsyms = SWAP_LONG(dyst->nextrefsyms);
  200.     dyst->indirectsymoff = SWAP_LONG(dyst->indirectsymoff);
  201.     dyst->nindirectsyms = SWAP_LONG(dyst->nindirectsyms);
  202.     dyst->extreloff = SWAP_LONG(dyst->extreloff);
  203.     dyst->nextrel = SWAP_LONG(dyst->nextrel);
  204.     dyst->locreloff = SWAP_LONG(dyst->locreloff);
  205.     dyst->nlocrel = SWAP_LONG(dyst->nlocrel);
  206. }
  207.  
  208. __private_extern__
  209. void
  210. swap_symseg_command(
  211. struct symseg_command *ss,
  212. enum byte_sex target_byte_sex)
  213. {
  214.     ss->cmd = SWAP_LONG(ss->cmd);
  215.     ss->cmdsize = SWAP_LONG(ss->cmdsize);
  216.     ss->offset = SWAP_LONG(ss->offset);
  217.     ss->size = SWAP_LONG(ss->size);
  218. }
  219.  
  220. __private_extern__
  221. void
  222. swap_fvmlib_command(
  223. struct fvmlib_command *fl,
  224. enum byte_sex target_byte_sex)
  225. {
  226.     fl->cmd = SWAP_LONG(fl->cmd);
  227.     fl->cmdsize = SWAP_LONG(fl->cmdsize);
  228.     fl->fvmlib.name.offset = SWAP_LONG(fl->fvmlib.name.offset);
  229.     fl->fvmlib.minor_version = SWAP_LONG(fl->fvmlib.minor_version);
  230.     fl->fvmlib.header_addr = SWAP_LONG(fl->fvmlib.header_addr);
  231. }
  232.  
  233. __private_extern__
  234. void
  235. swap_dylib_command(
  236. struct dylib_command *dl,
  237. enum byte_sex target_byte_sex)
  238. {
  239.     dl->cmd = SWAP_LONG(dl->cmd);
  240.     dl->cmdsize = SWAP_LONG(dl->cmdsize);
  241.     dl->dylib.name.offset = SWAP_LONG(dl->dylib.name.offset);
  242.     dl->dylib.timestamp = SWAP_LONG(dl->dylib.timestamp);
  243.     dl->dylib.current_version = SWAP_LONG(dl->dylib.current_version);
  244.     dl->dylib.compatibility_version =
  245.                 SWAP_LONG(dl->dylib.compatibility_version);
  246. }
  247.  
  248. __private_extern__
  249. void
  250. swap_prebound_dylib_command(
  251. struct prebound_dylib_command *pbdylib,
  252. enum byte_sex target_byte_sex)
  253. {
  254.     pbdylib->cmd = SWAP_LONG(pbdylib->cmd);
  255.     pbdylib->cmdsize = SWAP_LONG(pbdylib->cmdsize);
  256.     pbdylib->name.offset = SWAP_LONG(pbdylib->name.offset);
  257.     pbdylib->nmodules = SWAP_LONG(pbdylib->nmodules);
  258.     pbdylib->linked_modules.offset =
  259.         SWAP_LONG(pbdylib->linked_modules.offset);
  260. }
  261.  
  262. __private_extern__
  263. void
  264. swap_dylinker_command(
  265. struct dylinker_command *dyld,
  266. enum byte_sex target_byte_sex)
  267. {
  268.     dyld->cmd = SWAP_LONG(dyld->cmd);
  269.     dyld->cmdsize = SWAP_LONG(dyld->cmdsize);
  270.     dyld->name.offset = SWAP_LONG(dyld->name.offset);
  271. }
  272.  
  273. __private_extern__
  274. void
  275. swap_fvmfile_command(
  276. struct fvmfile_command *ff,
  277. enum byte_sex target_byte_sex)
  278. {
  279.     ff->cmd = SWAP_LONG(ff->cmd);
  280.     ff->cmdsize = SWAP_LONG(ff->cmdsize);
  281.     ff->name.offset = SWAP_LONG(ff->name.offset);
  282.     ff->header_addr = SWAP_LONG(ff->header_addr);
  283. }
  284.  
  285.  
  286. __private_extern__
  287. void
  288. swap_thread_command(
  289. struct thread_command *ut,
  290. enum byte_sex target_byte_sex)
  291. {
  292.     ut->cmd = SWAP_LONG(ut->cmd);
  293.     ut->cmdsize = SWAP_LONG(ut->cmdsize);
  294. }
  295.  
  296. __private_extern__
  297. void
  298. swap_m68k_thread_state_regs(
  299. struct m68k_thread_state_regs *cpu,
  300. enum byte_sex target_byte_sex)
  301. {
  302.     unsigned long i;
  303.  
  304.     for(i = 0; i < 8; i++)
  305.         cpu->dreg[i] = SWAP_LONG(cpu->dreg[i]);
  306.     for(i = 0; i < 8; i++)
  307.         cpu->areg[i] = SWAP_LONG(cpu->areg[i]);
  308.     cpu->pad0 = SWAP_SHORT(cpu->pad0);
  309.     cpu->sr = SWAP_SHORT(cpu->sr);
  310.     cpu->pc = SWAP_LONG(cpu->pc);
  311. }
  312.  
  313. __private_extern__
  314. void
  315. swap_m68k_thread_state_68882(
  316. struct m68k_thread_state_68882 *fpu,
  317. enum byte_sex target_byte_sex)
  318. {
  319.     unsigned long i, tmp;
  320.  
  321.     for(i = 0; i < 8; i++){
  322.                        tmp = SWAP_LONG(fpu->regs[i].fp[0]);
  323.         fpu->regs[i].fp[1] = SWAP_LONG(fpu->regs[i].fp[1]);
  324.         fpu->regs[i].fp[0] = SWAP_LONG(fpu->regs[i].fp[2]);
  325.         fpu->regs[i].fp[2] = tmp;
  326.     }
  327.     fpu->cr = SWAP_LONG(fpu->cr);
  328.     fpu->sr = SWAP_LONG(fpu->sr);
  329.     fpu->iar = SWAP_LONG(fpu->iar);
  330.     fpu->state = SWAP_LONG(fpu->state);
  331. }
  332.  
  333. __private_extern__
  334. void
  335. swap_m68k_thread_state_user_reg(
  336. struct m68k_thread_state_user_reg *user_reg,
  337. enum byte_sex target_byte_sex)
  338. {
  339.     user_reg->user_reg = SWAP_LONG(user_reg->user_reg);
  340. }
  341.  
  342. __private_extern__
  343. void
  344. swap_m98k_thread_state_grf_t(
  345. m98k_thread_state_grf_t *cpu,
  346. enum byte_sex target_byte_sex)
  347. {
  348.     enum byte_sex host_byte_sex;
  349.  
  350.     struct swapped_m98k_cr {
  351.     union {
  352.         struct {
  353.         unsigned rsvd:BITS_WIDTH(23,0);
  354.         unsigned ox:BIT_WIDTH(24);
  355.         unsigned vx:BIT_WIDTH(25);
  356.         unsigned fex:BIT_WIDTH(26);
  357.         unsigned fx:BIT_WIDTH(27);
  358.         unsigned so:BIT_WIDTH(28);
  359.         unsigned eq:BIT_WIDTH(29);
  360.         unsigned gt:BIT_WIDTH(30);
  361.         unsigned lt:BIT_WIDTH(31);
  362.         } fields;
  363.         unsigned long word;
  364.     } u;
  365.     } scr;
  366.     struct swapped_m98k_xer {
  367.     union {
  368.         struct {
  369.         unsigned byte_count:BITS_WIDTH(6,0);    
  370.         unsigned rsvd2:BIT_WIDTH(7);
  371.         unsigned byte:BITS_WIDTH(15,8);
  372.         unsigned rsvd1:BITS_WIDTH(28,16);
  373.         unsigned ca:BIT_WIDTH(29);
  374.         unsigned ov:BIT_WIDTH(30);
  375.         unsigned so:BIT_WIDTH(31);
  376.         } fields;
  377.         unsigned long word;
  378.     } u;
  379.     } sxer;
  380.     struct swapped_m98k_msr {
  381.     union {
  382.         struct {
  383.         unsigned psfr:BIT_WIDTH(0);
  384.         unsigned rsvd2:BITS_WIDTH(3,1);
  385.         unsigned dr:BIT_WIDTH(4);
  386.         unsigned ir:BIT_WIDTH(5);
  387.         unsigned ip:BIT_WIDTH(6);
  388.         unsigned rsvd1:BIT_WIDTH(7);
  389.         unsigned fe1:BIT_WIDTH(8);
  390.         unsigned be:BIT_WIDTH(9);
  391.         unsigned se:BIT_WIDTH(10);
  392.         unsigned fe0:BIT_WIDTH(11);
  393.         unsigned me:BIT_WIDTH(12);
  394.         unsigned fp:BIT_WIDTH(13);
  395.         unsigned pr:BIT_WIDTH(14);
  396.         unsigned ee:BIT_WIDTH(15);
  397.         unsigned rsvd3:BITS_WIDTH(31,16);
  398.         } fields;
  399.         unsigned long word;
  400.     } u;
  401.     } smsr;
  402.  
  403.     host_byte_sex = get_host_byte_sex();
  404.  
  405.     cpu->r0 = SWAP_LONG(cpu->r0);
  406.     cpu->r1 = SWAP_LONG(cpu->r1);
  407.     cpu->r2 = SWAP_LONG(cpu->r2);
  408.     cpu->r3 = SWAP_LONG(cpu->r3);
  409.     cpu->r4 = SWAP_LONG(cpu->r4);
  410.     cpu->r5 = SWAP_LONG(cpu->r5);
  411.     cpu->r6 = SWAP_LONG(cpu->r6);
  412.     cpu->r7 = SWAP_LONG(cpu->r7);
  413.     cpu->r8 = SWAP_LONG(cpu->r8);
  414.     cpu->r9 = SWAP_LONG(cpu->r9);
  415.     cpu->r10 = SWAP_LONG(cpu->r10);
  416.     cpu->r11 = SWAP_LONG(cpu->r11);
  417.     cpu->r12 = SWAP_LONG(cpu->r12);
  418.     cpu->r13 = SWAP_LONG(cpu->r13);
  419.     cpu->r14 = SWAP_LONG(cpu->r14);
  420.     cpu->r15 = SWAP_LONG(cpu->r15);
  421.     cpu->r16 = SWAP_LONG(cpu->r16);
  422.     cpu->r17 = SWAP_LONG(cpu->r17);
  423.     cpu->r18 = SWAP_LONG(cpu->r18);
  424.     cpu->r19 = SWAP_LONG(cpu->r19);
  425.     cpu->r20 = SWAP_LONG(cpu->r20);
  426.     cpu->r21 = SWAP_LONG(cpu->r21);
  427.     cpu->r22 = SWAP_LONG(cpu->r22);
  428.     cpu->r23 = SWAP_LONG(cpu->r23);
  429.     cpu->r24 = SWAP_LONG(cpu->r24);
  430.     cpu->r25 = SWAP_LONG(cpu->r25);
  431.     cpu->r26 = SWAP_LONG(cpu->r26);
  432.     cpu->r27 = SWAP_LONG(cpu->r27);
  433.     cpu->r28 = SWAP_LONG(cpu->r28);
  434.     cpu->r29 = SWAP_LONG(cpu->r29);
  435.     cpu->r30 = SWAP_LONG(cpu->r30);
  436.     cpu->r31 = SWAP_LONG(cpu->r31);
  437.     cpu->lr  = SWAP_LONG(cpu->lr);
  438.     cpu->ctr = SWAP_LONG(cpu->ctr);
  439.     cpu->cia = SWAP_LONG(cpu->cia);
  440.  
  441.     if(target_byte_sex == host_byte_sex){
  442.         memcpy(&scr, &(cpu->cr), sizeof(struct swapped_m98k_cr));
  443.         scr.u.word = SWAP_LONG(scr.u.word);
  444.         cpu->cr.lt = scr.u.fields.lt;
  445.         cpu->cr.gt = scr.u.fields.gt;
  446.         cpu->cr.eq = scr.u.fields.eq;
  447.         cpu->cr.so = scr.u.fields.so;
  448.         cpu->cr.fx = scr.u.fields.fx;
  449.         cpu->cr.fex = scr.u.fields.fex;
  450.         cpu->cr.vx = scr.u.fields.vx;
  451.         cpu->cr.ox = scr.u.fields.ox;
  452.         cpu->cr.rsvd = scr.u.fields.rsvd;
  453.  
  454.         memcpy(&sxer, &(cpu->xer), sizeof(struct swapped_m98k_xer));
  455.         sxer.u.word = SWAP_LONG(sxer.u.word);
  456.         cpu->xer.byte_count = sxer.u.fields.byte_count;
  457.         cpu->xer.rsvd2 = sxer.u.fields.rsvd2;
  458.         cpu->xer.byte = sxer.u.fields.byte;
  459.         cpu->xer.rsvd1 = sxer.u.fields.rsvd1;
  460.         cpu->xer.ca = sxer.u.fields.ca;
  461.         cpu->xer.ov = sxer.u.fields.ov;
  462.         cpu->xer.so = sxer.u.fields.so;
  463.  
  464.         memcpy(&smsr, &(cpu->msr), sizeof(struct swapped_m98k_msr));
  465.         smsr.u.word = SWAP_LONG(smsr.u.word);
  466.         cpu->msr.psfr = smsr.u.fields.psfr;
  467.         cpu->msr.rsvd2 = smsr.u.fields.rsvd2;
  468.         cpu->msr.dr = smsr.u.fields.dr;
  469.         cpu->msr.ir = smsr.u.fields.ir;
  470.         cpu->msr.ip = smsr.u.fields.ip;
  471.         cpu->msr.rsvd1 = smsr.u.fields.rsvd1;
  472.         cpu->msr.fe1 = smsr.u.fields.fe1;
  473.         cpu->msr.be = smsr.u.fields.be;
  474.         cpu->msr.se = smsr.u.fields.se;
  475.         cpu->msr.fe0 = smsr.u.fields.fe0;
  476.         cpu->msr.me = smsr.u.fields.me;
  477.         cpu->msr.fp = smsr.u.fields.fp;
  478.         cpu->msr.pr = smsr.u.fields.pr;
  479.         cpu->msr.ee = smsr.u.fields.ee;
  480.         cpu->msr.rsvd3 = smsr.u.fields.rsvd3;
  481.     }
  482.     else{
  483.         scr.u.fields.lt = cpu->cr.lt;
  484.         scr.u.fields.gt = cpu->cr.gt;
  485.         scr.u.fields.eq = cpu->cr.eq;
  486.         scr.u.fields.so = cpu->cr.so;
  487.         scr.u.fields.fx = cpu->cr.fx;
  488.         scr.u.fields.fex = cpu->cr.fex;
  489.         scr.u.fields.vx = cpu->cr.vx;
  490.         scr.u.fields.ox = cpu->cr.ox;
  491.         scr.u.fields.rsvd = cpu->cr.rsvd;
  492.         scr.u.word = SWAP_LONG(scr.u.word);
  493.         memcpy(&(cpu->cr), &scr, sizeof(struct swapped_m98k_cr));
  494.  
  495.         sxer.u.fields.byte_count = cpu->xer.byte_count;
  496.         sxer.u.fields.rsvd2 = cpu->xer.rsvd2;
  497.         sxer.u.fields.byte = cpu->xer.byte;
  498.         sxer.u.fields.rsvd1 = cpu->xer.rsvd1;
  499.         sxer.u.fields.ca = cpu->xer.ca;
  500.         sxer.u.fields.ov = cpu->xer.ov;
  501.         sxer.u.fields.so = cpu->xer.so;
  502.         sxer.u.word = SWAP_LONG(sxer.u.word);
  503.         memcpy(&(cpu->xer), &sxer, sizeof(struct swapped_m98k_xer));
  504.  
  505.         smsr.u.fields.psfr = cpu->msr.psfr;
  506.         smsr.u.fields.rsvd2 = cpu->msr.rsvd2;
  507.         smsr.u.fields.dr = cpu->msr.dr;
  508.         smsr.u.fields.ir = cpu->msr.ir;
  509.         smsr.u.fields.ip = cpu->msr.ip;
  510.         smsr.u.fields.rsvd1 = cpu->msr.rsvd1;
  511.         smsr.u.fields.fe1 = cpu->msr.fe1;
  512.         smsr.u.fields.be = cpu->msr.be;
  513.         smsr.u.fields.se = cpu->msr.se;
  514.         smsr.u.fields.fe0 = cpu->msr.fe0;
  515.         smsr.u.fields.me = cpu->msr.me;
  516.         smsr.u.fields.fp = cpu->msr.fp;
  517.         smsr.u.fields.pr = cpu->msr.pr;
  518.         smsr.u.fields.ee = cpu->msr.ee;
  519.         smsr.u.fields.rsvd3 = cpu->msr.rsvd3;
  520.         smsr.u.word = SWAP_LONG(smsr.u.word);
  521.         memcpy(&(cpu->msr), &smsr, sizeof(struct swapped_m98k_msr));
  522.     }
  523. }
  524.  
  525. __private_extern__
  526. void
  527. swap_m88k_thread_state_grf_t(
  528. m88k_thread_state_grf_t *cpu,
  529. enum byte_sex target_byte_sex)
  530. {
  531.     cpu->r1 = SWAP_LONG(cpu->r1);
  532.     cpu->r2 = SWAP_LONG(cpu->r2);
  533.     cpu->r3 = SWAP_LONG(cpu->r3);
  534.     cpu->r4 = SWAP_LONG(cpu->r4);
  535.     cpu->r5 = SWAP_LONG(cpu->r5);
  536.     cpu->r6 = SWAP_LONG(cpu->r6);
  537.     cpu->r7 = SWAP_LONG(cpu->r7);
  538.     cpu->r8 = SWAP_LONG(cpu->r8);
  539.     cpu->r9 = SWAP_LONG(cpu->r9);
  540.     cpu->r10 = SWAP_LONG(cpu->r10);
  541.     cpu->r11 = SWAP_LONG(cpu->r11);
  542.     cpu->r12 = SWAP_LONG(cpu->r12);
  543.     cpu->r13 = SWAP_LONG(cpu->r13);
  544.     cpu->r14 = SWAP_LONG(cpu->r14);
  545.     cpu->r15 = SWAP_LONG(cpu->r15);
  546.     cpu->r16 = SWAP_LONG(cpu->r16);
  547.     cpu->r17 = SWAP_LONG(cpu->r17);
  548.     cpu->r18 = SWAP_LONG(cpu->r18);
  549.     cpu->r19 = SWAP_LONG(cpu->r19);
  550.     cpu->r20 = SWAP_LONG(cpu->r20);
  551.     cpu->r21 = SWAP_LONG(cpu->r21);
  552.     cpu->r22 = SWAP_LONG(cpu->r22);
  553.     cpu->r23 = SWAP_LONG(cpu->r23);
  554.     cpu->r24 = SWAP_LONG(cpu->r24);
  555.     cpu->r25 = SWAP_LONG(cpu->r25);
  556.     cpu->r26 = SWAP_LONG(cpu->r26);
  557.     cpu->r27 = SWAP_LONG(cpu->r27);
  558.     cpu->r28 = SWAP_LONG(cpu->r28);
  559.     cpu->r29 = SWAP_LONG(cpu->r29);
  560.     cpu->r30 = SWAP_LONG(cpu->r30);
  561.     cpu->r31 = SWAP_LONG(cpu->r31);
  562.     cpu->xip = SWAP_LONG(cpu->xip);
  563.     cpu->xip_in_bd = SWAP_LONG(cpu->xip_in_bd);
  564.     cpu->nip = SWAP_LONG(cpu->nip);
  565. }
  566.  
  567. __private_extern__
  568. void
  569. swap_m88k_thread_state_xrf_t(
  570. m88k_thread_state_xrf_t *fpu,
  571. enum byte_sex target_byte_sex)
  572. {
  573.     enum byte_sex host_byte_sex;
  574.  
  575.     struct swapped_m88k_fpsr {
  576.     union {
  577.         struct {
  578.         unsigned    afinx:BIT_WIDTH(0);
  579.         unsigned    afovf:BIT_WIDTH(1);
  580.         unsigned    afunf:BIT_WIDTH(2);
  581.         unsigned    afdvz:BIT_WIDTH(3);
  582.         unsigned    afinv:BIT_WIDTH(4);
  583.         unsigned    :BITS_WIDTH(15,5);
  584.         unsigned    xmod:BIT_WIDTH(16);
  585.         unsigned    :BITS_WIDTH(31,17);
  586.         } fields;
  587.         unsigned long word;
  588.     } u;
  589.     } ssr;
  590.     struct swapped_m88k_fpcr {
  591.     union {
  592.         struct {
  593.         unsigned    efinx:BIT_WIDTH(0);
  594.         unsigned    efovf:BIT_WIDTH(1);
  595.         unsigned    efunf:BIT_WIDTH(2);
  596.         unsigned    efdvz:BIT_WIDTH(3);
  597.         unsigned    efinv:BIT_WIDTH(4);
  598.         unsigned    :BITS_WIDTH(13,5);
  599.         m88k_fpcr_rm_t    rm:BITS_WIDTH(15,14);
  600.         unsigned    :BITS_WIDTH(31,16);
  601.         } fields;
  602.         unsigned long word;
  603.     } u;
  604.     } scr;
  605.  
  606.     host_byte_sex = get_host_byte_sex();
  607.  
  608.     fpu->x1.x[0] = SWAP_LONG(fpu->x1.x[0]);
  609.     fpu->x1.x[1] = SWAP_LONG(fpu->x1.x[1]);
  610.     fpu->x1.x[2] = SWAP_LONG(fpu->x1.x[2]);
  611.     fpu->x1.x[3] = SWAP_LONG(fpu->x1.x[3]);
  612.     fpu->x2.x[0] = SWAP_LONG(fpu->x2.x[0]);
  613.     fpu->x2.x[1] = SWAP_LONG(fpu->x2.x[1]);
  614.     fpu->x2.x[2] = SWAP_LONG(fpu->x2.x[2]);
  615.     fpu->x2.x[3] = SWAP_LONG(fpu->x2.x[3]);
  616.     fpu->x3.x[0] = SWAP_LONG(fpu->x3.x[0]);
  617.     fpu->x3.x[1] = SWAP_LONG(fpu->x3.x[1]);
  618.     fpu->x3.x[2] = SWAP_LONG(fpu->x3.x[2]);
  619.     fpu->x3.x[3] = SWAP_LONG(fpu->x3.x[3]);
  620.     fpu->x4.x[0] = SWAP_LONG(fpu->x4.x[0]);
  621.     fpu->x4.x[1] = SWAP_LONG(fpu->x4.x[1]);
  622.     fpu->x4.x[2] = SWAP_LONG(fpu->x4.x[2]);
  623.     fpu->x4.x[3] = SWAP_LONG(fpu->x4.x[3]);
  624.     fpu->x5.x[0] = SWAP_LONG(fpu->x5.x[0]);
  625.     fpu->x5.x[1] = SWAP_LONG(fpu->x5.x[1]);
  626.     fpu->x5.x[2] = SWAP_LONG(fpu->x5.x[2]);
  627.     fpu->x5.x[3] = SWAP_LONG(fpu->x5.x[3]);
  628.     fpu->x6.x[0] = SWAP_LONG(fpu->x6.x[0]);
  629.     fpu->x6.x[1] = SWAP_LONG(fpu->x6.x[1]);
  630.     fpu->x6.x[2] = SWAP_LONG(fpu->x6.x[2]);
  631.     fpu->x6.x[3] = SWAP_LONG(fpu->x6.x[3]);
  632.     fpu->x7.x[0] = SWAP_LONG(fpu->x7.x[0]);
  633.     fpu->x7.x[1] = SWAP_LONG(fpu->x7.x[1]);
  634.     fpu->x7.x[2] = SWAP_LONG(fpu->x7.x[2]);
  635.     fpu->x7.x[3] = SWAP_LONG(fpu->x7.x[3]);
  636.     fpu->x8.x[0] = SWAP_LONG(fpu->x8.x[0]);
  637.     fpu->x8.x[1] = SWAP_LONG(fpu->x8.x[1]);
  638.     fpu->x8.x[2] = SWAP_LONG(fpu->x8.x[2]);
  639.     fpu->x8.x[3] = SWAP_LONG(fpu->x8.x[3]);
  640.     fpu->x9.x[0] = SWAP_LONG(fpu->x9.x[0]);
  641.     fpu->x9.x[1] = SWAP_LONG(fpu->x9.x[1]);
  642.     fpu->x9.x[2] = SWAP_LONG(fpu->x9.x[2]);
  643.     fpu->x9.x[3] = SWAP_LONG(fpu->x9.x[3]);
  644.     fpu->x10.x[0] = SWAP_LONG(fpu->x10.x[0]);
  645.     fpu->x10.x[1] = SWAP_LONG(fpu->x10.x[1]);
  646.     fpu->x10.x[2] = SWAP_LONG(fpu->x10.x[2]);
  647.     fpu->x10.x[3] = SWAP_LONG(fpu->x10.x[3]);
  648.     fpu->x11.x[0] = SWAP_LONG(fpu->x11.x[0]);
  649.     fpu->x11.x[1] = SWAP_LONG(fpu->x11.x[1]);
  650.     fpu->x11.x[2] = SWAP_LONG(fpu->x11.x[2]);
  651.     fpu->x11.x[3] = SWAP_LONG(fpu->x11.x[3]);
  652.     fpu->x12.x[0] = SWAP_LONG(fpu->x12.x[0]);
  653.     fpu->x12.x[1] = SWAP_LONG(fpu->x12.x[1]);
  654.     fpu->x12.x[2] = SWAP_LONG(fpu->x12.x[2]);
  655.     fpu->x12.x[3] = SWAP_LONG(fpu->x12.x[3]);
  656.     fpu->x13.x[0] = SWAP_LONG(fpu->x13.x[0]);
  657.     fpu->x13.x[1] = SWAP_LONG(fpu->x13.x[1]);
  658.     fpu->x13.x[2] = SWAP_LONG(fpu->x13.x[2]);
  659.     fpu->x13.x[3] = SWAP_LONG(fpu->x13.x[3]);
  660.     fpu->x14.x[0] = SWAP_LONG(fpu->x14.x[0]);
  661.     fpu->x14.x[1] = SWAP_LONG(fpu->x14.x[1]);
  662.     fpu->x14.x[2] = SWAP_LONG(fpu->x14.x[2]);
  663.     fpu->x14.x[3] = SWAP_LONG(fpu->x14.x[3]);
  664.     fpu->x15.x[0] = SWAP_LONG(fpu->x15.x[0]);
  665.     fpu->x15.x[1] = SWAP_LONG(fpu->x15.x[1]);
  666.     fpu->x15.x[2] = SWAP_LONG(fpu->x15.x[2]);
  667.     fpu->x15.x[3] = SWAP_LONG(fpu->x15.x[3]);
  668.     fpu->x16.x[0] = SWAP_LONG(fpu->x16.x[0]);
  669.     fpu->x16.x[1] = SWAP_LONG(fpu->x16.x[1]);
  670.     fpu->x16.x[2] = SWAP_LONG(fpu->x16.x[2]);
  671.     fpu->x16.x[3] = SWAP_LONG(fpu->x16.x[3]);
  672.     fpu->x17.x[0] = SWAP_LONG(fpu->x17.x[0]);
  673.     fpu->x17.x[1] = SWAP_LONG(fpu->x17.x[1]);
  674.     fpu->x17.x[2] = SWAP_LONG(fpu->x17.x[2]);
  675.     fpu->x17.x[3] = SWAP_LONG(fpu->x17.x[3]);
  676.     fpu->x18.x[0] = SWAP_LONG(fpu->x18.x[0]);
  677.     fpu->x18.x[1] = SWAP_LONG(fpu->x18.x[1]);
  678.     fpu->x18.x[2] = SWAP_LONG(fpu->x18.x[2]);
  679.     fpu->x18.x[3] = SWAP_LONG(fpu->x18.x[3]);
  680.     fpu->x19.x[0] = SWAP_LONG(fpu->x19.x[0]);
  681.     fpu->x19.x[1] = SWAP_LONG(fpu->x19.x[1]);
  682.     fpu->x19.x[2] = SWAP_LONG(fpu->x19.x[2]);
  683.     fpu->x19.x[3] = SWAP_LONG(fpu->x19.x[3]);
  684.     fpu->x20.x[0] = SWAP_LONG(fpu->x20.x[0]);
  685.     fpu->x20.x[1] = SWAP_LONG(fpu->x20.x[1]);
  686.     fpu->x20.x[2] = SWAP_LONG(fpu->x20.x[2]);
  687.     fpu->x20.x[3] = SWAP_LONG(fpu->x20.x[3]);
  688.     fpu->x21.x[0] = SWAP_LONG(fpu->x21.x[0]);
  689.     fpu->x21.x[1] = SWAP_LONG(fpu->x21.x[1]);
  690.     fpu->x21.x[2] = SWAP_LONG(fpu->x21.x[2]);
  691.     fpu->x21.x[3] = SWAP_LONG(fpu->x21.x[3]);
  692.     fpu->x22.x[0] = SWAP_LONG(fpu->x22.x[0]);
  693.     fpu->x22.x[1] = SWAP_LONG(fpu->x22.x[1]);
  694.     fpu->x22.x[2] = SWAP_LONG(fpu->x22.x[2]);
  695.     fpu->x22.x[3] = SWAP_LONG(fpu->x22.x[3]);
  696.     fpu->x23.x[0] = SWAP_LONG(fpu->x23.x[0]);
  697.     fpu->x23.x[1] = SWAP_LONG(fpu->x23.x[1]);
  698.     fpu->x23.x[2] = SWAP_LONG(fpu->x23.x[2]);
  699.     fpu->x23.x[3] = SWAP_LONG(fpu->x23.x[3]);
  700.     fpu->x24.x[0] = SWAP_LONG(fpu->x24.x[0]);
  701.     fpu->x24.x[1] = SWAP_LONG(fpu->x24.x[1]);
  702.     fpu->x24.x[2] = SWAP_LONG(fpu->x24.x[2]);
  703.     fpu->x24.x[3] = SWAP_LONG(fpu->x24.x[3]);
  704.     fpu->x25.x[0] = SWAP_LONG(fpu->x25.x[0]);
  705.     fpu->x25.x[1] = SWAP_LONG(fpu->x25.x[1]);
  706.     fpu->x25.x[2] = SWAP_LONG(fpu->x25.x[2]);
  707.     fpu->x25.x[3] = SWAP_LONG(fpu->x25.x[3]);
  708.     fpu->x26.x[0] = SWAP_LONG(fpu->x26.x[0]);
  709.     fpu->x26.x[1] = SWAP_LONG(fpu->x26.x[1]);
  710.     fpu->x26.x[2] = SWAP_LONG(fpu->x26.x[2]);
  711.     fpu->x26.x[3] = SWAP_LONG(fpu->x26.x[3]);
  712.     fpu->x27.x[0] = SWAP_LONG(fpu->x27.x[0]);
  713.     fpu->x27.x[1] = SWAP_LONG(fpu->x27.x[1]);
  714.     fpu->x27.x[2] = SWAP_LONG(fpu->x27.x[2]);
  715.     fpu->x27.x[3] = SWAP_LONG(fpu->x27.x[3]);
  716.     fpu->x28.x[0] = SWAP_LONG(fpu->x28.x[0]);
  717.     fpu->x28.x[1] = SWAP_LONG(fpu->x28.x[1]);
  718.     fpu->x28.x[2] = SWAP_LONG(fpu->x28.x[2]);
  719.     fpu->x28.x[3] = SWAP_LONG(fpu->x28.x[3]);
  720.     fpu->x29.x[0] = SWAP_LONG(fpu->x29.x[0]);
  721.     fpu->x29.x[1] = SWAP_LONG(fpu->x29.x[1]);
  722.     fpu->x29.x[2] = SWAP_LONG(fpu->x29.x[2]);
  723.     fpu->x29.x[3] = SWAP_LONG(fpu->x29.x[3]);
  724.     fpu->x30.x[0] = SWAP_LONG(fpu->x30.x[0]);
  725.     fpu->x30.x[1] = SWAP_LONG(fpu->x30.x[1]);
  726.     fpu->x30.x[2] = SWAP_LONG(fpu->x30.x[2]);
  727.     fpu->x30.x[3] = SWAP_LONG(fpu->x30.x[3]);
  728.     fpu->x31.x[0] = SWAP_LONG(fpu->x31.x[0]);
  729.     fpu->x31.x[1] = SWAP_LONG(fpu->x31.x[1]);
  730.     fpu->x31.x[2] = SWAP_LONG(fpu->x31.x[2]);
  731.     fpu->x31.x[3] = SWAP_LONG(fpu->x31.x[3]);
  732.  
  733.     if(target_byte_sex == host_byte_sex){
  734.         memcpy(&ssr, &(fpu->fpsr), sizeof(struct swapped_m88k_fpsr));
  735.         ssr.u.word = SWAP_LONG(ssr.u.word);
  736.         fpu->fpsr.afinx = ssr.u.fields.afinx;
  737.         fpu->fpsr.afovf = ssr.u.fields.afovf;
  738.         fpu->fpsr.afunf = ssr.u.fields.afunf;
  739.         fpu->fpsr.afdvz = ssr.u.fields.afdvz;
  740.         fpu->fpsr.afinv = ssr.u.fields.afinv;
  741.         fpu->fpsr.xmod = ssr.u.fields.xmod;
  742.  
  743.         memcpy(&scr, &(fpu->fpcr), sizeof(struct swapped_m88k_fpcr));
  744.         scr.u.word = SWAP_LONG(scr.u.word);
  745.         fpu->fpcr.efinx = scr.u.fields.efinx;
  746.         fpu->fpcr.efovf = scr.u.fields.efovf;
  747.         fpu->fpcr.efunf = scr.u.fields.efunf;
  748.         fpu->fpcr.efdvz = scr.u.fields.efdvz;
  749.         fpu->fpcr.efinv = scr.u.fields.efinv;
  750.         fpu->fpcr.rm = scr.u.fields.rm;
  751.     }
  752.     else{
  753.         ssr.u.fields.afinx = fpu->fpsr.afinx;
  754.         ssr.u.fields.afovf = fpu->fpsr.afovf;
  755.         ssr.u.fields.afunf = fpu->fpsr.afunf;
  756.         ssr.u.fields.afdvz = fpu->fpsr.afdvz;
  757.         ssr.u.fields.afinv = fpu->fpsr.afinv;
  758.         ssr.u.fields.xmod = fpu->fpsr.xmod;
  759.         ssr.u.word = SWAP_LONG(ssr.u.word);
  760.         memcpy(&(fpu->fpsr), &ssr, sizeof(struct swapped_m88k_fpsr));
  761.  
  762.         scr.u.fields.efinx = fpu->fpcr.efinx;
  763.         scr.u.fields.efovf = fpu->fpcr.efovf;
  764.         scr.u.fields.efunf = fpu->fpcr.efunf;
  765.         scr.u.fields.efdvz = fpu->fpcr.efdvz;
  766.         scr.u.fields.efinv = fpu->fpcr.efinv;
  767.         scr.u.fields.rm = fpu->fpcr.rm;
  768.         scr.u.word = SWAP_LONG(scr.u.word);
  769.         memcpy(&(fpu->fpcr), &scr, sizeof(struct swapped_m88k_fpcr));
  770.     }
  771. }
  772.  
  773. __private_extern__
  774. void
  775. swap_m88k_thread_state_user_t(
  776. m88k_thread_state_user_t *user,
  777. enum byte_sex target_byte_sex)
  778. {
  779.     user->user = SWAP_LONG(user->user);
  780. }
  781.  
  782. __private_extern__
  783. void
  784. swap_m88110_thread_state_impl_t(
  785. m88110_thread_state_impl_t *spu,
  786. enum byte_sex target_byte_sex)
  787. {
  788.     unsigned long i;
  789.     enum byte_sex host_byte_sex;
  790.  
  791.     struct swapped_m88110_bp_ctrl {
  792.     union {
  793.         struct {
  794.         unsigned    v:BIT_WIDTH(0);
  795.         m88110_match_t    addr_match:BITS_WIDTH(12,1);
  796.         unsigned    :BITS_WIDTH(26,13);
  797.         unsigned    rwm:BIT_WIDTH(27);
  798.         unsigned    rw:BIT_WIDTH(28);
  799.         unsigned    :BITS_WIDTH(31,29);
  800.         } fields;
  801.         unsigned long word;
  802.     } u;
  803.     } sbpc;
  804.  
  805.     struct swap_m88110_psr {
  806.     union {
  807.         struct {
  808.         unsigned    :BITS_WIDTH(1,0);
  809.         unsigned    mxm_dis:BIT_WIDTH(2);
  810.         unsigned    sfu1dis:BIT_WIDTH(3);
  811.         unsigned    :BITS_WIDTH(22,4);
  812.         unsigned    trace:BIT_WIDTH(23);
  813.         unsigned    :BIT_WIDTH(24);
  814.         unsigned    sm:BIT_WIDTH(25);
  815.         unsigned    sgn_imd:BIT_WIDTH(26);
  816.         unsigned    :BIT_WIDTH(27);
  817.         unsigned    c:BIT_WIDTH(28);
  818.         unsigned    se:BIT_WIDTH(29);
  819.         unsigned    le:BIT_WIDTH(30);
  820.         unsigned    supr:BIT_WIDTH(31);
  821.         } fields;
  822.         unsigned long word;
  823.     } u;
  824.     } spsr;
  825.  
  826.     struct swapped_m88110_fp_trap_status {
  827.     union {
  828.         struct {
  829.         unsigned    efinx:BIT_WIDTH(0);
  830.         unsigned    efovf:BIT_WIDTH(1);
  831.         unsigned    efunf:BIT_WIDTH(2);
  832.         unsigned    efdvz:BIT_WIDTH(3);
  833.         unsigned    efinv:BIT_WIDTH(4);
  834.         unsigned    priv:BIT_WIDTH(5);
  835.         unsigned    unimp:BIT_WIDTH(6);
  836.         unsigned    int:BIT_WIDTH(7);
  837.         unsigned    sfu1_disabled:BIT_WIDTH(8);
  838.         unsigned    :BITS_WIDTH(13,9);
  839.         m88110_iresult_size_t    iresult_size:BITS_WIDTH(15,14);
  840.         unsigned    :BITS_WIDTH(31,16);
  841.         } fields;
  842.         unsigned long word;
  843.     } u;
  844.     } sfps;
  845.  
  846.     host_byte_sex = get_host_byte_sex();
  847.  
  848.     if(target_byte_sex == host_byte_sex){
  849.         for(i = 0; i < M88110_N_DATA_BP; i++){
  850.         spu->data_bp[i].addr = SWAP_LONG(spu->data_bp[i].addr);
  851.         memcpy(&sbpc, &(spu->data_bp[i].ctrl),
  852.                sizeof(struct swapped_m88110_bp_ctrl));
  853.         sbpc.u.word = SWAP_LONG(sbpc.u.word);
  854.         spu->data_bp[i].ctrl.v = sbpc.u.fields.v;
  855.         spu->data_bp[i].ctrl.addr_match = sbpc.u.fields.addr_match;
  856.         spu->data_bp[i].ctrl.rwm = sbpc.u.fields.rwm;
  857.         spu->data_bp[i].ctrl.rw = sbpc.u.fields.rw;
  858.         }
  859.  
  860.         memcpy(&spsr, &(spu->psr), sizeof(struct swap_m88110_psr));
  861.         spsr.u.word = SWAP_LONG(spsr.u.word);
  862.         spu->psr.mxm_dis = spsr.u.fields.mxm_dis;
  863.         spu->psr.sfu1dis = spsr.u.fields.sfu1dis;
  864.         spu->psr.trace = spsr.u.fields.trace;
  865.         spu->psr.sm = spsr.u.fields.sm;
  866.         spu->psr.sgn_imd = spsr.u.fields.sgn_imd;
  867.         spu->psr.c = spsr.u.fields.c;
  868.         spu->psr.se = spsr.u.fields.se;
  869.         spu->psr.le = spsr.u.fields.le;
  870.         spu->psr.supr = spsr.u.fields.supr;
  871.  
  872.         memcpy(&sfps, &(spu->fp_trap_status),
  873.            sizeof(struct swapped_m88110_fp_trap_status));
  874.         sfps.u.word = SWAP_LONG(sfps.u.word);
  875.         spu->fp_trap_status.efinx = sfps.u.fields.efinx;
  876.         spu->fp_trap_status.efovf = sfps.u.fields.efovf;
  877.         spu->fp_trap_status.efunf = sfps.u.fields.efunf;
  878.         spu->fp_trap_status.efdvz = sfps.u.fields.efdvz;
  879.         spu->fp_trap_status.efinv = sfps.u.fields.efinv;
  880.         spu->fp_trap_status.priv = sfps.u.fields.priv;
  881.         spu->fp_trap_status.unimp = sfps.u.fields.unimp;
  882.         spu->fp_trap_status.sfu1_disabled = sfps.u.fields.sfu1_disabled;
  883.         spu->fp_trap_status.iresult_size = sfps.u.fields.iresult_size;
  884.     }
  885.     else{
  886.         for(i = 0; i < M88110_N_DATA_BP; i++){
  887.         spu->data_bp[i].addr = SWAP_LONG(spu->data_bp[i].addr);
  888.         sbpc.u.fields.v = spu->data_bp[i].ctrl.v;
  889.         sbpc.u.fields.addr_match = spu->data_bp[i].ctrl.addr_match;
  890.         sbpc.u.fields.rwm = spu->data_bp[i].ctrl.rwm;
  891.         sbpc.u.fields.rw = spu->data_bp[i].ctrl.rw;
  892.         sbpc.u.word = SWAP_LONG(sbpc.u.word);
  893.         memcpy(&(spu->data_bp[i].ctrl), &sbpc,
  894.                sizeof(struct swapped_m88110_bp_ctrl));
  895.         }
  896.  
  897.         spsr.u.fields.mxm_dis = spu->psr.mxm_dis;
  898.         spsr.u.fields.sfu1dis = spu->psr.sfu1dis;
  899.         spsr.u.fields.trace = spu->psr.trace;
  900.         spsr.u.fields.sm = spu->psr.sm;
  901.         spsr.u.fields.sgn_imd = spu->psr.sgn_imd;
  902.         spsr.u.fields.c = spu->psr.c;
  903.         spsr.u.fields.se = spu->psr.se;
  904.         spsr.u.fields.le = spu->psr.le;
  905.         spsr.u.fields.supr = spu->psr.supr;
  906.         spsr.u.word = SWAP_LONG(spsr.u.word);
  907.         memcpy(&(spu->psr), &spsr, sizeof(struct swap_m88110_psr));
  908.  
  909.         sfps.u.fields.efinx = spu->fp_trap_status.efinx;
  910.         sfps.u.fields.efovf = spu->fp_trap_status.efovf;
  911.         sfps.u.fields.efunf = spu->fp_trap_status.efunf;
  912.         sfps.u.fields.efdvz = spu->fp_trap_status.efdvz;
  913.         sfps.u.fields.efinv = spu->fp_trap_status.efinv;
  914.         sfps.u.fields.priv = spu->fp_trap_status.priv;
  915.         sfps.u.fields.unimp = spu->fp_trap_status.unimp;
  916.         sfps.u.fields.sfu1_disabled = spu->fp_trap_status.sfu1_disabled;
  917.         sfps.u.fields.iresult_size = spu->fp_trap_status.iresult_size;
  918.         sfps.u.word = SWAP_LONG(sfps.u.word);
  919.         memcpy(&(spu->fp_trap_status), &sfps,
  920.            sizeof(struct swapped_m88110_fp_trap_status));
  921.     }
  922.     spu->intermediate_result.x[0] =
  923.         SWAP_LONG(spu->intermediate_result.x[0]);
  924.     spu->intermediate_result.x[1] =
  925.         SWAP_LONG(spu->intermediate_result.x[1]);
  926.     spu->intermediate_result.x[2] =
  927.         SWAP_LONG(spu->intermediate_result.x[2]);
  928.     spu->intermediate_result.x[3] =
  929.         SWAP_LONG(spu->intermediate_result.x[3]);
  930. }
  931.  
  932. __private_extern__
  933. void
  934. swap_i860_thread_state_regs(
  935. struct i860_thread_state_regs *cpu,
  936. enum byte_sex target_byte_sex)
  937. {
  938.     unsigned long i;
  939.  
  940.     for(i = 0; i < 31; i++)
  941.         cpu->ireg[i] = SWAP_LONG(cpu->ireg[i]);
  942.     for(i = 0; i < 30; i++)
  943.         cpu->freg[i] = SWAP_LONG(cpu->freg[i]);
  944.     cpu->psr = SWAP_LONG(cpu->psr);
  945.     cpu->epsr = SWAP_LONG(cpu->epsr);
  946.     cpu->db = SWAP_LONG(cpu->db);
  947.     cpu->pc = SWAP_LONG(cpu->pc);
  948.     cpu->_padding_ = SWAP_LONG(cpu->_padding_);
  949.     cpu->Mres3 = SWAP_DOUBLE(cpu->Mres3);
  950.     cpu->Ares3 = SWAP_DOUBLE(cpu->Ares3);
  951.     cpu->Mres2 = SWAP_DOUBLE(cpu->Mres2);
  952.     cpu->Ares2 = SWAP_DOUBLE(cpu->Ares2);
  953.     cpu->Mres1 = SWAP_DOUBLE(cpu->Mres1);
  954.     cpu->Ares1 = SWAP_DOUBLE(cpu->Ares1);
  955.     cpu->Ires1 = SWAP_DOUBLE(cpu->Ires1);
  956.     cpu->Lres3m = SWAP_DOUBLE(cpu->Lres3m);
  957.     cpu->Lres2m = SWAP_DOUBLE(cpu->Lres2m);
  958.     cpu->Lres1m = SWAP_DOUBLE(cpu->Lres1m);
  959.     cpu->KR = SWAP_DOUBLE(cpu->KR);
  960.     cpu->KI = SWAP_DOUBLE(cpu->KI);
  961.     cpu->T = SWAP_DOUBLE(cpu->T);
  962.     cpu->Fsr3 = SWAP_LONG(cpu->Fsr3);
  963.     cpu->Fsr2 = SWAP_LONG(cpu->Fsr2);
  964.     cpu->Fsr1 = SWAP_LONG(cpu->Fsr1);
  965.     cpu->Mergelo32 = SWAP_LONG(cpu->Mergelo32);
  966.     cpu->Mergehi32 = SWAP_LONG(cpu->Mergehi32);
  967. }
  968.  
  969. __private_extern__
  970. void
  971. swap_i386_thread_state(
  972. i386_thread_state_t *cpu,
  973. enum byte_sex target_byte_sex)
  974. {
  975.     cpu->eax = SWAP_LONG(cpu->eax);
  976.     cpu->ebx = SWAP_LONG(cpu->ebx);
  977.     cpu->ecx = SWAP_LONG(cpu->ecx);
  978.     cpu->edx = SWAP_LONG(cpu->edx);
  979.     cpu->edi = SWAP_LONG(cpu->edi);
  980.     cpu->esi = SWAP_LONG(cpu->esi);
  981.     cpu->ebp = SWAP_LONG(cpu->ebp);
  982.     cpu->esp = SWAP_LONG(cpu->esp);
  983.     cpu->ss = SWAP_LONG(cpu->ss);
  984.     cpu->eflags = SWAP_LONG(cpu->eflags);
  985.     cpu->eip = SWAP_LONG(cpu->eip);
  986.     cpu->cs = SWAP_LONG(cpu->cs);
  987.     cpu->ds = SWAP_LONG(cpu->ds);
  988.     cpu->es = SWAP_LONG(cpu->es);
  989.     cpu->fs = SWAP_LONG(cpu->fs);
  990.     cpu->gs = SWAP_LONG(cpu->gs);
  991. }
  992.  
  993. __private_extern__
  994. void
  995. swap_i386_thread_fpstate(
  996. i386_thread_fpstate_t *fpu,
  997. enum byte_sex target_byte_sex)
  998. {
  999.     struct swapped_fp_control {
  1000.     union {
  1001.         struct {
  1002.         unsigned short
  1003.                 :3,
  1004.             /*inf*/ :1,
  1005.             rc        :2,
  1006.             pc        :2,
  1007.                 :2,
  1008.             precis  :1,
  1009.             undfl   :1,
  1010.             ovrfl   :1,
  1011.             zdiv    :1,
  1012.             denorm  :1,
  1013.             invalid :1;
  1014.         } fields;
  1015.         unsigned short half;
  1016.     } u;
  1017.     } sfpc;
  1018.  
  1019.     struct swapped_fp_status {
  1020.     union {
  1021.         struct {
  1022.         unsigned short
  1023.             busy    :1,
  1024.             c3        :1,
  1025.             tos        :3,
  1026.             c2        :1,
  1027.             c1        :1,
  1028.             c0        :1,
  1029.             errsumm :1,
  1030.             stkflt  :1,
  1031.             precis  :1,
  1032.             undfl   :1,
  1033.             ovrfl   :1,
  1034.             zdiv    :1,
  1035.             denorm  :1,
  1036.             invalid :1;
  1037.         } fields;
  1038.         unsigned short half;
  1039.     } u;
  1040.     } sfps;
  1041.  
  1042.     struct swapped_fp_tag {
  1043.     union {
  1044.         struct {
  1045.         unsigned short
  1046.             tag7 :2,
  1047.             tag6 :2,
  1048.             tag5 :2,
  1049.             tag4 :2,
  1050.             tag3 :2,
  1051.             tag2 :2,
  1052.             tag1 :2,
  1053.             tag0 :2;
  1054.         } fields;
  1055.         unsigned short half;
  1056.     } u;
  1057.     } sfpt;
  1058.  
  1059.     struct swapped_fp_data_reg {
  1060.     unsigned short mant;
  1061.     unsigned short mant1 :16,
  1062.                mant2 :16,
  1063.                mant3 :16;
  1064.     union {
  1065.         struct {
  1066.         unsigned short sign :1,
  1067.                    exp  :15;
  1068.         } fields;
  1069.         unsigned short half;
  1070.     } u;
  1071.     } sfpd;
  1072.  
  1073.     struct swapped_sel {
  1074.     union {
  1075.         struct {
  1076.             unsigned short
  1077.             index :13,
  1078.             ti    :1,
  1079.             rpl   :2;
  1080.         } fields;
  1081.         unsigned short half;
  1082.     } u;
  1083.     } ss;
  1084.  
  1085.     enum byte_sex host_byte_sex;
  1086.     unsigned long i;
  1087.  
  1088.     host_byte_sex = get_host_byte_sex();
  1089.  
  1090.     fpu->environ.ip = SWAP_LONG(fpu->environ.ip);
  1091.     fpu->environ.opcode = SWAP_SHORT(fpu->environ.opcode);
  1092.     fpu->environ.dp = SWAP_LONG(fpu->environ.dp);
  1093.  
  1094.     if(target_byte_sex == host_byte_sex){
  1095.         memcpy(&sfpc, &(fpu->environ.control),
  1096.            sizeof(struct swapped_fp_control));
  1097.         sfpc.u.half = SWAP_SHORT(sfpc.u.half);
  1098.         fpu->environ.control.rc = sfpc.u.fields.rc;
  1099.         fpu->environ.control.pc = sfpc.u.fields.pc;
  1100.         fpu->environ.control.precis = sfpc.u.fields.precis;
  1101.         fpu->environ.control.undfl = sfpc.u.fields.undfl;
  1102.         fpu->environ.control.ovrfl = sfpc.u.fields.ovrfl;
  1103.         fpu->environ.control.zdiv = sfpc.u.fields.zdiv;
  1104.         fpu->environ.control.denorm = sfpc.u.fields.denorm;
  1105.         fpu->environ.control.invalid = sfpc.u.fields.invalid;
  1106.  
  1107.         memcpy(&sfps, &(fpu->environ.status),
  1108.            sizeof(struct swapped_fp_status));
  1109.         sfps.u.half = SWAP_SHORT(sfps.u.half);
  1110.         fpu->environ.status.busy = sfps.u.fields.busy;
  1111.         fpu->environ.status.c3 = sfps.u.fields.c3;
  1112.         fpu->environ.status.tos = sfps.u.fields.tos;
  1113.         fpu->environ.status.c2 = sfps.u.fields.c2;
  1114.         fpu->environ.status.c1 = sfps.u.fields.c1;
  1115.         fpu->environ.status.c0 = sfps.u.fields.c0;
  1116.         fpu->environ.status.errsumm = sfps.u.fields.errsumm;
  1117.         fpu->environ.status.stkflt = sfps.u.fields.stkflt;
  1118.         fpu->environ.status.precis = sfps.u.fields.precis;
  1119.         fpu->environ.status.undfl = sfps.u.fields.undfl;
  1120.         fpu->environ.status.ovrfl = sfps.u.fields.ovrfl;
  1121.         fpu->environ.status.zdiv = sfps.u.fields.zdiv;
  1122.         fpu->environ.status.denorm = sfps.u.fields.denorm;
  1123.         fpu->environ.status.invalid = sfps.u.fields.invalid;
  1124.  
  1125.         memcpy(&sfpt, &(fpu->environ.tag),
  1126.            sizeof(struct swapped_fp_tag));
  1127.         sfpt.u.half = SWAP_SHORT(sfpt.u.half);
  1128.         fpu->environ.tag.tag7 = sfpt.u.fields.tag7;
  1129.         fpu->environ.tag.tag6 = sfpt.u.fields.tag6;
  1130.         fpu->environ.tag.tag5 = sfpt.u.fields.tag5;
  1131.         fpu->environ.tag.tag4 = sfpt.u.fields.tag4;
  1132.         fpu->environ.tag.tag3 = sfpt.u.fields.tag3;
  1133.         fpu->environ.tag.tag2 = sfpt.u.fields.tag2;
  1134.         fpu->environ.tag.tag1 = sfpt.u.fields.tag1;
  1135.         fpu->environ.tag.tag0 = sfpt.u.fields.tag0;
  1136.  
  1137.         memcpy(&ss, &(fpu->environ.cs),
  1138.            sizeof(struct swapped_sel));
  1139.         ss.u.half = SWAP_SHORT(ss.u.half);
  1140.         fpu->environ.cs.index = ss.u.fields.index;
  1141.         fpu->environ.cs.ti = ss.u.fields.ti;
  1142.         fpu->environ.cs.rpl = ss.u.fields.rpl;
  1143.  
  1144.         memcpy(&ss, &(fpu->environ.ds),
  1145.            sizeof(struct swapped_sel));
  1146.         ss.u.half = SWAP_SHORT(ss.u.half);
  1147.         fpu->environ.ds.index = ss.u.fields.index;
  1148.         fpu->environ.ds.ti = ss.u.fields.ti;
  1149.         fpu->environ.ds.rpl = ss.u.fields.rpl;
  1150.     
  1151.         for(i = 0; i < 8; i++){
  1152.         memcpy(&sfpd, &(fpu->stack.ST[i]),
  1153.                sizeof(struct swapped_fp_data_reg));
  1154.         fpu->stack.ST[i].mant = SWAP_SHORT(sfpd.mant);
  1155.         fpu->stack.ST[i].mant1 = SWAP_SHORT(sfpd.mant1);
  1156.         fpu->stack.ST[i].mant2 = SWAP_SHORT(sfpd.mant2);
  1157.         fpu->stack.ST[i].mant3 = SWAP_SHORT(sfpd.mant3);
  1158.         sfpd.u.half = SWAP_SHORT(sfpd.u.half);
  1159.         fpu->stack.ST[i].exp = sfpd.u.fields.exp;
  1160.         fpu->stack.ST[i].sign = sfpd.u.fields.sign;
  1161.         }
  1162.     }
  1163.     else{
  1164.         sfpc.u.fields.rc = fpu->environ.control.rc;
  1165.         sfpc.u.fields.pc = fpu->environ.control.pc;
  1166.         sfpc.u.fields.precis = fpu->environ.control.precis;
  1167.         sfpc.u.fields.undfl = fpu->environ.control.undfl;
  1168.         sfpc.u.fields.ovrfl = fpu->environ.control.ovrfl;
  1169.         sfpc.u.fields.zdiv = fpu->environ.control.zdiv;
  1170.         sfpc.u.fields.denorm = fpu->environ.control.denorm;
  1171.         sfpc.u.fields.invalid = fpu->environ.control.invalid;
  1172.         sfpc.u.half = SWAP_SHORT(sfpc.u.half);
  1173.         memcpy(&(fpu->environ.control), &sfpc,
  1174.            sizeof(struct swapped_fp_control));
  1175.  
  1176.         sfps.u.fields.busy = fpu->environ.status.busy;
  1177.         sfps.u.fields.c3 = fpu->environ.status.c3;
  1178.         sfps.u.fields.tos = fpu->environ.status.tos;
  1179.         sfps.u.fields.c2 = fpu->environ.status.c2;
  1180.         sfps.u.fields.c1 = fpu->environ.status.c1;
  1181.         sfps.u.fields.c0 = fpu->environ.status.c0;
  1182.         sfps.u.fields.errsumm = fpu->environ.status.errsumm;
  1183.         sfps.u.fields.stkflt = fpu->environ.status.stkflt;
  1184.         sfps.u.fields.precis = fpu->environ.status.precis;
  1185.         sfps.u.fields.undfl = fpu->environ.status.undfl;
  1186.         sfps.u.fields.ovrfl = fpu->environ.status.ovrfl;
  1187.         sfps.u.fields.zdiv = fpu->environ.status.zdiv;
  1188.         sfps.u.fields.denorm = fpu->environ.status.denorm;
  1189.         sfps.u.fields.invalid = fpu->environ.status.invalid;
  1190.         sfps.u.half = SWAP_SHORT(sfps.u.half);
  1191.         memcpy(&(fpu->environ.status), &sfps,
  1192.            sizeof(struct swapped_fp_status));
  1193.  
  1194.         sfpt.u.fields.tag7 = fpu->environ.tag.tag7;
  1195.         sfpt.u.fields.tag6 = fpu->environ.tag.tag6;
  1196.         sfpt.u.fields.tag5 = fpu->environ.tag.tag5;
  1197.         sfpt.u.fields.tag4 = fpu->environ.tag.tag4;
  1198.         sfpt.u.fields.tag3 = fpu->environ.tag.tag3;
  1199.         sfpt.u.fields.tag2 = fpu->environ.tag.tag2;
  1200.         sfpt.u.fields.tag1 = fpu->environ.tag.tag1;
  1201.         sfpt.u.fields.tag0 = fpu->environ.tag.tag0;
  1202.         sfpt.u.half = SWAP_SHORT(sfpt.u.half);
  1203.         memcpy(&(fpu->environ.tag), &sfpt,
  1204.            sizeof(struct swapped_fp_tag));
  1205.  
  1206.         ss.u.fields.index = fpu->environ.cs.index;
  1207.         ss.u.fields.ti = fpu->environ.cs.ti;
  1208.         ss.u.fields.rpl = fpu->environ.cs.rpl;
  1209.         ss.u.half = SWAP_SHORT(ss.u.half);
  1210.         memcpy(&(fpu->environ.cs), &ss,
  1211.            sizeof(struct swapped_sel));
  1212.  
  1213.         ss.u.fields.index = fpu->environ.ds.index;
  1214.         ss.u.fields.ti = fpu->environ.ds.ti;
  1215.         ss.u.fields.rpl = fpu->environ.ds.rpl;
  1216.         ss.u.half = SWAP_SHORT(ss.u.half);
  1217.         memcpy(&(fpu->environ.cs), &ss,
  1218.            sizeof(struct swapped_sel));
  1219.  
  1220.         for(i = 0; i < 8; i++){
  1221.         sfpd.mant = SWAP_SHORT(fpu->stack.ST[i].mant);
  1222.         sfpd.mant1 = SWAP_SHORT(fpu->stack.ST[i].mant1);
  1223.         sfpd.mant2 = SWAP_SHORT(fpu->stack.ST[i].mant2);
  1224.         sfpd.mant3 = SWAP_SHORT(fpu->stack.ST[i].mant3);
  1225.         sfpd.u.fields.exp = fpu->stack.ST[i].exp;
  1226.         sfpd.u.fields.sign = fpu->stack.ST[i].sign;
  1227.         sfpd.u.half = SWAP_SHORT(sfpd.u.half);
  1228.         memcpy(&(fpu->stack.ST[i]), &sfpd,
  1229.                sizeof(struct swapped_fp_data_reg));
  1230.         }
  1231.     }
  1232. }
  1233.  
  1234. __private_extern__
  1235. void
  1236. swap_i386_thread_exceptstate(
  1237. i386_thread_exceptstate_t *exc,
  1238. enum byte_sex target_byte_sex)
  1239. {
  1240.     struct swapped_err_code {
  1241.     union {
  1242.         struct err_code_normal {
  1243.         unsigned int        :16,
  1244.                 index    :13,
  1245.                 tbl    :2,
  1246.                 ext    :1;
  1247.         } normal;
  1248.         struct err_code_pgfault {
  1249.         unsigned int        :29,
  1250.                 user    :1,
  1251.                 wrtflt    :1,
  1252.                 prot    :1;
  1253.         } pgfault;
  1254.         unsigned long word;
  1255.     } u;
  1256.     } sec;
  1257.     unsigned long word;
  1258.     enum byte_sex host_byte_sex;
  1259.  
  1260.     host_byte_sex = get_host_byte_sex();
  1261.  
  1262.     exc->trapno = SWAP_LONG(exc->trapno);
  1263.     if(exc->trapno == 14){
  1264.         if(target_byte_sex == host_byte_sex){
  1265.         memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code));
  1266.         sec.u.word = SWAP_LONG(sec.u.word);
  1267.         exc->err.pgfault.user   = sec.u.pgfault.user;
  1268.         exc->err.pgfault.wrtflt = sec.u.pgfault.wrtflt;
  1269.         exc->err.pgfault.prot   = sec.u.pgfault.prot;
  1270.         }
  1271.         else{
  1272.         sec.u.pgfault.prot   = exc->err.pgfault.prot;
  1273.         sec.u.pgfault.wrtflt = exc->err.pgfault.wrtflt;
  1274.         sec.u.pgfault.user   = exc->err.pgfault.user;
  1275.         sec.u.word = SWAP_LONG(sec.u.word);
  1276.         memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code));
  1277.         }
  1278.     }
  1279.     else{
  1280.         if(target_byte_sex == host_byte_sex){
  1281.         memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code));
  1282.         sec.u.word = SWAP_LONG(sec.u.word);
  1283.         word = sec.u.normal.index;
  1284.         exc->err.normal.index = SWAP_LONG(word);
  1285.         exc->err.normal.tbl   = sec.u.normal.tbl;
  1286.         exc->err.normal.ext   = sec.u.normal.ext;
  1287.         }
  1288.         else{
  1289.         sec.u.normal.ext   = exc->err.normal.ext;
  1290.         sec.u.normal.tbl   = exc->err.normal.tbl;
  1291.         word = exc->err.normal.index;
  1292.         sec.u.normal.index = SWAP_LONG(word);
  1293.         sec.u.word = SWAP_LONG(sec.u.word);
  1294.         memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code));
  1295.         }
  1296.     }
  1297. }
  1298.  
  1299. __private_extern__
  1300. void
  1301. swap_i386_thread_cthreadstate(
  1302. i386_thread_cthreadstate_t *user,
  1303. enum byte_sex target_byte_sex)
  1304. {
  1305.     user->self = SWAP_LONG(user->self);
  1306. }
  1307.  
  1308. __private_extern__
  1309. void
  1310. swap_hppa_integer_thread_state(
  1311. struct hp_pa_integer_thread_state *regs,
  1312. enum byte_sex target_byte_order)
  1313. {
  1314.     regs->ts_gr1 = SWAP_LONG(regs->ts_gr1);
  1315.     regs->ts_gr2 = SWAP_LONG(regs->ts_gr2);
  1316.     regs->ts_gr3 = SWAP_LONG(regs->ts_gr3);
  1317.     regs->ts_gr4 = SWAP_LONG(regs->ts_gr4);
  1318.     regs->ts_gr5 = SWAP_LONG(regs->ts_gr5);
  1319.     regs->ts_gr6 = SWAP_LONG(regs->ts_gr6);
  1320.     regs->ts_gr7 = SWAP_LONG(regs->ts_gr7);
  1321.     regs->ts_gr8 = SWAP_LONG(regs->ts_gr8);
  1322.     regs->ts_gr9 = SWAP_LONG(regs->ts_gr9);
  1323.     regs->ts_gr10 = SWAP_LONG(regs->ts_gr10);
  1324.     regs->ts_gr11 = SWAP_LONG(regs->ts_gr11);
  1325.     regs->ts_gr12 = SWAP_LONG(regs->ts_gr12);
  1326.     regs->ts_gr13 = SWAP_LONG(regs->ts_gr13);
  1327.     regs->ts_gr14 = SWAP_LONG(regs->ts_gr14);
  1328.     regs->ts_gr15 = SWAP_LONG(regs->ts_gr15);
  1329.     regs->ts_gr16 = SWAP_LONG(regs->ts_gr16);
  1330.     regs->ts_gr17 = SWAP_LONG(regs->ts_gr17);
  1331.     regs->ts_gr18 = SWAP_LONG(regs->ts_gr18);
  1332.     regs->ts_gr19 = SWAP_LONG(regs->ts_gr19);
  1333.     regs->ts_gr20 = SWAP_LONG(regs->ts_gr20);
  1334.     regs->ts_gr21 = SWAP_LONG(regs->ts_gr21);
  1335.     regs->ts_gr22 = SWAP_LONG(regs->ts_gr22);
  1336.     regs->ts_gr23 = SWAP_LONG(regs->ts_gr23);
  1337.     regs->ts_gr24 = SWAP_LONG(regs->ts_gr24);
  1338.     regs->ts_gr25 = SWAP_LONG(regs->ts_gr25);
  1339.     regs->ts_gr26 = SWAP_LONG(regs->ts_gr26);
  1340.     regs->ts_gr27 = SWAP_LONG(regs->ts_gr27);
  1341.     regs->ts_gr28 = SWAP_LONG(regs->ts_gr28);
  1342.     regs->ts_gr29 = SWAP_LONG(regs->ts_gr29);
  1343.     regs->ts_gr30 = SWAP_LONG(regs->ts_gr30);
  1344.     regs->ts_gr31 = SWAP_LONG(regs->ts_gr31);
  1345.     regs->ts_sr0 = SWAP_LONG(regs->ts_sr0);
  1346.     regs->ts_sr1 = SWAP_LONG(regs->ts_sr1);
  1347.     regs->ts_sr2 = SWAP_LONG(regs->ts_sr2);
  1348.     regs->ts_sr3 = SWAP_LONG(regs->ts_sr3);
  1349.     regs->ts_sar = SWAP_LONG(regs->ts_sar);
  1350. }
  1351.  
  1352. __private_extern__
  1353. void swap_hppa_frame_thread_state(
  1354.   struct hp_pa_frame_thread_state *frame,
  1355.   enum byte_sex target_byte_order)
  1356. {
  1357.   frame->ts_pcsq_front = SWAP_LONG(frame->ts_pcsq_front);
  1358.   frame->ts_pcsq_back = SWAP_LONG(frame->ts_pcsq_back);
  1359.   frame->ts_pcoq_front = SWAP_LONG(frame->ts_pcoq_front);
  1360.   frame->ts_pcoq_back = SWAP_LONG(frame->ts_pcoq_back);
  1361.   frame->ts_psw = SWAP_LONG(frame->ts_psw);
  1362.   frame->ts_unaligned_faults = SWAP_LONG(frame->ts_unaligned_faults);
  1363.   frame->ts_fault_address = SWAP_LONG(frame->ts_fault_address);
  1364.   frame->ts_step_range_start = SWAP_LONG(frame->ts_step_range_start);
  1365.   frame->ts_step_range_stop = SWAP_LONG(frame->ts_step_range_stop);
  1366. }
  1367.  
  1368. __private_extern__
  1369. void swap_hppa_fp_thread_state(
  1370.   struct hp_pa_fp_thread_state *fp,
  1371.   enum byte_sex target_byte_order)
  1372. {
  1373.   fp->ts_fp0 = SWAP_DOUBLE(fp->ts_fp0);
  1374.   fp->ts_fp1 = SWAP_DOUBLE(fp->ts_fp1);
  1375.   fp->ts_fp2 = SWAP_DOUBLE(fp->ts_fp2);
  1376.   fp->ts_fp3 = SWAP_DOUBLE(fp->ts_fp3);
  1377.   fp->ts_fp4 = SWAP_DOUBLE(fp->ts_fp4);
  1378.   fp->ts_fp5 = SWAP_DOUBLE(fp->ts_fp5);
  1379.   fp->ts_fp6 = SWAP_DOUBLE(fp->ts_fp6);
  1380.   fp->ts_fp7 = SWAP_DOUBLE(fp->ts_fp7);
  1381.   fp->ts_fp8 = SWAP_DOUBLE(fp->ts_fp8);
  1382.   fp->ts_fp9 = SWAP_DOUBLE(fp->ts_fp9);
  1383.   fp->ts_fp10 = SWAP_DOUBLE(fp->ts_fp10);
  1384.   fp->ts_fp11 = SWAP_DOUBLE(fp->ts_fp11);
  1385.   fp->ts_fp12 = SWAP_DOUBLE(fp->ts_fp12);
  1386.   fp->ts_fp13 = SWAP_DOUBLE(fp->ts_fp13);
  1387.   fp->ts_fp14 = SWAP_DOUBLE(fp->ts_fp14);
  1388.   fp->ts_fp15 = SWAP_DOUBLE(fp->ts_fp15);
  1389.   fp->ts_fp16 = SWAP_DOUBLE(fp->ts_fp16);
  1390.   fp->ts_fp17 = SWAP_DOUBLE(fp->ts_fp17);
  1391.   fp->ts_fp18 = SWAP_DOUBLE(fp->ts_fp18);
  1392.   fp->ts_fp19 = SWAP_DOUBLE(fp->ts_fp19);
  1393.   fp->ts_fp20 = SWAP_DOUBLE(fp->ts_fp20);
  1394.   fp->ts_fp21 = SWAP_DOUBLE(fp->ts_fp21);
  1395.   fp->ts_fp22 = SWAP_DOUBLE(fp->ts_fp22);
  1396.   fp->ts_fp23 = SWAP_DOUBLE(fp->ts_fp23);
  1397.   fp->ts_fp24 = SWAP_DOUBLE(fp->ts_fp24);
  1398.   fp->ts_fp25 = SWAP_DOUBLE(fp->ts_fp25);
  1399.   fp->ts_fp26 = SWAP_DOUBLE(fp->ts_fp26);
  1400.   fp->ts_fp27 = SWAP_DOUBLE(fp->ts_fp27);
  1401.   fp->ts_fp28 = SWAP_DOUBLE(fp->ts_fp28);
  1402.   fp->ts_fp29 = SWAP_DOUBLE(fp->ts_fp29);
  1403.   fp->ts_fp30 = SWAP_DOUBLE(fp->ts_fp30);
  1404.   fp->ts_fp31 = SWAP_DOUBLE(fp->ts_fp31);
  1405. }
  1406.  
  1407. __private_extern__
  1408. void
  1409. swap_sparc_thread_state_regs(
  1410.             struct sparc_thread_state_regs *cpu,
  1411.             enum byte_sex target_byte_sex)
  1412. {
  1413.   enum byte_sex host_byte_sex = get_host_byte_sex();
  1414.   struct p_status *pr_status = (struct p_status *) &(cpu->regs.r_psr);
  1415.  
  1416.   struct swapped_psr {
  1417.     union {
  1418.       struct {
  1419.     unsigned int 
  1420.     cwp:BITS_WIDTH(4,0),
  1421.     et:BIT_WIDTH(5),
  1422.     ps:BIT_WIDTH(6),
  1423.     s:BIT_WIDTH(7),
  1424.     pil:BITS_WIDTH(11,8),
  1425.     ef:BIT_WIDTH(12),
  1426.     ec:BIT_WIDTH(13),
  1427.     reserved:BITS_WIDTH(19,14),
  1428.     icc:BITS_WIDTH(23,20),
  1429.     ver:BITS_WIDTH(27,24),
  1430.     impl:BITS_WIDTH(31,28);
  1431.       } fields;
  1432.       unsigned int word;
  1433.     } u;
  1434.   } spsr;
  1435.  
  1436.   cpu->regs.r_pc = SWAP_LONG(cpu->regs.r_pc);
  1437.   cpu->regs.r_npc = SWAP_LONG(cpu->regs.r_npc);
  1438.   cpu->regs.r_y = SWAP_LONG(cpu->regs.r_y);
  1439.   cpu->regs.r_g1 = SWAP_LONG(cpu->regs.r_g1);
  1440.   cpu->regs.r_g2 = SWAP_LONG(cpu->regs.r_g2);
  1441.   cpu->regs.r_g3 = SWAP_LONG(cpu->regs.r_g3);
  1442.   cpu->regs.r_g4 = SWAP_LONG(cpu->regs.r_g4);
  1443.   cpu->regs.r_g5 = SWAP_LONG(cpu->regs.r_g5);
  1444.   cpu->regs.r_g6 = SWAP_LONG(cpu->regs.r_g6);
  1445.   cpu->regs.r_g7 = SWAP_LONG(cpu->regs.r_g7);
  1446.   cpu->regs.r_o0 = SWAP_LONG(cpu->regs.r_o0);
  1447.   cpu->regs.r_o1 = SWAP_LONG(cpu->regs.r_o1);
  1448.   cpu->regs.r_o2 = SWAP_LONG(cpu->regs.r_o2);
  1449.   cpu->regs.r_o3 = SWAP_LONG(cpu->regs.r_o3);
  1450.   cpu->regs.r_o4 = SWAP_LONG(cpu->regs.r_o4);
  1451.   cpu->regs.r_o5 = SWAP_LONG(cpu->regs.r_o5);
  1452.   cpu->regs.r_o6 = SWAP_LONG(cpu->regs.r_o6);
  1453.   cpu->regs.r_o7 = SWAP_LONG(cpu->regs.r_o7);
  1454.  
  1455.   if (target_byte_sex == host_byte_sex) {
  1456.     memcpy(&spsr, &(cpu->regs.r_psr), sizeof(struct swapped_psr));
  1457.     spsr.u.word = SWAP_LONG(spsr.u.word);
  1458.     pr_status->PSRREG.psr_bits.cwp = spsr.u.fields.cwp;
  1459.     pr_status->PSRREG.psr_bits.ps = spsr.u.fields.ps;
  1460.     pr_status->PSRREG.psr_bits.s = spsr.u.fields.s;
  1461.     pr_status->PSRREG.psr_bits.pil = spsr.u.fields.pil;
  1462.     pr_status->PSRREG.psr_bits.ef = spsr.u.fields.ef;
  1463.     pr_status->PSRREG.psr_bits.ec = spsr.u.fields.ec;
  1464.     pr_status->PSRREG.psr_bits.reserved = spsr.u.fields.reserved;
  1465.     pr_status->PSRREG.psr_bits.icc = spsr.u.fields.icc;
  1466.     pr_status->PSRREG.psr_bits.et = spsr.u.fields.ver;
  1467.     pr_status->PSRREG.psr_bits.impl = spsr.u.fields.impl;
  1468.   } else {
  1469.     spsr.u.fields.cwp = pr_status->PSRREG.psr_bits.cwp;
  1470.     spsr.u.fields.ps = pr_status->PSRREG.psr_bits.ps;
  1471.     spsr.u.fields.s = pr_status->PSRREG.psr_bits.s;
  1472.     spsr.u.fields.pil = pr_status->PSRREG.psr_bits.pil;
  1473.     spsr.u.fields.ef = pr_status->PSRREG.psr_bits.ef;
  1474.     spsr.u.fields.ec = pr_status->PSRREG.psr_bits.ec;
  1475.     spsr.u.fields.reserved = pr_status->PSRREG.psr_bits.reserved;
  1476.     spsr.u.fields.icc = pr_status->PSRREG.psr_bits.icc;
  1477.     spsr.u.fields.ver = pr_status->PSRREG.psr_bits.et;
  1478.     spsr.u.fields.impl = pr_status->PSRREG.psr_bits.impl;
  1479.     spsr.u.word = SWAP_LONG(spsr.u.word);
  1480.     memcpy(&(cpu->regs.r_psr), &spsr, sizeof(struct swapped_psr));
  1481.   }
  1482. }
  1483.  
  1484. __private_extern__
  1485. void
  1486. swap_sparc_thread_state_fpu(
  1487.             struct sparc_thread_state_fpu *fpu,
  1488.             enum byte_sex target_byte_sex)
  1489. {
  1490.   enum byte_sex host_byte_sex = get_host_byte_sex();
  1491.   int i;
  1492.   struct f_status *fpu_status = (struct f_status *) &(fpu->fpu.Fpu_fsr);
  1493.  
  1494.   struct swapped_fsr {
  1495.     union {
  1496.       struct {
  1497.     unsigned int
  1498.     cexc:BITS_WIDTH(4,0),
  1499.     aexc:BITS_WIDTH(9,5),
  1500.     fcc:BITS_WIDTH(11,10),
  1501.     pr:BIT_WIDTH(12),
  1502.     qne:BIT_WIDTH(13),
  1503.     ftt:BITS_WIDTH(16,14),
  1504.     res:BITS_WIDTH(22,17),
  1505.     tem:BITS_WIDTH(27,23),
  1506.     rp:BITS_WIDTH(29,28),
  1507.     rd:BITS_WIDTH(31,30);
  1508.       } fields;
  1509.       unsigned int word;
  1510.     } u;
  1511.   } sfsr;
  1512.  
  1513.     
  1514.   /* floating point registers */
  1515.   for (i=0; i<16; i++)        /* 16 doubles */
  1516.     fpu->fpu.fpu_fr.Fpu_dregs[i] = SWAP_DOUBLE(fpu->fpu.fpu_fr.Fpu_dregs[i]);
  1517.   fpu->fpu.Fpu_q[0].FQu.whole = SWAP_DOUBLE(fpu->fpu.Fpu_q[0].FQu.whole);
  1518.   fpu->fpu.Fpu_q[1].FQu.whole = SWAP_DOUBLE(fpu->fpu.Fpu_q[1].FQu.whole);
  1519.   fpu->fpu.Fpu_flags = SWAP_LONG(fpu->fpu.Fpu_flags);
  1520.   fpu->fpu.Fpu_extra = SWAP_LONG(fpu->fpu.Fpu_extra);
  1521.   fpu->fpu.Fpu_qcnt = SWAP_LONG(fpu->fpu.Fpu_qcnt);
  1522.  
  1523.   if (target_byte_sex == host_byte_sex) {
  1524.     memcpy(&sfsr, &(fpu->fpu.Fpu_fsr), sizeof(unsigned int));
  1525.     sfsr.u.word = SWAP_LONG(sfsr.u.word);
  1526.     fpu_status->FPUREG.Fpu_fsr_bits.rd = sfsr.u.fields.rd;
  1527.     fpu_status->FPUREG.Fpu_fsr_bits.rp = sfsr.u.fields.rp;
  1528.     fpu_status->FPUREG.Fpu_fsr_bits.tem = sfsr.u.fields.tem;
  1529.     fpu_status->FPUREG.Fpu_fsr_bits.res = sfsr.u.fields.res;
  1530.     fpu_status->FPUREG.Fpu_fsr_bits.ftt = sfsr.u.fields.ftt;
  1531.     fpu_status->FPUREG.Fpu_fsr_bits.qne = sfsr.u.fields.qne;
  1532.     fpu_status->FPUREG.Fpu_fsr_bits.pr = sfsr.u.fields.pr;
  1533.     fpu_status->FPUREG.Fpu_fsr_bits.fcc = sfsr.u.fields.fcc;
  1534.     fpu_status->FPUREG.Fpu_fsr_bits.aexc = sfsr.u.fields.aexc;
  1535.     fpu_status->FPUREG.Fpu_fsr_bits.cexc = sfsr.u.fields.cexc;
  1536.   } else {
  1537.     sfsr.u.fields.rd = fpu_status->FPUREG.Fpu_fsr_bits.rd;
  1538.     sfsr.u.fields.rp = fpu_status->FPUREG.Fpu_fsr_bits.rp;
  1539.     sfsr.u.fields.tem = fpu_status->FPUREG.Fpu_fsr_bits.tem;
  1540.     sfsr.u.fields.res = fpu_status->FPUREG.Fpu_fsr_bits.res;
  1541.     sfsr.u.fields.ftt = fpu_status->FPUREG.Fpu_fsr_bits.ftt;
  1542.     sfsr.u.fields.qne = fpu_status->FPUREG.Fpu_fsr_bits.qne;
  1543.     sfsr.u.fields.pr = fpu_status->FPUREG.Fpu_fsr_bits.pr;
  1544.     sfsr.u.fields.fcc = fpu_status->FPUREG.Fpu_fsr_bits.fcc;
  1545.     sfsr.u.fields.aexc = fpu_status->FPUREG.Fpu_fsr_bits.aexc;
  1546.     sfsr.u.fields.cexc = fpu_status->FPUREG.Fpu_fsr_bits.cexc;
  1547.     sfsr.u.word = SWAP_LONG(sfsr.u.word);
  1548.     memcpy(&(fpu->fpu.Fpu_fsr), &sfsr, sizeof(struct swapped_fsr));
  1549.   }
  1550. }
  1551.  
  1552. __private_extern__
  1553. void
  1554. swap_ident_command(
  1555. struct ident_command *id_cmd,
  1556. enum byte_sex target_byte_sex)
  1557. {
  1558.     id_cmd->cmd = SWAP_LONG(id_cmd->cmd);
  1559.     id_cmd->cmdsize = SWAP_LONG(id_cmd->cmdsize);
  1560. }
  1561.  
  1562. __private_extern__
  1563. void
  1564. swap_nlist(
  1565. struct nlist *symbols,
  1566. unsigned long nsymbols,
  1567. enum byte_sex target_byte_sex)
  1568. {
  1569.     unsigned long i;
  1570.  
  1571.     for(i = 0; i < nsymbols; i++){
  1572.         symbols[i].n_un.n_strx = SWAP_LONG(symbols[i].n_un.n_strx);
  1573.         /* n_type */
  1574.         /* n_sect */
  1575.         symbols[i].n_desc = SWAP_SHORT(symbols[i].n_desc);
  1576.         symbols[i].n_value = SWAP_LONG(symbols[i].n_value);
  1577.     }
  1578. }
  1579.  
  1580. __private_extern__
  1581. void
  1582. swap_ranlib(
  1583. struct ranlib *ranlibs,
  1584. unsigned long nranlibs,
  1585. enum byte_sex target_byte_sex)
  1586. {
  1587.     unsigned long i;
  1588.  
  1589.     for(i = 0; i < nranlibs; i++){
  1590.         ranlibs[i].ran_un.ran_strx = SWAP_LONG(ranlibs[i].ran_un.ran_strx);
  1591.         ranlibs[i].ran_off = SWAP_LONG(ranlibs[i].ran_off);
  1592.     }
  1593. }
  1594.  
  1595. __private_extern__
  1596. void
  1597. swap_relocation_info(
  1598. struct relocation_info *relocs,
  1599. unsigned long nrelocs,
  1600. enum byte_sex target_byte_sex)
  1601. {
  1602.     unsigned long i;
  1603.     enum byte_sex host_byte_sex;
  1604.     enum bool to_host_byte_sex, scattered;
  1605.  
  1606.     struct swapped_relocation_info {
  1607.     long    r_address;
  1608.     union {
  1609.         struct {
  1610.         unsigned int
  1611.             r_type:4,
  1612.             r_extern:1,
  1613.             r_length:2,
  1614.             r_pcrel:1,
  1615.             r_symbolnum:24;
  1616.         } fields;
  1617.         unsigned long word;
  1618.     } u;
  1619.     } sr;
  1620.  
  1621.     struct swapped_scattered_relocation_info {
  1622.     unsigned long word;
  1623.     long    r_value;
  1624.     } *ssr;
  1625.  
  1626.     host_byte_sex = get_host_byte_sex();
  1627.     to_host_byte_sex = target_byte_sex == host_byte_sex;
  1628.  
  1629.     for(i = 0; i < nrelocs; i++){
  1630.         if(to_host_byte_sex)
  1631.         scattered = (SWAP_LONG(relocs[i].r_address) & R_SCATTERED) != 0;
  1632.         else
  1633.         scattered = ((relocs[i].r_address) & R_SCATTERED) != 0;
  1634.         if(scattered == FALSE){
  1635.         if(to_host_byte_sex){
  1636.             memcpy(&sr, relocs + i, sizeof(struct relocation_info));
  1637.             sr.r_address = SWAP_LONG(sr.r_address);
  1638.             sr.u.word = SWAP_LONG(sr.u.word);
  1639.             relocs[i].r_address = sr.r_address;
  1640.             relocs[i].r_symbolnum = sr.u.fields.r_symbolnum;
  1641.             relocs[i].r_pcrel = sr.u.fields.r_pcrel;
  1642.             relocs[i].r_length = sr.u.fields.r_length;
  1643.             relocs[i].r_extern = sr.u.fields.r_extern;
  1644.             relocs[i].r_type = sr.u.fields.r_type;
  1645.         }
  1646.         else{
  1647.             sr.r_address = relocs[i].r_address;
  1648.             sr.u.fields.r_symbolnum = relocs[i].r_symbolnum;
  1649.             sr.u.fields.r_length = relocs[i].r_length;
  1650.             sr.u.fields.r_pcrel = relocs[i].r_pcrel;
  1651.             sr.u.fields.r_extern = relocs[i].r_extern;
  1652.             sr.u.fields.r_type = relocs[i].r_type;
  1653.             sr.r_address = SWAP_LONG(sr.r_address);
  1654.             sr.u.word = SWAP_LONG(sr.u.word);
  1655.             memcpy(relocs + i, &sr, sizeof(struct relocation_info));
  1656.         }
  1657.         }
  1658.         else{
  1659.         ssr = (struct swapped_scattered_relocation_info *)(relocs + i);
  1660.         ssr->word = SWAP_LONG(ssr->word);
  1661.         ssr->r_value = SWAP_LONG(ssr->r_value);
  1662.         }
  1663.     }
  1664. }
  1665.  
  1666. __private_extern__
  1667. void
  1668. swap_indirect_symbols(
  1669. unsigned long *indirect_symbols,
  1670. unsigned long nindirect_symbols,
  1671. enum byte_sex target_byte_sex)
  1672. {
  1673.     unsigned long i;
  1674.  
  1675.     for(i = 0; i < nindirect_symbols; i++)
  1676.         indirect_symbols[i] = SWAP_LONG(indirect_symbols[i]);
  1677. }
  1678.  
  1679. __private_extern__
  1680. void
  1681. swap_dylib_reference(
  1682. struct dylib_reference *refs,
  1683. unsigned long nrefs,
  1684. enum byte_sex target_byte_sex)
  1685. {
  1686.     struct swapped_dylib_reference {
  1687.     union {
  1688.         struct {
  1689.         unsigned long
  1690.             flags:8,
  1691.             isym:24;
  1692.         } fields;
  1693.         unsigned long word;
  1694.     } u;
  1695.     } sref;
  1696.  
  1697.     unsigned long i;
  1698.     enum byte_sex host_byte_sex;
  1699.  
  1700.     host_byte_sex = get_host_byte_sex();
  1701.  
  1702.     for(i = 0; i < nrefs; i++){
  1703.         if(target_byte_sex == host_byte_sex){
  1704.         memcpy(&sref, refs + i, sizeof(struct swapped_dylib_reference));
  1705.         sref.u.word = SWAP_LONG(sref.u.word);
  1706.         refs[i].flags = sref.u.fields.flags;
  1707.         refs[i].isym = sref.u.fields.isym;
  1708.         }
  1709.         else{
  1710.         sref.u.fields.isym = refs[i].isym;
  1711.         sref.u.fields.flags = refs[i].flags;
  1712.         sref.u.word = SWAP_LONG(sref.u.word);
  1713.         memcpy(refs + i, &sref, sizeof(struct swapped_dylib_reference));
  1714.         }
  1715.     }
  1716.  
  1717. }
  1718.  
  1719. __private_extern__
  1720. void
  1721. swap_dylib_module(
  1722. struct dylib_module *mods,
  1723. unsigned long nmods,
  1724. enum byte_sex target_byte_sex)
  1725. {
  1726.     unsigned long i;
  1727.  
  1728.     for(i = 0; i < nmods; i++){
  1729.         mods[i].module_name = SWAP_LONG(mods[i].module_name);
  1730.         mods[i].iextdefsym  = SWAP_LONG(mods[i].iextdefsym);
  1731.         mods[i].nextdefsym  = SWAP_LONG(mods[i].nextdefsym);
  1732.         mods[i].irefsym     = SWAP_LONG(mods[i].irefsym);
  1733.         mods[i].nrefsym     = SWAP_LONG(mods[i].nrefsym);
  1734.         mods[i].ilocalsym   = SWAP_LONG(mods[i].ilocalsym);
  1735.         mods[i].nlocalsym   = SWAP_LONG(mods[i].nlocalsym);
  1736.         mods[i].iextrel     = SWAP_LONG(mods[i].iextrel);
  1737.         mods[i].nextrel     = SWAP_LONG(mods[i].nextrel);
  1738.         mods[i].iinit       = SWAP_LONG(mods[i].iinit);
  1739.         mods[i].ninit       = SWAP_LONG(mods[i].ninit);
  1740.     }
  1741. }
  1742.  
  1743. __private_extern__
  1744. void
  1745. swap_dylib_table_of_contents(
  1746. struct dylib_table_of_contents *tocs,
  1747. unsigned long ntocs,
  1748. enum byte_sex target_byte_sex)
  1749. {
  1750.     unsigned long i;
  1751.  
  1752.     for(i = 0; i < ntocs; i++){
  1753.         tocs[i].symbol_index = SWAP_LONG(tocs[i].symbol_index);
  1754.         tocs[i].module_index = SWAP_LONG(tocs[i].module_index);
  1755.     }
  1756. }
  1757.