]>
Commit | Line | Data |
---|---|---|
e9ce8d39 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
734aad71 A |
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 | |
11 | * file. | |
12 | * | |
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 | |
e9ce8d39 A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
734aad71 A |
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. | |
e9ce8d39 A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | /* | |
224c7076 A |
24 | * Copyright (c) 1989, 1993 |
25 | * The Regents of the University of California. All rights reserved. | |
e9ce8d39 A |
26 | * |
27 | * Redistribution and use in source and binary forms, with or without | |
28 | * modification, are permitted provided that the following conditions | |
29 | * are met: | |
30 | * 1. Redistributions of source code must retain the above copyright | |
31 | * notice, this list of conditions and the following disclaimer. | |
32 | * 2. Redistributions in binary form must reproduce the above copyright | |
33 | * notice, this list of conditions and the following disclaimer in the | |
34 | * documentation and/or other materials provided with the distribution. | |
35 | * 3. All advertising materials mentioning features or use of this software | |
36 | * must display the following acknowledgement: | |
37 | * This product includes software developed by the University of | |
38 | * California, Berkeley and its contributors. | |
39 | * 4. Neither the name of the University nor the names of its contributors | |
40 | * may be used to endorse or promote products derived from this software | |
41 | * without specific prior written permission. | |
42 | * | |
43 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
44 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
45 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
47 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
49 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
51 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
52 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
53 | * SUCH DAMAGE. | |
54 | */ | |
55 | ||
3d9156a7 A |
56 | /* temporarily comment this file out for LP64, until code can be modified */ |
57 | #ifndef __LP64__ | |
e9ce8d39 A |
58 | |
59 | #include <stdlib.h> | |
60 | #include <sys/types.h> | |
61 | #include <sys/file.h> | |
62 | #include <fcntl.h> | |
63 | #include <string.h> | |
64 | #include <unistd.h> | |
65 | ||
66 | /* Stuff lifted from <a.out.h> and <sys/exec.h> since they are gone */ | |
67 | /* | |
68 | * Header prepended to each a.out file. | |
69 | */ | |
70 | struct exec { | |
71 | unsigned short a_machtype; /* machine type */ | |
72 | unsigned short a_magic; /* magic number */ | |
73 | unsigned long a_text; /* size of text segment */ | |
74 | unsigned long a_data; /* size of initialized data */ | |
75 | unsigned long a_bss; /* size of uninitialized data */ | |
76 | unsigned long a_syms; /* size of symbol table */ | |
77 | unsigned long a_entry; /* entry point */ | |
78 | unsigned long a_trsize; /* size of text relocation */ | |
79 | unsigned long a_drsize; /* size of data relocation */ | |
80 | }; | |
81 | ||
82 | #define OMAGIC 0407 /* old impure format */ | |
83 | #define NMAGIC 0410 /* read-only text */ | |
84 | #define ZMAGIC 0413 /* demand load format */ | |
85 | ||
86 | #define N_BADMAG(x) \ | |
87 | (((x).a_magic)!=OMAGIC && ((x).a_magic)!=NMAGIC && ((x).a_magic)!=ZMAGIC) | |
88 | #define N_TXTOFF(x) \ | |
89 | ((x).a_magic==ZMAGIC ? 0 : sizeof (struct exec)) | |
90 | #define N_SYMOFF(x) \ | |
91 | (N_TXTOFF(x) + (x).a_text+(x).a_data + (x).a_trsize+(x).a_drsize) | |
92 | ||
93 | #include <mach/mach.h> | |
94 | #include <mach-o/nlist.h> | |
95 | #include <stdio.h> | |
96 | #include <mach-o/loader.h> | |
97 | #include <mach-o/fat.h> | |
98 | ||
99 | /* | |
100 | * CPUSUBTYPE_SUPPORT should be changed to non-zero once the | |
101 | * cpusubtype_* routines are available in libc. | |
102 | */ | |
103 | #define CPUSUBTYPE_SUPPORT 0 | |
104 | ||
9385eb3d A |
105 | int __fdnlist(int fd, struct nlist *list); |
106 | ||
e9ce8d39 A |
107 | /* |
108 | * nlist - retreive attributes from name list (string table version) | |
109 | */ | |
110 | ||
111 | int | |
112 | nlist(name, list) | |
113 | const char *name; | |
114 | struct nlist *list; | |
115 | { | |
116 | int fd, n; | |
117 | ||
118 | fd = open(name, O_RDONLY, 0); | |
119 | if (fd < 0) | |
120 | return (-1); | |
121 | n = __fdnlist(fd, list); | |
122 | (void)close(fd); | |
123 | return (n); | |
124 | } | |
125 | ||
126 | /* Note: __fdnlist() is called from kvm_nlist in libkvm's kvm.c */ | |
127 | ||
128 | int | |
129 | __fdnlist(fd, list) | |
130 | int fd; | |
131 | struct nlist *list; | |
132 | { | |
133 | register struct nlist *p, *q; | |
134 | register char *s1, *s2; | |
9385eb3d | 135 | register int n, m; |
e9ce8d39 A |
136 | int maxlen, nreq; |
137 | off_t sa; /* symbol address */ | |
138 | off_t ss; /* start of strings */ | |
139 | struct exec buf; | |
140 | struct nlist space[BUFSIZ/sizeof (struct nlist)]; | |
141 | unsigned arch_offset = 0; | |
142 | ||
143 | maxlen = 0; | |
144 | for (q = list, nreq = 0; q->n_un.n_name && q->n_un.n_name[0]; q++, nreq++) { | |
145 | q->n_type = 0; | |
146 | q->n_value = 0; | |
147 | q->n_desc = 0; | |
148 | q->n_sect = 0; | |
149 | n = strlen(q->n_un.n_name); | |
150 | if (n > maxlen) | |
151 | maxlen = n; | |
152 | } | |
153 | if (read(fd, (char *)&buf, sizeof(buf)) != sizeof(buf) || | |
154 | (N_BADMAG(buf) && *((long *)&buf) != MH_MAGIC && | |
155 | NXSwapBigLongToHost(*((long *)&buf)) != FAT_MAGIC)) { | |
156 | return (-1); | |
157 | } | |
158 | ||
159 | /* Deal with fat file if necessary */ | |
160 | if (NXSwapBigLongToHost(*((long *)&buf)) == FAT_MAGIC) { | |
161 | struct host_basic_info hbi; | |
162 | struct fat_header fh; | |
163 | struct fat_arch *fat_archs, *fap; | |
164 | unsigned i; | |
5b2abdfb | 165 | host_t host; |
e9ce8d39 A |
166 | |
167 | /* Get our host info */ | |
5b2abdfb | 168 | host = mach_host_self(); |
e9ce8d39 | 169 | i = HOST_BASIC_INFO_COUNT; |
5b2abdfb | 170 | if (host_info(host, HOST_BASIC_INFO, |
e9ce8d39 A |
171 | (host_info_t)(&hbi), &i) != KERN_SUCCESS) { |
172 | return (-1); | |
173 | } | |
5b2abdfb | 174 | mach_port_deallocate(mach_task_self(), host); |
e9ce8d39 A |
175 | |
176 | /* Read in the fat header */ | |
177 | lseek(fd, 0, SEEK_SET); | |
178 | if (read(fd, (char *)&fh, sizeof(fh)) != sizeof(fh)) { | |
179 | return (-1); | |
180 | } | |
181 | ||
182 | /* Convert fat_narchs to host byte order */ | |
183 | fh.nfat_arch = NXSwapBigLongToHost(fh.nfat_arch); | |
184 | ||
185 | /* Read in the fat archs */ | |
186 | fat_archs = (struct fat_arch *)malloc(fh.nfat_arch * | |
187 | sizeof(struct fat_arch)); | |
188 | if (fat_archs == NULL) { | |
189 | return (-1); | |
190 | } | |
191 | if (read(fd, (char *)fat_archs, | |
192 | sizeof(struct fat_arch) * fh.nfat_arch) != | |
193 | sizeof(struct fat_arch) * fh.nfat_arch) { | |
194 | free(fat_archs); | |
195 | return (-1); | |
196 | } | |
197 | ||
198 | /* | |
199 | * Convert archs to host byte ordering (a constraint of | |
200 | * cpusubtype_getbestarch() | |
201 | */ | |
202 | for (i = 0; i < fh.nfat_arch; i++) { | |
203 | fat_archs[i].cputype = | |
204 | NXSwapBigLongToHost(fat_archs[i].cputype); | |
205 | fat_archs[i].cpusubtype = | |
206 | NXSwapBigLongToHost(fat_archs[i].cpusubtype); | |
207 | fat_archs[i].offset = | |
208 | NXSwapBigLongToHost(fat_archs[i].offset); | |
209 | fat_archs[i].size = | |
210 | NXSwapBigLongToHost(fat_archs[i].size); | |
211 | fat_archs[i].align = | |
212 | NXSwapBigLongToHost(fat_archs[i].align); | |
213 | } | |
214 | ||
215 | #if CPUSUBTYPE_SUPPORT | |
216 | fap = cpusubtype_getbestarch(hbi.cpu_type, hbi.cpu_subtype, | |
217 | fat_archs, fh.nfat_arch); | |
9385eb3d | 218 | #else |
e9ce8d39 A |
219 | #warning Use the cpusubtype functions!!! |
220 | fap = NULL; | |
221 | for (i = 0; i < fh.nfat_arch; i++) { | |
222 | if (fat_archs[i].cputype == hbi.cpu_type) { | |
223 | fap = &fat_archs[i]; | |
224 | break; | |
225 | } | |
226 | } | |
9385eb3d | 227 | #endif /* CPUSUBTYPE_SUPPORT */ |
e9ce8d39 A |
228 | if (!fap) { |
229 | free(fat_archs); | |
230 | return (-1); | |
231 | } | |
232 | arch_offset = fap->offset; | |
233 | free(fat_archs); | |
234 | ||
235 | /* Read in the beginning of the architecture-specific file */ | |
236 | lseek(fd, arch_offset, SEEK_SET); | |
237 | if (read(fd, (char *)&buf, sizeof(buf)) != sizeof(buf)) { | |
238 | return (-1); | |
239 | } | |
240 | } | |
241 | ||
242 | if (*((long *)&buf) == MH_MAGIC) { | |
243 | struct mach_header mh; | |
244 | struct load_command *load_commands, *lcp; | |
245 | struct symtab_command *stp; | |
246 | long i; | |
247 | ||
248 | lseek(fd, arch_offset, SEEK_SET); | |
249 | if (read(fd, (char *)&mh, sizeof(mh)) != sizeof(mh)) { | |
250 | return (-1); | |
251 | } | |
252 | load_commands = (struct load_command *)malloc(mh.sizeofcmds); | |
253 | if (load_commands == NULL) { | |
254 | return (-1); | |
255 | } | |
256 | if (read(fd, (char *)load_commands, mh.sizeofcmds) != | |
257 | mh.sizeofcmds) { | |
258 | free(load_commands); | |
259 | return (-1); | |
260 | } | |
261 | stp = NULL; | |
262 | lcp = load_commands; | |
263 | for (i = 0; i < mh.ncmds; i++) { | |
264 | if (lcp->cmdsize % sizeof(long) != 0 || | |
265 | lcp->cmdsize <= 0 || | |
266 | (char *)lcp + lcp->cmdsize > | |
267 | (char *)load_commands + mh.sizeofcmds) { | |
268 | free(load_commands); | |
269 | return (-1); | |
270 | } | |
271 | if (lcp->cmd == LC_SYMTAB) { | |
272 | if (lcp->cmdsize != | |
273 | sizeof(struct symtab_command)) { | |
274 | free(load_commands); | |
275 | return (-1); | |
276 | } | |
277 | stp = (struct symtab_command *)lcp; | |
278 | break; | |
279 | } | |
280 | lcp = (struct load_command *) | |
281 | ((char *)lcp + lcp->cmdsize); | |
282 | } | |
283 | if (stp == NULL) { | |
284 | free(load_commands); | |
285 | return (-1); | |
286 | } | |
287 | sa = stp->symoff + arch_offset; | |
288 | ss = stp->stroff + arch_offset; | |
289 | n = stp->nsyms * sizeof(struct nlist); | |
290 | free(load_commands); | |
291 | } | |
292 | else { | |
293 | sa = N_SYMOFF(buf) + arch_offset; | |
294 | ss = sa + buf.a_syms + arch_offset; | |
295 | n = buf.a_syms; | |
296 | } | |
297 | ||
298 | lseek(fd, sa, SEEK_SET); | |
299 | while (n) { | |
300 | long savpos; | |
301 | ||
302 | m = sizeof (space); | |
303 | if (n < m) | |
304 | m = n; | |
305 | if (read(fd, (char *)space, m) != m) | |
306 | break; | |
307 | n -= m; | |
308 | savpos = lseek(fd, 0, SEEK_CUR); | |
309 | for (q = space; (m -= sizeof(struct nlist)) >= 0; q++) { | |
310 | char nambuf[BUFSIZ]; | |
311 | ||
312 | if (q->n_un.n_strx == 0 || q->n_type & N_STAB) | |
313 | continue; | |
314 | lseek(fd, ss+q->n_un.n_strx, SEEK_SET); | |
315 | read(fd, nambuf, maxlen+1); | |
316 | for (p = list; p->n_un.n_name && p->n_un.n_name[0]; p++) { | |
317 | s1 = p->n_un.n_name; | |
318 | s2 = nambuf; | |
319 | while (*s1) { | |
320 | if (*s1++ != *s2++) | |
321 | goto cont; | |
322 | } | |
323 | if (*s2) | |
324 | goto cont; | |
325 | p->n_value = q->n_value; | |
326 | p->n_type = q->n_type; | |
327 | p->n_desc = q->n_desc; | |
328 | p->n_sect = q->n_sect; | |
329 | if (--nreq == 0) | |
330 | return (nreq); | |
331 | break; | |
332 | cont: ; | |
333 | } | |
334 | } | |
335 | lseek(fd, savpos, SEEK_SET); | |
336 | } | |
337 | return (nreq); | |
338 | } | |
3d9156a7 A |
339 | |
340 | #endif /* !__LP64__ */ |