]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-file-old.mm
e047eed1ba38eb24702a80bd3dfed82d3114bc20
[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, sectname) \
73 type *name(const header_info *hi, size_t *outCount) \
74 { \
75 unsigned long byteCount = 0; \
76 type *data = (type *) \
77 getsectiondata(hi->mhdr, SEG_OBJC, sectname, &byteCount); \
78 *outCount = byteCount / sizeof(type); \
79 return data; \
80 }
81
82 GETSECT(_getObjcModules, struct objc_module, "__module_info");
83 GETSECT(_getObjcSelectorRefs, SEL, "__message_refs");
84 GETSECT(_getObjcClassRefs, Class, "__cls_refs");
85 GETSECT(_getObjcClassNames, const char, "__class_names");
86 // __OBJC,__class_names section only emitted by CodeWarrior rdar://4951638
87
88
89 objc_image_info *
90 _getObjcImageInfo(const headerType *mhdr, size_t *outBytes)
91 {
92 unsigned long byteCount = 0;
93 objc_image_info *info = (objc_image_info *)
94 getsectiondata(mhdr, SEG_OBJC, "__image_info", &byteCount);
95 *outBytes = byteCount;
96 return info;
97 }
98
99
100 struct old_protocol **
101 _getObjcProtocols(const header_info *hi, size_t *nprotos)
102 {
103 unsigned long size = 0;
104 struct old_protocol *protos = (struct old_protocol *)
105 getsectiondata(hi->mhdr, SEG_OBJC, "__protocol", &size);
106 *nprotos = size / sizeof(struct old_protocol);
107
108 if (!hi->proto_refs && *nprotos) {
109 size_t i;
110 header_info *whi = (header_info *)hi;
111 whi->proto_refs = (struct old_protocol **)
112 malloc(*nprotos * sizeof(*hi->proto_refs));
113 for (i = 0; i < *nprotos; i++) {
114 hi->proto_refs[i] = protos+i;
115 }
116 }
117
118 return hi->proto_refs;
119 }
120
121
122 static const segmentType *
123 getsegbynamefromheader(const headerType *head, const char *segname)
124 {
125 const segmentType *sgp;
126 unsigned long i;
127
128 sgp = (const segmentType *) (head + 1);
129 for (i = 0; i < head->ncmds; i++){
130 if (sgp->cmd == SEGMENT_CMD) {
131 if (strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) {
132 return sgp;
133 }
134 }
135 sgp = (const segmentType *)((char *)sgp + sgp->cmdsize);
136 }
137 return NULL;
138 }
139
140 BOOL
141 _hasObjcContents(const header_info *hi)
142 {
143 // Look for an __OBJC,* section other than __OBJC,__image_info
144 const segmentType *seg = getsegbynamefromheader(hi->mhdr, "__OBJC");
145 const sectionType *sect;
146 uint32_t i;
147 for (i = 0; i < seg->nsects; i++) {
148 sect = ((const sectionType *)(seg+1))+i;
149 if (0 != strncmp(sect->sectname, "__image_info", 12)) {
150 return YES;
151 }
152 }
153
154 return NO;
155 }
156
157
158 #endif
159
160 #endif