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