]>
Commit | Line | Data |
---|---|---|
8972963c A |
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 | ||
31875a97 A |
29 | |
30 | // Look for a __DATA or __DATA_CONST or __DATA_DIRTY section | |
31 | // with the given name that stores an array of T. | |
32 | template <typename T> | |
33 | T* getDataSection(const headerType *mhdr, const char *sectname, | |
34 | size_t *outBytes, size_t *outCount) | |
35 | { | |
36 | unsigned long byteCount = 0; | |
37 | T* data = (T*)getsectiondata(mhdr, "__DATA", sectname, &byteCount); | |
38 | if (!data) { | |
39 | data = (T*)getsectiondata(mhdr, "__DATA_CONST", sectname, &byteCount); | |
40 | } | |
41 | if (!data) { | |
42 | data = (T*)getsectiondata(mhdr, "__DATA_DIRTY", sectname, &byteCount); | |
43 | } | |
44 | if (outBytes) *outBytes = byteCount; | |
45 | if (outCount) *outCount = byteCount / sizeof(T); | |
46 | return data; | |
47 | } | |
48 | ||
8972963c | 49 | #define GETSECT(name, type, sectname) \ |
31875a97 A |
50 | type *name(const headerType *mhdr, size_t *outCount) { \ |
51 | return getDataSection<type>(mhdr, sectname, nil, outCount); \ | |
52 | } \ | |
53 | type *name(const header_info *hi, size_t *outCount) { \ | |
c1e772c4 | 54 | return getDataSection<type>(hi->mhdr(), sectname, nil, outCount); \ |
8972963c A |
55 | } |
56 | ||
57 | // function name content type section name | |
58 | GETSECT(_getObjc2SelectorRefs, SEL, "__objc_selrefs"); | |
59 | GETSECT(_getObjc2MessageRefs, message_ref_t, "__objc_msgrefs"); | |
31875a97 A |
60 | GETSECT(_getObjc2ClassRefs, Class, "__objc_classrefs"); |
61 | GETSECT(_getObjc2SuperRefs, Class, "__objc_superrefs"); | |
1807f628 A |
62 | GETSECT(_getObjc2ClassList, classref_t const, "__objc_classlist"); |
63 | GETSECT(_getObjc2NonlazyClassList, classref_t const, "__objc_nlclslist"); | |
64 | GETSECT(_getObjc2CategoryList, category_t * const, "__objc_catlist"); | |
65 | GETSECT(_getObjc2CategoryList2, category_t * const, "__objc_catlist2"); | |
66 | GETSECT(_getObjc2NonlazyCategoryList, category_t * const, "__objc_nlcatlist"); | |
67 | GETSECT(_getObjc2ProtocolList, protocol_t * const, "__objc_protolist"); | |
8972963c | 68 | GETSECT(_getObjc2ProtocolRefs, protocol_t *, "__objc_protorefs"); |
66799735 | 69 | GETSECT(getLibobjcInitializers, UnsignedInitializer, "__objc_init_func"); |
8972963c | 70 | |
34d5b5e8 A |
71 | uint32_t *getLibobjcInitializerOffsets(const headerType *mhdr, size_t *outCount) { |
72 | unsigned long byteCount = 0; | |
73 | uint32_t *offsets = (uint32_t *)getsectiondata(mhdr, "__TEXT", "__objc_init_offs", &byteCount); | |
74 | if (outCount) *outCount = byteCount / sizeof(uint32_t); | |
75 | return offsets; | |
76 | } | |
8972963c | 77 | |
cd5f04f5 | 78 | objc_image_info * |
8972963c A |
79 | _getObjcImageInfo(const headerType *mhdr, size_t *outBytes) |
80 | { | |
31875a97 A |
81 | return getDataSection<objc_image_info>(mhdr, "__objc_imageinfo", |
82 | outBytes, nil); | |
8972963c A |
83 | } |
84 | ||
31875a97 A |
85 | // Look for an __objc* section other than __objc_imageinfo |
86 | static bool segmentHasObjcContents(const segmentType *seg) | |
8972963c | 87 | { |
66799735 A |
88 | for (uint32_t i = 0; i < seg->nsects; i++) { |
89 | const sectionType *sect = ((const sectionType *)(seg+1))+i; | |
90 | if (sectnameStartsWith(sect->sectname, "__objc_") && | |
91 | !sectnameEquals(sect->sectname, "__objc_imageinfo")) | |
92 | { | |
93 | return true; | |
8972963c A |
94 | } |
95 | } | |
96 | ||
31875a97 A |
97 | return false; |
98 | } | |
99 | ||
100 | // Look for an __objc* section other than __objc_imageinfo | |
101 | bool | |
102 | _hasObjcContents(const header_info *hi) | |
103 | { | |
66799735 A |
104 | bool foundObjC = false; |
105 | ||
106 | foreach_data_segment(hi->mhdr(), [&](const segmentType *seg, intptr_t slide) | |
107 | { | |
108 | if (segmentHasObjcContents(seg)) foundObjC = true; | |
109 | }); | |
110 | ||
111 | return foundObjC; | |
31875a97 | 112 | |
8972963c A |
113 | } |
114 | ||
c1e772c4 A |
115 | |
116 | // OBJC2 | |
8972963c | 117 | #endif |