2 * kallsyms.c: in-kernel printing of symbolic oopses and stack traces.
4 * Rewritten and vastly simplified by Rusty Russell for in-kernel
6 * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
10 * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com>
11 * Changed the compression method from stem compression to "table lookup"
12 * compression (see scripts/kallsyms.c for a more complete description)
14 #include <linux/kallsyms.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/seq_file.h>
19 #include <linux/err.h>
20 #include <linux/proc_fs.h>
21 #include <linux/sched.h> /* for cond_resched */
23 #include <linux/ctype.h>
25 #include <asm/sections.h>
27 #ifdef CONFIG_KALLSYMS_ALL
34 * These will be re-linked against their real values
35 * during the second link stage.
37 extern const unsigned long kallsyms_addresses[] __attribute__((weak));
38 extern const u8 kallsyms_names[] __attribute__((weak));
41 * Tell the compiler that the count isn't in the small data section if the arch
44 extern const unsigned long kallsyms_num_syms
45 __attribute__((weak, section(".rodata")));
47 extern const u8 kallsyms_token_table[] __attribute__((weak));
48 extern const u16 kallsyms_token_index[] __attribute__((weak));
50 extern const unsigned long kallsyms_markers[] __attribute__((weak));
52 static inline int is_kernel_inittext(unsigned long addr)
54 if (addr >= (unsigned long)_sinittext
55 && addr <= (unsigned long)_einittext)
60 static inline int is_kernel_text(unsigned long addr)
62 if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) ||
63 arch_is_kernel_text(addr))
65 return in_gate_area_no_task(addr);
68 static inline int is_kernel(unsigned long addr)
70 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
72 return in_gate_area_no_task(addr);
75 static int is_ksym_addr(unsigned long addr)
78 return is_kernel(addr);
80 return is_kernel_text(addr) || is_kernel_inittext(addr);
84 * Expand a compressed symbol data into the resulting uncompressed string,
85 * given the offset to where the symbol is in the compressed stream.
87 static unsigned int kallsyms_expand_symbol(unsigned int off, char *result)
89 int len, skipped_first = 0;
90 const u8 *tptr, *data;
92 /* Get the compressed symbol length from the first symbol byte. */
93 data = &kallsyms_names[off];
98 * Update the offset to return the offset for the next symbol on
99 * the compressed stream.
104 * For every byte on the compressed symbol data, copy the table
105 * entry for that byte.
108 tptr = &kallsyms_token_table[kallsyms_token_index[*data]];
124 /* Return to offset to the next symbol. */
129 * Get symbol type information. This is encoded as a single char at the
130 * beginning of the symbol name.
132 static char kallsyms_get_symbol_type(unsigned int off)
135 * Get just the first code, look it up in the token table,
136 * and return the first char from this token.
138 return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]];
143 * Find the offset on the compressed stream given and index in the
146 static unsigned int get_symbol_offset(unsigned long pos)
152 * Use the closest marker we have. We have markers every 256 positions,
153 * so that should be close enough.
155 name = &kallsyms_names[kallsyms_markers[pos >> 8]];
158 * Sequentially scan all the symbols up to the point we're searching
159 * for. Every symbol is stored in a [<len>][<len> bytes of data] format,
160 * so we just need to add the len to the current pointer for every
161 * symbol we wish to skip.
163 for (i = 0; i < (pos & 0xFF); i++)
164 name = name + (*name) + 1;
166 return name - kallsyms_names;
169 /* Lookup the address for this symbol. Returns 0 if not found. */
170 unsigned long kallsyms_lookup_name(const char *name)
172 char namebuf[KSYM_NAME_LEN];
176 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
177 off = kallsyms_expand_symbol(off, namebuf);
179 if (strcmp(namebuf, name) == 0)
180 return kallsyms_addresses[i];
182 return module_kallsyms_lookup_name(name);
184 EXPORT_SYMBOL_GPL(kallsyms_lookup_name);
186 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
190 char namebuf[KSYM_NAME_LEN];
195 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
196 off = kallsyms_expand_symbol(off, namebuf);
197 ret = fn(data, namebuf, NULL, kallsyms_addresses[i]);
201 return module_kallsyms_on_each_symbol(fn, data);
203 EXPORT_SYMBOL_GPL(kallsyms_on_each_symbol);
205 static unsigned long get_symbol_pos(unsigned long addr,
206 unsigned long *symbolsize,
207 unsigned long *offset)
209 unsigned long symbol_start = 0, symbol_end = 0;
210 unsigned long i, low, high, mid;
212 /* This kernel should never had been booted. */
213 BUG_ON(!kallsyms_addresses);
215 /* Do a binary search on the sorted kallsyms_addresses array. */
217 high = kallsyms_num_syms;
219 while (high - low > 1) {
220 mid = low + (high - low) / 2;
221 if (kallsyms_addresses[mid] <= addr)
228 * Search for the first aliased symbol. Aliased
229 * symbols are symbols with the same address.
231 while (low && kallsyms_addresses[low-1] == kallsyms_addresses[low])
234 symbol_start = kallsyms_addresses[low];
236 /* Search for next non-aliased symbol. */
237 for (i = low + 1; i < kallsyms_num_syms; i++) {
238 if (kallsyms_addresses[i] > symbol_start) {
239 symbol_end = kallsyms_addresses[i];
244 /* If we found no next symbol, we use the end of the section. */
246 if (is_kernel_inittext(addr))
247 symbol_end = (unsigned long)_einittext;
249 symbol_end = (unsigned long)_end;
251 symbol_end = (unsigned long)_etext;
255 *symbolsize = symbol_end - symbol_start;
257 *offset = addr - symbol_start;
263 * Lookup an address but don't bother to find any names.
265 int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
266 unsigned long *offset)
268 char namebuf[KSYM_NAME_LEN];
269 if (is_ksym_addr(addr))
270 return !!get_symbol_pos(addr, symbolsize, offset);
272 return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf);
277 * - modname is set to NULL if it's in the kernel.
278 * - We guarantee that the returned name is valid until we reschedule even if.
279 * It resides in a module.
280 * - We also guarantee that modname will be valid until rescheduled.
282 const char *kallsyms_lookup(unsigned long addr,
283 unsigned long *symbolsize,
284 unsigned long *offset,
285 char **modname, char *namebuf)
287 namebuf[KSYM_NAME_LEN - 1] = 0;
290 if (is_ksym_addr(addr)) {
293 pos = get_symbol_pos(addr, symbolsize, offset);
295 kallsyms_expand_symbol(get_symbol_offset(pos), namebuf);
301 /* See if it's in a module. */
302 return module_address_lookup(addr, symbolsize, offset, modname,
306 int lookup_symbol_name(unsigned long addr, char *symname)
309 symname[KSYM_NAME_LEN - 1] = '\0';
311 if (is_ksym_addr(addr)) {
314 pos = get_symbol_pos(addr, NULL, NULL);
316 kallsyms_expand_symbol(get_symbol_offset(pos), symname);
319 /* See if it's in a module. */
320 return lookup_module_symbol_name(addr, symname);
323 int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
324 unsigned long *offset, char *modname, char *name)
327 name[KSYM_NAME_LEN - 1] = '\0';
329 if (is_ksym_addr(addr)) {
332 pos = get_symbol_pos(addr, size, offset);
334 kallsyms_expand_symbol(get_symbol_offset(pos), name);
338 /* See if it's in a module. */
339 return lookup_module_symbol_attrs(addr, size, offset, modname, name);
342 /* Look up a kernel symbol and return it in a text buffer. */
343 int sprint_symbol(char *buffer, unsigned long address)
347 unsigned long offset, size;
350 name = kallsyms_lookup(address, &size, &offset, &modname, buffer);
352 return sprintf(buffer, "0x%lx", address);
355 strcpy(buffer, name);
356 len = strlen(buffer);
360 len += sprintf(buffer, "+%#lx/%#lx [%s]",
361 offset, size, modname);
363 len += sprintf(buffer, "+%#lx/%#lx", offset, size);
367 EXPORT_SYMBOL_GPL(sprint_symbol);
369 /* Look up a kernel symbol and print it to the kernel messages. */
370 void __print_symbol(const char *fmt, unsigned long address)
372 char buffer[KSYM_SYMBOL_LEN];
374 sprint_symbol(buffer, address);
378 EXPORT_SYMBOL(__print_symbol);
380 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
381 struct kallsym_iter {
384 unsigned int nameoff; /* If iterating in core kernel symbols. */
386 char name[KSYM_NAME_LEN];
387 char module_name[MODULE_NAME_LEN];
391 static int get_ksymbol_mod(struct kallsym_iter *iter)
393 if (module_get_kallsym(iter->pos - kallsyms_num_syms, &iter->value,
394 &iter->type, iter->name, iter->module_name,
395 &iter->exported) < 0)
400 /* Returns space to next name. */
401 static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
403 unsigned off = iter->nameoff;
405 iter->module_name[0] = '\0';
406 iter->value = kallsyms_addresses[iter->pos];
408 iter->type = kallsyms_get_symbol_type(off);
410 off = kallsyms_expand_symbol(off, iter->name);
412 return off - iter->nameoff;
415 static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
417 iter->name[0] = '\0';
418 iter->nameoff = get_symbol_offset(new_pos);
422 /* Returns false if pos at or past end of file. */
423 static int update_iter(struct kallsym_iter *iter, loff_t pos)
425 /* Module symbols can be accessed randomly. */
426 if (pos >= kallsyms_num_syms) {
428 return get_ksymbol_mod(iter);
431 /* If we're not on the desired position, reset to new position. */
432 if (pos != iter->pos)
433 reset_iter(iter, pos);
435 iter->nameoff += get_ksymbol_core(iter);
441 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
445 if (!update_iter(m->private, *pos))
450 static void *s_start(struct seq_file *m, loff_t *pos)
452 if (!update_iter(m->private, *pos))
457 static void s_stop(struct seq_file *m, void *p)
461 static int s_show(struct seq_file *m, void *p)
463 struct kallsym_iter *iter = m->private;
465 /* Some debugging symbols have no name. Ignore them. */
469 if (iter->module_name[0]) {
473 * Label it "global" if it is exported,
474 * "local" if not exported.
476 type = iter->exported ? toupper(iter->type) :
478 seq_printf(m, "%0*lx %c %s\t[%s]\n",
479 (int)(2 * sizeof(void *)),
480 iter->value, type, iter->name, iter->module_name);
482 seq_printf(m, "%0*lx %c %s\n",
483 (int)(2 * sizeof(void *)),
484 iter->value, iter->type, iter->name);
488 static const struct seq_operations kallsyms_op = {
495 static int kallsyms_open(struct inode *inode, struct file *file)
498 * We keep iterator in m->private, since normal case is to
499 * s_start from where we left off, so we avoid doing
500 * using get_symbol_offset for every symbol.
502 struct kallsym_iter *iter;
505 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
510 ret = seq_open(file, &kallsyms_op);
512 ((struct seq_file *)file->private_data)->private = iter;
518 static const struct file_operations kallsyms_operations = {
519 .open = kallsyms_open,
522 .release = seq_release_private,
525 static int __init kallsyms_init(void)
527 proc_create("kallsyms", 0444, NULL, &kallsyms_operations);
530 device_initcall(kallsyms_init);