]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-file-old.mm
objc4-706.tar.gz
[apple/objc4.git] / runtime / objc-file-old.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 // Copyright 1988-1996 NeXT Software, Inc.
24
25 #if !__OBJC2__
26
27 #include "objc-private.h"
28 #include "objc-runtime-old.h"
29 #include "objc-file-old.h"
30
31 #if TARGET_OS_WIN32
32
33 /*
34 Module
35 _getObjcModules(const header_info *hi, size_t *nmodules)
36 {
37 if (nmodules) *nmodules = hi->moduleCount;
38 return hi->modules;
39 }
40 */
41 SEL *
42 _getObjcSelectorRefs(const header_info *hi, size_t *nmess)
43 {
44 if (nmess) *nmess = hi->selrefCount;
45 return hi->selrefs;
46 }
47
48 struct old_protocol **
49 _getObjcProtocols(const header_info *hi, size_t *nprotos)
50 {
51 if (nprotos) *nprotos = hi->protocolCount;
52 return hi->protocols;
53 }
54
55 Class*
56 _getObjcClassRefs(const header_info *hi, size_t *nclasses)
57 {
58 if (nclasses) *nclasses = hi->clsrefCount;
59 return (Class*)hi->clsrefs;
60 }
61
62 // __OBJC,__class_names section only emitted by CodeWarrior rdar://4951638
63 const char *
64 _getObjcClassNames(const header_info *hi, size_t *size)
65 {
66 if (size) *size = 0;
67 return NULL;
68 }
69
70 #else
71
72 #define GETSECT(name, type, segname, sectname) \
73 type *name(const headerType *mhdr, size_t *outCount) \
74 { \
75 unsigned long byteCount = 0; \
76 type *data = (type *) \
77 getsectiondata(mhdr, segname, sectname, &byteCount); \
78 *outCount = byteCount / sizeof(type); \
79 return data; \
80 } \
81 type *name(const header_info *hi, size_t *outCount) \
82 { \
83 return name(hi->mhdr(), outCount); \
84 }
85
86 GETSECT(_getObjcModules, objc_module, "__OBJC", "__module_info");
87 GETSECT(_getObjcSelectorRefs, SEL, "__OBJC", "__message_refs");
88 GETSECT(_getObjcClassRefs, Class, "__OBJC", "__cls_refs");
89 GETSECT(_getObjcClassNames, const char, "__OBJC", "__class_names");
90 // __OBJC,__class_names section only emitted by CodeWarrior rdar://4951638
91 GETSECT(getLibobjcInitializers, Initializer, "__DATA", "__objc_init_func");
92
93
94 objc_image_info *
95 _getObjcImageInfo(const headerType *mhdr, size_t *outBytes)
96 {
97 unsigned long byteCount = 0;
98 objc_image_info *info = (objc_image_info *)
99 getsectiondata(mhdr, SEG_OBJC, "__image_info", &byteCount);
100 *outBytes = byteCount;
101 return info;
102 }
103
104
105 struct old_protocol **
106 _getObjcProtocols(const header_info *hi, size_t *nprotos)
107 {
108 unsigned long size = 0;
109 struct old_protocol *protos = (struct old_protocol *)
110 getsectiondata(hi->mhdr(), SEG_OBJC, "__protocol", &size);
111 *nprotos = size / sizeof(struct old_protocol);
112
113 if (!hi->proto_refs && *nprotos) {
114 size_t i;
115 header_info *whi = (header_info *)hi;
116 whi->proto_refs = (struct old_protocol **)
117 malloc(*nprotos * sizeof(*hi->proto_refs));
118 for (i = 0; i < *nprotos; i++) {
119 hi->proto_refs[i] = protos+i;
120 }
121 }
122
123 return hi->proto_refs;
124 }
125
126
127 static const segmentType *
128 getsegbynamefromheader(const headerType *head, const char *segname)
129 {
130 const segmentType *sgp;
131 unsigned long i;
132
133 sgp = (const segmentType *) (head + 1);
134 for (i = 0; i < head->ncmds; i++){
135 if (sgp->cmd == SEGMENT_CMD) {
136 if (strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) {
137 return sgp;
138 }
139 }
140 sgp = (const segmentType *)((char *)sgp + sgp->cmdsize);
141 }
142 return NULL;
143 }
144
145 bool
146 _hasObjcContents(const header_info *hi)
147 {
148 // Look for an __OBJC,* section other than __OBJC,__image_info
149 const segmentType *seg = getsegbynamefromheader(hi->mhdr(), "__OBJC");
150 const sectionType *sect;
151 uint32_t i;
152 for (i = 0; i < seg->nsects; i++) {
153 sect = ((const sectionType *)(seg+1))+i;
154 if (0 != strncmp(sect->sectname, "__image_info", 12)) {
155 return YES;
156 }
157 }
158
159 return NO;
160 }
161
162
163 #endif
164
165 #endif