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@
26 #include "objc-private.h"
27 #include "objc-file.h"
29 // Segment and section names are 16 bytes and may be un-terminated.
30 bool segnameEquals(const char *lhs, const char *rhs) {
31 return 0 == strncmp(lhs, rhs, 16);
34 bool segnameStartsWith(const char *segname, const char *prefix) {
35 return 0 == strncmp(segname, prefix, strlen(prefix));
38 bool sectnameEquals(const char *lhs, const char *rhs) {
39 return segnameEquals(lhs, rhs);
42 bool sectnameStartsWith(const char *sectname, const char *prefix) {
43 return segnameStartsWith(sectname, prefix);
47 // Look for a __DATA or __DATA_CONST or __DATA_DIRTY section
48 // with the given name that stores an array of T.
50 T* getDataSection(const headerType *mhdr, const char *sectname,
51 size_t *outBytes, size_t *outCount)
53 unsigned long byteCount = 0;
54 T* data = (T*)getsectiondata(mhdr, "__DATA", sectname, &byteCount);
56 data = (T*)getsectiondata(mhdr, "__DATA_CONST", sectname, &byteCount);
59 data = (T*)getsectiondata(mhdr, "__DATA_DIRTY", sectname, &byteCount);
61 if (outBytes) *outBytes = byteCount;
62 if (outCount) *outCount = byteCount / sizeof(T);
66 #define GETSECT(name, type, sectname) \
67 type *name(const headerType *mhdr, size_t *outCount) { \
68 return getDataSection<type>(mhdr, sectname, nil, outCount); \
70 type *name(const header_info *hi, size_t *outCount) { \
71 return getDataSection<type>(hi->mhdr, sectname, nil, outCount); \
74 // function name content type section name
75 GETSECT(_getObjc2SelectorRefs, SEL, "__objc_selrefs");
76 GETSECT(_getObjc2MessageRefs, message_ref_t, "__objc_msgrefs");
77 GETSECT(_getObjc2ClassRefs, Class, "__objc_classrefs");
78 GETSECT(_getObjc2SuperRefs, Class, "__objc_superrefs");
79 GETSECT(_getObjc2ClassList, classref_t, "__objc_classlist");
80 GETSECT(_getObjc2NonlazyClassList, classref_t, "__objc_nlclslist");
81 GETSECT(_getObjc2CategoryList, category_t *, "__objc_catlist");
82 GETSECT(_getObjc2NonlazyCategoryList, category_t *, "__objc_nlcatlist");
83 GETSECT(_getObjc2ProtocolList, protocol_t *, "__objc_protolist");
84 GETSECT(_getObjc2ProtocolRefs, protocol_t *, "__objc_protorefs");
85 GETSECT(getLibobjcInitializers, Initializer, "__objc_init_func");
89 _getObjcImageInfo(const headerType *mhdr, size_t *outBytes)
91 return getDataSection<objc_image_info>(mhdr, "__objc_imageinfo",
96 static const segmentType *
97 getsegbynamefromheader(const headerType *mhdr, const char *segname)
99 const segmentType *seg = (const segmentType *) (mhdr + 1);
100 for (unsigned long i = 0; i < mhdr->ncmds; i++){
101 if (seg->cmd == SEGMENT_CMD && segnameEquals(seg->segname, segname)) {
104 seg = (const segmentType *)((char *)seg + seg->cmdsize);
109 // Look for an __objc* section other than __objc_imageinfo
110 static bool segmentHasObjcContents(const segmentType *seg)
113 for (uint32_t i = 0; i < seg->nsects; i++) {
114 const sectionType *sect = ((const sectionType *)(seg+1))+i;
115 if (sectnameStartsWith(sect->sectname, "__objc_") &&
116 !sectnameEquals(sect->sectname, "__objc_imageinfo"))
126 // Look for an __objc* section other than __objc_imageinfo
128 _hasObjcContents(const header_info *hi)
130 const segmentType *data =
131 getsegbynamefromheader(hi->mhdr, "__DATA");
132 const segmentType *data_const =
133 getsegbynamefromheader(hi->mhdr, "__DATA_CONST");
134 const segmentType *data_dirty =
135 getsegbynamefromheader(hi->mhdr, "__DATA_CONST");
137 return segmentHasObjcContents(data)
138 || segmentHasObjcContents(data_const)
139 || segmentHasObjcContents(data_dirty);