kern_ksyms.c revision 1.65 1 1.64 uebayasi /* $NetBSD: kern_ksyms.c,v 1.65 2011/07/28 13:42:16 uebayasi Exp $ */
2 1.38 skrll
3 1.39 ad /*-
4 1.39 ad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.39 ad * All rights reserved.
6 1.39 ad *
7 1.39 ad * This code is derived from software developed for The NetBSD Foundation
8 1.39 ad * by Andrew Doran.
9 1.39 ad *
10 1.39 ad * Redistribution and use in source and binary forms, with or without
11 1.39 ad * modification, are permitted provided that the following conditions
12 1.39 ad * are met:
13 1.39 ad * 1. Redistributions of source code must retain the above copyright
14 1.39 ad * notice, this list of conditions and the following disclaimer.
15 1.39 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.39 ad * notice, this list of conditions and the following disclaimer in the
17 1.39 ad * documentation and/or other materials provided with the distribution.
18 1.39 ad *
19 1.39 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.39 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.39 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.39 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.39 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.39 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.39 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.39 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.39 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.39 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.39 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.39 ad */
31 1.39 ad
32 1.1 ragge /*
33 1.1 ragge * Copyright (c) 2001, 2003 Anders Magnusson (ragge (at) ludd.luth.se).
34 1.1 ragge * All rights reserved.
35 1.1 ragge *
36 1.1 ragge * Redistribution and use in source and binary forms, with or without
37 1.1 ragge * modification, are permitted provided that the following conditions
38 1.1 ragge * are met:
39 1.1 ragge * 1. Redistributions of source code must retain the above copyright
40 1.1 ragge * notice, this list of conditions and the following disclaimer.
41 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 ragge * notice, this list of conditions and the following disclaimer in the
43 1.1 ragge * documentation and/or other materials provided with the distribution.
44 1.1 ragge * 3. The name of the author may not be used to endorse or promote products
45 1.1 ragge * derived from this software without specific prior written permission
46 1.1 ragge *
47 1.1 ragge * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 1.1 ragge * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 1.1 ragge * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 1.1 ragge * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 1.1 ragge * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 1.1 ragge * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 1.1 ragge * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 1.1 ragge * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 1.1 ragge * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 1.1 ragge * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 1.1 ragge */
58 1.1 ragge
59 1.1 ragge /*
60 1.1 ragge * Code to deal with in-kernel symbol table management + /dev/ksyms.
61 1.1 ragge *
62 1.1 ragge * For each loaded module the symbol table info is kept track of by a
63 1.1 ragge * struct, placed in a circular list. The first entry is the kernel
64 1.1 ragge * symbol table.
65 1.1 ragge */
66 1.1 ragge
67 1.1 ragge /*
68 1.1 ragge * TODO:
69 1.1 ragge *
70 1.39 ad * Add support for mmap, poll.
71 1.1 ragge */
72 1.11 jdolecek
73 1.11 jdolecek #include <sys/cdefs.h>
74 1.64 uebayasi __KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.65 2011/07/28 13:42:16 uebayasi Exp $");
75 1.1 ragge
76 1.49 pooka #if defined(_KERNEL) && defined(_KERNEL_OPT)
77 1.1 ragge #include "opt_ddb.h"
78 1.56 darran #include "opt_dtrace.h"
79 1.65 uebayasi #include "opt_ksyms.h"
80 1.1 ragge #endif
81 1.1 ragge
82 1.39 ad #define _KSYMS_PRIVATE
83 1.39 ad
84 1.1 ragge #include <sys/param.h>
85 1.1 ragge #include <sys/queue.h>
86 1.1 ragge #include <sys/exec.h>
87 1.1 ragge #include <sys/systm.h>
88 1.1 ragge #include <sys/conf.h>
89 1.39 ad #include <sys/kmem.h>
90 1.1 ragge #include <sys/proc.h>
91 1.39 ad #include <sys/atomic.h>
92 1.1 ragge #include <sys/ksyms.h>
93 1.1 ragge
94 1.1 ragge #ifdef DDB
95 1.1 ragge #include <ddb/db_output.h>
96 1.1 ragge #endif
97 1.1 ragge
98 1.1 ragge #include "ksyms.h"
99 1.1 ragge
100 1.56 darran #define KSYMS_MAX_ID 65536
101 1.56 darran #ifdef KDTRACE_HOOKS
102 1.56 darran static uint32_t ksyms_nmap[KSYMS_MAX_ID]; /* sorted symbol table map */
103 1.56 darran #else
104 1.56 darran static uint32_t *ksyms_nmap = NULL;
105 1.56 darran #endif
106 1.56 darran
107 1.39 ad static int ksyms_maxlen;
108 1.39 ad static bool ksyms_isopen;
109 1.39 ad static bool ksyms_initted;
110 1.39 ad static struct ksyms_hdr ksyms_hdr;
111 1.40 christos static kmutex_t ksyms_lock;
112 1.1 ragge
113 1.39 ad void ksymsattach(int);
114 1.40 christos static void ksyms_hdr_init(void *);
115 1.1 ragge static void ksyms_sizes_calc(void);
116 1.1 ragge
117 1.1 ragge #ifdef KSYMS_DEBUG
118 1.1 ragge #define FOLLOW_CALLS 1
119 1.1 ragge #define FOLLOW_MORE_CALLS 2
120 1.1 ragge #define FOLLOW_DEVKSYMS 4
121 1.1 ragge static int ksyms_debug;
122 1.1 ragge #endif
123 1.1 ragge
124 1.3 ragge #ifdef SYMTAB_SPACE
125 1.3 ragge #define SYMTAB_FILLER "|This is the symbol table!"
126 1.3 ragge
127 1.3 ragge char db_symtab[SYMTAB_SPACE] = SYMTAB_FILLER;
128 1.3 ragge int db_symtabsize = SYMTAB_SPACE;
129 1.3 ragge #endif
130 1.1 ragge
131 1.39 ad int ksyms_symsz;
132 1.39 ad int ksyms_strsz;
133 1.56 darran int ksyms_ctfsz;
134 1.39 ad TAILQ_HEAD(, ksyms_symtab) ksyms_symtabs =
135 1.39 ad TAILQ_HEAD_INITIALIZER(ksyms_symtabs);
136 1.39 ad static struct ksyms_symtab kernel_symtab;
137 1.1 ragge
138 1.33 christos static int
139 1.33 christos ksyms_verify(void *symstart, void *strstart)
140 1.33 christos {
141 1.33 christos #if defined(DIAGNOSTIC) || defined(DEBUG)
142 1.33 christos if (symstart == NULL)
143 1.33 christos printf("ksyms: Symbol table not found\n");
144 1.33 christos if (strstart == NULL)
145 1.33 christos printf("ksyms: String table not found\n");
146 1.33 christos if (symstart == NULL || strstart == NULL)
147 1.33 christos printf("ksyms: Perhaps the kernel is stripped?\n");
148 1.33 christos #endif
149 1.33 christos if (symstart == NULL || strstart == NULL)
150 1.33 christos return 0;
151 1.33 christos return 1;
152 1.33 christos }
153 1.33 christos
154 1.8 ragge /*
155 1.43 ad * Finds a certain symbol name in a certain symbol table.
156 1.8 ragge */
157 1.43 ad static Elf_Sym *
158 1.43 ad findsym(const char *name, struct ksyms_symtab *table, int type)
159 1.8 ragge {
160 1.43 ad Elf_Sym *sym, *maxsym;
161 1.43 ad int low, mid, high, nglob;
162 1.43 ad char *str, *cmp;
163 1.43 ad
164 1.43 ad sym = table->sd_symstart;
165 1.43 ad str = table->sd_strstart - table->sd_usroffset;
166 1.43 ad nglob = table->sd_nglob;
167 1.43 ad low = 0;
168 1.43 ad high = nglob;
169 1.8 ragge
170 1.43 ad /*
171 1.43 ad * Start with a binary search of all global symbols in this table.
172 1.43 ad * Global symbols must have unique names.
173 1.43 ad */
174 1.43 ad while (low < high) {
175 1.43 ad mid = (low + high) >> 1;
176 1.43 ad cmp = sym[mid].st_name + str;
177 1.43 ad if (cmp[0] < name[0] || strcmp(cmp, name) < 0) {
178 1.43 ad low = mid + 1;
179 1.43 ad } else {
180 1.43 ad high = mid;
181 1.43 ad }
182 1.8 ragge }
183 1.43 ad KASSERT(low == high);
184 1.43 ad if (__predict_true(low < nglob &&
185 1.43 ad strcmp(sym[low].st_name + str, name) == 0)) {
186 1.43 ad KASSERT(ELF_ST_BIND(sym[low].st_info) == STB_GLOBAL);
187 1.43 ad return &sym[low];
188 1.8 ragge }
189 1.8 ragge
190 1.43 ad /*
191 1.43 ad * Perform a linear search of local symbols (rare). Many local
192 1.43 ad * symbols with the same name can exist so are not included in
193 1.43 ad * the binary search.
194 1.43 ad */
195 1.43 ad if (type != KSYMS_EXTERN) {
196 1.43 ad maxsym = sym + table->sd_symsize / sizeof(Elf_Sym);
197 1.43 ad for (sym += nglob; sym < maxsym; sym++) {
198 1.43 ad if (strcmp(name, sym->st_name + str) == 0) {
199 1.43 ad return sym;
200 1.43 ad }
201 1.43 ad }
202 1.1 ragge }
203 1.1 ragge return NULL;
204 1.1 ragge }
205 1.1 ragge
206 1.1 ragge /*
207 1.1 ragge * The "attach" is in reality done in ksyms_init().
208 1.1 ragge */
209 1.1 ragge void
210 1.30 yamt ksymsattach(int arg)
211 1.1 ragge {
212 1.42 ad
213 1.1 ragge }
214 1.1 ragge
215 1.47 martin void
216 1.51 cegger ksyms_init(void)
217 1.47 martin {
218 1.47 martin
219 1.50 jmmv #ifdef SYMTAB_SPACE
220 1.50 jmmv if (!ksyms_initted &&
221 1.50 jmmv strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
222 1.50 jmmv ksyms_addsyms_elf(db_symtabsize, db_symtab,
223 1.50 jmmv db_symtab + db_symtabsize);
224 1.50 jmmv }
225 1.50 jmmv #endif
226 1.50 jmmv
227 1.47 martin mutex_init(&ksyms_lock, MUTEX_DEFAULT, IPL_NONE);
228 1.47 martin }
229 1.47 martin
230 1.1 ragge /*
231 1.29 jmmv * Add a symbol table.
232 1.29 jmmv * This is intended for use when the symbol table and its corresponding
233 1.29 jmmv * string table are easily available. If they are embedded in an ELF
234 1.29 jmmv * image, use addsymtab_elf() instead.
235 1.29 jmmv *
236 1.29 jmmv * name - Symbol's table name.
237 1.29 jmmv * symstart, symsize - Address and size of the symbol table.
238 1.29 jmmv * strstart, strsize - Address and size of the string table.
239 1.29 jmmv * tab - Symbol table to be updated with this information.
240 1.29 jmmv * newstart - Address to which the symbol table has to be copied during
241 1.29 jmmv * shrinking. If NULL, it is not moved.
242 1.1 ragge */
243 1.43 ad static const char *addsymtab_strstart;
244 1.43 ad
245 1.43 ad static int
246 1.43 ad addsymtab_compar(const void *a, const void *b)
247 1.43 ad {
248 1.43 ad const Elf_Sym *sa, *sb;
249 1.43 ad
250 1.43 ad sa = a;
251 1.43 ad sb = b;
252 1.43 ad
253 1.43 ad /*
254 1.43 ad * Split the symbol table into two, with globals at the start
255 1.43 ad * and locals at the end.
256 1.43 ad */
257 1.43 ad if (ELF_ST_BIND(sa->st_info) != ELF_ST_BIND(sb->st_info)) {
258 1.43 ad if (ELF_ST_BIND(sa->st_info) == STB_GLOBAL) {
259 1.43 ad return -1;
260 1.43 ad }
261 1.43 ad if (ELF_ST_BIND(sb->st_info) == STB_GLOBAL) {
262 1.43 ad return 1;
263 1.43 ad }
264 1.43 ad }
265 1.43 ad
266 1.43 ad /* Within each band, sort by name. */
267 1.43 ad return strcmp(sa->st_name + addsymtab_strstart,
268 1.43 ad sb->st_name + addsymtab_strstart);
269 1.43 ad }
270 1.43 ad
271 1.1 ragge static void
272 1.39 ad addsymtab(const char *name, void *symstart, size_t symsize,
273 1.39 ad void *strstart, size_t strsize, struct ksyms_symtab *tab,
274 1.56 darran void *newstart, void *ctfstart, size_t ctfsize, uint32_t *nmap)
275 1.1 ragge {
276 1.46 ad Elf_Sym *sym, *nsym, ts;
277 1.43 ad int i, j, n, nglob;
278 1.8 ragge char *str;
279 1.56 darran int nsyms = symsize / sizeof(Elf_Sym);
280 1.56 darran
281 1.63 rmind /* Sanity check for pre-allocated map table used during startup. */
282 1.56 darran if ((nmap == ksyms_nmap) && (nsyms >= KSYMS_MAX_ID)) {
283 1.60 tsutsui printf("kern_ksyms: ERROR %d > %d, increase KSYMS_MAX_ID\n",
284 1.56 darran nsyms, KSYMS_MAX_ID);
285 1.56 darran
286 1.60 tsutsui /* truncate for now */
287 1.60 tsutsui nsyms = KSYMS_MAX_ID - 1;
288 1.56 darran }
289 1.1 ragge
290 1.39 ad tab->sd_symstart = symstart;
291 1.29 jmmv tab->sd_symsize = symsize;
292 1.29 jmmv tab->sd_strstart = strstart;
293 1.29 jmmv tab->sd_strsize = strsize;
294 1.1 ragge tab->sd_name = name;
295 1.44 ad tab->sd_minsym = UINTPTR_MAX;
296 1.44 ad tab->sd_maxsym = 0;
297 1.39 ad tab->sd_usroffset = 0;
298 1.39 ad tab->sd_gone = false;
299 1.58 darran #ifdef KDTRACE_HOOKS
300 1.56 darran tab->sd_ctfstart = ctfstart;
301 1.56 darran tab->sd_ctfsize = ctfsize;
302 1.56 darran tab->sd_nmap = nmap;
303 1.56 darran tab->sd_nmapsize = nsyms;
304 1.58 darran #endif
305 1.8 ragge #ifdef KSYMS_DEBUG
306 1.61 jakllsch printf("newstart %p sym %p ksyms_symsz %zu str %p strsz %zu send %p\n",
307 1.39 ad newstart, symstart, symsize, strstart, strsize,
308 1.39 ad tab->sd_strstart + tab->sd_strsize);
309 1.8 ragge #endif
310 1.1 ragge
311 1.56 darran if (nmap) {
312 1.56 darran memset(nmap, 0, nsyms * sizeof(uint32_t));
313 1.56 darran }
314 1.56 darran
315 1.39 ad /* Pack symbol table by removing all file name references. */
316 1.8 ragge sym = tab->sd_symstart;
317 1.29 jmmv nsym = (Elf_Sym *)newstart;
318 1.8 ragge str = tab->sd_strstart;
319 1.43 ad nglob = 0;
320 1.56 darran for (i = n = 0; i < nsyms; i++) {
321 1.56 darran
322 1.56 darran /* This breaks CTF mapping, so don't do it when
323 1.56 darran * DTrace is enabled
324 1.56 darran */
325 1.56 darran #ifndef KDTRACE_HOOKS
326 1.8 ragge /*
327 1.8 ragge * Remove useless symbols.
328 1.8 ragge * Should actually remove all typeless symbols.
329 1.8 ragge */
330 1.5 ragge if (sym[i].st_name == 0)
331 1.8 ragge continue; /* Skip nameless entries */
332 1.34 ad if (sym[i].st_shndx == SHN_UNDEF)
333 1.34 ad continue; /* Skip external references */
334 1.8 ragge if (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
335 1.8 ragge continue; /* Skip filenames */
336 1.8 ragge if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
337 1.8 ragge sym[i].st_value == 0 &&
338 1.8 ragge strcmp(str + sym[i].st_name, "*ABS*") == 0)
339 1.8 ragge continue; /* XXX */
340 1.8 ragge if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
341 1.8 ragge strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
342 1.8 ragge continue; /* XXX */
343 1.56 darran #endif
344 1.8 ragge
345 1.8 ragge /* Save symbol. Set it as an absolute offset */
346 1.8 ragge nsym[n] = sym[i];
347 1.56 darran
348 1.58 darran #ifdef KDTRACE_HOOKS
349 1.56 darran if (nmap != NULL) {
350 1.60 tsutsui /*
351 1.60 tsutsui * Save the size, replace it with the symbol id so
352 1.56 darran * the mapping can be done after the cleanup and sort.
353 1.56 darran */
354 1.56 darran nmap[i] = nsym[n].st_size;
355 1.60 tsutsui nsym[n].st_size = i + 1; /* zero is reserved */
356 1.56 darran }
357 1.58 darran #endif
358 1.56 darran
359 1.48 ad nsym[n].st_shndx = SHBSS;
360 1.43 ad j = strlen(nsym[n].st_name + str) + 1;
361 1.39 ad if (j > ksyms_maxlen)
362 1.39 ad ksyms_maxlen = j;
363 1.43 ad nglob += (ELF_ST_BIND(nsym[n].st_info) == STB_GLOBAL);
364 1.43 ad
365 1.43 ad /* Compute min and max symbols. */
366 1.62 matt if (strcmp(str + sym[i].st_name, "*ABS*") != 0
367 1.62 matt && ELF_ST_TYPE(nsym[n].st_info) != STT_NOTYPE) {
368 1.62 matt if (nsym[n].st_value < tab->sd_minsym) {
369 1.62 matt tab->sd_minsym = nsym[n].st_value;
370 1.62 matt }
371 1.62 matt if (nsym[n].st_value > tab->sd_maxsym) {
372 1.62 matt tab->sd_maxsym = nsym[n].st_value;
373 1.62 matt }
374 1.43 ad }
375 1.8 ragge n++;
376 1.43 ad }
377 1.8 ragge
378 1.43 ad /* Fill the rest of the record, and sort the symbols. */
379 1.8 ragge tab->sd_symstart = nsym;
380 1.8 ragge tab->sd_symsize = n * sizeof(Elf_Sym);
381 1.43 ad tab->sd_nglob = nglob;
382 1.43 ad addsymtab_strstart = str;
383 1.46 ad if (kheapsort(nsym, n, sizeof(Elf_Sym), addsymtab_compar, &ts) != 0)
384 1.46 ad panic("addsymtab");
385 1.43 ad
386 1.58 darran #ifdef KDTRACE_HOOKS
387 1.56 darran /*
388 1.56 darran * Build the mapping from original symbol id to new symbol table.
389 1.56 darran * Deleted symbols will have a zero map, indices will be one based
390 1.56 darran * instead of zero based.
391 1.56 darran * Resulting map is sd_nmap[original_index] = new_index + 1
392 1.56 darran */
393 1.56 darran if (nmap != NULL) {
394 1.56 darran int new;
395 1.60 tsutsui for (new = 0; new < n; new++) {
396 1.56 darran uint32_t orig = nsym[new].st_size - 1;
397 1.56 darran uint32_t size = nmap[orig];
398 1.56 darran
399 1.56 darran nmap[orig] = new + 1;
400 1.56 darran
401 1.56 darran /* restore the size */
402 1.56 darran nsym[new].st_size = size;
403 1.56 darran }
404 1.56 darran }
405 1.58 darran #endif
406 1.56 darran
407 1.39 ad /* ksymsread() is unlocked, so membar. */
408 1.39 ad membar_producer();
409 1.39 ad TAILQ_INSERT_TAIL(&ksyms_symtabs, tab, sd_queue);
410 1.39 ad ksyms_sizes_calc();
411 1.39 ad ksyms_initted = true;
412 1.1 ragge }
413 1.1 ragge
414 1.1 ragge /*
415 1.39 ad * Setup the kernel symbol table stuff.
416 1.29 jmmv */
417 1.39 ad void
418 1.47 martin ksyms_addsyms_elf(int symsize, void *start, void *end)
419 1.29 jmmv {
420 1.29 jmmv int i, j;
421 1.29 jmmv Elf_Shdr *shdr;
422 1.32 christos char *symstart = NULL, *strstart = NULL;
423 1.39 ad size_t strsize = 0;
424 1.3 ragge Elf_Ehdr *ehdr;
425 1.56 darran char *ctfstart = NULL;
426 1.56 darran size_t ctfsize = 0;
427 1.3 ragge
428 1.3 ragge if (symsize <= 0) {
429 1.3 ragge printf("[ Kernel symbol table missing! ]\n");
430 1.3 ragge return;
431 1.3 ragge }
432 1.3 ragge
433 1.3 ragge /* Sanity check */
434 1.3 ragge if (ALIGNED_POINTER(start, long) == 0) {
435 1.3 ragge printf("[ Kernel symbol table has bad start address %p ]\n",
436 1.3 ragge start);
437 1.3 ragge return;
438 1.3 ragge }
439 1.3 ragge
440 1.3 ragge ehdr = (Elf_Ehdr *)start;
441 1.1 ragge
442 1.1 ragge /* check if this is a valid ELF header */
443 1.1 ragge /* No reason to verify arch type, the kernel is actually running! */
444 1.1 ragge if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
445 1.1 ragge ehdr->e_ident[EI_CLASS] != ELFCLASS ||
446 1.1 ragge ehdr->e_version > 1) {
447 1.3 ragge printf("[ Kernel symbol table invalid! ]\n");
448 1.1 ragge return; /* nothing to do */
449 1.1 ragge }
450 1.1 ragge
451 1.8 ragge /* Loaded header will be scratched in addsymtab */
452 1.8 ragge ksyms_hdr_init(start);
453 1.8 ragge
454 1.39 ad /* Find the symbol table and the corresponding string table. */
455 1.39 ad shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
456 1.39 ad for (i = 1; i < ehdr->e_shnum; i++) {
457 1.39 ad if (shdr[i].sh_type != SHT_SYMTAB)
458 1.39 ad continue;
459 1.39 ad if (shdr[i].sh_offset == 0)
460 1.39 ad continue;
461 1.39 ad symstart = (uint8_t *)start + shdr[i].sh_offset;
462 1.39 ad symsize = shdr[i].sh_size;
463 1.39 ad j = shdr[i].sh_link;
464 1.39 ad if (shdr[j].sh_offset == 0)
465 1.39 ad continue; /* Can this happen? */
466 1.39 ad strstart = (uint8_t *)start + shdr[j].sh_offset;
467 1.39 ad strsize = shdr[j].sh_size;
468 1.39 ad break;
469 1.39 ad }
470 1.8 ragge
471 1.58 darran #ifdef KDTRACE_HOOKS
472 1.56 darran /* Find the CTF section */
473 1.56 darran shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
474 1.56 darran if (ehdr->e_shstrndx != 0) {
475 1.60 tsutsui char *shstr = (uint8_t *)start +
476 1.60 tsutsui shdr[ehdr->e_shstrndx].sh_offset;
477 1.56 darran for (i = 1; i < ehdr->e_shnum; i++) {
478 1.59 darran #ifdef DEBUG
479 1.59 darran printf("ksyms: checking %s\n", &shstr[shdr[i].sh_name]);
480 1.59 darran #endif
481 1.56 darran if (shdr[i].sh_type != SHT_PROGBITS)
482 1.56 darran continue;
483 1.60 tsutsui if (strncmp(".SUNW_ctf", &shstr[shdr[i].sh_name], 10)
484 1.60 tsutsui != 0)
485 1.56 darran continue;
486 1.56 darran ctfstart = (uint8_t *)start + shdr[i].sh_offset;
487 1.56 darran ctfsize = shdr[i].sh_size;
488 1.56 darran ksyms_ctfsz = ctfsize;
489 1.56 darran #ifdef DEBUG
490 1.57 christos aprint_normal("Found CTF at %p, size 0x%zx\n",
491 1.57 christos ctfstart, ctfsize);
492 1.56 darran #endif
493 1.56 darran break;
494 1.56 darran }
495 1.59 darran #ifdef DEBUG
496 1.59 darran } else {
497 1.59 darran printf("ksyms: e_shstrndx == 0\n");
498 1.59 darran #endif
499 1.56 darran }
500 1.58 darran #endif
501 1.56 darran
502 1.39 ad if (!ksyms_verify(symstart, strstart))
503 1.39 ad return;
504 1.56 darran
505 1.39 ad addsymtab("netbsd", symstart, symsize, strstart, strsize,
506 1.56 darran &kernel_symtab, start, ctfstart, ctfsize, ksyms_nmap);
507 1.8 ragge
508 1.1 ragge #ifdef DEBUG
509 1.53 hubertf aprint_normal("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
510 1.1 ragge kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
511 1.2 ragge (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
512 1.1 ragge #endif
513 1.1 ragge }
514 1.1 ragge
515 1.1 ragge /*
516 1.29 jmmv * Setup the kernel symbol table stuff.
517 1.29 jmmv * Use this when the address of the symbol and string tables are known;
518 1.29 jmmv * otherwise use ksyms_init with an ELF image.
519 1.31 jmmv * We need to pass a minimal ELF header which will later be completed by
520 1.31 jmmv * ksyms_hdr_init and handed off to userland through /dev/ksyms. We use
521 1.32 christos * a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
522 1.29 jmmv */
523 1.29 jmmv void
524 1.47 martin ksyms_addsyms_explicit(void *ehdr, void *symstart, size_t symsize,
525 1.39 ad void *strstart, size_t strsize)
526 1.29 jmmv {
527 1.29 jmmv
528 1.33 christos if (!ksyms_verify(symstart, strstart))
529 1.33 christos return;
530 1.29 jmmv
531 1.31 jmmv ksyms_hdr_init(ehdr);
532 1.29 jmmv addsymtab("netbsd", symstart, symsize, strstart, strsize,
533 1.56 darran &kernel_symtab, symstart, NULL, 0, ksyms_nmap);
534 1.29 jmmv }
535 1.29 jmmv
536 1.29 jmmv /*
537 1.1 ragge * Get the value associated with a symbol.
538 1.23 perry * "mod" is the module name, or null if any module.
539 1.1 ragge * "sym" is the symbol name.
540 1.1 ragge * "val" is a pointer to the corresponding value, if call succeeded.
541 1.1 ragge * Returns 0 if success or ENOENT if no such entry.
542 1.39 ad *
543 1.39 ad * Call with ksyms_lock, unless known that the symbol table can't change.
544 1.1 ragge */
545 1.41 christos int
546 1.40 christos ksyms_getval_unlocked(const char *mod, const char *sym, unsigned long *val,
547 1.42 ad int type)
548 1.1 ragge {
549 1.39 ad struct ksyms_symtab *st;
550 1.1 ragge Elf_Sym *es;
551 1.1 ragge
552 1.1 ragge #ifdef KSYMS_DEBUG
553 1.1 ragge if (ksyms_debug & FOLLOW_CALLS)
554 1.40 christos printf("ksyms_getval_unlocked: mod %s sym %s valp %p\n",
555 1.40 christos mod, sym, val);
556 1.1 ragge #endif
557 1.1 ragge
558 1.39 ad TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
559 1.43 ad if (__predict_false(st->sd_gone))
560 1.1 ragge continue;
561 1.43 ad if (mod != NULL && strcmp(st->sd_name, mod))
562 1.1 ragge continue;
563 1.43 ad if ((es = findsym(sym, st, type)) != NULL) {
564 1.1 ragge *val = es->st_value;
565 1.43 ad return 0;
566 1.43 ad }
567 1.1 ragge }
568 1.1 ragge return ENOENT;
569 1.1 ragge }
570 1.1 ragge
571 1.40 christos int
572 1.40 christos ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
573 1.40 christos {
574 1.40 christos int rc;
575 1.40 christos
576 1.43 ad if (!ksyms_initted)
577 1.43 ad return ENOENT;
578 1.43 ad
579 1.40 christos mutex_enter(&ksyms_lock);
580 1.40 christos rc = ksyms_getval_unlocked(mod, sym, val, type);
581 1.40 christos mutex_exit(&ksyms_lock);
582 1.40 christos return rc;
583 1.40 christos }
584 1.40 christos
585 1.56 darran struct ksyms_symtab *
586 1.56 darran ksyms_get_mod(const char *mod)
587 1.56 darran {
588 1.56 darran struct ksyms_symtab *st;
589 1.56 darran
590 1.56 darran mutex_enter(&ksyms_lock);
591 1.56 darran TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
592 1.56 darran if (__predict_false(st->sd_gone))
593 1.56 darran continue;
594 1.56 darran if (mod != NULL && strcmp(st->sd_name, mod))
595 1.56 darran continue;
596 1.56 darran break;
597 1.56 darran }
598 1.56 darran mutex_exit(&ksyms_lock);
599 1.56 darran
600 1.56 darran return st;
601 1.56 darran }
602 1.56 darran
603 1.56 darran
604 1.56 darran /*
605 1.56 darran * ksyms_mod_foreach()
606 1.56 darran *
607 1.56 darran * Iterate over the symbol table of the specified module, calling the callback
608 1.56 darran * handler for each symbol. Stop iterating if the handler return is non-zero.
609 1.56 darran *
610 1.56 darran */
611 1.56 darran
612 1.56 darran int
613 1.56 darran ksyms_mod_foreach(const char *mod, ksyms_callback_t callback, void *opaque)
614 1.56 darran {
615 1.56 darran struct ksyms_symtab *st;
616 1.56 darran Elf_Sym *sym, *maxsym;
617 1.56 darran char *str;
618 1.56 darran int symindx;
619 1.56 darran
620 1.56 darran if (!ksyms_initted)
621 1.56 darran return ENOENT;
622 1.56 darran
623 1.56 darran mutex_enter(&ksyms_lock);
624 1.56 darran
625 1.56 darran /* find the module */
626 1.56 darran TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
627 1.56 darran if (__predict_false(st->sd_gone))
628 1.56 darran continue;
629 1.56 darran if (mod != NULL && strcmp(st->sd_name, mod))
630 1.56 darran continue;
631 1.56 darran
632 1.56 darran sym = st->sd_symstart;
633 1.56 darran str = st->sd_strstart - st->sd_usroffset;
634 1.56 darran
635 1.56 darran /* now iterate through the symbols */
636 1.56 darran maxsym = sym + st->sd_symsize / sizeof(Elf_Sym);
637 1.60 tsutsui for (symindx = 0; sym < maxsym; sym++, symindx++) {
638 1.56 darran if (callback(str + sym->st_name, symindx,
639 1.60 tsutsui (void *)sym->st_value,
640 1.60 tsutsui sym->st_size,
641 1.60 tsutsui sym->st_info,
642 1.60 tsutsui opaque) != 0) {
643 1.56 darran break;
644 1.56 darran }
645 1.56 darran }
646 1.56 darran }
647 1.56 darran mutex_exit(&ksyms_lock);
648 1.56 darran
649 1.56 darran return 0;
650 1.56 darran }
651 1.56 darran
652 1.1 ragge /*
653 1.1 ragge * Get "mod" and "symbol" associated with an address.
654 1.1 ragge * Returns 0 if success or ENOENT if no such entry.
655 1.39 ad *
656 1.39 ad * Call with ksyms_lock, unless known that the symbol table can't change.
657 1.1 ragge */
658 1.1 ragge int
659 1.24 christos ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
660 1.1 ragge {
661 1.39 ad struct ksyms_symtab *st;
662 1.1 ragge Elf_Sym *les, *es = NULL;
663 1.1 ragge vaddr_t laddr = 0;
664 1.15 christos const char *lmod = NULL;
665 1.15 christos char *stable = NULL;
666 1.1 ragge int type, i, sz;
667 1.1 ragge
668 1.39 ad if (!ksyms_initted)
669 1.1 ragge return ENOENT;
670 1.1 ragge
671 1.39 ad TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
672 1.39 ad if (st->sd_gone)
673 1.39 ad continue;
674 1.44 ad if (v < st->sd_minsym || v > st->sd_maxsym)
675 1.35 matt continue;
676 1.1 ragge sz = st->sd_symsize/sizeof(Elf_Sym);
677 1.1 ragge for (i = 0; i < sz; i++) {
678 1.1 ragge les = st->sd_symstart + i;
679 1.1 ragge type = ELF_ST_TYPE(les->st_info);
680 1.1 ragge
681 1.1 ragge if ((f & KSYMS_PROC) && (type != STT_FUNC))
682 1.1 ragge continue;
683 1.1 ragge
684 1.1 ragge if (type == STT_NOTYPE)
685 1.1 ragge continue;
686 1.1 ragge
687 1.1 ragge if (((f & KSYMS_ANY) == 0) &&
688 1.1 ragge (type != STT_FUNC) && (type != STT_OBJECT))
689 1.1 ragge continue;
690 1.1 ragge
691 1.1 ragge if ((les->st_value <= v) && (les->st_value > laddr)) {
692 1.1 ragge laddr = les->st_value;
693 1.1 ragge es = les;
694 1.1 ragge lmod = st->sd_name;
695 1.17 cube stable = st->sd_strstart - st->sd_usroffset;
696 1.1 ragge }
697 1.1 ragge }
698 1.1 ragge }
699 1.1 ragge if (es == NULL)
700 1.1 ragge return ENOENT;
701 1.1 ragge if ((f & KSYMS_EXACT) && (v != es->st_value))
702 1.1 ragge return ENOENT;
703 1.1 ragge if (mod)
704 1.1 ragge *mod = lmod;
705 1.1 ragge if (sym)
706 1.1 ragge *sym = stable + es->st_name;
707 1.1 ragge return 0;
708 1.1 ragge }
709 1.1 ragge
710 1.22 cube /*
711 1.39 ad * Add a symbol table from a loadable module.
712 1.39 ad */
713 1.39 ad void
714 1.39 ad ksyms_modload(const char *name, void *symstart, vsize_t symsize,
715 1.39 ad char *strstart, vsize_t strsize)
716 1.17 cube {
717 1.39 ad struct ksyms_symtab *st;
718 1.39 ad
719 1.39 ad st = kmem_zalloc(sizeof(*st), KM_SLEEP);
720 1.39 ad mutex_enter(&ksyms_lock);
721 1.56 darran addsymtab(name, symstart, symsize, strstart, strsize, st, symstart,
722 1.60 tsutsui NULL, 0, NULL);
723 1.39 ad mutex_exit(&ksyms_lock);
724 1.39 ad }
725 1.17 cube
726 1.39 ad /*
727 1.39 ad * Remove a symbol table from a loadable module.
728 1.39 ad */
729 1.39 ad void
730 1.39 ad ksyms_modunload(const char *name)
731 1.39 ad {
732 1.39 ad struct ksyms_symtab *st;
733 1.17 cube
734 1.39 ad mutex_enter(&ksyms_lock);
735 1.39 ad TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
736 1.39 ad if (st->sd_gone)
737 1.39 ad continue;
738 1.39 ad if (strcmp(name, st->sd_name) != 0)
739 1.39 ad continue;
740 1.39 ad st->sd_gone = true;
741 1.39 ad if (!ksyms_isopen) {
742 1.39 ad TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
743 1.39 ad ksyms_sizes_calc();
744 1.39 ad kmem_free(st, sizeof(*st));
745 1.39 ad }
746 1.39 ad break;
747 1.39 ad }
748 1.39 ad mutex_exit(&ksyms_lock);
749 1.39 ad KASSERT(st != NULL);
750 1.17 cube }
751 1.17 cube
752 1.1 ragge #ifdef DDB
753 1.1 ragge /*
754 1.1 ragge * Keep sifting stuff here, to avoid export of ksyms internals.
755 1.39 ad *
756 1.39 ad * Systems is expected to be quiescent, so no locking done.
757 1.1 ragge */
758 1.1 ragge int
759 1.1 ragge ksyms_sift(char *mod, char *sym, int mode)
760 1.1 ragge {
761 1.39 ad struct ksyms_symtab *st;
762 1.1 ragge char *sb;
763 1.1 ragge int i, sz;
764 1.1 ragge
765 1.39 ad if (!ksyms_initted)
766 1.1 ragge return ENOENT;
767 1.1 ragge
768 1.39 ad TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
769 1.39 ad if (st->sd_gone)
770 1.39 ad continue;
771 1.1 ragge if (mod && strcmp(mod, st->sd_name))
772 1.1 ragge continue;
773 1.39 ad sb = st->sd_strstart - st->sd_usroffset;
774 1.1 ragge
775 1.1 ragge sz = st->sd_symsize/sizeof(Elf_Sym);
776 1.1 ragge for (i = 0; i < sz; i++) {
777 1.1 ragge Elf_Sym *les = st->sd_symstart + i;
778 1.1 ragge char c;
779 1.1 ragge
780 1.39 ad if (strstr(sb + les->st_name, sym) == NULL)
781 1.1 ragge continue;
782 1.1 ragge
783 1.1 ragge if (mode == 'F') {
784 1.1 ragge switch (ELF_ST_TYPE(les->st_info)) {
785 1.1 ragge case STT_OBJECT:
786 1.1 ragge c = '+';
787 1.1 ragge break;
788 1.1 ragge case STT_FUNC:
789 1.1 ragge c = '*';
790 1.1 ragge break;
791 1.1 ragge case STT_SECTION:
792 1.1 ragge c = '&';
793 1.1 ragge break;
794 1.1 ragge case STT_FILE:
795 1.1 ragge c = '/';
796 1.1 ragge break;
797 1.1 ragge default:
798 1.1 ragge c = ' ';
799 1.1 ragge break;
800 1.1 ragge }
801 1.39 ad db_printf("%s%c ", sb + les->st_name, c);
802 1.1 ragge } else
803 1.39 ad db_printf("%s ", sb + les->st_name);
804 1.1 ragge }
805 1.1 ragge }
806 1.1 ragge return ENOENT;
807 1.1 ragge }
808 1.25 thorpej #endif /* DDB */
809 1.1 ragge
810 1.1 ragge /*
811 1.39 ad * In case we exposing the symbol table to the userland using the pseudo-
812 1.39 ad * device /dev/ksyms, it is easier to provide all the tables as one.
813 1.39 ad * However, it means we have to change all the st_name fields for the
814 1.39 ad * symbols so they match the ELF image that the userland will read
815 1.39 ad * through the device.
816 1.39 ad *
817 1.39 ad * The actual (correct) value of st_name is preserved through a global
818 1.39 ad * offset stored in the symbol table structure.
819 1.39 ad *
820 1.39 ad * Call with ksyms_lock held.
821 1.1 ragge */
822 1.39 ad static void
823 1.39 ad ksyms_sizes_calc(void)
824 1.39 ad {
825 1.39 ad struct ksyms_symtab *st;
826 1.39 ad int i, delta;
827 1.1 ragge
828 1.39 ad ksyms_symsz = ksyms_strsz = 0;
829 1.39 ad TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
830 1.39 ad delta = ksyms_strsz - st->sd_usroffset;
831 1.39 ad if (delta != 0) {
832 1.39 ad for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
833 1.39 ad st->sd_symstart[i].st_name += delta;
834 1.39 ad st->sd_usroffset = ksyms_strsz;
835 1.39 ad }
836 1.39 ad ksyms_symsz += st->sd_symsize;
837 1.39 ad ksyms_strsz += st->sd_strsize;
838 1.39 ad }
839 1.39 ad }
840 1.1 ragge
841 1.25 thorpej static void
842 1.32 christos ksyms_hdr_init(void *hdraddr)
843 1.1 ragge {
844 1.1 ragge
845 1.1 ragge /* Copy the loaded elf exec header */
846 1.1 ragge memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
847 1.1 ragge
848 1.1 ragge /* Set correct program/section header sizes, offsets and numbers */
849 1.1 ragge ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
850 1.1 ragge ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
851 1.1 ragge ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
852 1.1 ragge ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
853 1.1 ragge ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
854 1.1 ragge ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
855 1.48 ad ksyms_hdr.kh_ehdr.e_shstrndx = SHSTRTAB;
856 1.1 ragge
857 1.48 ad /* Text/data - fake */
858 1.39 ad ksyms_hdr.kh_phdr[0].p_type = PT_LOAD;
859 1.39 ad ksyms_hdr.kh_phdr[0].p_memsz = (unsigned long)-1L;
860 1.48 ad ksyms_hdr.kh_phdr[0].p_flags = PF_R | PF_X | PF_W;
861 1.39 ad
862 1.39 ad /* First section is null */
863 1.1 ragge
864 1.1 ragge /* Second section header; ".symtab" */
865 1.1 ragge ksyms_hdr.kh_shdr[SYMTAB].sh_name = 1; /* Section 3 offset */
866 1.1 ragge ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
867 1.1 ragge ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
868 1.1 ragge /* ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
869 1.1 ragge ksyms_hdr.kh_shdr[SYMTAB].sh_link = 2; /* Corresponding strtab */
870 1.1 ragge ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
871 1.1 ragge ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
872 1.1 ragge
873 1.1 ragge /* Third section header; ".strtab" */
874 1.1 ragge ksyms_hdr.kh_shdr[STRTAB].sh_name = 9; /* Section 3 offset */
875 1.1 ragge ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
876 1.1 ragge /* ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
877 1.1 ragge /* ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
878 1.1 ragge ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
879 1.1 ragge
880 1.1 ragge /* Fourth section, ".shstrtab" */
881 1.1 ragge ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = 17; /* This section name offset */
882 1.1 ragge ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
883 1.1 ragge ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
884 1.1 ragge offsetof(struct ksyms_hdr, kh_strtab);
885 1.1 ragge ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
886 1.1 ragge ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
887 1.1 ragge
888 1.48 ad /* Fifth section, ".bss". All symbols reside here. */
889 1.48 ad ksyms_hdr.kh_shdr[SHBSS].sh_name = 27; /* This section name offset */
890 1.55 darran ksyms_hdr.kh_shdr[SHBSS].sh_type = SHT_NOBITS;
891 1.48 ad ksyms_hdr.kh_shdr[SHBSS].sh_offset = 0;
892 1.48 ad ksyms_hdr.kh_shdr[SHBSS].sh_size = (unsigned long)-1L;
893 1.48 ad ksyms_hdr.kh_shdr[SHBSS].sh_addralign = PAGE_SIZE;
894 1.48 ad ksyms_hdr.kh_shdr[SHBSS].sh_flags = SHF_ALLOC | SHF_EXECINSTR;
895 1.48 ad
896 1.58 darran #ifdef KDTRACE_HOOKS
897 1.56 darran /* Sixth section header; ".SUNW_ctf" */
898 1.56 darran ksyms_hdr.kh_shdr[SHCTF].sh_name = 32; /* Section 6 offset */
899 1.56 darran ksyms_hdr.kh_shdr[SHCTF].sh_type = SHT_PROGBITS;
900 1.56 darran /* ksyms_hdr.kh_shdr[SHCTF].sh_offset = filled in at open */
901 1.56 darran /* ksyms_hdr.kh_shdr[SHCTF].sh_size = filled in at open */
902 1.56 darran ksyms_hdr.kh_shdr[SHCTF].sh_link = SYMTAB; /* Corresponding symtab */
903 1.56 darran ksyms_hdr.kh_shdr[SHCTF].sh_addralign = sizeof(char);
904 1.58 darran #endif
905 1.56 darran
906 1.1 ragge /* Set section names */
907 1.10 itojun strlcpy(&ksyms_hdr.kh_strtab[1], ".symtab",
908 1.10 itojun sizeof(ksyms_hdr.kh_strtab) - 1);
909 1.10 itojun strlcpy(&ksyms_hdr.kh_strtab[9], ".strtab",
910 1.10 itojun sizeof(ksyms_hdr.kh_strtab) - 9);
911 1.10 itojun strlcpy(&ksyms_hdr.kh_strtab[17], ".shstrtab",
912 1.10 itojun sizeof(ksyms_hdr.kh_strtab) - 17);
913 1.48 ad strlcpy(&ksyms_hdr.kh_strtab[27], ".bss",
914 1.48 ad sizeof(ksyms_hdr.kh_strtab) - 27);
915 1.58 darran #ifdef KDTRACE_HOOKS
916 1.56 darran strlcpy(&ksyms_hdr.kh_strtab[32], ".SUNW_ctf",
917 1.56 darran sizeof(ksyms_hdr.kh_strtab) - 32);
918 1.58 darran #endif
919 1.39 ad }
920 1.1 ragge
921 1.25 thorpej static int
922 1.30 yamt ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
923 1.1 ragge {
924 1.1 ragge
925 1.39 ad if (minor(dev) != 0 || !ksyms_initted)
926 1.18 cube return ENXIO;
927 1.1 ragge
928 1.39 ad /*
929 1.39 ad * Create a "snapshot" of the kernel symbol table. Setting
930 1.39 ad * ksyms_isopen will prevent symbol tables from being freed.
931 1.39 ad */
932 1.39 ad mutex_enter(&ksyms_lock);
933 1.39 ad ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
934 1.39 ad ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
935 1.55 darran ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
936 1.1 ragge ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
937 1.39 ad ksyms_hdr.kh_shdr[STRTAB].sh_size = ksyms_strsz;
938 1.58 darran #ifdef KDTRACE_HOOKS
939 1.56 darran ksyms_hdr.kh_shdr[SHCTF].sh_offset = ksyms_strsz +
940 1.56 darran ksyms_hdr.kh_shdr[STRTAB].sh_offset;
941 1.56 darran ksyms_hdr.kh_shdr[SHCTF].sh_size = ksyms_ctfsz;
942 1.58 darran #endif
943 1.39 ad ksyms_isopen = true;
944 1.39 ad mutex_exit(&ksyms_lock);
945 1.1 ragge
946 1.1 ragge return 0;
947 1.1 ragge }
948 1.1 ragge
949 1.25 thorpej static int
950 1.30 yamt ksymsclose(dev_t dev, int oflags, int devtype, struct lwp *l)
951 1.1 ragge {
952 1.39 ad struct ksyms_symtab *st, *next;
953 1.39 ad bool resize;
954 1.1 ragge
955 1.39 ad /* Discard refernces to symbol tables. */
956 1.39 ad mutex_enter(&ksyms_lock);
957 1.39 ad ksyms_isopen = false;
958 1.39 ad resize = false;
959 1.39 ad for (st = TAILQ_FIRST(&ksyms_symtabs); st != NULL; st = next) {
960 1.39 ad next = TAILQ_NEXT(st, sd_queue);
961 1.39 ad if (st->sd_gone) {
962 1.39 ad TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
963 1.39 ad kmem_free(st, sizeof(*st));
964 1.39 ad resize = true;
965 1.39 ad }
966 1.39 ad }
967 1.39 ad if (resize)
968 1.39 ad ksyms_sizes_calc();
969 1.39 ad mutex_exit(&ksyms_lock);
970 1.1 ragge
971 1.1 ragge return 0;
972 1.1 ragge }
973 1.1 ragge
974 1.25 thorpej static int
975 1.30 yamt ksymsread(dev_t dev, struct uio *uio, int ioflag)
976 1.1 ragge {
977 1.58 darran struct ksyms_symtab *st;
978 1.1 ragge size_t filepos, inpos, off;
979 1.39 ad int error;
980 1.58 darran #ifdef KDTRACE_HOOKS
981 1.58 darran struct ksyms_symtab *cst;
982 1.58 darran #endif
983 1.1 ragge
984 1.1 ragge /*
985 1.39 ad * First: Copy out the ELF header. XXX Lose if ksymsopen()
986 1.39 ad * occurs during read of the header.
987 1.1 ragge */
988 1.39 ad off = uio->uio_offset;
989 1.39 ad if (off < sizeof(struct ksyms_hdr)) {
990 1.39 ad error = uiomove((char *)&ksyms_hdr + off,
991 1.39 ad sizeof(struct ksyms_hdr) - off, uio);
992 1.39 ad if (error != 0)
993 1.39 ad return error;
994 1.39 ad }
995 1.1 ragge
996 1.1 ragge /*
997 1.1 ragge * Copy out the symbol table.
998 1.1 ragge */
999 1.39 ad filepos = sizeof(struct ksyms_hdr);
1000 1.39 ad TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
1001 1.1 ragge if (uio->uio_resid == 0)
1002 1.1 ragge return 0;
1003 1.1 ragge if (uio->uio_offset <= st->sd_symsize + filepos) {
1004 1.1 ragge inpos = uio->uio_offset - filepos;
1005 1.39 ad error = uiomove((char *)st->sd_symstart + inpos,
1006 1.1 ragge st->sd_symsize - inpos, uio);
1007 1.39 ad if (error != 0)
1008 1.39 ad return error;
1009 1.1 ragge }
1010 1.1 ragge filepos += st->sd_symsize;
1011 1.1 ragge }
1012 1.1 ragge
1013 1.1 ragge /*
1014 1.1 ragge * Copy out the string table
1015 1.1 ragge */
1016 1.39 ad KASSERT(filepos == sizeof(struct ksyms_hdr) +
1017 1.55 darran ksyms_hdr.kh_shdr[SYMTAB].sh_size);
1018 1.39 ad TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
1019 1.1 ragge if (uio->uio_resid == 0)
1020 1.1 ragge return 0;
1021 1.1 ragge if (uio->uio_offset <= st->sd_strsize + filepos) {
1022 1.1 ragge inpos = uio->uio_offset - filepos;
1023 1.39 ad error = uiomove((char *)st->sd_strstart + inpos,
1024 1.1 ragge st->sd_strsize - inpos, uio);
1025 1.39 ad if (error != 0)
1026 1.39 ad return error;
1027 1.1 ragge }
1028 1.1 ragge filepos += st->sd_strsize;
1029 1.1 ragge }
1030 1.39 ad
1031 1.58 darran #ifdef KDTRACE_HOOKS
1032 1.56 darran /*
1033 1.56 darran * Copy out the CTF table.
1034 1.56 darran */
1035 1.56 darran cst = TAILQ_FIRST(&ksyms_symtabs);
1036 1.56 darran if (cst->sd_ctfstart != NULL) {
1037 1.56 darran if (uio->uio_resid == 0)
1038 1.56 darran return 0;
1039 1.56 darran if (uio->uio_offset <= cst->sd_ctfsize + filepos) {
1040 1.56 darran inpos = uio->uio_offset - filepos;
1041 1.56 darran error = uiomove((char *)cst->sd_ctfstart + inpos,
1042 1.56 darran cst->sd_ctfsize - inpos, uio);
1043 1.56 darran if (error != 0)
1044 1.56 darran return error;
1045 1.56 darran }
1046 1.56 darran filepos += cst->sd_ctfsize;
1047 1.56 darran }
1048 1.58 darran #endif
1049 1.56 darran
1050 1.1 ragge return 0;
1051 1.1 ragge }
1052 1.1 ragge
1053 1.25 thorpej static int
1054 1.30 yamt ksymswrite(dev_t dev, struct uio *uio, int ioflag)
1055 1.1 ragge {
1056 1.30 yamt
1057 1.1 ragge return EROFS;
1058 1.1 ragge }
1059 1.1 ragge
1060 1.25 thorpej static int
1061 1.32 christos ksymsioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
1062 1.1 ragge {
1063 1.1 ragge struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
1064 1.39 ad struct ksyms_symtab *st;
1065 1.39 ad Elf_Sym *sym = NULL, copy;
1066 1.1 ragge unsigned long val;
1067 1.1 ragge int error = 0;
1068 1.15 christos char *str = NULL;
1069 1.39 ad int len;
1070 1.39 ad
1071 1.39 ad /* Read ksyms_maxlen only once while not holding the lock. */
1072 1.39 ad len = ksyms_maxlen;
1073 1.5 ragge
1074 1.39 ad if (cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
1075 1.39 ad str = kmem_alloc(len, KM_SLEEP);
1076 1.39 ad if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
1077 1.39 ad kmem_free(str, len);
1078 1.39 ad return error;
1079 1.39 ad }
1080 1.39 ad }
1081 1.1 ragge
1082 1.1 ragge switch (cmd) {
1083 1.1 ragge case KIOCGVALUE:
1084 1.1 ragge /*
1085 1.1 ragge * Use the in-kernel symbol lookup code for fast
1086 1.1 ragge * retreival of a value.
1087 1.1 ragge */
1088 1.39 ad error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
1089 1.39 ad if (error == 0)
1090 1.39 ad error = copyout(&val, kg->kg_value, sizeof(long));
1091 1.39 ad kmem_free(str, len);
1092 1.1 ragge break;
1093 1.1 ragge
1094 1.1 ragge case KIOCGSYMBOL:
1095 1.1 ragge /*
1096 1.1 ragge * Use the in-kernel symbol lookup code for fast
1097 1.1 ragge * retreival of a symbol.
1098 1.1 ragge */
1099 1.39 ad mutex_enter(&ksyms_lock);
1100 1.39 ad TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
1101 1.39 ad if (st->sd_gone)
1102 1.39 ad continue;
1103 1.43 ad if ((sym = findsym(str, st, KSYMS_ANY)) == NULL)
1104 1.1 ragge continue;
1105 1.36 christos #ifdef notdef
1106 1.1 ragge /* Skip if bad binding */
1107 1.1 ragge if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
1108 1.1 ragge sym = NULL;
1109 1.1 ragge continue;
1110 1.1 ragge }
1111 1.36 christos #endif
1112 1.1 ragge break;
1113 1.1 ragge }
1114 1.39 ad if (sym != NULL) {
1115 1.39 ad memcpy(©, sym, sizeof(copy));
1116 1.39 ad mutex_exit(&ksyms_lock);
1117 1.39 ad error = copyout(©, kg->kg_sym, sizeof(Elf_Sym));
1118 1.39 ad } else {
1119 1.39 ad mutex_exit(&ksyms_lock);
1120 1.1 ragge error = ENOENT;
1121 1.39 ad }
1122 1.39 ad kmem_free(str, len);
1123 1.1 ragge break;
1124 1.1 ragge
1125 1.1 ragge case KIOCGSIZE:
1126 1.1 ragge /*
1127 1.1 ragge * Get total size of symbol table.
1128 1.1 ragge */
1129 1.39 ad mutex_enter(&ksyms_lock);
1130 1.39 ad *(int *)data = ksyms_strsz + ksyms_symsz +
1131 1.39 ad sizeof(struct ksyms_hdr);
1132 1.39 ad mutex_exit(&ksyms_lock);
1133 1.1 ragge break;
1134 1.1 ragge
1135 1.1 ragge default:
1136 1.1 ragge error = ENOTTY;
1137 1.1 ragge break;
1138 1.1 ragge }
1139 1.5 ragge
1140 1.5 ragge return error;
1141 1.1 ragge }
1142 1.25 thorpej
1143 1.25 thorpej const struct cdevsw ksyms_cdevsw = {
1144 1.25 thorpej ksymsopen, ksymsclose, ksymsread, ksymswrite, ksymsioctl,
1145 1.39 ad nullstop, notty, nopoll, nommap, nullkqfilter, D_OTHER | D_MPSAFE
1146 1.25 thorpej };
1147