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
33 extern const unsigned long kallsyms_addresses
[];
34 extern const u8 kallsyms_names
[];
36 /* tell the compiler that the count isn't in the small data section if the arch
39 extern const unsigned long kallsyms_num_syms
40 __attribute__((__section__(".rodata")));
42 extern const u8 kallsyms_token_table
[];
43 extern const u16 kallsyms_token_index
[];
45 extern const unsigned long kallsyms_markers
[];
47 static inline int is_kernel_inittext(unsigned long addr
)
49 if (addr
>= (unsigned long)_sinittext
50 && addr
<= (unsigned long)_einittext
)
55 static inline int is_kernel_text(unsigned long addr
)
57 if (addr
>= (unsigned long)_stext
&& addr
<= (unsigned long)_etext
)
59 return in_gate_area_no_task(addr
);
62 static inline int is_kernel(unsigned long addr
)
64 if (addr
>= (unsigned long)_stext
&& addr
<= (unsigned long)_end
)
66 return in_gate_area_no_task(addr
);
69 static int is_ksym_addr(unsigned long addr
)
72 return is_kernel(addr
);
74 return is_kernel_text(addr
) || is_kernel_inittext(addr
);
77 /* expand a compressed symbol data into the resulting uncompressed string,
78 given the offset to where the symbol is in the compressed stream */
79 static unsigned int kallsyms_expand_symbol(unsigned int off
, char *result
)
81 int len
, skipped_first
= 0;
82 const u8
*tptr
, *data
;
84 /* get the compressed symbol length from the first symbol byte */
85 data
= &kallsyms_names
[off
];
89 /* update the offset to return the offset for the next symbol on
90 * the compressed stream */
93 /* for every byte on the compressed symbol data, copy the table
94 entry for that byte */
96 tptr
= &kallsyms_token_table
[ kallsyms_token_index
[*data
] ];
112 /* return to offset to the next symbol */
116 /* get symbol type information. This is encoded as a single char at the
117 * begining of the symbol name */
118 static char kallsyms_get_symbol_type(unsigned int off
)
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] ] ];
126 /* find the offset on the compressed stream given and index in the
128 static unsigned int get_symbol_offset(unsigned long pos
)
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] ];
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
141 for(i
= 0; i
< (pos
&0xFF); i
++)
142 name
= name
+ (*name
) + 1;
144 return name
- kallsyms_names
;
147 /* Lookup the address for this symbol. Returns 0 if not found. */
148 unsigned long kallsyms_lookup_name(const char *name
)
150 char namebuf
[KSYM_NAME_LEN
];
154 for (i
= 0, off
= 0; i
< kallsyms_num_syms
; i
++) {
155 off
= kallsyms_expand_symbol(off
, namebuf
);
157 if (strcmp(namebuf
, name
) == 0)
158 return kallsyms_addresses
[i
];
160 return module_kallsyms_lookup_name(name
);
163 static unsigned long get_symbol_pos(unsigned long addr
,
164 unsigned long *symbolsize
,
165 unsigned long *offset
)
167 unsigned long symbol_start
= 0, symbol_end
= 0;
168 unsigned long i
, low
, high
, mid
;
170 /* do a binary search on the sorted kallsyms_addresses array */
172 high
= kallsyms_num_syms
;
174 while (high
- low
> 1) {
175 mid
= low
+ (high
- low
) / 2;
176 if (kallsyms_addresses
[mid
] <= addr
)
183 * search for the first aliased symbol. Aliased
184 * symbols are symbols with the same address
186 while (low
&& kallsyms_addresses
[low
-1] == kallsyms_addresses
[low
])
189 symbol_start
= kallsyms_addresses
[low
];
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
];
199 /* if we found no next symbol, we use the end of the section */
201 if (is_kernel_inittext(addr
))
202 symbol_end
= (unsigned long)_einittext
;
204 symbol_end
= (unsigned long)_end
;
206 symbol_end
= (unsigned long)_etext
;
210 *symbolsize
= symbol_end
- symbol_start
;
212 *offset
= addr
- symbol_start
;
218 * Lookup an address but don't bother to find any names.
220 int kallsyms_lookup_size_offset(unsigned long addr
, unsigned long *symbolsize
,
221 unsigned long *offset
)
223 char namebuf
[KSYM_NAME_LEN
];
224 if (is_ksym_addr(addr
))
225 return !!get_symbol_pos(addr
, symbolsize
, offset
);
227 return !!module_address_lookup(addr
, symbolsize
, offset
, NULL
, namebuf
);
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
237 const char *kallsyms_lookup(unsigned long addr
,
238 unsigned long *symbolsize
,
239 unsigned long *offset
,
240 char **modname
, char *namebuf
)
242 namebuf
[KSYM_NAME_LEN
- 1] = 0;
245 if (is_ksym_addr(addr
)) {
248 pos
= get_symbol_pos(addr
, symbolsize
, offset
);
250 kallsyms_expand_symbol(get_symbol_offset(pos
), namebuf
);
256 /* see if it's in a module */
257 return module_address_lookup(addr
, symbolsize
, offset
, modname
,
261 int lookup_symbol_name(unsigned long addr
, char *symname
)
264 symname
[KSYM_NAME_LEN
- 1] = '\0';
266 if (is_ksym_addr(addr
)) {
269 pos
= get_symbol_pos(addr
, NULL
, NULL
);
271 kallsyms_expand_symbol(get_symbol_offset(pos
), symname
);
274 /* see if it's in a module */
275 return lookup_module_symbol_name(addr
, symname
);
278 int lookup_symbol_attrs(unsigned long addr
, unsigned long *size
,
279 unsigned long *offset
, char *modname
, char *name
)
282 name
[KSYM_NAME_LEN
- 1] = '\0';
284 if (is_ksym_addr(addr
)) {
287 pos
= get_symbol_pos(addr
, size
, offset
);
289 kallsyms_expand_symbol(get_symbol_offset(pos
), name
);
293 /* see if it's in a module */
294 return lookup_module_symbol_attrs(addr
, size
, offset
, modname
, name
);
297 /* Look up a kernel symbol and return it in a text buffer. */
298 int sprint_symbol(char *buffer
, unsigned long address
)
302 unsigned long offset
, size
;
305 name
= kallsyms_lookup(address
, &size
, &offset
, &modname
, buffer
);
307 return sprintf(buffer
, "0x%lx", address
);
310 strcpy(buffer
, name
);
311 len
= strlen(buffer
);
315 len
+= sprintf(buffer
, "+%#lx/%#lx [%s]",
316 offset
, size
, modname
);
318 len
+= sprintf(buffer
, "+%#lx/%#lx", offset
, size
);
323 /* Look up a kernel symbol and print it to the kernel messages. */
324 void __print_symbol(const char *fmt
, unsigned long address
)
326 char buffer
[KSYM_SYMBOL_LEN
];
328 sprint_symbol(buffer
, address
);
333 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
338 unsigned int nameoff
; /* If iterating in core kernel symbols */
340 char name
[KSYM_NAME_LEN
];
341 char module_name
[MODULE_NAME_LEN
];
345 static int get_ksymbol_mod(struct kallsym_iter
*iter
)
347 if (module_get_kallsym(iter
->pos
- kallsyms_num_syms
, &iter
->value
,
348 &iter
->type
, iter
->name
, iter
->module_name
,
349 &iter
->exported
) < 0)
354 /* Returns space to next name. */
355 static unsigned long get_ksymbol_core(struct kallsym_iter
*iter
)
357 unsigned off
= iter
->nameoff
;
359 iter
->module_name
[0] = '\0';
360 iter
->value
= kallsyms_addresses
[iter
->pos
];
362 iter
->type
= kallsyms_get_symbol_type(off
);
364 off
= kallsyms_expand_symbol(off
, iter
->name
);
366 return off
- iter
->nameoff
;
369 static void reset_iter(struct kallsym_iter
*iter
, loff_t new_pos
)
371 iter
->name
[0] = '\0';
372 iter
->nameoff
= get_symbol_offset(new_pos
);
376 /* Returns false if pos at or past end of file. */
377 static int update_iter(struct kallsym_iter
*iter
, loff_t pos
)
379 /* Module symbols can be accessed randomly. */
380 if (pos
>= kallsyms_num_syms
) {
382 return get_ksymbol_mod(iter
);
385 /* If we're not on the desired position, reset to new position. */
386 if (pos
!= iter
->pos
)
387 reset_iter(iter
, pos
);
389 iter
->nameoff
+= get_ksymbol_core(iter
);
395 static void *s_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
399 if (!update_iter(m
->private, *pos
))
404 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
406 if (!update_iter(m
->private, *pos
))
411 static void s_stop(struct seq_file
*m
, void *p
)
415 static int s_show(struct seq_file
*m
, void *p
)
417 struct kallsym_iter
*iter
= m
->private;
419 /* Some debugging symbols have no name. Ignore them. */
423 if (iter
->module_name
[0]) {
426 /* Label it "global" if it is exported,
427 * "local" if not exported. */
428 type
= iter
->exported
? toupper(iter
->type
) :
430 seq_printf(m
, "%0*lx %c %s\t[%s]\n",
431 (int)(2*sizeof(void*)),
432 iter
->value
, type
, iter
->name
, iter
->module_name
);
434 seq_printf(m
, "%0*lx %c %s\n",
435 (int)(2*sizeof(void*)),
436 iter
->value
, iter
->type
, iter
->name
);
440 static const struct seq_operations kallsyms_op
= {
447 static int kallsyms_open(struct inode
*inode
, struct file
*file
)
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
;
455 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
460 ret
= seq_open(file
, &kallsyms_op
);
462 ((struct seq_file
*)file
->private_data
)->private = iter
;
468 static const struct file_operations kallsyms_operations
= {
469 .open
= kallsyms_open
,
472 .release
= seq_release_private
,
475 static int __init
kallsyms_init(void)
477 proc_create("kallsyms", 0444, NULL
, &kallsyms_operations
);
480 __initcall(kallsyms_init
);
482 EXPORT_SYMBOL(__print_symbol
);
483 EXPORT_SYMBOL_GPL(sprint_symbol
);