1 /*
2  * symbols.c: in-kernel printing of symbolic oopses and stack traces.
3  *
4  * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
5  *
6  * ChangeLog:
7  *
8  * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com>
9  *      Changed the compression method from stem compression to "table lookup"
10  *      compression (see tools/symbols.c for a more complete description)
11  */
12 
13 #include <xen/symbols.h>
14 #include <xen/kernel.h>
15 #include <xen/init.h>
16 #include <xen/lib.h>
17 #include <xen/string.h>
18 #include <xen/spinlock.h>
19 #include <xen/virtual_region.h>
20 #include <public/platform.h>
21 #include <xen/guest_access.h>
22 #include <xen/errno.h>
23 
24 #ifdef SYMBOLS_ORIGIN
25 extern const unsigned int symbols_offsets[];
26 #define symbols_address(n) (SYMBOLS_ORIGIN + symbols_offsets[n])
27 #else
28 extern const unsigned long symbols_addresses[];
29 #define symbols_address(n) symbols_addresses[n]
30 #endif
31 extern const unsigned int symbols_num_syms;
32 extern const u8 symbols_names[];
33 
34 extern const struct symbol_offset symbols_sorted_offsets[];
35 
36 extern const u8 symbols_token_table[];
37 extern const u16 symbols_token_index[];
38 
39 extern const unsigned int symbols_markers[];
40 
41 /* expand a compressed symbol data into the resulting uncompressed string,
42    given the offset to where the symbol is in the compressed stream */
symbols_expand_symbol(unsigned int off,char * result)43 static unsigned int symbols_expand_symbol(unsigned int off, char *result)
44 {
45     int len, skipped_first = 0;
46     const u8 *tptr, *data;
47 
48     /* get the compressed symbol length from the first symbol byte */
49     data = &symbols_names[off];
50     len = *data;
51     data++;
52 
53     /* update the offset to return the offset for the next symbol on
54      * the compressed stream */
55     off += len + 1;
56 
57     /* for every byte on the compressed symbol data, copy the table
58        entry for that byte */
59     while(len) {
60         tptr = &symbols_token_table[ symbols_token_index[*data] ];
61         data++;
62         len--;
63 
64         while (*tptr) {
65             if(skipped_first) {
66                 *result = *tptr;
67                 result++;
68             } else
69                 skipped_first = 1;
70             tptr++;
71         }
72     }
73 
74     *result = '\0';
75 
76     /* return to offset to the next symbol */
77     return off;
78 }
79 
80 /* find the offset on the compressed stream given and index in the
81  * symbols array */
get_symbol_offset(unsigned long pos)82 static unsigned int get_symbol_offset(unsigned long pos)
83 {
84     const u8 *name;
85     int i;
86 
87     /* use the closest marker we have. We have markers every 256 positions,
88      * so that should be close enough */
89     name = &symbols_names[ symbols_markers[pos>>8] ];
90 
91     /* sequentially scan all the symbols up to the point we're searching for.
92      * Every symbol is stored in a [<len>][<len> bytes of data] format, so we
93      * just need to add the len to the current pointer for every symbol we
94      * wish to skip */
95     for(i = 0; i < (pos&0xFF); i++)
96         name = name + (*name) + 1;
97 
98     return name - symbols_names;
99 }
100 
is_active_kernel_text(unsigned long addr)101 bool is_active_kernel_text(unsigned long addr)
102 {
103     return !!find_text_region(addr);
104 }
105 
symbols_lookup(unsigned long addr,unsigned long * symbolsize,unsigned long * offset,char * namebuf)106 const char *symbols_lookup(unsigned long addr,
107                            unsigned long *symbolsize,
108                            unsigned long *offset,
109                            char *namebuf)
110 {
111     unsigned int i, low, high, mid;
112     unsigned long symbol_end = 0;
113     const struct virtual_region *region;
114 
115     namebuf[KSYM_NAME_LEN] = 0;
116     namebuf[0] = 0;
117 
118     region = find_text_region(addr);
119     if (!region)
120         return NULL;
121 
122     if (region->symbols_lookup)
123         return region->symbols_lookup(addr, symbolsize, offset, namebuf);
124 
125         /* do a binary search on the sorted symbols_addresses array */
126     low = 0;
127     high = symbols_num_syms;
128 
129     while (high-low > 1) {
130         mid = (low + high) / 2;
131         if (symbols_address(mid) <= addr) low = mid;
132         else high = mid;
133     }
134 
135     /* search for the first aliased symbol. Aliased symbols are
136            symbols with the same address */
137     while (low && symbols_address(low - 1) == symbols_address(low))
138         --low;
139 
140         /* Grab name */
141     symbols_expand_symbol(get_symbol_offset(low), namebuf);
142 
143     /* Search for next non-aliased symbol */
144     for (i = low + 1; i < symbols_num_syms; i++) {
145         if (symbols_address(i) > symbols_address(low)) {
146             symbol_end = symbols_address(i);
147             break;
148         }
149     }
150 
151     /* if we found no next symbol, we use the end of the section */
152     if (!symbol_end)
153         symbol_end = is_kernel_inittext(addr) ?
154             (unsigned long)_einittext : (unsigned long)_etext;
155 
156     *symbolsize = symbol_end - symbols_address(low);
157     *offset = addr - symbols_address(low);
158     return namebuf;
159 }
160 
161 /*
162  * Get symbol type information. This is encoded as a single char at the
163  * beginning of the symbol name.
164  */
symbols_get_symbol_type(unsigned int off)165 static char symbols_get_symbol_type(unsigned int off)
166 {
167     /*
168      * Get just the first code, look it up in the token table,
169      * and return the first char from this token.
170      */
171     return symbols_token_table[symbols_token_index[symbols_names[off + 1]]];
172 }
173 
xensyms_read(uint32_t * symnum,char * type,unsigned long * address,char * name)174 int xensyms_read(uint32_t *symnum, char *type,
175                  unsigned long *address, char *name)
176 {
177     /*
178      * Symbols are most likely accessed sequentially so we remember position
179      * from previous read. This can help us avoid the extra call to
180      * get_symbol_offset().
181      */
182     static unsigned int next_symbol, next_offset;
183     static DEFINE_SPINLOCK(symbols_mutex);
184 
185     if ( *symnum > symbols_num_syms )
186         return -ERANGE;
187     if ( *symnum == symbols_num_syms )
188     {
189         /* No more symbols */
190         name[0] = '\0';
191         return 0;
192     }
193 
194     spin_lock(&symbols_mutex);
195 
196     if ( *symnum == 0 )
197         next_offset = next_symbol = 0;
198     if ( next_symbol != *symnum )
199         /* Non-sequential access */
200         next_offset = get_symbol_offset(*symnum);
201 
202     *type = symbols_get_symbol_type(next_offset);
203     next_offset = symbols_expand_symbol(next_offset, name);
204     *address = symbols_address(*symnum);
205 
206     next_symbol = ++*symnum;
207 
208     spin_unlock(&symbols_mutex);
209 
210     return 0;
211 }
212 
symbols_lookup_by_name(const char * symname)213 unsigned long symbols_lookup_by_name(const char *symname)
214 {
215     char name[KSYM_NAME_LEN + 1];
216 #ifdef CONFIG_FAST_SYMBOL_LOOKUP
217     unsigned long low, high;
218 #else
219     uint32_t symnum = 0;
220     char type;
221     unsigned long addr;
222     int rc;
223 #endif
224 
225     if ( *symname == '\0' )
226         return 0;
227 
228 #ifdef CONFIG_FAST_SYMBOL_LOOKUP
229     low = 0;
230     high = symbols_num_syms;
231     while ( low < high )
232     {
233         unsigned long mid = low + ((high - low) / 2);
234         const struct symbol_offset *s;
235         int rc;
236 
237         s = &symbols_sorted_offsets[mid];
238         (void)symbols_expand_symbol(s->stream, name);
239         /* Format is: [filename]#<symbol>. symbols_expand_symbol eats type.*/
240         rc = strcmp(symname, name);
241         if ( rc < 0 )
242             high = mid;
243         else if ( rc > 0 )
244             low = mid + 1;
245         else
246             return symbols_address(s->addr);
247     }
248 #else
249     do {
250         rc = xensyms_read(&symnum, &type, &addr, name);
251         if ( rc )
252            break;
253 
254         if ( !strcmp(name, symname) )
255             return addr;
256 
257     } while ( name[0] != '\0' );
258 
259 #endif
260     return 0;
261 }
262 
263 /*
264  * Local variables:
265  * mode: C
266  * c-file-style: "BSD"
267  * c-basic-offset: 4
268  * tab-width: 4
269  * indent-tabs-mode: nil
270  * End:
271  */
272