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 #if TARGET_IPHONE_SIMULATOR
30 // getsectiondata() not yet available
32 // 1. Find segment with file offset == 0 and file size != 0. This segment's
33 // contents span the Mach-O header. (File size of 0 is .bss, for example)
34 // 2. Slide is header's address - segment's preferred address
36 objc_getImageSlide(const struct mach_header *header)
39 const struct segment_command *sgp = (const struct segment_command *)(header + 1);
41 for (i = 0; i < header->ncmds; i++){
42 if (sgp->cmd == LC_SEGMENT) {
43 if (sgp->fileoff == 0 && sgp->filesize != 0) {
44 return (uintptr_t)header - (uintptr_t)sgp->vmaddr;
47 sgp = (const struct segment_command *)((char *)sgp + sgp->cmdsize);
51 _objc_fatal("could not calculate VM slide for image");
52 return 0; // not reached
56 objc_getsectiondata(const struct mach_header *mh, const char *segname, const char *sectname, unsigned long *outSize)
60 char *data = getsectdatafromheader(mh, segname, sectname, &size);
63 return (uint8_t *)data + objc_getImageSlide(mh);
70 static const struct segment_command *
71 objc_getsegbynamefromheader(const mach_header *head, const char *segname)
73 const struct segment_command *sgp;
76 sgp = (const struct segment_command *) (head + 1);
77 for (i = 0; i < head->ncmds; i++){
78 if (sgp->cmd == LC_SEGMENT) {
79 if (strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) {
83 sgp = (const struct segment_command *)((char *)sgp + sgp->cmdsize);
89 objc_getsegmentdata(const struct mach_header *mh, const char *segname, unsigned long *outSize)
91 const struct segment_command *seg;
93 seg = objc_getsegbynamefromheader(mh, segname);
95 *outSize = seg->vmsize;
96 return (uint8_t *)seg->vmaddr + objc_getImageSlide(mh);
103 // TARGET_IPHONE_SIMULATOR
106 #define GETSECT(name, type, sectname) \
107 type *name(const header_info *hi, size_t *outCount) \
109 unsigned long byteCount = 0; \
110 type *data = (type *) \
111 getsectiondata(hi->mhdr, SEG_DATA, sectname, &byteCount); \
112 *outCount = byteCount / sizeof(type); \
116 // function name content type section name
117 GETSECT(_getObjc2SelectorRefs, SEL, "__objc_selrefs");
118 GETSECT(_getObjc2MessageRefs, message_ref_t, "__objc_msgrefs");
119 GETSECT(_getObjc2ClassRefs, class_t *, "__objc_classrefs");
120 GETSECT(_getObjc2SuperRefs, class_t *, "__objc_superrefs");
121 GETSECT(_getObjc2ClassList, classref_t, "__objc_classlist");
122 GETSECT(_getObjc2NonlazyClassList, classref_t, "__objc_nlclslist");
123 GETSECT(_getObjc2CategoryList, category_t *, "__objc_catlist");
124 GETSECT(_getObjc2NonlazyCategoryList, category_t *, "__objc_nlcatlist");
125 GETSECT(_getObjc2ProtocolList, protocol_t *, "__objc_protolist");
126 GETSECT(_getObjc2ProtocolRefs, protocol_t *, "__objc_protorefs");
130 _getObjcImageInfo(const headerType *mhdr, size_t *outBytes)
132 unsigned long byteCount = 0;
133 objc_image_info *data = (objc_image_info *)
134 getsectiondata(mhdr, SEG_DATA, "__objc_imageinfo", &byteCount);
135 *outBytes = byteCount;
140 static const segmentType *
141 getsegbynamefromheader(const headerType *head, const char *segname)
143 const segmentType *sgp;
146 sgp = (const segmentType *) (head + 1);
147 for (i = 0; i < head->ncmds; i++){
148 if (sgp->cmd == SEGMENT_CMD) {
149 if (strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) {
153 sgp = (const segmentType *)((char *)sgp + sgp->cmdsize);
159 _hasObjcContents(const header_info *hi)
161 // Look for a __DATA,__objc* section other than __DATA,__objc_imageinfo
162 const segmentType *seg = getsegbynamefromheader(hi->mhdr, "__DATA");
165 const sectionType *sect;
167 for (i = 0; i < seg->nsects; i++) {
168 sect = ((const sectionType *)(seg+1))+i;
169 if (0 == strncmp(sect->sectname, "__objc_", 7) &&
170 0 != strncmp(sect->sectname, "__objc_imageinfo", 16))