]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-file.m
objc4-235.tar.gz
[apple/objc4.git] / runtime / objc-file.m
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 // Copyright 1988-1996 NeXT Software, Inc.
26
27 #import "objc-private.h"
28 #import <mach-o/ldsyms.h>
29 #import <mach-o/dyld.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #import <crt_externs.h>
34
35 /* prototype coming soon to <mach-o/getsect.h> */
36 extern char *getsectdatafromheader(
37 struct mach_header *mhp,
38 char *segname,
39 char *sectname,
40 int *size);
41
42 /* Returns an array of all the objc headers in the executable
43 * Caller is responsible for freeing.
44 */
45 headerType **_getObjcHeaders()
46 {
47 const struct mach_header **headers;
48 headers = malloc(sizeof(struct mach_header *) * 2);
49 headers[0] = (const struct mach_header *)_NSGetMachExecuteHeader();
50 headers[1] = 0;
51 return (headerType**)headers;
52 }
53
54 Module _getObjcModules(headerType *head, int *nmodules)
55 {
56 unsigned size;
57 void *mods = getsectdatafromheader((headerType *)head,
58 SEG_OBJC,
59 SECT_OBJC_MODULES,
60 &size);
61 *nmodules = size / sizeof(struct objc_module);
62 return (Module)mods;
63 }
64
65 SEL *_getObjcMessageRefs(headerType *head, int *nmess)
66 {
67 unsigned size;
68 void *refs = getsectdatafromheader ((headerType *)head,
69 SEG_OBJC, "__message_refs", &size);
70 *nmess = size / sizeof(SEL);
71 return (SEL *)refs;
72 }
73
74 ProtocolTemplate *_getObjcProtocols(headerType *head, int *nprotos)
75 {
76 unsigned size;
77 void *protos = getsectdatafromheader ((headerType *)head,
78 SEG_OBJC, "__protocol", &size);
79 *nprotos = size / sizeof(ProtocolTemplate);
80 return (ProtocolTemplate *)protos;
81 }
82
83 NXConstantStringTemplate *_getObjcStringObjects(headerType *head, int *nstrs)
84 {
85 *nstrs = 0;
86 return NULL;
87 }
88
89 Class *_getObjcClassRefs(headerType *head, int *nclasses)
90 {
91 unsigned size;
92 void *classes = getsectdatafromheader ((headerType *)head,
93 SEG_OBJC, "__cls_refs", &size);
94 *nclasses = size / sizeof(Class);
95 return (Class *)classes;
96 }
97
98 objc_image_info *_getObjcImageInfo(headerType *head)
99 {
100 unsigned size;
101 void *info = getsectdatafromheader ((headerType *)head,
102 SEG_OBJC, "__image_info", &size);
103 return (objc_image_info *)info;
104 }
105
106 /* returns start of all objective-c info and the size of the data */
107 void *_getObjcHeaderData(const headerType *head, unsigned *size)
108 {
109 struct segment_command *sgp;
110 unsigned long i;
111
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;
117 return (void*)sgp;
118 }
119 sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
120 }
121 *size = 0;
122 return nil;
123 }
124
125 static const headerType *_getExecHeader (void)
126 {
127 return (const struct mach_header *)_NSGetMachExecuteHeader();
128 }
129
130 const char *_getObjcHeaderName(headerType *header)
131 {
132 const headerType *execHeader;
133 const struct fvmlib_command *libCmd, *endOfCmds;
134 char **argv;
135 extern char ***_NSGetArgv();
136 argv = *_NSGetArgv();
137
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;
147 }
148 }
149 return NULL;
150 } else {
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);
155 }
156 return argv[0];
157 }
158 }
159