1 /* $NetBSD: ksyms.h,v 1.27 2010/03/15 02:28:59 darran Exp $ */ 2 3 /* 4 * Copyright (c) 2001, 2003 Anders Magnusson (ragge@ludd.luth.se). 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #ifndef _SYS_KSYMS_H_ 31 #define _SYS_KSYMS_H_ 32 33 #ifdef _KSYMS_PRIVATE 34 35 #if defined(_KERNEL_OPT) 36 #include "opt_dtrace.h" 37 #endif 38 39 #define ELFSIZE ARCH_ELFSIZE 40 #include <sys/exec_elf.h> 41 #include <sys/queue.h> 42 43 struct ksyms_symtab { 44 TAILQ_ENTRY(ksyms_symtab) sd_queue; /* All active tables */ 45 const char *sd_name; /* Name of this table */ 46 Elf_Sym *sd_symstart; /* Address of symbol table */ 47 uintptr_t sd_minsym; /* symbol with minimum value */ 48 uintptr_t sd_maxsym; /* symbol with maximum value */ 49 char *sd_strstart; /* Address of corresponding string table */ 50 int sd_usroffset; /* Real address for userspace */ 51 int sd_symsize; /* Size in bytes of symbol table */ 52 int sd_strsize; /* Size of string table */ 53 int sd_nglob; /* Number of global symbols */ 54 bool sd_gone; /* dead but around for open() */ 55 #ifdef KDTRACE_HOOKS 56 void *sd_ctfstart; /* Address of CTF contents */ 57 int sd_ctfsize; /* Size in bytes of CTF contents */ 58 uint32_t *sd_nmap; /* Name map for sorted symbols */ 59 int sd_nmapsize; /* Total span of map */ 60 #endif 61 }; 62 63 /* 64 * Static allocated ELF header. 65 * Basic info is filled in at attach, sizes at open. 66 */ 67 #define SYMTAB 1 68 #define STRTAB 2 69 #define SHSTRTAB 3 70 #define SHBSS 4 71 #ifdef KDTRACE_HOOKS 72 #define SHCTF 5 73 #define NSECHDR 6 74 #else 75 #define NSECHDR 5 76 #endif 77 78 #define NPRGHDR 1 79 #define SHSTRSIZ 42 80 81 struct ksyms_hdr { 82 Elf_Ehdr kh_ehdr; 83 Elf_Phdr kh_phdr[NPRGHDR]; 84 Elf_Shdr kh_shdr[NSECHDR]; 85 char kh_strtab[SHSTRSIZ]; 86 }; 87 #endif /* _KSYMS_PRIVATE */ 88 89 /* 90 * Do a lookup of a symbol using the in-kernel lookup algorithm. 91 */ 92 struct ksyms_gsymbol { 93 const char *kg_name; 94 union { 95 void *ku_sym; /* Normally Elf_Sym */ 96 unsigned long *ku_value; 97 } _un; 98 #define kg_sym _un.ku_sym 99 #define kg_value _un.ku_value 100 }; 101 102 #define KIOCGSYMBOL _IOW('l', 1, struct ksyms_gsymbol) 103 #define KIOCGVALUE _IOW('l', 2, struct ksyms_gsymbol) 104 #define KIOCGSIZE _IOR('l', 3, int) 105 106 107 #if defined(_KERNEL) || defined(_KMEMUSER) 108 /* 109 * Definitions used in ksyms_getname() and ksyms_getval(). 110 */ 111 #define KSYMS_CLOSEST 0001 /* Nearest lower match */ 112 #define KSYMS_EXACT 0002 /* Only exact match allowed */ 113 #define KSYMS_EXTERN 0000 /* Only external symbols (pseudo) */ 114 #define KSYMS_PROC 0100 /* Procedures only */ 115 #define KSYMS_ANY 0200 /* Also local symbols (DDB use only) */ 116 117 typedef int (*ksyms_callback_t)(const char *, int, void *, 118 uint32_t, int, void *); 119 120 /* 121 * Prototypes 122 */ 123 124 int ksyms_getname(const char **, const char **, vaddr_t, int); 125 int ksyms_getval(const char *, const char *, unsigned long *, int); 126 int ksyms_getval_unlocked(const char *, const char *, unsigned long *, int); 127 struct ksyms_symtab *ksyms_get_mod(const char *); 128 int ksyms_mod_foreach(const char *mod, ksyms_callback_t, void *); 129 int ksyms_addsymtab(const char *, void *, vsize_t, char *, vsize_t); 130 int ksyms_delsymtab(const char *); 131 void ksyms_init(void); 132 void ksyms_addsyms_elf(int, void *, void *); 133 void ksyms_addsyms_explicit(void *, void *, size_t, void *, size_t); 134 int ksyms_sift(char *, char *, int); 135 void ksyms_modload(const char *, void *, vsize_t, char *, vsize_t); 136 void ksyms_modunload(const char *); 137 138 #endif /* _KERNEL */ 139 #endif /* _SYS_KSYMS_H_ */ 140