1 /* $OpenBSD: db_elf.c,v 1.2 2001/03/15 07:35:46 niklas Exp $ */ 2 /* $NetBSD: db_elf.c,v 1.13 2000/07/07 21:55:18 jhawk Exp $ */ 3 4 /*- 5 * Copyright (c) 1997 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 #include <sys/types.h> 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/proc.h> 45 #include <sys/exec.h> 46 47 #include <machine/db_machdep.h> 48 49 #include <ddb/db_sym.h> 50 #include <ddb/db_output.h> 51 #include <ddb/db_extern.h> 52 53 #ifdef DB_ELF_SYMBOLS 54 55 #ifndef DB_ELFSIZE 56 #error Must define DB_ELFSIZE! 57 #endif 58 59 #define ELFSIZE DB_ELFSIZE 60 61 #include <sys/exec_elf.h> 62 63 static char *db_elf_find_strtab __P((db_symtab_t *)); 64 65 #define STAB_TO_SYMSTART(stab) ((Elf_Sym *)((stab)->start)) 66 #define STAB_TO_SYMEND(stab) ((Elf_Sym *)((stab)->end)) 67 #define STAB_TO_EHDR(stab) ((Elf_Ehdr *)((stab)->private)) 68 #define STAB_TO_SHDR(stab, e) ((Elf_Shdr *)((stab)->private + (e)->e_shoff)) 69 70 boolean_t db_elf_sym_init __P((int, void *, void *, const char *)); 71 db_sym_t db_elf_lookup __P((db_symtab_t *, char *)); 72 db_sym_t db_elf_search_symbol __P((db_symtab_t *, db_addr_t, 73 db_strategy_t, db_expr_t *)); 74 void db_elf_symbol_values __P((db_symtab_t *, db_sym_t, 75 char **, db_expr_t *)); 76 boolean_t db_elf_line_at_pc __P((db_symtab_t *, db_sym_t, 77 char **, int *, db_expr_t)); 78 boolean_t db_elf_sym_numargs __P((db_symtab_t *, db_sym_t, int *, 79 char **)); 80 void db_elf_forall __P((db_symtab_t *, 81 db_forall_func_t db_forall_func, void *)); 82 83 db_symformat_t db_symformat_elf = { 84 "ELF", 85 db_elf_sym_init, 86 db_elf_lookup, 87 db_elf_search_symbol, 88 db_elf_symbol_values, 89 db_elf_line_at_pc, 90 db_elf_sym_numargs, 91 db_elf_forall 92 }; 93 94 /* 95 * Find the symbol table and strings; tell ddb about them. 96 */ 97 boolean_t 98 db_elf_sym_init(symsize, symtab, esymtab, name) 99 int symsize; /* size of symbol table */ 100 void *symtab; /* pointer to start of symbol table */ 101 void *esymtab; /* pointer to end of string table, 102 for checking - rounded up to integer 103 boundary */ 104 const char *name; 105 { 106 Elf_Ehdr *elf; 107 Elf_Shdr *shp; 108 Elf_Sym *symp, *symtab_start, *symtab_end; 109 char *shstrtab, *strtab_start, *strtab_end; 110 int i; 111 112 if (ALIGNED_POINTER(symtab, long) == 0) { 113 printf("[ %s symbol table has bad start address %p ]\n", 114 name, symtab); 115 return (FALSE); 116 } 117 118 symtab_start = symtab_end = NULL; 119 strtab_start = strtab_end = NULL; 120 121 /* 122 * The format of the symbols loaded by the boot program is: 123 * 124 * Elf exec header 125 * first section header 126 * . . . 127 * . . . 128 * last section header 129 * first symbol or string table section 130 * . . . 131 * . . . 132 * last symbol or string table section 133 */ 134 135 /* 136 * Validate the Elf header. 137 */ 138 elf = (Elf_Ehdr *)symtab; 139 if (memcmp(elf->e_ident, ELFMAG, SELFMAG) != 0 || 140 elf->e_ident[EI_CLASS] != ELFCLASS) 141 goto badheader; 142 143 if (elf->e_machine != ELF_TARG_MACH) 144 goto badheader; 145 146 /* 147 * Find the section header string table (.shstrtab), and look up 148 * the symbol table (.symtab) and string table (.strtab) via their 149 * names in shstrtab, rather than by table type. 150 * This works in the presence of multiple string tables, such as 151 * stabs data found when booting bsd.gdb. 152 */ 153 shp = (Elf_Shdr *)((char *)symtab + elf->e_shoff); 154 shstrtab = (char*)symtab + shp[elf->e_shstrndx].sh_offset; 155 for (i = 0; i < elf->e_shnum; i++) { 156 if (strcmp(".strtab", shstrtab+shp[i].sh_name) == 0) { 157 strtab_start = (char *)symtab + shp[i].sh_offset; 158 strtab_end = (char *)symtab + shp[i].sh_offset + 159 shp[i].sh_size; 160 } else if (strcmp(".symtab", shstrtab+shp[i].sh_name) == 0) { 161 symtab_start = (Elf_Sym *)((char *)symtab + 162 shp[i].sh_offset); 163 symtab_end = (Elf_Sym *)((char *)symtab + 164 shp[i].sh_offset + shp[i].sh_size); 165 } 166 } 167 168 /* 169 * Now, sanity check the symbols against the string table. 170 */ 171 if (symtab_start == NULL || strtab_start == NULL || 172 ALIGNED_POINTER(symtab_start, long) == 0 || 173 ALIGNED_POINTER(strtab_start, long) == 0) 174 goto badheader; 175 for (symp = symtab_start; symp < symtab_end; symp++) 176 if (symp->st_name + strtab_start > strtab_end) 177 goto badheader; 178 179 /* 180 * Link the symbol table into the debugger. 181 */ 182 if (db_add_symbol_table((char *)symtab_start, 183 (char *)symtab_end, name, (char *)symtab) != -1) { 184 printf("[ using %lu bytes of %s ELF symbol table ]\n", 185 (u_long)roundup(((char *)esymtab - (char *)symtab), 186 sizeof(u_long)), name); 187 return (TRUE); 188 } 189 190 return (FALSE); 191 192 badheader: 193 printf("[ %s ELF symbol table not valid ]\n", name); 194 return (FALSE); 195 } 196 197 /* 198 * Internal helper function - return a pointer to the string table 199 * for the current symbol table. 200 */ 201 static char * 202 db_elf_find_strtab(stab) 203 db_symtab_t *stab; 204 { 205 Elf_Ehdr *elf = STAB_TO_EHDR(stab); 206 Elf_Shdr *shp = STAB_TO_SHDR(stab, elf); 207 char *shstrtab; 208 int i; 209 210 shstrtab = (char*)elf + shp[elf->e_shstrndx].sh_offset; 211 for (i = 0; i < elf->e_shnum; i++) { 212 if (strcmp(".strtab", shstrtab+shp[i].sh_name) == 0) 213 return ((char*)elf + shp[i].sh_offset); 214 } 215 216 return (NULL); 217 } 218 219 /* 220 * Lookup the symbol with the given name. 221 */ 222 db_sym_t 223 db_elf_lookup(stab, symstr) 224 db_symtab_t *stab; 225 char *symstr; 226 { 227 Elf_Sym *symp, *symtab_start, *symtab_end; 228 char *strtab; 229 230 symtab_start = STAB_TO_SYMSTART(stab); 231 symtab_end = STAB_TO_SYMEND(stab); 232 233 strtab = db_elf_find_strtab(stab); 234 if (strtab == NULL) 235 return ((db_sym_t)0); 236 237 for (symp = symtab_start; symp < symtab_end; symp++) { 238 if (symp->st_name != 0 && 239 db_eqname(strtab + symp->st_name, symstr, 0)) 240 return ((db_sym_t)symp); 241 } 242 243 return ((db_sym_t)0); 244 } 245 246 /* 247 * Search for the symbol with the given address (matching within the 248 * provided threshold). 249 */ 250 db_sym_t 251 db_elf_search_symbol(symtab, off, strategy, diffp) 252 db_symtab_t *symtab; 253 db_addr_t off; 254 db_strategy_t strategy; 255 db_expr_t *diffp; /* in/out */ 256 { 257 Elf_Sym *rsymp, *symp, *symtab_start, *symtab_end; 258 db_expr_t diff = *diffp; 259 260 symtab_start = STAB_TO_SYMSTART(symtab); 261 symtab_end = STAB_TO_SYMEND(symtab); 262 263 rsymp = NULL; 264 265 for (symp = symtab_start; symp < symtab_end; symp++) { 266 if (symp->st_name == 0) 267 continue; 268 #if 0 269 /* This prevents me from seeing anythin in locore.s -- eeh */ 270 if (ELF_SYM_TYPE(symp->st_info) != Elf_estt_object && 271 ELF_SYM_TYPE(symp->st_info) != Elf_estt_func) 272 continue; 273 #endif 274 275 if (off >= symp->st_value) { 276 if ((off - symp->st_value) < diff) { 277 diff = off - symp->st_value; 278 rsymp = symp; 279 if (diff == 0) { 280 if (strategy == DB_STGY_PROC && 281 ELFDEFNNAME(ST_TYPE)(symp->st_info) 282 == STT_FUNC && 283 ELFDEFNNAME(ST_BIND)(symp->st_info) 284 != STB_LOCAL) 285 break; 286 if (strategy == DB_STGY_ANY && 287 ELFDEFNNAME(ST_BIND)(symp->st_info) 288 != STB_LOCAL) 289 break; 290 } 291 } else if ((off - symp->st_value) == diff) { 292 if (rsymp == NULL) 293 rsymp = symp; 294 else if (ELFDEFNNAME(ST_BIND)(rsymp->st_info) 295 == STB_LOCAL && 296 ELFDEFNNAME(ST_BIND)(symp->st_info) 297 != STB_LOCAL) { 298 /* pick the external symbol */ 299 rsymp = symp; 300 } 301 } 302 } 303 } 304 305 if (rsymp == NULL) 306 *diffp = off; 307 else 308 *diffp = diff; 309 310 return ((db_sym_t)rsymp); 311 } 312 313 /* 314 * Return the name and value for a symbol. 315 */ 316 void 317 db_elf_symbol_values(symtab, sym, namep, valuep) 318 db_symtab_t *symtab; 319 db_sym_t sym; 320 char **namep; 321 db_expr_t *valuep; 322 { 323 Elf_Sym *symp = (Elf_Sym *)sym; 324 char *strtab; 325 326 if (namep) { 327 strtab = db_elf_find_strtab(symtab); 328 if (strtab == NULL) 329 *namep = NULL; 330 else 331 *namep = strtab + symp->st_name; 332 } 333 334 if (valuep) 335 *valuep = symp->st_value; 336 } 337 338 /* 339 * Return the file and line number of the current program counter 340 * if we can find the appropriate debugging symbol. 341 */ 342 boolean_t 343 db_elf_line_at_pc(symtab, cursym, filename, linenum, off) 344 db_symtab_t *symtab; 345 db_sym_t cursym; 346 char **filename; 347 int *linenum; 348 db_expr_t off; 349 { 350 351 /* 352 * XXX We don't support this (yet). 353 */ 354 return (FALSE); 355 } 356 357 /* 358 * Returns the number of arguments to a function and their 359 * names if we can find the appropriate debugging symbol. 360 */ 361 boolean_t 362 db_elf_sym_numargs(symtab, cursym, nargp, argnamep) 363 db_symtab_t *symtab; 364 db_sym_t cursym; 365 int *nargp; 366 char **argnamep; 367 { 368 369 /* 370 * XXX We don't support this (yet). 371 */ 372 return (FALSE); 373 } 374 375 void 376 db_elf_forall(stab, db_forall_func, arg) 377 db_symtab_t *stab; 378 db_forall_func_t db_forall_func; 379 void *arg; 380 { 381 char *strtab; 382 static char suffix[2]; 383 Elf_Sym *symp, *symtab_start, *symtab_end; 384 385 symtab_start = STAB_TO_SYMSTART(stab); 386 symtab_end = STAB_TO_SYMEND(stab); 387 388 strtab = db_elf_find_strtab(stab); 389 if (strtab == NULL) 390 return; 391 392 for (symp = symtab_start; symp < symtab_end; symp++) 393 if (symp->st_name != 0) { 394 suffix[1] = '\0'; 395 switch (ELFDEFNNAME(ST_TYPE)(symp->st_info)) { 396 case STT_OBJECT: 397 suffix[0] = '+'; 398 break; 399 case STT_FUNC: 400 suffix[0] = '*'; 401 break; 402 case STT_SECTION: 403 suffix[0] = '&'; 404 break; 405 case STT_FILE: 406 suffix[0] = '/'; 407 break; 408 default: 409 suffix[0] = '\0'; 410 } 411 (*db_forall_func)(stab, (db_sym_t)symp, 412 strtab + symp->st_name, suffix, 0, arg); 413 } 414 return; 415 } 416 #endif /* DB_ELF_SYMBOLS */ 417