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