blob: faa84f5d4f54cb27076595d23a60f5ca4d7ff1a7 [file] [log] [blame]
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001#include "util.h"
2#include "../perf.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -03003#include "string.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03004#include "symbol.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03005#include "thread.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03006
Frederic Weisbecker8f288272009-08-16 22:05:48 +02007#include "debug.h"
8
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03009#include <libelf.h>
10#include <gelf.h>
11#include <elf.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030012#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020013
Ingo Molnar0b73da32009-06-06 15:48:52 +020014const char *sym_hist_filter;
15
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030016enum dso_origin {
17 DSO__ORIG_KERNEL = 0,
18 DSO__ORIG_JAVA_JIT,
19 DSO__ORIG_FEDORA,
20 DSO__ORIG_UBUNTU,
21 DSO__ORIG_BUILDID,
22 DSO__ORIG_DSO,
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030023 DSO__ORIG_KMODULE,
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030024 DSO__ORIG_NOT_FOUND,
25};
26
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030027static void dsos__add(struct dso *dso);
28static struct dso *dsos__find(const char *name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030029static struct map *map__new2(u64 start, struct dso *dso);
30static void kernel_maps__insert(struct map *map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030031
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030032static struct rb_root kernel_maps;
33
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030034static void dso__fixup_sym_end(struct dso *self)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030035{
36 struct rb_node *nd, *prevnd = rb_first(&self->syms);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030037 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030038
39 if (prevnd == NULL)
40 return;
41
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030042 curr = rb_entry(prevnd, struct symbol, rb_node);
43
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030044 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030045 prev = curr;
46 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030047
48 if (prev->end == prev->start)
49 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030050 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030051
52 /* Last entry */
53 if (curr->end == curr->start)
54 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030055}
56
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030057static void kernel_maps__fixup_end(void)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030058{
59 struct map *prev, *curr;
60 struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
61
62 if (prevnd == NULL)
63 return;
64
65 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030066
67 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
68 prev = curr;
69 curr = rb_entry(nd, struct map, rb_node);
70 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030071 }
72
73 nd = rb_last(&curr->dso->syms);
74 if (nd) {
75 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
76 curr->end = sym->end;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030077 }
78}
79
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030080static struct symbol *symbol__new(u64 start, u64 len, const char *name,
81 unsigned int priv_size, int v)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030082{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030083 size_t namelen = strlen(name) + 1;
Ingo Molnar0b73da32009-06-06 15:48:52 +020084 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030085
Ingo Molnar0b73da32009-06-06 15:48:52 +020086 if (!self)
87 return NULL;
88
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -030089 if (v > 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030090 printf("new symbol: %016Lx [%08lx]: %s, hist: %p\n",
91 start, (unsigned long)len, name, self->hist);
Ingo Molnar0b73da32009-06-06 15:48:52 +020092
Ingo Molnar0b73da32009-06-06 15:48:52 +020093 self->hist = NULL;
94 self->hist_sum = 0;
95
96 if (sym_hist_filter && !strcmp(name, sym_hist_filter))
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100097 self->hist = calloc(sizeof(u64), len);
Ingo Molnar0b73da32009-06-06 15:48:52 +020098
99 if (priv_size) {
100 memset(self, 0, priv_size);
101 self = ((void *)self) + priv_size;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300102 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200103 self->start = start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200104 self->end = len ? start + len - 1 : start;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200105 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300106
107 return self;
108}
109
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300110static void symbol__delete(struct symbol *self, unsigned int priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300111{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300112 free(((void *)self) - priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300113}
114
115static size_t symbol__fprintf(struct symbol *self, FILE *fp)
116{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300117 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300118 self->start, self->end, self->name);
119}
120
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300121struct dso *dso__new(const char *name, unsigned int sym_priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300122{
123 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
124
125 if (self != NULL) {
126 strcpy(self->name, name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300127 self->long_name = self->name;
128 self->short_name = self->name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300129 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300130 self->sym_priv_size = sym_priv_size;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200131 self->find_symbol = dso__find_symbol;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300132 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300133 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300134 }
135
136 return self;
137}
138
139static void dso__delete_symbols(struct dso *self)
140{
141 struct symbol *pos;
142 struct rb_node *next = rb_first(&self->syms);
143
144 while (next) {
145 pos = rb_entry(next, struct symbol, rb_node);
146 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloc8c96522009-06-01 17:50:57 -0300147 rb_erase(&pos->rb_node, &self->syms);
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300148 symbol__delete(pos, self->sym_priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300149 }
150}
151
152void dso__delete(struct dso *self)
153{
154 dso__delete_symbols(self);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300155 if (self->long_name != self->name)
156 free(self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300157 free(self);
158}
159
160static void dso__insert_symbol(struct dso *self, struct symbol *sym)
161{
162 struct rb_node **p = &self->syms.rb_node;
163 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000164 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300165 struct symbol *s;
166
167 while (*p != NULL) {
168 parent = *p;
169 s = rb_entry(parent, struct symbol, rb_node);
170 if (ip < s->start)
171 p = &(*p)->rb_left;
172 else
173 p = &(*p)->rb_right;
174 }
175 rb_link_node(&sym->rb_node, parent, p);
176 rb_insert_color(&sym->rb_node, &self->syms);
177}
178
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000179struct symbol *dso__find_symbol(struct dso *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300180{
181 struct rb_node *n;
182
183 if (self == NULL)
184 return NULL;
185
186 n = self->syms.rb_node;
187
188 while (n) {
189 struct symbol *s = rb_entry(n, struct symbol, rb_node);
190
191 if (ip < s->start)
192 n = n->rb_left;
193 else if (ip > s->end)
194 n = n->rb_right;
195 else
196 return s;
197 }
198
199 return NULL;
200}
201
202size_t dso__fprintf(struct dso *self, FILE *fp)
203{
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -0300204 size_t ret = fprintf(fp, "dso: %s\n", self->short_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300205
206 struct rb_node *nd;
207 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
208 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
209 ret += symbol__fprintf(pos, fp);
210 }
211
212 return ret;
213}
214
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300215/*
216 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
217 * so that we can in the next step set the symbol ->end address and then
218 * call kernel_maps__split_kallsyms.
219 */
220static int kernel_maps__load_all_kallsyms(int v)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300221{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300222 char *line = NULL;
223 size_t n;
224 FILE *file = fopen("/proc/kallsyms", "r");
225
226 if (file == NULL)
227 goto out_failure;
228
229 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000230 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300231 struct symbol *sym;
232 int line_len, len;
233 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300234 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300235
236 line_len = getline(&line, &n, file);
237 if (line_len < 0)
238 break;
239
240 if (!line)
241 goto out_failure;
242
243 line[--line_len] = '\0'; /* \n */
244
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300245 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300246
247 len++;
248 if (len + 2 >= line_len)
249 continue;
250
251 symbol_type = toupper(line[len]);
252 /*
253 * We're interested only in code ('T'ext)
254 */
255 if (symbol_type != 'T' && symbol_type != 'W')
256 continue;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300257
258 symbol_name = line + len + 2;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300259 /*
260 * Will fix up the end later, when we have all symbols sorted.
261 */
262 sym = symbol__new(start, 0, symbol_name,
263 kernel_map->dso->sym_priv_size, v);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300264
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300265 if (sym == NULL)
266 goto out_delete_line;
267
268 dso__insert_symbol(kernel_map->dso, sym);
269 }
270
271 free(line);
272 fclose(file);
273
274 return 0;
275
276out_delete_line:
277 free(line);
278out_failure:
279 return -1;
280}
281
282/*
283 * Split the symbols into maps, making sure there are no overlaps, i.e. the
284 * kernel range is broken in several maps, named [kernel].N, as we don't have
285 * the original ELF section names vmlinux have.
286 */
287static int kernel_maps__split_kallsyms(symbol_filter_t filter, int use_modules)
288{
289 struct map *map = kernel_map;
290 struct symbol *pos;
291 int count = 0;
292 struct rb_node *next = rb_first(&kernel_map->dso->syms);
293 int kernel_range = 0;
294
295 while (next) {
296 char *module;
297
298 pos = rb_entry(next, struct symbol, rb_node);
299 next = rb_next(&pos->rb_node);
300
301 module = strchr(pos->name, '\t');
302 if (module) {
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300303 if (!use_modules)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300304 goto delete_symbol;
305
306 *module++ = '\0';
307
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300308 if (strcmp(map->dso->name, module)) {
309 map = kernel_maps__find_by_dso_name(module);
310 if (!map) {
311 fputs("/proc/{kallsyms,modules} "
312 "inconsistency!\n", stderr);
313 return -1;
314 }
315 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300316 /*
317 * So that we look just like we get from .ko files,
318 * i.e. not prelinked, relative to map->start.
319 */
320 pos->start = map->map_ip(map, pos->start);
321 pos->end = map->map_ip(map, pos->end);
322 } else if (map != kernel_map) {
323 char dso_name[PATH_MAX];
324 struct dso *dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300325
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300326 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
327 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300328
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300329 dso = dso__new(dso_name,
330 kernel_map->dso->sym_priv_size);
331 if (dso == NULL)
332 return -1;
333
334 map = map__new2(pos->start, dso);
335 if (map == NULL) {
336 dso__delete(dso);
337 return -1;
338 }
339
340 map->map_ip = vdso__map_ip;
341 kernel_maps__insert(map);
342 ++kernel_range;
343 }
344
345 if (filter && filter(map, pos)) {
346delete_symbol:
347 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
348 symbol__delete(pos, kernel_map->dso->sym_priv_size);
349 } else {
350 if (map != kernel_map) {
351 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
352 dso__insert_symbol(map->dso, pos);
353 }
Mike Galbraith9974f492009-07-02 08:05:58 +0200354 count++;
355 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300356 }
357
Mike Galbraith9974f492009-07-02 08:05:58 +0200358 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300359}
360
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300361
362static int kernel_maps__load_kallsyms(symbol_filter_t filter,
363 int use_modules, int v)
364{
365 if (kernel_maps__load_all_kallsyms(v))
366 return -1;
367
368 dso__fixup_sym_end(kernel_map->dso);
369
370 return kernel_maps__split_kallsyms(filter, use_modules);
371}
372
373static size_t kernel_maps__fprintf(FILE *fp, int v)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300374{
375 size_t printed = fprintf(stderr, "Kernel maps:\n");
376 struct rb_node *nd;
377
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300378 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
379 struct map *pos = rb_entry(nd, struct map, rb_node);
380
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300381 printed += fprintf(fp, "Map:");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300382 printed += map__fprintf(pos, fp);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300383 if (v > 1) {
384 printed += dso__fprintf(pos->dso, fp);
385 printed += fprintf(fp, "--\n");
386 }
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300387 }
388
389 return printed + fprintf(stderr, "END kernel maps\n");
390}
391
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300392static int dso__load_perf_map(struct dso *self, struct map *map,
393 symbol_filter_t filter, int v)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300394{
395 char *line = NULL;
396 size_t n;
397 FILE *file;
398 int nr_syms = 0;
399
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300400 file = fopen(self->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300401 if (file == NULL)
402 goto out_failure;
403
404 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000405 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300406 struct symbol *sym;
407 int line_len, len;
408
409 line_len = getline(&line, &n, file);
410 if (line_len < 0)
411 break;
412
413 if (!line)
414 goto out_failure;
415
416 line[--line_len] = '\0'; /* \n */
417
418 len = hex2u64(line, &start);
419
420 len++;
421 if (len + 2 >= line_len)
422 continue;
423
424 len += hex2u64(line + len, &size);
425
426 len++;
427 if (len + 2 >= line_len)
428 continue;
429
430 sym = symbol__new(start, size, line + len,
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300431 self->sym_priv_size, v);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300432
433 if (sym == NULL)
434 goto out_delete_line;
435
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300436 if (filter && filter(map, sym))
Pekka Enberg80d496b2009-06-08 21:12:48 +0300437 symbol__delete(sym, self->sym_priv_size);
438 else {
439 dso__insert_symbol(self, sym);
440 nr_syms++;
441 }
442 }
443
444 free(line);
445 fclose(file);
446
447 return nr_syms;
448
449out_delete_line:
450 free(line);
451out_failure:
452 return -1;
453}
454
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300455/**
456 * elf_symtab__for_each_symbol - iterate thru all the symbols
457 *
458 * @self: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200459 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300460 * @sym: GElf_Sym iterator
461 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200462#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
463 for (idx = 0, gelf_getsym(syms, idx, &sym);\
464 idx < nr_syms; \
465 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300466
467static inline uint8_t elf_sym__type(const GElf_Sym *sym)
468{
469 return GELF_ST_TYPE(sym->st_info);
470}
471
472static inline int elf_sym__is_function(const GElf_Sym *sym)
473{
474 return elf_sym__type(sym) == STT_FUNC &&
475 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300476 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300477}
478
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200479static inline int elf_sym__is_label(const GElf_Sym *sym)
480{
481 return elf_sym__type(sym) == STT_NOTYPE &&
482 sym->st_name != 0 &&
483 sym->st_shndx != SHN_UNDEF &&
484 sym->st_shndx != SHN_ABS;
485}
486
487static inline const char *elf_sec__name(const GElf_Shdr *shdr,
488 const Elf_Data *secstrs)
489{
490 return secstrs->d_buf + shdr->sh_name;
491}
492
493static inline int elf_sec__is_text(const GElf_Shdr *shdr,
494 const Elf_Data *secstrs)
495{
496 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
497}
498
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300499static inline const char *elf_sym__name(const GElf_Sym *sym,
500 const Elf_Data *symstrs)
501{
502 return symstrs->d_buf + sym->st_name;
503}
504
505static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
506 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200507 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300508{
509 Elf_Scn *sec = NULL;
510 size_t cnt = 1;
511
512 while ((sec = elf_nextscn(elf, sec)) != NULL) {
513 char *str;
514
515 gelf_getshdr(sec, shp);
516 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
517 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200518 if (idx)
519 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300520 break;
521 }
522 ++cnt;
523 }
524
525 return sec;
526}
527
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300528#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
529 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
530 idx < nr_entries; \
531 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
532
533#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
534 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
535 idx < nr_entries; \
536 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
537
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300538/*
539 * We need to check if we have a .dynsym, so that we can handle the
540 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
541 * .dynsym or .symtab).
542 * And always look at the original dso, not at debuginfo packages, that
543 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
544 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200545static int dso__synthesize_plt_symbols(struct dso *self, int v)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300546{
547 uint32_t nr_rel_entries, idx;
548 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000549 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300550 GElf_Shdr shdr_plt;
551 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300552 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300553 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300554 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
555 size_t dynsym_idx;
556 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300557 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300558 Elf *elf;
559 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300560
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300561 fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300562 if (fd < 0)
563 goto out;
564
565 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
566 if (elf == NULL)
567 goto out_close;
568
569 if (gelf_getehdr(elf, &ehdr) == NULL)
570 goto out_elf_end;
571
572 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
573 ".dynsym", &dynsym_idx);
574 if (scn_dynsym == NULL)
575 goto out_elf_end;
576
577 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300578 ".rela.plt", NULL);
579 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300580 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300581 ".rel.plt", NULL);
582 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300583 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300584 }
585
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300586 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300587
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300588 if (shdr_rel_plt.sh_link != dynsym_idx)
589 goto out_elf_end;
590
591 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
592 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300593
594 /*
Ingo Molnar83a09442009-08-15 12:26:57 +0200595 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300596 * and the symbols in the .dynsym they refer to.
597 */
598 reldata = elf_getdata(scn_plt_rel, NULL);
599 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300600 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300601
602 syms = elf_getdata(scn_dynsym, NULL);
603 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300604 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300605
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300606 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300607 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300608 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300609
610 symstrs = elf_getdata(scn_symstrs, NULL);
611 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300612 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300613
614 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
615 plt_offset = shdr_plt.sh_offset;
616
617 if (shdr_rel_plt.sh_type == SHT_RELA) {
618 GElf_Rela pos_mem, *pos;
619
620 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
621 nr_rel_entries) {
622 symidx = GELF_R_SYM(pos->r_info);
623 plt_offset += shdr_plt.sh_entsize;
624 gelf_getsym(syms, symidx, &sym);
625 snprintf(sympltname, sizeof(sympltname),
626 "%s@plt", elf_sym__name(&sym, symstrs));
627
628 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300629 sympltname, self->sym_priv_size, v);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300630 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300631 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300632
633 dso__insert_symbol(self, f);
634 ++nr;
635 }
636 } else if (shdr_rel_plt.sh_type == SHT_REL) {
637 GElf_Rel pos_mem, *pos;
638 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
639 nr_rel_entries) {
640 symidx = GELF_R_SYM(pos->r_info);
641 plt_offset += shdr_plt.sh_entsize;
642 gelf_getsym(syms, symidx, &sym);
643 snprintf(sympltname, sizeof(sympltname),
644 "%s@plt", elf_sym__name(&sym, symstrs));
645
646 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300647 sympltname, self->sym_priv_size, v);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300648 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300649 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300650
651 dso__insert_symbol(self, f);
652 ++nr;
653 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300654 }
655
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300656 err = 0;
657out_elf_end:
658 elf_end(elf);
659out_close:
660 close(fd);
661
662 if (err == 0)
663 return nr;
664out:
665 fprintf(stderr, "%s: problems reading %s PLT info.\n",
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300666 __func__, self->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300667 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300668}
669
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300670static int dso__load_sym(struct dso *self, struct map *map, const char *name,
671 int fd, symbol_filter_t filter, int kernel,
672 int kmodule, int v)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300673{
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300674 struct map *curr_map = map;
675 struct dso *curr_dso = self;
676 size_t dso_name_len = strlen(self->short_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200677 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300678 uint32_t nr_syms;
679 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +0200680 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300681 GElf_Ehdr ehdr;
682 GElf_Shdr shdr;
683 Elf_Data *syms;
684 GElf_Sym sym;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300685 Elf_Scn *sec, *sec_strndx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300686 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300687 int nr = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300688
689 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
690 if (elf == NULL) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200691 if (v)
Ingo Molnarbd741372009-06-04 14:13:04 +0200692 fprintf(stderr, "%s: cannot read %s ELF file.\n",
693 __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300694 goto out_close;
695 }
696
697 if (gelf_getehdr(elf, &ehdr) == NULL) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200698 if (v)
Ingo Molnarbd741372009-06-04 14:13:04 +0200699 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300700 goto out_elf_end;
701 }
702
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300703 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
704 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300705 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
706 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300707 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300708 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300709
710 syms = elf_getdata(sec, NULL);
711 if (syms == NULL)
712 goto out_elf_end;
713
714 sec = elf_getscn(elf, shdr.sh_link);
715 if (sec == NULL)
716 goto out_elf_end;
717
718 symstrs = elf_getdata(sec, NULL);
719 if (symstrs == NULL)
720 goto out_elf_end;
721
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200722 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
723 if (sec_strndx == NULL)
724 goto out_elf_end;
725
726 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -0500727 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200728 goto out_elf_end;
729
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300730 nr_syms = shdr.sh_size / shdr.sh_entsize;
731
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +0200732 memset(&sym, 0, sizeof(sym));
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200733 if (!kernel) {
734 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300735 elf_section_by_name(elf, &ehdr, &shdr,
736 ".gnu.prelink_undo",
737 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200738 } else self->adjust_symbols = 0;
739
Ingo Molnar83a09442009-08-15 12:26:57 +0200740 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300741 struct symbol *f;
Ingo Molnar83a09442009-08-15 12:26:57 +0200742 const char *elf_name;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300743 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200744 int is_label = elf_sym__is_label(&sym);
745 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300746
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200747 if (!is_label && !elf_sym__is_function(&sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300748 continue;
749
750 sec = elf_getscn(elf, sym.st_shndx);
751 if (!sec)
752 goto out_elf_end;
753
754 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200755
756 if (is_label && !elf_sec__is_text(&shdr, secstrs))
757 continue;
758
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300759 elf_name = elf_sym__name(&sym, symstrs);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200760 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200761
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300762 if (kernel || kmodule) {
763 char dso_name[PATH_MAX];
764
765 if (strcmp(section_name,
766 curr_dso->short_name + dso_name_len) == 0)
767 goto new_symbol;
768
769 if (strcmp(section_name, ".text") == 0) {
770 curr_map = map;
771 curr_dso = self;
772 goto new_symbol;
773 }
774
775 snprintf(dso_name, sizeof(dso_name),
776 "%s%s", self->short_name, section_name);
777
778 curr_map = kernel_maps__find_by_dso_name(dso_name);
779 if (curr_map == NULL) {
780 u64 start = sym.st_value;
781
782 if (kmodule)
783 start += map->start + shdr.sh_offset;
784
785 curr_dso = dso__new(dso_name, self->sym_priv_size);
786 if (curr_dso == NULL)
787 goto out_elf_end;
788 curr_map = map__new2(start, curr_dso);
789 if (curr_map == NULL) {
790 dso__delete(curr_dso);
791 goto out_elf_end;
792 }
793 curr_map->map_ip = vdso__map_ip;
794 curr_dso->origin = DSO__ORIG_KERNEL;
795 kernel_maps__insert(curr_map);
796 dsos__add(curr_dso);
797 } else
798 curr_dso = curr_map->dso;
799
800 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300801 }
802
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300803 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300804 if (v > 2)
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300805 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
806 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200807
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300808 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300809 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300810 /*
811 * We need to figure out if the object was created from C++ sources
812 * DWARF DW_compile_unit has this, but we don't always have access
813 * to it...
814 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200815 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300816 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +0200817 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300818new_symbol:
819 f = symbol__new(sym.st_value, sym.st_size, elf_name,
820 curr_dso->sym_priv_size, v);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300821 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300822 if (!f)
823 goto out_elf_end;
824
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300825 if (filter && filter(curr_map, f))
826 symbol__delete(f, curr_dso->sym_priv_size);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300827 else {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300828 dso__insert_symbol(curr_dso, f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300829 nr++;
830 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300831 }
832
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300833 /*
834 * For misannotated, zeroed, ASM function sizes.
835 */
836 if (nr > 0)
837 dso__fixup_sym_end(self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300838 err = nr;
839out_elf_end:
840 elf_end(elf);
841out_close:
842 return err;
843}
844
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300845#define BUILD_ID_SIZE 128
846
Ingo Molnar83a09442009-08-15 12:26:57 +0200847static char *dso__read_build_id(struct dso *self, int v)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300848{
849 int i;
850 GElf_Ehdr ehdr;
851 GElf_Shdr shdr;
852 Elf_Data *build_id_data;
853 Elf_Scn *sec;
854 char *build_id = NULL, *bid;
855 unsigned char *raw;
856 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300857 int fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300858
859 if (fd < 0)
860 goto out;
861
862 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
863 if (elf == NULL) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200864 if (v)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300865 fprintf(stderr, "%s: cannot read %s ELF file.\n",
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300866 __func__, self->long_name);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300867 goto out_close;
868 }
869
870 if (gelf_getehdr(elf, &ehdr) == NULL) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200871 if (v)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300872 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
873 goto out_elf_end;
874 }
875
876 sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
877 if (sec == NULL)
878 goto out_elf_end;
879
880 build_id_data = elf_getdata(sec, NULL);
881 if (build_id_data == NULL)
882 goto out_elf_end;
883 build_id = malloc(BUILD_ID_SIZE);
884 if (build_id == NULL)
885 goto out_elf_end;
886 raw = build_id_data->d_buf + 16;
887 bid = build_id;
888
889 for (i = 0; i < 20; ++i) {
890 sprintf(bid, "%02x", *raw);
891 ++raw;
892 bid += 2;
893 }
Ingo Molnar83a09442009-08-15 12:26:57 +0200894 if (v >= 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300895 printf("%s(%s): %s\n", __func__, self->long_name, build_id);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300896out_elf_end:
897 elf_end(elf);
898out_close:
899 close(fd);
900out:
901 return build_id;
902}
903
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300904char dso__symtab_origin(const struct dso *self)
905{
906 static const char origin[] = {
907 [DSO__ORIG_KERNEL] = 'k',
908 [DSO__ORIG_JAVA_JIT] = 'j',
909 [DSO__ORIG_FEDORA] = 'f',
910 [DSO__ORIG_UBUNTU] = 'u',
911 [DSO__ORIG_BUILDID] = 'b',
912 [DSO__ORIG_DSO] = 'd',
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300913 [DSO__ORIG_KMODULE] = 'K',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300914 };
915
916 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
917 return '!';
918 return origin[self->origin];
919}
920
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300921int dso__load(struct dso *self, struct map *map, symbol_filter_t filter, int v)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300922{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300923 int size = PATH_MAX;
924 char *name = malloc(size), *build_id = NULL;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300925 int ret = -1;
926 int fd;
927
928 if (!name)
929 return -1;
930
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300931 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300932
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300933 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300934 ret = dso__load_perf_map(self, map, filter, v);
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300935 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
936 DSO__ORIG_NOT_FOUND;
937 return ret;
938 }
939
940 self->origin = DSO__ORIG_FEDORA - 1;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300941
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300942more:
943 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300944 self->origin++;
945 switch (self->origin) {
946 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300947 snprintf(name, size, "/usr/lib/debug%s.debug",
948 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300949 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300950 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300951 snprintf(name, size, "/usr/lib/debug%s",
952 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300953 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300954 case DSO__ORIG_BUILDID:
Ingo Molnar83a09442009-08-15 12:26:57 +0200955 build_id = dso__read_build_id(self, v);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300956 if (build_id != NULL) {
957 snprintf(name, size,
958 "/usr/lib/debug/.build-id/%.2s/%s.debug",
959 build_id, build_id + 2);
960 free(build_id);
961 break;
962 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300963 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300964 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300965 case DSO__ORIG_DSO:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300966 snprintf(name, size, "%s", self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300967 break;
968
969 default:
970 goto out;
971 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300972
973 fd = open(name, O_RDONLY);
974 } while (fd < 0);
975
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300976 ret = dso__load_sym(self, map, name, fd, filter, 0, 0, v);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300977 close(fd);
978
979 /*
980 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
981 */
982 if (!ret)
983 goto more;
984
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300985 if (ret > 0) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200986 int nr_plt = dso__synthesize_plt_symbols(self, v);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300987 if (nr_plt > 0)
988 ret += nr_plt;
989 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300990out:
991 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -0300992 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
993 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300994 return ret;
995}
996
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300997struct map *kernel_map;
998
999static void kernel_maps__insert(struct map *map)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001000{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001001 maps__insert(&kernel_maps, map);
1002}
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001003
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001004struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
1005{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001006 struct map *map = maps__find(&kernel_maps, ip);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001007
1008 if (mapp)
1009 *mapp = map;
1010
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001011 if (map) {
1012 ip = map->map_ip(map, ip);
1013 return map->dso->find_symbol(map->dso, ip);
1014 }
1015
1016 return NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001017}
1018
1019struct map *kernel_maps__find_by_dso_name(const char *name)
1020{
1021 struct rb_node *nd;
1022
1023 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1024 struct map *map = rb_entry(nd, struct map, rb_node);
1025
1026 if (map->dso && strcmp(map->dso->name, name) == 0)
1027 return map;
1028 }
1029
1030 return NULL;
1031}
1032
1033static int dso__load_module_sym(struct dso *self, struct map *map,
1034 symbol_filter_t filter, int v)
1035{
1036 int err = 0, fd = open(self->long_name, O_RDONLY);
1037
1038 if (fd < 0) {
1039 if (v)
1040 fprintf(stderr, "%s: cannot open %s\n",
1041 __func__, self->long_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001042 return err;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001043 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001044
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001045 err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1, v);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001046 close(fd);
1047
1048 return err;
1049}
1050
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001051static int dsos__load_modules_sym_dir(char *dirname,
1052 symbol_filter_t filter, int v)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001053{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001054 struct dirent *dent;
1055 int nr_symbols = 0, err;
1056 DIR *dir = opendir(dirname);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001057
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001058 if (!dir) {
1059 if (v)
1060 fprintf(stderr, "%s: cannot open %s dir\n", __func__,
1061 dirname);
1062 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001063 }
1064
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001065 while ((dent = readdir(dir)) != NULL) {
1066 char path[PATH_MAX];
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001067
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001068 if (dent->d_type == DT_DIR) {
1069 if (!strcmp(dent->d_name, ".") ||
1070 !strcmp(dent->d_name, ".."))
1071 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001072
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001073 snprintf(path, sizeof(path), "%s/%s",
1074 dirname, dent->d_name);
1075 err = dsos__load_modules_sym_dir(path, filter, v);
1076 if (err < 0)
1077 goto failure;
1078 } else {
1079 char *dot = strrchr(dent->d_name, '.'),
1080 dso_name[PATH_MAX];
1081 struct map *map;
1082 struct rb_node *last;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001083
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001084 if (dot == NULL || strcmp(dot, ".ko"))
1085 continue;
1086 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1087 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001088
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001089 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001090 map = kernel_maps__find_by_dso_name(dso_name);
1091 if (map == NULL)
1092 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001093
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001094 snprintf(path, sizeof(path), "%s/%s",
1095 dirname, dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001096
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001097 map->dso->long_name = strdup(path);
1098 if (map->dso->long_name == NULL)
1099 goto failure;
1100
1101 err = dso__load_module_sym(map->dso, map, filter, v);
1102 if (err < 0)
1103 goto failure;
1104 last = rb_last(&map->dso->syms);
1105 if (last) {
1106 struct symbol *sym;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001107 /*
1108 * We do this here as well, even having the
1109 * symbol size found in the symtab because
1110 * misannotated ASM symbols may have the size
1111 * set to zero.
1112 */
1113 dso__fixup_sym_end(map->dso);
1114
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001115 sym = rb_entry(last, struct symbol, rb_node);
1116 map->end = map->start + sym->end;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001117 }
1118 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001119 nr_symbols += err;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001120 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001121
1122 return nr_symbols;
1123failure:
1124 closedir(dir);
1125 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001126}
1127
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001128static int dsos__load_modules_sym(symbol_filter_t filter, int v)
1129{
1130 struct utsname uts;
1131 char modules_path[PATH_MAX];
1132
1133 if (uname(&uts) < 0)
1134 return -1;
1135
1136 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1137 uts.release);
1138
1139 return dsos__load_modules_sym_dir(modules_path, filter, v);
1140}
1141
1142/*
1143 * Constructor variant for modules (where we know from /proc/modules where
1144 * they are loaded) and for vmlinux, where only after we load all the
1145 * symbols we'll know where it starts and ends.
1146 */
1147static struct map *map__new2(u64 start, struct dso *dso)
1148{
1149 struct map *self = malloc(sizeof(*self));
1150
1151 if (self != NULL) {
1152 self->start = start;
1153 /*
1154 * Will be filled after we load all the symbols
1155 */
1156 self->end = 0;
1157
1158 self->pgoff = 0;
1159 self->dso = dso;
1160 self->map_ip = map__map_ip;
1161 RB_CLEAR_NODE(&self->rb_node);
1162 }
1163 return self;
1164}
1165
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001166static int dsos__load_modules(unsigned int sym_priv_size)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001167{
1168 char *line = NULL;
1169 size_t n;
1170 FILE *file = fopen("/proc/modules", "r");
1171 struct map *map;
1172
1173 if (file == NULL)
1174 return -1;
1175
1176 while (!feof(file)) {
1177 char name[PATH_MAX];
1178 u64 start;
1179 struct dso *dso;
1180 char *sep;
1181 int line_len;
1182
1183 line_len = getline(&line, &n, file);
1184 if (line_len < 0)
1185 break;
1186
1187 if (!line)
1188 goto out_failure;
1189
1190 line[--line_len] = '\0'; /* \n */
1191
1192 sep = strrchr(line, 'x');
1193 if (sep == NULL)
1194 continue;
1195
1196 hex2u64(sep + 1, &start);
1197
1198 sep = strchr(line, ' ');
1199 if (sep == NULL)
1200 continue;
1201
1202 *sep = '\0';
1203
1204 snprintf(name, sizeof(name), "[%s]", line);
1205 dso = dso__new(name, sym_priv_size);
1206
1207 if (dso == NULL)
1208 goto out_delete_line;
1209
1210 map = map__new2(start, dso);
1211 if (map == NULL) {
1212 dso__delete(dso);
1213 goto out_delete_line;
1214 }
1215
1216 dso->origin = DSO__ORIG_KMODULE;
1217 kernel_maps__insert(map);
1218 dsos__add(dso);
1219 }
1220
1221 free(line);
1222 fclose(file);
1223
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001224 return 0;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001225
1226out_delete_line:
1227 free(line);
1228out_failure:
1229 return -1;
1230}
1231
1232static int dso__load_vmlinux(struct dso *self, struct map *map,
1233 const char *vmlinux,
Ingo Molnar83a09442009-08-15 12:26:57 +02001234 symbol_filter_t filter, int v)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001235{
1236 int err, fd = open(vmlinux, O_RDONLY);
1237
1238 if (fd < 0)
1239 return -1;
1240
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001241 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0, v);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001242
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001243 close(fd);
1244
1245 return err;
1246}
1247
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001248int dsos__load_kernel(const char *vmlinux, unsigned int sym_priv_size,
1249 symbol_filter_t filter, int v, int use_modules)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001250{
1251 int err = -1;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001252 struct dso *dso = dso__new(vmlinux, sym_priv_size);
1253
1254 if (dso == NULL)
1255 return -1;
1256
1257 dso->short_name = "[kernel]";
1258 kernel_map = map__new2(0, dso);
1259 if (kernel_map == NULL)
1260 goto out_delete_dso;
1261
1262 kernel_map->map_ip = vdso__map_ip;
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001263
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001264 if (use_modules && dsos__load_modules(sym_priv_size) < 0) {
1265 fprintf(stderr, "Failed to load list of modules in use! "
1266 "Continuing...\n");
1267 use_modules = 0;
1268 }
1269
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001270 if (vmlinux) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001271 err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter, v);
Mike Galbraith508c4d02009-09-23 11:20:58 +02001272 if (err > 0 && use_modules) {
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001273 int syms = dsos__load_modules_sym(filter, v);
Mike Galbraith508c4d02009-09-23 11:20:58 +02001274
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001275 if (syms < 0)
1276 fprintf(stderr, "Failed to read module symbols!"
1277 " Continuing...\n");
1278 else
1279 err += syms;
Mike Galbraith508c4d02009-09-23 11:20:58 +02001280 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001281 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001282
Mike Galbraith9974f492009-07-02 08:05:58 +02001283 if (err <= 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001284 err = kernel_maps__load_kallsyms(filter, use_modules, v);
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001285
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001286 if (err > 0) {
1287 struct rb_node *node = rb_first(&dso->syms);
1288 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
1289
1290 kernel_map->start = sym->start;
1291 node = rb_last(&dso->syms);
1292 sym = rb_entry(node, struct symbol, rb_node);
1293 kernel_map->end = sym->end;
1294
1295 dso->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001296 kernel_maps__insert(kernel_map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001297 /*
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001298 * Now that we have all sorted out, just set the ->end of all
1299 * maps:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001300 */
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001301 kernel_maps__fixup_end();
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001302 dsos__add(dso);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001303
1304 if (v > 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001305 kernel_maps__fprintf(stderr, v);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001306 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001307
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001308 return err;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001309
1310out_delete_dso:
1311 dso__delete(dso);
1312 return -1;
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001313}
1314
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001315LIST_HEAD(dsos);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001316struct dso *vdso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001317
Ingo Molnar83a09442009-08-15 12:26:57 +02001318const char *vmlinux_name = "vmlinux";
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001319int modules;
1320
1321static void dsos__add(struct dso *dso)
1322{
1323 list_add_tail(&dso->node, &dsos);
1324}
1325
1326static struct dso *dsos__find(const char *name)
1327{
1328 struct dso *pos;
1329
1330 list_for_each_entry(pos, &dsos, node)
1331 if (strcmp(pos->name, name) == 0)
1332 return pos;
1333 return NULL;
1334}
1335
1336struct dso *dsos__findnew(const char *name)
1337{
1338 struct dso *dso = dsos__find(name);
1339 int nr;
1340
1341 if (dso)
1342 return dso;
1343
1344 dso = dso__new(name, 0);
1345 if (!dso)
1346 goto out_delete_dso;
1347
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001348 nr = dso__load(dso, NULL, NULL, verbose);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001349 if (nr < 0) {
1350 eprintf("Failed to open: %s\n", name);
1351 goto out_delete_dso;
1352 }
1353 if (!nr)
1354 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
1355
1356 dsos__add(dso);
1357
1358 return dso;
1359
1360out_delete_dso:
1361 dso__delete(dso);
1362 return NULL;
1363}
1364
1365void dsos__fprintf(FILE *fp)
1366{
1367 struct dso *pos;
1368
1369 list_for_each_entry(pos, &dsos, node)
1370 dso__fprintf(pos, fp);
1371}
1372
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001373int load_kernel(void)
1374{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001375 if (dsos__load_kernel(vmlinux_name, 0, NULL, verbose, modules) <= 0)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001376 return -1;
1377
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001378 vdso = dso__new("[vdso]", 0);
1379 if (!vdso)
1380 return -1;
1381
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001382 dsos__add(vdso);
1383
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001384 return 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001385}
1386
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001387void symbol__init(void)
1388{
1389 elf_version(EV_CURRENT);
1390}