2 * Copyright (c) 2004, Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
13 * its contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #ifndef _STUFF_BYTESEX_H_
30 #define _STUFF_BYTESEX_H_
32 #if defined(__MWERKS__) && !defined(__private_extern__)
33 #define __private_extern__ __declspec(private_extern)
36 #include <mach-o/fat.h>
37 #include <mach-o/loader.h>
38 #include <mach/m68k/thread_status.h>
39 #undef MACHINE_THREAD_STATE /* need to undef these to avoid warnings */
40 #undef MACHINE_THREAD_STATE_COUNT
41 #undef THREAD_STATE_NONE
42 #undef VALID_THREAD_STATE_FLAVOR
43 #include <mach/ppc/thread_status.h>
44 #undef MACHINE_THREAD_STATE /* need to undef these to avoid warnings */
45 #undef MACHINE_THREAD_STATE_COUNT
46 #undef THREAD_STATE_NONE
47 #undef VALID_THREAD_STATE_FLAVOR
48 #include <mach/m88k/thread_status.h>
49 #include <mach/i860/thread_status.h>
50 #include <mach/i386/thread_status.h>
51 #include <mach/hppa/thread_status.h>
52 #include <mach/sparc/thread_status.h>
53 #undef MACHINE_THREAD_STATE /* need to undef these to avoid warnings */
54 #undef MACHINE_THREAD_STATE_COUNT
55 #undef THREAD_STATE_NONE
56 #undef VALID_THREAD_STATE_FLAVOR
57 #include <mach/arm/thread_status.h>
58 #include <mach-o/nlist.h>
59 #include <mach-o/reloc.h>
60 #include <mach-o/ranlib.h>
61 #include "stuff/bool.h"
66 LITTLE_ENDIAN_BYTE_SEX
69 #define SWAP_SHORT(a) ( ((a & 0xff) << 8) | ((unsigned short)(a) >> 8) )
71 #define SWAP_INT(a) ( ((a) << 24) | \
72 (((a) << 8) & 0x00ff0000) | \
73 (((a) >> 8) & 0x0000ff00) | \
74 ((unsigned int)(a) >> 24) )
77 #define SWAP_LONG(a) ( ((a) << 24) | \
78 (((a) << 8) & 0x00ff0000) | \
79 (((a) >> 8) & 0x0000ff00) | \
80 ((unsigned long)(a) >> 24) )
83 __private_extern__
long long SWAP_LONG_LONG(
86 __private_extern__
float SWAP_FLOAT(
89 __private_extern__
double SWAP_DOUBLE(
92 __private_extern__
enum byte_sex
get_host_byte_sex(
95 __private_extern__
void swap_fat_header(
96 struct fat_header
*fat_header
,
97 enum byte_sex target_byte_sex
);
99 __private_extern__
void swap_fat_arch(
100 struct fat_arch
*fat_archs
,
102 enum byte_sex target_byte_sex
);
104 __private_extern__
void swap_mach_header(
105 struct mach_header
*mh
,
106 enum byte_sex target_byte_sex
);
108 __private_extern__
void swap_mach_header_64(
109 struct mach_header_64
*mh
,
110 enum byte_sex target_byte_sex
);
112 __private_extern__
void swap_load_command(
113 struct load_command
*lc
,
114 enum byte_sex target_byte_sex
);
116 __private_extern__
void swap_segment_command(
117 struct segment_command
*sg
,
118 enum byte_sex target_byte_sex
);
120 __private_extern__
void swap_segment_command_64(
121 struct segment_command_64
*sg
,
122 enum byte_sex target_byte_sex
);
124 __private_extern__
void swap_section(
127 enum byte_sex target_byte_sex
);
129 __private_extern__
void swap_section_64(
130 struct section_64
*s
,
132 enum byte_sex target_byte_sex
);
134 __private_extern__
void swap_symtab_command(
135 struct symtab_command
*st
,
136 enum byte_sex target_byte_sex
);
138 __private_extern__
void swap_dysymtab_command(
139 struct dysymtab_command
*dyst
,
140 enum byte_sex target_byte_sex
);
142 __private_extern__
void swap_symseg_command(
143 struct symseg_command
*ss
,
144 enum byte_sex target_byte_sex
);
146 __private_extern__
void swap_fvmlib_command(
147 struct fvmlib_command
*fl
,
148 enum byte_sex target_byte_sex
);
150 __private_extern__
void swap_dylib_command(
151 struct dylib_command
*dl
,
152 enum byte_sex target_byte_sex
);
154 __private_extern__
void swap_sub_framework_command(
155 struct sub_framework_command
*sub
,
156 enum byte_sex target_byte_sex
);
158 __private_extern__
void swap_sub_umbrella_command(
159 struct sub_umbrella_command
*usub
,
160 enum byte_sex target_byte_sex
);
162 __private_extern__
void swap_sub_library_command(
163 struct sub_library_command
*lsub
,
164 enum byte_sex target_byte_sex
);
166 __private_extern__
void swap_sub_client_command(
167 struct sub_client_command
*csub
,
168 enum byte_sex target_byte_sex
);
170 __private_extern__
void swap_prebound_dylib_command(
171 struct prebound_dylib_command
*pbdylib
,
172 enum byte_sex target_byte_sex
);
174 __private_extern__
void swap_dylinker_command(
175 struct dylinker_command
*dyld
,
176 enum byte_sex target_byte_sex
);
178 __private_extern__
void swap_fvmfile_command(
179 struct fvmfile_command
*ff
,
180 enum byte_sex target_byte_sex
);
182 __private_extern__
void swap_thread_command(
183 struct thread_command
*ut
,
184 enum byte_sex target_byte_sex
);
186 __private_extern__
void swap_m68k_thread_state_regs(
187 struct m68k_thread_state_regs
*cpu
,
188 enum byte_sex target_byte_sex
);
190 __private_extern__
void swap_m68k_thread_state_68882(
191 struct m68k_thread_state_68882
*fpu
,
192 enum byte_sex target_byte_sex
);
194 __private_extern__
void swap_m68k_thread_state_user_reg(
195 struct m68k_thread_state_user_reg
*user_reg
,
196 enum byte_sex target_byte_sex
);
198 __private_extern__
void swap_ppc_thread_state_t(
199 ppc_thread_state_t
*cpu
,
200 enum byte_sex target_byte_sex
);
202 __private_extern__
void swap_ppc_thread_state64_t(
203 ppc_thread_state64_t
*cpu
,
204 enum byte_sex target_byte_sex
);
206 __private_extern__
void swap_ppc_float_state_t(
207 ppc_float_state_t
*fpu
,
208 enum byte_sex target_byte_sex
);
210 __private_extern__
void swap_ppc_exception_state_t(
211 ppc_exception_state_t
*state
,
212 enum byte_sex target_byte_sex
);
214 __private_extern__
void swap_m88k_thread_state_grf_t(
215 m88k_thread_state_grf_t
*cpu
,
216 enum byte_sex target_byte_sex
);
218 __private_extern__
void swap_m88k_thread_state_xrf_t(
219 m88k_thread_state_xrf_t
*fpu
,
220 enum byte_sex target_byte_sex
);
222 __private_extern__
void swap_m88k_thread_state_user_t(
223 m88k_thread_state_user_t
*user
,
224 enum byte_sex target_byte_sex
);
226 __private_extern__
void swap_m88110_thread_state_impl_t(
227 m88110_thread_state_impl_t
*spu
,
228 enum byte_sex target_byte_sex
);
230 __private_extern__
void swap_i860_thread_state_regs(
231 struct i860_thread_state_regs
*cpu
,
232 enum byte_sex target_byte_sex
);
234 __private_extern__
void swap_i386_thread_state(
235 i386_thread_state_t
*cpu
,
236 enum byte_sex target_byte_sex
);
238 /* current i386 thread states */
239 #if i386_THREAD_STATE == 1
240 __private_extern__
void swap_i386_float_state(
241 struct __darwin_i386_float_state
*fpu
,
242 enum byte_sex target_byte_sex
);
244 __private_extern__
void swap_i386_exception_state(
245 i386_exception_state_t
*exc
,
246 enum byte_sex target_byte_sex
);
247 #endif /* i386_THREAD_STATE == 1 */
249 /* i386 thread states on older releases */
250 #if i386_THREAD_STATE == -1
251 __private_extern__
void swap_i386_thread_fpstate(
252 i386_thread_fpstate_t
*fpu
,
253 enum byte_sex target_byte_sex
);
255 __private_extern__
void swap_i386_thread_exceptstate(
256 i386_thread_exceptstate_t
*exc
,
257 enum byte_sex target_byte_sex
);
259 __private_extern__
void swap_i386_thread_cthreadstate(
260 i386_thread_cthreadstate_t
*user
,
261 enum byte_sex target_byte_sex
);
262 #endif /* i386_THREAD_STATE == -1 */
264 #ifdef x86_THREAD_STATE64
265 __private_extern__
void swap_x86_thread_state64(
266 x86_thread_state64_t
*cpu
,
267 enum byte_sex target_byte_sex
);
269 __private_extern__
void swap_x86_float_state64(
270 x86_float_state64_t
*fpu
,
271 enum byte_sex target_byte_sex
);
273 __private_extern__
void swap_x86_state_hdr(
274 struct x86_state_hdr
*hdr
,
275 enum byte_sex target_byte_sex
);
277 __private_extern__
void swap_x86_exception_state64(
278 x86_exception_state64_t
*exc
,
279 enum byte_sex target_byte_sex
);
281 __private_extern__
void swap_x86_debug_state32(
282 x86_debug_state32_t
*debug
,
283 enum byte_sex target_byte_sex
);
285 __private_extern__
void swap_x86_debug_state64(
286 x86_debug_state64_t
*debug
,
287 enum byte_sex target_byte_sex
);
288 #endif /* x86_THREAD_STATE64 */
290 __private_extern__
void swap_hppa_integer_thread_state(
291 struct hp_pa_integer_thread_state
*regs
,
292 enum byte_sex target_byte_order
);
294 __private_extern__
void swap_hppa_frame_thread_state(
295 struct hp_pa_frame_thread_state
*frame
,
296 enum byte_sex target_byte_order
);
298 __private_extern__
void swap_hppa_fp_thread_state(
299 struct hp_pa_fp_thread_state
*fp
,
300 enum byte_sex target_byte_order
);
302 __private_extern__
void swap_sparc_thread_state_regs(
303 struct sparc_thread_state_regs
*cpu
,
304 enum byte_sex target_byte_order
);
306 __private_extern__
void swap_sparc_thread_state_fpu(
307 struct sparc_thread_state_fpu
*fpu
,
308 enum byte_sex target_byte_order
);
310 __private_extern__
void swap_arm_thread_state_t(
311 arm_thread_state_t
*cpu
,
312 enum byte_sex target_byte_sex
);
314 __private_extern__
void swap_ident_command(
315 struct ident_command
*id_cmd
,
316 enum byte_sex target_byte_sex
);
318 __private_extern__
void swap_routines_command(
319 struct routines_command
*r_cmd
,
320 enum byte_sex target_byte_sex
);
322 __private_extern__
void swap_routines_command_64(
323 struct routines_command_64
*r_cmd
,
324 enum byte_sex target_byte_sex
);
326 __private_extern__
void swap_twolevel_hints_command(
327 struct twolevel_hints_command
*hints_cmd
,
328 enum byte_sex target_byte_sex
);
330 __private_extern__
void swap_prebind_cksum_command(
331 struct prebind_cksum_command
*cksum_cmd
,
332 enum byte_sex target_byte_sex
);
334 __private_extern__
void swap_uuid_command(
335 struct uuid_command
*uuid_cmd
,
336 enum byte_sex target_byte_sex
);
338 __private_extern__
void swap_linkedit_data_command(
339 struct linkedit_data_command
*ld
,
340 enum byte_sex target_byte_sex
);
342 __private_extern__
void swap_version_min_command(
343 struct version_min_command
*ver_cmd
,
344 enum byte_sex target_byte_sex
);
346 __private_extern__
void swap_rpath_command(
347 struct rpath_command
*rpath_cmd
,
348 enum byte_sex target_byte_sex
);
350 __private_extern__
void swap_encryption_command(
351 struct encryption_info_command
*ec
,
352 enum byte_sex target_byte_sex
);
354 __private_extern__
void swap_dyld_info_command(
355 struct dyld_info_command
*dc
,
356 enum byte_sex target_byte_sex
);
358 __private_extern__
void swap_entry_point_command(
359 struct entry_point_command
*ep
,
360 enum byte_sex target_byte_sex
);
362 __private_extern__
void swap_source_version_command(
363 struct source_version_command
*sv
,
364 enum byte_sex target_byte_sex
);
366 __private_extern__
void swap_nlist(
367 struct nlist
*symbols
,
369 enum byte_sex target_byte_sex
);
371 __private_extern__
void swap_nlist_64(
372 struct nlist_64
*symbols
,
374 enum byte_sex target_byte_sex
);
376 __private_extern__
void swap_ranlib(
377 struct ranlib
*ranlibs
,
379 enum byte_sex target_byte_sex
);
381 __private_extern__
void swap_relocation_info(
382 struct relocation_info
*relocs
,
384 enum byte_sex target_byte_sex
);
386 __private_extern__
void swap_indirect_symbols(
387 uint32_t *indirect_symbols
,
388 uint32_t nindirect_symbols
,
389 enum byte_sex target_byte_sex
);
391 __private_extern__
void swap_dylib_reference(
392 struct dylib_reference
*refs
,
394 enum byte_sex target_byte_sex
);
396 __private_extern__
void swap_dylib_module(
397 struct dylib_module
*mods
,
399 enum byte_sex target_byte_sex
);
401 __private_extern__
void swap_dylib_module_64(
402 struct dylib_module_64
*mods
,
404 enum byte_sex target_byte_sex
);
406 __private_extern__
void swap_dylib_table_of_contents(
407 struct dylib_table_of_contents
*tocs
,
409 enum byte_sex target_byte_sex
);
411 __private_extern__
void swap_twolevel_hint(
412 struct twolevel_hint
*hints
,
414 enum byte_sex target_byte_sex
);
417 * swap_object_headers() swaps the object file headers from the host byte sex
418 * into the non-host byte sex. It returns TRUE if it can and did swap the
419 * headers else returns FALSE and does not touch the headers and prints an error
420 * using the error() routine.
422 __private_extern__
enum bool swap_object_headers(
423 void *mach_header
, /* either a mach_header or a mach_header_64 */
424 struct load_command
*load_commands
);
427 * get_toc_byte_sex() guesses the byte sex of the table of contents of the
428 * library mapped in at the address, addr, of size, size based on the first
429 * object file's bytesex. If it can't figure it out, because the library has
430 * no object file members or is malformed it will return UNKNOWN_BYTE_SEX.
432 __private_extern__
enum byte_sex
get_toc_byte_sex(
436 #endif /* _STUFF_BYTESEX_H_ */