Commit | Line | Data |
---|---|---|
a2928c42 ACM |
1 | #include "util.h" |
2 | #include "../perf.h" | |
a0055ae2 | 3 | #include "string.h" |
a2928c42 | 4 | #include "symbol.h" |
439d473b | 5 | #include "thread.h" |
a2928c42 | 6 | |
8f28827a FW |
7 | #include "debug.h" |
8 | ||
a2928c42 ACM |
9 | #include <libelf.h> |
10 | #include <gelf.h> | |
11 | #include <elf.h> | |
439d473b | 12 | #include <sys/utsname.h> |
2cdbc46d | 13 | |
94cb9e38 ACM |
14 | enum dso_origin { |
15 | DSO__ORIG_KERNEL = 0, | |
16 | DSO__ORIG_JAVA_JIT, | |
17 | DSO__ORIG_FEDORA, | |
18 | DSO__ORIG_UBUNTU, | |
19 | DSO__ORIG_BUILDID, | |
20 | DSO__ORIG_DSO, | |
439d473b | 21 | DSO__ORIG_KMODULE, |
94cb9e38 ACM |
22 | DSO__ORIG_NOT_FOUND, |
23 | }; | |
24 | ||
439d473b ACM |
25 | static void dsos__add(struct dso *dso); |
26 | static struct dso *dsos__find(const char *name); | |
2e538c4a ACM |
27 | static struct map *map__new2(u64 start, struct dso *dso); |
28 | static void kernel_maps__insert(struct map *map); | |
00a192b3 | 29 | unsigned int symbol__priv_size; |
439d473b | 30 | |
af427bf5 ACM |
31 | static struct rb_root kernel_maps; |
32 | ||
2e538c4a | 33 | static void dso__fixup_sym_end(struct dso *self) |
af427bf5 ACM |
34 | { |
35 | struct rb_node *nd, *prevnd = rb_first(&self->syms); | |
2e538c4a | 36 | struct symbol *curr, *prev; |
af427bf5 ACM |
37 | |
38 | if (prevnd == NULL) | |
39 | return; | |
40 | ||
2e538c4a ACM |
41 | curr = rb_entry(prevnd, struct symbol, rb_node); |
42 | ||
af427bf5 | 43 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
2e538c4a ACM |
44 | prev = curr; |
45 | curr = rb_entry(nd, struct symbol, rb_node); | |
af427bf5 ACM |
46 | |
47 | if (prev->end == prev->start) | |
48 | prev->end = curr->start - 1; | |
af427bf5 | 49 | } |
2e538c4a ACM |
50 | |
51 | /* Last entry */ | |
52 | if (curr->end == curr->start) | |
53 | curr->end = roundup(curr->start, 4096); | |
af427bf5 ACM |
54 | } |
55 | ||
2e538c4a | 56 | static void kernel_maps__fixup_end(void) |
af427bf5 ACM |
57 | { |
58 | struct map *prev, *curr; | |
59 | struct rb_node *nd, *prevnd = rb_first(&kernel_maps); | |
60 | ||
61 | if (prevnd == NULL) | |
62 | return; | |
63 | ||
64 | curr = rb_entry(prevnd, struct map, rb_node); | |
af427bf5 ACM |
65 | |
66 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { | |
67 | prev = curr; | |
68 | curr = rb_entry(nd, struct map, rb_node); | |
69 | prev->end = curr->start - 1; | |
2e538c4a ACM |
70 | } |
71 | ||
72 | nd = rb_last(&curr->dso->syms); | |
73 | if (nd) { | |
74 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | |
75 | curr->end = sym->end; | |
af427bf5 ACM |
76 | } |
77 | } | |
78 | ||
00a192b3 | 79 | static struct symbol *symbol__new(u64 start, u64 len, const char *name) |
a2928c42 | 80 | { |
0085c954 | 81 | size_t namelen = strlen(name) + 1; |
00a192b3 ACM |
82 | struct symbol *self = calloc(1, (symbol__priv_size + |
83 | sizeof(*self) + namelen)); | |
0b73da3f IM |
84 | if (!self) |
85 | return NULL; | |
86 | ||
00a192b3 ACM |
87 | if (symbol__priv_size) { |
88 | memset(self, 0, symbol__priv_size); | |
89 | self = ((void *)self) + symbol__priv_size; | |
a2928c42 | 90 | } |
0b73da3f | 91 | self->start = start; |
6cfcc53e | 92 | self->end = len ? start + len - 1 : start; |
e4204992 | 93 | |
6beba7ad | 94 | pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end); |
e4204992 | 95 | |
0b73da3f | 96 | memcpy(self->name, name, namelen); |
a2928c42 ACM |
97 | |
98 | return self; | |
99 | } | |
100 | ||
00a192b3 | 101 | static void symbol__delete(struct symbol *self) |
a2928c42 | 102 | { |
00a192b3 | 103 | free(((void *)self) - symbol__priv_size); |
a2928c42 ACM |
104 | } |
105 | ||
106 | static size_t symbol__fprintf(struct symbol *self, FILE *fp) | |
107 | { | |
439d473b | 108 | return fprintf(fp, " %llx-%llx %s\n", |
a2928c42 ACM |
109 | self->start, self->end, self->name); |
110 | } | |
111 | ||
00a192b3 | 112 | struct dso *dso__new(const char *name) |
a2928c42 ACM |
113 | { |
114 | struct dso *self = malloc(sizeof(*self) + strlen(name) + 1); | |
115 | ||
116 | if (self != NULL) { | |
117 | strcpy(self->name, name); | |
439d473b ACM |
118 | self->long_name = self->name; |
119 | self->short_name = self->name; | |
a2928c42 | 120 | self->syms = RB_ROOT; |
fc54db51 | 121 | self->find_symbol = dso__find_symbol; |
52d422de | 122 | self->slen_calculated = 0; |
94cb9e38 | 123 | self->origin = DSO__ORIG_NOT_FOUND; |
8d06367f ACM |
124 | self->loaded = 0; |
125 | self->has_build_id = 0; | |
a2928c42 ACM |
126 | } |
127 | ||
128 | return self; | |
129 | } | |
130 | ||
131 | static void dso__delete_symbols(struct dso *self) | |
132 | { | |
133 | struct symbol *pos; | |
134 | struct rb_node *next = rb_first(&self->syms); | |
135 | ||
136 | while (next) { | |
137 | pos = rb_entry(next, struct symbol, rb_node); | |
138 | next = rb_next(&pos->rb_node); | |
c8c96525 | 139 | rb_erase(&pos->rb_node, &self->syms); |
00a192b3 | 140 | symbol__delete(pos); |
a2928c42 ACM |
141 | } |
142 | } | |
143 | ||
144 | void dso__delete(struct dso *self) | |
145 | { | |
146 | dso__delete_symbols(self); | |
439d473b ACM |
147 | if (self->long_name != self->name) |
148 | free(self->long_name); | |
a2928c42 ACM |
149 | free(self); |
150 | } | |
151 | ||
8d06367f ACM |
152 | void dso__set_build_id(struct dso *self, void *build_id) |
153 | { | |
154 | memcpy(self->build_id, build_id, sizeof(self->build_id)); | |
155 | self->has_build_id = 1; | |
156 | } | |
157 | ||
a2928c42 ACM |
158 | static void dso__insert_symbol(struct dso *self, struct symbol *sym) |
159 | { | |
160 | struct rb_node **p = &self->syms.rb_node; | |
161 | struct rb_node *parent = NULL; | |
9cffa8d5 | 162 | const u64 ip = sym->start; |
a2928c42 ACM |
163 | struct symbol *s; |
164 | ||
165 | while (*p != NULL) { | |
166 | parent = *p; | |
167 | s = rb_entry(parent, struct symbol, rb_node); | |
168 | if (ip < s->start) | |
169 | p = &(*p)->rb_left; | |
170 | else | |
171 | p = &(*p)->rb_right; | |
172 | } | |
173 | rb_link_node(&sym->rb_node, parent, p); | |
174 | rb_insert_color(&sym->rb_node, &self->syms); | |
175 | } | |
176 | ||
9cffa8d5 | 177 | struct symbol *dso__find_symbol(struct dso *self, u64 ip) |
a2928c42 ACM |
178 | { |
179 | struct rb_node *n; | |
180 | ||
181 | if (self == NULL) | |
182 | return NULL; | |
183 | ||
184 | n = self->syms.rb_node; | |
185 | ||
186 | while (n) { | |
187 | struct symbol *s = rb_entry(n, struct symbol, rb_node); | |
188 | ||
189 | if (ip < s->start) | |
190 | n = n->rb_left; | |
191 | else if (ip > s->end) | |
192 | n = n->rb_right; | |
193 | else | |
194 | return s; | |
195 | } | |
196 | ||
197 | return NULL; | |
198 | } | |
199 | ||
8d06367f | 200 | int build_id__sprintf(u8 *self, int len, char *bf) |
a2928c42 | 201 | { |
8d06367f ACM |
202 | char *bid = bf; |
203 | u8 *raw = self; | |
204 | int i; | |
a2928c42 | 205 | |
8d06367f ACM |
206 | for (i = 0; i < len; ++i) { |
207 | sprintf(bid, "%02x", *raw); | |
208 | ++raw; | |
209 | bid += 2; | |
210 | } | |
211 | ||
212 | return raw - self; | |
213 | } | |
214 | ||
215 | size_t dso__fprintf(struct dso *self, FILE *fp) | |
216 | { | |
217 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; | |
a2928c42 | 218 | struct rb_node *nd; |
8d06367f ACM |
219 | size_t ret; |
220 | ||
221 | build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id); | |
222 | ret = fprintf(fp, "dso: %s (%s)\n", self->short_name, sbuild_id); | |
223 | ||
a2928c42 ACM |
224 | for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) { |
225 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); | |
226 | ret += symbol__fprintf(pos, fp); | |
227 | } | |
228 | ||
229 | return ret; | |
230 | } | |
231 | ||
2e538c4a ACM |
232 | /* |
233 | * Loads the function entries in /proc/kallsyms into kernel_map->dso, | |
234 | * so that we can in the next step set the symbol ->end address and then | |
235 | * call kernel_maps__split_kallsyms. | |
236 | */ | |
6beba7ad | 237 | static int kernel_maps__load_all_kallsyms(void) |
a2928c42 | 238 | { |
a2928c42 ACM |
239 | char *line = NULL; |
240 | size_t n; | |
241 | FILE *file = fopen("/proc/kallsyms", "r"); | |
242 | ||
243 | if (file == NULL) | |
244 | goto out_failure; | |
245 | ||
246 | while (!feof(file)) { | |
9cffa8d5 | 247 | u64 start; |
a2928c42 ACM |
248 | struct symbol *sym; |
249 | int line_len, len; | |
250 | char symbol_type; | |
2e538c4a | 251 | char *symbol_name; |
a2928c42 ACM |
252 | |
253 | line_len = getline(&line, &n, file); | |
254 | if (line_len < 0) | |
255 | break; | |
256 | ||
257 | if (!line) | |
258 | goto out_failure; | |
259 | ||
260 | line[--line_len] = '\0'; /* \n */ | |
261 | ||
a0055ae2 | 262 | len = hex2u64(line, &start); |
a2928c42 ACM |
263 | |
264 | len++; | |
265 | if (len + 2 >= line_len) | |
266 | continue; | |
267 | ||
268 | symbol_type = toupper(line[len]); | |
269 | /* | |
270 | * We're interested only in code ('T'ext) | |
271 | */ | |
272 | if (symbol_type != 'T' && symbol_type != 'W') | |
273 | continue; | |
af427bf5 ACM |
274 | |
275 | symbol_name = line + len + 2; | |
2e538c4a ACM |
276 | /* |
277 | * Will fix up the end later, when we have all symbols sorted. | |
278 | */ | |
00a192b3 | 279 | sym = symbol__new(start, 0, symbol_name); |
af427bf5 | 280 | |
2e538c4a ACM |
281 | if (sym == NULL) |
282 | goto out_delete_line; | |
283 | ||
82164161 ACM |
284 | /* |
285 | * We will pass the symbols to the filter later, in | |
286 | * kernel_maps__split_kallsyms, when we have split the | |
287 | * maps per module | |
288 | */ | |
2e538c4a ACM |
289 | dso__insert_symbol(kernel_map->dso, sym); |
290 | } | |
291 | ||
292 | free(line); | |
293 | fclose(file); | |
294 | ||
295 | return 0; | |
296 | ||
297 | out_delete_line: | |
298 | free(line); | |
299 | out_failure: | |
300 | return -1; | |
301 | } | |
302 | ||
303 | /* | |
304 | * Split the symbols into maps, making sure there are no overlaps, i.e. the | |
305 | * kernel range is broken in several maps, named [kernel].N, as we don't have | |
306 | * the original ELF section names vmlinux have. | |
307 | */ | |
308 | static int kernel_maps__split_kallsyms(symbol_filter_t filter, int use_modules) | |
309 | { | |
310 | struct map *map = kernel_map; | |
311 | struct symbol *pos; | |
312 | int count = 0; | |
313 | struct rb_node *next = rb_first(&kernel_map->dso->syms); | |
314 | int kernel_range = 0; | |
315 | ||
316 | while (next) { | |
317 | char *module; | |
318 | ||
319 | pos = rb_entry(next, struct symbol, rb_node); | |
320 | next = rb_next(&pos->rb_node); | |
321 | ||
322 | module = strchr(pos->name, '\t'); | |
323 | if (module) { | |
af427bf5 | 324 | if (!use_modules) |
2e538c4a ACM |
325 | goto delete_symbol; |
326 | ||
327 | *module++ = '\0'; | |
328 | ||
af427bf5 ACM |
329 | if (strcmp(map->dso->name, module)) { |
330 | map = kernel_maps__find_by_dso_name(module); | |
331 | if (!map) { | |
6beba7ad ACM |
332 | pr_err("/proc/{kallsyms,modules} " |
333 | "inconsistency!\n"); | |
af427bf5 ACM |
334 | return -1; |
335 | } | |
336 | } | |
2e538c4a ACM |
337 | /* |
338 | * So that we look just like we get from .ko files, | |
339 | * i.e. not prelinked, relative to map->start. | |
340 | */ | |
341 | pos->start = map->map_ip(map, pos->start); | |
342 | pos->end = map->map_ip(map, pos->end); | |
343 | } else if (map != kernel_map) { | |
344 | char dso_name[PATH_MAX]; | |
345 | struct dso *dso; | |
346 | ||
347 | snprintf(dso_name, sizeof(dso_name), "[kernel].%d", | |
348 | kernel_range++); | |
349 | ||
00a192b3 | 350 | dso = dso__new(dso_name); |
2e538c4a ACM |
351 | if (dso == NULL) |
352 | return -1; | |
353 | ||
354 | map = map__new2(pos->start, dso); | |
355 | if (map == NULL) { | |
356 | dso__delete(dso); | |
357 | return -1; | |
358 | } | |
a2928c42 | 359 | |
ed52ce2e | 360 | map->map_ip = map->unmap_ip = identity__map_ip; |
2e538c4a ACM |
361 | kernel_maps__insert(map); |
362 | ++kernel_range; | |
363 | } | |
a2928c42 | 364 | |
2e538c4a ACM |
365 | if (filter && filter(map, pos)) { |
366 | delete_symbol: | |
367 | rb_erase(&pos->rb_node, &kernel_map->dso->syms); | |
00a192b3 | 368 | symbol__delete(pos); |
2e538c4a ACM |
369 | } else { |
370 | if (map != kernel_map) { | |
371 | rb_erase(&pos->rb_node, &kernel_map->dso->syms); | |
372 | dso__insert_symbol(map->dso, pos); | |
373 | } | |
9974f496 MG |
374 | count++; |
375 | } | |
a2928c42 ACM |
376 | } |
377 | ||
9974f496 | 378 | return count; |
2e538c4a | 379 | } |
a2928c42 | 380 | |
2e538c4a | 381 | |
6beba7ad | 382 | static int kernel_maps__load_kallsyms(symbol_filter_t filter, int use_modules) |
2e538c4a | 383 | { |
6beba7ad | 384 | if (kernel_maps__load_all_kallsyms()) |
2e538c4a ACM |
385 | return -1; |
386 | ||
387 | dso__fixup_sym_end(kernel_map->dso); | |
388 | ||
389 | return kernel_maps__split_kallsyms(filter, use_modules); | |
a2928c42 ACM |
390 | } |
391 | ||
6beba7ad | 392 | static size_t kernel_maps__fprintf(FILE *fp) |
af427bf5 | 393 | { |
6beba7ad | 394 | size_t printed = fprintf(fp, "Kernel maps:\n"); |
af427bf5 ACM |
395 | struct rb_node *nd; |
396 | ||
af427bf5 ACM |
397 | for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) { |
398 | struct map *pos = rb_entry(nd, struct map, rb_node); | |
399 | ||
2e538c4a | 400 | printed += fprintf(fp, "Map:"); |
af427bf5 | 401 | printed += map__fprintf(pos, fp); |
6beba7ad | 402 | if (verbose > 1) { |
2e538c4a ACM |
403 | printed += dso__fprintf(pos->dso, fp); |
404 | printed += fprintf(fp, "--\n"); | |
405 | } | |
af427bf5 ACM |
406 | } |
407 | ||
6beba7ad | 408 | return printed + fprintf(fp, "END kernel maps\n"); |
af427bf5 ACM |
409 | } |
410 | ||
439d473b | 411 | static int dso__load_perf_map(struct dso *self, struct map *map, |
6beba7ad | 412 | symbol_filter_t filter) |
80d496be PE |
413 | { |
414 | char *line = NULL; | |
415 | size_t n; | |
416 | FILE *file; | |
417 | int nr_syms = 0; | |
418 | ||
439d473b | 419 | file = fopen(self->long_name, "r"); |
80d496be PE |
420 | if (file == NULL) |
421 | goto out_failure; | |
422 | ||
423 | while (!feof(file)) { | |
9cffa8d5 | 424 | u64 start, size; |
80d496be PE |
425 | struct symbol *sym; |
426 | int line_len, len; | |
427 | ||
428 | line_len = getline(&line, &n, file); | |
429 | if (line_len < 0) | |
430 | break; | |
431 | ||
432 | if (!line) | |
433 | goto out_failure; | |
434 | ||
435 | line[--line_len] = '\0'; /* \n */ | |
436 | ||
437 | len = hex2u64(line, &start); | |
438 | ||
439 | len++; | |
440 | if (len + 2 >= line_len) | |
441 | continue; | |
442 | ||
443 | len += hex2u64(line + len, &size); | |
444 | ||
445 | len++; | |
446 | if (len + 2 >= line_len) | |
447 | continue; | |
448 | ||
00a192b3 | 449 | sym = symbol__new(start, size, line + len); |
80d496be PE |
450 | |
451 | if (sym == NULL) | |
452 | goto out_delete_line; | |
453 | ||
439d473b | 454 | if (filter && filter(map, sym)) |
00a192b3 | 455 | symbol__delete(sym); |
80d496be PE |
456 | else { |
457 | dso__insert_symbol(self, sym); | |
458 | nr_syms++; | |
459 | } | |
460 | } | |
461 | ||
462 | free(line); | |
463 | fclose(file); | |
464 | ||
465 | return nr_syms; | |
466 | ||
467 | out_delete_line: | |
468 | free(line); | |
469 | out_failure: | |
470 | return -1; | |
471 | } | |
472 | ||
a2928c42 ACM |
473 | /** |
474 | * elf_symtab__for_each_symbol - iterate thru all the symbols | |
475 | * | |
476 | * @self: struct elf_symtab instance to iterate | |
83a0944f | 477 | * @idx: uint32_t idx |
a2928c42 ACM |
478 | * @sym: GElf_Sym iterator |
479 | */ | |
83a0944f IM |
480 | #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \ |
481 | for (idx = 0, gelf_getsym(syms, idx, &sym);\ | |
482 | idx < nr_syms; \ | |
483 | idx++, gelf_getsym(syms, idx, &sym)) | |
a2928c42 ACM |
484 | |
485 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) | |
486 | { | |
487 | return GELF_ST_TYPE(sym->st_info); | |
488 | } | |
489 | ||
490 | static inline int elf_sym__is_function(const GElf_Sym *sym) | |
491 | { | |
492 | return elf_sym__type(sym) == STT_FUNC && | |
493 | sym->st_name != 0 && | |
81833130 | 494 | sym->st_shndx != SHN_UNDEF; |
a2928c42 ACM |
495 | } |
496 | ||
6cfcc53e MG |
497 | static inline int elf_sym__is_label(const GElf_Sym *sym) |
498 | { | |
499 | return elf_sym__type(sym) == STT_NOTYPE && | |
500 | sym->st_name != 0 && | |
501 | sym->st_shndx != SHN_UNDEF && | |
502 | sym->st_shndx != SHN_ABS; | |
503 | } | |
504 | ||
505 | static inline const char *elf_sec__name(const GElf_Shdr *shdr, | |
506 | const Elf_Data *secstrs) | |
507 | { | |
508 | return secstrs->d_buf + shdr->sh_name; | |
509 | } | |
510 | ||
511 | static inline int elf_sec__is_text(const GElf_Shdr *shdr, | |
512 | const Elf_Data *secstrs) | |
513 | { | |
514 | return strstr(elf_sec__name(shdr, secstrs), "text") != NULL; | |
515 | } | |
516 | ||
a2928c42 ACM |
517 | static inline const char *elf_sym__name(const GElf_Sym *sym, |
518 | const Elf_Data *symstrs) | |
519 | { | |
520 | return symstrs->d_buf + sym->st_name; | |
521 | } | |
522 | ||
523 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, | |
524 | GElf_Shdr *shp, const char *name, | |
83a0944f | 525 | size_t *idx) |
a2928c42 ACM |
526 | { |
527 | Elf_Scn *sec = NULL; | |
528 | size_t cnt = 1; | |
529 | ||
530 | while ((sec = elf_nextscn(elf, sec)) != NULL) { | |
531 | char *str; | |
532 | ||
533 | gelf_getshdr(sec, shp); | |
534 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); | |
535 | if (!strcmp(name, str)) { | |
83a0944f IM |
536 | if (idx) |
537 | *idx = cnt; | |
a2928c42 ACM |
538 | break; |
539 | } | |
540 | ++cnt; | |
541 | } | |
542 | ||
543 | return sec; | |
544 | } | |
545 | ||
8ce998d6 ACM |
546 | #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ |
547 | for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \ | |
548 | idx < nr_entries; \ | |
549 | ++idx, pos = gelf_getrel(reldata, idx, &pos_mem)) | |
550 | ||
551 | #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \ | |
552 | for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \ | |
553 | idx < nr_entries; \ | |
554 | ++idx, pos = gelf_getrela(reldata, idx, &pos_mem)) | |
555 | ||
a25e46c4 ACM |
556 | /* |
557 | * We need to check if we have a .dynsym, so that we can handle the | |
558 | * .plt, synthesizing its symbols, that aren't on the symtabs (be it | |
559 | * .dynsym or .symtab). | |
560 | * And always look at the original dso, not at debuginfo packages, that | |
561 | * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). | |
562 | */ | |
82164161 ACM |
563 | static int dso__synthesize_plt_symbols(struct dso *self, struct map *map, |
564 | symbol_filter_t filter) | |
8ce998d6 ACM |
565 | { |
566 | uint32_t nr_rel_entries, idx; | |
567 | GElf_Sym sym; | |
9cffa8d5 | 568 | u64 plt_offset; |
8ce998d6 ACM |
569 | GElf_Shdr shdr_plt; |
570 | struct symbol *f; | |
a25e46c4 | 571 | GElf_Shdr shdr_rel_plt, shdr_dynsym; |
8ce998d6 | 572 | Elf_Data *reldata, *syms, *symstrs; |
a25e46c4 ACM |
573 | Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; |
574 | size_t dynsym_idx; | |
575 | GElf_Ehdr ehdr; | |
8ce998d6 | 576 | char sympltname[1024]; |
a25e46c4 ACM |
577 | Elf *elf; |
578 | int nr = 0, symidx, fd, err = 0; | |
579 | ||
439d473b | 580 | fd = open(self->long_name, O_RDONLY); |
a25e46c4 ACM |
581 | if (fd < 0) |
582 | goto out; | |
583 | ||
84087126 | 584 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
a25e46c4 ACM |
585 | if (elf == NULL) |
586 | goto out_close; | |
587 | ||
588 | if (gelf_getehdr(elf, &ehdr) == NULL) | |
589 | goto out_elf_end; | |
590 | ||
591 | scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym, | |
592 | ".dynsym", &dynsym_idx); | |
593 | if (scn_dynsym == NULL) | |
594 | goto out_elf_end; | |
8ce998d6 | 595 | |
a25e46c4 | 596 | scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, |
8ce998d6 ACM |
597 | ".rela.plt", NULL); |
598 | if (scn_plt_rel == NULL) { | |
a25e46c4 | 599 | scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, |
8ce998d6 ACM |
600 | ".rel.plt", NULL); |
601 | if (scn_plt_rel == NULL) | |
a25e46c4 | 602 | goto out_elf_end; |
8ce998d6 ACM |
603 | } |
604 | ||
a25e46c4 ACM |
605 | err = -1; |
606 | ||
8ce998d6 | 607 | if (shdr_rel_plt.sh_link != dynsym_idx) |
a25e46c4 | 608 | goto out_elf_end; |
8ce998d6 | 609 | |
a25e46c4 ACM |
610 | if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL) |
611 | goto out_elf_end; | |
8ce998d6 ACM |
612 | |
613 | /* | |
83a0944f | 614 | * Fetch the relocation section to find the idxes to the GOT |
8ce998d6 ACM |
615 | * and the symbols in the .dynsym they refer to. |
616 | */ | |
617 | reldata = elf_getdata(scn_plt_rel, NULL); | |
618 | if (reldata == NULL) | |
a25e46c4 | 619 | goto out_elf_end; |
8ce998d6 ACM |
620 | |
621 | syms = elf_getdata(scn_dynsym, NULL); | |
622 | if (syms == NULL) | |
a25e46c4 | 623 | goto out_elf_end; |
8ce998d6 | 624 | |
a25e46c4 | 625 | scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link); |
8ce998d6 | 626 | if (scn_symstrs == NULL) |
a25e46c4 | 627 | goto out_elf_end; |
8ce998d6 ACM |
628 | |
629 | symstrs = elf_getdata(scn_symstrs, NULL); | |
630 | if (symstrs == NULL) | |
a25e46c4 | 631 | goto out_elf_end; |
8ce998d6 ACM |
632 | |
633 | nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; | |
634 | plt_offset = shdr_plt.sh_offset; | |
635 | ||
636 | if (shdr_rel_plt.sh_type == SHT_RELA) { | |
637 | GElf_Rela pos_mem, *pos; | |
638 | ||
639 | elf_section__for_each_rela(reldata, pos, pos_mem, idx, | |
640 | nr_rel_entries) { | |
641 | symidx = GELF_R_SYM(pos->r_info); | |
642 | plt_offset += shdr_plt.sh_entsize; | |
643 | gelf_getsym(syms, symidx, &sym); | |
644 | snprintf(sympltname, sizeof(sympltname), | |
645 | "%s@plt", elf_sym__name(&sym, symstrs)); | |
646 | ||
647 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, | |
00a192b3 | 648 | sympltname); |
8ce998d6 | 649 | if (!f) |
a25e46c4 | 650 | goto out_elf_end; |
8ce998d6 | 651 | |
82164161 ACM |
652 | if (filter && filter(map, f)) |
653 | symbol__delete(f); | |
654 | else { | |
655 | dso__insert_symbol(self, f); | |
656 | ++nr; | |
657 | } | |
8ce998d6 ACM |
658 | } |
659 | } else if (shdr_rel_plt.sh_type == SHT_REL) { | |
660 | GElf_Rel pos_mem, *pos; | |
661 | elf_section__for_each_rel(reldata, pos, pos_mem, idx, | |
662 | nr_rel_entries) { | |
663 | symidx = GELF_R_SYM(pos->r_info); | |
664 | plt_offset += shdr_plt.sh_entsize; | |
665 | gelf_getsym(syms, symidx, &sym); | |
666 | snprintf(sympltname, sizeof(sympltname), | |
667 | "%s@plt", elf_sym__name(&sym, symstrs)); | |
668 | ||
669 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, | |
00a192b3 | 670 | sympltname); |
8ce998d6 | 671 | if (!f) |
a25e46c4 | 672 | goto out_elf_end; |
8ce998d6 | 673 | |
82164161 ACM |
674 | if (filter && filter(map, f)) |
675 | symbol__delete(f); | |
676 | else { | |
677 | dso__insert_symbol(self, f); | |
678 | ++nr; | |
679 | } | |
8ce998d6 | 680 | } |
8ce998d6 ACM |
681 | } |
682 | ||
a25e46c4 ACM |
683 | err = 0; |
684 | out_elf_end: | |
685 | elf_end(elf); | |
686 | out_close: | |
687 | close(fd); | |
688 | ||
689 | if (err == 0) | |
690 | return nr; | |
691 | out: | |
6beba7ad ACM |
692 | pr_warning("%s: problems reading %s PLT info.\n", |
693 | __func__, self->long_name); | |
a25e46c4 | 694 | return 0; |
8ce998d6 ACM |
695 | } |
696 | ||
439d473b ACM |
697 | static int dso__load_sym(struct dso *self, struct map *map, const char *name, |
698 | int fd, symbol_filter_t filter, int kernel, | |
6beba7ad | 699 | int kmodule) |
a2928c42 | 700 | { |
2e538c4a ACM |
701 | struct map *curr_map = map; |
702 | struct dso *curr_dso = self; | |
703 | size_t dso_name_len = strlen(self->short_name); | |
6cfcc53e | 704 | Elf_Data *symstrs, *secstrs; |
a2928c42 ACM |
705 | uint32_t nr_syms; |
706 | int err = -1; | |
83a0944f | 707 | uint32_t idx; |
a2928c42 ACM |
708 | GElf_Ehdr ehdr; |
709 | GElf_Shdr shdr; | |
710 | Elf_Data *syms; | |
711 | GElf_Sym sym; | |
a25e46c4 | 712 | Elf_Scn *sec, *sec_strndx; |
a2928c42 | 713 | Elf *elf; |
439d473b | 714 | int nr = 0; |
a2928c42 | 715 | |
84087126 | 716 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
a2928c42 | 717 | if (elf == NULL) { |
6beba7ad | 718 | pr_err("%s: cannot read %s ELF file.\n", __func__, name); |
a2928c42 ACM |
719 | goto out_close; |
720 | } | |
721 | ||
722 | if (gelf_getehdr(elf, &ehdr) == NULL) { | |
6beba7ad | 723 | pr_err("%s: cannot get elf header.\n", __func__); |
a2928c42 ACM |
724 | goto out_elf_end; |
725 | } | |
726 | ||
727 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); | |
8ce998d6 | 728 | if (sec == NULL) { |
a25e46c4 ACM |
729 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); |
730 | if (sec == NULL) | |
8ce998d6 | 731 | goto out_elf_end; |
8ce998d6 | 732 | } |
a2928c42 ACM |
733 | |
734 | syms = elf_getdata(sec, NULL); | |
735 | if (syms == NULL) | |
736 | goto out_elf_end; | |
737 | ||
738 | sec = elf_getscn(elf, shdr.sh_link); | |
739 | if (sec == NULL) | |
740 | goto out_elf_end; | |
741 | ||
742 | symstrs = elf_getdata(sec, NULL); | |
743 | if (symstrs == NULL) | |
744 | goto out_elf_end; | |
745 | ||
6cfcc53e MG |
746 | sec_strndx = elf_getscn(elf, ehdr.e_shstrndx); |
747 | if (sec_strndx == NULL) | |
748 | goto out_elf_end; | |
749 | ||
750 | secstrs = elf_getdata(sec_strndx, NULL); | |
9b30a26b | 751 | if (secstrs == NULL) |
6cfcc53e MG |
752 | goto out_elf_end; |
753 | ||
a2928c42 ACM |
754 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
755 | ||
e9fbc9dc | 756 | memset(&sym, 0, sizeof(sym)); |
d20ff6bd MG |
757 | if (!kernel) { |
758 | self->adjust_symbols = (ehdr.e_type == ET_EXEC || | |
30d7a77d ACM |
759 | elf_section_by_name(elf, &ehdr, &shdr, |
760 | ".gnu.prelink_undo", | |
761 | NULL) != NULL); | |
d20ff6bd MG |
762 | } else self->adjust_symbols = 0; |
763 | ||
83a0944f | 764 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { |
a2928c42 | 765 | struct symbol *f; |
83a0944f | 766 | const char *elf_name; |
2e538c4a | 767 | char *demangled = NULL; |
6cfcc53e MG |
768 | int is_label = elf_sym__is_label(&sym); |
769 | const char *section_name; | |
a2928c42 | 770 | |
6cfcc53e | 771 | if (!is_label && !elf_sym__is_function(&sym)) |
a2928c42 ACM |
772 | continue; |
773 | ||
774 | sec = elf_getscn(elf, sym.st_shndx); | |
775 | if (!sec) | |
776 | goto out_elf_end; | |
777 | ||
778 | gelf_getshdr(sec, &shdr); | |
6cfcc53e MG |
779 | |
780 | if (is_label && !elf_sec__is_text(&shdr, secstrs)) | |
781 | continue; | |
782 | ||
2e538c4a | 783 | elf_name = elf_sym__name(&sym, symstrs); |
6cfcc53e | 784 | section_name = elf_sec__name(&shdr, secstrs); |
0b73da3f | 785 | |
2e538c4a ACM |
786 | if (kernel || kmodule) { |
787 | char dso_name[PATH_MAX]; | |
788 | ||
789 | if (strcmp(section_name, | |
790 | curr_dso->short_name + dso_name_len) == 0) | |
791 | goto new_symbol; | |
792 | ||
793 | if (strcmp(section_name, ".text") == 0) { | |
794 | curr_map = map; | |
795 | curr_dso = self; | |
796 | goto new_symbol; | |
797 | } | |
798 | ||
799 | snprintf(dso_name, sizeof(dso_name), | |
800 | "%s%s", self->short_name, section_name); | |
801 | ||
802 | curr_map = kernel_maps__find_by_dso_name(dso_name); | |
803 | if (curr_map == NULL) { | |
804 | u64 start = sym.st_value; | |
805 | ||
806 | if (kmodule) | |
807 | start += map->start + shdr.sh_offset; | |
808 | ||
00a192b3 | 809 | curr_dso = dso__new(dso_name); |
2e538c4a ACM |
810 | if (curr_dso == NULL) |
811 | goto out_elf_end; | |
812 | curr_map = map__new2(start, curr_dso); | |
813 | if (curr_map == NULL) { | |
814 | dso__delete(curr_dso); | |
815 | goto out_elf_end; | |
816 | } | |
ed52ce2e ACM |
817 | curr_map->map_ip = identity__map_ip; |
818 | curr_map->unmap_ip = identity__map_ip; | |
2e538c4a ACM |
819 | curr_dso->origin = DSO__ORIG_KERNEL; |
820 | kernel_maps__insert(curr_map); | |
821 | dsos__add(curr_dso); | |
822 | } else | |
823 | curr_dso = curr_map->dso; | |
824 | ||
825 | goto new_symbol; | |
af427bf5 ACM |
826 | } |
827 | ||
2e538c4a | 828 | if (curr_dso->adjust_symbols) { |
6beba7ad ACM |
829 | pr_debug2("adjusting symbol: st_value: %Lx sh_addr: " |
830 | "%Lx sh_offset: %Lx\n", (u64)sym.st_value, | |
831 | (u64)shdr.sh_addr, (u64)shdr.sh_offset); | |
f5812a7a | 832 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; |
af427bf5 | 833 | } |
28ac909b ACM |
834 | /* |
835 | * We need to figure out if the object was created from C++ sources | |
836 | * DWARF DW_compile_unit has this, but we don't always have access | |
837 | * to it... | |
838 | */ | |
83a0944f | 839 | demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI); |
28ac909b | 840 | if (demangled != NULL) |
83a0944f | 841 | elf_name = demangled; |
2e538c4a | 842 | new_symbol: |
00a192b3 | 843 | f = symbol__new(sym.st_value, sym.st_size, elf_name); |
28ac909b | 844 | free(demangled); |
a2928c42 ACM |
845 | if (!f) |
846 | goto out_elf_end; | |
847 | ||
2e538c4a | 848 | if (filter && filter(curr_map, f)) |
00a192b3 | 849 | symbol__delete(f); |
69ee69f6 | 850 | else { |
2e538c4a | 851 | dso__insert_symbol(curr_dso, f); |
69ee69f6 ACM |
852 | nr++; |
853 | } | |
a2928c42 ACM |
854 | } |
855 | ||
2e538c4a ACM |
856 | /* |
857 | * For misannotated, zeroed, ASM function sizes. | |
858 | */ | |
859 | if (nr > 0) | |
860 | dso__fixup_sym_end(self); | |
a2928c42 ACM |
861 | err = nr; |
862 | out_elf_end: | |
863 | elf_end(elf); | |
864 | out_close: | |
865 | return err; | |
866 | } | |
867 | ||
57f395a7 FW |
868 | bool fetch_build_id_table(struct list_head *head) |
869 | { | |
870 | bool have_buildid = false; | |
871 | struct dso *pos; | |
872 | ||
873 | list_for_each_entry(pos, &dsos, node) { | |
874 | struct build_id_list *new; | |
875 | struct build_id_event b; | |
876 | size_t len; | |
877 | ||
878 | if (filename__read_build_id(pos->long_name, | |
879 | &b.build_id, | |
880 | sizeof(b.build_id)) < 0) | |
881 | continue; | |
882 | have_buildid = true; | |
883 | memset(&b.header, 0, sizeof(b.header)); | |
884 | len = strlen(pos->long_name) + 1; | |
885 | len = ALIGN(len, 64); | |
886 | b.header.size = sizeof(b) + len; | |
887 | ||
888 | new = malloc(sizeof(*new)); | |
889 | if (!new) | |
890 | die("No memory\n"); | |
891 | ||
892 | memcpy(&new->event, &b, sizeof(b)); | |
893 | new->dso_name = pos->long_name; | |
894 | new->len = len; | |
895 | ||
896 | list_add_tail(&new->list, head); | |
897 | } | |
898 | ||
899 | return have_buildid; | |
900 | } | |
901 | ||
2643ce11 | 902 | int filename__read_build_id(const char *filename, void *bf, size_t size) |
4d1e00a8 | 903 | { |
2643ce11 | 904 | int fd, err = -1; |
4d1e00a8 ACM |
905 | GElf_Ehdr ehdr; |
906 | GElf_Shdr shdr; | |
907 | Elf_Data *build_id_data; | |
908 | Elf_Scn *sec; | |
4d1e00a8 | 909 | Elf *elf; |
4d1e00a8 | 910 | |
2643ce11 ACM |
911 | if (size < BUILD_ID_SIZE) |
912 | goto out; | |
913 | ||
914 | fd = open(filename, O_RDONLY); | |
4d1e00a8 ACM |
915 | if (fd < 0) |
916 | goto out; | |
917 | ||
84087126 | 918 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
4d1e00a8 | 919 | if (elf == NULL) { |
8d06367f | 920 | pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); |
4d1e00a8 ACM |
921 | goto out_close; |
922 | } | |
923 | ||
924 | if (gelf_getehdr(elf, &ehdr) == NULL) { | |
6beba7ad | 925 | pr_err("%s: cannot get elf header.\n", __func__); |
4d1e00a8 ACM |
926 | goto out_elf_end; |
927 | } | |
928 | ||
2643ce11 ACM |
929 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
930 | ".note.gnu.build-id", NULL); | |
4d1e00a8 ACM |
931 | if (sec == NULL) |
932 | goto out_elf_end; | |
933 | ||
934 | build_id_data = elf_getdata(sec, NULL); | |
935 | if (build_id_data == NULL) | |
936 | goto out_elf_end; | |
2643ce11 ACM |
937 | memcpy(bf, build_id_data->d_buf + 16, BUILD_ID_SIZE); |
938 | err = BUILD_ID_SIZE; | |
939 | out_elf_end: | |
940 | elf_end(elf); | |
941 | out_close: | |
942 | close(fd); | |
943 | out: | |
944 | return err; | |
945 | } | |
946 | ||
947 | static char *dso__read_build_id(struct dso *self) | |
948 | { | |
8d06367f ACM |
949 | int len; |
950 | char *build_id = NULL; | |
951 | unsigned char rawbf[BUILD_ID_SIZE]; | |
2643ce11 ACM |
952 | |
953 | len = filename__read_build_id(self->long_name, rawbf, sizeof(rawbf)); | |
954 | if (len < 0) | |
955 | goto out; | |
956 | ||
957 | build_id = malloc(len * 2 + 1); | |
4d1e00a8 | 958 | if (build_id == NULL) |
2643ce11 | 959 | goto out; |
4d1e00a8 | 960 | |
8d06367f | 961 | build_id__sprintf(rawbf, len, build_id); |
4d1e00a8 ACM |
962 | out: |
963 | return build_id; | |
964 | } | |
965 | ||
94cb9e38 ACM |
966 | char dso__symtab_origin(const struct dso *self) |
967 | { | |
968 | static const char origin[] = { | |
969 | [DSO__ORIG_KERNEL] = 'k', | |
970 | [DSO__ORIG_JAVA_JIT] = 'j', | |
971 | [DSO__ORIG_FEDORA] = 'f', | |
972 | [DSO__ORIG_UBUNTU] = 'u', | |
973 | [DSO__ORIG_BUILDID] = 'b', | |
974 | [DSO__ORIG_DSO] = 'd', | |
439d473b | 975 | [DSO__ORIG_KMODULE] = 'K', |
94cb9e38 ACM |
976 | }; |
977 | ||
978 | if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND) | |
979 | return '!'; | |
980 | return origin[self->origin]; | |
981 | } | |
982 | ||
6beba7ad | 983 | int dso__load(struct dso *self, struct map *map, symbol_filter_t filter) |
a2928c42 | 984 | { |
4d1e00a8 ACM |
985 | int size = PATH_MAX; |
986 | char *name = malloc(size), *build_id = NULL; | |
a2928c42 ACM |
987 | int ret = -1; |
988 | int fd; | |
989 | ||
8d06367f | 990 | self->loaded = 1; |
66bd8424 | 991 | |
a2928c42 ACM |
992 | if (!name) |
993 | return -1; | |
994 | ||
30d7a77d | 995 | self->adjust_symbols = 0; |
f5812a7a | 996 | |
94cb9e38 | 997 | if (strncmp(self->name, "/tmp/perf-", 10) == 0) { |
6beba7ad | 998 | ret = dso__load_perf_map(self, map, filter); |
94cb9e38 ACM |
999 | self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT : |
1000 | DSO__ORIG_NOT_FOUND; | |
1001 | return ret; | |
1002 | } | |
1003 | ||
1004 | self->origin = DSO__ORIG_FEDORA - 1; | |
80d496be | 1005 | |
a2928c42 ACM |
1006 | more: |
1007 | do { | |
8d06367f ACM |
1008 | int berr = 0; |
1009 | ||
94cb9e38 ACM |
1010 | self->origin++; |
1011 | switch (self->origin) { | |
1012 | case DSO__ORIG_FEDORA: | |
439d473b ACM |
1013 | snprintf(name, size, "/usr/lib/debug%s.debug", |
1014 | self->long_name); | |
a2928c42 | 1015 | break; |
94cb9e38 | 1016 | case DSO__ORIG_UBUNTU: |
439d473b ACM |
1017 | snprintf(name, size, "/usr/lib/debug%s", |
1018 | self->long_name); | |
a2928c42 | 1019 | break; |
94cb9e38 | 1020 | case DSO__ORIG_BUILDID: |
6beba7ad | 1021 | build_id = dso__read_build_id(self); |
4d1e00a8 ACM |
1022 | if (build_id != NULL) { |
1023 | snprintf(name, size, | |
1024 | "/usr/lib/debug/.build-id/%.2s/%s.debug", | |
1025 | build_id, build_id + 2); | |
8d06367f | 1026 | goto compare_build_id; |
4d1e00a8 | 1027 | } |
94cb9e38 | 1028 | self->origin++; |
4d1e00a8 | 1029 | /* Fall thru */ |
94cb9e38 | 1030 | case DSO__ORIG_DSO: |
439d473b | 1031 | snprintf(name, size, "%s", self->long_name); |
a2928c42 ACM |
1032 | break; |
1033 | ||
1034 | default: | |
1035 | goto out; | |
1036 | } | |
a2928c42 | 1037 | |
8d06367f ACM |
1038 | if (self->has_build_id) { |
1039 | bool match; | |
1040 | build_id = malloc(BUILD_ID_SIZE); | |
1041 | if (build_id == NULL) | |
1042 | goto more; | |
1043 | berr = filename__read_build_id(name, build_id, | |
1044 | BUILD_ID_SIZE); | |
1045 | compare_build_id: | |
1046 | match = berr > 0 && memcmp(build_id, self->build_id, | |
1047 | sizeof(self->build_id)) == 0; | |
1048 | free(build_id); | |
1049 | build_id = NULL; | |
1050 | if (!match) | |
1051 | goto more; | |
1052 | } | |
1053 | ||
a2928c42 ACM |
1054 | fd = open(name, O_RDONLY); |
1055 | } while (fd < 0); | |
1056 | ||
6beba7ad | 1057 | ret = dso__load_sym(self, map, name, fd, filter, 0, 0); |
a2928c42 ACM |
1058 | close(fd); |
1059 | ||
1060 | /* | |
1061 | * Some people seem to have debuginfo files _WITHOUT_ debug info!?!? | |
1062 | */ | |
1063 | if (!ret) | |
1064 | goto more; | |
1065 | ||
a25e46c4 | 1066 | if (ret > 0) { |
82164161 | 1067 | int nr_plt = dso__synthesize_plt_symbols(self, map, filter); |
a25e46c4 ACM |
1068 | if (nr_plt > 0) |
1069 | ret += nr_plt; | |
1070 | } | |
a2928c42 ACM |
1071 | out: |
1072 | free(name); | |
1340e6bb ACM |
1073 | if (ret < 0 && strstr(self->name, " (deleted)") != NULL) |
1074 | return 0; | |
a2928c42 ACM |
1075 | return ret; |
1076 | } | |
1077 | ||
439d473b ACM |
1078 | struct map *kernel_map; |
1079 | ||
1080 | static void kernel_maps__insert(struct map *map) | |
6cfcc53e | 1081 | { |
439d473b ACM |
1082 | maps__insert(&kernel_maps, map); |
1083 | } | |
6cfcc53e | 1084 | |
439d473b ACM |
1085 | struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp) |
1086 | { | |
439d473b | 1087 | struct map *map = maps__find(&kernel_maps, ip); |
2e538c4a ACM |
1088 | |
1089 | if (mapp) | |
1090 | *mapp = map; | |
439d473b ACM |
1091 | |
1092 | if (map) { | |
1093 | ip = map->map_ip(map, ip); | |
2e538c4a | 1094 | return map->dso->find_symbol(map->dso, ip); |
439d473b | 1095 | } |
6cfcc53e | 1096 | |
2e538c4a | 1097 | return NULL; |
439d473b ACM |
1098 | } |
1099 | ||
1100 | struct map *kernel_maps__find_by_dso_name(const char *name) | |
1101 | { | |
1102 | struct rb_node *nd; | |
1103 | ||
1104 | for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) { | |
1105 | struct map *map = rb_entry(nd, struct map, rb_node); | |
1106 | ||
1107 | if (map->dso && strcmp(map->dso->name, name) == 0) | |
1108 | return map; | |
1109 | } | |
1110 | ||
1111 | return NULL; | |
1112 | } | |
1113 | ||
1114 | static int dso__load_module_sym(struct dso *self, struct map *map, | |
6beba7ad | 1115 | symbol_filter_t filter) |
439d473b ACM |
1116 | { |
1117 | int err = 0, fd = open(self->long_name, O_RDONLY); | |
1118 | ||
8d06367f | 1119 | self->loaded = 1; |
66bd8424 | 1120 | |
439d473b | 1121 | if (fd < 0) { |
6beba7ad | 1122 | pr_err("%s: cannot open %s\n", __func__, self->long_name); |
6cfcc53e | 1123 | return err; |
439d473b | 1124 | } |
6cfcc53e | 1125 | |
6beba7ad | 1126 | err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1); |
6cfcc53e MG |
1127 | close(fd); |
1128 | ||
1129 | return err; | |
1130 | } | |
1131 | ||
6beba7ad | 1132 | static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter) |
6cfcc53e | 1133 | { |
439d473b ACM |
1134 | struct dirent *dent; |
1135 | int nr_symbols = 0, err; | |
1136 | DIR *dir = opendir(dirname); | |
6cfcc53e | 1137 | |
439d473b | 1138 | if (!dir) { |
6beba7ad | 1139 | pr_err("%s: cannot open %s dir\n", __func__, dirname); |
439d473b ACM |
1140 | return -1; |
1141 | } | |
6cfcc53e | 1142 | |
439d473b ACM |
1143 | while ((dent = readdir(dir)) != NULL) { |
1144 | char path[PATH_MAX]; | |
1145 | ||
1146 | if (dent->d_type == DT_DIR) { | |
1147 | if (!strcmp(dent->d_name, ".") || | |
1148 | !strcmp(dent->d_name, "..")) | |
1149 | continue; | |
1150 | ||
1151 | snprintf(path, sizeof(path), "%s/%s", | |
1152 | dirname, dent->d_name); | |
6beba7ad | 1153 | err = dsos__load_modules_sym_dir(path, filter); |
439d473b ACM |
1154 | if (err < 0) |
1155 | goto failure; | |
1156 | } else { | |
1157 | char *dot = strrchr(dent->d_name, '.'), | |
1158 | dso_name[PATH_MAX]; | |
1159 | struct map *map; | |
1160 | struct rb_node *last; | |
1161 | ||
1162 | if (dot == NULL || strcmp(dot, ".ko")) | |
1163 | continue; | |
1164 | snprintf(dso_name, sizeof(dso_name), "[%.*s]", | |
1165 | (int)(dot - dent->d_name), dent->d_name); | |
1166 | ||
a2a99e8e | 1167 | strxfrchar(dso_name, '-', '_'); |
439d473b ACM |
1168 | map = kernel_maps__find_by_dso_name(dso_name); |
1169 | if (map == NULL) | |
1170 | continue; | |
1171 | ||
1172 | snprintf(path, sizeof(path), "%s/%s", | |
1173 | dirname, dent->d_name); | |
1174 | ||
1175 | map->dso->long_name = strdup(path); | |
1176 | if (map->dso->long_name == NULL) | |
1177 | goto failure; | |
1178 | ||
6beba7ad | 1179 | err = dso__load_module_sym(map->dso, map, filter); |
439d473b ACM |
1180 | if (err < 0) |
1181 | goto failure; | |
1182 | last = rb_last(&map->dso->syms); | |
1183 | if (last) { | |
1184 | struct symbol *sym; | |
2e538c4a ACM |
1185 | /* |
1186 | * We do this here as well, even having the | |
1187 | * symbol size found in the symtab because | |
1188 | * misannotated ASM symbols may have the size | |
1189 | * set to zero. | |
1190 | */ | |
1191 | dso__fixup_sym_end(map->dso); | |
1192 | ||
439d473b ACM |
1193 | sym = rb_entry(last, struct symbol, rb_node); |
1194 | map->end = map->start + sym->end; | |
1195 | } | |
1196 | } | |
1197 | nr_symbols += err; | |
1198 | } | |
6cfcc53e | 1199 | |
439d473b ACM |
1200 | return nr_symbols; |
1201 | failure: | |
1202 | closedir(dir); | |
1203 | return -1; | |
1204 | } | |
6cfcc53e | 1205 | |
6beba7ad | 1206 | static int dsos__load_modules_sym(symbol_filter_t filter) |
439d473b ACM |
1207 | { |
1208 | struct utsname uts; | |
1209 | char modules_path[PATH_MAX]; | |
6cfcc53e | 1210 | |
439d473b ACM |
1211 | if (uname(&uts) < 0) |
1212 | return -1; | |
6cfcc53e | 1213 | |
439d473b ACM |
1214 | snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel", |
1215 | uts.release); | |
6cfcc53e | 1216 | |
6beba7ad | 1217 | return dsos__load_modules_sym_dir(modules_path, filter); |
6cfcc53e MG |
1218 | } |
1219 | ||
439d473b ACM |
1220 | /* |
1221 | * Constructor variant for modules (where we know from /proc/modules where | |
1222 | * they are loaded) and for vmlinux, where only after we load all the | |
1223 | * symbols we'll know where it starts and ends. | |
1224 | */ | |
1225 | static struct map *map__new2(u64 start, struct dso *dso) | |
6cfcc53e | 1226 | { |
439d473b | 1227 | struct map *self = malloc(sizeof(*self)); |
6cfcc53e | 1228 | |
439d473b | 1229 | if (self != NULL) { |
439d473b | 1230 | /* |
afb7b4f0 | 1231 | * ->end will be filled after we load all the symbols |
439d473b | 1232 | */ |
afb7b4f0 | 1233 | map__init(self, start, 0, 0, dso); |
439d473b | 1234 | } |
afb7b4f0 | 1235 | |
439d473b ACM |
1236 | return self; |
1237 | } | |
1238 | ||
00a192b3 | 1239 | static int dsos__load_modules(void) |
439d473b ACM |
1240 | { |
1241 | char *line = NULL; | |
1242 | size_t n; | |
1243 | FILE *file = fopen("/proc/modules", "r"); | |
1244 | struct map *map; | |
6cfcc53e | 1245 | |
439d473b ACM |
1246 | if (file == NULL) |
1247 | return -1; | |
6cfcc53e | 1248 | |
439d473b ACM |
1249 | while (!feof(file)) { |
1250 | char name[PATH_MAX]; | |
1251 | u64 start; | |
1252 | struct dso *dso; | |
1253 | char *sep; | |
1254 | int line_len; | |
6cfcc53e | 1255 | |
439d473b ACM |
1256 | line_len = getline(&line, &n, file); |
1257 | if (line_len < 0) | |
1258 | break; | |
1259 | ||
1260 | if (!line) | |
1261 | goto out_failure; | |
1262 | ||
1263 | line[--line_len] = '\0'; /* \n */ | |
1264 | ||
1265 | sep = strrchr(line, 'x'); | |
1266 | if (sep == NULL) | |
1267 | continue; | |
1268 | ||
1269 | hex2u64(sep + 1, &start); | |
1270 | ||
1271 | sep = strchr(line, ' '); | |
1272 | if (sep == NULL) | |
1273 | continue; | |
1274 | ||
1275 | *sep = '\0'; | |
1276 | ||
1277 | snprintf(name, sizeof(name), "[%s]", line); | |
00a192b3 | 1278 | dso = dso__new(name); |
439d473b ACM |
1279 | |
1280 | if (dso == NULL) | |
1281 | goto out_delete_line; | |
1282 | ||
1283 | map = map__new2(start, dso); | |
1284 | if (map == NULL) { | |
1285 | dso__delete(dso); | |
1286 | goto out_delete_line; | |
6cfcc53e | 1287 | } |
439d473b ACM |
1288 | |
1289 | dso->origin = DSO__ORIG_KMODULE; | |
1290 | kernel_maps__insert(map); | |
1291 | dsos__add(dso); | |
6cfcc53e | 1292 | } |
439d473b ACM |
1293 | |
1294 | free(line); | |
1295 | fclose(file); | |
1296 | ||
af427bf5 | 1297 | return 0; |
439d473b ACM |
1298 | |
1299 | out_delete_line: | |
1300 | free(line); | |
1301 | out_failure: | |
1302 | return -1; | |
6cfcc53e MG |
1303 | } |
1304 | ||
439d473b | 1305 | static int dso__load_vmlinux(struct dso *self, struct map *map, |
6beba7ad | 1306 | const char *vmlinux, symbol_filter_t filter) |
a2928c42 ACM |
1307 | { |
1308 | int err, fd = open(vmlinux, O_RDONLY); | |
1309 | ||
8d06367f | 1310 | self->loaded = 1; |
66bd8424 | 1311 | |
a2928c42 ACM |
1312 | if (fd < 0) |
1313 | return -1; | |
1314 | ||
6beba7ad | 1315 | err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0); |
6cfcc53e | 1316 | |
a2928c42 ACM |
1317 | close(fd); |
1318 | ||
1319 | return err; | |
1320 | } | |
1321 | ||
00a192b3 ACM |
1322 | int dsos__load_kernel(const char *vmlinux, symbol_filter_t filter, |
1323 | int use_modules) | |
a827c875 ACM |
1324 | { |
1325 | int err = -1; | |
00a192b3 | 1326 | struct dso *dso = dso__new(vmlinux); |
439d473b ACM |
1327 | |
1328 | if (dso == NULL) | |
1329 | return -1; | |
1330 | ||
1331 | dso->short_name = "[kernel]"; | |
1332 | kernel_map = map__new2(0, dso); | |
1333 | if (kernel_map == NULL) | |
1334 | goto out_delete_dso; | |
1335 | ||
ed52ce2e | 1336 | kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip; |
a827c875 | 1337 | |
00a192b3 | 1338 | if (use_modules && dsos__load_modules() < 0) { |
6beba7ad ACM |
1339 | pr_warning("Failed to load list of modules in use! " |
1340 | "Continuing...\n"); | |
af427bf5 ACM |
1341 | use_modules = 0; |
1342 | } | |
1343 | ||
6cfcc53e | 1344 | if (vmlinux) { |
6beba7ad | 1345 | err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter); |
508c4d08 | 1346 | if (err > 0 && use_modules) { |
6beba7ad | 1347 | int syms = dsos__load_modules_sym(filter); |
508c4d08 | 1348 | |
af427bf5 | 1349 | if (syms < 0) |
6beba7ad ACM |
1350 | pr_warning("Failed to read module symbols!" |
1351 | " Continuing...\n"); | |
af427bf5 ACM |
1352 | else |
1353 | err += syms; | |
508c4d08 | 1354 | } |
6cfcc53e | 1355 | } |
a827c875 | 1356 | |
9974f496 | 1357 | if (err <= 0) |
6beba7ad | 1358 | err = kernel_maps__load_kallsyms(filter, use_modules); |
439d473b ACM |
1359 | |
1360 | if (err > 0) { | |
1361 | struct rb_node *node = rb_first(&dso->syms); | |
1362 | struct symbol *sym = rb_entry(node, struct symbol, rb_node); | |
a827c875 | 1363 | |
439d473b ACM |
1364 | kernel_map->start = sym->start; |
1365 | node = rb_last(&dso->syms); | |
1366 | sym = rb_entry(node, struct symbol, rb_node); | |
1367 | kernel_map->end = sym->end; | |
1368 | ||
1369 | dso->origin = DSO__ORIG_KERNEL; | |
2e538c4a | 1370 | kernel_maps__insert(kernel_map); |
439d473b | 1371 | /* |
2e538c4a ACM |
1372 | * Now that we have all sorted out, just set the ->end of all |
1373 | * maps: | |
439d473b | 1374 | */ |
2e538c4a | 1375 | kernel_maps__fixup_end(); |
439d473b | 1376 | dsos__add(dso); |
af427bf5 | 1377 | |
6beba7ad ACM |
1378 | if (verbose) |
1379 | kernel_maps__fprintf(stderr); | |
439d473b | 1380 | } |
94cb9e38 | 1381 | |
a827c875 | 1382 | return err; |
439d473b ACM |
1383 | |
1384 | out_delete_dso: | |
1385 | dso__delete(dso); | |
1386 | return -1; | |
a827c875 ACM |
1387 | } |
1388 | ||
cd84c2ac | 1389 | LIST_HEAD(dsos); |
cd84c2ac | 1390 | struct dso *vdso; |
cd84c2ac | 1391 | |
83a0944f | 1392 | const char *vmlinux_name = "vmlinux"; |
cd84c2ac FW |
1393 | int modules; |
1394 | ||
1395 | static void dsos__add(struct dso *dso) | |
1396 | { | |
1397 | list_add_tail(&dso->node, &dsos); | |
1398 | } | |
1399 | ||
1400 | static struct dso *dsos__find(const char *name) | |
1401 | { | |
1402 | struct dso *pos; | |
1403 | ||
1404 | list_for_each_entry(pos, &dsos, node) | |
1405 | if (strcmp(pos->name, name) == 0) | |
1406 | return pos; | |
1407 | return NULL; | |
1408 | } | |
1409 | ||
00a192b3 | 1410 | struct dso *dsos__findnew(const char *name) |
cd84c2ac FW |
1411 | { |
1412 | struct dso *dso = dsos__find(name); | |
cd84c2ac | 1413 | |
e4204992 | 1414 | if (!dso) { |
00a192b3 | 1415 | dso = dso__new(name); |
66bd8424 | 1416 | if (dso != NULL) |
e4204992 | 1417 | dsos__add(dso); |
66bd8424 | 1418 | } |
cd84c2ac FW |
1419 | |
1420 | return dso; | |
cd84c2ac FW |
1421 | } |
1422 | ||
1423 | void dsos__fprintf(FILE *fp) | |
1424 | { | |
1425 | struct dso *pos; | |
1426 | ||
1427 | list_for_each_entry(pos, &dsos, node) | |
1428 | dso__fprintf(pos, fp); | |
1429 | } | |
1430 | ||
00a192b3 | 1431 | int load_kernel(symbol_filter_t filter) |
cd84c2ac | 1432 | { |
00a192b3 | 1433 | if (dsos__load_kernel(vmlinux_name, filter, modules) <= 0) |
cd84c2ac FW |
1434 | return -1; |
1435 | ||
00a192b3 | 1436 | vdso = dso__new("[vdso]"); |
cd84c2ac FW |
1437 | if (!vdso) |
1438 | return -1; | |
1439 | ||
cd84c2ac FW |
1440 | dsos__add(vdso); |
1441 | ||
439d473b | 1442 | return 0; |
cd84c2ac FW |
1443 | } |
1444 | ||
00a192b3 | 1445 | void symbol__init(unsigned int priv_size) |
a2928c42 ACM |
1446 | { |
1447 | elf_version(EV_CURRENT); | |
00a192b3 | 1448 | symbol__priv_size = priv_size; |
a2928c42 | 1449 | } |