xref: /freebsd/sys/dev/ksyms/ksyms.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
100a5db46SStacey Son /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
400a5db46SStacey Son  * Copyright (c) 2008-2009, Stacey Son <sson@freebsd.org>
500a5db46SStacey Son  * All rights reserved.
600a5db46SStacey Son  *
700a5db46SStacey Son  * Redistribution and use in source and binary forms, with or without
800a5db46SStacey Son  * modification, are permitted provided that the following conditions
900a5db46SStacey Son  * are met:
1000a5db46SStacey Son  * 1. Redistributions of source code must retain the above copyright
1100a5db46SStacey Son  *    notice, this list of conditions and the following disclaimer.
1200a5db46SStacey Son  * 2. Redistributions in binary form must reproduce the above copyright
1300a5db46SStacey Son  *    notice, this list of conditions and the following disclaimer in the
1400a5db46SStacey Son  *    documentation and/or other materials provided with the distribution.
1500a5db46SStacey Son  *
1600a5db46SStacey Son  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1700a5db46SStacey Son  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1800a5db46SStacey Son  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1900a5db46SStacey Son  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2000a5db46SStacey Son  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2100a5db46SStacey Son  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2200a5db46SStacey Son  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2300a5db46SStacey Son  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2400a5db46SStacey Son  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2500a5db46SStacey Son  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2600a5db46SStacey Son  * SUCH DAMAGE.
2700a5db46SStacey Son  */
2800a5db46SStacey Son 
2900a5db46SStacey Son #include <sys/param.h>
3000a5db46SStacey Son #include <sys/systm.h>
3100a5db46SStacey Son #include <sys/kernel.h>
3200a5db46SStacey Son 
3300a5db46SStacey Son #include <sys/conf.h>
3400a5db46SStacey Son #include <sys/elf.h>
3500a5db46SStacey Son #include <sys/linker.h>
3600a5db46SStacey Son #include <sys/malloc.h>
3700a5db46SStacey Son #include <sys/mman.h>
3800a5db46SStacey Son #include <sys/module.h>
3900a5db46SStacey Son #include <sys/proc.h>
4000a5db46SStacey Son #include <sys/queue.h>
4100a5db46SStacey Son #include <sys/resourcevar.h>
42fbf2a778SKonstantin Belousov #include <sys/rwlock.h>
4300a5db46SStacey Son #include <sys/stat.h>
4422e406c8SMark Johnston #include <sys/sx.h>
4500a5db46SStacey Son #include <sys/uio.h>
4600a5db46SStacey Son 
4700a5db46SStacey Son #include <machine/elf.h>
4800a5db46SStacey Son 
4900a5db46SStacey Son #include <vm/pmap.h>
5000a5db46SStacey Son #include <vm/vm.h>
5100a5db46SStacey Son #include <vm/vm_extern.h>
5222e406c8SMark Johnston #include <vm/vm_object.h>
53fbf2a778SKonstantin Belousov #include <vm/vm_page.h>
54fbf2a778SKonstantin Belousov #include <vm/vm_pager.h>
5500a5db46SStacey Son 
5600a5db46SStacey Son #include "linker_if.h"
5700a5db46SStacey Son 
5800a5db46SStacey Son #define SHDR_NULL	0
5900a5db46SStacey Son #define SHDR_SYMTAB	1
6000a5db46SStacey Son #define SHDR_STRTAB	2
6100a5db46SStacey Son #define SHDR_SHSTRTAB	3
6200a5db46SStacey Son 
6300a5db46SStacey Son #define SHDR_NUM	4
6400a5db46SStacey Son 
6500a5db46SStacey Son #define STR_SYMTAB	".symtab"
6600a5db46SStacey Son #define STR_STRTAB	".strtab"
6700a5db46SStacey Son #define STR_SHSTRTAB	".shstrtab"
6800a5db46SStacey Son 
6900a5db46SStacey Son #define KSYMS_DNAME	"ksyms"
7000a5db46SStacey Son 
7100a5db46SStacey Son static d_open_t ksyms_open;
7200a5db46SStacey Son static d_read_t ksyms_read;
7322e406c8SMark Johnston static d_mmap_single_t ksyms_mmap_single;
7400a5db46SStacey Son 
7500a5db46SStacey Son static struct cdevsw ksyms_cdevsw = {
7600a5db46SStacey Son 	.d_version =	D_VERSION,
7706955539SMark Johnston 	.d_flags =	0,
7800a5db46SStacey Son 	.d_open =	ksyms_open,
7900a5db46SStacey Son 	.d_read =	ksyms_read,
8022e406c8SMark Johnston 	.d_mmap_single = ksyms_mmap_single,
8100a5db46SStacey Son 	.d_name =	KSYMS_DNAME
8200a5db46SStacey Son };
8300a5db46SStacey Son 
8400a5db46SStacey Son struct ksyms_softc {
8500a5db46SStacey Son 	LIST_ENTRY(ksyms_softc)	sc_list;
8600a5db46SStacey Son 	vm_offset_t		sc_uaddr;
8700a5db46SStacey Son 	size_t			sc_usize;
8822e406c8SMark Johnston 	vm_object_t		sc_obj;
8922e406c8SMark Johnston 	vm_size_t		sc_objsz;
9000a5db46SStacey Son 	struct proc	       *sc_proc;
9100a5db46SStacey Son };
9200a5db46SStacey Son 
9322e406c8SMark Johnston static struct sx		 ksyms_mtx;
9400a5db46SStacey Son static struct cdev		*ksyms_dev;
951e163462SMark Johnston static LIST_HEAD(, ksyms_softc)	 ksyms_list = LIST_HEAD_INITIALIZER(ksyms_list);
9600a5db46SStacey Son 
9700a5db46SStacey Son static const char	ksyms_shstrtab[] =
9800a5db46SStacey Son 	"\0" STR_SYMTAB "\0" STR_STRTAB "\0" STR_SHSTRTAB "\0";
9900a5db46SStacey Son 
10000a5db46SStacey Son struct ksyms_hdr {
10100a5db46SStacey Son 	Elf_Ehdr	kh_ehdr;
10200a5db46SStacey Son 	Elf_Phdr	kh_txtphdr;
10300a5db46SStacey Son 	Elf_Phdr	kh_datphdr;
10400a5db46SStacey Son 	Elf_Shdr	kh_shdr[SHDR_NUM];
10500a5db46SStacey Son 	char		kh_shstrtab[sizeof(ksyms_shstrtab)];
10600a5db46SStacey Son };
10700a5db46SStacey Son 
10800a5db46SStacey Son struct tsizes {
10900a5db46SStacey Son 	size_t		ts_symsz;
11000a5db46SStacey Son 	size_t		ts_strsz;
11100a5db46SStacey Son };
11200a5db46SStacey Son 
11300a5db46SStacey Son struct toffsets {
11422e406c8SMark Johnston 	struct ksyms_softc *to_sc;
11500a5db46SStacey Son 	vm_offset_t	to_symoff;
11600a5db46SStacey Son 	vm_offset_t	to_stroff;
11700a5db46SStacey Son 	unsigned	to_stridx;
11800a5db46SStacey Son 	size_t		to_resid;
11900a5db46SStacey Son };
12000a5db46SStacey Son 
12100a5db46SStacey Son static MALLOC_DEFINE(M_KSYMS, "KSYMS", "Kernel Symbol Table");
12200a5db46SStacey Son 
12300a5db46SStacey Son /*
12400a5db46SStacey Son  * Get the symbol and string table sizes for a kernel module. Add it to the
12500a5db46SStacey Son  * running total.
12600a5db46SStacey Son  */
12700a5db46SStacey Son static int
ksyms_size_permod(linker_file_t lf,void * arg)12800a5db46SStacey Son ksyms_size_permod(linker_file_t lf, void *arg)
12900a5db46SStacey Son {
13000a5db46SStacey Son 	struct tsizes *ts;
131e76f11f4SAndriy Gapon 	const Elf_Sym *symtab;
13200a5db46SStacey Son 	caddr_t strtab;
13300a5db46SStacey Son 	long syms;
13400a5db46SStacey Son 
13500a5db46SStacey Son 	ts = arg;
13600a5db46SStacey Son 
13700a5db46SStacey Son 	syms = LINKER_SYMTAB_GET(lf, &symtab);
13800a5db46SStacey Son 	ts->ts_symsz += syms * sizeof(Elf_Sym);
13900a5db46SStacey Son 	ts->ts_strsz += LINKER_STRTAB_GET(lf, &strtab);
14000a5db46SStacey Son 
14100a5db46SStacey Son 	return (0);
14200a5db46SStacey Son }
14300a5db46SStacey Son 
14400a5db46SStacey Son /*
14500a5db46SStacey Son  * For kernel module get the symbol and string table sizes, returning the
14600a5db46SStacey Son  * totals in *ts.
14700a5db46SStacey Son  */
14800a5db46SStacey Son static void
ksyms_size_calc(struct tsizes * ts)14900a5db46SStacey Son ksyms_size_calc(struct tsizes *ts)
15000a5db46SStacey Son {
1511e163462SMark Johnston 
15200a5db46SStacey Son 	ts->ts_symsz = 0;
15300a5db46SStacey Son 	ts->ts_strsz = 0;
15400a5db46SStacey Son 
15500a5db46SStacey Son 	(void)linker_file_foreach(ksyms_size_permod, ts);
15600a5db46SStacey Son }
15700a5db46SStacey Son 
15822e406c8SMark Johnston static int
ksyms_emit(struct ksyms_softc * sc,void * buf,off_t off,size_t sz)15922e406c8SMark Johnston ksyms_emit(struct ksyms_softc *sc, void *buf, off_t off, size_t sz)
16022e406c8SMark Johnston {
16122e406c8SMark Johnston 	struct iovec iov;
16222e406c8SMark Johnston 	struct uio uio;
16322e406c8SMark Johnston 
16422e406c8SMark Johnston 	iov.iov_base = buf;
16522e406c8SMark Johnston 	iov.iov_len = sz;
16622e406c8SMark Johnston 	uio.uio_iov = &iov;
16722e406c8SMark Johnston 	uio.uio_iovcnt = 1;
16822e406c8SMark Johnston 	uio.uio_offset = off;
16922e406c8SMark Johnston 	uio.uio_resid = (ssize_t)sz;
17022e406c8SMark Johnston 	uio.uio_segflg = UIO_SYSSPACE;
17122e406c8SMark Johnston 	uio.uio_rw = UIO_WRITE;
17222e406c8SMark Johnston 	uio.uio_td = curthread;
17322e406c8SMark Johnston 
17422e406c8SMark Johnston 	return (uiomove_object(sc->sc_obj, sc->sc_objsz, &uio));
17522e406c8SMark Johnston }
17600a5db46SStacey Son 
1771e163462SMark Johnston #define SYMBLKSZ	(256 * sizeof(Elf_Sym))
17800a5db46SStacey Son 
17900a5db46SStacey Son /*
18000a5db46SStacey Son  * For a kernel module, add the symbol and string tables into the
18100a5db46SStacey Son  * snapshot buffer.  Fix up the offsets in the tables.
18200a5db46SStacey Son  */
18300a5db46SStacey Son static int
ksyms_add(linker_file_t lf,void * arg)18400a5db46SStacey Son ksyms_add(linker_file_t lf, void *arg)
18500a5db46SStacey Son {
1861e163462SMark Johnston 	char *buf;
18722e406c8SMark Johnston 	struct ksyms_softc *sc;
18800a5db46SStacey Son 	struct toffsets *to;
189e76f11f4SAndriy Gapon 	const Elf_Sym *symtab;
190e76f11f4SAndriy Gapon 	Elf_Sym *symp;
19100a5db46SStacey Son 	caddr_t strtab;
19222e406c8SMark Johnston 	size_t len, numsyms, strsz, symsz;
19300a5db46SStacey Son 	linker_symval_t symval;
19422e406c8SMark Johnston 	int error, i, nsyms;
1958740b338SJustin Hibbits 	bool fixup;
19600a5db46SStacey Son 
19722e406c8SMark Johnston 	buf = malloc(SYMBLKSZ, M_KSYMS, M_WAITOK);
19800a5db46SStacey Son 	to = arg;
19922e406c8SMark Johnston 	sc = to->to_sc;
20000a5db46SStacey Son 
20100a5db46SStacey Son 	MOD_SLOCK;
20200a5db46SStacey Son 	numsyms =  LINKER_SYMTAB_GET(lf, &symtab);
20300a5db46SStacey Son 	strsz = LINKER_STRTAB_GET(lf, &strtab);
20400a5db46SStacey Son 	symsz = numsyms * sizeof(Elf_Sym);
20500a5db46SStacey Son 
20640b664f6SBrandon Bergren #ifdef RELOCATABLE_KERNEL
2078740b338SJustin Hibbits 	fixup = true;
2088740b338SJustin Hibbits #else
2098740b338SJustin Hibbits 	fixup = lf->id > 1;
2108740b338SJustin Hibbits #endif
2118740b338SJustin Hibbits 
21200a5db46SStacey Son 	while (symsz > 0) {
21300a5db46SStacey Son 		len = min(SYMBLKSZ, symsz);
21400a5db46SStacey Son 		bcopy(symtab, buf, len);
21500a5db46SStacey Son 
21600a5db46SStacey Son 		/*
21700a5db46SStacey Son 		 * Fix up symbol table for kernel modules:
21800a5db46SStacey Son 		 *   string offsets need adjusted
21900a5db46SStacey Son 		 *   symbol values made absolute
22000a5db46SStacey Son 		 */
22100a5db46SStacey Son 		symp = (Elf_Sym *) buf;
22200a5db46SStacey Son 		nsyms = len / sizeof(Elf_Sym);
22300a5db46SStacey Son 		for (i = 0; i < nsyms; i++) {
22400a5db46SStacey Son 			symp[i].st_name += to->to_stridx;
2258740b338SJustin Hibbits 			if (fixup && LINKER_SYMBOL_VALUES(lf,
22600a5db46SStacey Son 			    (c_linker_sym_t)&symtab[i], &symval) == 0) {
22700a5db46SStacey Son 				symp[i].st_value = (uintptr_t)symval.value;
22800a5db46SStacey Son 			}
22900a5db46SStacey Son 		}
23000a5db46SStacey Son 
23100a5db46SStacey Son 		if (len > to->to_resid) {
23200a5db46SStacey Son 			MOD_SUNLOCK;
23300a5db46SStacey Son 			free(buf, M_KSYMS);
23400a5db46SStacey Son 			return (ENXIO);
2351e163462SMark Johnston 		}
23600a5db46SStacey Son 		to->to_resid -= len;
23722e406c8SMark Johnston 		error = ksyms_emit(sc, buf, to->to_symoff, len);
23822e406c8SMark Johnston 		to->to_symoff += len;
23922e406c8SMark Johnston 		if (error != 0) {
24022e406c8SMark Johnston 			MOD_SUNLOCK;
24122e406c8SMark Johnston 			free(buf, M_KSYMS);
24222e406c8SMark Johnston 			return (error);
24322e406c8SMark Johnston 		}
24400a5db46SStacey Son 
24500a5db46SStacey Son 		symtab += nsyms;
24600a5db46SStacey Son 		symsz -= len;
24700a5db46SStacey Son 	}
24800a5db46SStacey Son 	free(buf, M_KSYMS);
24900a5db46SStacey Son 	MOD_SUNLOCK;
25000a5db46SStacey Son 
25100a5db46SStacey Son 	if (strsz > to->to_resid)
25200a5db46SStacey Son 		return (ENXIO);
25300a5db46SStacey Son 	to->to_resid -= strsz;
25422e406c8SMark Johnston 	error = ksyms_emit(sc, strtab, to->to_stroff, strsz);
25522e406c8SMark Johnston 	to->to_stroff += strsz;
25600a5db46SStacey Son 	to->to_stridx += strsz;
25700a5db46SStacey Son 
25822e406c8SMark Johnston 	return (error);
25900a5db46SStacey Son }
26000a5db46SStacey Son 
26100a5db46SStacey Son /*
26200a5db46SStacey Son  * Create a single ELF symbol table for the kernel and kernel modules loaded
26300a5db46SStacey Son  * at this time. Write this snapshot out in the process address space. Return
26400a5db46SStacey Son  * 0 on success, otherwise error.
26500a5db46SStacey Son  */
26600a5db46SStacey Son static int
ksyms_snapshot(struct ksyms_softc * sc,struct tsizes * ts)26722e406c8SMark Johnston ksyms_snapshot(struct ksyms_softc *sc, struct tsizes *ts)
26800a5db46SStacey Son {
26900a5db46SStacey Son 	struct toffsets	to;
27022e406c8SMark Johnston 	struct ksyms_hdr *hdr;
27122e406c8SMark Johnston 	int error;
27200a5db46SStacey Son 
27300a5db46SStacey Son 	hdr = malloc(sizeof(*hdr), M_KSYMS, M_WAITOK | M_ZERO);
27400a5db46SStacey Son 
27500a5db46SStacey Son 	/*
27600a5db46SStacey Son 	 * Create the ELF header.
27700a5db46SStacey Son 	 */
27800a5db46SStacey Son 	hdr->kh_ehdr.e_ident[EI_PAD] = 0;
27900a5db46SStacey Son 	hdr->kh_ehdr.e_ident[EI_MAG0] = ELFMAG0;
28000a5db46SStacey Son 	hdr->kh_ehdr.e_ident[EI_MAG1] = ELFMAG1;
28100a5db46SStacey Son 	hdr->kh_ehdr.e_ident[EI_MAG2] = ELFMAG2;
28200a5db46SStacey Son 	hdr->kh_ehdr.e_ident[EI_MAG3] = ELFMAG3;
28300a5db46SStacey Son 	hdr->kh_ehdr.e_ident[EI_DATA] = ELF_DATA;
28400a5db46SStacey Son 	hdr->kh_ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
28500a5db46SStacey Son 	hdr->kh_ehdr.e_ident[EI_CLASS] = ELF_CLASS;
28600a5db46SStacey Son 	hdr->kh_ehdr.e_ident[EI_VERSION] = EV_CURRENT;
28700a5db46SStacey Son 	hdr->kh_ehdr.e_ident[EI_ABIVERSION] = 0;
28800a5db46SStacey Son 	hdr->kh_ehdr.e_type = ET_EXEC;
28900a5db46SStacey Son 	hdr->kh_ehdr.e_machine = ELF_ARCH;
29000a5db46SStacey Son 	hdr->kh_ehdr.e_version = EV_CURRENT;
29100a5db46SStacey Son 	hdr->kh_ehdr.e_entry = 0;
29200a5db46SStacey Son 	hdr->kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_txtphdr);
29300a5db46SStacey Son 	hdr->kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr);
29400a5db46SStacey Son 	hdr->kh_ehdr.e_flags = 0;
29500a5db46SStacey Son 	hdr->kh_ehdr.e_ehsize = sizeof(Elf_Ehdr);
29600a5db46SStacey Son 	hdr->kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
29700a5db46SStacey Son 	hdr->kh_ehdr.e_phnum = 2;	/* Text and Data */
29800a5db46SStacey Son 	hdr->kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
29900a5db46SStacey Son 	hdr->kh_ehdr.e_shnum = SHDR_NUM;
30000a5db46SStacey Son 	hdr->kh_ehdr.e_shstrndx = SHDR_SHSTRTAB;
30100a5db46SStacey Son 
30200a5db46SStacey Son 	/*
3031e163462SMark Johnston 	 * Add both the text and data program headers.
30400a5db46SStacey Son 	 */
30500a5db46SStacey Son 	hdr->kh_txtphdr.p_type = PT_LOAD;
30600a5db46SStacey Son 	/* XXX - is there a way to put the actual .text addr/size here? */
30700a5db46SStacey Son 	hdr->kh_txtphdr.p_vaddr = 0;
30800a5db46SStacey Son 	hdr->kh_txtphdr.p_memsz = 0;
30900a5db46SStacey Son 	hdr->kh_txtphdr.p_flags = PF_R | PF_X;
31000a5db46SStacey Son 
31100a5db46SStacey Son 	hdr->kh_datphdr.p_type = PT_LOAD;
31200a5db46SStacey Son 	/* XXX - is there a way to put the actual .data addr/size here? */
31300a5db46SStacey Son 	hdr->kh_datphdr.p_vaddr = 0;
31400a5db46SStacey Son 	hdr->kh_datphdr.p_memsz = 0;
31500a5db46SStacey Son 	hdr->kh_datphdr.p_flags = PF_R | PF_W | PF_X;
31600a5db46SStacey Son 
31700a5db46SStacey Son 	/*
3181e163462SMark Johnston 	 * Add the section headers: null, symtab, strtab, shstrtab.
31900a5db46SStacey Son 	 */
32000a5db46SStacey Son 
32100a5db46SStacey Son 	/* First section header - null */
32200a5db46SStacey Son 
32300a5db46SStacey Son 	/* Second section header - symtab */
32400a5db46SStacey Son 	hdr->kh_shdr[SHDR_SYMTAB].sh_name = 1; /* String offset (skip null) */
32500a5db46SStacey Son 	hdr->kh_shdr[SHDR_SYMTAB].sh_type = SHT_SYMTAB;
32600a5db46SStacey Son 	hdr->kh_shdr[SHDR_SYMTAB].sh_flags = 0;
32700a5db46SStacey Son 	hdr->kh_shdr[SHDR_SYMTAB].sh_addr = 0;
32800a5db46SStacey Son 	hdr->kh_shdr[SHDR_SYMTAB].sh_offset = sizeof(*hdr);
32900a5db46SStacey Son 	hdr->kh_shdr[SHDR_SYMTAB].sh_size = ts->ts_symsz;
33000a5db46SStacey Son 	hdr->kh_shdr[SHDR_SYMTAB].sh_link = SHDR_STRTAB;
33100a5db46SStacey Son 	hdr->kh_shdr[SHDR_SYMTAB].sh_info = ts->ts_symsz / sizeof(Elf_Sym);
33200a5db46SStacey Son 	hdr->kh_shdr[SHDR_SYMTAB].sh_addralign = sizeof(long);
33300a5db46SStacey Son 	hdr->kh_shdr[SHDR_SYMTAB].sh_entsize = sizeof(Elf_Sym);
33400a5db46SStacey Son 
33500a5db46SStacey Son 	/* Third section header - strtab */
33600a5db46SStacey Son 	hdr->kh_shdr[SHDR_STRTAB].sh_name = 1 + sizeof(STR_SYMTAB);
33700a5db46SStacey Son 	hdr->kh_shdr[SHDR_STRTAB].sh_type = SHT_STRTAB;
33800a5db46SStacey Son 	hdr->kh_shdr[SHDR_STRTAB].sh_flags = 0;
33900a5db46SStacey Son 	hdr->kh_shdr[SHDR_STRTAB].sh_addr = 0;
34000a5db46SStacey Son 	hdr->kh_shdr[SHDR_STRTAB].sh_offset =
34100a5db46SStacey Son 	    hdr->kh_shdr[SHDR_SYMTAB].sh_offset + ts->ts_symsz;
34200a5db46SStacey Son 	hdr->kh_shdr[SHDR_STRTAB].sh_size = ts->ts_strsz;
34300a5db46SStacey Son 	hdr->kh_shdr[SHDR_STRTAB].sh_link = 0;
34400a5db46SStacey Son 	hdr->kh_shdr[SHDR_STRTAB].sh_info = 0;
34500a5db46SStacey Son 	hdr->kh_shdr[SHDR_STRTAB].sh_addralign = sizeof(char);
34600a5db46SStacey Son 	hdr->kh_shdr[SHDR_STRTAB].sh_entsize = 0;
34700a5db46SStacey Son 
34800a5db46SStacey Son 	/* Fourth section - shstrtab */
34900a5db46SStacey Son 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_name = 1 + sizeof(STR_SYMTAB) +
35000a5db46SStacey Son 	    sizeof(STR_STRTAB);
35100a5db46SStacey Son 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_type = SHT_STRTAB;
35200a5db46SStacey Son 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_flags = 0;
35300a5db46SStacey Son 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_addr = 0;
35400a5db46SStacey Son 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_offset =
35500a5db46SStacey Son 	    offsetof(struct ksyms_hdr, kh_shstrtab);
35600a5db46SStacey Son 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_size = sizeof(ksyms_shstrtab);
35700a5db46SStacey Son 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_link = 0;
35800a5db46SStacey Son 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_info = 0;
35900a5db46SStacey Son 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_addralign = 0 /* sizeof(char) */;
36000a5db46SStacey Son 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_entsize = 0;
36100a5db46SStacey Son 
3621e163462SMark Johnston 	/* Copy shstrtab into the header. */
36300a5db46SStacey Son 	bcopy(ksyms_shstrtab, hdr->kh_shstrtab, sizeof(ksyms_shstrtab));
36400a5db46SStacey Son 
36522e406c8SMark Johnston 	to.to_sc = sc;
36622e406c8SMark Johnston 	to.to_symoff = hdr->kh_shdr[SHDR_SYMTAB].sh_offset;
36722e406c8SMark Johnston 	to.to_stroff = hdr->kh_shdr[SHDR_STRTAB].sh_offset;
36800a5db46SStacey Son 	to.to_stridx = 0;
36922e406c8SMark Johnston 	to.to_resid = sc->sc_objsz - sizeof(struct ksyms_hdr);
37000a5db46SStacey Son 
3711e163462SMark Johnston 	/* emit header */
37222e406c8SMark Johnston 	error = ksyms_emit(sc, hdr, 0, sizeof(*hdr));
37300a5db46SStacey Son 	free(hdr, M_KSYMS);
37422e406c8SMark Johnston 	if (error != 0)
37522e406c8SMark Johnston 		return (error);
37600a5db46SStacey Son 
3771e163462SMark Johnston 	/* Add symbol and string tables for each kernel module. */
37800a5db46SStacey Son 	error = linker_file_foreach(ksyms_add, &to);
37922e406c8SMark Johnston 	if (error != 0)
38022e406c8SMark Johnston 		return (error);
38100a5db46SStacey Son 	if (to.to_resid != 0)
38200a5db46SStacey Son 		return (ENXIO);
38322e406c8SMark Johnston 	return (0);
38400a5db46SStacey Son }
38500a5db46SStacey Son 
38600a5db46SStacey Son static void
ksyms_cdevpriv_dtr(void * data)38700a5db46SStacey Son ksyms_cdevpriv_dtr(void *data)
38800a5db46SStacey Son {
38900a5db46SStacey Son 	struct ksyms_softc *sc;
39022e406c8SMark Johnston 	vm_object_t obj;
39100a5db46SStacey Son 
39200a5db46SStacey Son 	sc = (struct ksyms_softc *)data;
39300a5db46SStacey Son 
39422e406c8SMark Johnston 	sx_xlock(&ksyms_mtx);
39500a5db46SStacey Son 	LIST_REMOVE(sc, sc_list);
39622e406c8SMark Johnston 	sx_xunlock(&ksyms_mtx);
39722e406c8SMark Johnston 	obj = sc->sc_obj;
39822e406c8SMark Johnston 	if (obj != NULL)
39922e406c8SMark Johnston 		vm_object_deallocate(obj);
40000a5db46SStacey Son 	free(sc, M_KSYMS);
40100a5db46SStacey Son }
40200a5db46SStacey Son 
40300a5db46SStacey Son static int
ksyms_open(struct cdev * dev,int flags,int fmt __unused,struct thread * td)40400a5db46SStacey Son ksyms_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
40500a5db46SStacey Son {
40600a5db46SStacey Son 	struct tsizes ts;
4071e163462SMark Johnston 	struct ksyms_softc *sc;
40887382b22SMark Johnston 	vm_object_t object;
40922e406c8SMark Johnston 	vm_size_t elfsz;
41000a5db46SStacey Son 	int error, try;
41100a5db46SStacey Son 
41200a5db46SStacey Son 	/*
41300a5db46SStacey Son 	 * Limit one open() per process. The process must close()
41400a5db46SStacey Son 	 * before open()'ing again.
41500a5db46SStacey Son 	 */
41622e406c8SMark Johnston 	sx_xlock(&ksyms_mtx);
41700a5db46SStacey Son 	LIST_FOREACH(sc, &ksyms_list, sc_list) {
41800a5db46SStacey Son 		if (sc->sc_proc == td->td_proc) {
41922e406c8SMark Johnston 			sx_xunlock(&ksyms_mtx);
42000a5db46SStacey Son 			return (EBUSY);
42100a5db46SStacey Son 		}
42200a5db46SStacey Son 	}
42300a5db46SStacey Son 
42422e406c8SMark Johnston 	sc = malloc(sizeof(*sc), M_KSYMS, M_WAITOK | M_ZERO);
42500a5db46SStacey Son 	sc->sc_proc = td->td_proc;
42600a5db46SStacey Son 	LIST_INSERT_HEAD(&ksyms_list, sc, sc_list);
42722e406c8SMark Johnston 	sx_xunlock(&ksyms_mtx);
42800a5db46SStacey Son 
42900a5db46SStacey Son 	error = devfs_set_cdevpriv(sc, ksyms_cdevpriv_dtr);
43022e406c8SMark Johnston 	if (error != 0) {
43122e406c8SMark Johnston 		ksyms_cdevpriv_dtr(sc);
43222e406c8SMark Johnston 		return (error);
43322e406c8SMark Johnston 	}
43400a5db46SStacey Son 
43500a5db46SStacey Son 	/*
43600a5db46SStacey Son 	 * MOD_SLOCK doesn't work here (because of a lock reversal with
43700a5db46SStacey Son 	 * KLD_SLOCK).  Therefore, simply try up to 3 times to get a "clean"
43800a5db46SStacey Son 	 * snapshot of the kernel symbol table.  This should work fine in the
43900a5db46SStacey Son 	 * rare case of a kernel module being loaded/unloaded at the same
44000a5db46SStacey Son 	 * time.
44100a5db46SStacey Son 	 */
44200a5db46SStacey Son 	for (try = 0; try < 3; try++) {
44300a5db46SStacey Son 		ksyms_size_calc(&ts);
44422e406c8SMark Johnston 		elfsz = sizeof(struct ksyms_hdr) + ts.ts_symsz + ts.ts_strsz;
44500a5db46SStacey Son 
446fbf2a778SKonstantin Belousov 		object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(elfsz),
447fbf2a778SKonstantin Belousov 		    VM_PROT_ALL, 0, td->td_ucred);
44887382b22SMark Johnston 		sc->sc_obj = object;
44922e406c8SMark Johnston 		sc->sc_objsz = elfsz;
45000a5db46SStacey Son 
45122e406c8SMark Johnston 		error = ksyms_snapshot(sc, &ts);
4521e163462SMark Johnston 		if (error == 0)
45322e406c8SMark Johnston 			break;
45400a5db46SStacey Son 
45522e406c8SMark Johnston 		vm_object_deallocate(sc->sc_obj);
45622e406c8SMark Johnston 		sc->sc_obj = NULL;
45700a5db46SStacey Son 	}
45800a5db46SStacey Son 	return (error);
45900a5db46SStacey Son }
46000a5db46SStacey Son 
46100a5db46SStacey Son static int
ksyms_read(struct cdev * dev,struct uio * uio,int flags __unused)46200a5db46SStacey Son ksyms_read(struct cdev *dev, struct uio *uio, int flags __unused)
46300a5db46SStacey Son {
46400a5db46SStacey Son 	struct ksyms_softc *sc;
46522e406c8SMark Johnston 	int error;
46622e406c8SMark Johnston 
46722e406c8SMark Johnston 	error = devfs_get_cdevpriv((void **)&sc);
46822e406c8SMark Johnston 	if (error != 0)
46922e406c8SMark Johnston 		return (error);
47022e406c8SMark Johnston 	return (uiomove_object(sc->sc_obj, sc->sc_objsz, uio));
47122e406c8SMark Johnston }
47222e406c8SMark Johnston 
47322e406c8SMark Johnston static int
ksyms_mmap_single(struct cdev * dev,vm_ooffset_t * offset,vm_size_t size,vm_object_t * objp,int nprot)47422e406c8SMark Johnston ksyms_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
47522e406c8SMark Johnston     vm_object_t *objp, int nprot)
47622e406c8SMark Johnston {
47722e406c8SMark Johnston 	struct ksyms_softc *sc;
47822e406c8SMark Johnston 	vm_object_t obj;
4791e163462SMark Johnston 	int error;
48000a5db46SStacey Son 
48100a5db46SStacey Son 	error = devfs_get_cdevpriv((void **)&sc);
4821e163462SMark Johnston 	if (error != 0)
48300a5db46SStacey Son 		return (error);
48400a5db46SStacey Son 
485f9cc8410SEric van Gyzen 	if (*offset >= round_page(sc->sc_objsz) ||
48622e406c8SMark Johnston 	    size > round_page(sc->sc_objsz) - *offset ||
48722e406c8SMark Johnston 	    (nprot & ~PROT_READ) != 0)
48822e406c8SMark Johnston 		return (EINVAL);
48900a5db46SStacey Son 
49022e406c8SMark Johnston 	obj = sc->sc_obj;
49122e406c8SMark Johnston 	vm_object_reference(obj);
49222e406c8SMark Johnston 	*objp = obj;
49300a5db46SStacey Son 	return (0);
49400a5db46SStacey Son }
49500a5db46SStacey Son 
49600a5db46SStacey Son static int
ksyms_modevent(module_t mod __unused,int type,void * data __unused)49700a5db46SStacey Son ksyms_modevent(module_t mod __unused, int type, void *data __unused)
49800a5db46SStacey Son {
4991e163462SMark Johnston 	int error;
50000a5db46SStacey Son 
5011e163462SMark Johnston 	error = 0;
50200a5db46SStacey Son 	switch (type) {
50300a5db46SStacey Son 	case MOD_LOAD:
50422e406c8SMark Johnston 		sx_init(&ksyms_mtx, "KSyms mtx");
50500a5db46SStacey Son 		ksyms_dev = make_dev(&ksyms_cdevsw, 0, UID_ROOT, GID_WHEEL,
5067c86ae1aSMark Johnston 		    0400, KSYMS_DNAME);
50700a5db46SStacey Son 		break;
50800a5db46SStacey Son 	case MOD_UNLOAD:
50900a5db46SStacey Son 		if (!LIST_EMPTY(&ksyms_list))
51000a5db46SStacey Son 			return (EBUSY);
51100a5db46SStacey Son 		destroy_dev(ksyms_dev);
51222e406c8SMark Johnston 		sx_destroy(&ksyms_mtx);
51300a5db46SStacey Son 		break;
51400a5db46SStacey Son 	case MOD_SHUTDOWN:
51500a5db46SStacey Son 		break;
51600a5db46SStacey Son 	default:
51700a5db46SStacey Son 		error = EOPNOTSUPP;
51800a5db46SStacey Son 		break;
51900a5db46SStacey Son 	}
52000a5db46SStacey Son 	return (error);
52100a5db46SStacey Son }
52200a5db46SStacey Son 
52300a5db46SStacey Son DEV_MODULE(ksyms, ksyms_modevent, NULL);
52400a5db46SStacey Son MODULE_VERSION(ksyms, 1);
525