1/*
2 * Copyright (c) 1999-2007 Apple Inc.  All Rights Reserved.
3 *
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.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#if __OBJC2__
25
26#include "objc-private.h"
27#include "objc-file.h"
28
29#define GETSECT(name, type, sectname)                                   \
30    type *name(const header_info *hi, size_t *outCount)  \
31    {                                                                   \
32        unsigned long byteCount = 0;                                    \
33        type *data = (type *)                                           \
34            getsectiondata(hi->mhdr, SEG_DATA, sectname, &byteCount);   \
35        *outCount = byteCount / sizeof(type);                           \
36        return data;                                                    \
37    }
38
39//      function name                 content type     section name
40GETSECT(_getObjc2SelectorRefs,        SEL,             "__objc_selrefs");
41GETSECT(_getObjc2MessageRefs,         message_ref_t,   "__objc_msgrefs");
42GETSECT(_getObjc2ClassRefs,           Class,       "__objc_classrefs");
43GETSECT(_getObjc2SuperRefs,           Class,       "__objc_superrefs");
44GETSECT(_getObjc2ClassList,           classref_t,       "__objc_classlist");
45GETSECT(_getObjc2NonlazyClassList,    classref_t,       "__objc_nlclslist");
46GETSECT(_getObjc2CategoryList,        category_t *,    "__objc_catlist");
47GETSECT(_getObjc2NonlazyCategoryList, category_t *,    "__objc_nlcatlist");
48GETSECT(_getObjc2ProtocolList,        protocol_t *,    "__objc_protolist");
49GETSECT(_getObjc2ProtocolRefs,        protocol_t *,    "__objc_protorefs");
50
51
52objc_image_info *
53_getObjcImageInfo(const headerType *mhdr, size_t *outBytes)
54{
55    unsigned long byteCount = 0;
56    objc_image_info *data = (objc_image_info *)
57        getsectiondata(mhdr, SEG_DATA, "__objc_imageinfo", &byteCount);
58    *outBytes = byteCount;
59    return data;
60}
61
62
63static const segmentType *
64getsegbynamefromheader(const headerType *head, const char *segname)
65{
66    const segmentType *sgp;
67    unsigned long i;
68
69    sgp = (const segmentType *) (head + 1);
70    for (i = 0; i < head->ncmds; i++){
71        if (sgp->cmd == SEGMENT_CMD) {
72            if (strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) {
73                return sgp;
74            }
75        }
76        sgp = (const segmentType *)((char *)sgp + sgp->cmdsize);
77    }
78    return NULL;
79}
80
81BOOL
82_hasObjcContents(const header_info *hi)
83{
84    // Look for a __DATA,__objc* section other than __DATA,__objc_imageinfo
85    const segmentType *seg = getsegbynamefromheader(hi->mhdr, "__DATA");
86    if (!seg) return NO;
87
88    const sectionType *sect;
89    uint32_t i;
90    for (i = 0; i < seg->nsects; i++) {
91        sect = ((const sectionType *)(seg+1))+i;
92        if (0 == strncmp(sect->sectname, "__objc_", 7)  &&
93            0 != strncmp(sect->sectname, "__objc_imageinfo", 16))
94        {
95            return YES;
96        }
97    }
98
99    return NO;
100}
101
102#endif
103