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