xref: /linux/tools/perf/util/symbol.c (revision 6000fc4d6f3e55ad52cce8d76317187fe01af2aa)
1 #include "util.h"
2 #include "../perf.h"
3 #include "string.h"
4 #include "symbol.h"
5 
6 #include <libelf.h>
7 #include <gelf.h>
8 #include <elf.h>
9 
10 const char *sym_hist_filter;
11 
12 enum dso_origin {
13 	DSO__ORIG_KERNEL = 0,
14 	DSO__ORIG_JAVA_JIT,
15 	DSO__ORIG_FEDORA,
16 	DSO__ORIG_UBUNTU,
17 	DSO__ORIG_BUILDID,
18 	DSO__ORIG_DSO,
19 	DSO__ORIG_NOT_FOUND,
20 };
21 
22 static struct symbol *symbol__new(u64 start, u64 len,
23 				  const char *name, unsigned int priv_size,
24 				  u64 obj_start, int verbose)
25 {
26 	size_t namelen = strlen(name) + 1;
27 	struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
28 
29 	if (!self)
30 		return NULL;
31 
32 	if (verbose >= 2)
33 		printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
34 			(u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
35 
36 	self->obj_start= obj_start;
37 	self->hist = NULL;
38 	self->hist_sum = 0;
39 
40 	if (sym_hist_filter && !strcmp(name, sym_hist_filter))
41 		self->hist = calloc(sizeof(u64), len);
42 
43 	if (priv_size) {
44 		memset(self, 0, priv_size);
45 		self = ((void *)self) + priv_size;
46 	}
47 	self->start = start;
48 	self->end   = len ? start + len - 1 : start;
49 	memcpy(self->name, name, namelen);
50 
51 	return self;
52 }
53 
54 static void symbol__delete(struct symbol *self, unsigned int priv_size)
55 {
56 	free(((void *)self) - priv_size);
57 }
58 
59 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
60 {
61 	if (!self->module)
62 		return fprintf(fp, " %llx-%llx %s\n",
63 		       self->start, self->end, self->name);
64 	else
65 		return fprintf(fp, " %llx-%llx %s \t[%s]\n",
66 		       self->start, self->end, self->name, self->module->name);
67 }
68 
69 struct dso *dso__new(const char *name, unsigned int sym_priv_size)
70 {
71 	struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
72 
73 	if (self != NULL) {
74 		strcpy(self->name, name);
75 		self->syms = RB_ROOT;
76 		self->sym_priv_size = sym_priv_size;
77 		self->find_symbol = dso__find_symbol;
78 		self->slen_calculated = 0;
79 		self->origin = DSO__ORIG_NOT_FOUND;
80 	}
81 
82 	return self;
83 }
84 
85 static void dso__delete_symbols(struct dso *self)
86 {
87 	struct symbol *pos;
88 	struct rb_node *next = rb_first(&self->syms);
89 
90 	while (next) {
91 		pos = rb_entry(next, struct symbol, rb_node);
92 		next = rb_next(&pos->rb_node);
93 		rb_erase(&pos->rb_node, &self->syms);
94 		symbol__delete(pos, self->sym_priv_size);
95 	}
96 }
97 
98 void dso__delete(struct dso *self)
99 {
100 	dso__delete_symbols(self);
101 	free(self);
102 }
103 
104 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
105 {
106 	struct rb_node **p = &self->syms.rb_node;
107 	struct rb_node *parent = NULL;
108 	const u64 ip = sym->start;
109 	struct symbol *s;
110 
111 	while (*p != NULL) {
112 		parent = *p;
113 		s = rb_entry(parent, struct symbol, rb_node);
114 		if (ip < s->start)
115 			p = &(*p)->rb_left;
116 		else
117 			p = &(*p)->rb_right;
118 	}
119 	rb_link_node(&sym->rb_node, parent, p);
120 	rb_insert_color(&sym->rb_node, &self->syms);
121 }
122 
123 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
124 {
125 	struct rb_node *n;
126 
127 	if (self == NULL)
128 		return NULL;
129 
130 	n = self->syms.rb_node;
131 
132 	while (n) {
133 		struct symbol *s = rb_entry(n, struct symbol, rb_node);
134 
135 		if (ip < s->start)
136 			n = n->rb_left;
137 		else if (ip > s->end)
138 			n = n->rb_right;
139 		else
140 			return s;
141 	}
142 
143 	return NULL;
144 }
145 
146 size_t dso__fprintf(struct dso *self, FILE *fp)
147 {
148 	size_t ret = fprintf(fp, "dso: %s\n", self->name);
149 
150 	struct rb_node *nd;
151 	for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
152 		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
153 		ret += symbol__fprintf(pos, fp);
154 	}
155 
156 	return ret;
157 }
158 
159 static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose)
160 {
161 	struct rb_node *nd, *prevnd;
162 	char *line = NULL;
163 	size_t n;
164 	FILE *file = fopen("/proc/kallsyms", "r");
165 	int count = 0;
166 
167 	if (file == NULL)
168 		goto out_failure;
169 
170 	while (!feof(file)) {
171 		u64 start;
172 		struct symbol *sym;
173 		int line_len, len;
174 		char symbol_type;
175 
176 		line_len = getline(&line, &n, file);
177 		if (line_len < 0)
178 			break;
179 
180 		if (!line)
181 			goto out_failure;
182 
183 		line[--line_len] = '\0'; /* \n */
184 
185 		len = hex2u64(line, &start);
186 
187 		len++;
188 		if (len + 2 >= line_len)
189 			continue;
190 
191 		symbol_type = toupper(line[len]);
192 		/*
193 		 * We're interested only in code ('T'ext)
194 		 */
195 		if (symbol_type != 'T' && symbol_type != 'W')
196 			continue;
197 		/*
198 		 * Well fix up the end later, when we have all sorted.
199 		 */
200 		sym = symbol__new(start, 0xdead, line + len + 2,
201 				  self->sym_priv_size, 0, verbose);
202 
203 		if (sym == NULL)
204 			goto out_delete_line;
205 
206 		if (filter && filter(self, sym))
207 			symbol__delete(sym, self->sym_priv_size);
208 		else {
209 			dso__insert_symbol(self, sym);
210 			count++;
211 		}
212 	}
213 
214 	/*
215 	 * Now that we have all sorted out, just set the ->end of all
216 	 * symbols
217 	 */
218 	prevnd = rb_first(&self->syms);
219 
220 	if (prevnd == NULL)
221 		goto out_delete_line;
222 
223 	for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
224 		struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
225 			      *curr = rb_entry(nd, struct symbol, rb_node);
226 
227 		prev->end = curr->start - 1;
228 		prevnd = nd;
229 	}
230 
231 	free(line);
232 	fclose(file);
233 
234 	return count;
235 
236 out_delete_line:
237 	free(line);
238 out_failure:
239 	return -1;
240 }
241 
242 static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int verbose)
243 {
244 	char *line = NULL;
245 	size_t n;
246 	FILE *file;
247 	int nr_syms = 0;
248 
249 	file = fopen(self->name, "r");
250 	if (file == NULL)
251 		goto out_failure;
252 
253 	while (!feof(file)) {
254 		u64 start, size;
255 		struct symbol *sym;
256 		int line_len, len;
257 
258 		line_len = getline(&line, &n, file);
259 		if (line_len < 0)
260 			break;
261 
262 		if (!line)
263 			goto out_failure;
264 
265 		line[--line_len] = '\0'; /* \n */
266 
267 		len = hex2u64(line, &start);
268 
269 		len++;
270 		if (len + 2 >= line_len)
271 			continue;
272 
273 		len += hex2u64(line + len, &size);
274 
275 		len++;
276 		if (len + 2 >= line_len)
277 			continue;
278 
279 		sym = symbol__new(start, size, line + len,
280 				  self->sym_priv_size, start, verbose);
281 
282 		if (sym == NULL)
283 			goto out_delete_line;
284 
285 		if (filter && filter(self, sym))
286 			symbol__delete(sym, self->sym_priv_size);
287 		else {
288 			dso__insert_symbol(self, sym);
289 			nr_syms++;
290 		}
291 	}
292 
293 	free(line);
294 	fclose(file);
295 
296 	return nr_syms;
297 
298 out_delete_line:
299 	free(line);
300 out_failure:
301 	return -1;
302 }
303 
304 /**
305  * elf_symtab__for_each_symbol - iterate thru all the symbols
306  *
307  * @self: struct elf_symtab instance to iterate
308  * @index: uint32_t index
309  * @sym: GElf_Sym iterator
310  */
311 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
312 	for (index = 0, gelf_getsym(syms, index, &sym);\
313 	     index < nr_syms; \
314 	     index++, gelf_getsym(syms, index, &sym))
315 
316 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
317 {
318 	return GELF_ST_TYPE(sym->st_info);
319 }
320 
321 static inline int elf_sym__is_function(const GElf_Sym *sym)
322 {
323 	return elf_sym__type(sym) == STT_FUNC &&
324 	       sym->st_name != 0 &&
325 	       sym->st_shndx != SHN_UNDEF &&
326 	       sym->st_size != 0;
327 }
328 
329 static inline int elf_sym__is_label(const GElf_Sym *sym)
330 {
331 	return elf_sym__type(sym) == STT_NOTYPE &&
332 		sym->st_name != 0 &&
333 		sym->st_shndx != SHN_UNDEF &&
334 		sym->st_shndx != SHN_ABS;
335 }
336 
337 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
338 					const Elf_Data *secstrs)
339 {
340 	return secstrs->d_buf + shdr->sh_name;
341 }
342 
343 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
344 					const Elf_Data *secstrs)
345 {
346 	return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
347 }
348 
349 static inline const char *elf_sym__name(const GElf_Sym *sym,
350 					const Elf_Data *symstrs)
351 {
352 	return symstrs->d_buf + sym->st_name;
353 }
354 
355 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
356 				    GElf_Shdr *shp, const char *name,
357 				    size_t *index)
358 {
359 	Elf_Scn *sec = NULL;
360 	size_t cnt = 1;
361 
362 	while ((sec = elf_nextscn(elf, sec)) != NULL) {
363 		char *str;
364 
365 		gelf_getshdr(sec, shp);
366 		str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
367 		if (!strcmp(name, str)) {
368 			if (index)
369 				*index = cnt;
370 			break;
371 		}
372 		++cnt;
373 	}
374 
375 	return sec;
376 }
377 
378 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
379 	for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
380 	     idx < nr_entries; \
381 	     ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
382 
383 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
384 	for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
385 	     idx < nr_entries; \
386 	     ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
387 
388 /*
389  * We need to check if we have a .dynsym, so that we can handle the
390  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
391  * .dynsym or .symtab).
392  * And always look at the original dso, not at debuginfo packages, that
393  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
394  */
395 static int dso__synthesize_plt_symbols(struct  dso *self, int verbose)
396 {
397 	uint32_t nr_rel_entries, idx;
398 	GElf_Sym sym;
399 	u64 plt_offset;
400 	GElf_Shdr shdr_plt;
401 	struct symbol *f;
402 	GElf_Shdr shdr_rel_plt, shdr_dynsym;
403 	Elf_Data *reldata, *syms, *symstrs;
404 	Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
405 	size_t dynsym_idx;
406 	GElf_Ehdr ehdr;
407 	char sympltname[1024];
408 	Elf *elf;
409 	int nr = 0, symidx, fd, err = 0;
410 
411 	fd = open(self->name, O_RDONLY);
412 	if (fd < 0)
413 		goto out;
414 
415 	elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
416 	if (elf == NULL)
417 		goto out_close;
418 
419 	if (gelf_getehdr(elf, &ehdr) == NULL)
420 		goto out_elf_end;
421 
422 	scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
423 					 ".dynsym", &dynsym_idx);
424 	if (scn_dynsym == NULL)
425 		goto out_elf_end;
426 
427 	scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
428 					  ".rela.plt", NULL);
429 	if (scn_plt_rel == NULL) {
430 		scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
431 						  ".rel.plt", NULL);
432 		if (scn_plt_rel == NULL)
433 			goto out_elf_end;
434 	}
435 
436 	err = -1;
437 
438 	if (shdr_rel_plt.sh_link != dynsym_idx)
439 		goto out_elf_end;
440 
441 	if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
442 		goto out_elf_end;
443 
444 	/*
445 	 * Fetch the relocation section to find the indexes to the GOT
446 	 * and the symbols in the .dynsym they refer to.
447 	 */
448 	reldata = elf_getdata(scn_plt_rel, NULL);
449 	if (reldata == NULL)
450 		goto out_elf_end;
451 
452 	syms = elf_getdata(scn_dynsym, NULL);
453 	if (syms == NULL)
454 		goto out_elf_end;
455 
456 	scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
457 	if (scn_symstrs == NULL)
458 		goto out_elf_end;
459 
460 	symstrs = elf_getdata(scn_symstrs, NULL);
461 	if (symstrs == NULL)
462 		goto out_elf_end;
463 
464 	nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
465 	plt_offset = shdr_plt.sh_offset;
466 
467 	if (shdr_rel_plt.sh_type == SHT_RELA) {
468 		GElf_Rela pos_mem, *pos;
469 
470 		elf_section__for_each_rela(reldata, pos, pos_mem, idx,
471 					   nr_rel_entries) {
472 			symidx = GELF_R_SYM(pos->r_info);
473 			plt_offset += shdr_plt.sh_entsize;
474 			gelf_getsym(syms, symidx, &sym);
475 			snprintf(sympltname, sizeof(sympltname),
476 				 "%s@plt", elf_sym__name(&sym, symstrs));
477 
478 			f = symbol__new(plt_offset, shdr_plt.sh_entsize,
479 					sympltname, self->sym_priv_size, 0, verbose);
480 			if (!f)
481 				goto out_elf_end;
482 
483 			dso__insert_symbol(self, f);
484 			++nr;
485 		}
486 	} else if (shdr_rel_plt.sh_type == SHT_REL) {
487 		GElf_Rel pos_mem, *pos;
488 		elf_section__for_each_rel(reldata, pos, pos_mem, idx,
489 					  nr_rel_entries) {
490 			symidx = GELF_R_SYM(pos->r_info);
491 			plt_offset += shdr_plt.sh_entsize;
492 			gelf_getsym(syms, symidx, &sym);
493 			snprintf(sympltname, sizeof(sympltname),
494 				 "%s@plt", elf_sym__name(&sym, symstrs));
495 
496 			f = symbol__new(plt_offset, shdr_plt.sh_entsize,
497 					sympltname, self->sym_priv_size, 0, verbose);
498 			if (!f)
499 				goto out_elf_end;
500 
501 			dso__insert_symbol(self, f);
502 			++nr;
503 		}
504 	}
505 
506 	err = 0;
507 out_elf_end:
508 	elf_end(elf);
509 out_close:
510 	close(fd);
511 
512 	if (err == 0)
513 		return nr;
514 out:
515 	fprintf(stderr, "%s: problems reading %s PLT info.\n",
516 		__func__, self->name);
517 	return 0;
518 }
519 
520 static int dso__load_sym(struct dso *self, int fd, const char *name,
521 			 symbol_filter_t filter, int verbose, struct module *mod)
522 {
523 	Elf_Data *symstrs, *secstrs;
524 	uint32_t nr_syms;
525 	int err = -1;
526 	uint32_t index;
527 	GElf_Ehdr ehdr;
528 	GElf_Shdr shdr;
529 	Elf_Data *syms;
530 	GElf_Sym sym;
531 	Elf_Scn *sec, *sec_strndx;
532 	Elf *elf;
533 	int nr = 0, kernel = !strcmp("[kernel]", self->name);
534 
535 	elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
536 	if (elf == NULL) {
537 		if (verbose)
538 			fprintf(stderr, "%s: cannot read %s ELF file.\n",
539 				__func__, name);
540 		goto out_close;
541 	}
542 
543 	if (gelf_getehdr(elf, &ehdr) == NULL) {
544 		if (verbose)
545 			fprintf(stderr, "%s: cannot get elf header.\n", __func__);
546 		goto out_elf_end;
547 	}
548 
549 	sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
550 	if (sec == NULL) {
551 		sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
552 		if (sec == NULL)
553 			goto out_elf_end;
554 	}
555 
556 	syms = elf_getdata(sec, NULL);
557 	if (syms == NULL)
558 		goto out_elf_end;
559 
560 	sec = elf_getscn(elf, shdr.sh_link);
561 	if (sec == NULL)
562 		goto out_elf_end;
563 
564 	symstrs = elf_getdata(sec, NULL);
565 	if (symstrs == NULL)
566 		goto out_elf_end;
567 
568 	sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
569 	if (sec_strndx == NULL)
570 		goto out_elf_end;
571 
572 	secstrs = elf_getdata(sec_strndx, NULL);
573 	if (secstrs == NULL)
574 		goto out_elf_end;
575 
576 	nr_syms = shdr.sh_size / shdr.sh_entsize;
577 
578 	memset(&sym, 0, sizeof(sym));
579 	if (!kernel) {
580 		self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
581 				elf_section_by_name(elf, &ehdr, &shdr,
582 						     ".gnu.prelink_undo",
583 						     NULL) != NULL);
584 	} else self->adjust_symbols = 0;
585 
586 	elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
587 		struct symbol *f;
588 		const char *name;
589 		char *demangled;
590 		u64 obj_start;
591 		struct section *section = NULL;
592 		int is_label = elf_sym__is_label(&sym);
593 		const char *section_name;
594 
595 		if (!is_label && !elf_sym__is_function(&sym))
596 			continue;
597 
598 		sec = elf_getscn(elf, sym.st_shndx);
599 		if (!sec)
600 			goto out_elf_end;
601 
602 		gelf_getshdr(sec, &shdr);
603 
604 		if (is_label && !elf_sec__is_text(&shdr, secstrs))
605 			continue;
606 
607 		section_name = elf_sec__name(&shdr, secstrs);
608 		obj_start = sym.st_value;
609 
610 		if (self->adjust_symbols) {
611 			if (verbose >= 2)
612 				printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
613 					(u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
614 
615 			sym.st_value -= shdr.sh_addr - shdr.sh_offset;
616 		}
617 
618 		if (mod) {
619 			section = mod->sections->find_section(mod->sections, section_name);
620 			if (section)
621 				sym.st_value += section->vma;
622 			else {
623 				fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
624 					mod->name, section_name);
625 				goto out_elf_end;
626 			}
627 		}
628 		/*
629 		 * We need to figure out if the object was created from C++ sources
630 		 * DWARF DW_compile_unit has this, but we don't always have access
631 		 * to it...
632 		 */
633 		name = elf_sym__name(&sym, symstrs);
634 		demangled = bfd_demangle(NULL, name, DMGL_PARAMS | DMGL_ANSI);
635 		if (demangled != NULL)
636 			name = demangled;
637 
638 		f = symbol__new(sym.st_value, sym.st_size, name,
639 				self->sym_priv_size, obj_start, verbose);
640 		free(demangled);
641 		if (!f)
642 			goto out_elf_end;
643 
644 		if (filter && filter(self, f))
645 			symbol__delete(f, self->sym_priv_size);
646 		else {
647 			f->module = mod;
648 			dso__insert_symbol(self, f);
649 			nr++;
650 		}
651 	}
652 
653 	err = nr;
654 out_elf_end:
655 	elf_end(elf);
656 out_close:
657 	return err;
658 }
659 
660 #define BUILD_ID_SIZE 128
661 
662 static char *dso__read_build_id(struct dso *self, int verbose)
663 {
664 	int i;
665 	GElf_Ehdr ehdr;
666 	GElf_Shdr shdr;
667 	Elf_Data *build_id_data;
668 	Elf_Scn *sec;
669 	char *build_id = NULL, *bid;
670 	unsigned char *raw;
671 	Elf *elf;
672 	int fd = open(self->name, O_RDONLY);
673 
674 	if (fd < 0)
675 		goto out;
676 
677 	elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
678 	if (elf == NULL) {
679 		if (verbose)
680 			fprintf(stderr, "%s: cannot read %s ELF file.\n",
681 				__func__, self->name);
682 		goto out_close;
683 	}
684 
685 	if (gelf_getehdr(elf, &ehdr) == NULL) {
686 		if (verbose)
687 			fprintf(stderr, "%s: cannot get elf header.\n", __func__);
688 		goto out_elf_end;
689 	}
690 
691 	sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
692 	if (sec == NULL)
693 		goto out_elf_end;
694 
695 	build_id_data = elf_getdata(sec, NULL);
696 	if (build_id_data == NULL)
697 		goto out_elf_end;
698 	build_id = malloc(BUILD_ID_SIZE);
699 	if (build_id == NULL)
700 		goto out_elf_end;
701 	raw = build_id_data->d_buf + 16;
702 	bid = build_id;
703 
704 	for (i = 0; i < 20; ++i) {
705 		sprintf(bid, "%02x", *raw);
706 		++raw;
707 		bid += 2;
708 	}
709 	if (verbose >= 2)
710 		printf("%s(%s): %s\n", __func__, self->name, build_id);
711 out_elf_end:
712 	elf_end(elf);
713 out_close:
714 	close(fd);
715 out:
716 	return build_id;
717 }
718 
719 char dso__symtab_origin(const struct dso *self)
720 {
721 	static const char origin[] = {
722 		[DSO__ORIG_KERNEL] =   'k',
723 		[DSO__ORIG_JAVA_JIT] = 'j',
724 		[DSO__ORIG_FEDORA] =   'f',
725 		[DSO__ORIG_UBUNTU] =   'u',
726 		[DSO__ORIG_BUILDID] =  'b',
727 		[DSO__ORIG_DSO] =      'd',
728 	};
729 
730 	if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
731 		return '!';
732 	return origin[self->origin];
733 }
734 
735 int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
736 {
737 	int size = PATH_MAX;
738 	char *name = malloc(size), *build_id = NULL;
739 	int ret = -1;
740 	int fd;
741 
742 	if (!name)
743 		return -1;
744 
745 	self->adjust_symbols = 0;
746 
747 	if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
748 		ret = dso__load_perf_map(self, filter, verbose);
749 		self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
750 					 DSO__ORIG_NOT_FOUND;
751 		return ret;
752 	}
753 
754 	self->origin = DSO__ORIG_FEDORA - 1;
755 
756 more:
757 	do {
758 		self->origin++;
759 		switch (self->origin) {
760 		case DSO__ORIG_FEDORA:
761 			snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
762 			break;
763 		case DSO__ORIG_UBUNTU:
764 			snprintf(name, size, "/usr/lib/debug%s", self->name);
765 			break;
766 		case DSO__ORIG_BUILDID:
767 			build_id = dso__read_build_id(self, verbose);
768 			if (build_id != NULL) {
769 				snprintf(name, size,
770 					 "/usr/lib/debug/.build-id/%.2s/%s.debug",
771 					build_id, build_id + 2);
772 				free(build_id);
773 				break;
774 			}
775 			self->origin++;
776 			/* Fall thru */
777 		case DSO__ORIG_DSO:
778 			snprintf(name, size, "%s", self->name);
779 			break;
780 
781 		default:
782 			goto out;
783 		}
784 
785 		fd = open(name, O_RDONLY);
786 	} while (fd < 0);
787 
788 	ret = dso__load_sym(self, fd, name, filter, verbose, NULL);
789 	close(fd);
790 
791 	/*
792 	 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
793 	 */
794 	if (!ret)
795 		goto more;
796 
797 	if (ret > 0) {
798 		int nr_plt = dso__synthesize_plt_symbols(self, verbose);
799 		if (nr_plt > 0)
800 			ret += nr_plt;
801 	}
802 out:
803 	free(name);
804 	if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
805 		return 0;
806 	return ret;
807 }
808 
809 static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
810 			     symbol_filter_t filter, int verbose)
811 {
812 	struct module *mod = mod_dso__find_module(mods, name);
813 	int err = 0, fd;
814 
815 	if (mod == NULL || !mod->active)
816 		return err;
817 
818 	fd = open(mod->path, O_RDONLY);
819 
820 	if (fd < 0)
821 		return err;
822 
823 	err = dso__load_sym(self, fd, name, filter, verbose, mod);
824 	close(fd);
825 
826 	return err;
827 }
828 
829 int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose)
830 {
831 	struct mod_dso *mods = mod_dso__new_dso("modules");
832 	struct module *pos;
833 	struct rb_node *next;
834 	int err;
835 
836 	err = mod_dso__load_modules(mods);
837 
838 	if (err <= 0)
839 		return err;
840 
841 	/*
842 	 * Iterate over modules, and load active symbols.
843 	 */
844 	next = rb_first(&mods->mods);
845 	while (next) {
846 		pos = rb_entry(next, struct module, rb_node);
847 		err = dso__load_module(self, mods, pos->name, filter, verbose);
848 
849 		if (err < 0)
850 			break;
851 
852 		next = rb_next(&pos->rb_node);
853 	}
854 
855 	if (err < 0) {
856 		mod_dso__delete_modules(mods);
857 		mod_dso__delete_self(mods);
858 	}
859 
860 	return err;
861 }
862 
863 static inline void dso__fill_symbol_holes(struct dso *self)
864 {
865 	struct symbol *prev = NULL;
866 	struct rb_node *nd;
867 
868 	for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
869 		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
870 
871 		if (prev) {
872 			u64 hole = 0;
873 			int alias = pos->start == prev->start;
874 
875 			if (!alias)
876 				hole = prev->start - pos->end - 1;
877 
878 			if (hole || alias) {
879 				if (alias)
880 					pos->end = prev->end;
881 				else if (hole)
882 					pos->end = prev->start - 1;
883 			}
884 		}
885 		prev = pos;
886 	}
887 }
888 
889 static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
890 			     symbol_filter_t filter, int verbose)
891 {
892 	int err, fd = open(vmlinux, O_RDONLY);
893 
894 	if (fd < 0)
895 		return -1;
896 
897 	err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL);
898 
899 	if (err > 0)
900 		dso__fill_symbol_holes(self);
901 
902 	close(fd);
903 
904 	return err;
905 }
906 
907 int dso__load_kernel(struct dso *self, const char *vmlinux,
908 		     symbol_filter_t filter, int verbose, int modules)
909 {
910 	int err = -1;
911 
912 	if (vmlinux) {
913 		err = dso__load_vmlinux(self, vmlinux, filter, verbose);
914 		if (err > 0 && modules)
915 			err = dso__load_modules(self, filter, verbose);
916 	}
917 
918 	if (err <= 0)
919 		err = dso__load_kallsyms(self, filter, verbose);
920 
921 	if (err > 0)
922 		self->origin = DSO__ORIG_KERNEL;
923 
924 	return err;
925 }
926 
927 void symbol__init(void)
928 {
929 	elf_version(EV_CURRENT);
930 }
931