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"
29 #include "objc-file-old.h"
35 _getObjcModules(const header_info *hi, size_t *nmodules)
37 if (nmodules) *nmodules = hi->moduleCount;
42 _getObjcSelectorRefs(const header_info *hi, size_t *nmess)
44 if (nmess) *nmess = hi->selrefCount;
48 struct old_protocol **
49 _getObjcProtocols(const header_info *hi, size_t *nprotos)
51 if (nprotos) *nprotos = hi->protocolCount;
56 _getObjcClassRefs(const header_info *hi, size_t *nclasses)
58 if (nclasses) *nclasses = hi->clsrefCount;
59 return (Class*)hi->clsrefs;
62 // __OBJC,__class_names section only emitted by CodeWarrior rdar://4951638
64 _getObjcClassNames(const header_info *hi, size_t *size)
72 #define GETSECT(name, type, segname, sectname) \
73 type *name(const headerType *mhdr, size_t *outCount) \
75 unsigned long byteCount = 0; \
76 type *data = (type *) \
77 getsectiondata(mhdr, segname, sectname, &byteCount); \
78 *outCount = byteCount / sizeof(type); \
81 type *name(const header_info *hi, size_t *outCount) \
83 return name(hi->mhdr(), outCount); \
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, UnsignedInitializer, "__DATA", "__objc_init_func");
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 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->proto_refs && *nprotos) {
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;
123 return hi->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)) {