2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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@
25 #include <mach-o/swap.h>
30 struct fat_header
*fat_header
,
31 enum NXByteOrder target_byte_sex
)
33 fat_header
->magic
= NXSwapLong(fat_header
->magic
);
34 fat_header
->nfat_arch
= NXSwapLong(fat_header
->nfat_arch
);
39 struct fat_arch
*fat_archs
,
41 enum NXByteOrder target_byte_sex
)
45 for(i
= 0; i
< nfat_arch
; i
++){
46 fat_archs
[i
].cputype
= NXSwapLong(fat_archs
[i
].cputype
);
47 fat_archs
[i
].cpusubtype
= NXSwapLong(fat_archs
[i
].cpusubtype
);
48 fat_archs
[i
].offset
= NXSwapLong(fat_archs
[i
].offset
);
49 fat_archs
[i
].size
= NXSwapLong(fat_archs
[i
].size
);
50 fat_archs
[i
].align
= NXSwapLong(fat_archs
[i
].align
);
56 struct mach_header
*mh
,
57 enum NXByteOrder target_byte_sex
)
59 mh
->magic
= NXSwapLong(mh
->magic
);
60 mh
->cputype
= NXSwapLong(mh
->cputype
);
61 mh
->cpusubtype
= NXSwapLong(mh
->cpusubtype
);
62 mh
->filetype
= NXSwapLong(mh
->filetype
);
63 mh
->ncmds
= NXSwapLong(mh
->ncmds
);
64 mh
->sizeofcmds
= NXSwapLong(mh
->sizeofcmds
);
65 mh
->flags
= NXSwapLong(mh
->flags
);
70 struct load_command
*lc
,
71 enum NXByteOrder target_byte_sex
)
73 lc
->cmd
= NXSwapLong(lc
->cmd
);
74 lc
->cmdsize
= NXSwapLong(lc
->cmdsize
);
79 struct segment_command
*sg
,
80 enum NXByteOrder target_byte_sex
)
83 sg
->cmd
= NXSwapLong(sg
->cmd
);
84 sg
->cmdsize
= NXSwapLong(sg
->cmdsize
);
85 sg
->vmaddr
= NXSwapLong(sg
->vmaddr
);
86 sg
->vmsize
= NXSwapLong(sg
->vmsize
);
87 sg
->fileoff
= NXSwapLong(sg
->fileoff
);
88 sg
->filesize
= NXSwapLong(sg
->filesize
);
89 sg
->maxprot
= NXSwapLong(sg
->maxprot
);
90 sg
->initprot
= NXSwapLong(sg
->initprot
);
91 sg
->nsects
= NXSwapLong(sg
->nsects
);
92 sg
->flags
= NXSwapLong(sg
->flags
);
99 enum NXByteOrder target_byte_sex
)
103 for(i
= 0; i
< nsects
; i
++){
106 s
[i
].addr
= NXSwapLong(s
[i
].addr
);
107 s
[i
].size
= NXSwapLong(s
[i
].size
);
108 s
[i
].offset
= NXSwapLong(s
[i
].offset
);
109 s
[i
].align
= NXSwapLong(s
[i
].align
);
110 s
[i
].reloff
= NXSwapLong(s
[i
].reloff
);
111 s
[i
].nreloc
= NXSwapLong(s
[i
].nreloc
);
112 s
[i
].flags
= NXSwapLong(s
[i
].flags
);
113 s
[i
].reserved1
= NXSwapLong(s
[i
].reserved1
);
114 s
[i
].reserved2
= NXSwapLong(s
[i
].reserved2
);
120 struct symtab_command
*st
,
121 enum NXByteOrder target_byte_sex
)
123 st
->cmd
= NXSwapLong(st
->cmd
);
124 st
->cmdsize
= NXSwapLong(st
->cmdsize
);
125 st
->symoff
= NXSwapLong(st
->symoff
);
126 st
->nsyms
= NXSwapLong(st
->nsyms
);
127 st
->stroff
= NXSwapLong(st
->stroff
);
128 st
->strsize
= NXSwapLong(st
->strsize
);
132 swap_dysymtab_command(
133 struct dysymtab_command
*dyst
,
134 enum NXByteOrder target_byte_sex
)
136 dyst
->cmd
= NXSwapLong(dyst
->cmd
);
137 dyst
->cmdsize
= NXSwapLong(dyst
->cmdsize
);
138 dyst
->ilocalsym
= NXSwapLong(dyst
->ilocalsym
);
139 dyst
->nlocalsym
= NXSwapLong(dyst
->nlocalsym
);
140 dyst
->iextdefsym
= NXSwapLong(dyst
->iextdefsym
);
141 dyst
->nextdefsym
= NXSwapLong(dyst
->nextdefsym
);
142 dyst
->iundefsym
= NXSwapLong(dyst
->iundefsym
);
143 dyst
->nundefsym
= NXSwapLong(dyst
->nundefsym
);
144 dyst
->tocoff
= NXSwapLong(dyst
->tocoff
);
145 dyst
->ntoc
= NXSwapLong(dyst
->ntoc
);
146 dyst
->modtaboff
= NXSwapLong(dyst
->modtaboff
);
147 dyst
->nmodtab
= NXSwapLong(dyst
->nmodtab
);
148 dyst
->extrefsymoff
= NXSwapLong(dyst
->extrefsymoff
);
149 dyst
->nextrefsyms
= NXSwapLong(dyst
->nextrefsyms
);
150 dyst
->indirectsymoff
= NXSwapLong(dyst
->indirectsymoff
);
151 dyst
->nindirectsyms
= NXSwapLong(dyst
->nindirectsyms
);
152 dyst
->extreloff
= NXSwapLong(dyst
->extreloff
);
153 dyst
->nextrel
= NXSwapLong(dyst
->nextrel
);
154 dyst
->locreloff
= NXSwapLong(dyst
->locreloff
);
155 dyst
->nlocrel
= NXSwapLong(dyst
->nlocrel
);
160 struct symseg_command
*ss
,
161 enum NXByteOrder target_byte_sex
)
163 ss
->cmd
= NXSwapLong(ss
->cmd
);
164 ss
->cmdsize
= NXSwapLong(ss
->cmdsize
);
165 ss
->offset
= NXSwapLong(ss
->offset
);
166 ss
->size
= NXSwapLong(ss
->size
);
171 struct fvmlib_command
*fl
,
172 enum NXByteOrder target_byte_sex
)
174 fl
->cmd
= NXSwapLong(fl
->cmd
);
175 fl
->cmdsize
= NXSwapLong(fl
->cmdsize
);
176 fl
->fvmlib
.name
.offset
= NXSwapLong(fl
->fvmlib
.name
.offset
);
177 fl
->fvmlib
.minor_version
= NXSwapLong(fl
->fvmlib
.minor_version
);
178 fl
->fvmlib
.header_addr
= NXSwapLong(fl
->fvmlib
.header_addr
);
183 struct dylib_command
*dl
,
184 enum NXByteOrder target_byte_sex
)
186 dl
->cmd
= NXSwapLong(dl
->cmd
);
187 dl
->cmdsize
= NXSwapLong(dl
->cmdsize
);
188 dl
->dylib
.name
.offset
= NXSwapLong(dl
->dylib
.name
.offset
);
189 dl
->dylib
.timestamp
= NXSwapLong(dl
->dylib
.timestamp
);
190 dl
->dylib
.current_version
= NXSwapLong(dl
->dylib
.current_version
);
191 dl
->dylib
.compatibility_version
=
192 NXSwapLong(dl
->dylib
.compatibility_version
);
196 swap_sub_framework_command(
197 struct sub_framework_command
*sub
,
198 enum NXByteOrder target_byte_sex
)
200 sub
->cmd
= NXSwapLong(sub
->cmd
);
201 sub
->cmdsize
= NXSwapLong(sub
->cmdsize
);
202 sub
->umbrella
.offset
= NXSwapLong(sub
->umbrella
.offset
);
206 swap_sub_umbrella_command(
207 struct sub_umbrella_command
*usub
,
208 enum NXByteOrder target_byte_sex
)
210 usub
->cmd
= NXSwapLong(usub
->cmd
);
211 usub
->cmdsize
= NXSwapLong(usub
->cmdsize
);
212 usub
->sub_umbrella
.offset
= NXSwapLong(usub
->sub_umbrella
.offset
);
216 swap_sub_library_command(
217 struct sub_library_command
*lsub
,
218 enum NXByteOrder target_byte_sex
)
220 lsub
->cmd
= NXSwapLong(lsub
->cmd
);
221 lsub
->cmdsize
= NXSwapLong(lsub
->cmdsize
);
222 lsub
->sub_library
.offset
= NXSwapLong(lsub
->sub_library
.offset
);
226 swap_sub_client_command(
227 struct sub_client_command
*csub
,
228 enum NXByteOrder target_byte_sex
)
230 csub
->cmd
= NXSwapLong(csub
->cmd
);
231 csub
->cmdsize
= NXSwapLong(csub
->cmdsize
);
232 csub
->client
.offset
= NXSwapLong(csub
->client
.offset
);
237 swap_prebound_dylib_command(
238 struct prebound_dylib_command
*pbdylib
,
239 enum NXByteOrder target_byte_sex
)
241 pbdylib
->cmd
= NXSwapLong(pbdylib
->cmd
);
242 pbdylib
->cmdsize
= NXSwapLong(pbdylib
->cmdsize
);
243 pbdylib
->name
.offset
= NXSwapLong(pbdylib
->name
.offset
);
244 pbdylib
->nmodules
= NXSwapLong(pbdylib
->nmodules
);
245 pbdylib
->linked_modules
.offset
=
246 NXSwapLong(pbdylib
->linked_modules
.offset
);
250 swap_dylinker_command(
251 struct dylinker_command
*dyld
,
252 enum NXByteOrder target_byte_sex
)
254 dyld
->cmd
= NXSwapLong(dyld
->cmd
);
255 dyld
->cmdsize
= NXSwapLong(dyld
->cmdsize
);
256 dyld
->name
.offset
= NXSwapLong(dyld
->name
.offset
);
260 swap_fvmfile_command(
261 struct fvmfile_command
*ff
,
262 enum NXByteOrder target_byte_sex
)
264 ff
->cmd
= NXSwapLong(ff
->cmd
);
265 ff
->cmdsize
= NXSwapLong(ff
->cmdsize
);
266 ff
->name
.offset
= NXSwapLong(ff
->name
.offset
);
267 ff
->header_addr
= NXSwapLong(ff
->header_addr
);
273 struct thread_command
*ut
,
274 enum NXByteOrder target_byte_sex
)
276 ut
->cmd
= NXSwapLong(ut
->cmd
);
277 ut
->cmdsize
= NXSwapLong(ut
->cmdsize
);
282 struct ident_command
*id_cmd
,
283 enum NXByteOrder target_byte_sex
)
285 id_cmd
->cmd
= NXSwapLong(id_cmd
->cmd
);
286 id_cmd
->cmdsize
= NXSwapLong(id_cmd
->cmdsize
);
290 swap_routines_command(
291 struct routines_command
*r_cmd
,
292 enum NXByteOrder target_byte_sex
)
294 r_cmd
->cmd
= NXSwapLong(r_cmd
->cmd
);
295 r_cmd
->cmdsize
= NXSwapLong(r_cmd
->cmdsize
);
296 r_cmd
->init_address
= NXSwapLong(r_cmd
->init_address
);
297 r_cmd
->init_module
= NXSwapLong(r_cmd
->init_module
);
298 r_cmd
->reserved1
= NXSwapLong(r_cmd
->reserved1
);
299 r_cmd
->reserved2
= NXSwapLong(r_cmd
->reserved2
);
300 r_cmd
->reserved3
= NXSwapLong(r_cmd
->reserved3
);
301 r_cmd
->reserved4
= NXSwapLong(r_cmd
->reserved4
);
302 r_cmd
->reserved5
= NXSwapLong(r_cmd
->reserved5
);
303 r_cmd
->reserved6
= NXSwapLong(r_cmd
->reserved6
);
307 swap_prebind_cksum_command(
308 struct prebind_cksum_command
*cksum_cmd
,
309 enum NXByteOrder target_byte_sex
)
311 cksum_cmd
->cmd
= NXSwapLong(cksum_cmd
->cmd
);
312 cksum_cmd
->cmdsize
= NXSwapLong(cksum_cmd
->cmdsize
);
313 cksum_cmd
->cksum
= NXSwapLong(cksum_cmd
->cksum
);
318 struct uuid_command
*uuid_cmd
,
319 enum NXByteOrder target_byte_sex
)
321 uuid_cmd
->cmd
= NXSwapLong(uuid_cmd
->cmd
);
322 uuid_cmd
->cmdsize
= NXSwapLong(uuid_cmd
->cmdsize
);
327 struct nlist
*symbols
,
329 enum NXByteOrder target_byte_sex
)
333 for(i
= 0; i
< nsymbols
; i
++){
334 symbols
[i
].n_un
.n_strx
= NXSwapLong(symbols
[i
].n_un
.n_strx
);
337 symbols
[i
].n_desc
= NXSwapShort(symbols
[i
].n_desc
);
338 symbols
[i
].n_value
= NXSwapLong(symbols
[i
].n_value
);
344 struct ranlib
*ranlibs
,
346 enum NXByteOrder target_byte_sex
)
350 for(i
= 0; i
< nranlibs
; i
++){
351 ranlibs
[i
].ran_un
.ran_strx
= NXSwapLong(ranlibs
[i
].ran_un
.ran_strx
);
352 ranlibs
[i
].ran_off
= NXSwapLong(ranlibs
[i
].ran_off
);
357 swap_relocation_info(
358 struct relocation_info
*relocs
,
360 enum NXByteOrder target_byte_sex
)
363 enum NXByteOrder host_byte_sex
;
364 uint32_t to_host_byte_sex
, scattered
;
366 struct swapped_relocation_info
{
381 struct swapped_scattered_relocation_info
{
386 host_byte_sex
= NXHostByteOrder();
387 to_host_byte_sex
= target_byte_sex
== host_byte_sex
;
389 for(i
= 0; i
< nrelocs
; i
++){
391 scattered
= (NXSwapLong(relocs
[i
].r_address
) & R_SCATTERED
) != 0;
393 scattered
= ((relocs
[i
].r_address
) & R_SCATTERED
) != 0;
394 if(scattered
== FALSE
){
395 if(to_host_byte_sex
){
396 memcpy(&sr
, relocs
+ i
, sizeof(struct relocation_info
));
397 sr
.r_address
= NXSwapLong(sr
.r_address
);
398 sr
.u
.word
= NXSwapLong(sr
.u
.word
);
399 relocs
[i
].r_address
= sr
.r_address
;
400 relocs
[i
].r_symbolnum
= sr
.u
.fields
.r_symbolnum
;
401 relocs
[i
].r_pcrel
= sr
.u
.fields
.r_pcrel
;
402 relocs
[i
].r_length
= sr
.u
.fields
.r_length
;
403 relocs
[i
].r_extern
= sr
.u
.fields
.r_extern
;
404 relocs
[i
].r_type
= sr
.u
.fields
.r_type
;
407 sr
.r_address
= relocs
[i
].r_address
;
408 sr
.u
.fields
.r_symbolnum
= relocs
[i
].r_symbolnum
;
409 sr
.u
.fields
.r_length
= relocs
[i
].r_length
;
410 sr
.u
.fields
.r_pcrel
= relocs
[i
].r_pcrel
;
411 sr
.u
.fields
.r_extern
= relocs
[i
].r_extern
;
412 sr
.u
.fields
.r_type
= relocs
[i
].r_type
;
413 sr
.r_address
= NXSwapLong(sr
.r_address
);
414 sr
.u
.word
= NXSwapLong(sr
.u
.word
);
415 memcpy(relocs
+ i
, &sr
, sizeof(struct relocation_info
));
419 ssr
= (struct swapped_scattered_relocation_info
*)(relocs
+ i
);
420 ssr
->word
= NXSwapLong(ssr
->word
);
421 ssr
->r_value
= NXSwapLong(ssr
->r_value
);
427 swap_indirect_symbols(
428 uint32_t *indirect_symbols
,
429 uint32_t nindirect_symbols
,
430 enum NXByteOrder target_byte_sex
)
434 for(i
= 0; i
< nindirect_symbols
; i
++)
435 indirect_symbols
[i
] = NXSwapLong(indirect_symbols
[i
]);
439 swap_dylib_reference(
440 struct dylib_reference
*refs
,
442 enum NXByteOrder target_byte_sex
)
444 struct swapped_dylib_reference
{
456 enum NXByteOrder host_byte_sex
;
458 host_byte_sex
= NXHostByteOrder();
460 for(i
= 0; i
< nrefs
; i
++){
461 if(target_byte_sex
== host_byte_sex
){
462 memcpy(&sref
, refs
+ i
, sizeof(struct swapped_dylib_reference
));
463 sref
.u
.word
= NXSwapLong(sref
.u
.word
);
464 refs
[i
].flags
= sref
.u
.fields
.flags
;
465 refs
[i
].isym
= sref
.u
.fields
.isym
;
468 sref
.u
.fields
.isym
= refs
[i
].isym
;
469 sref
.u
.fields
.flags
= refs
[i
].flags
;
470 sref
.u
.word
= NXSwapLong(sref
.u
.word
);
471 memcpy(refs
+ i
, &sref
, sizeof(struct swapped_dylib_reference
));
479 struct dylib_module
*mods
,
481 enum NXByteOrder target_byte_sex
)
485 for(i
= 0; i
< nmods
; i
++){
486 mods
[i
].module_name
= NXSwapLong(mods
[i
].module_name
);
487 mods
[i
].iextdefsym
= NXSwapLong(mods
[i
].iextdefsym
);
488 mods
[i
].nextdefsym
= NXSwapLong(mods
[i
].nextdefsym
);
489 mods
[i
].irefsym
= NXSwapLong(mods
[i
].irefsym
);
490 mods
[i
].nrefsym
= NXSwapLong(mods
[i
].nrefsym
);
491 mods
[i
].ilocalsym
= NXSwapLong(mods
[i
].ilocalsym
);
492 mods
[i
].nlocalsym
= NXSwapLong(mods
[i
].nlocalsym
);
493 mods
[i
].iextrel
= NXSwapLong(mods
[i
].iextrel
);
494 mods
[i
].nextrel
= NXSwapLong(mods
[i
].nextrel
);
495 mods
[i
].iinit_iterm
= NXSwapLong(mods
[i
].iinit_iterm
);
496 mods
[i
].ninit_nterm
= NXSwapLong(mods
[i
].ninit_nterm
);
501 swap_dylib_table_of_contents(
502 struct dylib_table_of_contents
*tocs
,
504 enum NXByteOrder target_byte_sex
)
508 for(i
= 0; i
< ntocs
; i
++){
509 tocs
[i
].symbol_index
= NXSwapLong(tocs
[i
].symbol_index
);
510 tocs
[i
].module_index
= NXSwapLong(tocs
[i
].module_index
);
513 #endif /* !defined(RLD) */