2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2009, Stacey Son <sson@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
37 #include <sys/linker.h>
38 #include <sys/malloc.h>
40 #include <sys/module.h>
42 #include <sys/queue.h>
43 #include <sys/resourcevar.h>
44 #include <sys/rwlock.h>
49 #include <machine/elf.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_pager.h>
58 #include "linker_if.h"
63 #define SHDR_SHSTRTAB 3
67 #define STR_SYMTAB ".symtab"
68 #define STR_STRTAB ".strtab"
69 #define STR_SHSTRTAB ".shstrtab"
71 #define KSYMS_DNAME "ksyms"
73 static d_open_t ksyms_open;
74 static d_read_t ksyms_read;
75 static d_mmap_single_t ksyms_mmap_single;
77 static struct cdevsw ksyms_cdevsw = {
78 .d_version = D_VERSION,
82 .d_mmap_single = ksyms_mmap_single,
87 LIST_ENTRY(ksyms_softc) sc_list;
95 static struct sx ksyms_mtx;
96 static struct cdev *ksyms_dev;
97 static LIST_HEAD(, ksyms_softc) ksyms_list = LIST_HEAD_INITIALIZER(ksyms_list);
99 static const char ksyms_shstrtab[] =
100 "\0" STR_SYMTAB "\0" STR_STRTAB "\0" STR_SHSTRTAB "\0";
106 Elf_Shdr kh_shdr[SHDR_NUM];
107 char kh_shstrtab[sizeof(ksyms_shstrtab)];
116 struct ksyms_softc *to_sc;
117 vm_offset_t to_symoff;
118 vm_offset_t to_stroff;
123 static MALLOC_DEFINE(M_KSYMS, "KSYMS", "Kernel Symbol Table");
126 * Get the symbol and string table sizes for a kernel module. Add it to the
130 ksyms_size_permod(linker_file_t lf, void *arg)
133 const Elf_Sym *symtab;
139 syms = LINKER_SYMTAB_GET(lf, &symtab);
140 ts->ts_symsz += syms * sizeof(Elf_Sym);
141 ts->ts_strsz += LINKER_STRTAB_GET(lf, &strtab);
147 * For kernel module get the symbol and string table sizes, returning the
151 ksyms_size_calc(struct tsizes *ts)
157 (void)linker_file_foreach(ksyms_size_permod, ts);
161 ksyms_emit(struct ksyms_softc *sc, void *buf, off_t off, size_t sz)
170 uio.uio_offset = off;
171 uio.uio_resid = (ssize_t)sz;
172 uio.uio_segflg = UIO_SYSSPACE;
173 uio.uio_rw = UIO_WRITE;
174 uio.uio_td = curthread;
176 return (uiomove_object(sc->sc_obj, sc->sc_objsz, &uio));
179 #define SYMBLKSZ (256 * sizeof(Elf_Sym))
182 * For a kernel module, add the symbol and string tables into the
183 * snapshot buffer. Fix up the offsets in the tables.
186 ksyms_add(linker_file_t lf, void *arg)
189 struct ksyms_softc *sc;
191 const Elf_Sym *symtab;
194 size_t len, numsyms, strsz, symsz;
195 linker_symval_t symval;
198 buf = malloc(SYMBLKSZ, M_KSYMS, M_WAITOK);
203 numsyms = LINKER_SYMTAB_GET(lf, &symtab);
204 strsz = LINKER_STRTAB_GET(lf, &strtab);
205 symsz = numsyms * sizeof(Elf_Sym);
208 len = min(SYMBLKSZ, symsz);
209 bcopy(symtab, buf, len);
212 * Fix up symbol table for kernel modules:
213 * string offsets need adjusted
214 * symbol values made absolute
216 symp = (Elf_Sym *) buf;
217 nsyms = len / sizeof(Elf_Sym);
218 for (i = 0; i < nsyms; i++) {
219 symp[i].st_name += to->to_stridx;
220 if (lf->id > 1 && LINKER_SYMBOL_VALUES(lf,
221 (c_linker_sym_t)&symtab[i], &symval) == 0) {
222 symp[i].st_value = (uintptr_t)symval.value;
226 if (len > to->to_resid) {
232 error = ksyms_emit(sc, buf, to->to_symoff, len);
233 to->to_symoff += len;
246 if (strsz > to->to_resid)
248 to->to_resid -= strsz;
249 error = ksyms_emit(sc, strtab, to->to_stroff, strsz);
250 to->to_stroff += strsz;
251 to->to_stridx += strsz;
257 * Create a single ELF symbol table for the kernel and kernel modules loaded
258 * at this time. Write this snapshot out in the process address space. Return
259 * 0 on success, otherwise error.
262 ksyms_snapshot(struct ksyms_softc *sc, struct tsizes *ts)
265 struct ksyms_hdr *hdr;
268 hdr = malloc(sizeof(*hdr), M_KSYMS, M_WAITOK | M_ZERO);
271 * Create the ELF header.
273 hdr->kh_ehdr.e_ident[EI_PAD] = 0;
274 hdr->kh_ehdr.e_ident[EI_MAG0] = ELFMAG0;
275 hdr->kh_ehdr.e_ident[EI_MAG1] = ELFMAG1;
276 hdr->kh_ehdr.e_ident[EI_MAG2] = ELFMAG2;
277 hdr->kh_ehdr.e_ident[EI_MAG3] = ELFMAG3;
278 hdr->kh_ehdr.e_ident[EI_DATA] = ELF_DATA;
279 hdr->kh_ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
280 hdr->kh_ehdr.e_ident[EI_CLASS] = ELF_CLASS;
281 hdr->kh_ehdr.e_ident[EI_VERSION] = EV_CURRENT;
282 hdr->kh_ehdr.e_ident[EI_ABIVERSION] = 0;
283 hdr->kh_ehdr.e_type = ET_EXEC;
284 hdr->kh_ehdr.e_machine = ELF_ARCH;
285 hdr->kh_ehdr.e_version = EV_CURRENT;
286 hdr->kh_ehdr.e_entry = 0;
287 hdr->kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_txtphdr);
288 hdr->kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr);
289 hdr->kh_ehdr.e_flags = 0;
290 hdr->kh_ehdr.e_ehsize = sizeof(Elf_Ehdr);
291 hdr->kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
292 hdr->kh_ehdr.e_phnum = 2; /* Text and Data */
293 hdr->kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
294 hdr->kh_ehdr.e_shnum = SHDR_NUM;
295 hdr->kh_ehdr.e_shstrndx = SHDR_SHSTRTAB;
298 * Add both the text and data program headers.
300 hdr->kh_txtphdr.p_type = PT_LOAD;
301 /* XXX - is there a way to put the actual .text addr/size here? */
302 hdr->kh_txtphdr.p_vaddr = 0;
303 hdr->kh_txtphdr.p_memsz = 0;
304 hdr->kh_txtphdr.p_flags = PF_R | PF_X;
306 hdr->kh_datphdr.p_type = PT_LOAD;
307 /* XXX - is there a way to put the actual .data addr/size here? */
308 hdr->kh_datphdr.p_vaddr = 0;
309 hdr->kh_datphdr.p_memsz = 0;
310 hdr->kh_datphdr.p_flags = PF_R | PF_W | PF_X;
313 * Add the section headers: null, symtab, strtab, shstrtab.
316 /* First section header - null */
318 /* Second section header - symtab */
319 hdr->kh_shdr[SHDR_SYMTAB].sh_name = 1; /* String offset (skip null) */
320 hdr->kh_shdr[SHDR_SYMTAB].sh_type = SHT_SYMTAB;
321 hdr->kh_shdr[SHDR_SYMTAB].sh_flags = 0;
322 hdr->kh_shdr[SHDR_SYMTAB].sh_addr = 0;
323 hdr->kh_shdr[SHDR_SYMTAB].sh_offset = sizeof(*hdr);
324 hdr->kh_shdr[SHDR_SYMTAB].sh_size = ts->ts_symsz;
325 hdr->kh_shdr[SHDR_SYMTAB].sh_link = SHDR_STRTAB;
326 hdr->kh_shdr[SHDR_SYMTAB].sh_info = ts->ts_symsz / sizeof(Elf_Sym);
327 hdr->kh_shdr[SHDR_SYMTAB].sh_addralign = sizeof(long);
328 hdr->kh_shdr[SHDR_SYMTAB].sh_entsize = sizeof(Elf_Sym);
330 /* Third section header - strtab */
331 hdr->kh_shdr[SHDR_STRTAB].sh_name = 1 + sizeof(STR_SYMTAB);
332 hdr->kh_shdr[SHDR_STRTAB].sh_type = SHT_STRTAB;
333 hdr->kh_shdr[SHDR_STRTAB].sh_flags = 0;
334 hdr->kh_shdr[SHDR_STRTAB].sh_addr = 0;
335 hdr->kh_shdr[SHDR_STRTAB].sh_offset =
336 hdr->kh_shdr[SHDR_SYMTAB].sh_offset + ts->ts_symsz;
337 hdr->kh_shdr[SHDR_STRTAB].sh_size = ts->ts_strsz;
338 hdr->kh_shdr[SHDR_STRTAB].sh_link = 0;
339 hdr->kh_shdr[SHDR_STRTAB].sh_info = 0;
340 hdr->kh_shdr[SHDR_STRTAB].sh_addralign = sizeof(char);
341 hdr->kh_shdr[SHDR_STRTAB].sh_entsize = 0;
343 /* Fourth section - shstrtab */
344 hdr->kh_shdr[SHDR_SHSTRTAB].sh_name = 1 + sizeof(STR_SYMTAB) +
346 hdr->kh_shdr[SHDR_SHSTRTAB].sh_type = SHT_STRTAB;
347 hdr->kh_shdr[SHDR_SHSTRTAB].sh_flags = 0;
348 hdr->kh_shdr[SHDR_SHSTRTAB].sh_addr = 0;
349 hdr->kh_shdr[SHDR_SHSTRTAB].sh_offset =
350 offsetof(struct ksyms_hdr, kh_shstrtab);
351 hdr->kh_shdr[SHDR_SHSTRTAB].sh_size = sizeof(ksyms_shstrtab);
352 hdr->kh_shdr[SHDR_SHSTRTAB].sh_link = 0;
353 hdr->kh_shdr[SHDR_SHSTRTAB].sh_info = 0;
354 hdr->kh_shdr[SHDR_SHSTRTAB].sh_addralign = 0 /* sizeof(char) */;
355 hdr->kh_shdr[SHDR_SHSTRTAB].sh_entsize = 0;
357 /* Copy shstrtab into the header. */
358 bcopy(ksyms_shstrtab, hdr->kh_shstrtab, sizeof(ksyms_shstrtab));
361 to.to_symoff = hdr->kh_shdr[SHDR_SYMTAB].sh_offset;
362 to.to_stroff = hdr->kh_shdr[SHDR_STRTAB].sh_offset;
364 to.to_resid = sc->sc_objsz - sizeof(struct ksyms_hdr);
367 error = ksyms_emit(sc, hdr, 0, sizeof(*hdr));
372 /* Add symbol and string tables for each kernel module. */
373 error = linker_file_foreach(ksyms_add, &to);
376 if (to.to_resid != 0)
382 ksyms_cdevpriv_dtr(void *data)
384 struct ksyms_softc *sc;
387 sc = (struct ksyms_softc *)data;
389 sx_xlock(&ksyms_mtx);
390 LIST_REMOVE(sc, sc_list);
391 sx_xunlock(&ksyms_mtx);
394 vm_object_deallocate(obj);
399 ksyms_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
402 struct ksyms_softc *sc;
408 * Limit one open() per process. The process must close()
409 * before open()'ing again.
411 sx_xlock(&ksyms_mtx);
412 LIST_FOREACH(sc, &ksyms_list, sc_list) {
413 if (sc->sc_proc == td->td_proc) {
414 sx_xunlock(&ksyms_mtx);
419 sc = malloc(sizeof(*sc), M_KSYMS, M_WAITOK | M_ZERO);
420 sc->sc_proc = td->td_proc;
421 LIST_INSERT_HEAD(&ksyms_list, sc, sc_list);
422 sx_xunlock(&ksyms_mtx);
424 error = devfs_set_cdevpriv(sc, ksyms_cdevpriv_dtr);
426 ksyms_cdevpriv_dtr(sc);
431 * MOD_SLOCK doesn't work here (because of a lock reversal with
432 * KLD_SLOCK). Therefore, simply try up to 3 times to get a "clean"
433 * snapshot of the kernel symbol table. This should work fine in the
434 * rare case of a kernel module being loaded/unloaded at the same
437 for (try = 0; try < 3; try++) {
438 ksyms_size_calc(&ts);
439 elfsz = sizeof(struct ksyms_hdr) + ts.ts_symsz + ts.ts_strsz;
441 object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(elfsz),
442 VM_PROT_ALL, 0, td->td_ucred);
444 sc->sc_objsz = elfsz;
446 error = ksyms_snapshot(sc, &ts);
450 vm_object_deallocate(sc->sc_obj);
457 ksyms_read(struct cdev *dev, struct uio *uio, int flags __unused)
459 struct ksyms_softc *sc;
462 error = devfs_get_cdevpriv((void **)&sc);
465 return (uiomove_object(sc->sc_obj, sc->sc_objsz, uio));
469 ksyms_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
470 vm_object_t *objp, int nprot)
472 struct ksyms_softc *sc;
476 error = devfs_get_cdevpriv((void **)&sc);
480 if (*offset < 0 || *offset >= round_page(sc->sc_objsz) ||
481 size > round_page(sc->sc_objsz) - *offset ||
482 (nprot & ~PROT_READ) != 0)
486 vm_object_reference(obj);
492 ksyms_modevent(module_t mod __unused, int type, void *data __unused)
499 sx_init(&ksyms_mtx, "KSyms mtx");
500 ksyms_dev = make_dev(&ksyms_cdevsw, 0, UID_ROOT, GID_WHEEL,
504 if (!LIST_EMPTY(&ksyms_list))
506 destroy_dev(ksyms_dev);
507 sx_destroy(&ksyms_mtx);
518 DEV_MODULE(ksyms, ksyms_modevent, NULL);
519 MODULE_VERSION(ksyms, 1);