]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-file.mm
objc4-532.tar.gz
[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 #if TARGET_IPHONE_SIMULATOR
30 // getsectiondata() not yet available
31
32 // 1. Find segment with file offset == 0 and file size != 0. This segment's
33 // contents span the Mach-O header. (File size of 0 is .bss, for example)
34 // 2. Slide is header's address - segment's preferred address
35 static ptrdiff_t
36 objc_getImageSlide(const struct mach_header *header)
37 {
38 unsigned long i;
39 const struct segment_command *sgp = (const struct segment_command *)(header + 1);
40
41 for (i = 0; i < header->ncmds; i++){
42 if (sgp->cmd == LC_SEGMENT) {
43 if (sgp->fileoff == 0 && sgp->filesize != 0) {
44 return (uintptr_t)header - (uintptr_t)sgp->vmaddr;
45 }
46 }
47 sgp = (const struct segment_command *)((char *)sgp + sgp->cmdsize);
48 }
49
50 // uh-oh
51 _objc_fatal("could not calculate VM slide for image");
52 return 0; // not reached
53 }
54
55 uint8_t *
56 objc_getsectiondata(const struct mach_header *mh, const char *segname, const char *sectname, unsigned long *outSize)
57 {
58 uint32_t size = 0;
59
60 char *data = getsectdatafromheader(mh, segname, sectname, &size);
61 if (data) {
62 *outSize = size;
63 return (uint8_t *)data + objc_getImageSlide(mh);
64 } else {
65 *outSize = 0;
66 return NULL;
67 }
68 }
69
70 static const struct segment_command *
71 objc_getsegbynamefromheader(const mach_header *head, const char *segname)
72 {
73 const struct segment_command *sgp;
74 unsigned long i;
75
76 sgp = (const struct segment_command *) (head + 1);
77 for (i = 0; i < head->ncmds; i++){
78 if (sgp->cmd == LC_SEGMENT) {
79 if (strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) {
80 return sgp;
81 }
82 }
83 sgp = (const struct segment_command *)((char *)sgp + sgp->cmdsize);
84 }
85 return NULL;
86 }
87
88 uint8_t *
89 objc_getsegmentdata(const struct mach_header *mh, const char *segname, unsigned long *outSize)
90 {
91 const struct segment_command *seg;
92
93 seg = objc_getsegbynamefromheader(mh, segname);
94 if (seg) {
95 *outSize = seg->vmsize;
96 return (uint8_t *)seg->vmaddr + objc_getImageSlide(mh);
97 } else {
98 *outSize = 0;
99 return NULL;
100 }
101 }
102
103 // TARGET_IPHONE_SIMULATOR
104 #endif
105
106 #define GETSECT(name, type, sectname) \
107 type *name(const header_info *hi, size_t *outCount) \
108 { \
109 unsigned long byteCount = 0; \
110 type *data = (type *) \
111 getsectiondata(hi->mhdr, SEG_DATA, sectname, &byteCount); \
112 *outCount = byteCount / sizeof(type); \
113 return data; \
114 }
115
116 // function name content type section name
117 GETSECT(_getObjc2SelectorRefs, SEL, "__objc_selrefs");
118 GETSECT(_getObjc2MessageRefs, message_ref_t, "__objc_msgrefs");
119 GETSECT(_getObjc2ClassRefs, class_t *, "__objc_classrefs");
120 GETSECT(_getObjc2SuperRefs, class_t *, "__objc_superrefs");
121 GETSECT(_getObjc2ClassList, classref_t, "__objc_classlist");
122 GETSECT(_getObjc2NonlazyClassList, classref_t, "__objc_nlclslist");
123 GETSECT(_getObjc2CategoryList, category_t *, "__objc_catlist");
124 GETSECT(_getObjc2NonlazyCategoryList, category_t *, "__objc_nlcatlist");
125 GETSECT(_getObjc2ProtocolList, protocol_t *, "__objc_protolist");
126 GETSECT(_getObjc2ProtocolRefs, protocol_t *, "__objc_protorefs");
127
128
129 objc_image_info *
130 _getObjcImageInfo(const headerType *mhdr, size_t *outBytes)
131 {
132 unsigned long byteCount = 0;
133 objc_image_info *data = (objc_image_info *)
134 getsectiondata(mhdr, SEG_DATA, "__objc_imageinfo", &byteCount);
135 *outBytes = byteCount;
136 return data;
137 }
138
139
140 static const segmentType *
141 getsegbynamefromheader(const headerType *head, const char *segname)
142 {
143 const segmentType *sgp;
144 unsigned long i;
145
146 sgp = (const segmentType *) (head + 1);
147 for (i = 0; i < head->ncmds; i++){
148 if (sgp->cmd == SEGMENT_CMD) {
149 if (strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) {
150 return sgp;
151 }
152 }
153 sgp = (const segmentType *)((char *)sgp + sgp->cmdsize);
154 }
155 return NULL;
156 }
157
158 BOOL
159 _hasObjcContents(const header_info *hi)
160 {
161 // Look for a __DATA,__objc* section other than __DATA,__objc_imageinfo
162 const segmentType *seg = getsegbynamefromheader(hi->mhdr, "__DATA");
163 if (!seg) return NO;
164
165 const sectionType *sect;
166 uint32_t i;
167 for (i = 0; i < seg->nsects; i++) {
168 sect = ((const sectionType *)(seg+1))+i;
169 if (0 == strncmp(sect->sectname, "__objc_", 7) &&
170 0 != strncmp(sect->sectname, "__objc_imageinfo", 16))
171 {
172 return YES;
173 }
174 }
175
176 return NO;
177 }
178
179 #endif