2 * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 // Copyright 1988-1996 NeXT Software, Inc.
27 #include "objc-private.h"
28 #include "objc-runtime-old.h"
33 PRIVATE_EXTERN const char *_getObjcHeaderName(const headerType *head)
40 _getObjcModules(const header_info *hi, size_t *nmodules)
42 if (nmodules) *nmodules = hi->os.moduleCount;
43 return hi->os.modules;
47 _getObjcSelectorRefs(const header_info *hi, size_t *nmess)
49 if (nmess) *nmess = hi->os.selrefCount;
50 return hi->os.selrefs;
53 PRIVATE_EXTERN struct old_protocol **
54 _getObjcProtocols(const header_info *hi, size_t *nprotos)
56 if (nprotos) *nprotos = hi->os.protocolCount;
57 return hi->os.protocols;
60 PRIVATE_EXTERN struct old_class **
61 _getObjcClassRefs(const header_info *hi, size_t *nclasses)
63 if (nclasses) *nclasses = hi->os.clsrefCount;
64 return (struct old_class **)hi->os.clsrefs;
67 // __OBJC,__class_names section only emitted by CodeWarrior rdar://4951638
68 PRIVATE_EXTERN const char *
69 _getObjcClassNames(const header_info *hi, size_t *size)
77 #define GETSECT(name, type, sectname) \
78 PRIVATE_EXTERN type *name(const header_info *hi, size_t *outCount) \
80 unsigned long byteCount = 0; \
81 type *data = (type *) \
82 getsectiondata(hi->mhdr, SEG_OBJC, sectname, &byteCount); \
83 *outCount = byteCount / sizeof(type); \
87 GETSECT(_getObjcModules, struct objc_module, "__module_info");
88 GETSECT(_getObjcSelectorRefs, SEL, "__message_refs");
89 GETSECT(_getObjcClassRefs, struct old_class *, "__cls_refs");
90 GETSECT(_getObjcClassNames, const char, "__class_names");
91 // __OBJC,__class_names section only emitted by CodeWarrior rdar://4951638
94 PRIVATE_EXTERN objc_image_info *
95 _getObjcImageInfo(const headerType *mhdr, size_t *outBytes)
97 unsigned long byteCount = 0;
98 objc_image_info *info = (objc_image_info *)
99 getsectiondata(mhdr, SEG_OBJC, "__image_info", &byteCount);
100 *outBytes = byteCount;
105 PRIVATE_EXTERN struct old_protocol **
106 _getObjcProtocols(const header_info *hi, size_t *nprotos)
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);
113 if (!hi->os.proto_refs && *nprotos) {
115 header_info *whi = (header_info *)hi;
116 whi->os.proto_refs = (struct old_protocol **)
117 malloc(*nprotos * sizeof(*hi->os.proto_refs));
118 for (i = 0; i < *nprotos; i++) {
119 hi->os.proto_refs[i] = protos+i;
123 return hi->os.proto_refs;
127 static const segmentType *
128 getsegbynamefromheader(const headerType *head, const char *segname)
130 const segmentType *sgp;
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) {
140 sgp = (const segmentType *)((char *)sgp + sgp->cmdsize);
146 _hasObjcContents(const header_info *hi)
148 // Look for an __OBJC,* section other than __OBJC,__image_info
149 const segmentType *seg = getsegbynamefromheader(hi->mhdr, "__OBJC");
150 const sectionType *sect;
152 for (i = 0; i < seg->nsects; i++) {
153 sect = ((const sectionType *)(seg+1))+i;
154 if (0 != strncmp(sect->sectname, "__image_info", 12)) {