ranlib: fix it
[odcctools-svp.git] / libstuff / get_toc_byte_sex.c
blobb3a48aa268aa723cd059b0f886f339df8833ec53
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 #include <stdlib.h>
24 #include <string.h>
25 #include <ar.h>
26 #ifndef AR_EFMT1
27 #define AR_EFMT1 "#1/" /* extended format #1 */
28 #endif
29 #include <mach-o/loader.h>
30 #include <stuff/bytesex.h>
31 #include <stuff/round.h>
34 * get_toc_byte_sex() guesses the byte sex of the table of contents of the
35 * library mapped in at the address, addr, of size, size based on the first
36 * object file's bytesex. If it can't figure it out, because the library has
37 * no object file members or is malformed it will return UNKNOWN_BYTE_SEX.
39 __private_extern__
40 enum byte_sex
41 get_toc_byte_sex(
42 char *addr,
43 unsigned long size)
45 uint32_t magic;
46 unsigned long ar_name_size;
47 struct ar_hdr *ar_hdr;
48 char *p;
50 ar_hdr = (struct ar_hdr *)(addr + SARMAG);
52 p = addr + SARMAG + sizeof(struct ar_hdr) +
53 round(strtoul(ar_hdr->ar_size, NULL, 10), sizeof(short));
54 while(p + sizeof(struct ar_hdr) + sizeof(uint32_t) < addr + size){
55 ar_hdr = (struct ar_hdr *)p;
56 if(strncmp(ar_hdr->ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0)
57 ar_name_size = strtoul(ar_hdr->ar_name + sizeof(AR_EFMT1) - 1,
58 NULL, 10);
59 else
60 ar_name_size = 0;
61 p += sizeof(struct ar_hdr);
62 memcpy(&magic, p + ar_name_size, sizeof(uint32_t));
63 if(magic == MH_MAGIC || magic == MH_MAGIC_64)
64 return(get_host_byte_sex());
65 else if(magic == SWAP_INT(MH_MAGIC) ||
66 magic == SWAP_INT(MH_MAGIC_64))
67 return(get_host_byte_sex() == BIG_ENDIAN_BYTE_SEX ?
68 LITTLE_ENDIAN_BYTE_SEX : BIG_ENDIAN_BYTE_SEX);
69 p += round(strtoul(ar_hdr->ar_size, NULL, 10), sizeof(short));
71 return(UNKNOWN_BYTE_SEX);