2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 // Copyright 1988-1996 NeXT Software, Inc.
26 #import "objc-private.h"
27 #import <mach-o/ldsyms.h>
28 #import <mach-o/dyld.h>
32 #import <crt_externs.h>
34 /* prototype coming soon to <mach-o/getsect.h> */
35 extern char *getsectdatafromheader(
36 struct mach_header *mhp,
41 /* Returns an array of all the objc headers in the executable
42 * Caller is responsible for freeing.
44 headerType **_getObjcHeaders()
46 const struct mach_header **headers;
47 headers = malloc(sizeof(struct mach_header *) * 2);
48 headers[0] = (const struct mach_header *)_NSGetMachExecuteHeader();
50 return (headerType**)headers;
53 Module _getObjcModules(headerType *head, int *nmodules)
56 void *mods = getsectdatafromheader((headerType *)head,
60 *nmodules = size / sizeof(struct objc_module);
64 SEL *_getObjcMessageRefs(headerType *head, int *nmess)
67 void *refs = getsectdatafromheader ((headerType *)head,
68 SEG_OBJC, "__message_refs", &size);
69 *nmess = size / sizeof(SEL);
73 ProtocolTemplate *_getObjcProtocols(headerType *head, int *nprotos)
76 void *protos = getsectdatafromheader ((headerType *)head,
77 SEG_OBJC, "__protocol", &size);
78 *nprotos = size / sizeof(ProtocolTemplate);
79 return (ProtocolTemplate *)protos;
82 NXConstantStringTemplate *_getObjcStringObjects(headerType *head, int *nstrs)
88 Class *_getObjcClassRefs(headerType *head, int *nclasses)
91 void *classes = getsectdatafromheader ((headerType *)head,
92 SEG_OBJC, "__cls_refs", &size);
93 *nclasses = size / sizeof(Class);
94 return (Class *)classes;
97 /* returns start of all objective-c info and the size of the data */
98 void *_getObjcHeaderData(headerType *head, unsigned *size)
100 struct segment_command *sgp;
103 sgp = (struct segment_command *) ((char *)head + sizeof(headerType));
104 for(i = 0; i < ((headerType *)head)->ncmds; i++){
105 if(sgp->cmd == LC_SEGMENT)
106 if(strncmp(sgp->segname, "__OBJC", sizeof(sgp->segname)) == 0) {
107 *size = sgp->filesize;
110 sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
116 static const headerType *_getExecHeader (void)
118 return (const struct mach_header *)_NSGetMachExecuteHeader();
121 const char *_getObjcHeaderName(headerType *header)
123 const headerType *execHeader;
124 const struct fvmlib_command *libCmd, *endOfCmds;
126 extern char ***_NSGetArgv();
127 argv = *_NSGetArgv();
129 if (header && ((headerType *)header)->filetype == MH_FVMLIB) {
130 execHeader = _getExecHeader();
131 for (libCmd = (const struct fvmlib_command *)(execHeader + 1),
132 endOfCmds = ((void *)libCmd) + execHeader->sizeofcmds;
133 libCmd < endOfCmds; ((void *)libCmd) += libCmd->cmdsize) {
134 if ((libCmd->cmd == LC_LOADFVMLIB) && (libCmd->fvmlib.header_addr
135 == (unsigned long)header)) {
136 return (char *)libCmd
137 + libCmd->fvmlib.name.offset;
142 unsigned long i, n = _dyld_image_count();
143 for( i = 0; i < n ; i++ ) {
144 if ( _dyld_get_image_header(i) == header )
145 return _dyld_get_image_name(i);