perf symbols: Introduce symbol__elf_init()
[deliverable/linux.git] / tools / perf / util / symbol.c
CommitLineData
5aab621b
ACM
1#include <dirent.h>
2#include <errno.h>
5aab621b
ACM
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <sys/param.h>
9#include <fcntl.h>
10#include <unistd.h>
9486aa38 11#include <inttypes.h>
b36f19d5 12#include "build-id.h"
e334c726 13#include "util.h"
8a6c5b26 14#include "debug.h"
a2928c42 15#include "symbol.h"
5aab621b 16#include "strlist.h"
a2928c42
ACM
17
18#include <libelf.h>
19#include <gelf.h>
20#include <elf.h>
f1617b40 21#include <limits.h>
439d473b 22#include <sys/utsname.h>
2cdbc46d 23
3b01a413 24#ifndef KSYM_NAME_LEN
c752d040 25#define KSYM_NAME_LEN 256
3b01a413
ACM
26#endif
27
c12e15e7
ACM
28#ifndef NT_GNU_BUILD_ID
29#define NT_GNU_BUILD_ID 3
30#endif
31
4dff624a 32static void dso_cache__free(struct rb_root *root);
aeafcbaf 33static bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
21916c38 34static int elf_read_build_id(Elf *elf, void *bf, size_t size);
b0da954a 35static void dsos__add(struct list_head *head, struct dso *dso);
3610583c 36static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
aeafcbaf 37static int dso__load_kernel_sym(struct dso *dso, struct map *map,
9de89fe7 38 symbol_filter_t filter);
aeafcbaf 39static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
a1645ce1 40 symbol_filter_t filter);
cc612d81
ACM
41static int vmlinux_path__nr_entries;
42static char **vmlinux_path;
439d473b 43
75be6cf4 44struct symbol_conf symbol_conf = {
d599db3f 45 .exclude_other = true,
b32d133a
ACM
46 .use_modules = true,
47 .try_vmlinux_path = true,
3e6a2a7f 48 .annotate_src = true,
ec5761ea 49 .symfs = "",
b32d133a
ACM
50};
51
44f24cb3
JO
52static enum dso_binary_type binary_type_symtab[] = {
53 DSO_BINARY_TYPE__KALLSYMS,
54 DSO_BINARY_TYPE__GUEST_KALLSYMS,
55 DSO_BINARY_TYPE__JAVA_JIT,
56 DSO_BINARY_TYPE__DEBUGLINK,
57 DSO_BINARY_TYPE__BUILD_ID_CACHE,
58 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
59 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
60 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
61 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
62 DSO_BINARY_TYPE__GUEST_KMODULE,
63 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
64 DSO_BINARY_TYPE__NOT_FOUND,
65};
66
028df767 67#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
44f24cb3 68
949d160b
JO
69static enum dso_binary_type binary_type_data[] = {
70 DSO_BINARY_TYPE__BUILD_ID_CACHE,
71 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
72 DSO_BINARY_TYPE__NOT_FOUND,
73};
74
028df767 75#define DSO_BINARY_TYPE__DATA_CNT ARRAY_SIZE(binary_type_data)
949d160b 76
aeafcbaf 77int dso__name_len(const struct dso *dso)
8a6c5b26 78{
1e2dd2f7
DM
79 if (!dso)
80 return strlen("[unknown]");
8a6c5b26 81 if (verbose)
aeafcbaf 82 return dso->long_name_len;
8a6c5b26 83
aeafcbaf 84 return dso->short_name_len;
8a6c5b26
ACM
85}
86
aeafcbaf 87bool dso__loaded(const struct dso *dso, enum map_type type)
3610583c 88{
aeafcbaf 89 return dso->loaded & (1 << type);
3610583c
ACM
90}
91
aeafcbaf 92bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
79406cd7 93{
aeafcbaf 94 return dso->sorted_by_name & (1 << type);
79406cd7
ACM
95}
96
aeafcbaf 97static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
79406cd7 98{
aeafcbaf 99 dso->sorted_by_name |= (1 << type);
79406cd7
ACM
100}
101
36a3e646 102bool symbol_type__is_a(char symbol_type, enum map_type map_type)
6893d4ee 103{
31877908
AB
104 symbol_type = toupper(symbol_type);
105
6893d4ee
ACM
106 switch (map_type) {
107 case MAP__FUNCTION:
108 return symbol_type == 'T' || symbol_type == 'W';
f1dfa0b1 109 case MAP__VARIABLE:
31877908 110 return symbol_type == 'D';
6893d4ee
ACM
111 default:
112 return false;
113 }
114}
115
694bf407
AB
116static int prefix_underscores_count(const char *str)
117{
118 const char *tail = str;
119
120 while (*tail == '_')
121 tail++;
122
123 return tail - str;
124}
125
126#define SYMBOL_A 0
127#define SYMBOL_B 1
128
129static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
130{
131 s64 a;
132 s64 b;
133
134 /* Prefer a symbol with non zero length */
135 a = syma->end - syma->start;
136 b = symb->end - symb->start;
137 if ((b == 0) && (a > 0))
138 return SYMBOL_A;
139 else if ((a == 0) && (b > 0))
140 return SYMBOL_B;
141
142 /* Prefer a non weak symbol over a weak one */
143 a = syma->binding == STB_WEAK;
144 b = symb->binding == STB_WEAK;
145 if (b && !a)
146 return SYMBOL_A;
147 if (a && !b)
148 return SYMBOL_B;
149
150 /* Prefer a global symbol over a non global one */
151 a = syma->binding == STB_GLOBAL;
152 b = symb->binding == STB_GLOBAL;
153 if (a && !b)
154 return SYMBOL_A;
155 if (b && !a)
156 return SYMBOL_B;
157
158 /* Prefer a symbol with less underscores */
159 a = prefix_underscores_count(syma->name);
160 b = prefix_underscores_count(symb->name);
161 if (b > a)
162 return SYMBOL_A;
163 else if (a > b)
164 return SYMBOL_B;
165
166 /* If all else fails, choose the symbol with the longest name */
167 if (strlen(syma->name) >= strlen(symb->name))
168 return SYMBOL_A;
169 else
170 return SYMBOL_B;
171}
172
173static void symbols__fixup_duplicate(struct rb_root *symbols)
174{
175 struct rb_node *nd;
176 struct symbol *curr, *next;
177
178 nd = rb_first(symbols);
179
180 while (nd) {
181 curr = rb_entry(nd, struct symbol, rb_node);
182again:
183 nd = rb_next(&curr->rb_node);
184 next = rb_entry(nd, struct symbol, rb_node);
185
186 if (!nd)
187 break;
188
189 if (curr->start != next->start)
190 continue;
191
192 if (choose_best_symbol(curr, next) == SYMBOL_A) {
193 rb_erase(&next->rb_node, symbols);
194 goto again;
195 } else {
196 nd = rb_next(&curr->rb_node);
197 rb_erase(&curr->rb_node, symbols);
198 }
199 }
200}
201
aeafcbaf 202static void symbols__fixup_end(struct rb_root *symbols)
af427bf5 203{
aeafcbaf 204 struct rb_node *nd, *prevnd = rb_first(symbols);
2e538c4a 205 struct symbol *curr, *prev;
af427bf5
ACM
206
207 if (prevnd == NULL)
208 return;
209
2e538c4a
ACM
210 curr = rb_entry(prevnd, struct symbol, rb_node);
211
af427bf5 212 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
2e538c4a
ACM
213 prev = curr;
214 curr = rb_entry(nd, struct symbol, rb_node);
af427bf5 215
3b01a413 216 if (prev->end == prev->start && prev->end != curr->start)
af427bf5 217 prev->end = curr->start - 1;
af427bf5 218 }
2e538c4a
ACM
219
220 /* Last entry */
221 if (curr->end == curr->start)
222 curr->end = roundup(curr->start, 4096);
af427bf5
ACM
223}
224
aeafcbaf 225static void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
af427bf5
ACM
226{
227 struct map *prev, *curr;
aeafcbaf 228 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
af427bf5
ACM
229
230 if (prevnd == NULL)
231 return;
232
233 curr = rb_entry(prevnd, struct map, rb_node);
af427bf5
ACM
234
235 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
236 prev = curr;
237 curr = rb_entry(nd, struct map, rb_node);
238 prev->end = curr->start - 1;
2e538c4a 239 }
90c83218
ACM
240
241 /*
242 * We still haven't the actual symbols, so guess the
243 * last map final address.
244 */
9d1faba5 245 curr->end = ~0ULL;
af427bf5
ACM
246}
247
aeafcbaf 248static void map_groups__fixup_end(struct map_groups *mg)
23ea4a3f
ACM
249{
250 int i;
251 for (i = 0; i < MAP__NR_TYPES; ++i)
aeafcbaf 252 __map_groups__fixup_end(mg, i);
23ea4a3f
ACM
253}
254
c408fedf
ACM
255static struct symbol *symbol__new(u64 start, u64 len, u8 binding,
256 const char *name)
a2928c42 257{
0085c954 258 size_t namelen = strlen(name) + 1;
aeafcbaf
ACM
259 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
260 sizeof(*sym) + namelen));
261 if (sym == NULL)
0b73da3f
IM
262 return NULL;
263
75be6cf4 264 if (symbol_conf.priv_size)
aeafcbaf 265 sym = ((void *)sym) + symbol_conf.priv_size;
e4204992 266
aeafcbaf
ACM
267 sym->start = start;
268 sym->end = len ? start + len - 1 : start;
269 sym->binding = binding;
270 sym->namelen = namelen - 1;
e4204992 271
aeafcbaf
ACM
272 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
273 __func__, name, start, sym->end);
274 memcpy(sym->name, name, namelen);
a2928c42 275
aeafcbaf 276 return sym;
a2928c42
ACM
277}
278
aeafcbaf 279void symbol__delete(struct symbol *sym)
a2928c42 280{
aeafcbaf 281 free(((void *)sym) - symbol_conf.priv_size);
a2928c42
ACM
282}
283
aeafcbaf 284static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
a2928c42 285{
9486aa38 286 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
aeafcbaf
ACM
287 sym->start, sym->end,
288 sym->binding == STB_GLOBAL ? 'g' :
289 sym->binding == STB_LOCAL ? 'l' : 'w',
290 sym->name);
a2928c42
ACM
291}
292
a978f2ab
AN
293size_t symbol__fprintf_symname_offs(const struct symbol *sym,
294 const struct addr_location *al, FILE *fp)
547a92e0 295{
a978f2ab
AN
296 unsigned long offset;
297 size_t length;
298
299 if (sym && sym->name) {
300 length = fprintf(fp, "%s", sym->name);
301 if (al) {
302 offset = al->addr - sym->start;
303 length += fprintf(fp, "+0x%lx", offset);
304 }
305 return length;
306 } else
307 return fprintf(fp, "[unknown]");
308}
547a92e0 309
a978f2ab
AN
310size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
311{
312 return symbol__fprintf_symname_offs(sym, NULL, fp);
547a92e0
AN
313}
314
aeafcbaf 315void dso__set_long_name(struct dso *dso, char *name)
cfc10d3b 316{
ef6ae724
ACM
317 if (name == NULL)
318 return;
aeafcbaf
ACM
319 dso->long_name = name;
320 dso->long_name_len = strlen(name);
cfc10d3b
ACM
321}
322
aeafcbaf 323static void dso__set_short_name(struct dso *dso, const char *name)
b63be8d7
ACM
324{
325 if (name == NULL)
326 return;
aeafcbaf
ACM
327 dso->short_name = name;
328 dso->short_name_len = strlen(name);
b63be8d7
ACM
329}
330
aeafcbaf 331static void dso__set_basename(struct dso *dso)
cfc10d3b 332{
aeafcbaf 333 dso__set_short_name(dso, basename(dso->long_name));
cfc10d3b
ACM
334}
335
00a192b3 336struct dso *dso__new(const char *name)
a2928c42 337{
aeafcbaf 338 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
a2928c42 339
aeafcbaf 340 if (dso != NULL) {
6a4694a4 341 int i;
aeafcbaf
ACM
342 strcpy(dso->name, name);
343 dso__set_long_name(dso, dso->name);
344 dso__set_short_name(dso, dso->name);
6a4694a4 345 for (i = 0; i < MAP__NR_TYPES; ++i)
aeafcbaf 346 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
4dff624a 347 dso->cache = RB_ROOT;
44f24cb3 348 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
949d160b 349 dso->data_type = DSO_BINARY_TYPE__NOT_FOUND;
aeafcbaf
ACM
350 dso->loaded = 0;
351 dso->sorted_by_name = 0;
352 dso->has_build_id = 0;
353 dso->kernel = DSO_TYPE_USER;
8db4841f 354 dso->needs_swap = DSO_SWAP__UNSET;
aeafcbaf 355 INIT_LIST_HEAD(&dso->node);
a2928c42
ACM
356 }
357
aeafcbaf 358 return dso;
a2928c42
ACM
359}
360
aeafcbaf 361static void symbols__delete(struct rb_root *symbols)
a2928c42
ACM
362{
363 struct symbol *pos;
aeafcbaf 364 struct rb_node *next = rb_first(symbols);
a2928c42
ACM
365
366 while (next) {
367 pos = rb_entry(next, struct symbol, rb_node);
368 next = rb_next(&pos->rb_node);
aeafcbaf 369 rb_erase(&pos->rb_node, symbols);
00a192b3 370 symbol__delete(pos);
a2928c42
ACM
371 }
372}
373
aeafcbaf 374void dso__delete(struct dso *dso)
a2928c42 375{
6a4694a4
ACM
376 int i;
377 for (i = 0; i < MAP__NR_TYPES; ++i)
aeafcbaf
ACM
378 symbols__delete(&dso->symbols[i]);
379 if (dso->sname_alloc)
380 free((char *)dso->short_name);
381 if (dso->lname_alloc)
382 free(dso->long_name);
4dff624a 383 dso_cache__free(&dso->cache);
aeafcbaf 384 free(dso);
a2928c42
ACM
385}
386
aeafcbaf 387void dso__set_build_id(struct dso *dso, void *build_id)
8d06367f 388{
aeafcbaf
ACM
389 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
390 dso->has_build_id = 1;
8d06367f
ACM
391}
392
aeafcbaf 393static void symbols__insert(struct rb_root *symbols, struct symbol *sym)
a2928c42 394{
aeafcbaf 395 struct rb_node **p = &symbols->rb_node;
a2928c42 396 struct rb_node *parent = NULL;
9cffa8d5 397 const u64 ip = sym->start;
a2928c42
ACM
398 struct symbol *s;
399
400 while (*p != NULL) {
401 parent = *p;
402 s = rb_entry(parent, struct symbol, rb_node);
403 if (ip < s->start)
404 p = &(*p)->rb_left;
405 else
406 p = &(*p)->rb_right;
407 }
408 rb_link_node(&sym->rb_node, parent, p);
aeafcbaf 409 rb_insert_color(&sym->rb_node, symbols);
a2928c42
ACM
410}
411
aeafcbaf 412static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
a2928c42
ACM
413{
414 struct rb_node *n;
415
aeafcbaf 416 if (symbols == NULL)
a2928c42
ACM
417 return NULL;
418
aeafcbaf 419 n = symbols->rb_node;
a2928c42
ACM
420
421 while (n) {
422 struct symbol *s = rb_entry(n, struct symbol, rb_node);
423
424 if (ip < s->start)
425 n = n->rb_left;
426 else if (ip > s->end)
427 n = n->rb_right;
428 else
429 return s;
430 }
431
432 return NULL;
433}
434
79406cd7
ACM
435struct symbol_name_rb_node {
436 struct rb_node rb_node;
437 struct symbol sym;
438};
439
aeafcbaf 440static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
79406cd7 441{
aeafcbaf 442 struct rb_node **p = &symbols->rb_node;
79406cd7 443 struct rb_node *parent = NULL;
02a9d037
RV
444 struct symbol_name_rb_node *symn, *s;
445
446 symn = container_of(sym, struct symbol_name_rb_node, sym);
79406cd7
ACM
447
448 while (*p != NULL) {
449 parent = *p;
450 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
451 if (strcmp(sym->name, s->sym.name) < 0)
452 p = &(*p)->rb_left;
453 else
454 p = &(*p)->rb_right;
455 }
456 rb_link_node(&symn->rb_node, parent, p);
aeafcbaf 457 rb_insert_color(&symn->rb_node, symbols);
79406cd7
ACM
458}
459
aeafcbaf
ACM
460static void symbols__sort_by_name(struct rb_root *symbols,
461 struct rb_root *source)
79406cd7
ACM
462{
463 struct rb_node *nd;
464
465 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
466 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
aeafcbaf 467 symbols__insert_by_name(symbols, pos);
79406cd7
ACM
468 }
469}
470
aeafcbaf
ACM
471static struct symbol *symbols__find_by_name(struct rb_root *symbols,
472 const char *name)
79406cd7
ACM
473{
474 struct rb_node *n;
475
aeafcbaf 476 if (symbols == NULL)
79406cd7
ACM
477 return NULL;
478
aeafcbaf 479 n = symbols->rb_node;
79406cd7
ACM
480
481 while (n) {
482 struct symbol_name_rb_node *s;
483 int cmp;
484
485 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
486 cmp = strcmp(name, s->sym.name);
487
488 if (cmp < 0)
489 n = n->rb_left;
490 else if (cmp > 0)
491 n = n->rb_right;
492 else
493 return &s->sym;
494 }
495
496 return NULL;
497}
498
aeafcbaf 499struct symbol *dso__find_symbol(struct dso *dso,
79406cd7 500 enum map_type type, u64 addr)
fcf1203a 501{
aeafcbaf 502 return symbols__find(&dso->symbols[type], addr);
fcf1203a
ACM
503}
504
aeafcbaf 505struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
79406cd7
ACM
506 const char *name)
507{
aeafcbaf 508 return symbols__find_by_name(&dso->symbol_names[type], name);
79406cd7
ACM
509}
510
aeafcbaf 511void dso__sort_by_name(struct dso *dso, enum map_type type)
79406cd7 512{
aeafcbaf
ACM
513 dso__set_sorted_by_name(dso, type);
514 return symbols__sort_by_name(&dso->symbol_names[type],
515 &dso->symbols[type]);
79406cd7
ACM
516}
517
aeafcbaf 518int build_id__sprintf(const u8 *build_id, int len, char *bf)
a2928c42 519{
8d06367f 520 char *bid = bf;
aeafcbaf 521 const u8 *raw = build_id;
8d06367f 522 int i;
a2928c42 523
8d06367f
ACM
524 for (i = 0; i < len; ++i) {
525 sprintf(bid, "%02x", *raw);
526 ++raw;
527 bid += 2;
528 }
529
aeafcbaf 530 return raw - build_id;
8d06367f
ACM
531}
532
aeafcbaf 533size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
8d06367f
ACM
534{
535 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
8d06367f 536
aeafcbaf 537 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
9e03eb2d
ACM
538 return fprintf(fp, "%s", sbuild_id);
539}
540
aeafcbaf
ACM
541size_t dso__fprintf_symbols_by_name(struct dso *dso,
542 enum map_type type, FILE *fp)
90f18e63
SD
543{
544 size_t ret = 0;
545 struct rb_node *nd;
546 struct symbol_name_rb_node *pos;
547
aeafcbaf 548 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
90f18e63
SD
549 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
550 fprintf(fp, "%s\n", pos->sym.name);
551 }
552
553 return ret;
554}
555
aeafcbaf 556size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
9e03eb2d
ACM
557{
558 struct rb_node *nd;
aeafcbaf 559 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
9e03eb2d 560
aeafcbaf
ACM
561 if (dso->short_name != dso->long_name)
562 ret += fprintf(fp, "%s, ", dso->long_name);
3846df2e 563 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
aeafcbaf
ACM
564 dso->loaded ? "" : "NOT ");
565 ret += dso__fprintf_buildid(dso, fp);
6a4694a4 566 ret += fprintf(fp, ")\n");
aeafcbaf 567 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
95011c60
ACM
568 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
569 ret += symbol__fprintf(pos, fp);
a2928c42
ACM
570 }
571
572 return ret;
573}
574
9e201442
ACM
575int kallsyms__parse(const char *filename, void *arg,
576 int (*process_symbol)(void *arg, const char *name,
3b01a413 577 char type, u64 start, u64 end))
a2928c42 578{
a2928c42
ACM
579 char *line = NULL;
580 size_t n;
3b01a413 581 int err = -1;
9e201442 582 FILE *file = fopen(filename, "r");
a2928c42
ACM
583
584 if (file == NULL)
585 goto out_failure;
586
3b01a413
ACM
587 err = 0;
588
a2928c42 589 while (!feof(file)) {
9cffa8d5 590 u64 start;
a2928c42
ACM
591 int line_len, len;
592 char symbol_type;
2e538c4a 593 char *symbol_name;
a2928c42
ACM
594
595 line_len = getline(&line, &n, file);
a1645ce1 596 if (line_len < 0 || !line)
a2928c42
ACM
597 break;
598
a2928c42
ACM
599 line[--line_len] = '\0'; /* \n */
600
a0055ae2 601 len = hex2u64(line, &start);
a2928c42
ACM
602
603 len++;
604 if (len + 2 >= line_len)
605 continue;
606
31877908 607 symbol_type = line[len];
3b01a413
ACM
608 len += 2;
609 symbol_name = line + len;
610 len = line_len - len;
682b335a 611
3b01a413
ACM
612 if (len >= KSYM_NAME_LEN) {
613 err = -1;
682b335a 614 break;
3b01a413
ACM
615 }
616
3f5a4272
AB
617 /*
618 * module symbols are not sorted so we add all
619 * symbols with zero length and rely on
620 * symbols__fixup_end() to fix it up.
621 */
622 err = process_symbol(arg, symbol_name,
623 symbol_type, start, start);
624 if (err)
625 break;
2e538c4a
ACM
626 }
627
628 free(line);
629 fclose(file);
682b335a 630 return err;
2e538c4a 631
2e538c4a
ACM
632out_failure:
633 return -1;
634}
635
682b335a
ACM
636struct process_kallsyms_args {
637 struct map *map;
638 struct dso *dso;
639};
640
c408fedf
ACM
641static u8 kallsyms2elf_type(char type)
642{
643 if (type == 'W')
644 return STB_WEAK;
645
646 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
647}
648
682b335a 649static int map__process_kallsym_symbol(void *arg, const char *name,
3b01a413 650 char type, u64 start, u64 end)
682b335a
ACM
651{
652 struct symbol *sym;
653 struct process_kallsyms_args *a = arg;
654 struct rb_root *root = &a->dso->symbols[a->map->type];
655
656 if (!symbol_type__is_a(type, a->map->type))
657 return 0;
658
3b01a413
ACM
659 sym = symbol__new(start, end - start + 1,
660 kallsyms2elf_type(type), name);
682b335a
ACM
661 if (sym == NULL)
662 return -ENOMEM;
663 /*
664 * We will pass the symbols to the filter later, in
665 * map__split_kallsyms, when we have split the maps per module
666 */
667 symbols__insert(root, sym);
a1645ce1 668
682b335a
ACM
669 return 0;
670}
671
672/*
673 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
674 * so that we can in the next step set the symbol ->end address and then
675 * call kernel_maps__split_kallsyms.
676 */
aeafcbaf 677static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
9e201442 678 struct map *map)
682b335a 679{
aeafcbaf 680 struct process_kallsyms_args args = { .map = map, .dso = dso, };
9e201442 681 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
682b335a
ACM
682}
683
2e538c4a
ACM
684/*
685 * Split the symbols into maps, making sure there are no overlaps, i.e. the
686 * kernel range is broken in several maps, named [kernel].N, as we don't have
687 * the original ELF section names vmlinux have.
688 */
aeafcbaf 689static int dso__split_kallsyms(struct dso *dso, struct map *map,
9de89fe7 690 symbol_filter_t filter)
2e538c4a 691{
9de89fe7 692 struct map_groups *kmaps = map__kmap(map)->kmaps;
23346f21 693 struct machine *machine = kmaps->machine;
4e06255f 694 struct map *curr_map = map;
2e538c4a 695 struct symbol *pos;
8a953312 696 int count = 0, moved = 0;
aeafcbaf 697 struct rb_root *root = &dso->symbols[map->type];
4e06255f 698 struct rb_node *next = rb_first(root);
2e538c4a
ACM
699 int kernel_range = 0;
700
701 while (next) {
702 char *module;
703
704 pos = rb_entry(next, struct symbol, rb_node);
705 next = rb_next(&pos->rb_node);
706
707 module = strchr(pos->name, '\t');
708 if (module) {
75be6cf4 709 if (!symbol_conf.use_modules)
1de8e245
ACM
710 goto discard_symbol;
711
2e538c4a
ACM
712 *module++ = '\0';
713
b7cece76 714 if (strcmp(curr_map->dso->short_name, module)) {
a1645ce1 715 if (curr_map != map &&
aeafcbaf 716 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
23346f21 717 machine__is_default_guest(machine)) {
a1645ce1
ZY
718 /*
719 * We assume all symbols of a module are
720 * continuous in * kallsyms, so curr_map
721 * points to a module and all its
722 * symbols are in its kmap. Mark it as
723 * loaded.
724 */
725 dso__set_loaded(curr_map->dso,
726 curr_map->type);
727 }
728
729 curr_map = map_groups__find_by_name(kmaps,
730 map->type, module);
4e06255f 731 if (curr_map == NULL) {
2f51903b 732 pr_debug("%s/proc/{kallsyms,modules} "
b7cece76 733 "inconsistency while looking "
a1645ce1 734 "for \"%s\" module!\n",
23346f21 735 machine->root_dir, module);
a1645ce1
ZY
736 curr_map = map;
737 goto discard_symbol;
af427bf5 738 }
b7cece76 739
a1645ce1 740 if (curr_map->dso->loaded &&
23346f21 741 !machine__is_default_guest(machine))
b7cece76 742 goto discard_symbol;
af427bf5 743 }
2e538c4a
ACM
744 /*
745 * So that we look just like we get from .ko files,
746 * i.e. not prelinked, relative to map->start.
747 */
4e06255f
ACM
748 pos->start = curr_map->map_ip(curr_map, pos->start);
749 pos->end = curr_map->map_ip(curr_map, pos->end);
750 } else if (curr_map != map) {
2e538c4a 751 char dso_name[PATH_MAX];
aeafcbaf 752 struct dso *ndso;
2e538c4a 753
8a953312
ACM
754 if (count == 0) {
755 curr_map = map;
756 goto filter_symbol;
757 }
758
aeafcbaf 759 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
a1645ce1
ZY
760 snprintf(dso_name, sizeof(dso_name),
761 "[guest.kernel].%d",
762 kernel_range++);
763 else
764 snprintf(dso_name, sizeof(dso_name),
765 "[kernel].%d",
766 kernel_range++);
2e538c4a 767
aeafcbaf
ACM
768 ndso = dso__new(dso_name);
769 if (ndso == NULL)
2e538c4a
ACM
770 return -1;
771
aeafcbaf 772 ndso->kernel = dso->kernel;
a1645ce1 773
aeafcbaf 774 curr_map = map__new2(pos->start, ndso, map->type);
37fe5fcb 775 if (curr_map == NULL) {
aeafcbaf 776 dso__delete(ndso);
2e538c4a
ACM
777 return -1;
778 }
a2928c42 779
4e06255f 780 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
9de89fe7 781 map_groups__insert(kmaps, curr_map);
2e538c4a
ACM
782 ++kernel_range;
783 }
8a953312 784filter_symbol:
4e06255f 785 if (filter && filter(curr_map, pos)) {
1de8e245 786discard_symbol: rb_erase(&pos->rb_node, root);
00a192b3 787 symbol__delete(pos);
2e538c4a 788 } else {
4e06255f
ACM
789 if (curr_map != map) {
790 rb_erase(&pos->rb_node, root);
791 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
8a953312
ACM
792 ++moved;
793 } else
794 ++count;
9974f496 795 }
a2928c42
ACM
796 }
797
a1645ce1 798 if (curr_map != map &&
aeafcbaf 799 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
23346f21 800 machine__is_default_guest(kmaps->machine)) {
a1645ce1
ZY
801 dso__set_loaded(curr_map->dso, curr_map->type);
802 }
803
8a953312 804 return count + moved;
2e538c4a 805}
a2928c42 806
ec80fde7
ACM
807static bool symbol__restricted_filename(const char *filename,
808 const char *restricted_filename)
809{
810 bool restricted = false;
811
812 if (symbol_conf.kptr_restrict) {
813 char *r = realpath(filename, NULL);
814
815 if (r != NULL) {
816 restricted = strcmp(r, restricted_filename) == 0;
817 free(r);
818 return restricted;
819 }
820 }
821
822 return restricted;
823}
824
aeafcbaf 825int dso__load_kallsyms(struct dso *dso, const char *filename,
9de89fe7 826 struct map *map, symbol_filter_t filter)
2e538c4a 827{
ec80fde7
ACM
828 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
829 return -1;
830
aeafcbaf 831 if (dso__load_all_kallsyms(dso, filename, map) < 0)
2e538c4a
ACM
832 return -1;
833
694bf407 834 symbols__fixup_duplicate(&dso->symbols[map->type]);
3f5a4272
AB
835 symbols__fixup_end(&dso->symbols[map->type]);
836
aeafcbaf 837 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
44f24cb3 838 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
a1645ce1 839 else
44f24cb3 840 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
2e538c4a 841
aeafcbaf 842 return dso__split_kallsyms(dso, map, filter);
af427bf5
ACM
843}
844
aeafcbaf 845static int dso__load_perf_map(struct dso *dso, struct map *map,
6beba7ad 846 symbol_filter_t filter)
80d496be
PE
847{
848 char *line = NULL;
849 size_t n;
850 FILE *file;
851 int nr_syms = 0;
852
aeafcbaf 853 file = fopen(dso->long_name, "r");
80d496be
PE
854 if (file == NULL)
855 goto out_failure;
856
857 while (!feof(file)) {
9cffa8d5 858 u64 start, size;
80d496be
PE
859 struct symbol *sym;
860 int line_len, len;
861
862 line_len = getline(&line, &n, file);
863 if (line_len < 0)
864 break;
865
866 if (!line)
867 goto out_failure;
868
869 line[--line_len] = '\0'; /* \n */
870
871 len = hex2u64(line, &start);
872
873 len++;
874 if (len + 2 >= line_len)
875 continue;
876
877 len += hex2u64(line + len, &size);
878
879 len++;
880 if (len + 2 >= line_len)
881 continue;
882
c408fedf 883 sym = symbol__new(start, size, STB_GLOBAL, line + len);
80d496be
PE
884
885 if (sym == NULL)
886 goto out_delete_line;
887
439d473b 888 if (filter && filter(map, sym))
00a192b3 889 symbol__delete(sym);
80d496be 890 else {
aeafcbaf 891 symbols__insert(&dso->symbols[map->type], sym);
80d496be
PE
892 nr_syms++;
893 }
894 }
895
896 free(line);
897 fclose(file);
898
899 return nr_syms;
900
901out_delete_line:
902 free(line);
903out_failure:
904 return -1;
905}
906
a2928c42
ACM
907/**
908 * elf_symtab__for_each_symbol - iterate thru all the symbols
909 *
aeafcbaf 910 * @syms: struct elf_symtab instance to iterate
83a0944f 911 * @idx: uint32_t idx
a2928c42
ACM
912 * @sym: GElf_Sym iterator
913 */
83a0944f
IM
914#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
915 for (idx = 0, gelf_getsym(syms, idx, &sym);\
916 idx < nr_syms; \
917 idx++, gelf_getsym(syms, idx, &sym))
a2928c42
ACM
918
919static inline uint8_t elf_sym__type(const GElf_Sym *sym)
920{
921 return GELF_ST_TYPE(sym->st_info);
922}
923
924static inline int elf_sym__is_function(const GElf_Sym *sym)
925{
926 return elf_sym__type(sym) == STT_FUNC &&
927 sym->st_name != 0 &&
81833130 928 sym->st_shndx != SHN_UNDEF;
a2928c42
ACM
929}
930
f1dfa0b1
ACM
931static inline bool elf_sym__is_object(const GElf_Sym *sym)
932{
933 return elf_sym__type(sym) == STT_OBJECT &&
934 sym->st_name != 0 &&
935 sym->st_shndx != SHN_UNDEF;
936}
937
6cfcc53e
MG
938static inline int elf_sym__is_label(const GElf_Sym *sym)
939{
940 return elf_sym__type(sym) == STT_NOTYPE &&
941 sym->st_name != 0 &&
942 sym->st_shndx != SHN_UNDEF &&
943 sym->st_shndx != SHN_ABS;
944}
945
946static inline const char *elf_sec__name(const GElf_Shdr *shdr,
947 const Elf_Data *secstrs)
948{
949 return secstrs->d_buf + shdr->sh_name;
950}
951
952static inline int elf_sec__is_text(const GElf_Shdr *shdr,
953 const Elf_Data *secstrs)
954{
955 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
956}
957
f1dfa0b1
ACM
958static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
959 const Elf_Data *secstrs)
960{
961 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
962}
963
a2928c42
ACM
964static inline const char *elf_sym__name(const GElf_Sym *sym,
965 const Elf_Data *symstrs)
966{
967 return symstrs->d_buf + sym->st_name;
968}
969
970static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
971 GElf_Shdr *shp, const char *name,
83a0944f 972 size_t *idx)
a2928c42
ACM
973{
974 Elf_Scn *sec = NULL;
975 size_t cnt = 1;
976
977 while ((sec = elf_nextscn(elf, sec)) != NULL) {
978 char *str;
979
980 gelf_getshdr(sec, shp);
981 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
982 if (!strcmp(name, str)) {
83a0944f
IM
983 if (idx)
984 *idx = cnt;
a2928c42
ACM
985 break;
986 }
987 ++cnt;
988 }
989
990 return sec;
991}
992
8ce998d6
ACM
993#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
994 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
995 idx < nr_entries; \
996 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
997
998#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
999 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
1000 idx < nr_entries; \
1001 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
1002
a25e46c4
ACM
1003/*
1004 * We need to check if we have a .dynsym, so that we can handle the
1005 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
1006 * .dynsym or .symtab).
1007 * And always look at the original dso, not at debuginfo packages, that
1008 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
1009 */
33ff581e
JO
1010static int
1011dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
1012 symbol_filter_t filter)
8ce998d6
ACM
1013{
1014 uint32_t nr_rel_entries, idx;
1015 GElf_Sym sym;
9cffa8d5 1016 u64 plt_offset;
8ce998d6
ACM
1017 GElf_Shdr shdr_plt;
1018 struct symbol *f;
a25e46c4 1019 GElf_Shdr shdr_rel_plt, shdr_dynsym;
8ce998d6 1020 Elf_Data *reldata, *syms, *symstrs;
a25e46c4
ACM
1021 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
1022 size_t dynsym_idx;
1023 GElf_Ehdr ehdr;
8ce998d6 1024 char sympltname[1024];
a25e46c4
ACM
1025 Elf *elf;
1026 int nr = 0, symidx, fd, err = 0;
1027
ec5761ea 1028 fd = open(name, O_RDONLY);
a25e46c4
ACM
1029 if (fd < 0)
1030 goto out;
1031
84087126 1032 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a25e46c4
ACM
1033 if (elf == NULL)
1034 goto out_close;
1035
1036 if (gelf_getehdr(elf, &ehdr) == NULL)
1037 goto out_elf_end;
1038
1039 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
1040 ".dynsym", &dynsym_idx);
1041 if (scn_dynsym == NULL)
1042 goto out_elf_end;
8ce998d6 1043
a25e46c4 1044 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
1045 ".rela.plt", NULL);
1046 if (scn_plt_rel == NULL) {
a25e46c4 1047 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
1048 ".rel.plt", NULL);
1049 if (scn_plt_rel == NULL)
a25e46c4 1050 goto out_elf_end;
8ce998d6
ACM
1051 }
1052
a25e46c4
ACM
1053 err = -1;
1054
8ce998d6 1055 if (shdr_rel_plt.sh_link != dynsym_idx)
a25e46c4 1056 goto out_elf_end;
8ce998d6 1057
a25e46c4
ACM
1058 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
1059 goto out_elf_end;
8ce998d6
ACM
1060
1061 /*
83a0944f 1062 * Fetch the relocation section to find the idxes to the GOT
8ce998d6
ACM
1063 * and the symbols in the .dynsym they refer to.
1064 */
1065 reldata = elf_getdata(scn_plt_rel, NULL);
1066 if (reldata == NULL)
a25e46c4 1067 goto out_elf_end;
8ce998d6
ACM
1068
1069 syms = elf_getdata(scn_dynsym, NULL);
1070 if (syms == NULL)
a25e46c4 1071 goto out_elf_end;
8ce998d6 1072
a25e46c4 1073 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
8ce998d6 1074 if (scn_symstrs == NULL)
a25e46c4 1075 goto out_elf_end;
8ce998d6
ACM
1076
1077 symstrs = elf_getdata(scn_symstrs, NULL);
1078 if (symstrs == NULL)
a25e46c4 1079 goto out_elf_end;
8ce998d6
ACM
1080
1081 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
1082 plt_offset = shdr_plt.sh_offset;
1083
1084 if (shdr_rel_plt.sh_type == SHT_RELA) {
1085 GElf_Rela pos_mem, *pos;
1086
1087 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
1088 nr_rel_entries) {
1089 symidx = GELF_R_SYM(pos->r_info);
1090 plt_offset += shdr_plt.sh_entsize;
1091 gelf_getsym(syms, symidx, &sym);
1092 snprintf(sympltname, sizeof(sympltname),
1093 "%s@plt", elf_sym__name(&sym, symstrs));
1094
1095 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
c408fedf 1096 STB_GLOBAL, sympltname);
8ce998d6 1097 if (!f)
a25e46c4 1098 goto out_elf_end;
8ce998d6 1099
82164161
ACM
1100 if (filter && filter(map, f))
1101 symbol__delete(f);
1102 else {
aeafcbaf 1103 symbols__insert(&dso->symbols[map->type], f);
82164161
ACM
1104 ++nr;
1105 }
8ce998d6
ACM
1106 }
1107 } else if (shdr_rel_plt.sh_type == SHT_REL) {
1108 GElf_Rel pos_mem, *pos;
1109 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
1110 nr_rel_entries) {
1111 symidx = GELF_R_SYM(pos->r_info);
1112 plt_offset += shdr_plt.sh_entsize;
1113 gelf_getsym(syms, symidx, &sym);
1114 snprintf(sympltname, sizeof(sympltname),
1115 "%s@plt", elf_sym__name(&sym, symstrs));
1116
1117 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
c408fedf 1118 STB_GLOBAL, sympltname);
8ce998d6 1119 if (!f)
a25e46c4 1120 goto out_elf_end;
8ce998d6 1121
82164161
ACM
1122 if (filter && filter(map, f))
1123 symbol__delete(f);
1124 else {
aeafcbaf 1125 symbols__insert(&dso->symbols[map->type], f);
82164161
ACM
1126 ++nr;
1127 }
8ce998d6 1128 }
8ce998d6
ACM
1129 }
1130
a25e46c4
ACM
1131 err = 0;
1132out_elf_end:
1133 elf_end(elf);
1134out_close:
1135 close(fd);
1136
1137 if (err == 0)
1138 return nr;
1139out:
fe2197b8 1140 pr_debug("%s: problems reading %s PLT info.\n",
aeafcbaf 1141 __func__, dso->long_name);
a25e46c4 1142 return 0;
8ce998d6
ACM
1143}
1144
aeafcbaf 1145static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
d45868d3
ACM
1146{
1147 switch (type) {
1148 case MAP__FUNCTION:
aeafcbaf 1149 return elf_sym__is_function(sym);
f1dfa0b1 1150 case MAP__VARIABLE:
aeafcbaf 1151 return elf_sym__is_object(sym);
d45868d3
ACM
1152 default:
1153 return false;
1154 }
1155}
1156
aeafcbaf
ACM
1157static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
1158 enum map_type type)
d45868d3
ACM
1159{
1160 switch (type) {
1161 case MAP__FUNCTION:
aeafcbaf 1162 return elf_sec__is_text(shdr, secstrs);
f1dfa0b1 1163 case MAP__VARIABLE:
aeafcbaf 1164 return elf_sec__is_data(shdr, secstrs);
d45868d3
ACM
1165 default:
1166 return false;
1167 }
1168}
1169
70c3856b
EM
1170static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
1171{
1172 Elf_Scn *sec = NULL;
1173 GElf_Shdr shdr;
1174 size_t cnt = 1;
1175
1176 while ((sec = elf_nextscn(elf, sec)) != NULL) {
1177 gelf_getshdr(sec, &shdr);
1178
1179 if ((addr >= shdr.sh_addr) &&
1180 (addr < (shdr.sh_addr + shdr.sh_size)))
1181 return cnt;
1182
1183 ++cnt;
1184 }
1185
1186 return -1;
1187}
1188
8db4841f
JO
1189static int dso__swap_init(struct dso *dso, unsigned char eidata)
1190{
1191 static unsigned int const endian = 1;
1192
1193 dso->needs_swap = DSO_SWAP__NO;
1194
1195 switch (eidata) {
1196 case ELFDATA2LSB:
1197 /* We are big endian, DSO is little endian. */
1198 if (*(unsigned char const *)&endian != 1)
1199 dso->needs_swap = DSO_SWAP__YES;
1200 break;
1201
1202 case ELFDATA2MSB:
1203 /* We are little endian, DSO is big endian. */
1204 if (*(unsigned char const *)&endian != 0)
1205 dso->needs_swap = DSO_SWAP__YES;
1206 break;
1207
1208 default:
1209 pr_err("unrecognized DSO data encoding %d\n", eidata);
1210 return -EINVAL;
1211 }
1212
1213 return 0;
1214}
1215
aeafcbaf 1216static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
6da80ce8
DM
1217 int fd, symbol_filter_t filter, int kmodule,
1218 int want_symtab)
a2928c42 1219{
aeafcbaf 1220 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
2e538c4a 1221 struct map *curr_map = map;
aeafcbaf 1222 struct dso *curr_dso = dso;
6cfcc53e 1223 Elf_Data *symstrs, *secstrs;
a2928c42
ACM
1224 uint32_t nr_syms;
1225 int err = -1;
83a0944f 1226 uint32_t idx;
a2928c42 1227 GElf_Ehdr ehdr;
70c3856b
EM
1228 GElf_Shdr shdr, opdshdr;
1229 Elf_Data *syms, *opddata = NULL;
a2928c42 1230 GElf_Sym sym;
70c3856b 1231 Elf_Scn *sec, *sec_strndx, *opdsec;
a2928c42 1232 Elf *elf;
439d473b 1233 int nr = 0;
70c3856b 1234 size_t opdidx = 0;
a2928c42 1235
84087126 1236 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a2928c42 1237 if (elf == NULL) {
8b1389ef 1238 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
a2928c42
ACM
1239 goto out_close;
1240 }
1241
1242 if (gelf_getehdr(elf, &ehdr) == NULL) {
8b1389ef 1243 pr_debug("%s: cannot get elf header.\n", __func__);
a2928c42
ACM
1244 goto out_elf_end;
1245 }
1246
8db4841f
JO
1247 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
1248 goto out_elf_end;
1249
6da80ce8 1250 /* Always reject images with a mismatched build-id: */
aeafcbaf 1251 if (dso->has_build_id) {
21916c38
DM
1252 u8 build_id[BUILD_ID_SIZE];
1253
be96ea8f 1254 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
21916c38
DM
1255 goto out_elf_end;
1256
aeafcbaf 1257 if (!dso__build_id_equal(dso, build_id))
21916c38
DM
1258 goto out_elf_end;
1259 }
1260
a2928c42 1261 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
8ce998d6 1262 if (sec == NULL) {
6da80ce8
DM
1263 if (want_symtab)
1264 goto out_elf_end;
1265
a25e46c4
ACM
1266 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
1267 if (sec == NULL)
8ce998d6 1268 goto out_elf_end;
8ce998d6 1269 }
a2928c42 1270
70c3856b 1271 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
adb09184
AB
1272 if (opdshdr.sh_type != SHT_PROGBITS)
1273 opdsec = NULL;
70c3856b
EM
1274 if (opdsec)
1275 opddata = elf_rawdata(opdsec, NULL);
1276
a2928c42
ACM
1277 syms = elf_getdata(sec, NULL);
1278 if (syms == NULL)
1279 goto out_elf_end;
1280
1281 sec = elf_getscn(elf, shdr.sh_link);
1282 if (sec == NULL)
1283 goto out_elf_end;
1284
1285 symstrs = elf_getdata(sec, NULL);
1286 if (symstrs == NULL)
1287 goto out_elf_end;
1288
6cfcc53e
MG
1289 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1290 if (sec_strndx == NULL)
1291 goto out_elf_end;
1292
1293 secstrs = elf_getdata(sec_strndx, NULL);
9b30a26b 1294 if (secstrs == NULL)
6cfcc53e
MG
1295 goto out_elf_end;
1296
a2928c42
ACM
1297 nr_syms = shdr.sh_size / shdr.sh_entsize;
1298
e9fbc9dc 1299 memset(&sym, 0, sizeof(sym));
aeafcbaf
ACM
1300 if (dso->kernel == DSO_TYPE_USER) {
1301 dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
30d7a77d
ACM
1302 elf_section_by_name(elf, &ehdr, &shdr,
1303 ".gnu.prelink_undo",
1304 NULL) != NULL);
aeafcbaf
ACM
1305 } else {
1306 dso->adjust_symbols = 0;
1307 }
83a0944f 1308 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
a2928c42 1309 struct symbol *f;
56b03f3c 1310 const char *elf_name = elf_sym__name(&sym, symstrs);
2e538c4a 1311 char *demangled = NULL;
6cfcc53e
MG
1312 int is_label = elf_sym__is_label(&sym);
1313 const char *section_name;
a2928c42 1314
9de89fe7
ACM
1315 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1316 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1317 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
56b03f3c 1318
d45868d3 1319 if (!is_label && !elf_sym__is_a(&sym, map->type))
a2928c42
ACM
1320 continue;
1321
696b97a5
DM
1322 /* Reject ARM ELF "mapping symbols": these aren't unique and
1323 * don't identify functions, so will confuse the profile
1324 * output: */
1325 if (ehdr.e_machine == EM_ARM) {
1326 if (!strcmp(elf_name, "$a") ||
1327 !strcmp(elf_name, "$d") ||
1328 !strcmp(elf_name, "$t"))
1329 continue;
1330 }
1331
70c3856b
EM
1332 if (opdsec && sym.st_shndx == opdidx) {
1333 u32 offset = sym.st_value - opdshdr.sh_addr;
1334 u64 *opd = opddata->d_buf + offset;
8db4841f 1335 sym.st_value = DSO__SWAP(dso, u64, *opd);
70c3856b
EM
1336 sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1337 }
1338
a2928c42
ACM
1339 sec = elf_getscn(elf, sym.st_shndx);
1340 if (!sec)
1341 goto out_elf_end;
1342
1343 gelf_getshdr(sec, &shdr);
6cfcc53e 1344
d45868d3 1345 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
6cfcc53e
MG
1346 continue;
1347
1348 section_name = elf_sec__name(&shdr, secstrs);
0b73da3f 1349
b2f8fb23
DDAG
1350 /* On ARM, symbols for thumb functions have 1 added to
1351 * the symbol address as a flag - remove it */
1352 if ((ehdr.e_machine == EM_ARM) &&
1353 (map->type == MAP__FUNCTION) &&
1354 (sym.st_value & 1))
1355 --sym.st_value;
1356
aeafcbaf 1357 if (dso->kernel != DSO_TYPE_USER || kmodule) {
2e538c4a
ACM
1358 char dso_name[PATH_MAX];
1359
1360 if (strcmp(section_name,
b63be8d7 1361 (curr_dso->short_name +
aeafcbaf 1362 dso->short_name_len)) == 0)
2e538c4a
ACM
1363 goto new_symbol;
1364
1365 if (strcmp(section_name, ".text") == 0) {
1366 curr_map = map;
aeafcbaf 1367 curr_dso = dso;
2e538c4a
ACM
1368 goto new_symbol;
1369 }
1370
1371 snprintf(dso_name, sizeof(dso_name),
aeafcbaf 1372 "%s%s", dso->short_name, section_name);
2e538c4a 1373
9de89fe7 1374 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
2e538c4a
ACM
1375 if (curr_map == NULL) {
1376 u64 start = sym.st_value;
1377
1378 if (kmodule)
1379 start += map->start + shdr.sh_offset;
1380
00a192b3 1381 curr_dso = dso__new(dso_name);
2e538c4a
ACM
1382 if (curr_dso == NULL)
1383 goto out_elf_end;
aeafcbaf
ACM
1384 curr_dso->kernel = dso->kernel;
1385 curr_dso->long_name = dso->long_name;
1386 curr_dso->long_name_len = dso->long_name_len;
3610583c 1387 curr_map = map__new2(start, curr_dso,
6275ce2d 1388 map->type);
2e538c4a
ACM
1389 if (curr_map == NULL) {
1390 dso__delete(curr_dso);
1391 goto out_elf_end;
1392 }
ed52ce2e
ACM
1393 curr_map->map_ip = identity__map_ip;
1394 curr_map->unmap_ip = identity__map_ip;
aeafcbaf 1395 curr_dso->symtab_type = dso->symtab_type;
9de89fe7 1396 map_groups__insert(kmap->kmaps, curr_map);
aeafcbaf 1397 dsos__add(&dso->node, curr_dso);
6275ce2d 1398 dso__set_loaded(curr_dso, map->type);
2e538c4a
ACM
1399 } else
1400 curr_dso = curr_map->dso;
1401
1402 goto new_symbol;
af427bf5
ACM
1403 }
1404
2e538c4a 1405 if (curr_dso->adjust_symbols) {
9486aa38
ACM
1406 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1407 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
29a9f66d
ACM
1408 (u64)sym.st_value, (u64)shdr.sh_addr,
1409 (u64)shdr.sh_offset);
f5812a7a 1410 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
af427bf5 1411 }
28ac909b
ACM
1412 /*
1413 * We need to figure out if the object was created from C++ sources
1414 * DWARF DW_compile_unit has this, but we don't always have access
1415 * to it...
1416 */
83a0944f 1417 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
28ac909b 1418 if (demangled != NULL)
83a0944f 1419 elf_name = demangled;
2e538c4a 1420new_symbol:
c408fedf
ACM
1421 f = symbol__new(sym.st_value, sym.st_size,
1422 GELF_ST_BIND(sym.st_info), elf_name);
28ac909b 1423 free(demangled);
a2928c42
ACM
1424 if (!f)
1425 goto out_elf_end;
1426
2e538c4a 1427 if (filter && filter(curr_map, f))
00a192b3 1428 symbol__delete(f);
69ee69f6 1429 else {
6a4694a4 1430 symbols__insert(&curr_dso->symbols[curr_map->type], f);
69ee69f6
ACM
1431 nr++;
1432 }
a2928c42
ACM
1433 }
1434
2e538c4a
ACM
1435 /*
1436 * For misannotated, zeroed, ASM function sizes.
1437 */
6275ce2d 1438 if (nr > 0) {
694bf407 1439 symbols__fixup_duplicate(&dso->symbols[map->type]);
aeafcbaf 1440 symbols__fixup_end(&dso->symbols[map->type]);
6275ce2d
ACM
1441 if (kmap) {
1442 /*
1443 * We need to fixup this here too because we create new
1444 * maps here, for things like vsyscall sections.
1445 */
1446 __map_groups__fixup_end(kmap->kmaps, map->type);
1447 }
1448 }
a2928c42
ACM
1449 err = nr;
1450out_elf_end:
1451 elf_end(elf);
1452out_close:
1453 return err;
1454}
1455
166ccc9c
NK
1456void symbol__elf_init(void)
1457{
1458 elf_version(EV_CURRENT);
1459}
1460
aeafcbaf 1461static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
78075caa 1462{
aeafcbaf 1463 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
78075caa
ACM
1464}
1465
a1645ce1 1466bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
57f395a7 1467{
e30a3d12 1468 bool have_build_id = false;
57f395a7
FW
1469 struct dso *pos;
1470
6122e4e4
ACM
1471 list_for_each_entry(pos, head, node) {
1472 if (with_hits && !pos->hit)
1473 continue;
f6e1467d
ACM
1474 if (pos->has_build_id) {
1475 have_build_id = true;
1476 continue;
1477 }
e30a3d12
ACM
1478 if (filename__read_build_id(pos->long_name, pos->build_id,
1479 sizeof(pos->build_id)) > 0) {
1480 have_build_id = true;
1481 pos->has_build_id = true;
1482 }
6122e4e4 1483 }
57f395a7 1484
e30a3d12 1485 return have_build_id;
57f395a7
FW
1486}
1487
fd7a346e
ACM
1488/*
1489 * Align offset to 4 bytes as needed for note name and descriptor data.
1490 */
1491#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1492
21916c38 1493static int elf_read_build_id(Elf *elf, void *bf, size_t size)
4d1e00a8 1494{
21916c38 1495 int err = -1;
4d1e00a8
ACM
1496 GElf_Ehdr ehdr;
1497 GElf_Shdr shdr;
fd7a346e 1498 Elf_Data *data;
4d1e00a8 1499 Elf_Scn *sec;
e57cfcda 1500 Elf_Kind ek;
fd7a346e 1501 void *ptr;
4d1e00a8 1502
2643ce11
ACM
1503 if (size < BUILD_ID_SIZE)
1504 goto out;
1505
e57cfcda
PE
1506 ek = elf_kind(elf);
1507 if (ek != ELF_K_ELF)
21916c38 1508 goto out;
e57cfcda 1509
4d1e00a8 1510 if (gelf_getehdr(elf, &ehdr) == NULL) {
6beba7ad 1511 pr_err("%s: cannot get elf header.\n", __func__);
21916c38 1512 goto out;
4d1e00a8
ACM
1513 }
1514
1388d715
JO
1515 /*
1516 * Check following sections for notes:
1517 * '.note.gnu.build-id'
1518 * '.notes'
1519 * '.note' (VDSO specific)
1520 */
1521 do {
1522 sec = elf_section_by_name(elf, &ehdr, &shdr,
1523 ".note.gnu.build-id", NULL);
1524 if (sec)
1525 break;
1526
fd7a346e
ACM
1527 sec = elf_section_by_name(elf, &ehdr, &shdr,
1528 ".notes", NULL);
1388d715
JO
1529 if (sec)
1530 break;
1531
1532 sec = elf_section_by_name(elf, &ehdr, &shdr,
1533 ".note", NULL);
1534 if (sec)
1535 break;
1536
1537 return err;
1538
1539 } while (0);
4d1e00a8 1540
fd7a346e
ACM
1541 data = elf_getdata(sec, NULL);
1542 if (data == NULL)
21916c38 1543 goto out;
fd7a346e
ACM
1544
1545 ptr = data->d_buf;
1546 while (ptr < (data->d_buf + data->d_size)) {
1547 GElf_Nhdr *nhdr = ptr;
be96ea8f
SE
1548 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
1549 descsz = NOTE_ALIGN(nhdr->n_descsz);
fd7a346e
ACM
1550 const char *name;
1551
1552 ptr += sizeof(*nhdr);
1553 name = ptr;
1554 ptr += namesz;
1555 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1556 nhdr->n_namesz == sizeof("GNU")) {
1557 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
be96ea8f
SE
1558 size_t sz = min(size, descsz);
1559 memcpy(bf, ptr, sz);
1560 memset(bf + sz, 0, size - sz);
1561 err = descsz;
fd7a346e
ACM
1562 break;
1563 }
1564 }
1565 ptr += descsz;
1566 }
21916c38
DM
1567
1568out:
1569 return err;
1570}
1571
1572int filename__read_build_id(const char *filename, void *bf, size_t size)
1573{
1574 int fd, err = -1;
1575 Elf *elf;
1576
1577 if (size < BUILD_ID_SIZE)
1578 goto out;
1579
1580 fd = open(filename, O_RDONLY);
1581 if (fd < 0)
1582 goto out;
1583
1584 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1585 if (elf == NULL) {
1586 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1587 goto out_close;
1588 }
1589
1590 err = elf_read_build_id(elf, bf, size);
1591
2643ce11
ACM
1592 elf_end(elf);
1593out_close:
1594 close(fd);
1595out:
1596 return err;
1597}
1598
f1617b40
ACM
1599int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1600{
1601 int fd, err = -1;
1602
1603 if (size < BUILD_ID_SIZE)
1604 goto out;
1605
1606 fd = open(filename, O_RDONLY);
1607 if (fd < 0)
1608 goto out;
1609
1610 while (1) {
1611 char bf[BUFSIZ];
1612 GElf_Nhdr nhdr;
be96ea8f 1613 size_t namesz, descsz;
f1617b40
ACM
1614
1615 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1616 break;
1617
fd7a346e
ACM
1618 namesz = NOTE_ALIGN(nhdr.n_namesz);
1619 descsz = NOTE_ALIGN(nhdr.n_descsz);
f1617b40
ACM
1620 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1621 nhdr.n_namesz == sizeof("GNU")) {
be96ea8f 1622 if (read(fd, bf, namesz) != (ssize_t)namesz)
f1617b40
ACM
1623 break;
1624 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
be96ea8f
SE
1625 size_t sz = min(descsz, size);
1626 if (read(fd, build_id, sz) == (ssize_t)sz) {
1627 memset(build_id + sz, 0, size - sz);
f1617b40
ACM
1628 err = 0;
1629 break;
1630 }
be96ea8f 1631 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
f1617b40
ACM
1632 break;
1633 } else {
1634 int n = namesz + descsz;
1635 if (read(fd, bf, n) != n)
1636 break;
1637 }
1638 }
1639 close(fd);
1640out:
1641 return err;
1642}
1643
209bd9e3
PLG
1644static int filename__read_debuglink(const char *filename,
1645 char *debuglink, size_t size)
1646{
1647 int fd, err = -1;
1648 Elf *elf;
1649 GElf_Ehdr ehdr;
1650 GElf_Shdr shdr;
1651 Elf_Data *data;
1652 Elf_Scn *sec;
1653 Elf_Kind ek;
1654
1655 fd = open(filename, O_RDONLY);
1656 if (fd < 0)
1657 goto out;
1658
1659 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1660 if (elf == NULL) {
1661 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1662 goto out_close;
1663 }
1664
1665 ek = elf_kind(elf);
1666 if (ek != ELF_K_ELF)
1667 goto out_close;
1668
1669 if (gelf_getehdr(elf, &ehdr) == NULL) {
1670 pr_err("%s: cannot get elf header.\n", __func__);
1671 goto out_close;
1672 }
1673
1674 sec = elf_section_by_name(elf, &ehdr, &shdr,
1675 ".gnu_debuglink", NULL);
1676 if (sec == NULL)
1677 goto out_close;
1678
1679 data = elf_getdata(sec, NULL);
1680 if (data == NULL)
1681 goto out_close;
1682
1683 /* the start of this section is a zero-terminated string */
1684 strncpy(debuglink, data->d_buf, size);
1685
1686 elf_end(elf);
1687
1688out_close:
1689 close(fd);
1690out:
1691 return err;
1692}
1693
aeafcbaf 1694char dso__symtab_origin(const struct dso *dso)
94cb9e38
ACM
1695{
1696 static const char origin[] = {
44f24cb3
JO
1697 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
1698 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
1699 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
1700 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
1701 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
1702 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
1703 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
1704 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
1705 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
1706 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
1707 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
94cb9e38
ACM
1708 };
1709
44f24cb3 1710 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
94cb9e38 1711 return '!';
aeafcbaf 1712 return origin[dso->symtab_type];
94cb9e38
ACM
1713}
1714
44f24cb3
JO
1715int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
1716 char *root_dir, char *file, size_t size)
1717{
1718 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1719 int ret = 0;
1720
1721 switch (type) {
1722 case DSO_BINARY_TYPE__DEBUGLINK: {
1723 char *debuglink;
1724
1725 strncpy(file, dso->long_name, size);
1726 debuglink = file + dso->long_name_len;
1727 while (debuglink != file && *debuglink != '/')
1728 debuglink--;
1729 if (*debuglink == '/')
1730 debuglink++;
1731 filename__read_debuglink(dso->long_name, debuglink,
1732 size - (debuglink - file));
1733 }
1734 break;
1735 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1736 /* skip the locally configured cache if a symfs is given */
1737 if (symbol_conf.symfs[0] ||
1738 (dso__build_id_filename(dso, file, size) == NULL))
1739 ret = -1;
1740 break;
1741
1742 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1743 snprintf(file, size, "%s/usr/lib/debug%s.debug",
1744 symbol_conf.symfs, dso->long_name);
1745 break;
1746
1747 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1748 snprintf(file, size, "%s/usr/lib/debug%s",
1749 symbol_conf.symfs, dso->long_name);
1750 break;
1751
1752 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1753 if (!dso->has_build_id) {
1754 ret = -1;
1755 break;
1756 }
1757
1758 build_id__sprintf(dso->build_id,
1759 sizeof(dso->build_id),
1760 build_id_hex);
1761 snprintf(file, size,
1762 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
1763 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
1764 break;
1765
1766 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1767 snprintf(file, size, "%s%s",
1768 symbol_conf.symfs, dso->long_name);
1769 break;
1770
1771 case DSO_BINARY_TYPE__GUEST_KMODULE:
1772 snprintf(file, size, "%s%s%s", symbol_conf.symfs,
1773 root_dir, dso->long_name);
1774 break;
1775
1776 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1777 snprintf(file, size, "%s%s", symbol_conf.symfs,
1778 dso->long_name);
1779 break;
1780
1781 default:
1782 case DSO_BINARY_TYPE__KALLSYMS:
1783 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1784 case DSO_BINARY_TYPE__JAVA_JIT:
1785 case DSO_BINARY_TYPE__NOT_FOUND:
1786 ret = -1;
1787 break;
1788 }
1789
1790 return ret;
1791}
1792
aeafcbaf 1793int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
a2928c42 1794{
c338aee8 1795 char *name;
a2928c42
ACM
1796 int ret = -1;
1797 int fd;
44f24cb3 1798 u_int i;
23346f21 1799 struct machine *machine;
44f24cb3 1800 char *root_dir = (char *) "";
6da80ce8 1801 int want_symtab;
a2928c42 1802
aeafcbaf 1803 dso__set_loaded(dso, map->type);
66bd8424 1804
aeafcbaf
ACM
1805 if (dso->kernel == DSO_TYPE_KERNEL)
1806 return dso__load_kernel_sym(dso, map, filter);
1807 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1808 return dso__load_guest_kernel_sym(dso, map, filter);
a1645ce1 1809
23346f21
ACM
1810 if (map->groups && map->groups->machine)
1811 machine = map->groups->machine;
a1645ce1 1812 else
23346f21 1813 machine = NULL;
c338aee8 1814
44f24cb3 1815 name = malloc(PATH_MAX);
a2928c42
ACM
1816 if (!name)
1817 return -1;
1818
aeafcbaf 1819 dso->adjust_symbols = 0;
f5812a7a 1820
aeafcbaf 1821 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
981c1252
PE
1822 struct stat st;
1823
e9b52ef2 1824 if (lstat(dso->name, &st) < 0)
981c1252
PE
1825 return -1;
1826
1827 if (st.st_uid && (st.st_uid != geteuid())) {
1828 pr_warning("File %s not owned by current user or root, "
1829 "ignoring it.\n", dso->name);
1830 return -1;
1831 }
1832
aeafcbaf 1833 ret = dso__load_perf_map(dso, map, filter);
44f24cb3
JO
1834 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1835 DSO_BINARY_TYPE__NOT_FOUND;
94cb9e38
ACM
1836 return ret;
1837 }
1838
44f24cb3
JO
1839 if (machine)
1840 root_dir = machine->root_dir;
1841
6da80ce8
DM
1842 /* Iterate over candidate debug images.
1843 * On the first pass, only load images if they have a full symtab.
1844 * Failing that, do a second pass where we accept .dynsym also
1845 */
60e4b10c
ACM
1846 want_symtab = 1;
1847restart:
44f24cb3 1848 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
6da80ce8 1849
44f24cb3 1850 dso->symtab_type = binary_type_symtab[i];
ec5761ea 1851
44f24cb3
JO
1852 if (dso__binary_type_file(dso, dso->symtab_type,
1853 root_dir, name, PATH_MAX))
1854 continue;
6da80ce8
DM
1855
1856 /* Name is now the name of the next image to try */
a2928c42 1857 fd = open(name, O_RDONLY);
6da80ce8
DM
1858 if (fd < 0)
1859 continue;
a2928c42 1860
aeafcbaf 1861 ret = dso__load_sym(dso, map, name, fd, filter, 0,
6da80ce8
DM
1862 want_symtab);
1863 close(fd);
a2928c42 1864
6da80ce8
DM
1865 /*
1866 * Some people seem to have debuginfo files _WITHOUT_ debug
1867 * info!?!?
1868 */
1869 if (!ret)
1870 continue;
a2928c42 1871
6da80ce8 1872 if (ret > 0) {
33ff581e
JO
1873 int nr_plt;
1874
1875 nr_plt = dso__synthesize_plt_symbols(dso, name, map, filter);
6da80ce8
DM
1876 if (nr_plt > 0)
1877 ret += nr_plt;
1878 break;
1879 }
a25e46c4 1880 }
6da80ce8 1881
60e4b10c
ACM
1882 /*
1883 * If we wanted a full symtab but no image had one,
1884 * relax our requirements and repeat the search.
1885 */
1886 if (ret <= 0 && want_symtab) {
1887 want_symtab = 0;
1888 goto restart;
1889 }
1890
a2928c42 1891 free(name);
aeafcbaf 1892 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1340e6bb 1893 return 0;
a2928c42
ACM
1894 return ret;
1895}
1896
aeafcbaf 1897struct map *map_groups__find_by_name(struct map_groups *mg,
79406cd7 1898 enum map_type type, const char *name)
439d473b
ACM
1899{
1900 struct rb_node *nd;
1901
aeafcbaf 1902 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
439d473b
ACM
1903 struct map *map = rb_entry(nd, struct map, rb_node);
1904
b7cece76 1905 if (map->dso && strcmp(map->dso->short_name, name) == 0)
439d473b
ACM
1906 return map;
1907 }
1908
1909 return NULL;
1910}
1911
aeafcbaf
ACM
1912static int dso__kernel_module_get_build_id(struct dso *dso,
1913 const char *root_dir)
b7cece76
ACM
1914{
1915 char filename[PATH_MAX];
1916 /*
1917 * kernel module short names are of the form "[module]" and
1918 * we need just "module" here.
1919 */
aeafcbaf 1920 const char *name = dso->short_name + 1;
b7cece76
ACM
1921
1922 snprintf(filename, sizeof(filename),
a1645ce1
ZY
1923 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1924 root_dir, (int)strlen(name) - 1, name);
b7cece76 1925
aeafcbaf
ACM
1926 if (sysfs__read_build_id(filename, dso->build_id,
1927 sizeof(dso->build_id)) == 0)
1928 dso->has_build_id = true;
b7cece76
ACM
1929
1930 return 0;
1931}
1932
aeafcbaf 1933static int map_groups__set_modules_path_dir(struct map_groups *mg,
a1645ce1 1934 const char *dir_name)
6cfcc53e 1935{
439d473b 1936 struct dirent *dent;
5aab621b 1937 DIR *dir = opendir(dir_name);
74534341 1938 int ret = 0;
6cfcc53e 1939
439d473b 1940 if (!dir) {
5aab621b 1941 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
439d473b
ACM
1942 return -1;
1943 }
6cfcc53e 1944
439d473b
ACM
1945 while ((dent = readdir(dir)) != NULL) {
1946 char path[PATH_MAX];
a1645ce1
ZY
1947 struct stat st;
1948
1949 /*sshfs might return bad dent->d_type, so we have to stat*/
2b600f95 1950 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
a1645ce1
ZY
1951 if (stat(path, &st))
1952 continue;
439d473b 1953
a1645ce1 1954 if (S_ISDIR(st.st_mode)) {
439d473b
ACM
1955 if (!strcmp(dent->d_name, ".") ||
1956 !strcmp(dent->d_name, ".."))
1957 continue;
1958
aeafcbaf 1959 ret = map_groups__set_modules_path_dir(mg, path);
74534341
GJ
1960 if (ret < 0)
1961 goto out;
439d473b
ACM
1962 } else {
1963 char *dot = strrchr(dent->d_name, '.'),
1964 dso_name[PATH_MAX];
1965 struct map *map;
cfc10d3b 1966 char *long_name;
439d473b
ACM
1967
1968 if (dot == NULL || strcmp(dot, ".ko"))
1969 continue;
1970 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1971 (int)(dot - dent->d_name), dent->d_name);
1972
a2a99e8e 1973 strxfrchar(dso_name, '-', '_');
aeafcbaf
ACM
1974 map = map_groups__find_by_name(mg, MAP__FUNCTION,
1975 dso_name);
439d473b
ACM
1976 if (map == NULL)
1977 continue;
1978
cfc10d3b 1979 long_name = strdup(path);
74534341
GJ
1980 if (long_name == NULL) {
1981 ret = -1;
1982 goto out;
1983 }
cfc10d3b 1984 dso__set_long_name(map->dso, long_name);
6e406257 1985 map->dso->lname_alloc = 1;
a1645ce1 1986 dso__kernel_module_get_build_id(map->dso, "");
439d473b 1987 }
439d473b 1988 }
6cfcc53e 1989
74534341 1990out:
439d473b 1991 closedir(dir);
74534341 1992 return ret;
439d473b 1993}
6cfcc53e 1994
a1645ce1 1995static char *get_kernel_version(const char *root_dir)
439d473b 1996{
a1645ce1
ZY
1997 char version[PATH_MAX];
1998 FILE *file;
1999 char *name, *tmp;
2000 const char *prefix = "Linux version ";
2001
2002 sprintf(version, "%s/proc/version", root_dir);
2003 file = fopen(version, "r");
2004 if (!file)
2005 return NULL;
2006
2007 version[0] = '\0';
2008 tmp = fgets(version, sizeof(version), file);
2009 fclose(file);
2010
2011 name = strstr(version, prefix);
2012 if (!name)
2013 return NULL;
2014 name += strlen(prefix);
2015 tmp = strchr(name, ' ');
2016 if (tmp)
2017 *tmp = '\0';
2018
2019 return strdup(name);
2020}
2021
aeafcbaf 2022static int machine__set_modules_path(struct machine *machine)
a1645ce1
ZY
2023{
2024 char *version;
439d473b 2025 char modules_path[PATH_MAX];
6cfcc53e 2026
aeafcbaf 2027 version = get_kernel_version(machine->root_dir);
a1645ce1 2028 if (!version)
439d473b 2029 return -1;
6cfcc53e 2030
a1645ce1 2031 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
aeafcbaf 2032 machine->root_dir, version);
a1645ce1 2033 free(version);
6cfcc53e 2034
aeafcbaf 2035 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
6cfcc53e
MG
2036}
2037
439d473b
ACM
2038/*
2039 * Constructor variant for modules (where we know from /proc/modules where
2040 * they are loaded) and for vmlinux, where only after we load all the
2041 * symbols we'll know where it starts and ends.
2042 */
3610583c 2043static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
6cfcc53e 2044{
aeafcbaf
ACM
2045 struct map *map = calloc(1, (sizeof(*map) +
2046 (dso->kernel ? sizeof(struct kmap) : 0)));
2047 if (map != NULL) {
439d473b 2048 /*
afb7b4f0 2049 * ->end will be filled after we load all the symbols
439d473b 2050 */
aeafcbaf 2051 map__init(map, type, start, 0, 0, dso);
439d473b 2052 }
afb7b4f0 2053
aeafcbaf 2054 return map;
439d473b
ACM
2055}
2056
aeafcbaf 2057struct map *machine__new_module(struct machine *machine, u64 start,
d28c6223 2058 const char *filename)
b7cece76
ACM
2059{
2060 struct map *map;
aeafcbaf 2061 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
b7cece76
ACM
2062
2063 if (dso == NULL)
2064 return NULL;
2065
2066 map = map__new2(start, dso, MAP__FUNCTION);
2067 if (map == NULL)
2068 return NULL;
2069
aeafcbaf 2070 if (machine__is_host(machine))
44f24cb3 2071 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
a1645ce1 2072 else
44f24cb3 2073 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
aeafcbaf 2074 map_groups__insert(&machine->kmaps, map);
b7cece76
ACM
2075 return map;
2076}
2077
aeafcbaf 2078static int machine__create_modules(struct machine *machine)
439d473b
ACM
2079{
2080 char *line = NULL;
2081 size_t n;
a1645ce1 2082 FILE *file;
439d473b 2083 struct map *map;
a1645ce1
ZY
2084 const char *modules;
2085 char path[PATH_MAX];
2086
aeafcbaf 2087 if (machine__is_default_guest(machine))
a1645ce1
ZY
2088 modules = symbol_conf.default_guest_modules;
2089 else {
aeafcbaf 2090 sprintf(path, "%s/proc/modules", machine->root_dir);
a1645ce1
ZY
2091 modules = path;
2092 }
6cfcc53e 2093
ec80fde7
ACM
2094 if (symbol__restricted_filename(path, "/proc/modules"))
2095 return -1;
2096
a1645ce1 2097 file = fopen(modules, "r");
439d473b
ACM
2098 if (file == NULL)
2099 return -1;
6cfcc53e 2100
439d473b
ACM
2101 while (!feof(file)) {
2102 char name[PATH_MAX];
2103 u64 start;
439d473b
ACM
2104 char *sep;
2105 int line_len;
6cfcc53e 2106
439d473b
ACM
2107 line_len = getline(&line, &n, file);
2108 if (line_len < 0)
2109 break;
2110
2111 if (!line)
2112 goto out_failure;
2113
2114 line[--line_len] = '\0'; /* \n */
2115
2116 sep = strrchr(line, 'x');
2117 if (sep == NULL)
2118 continue;
2119
2120 hex2u64(sep + 1, &start);
2121
2122 sep = strchr(line, ' ');
2123 if (sep == NULL)
2124 continue;
2125
2126 *sep = '\0';
2127
2128 snprintf(name, sizeof(name), "[%s]", line);
aeafcbaf 2129 map = machine__new_module(machine, start, name);
b7cece76 2130 if (map == NULL)
439d473b 2131 goto out_delete_line;
aeafcbaf 2132 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
6cfcc53e 2133 }
439d473b
ACM
2134
2135 free(line);
2136 fclose(file);
2137
aeafcbaf 2138 return machine__set_modules_path(machine);
439d473b
ACM
2139
2140out_delete_line:
2141 free(line);
2142out_failure:
2143 return -1;
6cfcc53e
MG
2144}
2145
aeafcbaf 2146int dso__load_vmlinux(struct dso *dso, struct map *map,
fd930ff9 2147 const char *vmlinux, symbol_filter_t filter)
a2928c42 2148{
fbd733b8 2149 int err = -1, fd;
ec5761ea 2150 char symfs_vmlinux[PATH_MAX];
a2928c42 2151
a639dc64 2152 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
ec5761ea
DA
2153 symbol_conf.symfs, vmlinux);
2154 fd = open(symfs_vmlinux, O_RDONLY);
a2928c42
ACM
2155 if (fd < 0)
2156 return -1;
2157
aeafcbaf
ACM
2158 dso__set_long_name(dso, (char *)vmlinux);
2159 dso__set_loaded(dso, map->type);
2160 err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
a2928c42
ACM
2161 close(fd);
2162
3846df2e 2163 if (err > 0)
ec5761ea 2164 pr_debug("Using %s for symbols\n", symfs_vmlinux);
3846df2e 2165
a2928c42
ACM
2166 return err;
2167}
2168
aeafcbaf 2169int dso__load_vmlinux_path(struct dso *dso, struct map *map,
9de89fe7 2170 symbol_filter_t filter)
a19afe46
ACM
2171{
2172 int i, err = 0;
5ad90e4e 2173 char *filename;
a19afe46
ACM
2174
2175 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
5ad90e4e
ACM
2176 vmlinux_path__nr_entries + 1);
2177
aeafcbaf 2178 filename = dso__build_id_filename(dso, NULL, 0);
5ad90e4e 2179 if (filename != NULL) {
aeafcbaf 2180 err = dso__load_vmlinux(dso, map, filename, filter);
5ad90e4e 2181 if (err > 0) {
aeafcbaf 2182 dso__set_long_name(dso, filename);
5ad90e4e
ACM
2183 goto out;
2184 }
2185 free(filename);
2186 }
a19afe46
ACM
2187
2188 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
aeafcbaf 2189 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
a19afe46 2190 if (err > 0) {
aeafcbaf 2191 dso__set_long_name(dso, strdup(vmlinux_path[i]));
a19afe46
ACM
2192 break;
2193 }
2194 }
5ad90e4e 2195out:
a19afe46
ACM
2196 return err;
2197}
2198
aeafcbaf 2199static int dso__load_kernel_sym(struct dso *dso, struct map *map,
9de89fe7 2200 symbol_filter_t filter)
a827c875 2201{
cc612d81 2202 int err;
9e201442
ACM
2203 const char *kallsyms_filename = NULL;
2204 char *kallsyms_allocated_filename = NULL;
dc8d6ab2 2205 /*
b226a5a7
DA
2206 * Step 1: if the user specified a kallsyms or vmlinux filename, use
2207 * it and only it, reporting errors to the user if it cannot be used.
dc8d6ab2
ACM
2208 *
2209 * For instance, try to analyse an ARM perf.data file _without_ a
2210 * build-id, or if the user specifies the wrong path to the right
2211 * vmlinux file, obviously we can't fallback to another vmlinux (a
2212 * x86_86 one, on the machine where analysis is being performed, say),
2213 * or worse, /proc/kallsyms.
2214 *
2215 * If the specified file _has_ a build-id and there is a build-id
2216 * section in the perf.data file, we will still do the expected
2217 * validation in dso__load_vmlinux and will bail out if they don't
2218 * match.
2219 */
b226a5a7
DA
2220 if (symbol_conf.kallsyms_name != NULL) {
2221 kallsyms_filename = symbol_conf.kallsyms_name;
2222 goto do_kallsyms;
2223 }
2224
dc8d6ab2 2225 if (symbol_conf.vmlinux_name != NULL) {
aeafcbaf 2226 err = dso__load_vmlinux(dso, map,
dc8d6ab2 2227 symbol_conf.vmlinux_name, filter);
e7dadc00 2228 if (err > 0) {
aeafcbaf 2229 dso__set_long_name(dso,
e7dadc00
ACM
2230 strdup(symbol_conf.vmlinux_name));
2231 goto out_fixup;
2232 }
2233 return err;
dc8d6ab2 2234 }
cc612d81
ACM
2235
2236 if (vmlinux_path != NULL) {
aeafcbaf 2237 err = dso__load_vmlinux_path(dso, map, filter);
a19afe46
ACM
2238 if (err > 0)
2239 goto out_fixup;
cc612d81
ACM
2240 }
2241
ec5761ea
DA
2242 /* do not try local files if a symfs was given */
2243 if (symbol_conf.symfs[0] != 0)
2244 return -1;
2245
b7cece76
ACM
2246 /*
2247 * Say the kernel DSO was created when processing the build-id header table,
2248 * we have a build-id, so check if it is the same as the running kernel,
2249 * using it if it is.
2250 */
aeafcbaf 2251 if (dso->has_build_id) {
b7cece76 2252 u8 kallsyms_build_id[BUILD_ID_SIZE];
9e201442 2253 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
b7cece76
ACM
2254
2255 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
8d0591f6 2256 sizeof(kallsyms_build_id)) == 0) {
aeafcbaf 2257 if (dso__build_id_equal(dso, kallsyms_build_id)) {
9e201442 2258 kallsyms_filename = "/proc/kallsyms";
8d0591f6 2259 goto do_kallsyms;
9e201442 2260 }
8d0591f6 2261 }
dc8d6ab2
ACM
2262 /*
2263 * Now look if we have it on the build-id cache in
2264 * $HOME/.debug/[kernel.kallsyms].
2265 */
aeafcbaf 2266 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
9e201442
ACM
2267 sbuild_id);
2268
2269 if (asprintf(&kallsyms_allocated_filename,
2270 "%s/.debug/[kernel.kallsyms]/%s",
3846df2e
ACM
2271 getenv("HOME"), sbuild_id) == -1) {
2272 pr_err("Not enough memory for kallsyms file lookup\n");
dc8d6ab2 2273 return -1;
3846df2e 2274 }
dc8d6ab2 2275
19fc2ded
ACM
2276 kallsyms_filename = kallsyms_allocated_filename;
2277
dc8d6ab2 2278 if (access(kallsyms_filename, F_OK)) {
3846df2e
ACM
2279 pr_err("No kallsyms or vmlinux with build-id %s "
2280 "was found\n", sbuild_id);
9e201442 2281 free(kallsyms_allocated_filename);
dc8d6ab2 2282 return -1;
9e201442 2283 }
dc8d6ab2
ACM
2284 } else {
2285 /*
2286 * Last resort, if we don't have a build-id and couldn't find
2287 * any vmlinux file, try the running kernel kallsyms table.
2288 */
9e201442 2289 kallsyms_filename = "/proc/kallsyms";
9e201442 2290 }
439d473b 2291
cc612d81 2292do_kallsyms:
aeafcbaf 2293 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
3846df2e
ACM
2294 if (err > 0)
2295 pr_debug("Using %s for symbols\n", kallsyms_filename);
dc8d6ab2 2296 free(kallsyms_allocated_filename);
439d473b
ACM
2297
2298 if (err > 0) {
cc612d81 2299out_fixup:
e1c7c6a4 2300 if (kallsyms_filename != NULL)
aeafcbaf 2301 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
6a4694a4
ACM
2302 map__fixup_start(map);
2303 map__fixup_end(map);
439d473b 2304 }
94cb9e38 2305
a827c875
ACM
2306 return err;
2307}
2308
aeafcbaf
ACM
2309static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
2310 symbol_filter_t filter)
a1645ce1
ZY
2311{
2312 int err;
2313 const char *kallsyms_filename = NULL;
23346f21 2314 struct machine *machine;
a1645ce1
ZY
2315 char path[PATH_MAX];
2316
2317 if (!map->groups) {
2318 pr_debug("Guest kernel map hasn't the point to groups\n");
2319 return -1;
2320 }
23346f21 2321 machine = map->groups->machine;
a1645ce1 2322
23346f21 2323 if (machine__is_default_guest(machine)) {
a1645ce1
ZY
2324 /*
2325 * if the user specified a vmlinux filename, use it and only
2326 * it, reporting errors to the user if it cannot be used.
2327 * Or use file guest_kallsyms inputted by user on commandline
2328 */
2329 if (symbol_conf.default_guest_vmlinux_name != NULL) {
aeafcbaf 2330 err = dso__load_vmlinux(dso, map,
a1645ce1
ZY
2331 symbol_conf.default_guest_vmlinux_name, filter);
2332 goto out_try_fixup;
2333 }
2334
2335 kallsyms_filename = symbol_conf.default_guest_kallsyms;
2336 if (!kallsyms_filename)
2337 return -1;
2338 } else {
23346f21 2339 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
a1645ce1
ZY
2340 kallsyms_filename = path;
2341 }
2342
aeafcbaf 2343 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
a1645ce1
ZY
2344 if (err > 0)
2345 pr_debug("Using %s for symbols\n", kallsyms_filename);
2346
2347out_try_fixup:
2348 if (err > 0) {
2349 if (kallsyms_filename != NULL) {
48ea8f54 2350 machine__mmap_name(machine, path, sizeof(path));
aeafcbaf 2351 dso__set_long_name(dso, strdup(path));
a1645ce1
ZY
2352 }
2353 map__fixup_start(map);
2354 map__fixup_end(map);
2355 }
2356
2357 return err;
2358}
cd84c2ac 2359
b0da954a 2360static void dsos__add(struct list_head *head, struct dso *dso)
cd84c2ac 2361{
b0da954a 2362 list_add_tail(&dso->node, head);
cd84c2ac
FW
2363}
2364
b0da954a 2365static struct dso *dsos__find(struct list_head *head, const char *name)
cd84c2ac
FW
2366{
2367 struct dso *pos;
2368
b0da954a 2369 list_for_each_entry(pos, head, node)
cf4e5b08 2370 if (strcmp(pos->long_name, name) == 0)
cd84c2ac
FW
2371 return pos;
2372 return NULL;
2373}
2374
a89e5abe 2375struct dso *__dsos__findnew(struct list_head *head, const char *name)
cd84c2ac 2376{
a89e5abe 2377 struct dso *dso = dsos__find(head, name);
cd84c2ac 2378
e4204992 2379 if (!dso) {
00a192b3 2380 dso = dso__new(name);
cfc10d3b 2381 if (dso != NULL) {
a89e5abe 2382 dsos__add(head, dso);
cfc10d3b
ACM
2383 dso__set_basename(dso);
2384 }
66bd8424 2385 }
cd84c2ac
FW
2386
2387 return dso;
cd84c2ac
FW
2388}
2389
1f626bc3 2390size_t __dsos__fprintf(struct list_head *head, FILE *fp)
cd84c2ac
FW
2391{
2392 struct dso *pos;
cbf69680 2393 size_t ret = 0;
cd84c2ac 2394
95011c60
ACM
2395 list_for_each_entry(pos, head, node) {
2396 int i;
2397 for (i = 0; i < MAP__NR_TYPES; ++i)
cbf69680 2398 ret += dso__fprintf(pos, i, fp);
95011c60 2399 }
cbf69680
ACM
2400
2401 return ret;
cd84c2ac
FW
2402}
2403
aeafcbaf 2404size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
b0da954a 2405{
a1645ce1 2406 struct rb_node *nd;
cbf69680 2407 size_t ret = 0;
a1645ce1 2408
aeafcbaf 2409 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
23346f21 2410 struct machine *pos = rb_entry(nd, struct machine, rb_node);
cbf69680
ACM
2411 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
2412 ret += __dsos__fprintf(&pos->user_dsos, fp);
a1645ce1 2413 }
cbf69680
ACM
2414
2415 return ret;
b0da954a
ACM
2416}
2417
88d3d9b7
ACM
2418static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
2419 bool with_hits)
9e03eb2d
ACM
2420{
2421 struct dso *pos;
2422 size_t ret = 0;
2423
b0da954a 2424 list_for_each_entry(pos, head, node) {
88d3d9b7
ACM
2425 if (with_hits && !pos->hit)
2426 continue;
9e03eb2d 2427 ret += dso__fprintf_buildid(pos, fp);
1124ba73 2428 ret += fprintf(fp, " %s\n", pos->long_name);
9e03eb2d
ACM
2429 }
2430 return ret;
2431}
2432
aeafcbaf
ACM
2433size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
2434 bool with_hits)
f869097e 2435{
aeafcbaf
ACM
2436 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
2437 __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
f869097e
ACM
2438}
2439
aeafcbaf
ACM
2440size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
2441 FILE *fp, bool with_hits)
b0da954a 2442{
a1645ce1
ZY
2443 struct rb_node *nd;
2444 size_t ret = 0;
2445
aeafcbaf 2446 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
23346f21 2447 struct machine *pos = rb_entry(nd, struct machine, rb_node);
f869097e 2448 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
a1645ce1
ZY
2449 }
2450 return ret;
b0da954a
ACM
2451}
2452
f57b05ed
JO
2453static struct dso*
2454dso__kernel_findnew(struct machine *machine, const char *name,
2455 const char *short_name, int dso_type)
fd1d908c 2456{
f57b05ed
JO
2457 /*
2458 * The kernel dso could be created by build_id processing.
2459 */
2460 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
a1645ce1 2461
f57b05ed
JO
2462 /*
2463 * We need to run this in all cases, since during the build_id
2464 * processing we had no idea this was the kernel dso.
2465 */
aeafcbaf 2466 if (dso != NULL) {
f57b05ed
JO
2467 dso__set_short_name(dso, short_name);
2468 dso->kernel = dso_type;
fd1d908c
ACM
2469 }
2470
aeafcbaf 2471 return dso;
fd1d908c
ACM
2472}
2473
aeafcbaf 2474void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
fd1d908c 2475{
a1645ce1
ZY
2476 char path[PATH_MAX];
2477
23346f21 2478 if (machine__is_default_guest(machine))
a1645ce1 2479 return;
23346f21 2480 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
aeafcbaf
ACM
2481 if (sysfs__read_build_id(path, dso->build_id,
2482 sizeof(dso->build_id)) == 0)
2483 dso->has_build_id = true;
fd1d908c
ACM
2484}
2485
f57b05ed 2486static struct dso *machine__get_kernel(struct machine *machine)
cd84c2ac 2487{
a1645ce1
ZY
2488 const char *vmlinux_name = NULL;
2489 struct dso *kernel;
cd84c2ac 2490
aeafcbaf 2491 if (machine__is_host(machine)) {
a1645ce1 2492 vmlinux_name = symbol_conf.vmlinux_name;
f57b05ed
JO
2493 if (!vmlinux_name)
2494 vmlinux_name = "[kernel.kallsyms]";
2495
2496 kernel = dso__kernel_findnew(machine, vmlinux_name,
2497 "[kernel]",
2498 DSO_TYPE_KERNEL);
a1645ce1 2499 } else {
f57b05ed
JO
2500 char bf[PATH_MAX];
2501
aeafcbaf 2502 if (machine__is_default_guest(machine))
a1645ce1 2503 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
f57b05ed
JO
2504 if (!vmlinux_name)
2505 vmlinux_name = machine__mmap_name(machine, bf,
2506 sizeof(bf));
2507
2508 kernel = dso__kernel_findnew(machine, vmlinux_name,
2509 "[guest.kernel]",
2510 DSO_TYPE_GUEST_KERNEL);
8d92c02a 2511 }
cd84c2ac 2512
f57b05ed 2513 if (kernel != NULL && (!kernel->has_build_id))
aeafcbaf 2514 dso__read_running_kernel_build_id(kernel, machine);
f57b05ed 2515
f1dfa0b1 2516 return kernel;
f1dfa0b1
ACM
2517}
2518
d214afbd
ML
2519struct process_args {
2520 u64 start;
2521};
2522
2523static int symbol__in_kernel(void *arg, const char *name,
3b01a413 2524 char type __used, u64 start, u64 end __used)
d214afbd
ML
2525{
2526 struct process_args *args = arg;
2527
2528 if (strchr(name, '['))
2529 return 0;
2530
2531 args->start = start;
2532 return 1;
2533}
2534
2535/* Figure out the start address of kernel map from /proc/kallsyms */
2536static u64 machine__get_kernel_start_addr(struct machine *machine)
2537{
2538 const char *filename;
2539 char path[PATH_MAX];
2540 struct process_args args;
2541
2542 if (machine__is_host(machine)) {
2543 filename = "/proc/kallsyms";
2544 } else {
2545 if (machine__is_default_guest(machine))
2546 filename = (char *)symbol_conf.default_guest_kallsyms;
2547 else {
2548 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2549 filename = path;
2550 }
2551 }
2552
ec80fde7
ACM
2553 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
2554 return 0;
2555
d214afbd
ML
2556 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
2557 return 0;
2558
2559 return args.start;
2560}
2561
aeafcbaf 2562int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
f1dfa0b1 2563{
de176489 2564 enum map_type type;
aeafcbaf 2565 u64 start = machine__get_kernel_start_addr(machine);
f1dfa0b1 2566
de176489 2567 for (type = 0; type < MAP__NR_TYPES; ++type) {
9de89fe7
ACM
2568 struct kmap *kmap;
2569
aeafcbaf
ACM
2570 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
2571 if (machine->vmlinux_maps[type] == NULL)
de176489 2572 return -1;
f1dfa0b1 2573
aeafcbaf
ACM
2574 machine->vmlinux_maps[type]->map_ip =
2575 machine->vmlinux_maps[type]->unmap_ip =
2576 identity__map_ip;
2577 kmap = map__kmap(machine->vmlinux_maps[type]);
2578 kmap->kmaps = &machine->kmaps;
2579 map_groups__insert(&machine->kmaps,
2580 machine->vmlinux_maps[type]);
f1dfa0b1
ACM
2581 }
2582
f1dfa0b1 2583 return 0;
2446042c
ACM
2584}
2585
aeafcbaf 2586void machine__destroy_kernel_maps(struct machine *machine)
076c6e45
ACM
2587{
2588 enum map_type type;
2589
2590 for (type = 0; type < MAP__NR_TYPES; ++type) {
2591 struct kmap *kmap;
2592
aeafcbaf 2593 if (machine->vmlinux_maps[type] == NULL)
076c6e45
ACM
2594 continue;
2595
aeafcbaf
ACM
2596 kmap = map__kmap(machine->vmlinux_maps[type]);
2597 map_groups__remove(&machine->kmaps,
2598 machine->vmlinux_maps[type]);
076c6e45
ACM
2599 if (kmap->ref_reloc_sym) {
2600 /*
2601 * ref_reloc_sym is shared among all maps, so free just
2602 * on one of them.
2603 */
2604 if (type == MAP__FUNCTION) {
2605 free((char *)kmap->ref_reloc_sym->name);
2606 kmap->ref_reloc_sym->name = NULL;
2607 free(kmap->ref_reloc_sym);
2608 }
2609 kmap->ref_reloc_sym = NULL;
2610 }
2611
aeafcbaf
ACM
2612 map__delete(machine->vmlinux_maps[type]);
2613 machine->vmlinux_maps[type] = NULL;
076c6e45
ACM
2614 }
2615}
2616
aeafcbaf 2617int machine__create_kernel_maps(struct machine *machine)
5c0541d5 2618{
f57b05ed 2619 struct dso *kernel = machine__get_kernel(machine);
5c0541d5
ACM
2620
2621 if (kernel == NULL ||
aeafcbaf 2622 __machine__create_kernel_maps(machine, kernel) < 0)
5c0541d5
ACM
2623 return -1;
2624
f51304d3
DA
2625 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
2626 if (machine__is_host(machine))
2627 pr_debug("Problems creating module maps, "
2628 "continuing anyway...\n");
2629 else
2630 pr_debug("Problems creating module maps for guest %d, "
2631 "continuing anyway...\n", machine->pid);
2632 }
2633
5c0541d5
ACM
2634 /*
2635 * Now that we have all the maps created, just set the ->end of them:
2636 */
aeafcbaf 2637 map_groups__fixup_end(&machine->kmaps);
5c0541d5
ACM
2638 return 0;
2639}
2640
cc612d81
ACM
2641static void vmlinux_path__exit(void)
2642{
2643 while (--vmlinux_path__nr_entries >= 0) {
2644 free(vmlinux_path[vmlinux_path__nr_entries]);
2645 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2646 }
2647
2648 free(vmlinux_path);
2649 vmlinux_path = NULL;
2650}
2651
2652static int vmlinux_path__init(void)
2653{
2654 struct utsname uts;
2655 char bf[PATH_MAX];
2656
cc612d81
ACM
2657 vmlinux_path = malloc(sizeof(char *) * 5);
2658 if (vmlinux_path == NULL)
2659 return -1;
2660
2661 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2662 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2663 goto out_fail;
2664 ++vmlinux_path__nr_entries;
2665 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2666 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2667 goto out_fail;
2668 ++vmlinux_path__nr_entries;
ec5761ea
DA
2669
2670 /* only try running kernel version if no symfs was given */
2671 if (symbol_conf.symfs[0] != 0)
2672 return 0;
2673
2674 if (uname(&uts) < 0)
2675 return -1;
2676
cc612d81
ACM
2677 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2678 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2679 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2680 goto out_fail;
2681 ++vmlinux_path__nr_entries;
2682 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2683 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2684 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2685 goto out_fail;
2686 ++vmlinux_path__nr_entries;
2687 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2688 uts.release);
2689 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2690 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2691 goto out_fail;
2692 ++vmlinux_path__nr_entries;
2693
2694 return 0;
2695
2696out_fail:
2697 vmlinux_path__exit();
2698 return -1;
2699}
2700
aeafcbaf 2701size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
b0a9ab62
ACM
2702{
2703 int i;
2704 size_t printed = 0;
aeafcbaf 2705 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
5ad90e4e
ACM
2706
2707 if (kdso->has_build_id) {
2708 char filename[PATH_MAX];
2709 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2710 printed += fprintf(fp, "[0] %s\n", filename);
2711 }
b0a9ab62
ACM
2712
2713 for (i = 0; i < vmlinux_path__nr_entries; ++i)
5ad90e4e
ACM
2714 printed += fprintf(fp, "[%d] %s\n",
2715 i + kdso->has_build_id, vmlinux_path[i]);
b0a9ab62
ACM
2716
2717 return printed;
2718}
2719
655000e7
ACM
2720static int setup_list(struct strlist **list, const char *list_str,
2721 const char *list_name)
2722{
2723 if (list_str == NULL)
2724 return 0;
2725
2726 *list = strlist__new(true, list_str);
2727 if (!*list) {
2728 pr_err("problems parsing %s list\n", list_name);
2729 return -1;
2730 }
2731 return 0;
2732}
2733
ec80fde7
ACM
2734static bool symbol__read_kptr_restrict(void)
2735{
2736 bool value = false;
2737
2738 if (geteuid() != 0) {
2739 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2740 if (fp != NULL) {
2741 char line[8];
2742
2743 if (fgets(line, sizeof(line), fp) != NULL)
2744 value = atoi(line) != 0;
2745
2746 fclose(fp);
2747 }
2748 }
2749
2750 return value;
2751}
2752
75be6cf4 2753int symbol__init(void)
2446042c 2754{
ec5761ea
DA
2755 const char *symfs;
2756
85e00b55
JZ
2757 if (symbol_conf.initialized)
2758 return 0;
2759
4d439517
DM
2760 symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
2761
166ccc9c
NK
2762 symbol__elf_init();
2763
75be6cf4
ACM
2764 if (symbol_conf.sort_by_name)
2765 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2766 sizeof(struct symbol));
b32d133a 2767
75be6cf4 2768 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2446042c
ACM
2769 return -1;
2770
c410a338
ACM
2771 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2772 pr_err("'.' is the only non valid --field-separator argument\n");
2773 return -1;
2774 }
2775
655000e7
ACM
2776 if (setup_list(&symbol_conf.dso_list,
2777 symbol_conf.dso_list_str, "dso") < 0)
2778 return -1;
2779
2780 if (setup_list(&symbol_conf.comm_list,
2781 symbol_conf.comm_list_str, "comm") < 0)
2782 goto out_free_dso_list;
2783
2784 if (setup_list(&symbol_conf.sym_list,
2785 symbol_conf.sym_list_str, "symbol") < 0)
2786 goto out_free_comm_list;
2787
ec5761ea
DA
2788 /*
2789 * A path to symbols of "/" is identical to ""
2790 * reset here for simplicity.
2791 */
2792 symfs = realpath(symbol_conf.symfs, NULL);
2793 if (symfs == NULL)
2794 symfs = symbol_conf.symfs;
2795 if (strcmp(symfs, "/") == 0)
2796 symbol_conf.symfs = "";
2797 if (symfs != symbol_conf.symfs)
2798 free((void *)symfs);
2799
ec80fde7
ACM
2800 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2801
85e00b55 2802 symbol_conf.initialized = true;
4aa65636 2803 return 0;
655000e7 2804
655000e7
ACM
2805out_free_comm_list:
2806 strlist__delete(symbol_conf.comm_list);
d74c896b
NK
2807out_free_dso_list:
2808 strlist__delete(symbol_conf.dso_list);
655000e7 2809 return -1;
4aa65636
ACM
2810}
2811
d65a458b
ACM
2812void symbol__exit(void)
2813{
85e00b55
JZ
2814 if (!symbol_conf.initialized)
2815 return;
d65a458b
ACM
2816 strlist__delete(symbol_conf.sym_list);
2817 strlist__delete(symbol_conf.dso_list);
2818 strlist__delete(symbol_conf.comm_list);
2819 vmlinux_path__exit();
2820 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
85e00b55 2821 symbol_conf.initialized = false;
d65a458b
ACM
2822}
2823
aeafcbaf 2824int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
4aa65636 2825{
aeafcbaf 2826 struct machine *machine = machines__findnew(machines, pid);
9de89fe7 2827
23346f21 2828 if (machine == NULL)
a1645ce1 2829 return -1;
cc612d81 2830
5c0541d5 2831 return machine__create_kernel_maps(machine);
cd84c2ac 2832}
5aab621b
ACM
2833
2834static int hex(char ch)
2835{
2836 if ((ch >= '0') && (ch <= '9'))
2837 return ch - '0';
2838 if ((ch >= 'a') && (ch <= 'f'))
2839 return ch - 'a' + 10;
2840 if ((ch >= 'A') && (ch <= 'F'))
2841 return ch - 'A' + 10;
2842 return -1;
2843}
2844
2845/*
2846 * While we find nice hex chars, build a long_val.
2847 * Return number of chars processed.
2848 */
2849int hex2u64(const char *ptr, u64 *long_val)
2850{
2851 const char *p = ptr;
2852 *long_val = 0;
2853
2854 while (*p) {
2855 const int hex_val = hex(*p);
2856
2857 if (hex_val < 0)
2858 break;
2859
2860 *long_val = (*long_val << 4) | hex_val;
2861 p++;
2862 }
2863
2864 return p - ptr;
2865}
2866
2867char *strxfrchar(char *s, char from, char to)
2868{
2869 char *p = s;
2870
2871 while ((p = strchr(p, from)) != NULL)
2872 *p++ = to;
2873
2874 return s;
2875}
a1645ce1 2876
aeafcbaf 2877int machines__create_guest_kernel_maps(struct rb_root *machines)
a1645ce1
ZY
2878{
2879 int ret = 0;
2880 struct dirent **namelist = NULL;
2881 int i, items = 0;
2882 char path[PATH_MAX];
2883 pid_t pid;
347ed990 2884 char *endp;
a1645ce1
ZY
2885
2886 if (symbol_conf.default_guest_vmlinux_name ||
2887 symbol_conf.default_guest_modules ||
2888 symbol_conf.default_guest_kallsyms) {
aeafcbaf 2889 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
a1645ce1
ZY
2890 }
2891
2892 if (symbol_conf.guestmount) {
2893 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2894 if (items <= 0)
2895 return -ENOENT;
2896 for (i = 0; i < items; i++) {
2897 if (!isdigit(namelist[i]->d_name[0])) {
2898 /* Filter out . and .. */
2899 continue;
2900 }
347ed990
DA
2901 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
2902 if ((*endp != '\0') ||
2903 (endp == namelist[i]->d_name) ||
2904 (errno == ERANGE)) {
2905 pr_debug("invalid directory (%s). Skipping.\n",
2906 namelist[i]->d_name);
2907 continue;
2908 }
a1645ce1
ZY
2909 sprintf(path, "%s/%s/proc/kallsyms",
2910 symbol_conf.guestmount,
2911 namelist[i]->d_name);
2912 ret = access(path, R_OK);
2913 if (ret) {
2914 pr_debug("Can't access file %s\n", path);
2915 goto failure;
2916 }
aeafcbaf 2917 machines__create_kernel_maps(machines, pid);
a1645ce1
ZY
2918 }
2919failure:
2920 free(namelist);
2921 }
2922
2923 return ret;
2924}
5c0541d5 2925
aeafcbaf 2926void machines__destroy_guest_kernel_maps(struct rb_root *machines)
076c6e45 2927{
aeafcbaf 2928 struct rb_node *next = rb_first(machines);
076c6e45
ACM
2929
2930 while (next) {
2931 struct machine *pos = rb_entry(next, struct machine, rb_node);
2932
2933 next = rb_next(&pos->rb_node);
aeafcbaf 2934 rb_erase(&pos->rb_node, machines);
076c6e45
ACM
2935 machine__delete(pos);
2936 }
2937}
2938
aeafcbaf 2939int machine__load_kallsyms(struct machine *machine, const char *filename,
5c0541d5
ACM
2940 enum map_type type, symbol_filter_t filter)
2941{
aeafcbaf 2942 struct map *map = machine->vmlinux_maps[type];
5c0541d5
ACM
2943 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2944
2945 if (ret > 0) {
2946 dso__set_loaded(map->dso, type);
2947 /*
2948 * Since /proc/kallsyms will have multiple sessions for the
2949 * kernel, with modules between them, fixup the end of all
2950 * sections.
2951 */
aeafcbaf 2952 __map_groups__fixup_end(&machine->kmaps, type);
5c0541d5
ACM
2953 }
2954
2955 return ret;
2956}
2957
aeafcbaf 2958int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
5c0541d5
ACM
2959 symbol_filter_t filter)
2960{
aeafcbaf 2961 struct map *map = machine->vmlinux_maps[type];
5c0541d5
ACM
2962 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2963
2964 if (ret > 0) {
2965 dso__set_loaded(map->dso, type);
2966 map__reloc_vmlinux(map);
2967 }
2968
2969 return ret;
2970}
225466f1
SD
2971
2972struct map *dso__new_map(const char *name)
2973{
378474e4 2974 struct map *map = NULL;
225466f1 2975 struct dso *dso = dso__new(name);
378474e4
SD
2976
2977 if (dso)
2978 map = map__new2(0, dso, MAP__FUNCTION);
225466f1
SD
2979
2980 return map;
2981}
949d160b
JO
2982
2983static int open_dso(struct dso *dso, struct machine *machine)
2984{
2985 char *root_dir = (char *) "";
2986 char *name;
2987 int fd;
2988
2989 name = malloc(PATH_MAX);
2990 if (!name)
2991 return -ENOMEM;
2992
2993 if (machine)
2994 root_dir = machine->root_dir;
2995
2996 if (dso__binary_type_file(dso, dso->data_type,
2997 root_dir, name, PATH_MAX)) {
2998 free(name);
2999 return -EINVAL;
3000 }
3001
3002 fd = open(name, O_RDONLY);
3003 free(name);
3004 return fd;
3005}
3006
3007int dso__data_fd(struct dso *dso, struct machine *machine)
3008{
3009 int i = 0;
3010
3011 if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
3012 return open_dso(dso, machine);
3013
3014 do {
3015 int fd;
3016
3017 dso->data_type = binary_type_data[i++];
3018
3019 fd = open_dso(dso, machine);
3020 if (fd >= 0)
3021 return fd;
3022
3023 } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
3024
3025 return -EINVAL;
3026}
3027
4dff624a
JO
3028static void
3029dso_cache__free(struct rb_root *root)
949d160b 3030{
4dff624a
JO
3031 struct rb_node *next = rb_first(root);
3032
3033 while (next) {
3034 struct dso_cache *cache;
3035
3036 cache = rb_entry(next, struct dso_cache, rb_node);
3037 next = rb_next(&cache->rb_node);
3038 rb_erase(&cache->rb_node, root);
3039 free(cache);
3040 }
949d160b
JO
3041}
3042
4dff624a
JO
3043static struct dso_cache*
3044dso_cache__find(struct rb_root *root, u64 offset)
949d160b 3045{
4dff624a
JO
3046 struct rb_node **p = &root->rb_node;
3047 struct rb_node *parent = NULL;
3048 struct dso_cache *cache;
3049
3050 while (*p != NULL) {
3051 u64 end;
3052
3053 parent = *p;
3054 cache = rb_entry(parent, struct dso_cache, rb_node);
3055 end = cache->offset + DSO__DATA_CACHE_SIZE;
3056
3057 if (offset < cache->offset)
3058 p = &(*p)->rb_left;
3059 else if (offset >= end)
3060 p = &(*p)->rb_right;
3061 else
3062 return cache;
3063 }
3064 return NULL;
3065}
3066
3067static void
3068dso_cache__insert(struct rb_root *root, struct dso_cache *new)
3069{
3070 struct rb_node **p = &root->rb_node;
3071 struct rb_node *parent = NULL;
3072 struct dso_cache *cache;
3073 u64 offset = new->offset;
3074
3075 while (*p != NULL) {
3076 u64 end;
3077
3078 parent = *p;
3079 cache = rb_entry(parent, struct dso_cache, rb_node);
3080 end = cache->offset + DSO__DATA_CACHE_SIZE;
3081
3082 if (offset < cache->offset)
3083 p = &(*p)->rb_left;
3084 else if (offset >= end)
3085 p = &(*p)->rb_right;
3086 }
3087
3088 rb_link_node(&new->rb_node, parent, p);
3089 rb_insert_color(&new->rb_node, root);
3090}
3091
3092static ssize_t
3093dso_cache__memcpy(struct dso_cache *cache, u64 offset,
3094 u8 *data, u64 size)
3095{
3096 u64 cache_offset = offset - cache->offset;
3097 u64 cache_size = min(cache->size - cache_offset, size);
3098
3099 memcpy(data, cache->data + cache_offset, cache_size);
3100 return cache_size;
949d160b
JO
3101}
3102
4dff624a
JO
3103static ssize_t
3104dso_cache__read(struct dso *dso, struct machine *machine,
3105 u64 offset, u8 *data, ssize_t size)
949d160b 3106{
4dff624a
JO
3107 struct dso_cache *cache;
3108 ssize_t ret;
949d160b
JO
3109 int fd;
3110
3111 fd = dso__data_fd(dso, machine);
3112 if (fd < 0)
3113 return -1;
3114
3115 do {
4dff624a
JO
3116 u64 cache_offset;
3117
3118 ret = -ENOMEM;
3119
3120 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
3121 if (!cache)
949d160b
JO
3122 break;
3123
4dff624a
JO
3124 cache_offset = offset & DSO__DATA_CACHE_MASK;
3125 ret = -EINVAL;
3126
3127 if (-1 == lseek(fd, cache_offset, SEEK_SET))
949d160b
JO
3128 break;
3129
4dff624a
JO
3130 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
3131 if (ret <= 0)
3132 break;
3133
3134 cache->offset = cache_offset;
3135 cache->size = ret;
3136 dso_cache__insert(&dso->cache, cache);
3137
3138 ret = dso_cache__memcpy(cache, offset, data, size);
949d160b
JO
3139
3140 } while (0);
3141
4dff624a
JO
3142 if (ret <= 0)
3143 free(cache);
3144
949d160b 3145 close(fd);
4dff624a
JO
3146 return ret;
3147}
3148
3149static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
3150 u64 offset, u8 *data, ssize_t size)
3151{
3152 struct dso_cache *cache;
3153
3154 cache = dso_cache__find(&dso->cache, offset);
3155 if (cache)
3156 return dso_cache__memcpy(cache, offset, data, size);
3157 else
3158 return dso_cache__read(dso, machine, offset, data, size);
949d160b
JO
3159}
3160
3161ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
3162 u64 offset, u8 *data, ssize_t size)
3163{
4dff624a
JO
3164 ssize_t r = 0;
3165 u8 *p = data;
3166
3167 do {
3168 ssize_t ret;
3169
3170 ret = dso_cache_read(dso, machine, offset, p, size);
3171 if (ret < 0)
3172 return ret;
3173
3174 /* Reached EOF, return what we have. */
3175 if (!ret)
3176 break;
3177
3178 BUG_ON(ret > size);
3179
3180 r += ret;
3181 p += ret;
3182 offset += ret;
3183 size -= ret;
3184
3185 } while (size);
3186
3187 return r;
949d160b
JO
3188}
3189
3190ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
3191 struct machine *machine, u64 addr,
3192 u8 *data, ssize_t size)
3193{
3194 u64 offset = map->map_ip(map, addr);
3195 return dso__data_read_offset(dso, machine, offset, data, size);
3196}
This page took 0.531153 seconds and 5 git commands to generate.