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