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