1 /* $OpenBSD: ksyms.c,v 1.21 2011/06/23 16:02:33 tedu Exp $ */ 2 /* 3 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 4 * Copyright (c) 2001 Artur Grabowski <art@openbsd.org> 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 * 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 19 * THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/param.h> 29 #include <sys/buf.h> 30 #include <sys/exec.h> 31 #include <sys/systm.h> 32 #include <sys/uio.h> 33 #include <sys/malloc.h> 34 #include <sys/fcntl.h> 35 #include <sys/conf.h> 36 37 #include <uvm/uvm_extern.h> 38 39 #ifdef _NLIST_DO_ELF 40 #include <sys/exec_elf.h> 41 #endif 42 43 #include <machine/cpu.h> 44 45 extern char *esym; /* end of symbol table */ 46 #if defined(__sparc64__) || defined(__mips__) 47 extern char *ssym; /* end of kernel */ 48 #else 49 extern long end; /* end of kernel */ 50 #endif 51 52 static caddr_t ksym_head; 53 static caddr_t ksym_syms; 54 static size_t ksym_head_size; 55 static size_t ksym_syms_size; 56 57 void ksymsattach(int); 58 59 /* 60 * We assume __LDPGSZ is a multiple of PAGE_SIZE (it is) 61 */ 62 63 /*ARGSUSED*/ 64 void 65 ksymsattach(int num) 66 { 67 68 #if defined(__sparc64__) || defined(__mips__) 69 if (esym <= ssym) { 70 printf("/dev/ksyms: Symbol table not valid.\n"); 71 return; 72 } 73 #else 74 if (esym <= (char *)&end) { 75 printf("/dev/ksyms: Symbol table not valid.\n"); 76 return; 77 } 78 #endif 79 80 #ifdef _NLIST_DO_ELF 81 do { 82 #if defined(__sparc64__) || defined(__mips__) 83 caddr_t symtab = ssym; 84 #else 85 caddr_t symtab = (caddr_t)&end; 86 #endif 87 Elf_Ehdr *elf; 88 Elf_Shdr *shdr; 89 int i; 90 91 elf = (Elf_Ehdr *)symtab; 92 if (memcmp(elf->e_ident, ELFMAG, SELFMAG) != 0 || 93 elf->e_ident[EI_CLASS] != ELFCLASS || 94 elf->e_machine != ELF_TARG_MACH) 95 break; 96 97 shdr = (Elf_Shdr *)&symtab[elf->e_shoff]; 98 for (i = 0; i < elf->e_shnum; i++) { 99 if (shdr[i].sh_type == SHT_SYMTAB) { 100 break; 101 } 102 } 103 104 /* 105 * No symbol table found. 106 */ 107 if (i == elf->e_shnum) 108 break; 109 110 /* 111 * No additional header. 112 */ 113 ksym_head_size = 0; 114 ksym_syms = symtab; 115 ksym_syms_size = (size_t)(esym - symtab); 116 117 return; 118 } while (0); 119 #endif 120 121 #ifdef _NLIST_DO_AOUT 122 { 123 /* 124 * a.out header. 125 * Fake up a struct exec. 126 * We only fill in the following non-zero entries: 127 * a_text - fake text segment (struct exec only) 128 * a_syms - size of symbol table 129 */ 130 caddr_t symtab = (char *)(&end + 1); 131 struct exec *k1; 132 133 ksym_head_size = __LDPGSZ; 134 ksym_head = malloc(ksym_head_size, M_DEVBUF, M_NOWAIT | M_ZERO); 135 if (ksym_head == NULL) { 136 printf("failed to allocate memory for /dev/ksyms\n"); 137 return; 138 } 139 140 k1 = (struct exec *)ksym_head; 141 142 N_SETMAGIC(*k1, ZMAGIC, MID_MACHINE, 0); 143 k1->a_text = __LDPGSZ; 144 k1->a_syms = end; 145 146 ksym_syms = symtab; 147 ksym_syms_size = (size_t)(esym - symtab); 148 } 149 #endif 150 } 151 152 /*ARGSUSED*/ 153 int 154 ksymsopen(dev_t dev, int flag, int mode, struct proc *p) 155 { 156 157 /* There are no non-zero minor devices */ 158 if (minor(dev) != 0) 159 return (ENXIO); 160 161 /* This device is read-only */ 162 if ((flag & FWRITE)) 163 return (EPERM); 164 165 /* ksym_syms must be initialized */ 166 if (ksym_syms == NULL) 167 return (ENXIO); 168 169 return (0); 170 } 171 172 /*ARGSUSED*/ 173 int 174 ksymsclose(dev_t dev, int flag, int mode, struct proc *p) 175 { 176 177 return (0); 178 } 179 180 /*ARGSUSED*/ 181 int 182 ksymsread(dev_t dev, struct uio *uio, int flags) 183 { 184 int error; 185 size_t len; 186 caddr_t v; 187 size_t off; 188 189 while (uio->uio_resid > 0) { 190 if (uio->uio_offset >= ksym_head_size + ksym_syms_size) 191 break; 192 193 if (uio->uio_offset < ksym_head_size) { 194 v = ksym_head + uio->uio_offset; 195 len = ksym_head_size - uio->uio_offset; 196 } else { 197 off = uio->uio_offset - ksym_head_size; 198 v = ksym_syms + off; 199 len = ksym_syms_size - off; 200 } 201 202 if (len > uio->uio_resid) 203 len = uio->uio_resid; 204 205 if ((error = uiomove(v, len, uio)) != 0) 206 return (error); 207 } 208 209 return (0); 210 } 211