1 1.19 joerg /* $NetBSD: nlist_elf32.c,v 1.19 2010/08/28 21:30:03 joerg Exp $ */ 2 1.1 cgd 3 1.1 cgd /* 4 1.12 cgd * Copyright (c) 1996 Christopher G. Demetriou 5 1.12 cgd * All rights reserved. 6 1.12 cgd * 7 1.1 cgd * Redistribution and use in source and binary forms, with or without 8 1.1 cgd * modification, are permitted provided that the following conditions 9 1.1 cgd * are met: 10 1.1 cgd * 1. Redistributions of source code must retain the above copyright 11 1.1 cgd * notice, this list of conditions and the following disclaimer. 12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 cgd * notice, this list of conditions and the following disclaimer in the 14 1.1 cgd * documentation and/or other materials provided with the distribution. 15 1.1 cgd * 3. All advertising materials mentioning features or use of this software 16 1.1 cgd * must display the following acknowledgement: 17 1.12 cgd * This product includes software developed for the 18 1.18 grant * NetBSD Project. See http://www.NetBSD.org/ for 19 1.12 cgd * information about NetBSD. 20 1.1 cgd * 4. The name of the author may not be used to endorse or promote products 21 1.12 cgd * derived from this software without specific prior written permission. 22 1.12 cgd * 23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 1.12 cgd * 34 1.12 cgd * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>> 35 1.1 cgd */ 36 1.1 cgd 37 1.3 lukem #include <sys/cdefs.h> 38 1.1 cgd #ifndef lint 39 1.19 joerg __RCSID("$NetBSD: nlist_elf32.c,v 1.19 2010/08/28 21:30:03 joerg Exp $"); 40 1.1 cgd #endif /* not lint */ 41 1.1 cgd 42 1.1 cgd /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */ 43 1.1 cgd #ifndef ELFSIZE 44 1.1 cgd #define ELFSIZE 32 45 1.1 cgd #endif 46 1.1 cgd 47 1.1 cgd #include <sys/param.h> 48 1.1 cgd #include <sys/mman.h> 49 1.1 cgd #include <sys/stat.h> 50 1.15 ragge #include <sys/sysctl.h> 51 1.15 ragge #include <sys/ioctl.h> 52 1.15 ragge #include <sys/ksyms.h> 53 1.1 cgd 54 1.1 cgd #include <a.out.h> 55 1.1 cgd #include <db.h> 56 1.1 cgd #include <err.h> 57 1.1 cgd #include <errno.h> 58 1.1 cgd #include <fcntl.h> 59 1.1 cgd #include <kvm.h> 60 1.1 cgd #include <limits.h> 61 1.15 ragge #include <paths.h> 62 1.1 cgd #include <stdio.h> 63 1.1 cgd #include <stdlib.h> 64 1.1 cgd #include <string.h> 65 1.1 cgd #include <unistd.h> 66 1.1 cgd 67 1.1 cgd #include "extern.h" 68 1.1 cgd 69 1.1 cgd #if defined(NLIST_ELF32) || defined(NLIST_ELF64) 70 1.1 cgd #include <sys/exec_elf.h> 71 1.1 cgd #endif 72 1.1 cgd 73 1.1 cgd #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \ 74 1.1 cgd (defined(NLIST_ELF64) && (ELFSIZE == 64)) 75 1.1 cgd 76 1.1 cgd typedef struct nlist NLIST; 77 1.1 cgd #define _strx n_un.n_strx 78 1.1 cgd #define _name n_un.n_name 79 1.1 cgd 80 1.1 cgd #define badfmt(str) \ 81 1.1 cgd do { \ 82 1.1 cgd warnx("%s: %s: %s", kfile, str, strerror(EFTYPE)); \ 83 1.1 cgd punt(); \ 84 1.1 cgd } while (0) 85 1.1 cgd 86 1.1 cgd #define check(off, size) ((off < 0) || (off + size > mappedsize)) 87 1.1 cgd #define BAD do { rv = -1; goto out; } while (0) 88 1.1 cgd #define BADUNMAP do { rv = -1; goto unmap; } while (0) 89 1.1 cgd 90 1.1 cgd static const char *kfile; 91 1.1 cgd 92 1.1 cgd int 93 1.1 cgd ELFNAMEEND(create_knlist)(name, db) 94 1.1 cgd const char *name; 95 1.1 cgd DB *db; 96 1.1 cgd { 97 1.1 cgd struct stat st; 98 1.1 cgd struct nlist nbuf; 99 1.1 cgd DBT key, data; 100 1.17 itojun char *mappedfile, *symname, *nsymname, *fsymname, *tmpcp, *strtab; 101 1.1 cgd size_t mappedsize, symnamesize, fsymnamesize; 102 1.1 cgd Elf_Ehdr *ehdrp; 103 1.1 cgd Elf_Shdr *shdrp, *symshdrp, *symstrshdrp; 104 1.1 cgd Elf_Sym *symp; 105 1.2 cgd Elf_Off shdr_off; 106 1.2 cgd Elf_Word shdr_size; 107 1.1 cgd #if (ELFSIZE == 32) 108 1.1 cgd Elf32_Half nshdr; 109 1.1 cgd #elif (ELFSIZE == 64) 110 1.19 joerg Elf64_Word nshdr; 111 1.1 cgd #endif 112 1.1 cgd unsigned long i, nsyms; 113 1.15 ragge int fd, rv, malloced = 0, isksyms; 114 1.1 cgd 115 1.1 cgd rv = -1; 116 1.5 enami #ifdef __GNUC__ 117 1.5 enami /* fix compiler warnings */ 118 1.5 enami symshdrp = NULL; 119 1.5 enami symstrshdrp = NULL; 120 1.5 enami #endif 121 1.1 cgd 122 1.1 cgd /* 123 1.1 cgd * Open and map the whole file. If we can't open/stat it, 124 1.1 cgd * something bad is going on so we punt. 125 1.1 cgd */ 126 1.1 cgd kfile = name; 127 1.1 cgd if ((fd = open(name, O_RDONLY, 0)) < 0) { 128 1.1 cgd warn("%s", kfile); 129 1.1 cgd punt(); 130 1.1 cgd } 131 1.1 cgd if (fstat(fd, &st) < 0) { 132 1.1 cgd warn("%s", kfile); 133 1.1 cgd punt(); 134 1.1 cgd } 135 1.1 cgd if (st.st_size > SIZE_T_MAX) 136 1.1 cgd BAD; 137 1.1 cgd 138 1.15 ragge 139 1.1 cgd /* 140 1.1 cgd * Map the file in its entirety. 141 1.1 cgd */ 142 1.15 ragge mappedfile = MAP_FAILED; 143 1.1 cgd mappedsize = st.st_size; 144 1.15 ragge isksyms = S_ISCHR(st.st_mode) && 145 1.15 ragge strncmp(name, _PATH_KSYMS, sizeof(_PATH_KSYMS)) == 0; 146 1.15 ragge 147 1.15 ragge if (mappedsize == 0) { 148 1.15 ragge /* if it's a character device, stat returns size 0 */ 149 1.15 ragge if (!isksyms) 150 1.15 ragge BAD; 151 1.15 ragge } else { 152 1.15 ragge mappedfile = mmap(NULL, mappedsize, PROT_READ, 153 1.15 ragge MAP_PRIVATE|MAP_FILE, fd, 0); 154 1.15 ragge } 155 1.15 ragge 156 1.15 ragge /* 157 1.15 ragge * If mmap failed, try to read the file instead. 158 1.15 ragge */ 159 1.15 ragge if (mappedfile == MAP_FAILED) { 160 1.15 ragge int allocsiz, readsz; 161 1.15 ragge 162 1.15 ragge if (isksyms != 0) { 163 1.15 ragge if (ioctl(fd, KIOCGSIZE, &allocsiz) < 0) 164 1.15 ragge BAD; 165 1.15 ragge mappedsize = allocsiz; 166 1.15 ragge } else 167 1.15 ragge allocsiz = mappedsize; 168 1.15 ragge 169 1.15 ragge if ((mappedfile = malloc(mappedsize)) == NULL) 170 1.15 ragge BAD; 171 1.15 ragge malloced = 1; 172 1.15 ragge if ((readsz = read(fd, mappedfile, mappedsize)) < 0) 173 1.15 ragge BADUNMAP; 174 1.1 cgd 175 1.15 ragge if (readsz != mappedsize) /* Sanity */ 176 1.15 ragge BADUNMAP; 177 1.15 ragge } 178 1.1 cgd /* 179 1.1 cgd * Make sure we can access the executable's header 180 1.1 cgd * directly, and make sure the recognize the executable 181 1.1 cgd * as an ELF binary. 182 1.1 cgd */ 183 1.1 cgd if (check(0, sizeof *ehdrp)) 184 1.1 cgd BADUNMAP; 185 1.1 cgd ehdrp = (Elf_Ehdr *)&mappedfile[0]; 186 1.1 cgd 187 1.9 kleink if (memcmp(ehdrp->e_ident, ELFMAG, SELFMAG) != 0 || 188 1.9 kleink ehdrp->e_ident[EI_CLASS] != ELFCLASS) 189 1.1 cgd BADUNMAP; 190 1.1 cgd 191 1.1 cgd switch (ehdrp->e_machine) { 192 1.1 cgd ELFDEFNNAME(MACHDEP_ID_CASES) 193 1.1 cgd 194 1.1 cgd default: 195 1.1 cgd BADUNMAP; 196 1.1 cgd } 197 1.1 cgd 198 1.1 cgd /* 199 1.1 cgd * We've recognized it as an ELF binary. From here 200 1.1 cgd * on out, all errors are fatal. 201 1.1 cgd */ 202 1.1 cgd 203 1.1 cgd /* 204 1.1 cgd * Find the symbol list and string table. 205 1.1 cgd */ 206 1.1 cgd nshdr = ehdrp->e_shnum; 207 1.1 cgd shdr_off = ehdrp->e_shoff; 208 1.1 cgd shdr_size = ehdrp->e_shentsize * nshdr; 209 1.1 cgd 210 1.1 cgd if (check(shdr_off, shdr_size) || 211 1.1 cgd (sizeof *shdrp != ehdrp->e_shentsize)) 212 1.1 cgd badfmt("bogus section header table"); 213 1.1 cgd shdrp = (Elf_Shdr *)&mappedfile[shdr_off]; 214 1.1 cgd 215 1.1 cgd for (i = 0; i < nshdr; i++) { 216 1.9 kleink if (shdrp[i].sh_type == SHT_SYMTAB) { 217 1.1 cgd symshdrp = &shdrp[i]; 218 1.1 cgd symstrshdrp = &shdrp[shdrp[i].sh_link]; 219 1.1 cgd } 220 1.1 cgd } 221 1.1 cgd 222 1.13 agc if (symshdrp == NULL) 223 1.13 agc badfmt("no symbol section header found"); 224 1.1 cgd if (symshdrp->sh_offset == 0) 225 1.1 cgd badfmt("stripped"); 226 1.1 cgd if (check(symshdrp->sh_offset, symshdrp->sh_size)) 227 1.1 cgd badfmt("bogus symbol section header"); 228 1.1 cgd if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size)) 229 1.1 cgd badfmt("bogus symbol string section header"); 230 1.1 cgd 231 1.1 cgd symp = (Elf_Sym *)&mappedfile[symshdrp->sh_offset]; 232 1.1 cgd nsyms = symshdrp->sh_size / sizeof(*symp); 233 1.1 cgd strtab = &mappedfile[symstrshdrp->sh_offset]; 234 1.1 cgd 235 1.1 cgd /* 236 1.1 cgd * Set up the data item, pointing to a nlist structure. 237 1.1 cgd * which we fill in for each symbol. 238 1.1 cgd */ 239 1.1 cgd data.data = (u_char *)&nbuf; 240 1.1 cgd data.size = sizeof(nbuf); 241 1.1 cgd 242 1.1 cgd /* 243 1.1 cgd * Create a buffer (to be expanded later, if necessary) 244 1.1 cgd * to hold symbol names after we've added underscores 245 1.1 cgd * to them. 246 1.1 cgd */ 247 1.1 cgd symnamesize = 1024; 248 1.1 cgd if ((symname = malloc(symnamesize)) == NULL) { 249 1.1 cgd warn("malloc"); 250 1.1 cgd punt(); 251 1.1 cgd } 252 1.1 cgd 253 1.1 cgd /* 254 1.1 cgd * Read each symbol and enter it into the database. 255 1.1 cgd */ 256 1.1 cgd for (i = 0; i < nsyms; i++) { 257 1.1 cgd 258 1.1 cgd /* 259 1.1 cgd * No symbol name; ignore this symbol. 260 1.1 cgd */ 261 1.1 cgd if (symp[i].st_name == 0) 262 1.1 cgd continue; 263 1.1 cgd 264 1.1 cgd /* 265 1.1 cgd * Find symbol name, copy it (with added underscore) to 266 1.1 cgd * temporary buffer, and prepare the database key for 267 1.1 cgd * insertion. 268 1.1 cgd */ 269 1.1 cgd fsymname = &strtab[symp[i].st_name]; 270 1.1 cgd fsymnamesize = strlen(fsymname) + 1; 271 1.1 cgd while (symnamesize < fsymnamesize + 1) { 272 1.17 itojun if ((nsymname = realloc(symname, symnamesize * 2)) == NULL) { 273 1.1 cgd warn("malloc"); 274 1.1 cgd punt(); 275 1.1 cgd } 276 1.17 itojun symname = nsymname; 277 1.17 itojun symnamesize *= 2; 278 1.1 cgd } 279 1.16 itojun strlcpy(symname, "_", symnamesize); 280 1.16 itojun strlcat(symname, fsymname, symnamesize); 281 1.1 cgd 282 1.1 cgd key.data = symname; 283 1.1 cgd key.size = strlen((char *)key.data); 284 1.1 cgd 285 1.1 cgd /* 286 1.1 cgd * Convert the symbol information into an nlist structure, 287 1.1 cgd * as best we can. 288 1.1 cgd */ 289 1.1 cgd nbuf.n_value = symp[i].st_value; 290 1.9 kleink switch (ELFDEFNNAME(ST_TYPE)(symp[i].st_info)) { 291 1.1 cgd default: 292 1.9 kleink case STT_NOTYPE: 293 1.1 cgd nbuf.n_type = N_UNDF; 294 1.1 cgd break; 295 1.9 kleink case STT_OBJECT: 296 1.1 cgd nbuf.n_type = N_DATA; 297 1.1 cgd break; 298 1.9 kleink case STT_FUNC: 299 1.1 cgd nbuf.n_type = N_TEXT; 300 1.1 cgd break; 301 1.9 kleink case STT_FILE: 302 1.1 cgd nbuf.n_type = N_FN; 303 1.1 cgd break; 304 1.1 cgd } 305 1.9 kleink if (ELFDEFNNAME(ST_BIND)(symp[i].st_info) != STB_LOCAL) 306 1.1 cgd nbuf.n_type |= N_EXT; 307 1.1 cgd nbuf.n_desc = 0; /* XXX */ 308 1.1 cgd nbuf.n_other = 0; /* XXX */ 309 1.1 cgd 310 1.1 cgd /* 311 1.1 cgd * Enter the symbol into the database. 312 1.1 cgd */ 313 1.1 cgd if (db->put(db, &key, &data, 0)) { 314 1.1 cgd warn("record enter"); 315 1.1 cgd punt(); 316 1.1 cgd } 317 1.1 cgd 318 1.1 cgd /* 319 1.1 cgd * If it's the kernel version string, we've gotta keep 320 1.14 wiz * some extra data around. Under a separate key, 321 1.1 cgd * we enter the first line (i.e. up to the first newline, 322 1.1 cgd * with the newline replaced by a NUL to terminate the 323 1.1 cgd * entered string) of the version string. 324 1.1 cgd */ 325 1.1 cgd if (strcmp((char *)key.data, VRS_SYM) == 0) { 326 1.1 cgd key.data = (u_char *)VRS_KEY; 327 1.1 cgd key.size = sizeof(VRS_KEY) - 1; 328 1.1 cgd /* Find the version string, relative to its section */ 329 1.15 ragge if (isksyms) { 330 1.15 ragge /* reading from /dev/ksyms, use sysctl */ 331 1.15 ragge size_t sz; 332 1.15 ragge int mib[2]; 333 1.15 ragge char *kv; 334 1.15 ragge 335 1.15 ragge mib[0] = CTL_KERN; 336 1.15 ragge mib[1] = KERN_VERSION; 337 1.15 ragge if (sysctl(mib, 2, NULL, &sz, NULL, 0) == -1) { 338 1.15 ragge warn("sysctl version size"); 339 1.15 ragge punt(); 340 1.15 ragge } 341 1.15 ragge if ((kv = malloc(sz)) == NULL) { 342 1.15 ragge warn("malloc version string"); 343 1.15 ragge punt(); 344 1.15 ragge } 345 1.15 ragge if (sysctl(mib, 2, kv, &sz, NULL, 0) == -1) { 346 1.15 ragge warn("sysctl version string"); 347 1.15 ragge punt(); 348 1.15 ragge } 349 1.15 ragge data.data = kv; 350 1.15 ragge } else 351 1.15 ragge data.data = strdup(&mappedfile[nbuf.n_value - 352 1.15 ragge shdrp[symp[i].st_shndx].sh_addr + 353 1.15 ragge shdrp[symp[i].st_shndx].sh_offset]); 354 1.1 cgd /* assumes newline terminates version. */ 355 1.1 cgd if ((tmpcp = strchr(data.data, '\n')) != NULL) 356 1.1 cgd *tmpcp = '\0'; 357 1.1 cgd data.size = strlen((char *)data.data); 358 1.1 cgd 359 1.1 cgd if (db->put(db, &key, &data, 0)) { 360 1.1 cgd warn("record enter"); 361 1.1 cgd punt(); 362 1.1 cgd } 363 1.1 cgd 364 1.1 cgd /* free pointer created by strdup(). */ 365 1.1 cgd free(data.data); 366 1.1 cgd 367 1.1 cgd /* Restore to original values */ 368 1.1 cgd data.data = (u_char *)&nbuf; 369 1.1 cgd data.size = sizeof(nbuf); 370 1.1 cgd } 371 1.1 cgd } 372 1.1 cgd 373 1.1 cgd rv = 0; 374 1.1 cgd 375 1.1 cgd unmap: 376 1.15 ragge if (malloced) 377 1.15 ragge free(mappedfile); 378 1.15 ragge else 379 1.15 ragge munmap(mappedfile, mappedsize); 380 1.1 cgd out: 381 1.1 cgd return (rv); 382 1.1 cgd } 383 1.1 cgd 384 1.1 cgd #endif 385