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
)
64 return in_gate_area_no_task(addr
);
67 static inline int is_kernel(unsigned long addr
)
69 if (addr
>= (unsigned long)_stext
&& addr
<= (unsigned long)_end
)
71 return in_gate_area_no_task(addr
);
74 static int is_ksym_addr(unsigned long addr
)
77 return is_kernel(addr
);
79 return is_kernel_text(addr
) || is_kernel_inittext(addr
);
83 * Expand a compressed symbol data into the resulting uncompressed string,
84 * given the offset to where the symbol is in the compressed stream.
86 static unsigned int kallsyms_expand_symbol(unsigned int off
, char *result
)
88 int len
, skipped_first
= 0;
89 const u8
*tptr
, *data
;
91 /* Get the compressed symbol length from the first symbol byte. */
92 data
= &kallsyms_names
[off
];
97 * Update the offset to return the offset for the next symbol on
98 * the compressed stream.
103 * For every byte on the compressed symbol data, copy the table
104 * entry for that byte.
107 tptr
= &kallsyms_token_table
[kallsyms_token_index
[*data
]];
123 /* Return to offset to the next symbol. */
128 * Get symbol type information. This is encoded as a single char at the
129 * beginning of the symbol name.
131 static char kallsyms_get_symbol_type(unsigned int off
)
134 * Get just the first code, look it up in the token table,
135 * and return the first char from this token.
137 return kallsyms_token_table
[kallsyms_token_index
[kallsyms_names
[off
+ 1]]];
142 * Find the offset on the compressed stream given and index in the
145 static unsigned int get_symbol_offset(unsigned long pos
)
151 * Use the closest marker we have. We have markers every 256 positions,
152 * so that should be close enough.
154 name
= &kallsyms_names
[kallsyms_markers
[pos
>> 8]];
157 * Sequentially scan all the symbols up to the point we're searching
158 * for. Every symbol is stored in a [<len>][<len> bytes of data] format,
159 * so we just need to add the len to the current pointer for every
160 * symbol we wish to skip.
162 for (i
= 0; i
< (pos
& 0xFF); i
++)
163 name
= name
+ (*name
) + 1;
165 return name
- kallsyms_names
;
168 /* Lookup the address for this symbol. Returns 0 if not found. */
169 unsigned long kallsyms_lookup_name(const char *name
)
171 char namebuf
[KSYM_NAME_LEN
];
175 for (i
= 0, off
= 0; i
< kallsyms_num_syms
; i
++) {
176 off
= kallsyms_expand_symbol(off
, namebuf
);
178 if (strcmp(namebuf
, name
) == 0)
179 return kallsyms_addresses
[i
];
181 return module_kallsyms_lookup_name(name
);
184 int kallsyms_on_each_symbol(int (*fn
)(void *, const char *, struct module
*,
188 char namebuf
[KSYM_NAME_LEN
];
193 for (i
= 0, off
= 0; i
< kallsyms_num_syms
; i
++) {
194 off
= kallsyms_expand_symbol(off
, namebuf
);
195 ret
= fn(data
, namebuf
, NULL
, kallsyms_addresses
[i
]);
199 return module_kallsyms_on_each_symbol(fn
, data
);
201 EXPORT_SYMBOL_GPL(kallsyms_on_each_symbol
);
203 static unsigned long get_symbol_pos(unsigned long addr
,
204 unsigned long *symbolsize
,
205 unsigned long *offset
)
207 unsigned long symbol_start
= 0, symbol_end
= 0;
208 unsigned long i
, low
, high
, mid
;
210 /* This kernel should never had been booted. */
211 BUG_ON(!kallsyms_addresses
);
213 /* Do a binary search on the sorted kallsyms_addresses array. */
215 high
= kallsyms_num_syms
;
217 while (high
- low
> 1) {
218 mid
= low
+ (high
- low
) / 2;
219 if (kallsyms_addresses
[mid
] <= addr
)
226 * Search for the first aliased symbol. Aliased
227 * symbols are symbols with the same address.
229 while (low
&& kallsyms_addresses
[low
-1] == kallsyms_addresses
[low
])
232 symbol_start
= kallsyms_addresses
[low
];
234 /* Search for next non-aliased symbol. */
235 for (i
= low
+ 1; i
< kallsyms_num_syms
; i
++) {
236 if (kallsyms_addresses
[i
] > symbol_start
) {
237 symbol_end
= kallsyms_addresses
[i
];
242 /* If we found no next symbol, we use the end of the section. */
244 if (is_kernel_inittext(addr
))
245 symbol_end
= (unsigned long)_einittext
;
247 symbol_end
= (unsigned long)_end
;
249 symbol_end
= (unsigned long)_etext
;
253 *symbolsize
= symbol_end
- symbol_start
;
255 *offset
= addr
- symbol_start
;
261 * Lookup an address but don't bother to find any names.
263 int kallsyms_lookup_size_offset(unsigned long addr
, unsigned long *symbolsize
,
264 unsigned long *offset
)
266 char namebuf
[KSYM_NAME_LEN
];
267 if (is_ksym_addr(addr
))
268 return !!get_symbol_pos(addr
, symbolsize
, offset
);
270 return !!module_address_lookup(addr
, symbolsize
, offset
, NULL
, namebuf
);
275 * - modname is set to NULL if it's in the kernel.
276 * - We guarantee that the returned name is valid until we reschedule even if.
277 * It resides in a module.
278 * - We also guarantee that modname will be valid until rescheduled.
280 const char *kallsyms_lookup(unsigned long addr
,
281 unsigned long *symbolsize
,
282 unsigned long *offset
,
283 char **modname
, char *namebuf
)
285 namebuf
[KSYM_NAME_LEN
- 1] = 0;
288 if (is_ksym_addr(addr
)) {
291 pos
= get_symbol_pos(addr
, symbolsize
, offset
);
293 kallsyms_expand_symbol(get_symbol_offset(pos
), namebuf
);
299 /* See if it's in a module. */
300 return module_address_lookup(addr
, symbolsize
, offset
, modname
,
304 int lookup_symbol_name(unsigned long addr
, char *symname
)
307 symname
[KSYM_NAME_LEN
- 1] = '\0';
309 if (is_ksym_addr(addr
)) {
312 pos
= get_symbol_pos(addr
, NULL
, NULL
);
314 kallsyms_expand_symbol(get_symbol_offset(pos
), symname
);
317 /* See if it's in a module. */
318 return lookup_module_symbol_name(addr
, symname
);
321 int lookup_symbol_attrs(unsigned long addr
, unsigned long *size
,
322 unsigned long *offset
, char *modname
, char *name
)
325 name
[KSYM_NAME_LEN
- 1] = '\0';
327 if (is_ksym_addr(addr
)) {
330 pos
= get_symbol_pos(addr
, size
, offset
);
332 kallsyms_expand_symbol(get_symbol_offset(pos
), name
);
336 /* See if it's in a module. */
337 return lookup_module_symbol_attrs(addr
, size
, offset
, modname
, name
);
340 /* Look up a kernel symbol and return it in a text buffer. */
341 int sprint_symbol(char *buffer
, unsigned long address
)
345 unsigned long offset
, size
;
348 name
= kallsyms_lookup(address
, &size
, &offset
, &modname
, buffer
);
350 return sprintf(buffer
, "0x%lx", address
);
353 strcpy(buffer
, name
);
354 len
= strlen(buffer
);
358 len
+= sprintf(buffer
, "+%#lx/%#lx [%s]",
359 offset
, size
, modname
);
361 len
+= sprintf(buffer
, "+%#lx/%#lx", offset
, size
);
365 EXPORT_SYMBOL_GPL(sprint_symbol
);
367 /* Look up a kernel symbol and print it to the kernel messages. */
368 void __print_symbol(const char *fmt
, unsigned long address
)
370 char buffer
[KSYM_SYMBOL_LEN
];
372 sprint_symbol(buffer
, address
);
376 EXPORT_SYMBOL(__print_symbol
);
378 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
379 struct kallsym_iter
{
382 unsigned int nameoff
; /* If iterating in core kernel symbols. */
384 char name
[KSYM_NAME_LEN
];
385 char module_name
[MODULE_NAME_LEN
];
389 static int get_ksymbol_mod(struct kallsym_iter
*iter
)
391 if (module_get_kallsym(iter
->pos
- kallsyms_num_syms
, &iter
->value
,
392 &iter
->type
, iter
->name
, iter
->module_name
,
393 &iter
->exported
) < 0)
398 /* Returns space to next name. */
399 static unsigned long get_ksymbol_core(struct kallsym_iter
*iter
)
401 unsigned off
= iter
->nameoff
;
403 iter
->module_name
[0] = '\0';
404 iter
->value
= kallsyms_addresses
[iter
->pos
];
406 iter
->type
= kallsyms_get_symbol_type(off
);
408 off
= kallsyms_expand_symbol(off
, iter
->name
);
410 return off
- iter
->nameoff
;
413 static void reset_iter(struct kallsym_iter
*iter
, loff_t new_pos
)
415 iter
->name
[0] = '\0';
416 iter
->nameoff
= get_symbol_offset(new_pos
);
420 /* Returns false if pos at or past end of file. */
421 static int update_iter(struct kallsym_iter
*iter
, loff_t pos
)
423 /* Module symbols can be accessed randomly. */
424 if (pos
>= kallsyms_num_syms
) {
426 return get_ksymbol_mod(iter
);
429 /* If we're not on the desired position, reset to new position. */
430 if (pos
!= iter
->pos
)
431 reset_iter(iter
, pos
);
433 iter
->nameoff
+= get_ksymbol_core(iter
);
439 static void *s_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
443 if (!update_iter(m
->private, *pos
))
448 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
450 if (!update_iter(m
->private, *pos
))
455 static void s_stop(struct seq_file
*m
, void *p
)
459 static int s_show(struct seq_file
*m
, void *p
)
461 struct kallsym_iter
*iter
= m
->private;
463 /* Some debugging symbols have no name. Ignore them. */
467 if (iter
->module_name
[0]) {
471 * Label it "global" if it is exported,
472 * "local" if not exported.
474 type
= iter
->exported
? toupper(iter
->type
) :
476 seq_printf(m
, "%0*lx %c %s\t[%s]\n",
477 (int)(2 * sizeof(void *)),
478 iter
->value
, type
, iter
->name
, iter
->module_name
);
480 seq_printf(m
, "%0*lx %c %s\n",
481 (int)(2 * sizeof(void *)),
482 iter
->value
, iter
->type
, iter
->name
);
486 static const struct seq_operations kallsyms_op
= {
493 static int kallsyms_open(struct inode
*inode
, struct file
*file
)
496 * We keep iterator in m->private, since normal case is to
497 * s_start from where we left off, so we avoid doing
498 * using get_symbol_offset for every symbol.
500 struct kallsym_iter
*iter
;
503 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
508 ret
= seq_open(file
, &kallsyms_op
);
510 ((struct seq_file
*)file
->private_data
)->private = iter
;
516 static const struct file_operations kallsyms_operations
= {
517 .open
= kallsyms_open
,
520 .release
= seq_release_private
,
523 static int __init
kallsyms_init(void)
525 proc_create("kallsyms", 0444, NULL
, &kallsyms_operations
);
528 device_initcall(kallsyms_init
);