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>
22 /* These will be re-linked against their real values during the second link stage */
23 extern unsigned long kallsyms_addresses
[] __attribute__((weak
));
24 extern unsigned long kallsyms_num_syms
__attribute__((weak
));
25 extern u8 kallsyms_names
[] __attribute__((weak
));
27 extern u8 kallsyms_token_table
[] __attribute__((weak
));
28 extern u16 kallsyms_token_index
[] __attribute__((weak
));
30 extern unsigned long kallsyms_markers
[] __attribute__((weak
));
32 /* Defined by the linker script. */
33 extern char _stext
[], _etext
[], _sinittext
[], _einittext
[];
35 static inline int is_kernel_inittext(unsigned long addr
)
37 if (addr
>= (unsigned long)_sinittext
38 && addr
<= (unsigned long)_einittext
)
43 static inline int is_kernel_text(unsigned long addr
)
45 if (addr
>= (unsigned long)_stext
&& addr
<= (unsigned long)_etext
)
50 /* expand a compressed symbol data into the resulting uncompressed string,
51 given the offset to where the symbol is in the compressed stream */
52 static unsigned int kallsyms_expand_symbol(unsigned int off
, char *result
)
54 int len
, skipped_first
= 0;
57 /* get the compressed symbol length from the first symbol byte */
58 data
= &kallsyms_names
[off
];
62 /* update the offset to return the offset for the next symbol on
63 * the compressed stream */
66 /* for every byte on the compressed symbol data, copy the table
67 entry for that byte */
69 tptr
= &kallsyms_token_table
[ kallsyms_token_index
[*data
] ];
85 /* return to offset to the next symbol */
89 /* get symbol type information. This is encoded as a single char at the
90 * begining of the symbol name */
91 static char kallsyms_get_symbol_type(unsigned int off
)
93 /* get just the first code, look it up in the token table, and return the
94 * first char from this token */
95 return kallsyms_token_table
[ kallsyms_token_index
[ kallsyms_names
[off
+1] ] ];
99 /* find the offset on the compressed stream given and index in the
101 static unsigned int get_symbol_offset(unsigned long pos
)
106 /* use the closest marker we have. We have markers every 256 positions,
107 * so that should be close enough */
108 name
= &kallsyms_names
[ kallsyms_markers
[pos
>>8] ];
110 /* sequentially scan all the symbols up to the point we're searching for.
111 * Every symbol is stored in a [<len>][<len> bytes of data] format, so we
112 * just need to add the len to the current pointer for every symbol we
114 for(i
= 0; i
< (pos
&0xFF); i
++)
115 name
= name
+ (*name
) + 1;
117 return name
- kallsyms_names
;
120 /* Lookup the address for this symbol. Returns 0 if not found. */
121 unsigned long kallsyms_lookup_name(const char *name
)
123 char namebuf
[KSYM_NAME_LEN
+1];
127 for (i
= 0, off
= 0; i
< kallsyms_num_syms
; i
++) {
128 off
= kallsyms_expand_symbol(off
, namebuf
);
130 if (strcmp(namebuf
, name
) == 0)
131 return kallsyms_addresses
[i
];
133 return module_kallsyms_lookup_name(name
);
136 /* Lookup an address. modname is set to NULL if it's in the kernel. */
137 const char *kallsyms_lookup(unsigned long addr
,
138 unsigned long *symbolsize
,
139 unsigned long *offset
,
140 char **modname
, char *namebuf
)
142 unsigned long i
, low
, high
, mid
;
144 /* This kernel should never had been booted. */
145 BUG_ON(!kallsyms_addresses
);
147 namebuf
[KSYM_NAME_LEN
] = 0;
150 if (is_kernel_text(addr
) || is_kernel_inittext(addr
)) {
151 unsigned long symbol_end
=0;
153 /* do a binary search on the sorted kallsyms_addresses array */
155 high
= kallsyms_num_syms
;
157 while (high
-low
> 1) {
158 mid
= (low
+ high
) / 2;
159 if (kallsyms_addresses
[mid
] <= addr
) low
= mid
;
163 /* search for the first aliased symbol. Aliased symbols are
164 symbols with the same address */
165 while (low
&& kallsyms_addresses
[low
- 1] == kallsyms_addresses
[low
])
169 kallsyms_expand_symbol(get_symbol_offset(low
), namebuf
);
171 /* Search for next non-aliased symbol */
172 for (i
= low
+ 1; i
< kallsyms_num_syms
; i
++) {
173 if (kallsyms_addresses
[i
] > kallsyms_addresses
[low
]) {
174 symbol_end
= kallsyms_addresses
[i
];
179 /* if we found no next symbol, we use the end of the section */
181 if (is_kernel_inittext(addr
))
182 symbol_end
= (unsigned long)_einittext
;
184 symbol_end
= (unsigned long)_etext
;
187 *symbolsize
= symbol_end
- kallsyms_addresses
[low
];
189 *offset
= addr
- kallsyms_addresses
[low
];
193 return module_address_lookup(addr
, symbolsize
, offset
, modname
);
196 /* Replace "%s" in format with address, or returns -errno. */
197 void __print_symbol(const char *fmt
, unsigned long address
)
201 unsigned long offset
, size
;
202 char namebuf
[KSYM_NAME_LEN
+1];
203 char buffer
[sizeof("%s+%#lx/%#lx [%s]") + KSYM_NAME_LEN
+
204 2*(BITS_PER_LONG
*3/10) + MODULE_NAME_LEN
+ 1];
206 name
= kallsyms_lookup(address
, &size
, &offset
, &modname
, namebuf
);
209 sprintf(buffer
, "0x%lx", address
);
212 sprintf(buffer
, "%s+%#lx/%#lx [%s]", name
, offset
,
215 sprintf(buffer
, "%s+%#lx/%#lx", name
, offset
, size
);
220 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
224 struct module
*owner
;
226 unsigned int nameoff
; /* If iterating in core kernel symbols */
228 char name
[KSYM_NAME_LEN
+1];
231 /* Only label it "global" if it is exported. */
232 static void upcase_if_global(struct kallsym_iter
*iter
)
234 if (is_exported(iter
->name
, iter
->owner
))
235 iter
->type
+= 'A' - 'a';
238 static int get_ksymbol_mod(struct kallsym_iter
*iter
)
240 iter
->owner
= module_get_kallsym(iter
->pos
- kallsyms_num_syms
,
242 &iter
->type
, iter
->name
);
243 if (iter
->owner
== NULL
)
246 upcase_if_global(iter
);
250 /* Returns space to next name. */
251 static unsigned long get_ksymbol_core(struct kallsym_iter
*iter
)
253 unsigned off
= iter
->nameoff
;
256 iter
->value
= kallsyms_addresses
[iter
->pos
];
258 iter
->type
= kallsyms_get_symbol_type(off
);
260 off
= kallsyms_expand_symbol(off
, iter
->name
);
262 return off
- iter
->nameoff
;
265 static void reset_iter(struct kallsym_iter
*iter
, loff_t new_pos
)
267 iter
->name
[0] = '\0';
268 iter
->nameoff
= get_symbol_offset(new_pos
);
272 /* Returns false if pos at or past end of file. */
273 static int update_iter(struct kallsym_iter
*iter
, loff_t pos
)
275 /* Module symbols can be accessed randomly. */
276 if (pos
>= kallsyms_num_syms
) {
278 return get_ksymbol_mod(iter
);
281 /* If we're not on the desired position, reset to new position. */
282 if (pos
!= iter
->pos
)
283 reset_iter(iter
, pos
);
285 iter
->nameoff
+= get_ksymbol_core(iter
);
291 static void *s_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
295 if (!update_iter(m
->private, *pos
))
300 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
302 if (!update_iter(m
->private, *pos
))
307 static void s_stop(struct seq_file
*m
, void *p
)
311 static int s_show(struct seq_file
*m
, void *p
)
313 struct kallsym_iter
*iter
= m
->private;
315 /* Some debugging symbols have no name. Ignore them. */
320 seq_printf(m
, "%0*lx %c %s\t[%s]\n",
321 (int)(2*sizeof(void*)),
322 iter
->value
, iter
->type
, iter
->name
,
323 module_name(iter
->owner
));
325 seq_printf(m
, "%0*lx %c %s\n",
326 (int)(2*sizeof(void*)),
327 iter
->value
, iter
->type
, iter
->name
);
331 struct seq_operations kallsyms_op
= {
338 static int kallsyms_open(struct inode
*inode
, struct file
*file
)
340 /* We keep iterator in m->private, since normal case is to
341 * s_start from where we left off, so we avoid doing
342 * using get_symbol_offset for every symbol */
343 struct kallsym_iter
*iter
;
346 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
351 ret
= seq_open(file
, &kallsyms_op
);
353 ((struct seq_file
*)file
->private_data
)->private = iter
;
359 static int kallsyms_release(struct inode
*inode
, struct file
*file
)
361 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
363 return seq_release(inode
, file
);
366 static struct file_operations kallsyms_operations
= {
367 .open
= kallsyms_open
,
370 .release
= kallsyms_release
,
373 int __init
kallsyms_init(void)
375 struct proc_dir_entry
*entry
;
377 entry
= create_proc_entry("kallsyms", 0444, NULL
);
379 entry
->proc_fops
= &kallsyms_operations
;
382 __initcall(kallsyms_init
);
384 EXPORT_SYMBOL(__print_symbol
);