ranlib: fix it
[odcctools-svp.git] / libmacho / i386_swap.c
blob70f8782e7bfa5b20e34f661a722b73068267ce2b
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #ifndef RLD
24 #ifdef SHLIB
25 #undef environ
26 #endif
28 #include <string.h>
29 #include <mach-o/i386/swap.h>
31 void
32 swap_i386_thread_state(
33 i386_thread_state_t *cpu,
34 enum NXByteOrder target_byte_sex)
36 cpu->eax = NXSwapLong(cpu->eax);
37 cpu->ebx = NXSwapLong(cpu->ebx);
38 cpu->ecx = NXSwapLong(cpu->ecx);
39 cpu->edx = NXSwapLong(cpu->edx);
40 cpu->edi = NXSwapLong(cpu->edi);
41 cpu->esi = NXSwapLong(cpu->esi);
42 cpu->ebp = NXSwapLong(cpu->ebp);
43 cpu->esp = NXSwapLong(cpu->esp);
44 cpu->ss = NXSwapLong(cpu->ss);
45 cpu->eflags = NXSwapLong(cpu->eflags);
46 cpu->eip = NXSwapLong(cpu->eip);
47 cpu->cs = NXSwapLong(cpu->cs);
48 cpu->ds = NXSwapLong(cpu->ds);
49 cpu->es = NXSwapLong(cpu->es);
50 cpu->fs = NXSwapLong(cpu->fs);
51 cpu->gs = NXSwapLong(cpu->gs);
54 /* current i386 thread states */
55 #if i386_THREAD_STATE == 1
56 void
57 swap_i386_float_state(
58 struct i386_float_state *fpu,
59 enum NXByteOrder target_byte_sex)
61 #ifndef i386_EXCEPTION_STATE_COUNT
62 /* this routine does nothing as their are currently no non-byte fields */
63 #else /* !defined(i386_EXCEPTION_STATE_COUNT) */
64 struct swapped_fp_control {
65 union {
66 struct {
67 unsigned short
68 :3,
69 /*inf*/ :1,
70 rc :2,
71 pc :2,
72 :2,
73 precis :1,
74 undfl :1,
75 ovrfl :1,
76 zdiv :1,
77 denorm :1,
78 invalid :1;
79 } fields;
80 unsigned short half;
81 } u;
82 } sfpc;
84 struct swapped_fp_status {
85 union {
86 struct {
87 unsigned short
88 busy :1,
89 c3 :1,
90 tos :3,
91 c2 :1,
92 c1 :1,
93 c0 :1,
94 errsumm :1,
95 stkflt :1,
96 precis :1,
97 undfl :1,
98 ovrfl :1,
99 zdiv :1,
100 denorm :1,
101 invalid :1;
102 } fields;
103 unsigned short half;
104 } u;
105 } sfps;
107 enum NXByteOrder host_byte_sex;
109 host_byte_sex = NXHostByteOrder();
111 fpu->fpu_reserved[0] = NXSwapLong(fpu->fpu_reserved[0]);
112 fpu->fpu_reserved[1] = NXSwapLong(fpu->fpu_reserved[1]);
114 if(target_byte_sex == host_byte_sex){
115 memcpy(&sfpc, &(fpu->fpu_fcw),
116 sizeof(struct swapped_fp_control));
117 sfpc.u.half = NXSwapShort(sfpc.u.half);
118 fpu->fpu_fcw.rc = sfpc.u.fields.rc;
119 fpu->fpu_fcw.pc = sfpc.u.fields.pc;
120 fpu->fpu_fcw.precis = sfpc.u.fields.precis;
121 fpu->fpu_fcw.undfl = sfpc.u.fields.undfl;
122 fpu->fpu_fcw.ovrfl = sfpc.u.fields.ovrfl;
123 fpu->fpu_fcw.zdiv = sfpc.u.fields.zdiv;
124 fpu->fpu_fcw.denorm = sfpc.u.fields.denorm;
125 fpu->fpu_fcw.invalid = sfpc.u.fields.invalid;
127 memcpy(&sfps, &(fpu->fpu_fsw),
128 sizeof(struct swapped_fp_status));
129 sfps.u.half = NXSwapShort(sfps.u.half);
130 fpu->fpu_fsw.busy = sfps.u.fields.busy;
131 fpu->fpu_fsw.c3 = sfps.u.fields.c3;
132 fpu->fpu_fsw.tos = sfps.u.fields.tos;
133 fpu->fpu_fsw.c2 = sfps.u.fields.c2;
134 fpu->fpu_fsw.c1 = sfps.u.fields.c1;
135 fpu->fpu_fsw.c0 = sfps.u.fields.c0;
136 fpu->fpu_fsw.errsumm = sfps.u.fields.errsumm;
137 fpu->fpu_fsw.stkflt = sfps.u.fields.stkflt;
138 fpu->fpu_fsw.precis = sfps.u.fields.precis;
139 fpu->fpu_fsw.undfl = sfps.u.fields.undfl;
140 fpu->fpu_fsw.ovrfl = sfps.u.fields.ovrfl;
141 fpu->fpu_fsw.zdiv = sfps.u.fields.zdiv;
142 fpu->fpu_fsw.denorm = sfps.u.fields.denorm;
143 fpu->fpu_fsw.invalid = sfps.u.fields.invalid;
145 else{
146 sfpc.u.fields.rc = fpu->fpu_fcw.rc;
147 sfpc.u.fields.pc = fpu->fpu_fcw.pc;
148 sfpc.u.fields.precis = fpu->fpu_fcw.precis;
149 sfpc.u.fields.undfl = fpu->fpu_fcw.undfl;
150 sfpc.u.fields.ovrfl = fpu->fpu_fcw.ovrfl;
151 sfpc.u.fields.zdiv = fpu->fpu_fcw.zdiv;
152 sfpc.u.fields.denorm = fpu->fpu_fcw.denorm;
153 sfpc.u.fields.invalid = fpu->fpu_fcw.invalid;
154 sfpc.u.half = NXSwapShort(sfpc.u.half);
155 memcpy(&(fpu->fpu_fcw), &sfpc,
156 sizeof(struct swapped_fp_control));
158 sfps.u.fields.busy = fpu->fpu_fsw.busy;
159 sfps.u.fields.c3 = fpu->fpu_fsw.c3;
160 sfps.u.fields.tos = fpu->fpu_fsw.tos;
161 sfps.u.fields.c2 = fpu->fpu_fsw.c2;
162 sfps.u.fields.c1 = fpu->fpu_fsw.c1;
163 sfps.u.fields.c0 = fpu->fpu_fsw.c0;
164 sfps.u.fields.errsumm = fpu->fpu_fsw.errsumm;
165 sfps.u.fields.stkflt = fpu->fpu_fsw.stkflt;
166 sfps.u.fields.precis = fpu->fpu_fsw.precis;
167 sfps.u.fields.undfl = fpu->fpu_fsw.undfl;
168 sfps.u.fields.ovrfl = fpu->fpu_fsw.ovrfl;
169 sfps.u.fields.zdiv = fpu->fpu_fsw.zdiv;
170 sfps.u.fields.denorm = fpu->fpu_fsw.denorm;
171 sfps.u.fields.invalid = fpu->fpu_fsw.invalid;
172 sfps.u.half = NXSwapShort(sfps.u.half);
173 memcpy(&(fpu->fpu_fsw), &sfps,
174 sizeof(struct swapped_fp_status));
176 fpu->fpu_fop = NXSwapShort(fpu->fpu_fop);
177 fpu->fpu_ip = NXSwapLong(fpu->fpu_ip);
178 fpu->fpu_cs = NXSwapShort(fpu->fpu_cs);
179 fpu->fpu_rsrv2 = NXSwapShort(fpu->fpu_rsrv2);
180 fpu->fpu_dp = NXSwapLong(fpu->fpu_dp);
181 fpu->fpu_ds = NXSwapShort(fpu->fpu_ds);
182 fpu->fpu_rsrv3 = NXSwapShort(fpu->fpu_rsrv3);
183 fpu->fpu_mxcsr = NXSwapLong(fpu->fpu_mxcsr);
184 fpu->fpu_mxcsrmask = NXSwapLong(fpu->fpu_mxcsrmask);
185 fpu->fpu_reserved1 = NXSwapLong(fpu->fpu_reserved1);
187 #endif /* !defined(i386_EXCEPTION_STATE_COUNT) */
190 void
191 swap_i386_exception_state(
192 i386_exception_state_t *exc,
193 enum NXByteOrder target_byte_order)
195 exc->trapno = NXSwapLong(exc->trapno);
196 exc->err = NXSwapLong(exc->err);
197 exc->faultvaddr = NXSwapLong(exc->faultvaddr);
199 #endif /* i386_THREAD_STATE == 1 */
201 /* i386 thread states on older releases */
202 #if i386_THREAD_STATE == -1
203 void
204 swap_i386_thread_fpstate(
205 i386_thread_fpstate_t *fpu,
206 enum NXByteOrder target_byte_sex)
208 struct swapped_fp_control {
209 union {
210 struct {
211 unsigned short
213 /*inf*/ :1,
214 rc :2,
215 pc :2,
217 precis :1,
218 undfl :1,
219 ovrfl :1,
220 zdiv :1,
221 denorm :1,
222 invalid :1;
223 } fields;
224 unsigned short half;
225 } u;
226 } sfpc;
228 struct swapped_fp_status {
229 union {
230 struct {
231 unsigned short
232 busy :1,
233 c3 :1,
234 tos :3,
235 c2 :1,
236 c1 :1,
237 c0 :1,
238 errsumm :1,
239 stkflt :1,
240 precis :1,
241 undfl :1,
242 ovrfl :1,
243 zdiv :1,
244 denorm :1,
245 invalid :1;
246 } fields;
247 unsigned short half;
248 } u;
249 } sfps;
251 struct swapped_fp_tag {
252 union {
253 struct {
254 unsigned short
255 tag7 :2,
256 tag6 :2,
257 tag5 :2,
258 tag4 :2,
259 tag3 :2,
260 tag2 :2,
261 tag1 :2,
262 tag0 :2;
263 } fields;
264 unsigned short half;
265 } u;
266 } sfpt;
268 struct swapped_fp_data_reg {
269 unsigned short mant;
270 unsigned short mant1 :16,
271 mant2 :16,
272 mant3 :16;
273 union {
274 struct {
275 unsigned short sign :1,
276 exp :15;
277 } fields;
278 unsigned short half;
279 } u;
280 } sfpd;
282 struct swapped_sel {
283 union {
284 struct {
285 unsigned short
286 index :13,
287 ti :1,
288 rpl :2;
289 } fields;
290 unsigned short half;
291 } u;
292 } ss;
294 enum NXByteOrder host_byte_sex;
295 unsigned long i;
297 host_byte_sex = NXHostByteOrder();
299 fpu->environ.ip = NXSwapLong(fpu->environ.ip);
300 fpu->environ.opcode = NXSwapShort(fpu->environ.opcode);
301 fpu->environ.dp = NXSwapLong(fpu->environ.dp);
303 if(target_byte_sex == host_byte_sex){
304 memcpy(&sfpc, &(fpu->environ.control),
305 sizeof(struct swapped_fp_control));
306 sfpc.u.half = NXSwapShort(sfpc.u.half);
307 fpu->environ.control.rc = sfpc.u.fields.rc;
308 fpu->environ.control.pc = sfpc.u.fields.pc;
309 fpu->environ.control.precis = sfpc.u.fields.precis;
310 fpu->environ.control.undfl = sfpc.u.fields.undfl;
311 fpu->environ.control.ovrfl = sfpc.u.fields.ovrfl;
312 fpu->environ.control.zdiv = sfpc.u.fields.zdiv;
313 fpu->environ.control.denorm = sfpc.u.fields.denorm;
314 fpu->environ.control.invalid = sfpc.u.fields.invalid;
316 memcpy(&sfps, &(fpu->environ.status),
317 sizeof(struct swapped_fp_status));
318 sfps.u.half = NXSwapShort(sfps.u.half);
319 fpu->environ.status.busy = sfps.u.fields.busy;
320 fpu->environ.status.c3 = sfps.u.fields.c3;
321 fpu->environ.status.tos = sfps.u.fields.tos;
322 fpu->environ.status.c2 = sfps.u.fields.c2;
323 fpu->environ.status.c1 = sfps.u.fields.c1;
324 fpu->environ.status.c0 = sfps.u.fields.c0;
325 fpu->environ.status.errsumm = sfps.u.fields.errsumm;
326 fpu->environ.status.stkflt = sfps.u.fields.stkflt;
327 fpu->environ.status.precis = sfps.u.fields.precis;
328 fpu->environ.status.undfl = sfps.u.fields.undfl;
329 fpu->environ.status.ovrfl = sfps.u.fields.ovrfl;
330 fpu->environ.status.zdiv = sfps.u.fields.zdiv;
331 fpu->environ.status.denorm = sfps.u.fields.denorm;
332 fpu->environ.status.invalid = sfps.u.fields.invalid;
334 memcpy(&sfpt, &(fpu->environ.tag),
335 sizeof(struct swapped_fp_tag));
336 sfpt.u.half = NXSwapShort(sfpt.u.half);
337 fpu->environ.tag.tag7 = sfpt.u.fields.tag7;
338 fpu->environ.tag.tag6 = sfpt.u.fields.tag6;
339 fpu->environ.tag.tag5 = sfpt.u.fields.tag5;
340 fpu->environ.tag.tag4 = sfpt.u.fields.tag4;
341 fpu->environ.tag.tag3 = sfpt.u.fields.tag3;
342 fpu->environ.tag.tag2 = sfpt.u.fields.tag2;
343 fpu->environ.tag.tag1 = sfpt.u.fields.tag1;
344 fpu->environ.tag.tag0 = sfpt.u.fields.tag0;
346 memcpy(&ss, &(fpu->environ.cs),
347 sizeof(struct swapped_sel));
348 ss.u.half = NXSwapShort(ss.u.half);
349 fpu->environ.cs.index = ss.u.fields.index;
350 fpu->environ.cs.ti = ss.u.fields.ti;
351 fpu->environ.cs.rpl = ss.u.fields.rpl;
353 memcpy(&ss, &(fpu->environ.ds),
354 sizeof(struct swapped_sel));
355 ss.u.half = NXSwapShort(ss.u.half);
356 fpu->environ.ds.index = ss.u.fields.index;
357 fpu->environ.ds.ti = ss.u.fields.ti;
358 fpu->environ.ds.rpl = ss.u.fields.rpl;
360 for(i = 0; i < 8; i++){
361 memcpy(&sfpd, &(fpu->stack.ST[i]),
362 sizeof(struct swapped_fp_data_reg));
363 fpu->stack.ST[i].mant = NXSwapShort(sfpd.mant);
364 fpu->stack.ST[i].mant1 = NXSwapShort(sfpd.mant1);
365 fpu->stack.ST[i].mant2 = NXSwapShort(sfpd.mant2);
366 fpu->stack.ST[i].mant3 = NXSwapShort(sfpd.mant3);
367 sfpd.u.half = NXSwapShort(sfpd.u.half);
368 fpu->stack.ST[i].exp = sfpd.u.fields.exp;
369 fpu->stack.ST[i].sign = sfpd.u.fields.sign;
372 else{
373 sfpc.u.fields.rc = fpu->environ.control.rc;
374 sfpc.u.fields.pc = fpu->environ.control.pc;
375 sfpc.u.fields.precis = fpu->environ.control.precis;
376 sfpc.u.fields.undfl = fpu->environ.control.undfl;
377 sfpc.u.fields.ovrfl = fpu->environ.control.ovrfl;
378 sfpc.u.fields.zdiv = fpu->environ.control.zdiv;
379 sfpc.u.fields.denorm = fpu->environ.control.denorm;
380 sfpc.u.fields.invalid = fpu->environ.control.invalid;
381 sfpc.u.half = NXSwapShort(sfpc.u.half);
382 memcpy(&(fpu->environ.control), &sfpc,
383 sizeof(struct swapped_fp_control));
385 sfps.u.fields.busy = fpu->environ.status.busy;
386 sfps.u.fields.c3 = fpu->environ.status.c3;
387 sfps.u.fields.tos = fpu->environ.status.tos;
388 sfps.u.fields.c2 = fpu->environ.status.c2;
389 sfps.u.fields.c1 = fpu->environ.status.c1;
390 sfps.u.fields.c0 = fpu->environ.status.c0;
391 sfps.u.fields.errsumm = fpu->environ.status.errsumm;
392 sfps.u.fields.stkflt = fpu->environ.status.stkflt;
393 sfps.u.fields.precis = fpu->environ.status.precis;
394 sfps.u.fields.undfl = fpu->environ.status.undfl;
395 sfps.u.fields.ovrfl = fpu->environ.status.ovrfl;
396 sfps.u.fields.zdiv = fpu->environ.status.zdiv;
397 sfps.u.fields.denorm = fpu->environ.status.denorm;
398 sfps.u.fields.invalid = fpu->environ.status.invalid;
399 sfps.u.half = NXSwapShort(sfps.u.half);
400 memcpy(&(fpu->environ.status), &sfps,
401 sizeof(struct swapped_fp_status));
403 sfpt.u.fields.tag7 = fpu->environ.tag.tag7;
404 sfpt.u.fields.tag6 = fpu->environ.tag.tag6;
405 sfpt.u.fields.tag5 = fpu->environ.tag.tag5;
406 sfpt.u.fields.tag4 = fpu->environ.tag.tag4;
407 sfpt.u.fields.tag3 = fpu->environ.tag.tag3;
408 sfpt.u.fields.tag2 = fpu->environ.tag.tag2;
409 sfpt.u.fields.tag1 = fpu->environ.tag.tag1;
410 sfpt.u.fields.tag0 = fpu->environ.tag.tag0;
411 sfpt.u.half = NXSwapShort(sfpt.u.half);
412 memcpy(&(fpu->environ.tag), &sfpt,
413 sizeof(struct swapped_fp_tag));
415 ss.u.fields.index = fpu->environ.cs.index;
416 ss.u.fields.ti = fpu->environ.cs.ti;
417 ss.u.fields.rpl = fpu->environ.cs.rpl;
418 ss.u.half = NXSwapShort(ss.u.half);
419 memcpy(&(fpu->environ.cs), &ss,
420 sizeof(struct swapped_sel));
422 ss.u.fields.index = fpu->environ.ds.index;
423 ss.u.fields.ti = fpu->environ.ds.ti;
424 ss.u.fields.rpl = fpu->environ.ds.rpl;
425 ss.u.half = NXSwapShort(ss.u.half);
426 memcpy(&(fpu->environ.cs), &ss,
427 sizeof(struct swapped_sel));
429 for(i = 0; i < 8; i++){
430 sfpd.mant = NXSwapShort(fpu->stack.ST[i].mant);
431 sfpd.mant1 = NXSwapShort(fpu->stack.ST[i].mant1);
432 sfpd.mant2 = NXSwapShort(fpu->stack.ST[i].mant2);
433 sfpd.mant3 = NXSwapShort(fpu->stack.ST[i].mant3);
434 sfpd.u.fields.exp = fpu->stack.ST[i].exp;
435 sfpd.u.fields.sign = fpu->stack.ST[i].sign;
436 sfpd.u.half = NXSwapShort(sfpd.u.half);
437 memcpy(&(fpu->stack.ST[i]), &sfpd,
438 sizeof(struct swapped_fp_data_reg));
443 void
444 swap_i386_thread_exceptstate(
445 i386_thread_exceptstate_t *exc,
446 enum NXByteOrder target_byte_sex)
448 struct swapped_err_code {
449 union {
450 struct err_code_normal {
451 unsigned int :16,
452 index :13,
453 tbl :2,
454 ext :1;
455 } normal;
456 struct err_code_pgfault {
457 unsigned int :29,
458 user :1,
459 wrtflt :1,
460 prot :1;
461 } pgfault;
462 unsigned long word;
463 } u;
464 } sec;
465 unsigned long word;
466 enum NXByteOrder host_byte_sex;
468 host_byte_sex = NXHostByteOrder();
470 exc->trapno = NXSwapLong(exc->trapno);
471 if(exc->trapno == 14){
472 if(target_byte_sex == host_byte_sex){
473 memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code));
474 sec.u.word = NXSwapLong(sec.u.word);
475 exc->err.pgfault.user = sec.u.pgfault.user;
476 exc->err.pgfault.wrtflt = sec.u.pgfault.wrtflt;
477 exc->err.pgfault.prot = sec.u.pgfault.prot;
479 else{
480 sec.u.pgfault.prot = exc->err.pgfault.prot;
481 sec.u.pgfault.wrtflt = exc->err.pgfault.wrtflt;
482 sec.u.pgfault.user = exc->err.pgfault.user;
483 sec.u.word = NXSwapLong(sec.u.word);
484 memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code));
487 else{
488 if(target_byte_sex == host_byte_sex){
489 memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code));
490 sec.u.word = NXSwapLong(sec.u.word);
491 word = sec.u.normal.index;
492 exc->err.normal.index = NXSwapLong(word);
493 exc->err.normal.tbl = sec.u.normal.tbl;
494 exc->err.normal.ext = sec.u.normal.ext;
496 else{
497 sec.u.normal.ext = exc->err.normal.ext;
498 sec.u.normal.tbl = exc->err.normal.tbl;
499 word = exc->err.normal.index;
500 sec.u.normal.index = NXSwapLong(word);
501 sec.u.word = NXSwapLong(sec.u.word);
502 memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code));
507 void
508 swap_i386_thread_cthreadstate(
509 i386_thread_cthreadstate_t *user,
510 enum NXByteOrder target_byte_sex)
512 user->self = NXSwapLong(user->self);
514 #endif /* i386_THREAD_STATE == -1 */
515 #endif /* !defined(RLD) */