2 * Copyright (c) 2008-2009, Stacey Son <sson@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
35 #include <sys/linker.h>
36 #include <sys/malloc.h>
38 #include <sys/module.h>
40 #include <sys/queue.h>
41 #include <sys/resourcevar.h>
46 #include <machine/elf.h>
50 #include <vm/vm_extern.h>
51 #include <vm/vm_object.h>
53 #include "linker_if.h"
58 #define SHDR_SHSTRTAB 3
62 #define STR_SYMTAB ".symtab"
63 #define STR_STRTAB ".strtab"
64 #define STR_SHSTRTAB ".shstrtab"
66 #define KSYMS_DNAME "ksyms"
68 static d_open_t ksyms_open;
69 static d_read_t ksyms_read;
70 static d_mmap_single_t ksyms_mmap_single;
72 static struct cdevsw ksyms_cdevsw = {
73 .d_version = D_VERSION,
77 .d_mmap_single = ksyms_mmap_single,
82 LIST_ENTRY(ksyms_softc) sc_list;
90 static struct sx ksyms_mtx;
91 static struct cdev *ksyms_dev;
92 static LIST_HEAD(, ksyms_softc) ksyms_list = LIST_HEAD_INITIALIZER(ksyms_list);
94 static const char ksyms_shstrtab[] =
95 "\0" STR_SYMTAB "\0" STR_STRTAB "\0" STR_SHSTRTAB "\0";
101 Elf_Shdr kh_shdr[SHDR_NUM];
102 char kh_shstrtab[sizeof(ksyms_shstrtab)];
111 struct ksyms_softc *to_sc;
112 vm_offset_t to_symoff;
113 vm_offset_t to_stroff;
118 static MALLOC_DEFINE(M_KSYMS, "KSYMS", "Kernel Symbol Table");
121 * Get the symbol and string table sizes for a kernel module. Add it to the
125 ksyms_size_permod(linker_file_t lf, void *arg)
128 const Elf_Sym *symtab;
134 syms = LINKER_SYMTAB_GET(lf, &symtab);
135 ts->ts_symsz += syms * sizeof(Elf_Sym);
136 ts->ts_strsz += LINKER_STRTAB_GET(lf, &strtab);
142 * For kernel module get the symbol and string table sizes, returning the
146 ksyms_size_calc(struct tsizes *ts)
152 (void)linker_file_foreach(ksyms_size_permod, ts);
156 ksyms_emit(struct ksyms_softc *sc, void *buf, off_t off, size_t sz)
165 uio.uio_offset = off;
166 uio.uio_resid = (ssize_t)sz;
167 uio.uio_segflg = UIO_SYSSPACE;
168 uio.uio_rw = UIO_WRITE;
169 uio.uio_td = curthread;
171 return (uiomove_object(sc->sc_obj, sc->sc_objsz, &uio));
174 #define SYMBLKSZ (256 * sizeof(Elf_Sym))
177 * For a kernel module, add the symbol and string tables into the
178 * snapshot buffer. Fix up the offsets in the tables.
181 ksyms_add(linker_file_t lf, void *arg)
184 struct ksyms_softc *sc;
186 const Elf_Sym *symtab;
189 size_t len, numsyms, strsz, symsz;
190 linker_symval_t symval;
193 buf = malloc(SYMBLKSZ, M_KSYMS, M_WAITOK);
198 numsyms = LINKER_SYMTAB_GET(lf, &symtab);
199 strsz = LINKER_STRTAB_GET(lf, &strtab);
200 symsz = numsyms * sizeof(Elf_Sym);
203 len = min(SYMBLKSZ, symsz);
204 bcopy(symtab, buf, len);
207 * Fix up symbol table for kernel modules:
208 * string offsets need adjusted
209 * symbol values made absolute
211 symp = (Elf_Sym *) buf;
212 nsyms = len / sizeof(Elf_Sym);
213 for (i = 0; i < nsyms; i++) {
214 symp[i].st_name += to->to_stridx;
215 if (lf->id > 1 && LINKER_SYMBOL_VALUES(lf,
216 (c_linker_sym_t)&symtab[i], &symval) == 0) {
217 symp[i].st_value = (uintptr_t)symval.value;
221 if (len > to->to_resid) {
227 error = ksyms_emit(sc, buf, to->to_symoff, len);
228 to->to_symoff += len;
241 if (strsz > to->to_resid)
243 to->to_resid -= strsz;
244 error = ksyms_emit(sc, strtab, to->to_stroff, strsz);
245 to->to_stroff += strsz;
246 to->to_stridx += strsz;
252 * Create a single ELF symbol table for the kernel and kernel modules loaded
253 * at this time. Write this snapshot out in the process address space. Return
254 * 0 on success, otherwise error.
257 ksyms_snapshot(struct ksyms_softc *sc, struct tsizes *ts)
260 struct ksyms_hdr *hdr;
263 hdr = malloc(sizeof(*hdr), M_KSYMS, M_WAITOK | M_ZERO);
266 * Create the ELF header.
268 hdr->kh_ehdr.e_ident[EI_PAD] = 0;
269 hdr->kh_ehdr.e_ident[EI_MAG0] = ELFMAG0;
270 hdr->kh_ehdr.e_ident[EI_MAG1] = ELFMAG1;
271 hdr->kh_ehdr.e_ident[EI_MAG2] = ELFMAG2;
272 hdr->kh_ehdr.e_ident[EI_MAG3] = ELFMAG3;
273 hdr->kh_ehdr.e_ident[EI_DATA] = ELF_DATA;
274 hdr->kh_ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
275 hdr->kh_ehdr.e_ident[EI_CLASS] = ELF_CLASS;
276 hdr->kh_ehdr.e_ident[EI_VERSION] = EV_CURRENT;
277 hdr->kh_ehdr.e_ident[EI_ABIVERSION] = 0;
278 hdr->kh_ehdr.e_type = ET_EXEC;
279 hdr->kh_ehdr.e_machine = ELF_ARCH;
280 hdr->kh_ehdr.e_version = EV_CURRENT;
281 hdr->kh_ehdr.e_entry = 0;
282 hdr->kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_txtphdr);
283 hdr->kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr);
284 hdr->kh_ehdr.e_flags = 0;
285 hdr->kh_ehdr.e_ehsize = sizeof(Elf_Ehdr);
286 hdr->kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
287 hdr->kh_ehdr.e_phnum = 2; /* Text and Data */
288 hdr->kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
289 hdr->kh_ehdr.e_shnum = SHDR_NUM;
290 hdr->kh_ehdr.e_shstrndx = SHDR_SHSTRTAB;
293 * Add both the text and data program headers.
295 hdr->kh_txtphdr.p_type = PT_LOAD;
296 /* XXX - is there a way to put the actual .text addr/size here? */
297 hdr->kh_txtphdr.p_vaddr = 0;
298 hdr->kh_txtphdr.p_memsz = 0;
299 hdr->kh_txtphdr.p_flags = PF_R | PF_X;
301 hdr->kh_datphdr.p_type = PT_LOAD;
302 /* XXX - is there a way to put the actual .data addr/size here? */
303 hdr->kh_datphdr.p_vaddr = 0;
304 hdr->kh_datphdr.p_memsz = 0;
305 hdr->kh_datphdr.p_flags = PF_R | PF_W | PF_X;
308 * Add the section headers: null, symtab, strtab, shstrtab.
311 /* First section header - null */
313 /* Second section header - symtab */
314 hdr->kh_shdr[SHDR_SYMTAB].sh_name = 1; /* String offset (skip null) */
315 hdr->kh_shdr[SHDR_SYMTAB].sh_type = SHT_SYMTAB;
316 hdr->kh_shdr[SHDR_SYMTAB].sh_flags = 0;
317 hdr->kh_shdr[SHDR_SYMTAB].sh_addr = 0;
318 hdr->kh_shdr[SHDR_SYMTAB].sh_offset = sizeof(*hdr);
319 hdr->kh_shdr[SHDR_SYMTAB].sh_size = ts->ts_symsz;
320 hdr->kh_shdr[SHDR_SYMTAB].sh_link = SHDR_STRTAB;
321 hdr->kh_shdr[SHDR_SYMTAB].sh_info = ts->ts_symsz / sizeof(Elf_Sym);
322 hdr->kh_shdr[SHDR_SYMTAB].sh_addralign = sizeof(long);
323 hdr->kh_shdr[SHDR_SYMTAB].sh_entsize = sizeof(Elf_Sym);
325 /* Third section header - strtab */
326 hdr->kh_shdr[SHDR_STRTAB].sh_name = 1 + sizeof(STR_SYMTAB);
327 hdr->kh_shdr[SHDR_STRTAB].sh_type = SHT_STRTAB;
328 hdr->kh_shdr[SHDR_STRTAB].sh_flags = 0;
329 hdr->kh_shdr[SHDR_STRTAB].sh_addr = 0;
330 hdr->kh_shdr[SHDR_STRTAB].sh_offset =
331 hdr->kh_shdr[SHDR_SYMTAB].sh_offset + ts->ts_symsz;
332 hdr->kh_shdr[SHDR_STRTAB].sh_size = ts->ts_strsz;
333 hdr->kh_shdr[SHDR_STRTAB].sh_link = 0;
334 hdr->kh_shdr[SHDR_STRTAB].sh_info = 0;
335 hdr->kh_shdr[SHDR_STRTAB].sh_addralign = sizeof(char);
336 hdr->kh_shdr[SHDR_STRTAB].sh_entsize = 0;
338 /* Fourth section - shstrtab */
339 hdr->kh_shdr[SHDR_SHSTRTAB].sh_name = 1 + sizeof(STR_SYMTAB) +
341 hdr->kh_shdr[SHDR_SHSTRTAB].sh_type = SHT_STRTAB;
342 hdr->kh_shdr[SHDR_SHSTRTAB].sh_flags = 0;
343 hdr->kh_shdr[SHDR_SHSTRTAB].sh_addr = 0;
344 hdr->kh_shdr[SHDR_SHSTRTAB].sh_offset =
345 offsetof(struct ksyms_hdr, kh_shstrtab);
346 hdr->kh_shdr[SHDR_SHSTRTAB].sh_size = sizeof(ksyms_shstrtab);
347 hdr->kh_shdr[SHDR_SHSTRTAB].sh_link = 0;
348 hdr->kh_shdr[SHDR_SHSTRTAB].sh_info = 0;
349 hdr->kh_shdr[SHDR_SHSTRTAB].sh_addralign = 0 /* sizeof(char) */;
350 hdr->kh_shdr[SHDR_SHSTRTAB].sh_entsize = 0;
352 /* Copy shstrtab into the header. */
353 bcopy(ksyms_shstrtab, hdr->kh_shstrtab, sizeof(ksyms_shstrtab));
356 to.to_symoff = hdr->kh_shdr[SHDR_SYMTAB].sh_offset;
357 to.to_stroff = hdr->kh_shdr[SHDR_STRTAB].sh_offset;
359 to.to_resid = sc->sc_objsz - sizeof(struct ksyms_hdr);
362 error = ksyms_emit(sc, hdr, 0, sizeof(*hdr));
367 /* Add symbol and string tables for each kernel module. */
368 error = linker_file_foreach(ksyms_add, &to);
371 if (to.to_resid != 0)
377 ksyms_cdevpriv_dtr(void *data)
379 struct ksyms_softc *sc;
382 sc = (struct ksyms_softc *)data;
384 sx_xlock(&ksyms_mtx);
385 LIST_REMOVE(sc, sc_list);
386 sx_xunlock(&ksyms_mtx);
389 vm_object_deallocate(obj);
394 ksyms_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
397 struct ksyms_softc *sc;
402 * Limit one open() per process. The process must close()
403 * before open()'ing again.
405 sx_xlock(&ksyms_mtx);
406 LIST_FOREACH(sc, &ksyms_list, sc_list) {
407 if (sc->sc_proc == td->td_proc) {
408 sx_xunlock(&ksyms_mtx);
413 sc = malloc(sizeof(*sc), M_KSYMS, M_WAITOK | M_ZERO);
414 sc->sc_proc = td->td_proc;
415 LIST_INSERT_HEAD(&ksyms_list, sc, sc_list);
416 sx_xunlock(&ksyms_mtx);
418 error = devfs_set_cdevpriv(sc, ksyms_cdevpriv_dtr);
420 ksyms_cdevpriv_dtr(sc);
425 * MOD_SLOCK doesn't work here (because of a lock reversal with
426 * KLD_SLOCK). Therefore, simply try up to 3 times to get a "clean"
427 * snapshot of the kernel symbol table. This should work fine in the
428 * rare case of a kernel module being loaded/unloaded at the same
431 for (try = 0; try < 3; try++) {
432 ksyms_size_calc(&ts);
433 elfsz = sizeof(struct ksyms_hdr) + ts.ts_symsz + ts.ts_strsz;
435 sc->sc_obj = vm_object_allocate(OBJT_DEFAULT,
436 OFF_TO_IDX(round_page(elfsz)));
437 sc->sc_objsz = elfsz;
439 error = ksyms_snapshot(sc, &ts);
443 vm_object_deallocate(sc->sc_obj);
450 ksyms_read(struct cdev *dev, struct uio *uio, int flags __unused)
452 struct ksyms_softc *sc;
455 error = devfs_get_cdevpriv((void **)&sc);
458 return (uiomove_object(sc->sc_obj, sc->sc_objsz, uio));
462 ksyms_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
463 vm_object_t *objp, int nprot)
465 struct ksyms_softc *sc;
469 error = devfs_get_cdevpriv((void **)&sc);
473 if (*offset < 0 || *offset >= round_page(sc->sc_objsz) ||
474 size > round_page(sc->sc_objsz) - *offset ||
475 (nprot & ~PROT_READ) != 0)
479 vm_object_reference(obj);
485 ksyms_modevent(module_t mod __unused, int type, void *data __unused)
492 sx_init(&ksyms_mtx, "KSyms mtx");
493 ksyms_dev = make_dev(&ksyms_cdevsw, 0, UID_ROOT, GID_WHEEL,
497 if (!LIST_EMPTY(&ksyms_list))
499 destroy_dev(ksyms_dev);
500 sx_destroy(&ksyms_mtx);
511 DEV_MODULE(ksyms, ksyms_modevent, NULL);
512 MODULE_VERSION(ksyms, 1);