]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-file.mm
dac4ecd91154694da0b9e21442a446fcfdcd8c6d
[apple/objc4.git] / runtime / objc-file.mm
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 // Segment and section names are 16 bytes and may be un-terminated.
30 bool segnameEquals(const char *lhs, const char *rhs) {
31 return 0 == strncmp(lhs, rhs, 16);
32 }
33
34 bool segnameStartsWith(const char *segname, const char *prefix) {
35 return 0 == strncmp(segname, prefix, strlen(prefix));
36 }
37
38 bool sectnameEquals(const char *lhs, const char *rhs) {
39 return segnameEquals(lhs, rhs);
40 }
41
42 bool sectnameStartsWith(const char *sectname, const char *prefix) {
43 return segnameStartsWith(sectname, prefix);
44 }
45
46
47 // Look for a __DATA or __DATA_CONST or __DATA_DIRTY section
48 // with the given name that stores an array of T.
49 template <typename T>
50 T* getDataSection(const headerType *mhdr, const char *sectname,
51 size_t *outBytes, size_t *outCount)
52 {
53 unsigned long byteCount = 0;
54 T* data = (T*)getsectiondata(mhdr, "__DATA", sectname, &byteCount);
55 if (!data) {
56 data = (T*)getsectiondata(mhdr, "__DATA_CONST", sectname, &byteCount);
57 }
58 if (!data) {
59 data = (T*)getsectiondata(mhdr, "__DATA_DIRTY", sectname, &byteCount);
60 }
61 if (outBytes) *outBytes = byteCount;
62 if (outCount) *outCount = byteCount / sizeof(T);
63 return data;
64 }
65
66 #define GETSECT(name, type, sectname) \
67 type *name(const headerType *mhdr, size_t *outCount) { \
68 return getDataSection<type>(mhdr, sectname, nil, outCount); \
69 } \
70 type *name(const header_info *hi, size_t *outCount) { \
71 return getDataSection<type>(hi->mhdr, sectname, nil, outCount); \
72 }
73
74 // function name content type section name
75 GETSECT(_getObjc2SelectorRefs, SEL, "__objc_selrefs");
76 GETSECT(_getObjc2MessageRefs, message_ref_t, "__objc_msgrefs");
77 GETSECT(_getObjc2ClassRefs, Class, "__objc_classrefs");
78 GETSECT(_getObjc2SuperRefs, Class, "__objc_superrefs");
79 GETSECT(_getObjc2ClassList, classref_t, "__objc_classlist");
80 GETSECT(_getObjc2NonlazyClassList, classref_t, "__objc_nlclslist");
81 GETSECT(_getObjc2CategoryList, category_t *, "__objc_catlist");
82 GETSECT(_getObjc2NonlazyCategoryList, category_t *, "__objc_nlcatlist");
83 GETSECT(_getObjc2ProtocolList, protocol_t *, "__objc_protolist");
84 GETSECT(_getObjc2ProtocolRefs, protocol_t *, "__objc_protorefs");
85 GETSECT(getLibobjcInitializers, Initializer, "__objc_init_func");
86
87
88 objc_image_info *
89 _getObjcImageInfo(const headerType *mhdr, size_t *outBytes)
90 {
91 return getDataSection<objc_image_info>(mhdr, "__objc_imageinfo",
92 outBytes, nil);
93 }
94
95
96 static const segmentType *
97 getsegbynamefromheader(const headerType *mhdr, const char *segname)
98 {
99 const segmentType *seg = (const segmentType *) (mhdr + 1);
100 for (unsigned long i = 0; i < mhdr->ncmds; i++){
101 if (seg->cmd == SEGMENT_CMD && segnameEquals(seg->segname, segname)) {
102 return seg;
103 }
104 seg = (const segmentType *)((char *)seg + seg->cmdsize);
105 }
106 return nil;
107 }
108
109 // Look for an __objc* section other than __objc_imageinfo
110 static bool segmentHasObjcContents(const segmentType *seg)
111 {
112 if (seg) {
113 for (uint32_t i = 0; i < seg->nsects; i++) {
114 const sectionType *sect = ((const sectionType *)(seg+1))+i;
115 if (sectnameStartsWith(sect->sectname, "__objc_") &&
116 !sectnameEquals(sect->sectname, "__objc_imageinfo"))
117 {
118 return true;
119 }
120 }
121 }
122
123 return false;
124 }
125
126 // Look for an __objc* section other than __objc_imageinfo
127 bool
128 _hasObjcContents(const header_info *hi)
129 {
130 const segmentType *data =
131 getsegbynamefromheader(hi->mhdr, "__DATA");
132 const segmentType *data_const =
133 getsegbynamefromheader(hi->mhdr, "__DATA_CONST");
134 const segmentType *data_dirty =
135 getsegbynamefromheader(hi->mhdr, "__DATA_CONST");
136
137 return segmentHasObjcContents(data)
138 || segmentHasObjcContents(data_const)
139 || segmentHasObjcContents(data_dirty);
140 }
141
142 #endif