Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 1 | #include <fcntl.h> |
| 2 | #include <stdio.h> |
| 3 | #include <errno.h> |
| 4 | #include <string.h> |
| 5 | #include <unistd.h> |
| 6 | #include <inttypes.h> |
| 7 | |
| 8 | #include "symbol.h" |
| 9 | #include "debug.h" |
| 10 | |
Adrian Hunter | e955d5c | 2013-09-13 16:49:30 +0300 | [diff] [blame] | 11 | #ifndef HAVE_ELF_GETPHDRNUM |
| 12 | static int elf_getphdrnum(Elf *elf, size_t *dst) |
| 13 | { |
| 14 | GElf_Ehdr gehdr; |
| 15 | GElf_Ehdr *ehdr; |
| 16 | |
| 17 | ehdr = gelf_getehdr(elf, &gehdr); |
| 18 | if (!ehdr) |
| 19 | return -1; |
| 20 | |
| 21 | *dst = ehdr->e_phnum; |
| 22 | |
| 23 | return 0; |
| 24 | } |
| 25 | #endif |
| 26 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 27 | #ifndef NT_GNU_BUILD_ID |
| 28 | #define NT_GNU_BUILD_ID 3 |
| 29 | #endif |
| 30 | |
| 31 | /** |
| 32 | * elf_symtab__for_each_symbol - iterate thru all the symbols |
| 33 | * |
| 34 | * @syms: struct elf_symtab instance to iterate |
| 35 | * @idx: uint32_t idx |
| 36 | * @sym: GElf_Sym iterator |
| 37 | */ |
| 38 | #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \ |
| 39 | for (idx = 0, gelf_getsym(syms, idx, &sym);\ |
| 40 | idx < nr_syms; \ |
| 41 | idx++, gelf_getsym(syms, idx, &sym)) |
| 42 | |
| 43 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) |
| 44 | { |
| 45 | return GELF_ST_TYPE(sym->st_info); |
| 46 | } |
| 47 | |
| 48 | static inline int elf_sym__is_function(const GElf_Sym *sym) |
| 49 | { |
| 50 | return elf_sym__type(sym) == STT_FUNC && |
| 51 | sym->st_name != 0 && |
| 52 | sym->st_shndx != SHN_UNDEF; |
| 53 | } |
| 54 | |
| 55 | static inline bool elf_sym__is_object(const GElf_Sym *sym) |
| 56 | { |
| 57 | return elf_sym__type(sym) == STT_OBJECT && |
| 58 | sym->st_name != 0 && |
| 59 | sym->st_shndx != SHN_UNDEF; |
| 60 | } |
| 61 | |
| 62 | static inline int elf_sym__is_label(const GElf_Sym *sym) |
| 63 | { |
| 64 | return elf_sym__type(sym) == STT_NOTYPE && |
| 65 | sym->st_name != 0 && |
| 66 | sym->st_shndx != SHN_UNDEF && |
| 67 | sym->st_shndx != SHN_ABS; |
| 68 | } |
| 69 | |
| 70 | static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type) |
| 71 | { |
| 72 | switch (type) { |
| 73 | case MAP__FUNCTION: |
| 74 | return elf_sym__is_function(sym); |
| 75 | case MAP__VARIABLE: |
| 76 | return elf_sym__is_object(sym); |
| 77 | default: |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static inline const char *elf_sym__name(const GElf_Sym *sym, |
| 83 | const Elf_Data *symstrs) |
| 84 | { |
| 85 | return symstrs->d_buf + sym->st_name; |
| 86 | } |
| 87 | |
| 88 | static inline const char *elf_sec__name(const GElf_Shdr *shdr, |
| 89 | const Elf_Data *secstrs) |
| 90 | { |
| 91 | return secstrs->d_buf + shdr->sh_name; |
| 92 | } |
| 93 | |
| 94 | static inline int elf_sec__is_text(const GElf_Shdr *shdr, |
| 95 | const Elf_Data *secstrs) |
| 96 | { |
| 97 | return strstr(elf_sec__name(shdr, secstrs), "text") != NULL; |
| 98 | } |
| 99 | |
| 100 | static inline bool elf_sec__is_data(const GElf_Shdr *shdr, |
| 101 | const Elf_Data *secstrs) |
| 102 | { |
| 103 | return strstr(elf_sec__name(shdr, secstrs), "data") != NULL; |
| 104 | } |
| 105 | |
| 106 | static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs, |
| 107 | enum map_type type) |
| 108 | { |
| 109 | switch (type) { |
| 110 | case MAP__FUNCTION: |
| 111 | return elf_sec__is_text(shdr, secstrs); |
| 112 | case MAP__VARIABLE: |
| 113 | return elf_sec__is_data(shdr, secstrs); |
| 114 | default: |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr) |
| 120 | { |
| 121 | Elf_Scn *sec = NULL; |
| 122 | GElf_Shdr shdr; |
| 123 | size_t cnt = 1; |
| 124 | |
| 125 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
| 126 | gelf_getshdr(sec, &shdr); |
| 127 | |
| 128 | if ((addr >= shdr.sh_addr) && |
| 129 | (addr < (shdr.sh_addr + shdr.sh_size))) |
| 130 | return cnt; |
| 131 | |
| 132 | ++cnt; |
| 133 | } |
| 134 | |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, |
| 139 | GElf_Shdr *shp, const char *name, |
| 140 | size_t *idx) |
| 141 | { |
| 142 | Elf_Scn *sec = NULL; |
| 143 | size_t cnt = 1; |
| 144 | |
Cody P Schafer | 4927465 | 2012-08-10 15:22:55 -0700 | [diff] [blame] | 145 | /* Elf is corrupted/truncated, avoid calling elf_strptr. */ |
| 146 | if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) |
| 147 | return NULL; |
| 148 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 149 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
| 150 | char *str; |
| 151 | |
| 152 | gelf_getshdr(sec, shp); |
| 153 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); |
| 154 | if (!strcmp(name, str)) { |
| 155 | if (idx) |
| 156 | *idx = cnt; |
| 157 | break; |
| 158 | } |
| 159 | ++cnt; |
| 160 | } |
| 161 | |
| 162 | return sec; |
| 163 | } |
| 164 | |
| 165 | #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ |
| 166 | for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \ |
| 167 | idx < nr_entries; \ |
| 168 | ++idx, pos = gelf_getrel(reldata, idx, &pos_mem)) |
| 169 | |
| 170 | #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \ |
| 171 | for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \ |
| 172 | idx < nr_entries; \ |
| 173 | ++idx, pos = gelf_getrela(reldata, idx, &pos_mem)) |
| 174 | |
| 175 | /* |
| 176 | * We need to check if we have a .dynsym, so that we can handle the |
| 177 | * .plt, synthesizing its symbols, that aren't on the symtabs (be it |
| 178 | * .dynsym or .symtab). |
| 179 | * And always look at the original dso, not at debuginfo packages, that |
| 180 | * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). |
| 181 | */ |
Cody P Schafer | a44f605 | 2012-08-10 15:22:59 -0700 | [diff] [blame] | 182 | int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map, |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 183 | symbol_filter_t filter) |
| 184 | { |
| 185 | uint32_t nr_rel_entries, idx; |
| 186 | GElf_Sym sym; |
| 187 | u64 plt_offset; |
| 188 | GElf_Shdr shdr_plt; |
| 189 | struct symbol *f; |
| 190 | GElf_Shdr shdr_rel_plt, shdr_dynsym; |
| 191 | Elf_Data *reldata, *syms, *symstrs; |
| 192 | Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; |
| 193 | size_t dynsym_idx; |
| 194 | GElf_Ehdr ehdr; |
| 195 | char sympltname[1024]; |
| 196 | Elf *elf; |
Cody P Schafer | a44f605 | 2012-08-10 15:22:59 -0700 | [diff] [blame] | 197 | int nr = 0, symidx, err = 0; |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 198 | |
David Ahern | f47b58b | 2012-08-19 09:47:14 -0600 | [diff] [blame] | 199 | if (!ss->dynsym) |
| 200 | return 0; |
| 201 | |
Cody P Schafer | a44f605 | 2012-08-10 15:22:59 -0700 | [diff] [blame] | 202 | elf = ss->elf; |
| 203 | ehdr = ss->ehdr; |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 204 | |
Cody P Schafer | a44f605 | 2012-08-10 15:22:59 -0700 | [diff] [blame] | 205 | scn_dynsym = ss->dynsym; |
| 206 | shdr_dynsym = ss->dynshdr; |
| 207 | dynsym_idx = ss->dynsym_idx; |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 208 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 209 | if (scn_dynsym == NULL) |
| 210 | goto out_elf_end; |
| 211 | |
| 212 | scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, |
| 213 | ".rela.plt", NULL); |
| 214 | if (scn_plt_rel == NULL) { |
| 215 | scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, |
| 216 | ".rel.plt", NULL); |
| 217 | if (scn_plt_rel == NULL) |
| 218 | goto out_elf_end; |
| 219 | } |
| 220 | |
| 221 | err = -1; |
| 222 | |
| 223 | if (shdr_rel_plt.sh_link != dynsym_idx) |
| 224 | goto out_elf_end; |
| 225 | |
| 226 | if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL) |
| 227 | goto out_elf_end; |
| 228 | |
| 229 | /* |
| 230 | * Fetch the relocation section to find the idxes to the GOT |
| 231 | * and the symbols in the .dynsym they refer to. |
| 232 | */ |
| 233 | reldata = elf_getdata(scn_plt_rel, NULL); |
| 234 | if (reldata == NULL) |
| 235 | goto out_elf_end; |
| 236 | |
| 237 | syms = elf_getdata(scn_dynsym, NULL); |
| 238 | if (syms == NULL) |
| 239 | goto out_elf_end; |
| 240 | |
| 241 | scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link); |
| 242 | if (scn_symstrs == NULL) |
| 243 | goto out_elf_end; |
| 244 | |
| 245 | symstrs = elf_getdata(scn_symstrs, NULL); |
| 246 | if (symstrs == NULL) |
| 247 | goto out_elf_end; |
| 248 | |
Cody P Schafer | 52f9ddb | 2012-08-10 15:22:51 -0700 | [diff] [blame] | 249 | if (symstrs->d_size == 0) |
| 250 | goto out_elf_end; |
| 251 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 252 | nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; |
| 253 | plt_offset = shdr_plt.sh_offset; |
| 254 | |
| 255 | if (shdr_rel_plt.sh_type == SHT_RELA) { |
| 256 | GElf_Rela pos_mem, *pos; |
| 257 | |
| 258 | elf_section__for_each_rela(reldata, pos, pos_mem, idx, |
| 259 | nr_rel_entries) { |
| 260 | symidx = GELF_R_SYM(pos->r_info); |
| 261 | plt_offset += shdr_plt.sh_entsize; |
| 262 | gelf_getsym(syms, symidx, &sym); |
| 263 | snprintf(sympltname, sizeof(sympltname), |
| 264 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 265 | |
| 266 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
| 267 | STB_GLOBAL, sympltname); |
| 268 | if (!f) |
| 269 | goto out_elf_end; |
| 270 | |
| 271 | if (filter && filter(map, f)) |
| 272 | symbol__delete(f); |
| 273 | else { |
| 274 | symbols__insert(&dso->symbols[map->type], f); |
| 275 | ++nr; |
| 276 | } |
| 277 | } |
| 278 | } else if (shdr_rel_plt.sh_type == SHT_REL) { |
| 279 | GElf_Rel pos_mem, *pos; |
| 280 | elf_section__for_each_rel(reldata, pos, pos_mem, idx, |
| 281 | nr_rel_entries) { |
| 282 | symidx = GELF_R_SYM(pos->r_info); |
| 283 | plt_offset += shdr_plt.sh_entsize; |
| 284 | gelf_getsym(syms, symidx, &sym); |
| 285 | snprintf(sympltname, sizeof(sympltname), |
| 286 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 287 | |
| 288 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
| 289 | STB_GLOBAL, sympltname); |
| 290 | if (!f) |
| 291 | goto out_elf_end; |
| 292 | |
| 293 | if (filter && filter(map, f)) |
| 294 | symbol__delete(f); |
| 295 | else { |
| 296 | symbols__insert(&dso->symbols[map->type], f); |
| 297 | ++nr; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | err = 0; |
| 303 | out_elf_end: |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 304 | if (err == 0) |
| 305 | return nr; |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 306 | pr_debug("%s: problems reading %s PLT info.\n", |
| 307 | __func__, dso->long_name); |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Align offset to 4 bytes as needed for note name and descriptor data. |
| 313 | */ |
| 314 | #define NOTE_ALIGN(n) (((n) + 3) & -4U) |
| 315 | |
| 316 | static int elf_read_build_id(Elf *elf, void *bf, size_t size) |
| 317 | { |
| 318 | int err = -1; |
| 319 | GElf_Ehdr ehdr; |
| 320 | GElf_Shdr shdr; |
| 321 | Elf_Data *data; |
| 322 | Elf_Scn *sec; |
| 323 | Elf_Kind ek; |
| 324 | void *ptr; |
| 325 | |
| 326 | if (size < BUILD_ID_SIZE) |
| 327 | goto out; |
| 328 | |
| 329 | ek = elf_kind(elf); |
| 330 | if (ek != ELF_K_ELF) |
| 331 | goto out; |
| 332 | |
| 333 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
| 334 | pr_err("%s: cannot get elf header.\n", __func__); |
| 335 | goto out; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Check following sections for notes: |
| 340 | * '.note.gnu.build-id' |
| 341 | * '.notes' |
| 342 | * '.note' (VDSO specific) |
| 343 | */ |
| 344 | do { |
| 345 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 346 | ".note.gnu.build-id", NULL); |
| 347 | if (sec) |
| 348 | break; |
| 349 | |
| 350 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 351 | ".notes", NULL); |
| 352 | if (sec) |
| 353 | break; |
| 354 | |
| 355 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 356 | ".note", NULL); |
| 357 | if (sec) |
| 358 | break; |
| 359 | |
| 360 | return err; |
| 361 | |
| 362 | } while (0); |
| 363 | |
| 364 | data = elf_getdata(sec, NULL); |
| 365 | if (data == NULL) |
| 366 | goto out; |
| 367 | |
| 368 | ptr = data->d_buf; |
| 369 | while (ptr < (data->d_buf + data->d_size)) { |
| 370 | GElf_Nhdr *nhdr = ptr; |
| 371 | size_t namesz = NOTE_ALIGN(nhdr->n_namesz), |
| 372 | descsz = NOTE_ALIGN(nhdr->n_descsz); |
| 373 | const char *name; |
| 374 | |
| 375 | ptr += sizeof(*nhdr); |
| 376 | name = ptr; |
| 377 | ptr += namesz; |
| 378 | if (nhdr->n_type == NT_GNU_BUILD_ID && |
| 379 | nhdr->n_namesz == sizeof("GNU")) { |
| 380 | if (memcmp(name, "GNU", sizeof("GNU")) == 0) { |
| 381 | size_t sz = min(size, descsz); |
| 382 | memcpy(bf, ptr, sz); |
| 383 | memset(bf + sz, 0, size - sz); |
| 384 | err = descsz; |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | ptr += descsz; |
| 389 | } |
| 390 | |
| 391 | out: |
| 392 | return err; |
| 393 | } |
| 394 | |
| 395 | int filename__read_build_id(const char *filename, void *bf, size_t size) |
| 396 | { |
| 397 | int fd, err = -1; |
| 398 | Elf *elf; |
| 399 | |
| 400 | if (size < BUILD_ID_SIZE) |
| 401 | goto out; |
| 402 | |
| 403 | fd = open(filename, O_RDONLY); |
| 404 | if (fd < 0) |
| 405 | goto out; |
| 406 | |
| 407 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 408 | if (elf == NULL) { |
| 409 | pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); |
| 410 | goto out_close; |
| 411 | } |
| 412 | |
| 413 | err = elf_read_build_id(elf, bf, size); |
| 414 | |
| 415 | elf_end(elf); |
| 416 | out_close: |
| 417 | close(fd); |
| 418 | out: |
| 419 | return err; |
| 420 | } |
| 421 | |
| 422 | int sysfs__read_build_id(const char *filename, void *build_id, size_t size) |
| 423 | { |
| 424 | int fd, err = -1; |
| 425 | |
| 426 | if (size < BUILD_ID_SIZE) |
| 427 | goto out; |
| 428 | |
| 429 | fd = open(filename, O_RDONLY); |
| 430 | if (fd < 0) |
| 431 | goto out; |
| 432 | |
| 433 | while (1) { |
| 434 | char bf[BUFSIZ]; |
| 435 | GElf_Nhdr nhdr; |
| 436 | size_t namesz, descsz; |
| 437 | |
| 438 | if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr)) |
| 439 | break; |
| 440 | |
| 441 | namesz = NOTE_ALIGN(nhdr.n_namesz); |
| 442 | descsz = NOTE_ALIGN(nhdr.n_descsz); |
| 443 | if (nhdr.n_type == NT_GNU_BUILD_ID && |
| 444 | nhdr.n_namesz == sizeof("GNU")) { |
| 445 | if (read(fd, bf, namesz) != (ssize_t)namesz) |
| 446 | break; |
| 447 | if (memcmp(bf, "GNU", sizeof("GNU")) == 0) { |
| 448 | size_t sz = min(descsz, size); |
| 449 | if (read(fd, build_id, sz) == (ssize_t)sz) { |
| 450 | memset(build_id + sz, 0, size - sz); |
| 451 | err = 0; |
| 452 | break; |
| 453 | } |
| 454 | } else if (read(fd, bf, descsz) != (ssize_t)descsz) |
| 455 | break; |
| 456 | } else { |
| 457 | int n = namesz + descsz; |
| 458 | if (read(fd, bf, n) != n) |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | close(fd); |
| 463 | out: |
| 464 | return err; |
| 465 | } |
| 466 | |
| 467 | int filename__read_debuglink(const char *filename, char *debuglink, |
| 468 | size_t size) |
| 469 | { |
| 470 | int fd, err = -1; |
| 471 | Elf *elf; |
| 472 | GElf_Ehdr ehdr; |
| 473 | GElf_Shdr shdr; |
| 474 | Elf_Data *data; |
| 475 | Elf_Scn *sec; |
| 476 | Elf_Kind ek; |
| 477 | |
| 478 | fd = open(filename, O_RDONLY); |
| 479 | if (fd < 0) |
| 480 | goto out; |
| 481 | |
| 482 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 483 | if (elf == NULL) { |
| 484 | pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); |
| 485 | goto out_close; |
| 486 | } |
| 487 | |
| 488 | ek = elf_kind(elf); |
| 489 | if (ek != ELF_K_ELF) |
| 490 | goto out_close; |
| 491 | |
| 492 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
| 493 | pr_err("%s: cannot get elf header.\n", __func__); |
| 494 | goto out_close; |
| 495 | } |
| 496 | |
| 497 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 498 | ".gnu_debuglink", NULL); |
| 499 | if (sec == NULL) |
| 500 | goto out_close; |
| 501 | |
| 502 | data = elf_getdata(sec, NULL); |
| 503 | if (data == NULL) |
| 504 | goto out_close; |
| 505 | |
| 506 | /* the start of this section is a zero-terminated string */ |
| 507 | strncpy(debuglink, data->d_buf, size); |
| 508 | |
| 509 | elf_end(elf); |
| 510 | |
| 511 | out_close: |
| 512 | close(fd); |
| 513 | out: |
| 514 | return err; |
| 515 | } |
| 516 | |
| 517 | static int dso__swap_init(struct dso *dso, unsigned char eidata) |
| 518 | { |
| 519 | static unsigned int const endian = 1; |
| 520 | |
| 521 | dso->needs_swap = DSO_SWAP__NO; |
| 522 | |
| 523 | switch (eidata) { |
| 524 | case ELFDATA2LSB: |
| 525 | /* We are big endian, DSO is little endian. */ |
| 526 | if (*(unsigned char const *)&endian != 1) |
| 527 | dso->needs_swap = DSO_SWAP__YES; |
| 528 | break; |
| 529 | |
| 530 | case ELFDATA2MSB: |
| 531 | /* We are little endian, DSO is big endian. */ |
| 532 | if (*(unsigned char const *)&endian != 0) |
| 533 | dso->needs_swap = DSO_SWAP__YES; |
| 534 | break; |
| 535 | |
| 536 | default: |
| 537 | pr_err("unrecognized DSO data encoding %d\n", eidata); |
| 538 | return -EINVAL; |
| 539 | } |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 544 | bool symsrc__possibly_runtime(struct symsrc *ss) |
| 545 | { |
| 546 | return ss->dynsym || ss->opdsec; |
| 547 | } |
| 548 | |
Cody P Schafer | d26cd12 | 2012-08-10 15:23:00 -0700 | [diff] [blame] | 549 | bool symsrc__has_symtab(struct symsrc *ss) |
| 550 | { |
| 551 | return ss->symtab != NULL; |
| 552 | } |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 553 | |
| 554 | void symsrc__destroy(struct symsrc *ss) |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 555 | { |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 556 | free(ss->name); |
| 557 | elf_end(ss->elf); |
| 558 | close(ss->fd); |
| 559 | } |
| 560 | |
| 561 | int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name, |
| 562 | enum dso_binary_type type) |
| 563 | { |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 564 | int err = -1; |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 565 | GElf_Ehdr ehdr; |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 566 | Elf *elf; |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 567 | int fd; |
| 568 | |
| 569 | fd = open(name, O_RDONLY); |
| 570 | if (fd < 0) |
| 571 | return -1; |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 572 | |
| 573 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 574 | if (elf == NULL) { |
| 575 | pr_debug("%s: cannot read %s ELF file.\n", __func__, name); |
| 576 | goto out_close; |
| 577 | } |
| 578 | |
| 579 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
| 580 | pr_debug("%s: cannot get elf header.\n", __func__); |
| 581 | goto out_elf_end; |
| 582 | } |
| 583 | |
| 584 | if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) |
| 585 | goto out_elf_end; |
| 586 | |
| 587 | /* Always reject images with a mismatched build-id: */ |
| 588 | if (dso->has_build_id) { |
| 589 | u8 build_id[BUILD_ID_SIZE]; |
| 590 | |
| 591 | if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0) |
| 592 | goto out_elf_end; |
| 593 | |
| 594 | if (!dso__build_id_equal(dso, build_id)) |
| 595 | goto out_elf_end; |
| 596 | } |
| 597 | |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 598 | ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab", |
| 599 | NULL); |
| 600 | if (ss->symshdr.sh_type != SHT_SYMTAB) |
| 601 | ss->symtab = NULL; |
| 602 | |
| 603 | ss->dynsym_idx = 0; |
| 604 | ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym", |
| 605 | &ss->dynsym_idx); |
| 606 | if (ss->dynshdr.sh_type != SHT_DYNSYM) |
| 607 | ss->dynsym = NULL; |
| 608 | |
| 609 | ss->opdidx = 0; |
| 610 | ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd", |
| 611 | &ss->opdidx); |
| 612 | if (ss->opdshdr.sh_type != SHT_PROGBITS) |
| 613 | ss->opdsec = NULL; |
| 614 | |
| 615 | if (dso->kernel == DSO_TYPE_USER) { |
| 616 | GElf_Shdr shdr; |
| 617 | ss->adjust_symbols = (ehdr.e_type == ET_EXEC || |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 618 | ehdr.e_type == ET_REL || |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 619 | elf_section_by_name(elf, &ehdr, &shdr, |
| 620 | ".gnu.prelink_undo", |
| 621 | NULL) != NULL); |
| 622 | } else { |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 623 | ss->adjust_symbols = ehdr.e_type == ET_EXEC || |
| 624 | ehdr.e_type == ET_REL; |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | ss->name = strdup(name); |
| 628 | if (!ss->name) |
| 629 | goto out_elf_end; |
| 630 | |
| 631 | ss->elf = elf; |
| 632 | ss->fd = fd; |
| 633 | ss->ehdr = ehdr; |
| 634 | ss->type = type; |
| 635 | |
| 636 | return 0; |
| 637 | |
| 638 | out_elf_end: |
| 639 | elf_end(elf); |
| 640 | out_close: |
| 641 | close(fd); |
| 642 | return err; |
| 643 | } |
| 644 | |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 645 | /** |
| 646 | * ref_reloc_sym_not_found - has kernel relocation symbol been found. |
| 647 | * @kmap: kernel maps and relocation reference symbol |
| 648 | * |
| 649 | * This function returns %true if we are dealing with the kernel maps and the |
| 650 | * relocation reference symbol has not yet been found. Otherwise %false is |
| 651 | * returned. |
| 652 | */ |
| 653 | static bool ref_reloc_sym_not_found(struct kmap *kmap) |
| 654 | { |
| 655 | return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name && |
| 656 | !kmap->ref_reloc_sym->unrelocated_addr; |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * ref_reloc - kernel relocation offset. |
| 661 | * @kmap: kernel maps and relocation reference symbol |
| 662 | * |
| 663 | * This function returns the offset of kernel addresses as determined by using |
| 664 | * the relocation reference symbol i.e. if the kernel has not been relocated |
| 665 | * then the return value is zero. |
| 666 | */ |
| 667 | static u64 ref_reloc(struct kmap *kmap) |
| 668 | { |
| 669 | if (kmap && kmap->ref_reloc_sym && |
| 670 | kmap->ref_reloc_sym->unrelocated_addr) |
| 671 | return kmap->ref_reloc_sym->addr - |
| 672 | kmap->ref_reloc_sym->unrelocated_addr; |
| 673 | return 0; |
| 674 | } |
| 675 | |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 676 | int dso__load_sym(struct dso *dso, struct map *map, |
| 677 | struct symsrc *syms_ss, struct symsrc *runtime_ss, |
Cody P Schafer | d26cd12 | 2012-08-10 15:23:00 -0700 | [diff] [blame] | 678 | symbol_filter_t filter, int kmodule) |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 679 | { |
| 680 | struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL; |
| 681 | struct map *curr_map = map; |
| 682 | struct dso *curr_dso = dso; |
| 683 | Elf_Data *symstrs, *secstrs; |
| 684 | uint32_t nr_syms; |
| 685 | int err = -1; |
| 686 | uint32_t idx; |
| 687 | GElf_Ehdr ehdr; |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 688 | GElf_Shdr shdr; |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 689 | Elf_Data *syms, *opddata = NULL; |
| 690 | GElf_Sym sym; |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 691 | Elf_Scn *sec, *sec_strndx; |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 692 | Elf *elf; |
| 693 | int nr = 0; |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 694 | bool remap_kernel = false, adjust_kernel_syms = false; |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 695 | |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 696 | dso->symtab_type = syms_ss->type; |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 697 | dso->rel = syms_ss->ehdr.e_type == ET_REL; |
| 698 | |
| 699 | /* |
| 700 | * Modules may already have symbols from kallsyms, but those symbols |
| 701 | * have the wrong values for the dso maps, so remove them. |
| 702 | */ |
| 703 | if (kmodule && syms_ss->symtab) |
| 704 | symbols__delete(&dso->symbols[map->type]); |
Cody P Schafer | 005f929 | 2012-08-10 15:22:58 -0700 | [diff] [blame] | 705 | |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 706 | if (!syms_ss->symtab) { |
| 707 | syms_ss->symtab = syms_ss->dynsym; |
| 708 | syms_ss->symshdr = syms_ss->dynshdr; |
Cody P Schafer | d26cd12 | 2012-08-10 15:23:00 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 711 | elf = syms_ss->elf; |
| 712 | ehdr = syms_ss->ehdr; |
| 713 | sec = syms_ss->symtab; |
| 714 | shdr = syms_ss->symshdr; |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 715 | |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 716 | if (runtime_ss->opdsec) |
| 717 | opddata = elf_rawdata(runtime_ss->opdsec, NULL); |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 718 | |
| 719 | syms = elf_getdata(sec, NULL); |
| 720 | if (syms == NULL) |
| 721 | goto out_elf_end; |
| 722 | |
| 723 | sec = elf_getscn(elf, shdr.sh_link); |
| 724 | if (sec == NULL) |
| 725 | goto out_elf_end; |
| 726 | |
| 727 | symstrs = elf_getdata(sec, NULL); |
| 728 | if (symstrs == NULL) |
| 729 | goto out_elf_end; |
| 730 | |
| 731 | sec_strndx = elf_getscn(elf, ehdr.e_shstrndx); |
| 732 | if (sec_strndx == NULL) |
| 733 | goto out_elf_end; |
| 734 | |
| 735 | secstrs = elf_getdata(sec_strndx, NULL); |
| 736 | if (secstrs == NULL) |
| 737 | goto out_elf_end; |
| 738 | |
| 739 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
| 740 | |
| 741 | memset(&sym, 0, sizeof(sym)); |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 742 | |
| 743 | /* |
| 744 | * The kernel relocation symbol is needed in advance in order to adjust |
| 745 | * kernel maps correctly. |
| 746 | */ |
| 747 | if (ref_reloc_sym_not_found(kmap)) { |
| 748 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { |
| 749 | const char *elf_name = elf_sym__name(&sym, symstrs); |
| 750 | |
| 751 | if (strcmp(elf_name, kmap->ref_reloc_sym->name)) |
| 752 | continue; |
| 753 | kmap->ref_reloc_sym->unrelocated_addr = sym.st_value; |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap); |
| 759 | /* |
| 760 | * Initial kernel and module mappings do not map to the dso. For |
| 761 | * function mappings, flag the fixups. |
| 762 | */ |
| 763 | if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) { |
| 764 | remap_kernel = true; |
| 765 | adjust_kernel_syms = dso->adjust_symbols; |
| 766 | } |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 767 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { |
| 768 | struct symbol *f; |
| 769 | const char *elf_name = elf_sym__name(&sym, symstrs); |
| 770 | char *demangled = NULL; |
| 771 | int is_label = elf_sym__is_label(&sym); |
| 772 | const char *section_name; |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 773 | bool used_opd = false; |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 774 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 775 | if (!is_label && !elf_sym__is_a(&sym, map->type)) |
| 776 | continue; |
| 777 | |
| 778 | /* Reject ARM ELF "mapping symbols": these aren't unique and |
| 779 | * don't identify functions, so will confuse the profile |
| 780 | * output: */ |
| 781 | if (ehdr.e_machine == EM_ARM) { |
| 782 | if (!strcmp(elf_name, "$a") || |
| 783 | !strcmp(elf_name, "$d") || |
| 784 | !strcmp(elf_name, "$t")) |
| 785 | continue; |
| 786 | } |
| 787 | |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 788 | if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) { |
| 789 | u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr; |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 790 | u64 *opd = opddata->d_buf + offset; |
| 791 | sym.st_value = DSO__SWAP(dso, u64, *opd); |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 792 | sym.st_shndx = elf_addr_to_index(runtime_ss->elf, |
| 793 | sym.st_value); |
| 794 | used_opd = true; |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 795 | } |
Namhyung Kim | 3843b05 | 2012-11-21 13:49:44 +0100 | [diff] [blame] | 796 | /* |
| 797 | * When loading symbols in a data mapping, ABS symbols (which |
| 798 | * has a value of SHN_ABS in its st_shndx) failed at |
| 799 | * elf_getscn(). And it marks the loading as a failure so |
| 800 | * already loaded symbols cannot be fixed up. |
| 801 | * |
| 802 | * I'm not sure what should be done. Just ignore them for now. |
| 803 | * - Namhyung Kim |
| 804 | */ |
| 805 | if (sym.st_shndx == SHN_ABS) |
| 806 | continue; |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 807 | |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 808 | sec = elf_getscn(runtime_ss->elf, sym.st_shndx); |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 809 | if (!sec) |
| 810 | goto out_elf_end; |
| 811 | |
| 812 | gelf_getshdr(sec, &shdr); |
| 813 | |
| 814 | if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type)) |
| 815 | continue; |
| 816 | |
| 817 | section_name = elf_sec__name(&shdr, secstrs); |
| 818 | |
| 819 | /* On ARM, symbols for thumb functions have 1 added to |
| 820 | * the symbol address as a flag - remove it */ |
| 821 | if ((ehdr.e_machine == EM_ARM) && |
| 822 | (map->type == MAP__FUNCTION) && |
| 823 | (sym.st_value & 1)) |
| 824 | --sym.st_value; |
| 825 | |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 826 | if (dso->kernel || kmodule) { |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 827 | char dso_name[PATH_MAX]; |
| 828 | |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 829 | /* Adjust symbol to map to file offset */ |
| 830 | if (adjust_kernel_syms) |
| 831 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; |
| 832 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 833 | if (strcmp(section_name, |
| 834 | (curr_dso->short_name + |
| 835 | dso->short_name_len)) == 0) |
| 836 | goto new_symbol; |
| 837 | |
| 838 | if (strcmp(section_name, ".text") == 0) { |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 839 | /* |
| 840 | * The initial kernel mapping is based on |
| 841 | * kallsyms and identity maps. Overwrite it to |
| 842 | * map to the kernel dso. |
| 843 | */ |
| 844 | if (remap_kernel && dso->kernel) { |
| 845 | remap_kernel = false; |
| 846 | map->start = shdr.sh_addr + |
| 847 | ref_reloc(kmap); |
| 848 | map->end = map->start + shdr.sh_size; |
| 849 | map->pgoff = shdr.sh_offset; |
| 850 | map->map_ip = map__map_ip; |
| 851 | map->unmap_ip = map__unmap_ip; |
| 852 | /* Ensure maps are correctly ordered */ |
| 853 | map_groups__remove(kmap->kmaps, map); |
| 854 | map_groups__insert(kmap->kmaps, map); |
| 855 | } |
| 856 | |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 857 | /* |
| 858 | * The initial module mapping is based on |
| 859 | * /proc/modules mapped to offset zero. |
| 860 | * Overwrite it to map to the module dso. |
| 861 | */ |
| 862 | if (remap_kernel && kmodule) { |
| 863 | remap_kernel = false; |
| 864 | map->pgoff = shdr.sh_offset; |
| 865 | } |
| 866 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 867 | curr_map = map; |
| 868 | curr_dso = dso; |
| 869 | goto new_symbol; |
| 870 | } |
| 871 | |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 872 | if (!kmap) |
| 873 | goto new_symbol; |
| 874 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 875 | snprintf(dso_name, sizeof(dso_name), |
| 876 | "%s%s", dso->short_name, section_name); |
| 877 | |
| 878 | curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name); |
| 879 | if (curr_map == NULL) { |
| 880 | u64 start = sym.st_value; |
| 881 | |
| 882 | if (kmodule) |
| 883 | start += map->start + shdr.sh_offset; |
| 884 | |
| 885 | curr_dso = dso__new(dso_name); |
| 886 | if (curr_dso == NULL) |
| 887 | goto out_elf_end; |
| 888 | curr_dso->kernel = dso->kernel; |
| 889 | curr_dso->long_name = dso->long_name; |
| 890 | curr_dso->long_name_len = dso->long_name_len; |
| 891 | curr_map = map__new2(start, curr_dso, |
| 892 | map->type); |
| 893 | if (curr_map == NULL) { |
| 894 | dso__delete(curr_dso); |
| 895 | goto out_elf_end; |
| 896 | } |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 897 | if (adjust_kernel_syms) { |
| 898 | curr_map->start = shdr.sh_addr + |
| 899 | ref_reloc(kmap); |
| 900 | curr_map->end = curr_map->start + |
| 901 | shdr.sh_size; |
| 902 | curr_map->pgoff = shdr.sh_offset; |
| 903 | } else { |
| 904 | curr_map->map_ip = identity__map_ip; |
| 905 | curr_map->unmap_ip = identity__map_ip; |
| 906 | } |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 907 | curr_dso->symtab_type = dso->symtab_type; |
| 908 | map_groups__insert(kmap->kmaps, curr_map); |
| 909 | dsos__add(&dso->node, curr_dso); |
| 910 | dso__set_loaded(curr_dso, map->type); |
| 911 | } else |
| 912 | curr_dso = curr_map->dso; |
| 913 | |
| 914 | goto new_symbol; |
| 915 | } |
| 916 | |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 917 | if ((used_opd && runtime_ss->adjust_symbols) |
| 918 | || (!used_opd && syms_ss->adjust_symbols)) { |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 919 | pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " " |
| 920 | "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__, |
| 921 | (u64)sym.st_value, (u64)shdr.sh_addr, |
| 922 | (u64)shdr.sh_offset); |
| 923 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; |
| 924 | } |
| 925 | /* |
| 926 | * We need to figure out if the object was created from C++ sources |
| 927 | * DWARF DW_compile_unit has this, but we don't always have access |
| 928 | * to it... |
| 929 | */ |
Namhyung Kim | 328ccda | 2013-03-25 18:18:18 +0900 | [diff] [blame] | 930 | if (symbol_conf.demangle) { |
Ingo Molnar | 14951f2 | 2013-09-29 16:12:54 +0200 | [diff] [blame] | 931 | demangled = bfd_demangle(NULL, elf_name, |
Namhyung Kim | 328ccda | 2013-03-25 18:18:18 +0900 | [diff] [blame] | 932 | DMGL_PARAMS | DMGL_ANSI); |
| 933 | if (demangled != NULL) |
| 934 | elf_name = demangled; |
| 935 | } |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 936 | new_symbol: |
| 937 | f = symbol__new(sym.st_value, sym.st_size, |
| 938 | GELF_ST_BIND(sym.st_info), elf_name); |
| 939 | free(demangled); |
| 940 | if (!f) |
| 941 | goto out_elf_end; |
| 942 | |
| 943 | if (filter && filter(curr_map, f)) |
| 944 | symbol__delete(f); |
| 945 | else { |
| 946 | symbols__insert(&curr_dso->symbols[curr_map->type], f); |
| 947 | nr++; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * For misannotated, zeroed, ASM function sizes. |
| 953 | */ |
| 954 | if (nr > 0) { |
| 955 | symbols__fixup_duplicate(&dso->symbols[map->type]); |
| 956 | symbols__fixup_end(&dso->symbols[map->type]); |
| 957 | if (kmap) { |
| 958 | /* |
| 959 | * We need to fixup this here too because we create new |
| 960 | * maps here, for things like vsyscall sections. |
| 961 | */ |
| 962 | __map_groups__fixup_end(kmap->kmaps, map->type); |
| 963 | } |
| 964 | } |
| 965 | err = nr; |
| 966 | out_elf_end: |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 967 | return err; |
| 968 | } |
| 969 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 970 | static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data) |
| 971 | { |
| 972 | GElf_Phdr phdr; |
| 973 | size_t i, phdrnum; |
| 974 | int err; |
| 975 | u64 sz; |
| 976 | |
| 977 | if (elf_getphdrnum(elf, &phdrnum)) |
| 978 | return -1; |
| 979 | |
| 980 | for (i = 0; i < phdrnum; i++) { |
| 981 | if (gelf_getphdr(elf, i, &phdr) == NULL) |
| 982 | return -1; |
| 983 | if (phdr.p_type != PT_LOAD) |
| 984 | continue; |
| 985 | if (exe) { |
| 986 | if (!(phdr.p_flags & PF_X)) |
| 987 | continue; |
| 988 | } else { |
| 989 | if (!(phdr.p_flags & PF_R)) |
| 990 | continue; |
| 991 | } |
| 992 | sz = min(phdr.p_memsz, phdr.p_filesz); |
| 993 | if (!sz) |
| 994 | continue; |
| 995 | err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data); |
| 996 | if (err) |
| 997 | return err; |
| 998 | } |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data, |
| 1003 | bool *is_64_bit) |
| 1004 | { |
| 1005 | int err; |
| 1006 | Elf *elf; |
| 1007 | |
| 1008 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 1009 | if (elf == NULL) |
| 1010 | return -1; |
| 1011 | |
| 1012 | if (is_64_bit) |
| 1013 | *is_64_bit = (gelf_getclass(elf) == ELFCLASS64); |
| 1014 | |
| 1015 | err = elf_read_maps(elf, exe, mapfn, data); |
| 1016 | |
| 1017 | elf_end(elf); |
| 1018 | return err; |
| 1019 | } |
| 1020 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 1021 | void symbol__elf_init(void) |
| 1022 | { |
| 1023 | elf_version(EV_CURRENT); |
| 1024 | } |