nlist_elf32.c revision 1.1 1 1.1 cgd /* $NetBSD: nlist_elf32.c,v 1.1 1996/09/27 22:23:06 cgd Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 1.1 cgd * Copyright (c) 1989, 1993
6 1.1 cgd * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
38 1.1 cgd #if 0
39 1.1 cgd static char sccsid[] = "@(#)nlist.c 8.1 (Berkeley) 6/4/93";
40 1.1 cgd #else
41 1.1 cgd static char rcsid[] = "$NetBSD: nlist_elf32.c,v 1.1 1996/09/27 22:23:06 cgd Exp $";
42 1.1 cgd #endif
43 1.1 cgd #endif /* LIBC_SCCS and not lint */
44 1.1 cgd
45 1.1 cgd /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
46 1.1 cgd #ifndef ELFSIZE
47 1.1 cgd #define ELFSIZE 32
48 1.1 cgd #endif
49 1.1 cgd
50 1.1 cgd #include <sys/param.h>
51 1.1 cgd #include <sys/mman.h>
52 1.1 cgd #include <sys/stat.h>
53 1.1 cgd #include <sys/file.h>
54 1.1 cgd
55 1.1 cgd #include <errno.h>
56 1.1 cgd #include <stdio.h>
57 1.1 cgd #include <string.h>
58 1.1 cgd #include <unistd.h>
59 1.1 cgd #include <a.out.h> /* for 'struct nlist' declaration */
60 1.1 cgd
61 1.1 cgd #include "nlist_private.h"
62 1.1 cgd #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
63 1.1 cgd #include <sys/exec_elf.h>
64 1.1 cgd #endif
65 1.1 cgd
66 1.1 cgd #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
67 1.1 cgd (defined(NLIST_ELF64) && (ELFSIZE == 64))
68 1.1 cgd
69 1.1 cgd #define CONCAT(x,y) __CONCAT(x,y)
70 1.1 cgd #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
71 1.1 cgd #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
72 1.1 cgd #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
73 1.1 cgd #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
74 1.1 cgd
75 1.1 cgd int
76 1.1 cgd ELFNAMEEND(__fdnlist_is)(fd)
77 1.1 cgd int fd;
78 1.1 cgd {
79 1.1 cgd Elf_Ehdr ehdr;
80 1.1 cgd
81 1.1 cgd if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
82 1.1 cgd read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr))
83 1.1 cgd return (-1);
84 1.1 cgd
85 1.1 cgd if (bcmp(ehdr.e_ident, Elf_e_ident, Elf_e_siz))
86 1.1 cgd return (-1);
87 1.1 cgd
88 1.1 cgd switch (ehdr.e_machine) {
89 1.1 cgd ELFDEFNNAME(MACHDEP_ID_CASES)
90 1.1 cgd
91 1.1 cgd default:
92 1.1 cgd return (-1);
93 1.1 cgd }
94 1.1 cgd
95 1.1 cgd return (0);
96 1.1 cgd }
97 1.1 cgd
98 1.1 cgd int
99 1.1 cgd ELFNAMEEND(__fdnlist)(fd, list)
100 1.1 cgd register int fd;
101 1.1 cgd register struct nlist *list;
102 1.1 cgd {
103 1.1 cgd register struct nlist *p;
104 1.1 cgd register caddr_t strtab;
105 1.1 cgd #if (ELFSIZE == 32)
106 1.1 cgd register Elf32_Word symsize, symstrsize;
107 1.1 cgd register Elf32_Off symoff = 0, symstroff;
108 1.1 cgd #elif (ELFSIZE == 64)
109 1.1 cgd register Elf64_Word symsize, symstrsize;
110 1.1 cgd register Elf64_Off symoff = 0, symstroff;
111 1.1 cgd #endif
112 1.1 cgd register long nent, cc, i;
113 1.1 cgd Elf_Sym sbuf[1024];
114 1.1 cgd Elf_Sym *s;
115 1.1 cgd Elf_Ehdr ehdr;
116 1.1 cgd Elf_Shdr *shdr = NULL;
117 1.1 cgd Elf_Word shdr_size;
118 1.1 cgd struct stat st;
119 1.1 cgd
120 1.1 cgd /* Make sure obj is OK */
121 1.1 cgd if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
122 1.1 cgd read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) ||
123 1.1 cgd fstat(fd, &st) < 0)
124 1.1 cgd return (-1);
125 1.1 cgd
126 1.1 cgd /* calculate section header table size */
127 1.1 cgd shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
128 1.1 cgd
129 1.1 cgd /* Make sure it's not too big to mmap */
130 1.1 cgd if (shdr_size > SIZE_T_MAX) {
131 1.1 cgd errno = EFBIG;
132 1.1 cgd return (-1);
133 1.1 cgd }
134 1.1 cgd
135 1.1 cgd /* mmap section header table */
136 1.1 cgd shdr = (Elf_Shdr *)mmap(NULL, (size_t)shdr_size,
137 1.1 cgd PROT_READ, 0, fd, ehdr.e_shoff);
138 1.1 cgd if (shdr == (Elf_Shdr *)-1)
139 1.1 cgd return (-1);
140 1.1 cgd
141 1.1 cgd /*
142 1.1 cgd * Find the symbol table entry and it's corresponding
143 1.1 cgd * string table entry. Version 1.1 of the ABI states
144 1.1 cgd * that there is only one symbol table but that this
145 1.1 cgd * could change in the future.
146 1.1 cgd */
147 1.1 cgd for (i = 0; i < ehdr.e_shnum; i++) {
148 1.1 cgd if (shdr[i].sh_type == Elf_sht_symtab) {
149 1.1 cgd symoff = shdr[i].sh_offset;
150 1.1 cgd symsize = shdr[i].sh_size;
151 1.1 cgd symstroff = shdr[shdr[i].sh_link].sh_offset;
152 1.1 cgd symstrsize = shdr[shdr[i].sh_link].sh_size;
153 1.1 cgd break;
154 1.1 cgd }
155 1.1 cgd }
156 1.1 cgd
157 1.1 cgd /* Flush the section header table */
158 1.1 cgd munmap((caddr_t)shdr, shdr_size);
159 1.1 cgd
160 1.1 cgd /* Check for files too large to mmap. */
161 1.1 cgd /* XXX is this really possible? */
162 1.1 cgd if (symstrsize > SIZE_T_MAX) {
163 1.1 cgd errno = EFBIG;
164 1.1 cgd return (-1);
165 1.1 cgd }
166 1.1 cgd /*
167 1.1 cgd * Map string table into our address space. This gives us
168 1.1 cgd * an easy way to randomly access all the strings, without
169 1.1 cgd * making the memory allocation permanent as with malloc/free
170 1.1 cgd * (i.e., munmap will return it to the system).
171 1.1 cgd */
172 1.1 cgd strtab = mmap(NULL, (size_t)symstrsize, PROT_READ, 0, fd, symstroff);
173 1.1 cgd if (strtab == (char *)-1)
174 1.1 cgd return (-1);
175 1.1 cgd /*
176 1.1 cgd * clean out any left-over information for all valid entries.
177 1.1 cgd * Type and value defined to be 0 if not found; historical
178 1.1 cgd * versions cleared other and desc as well. Also figure out
179 1.1 cgd * the largest string length so don't read any more of the
180 1.1 cgd * string table than we have to.
181 1.1 cgd *
182 1.1 cgd * XXX clearing anything other than n_type and n_value violates
183 1.1 cgd * the semantics given in the man page.
184 1.1 cgd */
185 1.1 cgd nent = 0;
186 1.1 cgd for (p = list; !ISLAST(p); ++p) {
187 1.1 cgd p->n_type = 0;
188 1.1 cgd p->n_other = 0;
189 1.1 cgd p->n_desc = 0;
190 1.1 cgd p->n_value = 0;
191 1.1 cgd ++nent;
192 1.1 cgd }
193 1.1 cgd
194 1.1 cgd /* Don't process any further if object is stripped. */
195 1.1 cgd /* ELFism - dunno if stripped by looking at header */
196 1.1 cgd if (symoff == 0)
197 1.1 cgd goto done;
198 1.1 cgd
199 1.1 cgd if (lseek(fd, symoff, SEEK_SET) == -1) {
200 1.1 cgd nent = -1;
201 1.1 cgd goto done;
202 1.1 cgd }
203 1.1 cgd
204 1.1 cgd while (symsize > 0) {
205 1.1 cgd cc = MIN(symsize, sizeof(sbuf));
206 1.1 cgd if (read(fd, sbuf, cc) != cc)
207 1.1 cgd break;
208 1.1 cgd symsize -= cc;
209 1.1 cgd for (s = sbuf; cc > 0; ++s, cc -= sizeof(*s)) {
210 1.1 cgd register int soff = s->st_name;
211 1.1 cgd
212 1.1 cgd if (soff == 0)
213 1.1 cgd continue;
214 1.1 cgd
215 1.1 cgd for (p = list; !ISLAST(p); p++) {
216 1.1 cgd char *nlistname;
217 1.1 cgd
218 1.1 cgd nlistname = p->n_un.n_name;
219 1.1 cgd if (*nlistname == '_')
220 1.1 cgd nlistname++;
221 1.1 cgd
222 1.1 cgd if (!strcmp(&strtab[soff], nlistname)) {
223 1.1 cgd p->n_value = s->st_value;
224 1.1 cgd
225 1.1 cgd /* XXX - type conversion */
226 1.1 cgd /* is pretty rude. */
227 1.1 cgd switch(ELF_SYM_TYPE(s->st_info)) {
228 1.1 cgd case Elf_estt_notype:
229 1.1 cgd p->n_type = N_UNDF;
230 1.1 cgd break;
231 1.1 cgd case Elf_estt_object:
232 1.1 cgd p->n_type = N_DATA;
233 1.1 cgd break;
234 1.1 cgd case Elf_estt_func:
235 1.1 cgd p->n_type = N_TEXT;
236 1.1 cgd break;
237 1.1 cgd case Elf_estt_file:
238 1.1 cgd p->n_type = N_FN;
239 1.1 cgd break;
240 1.1 cgd }
241 1.1 cgd if (ELF_SYM_BIND(s->st_info) !=
242 1.1 cgd Elf_estb_local)
243 1.1 cgd p->n_type |= N_EXT;
244 1.1 cgd p->n_desc = 0;
245 1.1 cgd p->n_other = 0;
246 1.1 cgd if (--nent <= 0)
247 1.1 cgd break;
248 1.1 cgd }
249 1.1 cgd }
250 1.1 cgd }
251 1.1 cgd }
252 1.1 cgd done:
253 1.1 cgd munmap(strtab, symstrsize);
254 1.1 cgd
255 1.1 cgd return (nent);
256 1.1 cgd }
257 1.1 cgd
258 1.1 cgd #endif
259