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