upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / kernel / kallsyms.c
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>
21
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));
26
27 extern u8 kallsyms_token_table[] __attribute__((weak));
28 extern u16 kallsyms_token_index[] __attribute__((weak));
29
30 extern unsigned long kallsyms_markers[] __attribute__((weak));
31
32 /* Defined by the linker script. */
33 extern char _stext[], _etext[], _sinittext[], _einittext[];
34
35 static inline int is_kernel_inittext(unsigned long addr)
36 {
37         if (addr >= (unsigned long)_sinittext
38             && addr <= (unsigned long)_einittext)
39                 return 1;
40         return 0;
41 }
42
43 static inline int is_kernel_text(unsigned long addr)
44 {
45         if (addr >= (unsigned long)_stext && addr <= (unsigned long)_etext)
46                 return 1;
47         return 0;
48 }
49
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)
53 {
54         int len, skipped_first = 0;
55         u8 *tptr, *data;
56
57         /* get the compressed symbol length from the first symbol byte */
58         data = &kallsyms_names[off];
59         len = *data;
60         data++;
61
62         /* update the offset to return the offset for the next symbol on
63          * the compressed stream */
64         off += len + 1;
65
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--;
72
73                 while (*tptr) {
74                         if(skipped_first) {
75                                 *result = *tptr;
76                                 result++;
77                         } else
78                                 skipped_first = 1;
79                         tptr++;
80                 }
81         }
82
83         *result = '\0';
84
85         /* return to offset to the next symbol */
86         return off;
87 }
88
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)
92 {
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] ] ];
96 }
97
98
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)
102 {
103         u8 *name;
104         int i;
105
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] ];
109
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;
116
117         return name - kallsyms_names;
118 }
119
120 /* Lookup the address for this symbol. Returns 0 if not found. */
121 unsigned long kallsyms_lookup_name(const char *name)
122 {
123         char namebuf[KSYM_NAME_LEN+1];
124         unsigned long i;
125         unsigned int off;
126
127         for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
128                 off = kallsyms_expand_symbol(off, namebuf);
129
130                 if (strcmp(namebuf, name) == 0)
131                         return kallsyms_addresses[i];
132         }
133         return module_kallsyms_lookup_name(name);
134 }
135
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)
141 {
142         unsigned long i, low, high, mid;
143
144         /* This kernel should never had been booted. */
145         BUG_ON(!kallsyms_addresses);
146
147         namebuf[KSYM_NAME_LEN] = 0;
148         namebuf[0] = 0;
149
150         if (is_kernel_text(addr) || is_kernel_inittext(addr)) {
151                 unsigned long symbol_end=0;
152
153                 /* do a binary search on the sorted kallsyms_addresses array */
154                 low = 0;
155                 high = kallsyms_num_syms;
156
157                 while (high-low > 1) {
158                         mid = (low + high) / 2;
159                         if (kallsyms_addresses[mid] <= addr) low = mid;
160                         else high = mid;
161                 }
162
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;
167
168                 /* Grab name */
169                 kallsyms_expand_symbol(get_symbol_offset(low), namebuf);
170
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;
176                         }
177                 }
178
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;
185                 }
186
187                 *symbolsize = symbol_end - kallsyms_addresses[low];
188                 *modname = NULL;
189                 *offset = addr - kallsyms_addresses[low];
190                 return namebuf;
191         }
192
193         return module_address_lookup(addr, symbolsize, offset, modname);
194 }
195
196 /* Replace "%s" in format with address, or returns -errno. */
197 void __print_symbol(const char *fmt, unsigned long address)
198 {
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];
205
206         name = kallsyms_lookup(address, &size, &offset, &modname, namebuf);
207
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);
216         }
217         printk(fmt, buffer);
218 }
219
220 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
221 struct kallsym_iter
222 {
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];
229 };
230
231 /* Only label it "global" if it is exported. */
232 static void upcase_if_global(struct kallsym_iter *iter)
233 {
234         if (is_exported(iter->name, iter->owner))
235                 iter->type += 'A' - 'a';
236 }
237
238 static int get_ksymbol_mod(struct kallsym_iter *iter)
239 {
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;
245
246         upcase_if_global(iter);
247         return 1;
248 }
249
250 /* Returns space to next name. */
251 static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
252 {
253         unsigned off = iter->nameoff;
254
255         iter->owner = NULL;
256         iter->value = kallsyms_addresses[iter->pos];
257
258         iter->type = kallsyms_get_symbol_type(off);
259
260         off = kallsyms_expand_symbol(off, iter->name);
261
262         return off - iter->nameoff;
263 }
264
265 static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
266 {
267         iter->name[0] = '\0';
268         iter->nameoff = get_symbol_offset(new_pos);
269         iter->pos = new_pos;
270 }
271
272 /* Returns false if pos at or past end of file. */
273 static int update_iter(struct kallsym_iter *iter, loff_t pos)
274 {
275         /* Module symbols can be accessed randomly. */
276         if (pos >= kallsyms_num_syms) {
277                 iter->pos = pos;
278                 return get_ksymbol_mod(iter);
279         }
280         
281         /* If we're not on the desired position, reset to new position. */
282         if (pos != iter->pos)
283                 reset_iter(iter, pos);
284
285         iter->nameoff += get_ksymbol_core(iter);
286         iter->pos++;
287
288         return 1;
289 }
290
291 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
292 {
293         (*pos)++;
294
295         if (!update_iter(m->private, *pos))
296                 return NULL;
297         return p;
298 }
299
300 static void *s_start(struct seq_file *m, loff_t *pos)
301 {
302         if (!update_iter(m->private, *pos))
303                 return NULL;
304         return m->private;
305 }
306
307 static void s_stop(struct seq_file *m, void *p)
308 {
309 }
310
311 static int s_show(struct seq_file *m, void *p)
312 {
313         struct kallsym_iter *iter = m->private;
314
315         /* Some debugging symbols have no name.  Ignore them. */ 
316         if (!iter->name[0])
317                 return 0;
318
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;
329 }
330
331 struct seq_operations kallsyms_op = {
332         .start = s_start,
333         .next = s_next,
334         .stop = s_stop,
335         .show = s_show
336 };
337
338 static int kallsyms_open(struct inode *inode, struct file *file)
339 {
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;
345
346         iter = kmalloc(sizeof(*iter), GFP_KERNEL);
347         if (!iter)
348                 return -ENOMEM;
349         reset_iter(iter, 0);
350
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;
357 }
358
359 static int kallsyms_release(struct inode *inode, struct file *file)
360 {
361         struct seq_file *m = (struct seq_file *)file->private_data;
362         kfree(m->private);
363         return seq_release(inode, file);
364 }
365
366 static struct file_operations kallsyms_operations = {
367         .open = kallsyms_open,
368         .read = seq_read,
369         .llseek = seq_lseek,
370         .release = kallsyms_release,
371 };
372
373 int __init kallsyms_init(void)
374 {
375         struct proc_dir_entry *entry;
376
377         entry = create_proc_entry("kallsyms", 0444, NULL);
378         if (entry)
379                 entry->proc_fops = &kallsyms_operations;
380         return 0;
381 }
382 __initcall(kallsyms_init);
383
384 EXPORT_SYMBOL(__print_symbol);