]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/kallsyms.c
tg3: Restore likely() check in tg3_poll_msix()
[net-next-2.6.git] / kernel / kallsyms.c
CommitLineData
1da177e4
LT
1/*
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>
4e57b681 21#include <linux/sched.h> /* for cond_resched */
1da177e4 22#include <linux/mm.h>
07354a00 23#include <linux/ctype.h>
1da177e4
LT
24
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
ad6ccfad
MK
33/*
34 * These will be re-linked against their real values
35 * during the second link stage.
36 */
2ea03891
SR
37extern const unsigned long kallsyms_addresses[] __attribute__((weak));
38extern const u8 kallsyms_names[] __attribute__((weak));
1da177e4 39
ad6ccfad
MK
40/*
41 * Tell the compiler that the count isn't in the small data section if the arch
42 * has one (eg: FRV).
9e6c1e63
DH
43 */
44extern const unsigned long kallsyms_num_syms
2ea03891 45__attribute__((weak, section(".rodata")));
9e6c1e63 46
2ea03891
SR
47extern const u8 kallsyms_token_table[] __attribute__((weak));
48extern const u16 kallsyms_token_index[] __attribute__((weak));
1da177e4 49
2ea03891 50extern const unsigned long kallsyms_markers[] __attribute__((weak));
1da177e4
LT
51
52static inline int is_kernel_inittext(unsigned long addr)
53{
54 if (addr >= (unsigned long)_sinittext
55 && addr <= (unsigned long)_einittext)
56 return 1;
57 return 0;
58}
59
60static inline int is_kernel_text(unsigned long addr)
61{
128e8db3
MF
62 if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) ||
63 arch_is_kernel_text(addr))
1da177e4
LT
64 return 1;
65 return in_gate_area_no_task(addr);
66}
67
68static inline int is_kernel(unsigned long addr)
69{
70 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
71 return 1;
72 return in_gate_area_no_task(addr);
73}
74
ffc50891
FBH
75static int is_ksym_addr(unsigned long addr)
76{
77 if (all_var)
78 return is_kernel(addr);
79
a3b81113 80 return is_kernel_text(addr) || is_kernel_inittext(addr);
ffc50891
FBH
81}
82
ad6ccfad
MK
83/*
84 * Expand a compressed symbol data into the resulting uncompressed string,
85 * given the offset to where the symbol is in the compressed stream.
86 */
1da177e4
LT
87static unsigned int kallsyms_expand_symbol(unsigned int off, char *result)
88{
89 int len, skipped_first = 0;
aad09470 90 const u8 *tptr, *data;
1da177e4 91
ad6ccfad 92 /* Get the compressed symbol length from the first symbol byte. */
1da177e4
LT
93 data = &kallsyms_names[off];
94 len = *data;
95 data++;
96
ad6ccfad
MK
97 /*
98 * Update the offset to return the offset for the next symbol on
99 * the compressed stream.
100 */
1da177e4
LT
101 off += len + 1;
102
ad6ccfad
MK
103 /*
104 * For every byte on the compressed symbol data, copy the table
105 * entry for that byte.
106 */
107 while (len) {
108 tptr = &kallsyms_token_table[kallsyms_token_index[*data]];
1da177e4
LT
109 data++;
110 len--;
111
112 while (*tptr) {
ad6ccfad 113 if (skipped_first) {
1da177e4
LT
114 *result = *tptr;
115 result++;
116 } else
117 skipped_first = 1;
118 tptr++;
119 }
120 }
121
122 *result = '\0';
123
ad6ccfad 124 /* Return to offset to the next symbol. */
1da177e4
LT
125 return off;
126}
127
ad6ccfad
MK
128/*
129 * Get symbol type information. This is encoded as a single char at the
130 * beginning of the symbol name.
131 */
1da177e4
LT
132static char kallsyms_get_symbol_type(unsigned int off)
133{
ad6ccfad
MK
134 /*
135 * Get just the first code, look it up in the token table,
136 * and return the first char from this token.
137 */
138 return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]];
1da177e4
LT
139}
140
141
ad6ccfad
MK
142/*
143 * Find the offset on the compressed stream given and index in the
144 * kallsyms array.
145 */
1da177e4
LT
146static unsigned int get_symbol_offset(unsigned long pos)
147{
aad09470 148 const u8 *name;
1da177e4
LT
149 int i;
150
ad6ccfad
MK
151 /*
152 * Use the closest marker we have. We have markers every 256 positions,
153 * so that should be close enough.
154 */
155 name = &kallsyms_names[kallsyms_markers[pos >> 8]];
1da177e4 156
ad6ccfad
MK
157 /*
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.
162 */
163 for (i = 0; i < (pos & 0xFF); i++)
1da177e4
LT
164 name = name + (*name) + 1;
165
166 return name - kallsyms_names;
167}
168
169/* Lookup the address for this symbol. Returns 0 if not found. */
170unsigned long kallsyms_lookup_name(const char *name)
171{
9281acea 172 char namebuf[KSYM_NAME_LEN];
1da177e4
LT
173 unsigned long i;
174 unsigned int off;
175
176 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
177 off = kallsyms_expand_symbol(off, namebuf);
178
179 if (strcmp(namebuf, name) == 0)
180 return kallsyms_addresses[i];
181 }
182 return module_kallsyms_lookup_name(name);
183}
f60d24d2 184EXPORT_SYMBOL_GPL(kallsyms_lookup_name);
1da177e4 185
75a66614
AK
186int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
187 unsigned long),
188 void *data)
189{
190 char namebuf[KSYM_NAME_LEN];
191 unsigned long i;
192 unsigned int off;
193 int ret;
194
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]);
198 if (ret != 0)
199 return ret;
200 }
201 return module_kallsyms_on_each_symbol(fn, data);
202}
203EXPORT_SYMBOL_GPL(kallsyms_on_each_symbol);
204
ffc50891
FBH
205static unsigned long get_symbol_pos(unsigned long addr,
206 unsigned long *symbolsize,
207 unsigned long *offset)
208{
209 unsigned long symbol_start = 0, symbol_end = 0;
210 unsigned long i, low, high, mid;
211
2ea03891
SR
212 /* This kernel should never had been booted. */
213 BUG_ON(!kallsyms_addresses);
214
ad6ccfad 215 /* Do a binary search on the sorted kallsyms_addresses array. */
ffc50891
FBH
216 low = 0;
217 high = kallsyms_num_syms;
218
219 while (high - low > 1) {
2fc9c4e1 220 mid = low + (high - low) / 2;
ffc50891
FBH
221 if (kallsyms_addresses[mid] <= addr)
222 low = mid;
223 else
224 high = mid;
225 }
226
227 /*
ad6ccfad
MK
228 * Search for the first aliased symbol. Aliased
229 * symbols are symbols with the same address.
ffc50891
FBH
230 */
231 while (low && kallsyms_addresses[low-1] == kallsyms_addresses[low])
232 --low;
233
234 symbol_start = kallsyms_addresses[low];
235
ad6ccfad 236 /* Search for next non-aliased symbol. */
ffc50891
FBH
237 for (i = low + 1; i < kallsyms_num_syms; i++) {
238 if (kallsyms_addresses[i] > symbol_start) {
239 symbol_end = kallsyms_addresses[i];
240 break;
241 }
242 }
243
ad6ccfad 244 /* If we found no next symbol, we use the end of the section. */
ffc50891
FBH
245 if (!symbol_end) {
246 if (is_kernel_inittext(addr))
247 symbol_end = (unsigned long)_einittext;
248 else if (all_var)
249 symbol_end = (unsigned long)_end;
250 else
251 symbol_end = (unsigned long)_etext;
252 }
253
ffb45122
AD
254 if (symbolsize)
255 *symbolsize = symbol_end - symbol_start;
256 if (offset)
257 *offset = addr - symbol_start;
ffc50891
FBH
258
259 return low;
260}
261
262/*
263 * Lookup an address but don't bother to find any names.
264 */
265int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
266 unsigned long *offset)
267{
6dd06c9f 268 char namebuf[KSYM_NAME_LEN];
ffc50891
FBH
269 if (is_ksym_addr(addr))
270 return !!get_symbol_pos(addr, symbolsize, offset);
271
6dd06c9f 272 return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf);
ffc50891
FBH
273}
274
1da177e4
LT
275/*
276 * Lookup an address
ad6ccfad
MK
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.
1da177e4
LT
281 */
282const char *kallsyms_lookup(unsigned long addr,
283 unsigned long *symbolsize,
284 unsigned long *offset,
285 char **modname, char *namebuf)
286{
9281acea 287 namebuf[KSYM_NAME_LEN - 1] = 0;
1da177e4
LT
288 namebuf[0] = 0;
289
ffc50891
FBH
290 if (is_ksym_addr(addr)) {
291 unsigned long pos;
1da177e4 292
ffc50891 293 pos = get_symbol_pos(addr, symbolsize, offset);
1da177e4 294 /* Grab name */
ffc50891 295 kallsyms_expand_symbol(get_symbol_offset(pos), namebuf);
7a74fc49
KM
296 if (modname)
297 *modname = NULL;
1da177e4
LT
298 return namebuf;
299 }
300
ad6ccfad 301 /* See if it's in a module. */
6dd06c9f
RR
302 return module_address_lookup(addr, symbolsize, offset, modname,
303 namebuf);
1da177e4
LT
304}
305
9d65cb4a
AD
306int lookup_symbol_name(unsigned long addr, char *symname)
307{
308 symname[0] = '\0';
9281acea 309 symname[KSYM_NAME_LEN - 1] = '\0';
9d65cb4a
AD
310
311 if (is_ksym_addr(addr)) {
312 unsigned long pos;
313
314 pos = get_symbol_pos(addr, NULL, NULL);
315 /* Grab name */
316 kallsyms_expand_symbol(get_symbol_offset(pos), symname);
317 return 0;
318 }
ad6ccfad 319 /* See if it's in a module. */
9d65cb4a
AD
320 return lookup_module_symbol_name(addr, symname);
321}
322
a5c43dae
AD
323int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
324 unsigned long *offset, char *modname, char *name)
325{
326 name[0] = '\0';
9281acea 327 name[KSYM_NAME_LEN - 1] = '\0';
a5c43dae
AD
328
329 if (is_ksym_addr(addr)) {
330 unsigned long pos;
331
332 pos = get_symbol_pos(addr, size, offset);
333 /* Grab name */
334 kallsyms_expand_symbol(get_symbol_offset(pos), name);
335 modname[0] = '\0';
336 return 0;
337 }
ad6ccfad 338 /* See if it's in a module. */
a5c43dae
AD
339 return lookup_module_symbol_attrs(addr, size, offset, modname, name);
340}
341
42e38083
RP
342/* Look up a kernel symbol and return it in a text buffer. */
343int sprint_symbol(char *buffer, unsigned long address)
1da177e4
LT
344{
345 char *modname;
346 const char *name;
347 unsigned long offset, size;
966c8c12 348 int len;
1da177e4 349
966c8c12 350 name = kallsyms_lookup(address, &size, &offset, &modname, buffer);
1da177e4 351 if (!name)
42e38083 352 return sprintf(buffer, "0x%lx", address);
19769b76 353
966c8c12
HD
354 if (name != buffer)
355 strcpy(buffer, name);
356 len = strlen(buffer);
357 buffer += len;
358
19769b76 359 if (modname)
966c8c12
HD
360 len += sprintf(buffer, "+%#lx/%#lx [%s]",
361 offset, size, modname);
19769b76 362 else
966c8c12
HD
363 len += sprintf(buffer, "+%#lx/%#lx", offset, size);
364
365 return len;
42e38083 366}
ad6ccfad 367EXPORT_SYMBOL_GPL(sprint_symbol);
42e38083
RP
368
369/* Look up a kernel symbol and print it to the kernel messages. */
370void __print_symbol(const char *fmt, unsigned long address)
371{
372 char buffer[KSYM_SYMBOL_LEN];
373
374 sprint_symbol(buffer, address);
375
1da177e4
LT
376 printk(fmt, buffer);
377}
ad6ccfad 378EXPORT_SYMBOL(__print_symbol);
1da177e4
LT
379
380/* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
ad6ccfad 381struct kallsym_iter {
1da177e4 382 loff_t pos;
1da177e4 383 unsigned long value;
ad6ccfad 384 unsigned int nameoff; /* If iterating in core kernel symbols. */
1da177e4 385 char type;
9281acea
TH
386 char name[KSYM_NAME_LEN];
387 char module_name[MODULE_NAME_LEN];
ea07890a 388 int exported;
1da177e4
LT
389};
390
1da177e4
LT
391static int get_ksymbol_mod(struct kallsym_iter *iter)
392{
ea07890a
AD
393 if (module_get_kallsym(iter->pos - kallsyms_num_syms, &iter->value,
394 &iter->type, iter->name, iter->module_name,
395 &iter->exported) < 0)
1da177e4 396 return 0;
1da177e4
LT
397 return 1;
398}
399
400/* Returns space to next name. */
401static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
402{
403 unsigned off = iter->nameoff;
404
ea07890a 405 iter->module_name[0] = '\0';
1da177e4
LT
406 iter->value = kallsyms_addresses[iter->pos];
407
408 iter->type = kallsyms_get_symbol_type(off);
409
410 off = kallsyms_expand_symbol(off, iter->name);
411
412 return off - iter->nameoff;
413}
414
415static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
416{
417 iter->name[0] = '\0';
418 iter->nameoff = get_symbol_offset(new_pos);
419 iter->pos = new_pos;
420}
421
422/* Returns false if pos at or past end of file. */
423static int update_iter(struct kallsym_iter *iter, loff_t pos)
424{
425 /* Module symbols can be accessed randomly. */
426 if (pos >= kallsyms_num_syms) {
427 iter->pos = pos;
428 return get_ksymbol_mod(iter);
429 }
ad6ccfad 430
1da177e4
LT
431 /* If we're not on the desired position, reset to new position. */
432 if (pos != iter->pos)
433 reset_iter(iter, pos);
434
435 iter->nameoff += get_ksymbol_core(iter);
436 iter->pos++;
437
438 return 1;
439}
440
441static void *s_next(struct seq_file *m, void *p, loff_t *pos)
442{
443 (*pos)++;
444
445 if (!update_iter(m->private, *pos))
446 return NULL;
447 return p;
448}
449
450static void *s_start(struct seq_file *m, loff_t *pos)
451{
452 if (!update_iter(m->private, *pos))
453 return NULL;
454 return m->private;
455}
456
457static void s_stop(struct seq_file *m, void *p)
458{
459}
460
461static int s_show(struct seq_file *m, void *p)
462{
463 struct kallsym_iter *iter = m->private;
464
ad6ccfad 465 /* Some debugging symbols have no name. Ignore them. */
1da177e4
LT
466 if (!iter->name[0])
467 return 0;
468
ea07890a
AD
469 if (iter->module_name[0]) {
470 char type;
471
ad6ccfad
MK
472 /*
473 * Label it "global" if it is exported,
474 * "local" if not exported.
475 */
ea07890a
AD
476 type = iter->exported ? toupper(iter->type) :
477 tolower(iter->type);
1da177e4 478 seq_printf(m, "%0*lx %c %s\t[%s]\n",
ad6ccfad 479 (int)(2 * sizeof(void *)),
ea07890a
AD
480 iter->value, type, iter->name, iter->module_name);
481 } else
1da177e4 482 seq_printf(m, "%0*lx %c %s\n",
ad6ccfad 483 (int)(2 * sizeof(void *)),
1da177e4
LT
484 iter->value, iter->type, iter->name);
485 return 0;
486}
487
15ad7cdc 488static const struct seq_operations kallsyms_op = {
1da177e4
LT
489 .start = s_start,
490 .next = s_next,
491 .stop = s_stop,
492 .show = s_show
493};
494
495static int kallsyms_open(struct inode *inode, struct file *file)
496{
ad6ccfad
MK
497 /*
498 * We keep iterator in m->private, since normal case is to
1da177e4 499 * s_start from where we left off, so we avoid doing
ad6ccfad
MK
500 * using get_symbol_offset for every symbol.
501 */
1da177e4
LT
502 struct kallsym_iter *iter;
503 int ret;
504
505 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
506 if (!iter)
507 return -ENOMEM;
508 reset_iter(iter, 0);
509
510 ret = seq_open(file, &kallsyms_op);
511 if (ret == 0)
512 ((struct seq_file *)file->private_data)->private = iter;
513 else
514 kfree(iter);
515 return ret;
516}
517
15ad7cdc 518static const struct file_operations kallsyms_operations = {
1da177e4
LT
519 .open = kallsyms_open,
520 .read = seq_read,
521 .llseek = seq_lseek,
5a0c6a0d 522 .release = seq_release_private,
1da177e4
LT
523};
524
525static int __init kallsyms_init(void)
526{
c33fff0a 527 proc_create("kallsyms", 0444, NULL, &kallsyms_operations);
1da177e4
LT
528 return 0;
529}
ad6ccfad 530device_initcall(kallsyms_init);