blob: e694afa0eb8c440e2184166ed8669ca08950103e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kallsyms.c: in-kernel printing of symbolic oopses and stack traces.
3 *
4 * Rewritten and vastly simplified by Rusty Russell for in-kernel
5 * module loader:
6 * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
7 *
8 * ChangeLog:
9 *
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)
13 */
14#include <linux/kallsyms.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/seq_file.h>
18#include <linux/fs.h>
19#include <linux/err.h>
20#include <linux/proc_fs.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080021#include <linux/sched.h> /* for cond_resched */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Adam B. Jerome07354a02006-12-06 20:35:30 -080023#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/sections.h>
26
27#ifdef CONFIG_KALLSYMS_ALL
28#define all_var 1
29#else
30#define all_var 0
31#endif
32
Jan Beulich9bb48242008-12-16 11:30:08 +000033extern const unsigned long kallsyms_addresses[];
34extern const u8 kallsyms_names[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David Howells9e6c1e62007-11-28 16:22:04 -080036/* tell the compiler that the count isn't in the small data section if the arch
37 * has one (eg: FRV)
38 */
39extern const unsigned long kallsyms_num_syms
Jan Beulich9bb48242008-12-16 11:30:08 +000040 __attribute__((__section__(".rodata")));
David Howells9e6c1e62007-11-28 16:22:04 -080041
Jan Beulich9bb48242008-12-16 11:30:08 +000042extern const u8 kallsyms_token_table[];
43extern const u16 kallsyms_token_index[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jan Beulich9bb48242008-12-16 11:30:08 +000045extern const unsigned long kallsyms_markers[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static inline int is_kernel_inittext(unsigned long addr)
48{
49 if (addr >= (unsigned long)_sinittext
50 && addr <= (unsigned long)_einittext)
51 return 1;
52 return 0;
53}
54
55static inline int is_kernel_text(unsigned long addr)
56{
57 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_etext)
58 return 1;
59 return in_gate_area_no_task(addr);
60}
61
62static inline int is_kernel(unsigned long addr)
63{
64 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
65 return 1;
66 return in_gate_area_no_task(addr);
67}
68
Franck Bui-Huuffc50892006-10-03 01:13:48 -070069static int is_ksym_addr(unsigned long addr)
70{
71 if (all_var)
72 return is_kernel(addr);
73
Robin Getza3b81112008-02-06 01:36:26 -080074 return is_kernel_text(addr) || is_kernel_inittext(addr);
Franck Bui-Huuffc50892006-10-03 01:13:48 -070075}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* expand a compressed symbol data into the resulting uncompressed string,
78 given the offset to where the symbol is in the compressed stream */
79static unsigned int kallsyms_expand_symbol(unsigned int off, char *result)
80{
81 int len, skipped_first = 0;
Jan Beulichaad09472006-12-08 02:35:57 -080082 const u8 *tptr, *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 /* get the compressed symbol length from the first symbol byte */
85 data = &kallsyms_names[off];
86 len = *data;
87 data++;
88
89 /* update the offset to return the offset for the next symbol on
90 * the compressed stream */
91 off += len + 1;
92
93 /* for every byte on the compressed symbol data, copy the table
94 entry for that byte */
95 while(len) {
96 tptr = &kallsyms_token_table[ kallsyms_token_index[*data] ];
97 data++;
98 len--;
99
100 while (*tptr) {
101 if(skipped_first) {
102 *result = *tptr;
103 result++;
104 } else
105 skipped_first = 1;
106 tptr++;
107 }
108 }
109
110 *result = '\0';
111
112 /* return to offset to the next symbol */
113 return off;
114}
115
116/* get symbol type information. This is encoded as a single char at the
117 * begining of the symbol name */
118static char kallsyms_get_symbol_type(unsigned int off)
119{
120 /* get just the first code, look it up in the token table, and return the
121 * first char from this token */
122 return kallsyms_token_table[ kallsyms_token_index[ kallsyms_names[off+1] ] ];
123}
124
125
126/* find the offset on the compressed stream given and index in the
127 * kallsyms array */
128static unsigned int get_symbol_offset(unsigned long pos)
129{
Jan Beulichaad09472006-12-08 02:35:57 -0800130 const u8 *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int i;
132
133 /* use the closest marker we have. We have markers every 256 positions,
134 * so that should be close enough */
135 name = &kallsyms_names[ kallsyms_markers[pos>>8] ];
136
137 /* sequentially scan all the symbols up to the point we're searching for.
138 * Every symbol is stored in a [<len>][<len> bytes of data] format, so we
139 * just need to add the len to the current pointer for every symbol we
140 * wish to skip */
141 for(i = 0; i < (pos&0xFF); i++)
142 name = name + (*name) + 1;
143
144 return name - kallsyms_names;
145}
146
147/* Lookup the address for this symbol. Returns 0 if not found. */
148unsigned long kallsyms_lookup_name(const char *name)
149{
Tejun Heo9281ace2007-07-17 04:03:51 -0700150 char namebuf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unsigned long i;
152 unsigned int off;
153
154 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
155 off = kallsyms_expand_symbol(off, namebuf);
156
157 if (strcmp(namebuf, name) == 0)
158 return kallsyms_addresses[i];
159 }
160 return module_kallsyms_lookup_name(name);
161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700163static unsigned long get_symbol_pos(unsigned long addr,
164 unsigned long *symbolsize,
165 unsigned long *offset)
166{
167 unsigned long symbol_start = 0, symbol_end = 0;
168 unsigned long i, low, high, mid;
169
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700170 /* do a binary search on the sorted kallsyms_addresses array */
171 low = 0;
172 high = kallsyms_num_syms;
173
174 while (high - low > 1) {
Vegard Nossum2fc9c4e2008-07-25 01:45:34 -0700175 mid = low + (high - low) / 2;
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700176 if (kallsyms_addresses[mid] <= addr)
177 low = mid;
178 else
179 high = mid;
180 }
181
182 /*
183 * search for the first aliased symbol. Aliased
184 * symbols are symbols with the same address
185 */
186 while (low && kallsyms_addresses[low-1] == kallsyms_addresses[low])
187 --low;
188
189 symbol_start = kallsyms_addresses[low];
190
191 /* Search for next non-aliased symbol */
192 for (i = low + 1; i < kallsyms_num_syms; i++) {
193 if (kallsyms_addresses[i] > symbol_start) {
194 symbol_end = kallsyms_addresses[i];
195 break;
196 }
197 }
198
199 /* if we found no next symbol, we use the end of the section */
200 if (!symbol_end) {
201 if (is_kernel_inittext(addr))
202 symbol_end = (unsigned long)_einittext;
203 else if (all_var)
204 symbol_end = (unsigned long)_end;
205 else
206 symbol_end = (unsigned long)_etext;
207 }
208
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700209 if (symbolsize)
210 *symbolsize = symbol_end - symbol_start;
211 if (offset)
212 *offset = addr - symbol_start;
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700213
214 return low;
215}
216
217/*
218 * Lookup an address but don't bother to find any names.
219 */
220int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
221 unsigned long *offset)
222{
Rusty Russell6dd06c92008-01-29 17:13:22 -0500223 char namebuf[KSYM_NAME_LEN];
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700224 if (is_ksym_addr(addr))
225 return !!get_symbol_pos(addr, symbolsize, offset);
226
Rusty Russell6dd06c92008-01-29 17:13:22 -0500227 return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf);
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/*
231 * Lookup an address
232 * - modname is set to NULL if it's in the kernel
233 * - we guarantee that the returned name is valid until we reschedule even if
234 * it resides in a module
235 * - we also guarantee that modname will be valid until rescheduled
236 */
237const char *kallsyms_lookup(unsigned long addr,
238 unsigned long *symbolsize,
239 unsigned long *offset,
240 char **modname, char *namebuf)
241{
Tejun Heo9281ace2007-07-17 04:03:51 -0700242 namebuf[KSYM_NAME_LEN - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 namebuf[0] = 0;
244
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700245 if (is_ksym_addr(addr)) {
246 unsigned long pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700248 pos = get_symbol_pos(addr, symbolsize, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* Grab name */
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700250 kallsyms_expand_symbol(get_symbol_offset(pos), namebuf);
Kyle McMartin7a74fc42007-05-30 02:43:16 -0400251 if (modname)
252 *modname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return namebuf;
254 }
255
256 /* see if it's in a module */
Rusty Russell6dd06c92008-01-29 17:13:22 -0500257 return module_address_lookup(addr, symbolsize, offset, modname,
258 namebuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700261int lookup_symbol_name(unsigned long addr, char *symname)
262{
263 symname[0] = '\0';
Tejun Heo9281ace2007-07-17 04:03:51 -0700264 symname[KSYM_NAME_LEN - 1] = '\0';
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700265
266 if (is_ksym_addr(addr)) {
267 unsigned long pos;
268
269 pos = get_symbol_pos(addr, NULL, NULL);
270 /* Grab name */
271 kallsyms_expand_symbol(get_symbol_offset(pos), symname);
272 return 0;
273 }
274 /* see if it's in a module */
275 return lookup_module_symbol_name(addr, symname);
276}
277
Alexey Dobriyana5c43da2007-05-08 00:28:47 -0700278int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
279 unsigned long *offset, char *modname, char *name)
280{
281 name[0] = '\0';
Tejun Heo9281ace2007-07-17 04:03:51 -0700282 name[KSYM_NAME_LEN - 1] = '\0';
Alexey Dobriyana5c43da2007-05-08 00:28:47 -0700283
284 if (is_ksym_addr(addr)) {
285 unsigned long pos;
286
287 pos = get_symbol_pos(addr, size, offset);
288 /* Grab name */
289 kallsyms_expand_symbol(get_symbol_offset(pos), name);
290 modname[0] = '\0';
291 return 0;
292 }
293 /* see if it's in a module */
294 return lookup_module_symbol_attrs(addr, size, offset, modname, name);
295}
296
Robert Peterson42e38082007-04-30 15:09:48 -0700297/* Look up a kernel symbol and return it in a text buffer. */
298int sprint_symbol(char *buffer, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 char *modname;
301 const char *name;
302 unsigned long offset, size;
Hugh Dickins966c8c12008-11-19 15:36:36 -0800303 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Hugh Dickins966c8c12008-11-19 15:36:36 -0800305 name = kallsyms_lookup(address, &size, &offset, &modname, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!name)
Robert Peterson42e38082007-04-30 15:09:48 -0700307 return sprintf(buffer, "0x%lx", address);
Andrew Morton19769b72007-07-15 23:41:24 -0700308
Hugh Dickins966c8c12008-11-19 15:36:36 -0800309 if (name != buffer)
310 strcpy(buffer, name);
311 len = strlen(buffer);
312 buffer += len;
313
Andrew Morton19769b72007-07-15 23:41:24 -0700314 if (modname)
Hugh Dickins966c8c12008-11-19 15:36:36 -0800315 len += sprintf(buffer, "+%#lx/%#lx [%s]",
316 offset, size, modname);
Andrew Morton19769b72007-07-15 23:41:24 -0700317 else
Hugh Dickins966c8c12008-11-19 15:36:36 -0800318 len += sprintf(buffer, "+%#lx/%#lx", offset, size);
319
320 return len;
Robert Peterson42e38082007-04-30 15:09:48 -0700321}
322
323/* Look up a kernel symbol and print it to the kernel messages. */
324void __print_symbol(const char *fmt, unsigned long address)
325{
326 char buffer[KSYM_SYMBOL_LEN];
327
328 sprint_symbol(buffer, address);
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 printk(fmt, buffer);
331}
332
333/* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
334struct kallsym_iter
335{
336 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unsigned long value;
338 unsigned int nameoff; /* If iterating in core kernel symbols */
339 char type;
Tejun Heo9281ace2007-07-17 04:03:51 -0700340 char name[KSYM_NAME_LEN];
341 char module_name[MODULE_NAME_LEN];
Alexey Dobriyanea078902007-05-08 00:28:39 -0700342 int exported;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343};
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static int get_ksymbol_mod(struct kallsym_iter *iter)
346{
Alexey Dobriyanea078902007-05-08 00:28:39 -0700347 if (module_get_kallsym(iter->pos - kallsyms_num_syms, &iter->value,
348 &iter->type, iter->name, iter->module_name,
349 &iter->exported) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return 1;
352}
353
354/* Returns space to next name. */
355static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
356{
357 unsigned off = iter->nameoff;
358
Alexey Dobriyanea078902007-05-08 00:28:39 -0700359 iter->module_name[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 iter->value = kallsyms_addresses[iter->pos];
361
362 iter->type = kallsyms_get_symbol_type(off);
363
364 off = kallsyms_expand_symbol(off, iter->name);
365
366 return off - iter->nameoff;
367}
368
369static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
370{
371 iter->name[0] = '\0';
372 iter->nameoff = get_symbol_offset(new_pos);
373 iter->pos = new_pos;
374}
375
376/* Returns false if pos at or past end of file. */
377static int update_iter(struct kallsym_iter *iter, loff_t pos)
378{
379 /* Module symbols can be accessed randomly. */
380 if (pos >= kallsyms_num_syms) {
381 iter->pos = pos;
382 return get_ksymbol_mod(iter);
383 }
384
385 /* If we're not on the desired position, reset to new position. */
386 if (pos != iter->pos)
387 reset_iter(iter, pos);
388
389 iter->nameoff += get_ksymbol_core(iter);
390 iter->pos++;
391
392 return 1;
393}
394
395static void *s_next(struct seq_file *m, void *p, loff_t *pos)
396{
397 (*pos)++;
398
399 if (!update_iter(m->private, *pos))
400 return NULL;
401 return p;
402}
403
404static void *s_start(struct seq_file *m, loff_t *pos)
405{
406 if (!update_iter(m->private, *pos))
407 return NULL;
408 return m->private;
409}
410
411static void s_stop(struct seq_file *m, void *p)
412{
413}
414
415static int s_show(struct seq_file *m, void *p)
416{
417 struct kallsym_iter *iter = m->private;
418
419 /* Some debugging symbols have no name. Ignore them. */
420 if (!iter->name[0])
421 return 0;
422
Alexey Dobriyanea078902007-05-08 00:28:39 -0700423 if (iter->module_name[0]) {
424 char type;
425
426 /* Label it "global" if it is exported,
427 * "local" if not exported. */
428 type = iter->exported ? toupper(iter->type) :
429 tolower(iter->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 seq_printf(m, "%0*lx %c %s\t[%s]\n",
431 (int)(2*sizeof(void*)),
Alexey Dobriyanea078902007-05-08 00:28:39 -0700432 iter->value, type, iter->name, iter->module_name);
433 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 seq_printf(m, "%0*lx %c %s\n",
435 (int)(2*sizeof(void*)),
436 iter->value, iter->type, iter->name);
437 return 0;
438}
439
Helge Deller15ad7cd2006-12-06 20:40:36 -0800440static const struct seq_operations kallsyms_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 .start = s_start,
442 .next = s_next,
443 .stop = s_stop,
444 .show = s_show
445};
446
447static int kallsyms_open(struct inode *inode, struct file *file)
448{
449 /* We keep iterator in m->private, since normal case is to
450 * s_start from where we left off, so we avoid doing
451 * using get_symbol_offset for every symbol */
452 struct kallsym_iter *iter;
453 int ret;
454
455 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
456 if (!iter)
457 return -ENOMEM;
458 reset_iter(iter, 0);
459
460 ret = seq_open(file, &kallsyms_op);
461 if (ret == 0)
462 ((struct seq_file *)file->private_data)->private = iter;
463 else
464 kfree(iter);
465 return ret;
466}
467
Helge Deller15ad7cd2006-12-06 20:40:36 -0800468static const struct file_operations kallsyms_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 .open = kallsyms_open,
470 .read = seq_read,
471 .llseek = seq_lseek,
Martin Peschke5a0c6a02007-05-08 00:29:25 -0700472 .release = seq_release_private,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473};
474
475static int __init kallsyms_init(void)
476{
Denis V. Lunevc33fff02008-04-29 01:02:31 -0700477 proc_create("kallsyms", 0444, NULL, &kallsyms_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 0;
479}
480__initcall(kallsyms_init);
481
482EXPORT_SYMBOL(__print_symbol);
Robert Peterson42e38082007-04-30 15:09:48 -0700483EXPORT_SYMBOL_GPL(sprint_symbol);