2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 // Copyright 1988-1996 NeXT Software, Inc.
27 #import "objc-private.h"
28 #import <mach-o/ldsyms.h>
29 #import <mach-o/dyld.h>
33 #import <crt_externs.h>
35 /* prototype coming soon to <mach-o/getsect.h> */
36 extern char *getsectdatafromheader(
37 struct mach_header *mhp,
42 /* Returns an array of all the objc headers in the executable
43 * Caller is responsible for freeing.
45 headerType **_getObjcHeaders()
47 const struct mach_header **headers;
48 headers = malloc(sizeof(struct mach_header *) * 2);
49 headers[0] = (const struct mach_header *)_NSGetMachExecuteHeader();
51 return (headerType**)headers;
54 Module _getObjcModules(headerType *head, int *nmodules)
57 void *mods = getsectdatafromheader((headerType *)head,
61 *nmodules = size / sizeof(struct objc_module);
65 SEL *_getObjcMessageRefs(headerType *head, int *nmess)
68 void *refs = getsectdatafromheader ((headerType *)head,
69 SEG_OBJC, "__message_refs", &size);
70 *nmess = size / sizeof(SEL);
74 ProtocolTemplate *_getObjcProtocols(headerType *head, int *nprotos)
77 void *protos = getsectdatafromheader ((headerType *)head,
78 SEG_OBJC, "__protocol", &size);
79 *nprotos = size / sizeof(ProtocolTemplate);
80 return (ProtocolTemplate *)protos;
83 NXConstantStringTemplate *_getObjcStringObjects(headerType *head, int *nstrs)
89 Class *_getObjcClassRefs(headerType *head, int *nclasses)
92 void *classes = getsectdatafromheader ((headerType *)head,
93 SEG_OBJC, "__cls_refs", &size);
94 *nclasses = size / sizeof(Class);
95 return (Class *)classes;
98 objc_image_info *_getObjcImageInfo(headerType *head)
101 void *info = getsectdatafromheader ((headerType *)head,
102 SEG_OBJC, "__image_info", &size);
103 return (objc_image_info *)info;
106 /* returns start of all objective-c info and the size of the data */
107 void *_getObjcHeaderData(const headerType *head, unsigned *size)
109 struct segment_command *sgp;
112 sgp = (struct segment_command *) ((char *)head + sizeof(headerType));
113 for(i = 0; i < ((headerType *)head)->ncmds; i++){
114 if(sgp->cmd == LC_SEGMENT)
115 if(strncmp(sgp->segname, "__OBJC", sizeof(sgp->segname)) == 0) {
116 *size = sgp->filesize;
119 sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
125 static const headerType *_getExecHeader (void)
127 return (const struct mach_header *)_NSGetMachExecuteHeader();
130 const char *_getObjcHeaderName(headerType *header)
132 const headerType *execHeader;
133 const struct fvmlib_command *libCmd, *endOfCmds;
135 extern char ***_NSGetArgv();
136 argv = *_NSGetArgv();
138 if (header && ((headerType *)header)->filetype == MH_FVMLIB) {
139 execHeader = _getExecHeader();
140 for (libCmd = (const struct fvmlib_command *)(execHeader + 1),
141 endOfCmds = ((void *)libCmd) + execHeader->sizeofcmds;
142 libCmd < endOfCmds; ((void *)libCmd) += libCmd->cmdsize) {
143 if ((libCmd->cmd == LC_LOADFVMLIB) && (libCmd->fvmlib.header_addr
144 == (unsigned long)header)) {
145 return (char *)libCmd
146 + libCmd->fvmlib.name.offset;
151 unsigned long i, n = _dyld_image_count();
152 for( i = 0; i < n ; i++ ) {
153 if ( _dyld_get_image_header(i) == header )
154 return _dyld_get_image_name(i);