1 /* $NetBSD: nlist_elf32.c,v 1.17 2003/09/19 06:24:04 itojun Exp $ */
4 * Copyright (c) 1996 Christopher G. Demetriou
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the
18 * NetBSD Project. See http://www.NetBSD.org/ for
19 * information about NetBSD.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
37 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: nlist_elf32.c,v 1.17 2003/09/19 06:24:04 itojun Exp $");
42 /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
47 #include <sys/param.h>
50 #include <sys/sysctl.h>
51 #include <sys/ioctl.h>
52 #include <sys/ksyms.h>
69 #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
70 #include <sys/exec_elf.h>
73 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
74 (defined(NLIST_ELF64) && (ELFSIZE == 64))
76 typedef struct nlist NLIST
;
77 #define _strx n_un.n_strx
78 #define _name n_un.n_name
82 warnx("%s: %s: %s", kfile, str, strerror(EFTYPE)); \
86 #define check(off, size) ((off < 0) || (off + size > mappedsize))
87 #define BAD do { rv = -1; goto out; } while (0)
88 #define BADUNMAP do { rv = -1; goto unmap; } while (0)
90 static const char *kfile
;
93 ELFNAMEEND(create_knlist
)(name
, db
)
100 char *mappedfile
, *symname
, *nsymname
, *fsymname
, *tmpcp
, *strtab
;
101 size_t mappedsize
, symnamesize
, fsymnamesize
;
103 Elf_Shdr
*shdrp
, *symshdrp
, *symstrshdrp
;
109 #elif (ELFSIZE == 64)
112 unsigned long i
, nsyms
;
113 int fd
, rv
, malloced
= 0, isksyms
;
117 /* fix compiler warnings */
123 * Open and map the whole file. If we can't open/stat it,
124 * something bad is going on so we punt.
127 if ((fd
= open(name
, O_RDONLY
, 0)) < 0) {
131 if (fstat(fd
, &st
) < 0) {
135 if (st
.st_size
> SIZE_T_MAX
)
140 * Map the file in its entirety.
142 mappedfile
= MAP_FAILED
;
143 mappedsize
= st
.st_size
;
144 isksyms
= S_ISCHR(st
.st_mode
) &&
145 strncmp(name
, _PATH_KSYMS
, sizeof(_PATH_KSYMS
)) == 0;
147 if (mappedsize
== 0) {
148 /* if it's a character device, stat returns size 0 */
152 mappedfile
= mmap(NULL
, mappedsize
, PROT_READ
,
153 MAP_PRIVATE
|MAP_FILE
, fd
, 0);
157 * If mmap failed, try to read the file instead.
159 if (mappedfile
== MAP_FAILED
) {
160 int allocsiz
, readsz
;
163 if (ioctl(fd
, KIOCGSIZE
, &allocsiz
) < 0)
165 mappedsize
= allocsiz
;
167 allocsiz
= mappedsize
;
169 if ((mappedfile
= malloc(mappedsize
)) == NULL
)
172 if ((readsz
= read(fd
, mappedfile
, mappedsize
)) < 0)
175 if (readsz
!= mappedsize
) /* Sanity */
179 * Make sure we can access the executable's header
180 * directly, and make sure the recognize the executable
183 if (check(0, sizeof *ehdrp
))
185 ehdrp
= (Elf_Ehdr
*)&mappedfile
[0];
187 if (memcmp(ehdrp
->e_ident
, ELFMAG
, SELFMAG
) != 0 ||
188 ehdrp
->e_ident
[EI_CLASS
] != ELFCLASS
)
191 switch (ehdrp
->e_machine
) {
192 ELFDEFNNAME(MACHDEP_ID_CASES
)
199 * We've recognized it as an ELF binary. From here
200 * on out, all errors are fatal.
204 * Find the symbol list and string table.
206 nshdr
= ehdrp
->e_shnum
;
207 shdr_off
= ehdrp
->e_shoff
;
208 shdr_size
= ehdrp
->e_shentsize
* nshdr
;
210 if (check(shdr_off
, shdr_size
) ||
211 (sizeof *shdrp
!= ehdrp
->e_shentsize
))
212 badfmt("bogus section header table");
213 shdrp
= (Elf_Shdr
*)&mappedfile
[shdr_off
];
215 for (i
= 0; i
< nshdr
; i
++) {
216 if (shdrp
[i
].sh_type
== SHT_SYMTAB
) {
217 symshdrp
= &shdrp
[i
];
218 symstrshdrp
= &shdrp
[shdrp
[i
].sh_link
];
222 if (symshdrp
== NULL
)
223 badfmt("no symbol section header found");
224 if (symshdrp
->sh_offset
== 0)
226 if (check(symshdrp
->sh_offset
, symshdrp
->sh_size
))
227 badfmt("bogus symbol section header");
228 if (check(symstrshdrp
->sh_offset
, symstrshdrp
->sh_size
))
229 badfmt("bogus symbol string section header");
231 symp
= (Elf_Sym
*)&mappedfile
[symshdrp
->sh_offset
];
232 nsyms
= symshdrp
->sh_size
/ sizeof(*symp
);
233 strtab
= &mappedfile
[symstrshdrp
->sh_offset
];
236 * Set up the data item, pointing to a nlist structure.
237 * which we fill in for each symbol.
239 data
.data
= (u_char
*)&nbuf
;
240 data
.size
= sizeof(nbuf
);
243 * Create a buffer (to be expanded later, if necessary)
244 * to hold symbol names after we've added underscores
248 if ((symname
= malloc(symnamesize
)) == NULL
) {
254 * Read each symbol and enter it into the database.
256 for (i
= 0; i
< nsyms
; i
++) {
259 * No symbol name; ignore this symbol.
261 if (symp
[i
].st_name
== 0)
265 * Find symbol name, copy it (with added underscore) to
266 * temporary buffer, and prepare the database key for
269 fsymname
= &strtab
[symp
[i
].st_name
];
270 fsymnamesize
= strlen(fsymname
) + 1;
271 while (symnamesize
< fsymnamesize
+ 1) {
272 if ((nsymname
= realloc(symname
, symnamesize
* 2)) == NULL
) {
279 strlcpy(symname
, "_", symnamesize
);
280 strlcat(symname
, fsymname
, symnamesize
);
283 key
.size
= strlen((char *)key
.data
);
286 * Convert the symbol information into an nlist structure,
289 nbuf
.n_value
= symp
[i
].st_value
;
290 switch (ELFDEFNNAME(ST_TYPE
)(symp
[i
].st_info
)) {
293 nbuf
.n_type
= N_UNDF
;
296 nbuf
.n_type
= N_DATA
;
299 nbuf
.n_type
= N_TEXT
;
305 if (ELFDEFNNAME(ST_BIND
)(symp
[i
].st_info
) != STB_LOCAL
)
306 nbuf
.n_type
|= N_EXT
;
307 nbuf
.n_desc
= 0; /* XXX */
308 nbuf
.n_other
= 0; /* XXX */
311 * Enter the symbol into the database.
313 if (db
->put(db
, &key
, &data
, 0)) {
314 warn("record enter");
319 * If it's the kernel version string, we've gotta keep
320 * some extra data around. Under a separate key,
321 * we enter the first line (i.e. up to the first newline,
322 * with the newline replaced by a NUL to terminate the
323 * entered string) of the version string.
325 if (strcmp((char *)key
.data
, VRS_SYM
) == 0) {
326 key
.data
= (u_char
*)VRS_KEY
;
327 key
.size
= sizeof(VRS_KEY
) - 1;
328 /* Find the version string, relative to its section */
330 /* reading from /dev/ksyms, use sysctl */
336 mib
[1] = KERN_VERSION
;
337 if (sysctl(mib
, 2, NULL
, &sz
, NULL
, 0) == -1) {
338 warn("sysctl version size");
341 if ((kv
= malloc(sz
)) == NULL
) {
342 warn("malloc version string");
345 if (sysctl(mib
, 2, kv
, &sz
, NULL
, 0) == -1) {
346 warn("sysctl version string");
351 data
.data
= strdup(&mappedfile
[nbuf
.n_value
-
352 shdrp
[symp
[i
].st_shndx
].sh_addr
+
353 shdrp
[symp
[i
].st_shndx
].sh_offset
]);
354 /* assumes newline terminates version. */
355 if ((tmpcp
= strchr(data
.data
, '\n')) != NULL
)
357 data
.size
= strlen((char *)data
.data
);
359 if (db
->put(db
, &key
, &data
, 0)) {
360 warn("record enter");
364 /* free pointer created by strdup(). */
367 /* Restore to original values */
368 data
.data
= (u_char
*)&nbuf
;
369 data
.size
= sizeof(nbuf
);
379 munmap(mappedfile
, mappedsize
);