Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1 | #include "util.h" |
| 2 | #include "../perf.h" |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 3 | #include "string.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 4 | #include "symbol.h" |
| 5 | |
| 6 | #include <libelf.h> |
| 7 | #include <gelf.h> |
| 8 | #include <elf.h> |
Peter Zijlstra | 2cdbc46 | 2009-08-05 14:05:16 +0200 | [diff] [blame] | 9 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 10 | const char *sym_hist_filter; |
| 11 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 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 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 22 | static struct symbol *symbol__new(u64 start, u64 len, |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 23 | const char *name, unsigned int priv_size, |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 24 | u64 obj_start, int verbose) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 25 | { |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 26 | size_t namelen = strlen(name) + 1; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 27 | struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 28 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 29 | if (!self) |
| 30 | return NULL; |
| 31 | |
| 32 | if (verbose >= 2) |
| 33 | printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n", |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 34 | (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 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)) |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 41 | self->hist = calloc(sizeof(u64), len); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 42 | |
| 43 | if (priv_size) { |
| 44 | memset(self, 0, priv_size); |
| 45 | self = ((void *)self) + priv_size; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 46 | } |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 47 | self->start = start; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 48 | self->end = len ? start + len - 1 : start; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 49 | memcpy(self->name, name, namelen); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 50 | |
| 51 | return self; |
| 52 | } |
| 53 | |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 54 | static void symbol__delete(struct symbol *self, unsigned int priv_size) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 55 | { |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 56 | free(((void *)self) - priv_size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static size_t symbol__fprintf(struct symbol *self, FILE *fp) |
| 60 | { |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 61 | if (!self->module) |
| 62 | return fprintf(fp, " %llx-%llx %s\n", |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 63 | self->start, self->end, self->name); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 64 | else |
| 65 | return fprintf(fp, " %llx-%llx %s \t[%s]\n", |
| 66 | self->start, self->end, self->name, self->module->name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 67 | } |
| 68 | |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 69 | struct dso *dso__new(const char *name, unsigned int sym_priv_size) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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; |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 76 | self->sym_priv_size = sym_priv_size; |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame] | 77 | self->find_symbol = dso__find_symbol; |
Arnaldo Carvalho de Melo | 52d422d | 2009-07-10 22:47:28 -0300 | [diff] [blame] | 78 | self->slen_calculated = 0; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 79 | self->origin = DSO__ORIG_NOT_FOUND; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | c8c9652 | 2009-06-01 17:50:57 -0300 | [diff] [blame] | 93 | rb_erase(&pos->rb_node, &self->syms); |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 94 | symbol__delete(pos, self->sym_priv_size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 108 | const u64 ip = sym->start; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 123 | struct symbol *dso__find_symbol(struct dso *self, u64 ip) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 159 | static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 160 | { |
| 161 | struct rb_node *nd, *prevnd; |
| 162 | char *line = NULL; |
| 163 | size_t n; |
| 164 | FILE *file = fopen("/proc/kallsyms", "r"); |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 165 | int count = 0; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 166 | |
| 167 | if (file == NULL) |
| 168 | goto out_failure; |
| 169 | |
| 170 | while (!feof(file)) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 171 | u64 start; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 185 | len = hex2u64(line, &start); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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 | */ |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 200 | sym = symbol__new(start, 0xdead, line + len + 2, |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 201 | self->sym_priv_size, 0, verbose); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 202 | |
| 203 | if (sym == NULL) |
| 204 | goto out_delete_line; |
| 205 | |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 206 | if (filter && filter(self, sym)) |
| 207 | symbol__delete(sym, self->sym_priv_size); |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 208 | else { |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 209 | dso__insert_symbol(self, sym); |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 210 | count++; |
| 211 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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 | |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 234 | return count; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 235 | |
| 236 | out_delete_line: |
| 237 | free(line); |
| 238 | out_failure: |
| 239 | return -1; |
| 240 | } |
| 241 | |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 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)) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 254 | u64 start, size; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 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) |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 396 | { |
| 397 | uint32_t nr_rel_entries, idx; |
| 398 | GElf_Sym sym; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 399 | u64 plt_offset; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 400 | GElf_Shdr shdr_plt; |
| 401 | struct symbol *f; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 402 | GElf_Shdr shdr_rel_plt, shdr_dynsym; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 403 | Elf_Data *reldata, *syms, *symstrs; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 404 | Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; |
| 405 | size_t dynsym_idx; |
| 406 | GElf_Ehdr ehdr; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 407 | char sympltname[1024]; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 408 | Elf *elf; |
| 409 | int nr = 0, symidx, fd, err = 0; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 410 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 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, |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 428 | ".rela.plt", NULL); |
| 429 | if (scn_plt_rel == NULL) { |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 430 | scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 431 | ".rel.plt", NULL); |
| 432 | if (scn_plt_rel == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 433 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 434 | } |
| 435 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 436 | err = -1; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 437 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 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; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 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) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 450 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 451 | |
| 452 | syms = elf_getdata(scn_dynsym, NULL); |
| 453 | if (syms == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 454 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 455 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 456 | scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 457 | if (scn_symstrs == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 458 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 459 | |
| 460 | symstrs = elf_getdata(scn_symstrs, NULL); |
| 461 | if (symstrs == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 462 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 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, |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 479 | sympltname, self->sym_priv_size, 0, verbose); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 480 | if (!f) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 481 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 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, |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 497 | sympltname, self->sym_priv_size, 0, verbose); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 498 | if (!f) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 499 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 500 | |
| 501 | dso__insert_symbol(self, f); |
| 502 | ++nr; |
| 503 | } |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 504 | } |
| 505 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 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; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 518 | } |
| 519 | |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 520 | static int dso__load_sym(struct dso *self, int fd, const char *name, |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 521 | symbol_filter_t filter, int verbose, struct module *mod) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 522 | { |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 523 | Elf_Data *symstrs, *secstrs; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 531 | Elf_Scn *sec, *sec_strndx; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 532 | Elf *elf; |
Mike Galbraith | d20ff6b | 2009-07-20 14:01:38 +0200 | [diff] [blame] | 533 | int nr = 0, kernel = !strcmp("[kernel]", self->name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 534 | |
| 535 | elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); |
| 536 | if (elf == NULL) { |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 537 | if (verbose) |
| 538 | fprintf(stderr, "%s: cannot read %s ELF file.\n", |
| 539 | __func__, name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 540 | goto out_close; |
| 541 | } |
| 542 | |
| 543 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 544 | if (verbose) |
| 545 | fprintf(stderr, "%s: cannot get elf header.\n", __func__); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 546 | goto out_elf_end; |
| 547 | } |
| 548 | |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 549 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); |
| 550 | if (sec == NULL) { |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 551 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); |
| 552 | if (sec == NULL) |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 553 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 554 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 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); |
Stoyan Gaydarov | 9b30a26 | 2009-07-30 05:25:29 -0500 | [diff] [blame] | 573 | if (secstrs == NULL) |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 574 | goto out_elf_end; |
| 575 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 576 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
| 577 | |
Arjan van de Ven | e9fbc9d | 2009-06-06 21:22:33 +0200 | [diff] [blame] | 578 | memset(&sym, 0, sizeof(sym)); |
Mike Galbraith | d20ff6b | 2009-07-20 14:01:38 +0200 | [diff] [blame] | 579 | if (!kernel) { |
| 580 | self->adjust_symbols = (ehdr.e_type == ET_EXEC || |
Arnaldo Carvalho de Melo | 30d7a77 | 2009-07-02 21:24:14 -0300 | [diff] [blame] | 581 | elf_section_by_name(elf, &ehdr, &shdr, |
| 582 | ".gnu.prelink_undo", |
| 583 | NULL) != NULL); |
Mike Galbraith | d20ff6b | 2009-07-20 14:01:38 +0200 | [diff] [blame] | 584 | } else self->adjust_symbols = 0; |
| 585 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 586 | elf_symtab__for_each_symbol(syms, nr_syms, index, sym) { |
| 587 | struct symbol *f; |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 588 | const char *name; |
| 589 | char *demangled; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 590 | u64 obj_start; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 591 | struct section *section = NULL; |
| 592 | int is_label = elf_sym__is_label(&sym); |
| 593 | const char *section_name; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 594 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 595 | if (!is_label && !elf_sym__is_function(&sym)) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 603 | |
| 604 | if (is_label && !elf_sec__is_text(&shdr, secstrs)) |
| 605 | continue; |
| 606 | |
| 607 | section_name = elf_sec__name(&shdr, secstrs); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 608 | obj_start = sym.st_value; |
| 609 | |
Arnaldo Carvalho de Melo | 30d7a77 | 2009-07-02 21:24:14 -0300 | [diff] [blame] | 610 | if (self->adjust_symbols) { |
Arnaldo Carvalho de Melo | f5812a7 | 2009-06-30 11:43:17 -0300 | [diff] [blame] | 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); |
Peter Zijlstra | 520f2c3 | 2009-06-22 16:52:51 +0200 | [diff] [blame] | 614 | |
Arnaldo Carvalho de Melo | f5812a7 | 2009-06-30 11:43:17 -0300 | [diff] [blame] | 615 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; |
| 616 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 617 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 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 | } |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 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; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 637 | |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 638 | f = symbol__new(sym.st_value, sym.st_size, name, |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 639 | self->sym_priv_size, obj_start, verbose); |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 640 | free(demangled); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 641 | if (!f) |
| 642 | goto out_elf_end; |
| 643 | |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 644 | if (filter && filter(self, f)) |
| 645 | symbol__delete(f, self->sym_priv_size); |
| 646 | else { |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 647 | f->module = mod; |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 648 | dso__insert_symbol(self, f); |
| 649 | nr++; |
| 650 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | err = nr; |
| 654 | out_elf_end: |
| 655 | elf_end(elf); |
| 656 | out_close: |
| 657 | return err; |
| 658 | } |
| 659 | |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 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 | } |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 709 | if (verbose >= 2) |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 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 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 735 | int dso__load(struct dso *self, symbol_filter_t filter, int verbose) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 736 | { |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 737 | int size = PATH_MAX; |
| 738 | char *name = malloc(size), *build_id = NULL; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 739 | int ret = -1; |
| 740 | int fd; |
| 741 | |
| 742 | if (!name) |
| 743 | return -1; |
| 744 | |
Arnaldo Carvalho de Melo | 30d7a77 | 2009-07-02 21:24:14 -0300 | [diff] [blame] | 745 | self->adjust_symbols = 0; |
Arnaldo Carvalho de Melo | f5812a7 | 2009-06-30 11:43:17 -0300 | [diff] [blame] | 746 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 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; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 755 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 756 | more: |
| 757 | do { |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 758 | self->origin++; |
| 759 | switch (self->origin) { |
| 760 | case DSO__ORIG_FEDORA: |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 761 | snprintf(name, size, "/usr/lib/debug%s.debug", self->name); |
| 762 | break; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 763 | case DSO__ORIG_UBUNTU: |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 764 | snprintf(name, size, "/usr/lib/debug%s", self->name); |
| 765 | break; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 766 | case DSO__ORIG_BUILDID: |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 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 | } |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 775 | self->origin++; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 776 | /* Fall thru */ |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 777 | case DSO__ORIG_DSO: |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 778 | snprintf(name, size, "%s", self->name); |
| 779 | break; |
| 780 | |
| 781 | default: |
| 782 | goto out; |
| 783 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 784 | |
| 785 | fd = open(name, O_RDONLY); |
| 786 | } while (fd < 0); |
| 787 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 788 | ret = dso__load_sym(self, fd, name, filter, verbose, NULL); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 789 | close(fd); |
| 790 | |
| 791 | /* |
| 792 | * Some people seem to have debuginfo files _WITHOUT_ debug info!?!? |
| 793 | */ |
| 794 | if (!ret) |
| 795 | goto more; |
| 796 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 797 | if (ret > 0) { |
| 798 | int nr_plt = dso__synthesize_plt_symbols(self, verbose); |
| 799 | if (nr_plt > 0) |
| 800 | ret += nr_plt; |
| 801 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 802 | out: |
| 803 | free(name); |
Arnaldo Carvalho de Melo | 1340e6b | 2009-08-11 17:04:36 -0300 | [diff] [blame] | 804 | if (ret < 0 && strstr(self->name, " (deleted)") != NULL) |
| 805 | return 0; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 806 | return ret; |
| 807 | } |
| 808 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 889 | static int dso__load_vmlinux(struct dso *self, const char *vmlinux, |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 890 | symbol_filter_t filter, int verbose) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 891 | { |
| 892 | int err, fd = open(vmlinux, O_RDONLY); |
| 893 | |
| 894 | if (fd < 0) |
| 895 | return -1; |
| 896 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 897 | err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL); |
| 898 | |
| 899 | if (err > 0) |
| 900 | dso__fill_symbol_holes(self); |
| 901 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 902 | close(fd); |
| 903 | |
| 904 | return err; |
| 905 | } |
| 906 | |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 907 | int dso__load_kernel(struct dso *self, const char *vmlinux, |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 908 | symbol_filter_t filter, int verbose, int modules) |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 909 | { |
| 910 | int err = -1; |
| 911 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 912 | if (vmlinux) { |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 913 | err = dso__load_vmlinux(self, vmlinux, filter, verbose); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 914 | if (err > 0 && modules) |
| 915 | err = dso__load_modules(self, filter, verbose); |
| 916 | } |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 917 | |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 918 | if (err <= 0) |
Ingo Molnar | bd74137 | 2009-06-04 14:13:04 +0200 | [diff] [blame] | 919 | err = dso__load_kallsyms(self, filter, verbose); |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 920 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 921 | if (err > 0) |
| 922 | self->origin = DSO__ORIG_KERNEL; |
| 923 | |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 924 | return err; |
| 925 | } |
| 926 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 927 | void symbol__init(void) |
| 928 | { |
| 929 | elf_version(EV_CURRENT); |
| 930 | } |