home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Source / GNU / cctools / libstuff / bytesex.c < prev    next >
Text File  |  1993-01-04  |  39KB  |  1,320 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. #include <mach-o/nlist.h>
  10. #include <mach-o/reloc.h>
  11. #include <bsd/ranlib.h>
  12. #include "stuff/bool.h"
  13. #include "stuff/bytesex.h"
  14.  
  15. double
  16. SWAP_DOUBLE(
  17. double d)
  18. {
  19.     union {
  20.         char c[8];
  21.         double d;
  22.     } in, out;
  23.     in.d = d;
  24.     out.c[0] = in.c[7];
  25.     out.c[1] = in.c[6];
  26.     out.c[2] = in.c[5];
  27.     out.c[3] = in.c[4];
  28.     out.c[4] = in.c[3];
  29.     out.c[5] = in.c[2];
  30.     out.c[6] = in.c[1];
  31.     out.c[7] = in.c[0];
  32.     return(out.d);
  33. }
  34.  
  35. float
  36. SWAP_FLOAT(
  37. float f)
  38. {
  39.     union {
  40.         char c[7];
  41.         float f;
  42.     } in, out;
  43.     in.f = f;
  44.     out.c[0] = in.c[3];
  45.     out.c[1] = in.c[2];
  46.     out.c[2] = in.c[1];
  47.     out.c[3] = in.c[0];
  48.     return(out.f);
  49. }
  50.  
  51. /*
  52.  * get_host_byte_sex() returns the enum constant for the byte sex of the host
  53.  * it is running on.
  54.  */
  55. enum byte_sex
  56. get_host_byte_sex(
  57. void)
  58. {
  59.     unsigned long s;
  60.  
  61.     s = (BIG_ENDIAN_BYTE_SEX << 24) | LITTLE_ENDIAN_BYTE_SEX;
  62.     return((enum byte_sex)*((char *)&s));
  63. }
  64.  
  65. void
  66. swap_fat_header(
  67. struct fat_header *fat_header,
  68. enum byte_sex target_byte_sex)
  69. {
  70.     fat_header->magic     = SWAP_LONG(fat_header->magic);
  71.     fat_header->nfat_arch = SWAP_LONG(fat_header->nfat_arch);
  72. }
  73.  
  74. void
  75. swap_fat_arch(
  76. struct fat_arch *fat_archs,
  77. unsigned long nfat_arch,
  78. enum byte_sex target_byte_sex)
  79. {
  80.     unsigned long i;
  81.  
  82.     for(i = 0; i < nfat_arch; i++){
  83.         fat_archs[i].cputype    = SWAP_LONG(fat_archs[i].cputype);
  84.         fat_archs[i].cpusubtype = SWAP_LONG(fat_archs[i].cpusubtype);
  85.         fat_archs[i].offset     = SWAP_LONG(fat_archs[i].offset);
  86.         fat_archs[i].size       = SWAP_LONG(fat_archs[i].size);
  87.         fat_archs[i].align      = SWAP_LONG(fat_archs[i].align);
  88.     }
  89. }
  90.  
  91. void
  92. swap_mach_header(
  93. struct mach_header *mh,
  94. enum byte_sex target_byte_sex)
  95. {
  96.     mh->magic = SWAP_LONG(mh->magic);
  97.     mh->cputype = SWAP_LONG(mh->cputype);
  98.     mh->cpusubtype = SWAP_LONG(mh->cpusubtype);
  99.     mh->filetype = SWAP_LONG(mh->filetype);
  100.     mh->ncmds = SWAP_LONG(mh->ncmds);
  101.     mh->sizeofcmds = SWAP_LONG(mh->sizeofcmds);
  102.     mh->flags = SWAP_LONG(mh->flags);
  103. }
  104.  
  105. void
  106. swap_load_command(
  107. struct load_command *lc,
  108. enum byte_sex target_byte_sex)
  109. {
  110.     lc->cmd = SWAP_LONG(lc->cmd);
  111.     lc->cmdsize = SWAP_LONG(lc->cmdsize);
  112. }
  113.  
  114. void
  115. swap_segment_command(
  116. struct segment_command *sg,
  117. enum byte_sex target_byte_sex)
  118. {
  119.     /* segname[16] */
  120.     sg->cmd = SWAP_LONG(sg->cmd);
  121.     sg->cmdsize = SWAP_LONG(sg->cmdsize);
  122.     sg->vmaddr = SWAP_LONG(sg->vmaddr);
  123.     sg->vmsize = SWAP_LONG(sg->vmsize);
  124.     sg->fileoff = SWAP_LONG(sg->fileoff);
  125.     sg->filesize = SWAP_LONG(sg->filesize);
  126.     sg->maxprot = SWAP_LONG(sg->maxprot);
  127.     sg->initprot = SWAP_LONG(sg->initprot);
  128.     sg->nsects = SWAP_LONG(sg->nsects);
  129.     sg->flags = SWAP_LONG(sg->flags);
  130. }
  131.  
  132. void
  133. swap_section(
  134. struct section *s,
  135. unsigned long nsects,
  136. enum byte_sex target_byte_sex)
  137. {
  138.     unsigned long i;
  139.  
  140.     for(i = 0; i < nsects; i++){
  141.         /* sectname[16] */
  142.         /* segname[16] */
  143.         s[i].addr = SWAP_LONG(s[i].addr);
  144.         s[i].size = SWAP_LONG(s[i].size);
  145.         s[i].offset = SWAP_LONG(s[i].offset);
  146.         s[i].align = SWAP_LONG(s[i].align);
  147.         s[i].reloff = SWAP_LONG(s[i].reloff);
  148.         s[i].nreloc = SWAP_LONG(s[i].nreloc);
  149.         s[i].flags = SWAP_LONG(s[i].flags);
  150.         s[i].reserved1 = SWAP_LONG(s[i].reserved1);
  151.         s[i].reserved2 = SWAP_LONG(s[i].reserved2);
  152.     }
  153. }
  154.  
  155. void
  156. swap_symtab_command(
  157. struct symtab_command *st,
  158. enum byte_sex target_byte_sex)
  159. {
  160.     st->cmd = SWAP_LONG(st->cmd);
  161.     st->cmdsize = SWAP_LONG(st->cmdsize);
  162.     st->symoff = SWAP_LONG(st->symoff);
  163.     st->nsyms = SWAP_LONG(st->nsyms);
  164.     st->stroff = SWAP_LONG(st->stroff);
  165.     st->strsize = SWAP_LONG(st->strsize);
  166. }
  167.  
  168. void
  169. swap_symseg_command(
  170. struct symseg_command *ss,
  171. enum byte_sex target_byte_sex)
  172. {
  173.     ss->cmd = SWAP_LONG(ss->cmd);
  174.     ss->cmdsize = SWAP_LONG(ss->cmdsize);
  175.     ss->offset = SWAP_LONG(ss->offset);
  176.     ss->size = SWAP_LONG(ss->size);
  177. }
  178.  
  179. void
  180. swap_fvmlib_command(
  181. struct fvmlib_command *fl,
  182. enum byte_sex target_byte_sex)
  183. {
  184.     fl->cmd = SWAP_LONG(fl->cmd);
  185.     fl->cmdsize = SWAP_LONG(fl->cmdsize);
  186.     fl->fvmlib.name.offset = SWAP_LONG(fl->fvmlib.name.offset);
  187.     fl->fvmlib.minor_version = SWAP_LONG(fl->fvmlib.minor_version);
  188.     fl->fvmlib.header_addr = SWAP_LONG(fl->fvmlib.header_addr);
  189. }
  190.  
  191. void
  192. swap_fvmfile_command(
  193. struct fvmfile_command *ff,
  194. enum byte_sex target_byte_sex)
  195. {
  196.     ff->cmd = SWAP_LONG(ff->cmd);
  197.     ff->cmdsize = SWAP_LONG(ff->cmdsize);
  198.     ff->name.offset = SWAP_LONG(ff->name.offset);
  199.     ff->header_addr = SWAP_LONG(ff->header_addr);
  200. }
  201.  
  202.  
  203. void
  204. swap_thread_command(
  205. struct thread_command *ut,
  206. enum byte_sex target_byte_sex)
  207. {
  208.     ut->cmd = SWAP_LONG(ut->cmd);
  209.     ut->cmdsize = SWAP_LONG(ut->cmdsize);
  210. }
  211.  
  212. void
  213. swap_m68k_thread_state_regs(
  214. struct m68k_thread_state_regs *cpu,
  215. enum byte_sex target_byte_sex)
  216. {
  217.     unsigned long i;
  218.  
  219.     for(i = 0; i < 8; i++)
  220.         cpu->dreg[i] = SWAP_LONG(cpu->dreg[i]);
  221.     for(i = 0; i < 8; i++)
  222.         cpu->areg[i] = SWAP_LONG(cpu->areg[i]);
  223.     cpu->pad0 = SWAP_SHORT(cpu->pad0);
  224.     cpu->sr = SWAP_SHORT(cpu->sr);
  225.     cpu->pc = SWAP_LONG(cpu->pc);
  226. }
  227.  
  228. void
  229. swap_m68k_thread_state_68882(
  230. struct m68k_thread_state_68882 *fpu,
  231. enum byte_sex target_byte_sex)
  232. {
  233.     unsigned long i, tmp;
  234.  
  235.     for(i = 0; i < 8; i++){
  236.                        tmp = SWAP_LONG(fpu->regs[i].fp[0]);
  237.         fpu->regs[i].fp[1] = SWAP_LONG(fpu->regs[i].fp[1]);
  238.         fpu->regs[i].fp[0] = SWAP_LONG(fpu->regs[i].fp[2]);
  239.         fpu->regs[i].fp[2] = tmp;
  240.     }
  241.     fpu->cr = SWAP_LONG(fpu->cr);
  242.     fpu->sr = SWAP_LONG(fpu->sr);
  243.     fpu->iar = SWAP_LONG(fpu->iar);
  244.     fpu->state = SWAP_LONG(fpu->state);
  245. }
  246.  
  247. void
  248. swap_m68k_thread_state_user_reg(
  249. struct m68k_thread_state_user_reg *user_reg,
  250. enum byte_sex target_byte_sex)
  251. {
  252.     user_reg->user_reg = SWAP_LONG(user_reg->user_reg);
  253. }
  254.  
  255. void
  256. swap_m98k_thread_state_grf_t(
  257. m98k_thread_state_grf_t *cpu,
  258. enum byte_sex target_byte_sex)
  259. {
  260.     enum byte_sex host_byte_sex;
  261.  
  262.     struct swapped_m98k_cr {
  263.     union {
  264.         struct {
  265.         unsigned rsvd:BITS_WIDTH(23,0);
  266.         unsigned ox:BIT_WIDTH(24);
  267.         unsigned vx:BIT_WIDTH(25);
  268.         unsigned fex:BIT_WIDTH(26);
  269.         unsigned fx:BIT_WIDTH(27);
  270.         unsigned so:BIT_WIDTH(28);
  271.         unsigned eq:BIT_WIDTH(29);
  272.         unsigned gt:BIT_WIDTH(30);
  273.         unsigned lt:BIT_WIDTH(31);
  274.         } fields;
  275.         unsigned long word;
  276.     } u;
  277.     } scr;
  278.     struct swapped_m98k_xer {
  279.     union {
  280.         struct {
  281.         unsigned byte_count:BITS_WIDTH(6,0);    
  282.         unsigned rsvd2:BIT_WIDTH(7);
  283.         unsigned byte:BITS_WIDTH(15,8);
  284.         unsigned rsvd1:BITS_WIDTH(28,16);
  285.         unsigned ca:BIT_WIDTH(29);
  286.         unsigned ov:BIT_WIDTH(30);
  287.         unsigned so:BIT_WIDTH(31);
  288.         } fields;
  289.         unsigned long word;
  290.     } u;
  291.     } sxer;
  292.     struct swapped_m98k_msr {
  293.     union {
  294.         struct {
  295.         unsigned psfr:BIT_WIDTH(0);
  296.         unsigned rsvd2:BITS_WIDTH(3,1);
  297.         unsigned dr:BIT_WIDTH(4);
  298.         unsigned ir:BIT_WIDTH(5);
  299.         unsigned ip:BIT_WIDTH(6);
  300.         unsigned rsvd1:BIT_WIDTH(7);
  301.         unsigned fe1:BIT_WIDTH(8);
  302.         unsigned be:BIT_WIDTH(9);
  303.         unsigned se:BIT_WIDTH(10);
  304.         unsigned fe0:BIT_WIDTH(11);
  305.         unsigned me:BIT_WIDTH(12);
  306.         unsigned fp:BIT_WIDTH(13);
  307.         unsigned pr:BIT_WIDTH(14);
  308.         unsigned ee:BIT_WIDTH(15);
  309.         unsigned rsvd3:BITS_WIDTH(31,16);
  310.         } fields;
  311.         unsigned long word;
  312.     } u;
  313.     } smsr;
  314.  
  315.     host_byte_sex = get_host_byte_sex();
  316.  
  317.     cpu->r0 = SWAP_LONG(cpu->r0);
  318.     cpu->r1 = SWAP_LONG(cpu->r1);
  319.     cpu->r2 = SWAP_LONG(cpu->r2);
  320.     cpu->r3 = SWAP_LONG(cpu->r3);
  321.     cpu->r4 = SWAP_LONG(cpu->r4);
  322.     cpu->r5 = SWAP_LONG(cpu->r5);
  323.     cpu->r6 = SWAP_LONG(cpu->r6);
  324.     cpu->r7 = SWAP_LONG(cpu->r7);
  325.     cpu->r8 = SWAP_LONG(cpu->r8);
  326.     cpu->r9 = SWAP_LONG(cpu->r9);
  327.     cpu->r10 = SWAP_LONG(cpu->r10);
  328.     cpu->r11 = SWAP_LONG(cpu->r11);
  329.     cpu->r12 = SWAP_LONG(cpu->r12);
  330.     cpu->r13 = SWAP_LONG(cpu->r13);
  331.     cpu->r14 = SWAP_LONG(cpu->r14);
  332.     cpu->r15 = SWAP_LONG(cpu->r15);
  333.     cpu->r16 = SWAP_LONG(cpu->r16);
  334.     cpu->r17 = SWAP_LONG(cpu->r17);
  335.     cpu->r18 = SWAP_LONG(cpu->r18);
  336.     cpu->r19 = SWAP_LONG(cpu->r19);
  337.     cpu->r20 = SWAP_LONG(cpu->r20);
  338.     cpu->r21 = SWAP_LONG(cpu->r21);
  339.     cpu->r22 = SWAP_LONG(cpu->r22);
  340.     cpu->r23 = SWAP_LONG(cpu->r23);
  341.     cpu->r24 = SWAP_LONG(cpu->r24);
  342.     cpu->r25 = SWAP_LONG(cpu->r25);
  343.     cpu->r26 = SWAP_LONG(cpu->r26);
  344.     cpu->r27 = SWAP_LONG(cpu->r27);
  345.     cpu->r28 = SWAP_LONG(cpu->r28);
  346.     cpu->r29 = SWAP_LONG(cpu->r29);
  347.     cpu->r30 = SWAP_LONG(cpu->r30);
  348.     cpu->r31 = SWAP_LONG(cpu->r31);
  349.     cpu->lr  = SWAP_LONG(cpu->lr);
  350.     cpu->ctr = SWAP_LONG(cpu->ctr);
  351.     cpu->cia = SWAP_LONG(cpu->cia);
  352.  
  353.     if(target_byte_sex == host_byte_sex){
  354.         memcpy(&scr, &(cpu->cr), sizeof(struct swapped_m98k_cr));
  355.         scr.u.word = SWAP_LONG(scr.u.word);
  356.         cpu->cr.lt = scr.u.fields.lt;
  357.         cpu->cr.gt = scr.u.fields.gt;
  358.         cpu->cr.eq = scr.u.fields.eq;
  359.         cpu->cr.so = scr.u.fields.so;
  360.         cpu->cr.fx = scr.u.fields.fx;
  361.         cpu->cr.fex = scr.u.fields.fex;
  362.         cpu->cr.vx = scr.u.fields.vx;
  363.         cpu->cr.ox = scr.u.fields.ox;
  364.         cpu->cr.rsvd = scr.u.fields.rsvd;
  365.  
  366.         memcpy(&sxer, &(cpu->xer), sizeof(struct swapped_m98k_xer));
  367.         sxer.u.word = SWAP_LONG(sxer.u.word);
  368.         cpu->xer.byte_count = sxer.u.fields.byte_count;
  369.         cpu->xer.rsvd2 = sxer.u.fields.rsvd2;
  370.         cpu->xer.byte = sxer.u.fields.byte;
  371.         cpu->xer.rsvd1 = sxer.u.fields.rsvd1;
  372.         cpu->xer.ca = sxer.u.fields.ca;
  373.         cpu->xer.ov = sxer.u.fields.ov;
  374.         cpu->xer.so = sxer.u.fields.so;
  375.  
  376.         memcpy(&smsr, &(cpu->msr), sizeof(struct swapped_m98k_msr));
  377.         smsr.u.word = SWAP_LONG(smsr.u.word);
  378.         cpu->msr.psfr = smsr.u.fields.psfr;
  379.         cpu->msr.rsvd2 = smsr.u.fields.rsvd2;
  380.         cpu->msr.dr = smsr.u.fields.dr;
  381.         cpu->msr.ir = smsr.u.fields.ir;
  382.         cpu->msr.ip = smsr.u.fields.ip;
  383.         cpu->msr.rsvd1 = smsr.u.fields.rsvd1;
  384.         cpu->msr.fe1 = smsr.u.fields.fe1;
  385.         cpu->msr.be = smsr.u.fields.be;
  386.         cpu->msr.se = smsr.u.fields.se;
  387.         cpu->msr.fe0 = smsr.u.fields.fe0;
  388.         cpu->msr.me = smsr.u.fields.me;
  389.         cpu->msr.fp = smsr.u.fields.fp;
  390.         cpu->msr.pr = smsr.u.fields.pr;
  391.         cpu->msr.ee = smsr.u.fields.ee;
  392.         cpu->msr.rsvd3 = smsr.u.fields.rsvd3;
  393.     }
  394.     else{
  395.         scr.u.fields.lt = cpu->cr.lt;
  396.         scr.u.fields.gt = cpu->cr.gt;
  397.         scr.u.fields.eq = cpu->cr.eq;
  398.         scr.u.fields.so = cpu->cr.so;
  399.         scr.u.fields.fx = cpu->cr.fx;
  400.         scr.u.fields.fex = cpu->cr.fex;
  401.         scr.u.fields.vx = cpu->cr.vx;
  402.         scr.u.fields.ox = cpu->cr.ox;
  403.         scr.u.fields.rsvd = cpu->cr.rsvd;
  404.         scr.u.word = SWAP_LONG(scr.u.word);
  405.         memcpy(&(cpu->cr), &scr, sizeof(struct swapped_m98k_cr));
  406.  
  407.         sxer.u.fields.byte_count = cpu->xer.byte_count;
  408.         sxer.u.fields.rsvd2 = cpu->xer.rsvd2;
  409.         sxer.u.fields.byte = cpu->xer.byte;
  410.         sxer.u.fields.rsvd1 = cpu->xer.rsvd1;
  411.         sxer.u.fields.ca = cpu->xer.ca;
  412.         sxer.u.fields.ov = cpu->xer.ov;
  413.         sxer.u.fields.so = cpu->xer.so;
  414.         sxer.u.word = SWAP_LONG(sxer.u.word);
  415.         memcpy(&(cpu->xer), &sxer, sizeof(struct swapped_m98k_xer));
  416.  
  417.         smsr.u.fields.psfr = cpu->msr.psfr;
  418.         smsr.u.fields.rsvd2 = cpu->msr.rsvd2;
  419.         smsr.u.fields.dr = cpu->msr.dr;
  420.         smsr.u.fields.ir = cpu->msr.ir;
  421.         smsr.u.fields.ip = cpu->msr.ip;
  422.         smsr.u.fields.rsvd1 = cpu->msr.rsvd1;
  423.         smsr.u.fields.fe1 = cpu->msr.fe1;
  424.         smsr.u.fields.be = cpu->msr.be;
  425.         smsr.u.fields.se = cpu->msr.se;
  426.         smsr.u.fields.fe0 = cpu->msr.fe0;
  427.         smsr.u.fields.me = cpu->msr.me;
  428.         smsr.u.fields.fp = cpu->msr.fp;
  429.         smsr.u.fields.pr = cpu->msr.pr;
  430.         smsr.u.fields.ee = cpu->msr.ee;
  431.         smsr.u.fields.rsvd3 = cpu->msr.rsvd3;
  432.         smsr.u.word = SWAP_LONG(smsr.u.word);
  433.         memcpy(&(cpu->msr), &smsr, sizeof(struct swapped_m98k_msr));
  434.     }
  435. }
  436.  
  437. void
  438. swap_m88k_thread_state_grf_t(
  439. m88k_thread_state_grf_t *cpu,
  440. enum byte_sex target_byte_sex)
  441. {
  442.     cpu->r1 = SWAP_LONG(cpu->r1);
  443.     cpu->r2 = SWAP_LONG(cpu->r2);
  444.     cpu->r3 = SWAP_LONG(cpu->r3);
  445.     cpu->r4 = SWAP_LONG(cpu->r4);
  446.     cpu->r5 = SWAP_LONG(cpu->r5);
  447.     cpu->r6 = SWAP_LONG(cpu->r6);
  448.     cpu->r7 = SWAP_LONG(cpu->r7);
  449.     cpu->r8 = SWAP_LONG(cpu->r8);
  450.     cpu->r9 = SWAP_LONG(cpu->r9);
  451.     cpu->r10 = SWAP_LONG(cpu->r10);
  452.     cpu->r11 = SWAP_LONG(cpu->r11);
  453.     cpu->r12 = SWAP_LONG(cpu->r12);
  454.     cpu->r13 = SWAP_LONG(cpu->r13);
  455.     cpu->r14 = SWAP_LONG(cpu->r14);
  456.     cpu->r15 = SWAP_LONG(cpu->r15);
  457.     cpu->r16 = SWAP_LONG(cpu->r16);
  458.     cpu->r17 = SWAP_LONG(cpu->r17);
  459.     cpu->r18 = SWAP_LONG(cpu->r18);
  460.     cpu->r19 = SWAP_LONG(cpu->r19);
  461.     cpu->r20 = SWAP_LONG(cpu->r20);
  462.     cpu->r21 = SWAP_LONG(cpu->r21);
  463.     cpu->r22 = SWAP_LONG(cpu->r22);
  464.     cpu->r23 = SWAP_LONG(cpu->r23);
  465.     cpu->r24 = SWAP_LONG(cpu->r24);
  466.     cpu->r25 = SWAP_LONG(cpu->r25);
  467.     cpu->r26 = SWAP_LONG(cpu->r26);
  468.     cpu->r27 = SWAP_LONG(cpu->r27);
  469.     cpu->r28 = SWAP_LONG(cpu->r28);
  470.     cpu->r29 = SWAP_LONG(cpu->r29);
  471.     cpu->r30 = SWAP_LONG(cpu->r30);
  472.     cpu->r31 = SWAP_LONG(cpu->r31);
  473.     cpu->xip = SWAP_LONG(cpu->xip);
  474.     cpu->xip_in_bd = SWAP_LONG(cpu->xip_in_bd);
  475.     cpu->nip = SWAP_LONG(cpu->nip);
  476. }
  477.  
  478. void
  479. swap_m88k_thread_state_xrf_t(
  480. m88k_thread_state_xrf_t *fpu,
  481. enum byte_sex target_byte_sex)
  482. {
  483.     enum byte_sex host_byte_sex;
  484.  
  485.     struct swapped_m88k_fpsr {
  486.     union {
  487.         struct {
  488.         unsigned    afinx:BIT_WIDTH(0);
  489.         unsigned    afovf:BIT_WIDTH(1);
  490.         unsigned    afunf:BIT_WIDTH(2);
  491.         unsigned    afdvz:BIT_WIDTH(3);
  492.         unsigned    afinv:BIT_WIDTH(4);
  493.         unsigned    :BITS_WIDTH(15,5);
  494.         unsigned    xmod:BIT_WIDTH(16);
  495.         unsigned    :BITS_WIDTH(31,17);
  496.         } fields;
  497.         unsigned long word;
  498.     } u;
  499.     } ssr;
  500.     struct swapped_m88k_fpcr {
  501.     union {
  502.         struct {
  503.         unsigned    efinx:BIT_WIDTH(0);
  504.         unsigned    efovf:BIT_WIDTH(1);
  505.         unsigned    efunf:BIT_WIDTH(2);
  506.         unsigned    efdvz:BIT_WIDTH(3);
  507.         unsigned    efinv:BIT_WIDTH(4);
  508.         unsigned    :BITS_WIDTH(13,5);
  509.         m88k_fpcr_rm_t    rm:BITS_WIDTH(15,14);
  510.         unsigned    :BITS_WIDTH(31,16);
  511.         } fields;
  512.         unsigned long word;
  513.     } u;
  514.     } scr;
  515.  
  516.     host_byte_sex = get_host_byte_sex();
  517.  
  518.     fpu->x1.x[0] = SWAP_LONG(fpu->x1.x[0]);
  519.     fpu->x1.x[1] = SWAP_LONG(fpu->x1.x[1]);
  520.     fpu->x1.x[2] = SWAP_LONG(fpu->x1.x[2]);
  521.     fpu->x1.x[3] = SWAP_LONG(fpu->x1.x[3]);
  522.     fpu->x2.x[0] = SWAP_LONG(fpu->x2.x[0]);
  523.     fpu->x2.x[1] = SWAP_LONG(fpu->x2.x[1]);
  524.     fpu->x2.x[2] = SWAP_LONG(fpu->x2.x[2]);
  525.     fpu->x2.x[3] = SWAP_LONG(fpu->x2.x[3]);
  526.     fpu->x3.x[0] = SWAP_LONG(fpu->x3.x[0]);
  527.     fpu->x3.x[1] = SWAP_LONG(fpu->x3.x[1]);
  528.     fpu->x3.x[2] = SWAP_LONG(fpu->x3.x[2]);
  529.     fpu->x3.x[3] = SWAP_LONG(fpu->x3.x[3]);
  530.     fpu->x4.x[0] = SWAP_LONG(fpu->x4.x[0]);
  531.     fpu->x4.x[1] = SWAP_LONG(fpu->x4.x[1]);
  532.     fpu->x4.x[2] = SWAP_LONG(fpu->x4.x[2]);
  533.     fpu->x4.x[3] = SWAP_LONG(fpu->x4.x[3]);
  534.     fpu->x5.x[0] = SWAP_LONG(fpu->x5.x[0]);
  535.     fpu->x5.x[1] = SWAP_LONG(fpu->x5.x[1]);
  536.     fpu->x5.x[2] = SWAP_LONG(fpu->x5.x[2]);
  537.     fpu->x5.x[3] = SWAP_LONG(fpu->x5.x[3]);
  538.     fpu->x6.x[0] = SWAP_LONG(fpu->x6.x[0]);
  539.     fpu->x6.x[1] = SWAP_LONG(fpu->x6.x[1]);
  540.     fpu->x6.x[2] = SWAP_LONG(fpu->x6.x[2]);
  541.     fpu->x6.x[3] = SWAP_LONG(fpu->x6.x[3]);
  542.     fpu->x7.x[0] = SWAP_LONG(fpu->x7.x[0]);
  543.     fpu->x7.x[1] = SWAP_LONG(fpu->x7.x[1]);
  544.     fpu->x7.x[2] = SWAP_LONG(fpu->x7.x[2]);
  545.     fpu->x7.x[3] = SWAP_LONG(fpu->x7.x[3]);
  546.     fpu->x8.x[0] = SWAP_LONG(fpu->x8.x[0]);
  547.     fpu->x8.x[1] = SWAP_LONG(fpu->x8.x[1]);
  548.     fpu->x8.x[2] = SWAP_LONG(fpu->x8.x[2]);
  549.     fpu->x8.x[3] = SWAP_LONG(fpu->x8.x[3]);
  550.     fpu->x9.x[0] = SWAP_LONG(fpu->x9.x[0]);
  551.     fpu->x9.x[1] = SWAP_LONG(fpu->x9.x[1]);
  552.     fpu->x9.x[2] = SWAP_LONG(fpu->x9.x[2]);
  553.     fpu->x9.x[3] = SWAP_LONG(fpu->x9.x[3]);
  554.     fpu->x10.x[0] = SWAP_LONG(fpu->x10.x[0]);
  555.     fpu->x10.x[1] = SWAP_LONG(fpu->x10.x[1]);
  556.     fpu->x10.x[2] = SWAP_LONG(fpu->x10.x[2]);
  557.     fpu->x10.x[3] = SWAP_LONG(fpu->x10.x[3]);
  558.     fpu->x11.x[0] = SWAP_LONG(fpu->x11.x[0]);
  559.     fpu->x11.x[1] = SWAP_LONG(fpu->x11.x[1]);
  560.     fpu->x11.x[2] = SWAP_LONG(fpu->x11.x[2]);
  561.     fpu->x11.x[3] = SWAP_LONG(fpu->x11.x[3]);
  562.     fpu->x12.x[0] = SWAP_LONG(fpu->x12.x[0]);
  563.     fpu->x12.x[1] = SWAP_LONG(fpu->x12.x[1]);
  564.     fpu->x12.x[2] = SWAP_LONG(fpu->x12.x[2]);
  565.     fpu->x12.x[3] = SWAP_LONG(fpu->x12.x[3]);
  566.     fpu->x13.x[0] = SWAP_LONG(fpu->x13.x[0]);
  567.     fpu->x13.x[1] = SWAP_LONG(fpu->x13.x[1]);
  568.     fpu->x13.x[2] = SWAP_LONG(fpu->x13.x[2]);
  569.     fpu->x13.x[3] = SWAP_LONG(fpu->x13.x[3]);
  570.     fpu->x14.x[0] = SWAP_LONG(fpu->x14.x[0]);
  571.     fpu->x14.x[1] = SWAP_LONG(fpu->x14.x[1]);
  572.     fpu->x14.x[2] = SWAP_LONG(fpu->x14.x[2]);
  573.     fpu->x14.x[3] = SWAP_LONG(fpu->x14.x[3]);
  574.     fpu->x15.x[0] = SWAP_LONG(fpu->x15.x[0]);
  575.     fpu->x15.x[1] = SWAP_LONG(fpu->x15.x[1]);
  576.     fpu->x15.x[2] = SWAP_LONG(fpu->x15.x[2]);
  577.     fpu->x15.x[3] = SWAP_LONG(fpu->x15.x[3]);
  578.     fpu->x16.x[0] = SWAP_LONG(fpu->x16.x[0]);
  579.     fpu->x16.x[1] = SWAP_LONG(fpu->x16.x[1]);
  580.     fpu->x16.x[2] = SWAP_LONG(fpu->x16.x[2]);
  581.     fpu->x16.x[3] = SWAP_LONG(fpu->x16.x[3]);
  582.     fpu->x17.x[0] = SWAP_LONG(fpu->x17.x[0]);
  583.     fpu->x17.x[1] = SWAP_LONG(fpu->x17.x[1]);
  584.     fpu->x17.x[2] = SWAP_LONG(fpu->x17.x[2]);
  585.     fpu->x17.x[3] = SWAP_LONG(fpu->x17.x[3]);
  586.     fpu->x18.x[0] = SWAP_LONG(fpu->x18.x[0]);
  587.     fpu->x18.x[1] = SWAP_LONG(fpu->x18.x[1]);
  588.     fpu->x18.x[2] = SWAP_LONG(fpu->x18.x[2]);
  589.     fpu->x18.x[3] = SWAP_LONG(fpu->x18.x[3]);
  590.     fpu->x19.x[0] = SWAP_LONG(fpu->x19.x[0]);
  591.     fpu->x19.x[1] = SWAP_LONG(fpu->x19.x[1]);
  592.     fpu->x19.x[2] = SWAP_LONG(fpu->x19.x[2]);
  593.     fpu->x19.x[3] = SWAP_LONG(fpu->x19.x[3]);
  594.     fpu->x20.x[0] = SWAP_LONG(fpu->x20.x[0]);
  595.     fpu->x20.x[1] = SWAP_LONG(fpu->x20.x[1]);
  596.     fpu->x20.x[2] = SWAP_LONG(fpu->x20.x[2]);
  597.     fpu->x20.x[3] = SWAP_LONG(fpu->x20.x[3]);
  598.     fpu->x21.x[0] = SWAP_LONG(fpu->x21.x[0]);
  599.     fpu->x21.x[1] = SWAP_LONG(fpu->x21.x[1]);
  600.     fpu->x21.x[2] = SWAP_LONG(fpu->x21.x[2]);
  601.     fpu->x21.x[3] = SWAP_LONG(fpu->x21.x[3]);
  602.     fpu->x22.x[0] = SWAP_LONG(fpu->x22.x[0]);
  603.     fpu->x22.x[1] = SWAP_LONG(fpu->x22.x[1]);
  604.     fpu->x22.x[2] = SWAP_LONG(fpu->x22.x[2]);
  605.     fpu->x22.x[3] = SWAP_LONG(fpu->x22.x[3]);
  606.     fpu->x23.x[0] = SWAP_LONG(fpu->x23.x[0]);
  607.     fpu->x23.x[1] = SWAP_LONG(fpu->x23.x[1]);
  608.     fpu->x23.x[2] = SWAP_LONG(fpu->x23.x[2]);
  609.     fpu->x23.x[3] = SWAP_LONG(fpu->x23.x[3]);
  610.     fpu->x24.x[0] = SWAP_LONG(fpu->x24.x[0]);
  611.     fpu->x24.x[1] = SWAP_LONG(fpu->x24.x[1]);
  612.     fpu->x24.x[2] = SWAP_LONG(fpu->x24.x[2]);
  613.     fpu->x24.x[3] = SWAP_LONG(fpu->x24.x[3]);
  614.     fpu->x25.x[0] = SWAP_LONG(fpu->x25.x[0]);
  615.     fpu->x25.x[1] = SWAP_LONG(fpu->x25.x[1]);
  616.     fpu->x25.x[2] = SWAP_LONG(fpu->x25.x[2]);
  617.     fpu->x25.x[3] = SWAP_LONG(fpu->x25.x[3]);
  618.     fpu->x26.x[0] = SWAP_LONG(fpu->x26.x[0]);
  619.     fpu->x26.x[1] = SWAP_LONG(fpu->x26.x[1]);
  620.     fpu->x26.x[2] = SWAP_LONG(fpu->x26.x[2]);
  621.     fpu->x26.x[3] = SWAP_LONG(fpu->x26.x[3]);
  622.     fpu->x27.x[0] = SWAP_LONG(fpu->x27.x[0]);
  623.     fpu->x27.x[1] = SWAP_LONG(fpu->x27.x[1]);
  624.     fpu->x27.x[2] = SWAP_LONG(fpu->x27.x[2]);
  625.     fpu->x27.x[3] = SWAP_LONG(fpu->x27.x[3]);
  626.     fpu->x28.x[0] = SWAP_LONG(fpu->x28.x[0]);
  627.     fpu->x28.x[1] = SWAP_LONG(fpu->x28.x[1]);
  628.     fpu->x28.x[2] = SWAP_LONG(fpu->x28.x[2]);
  629.     fpu->x28.x[3] = SWAP_LONG(fpu->x28.x[3]);
  630.     fpu->x29.x[0] = SWAP_LONG(fpu->x29.x[0]);
  631.     fpu->x29.x[1] = SWAP_LONG(fpu->x29.x[1]);
  632.     fpu->x29.x[2] = SWAP_LONG(fpu->x29.x[2]);
  633.     fpu->x29.x[3] = SWAP_LONG(fpu->x29.x[3]);
  634.     fpu->x30.x[0] = SWAP_LONG(fpu->x30.x[0]);
  635.     fpu->x30.x[1] = SWAP_LONG(fpu->x30.x[1]);
  636.     fpu->x30.x[2] = SWAP_LONG(fpu->x30.x[2]);
  637.     fpu->x30.x[3] = SWAP_LONG(fpu->x30.x[3]);
  638.     fpu->x31.x[0] = SWAP_LONG(fpu->x31.x[0]);
  639.     fpu->x31.x[1] = SWAP_LONG(fpu->x31.x[1]);
  640.     fpu->x31.x[2] = SWAP_LONG(fpu->x31.x[2]);
  641.     fpu->x31.x[3] = SWAP_LONG(fpu->x31.x[3]);
  642.  
  643.     if(target_byte_sex == host_byte_sex){
  644.         memcpy(&ssr, &(fpu->fpsr), sizeof(struct swapped_m88k_fpsr));
  645.         ssr.u.word = SWAP_LONG(ssr.u.word);
  646.         fpu->fpsr.afinx = ssr.u.fields.afinx;
  647.         fpu->fpsr.afovf = ssr.u.fields.afovf;
  648.         fpu->fpsr.afunf = ssr.u.fields.afunf;
  649.         fpu->fpsr.afdvz = ssr.u.fields.afdvz;
  650.         fpu->fpsr.afinv = ssr.u.fields.afinv;
  651.         fpu->fpsr.xmod = ssr.u.fields.xmod;
  652.  
  653.         memcpy(&scr, &(fpu->fpcr), sizeof(struct swapped_m88k_fpcr));
  654.         scr.u.word = SWAP_LONG(scr.u.word);
  655.         fpu->fpcr.efinx = scr.u.fields.efinx;
  656.         fpu->fpcr.efovf = scr.u.fields.efovf;
  657.         fpu->fpcr.efunf = scr.u.fields.efunf;
  658.         fpu->fpcr.efdvz = scr.u.fields.efdvz;
  659.         fpu->fpcr.efinv = scr.u.fields.efinv;
  660.         fpu->fpcr.rm = scr.u.fields.rm;
  661.     }
  662.     else{
  663.         ssr.u.fields.afinx = fpu->fpsr.afinx;
  664.         ssr.u.fields.afovf = fpu->fpsr.afovf;
  665.         ssr.u.fields.afunf = fpu->fpsr.afunf;
  666.         ssr.u.fields.afdvz = fpu->fpsr.afdvz;
  667.         ssr.u.fields.afinv = fpu->fpsr.afinv;
  668.         ssr.u.fields.xmod = fpu->fpsr.xmod;
  669.         ssr.u.word = SWAP_LONG(ssr.u.word);
  670.         memcpy(&(fpu->fpsr), &ssr, sizeof(struct swapped_m88k_fpsr));
  671.  
  672.         scr.u.fields.efinx = fpu->fpcr.efinx;
  673.         scr.u.fields.efovf = fpu->fpcr.efovf;
  674.         scr.u.fields.efunf = fpu->fpcr.efunf;
  675.         scr.u.fields.efdvz = fpu->fpcr.efdvz;
  676.         scr.u.fields.efinv = fpu->fpcr.efinv;
  677.         scr.u.fields.rm = fpu->fpcr.rm;
  678.         scr.u.word = SWAP_LONG(scr.u.word);
  679.         memcpy(&(fpu->fpcr), &scr, sizeof(struct swapped_m88k_fpcr));
  680.     }
  681. }
  682.  
  683. void
  684. swap_m88k_thread_state_user_t(
  685. m88k_thread_state_user_t *user,
  686. enum byte_sex target_byte_sex)
  687. {
  688.     user->user = SWAP_LONG(user->user);
  689. }
  690.  
  691. void
  692. swap_m88110_thread_state_impl_t(
  693. m88110_thread_state_impl_t *spu,
  694. enum byte_sex target_byte_sex)
  695. {
  696.     unsigned long i;
  697.     enum byte_sex host_byte_sex;
  698.  
  699.     struct swapped_m88110_bp_ctrl {
  700.     union {
  701.         struct {
  702.         unsigned    v:BIT_WIDTH(0);
  703.         m88110_match_t    addr_match:BITS_WIDTH(12,1);
  704.         unsigned    :BITS_WIDTH(26,13);
  705.         unsigned    rwm:BIT_WIDTH(27);
  706.         unsigned    rw:BIT_WIDTH(28);
  707.         unsigned    :BITS_WIDTH(31,29);
  708.         } fields;
  709.         unsigned long word;
  710.     } u;
  711.     } sbpc;
  712.  
  713.     struct swap_m88110_psr {
  714.     union {
  715.         struct {
  716.         unsigned    :BITS_WIDTH(1,0);
  717.         unsigned    mxm_dis:BIT_WIDTH(2);
  718.         unsigned    sfu1dis:BIT_WIDTH(3);
  719.         unsigned    :BITS_WIDTH(22,4);
  720.         unsigned    trace:BIT_WIDTH(23);
  721.         unsigned    :BIT_WIDTH(24);
  722.         unsigned    sm:BIT_WIDTH(25);
  723.         unsigned    sgn_imd:BIT_WIDTH(26);
  724.         unsigned    :BIT_WIDTH(27);
  725.         unsigned    c:BIT_WIDTH(28);
  726.         unsigned    se:BIT_WIDTH(29);
  727.         unsigned    le:BIT_WIDTH(30);
  728.         unsigned    supr:BIT_WIDTH(31);
  729.         } fields;
  730.         unsigned long word;
  731.     } u;
  732.     } spsr;
  733.  
  734.     struct swapped_m88110_fp_trap_status {
  735.     union {
  736.         struct {
  737.         unsigned    efinx:BIT_WIDTH(0);
  738.         unsigned    efovf:BIT_WIDTH(1);
  739.         unsigned    efunf:BIT_WIDTH(2);
  740.         unsigned    efdvz:BIT_WIDTH(3);
  741.         unsigned    efinv:BIT_WIDTH(4);
  742.         unsigned    priv:BIT_WIDTH(5);
  743.         unsigned    unimp:BIT_WIDTH(6);
  744.         unsigned    int:BIT_WIDTH(7);
  745.         unsigned    sfu1_disabled:BIT_WIDTH(8);
  746.         unsigned    :BITS_WIDTH(13,9);
  747.         m88110_iresult_size_t    iresult_size:BITS_WIDTH(15,14);
  748.         unsigned    :BITS_WIDTH(31,16);
  749.         } fields;
  750.         unsigned long word;
  751.     } u;
  752.     } sfps;
  753.  
  754.     host_byte_sex = get_host_byte_sex();
  755.  
  756.     if(target_byte_sex == host_byte_sex){
  757.         for(i = 0; i < M88110_N_DATA_BP; i++){
  758.         spu->data_bp[i].addr = SWAP_LONG(spu->data_bp[i].addr);
  759.         memcpy(&sbpc, &(spu->data_bp[i].ctrl),
  760.                sizeof(struct swapped_m88110_bp_ctrl));
  761.         sbpc.u.word = SWAP_LONG(sbpc.u.word);
  762.         spu->data_bp[i].ctrl.v = sbpc.u.fields.v;
  763.         spu->data_bp[i].ctrl.addr_match = sbpc.u.fields.addr_match;
  764.         spu->data_bp[i].ctrl.rwm = sbpc.u.fields.rwm;
  765.         spu->data_bp[i].ctrl.rw = sbpc.u.fields.rw;
  766.         }
  767.  
  768.         memcpy(&spsr, &(spu->psr), sizeof(struct swap_m88110_psr));
  769.         spsr.u.word = SWAP_LONG(spsr.u.word);
  770.         spu->psr.mxm_dis = spsr.u.fields.mxm_dis;
  771.         spu->psr.sfu1dis = spsr.u.fields.sfu1dis;
  772.         spu->psr.trace = spsr.u.fields.trace;
  773.         spu->psr.sm = spsr.u.fields.sm;
  774.         spu->psr.sgn_imd = spsr.u.fields.sgn_imd;
  775.         spu->psr.c = spsr.u.fields.c;
  776.         spu->psr.se = spsr.u.fields.se;
  777.         spu->psr.le = spsr.u.fields.le;
  778.         spu->psr.supr = spsr.u.fields.supr;
  779.  
  780.         memcpy(&sfps, &(spu->fp_trap_status),
  781.            sizeof(struct swapped_m88110_fp_trap_status));
  782.         sfps.u.word = SWAP_LONG(sfps.u.word);
  783.         spu->fp_trap_status.efinx = sfps.u.fields.efinx;
  784.         spu->fp_trap_status.efovf = sfps.u.fields.efovf;
  785.         spu->fp_trap_status.efunf = sfps.u.fields.efunf;
  786.         spu->fp_trap_status.efdvz = sfps.u.fields.efdvz;
  787.         spu->fp_trap_status.efinv = sfps.u.fields.efinv;
  788.         spu->fp_trap_status.priv = sfps.u.fields.priv;
  789.         spu->fp_trap_status.unimp = sfps.u.fields.unimp;
  790.         spu->fp_trap_status.sfu1_disabled = sfps.u.fields.sfu1_disabled;
  791.         spu->fp_trap_status.iresult_size = sfps.u.fields.iresult_size;
  792.     }
  793.     else{
  794.         for(i = 0; i < M88110_N_DATA_BP; i++){
  795.         spu->data_bp[i].addr = SWAP_LONG(spu->data_bp[i].addr);
  796.         sbpc.u.fields.v = spu->data_bp[i].ctrl.v;
  797.         sbpc.u.fields.addr_match = spu->data_bp[i].ctrl.addr_match;
  798.         sbpc.u.fields.rwm = spu->data_bp[i].ctrl.rwm;
  799.         sbpc.u.fields.rw = spu->data_bp[i].ctrl.rw;
  800.         sbpc.u.word = SWAP_LONG(sbpc.u.word);
  801.         memcpy(&(spu->data_bp[i].ctrl), &sbpc,
  802.                sizeof(struct swapped_m88110_bp_ctrl));
  803.         }
  804.  
  805.         spsr.u.fields.mxm_dis = spu->psr.mxm_dis;
  806.         spsr.u.fields.sfu1dis = spu->psr.sfu1dis;
  807.         spsr.u.fields.trace = spu->psr.trace;
  808.         spsr.u.fields.sm = spu->psr.sm;
  809.         spsr.u.fields.sgn_imd = spu->psr.sgn_imd;
  810.         spsr.u.fields.c = spu->psr.c;
  811.         spsr.u.fields.se = spu->psr.se;
  812.         spsr.u.fields.le = spu->psr.le;
  813.         spsr.u.fields.supr = spu->psr.supr;
  814.         spsr.u.word = SWAP_LONG(spsr.u.word);
  815.         memcpy(&(spu->psr), &spsr, sizeof(struct swap_m88110_psr));
  816.  
  817.         sfps.u.fields.efinx = spu->fp_trap_status.efinx;
  818.         sfps.u.fields.efovf = spu->fp_trap_status.efovf;
  819.         sfps.u.fields.efunf = spu->fp_trap_status.efunf;
  820.         sfps.u.fields.efdvz = spu->fp_trap_status.efdvz;
  821.         sfps.u.fields.efinv = spu->fp_trap_status.efinv;
  822.         sfps.u.fields.priv = spu->fp_trap_status.priv;
  823.         sfps.u.fields.unimp = spu->fp_trap_status.unimp;
  824.         sfps.u.fields.sfu1_disabled = spu->fp_trap_status.sfu1_disabled;
  825.         sfps.u.fields.iresult_size = spu->fp_trap_status.iresult_size;
  826.         sfps.u.word = SWAP_LONG(sfps.u.word);
  827.         memcpy(&(spu->fp_trap_status), &sfps,
  828.            sizeof(struct swapped_m88110_fp_trap_status));
  829.     }
  830.     spu->intermediate_result.x[0] =
  831.         SWAP_LONG(spu->intermediate_result.x[0]);
  832.     spu->intermediate_result.x[1] =
  833.         SWAP_LONG(spu->intermediate_result.x[1]);
  834.     spu->intermediate_result.x[2] =
  835.         SWAP_LONG(spu->intermediate_result.x[2]);
  836.     spu->intermediate_result.x[3] =
  837.         SWAP_LONG(spu->intermediate_result.x[3]);
  838. }
  839.  
  840. void
  841. swap_i860_thread_state_regs(
  842. struct i860_thread_state_regs *cpu,
  843. enum byte_sex target_byte_sex)
  844. {
  845.     unsigned long i;
  846.  
  847.     for(i = 0; i < 31; i++)
  848.         cpu->ireg[i] = SWAP_LONG(cpu->ireg[i]);
  849.     for(i = 0; i < 30; i++)
  850.         cpu->freg[i] = SWAP_LONG(cpu->freg[i]);
  851.     cpu->psr = SWAP_LONG(cpu->psr);
  852.     cpu->epsr = SWAP_LONG(cpu->epsr);
  853.     cpu->db = SWAP_LONG(cpu->db);
  854.     cpu->pc = SWAP_LONG(cpu->pc);
  855.     cpu->_padding_ = SWAP_LONG(cpu->_padding_);
  856.     cpu->Mres3 = SWAP_DOUBLE(cpu->Mres3);
  857.     cpu->Ares3 = SWAP_DOUBLE(cpu->Ares3);
  858.     cpu->Mres2 = SWAP_DOUBLE(cpu->Mres2);
  859.     cpu->Ares2 = SWAP_DOUBLE(cpu->Ares2);
  860.     cpu->Mres1 = SWAP_DOUBLE(cpu->Mres1);
  861.     cpu->Ares1 = SWAP_DOUBLE(cpu->Ares1);
  862.     cpu->Ires1 = SWAP_DOUBLE(cpu->Ires1);
  863.     cpu->Lres3m = SWAP_DOUBLE(cpu->Lres3m);
  864.     cpu->Lres2m = SWAP_DOUBLE(cpu->Lres2m);
  865.     cpu->Lres1m = SWAP_DOUBLE(cpu->Lres1m);
  866.     cpu->KR = SWAP_DOUBLE(cpu->KR);
  867.     cpu->KI = SWAP_DOUBLE(cpu->KI);
  868.     cpu->T = SWAP_DOUBLE(cpu->T);
  869.     cpu->Fsr3 = SWAP_LONG(cpu->Fsr3);
  870.     cpu->Fsr2 = SWAP_LONG(cpu->Fsr2);
  871.     cpu->Fsr1 = SWAP_LONG(cpu->Fsr1);
  872.     cpu->Mergelo32 = SWAP_LONG(cpu->Mergelo32);
  873.     cpu->Mergehi32 = SWAP_LONG(cpu->Mergehi32);
  874. }
  875.  
  876. void
  877. swap_i386_thread_state(
  878. i386_thread_state_t *cpu,
  879. enum byte_sex target_byte_sex)
  880. {
  881.     cpu->eax = SWAP_LONG(cpu->eax);
  882.     cpu->ebx = SWAP_LONG(cpu->ebx);
  883.     cpu->ecx = SWAP_LONG(cpu->ecx);
  884.     cpu->edx = SWAP_LONG(cpu->edx);
  885.     cpu->edi = SWAP_LONG(cpu->edi);
  886.     cpu->esi = SWAP_LONG(cpu->esi);
  887.     cpu->ebp = SWAP_LONG(cpu->ebp);
  888.     cpu->esp = SWAP_LONG(cpu->esp);
  889.     cpu->ss = SWAP_LONG(cpu->ss);
  890.     cpu->eflags = SWAP_LONG(cpu->eflags);
  891.     cpu->eip = SWAP_LONG(cpu->eip);
  892.     cpu->cs = SWAP_LONG(cpu->cs);
  893.     cpu->ds = SWAP_LONG(cpu->ds);
  894.     cpu->es = SWAP_LONG(cpu->es);
  895.     cpu->fs = SWAP_LONG(cpu->fs);
  896.     cpu->gs = SWAP_LONG(cpu->gs);
  897. }
  898.  
  899. void
  900. swap_i386_thread_fpstate(
  901. i386_thread_fpstate_t *fpu,
  902. enum byte_sex target_byte_sex)
  903. {
  904.     struct swapped_fp_control {
  905.     union {
  906.         struct {
  907.         unsigned short
  908.                 :3,
  909.             /*inf*/ :1,
  910.             rc        :2,
  911.             pc        :2,
  912.                 :2,
  913.             precis  :1,
  914.             undfl   :1,
  915.             ovrfl   :1,
  916.             zdiv    :1,
  917.             denorm  :1,
  918.             invalid :1;
  919.         } fields;
  920.         unsigned short half;
  921.     } u;
  922.     } sfpc;
  923.  
  924.     struct swapped_fp_status {
  925.     union {
  926.         struct {
  927.         unsigned short
  928.             busy    :1,
  929.             c3        :1,
  930.             tos        :3,
  931.             c2        :1,
  932.             c1        :1,
  933.             c0        :1,
  934.             errsumm :1,
  935.             stkflt  :1,
  936.             precis  :1,
  937.             undfl   :1,
  938.             ovrfl   :1,
  939.             zdiv    :1,
  940.             denorm  :1,
  941.             invalid :1;
  942.         } fields;
  943.         unsigned short half;
  944.     } u;
  945.     } sfps;
  946.  
  947.     struct swapped_fp_tag {
  948.     union {
  949.         struct {
  950.         unsigned short
  951.             tag7 :2,
  952.             tag6 :2,
  953.             tag5 :2,
  954.             tag4 :2,
  955.             tag3 :2,
  956.             tag2 :2,
  957.             tag1 :2,
  958.             tag0 :2;
  959.         } fields;
  960.         unsigned short half;
  961.     } u;
  962.     } sfpt;
  963.  
  964.     struct swapped_fp_data_reg {
  965.     unsigned short mant;
  966.     unsigned short mant1 :16,
  967.                mant2 :16,
  968.                mant3 :16;
  969.     union {
  970.         struct {
  971.         unsigned short sign :1,
  972.                    exp  :15;
  973.         } fields;
  974.         unsigned short half;
  975.     } u;
  976.     } sfpd;
  977.  
  978.     struct swapped_sel {
  979.     union {
  980.         struct {
  981.             unsigned short
  982.             index :13,
  983.             ti    :1,
  984.             rpl   :2;
  985.         } fields;
  986.         unsigned short half;
  987.     } u;
  988.     } ss;
  989.  
  990.     enum byte_sex host_byte_sex;
  991.     unsigned long i;
  992.  
  993.     host_byte_sex = get_host_byte_sex();
  994.  
  995.     fpu->environ.ip = SWAP_LONG(fpu->environ.ip);
  996.     fpu->environ.opcode = SWAP_SHORT(fpu->environ.opcode);
  997.     fpu->environ.dp = SWAP_LONG(fpu->environ.dp);
  998.  
  999.     if(target_byte_sex == host_byte_sex){
  1000.         memcpy(&sfpc, &(fpu->environ.control),
  1001.            sizeof(struct swapped_fp_control));
  1002.         sfpc.u.half = SWAP_SHORT(sfpc.u.half);
  1003.         fpu->environ.control.rc = sfpc.u.fields.rc;
  1004.         fpu->environ.control.pc = sfpc.u.fields.pc;
  1005.         fpu->environ.control.precis = sfpc.u.fields.precis;
  1006.         fpu->environ.control.undfl = sfpc.u.fields.undfl;
  1007.         fpu->environ.control.ovrfl = sfpc.u.fields.ovrfl;
  1008.         fpu->environ.control.zdiv = sfpc.u.fields.zdiv;
  1009.         fpu->environ.control.denorm = sfpc.u.fields.denorm;
  1010.         fpu->environ.control.invalid = sfpc.u.fields.invalid;
  1011.  
  1012.         memcpy(&sfps, &(fpu->environ.status),
  1013.            sizeof(struct swapped_fp_status));
  1014.         sfps.u.half = SWAP_SHORT(sfps.u.half);
  1015.         fpu->environ.status.busy = sfps.u.fields.busy;
  1016.         fpu->environ.status.c3 = sfps.u.fields.c3;
  1017.         fpu->environ.status.tos = sfps.u.fields.tos;
  1018.         fpu->environ.status.c2 = sfps.u.fields.c2;
  1019.         fpu->environ.status.c1 = sfps.u.fields.c1;
  1020.         fpu->environ.status.c0 = sfps.u.fields.c0;
  1021.         fpu->environ.status.errsumm = sfps.u.fields.errsumm;
  1022.         fpu->environ.status.stkflt = sfps.u.fields.stkflt;
  1023.         fpu->environ.status.precis = sfps.u.fields.precis;
  1024.         fpu->environ.status.undfl = sfps.u.fields.undfl;
  1025.         fpu->environ.status.ovrfl = sfps.u.fields.ovrfl;
  1026.         fpu->environ.status.zdiv = sfps.u.fields.zdiv;
  1027.         fpu->environ.status.denorm = sfps.u.fields.denorm;
  1028.         fpu->environ.status.invalid = sfps.u.fields.invalid;
  1029.  
  1030.         memcpy(&sfpt, &(fpu->environ.tag),
  1031.            sizeof(struct swapped_fp_tag));
  1032.         sfpt.u.half = SWAP_SHORT(sfpt.u.half);
  1033.         fpu->environ.tag.tag7 = sfpt.u.fields.tag7;
  1034.         fpu->environ.tag.tag6 = sfpt.u.fields.tag6;
  1035.         fpu->environ.tag.tag5 = sfpt.u.fields.tag5;
  1036.         fpu->environ.tag.tag4 = sfpt.u.fields.tag4;
  1037.         fpu->environ.tag.tag3 = sfpt.u.fields.tag3;
  1038.         fpu->environ.tag.tag2 = sfpt.u.fields.tag2;
  1039.         fpu->environ.tag.tag1 = sfpt.u.fields.tag1;
  1040.         fpu->environ.tag.tag0 = sfpt.u.fields.tag0;
  1041.  
  1042.         memcpy(&ss, &(fpu->environ.cs),
  1043.            sizeof(struct swapped_sel));
  1044.         ss.u.half = SWAP_SHORT(ss.u.half);
  1045.         fpu->environ.cs.index = ss.u.fields.index;
  1046.         fpu->environ.cs.ti = ss.u.fields.ti;
  1047.         fpu->environ.cs.rpl = ss.u.fields.rpl;
  1048.  
  1049.         memcpy(&ss, &(fpu->environ.ds),
  1050.            sizeof(struct swapped_sel));
  1051.         ss.u.half = SWAP_SHORT(ss.u.half);
  1052.         fpu->environ.ds.index = ss.u.fields.index;
  1053.         fpu->environ.ds.ti = ss.u.fields.ti;
  1054.         fpu->environ.ds.rpl = ss.u.fields.rpl;
  1055.     
  1056.         for(i = 0; i < 8; i++){
  1057.         memcpy(&sfpd, &(fpu->stack.ST[i]),
  1058.                sizeof(struct swapped_fp_data_reg));
  1059.         fpu->stack.ST[i].mant = SWAP_SHORT(sfpd.mant);
  1060.         fpu->stack.ST[i].mant1 = SWAP_SHORT(sfpd.mant1);
  1061.         fpu->stack.ST[i].mant2 = SWAP_SHORT(sfpd.mant2);
  1062.         fpu->stack.ST[i].mant3 = SWAP_SHORT(sfpd.mant3);
  1063.         sfpd.u.half = SWAP_SHORT(sfpd.u.half);
  1064.         fpu->stack.ST[i].exp = sfpd.u.fields.exp;
  1065.         fpu->stack.ST[i].sign = sfpd.u.fields.sign;
  1066.         }
  1067.     }
  1068.     else{
  1069.         sfpc.u.fields.rc = fpu->environ.control.rc;
  1070.         sfpc.u.fields.pc = fpu->environ.control.pc;
  1071.         sfpc.u.fields.precis = fpu->environ.control.precis;
  1072.         sfpc.u.fields.undfl = fpu->environ.control.undfl;
  1073.         sfpc.u.fields.ovrfl = fpu->environ.control.ovrfl;
  1074.         sfpc.u.fields.zdiv = fpu->environ.control.zdiv;
  1075.         sfpc.u.fields.denorm = fpu->environ.control.denorm;
  1076.         sfpc.u.fields.invalid = fpu->environ.control.invalid;
  1077.         sfpc.u.half = SWAP_SHORT(sfpc.u.half);
  1078.         memcpy(&(fpu->environ.control), &sfpc,
  1079.            sizeof(struct swapped_fp_control));
  1080.  
  1081.         sfps.u.fields.busy = fpu->environ.status.busy;
  1082.         sfps.u.fields.c3 = fpu->environ.status.c3;
  1083.         sfps.u.fields.tos = fpu->environ.status.tos;
  1084.         sfps.u.fields.c2 = fpu->environ.status.c2;
  1085.         sfps.u.fields.c1 = fpu->environ.status.c1;
  1086.         sfps.u.fields.c0 = fpu->environ.status.c0;
  1087.         sfps.u.fields.errsumm = fpu->environ.status.errsumm;
  1088.         sfps.u.fields.stkflt = fpu->environ.status.stkflt;
  1089.         sfps.u.fields.precis = fpu->environ.status.precis;
  1090.         sfps.u.fields.undfl = fpu->environ.status.undfl;
  1091.         sfps.u.fields.ovrfl = fpu->environ.status.ovrfl;
  1092.         sfps.u.fields.zdiv = fpu->environ.status.zdiv;
  1093.         sfps.u.fields.denorm = fpu->environ.status.denorm;
  1094.         sfps.u.fields.invalid = fpu->environ.status.invalid;
  1095.         sfps.u.half = SWAP_SHORT(sfps.u.half);
  1096.         memcpy(&(fpu->environ.status), &sfps,
  1097.            sizeof(struct swapped_fp_status));
  1098.  
  1099.         sfpt.u.fields.tag7 = fpu->environ.tag.tag7;
  1100.         sfpt.u.fields.tag6 = fpu->environ.tag.tag6;
  1101.         sfpt.u.fields.tag5 = fpu->environ.tag.tag5;
  1102.         sfpt.u.fields.tag4 = fpu->environ.tag.tag4;
  1103.         sfpt.u.fields.tag3 = fpu->environ.tag.tag3;
  1104.         sfpt.u.fields.tag2 = fpu->environ.tag.tag2;
  1105.         sfpt.u.fields.tag1 = fpu->environ.tag.tag1;
  1106.         sfpt.u.fields.tag0 = fpu->environ.tag.tag0;
  1107.         sfpt.u.half = SWAP_SHORT(sfpt.u.half);
  1108.         memcpy(&(fpu->environ.tag), &sfpt,
  1109.            sizeof(struct swapped_fp_tag));
  1110.  
  1111.         ss.u.fields.index = fpu->environ.cs.index;
  1112.         ss.u.fields.ti = fpu->environ.cs.ti;
  1113.         ss.u.fields.rpl = fpu->environ.cs.rpl;
  1114.         ss.u.half = SWAP_SHORT(ss.u.half);
  1115.         memcpy(&(fpu->environ.cs), &ss,
  1116.            sizeof(struct swapped_sel));
  1117.  
  1118.         ss.u.fields.index = fpu->environ.ds.index;
  1119.         ss.u.fields.ti = fpu->environ.ds.ti;
  1120.         ss.u.fields.rpl = fpu->environ.ds.rpl;
  1121.         ss.u.half = SWAP_SHORT(ss.u.half);
  1122.         memcpy(&(fpu->environ.cs), &ss,
  1123.            sizeof(struct swapped_sel));
  1124.  
  1125.         for(i = 0; i < 8; i++){
  1126.         sfpd.mant = SWAP_SHORT(fpu->stack.ST[i].mant);
  1127.         sfpd.mant1 = SWAP_SHORT(fpu->stack.ST[i].mant1);
  1128.         sfpd.mant2 = SWAP_SHORT(fpu->stack.ST[i].mant2);
  1129.         sfpd.mant3 = SWAP_SHORT(fpu->stack.ST[i].mant3);
  1130.         sfpd.u.fields.exp = fpu->stack.ST[i].exp;
  1131.         sfpd.u.fields.sign = fpu->stack.ST[i].sign;
  1132.         sfpd.u.half = SWAP_SHORT(sfpd.u.half);
  1133.         memcpy(&(fpu->stack.ST[i]), &sfpd,
  1134.                sizeof(struct swapped_fp_data_reg));
  1135.         }
  1136.     }
  1137. }
  1138.  
  1139. void
  1140. swap_i386_thread_exceptstate(
  1141. i386_thread_exceptstate_t *exc,
  1142. enum byte_sex target_byte_sex)
  1143. {
  1144.     struct swapped_err_code {
  1145.     union {
  1146.         struct err_code_normal {
  1147.         unsigned int        :16,
  1148.                 index    :13,
  1149.                 tbl    :2,
  1150.                 ext    :1;
  1151.         } normal;
  1152.         struct err_code_pgfault {
  1153.         unsigned int        :29,
  1154.                 user    :1,
  1155.                 wrtflt    :1,
  1156.                 prot    :1;
  1157.         } pgfault;
  1158.         unsigned long word;
  1159.     } u;
  1160.     } sec;
  1161.     unsigned long word;
  1162.     enum byte_sex host_byte_sex;
  1163.  
  1164.     host_byte_sex = get_host_byte_sex();
  1165.  
  1166.     exc->trapno = SWAP_LONG(exc->trapno);
  1167.     if(exc->trapno == 14){
  1168.         if(target_byte_sex == host_byte_sex){
  1169.         memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code));
  1170.         sec.u.word = SWAP_LONG(sec.u.word);
  1171.         exc->err.pgfault.user   = sec.u.pgfault.user;
  1172.         exc->err.pgfault.wrtflt = sec.u.pgfault.wrtflt;
  1173.         exc->err.pgfault.prot   = sec.u.pgfault.prot;
  1174.         }
  1175.         else{
  1176.         sec.u.pgfault.prot   = exc->err.pgfault.prot;
  1177.         sec.u.pgfault.wrtflt = exc->err.pgfault.wrtflt;
  1178.         sec.u.pgfault.user   = exc->err.pgfault.user;
  1179.         sec.u.word = SWAP_LONG(sec.u.word);
  1180.         memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code));
  1181.         }
  1182.     }
  1183.     else{
  1184.         if(target_byte_sex == host_byte_sex){
  1185.         memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code));
  1186.         sec.u.word = SWAP_LONG(sec.u.word);
  1187.         word = sec.u.normal.index;
  1188.         exc->err.normal.index = SWAP_LONG(word);
  1189.         exc->err.normal.tbl   = sec.u.normal.tbl;
  1190.         exc->err.normal.ext   = sec.u.normal.ext;
  1191.         }
  1192.         else{
  1193.         sec.u.normal.ext   = exc->err.normal.ext;
  1194.         sec.u.normal.tbl   = exc->err.normal.tbl;
  1195.         word = exc->err.normal.index;
  1196.         sec.u.normal.index = SWAP_LONG(word);
  1197.         sec.u.word = SWAP_LONG(sec.u.word);
  1198.         memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code));
  1199.         }
  1200.     }
  1201. }
  1202.  
  1203. void
  1204. swap_i386_thread_cthreadstate(
  1205. i386_thread_cthreadstate_t *user,
  1206. enum byte_sex target_byte_sex)
  1207. {
  1208.     user->self = SWAP_LONG(user->self);
  1209. }
  1210.  
  1211. void
  1212. swap_ident_command(
  1213. struct ident_command *id,
  1214. enum byte_sex target_byte_sex)
  1215. {
  1216.     id->cmd = SWAP_LONG(id->cmd);
  1217.     id->cmdsize = SWAP_LONG(id->cmdsize);
  1218. }
  1219.  
  1220. void
  1221. swap_nlist(
  1222. struct nlist *symbols,
  1223. unsigned long nsymbols,
  1224. enum byte_sex target_byte_sex)
  1225. {
  1226.     unsigned long i;
  1227.  
  1228.     for(i = 0; i < nsymbols; i++){
  1229.         symbols[i].n_un.n_strx = SWAP_LONG(symbols[i].n_un.n_strx);
  1230.         /* n_type */
  1231.         /* n_sect */
  1232.         symbols[i].n_desc = SWAP_SHORT(symbols[i].n_desc);
  1233.         symbols[i].n_value = SWAP_LONG(symbols[i].n_value);
  1234.     }
  1235. }
  1236.  
  1237. void
  1238. swap_ranlib(
  1239. struct ranlib *ranlibs,
  1240. unsigned long nranlibs,
  1241. enum byte_sex target_byte_sex)
  1242. {
  1243.     unsigned long i;
  1244.  
  1245.     for(i = 0; i < nranlibs; i++){
  1246.         ranlibs[i].ran_un.ran_strx = SWAP_LONG(ranlibs[i].ran_un.ran_strx);
  1247.         ranlibs[i].ran_off = SWAP_LONG(ranlibs[i].ran_off);
  1248.     }
  1249. }
  1250.  
  1251. void
  1252. swap_relocation_info(
  1253. struct relocation_info *relocs,
  1254. unsigned long nrelocs,
  1255. enum byte_sex target_byte_sex)
  1256. {
  1257.     unsigned long i;
  1258.     enum byte_sex host_byte_sex;
  1259.     enum bool to_host_byte_sex, scattered;
  1260.  
  1261.     struct swapped_relocation_info {
  1262.     long    r_address;
  1263.     union {
  1264.         struct {
  1265.         unsigned int
  1266.             r_type:4,
  1267.             r_extern:1,
  1268.             r_length:2,
  1269.             r_pcrel:1,
  1270.             r_symbolnum:24;
  1271.         } fields;
  1272.         unsigned long word;
  1273.     } u;
  1274.     } sr;
  1275.  
  1276.     struct swapped_scattered_relocation_info {
  1277.     unsigned long word;
  1278.     long    r_value;
  1279.     } *ssr;
  1280.  
  1281.     host_byte_sex = get_host_byte_sex();
  1282.     to_host_byte_sex = target_byte_sex == host_byte_sex;
  1283.  
  1284.     for(i = 0; i < nrelocs; i++){
  1285.         if(to_host_byte_sex)
  1286.         scattered = (SWAP_LONG(relocs[i].r_address) & R_SCATTERED) != 0;
  1287.         else
  1288.         scattered = ((relocs[i].r_address) & R_SCATTERED) != 0;
  1289.         if(scattered == FALSE){
  1290.         if(to_host_byte_sex){
  1291.             memcpy(&sr, relocs + i, sizeof(struct relocation_info));
  1292.             sr.r_address = SWAP_LONG(sr.r_address);
  1293.             sr.u.word = SWAP_LONG(sr.u.word);
  1294.             relocs[i].r_address = sr.r_address;
  1295.             relocs[i].r_symbolnum = sr.u.fields.r_symbolnum;
  1296.             relocs[i].r_pcrel = sr.u.fields.r_pcrel;
  1297.             relocs[i].r_length = sr.u.fields.r_length;
  1298.             relocs[i].r_extern = sr.u.fields.r_extern;
  1299.             relocs[i].r_type = sr.u.fields.r_type;
  1300.         }
  1301.         else{
  1302.             sr.r_address = relocs[i].r_address;
  1303.             sr.u.fields.r_symbolnum = relocs[i].r_symbolnum;
  1304.             sr.u.fields.r_length = relocs[i].r_length;
  1305.             sr.u.fields.r_pcrel = relocs[i].r_pcrel;
  1306.             sr.u.fields.r_extern = relocs[i].r_extern;
  1307.             sr.u.fields.r_type = relocs[i].r_type;
  1308.             sr.r_address = SWAP_LONG(sr.r_address);
  1309.             sr.u.word = SWAP_LONG(sr.u.word);
  1310.             memcpy(relocs + i, &sr, sizeof(struct relocation_info));
  1311.         }
  1312.         }
  1313.         else{
  1314.         ssr = (struct swapped_scattered_relocation_info *)(relocs + i);
  1315.         ssr->word = SWAP_LONG(ssr->word);
  1316.         ssr->r_value = SWAP_LONG(ssr->r_value);
  1317.         }
  1318.     }
  1319. }
  1320.