[PATCH] DVB: misc. updates to the dvb-core
[linux-2.6/history.git] / kernel / kallsyms.c
blob8f3c6c1d1ce7a6df4a7feea07dd88e3f70b322e3
1 /*
2 * kallsyms.c: in-kernel printing of symbolic oopses and stack traces.
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
8 * ChangeLog:
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>
18 #include <linux/fs.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)
39 return 1;
40 return 0;
43 static inline int is_kernel_text(unsigned long addr)
45 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_etext)
46 return 1;
47 return 0;
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;
55 u8 *tptr, *data;
57 /* get the compressed symbol length from the first symbol byte */
58 data = &kallsyms_names[off];
59 len = *data;
60 data++;
62 /* update the offset to return the offset for the next symbol on
63 * the compressed stream */
64 off += len + 1;
66 /* for every byte on the compressed symbol data, copy the table
67 entry for that byte */
68 while(len) {
69 tptr = &kallsyms_token_table[ kallsyms_token_index[*data] ];
70 data++;
71 len--;
73 while (*tptr) {
74 if(skipped_first) {
75 *result = *tptr;
76 result++;
77 } else
78 skipped_first = 1;
79 tptr++;
83 *result = '\0';
85 /* return to offset to the next symbol */
86 return off;
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
100 * kallsyms array */
101 static unsigned int get_symbol_offset(unsigned long pos)
103 u8 *name;
104 int i;
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
113 * wish to skip */
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];
124 unsigned long i;
125 unsigned int off;
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;
148 namebuf[0] = 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 */
154 low = 0;
155 high = kallsyms_num_syms;
157 while (high-low > 1) {
158 mid = (low + high) / 2;
159 if (kallsyms_addresses[mid] <= addr) low = mid;
160 else high = 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])
166 --low;
168 /* Grab name */
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];
175 break;
179 /* if we found no next symbol, we use the end of the section */
180 if (!symbol_end) {
181 if (is_kernel_inittext(addr))
182 symbol_end = (unsigned long)_einittext;
183 else
184 symbol_end = (unsigned long)_etext;
187 *symbolsize = symbol_end - kallsyms_addresses[low];
188 *modname = NULL;
189 *offset = addr - kallsyms_addresses[low];
190 return namebuf;
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)
199 char *modname;
200 const char *name;
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);
208 if (!name)
209 sprintf(buffer, "0x%lx", address);
210 else {
211 if (modname)
212 sprintf(buffer, "%s+%#lx/%#lx [%s]", name, offset,
213 size, modname);
214 else
215 sprintf(buffer, "%s+%#lx/%#lx", name, offset, size);
217 printk(fmt, buffer);
220 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
221 struct kallsym_iter
223 loff_t pos;
224 struct module *owner;
225 unsigned long value;
226 unsigned int nameoff; /* If iterating in core kernel symbols */
227 char type;
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,
241 &iter->value,
242 &iter->type, iter->name);
243 if (iter->owner == NULL)
244 return 0;
246 upcase_if_global(iter);
247 return 1;
250 /* Returns space to next name. */
251 static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
253 unsigned off = iter->nameoff;
255 iter->owner = NULL;
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);
269 iter->pos = 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) {
277 iter->pos = pos;
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);
286 iter->pos++;
288 return 1;
291 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
293 (*pos)++;
295 if (!update_iter(m->private, *pos))
296 return NULL;
297 return p;
300 static void *s_start(struct seq_file *m, loff_t *pos)
302 if (!update_iter(m->private, *pos))
303 return NULL;
304 return m->private;
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. */
316 if (!iter->name[0])
317 return 0;
319 if (iter->owner)
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));
324 else
325 seq_printf(m, "%0*lx %c %s\n",
326 (int)(2*sizeof(void*)),
327 iter->value, iter->type, iter->name);
328 return 0;
331 struct seq_operations kallsyms_op = {
332 .start = s_start,
333 .next = s_next,
334 .stop = s_stop,
335 .show = s_show
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;
344 int ret;
346 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
347 if (!iter)
348 return -ENOMEM;
349 reset_iter(iter, 0);
351 ret = seq_open(file, &kallsyms_op);
352 if (ret == 0)
353 ((struct seq_file *)file->private_data)->private = iter;
354 else
355 kfree(iter);
356 return ret;
359 static int kallsyms_release(struct inode *inode, struct file *file)
361 struct seq_file *m = (struct seq_file *)file->private_data;
362 kfree(m->private);
363 return seq_release(inode, file);
366 static struct file_operations kallsyms_operations = {
367 .open = kallsyms_open,
368 .read = seq_read,
369 .llseek = seq_lseek,
370 .release = kallsyms_release,
373 int __init kallsyms_init(void)
375 struct proc_dir_entry *entry;
377 entry = create_proc_entry("kallsyms", 0444, NULL);
378 if (entry)
379 entry->proc_fops = &kallsyms_operations;
380 return 0;
382 __initcall(kallsyms_init);
384 EXPORT_SYMBOL(__print_symbol);