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 #include <mach/arm/thread_status.h>
54 #include <mach-o/nlist.h>
55 #include <mach-o/reloc.h>
56 #include <mach-o/ranlib.h>
57 #include "stuff/bool.h"
62 LITTLE_ENDIAN_BYTE_SEX
65 #define SWAP_SHORT(a) ( ((a & 0xff) << 8) | ((unsigned short)(a) >> 8) )
67 #define SWAP_INT(a) ( ((a) << 24) | \
68 (((a) << 8) & 0x00ff0000) | \
69 (((a) >> 8) & 0x0000ff00) | \
70 ((unsigned int)(a) >> 24) )
73 #define SWAP_LONG(a) ( ((a) << 24) | \
74 (((a) << 8) & 0x00ff0000) | \
75 (((a) >> 8) & 0x0000ff00) | \
76 ((unsigned long)(a) >> 24) )
79 __private_extern__
long long SWAP_LONG_LONG(
82 __private_extern__
float SWAP_FLOAT(
85 __private_extern__
double SWAP_DOUBLE(
88 __private_extern__
enum byte_sex
get_host_byte_sex(
91 __private_extern__
void swap_fat_header(
92 struct fat_header
*fat_header
,
93 enum byte_sex target_byte_sex
);
95 __private_extern__
void swap_fat_arch(
96 struct fat_arch
*fat_archs
,
98 enum byte_sex target_byte_sex
);
100 __private_extern__
void swap_mach_header(
101 struct mach_header
*mh
,
102 enum byte_sex target_byte_sex
);
104 __private_extern__
void swap_mach_header_64(
105 struct mach_header_64
*mh
,
106 enum byte_sex target_byte_sex
);
108 __private_extern__
void swap_load_command(
109 struct load_command
*lc
,
110 enum byte_sex target_byte_sex
);
112 __private_extern__
void swap_segment_command(
113 struct segment_command
*sg
,
114 enum byte_sex target_byte_sex
);
116 __private_extern__
void swap_segment_command_64(
117 struct segment_command_64
*sg
,
118 enum byte_sex target_byte_sex
);
120 __private_extern__
void swap_section(
123 enum byte_sex target_byte_sex
);
125 __private_extern__
void swap_section_64(
126 struct section_64
*s
,
128 enum byte_sex target_byte_sex
);
130 __private_extern__
void swap_symtab_command(
131 struct symtab_command
*st
,
132 enum byte_sex target_byte_sex
);
134 __private_extern__
void swap_dysymtab_command(
135 struct dysymtab_command
*dyst
,
136 enum byte_sex target_byte_sex
);
138 __private_extern__
void swap_symseg_command(
139 struct symseg_command
*ss
,
140 enum byte_sex target_byte_sex
);
142 __private_extern__
void swap_fvmlib_command(
143 struct fvmlib_command
*fl
,
144 enum byte_sex target_byte_sex
);
146 __private_extern__
void swap_dylib_command(
147 struct dylib_command
*dl
,
148 enum byte_sex target_byte_sex
);
150 __private_extern__
void swap_sub_framework_command(
151 struct sub_framework_command
*sub
,
152 enum byte_sex target_byte_sex
);
154 __private_extern__
void swap_sub_umbrella_command(
155 struct sub_umbrella_command
*usub
,
156 enum byte_sex target_byte_sex
);
158 __private_extern__
void swap_sub_library_command(
159 struct sub_library_command
*lsub
,
160 enum byte_sex target_byte_sex
);
162 __private_extern__
void swap_sub_client_command(
163 struct sub_client_command
*csub
,
164 enum byte_sex target_byte_sex
);
166 __private_extern__
void swap_prebound_dylib_command(
167 struct prebound_dylib_command
*pbdylib
,
168 enum byte_sex target_byte_sex
);
170 __private_extern__
void swap_dylinker_command(
171 struct dylinker_command
*dyld
,
172 enum byte_sex target_byte_sex
);
174 __private_extern__
void swap_fvmfile_command(
175 struct fvmfile_command
*ff
,
176 enum byte_sex target_byte_sex
);
178 __private_extern__
void swap_thread_command(
179 struct thread_command
*ut
,
180 enum byte_sex target_byte_sex
);
182 __private_extern__
void swap_m68k_thread_state_regs(
183 struct m68k_thread_state_regs
*cpu
,
184 enum byte_sex target_byte_sex
);
186 __private_extern__
void swap_m68k_thread_state_68882(
187 struct m68k_thread_state_68882
*fpu
,
188 enum byte_sex target_byte_sex
);
190 __private_extern__
void swap_m68k_thread_state_user_reg(
191 struct m68k_thread_state_user_reg
*user_reg
,
192 enum byte_sex target_byte_sex
);
194 __private_extern__
void swap_ppc_thread_state_t(
195 ppc_thread_state_t
*cpu
,
196 enum byte_sex target_byte_sex
);
198 __private_extern__
void swap_ppc_thread_state64_t(
199 ppc_thread_state64_t
*cpu
,
200 enum byte_sex target_byte_sex
);
202 __private_extern__
void swap_ppc_float_state_t(
203 ppc_float_state_t
*fpu
,
204 enum byte_sex target_byte_sex
);
206 __private_extern__
void swap_ppc_exception_state_t(
207 ppc_exception_state_t
*state
,
208 enum byte_sex target_byte_sex
);
210 __private_extern__
void swap_m88k_thread_state_grf_t(
211 m88k_thread_state_grf_t
*cpu
,
212 enum byte_sex target_byte_sex
);
214 __private_extern__
void swap_m88k_thread_state_xrf_t(
215 m88k_thread_state_xrf_t
*fpu
,
216 enum byte_sex target_byte_sex
);
218 __private_extern__
void swap_m88k_thread_state_user_t(
219 m88k_thread_state_user_t
*user
,
220 enum byte_sex target_byte_sex
);
222 __private_extern__
void swap_m88110_thread_state_impl_t(
223 m88110_thread_state_impl_t
*spu
,
224 enum byte_sex target_byte_sex
);
226 __private_extern__
void swap_i860_thread_state_regs(
227 struct i860_thread_state_regs
*cpu
,
228 enum byte_sex target_byte_sex
);
230 __private_extern__
void swap_i386_thread_state(
231 i386_thread_state_t
*cpu
,
232 enum byte_sex target_byte_sex
);
234 /* current i386 thread states */
235 #if i386_THREAD_STATE == 1
236 __private_extern__
void swap_i386_float_state(
237 struct __darwin_i386_float_state
*fpu
,
238 enum byte_sex target_byte_sex
);
240 __private_extern__
void swap_i386_exception_state(
241 i386_exception_state_t
*exc
,
242 enum byte_sex target_byte_sex
);
243 #endif /* i386_THREAD_STATE == 1 */
245 /* i386 thread states on older releases */
246 #if i386_THREAD_STATE == -1
247 __private_extern__
void swap_i386_thread_fpstate(
248 i386_thread_fpstate_t
*fpu
,
249 enum byte_sex target_byte_sex
);
251 __private_extern__
void swap_i386_thread_exceptstate(
252 i386_thread_exceptstate_t
*exc
,
253 enum byte_sex target_byte_sex
);
255 __private_extern__
void swap_i386_thread_cthreadstate(
256 i386_thread_cthreadstate_t
*user
,
257 enum byte_sex target_byte_sex
);
258 #endif /* i386_THREAD_STATE == -1 */
260 #ifdef x86_THREAD_STATE64
261 __private_extern__
void swap_x86_thread_state64(
262 x86_thread_state64_t
*cpu
,
263 enum byte_sex target_byte_sex
);
265 __private_extern__
void swap_x86_float_state64(
266 x86_float_state64_t
*fpu
,
267 enum byte_sex target_byte_sex
);
269 __private_extern__
void swap_x86_state_hdr(
270 struct x86_state_hdr
*hdr
,
271 enum byte_sex target_byte_sex
);
273 __private_extern__
void swap_x86_exception_state64(
274 x86_exception_state64_t
*exc
,
275 enum byte_sex target_byte_sex
);
277 __private_extern__
void swap_x86_debug_state32(
278 x86_debug_state32_t
*debug
,
279 enum byte_sex target_byte_sex
);
281 __private_extern__
void swap_x86_debug_state64(
282 x86_debug_state64_t
*debug
,
283 enum byte_sex target_byte_sex
);
284 #endif /* x86_THREAD_STATE64 */
286 __private_extern__
void swap_hppa_integer_thread_state(
287 struct hp_pa_integer_thread_state
*regs
,
288 enum byte_sex target_byte_order
);
290 __private_extern__
void swap_hppa_frame_thread_state(
291 struct hp_pa_frame_thread_state
*frame
,
292 enum byte_sex target_byte_order
);
294 __private_extern__
void swap_hppa_fp_thread_state(
295 struct hp_pa_fp_thread_state
*fp
,
296 enum byte_sex target_byte_order
);
298 __private_extern__
void swap_sparc_thread_state_regs(
299 struct sparc_thread_state_regs
*cpu
,
300 enum byte_sex target_byte_order
);
302 __private_extern__
void swap_sparc_thread_state_fpu(
303 struct sparc_thread_state_fpu
*fpu
,
304 enum byte_sex target_byte_order
);
306 __private_extern__
void swap_arm_thread_state_t(
307 arm_thread_state_t
*cpu
,
308 enum byte_sex target_byte_sex
);
310 __private_extern__
void swap_ident_command(
311 struct ident_command
*id_cmd
,
312 enum byte_sex target_byte_sex
);
314 __private_extern__
void swap_routines_command(
315 struct routines_command
*r_cmd
,
316 enum byte_sex target_byte_sex
);
318 __private_extern__
void swap_routines_command_64(
319 struct routines_command_64
*r_cmd
,
320 enum byte_sex target_byte_sex
);
322 __private_extern__
void swap_twolevel_hints_command(
323 struct twolevel_hints_command
*hints_cmd
,
324 enum byte_sex target_byte_sex
);
326 __private_extern__
void swap_prebind_cksum_command(
327 struct prebind_cksum_command
*cksum_cmd
,
328 enum byte_sex target_byte_sex
);
330 __private_extern__
void swap_uuid_command(
331 struct uuid_command
*uuid_cmd
,
332 enum byte_sex target_byte_sex
);
334 __private_extern__
void swap_linkedit_data_command(
335 struct linkedit_data_command
*ld
,
336 enum byte_sex target_byte_sex
);
338 __private_extern__
void swap_version_min_command(
339 struct version_min_command
*ver_cmd
,
340 enum byte_sex target_byte_sex
);
342 __private_extern__
void swap_rpath_command(
343 struct rpath_command
*rpath_cmd
,
344 enum byte_sex target_byte_sex
);
346 __private_extern__
void swap_encryption_command(
347 struct encryption_info_command
*ec
,
348 enum byte_sex target_byte_sex
);
350 __private_extern__
void swap_dyld_info_command(
351 struct dyld_info_command
*dc
,
352 enum byte_sex target_byte_sex
);
354 __private_extern__
void swap_entry_point_command(
355 struct entry_point_command
*ep
,
356 enum byte_sex target_byte_sex
);
358 __private_extern__
void swap_source_version_command(
359 struct source_version_command
*sv
,
360 enum byte_sex target_byte_sex
);
362 __private_extern__
void swap_nlist(
363 struct nlist
*symbols
,
365 enum byte_sex target_byte_sex
);
367 __private_extern__
void swap_nlist_64(
368 struct nlist_64
*symbols
,
370 enum byte_sex target_byte_sex
);
372 __private_extern__
void swap_ranlib(
373 struct ranlib
*ranlibs
,
375 enum byte_sex target_byte_sex
);
377 __private_extern__
void swap_relocation_info(
378 struct relocation_info
*relocs
,
380 enum byte_sex target_byte_sex
);
382 __private_extern__
void swap_indirect_symbols(
383 uint32_t *indirect_symbols
,
384 uint32_t nindirect_symbols
,
385 enum byte_sex target_byte_sex
);
387 __private_extern__
void swap_dylib_reference(
388 struct dylib_reference
*refs
,
390 enum byte_sex target_byte_sex
);
392 __private_extern__
void swap_dylib_module(
393 struct dylib_module
*mods
,
395 enum byte_sex target_byte_sex
);
397 __private_extern__
void swap_dylib_module_64(
398 struct dylib_module_64
*mods
,
400 enum byte_sex target_byte_sex
);
402 __private_extern__
void swap_dylib_table_of_contents(
403 struct dylib_table_of_contents
*tocs
,
405 enum byte_sex target_byte_sex
);
407 __private_extern__
void swap_twolevel_hint(
408 struct twolevel_hint
*hints
,
410 enum byte_sex target_byte_sex
);
413 * swap_object_headers() swaps the object file headers from the host byte sex
414 * into the non-host byte sex. It returns TRUE if it can and did swap the
415 * headers else returns FALSE and does not touch the headers and prints an error
416 * using the error() routine.
418 __private_extern__
enum bool swap_object_headers(
419 void *mach_header
, /* either a mach_header or a mach_header_64 */
420 struct load_command
*load_commands
);
423 * get_toc_byte_sex() guesses the byte sex of the table of contents of the
424 * library mapped in at the address, addr, of size, size based on the first
425 * object file's bytesex. If it can't figure it out, because the library has
426 * no object file members or is malformed it will return UNKNOWN_BYTE_SEX.
428 __private_extern__
enum byte_sex
get_toc_byte_sex(
432 #endif /* _STUFF_BYTESEX_H_ */