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