Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 1 | #include <libelf.h> |
| 2 | #include <gelf.h> |
| 3 | #include <elf.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <stdio.h> |
| 6 | #include <errno.h> |
| 7 | #include <string.h> |
| 8 | #include <unistd.h> |
| 9 | #include <inttypes.h> |
| 10 | |
| 11 | #include "symbol.h" |
| 12 | #include "debug.h" |
| 13 | |
| 14 | #ifndef NT_GNU_BUILD_ID |
| 15 | #define NT_GNU_BUILD_ID 3 |
| 16 | #endif |
| 17 | |
| 18 | /** |
| 19 | * elf_symtab__for_each_symbol - iterate thru all the symbols |
| 20 | * |
| 21 | * @syms: struct elf_symtab instance to iterate |
| 22 | * @idx: uint32_t idx |
| 23 | * @sym: GElf_Sym iterator |
| 24 | */ |
| 25 | #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \ |
| 26 | for (idx = 0, gelf_getsym(syms, idx, &sym);\ |
| 27 | idx < nr_syms; \ |
| 28 | idx++, gelf_getsym(syms, idx, &sym)) |
| 29 | |
| 30 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) |
| 31 | { |
| 32 | return GELF_ST_TYPE(sym->st_info); |
| 33 | } |
| 34 | |
| 35 | static inline int elf_sym__is_function(const GElf_Sym *sym) |
| 36 | { |
| 37 | return elf_sym__type(sym) == STT_FUNC && |
| 38 | sym->st_name != 0 && |
| 39 | sym->st_shndx != SHN_UNDEF; |
| 40 | } |
| 41 | |
| 42 | static inline bool elf_sym__is_object(const GElf_Sym *sym) |
| 43 | { |
| 44 | return elf_sym__type(sym) == STT_OBJECT && |
| 45 | sym->st_name != 0 && |
| 46 | sym->st_shndx != SHN_UNDEF; |
| 47 | } |
| 48 | |
| 49 | static inline int elf_sym__is_label(const GElf_Sym *sym) |
| 50 | { |
| 51 | return elf_sym__type(sym) == STT_NOTYPE && |
| 52 | sym->st_name != 0 && |
| 53 | sym->st_shndx != SHN_UNDEF && |
| 54 | sym->st_shndx != SHN_ABS; |
| 55 | } |
| 56 | |
| 57 | static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type) |
| 58 | { |
| 59 | switch (type) { |
| 60 | case MAP__FUNCTION: |
| 61 | return elf_sym__is_function(sym); |
| 62 | case MAP__VARIABLE: |
| 63 | return elf_sym__is_object(sym); |
| 64 | default: |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static inline const char *elf_sym__name(const GElf_Sym *sym, |
| 70 | const Elf_Data *symstrs) |
| 71 | { |
| 72 | return symstrs->d_buf + sym->st_name; |
| 73 | } |
| 74 | |
| 75 | static inline const char *elf_sec__name(const GElf_Shdr *shdr, |
| 76 | const Elf_Data *secstrs) |
| 77 | { |
| 78 | return secstrs->d_buf + shdr->sh_name; |
| 79 | } |
| 80 | |
| 81 | static inline int elf_sec__is_text(const GElf_Shdr *shdr, |
| 82 | const Elf_Data *secstrs) |
| 83 | { |
| 84 | return strstr(elf_sec__name(shdr, secstrs), "text") != NULL; |
| 85 | } |
| 86 | |
| 87 | static inline bool elf_sec__is_data(const GElf_Shdr *shdr, |
| 88 | const Elf_Data *secstrs) |
| 89 | { |
| 90 | return strstr(elf_sec__name(shdr, secstrs), "data") != NULL; |
| 91 | } |
| 92 | |
| 93 | static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs, |
| 94 | enum map_type type) |
| 95 | { |
| 96 | switch (type) { |
| 97 | case MAP__FUNCTION: |
| 98 | return elf_sec__is_text(shdr, secstrs); |
| 99 | case MAP__VARIABLE: |
| 100 | return elf_sec__is_data(shdr, secstrs); |
| 101 | default: |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr) |
| 107 | { |
| 108 | Elf_Scn *sec = NULL; |
| 109 | GElf_Shdr shdr; |
| 110 | size_t cnt = 1; |
| 111 | |
| 112 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
| 113 | gelf_getshdr(sec, &shdr); |
| 114 | |
| 115 | if ((addr >= shdr.sh_addr) && |
| 116 | (addr < (shdr.sh_addr + shdr.sh_size))) |
| 117 | return cnt; |
| 118 | |
| 119 | ++cnt; |
| 120 | } |
| 121 | |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, |
| 126 | GElf_Shdr *shp, const char *name, |
| 127 | size_t *idx) |
| 128 | { |
| 129 | Elf_Scn *sec = NULL; |
| 130 | size_t cnt = 1; |
| 131 | |
| 132 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
| 133 | char *str; |
| 134 | |
| 135 | gelf_getshdr(sec, shp); |
| 136 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); |
| 137 | if (!strcmp(name, str)) { |
| 138 | if (idx) |
| 139 | *idx = cnt; |
| 140 | break; |
| 141 | } |
| 142 | ++cnt; |
| 143 | } |
| 144 | |
| 145 | return sec; |
| 146 | } |
| 147 | |
| 148 | #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ |
| 149 | for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \ |
| 150 | idx < nr_entries; \ |
| 151 | ++idx, pos = gelf_getrel(reldata, idx, &pos_mem)) |
| 152 | |
| 153 | #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \ |
| 154 | for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \ |
| 155 | idx < nr_entries; \ |
| 156 | ++idx, pos = gelf_getrela(reldata, idx, &pos_mem)) |
| 157 | |
| 158 | /* |
| 159 | * We need to check if we have a .dynsym, so that we can handle the |
| 160 | * .plt, synthesizing its symbols, that aren't on the symtabs (be it |
| 161 | * .dynsym or .symtab). |
| 162 | * And always look at the original dso, not at debuginfo packages, that |
| 163 | * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). |
| 164 | */ |
| 165 | int dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map, |
| 166 | symbol_filter_t filter) |
| 167 | { |
| 168 | uint32_t nr_rel_entries, idx; |
| 169 | GElf_Sym sym; |
| 170 | u64 plt_offset; |
| 171 | GElf_Shdr shdr_plt; |
| 172 | struct symbol *f; |
| 173 | GElf_Shdr shdr_rel_plt, shdr_dynsym; |
| 174 | Elf_Data *reldata, *syms, *symstrs; |
| 175 | Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; |
| 176 | size_t dynsym_idx; |
| 177 | GElf_Ehdr ehdr; |
| 178 | char sympltname[1024]; |
| 179 | Elf *elf; |
| 180 | int nr = 0, symidx, fd, err = 0; |
| 181 | |
| 182 | fd = open(name, O_RDONLY); |
| 183 | if (fd < 0) |
| 184 | goto out; |
| 185 | |
| 186 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 187 | if (elf == NULL) |
| 188 | goto out_close; |
| 189 | |
| 190 | if (gelf_getehdr(elf, &ehdr) == NULL) |
| 191 | goto out_elf_end; |
| 192 | |
| 193 | scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym, |
| 194 | ".dynsym", &dynsym_idx); |
| 195 | if (scn_dynsym == NULL) |
| 196 | goto out_elf_end; |
| 197 | |
| 198 | scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, |
| 199 | ".rela.plt", NULL); |
| 200 | if (scn_plt_rel == NULL) { |
| 201 | scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, |
| 202 | ".rel.plt", NULL); |
| 203 | if (scn_plt_rel == NULL) |
| 204 | goto out_elf_end; |
| 205 | } |
| 206 | |
| 207 | err = -1; |
| 208 | |
| 209 | if (shdr_rel_plt.sh_link != dynsym_idx) |
| 210 | goto out_elf_end; |
| 211 | |
| 212 | if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL) |
| 213 | goto out_elf_end; |
| 214 | |
| 215 | /* |
| 216 | * Fetch the relocation section to find the idxes to the GOT |
| 217 | * and the symbols in the .dynsym they refer to. |
| 218 | */ |
| 219 | reldata = elf_getdata(scn_plt_rel, NULL); |
| 220 | if (reldata == NULL) |
| 221 | goto out_elf_end; |
| 222 | |
| 223 | syms = elf_getdata(scn_dynsym, NULL); |
| 224 | if (syms == NULL) |
| 225 | goto out_elf_end; |
| 226 | |
| 227 | scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link); |
| 228 | if (scn_symstrs == NULL) |
| 229 | goto out_elf_end; |
| 230 | |
| 231 | symstrs = elf_getdata(scn_symstrs, NULL); |
| 232 | if (symstrs == NULL) |
| 233 | goto out_elf_end; |
| 234 | |
| 235 | nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; |
| 236 | plt_offset = shdr_plt.sh_offset; |
| 237 | |
| 238 | if (shdr_rel_plt.sh_type == SHT_RELA) { |
| 239 | GElf_Rela pos_mem, *pos; |
| 240 | |
| 241 | elf_section__for_each_rela(reldata, pos, pos_mem, idx, |
| 242 | nr_rel_entries) { |
| 243 | symidx = GELF_R_SYM(pos->r_info); |
| 244 | plt_offset += shdr_plt.sh_entsize; |
| 245 | gelf_getsym(syms, symidx, &sym); |
| 246 | snprintf(sympltname, sizeof(sympltname), |
| 247 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 248 | |
| 249 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
| 250 | STB_GLOBAL, sympltname); |
| 251 | if (!f) |
| 252 | goto out_elf_end; |
| 253 | |
| 254 | if (filter && filter(map, f)) |
| 255 | symbol__delete(f); |
| 256 | else { |
| 257 | symbols__insert(&dso->symbols[map->type], f); |
| 258 | ++nr; |
| 259 | } |
| 260 | } |
| 261 | } else if (shdr_rel_plt.sh_type == SHT_REL) { |
| 262 | GElf_Rel pos_mem, *pos; |
| 263 | elf_section__for_each_rel(reldata, pos, pos_mem, idx, |
| 264 | nr_rel_entries) { |
| 265 | symidx = GELF_R_SYM(pos->r_info); |
| 266 | plt_offset += shdr_plt.sh_entsize; |
| 267 | gelf_getsym(syms, symidx, &sym); |
| 268 | snprintf(sympltname, sizeof(sympltname), |
| 269 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 270 | |
| 271 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
| 272 | STB_GLOBAL, sympltname); |
| 273 | if (!f) |
| 274 | goto out_elf_end; |
| 275 | |
| 276 | if (filter && filter(map, f)) |
| 277 | symbol__delete(f); |
| 278 | else { |
| 279 | symbols__insert(&dso->symbols[map->type], f); |
| 280 | ++nr; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | err = 0; |
| 286 | out_elf_end: |
| 287 | elf_end(elf); |
| 288 | out_close: |
| 289 | close(fd); |
| 290 | |
| 291 | if (err == 0) |
| 292 | return nr; |
| 293 | out: |
| 294 | pr_debug("%s: problems reading %s PLT info.\n", |
| 295 | __func__, dso->long_name); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Align offset to 4 bytes as needed for note name and descriptor data. |
| 301 | */ |
| 302 | #define NOTE_ALIGN(n) (((n) + 3) & -4U) |
| 303 | |
| 304 | static int elf_read_build_id(Elf *elf, void *bf, size_t size) |
| 305 | { |
| 306 | int err = -1; |
| 307 | GElf_Ehdr ehdr; |
| 308 | GElf_Shdr shdr; |
| 309 | Elf_Data *data; |
| 310 | Elf_Scn *sec; |
| 311 | Elf_Kind ek; |
| 312 | void *ptr; |
| 313 | |
| 314 | if (size < BUILD_ID_SIZE) |
| 315 | goto out; |
| 316 | |
| 317 | ek = elf_kind(elf); |
| 318 | if (ek != ELF_K_ELF) |
| 319 | goto out; |
| 320 | |
| 321 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
| 322 | pr_err("%s: cannot get elf header.\n", __func__); |
| 323 | goto out; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Check following sections for notes: |
| 328 | * '.note.gnu.build-id' |
| 329 | * '.notes' |
| 330 | * '.note' (VDSO specific) |
| 331 | */ |
| 332 | do { |
| 333 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 334 | ".note.gnu.build-id", NULL); |
| 335 | if (sec) |
| 336 | break; |
| 337 | |
| 338 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 339 | ".notes", NULL); |
| 340 | if (sec) |
| 341 | break; |
| 342 | |
| 343 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 344 | ".note", NULL); |
| 345 | if (sec) |
| 346 | break; |
| 347 | |
| 348 | return err; |
| 349 | |
| 350 | } while (0); |
| 351 | |
| 352 | data = elf_getdata(sec, NULL); |
| 353 | if (data == NULL) |
| 354 | goto out; |
| 355 | |
| 356 | ptr = data->d_buf; |
| 357 | while (ptr < (data->d_buf + data->d_size)) { |
| 358 | GElf_Nhdr *nhdr = ptr; |
| 359 | size_t namesz = NOTE_ALIGN(nhdr->n_namesz), |
| 360 | descsz = NOTE_ALIGN(nhdr->n_descsz); |
| 361 | const char *name; |
| 362 | |
| 363 | ptr += sizeof(*nhdr); |
| 364 | name = ptr; |
| 365 | ptr += namesz; |
| 366 | if (nhdr->n_type == NT_GNU_BUILD_ID && |
| 367 | nhdr->n_namesz == sizeof("GNU")) { |
| 368 | if (memcmp(name, "GNU", sizeof("GNU")) == 0) { |
| 369 | size_t sz = min(size, descsz); |
| 370 | memcpy(bf, ptr, sz); |
| 371 | memset(bf + sz, 0, size - sz); |
| 372 | err = descsz; |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | ptr += descsz; |
| 377 | } |
| 378 | |
| 379 | out: |
| 380 | return err; |
| 381 | } |
| 382 | |
| 383 | int filename__read_build_id(const char *filename, void *bf, size_t size) |
| 384 | { |
| 385 | int fd, err = -1; |
| 386 | Elf *elf; |
| 387 | |
| 388 | if (size < BUILD_ID_SIZE) |
| 389 | goto out; |
| 390 | |
| 391 | fd = open(filename, O_RDONLY); |
| 392 | if (fd < 0) |
| 393 | goto out; |
| 394 | |
| 395 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 396 | if (elf == NULL) { |
| 397 | pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); |
| 398 | goto out_close; |
| 399 | } |
| 400 | |
| 401 | err = elf_read_build_id(elf, bf, size); |
| 402 | |
| 403 | elf_end(elf); |
| 404 | out_close: |
| 405 | close(fd); |
| 406 | out: |
| 407 | return err; |
| 408 | } |
| 409 | |
| 410 | int sysfs__read_build_id(const char *filename, void *build_id, size_t size) |
| 411 | { |
| 412 | int fd, err = -1; |
| 413 | |
| 414 | if (size < BUILD_ID_SIZE) |
| 415 | goto out; |
| 416 | |
| 417 | fd = open(filename, O_RDONLY); |
| 418 | if (fd < 0) |
| 419 | goto out; |
| 420 | |
| 421 | while (1) { |
| 422 | char bf[BUFSIZ]; |
| 423 | GElf_Nhdr nhdr; |
| 424 | size_t namesz, descsz; |
| 425 | |
| 426 | if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr)) |
| 427 | break; |
| 428 | |
| 429 | namesz = NOTE_ALIGN(nhdr.n_namesz); |
| 430 | descsz = NOTE_ALIGN(nhdr.n_descsz); |
| 431 | if (nhdr.n_type == NT_GNU_BUILD_ID && |
| 432 | nhdr.n_namesz == sizeof("GNU")) { |
| 433 | if (read(fd, bf, namesz) != (ssize_t)namesz) |
| 434 | break; |
| 435 | if (memcmp(bf, "GNU", sizeof("GNU")) == 0) { |
| 436 | size_t sz = min(descsz, size); |
| 437 | if (read(fd, build_id, sz) == (ssize_t)sz) { |
| 438 | memset(build_id + sz, 0, size - sz); |
| 439 | err = 0; |
| 440 | break; |
| 441 | } |
| 442 | } else if (read(fd, bf, descsz) != (ssize_t)descsz) |
| 443 | break; |
| 444 | } else { |
| 445 | int n = namesz + descsz; |
| 446 | if (read(fd, bf, n) != n) |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | close(fd); |
| 451 | out: |
| 452 | return err; |
| 453 | } |
| 454 | |
| 455 | int filename__read_debuglink(const char *filename, char *debuglink, |
| 456 | size_t size) |
| 457 | { |
| 458 | int fd, err = -1; |
| 459 | Elf *elf; |
| 460 | GElf_Ehdr ehdr; |
| 461 | GElf_Shdr shdr; |
| 462 | Elf_Data *data; |
| 463 | Elf_Scn *sec; |
| 464 | Elf_Kind ek; |
| 465 | |
| 466 | fd = open(filename, O_RDONLY); |
| 467 | if (fd < 0) |
| 468 | goto out; |
| 469 | |
| 470 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 471 | if (elf == NULL) { |
| 472 | pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); |
| 473 | goto out_close; |
| 474 | } |
| 475 | |
| 476 | ek = elf_kind(elf); |
| 477 | if (ek != ELF_K_ELF) |
| 478 | goto out_close; |
| 479 | |
| 480 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
| 481 | pr_err("%s: cannot get elf header.\n", __func__); |
| 482 | goto out_close; |
| 483 | } |
| 484 | |
| 485 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 486 | ".gnu_debuglink", NULL); |
| 487 | if (sec == NULL) |
| 488 | goto out_close; |
| 489 | |
| 490 | data = elf_getdata(sec, NULL); |
| 491 | if (data == NULL) |
| 492 | goto out_close; |
| 493 | |
| 494 | /* the start of this section is a zero-terminated string */ |
| 495 | strncpy(debuglink, data->d_buf, size); |
| 496 | |
| 497 | elf_end(elf); |
| 498 | |
| 499 | out_close: |
| 500 | close(fd); |
| 501 | out: |
| 502 | return err; |
| 503 | } |
| 504 | |
| 505 | static int dso__swap_init(struct dso *dso, unsigned char eidata) |
| 506 | { |
| 507 | static unsigned int const endian = 1; |
| 508 | |
| 509 | dso->needs_swap = DSO_SWAP__NO; |
| 510 | |
| 511 | switch (eidata) { |
| 512 | case ELFDATA2LSB: |
| 513 | /* We are big endian, DSO is little endian. */ |
| 514 | if (*(unsigned char const *)&endian != 1) |
| 515 | dso->needs_swap = DSO_SWAP__YES; |
| 516 | break; |
| 517 | |
| 518 | case ELFDATA2MSB: |
| 519 | /* We are little endian, DSO is big endian. */ |
| 520 | if (*(unsigned char const *)&endian != 0) |
| 521 | dso->needs_swap = DSO_SWAP__YES; |
| 522 | break; |
| 523 | |
| 524 | default: |
| 525 | pr_err("unrecognized DSO data encoding %d\n", eidata); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | int dso__load_sym(struct dso *dso, struct map *map, const char *name, int fd, |
| 533 | symbol_filter_t filter, int kmodule, int want_symtab) |
| 534 | { |
| 535 | struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL; |
| 536 | struct map *curr_map = map; |
| 537 | struct dso *curr_dso = dso; |
| 538 | Elf_Data *symstrs, *secstrs; |
| 539 | uint32_t nr_syms; |
| 540 | int err = -1; |
| 541 | uint32_t idx; |
| 542 | GElf_Ehdr ehdr; |
| 543 | GElf_Shdr shdr, opdshdr; |
| 544 | Elf_Data *syms, *opddata = NULL; |
| 545 | GElf_Sym sym; |
| 546 | Elf_Scn *sec, *sec_strndx, *opdsec; |
| 547 | Elf *elf; |
| 548 | int nr = 0; |
| 549 | size_t opdidx = 0; |
| 550 | |
| 551 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 552 | if (elf == NULL) { |
| 553 | pr_debug("%s: cannot read %s ELF file.\n", __func__, name); |
| 554 | goto out_close; |
| 555 | } |
| 556 | |
| 557 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
| 558 | pr_debug("%s: cannot get elf header.\n", __func__); |
| 559 | goto out_elf_end; |
| 560 | } |
| 561 | |
| 562 | if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) |
| 563 | goto out_elf_end; |
| 564 | |
| 565 | /* Always reject images with a mismatched build-id: */ |
| 566 | if (dso->has_build_id) { |
| 567 | u8 build_id[BUILD_ID_SIZE]; |
| 568 | |
| 569 | if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0) |
| 570 | goto out_elf_end; |
| 571 | |
| 572 | if (!dso__build_id_equal(dso, build_id)) |
| 573 | goto out_elf_end; |
| 574 | } |
| 575 | |
| 576 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); |
| 577 | if (sec == NULL) { |
| 578 | if (want_symtab) |
| 579 | goto out_elf_end; |
| 580 | |
| 581 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); |
| 582 | if (sec == NULL) |
| 583 | goto out_elf_end; |
| 584 | } |
| 585 | |
| 586 | opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx); |
| 587 | if (opdshdr.sh_type != SHT_PROGBITS) |
| 588 | opdsec = NULL; |
| 589 | if (opdsec) |
| 590 | opddata = elf_rawdata(opdsec, NULL); |
| 591 | |
| 592 | syms = elf_getdata(sec, NULL); |
| 593 | if (syms == NULL) |
| 594 | goto out_elf_end; |
| 595 | |
| 596 | sec = elf_getscn(elf, shdr.sh_link); |
| 597 | if (sec == NULL) |
| 598 | goto out_elf_end; |
| 599 | |
| 600 | symstrs = elf_getdata(sec, NULL); |
| 601 | if (symstrs == NULL) |
| 602 | goto out_elf_end; |
| 603 | |
| 604 | sec_strndx = elf_getscn(elf, ehdr.e_shstrndx); |
| 605 | if (sec_strndx == NULL) |
| 606 | goto out_elf_end; |
| 607 | |
| 608 | secstrs = elf_getdata(sec_strndx, NULL); |
| 609 | if (secstrs == NULL) |
| 610 | goto out_elf_end; |
| 611 | |
| 612 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
| 613 | |
| 614 | memset(&sym, 0, sizeof(sym)); |
| 615 | if (dso->kernel == DSO_TYPE_USER) { |
| 616 | dso->adjust_symbols = (ehdr.e_type == ET_EXEC || |
| 617 | elf_section_by_name(elf, &ehdr, &shdr, |
| 618 | ".gnu.prelink_undo", |
| 619 | NULL) != NULL); |
| 620 | } else { |
| 621 | dso->adjust_symbols = 0; |
| 622 | } |
| 623 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { |
| 624 | struct symbol *f; |
| 625 | const char *elf_name = elf_sym__name(&sym, symstrs); |
| 626 | char *demangled = NULL; |
| 627 | int is_label = elf_sym__is_label(&sym); |
| 628 | const char *section_name; |
| 629 | |
| 630 | if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name && |
| 631 | strcmp(elf_name, kmap->ref_reloc_sym->name) == 0) |
| 632 | kmap->ref_reloc_sym->unrelocated_addr = sym.st_value; |
| 633 | |
| 634 | if (!is_label && !elf_sym__is_a(&sym, map->type)) |
| 635 | continue; |
| 636 | |
| 637 | /* Reject ARM ELF "mapping symbols": these aren't unique and |
| 638 | * don't identify functions, so will confuse the profile |
| 639 | * output: */ |
| 640 | if (ehdr.e_machine == EM_ARM) { |
| 641 | if (!strcmp(elf_name, "$a") || |
| 642 | !strcmp(elf_name, "$d") || |
| 643 | !strcmp(elf_name, "$t")) |
| 644 | continue; |
| 645 | } |
| 646 | |
| 647 | if (opdsec && sym.st_shndx == opdidx) { |
| 648 | u32 offset = sym.st_value - opdshdr.sh_addr; |
| 649 | u64 *opd = opddata->d_buf + offset; |
| 650 | sym.st_value = DSO__SWAP(dso, u64, *opd); |
| 651 | sym.st_shndx = elf_addr_to_index(elf, sym.st_value); |
| 652 | } |
| 653 | |
| 654 | sec = elf_getscn(elf, sym.st_shndx); |
| 655 | if (!sec) |
| 656 | goto out_elf_end; |
| 657 | |
| 658 | gelf_getshdr(sec, &shdr); |
| 659 | |
| 660 | if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type)) |
| 661 | continue; |
| 662 | |
| 663 | section_name = elf_sec__name(&shdr, secstrs); |
| 664 | |
| 665 | /* On ARM, symbols for thumb functions have 1 added to |
| 666 | * the symbol address as a flag - remove it */ |
| 667 | if ((ehdr.e_machine == EM_ARM) && |
| 668 | (map->type == MAP__FUNCTION) && |
| 669 | (sym.st_value & 1)) |
| 670 | --sym.st_value; |
| 671 | |
| 672 | if (dso->kernel != DSO_TYPE_USER || kmodule) { |
| 673 | char dso_name[PATH_MAX]; |
| 674 | |
| 675 | if (strcmp(section_name, |
| 676 | (curr_dso->short_name + |
| 677 | dso->short_name_len)) == 0) |
| 678 | goto new_symbol; |
| 679 | |
| 680 | if (strcmp(section_name, ".text") == 0) { |
| 681 | curr_map = map; |
| 682 | curr_dso = dso; |
| 683 | goto new_symbol; |
| 684 | } |
| 685 | |
| 686 | snprintf(dso_name, sizeof(dso_name), |
| 687 | "%s%s", dso->short_name, section_name); |
| 688 | |
| 689 | curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name); |
| 690 | if (curr_map == NULL) { |
| 691 | u64 start = sym.st_value; |
| 692 | |
| 693 | if (kmodule) |
| 694 | start += map->start + shdr.sh_offset; |
| 695 | |
| 696 | curr_dso = dso__new(dso_name); |
| 697 | if (curr_dso == NULL) |
| 698 | goto out_elf_end; |
| 699 | curr_dso->kernel = dso->kernel; |
| 700 | curr_dso->long_name = dso->long_name; |
| 701 | curr_dso->long_name_len = dso->long_name_len; |
| 702 | curr_map = map__new2(start, curr_dso, |
| 703 | map->type); |
| 704 | if (curr_map == NULL) { |
| 705 | dso__delete(curr_dso); |
| 706 | goto out_elf_end; |
| 707 | } |
| 708 | curr_map->map_ip = identity__map_ip; |
| 709 | curr_map->unmap_ip = identity__map_ip; |
| 710 | curr_dso->symtab_type = dso->symtab_type; |
| 711 | map_groups__insert(kmap->kmaps, curr_map); |
| 712 | dsos__add(&dso->node, curr_dso); |
| 713 | dso__set_loaded(curr_dso, map->type); |
| 714 | } else |
| 715 | curr_dso = curr_map->dso; |
| 716 | |
| 717 | goto new_symbol; |
| 718 | } |
| 719 | |
Cody P Schafer | 8db24c7 | 2012-08-10 15:22:49 -0700 | [diff] [blame^] | 720 | if (curr_dso->adjust_symbols && sym.st_value) { |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 721 | pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " " |
| 722 | "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__, |
| 723 | (u64)sym.st_value, (u64)shdr.sh_addr, |
| 724 | (u64)shdr.sh_offset); |
| 725 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; |
| 726 | } |
| 727 | /* |
| 728 | * We need to figure out if the object was created from C++ sources |
| 729 | * DWARF DW_compile_unit has this, but we don't always have access |
| 730 | * to it... |
| 731 | */ |
| 732 | demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI); |
| 733 | if (demangled != NULL) |
| 734 | elf_name = demangled; |
| 735 | new_symbol: |
| 736 | f = symbol__new(sym.st_value, sym.st_size, |
| 737 | GELF_ST_BIND(sym.st_info), elf_name); |
| 738 | free(demangled); |
| 739 | if (!f) |
| 740 | goto out_elf_end; |
| 741 | |
| 742 | if (filter && filter(curr_map, f)) |
| 743 | symbol__delete(f); |
| 744 | else { |
| 745 | symbols__insert(&curr_dso->symbols[curr_map->type], f); |
| 746 | nr++; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * For misannotated, zeroed, ASM function sizes. |
| 752 | */ |
| 753 | if (nr > 0) { |
| 754 | symbols__fixup_duplicate(&dso->symbols[map->type]); |
| 755 | symbols__fixup_end(&dso->symbols[map->type]); |
| 756 | if (kmap) { |
| 757 | /* |
| 758 | * We need to fixup this here too because we create new |
| 759 | * maps here, for things like vsyscall sections. |
| 760 | */ |
| 761 | __map_groups__fixup_end(kmap->kmaps, map->type); |
| 762 | } |
| 763 | } |
| 764 | err = nr; |
| 765 | out_elf_end: |
| 766 | elf_end(elf); |
| 767 | out_close: |
| 768 | return err; |
| 769 | } |
| 770 | |
| 771 | void symbol__elf_init(void) |
| 772 | { |
| 773 | elf_version(EV_CURRENT); |
| 774 | } |