1 // SPDX-License-Identifier: GPL-2.0 2 #include <fcntl.h> 3 #include <stdio.h> 4 #include <errno.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <unistd.h> 8 #include <inttypes.h> 9 10 #include "dso.h" 11 #include "map.h" 12 #include "maps.h" 13 #include "symbol.h" 14 #include "symsrc.h" 15 #include "demangle-ocaml.h" 16 #include "demangle-java.h" 17 #include "demangle-rust.h" 18 #include "machine.h" 19 #include "vdso.h" 20 #include "debug.h" 21 #include "util/copyfile.h" 22 #include <linux/ctype.h> 23 #include <linux/kernel.h> 24 #include <linux/zalloc.h> 25 #include <symbol/kallsyms.h> 26 #include <internal/lib.h> 27 28 #ifndef EM_AARCH64 29 #define EM_AARCH64 183 /* ARM 64 bit */ 30 #endif 31 32 #ifndef ELF32_ST_VISIBILITY 33 #define ELF32_ST_VISIBILITY(o) ((o) & 0x03) 34 #endif 35 36 /* For ELF64 the definitions are the same. */ 37 #ifndef ELF64_ST_VISIBILITY 38 #define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o) 39 #endif 40 41 /* How to extract information held in the st_other field. */ 42 #ifndef GELF_ST_VISIBILITY 43 #define GELF_ST_VISIBILITY(val) ELF64_ST_VISIBILITY (val) 44 #endif 45 46 typedef Elf64_Nhdr GElf_Nhdr; 47 48 #ifndef DMGL_PARAMS 49 #define DMGL_NO_OPTS 0 /* For readability... */ 50 #define DMGL_PARAMS (1 << 0) /* Include function args */ 51 #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */ 52 #endif 53 54 #ifdef HAVE_LIBBFD_SUPPORT 55 #define PACKAGE 'perf' 56 #include <bfd.h> 57 #else 58 #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT 59 extern char *cplus_demangle(const char *, int); 60 61 static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i) 62 { 63 return cplus_demangle(c, i); 64 } 65 #else 66 #ifdef NO_DEMANGLE 67 static inline char *bfd_demangle(void __maybe_unused *v, 68 const char __maybe_unused *c, 69 int __maybe_unused i) 70 { 71 return NULL; 72 } 73 #endif 74 #endif 75 #endif 76 77 #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT 78 static int elf_getphdrnum(Elf *elf, size_t *dst) 79 { 80 GElf_Ehdr gehdr; 81 GElf_Ehdr *ehdr; 82 83 ehdr = gelf_getehdr(elf, &gehdr); 84 if (!ehdr) 85 return -1; 86 87 *dst = ehdr->e_phnum; 88 89 return 0; 90 } 91 #endif 92 93 #ifndef HAVE_ELF_GETSHDRSTRNDX_SUPPORT 94 static int elf_getshdrstrndx(Elf *elf __maybe_unused, size_t *dst __maybe_unused) 95 { 96 pr_err("%s: update your libelf to > 0.140, this one lacks elf_getshdrstrndx().\n", __func__); 97 return -1; 98 } 99 #endif 100 101 #ifndef NT_GNU_BUILD_ID 102 #define NT_GNU_BUILD_ID 3 103 #endif 104 105 /** 106 * elf_symtab__for_each_symbol - iterate thru all the symbols 107 * 108 * @syms: struct elf_symtab instance to iterate 109 * @idx: uint32_t idx 110 * @sym: GElf_Sym iterator 111 */ 112 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \ 113 for (idx = 0, gelf_getsym(syms, idx, &sym);\ 114 idx < nr_syms; \ 115 idx++, gelf_getsym(syms, idx, &sym)) 116 117 static inline uint8_t elf_sym__type(const GElf_Sym *sym) 118 { 119 return GELF_ST_TYPE(sym->st_info); 120 } 121 122 static inline uint8_t elf_sym__visibility(const GElf_Sym *sym) 123 { 124 return GELF_ST_VISIBILITY(sym->st_other); 125 } 126 127 #ifndef STT_GNU_IFUNC 128 #define STT_GNU_IFUNC 10 129 #endif 130 131 static inline int elf_sym__is_function(const GElf_Sym *sym) 132 { 133 return (elf_sym__type(sym) == STT_FUNC || 134 elf_sym__type(sym) == STT_GNU_IFUNC) && 135 sym->st_name != 0 && 136 sym->st_shndx != SHN_UNDEF; 137 } 138 139 static inline bool elf_sym__is_object(const GElf_Sym *sym) 140 { 141 return elf_sym__type(sym) == STT_OBJECT && 142 sym->st_name != 0 && 143 sym->st_shndx != SHN_UNDEF; 144 } 145 146 static inline int elf_sym__is_label(const GElf_Sym *sym) 147 { 148 return elf_sym__type(sym) == STT_NOTYPE && 149 sym->st_name != 0 && 150 sym->st_shndx != SHN_UNDEF && 151 sym->st_shndx != SHN_ABS && 152 elf_sym__visibility(sym) != STV_HIDDEN && 153 elf_sym__visibility(sym) != STV_INTERNAL; 154 } 155 156 static bool elf_sym__filter(GElf_Sym *sym) 157 { 158 return elf_sym__is_function(sym) || elf_sym__is_object(sym); 159 } 160 161 static inline const char *elf_sym__name(const GElf_Sym *sym, 162 const Elf_Data *symstrs) 163 { 164 return symstrs->d_buf + sym->st_name; 165 } 166 167 static inline const char *elf_sec__name(const GElf_Shdr *shdr, 168 const Elf_Data *secstrs) 169 { 170 return secstrs->d_buf + shdr->sh_name; 171 } 172 173 static inline int elf_sec__is_text(const GElf_Shdr *shdr, 174 const Elf_Data *secstrs) 175 { 176 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL; 177 } 178 179 static inline bool elf_sec__is_data(const GElf_Shdr *shdr, 180 const Elf_Data *secstrs) 181 { 182 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL; 183 } 184 185 static bool elf_sec__filter(GElf_Shdr *shdr, Elf_Data *secstrs) 186 { 187 return elf_sec__is_text(shdr, secstrs) || 188 elf_sec__is_data(shdr, secstrs); 189 } 190 191 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr) 192 { 193 Elf_Scn *sec = NULL; 194 GElf_Shdr shdr; 195 size_t cnt = 1; 196 197 while ((sec = elf_nextscn(elf, sec)) != NULL) { 198 gelf_getshdr(sec, &shdr); 199 200 if ((addr >= shdr.sh_addr) && 201 (addr < (shdr.sh_addr + shdr.sh_size))) 202 return cnt; 203 204 ++cnt; 205 } 206 207 return -1; 208 } 209 210 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, 211 GElf_Shdr *shp, const char *name, size_t *idx) 212 { 213 Elf_Scn *sec = NULL; 214 size_t cnt = 1; 215 216 /* Elf is corrupted/truncated, avoid calling elf_strptr. */ 217 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) 218 return NULL; 219 220 while ((sec = elf_nextscn(elf, sec)) != NULL) { 221 char *str; 222 223 gelf_getshdr(sec, shp); 224 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); 225 if (str && !strcmp(name, str)) { 226 if (idx) 227 *idx = cnt; 228 return sec; 229 } 230 ++cnt; 231 } 232 233 return NULL; 234 } 235 236 bool filename__has_section(const char *filename, const char *sec) 237 { 238 int fd; 239 Elf *elf; 240 GElf_Ehdr ehdr; 241 GElf_Shdr shdr; 242 bool found = false; 243 244 fd = open(filename, O_RDONLY); 245 if (fd < 0) 246 return false; 247 248 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 249 if (elf == NULL) 250 goto out; 251 252 if (gelf_getehdr(elf, &ehdr) == NULL) 253 goto elf_out; 254 255 found = !!elf_section_by_name(elf, &ehdr, &shdr, sec, NULL); 256 257 elf_out: 258 elf_end(elf); 259 out: 260 close(fd); 261 return found; 262 } 263 264 static int elf_read_program_header(Elf *elf, u64 vaddr, GElf_Phdr *phdr) 265 { 266 size_t i, phdrnum; 267 u64 sz; 268 269 if (elf_getphdrnum(elf, &phdrnum)) 270 return -1; 271 272 for (i = 0; i < phdrnum; i++) { 273 if (gelf_getphdr(elf, i, phdr) == NULL) 274 return -1; 275 276 if (phdr->p_type != PT_LOAD) 277 continue; 278 279 sz = max(phdr->p_memsz, phdr->p_filesz); 280 if (!sz) 281 continue; 282 283 if (vaddr >= phdr->p_vaddr && (vaddr < phdr->p_vaddr + sz)) 284 return 0; 285 } 286 287 /* Not found any valid program header */ 288 return -1; 289 } 290 291 static bool want_demangle(bool is_kernel_sym) 292 { 293 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle; 294 } 295 296 static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name) 297 { 298 int demangle_flags = verbose > 0 ? (DMGL_PARAMS | DMGL_ANSI) : DMGL_NO_OPTS; 299 char *demangled = NULL; 300 301 /* 302 * We need to figure out if the object was created from C++ sources 303 * DWARF DW_compile_unit has this, but we don't always have access 304 * to it... 305 */ 306 if (!want_demangle(dso->kernel || kmodule)) 307 return demangled; 308 309 demangled = bfd_demangle(NULL, elf_name, demangle_flags); 310 if (demangled == NULL) { 311 demangled = ocaml_demangle_sym(elf_name); 312 if (demangled == NULL) { 313 demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET); 314 } 315 } 316 else if (rust_is_mangled(demangled)) 317 /* 318 * Input to Rust demangling is the BFD-demangled 319 * name which it Rust-demangles in place. 320 */ 321 rust_demangle_sym(demangled); 322 323 return demangled; 324 } 325 326 static bool get_plt_sizes(struct dso *dso, GElf_Ehdr *ehdr, GElf_Shdr *shdr_plt, 327 u64 *plt_header_size, u64 *plt_entry_size) 328 { 329 switch (ehdr->e_machine) { 330 case EM_ARM: 331 *plt_header_size = 20; 332 *plt_entry_size = 12; 333 return true; 334 case EM_AARCH64: 335 *plt_header_size = 32; 336 *plt_entry_size = 16; 337 return true; 338 case EM_SPARC: 339 *plt_header_size = 48; 340 *plt_entry_size = 12; 341 return true; 342 case EM_SPARCV9: 343 *plt_header_size = 128; 344 *plt_entry_size = 32; 345 return true; 346 default: /* FIXME: s390/alpha/mips/parisc/poperpc/sh/xtensa need to be checked */ 347 *plt_header_size = shdr_plt->sh_entsize; 348 *plt_entry_size = shdr_plt->sh_entsize; 349 if (*plt_entry_size) 350 return true; 351 pr_debug("Missing PLT entry size for %s\n", dso->long_name); 352 return false; 353 } 354 } 355 356 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ 357 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \ 358 idx < nr_entries; \ 359 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem)) 360 361 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \ 362 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \ 363 idx < nr_entries; \ 364 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem)) 365 366 /* 367 * We need to check if we have a .dynsym, so that we can handle the 368 * .plt, synthesizing its symbols, that aren't on the symtabs (be it 369 * .dynsym or .symtab). 370 * And always look at the original dso, not at debuginfo packages, that 371 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). 372 */ 373 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) 374 { 375 uint32_t nr_rel_entries, idx; 376 GElf_Sym sym; 377 u64 plt_offset, plt_header_size, plt_entry_size; 378 GElf_Shdr shdr_plt; 379 struct symbol *f; 380 GElf_Shdr shdr_rel_plt, shdr_dynsym; 381 Elf_Data *reldata, *syms, *symstrs; 382 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; 383 size_t dynsym_idx; 384 GElf_Ehdr ehdr; 385 char sympltname[1024]; 386 Elf *elf; 387 int nr = 0, symidx, err = -1; 388 389 elf = ss->elf; 390 ehdr = ss->ehdr; 391 392 scn_dynsym = ss->dynsym; 393 shdr_dynsym = ss->dynshdr; 394 dynsym_idx = ss->dynsym_idx; 395 396 if (scn_dynsym == NULL) 397 return 0; 398 399 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, 400 ".rela.plt", NULL); 401 if (scn_plt_rel == NULL) { 402 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, 403 ".rel.plt", NULL); 404 if (scn_plt_rel == NULL) 405 return 0; 406 } 407 408 if (shdr_rel_plt.sh_link != dynsym_idx) 409 goto out_elf_end; 410 411 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL) 412 goto out_elf_end; 413 414 /* 415 * Fetch the relocation section to find the idxes to the GOT 416 * and the symbols in the .dynsym they refer to. 417 */ 418 reldata = elf_getdata(scn_plt_rel, NULL); 419 if (reldata == NULL) 420 goto out_elf_end; 421 422 syms = elf_getdata(scn_dynsym, NULL); 423 if (syms == NULL) 424 goto out_elf_end; 425 426 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link); 427 if (scn_symstrs == NULL) 428 goto out_elf_end; 429 430 symstrs = elf_getdata(scn_symstrs, NULL); 431 if (symstrs == NULL) 432 goto out_elf_end; 433 434 if (symstrs->d_size == 0) 435 goto out_elf_end; 436 437 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; 438 plt_offset = shdr_plt.sh_offset; 439 if (!get_plt_sizes(dso, &ehdr, &shdr_plt, &plt_header_size, &plt_entry_size)) 440 return 0; 441 plt_offset += plt_header_size; 442 443 if (shdr_rel_plt.sh_type == SHT_RELA) { 444 GElf_Rela pos_mem, *pos; 445 446 elf_section__for_each_rela(reldata, pos, pos_mem, idx, 447 nr_rel_entries) { 448 const char *elf_name = NULL; 449 char *demangled = NULL; 450 symidx = GELF_R_SYM(pos->r_info); 451 gelf_getsym(syms, symidx, &sym); 452 453 elf_name = elf_sym__name(&sym, symstrs); 454 demangled = demangle_sym(dso, 0, elf_name); 455 if (demangled != NULL) 456 elf_name = demangled; 457 snprintf(sympltname, sizeof(sympltname), 458 "%s@plt", elf_name); 459 free(demangled); 460 461 f = symbol__new(plt_offset, plt_entry_size, 462 STB_GLOBAL, STT_FUNC, sympltname); 463 if (!f) 464 goto out_elf_end; 465 466 plt_offset += plt_entry_size; 467 symbols__insert(&dso->symbols, f); 468 ++nr; 469 } 470 } else if (shdr_rel_plt.sh_type == SHT_REL) { 471 GElf_Rel pos_mem, *pos; 472 elf_section__for_each_rel(reldata, pos, pos_mem, idx, 473 nr_rel_entries) { 474 const char *elf_name = NULL; 475 char *demangled = NULL; 476 symidx = GELF_R_SYM(pos->r_info); 477 gelf_getsym(syms, symidx, &sym); 478 479 elf_name = elf_sym__name(&sym, symstrs); 480 demangled = demangle_sym(dso, 0, elf_name); 481 if (demangled != NULL) 482 elf_name = demangled; 483 snprintf(sympltname, sizeof(sympltname), 484 "%s@plt", elf_name); 485 free(demangled); 486 487 f = symbol__new(plt_offset, plt_entry_size, 488 STB_GLOBAL, STT_FUNC, sympltname); 489 if (!f) 490 goto out_elf_end; 491 492 plt_offset += plt_entry_size; 493 symbols__insert(&dso->symbols, f); 494 ++nr; 495 } 496 } 497 498 err = 0; 499 out_elf_end: 500 if (err == 0) 501 return nr; 502 pr_debug("%s: problems reading %s PLT info.\n", 503 __func__, dso->long_name); 504 return 0; 505 } 506 507 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name) 508 { 509 return demangle_sym(dso, kmodule, elf_name); 510 } 511 512 /* 513 * Align offset to 4 bytes as needed for note name and descriptor data. 514 */ 515 #define NOTE_ALIGN(n) (((n) + 3) & -4U) 516 517 static int elf_read_build_id(Elf *elf, void *bf, size_t size) 518 { 519 int err = -1; 520 GElf_Ehdr ehdr; 521 GElf_Shdr shdr; 522 Elf_Data *data; 523 Elf_Scn *sec; 524 Elf_Kind ek; 525 void *ptr; 526 527 if (size < BUILD_ID_SIZE) 528 goto out; 529 530 ek = elf_kind(elf); 531 if (ek != ELF_K_ELF) 532 goto out; 533 534 if (gelf_getehdr(elf, &ehdr) == NULL) { 535 pr_err("%s: cannot get elf header.\n", __func__); 536 goto out; 537 } 538 539 /* 540 * Check following sections for notes: 541 * '.note.gnu.build-id' 542 * '.notes' 543 * '.note' (VDSO specific) 544 */ 545 do { 546 sec = elf_section_by_name(elf, &ehdr, &shdr, 547 ".note.gnu.build-id", NULL); 548 if (sec) 549 break; 550 551 sec = elf_section_by_name(elf, &ehdr, &shdr, 552 ".notes", NULL); 553 if (sec) 554 break; 555 556 sec = elf_section_by_name(elf, &ehdr, &shdr, 557 ".note", NULL); 558 if (sec) 559 break; 560 561 return err; 562 563 } while (0); 564 565 data = elf_getdata(sec, NULL); 566 if (data == NULL) 567 goto out; 568 569 ptr = data->d_buf; 570 while (ptr < (data->d_buf + data->d_size)) { 571 GElf_Nhdr *nhdr = ptr; 572 size_t namesz = NOTE_ALIGN(nhdr->n_namesz), 573 descsz = NOTE_ALIGN(nhdr->n_descsz); 574 const char *name; 575 576 ptr += sizeof(*nhdr); 577 name = ptr; 578 ptr += namesz; 579 if (nhdr->n_type == NT_GNU_BUILD_ID && 580 nhdr->n_namesz == sizeof("GNU")) { 581 if (memcmp(name, "GNU", sizeof("GNU")) == 0) { 582 size_t sz = min(size, descsz); 583 memcpy(bf, ptr, sz); 584 memset(bf + sz, 0, size - sz); 585 err = descsz; 586 break; 587 } 588 } 589 ptr += descsz; 590 } 591 592 out: 593 return err; 594 } 595 596 #ifdef HAVE_LIBBFD_BUILDID_SUPPORT 597 598 static int read_build_id(const char *filename, struct build_id *bid) 599 { 600 size_t size = sizeof(bid->data); 601 int err = -1; 602 bfd *abfd; 603 604 abfd = bfd_openr(filename, NULL); 605 if (!abfd) 606 return -1; 607 608 if (!bfd_check_format(abfd, bfd_object)) { 609 pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename); 610 goto out_close; 611 } 612 613 if (!abfd->build_id || abfd->build_id->size > size) 614 goto out_close; 615 616 memcpy(bid->data, abfd->build_id->data, abfd->build_id->size); 617 memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size); 618 err = bid->size = abfd->build_id->size; 619 620 out_close: 621 bfd_close(abfd); 622 return err; 623 } 624 625 #else // HAVE_LIBBFD_BUILDID_SUPPORT 626 627 static int read_build_id(const char *filename, struct build_id *bid) 628 { 629 size_t size = sizeof(bid->data); 630 int fd, err = -1; 631 Elf *elf; 632 633 if (size < BUILD_ID_SIZE) 634 goto out; 635 636 fd = open(filename, O_RDONLY); 637 if (fd < 0) 638 goto out; 639 640 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 641 if (elf == NULL) { 642 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); 643 goto out_close; 644 } 645 646 err = elf_read_build_id(elf, bid->data, size); 647 if (err > 0) 648 bid->size = err; 649 650 elf_end(elf); 651 out_close: 652 close(fd); 653 out: 654 return err; 655 } 656 657 #endif // HAVE_LIBBFD_BUILDID_SUPPORT 658 659 int filename__read_build_id(const char *filename, struct build_id *bid) 660 { 661 struct kmod_path m = { .name = NULL, }; 662 char path[PATH_MAX]; 663 int err; 664 665 if (!filename) 666 return -EFAULT; 667 668 err = kmod_path__parse(&m, filename); 669 if (err) 670 return -1; 671 672 if (m.comp) { 673 int error = 0, fd; 674 675 fd = filename__decompress(filename, path, sizeof(path), m.comp, &error); 676 if (fd < 0) { 677 pr_debug("Failed to decompress (error %d) %s\n", 678 error, filename); 679 return -1; 680 } 681 close(fd); 682 filename = path; 683 } 684 685 err = read_build_id(filename, bid); 686 687 if (m.comp) 688 unlink(filename); 689 return err; 690 } 691 692 int sysfs__read_build_id(const char *filename, struct build_id *bid) 693 { 694 size_t size = sizeof(bid->data); 695 int fd, err = -1; 696 697 fd = open(filename, O_RDONLY); 698 if (fd < 0) 699 goto out; 700 701 while (1) { 702 char bf[BUFSIZ]; 703 GElf_Nhdr nhdr; 704 size_t namesz, descsz; 705 706 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr)) 707 break; 708 709 namesz = NOTE_ALIGN(nhdr.n_namesz); 710 descsz = NOTE_ALIGN(nhdr.n_descsz); 711 if (nhdr.n_type == NT_GNU_BUILD_ID && 712 nhdr.n_namesz == sizeof("GNU")) { 713 if (read(fd, bf, namesz) != (ssize_t)namesz) 714 break; 715 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) { 716 size_t sz = min(descsz, size); 717 if (read(fd, bid->data, sz) == (ssize_t)sz) { 718 memset(bid->data + sz, 0, size - sz); 719 bid->size = sz; 720 err = 0; 721 break; 722 } 723 } else if (read(fd, bf, descsz) != (ssize_t)descsz) 724 break; 725 } else { 726 int n = namesz + descsz; 727 728 if (n > (int)sizeof(bf)) { 729 n = sizeof(bf); 730 pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n", 731 __func__, filename, nhdr.n_namesz, nhdr.n_descsz); 732 } 733 if (read(fd, bf, n) != n) 734 break; 735 } 736 } 737 close(fd); 738 out: 739 return err; 740 } 741 742 #ifdef HAVE_LIBBFD_SUPPORT 743 744 int filename__read_debuglink(const char *filename, char *debuglink, 745 size_t size) 746 { 747 int err = -1; 748 asection *section; 749 bfd *abfd; 750 751 abfd = bfd_openr(filename, NULL); 752 if (!abfd) 753 return -1; 754 755 if (!bfd_check_format(abfd, bfd_object)) { 756 pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename); 757 goto out_close; 758 } 759 760 section = bfd_get_section_by_name(abfd, ".gnu_debuglink"); 761 if (!section) 762 goto out_close; 763 764 if (section->size > size) 765 goto out_close; 766 767 if (!bfd_get_section_contents(abfd, section, debuglink, 0, 768 section->size)) 769 goto out_close; 770 771 err = 0; 772 773 out_close: 774 bfd_close(abfd); 775 return err; 776 } 777 778 #else 779 780 int filename__read_debuglink(const char *filename, char *debuglink, 781 size_t size) 782 { 783 int fd, err = -1; 784 Elf *elf; 785 GElf_Ehdr ehdr; 786 GElf_Shdr shdr; 787 Elf_Data *data; 788 Elf_Scn *sec; 789 Elf_Kind ek; 790 791 fd = open(filename, O_RDONLY); 792 if (fd < 0) 793 goto out; 794 795 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 796 if (elf == NULL) { 797 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); 798 goto out_close; 799 } 800 801 ek = elf_kind(elf); 802 if (ek != ELF_K_ELF) 803 goto out_elf_end; 804 805 if (gelf_getehdr(elf, &ehdr) == NULL) { 806 pr_err("%s: cannot get elf header.\n", __func__); 807 goto out_elf_end; 808 } 809 810 sec = elf_section_by_name(elf, &ehdr, &shdr, 811 ".gnu_debuglink", NULL); 812 if (sec == NULL) 813 goto out_elf_end; 814 815 data = elf_getdata(sec, NULL); 816 if (data == NULL) 817 goto out_elf_end; 818 819 /* the start of this section is a zero-terminated string */ 820 strncpy(debuglink, data->d_buf, size); 821 822 err = 0; 823 824 out_elf_end: 825 elf_end(elf); 826 out_close: 827 close(fd); 828 out: 829 return err; 830 } 831 832 #endif 833 834 static int dso__swap_init(struct dso *dso, unsigned char eidata) 835 { 836 static unsigned int const endian = 1; 837 838 dso->needs_swap = DSO_SWAP__NO; 839 840 switch (eidata) { 841 case ELFDATA2LSB: 842 /* We are big endian, DSO is little endian. */ 843 if (*(unsigned char const *)&endian != 1) 844 dso->needs_swap = DSO_SWAP__YES; 845 break; 846 847 case ELFDATA2MSB: 848 /* We are little endian, DSO is big endian. */ 849 if (*(unsigned char const *)&endian != 0) 850 dso->needs_swap = DSO_SWAP__YES; 851 break; 852 853 default: 854 pr_err("unrecognized DSO data encoding %d\n", eidata); 855 return -EINVAL; 856 } 857 858 return 0; 859 } 860 861 bool symsrc__possibly_runtime(struct symsrc *ss) 862 { 863 return ss->dynsym || ss->opdsec; 864 } 865 866 bool symsrc__has_symtab(struct symsrc *ss) 867 { 868 return ss->symtab != NULL; 869 } 870 871 void symsrc__destroy(struct symsrc *ss) 872 { 873 zfree(&ss->name); 874 elf_end(ss->elf); 875 close(ss->fd); 876 } 877 878 bool elf__needs_adjust_symbols(GElf_Ehdr ehdr) 879 { 880 /* 881 * Usually vmlinux is an ELF file with type ET_EXEC for most 882 * architectures; except Arm64 kernel is linked with option 883 * '-share', so need to check type ET_DYN. 884 */ 885 return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL || 886 ehdr.e_type == ET_DYN; 887 } 888 889 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name, 890 enum dso_binary_type type) 891 { 892 GElf_Ehdr ehdr; 893 Elf *elf; 894 int fd; 895 896 if (dso__needs_decompress(dso)) { 897 fd = dso__decompress_kmodule_fd(dso, name); 898 if (fd < 0) 899 return -1; 900 901 type = dso->symtab_type; 902 } else { 903 fd = open(name, O_RDONLY); 904 if (fd < 0) { 905 dso->load_errno = errno; 906 return -1; 907 } 908 } 909 910 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 911 if (elf == NULL) { 912 pr_debug("%s: cannot read %s ELF file.\n", __func__, name); 913 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF; 914 goto out_close; 915 } 916 917 if (gelf_getehdr(elf, &ehdr) == NULL) { 918 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF; 919 pr_debug("%s: cannot get elf header.\n", __func__); 920 goto out_elf_end; 921 } 922 923 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) { 924 dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR; 925 goto out_elf_end; 926 } 927 928 /* Always reject images with a mismatched build-id: */ 929 if (dso->has_build_id && !symbol_conf.ignore_vmlinux_buildid) { 930 u8 build_id[BUILD_ID_SIZE]; 931 struct build_id bid; 932 int size; 933 934 size = elf_read_build_id(elf, build_id, BUILD_ID_SIZE); 935 if (size <= 0) { 936 dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID; 937 goto out_elf_end; 938 } 939 940 build_id__init(&bid, build_id, size); 941 if (!dso__build_id_equal(dso, &bid)) { 942 pr_debug("%s: build id mismatch for %s.\n", __func__, name); 943 dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID; 944 goto out_elf_end; 945 } 946 } 947 948 ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64); 949 950 ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab", 951 NULL); 952 if (ss->symshdr.sh_type != SHT_SYMTAB) 953 ss->symtab = NULL; 954 955 ss->dynsym_idx = 0; 956 ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym", 957 &ss->dynsym_idx); 958 if (ss->dynshdr.sh_type != SHT_DYNSYM) 959 ss->dynsym = NULL; 960 961 ss->opdidx = 0; 962 ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd", 963 &ss->opdidx); 964 if (ss->opdshdr.sh_type != SHT_PROGBITS) 965 ss->opdsec = NULL; 966 967 if (dso->kernel == DSO_SPACE__USER) 968 ss->adjust_symbols = true; 969 else 970 ss->adjust_symbols = elf__needs_adjust_symbols(ehdr); 971 972 ss->name = strdup(name); 973 if (!ss->name) { 974 dso->load_errno = errno; 975 goto out_elf_end; 976 } 977 978 ss->elf = elf; 979 ss->fd = fd; 980 ss->ehdr = ehdr; 981 ss->type = type; 982 983 return 0; 984 985 out_elf_end: 986 elf_end(elf); 987 out_close: 988 close(fd); 989 return -1; 990 } 991 992 /** 993 * ref_reloc_sym_not_found - has kernel relocation symbol been found. 994 * @kmap: kernel maps and relocation reference symbol 995 * 996 * This function returns %true if we are dealing with the kernel maps and the 997 * relocation reference symbol has not yet been found. Otherwise %false is 998 * returned. 999 */ 1000 static bool ref_reloc_sym_not_found(struct kmap *kmap) 1001 { 1002 return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name && 1003 !kmap->ref_reloc_sym->unrelocated_addr; 1004 } 1005 1006 /** 1007 * ref_reloc - kernel relocation offset. 1008 * @kmap: kernel maps and relocation reference symbol 1009 * 1010 * This function returns the offset of kernel addresses as determined by using 1011 * the relocation reference symbol i.e. if the kernel has not been relocated 1012 * then the return value is zero. 1013 */ 1014 static u64 ref_reloc(struct kmap *kmap) 1015 { 1016 if (kmap && kmap->ref_reloc_sym && 1017 kmap->ref_reloc_sym->unrelocated_addr) 1018 return kmap->ref_reloc_sym->addr - 1019 kmap->ref_reloc_sym->unrelocated_addr; 1020 return 0; 1021 } 1022 1023 void __weak arch__sym_update(struct symbol *s __maybe_unused, 1024 GElf_Sym *sym __maybe_unused) { } 1025 1026 static int dso__process_kernel_symbol(struct dso *dso, struct map *map, 1027 GElf_Sym *sym, GElf_Shdr *shdr, 1028 struct maps *kmaps, struct kmap *kmap, 1029 struct dso **curr_dsop, struct map **curr_mapp, 1030 const char *section_name, 1031 bool adjust_kernel_syms, bool kmodule, bool *remap_kernel) 1032 { 1033 struct dso *curr_dso = *curr_dsop; 1034 struct map *curr_map; 1035 char dso_name[PATH_MAX]; 1036 1037 /* Adjust symbol to map to file offset */ 1038 if (adjust_kernel_syms) 1039 sym->st_value -= shdr->sh_addr - shdr->sh_offset; 1040 1041 if (strcmp(section_name, (curr_dso->short_name + dso->short_name_len)) == 0) 1042 return 0; 1043 1044 if (strcmp(section_name, ".text") == 0) { 1045 /* 1046 * The initial kernel mapping is based on 1047 * kallsyms and identity maps. Overwrite it to 1048 * map to the kernel dso. 1049 */ 1050 if (*remap_kernel && dso->kernel && !kmodule) { 1051 *remap_kernel = false; 1052 map->start = shdr->sh_addr + ref_reloc(kmap); 1053 map->end = map->start + shdr->sh_size; 1054 map->pgoff = shdr->sh_offset; 1055 map->map_ip = map__map_ip; 1056 map->unmap_ip = map__unmap_ip; 1057 /* Ensure maps are correctly ordered */ 1058 if (kmaps) { 1059 map__get(map); 1060 maps__remove(kmaps, map); 1061 maps__insert(kmaps, map); 1062 map__put(map); 1063 } 1064 } 1065 1066 /* 1067 * The initial module mapping is based on 1068 * /proc/modules mapped to offset zero. 1069 * Overwrite it to map to the module dso. 1070 */ 1071 if (*remap_kernel && kmodule) { 1072 *remap_kernel = false; 1073 map->pgoff = shdr->sh_offset; 1074 } 1075 1076 *curr_mapp = map; 1077 *curr_dsop = dso; 1078 return 0; 1079 } 1080 1081 if (!kmap) 1082 return 0; 1083 1084 snprintf(dso_name, sizeof(dso_name), "%s%s", dso->short_name, section_name); 1085 1086 curr_map = maps__find_by_name(kmaps, dso_name); 1087 if (curr_map == NULL) { 1088 u64 start = sym->st_value; 1089 1090 if (kmodule) 1091 start += map->start + shdr->sh_offset; 1092 1093 curr_dso = dso__new(dso_name); 1094 if (curr_dso == NULL) 1095 return -1; 1096 curr_dso->kernel = dso->kernel; 1097 curr_dso->long_name = dso->long_name; 1098 curr_dso->long_name_len = dso->long_name_len; 1099 curr_map = map__new2(start, curr_dso); 1100 dso__put(curr_dso); 1101 if (curr_map == NULL) 1102 return -1; 1103 1104 if (curr_dso->kernel) 1105 map__kmap(curr_map)->kmaps = kmaps; 1106 1107 if (adjust_kernel_syms) { 1108 curr_map->start = shdr->sh_addr + ref_reloc(kmap); 1109 curr_map->end = curr_map->start + shdr->sh_size; 1110 curr_map->pgoff = shdr->sh_offset; 1111 } else { 1112 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip; 1113 } 1114 curr_dso->symtab_type = dso->symtab_type; 1115 maps__insert(kmaps, curr_map); 1116 /* 1117 * Add it before we drop the reference to curr_map, i.e. while 1118 * we still are sure to have a reference to this DSO via 1119 * *curr_map->dso. 1120 */ 1121 dsos__add(&kmaps->machine->dsos, curr_dso); 1122 /* kmaps already got it */ 1123 map__put(curr_map); 1124 dso__set_loaded(curr_dso); 1125 *curr_mapp = curr_map; 1126 *curr_dsop = curr_dso; 1127 } else 1128 *curr_dsop = curr_map->dso; 1129 1130 return 0; 1131 } 1132 1133 static int 1134 dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss, 1135 struct symsrc *runtime_ss, int kmodule, int dynsym) 1136 { 1137 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL; 1138 struct maps *kmaps = kmap ? map__kmaps(map) : NULL; 1139 struct map *curr_map = map; 1140 struct dso *curr_dso = dso; 1141 Elf_Data *symstrs, *secstrs, *secstrs_run, *secstrs_sym; 1142 uint32_t nr_syms; 1143 int err = -1; 1144 uint32_t idx; 1145 GElf_Ehdr ehdr; 1146 GElf_Shdr shdr; 1147 GElf_Shdr tshdr; 1148 Elf_Data *syms, *opddata = NULL; 1149 GElf_Sym sym; 1150 Elf_Scn *sec, *sec_strndx; 1151 Elf *elf; 1152 int nr = 0; 1153 bool remap_kernel = false, adjust_kernel_syms = false; 1154 1155 if (kmap && !kmaps) 1156 return -1; 1157 1158 elf = syms_ss->elf; 1159 ehdr = syms_ss->ehdr; 1160 if (dynsym) { 1161 sec = syms_ss->dynsym; 1162 shdr = syms_ss->dynshdr; 1163 } else { 1164 sec = syms_ss->symtab; 1165 shdr = syms_ss->symshdr; 1166 } 1167 1168 if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr, 1169 ".text", NULL)) 1170 dso->text_offset = tshdr.sh_addr - tshdr.sh_offset; 1171 1172 if (runtime_ss->opdsec) 1173 opddata = elf_rawdata(runtime_ss->opdsec, NULL); 1174 1175 syms = elf_getdata(sec, NULL); 1176 if (syms == NULL) 1177 goto out_elf_end; 1178 1179 sec = elf_getscn(elf, shdr.sh_link); 1180 if (sec == NULL) 1181 goto out_elf_end; 1182 1183 symstrs = elf_getdata(sec, NULL); 1184 if (symstrs == NULL) 1185 goto out_elf_end; 1186 1187 sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx); 1188 if (sec_strndx == NULL) 1189 goto out_elf_end; 1190 1191 secstrs_run = elf_getdata(sec_strndx, NULL); 1192 if (secstrs_run == NULL) 1193 goto out_elf_end; 1194 1195 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx); 1196 if (sec_strndx == NULL) 1197 goto out_elf_end; 1198 1199 secstrs_sym = elf_getdata(sec_strndx, NULL); 1200 if (secstrs_sym == NULL) 1201 goto out_elf_end; 1202 1203 nr_syms = shdr.sh_size / shdr.sh_entsize; 1204 1205 memset(&sym, 0, sizeof(sym)); 1206 1207 /* 1208 * The kernel relocation symbol is needed in advance in order to adjust 1209 * kernel maps correctly. 1210 */ 1211 if (ref_reloc_sym_not_found(kmap)) { 1212 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { 1213 const char *elf_name = elf_sym__name(&sym, symstrs); 1214 1215 if (strcmp(elf_name, kmap->ref_reloc_sym->name)) 1216 continue; 1217 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value; 1218 map->reloc = kmap->ref_reloc_sym->addr - 1219 kmap->ref_reloc_sym->unrelocated_addr; 1220 break; 1221 } 1222 } 1223 1224 /* 1225 * Handle any relocation of vdso necessary because older kernels 1226 * attempted to prelink vdso to its virtual address. 1227 */ 1228 if (dso__is_vdso(dso)) 1229 map->reloc = map->start - dso->text_offset; 1230 1231 dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap); 1232 /* 1233 * Initial kernel and module mappings do not map to the dso. 1234 * Flag the fixups. 1235 */ 1236 if (dso->kernel) { 1237 remap_kernel = true; 1238 adjust_kernel_syms = dso->adjust_symbols; 1239 } 1240 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { 1241 struct symbol *f; 1242 const char *elf_name = elf_sym__name(&sym, symstrs); 1243 char *demangled = NULL; 1244 int is_label = elf_sym__is_label(&sym); 1245 const char *section_name; 1246 bool used_opd = false; 1247 1248 if (!is_label && !elf_sym__filter(&sym)) 1249 continue; 1250 1251 /* Reject ARM ELF "mapping symbols": these aren't unique and 1252 * don't identify functions, so will confuse the profile 1253 * output: */ 1254 if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) { 1255 if (elf_name[0] == '$' && strchr("adtx", elf_name[1]) 1256 && (elf_name[2] == '\0' || elf_name[2] == '.')) 1257 continue; 1258 } 1259 1260 if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) { 1261 u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr; 1262 u64 *opd = opddata->d_buf + offset; 1263 sym.st_value = DSO__SWAP(dso, u64, *opd); 1264 sym.st_shndx = elf_addr_to_index(runtime_ss->elf, 1265 sym.st_value); 1266 used_opd = true; 1267 } 1268 1269 /* 1270 * When loading symbols in a data mapping, ABS symbols (which 1271 * has a value of SHN_ABS in its st_shndx) failed at 1272 * elf_getscn(). And it marks the loading as a failure so 1273 * already loaded symbols cannot be fixed up. 1274 * 1275 * I'm not sure what should be done. Just ignore them for now. 1276 * - Namhyung Kim 1277 */ 1278 if (sym.st_shndx == SHN_ABS) 1279 continue; 1280 1281 sec = elf_getscn(syms_ss->elf, sym.st_shndx); 1282 if (!sec) 1283 goto out_elf_end; 1284 1285 gelf_getshdr(sec, &shdr); 1286 1287 /* 1288 * If the attribute bit SHF_ALLOC is not set, the section 1289 * doesn't occupy memory during process execution. 1290 * E.g. ".gnu.warning.*" section is used by linker to generate 1291 * warnings when calling deprecated functions, the symbols in 1292 * the section aren't loaded to memory during process execution, 1293 * so skip them. 1294 */ 1295 if (!(shdr.sh_flags & SHF_ALLOC)) 1296 continue; 1297 1298 secstrs = secstrs_sym; 1299 1300 /* 1301 * We have to fallback to runtime when syms' section header has 1302 * NOBITS set. NOBITS results in file offset (sh_offset) not 1303 * being incremented. So sh_offset used below has different 1304 * values for syms (invalid) and runtime (valid). 1305 */ 1306 if (shdr.sh_type == SHT_NOBITS) { 1307 sec = elf_getscn(runtime_ss->elf, sym.st_shndx); 1308 if (!sec) 1309 goto out_elf_end; 1310 1311 gelf_getshdr(sec, &shdr); 1312 secstrs = secstrs_run; 1313 } 1314 1315 if (is_label && !elf_sec__filter(&shdr, secstrs)) 1316 continue; 1317 1318 section_name = elf_sec__name(&shdr, secstrs); 1319 1320 /* On ARM, symbols for thumb functions have 1 added to 1321 * the symbol address as a flag - remove it */ 1322 if ((ehdr.e_machine == EM_ARM) && 1323 (GELF_ST_TYPE(sym.st_info) == STT_FUNC) && 1324 (sym.st_value & 1)) 1325 --sym.st_value; 1326 1327 if (dso->kernel) { 1328 if (dso__process_kernel_symbol(dso, map, &sym, &shdr, kmaps, kmap, &curr_dso, &curr_map, 1329 section_name, adjust_kernel_syms, kmodule, &remap_kernel)) 1330 goto out_elf_end; 1331 } else if ((used_opd && runtime_ss->adjust_symbols) || 1332 (!used_opd && syms_ss->adjust_symbols)) { 1333 GElf_Phdr phdr; 1334 1335 if (elf_read_program_header(runtime_ss->elf, 1336 (u64)sym.st_value, &phdr)) { 1337 pr_debug4("%s: failed to find program header for " 1338 "symbol: %s st_value: %#" PRIx64 "\n", 1339 __func__, elf_name, (u64)sym.st_value); 1340 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " " 1341 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", 1342 __func__, (u64)sym.st_value, (u64)shdr.sh_addr, 1343 (u64)shdr.sh_offset); 1344 /* 1345 * Fail to find program header, let's rollback 1346 * to use shdr.sh_addr and shdr.sh_offset to 1347 * calibrate symbol's file address, though this 1348 * is not necessary for normal C ELF file, we 1349 * still need to handle java JIT symbols in this 1350 * case. 1351 */ 1352 sym.st_value -= shdr.sh_addr - shdr.sh_offset; 1353 } else { 1354 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " " 1355 "p_vaddr: %#" PRIx64 " p_offset: %#" PRIx64 "\n", 1356 __func__, (u64)sym.st_value, (u64)phdr.p_vaddr, 1357 (u64)phdr.p_offset); 1358 sym.st_value -= phdr.p_vaddr - phdr.p_offset; 1359 } 1360 } 1361 1362 demangled = demangle_sym(dso, kmodule, elf_name); 1363 if (demangled != NULL) 1364 elf_name = demangled; 1365 1366 f = symbol__new(sym.st_value, sym.st_size, 1367 GELF_ST_BIND(sym.st_info), 1368 GELF_ST_TYPE(sym.st_info), elf_name); 1369 free(demangled); 1370 if (!f) 1371 goto out_elf_end; 1372 1373 arch__sym_update(f, &sym); 1374 1375 __symbols__insert(&curr_dso->symbols, f, dso->kernel); 1376 nr++; 1377 } 1378 1379 /* 1380 * For misannotated, zeroed, ASM function sizes. 1381 */ 1382 if (nr > 0) { 1383 symbols__fixup_end(&dso->symbols, false); 1384 symbols__fixup_duplicate(&dso->symbols); 1385 if (kmap) { 1386 /* 1387 * We need to fixup this here too because we create new 1388 * maps here, for things like vsyscall sections. 1389 */ 1390 maps__fixup_end(kmaps); 1391 } 1392 } 1393 err = nr; 1394 out_elf_end: 1395 return err; 1396 } 1397 1398 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss, 1399 struct symsrc *runtime_ss, int kmodule) 1400 { 1401 int nr = 0; 1402 int err = -1; 1403 1404 dso->symtab_type = syms_ss->type; 1405 dso->is_64_bit = syms_ss->is_64_bit; 1406 dso->rel = syms_ss->ehdr.e_type == ET_REL; 1407 1408 /* 1409 * Modules may already have symbols from kallsyms, but those symbols 1410 * have the wrong values for the dso maps, so remove them. 1411 */ 1412 if (kmodule && syms_ss->symtab) 1413 symbols__delete(&dso->symbols); 1414 1415 if (!syms_ss->symtab) { 1416 /* 1417 * If the vmlinux is stripped, fail so we will fall back 1418 * to using kallsyms. The vmlinux runtime symbols aren't 1419 * of much use. 1420 */ 1421 if (dso->kernel) 1422 return err; 1423 } else { 1424 err = dso__load_sym_internal(dso, map, syms_ss, runtime_ss, 1425 kmodule, 0); 1426 if (err < 0) 1427 return err; 1428 nr = err; 1429 } 1430 1431 if (syms_ss->dynsym) { 1432 err = dso__load_sym_internal(dso, map, syms_ss, runtime_ss, 1433 kmodule, 1); 1434 if (err < 0) 1435 return err; 1436 err += nr; 1437 } 1438 1439 return err; 1440 } 1441 1442 static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data) 1443 { 1444 GElf_Phdr phdr; 1445 size_t i, phdrnum; 1446 int err; 1447 u64 sz; 1448 1449 if (elf_getphdrnum(elf, &phdrnum)) 1450 return -1; 1451 1452 for (i = 0; i < phdrnum; i++) { 1453 if (gelf_getphdr(elf, i, &phdr) == NULL) 1454 return -1; 1455 if (phdr.p_type != PT_LOAD) 1456 continue; 1457 if (exe) { 1458 if (!(phdr.p_flags & PF_X)) 1459 continue; 1460 } else { 1461 if (!(phdr.p_flags & PF_R)) 1462 continue; 1463 } 1464 sz = min(phdr.p_memsz, phdr.p_filesz); 1465 if (!sz) 1466 continue; 1467 err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data); 1468 if (err) 1469 return err; 1470 } 1471 return 0; 1472 } 1473 1474 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data, 1475 bool *is_64_bit) 1476 { 1477 int err; 1478 Elf *elf; 1479 1480 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 1481 if (elf == NULL) 1482 return -1; 1483 1484 if (is_64_bit) 1485 *is_64_bit = (gelf_getclass(elf) == ELFCLASS64); 1486 1487 err = elf_read_maps(elf, exe, mapfn, data); 1488 1489 elf_end(elf); 1490 return err; 1491 } 1492 1493 enum dso_type dso__type_fd(int fd) 1494 { 1495 enum dso_type dso_type = DSO__TYPE_UNKNOWN; 1496 GElf_Ehdr ehdr; 1497 Elf_Kind ek; 1498 Elf *elf; 1499 1500 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 1501 if (elf == NULL) 1502 goto out; 1503 1504 ek = elf_kind(elf); 1505 if (ek != ELF_K_ELF) 1506 goto out_end; 1507 1508 if (gelf_getclass(elf) == ELFCLASS64) { 1509 dso_type = DSO__TYPE_64BIT; 1510 goto out_end; 1511 } 1512 1513 if (gelf_getehdr(elf, &ehdr) == NULL) 1514 goto out_end; 1515 1516 if (ehdr.e_machine == EM_X86_64) 1517 dso_type = DSO__TYPE_X32BIT; 1518 else 1519 dso_type = DSO__TYPE_32BIT; 1520 out_end: 1521 elf_end(elf); 1522 out: 1523 return dso_type; 1524 } 1525 1526 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len) 1527 { 1528 ssize_t r; 1529 size_t n; 1530 int err = -1; 1531 char *buf = malloc(page_size); 1532 1533 if (buf == NULL) 1534 return -1; 1535 1536 if (lseek(to, to_offs, SEEK_SET) != to_offs) 1537 goto out; 1538 1539 if (lseek(from, from_offs, SEEK_SET) != from_offs) 1540 goto out; 1541 1542 while (len) { 1543 n = page_size; 1544 if (len < n) 1545 n = len; 1546 /* Use read because mmap won't work on proc files */ 1547 r = read(from, buf, n); 1548 if (r < 0) 1549 goto out; 1550 if (!r) 1551 break; 1552 n = r; 1553 r = write(to, buf, n); 1554 if (r < 0) 1555 goto out; 1556 if ((size_t)r != n) 1557 goto out; 1558 len -= n; 1559 } 1560 1561 err = 0; 1562 out: 1563 free(buf); 1564 return err; 1565 } 1566 1567 struct kcore { 1568 int fd; 1569 int elfclass; 1570 Elf *elf; 1571 GElf_Ehdr ehdr; 1572 }; 1573 1574 static int kcore__open(struct kcore *kcore, const char *filename) 1575 { 1576 GElf_Ehdr *ehdr; 1577 1578 kcore->fd = open(filename, O_RDONLY); 1579 if (kcore->fd == -1) 1580 return -1; 1581 1582 kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL); 1583 if (!kcore->elf) 1584 goto out_close; 1585 1586 kcore->elfclass = gelf_getclass(kcore->elf); 1587 if (kcore->elfclass == ELFCLASSNONE) 1588 goto out_end; 1589 1590 ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr); 1591 if (!ehdr) 1592 goto out_end; 1593 1594 return 0; 1595 1596 out_end: 1597 elf_end(kcore->elf); 1598 out_close: 1599 close(kcore->fd); 1600 return -1; 1601 } 1602 1603 static int kcore__init(struct kcore *kcore, char *filename, int elfclass, 1604 bool temp) 1605 { 1606 kcore->elfclass = elfclass; 1607 1608 if (temp) 1609 kcore->fd = mkstemp(filename); 1610 else 1611 kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400); 1612 if (kcore->fd == -1) 1613 return -1; 1614 1615 kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL); 1616 if (!kcore->elf) 1617 goto out_close; 1618 1619 if (!gelf_newehdr(kcore->elf, elfclass)) 1620 goto out_end; 1621 1622 memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr)); 1623 1624 return 0; 1625 1626 out_end: 1627 elf_end(kcore->elf); 1628 out_close: 1629 close(kcore->fd); 1630 unlink(filename); 1631 return -1; 1632 } 1633 1634 static void kcore__close(struct kcore *kcore) 1635 { 1636 elf_end(kcore->elf); 1637 close(kcore->fd); 1638 } 1639 1640 static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count) 1641 { 1642 GElf_Ehdr *ehdr = &to->ehdr; 1643 GElf_Ehdr *kehdr = &from->ehdr; 1644 1645 memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT); 1646 ehdr->e_type = kehdr->e_type; 1647 ehdr->e_machine = kehdr->e_machine; 1648 ehdr->e_version = kehdr->e_version; 1649 ehdr->e_entry = 0; 1650 ehdr->e_shoff = 0; 1651 ehdr->e_flags = kehdr->e_flags; 1652 ehdr->e_phnum = count; 1653 ehdr->e_shentsize = 0; 1654 ehdr->e_shnum = 0; 1655 ehdr->e_shstrndx = 0; 1656 1657 if (from->elfclass == ELFCLASS32) { 1658 ehdr->e_phoff = sizeof(Elf32_Ehdr); 1659 ehdr->e_ehsize = sizeof(Elf32_Ehdr); 1660 ehdr->e_phentsize = sizeof(Elf32_Phdr); 1661 } else { 1662 ehdr->e_phoff = sizeof(Elf64_Ehdr); 1663 ehdr->e_ehsize = sizeof(Elf64_Ehdr); 1664 ehdr->e_phentsize = sizeof(Elf64_Phdr); 1665 } 1666 1667 if (!gelf_update_ehdr(to->elf, ehdr)) 1668 return -1; 1669 1670 if (!gelf_newphdr(to->elf, count)) 1671 return -1; 1672 1673 return 0; 1674 } 1675 1676 static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset, 1677 u64 addr, u64 len) 1678 { 1679 GElf_Phdr phdr = { 1680 .p_type = PT_LOAD, 1681 .p_flags = PF_R | PF_W | PF_X, 1682 .p_offset = offset, 1683 .p_vaddr = addr, 1684 .p_paddr = 0, 1685 .p_filesz = len, 1686 .p_memsz = len, 1687 .p_align = page_size, 1688 }; 1689 1690 if (!gelf_update_phdr(kcore->elf, idx, &phdr)) 1691 return -1; 1692 1693 return 0; 1694 } 1695 1696 static off_t kcore__write(struct kcore *kcore) 1697 { 1698 return elf_update(kcore->elf, ELF_C_WRITE); 1699 } 1700 1701 struct phdr_data { 1702 off_t offset; 1703 off_t rel; 1704 u64 addr; 1705 u64 len; 1706 struct list_head node; 1707 struct phdr_data *remaps; 1708 }; 1709 1710 struct sym_data { 1711 u64 addr; 1712 struct list_head node; 1713 }; 1714 1715 struct kcore_copy_info { 1716 u64 stext; 1717 u64 etext; 1718 u64 first_symbol; 1719 u64 last_symbol; 1720 u64 first_module; 1721 u64 first_module_symbol; 1722 u64 last_module_symbol; 1723 size_t phnum; 1724 struct list_head phdrs; 1725 struct list_head syms; 1726 }; 1727 1728 #define kcore_copy__for_each_phdr(k, p) \ 1729 list_for_each_entry((p), &(k)->phdrs, node) 1730 1731 static struct phdr_data *phdr_data__new(u64 addr, u64 len, off_t offset) 1732 { 1733 struct phdr_data *p = zalloc(sizeof(*p)); 1734 1735 if (p) { 1736 p->addr = addr; 1737 p->len = len; 1738 p->offset = offset; 1739 } 1740 1741 return p; 1742 } 1743 1744 static struct phdr_data *kcore_copy_info__addnew(struct kcore_copy_info *kci, 1745 u64 addr, u64 len, 1746 off_t offset) 1747 { 1748 struct phdr_data *p = phdr_data__new(addr, len, offset); 1749 1750 if (p) 1751 list_add_tail(&p->node, &kci->phdrs); 1752 1753 return p; 1754 } 1755 1756 static void kcore_copy__free_phdrs(struct kcore_copy_info *kci) 1757 { 1758 struct phdr_data *p, *tmp; 1759 1760 list_for_each_entry_safe(p, tmp, &kci->phdrs, node) { 1761 list_del_init(&p->node); 1762 free(p); 1763 } 1764 } 1765 1766 static struct sym_data *kcore_copy__new_sym(struct kcore_copy_info *kci, 1767 u64 addr) 1768 { 1769 struct sym_data *s = zalloc(sizeof(*s)); 1770 1771 if (s) { 1772 s->addr = addr; 1773 list_add_tail(&s->node, &kci->syms); 1774 } 1775 1776 return s; 1777 } 1778 1779 static void kcore_copy__free_syms(struct kcore_copy_info *kci) 1780 { 1781 struct sym_data *s, *tmp; 1782 1783 list_for_each_entry_safe(s, tmp, &kci->syms, node) { 1784 list_del_init(&s->node); 1785 free(s); 1786 } 1787 } 1788 1789 static int kcore_copy__process_kallsyms(void *arg, const char *name, char type, 1790 u64 start) 1791 { 1792 struct kcore_copy_info *kci = arg; 1793 1794 if (!kallsyms__is_function(type)) 1795 return 0; 1796 1797 if (strchr(name, '[')) { 1798 if (!kci->first_module_symbol || start < kci->first_module_symbol) 1799 kci->first_module_symbol = start; 1800 if (start > kci->last_module_symbol) 1801 kci->last_module_symbol = start; 1802 return 0; 1803 } 1804 1805 if (!kci->first_symbol || start < kci->first_symbol) 1806 kci->first_symbol = start; 1807 1808 if (!kci->last_symbol || start > kci->last_symbol) 1809 kci->last_symbol = start; 1810 1811 if (!strcmp(name, "_stext")) { 1812 kci->stext = start; 1813 return 0; 1814 } 1815 1816 if (!strcmp(name, "_etext")) { 1817 kci->etext = start; 1818 return 0; 1819 } 1820 1821 if (is_entry_trampoline(name) && !kcore_copy__new_sym(kci, start)) 1822 return -1; 1823 1824 return 0; 1825 } 1826 1827 static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci, 1828 const char *dir) 1829 { 1830 char kallsyms_filename[PATH_MAX]; 1831 1832 scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir); 1833 1834 if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms")) 1835 return -1; 1836 1837 if (kallsyms__parse(kallsyms_filename, kci, 1838 kcore_copy__process_kallsyms) < 0) 1839 return -1; 1840 1841 return 0; 1842 } 1843 1844 static int kcore_copy__process_modules(void *arg, 1845 const char *name __maybe_unused, 1846 u64 start, u64 size __maybe_unused) 1847 { 1848 struct kcore_copy_info *kci = arg; 1849 1850 if (!kci->first_module || start < kci->first_module) 1851 kci->first_module = start; 1852 1853 return 0; 1854 } 1855 1856 static int kcore_copy__parse_modules(struct kcore_copy_info *kci, 1857 const char *dir) 1858 { 1859 char modules_filename[PATH_MAX]; 1860 1861 scnprintf(modules_filename, PATH_MAX, "%s/modules", dir); 1862 1863 if (symbol__restricted_filename(modules_filename, "/proc/modules")) 1864 return -1; 1865 1866 if (modules__parse(modules_filename, kci, 1867 kcore_copy__process_modules) < 0) 1868 return -1; 1869 1870 return 0; 1871 } 1872 1873 static int kcore_copy__map(struct kcore_copy_info *kci, u64 start, u64 end, 1874 u64 pgoff, u64 s, u64 e) 1875 { 1876 u64 len, offset; 1877 1878 if (s < start || s >= end) 1879 return 0; 1880 1881 offset = (s - start) + pgoff; 1882 len = e < end ? e - s : end - s; 1883 1884 return kcore_copy_info__addnew(kci, s, len, offset) ? 0 : -1; 1885 } 1886 1887 static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data) 1888 { 1889 struct kcore_copy_info *kci = data; 1890 u64 end = start + len; 1891 struct sym_data *sdat; 1892 1893 if (kcore_copy__map(kci, start, end, pgoff, kci->stext, kci->etext)) 1894 return -1; 1895 1896 if (kcore_copy__map(kci, start, end, pgoff, kci->first_module, 1897 kci->last_module_symbol)) 1898 return -1; 1899 1900 list_for_each_entry(sdat, &kci->syms, node) { 1901 u64 s = round_down(sdat->addr, page_size); 1902 1903 if (kcore_copy__map(kci, start, end, pgoff, s, s + len)) 1904 return -1; 1905 } 1906 1907 return 0; 1908 } 1909 1910 static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf) 1911 { 1912 if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0) 1913 return -1; 1914 1915 return 0; 1916 } 1917 1918 static void kcore_copy__find_remaps(struct kcore_copy_info *kci) 1919 { 1920 struct phdr_data *p, *k = NULL; 1921 u64 kend; 1922 1923 if (!kci->stext) 1924 return; 1925 1926 /* Find phdr that corresponds to the kernel map (contains stext) */ 1927 kcore_copy__for_each_phdr(kci, p) { 1928 u64 pend = p->addr + p->len - 1; 1929 1930 if (p->addr <= kci->stext && pend >= kci->stext) { 1931 k = p; 1932 break; 1933 } 1934 } 1935 1936 if (!k) 1937 return; 1938 1939 kend = k->offset + k->len; 1940 1941 /* Find phdrs that remap the kernel */ 1942 kcore_copy__for_each_phdr(kci, p) { 1943 u64 pend = p->offset + p->len; 1944 1945 if (p == k) 1946 continue; 1947 1948 if (p->offset >= k->offset && pend <= kend) 1949 p->remaps = k; 1950 } 1951 } 1952 1953 static void kcore_copy__layout(struct kcore_copy_info *kci) 1954 { 1955 struct phdr_data *p; 1956 off_t rel = 0; 1957 1958 kcore_copy__find_remaps(kci); 1959 1960 kcore_copy__for_each_phdr(kci, p) { 1961 if (!p->remaps) { 1962 p->rel = rel; 1963 rel += p->len; 1964 } 1965 kci->phnum += 1; 1966 } 1967 1968 kcore_copy__for_each_phdr(kci, p) { 1969 struct phdr_data *k = p->remaps; 1970 1971 if (k) 1972 p->rel = p->offset - k->offset + k->rel; 1973 } 1974 } 1975 1976 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir, 1977 Elf *elf) 1978 { 1979 if (kcore_copy__parse_kallsyms(kci, dir)) 1980 return -1; 1981 1982 if (kcore_copy__parse_modules(kci, dir)) 1983 return -1; 1984 1985 if (kci->stext) 1986 kci->stext = round_down(kci->stext, page_size); 1987 else 1988 kci->stext = round_down(kci->first_symbol, page_size); 1989 1990 if (kci->etext) { 1991 kci->etext = round_up(kci->etext, page_size); 1992 } else if (kci->last_symbol) { 1993 kci->etext = round_up(kci->last_symbol, page_size); 1994 kci->etext += page_size; 1995 } 1996 1997 if (kci->first_module_symbol && 1998 (!kci->first_module || kci->first_module_symbol < kci->first_module)) 1999 kci->first_module = kci->first_module_symbol; 2000 2001 kci->first_module = round_down(kci->first_module, page_size); 2002 2003 if (kci->last_module_symbol) { 2004 kci->last_module_symbol = round_up(kci->last_module_symbol, 2005 page_size); 2006 kci->last_module_symbol += page_size; 2007 } 2008 2009 if (!kci->stext || !kci->etext) 2010 return -1; 2011 2012 if (kci->first_module && !kci->last_module_symbol) 2013 return -1; 2014 2015 if (kcore_copy__read_maps(kci, elf)) 2016 return -1; 2017 2018 kcore_copy__layout(kci); 2019 2020 return 0; 2021 } 2022 2023 static int kcore_copy__copy_file(const char *from_dir, const char *to_dir, 2024 const char *name) 2025 { 2026 char from_filename[PATH_MAX]; 2027 char to_filename[PATH_MAX]; 2028 2029 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name); 2030 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name); 2031 2032 return copyfile_mode(from_filename, to_filename, 0400); 2033 } 2034 2035 static int kcore_copy__unlink(const char *dir, const char *name) 2036 { 2037 char filename[PATH_MAX]; 2038 2039 scnprintf(filename, PATH_MAX, "%s/%s", dir, name); 2040 2041 return unlink(filename); 2042 } 2043 2044 static int kcore_copy__compare_fds(int from, int to) 2045 { 2046 char *buf_from; 2047 char *buf_to; 2048 ssize_t ret; 2049 size_t len; 2050 int err = -1; 2051 2052 buf_from = malloc(page_size); 2053 buf_to = malloc(page_size); 2054 if (!buf_from || !buf_to) 2055 goto out; 2056 2057 while (1) { 2058 /* Use read because mmap won't work on proc files */ 2059 ret = read(from, buf_from, page_size); 2060 if (ret < 0) 2061 goto out; 2062 2063 if (!ret) 2064 break; 2065 2066 len = ret; 2067 2068 if (readn(to, buf_to, len) != (int)len) 2069 goto out; 2070 2071 if (memcmp(buf_from, buf_to, len)) 2072 goto out; 2073 } 2074 2075 err = 0; 2076 out: 2077 free(buf_to); 2078 free(buf_from); 2079 return err; 2080 } 2081 2082 static int kcore_copy__compare_files(const char *from_filename, 2083 const char *to_filename) 2084 { 2085 int from, to, err = -1; 2086 2087 from = open(from_filename, O_RDONLY); 2088 if (from < 0) 2089 return -1; 2090 2091 to = open(to_filename, O_RDONLY); 2092 if (to < 0) 2093 goto out_close_from; 2094 2095 err = kcore_copy__compare_fds(from, to); 2096 2097 close(to); 2098 out_close_from: 2099 close(from); 2100 return err; 2101 } 2102 2103 static int kcore_copy__compare_file(const char *from_dir, const char *to_dir, 2104 const char *name) 2105 { 2106 char from_filename[PATH_MAX]; 2107 char to_filename[PATH_MAX]; 2108 2109 scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name); 2110 scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name); 2111 2112 return kcore_copy__compare_files(from_filename, to_filename); 2113 } 2114 2115 /** 2116 * kcore_copy - copy kallsyms, modules and kcore from one directory to another. 2117 * @from_dir: from directory 2118 * @to_dir: to directory 2119 * 2120 * This function copies kallsyms, modules and kcore files from one directory to 2121 * another. kallsyms and modules are copied entirely. Only code segments are 2122 * copied from kcore. It is assumed that two segments suffice: one for the 2123 * kernel proper and one for all the modules. The code segments are determined 2124 * from kallsyms and modules files. The kernel map starts at _stext or the 2125 * lowest function symbol, and ends at _etext or the highest function symbol. 2126 * The module map starts at the lowest module address and ends at the highest 2127 * module symbol. Start addresses are rounded down to the nearest page. End 2128 * addresses are rounded up to the nearest page. An extra page is added to the 2129 * highest kernel symbol and highest module symbol to, hopefully, encompass that 2130 * symbol too. Because it contains only code sections, the resulting kcore is 2131 * unusual. One significant peculiarity is that the mapping (start -> pgoff) 2132 * is not the same for the kernel map and the modules map. That happens because 2133 * the data is copied adjacently whereas the original kcore has gaps. Finally, 2134 * kallsyms file is compared with its copy to check that modules have not been 2135 * loaded or unloaded while the copies were taking place. 2136 * 2137 * Return: %0 on success, %-1 on failure. 2138 */ 2139 int kcore_copy(const char *from_dir, const char *to_dir) 2140 { 2141 struct kcore kcore; 2142 struct kcore extract; 2143 int idx = 0, err = -1; 2144 off_t offset, sz; 2145 struct kcore_copy_info kci = { .stext = 0, }; 2146 char kcore_filename[PATH_MAX]; 2147 char extract_filename[PATH_MAX]; 2148 struct phdr_data *p; 2149 2150 INIT_LIST_HEAD(&kci.phdrs); 2151 INIT_LIST_HEAD(&kci.syms); 2152 2153 if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms")) 2154 return -1; 2155 2156 if (kcore_copy__copy_file(from_dir, to_dir, "modules")) 2157 goto out_unlink_kallsyms; 2158 2159 scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir); 2160 scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir); 2161 2162 if (kcore__open(&kcore, kcore_filename)) 2163 goto out_unlink_modules; 2164 2165 if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf)) 2166 goto out_kcore_close; 2167 2168 if (kcore__init(&extract, extract_filename, kcore.elfclass, false)) 2169 goto out_kcore_close; 2170 2171 if (kcore__copy_hdr(&kcore, &extract, kci.phnum)) 2172 goto out_extract_close; 2173 2174 offset = gelf_fsize(extract.elf, ELF_T_EHDR, 1, EV_CURRENT) + 2175 gelf_fsize(extract.elf, ELF_T_PHDR, kci.phnum, EV_CURRENT); 2176 offset = round_up(offset, page_size); 2177 2178 kcore_copy__for_each_phdr(&kci, p) { 2179 off_t offs = p->rel + offset; 2180 2181 if (kcore__add_phdr(&extract, idx++, offs, p->addr, p->len)) 2182 goto out_extract_close; 2183 } 2184 2185 sz = kcore__write(&extract); 2186 if (sz < 0 || sz > offset) 2187 goto out_extract_close; 2188 2189 kcore_copy__for_each_phdr(&kci, p) { 2190 off_t offs = p->rel + offset; 2191 2192 if (p->remaps) 2193 continue; 2194 if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len)) 2195 goto out_extract_close; 2196 } 2197 2198 if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms")) 2199 goto out_extract_close; 2200 2201 err = 0; 2202 2203 out_extract_close: 2204 kcore__close(&extract); 2205 if (err) 2206 unlink(extract_filename); 2207 out_kcore_close: 2208 kcore__close(&kcore); 2209 out_unlink_modules: 2210 if (err) 2211 kcore_copy__unlink(to_dir, "modules"); 2212 out_unlink_kallsyms: 2213 if (err) 2214 kcore_copy__unlink(to_dir, "kallsyms"); 2215 2216 kcore_copy__free_phdrs(&kci); 2217 kcore_copy__free_syms(&kci); 2218 2219 return err; 2220 } 2221 2222 int kcore_extract__create(struct kcore_extract *kce) 2223 { 2224 struct kcore kcore; 2225 struct kcore extract; 2226 size_t count = 1; 2227 int idx = 0, err = -1; 2228 off_t offset = page_size, sz; 2229 2230 if (kcore__open(&kcore, kce->kcore_filename)) 2231 return -1; 2232 2233 strcpy(kce->extract_filename, PERF_KCORE_EXTRACT); 2234 if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true)) 2235 goto out_kcore_close; 2236 2237 if (kcore__copy_hdr(&kcore, &extract, count)) 2238 goto out_extract_close; 2239 2240 if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len)) 2241 goto out_extract_close; 2242 2243 sz = kcore__write(&extract); 2244 if (sz < 0 || sz > offset) 2245 goto out_extract_close; 2246 2247 if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len)) 2248 goto out_extract_close; 2249 2250 err = 0; 2251 2252 out_extract_close: 2253 kcore__close(&extract); 2254 if (err) 2255 unlink(kce->extract_filename); 2256 out_kcore_close: 2257 kcore__close(&kcore); 2258 2259 return err; 2260 } 2261 2262 void kcore_extract__delete(struct kcore_extract *kce) 2263 { 2264 unlink(kce->extract_filename); 2265 } 2266 2267 #ifdef HAVE_GELF_GETNOTE_SUPPORT 2268 2269 static void sdt_adjust_loc(struct sdt_note *tmp, GElf_Addr base_off) 2270 { 2271 if (!base_off) 2272 return; 2273 2274 if (tmp->bit32) 2275 tmp->addr.a32[SDT_NOTE_IDX_LOC] = 2276 tmp->addr.a32[SDT_NOTE_IDX_LOC] + base_off - 2277 tmp->addr.a32[SDT_NOTE_IDX_BASE]; 2278 else 2279 tmp->addr.a64[SDT_NOTE_IDX_LOC] = 2280 tmp->addr.a64[SDT_NOTE_IDX_LOC] + base_off - 2281 tmp->addr.a64[SDT_NOTE_IDX_BASE]; 2282 } 2283 2284 static void sdt_adjust_refctr(struct sdt_note *tmp, GElf_Addr base_addr, 2285 GElf_Addr base_off) 2286 { 2287 if (!base_off) 2288 return; 2289 2290 if (tmp->bit32 && tmp->addr.a32[SDT_NOTE_IDX_REFCTR]) 2291 tmp->addr.a32[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off); 2292 else if (tmp->addr.a64[SDT_NOTE_IDX_REFCTR]) 2293 tmp->addr.a64[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off); 2294 } 2295 2296 /** 2297 * populate_sdt_note : Parse raw data and identify SDT note 2298 * @elf: elf of the opened file 2299 * @data: raw data of a section with description offset applied 2300 * @len: note description size 2301 * @type: type of the note 2302 * @sdt_notes: List to add the SDT note 2303 * 2304 * Responsible for parsing the @data in section .note.stapsdt in @elf and 2305 * if its an SDT note, it appends to @sdt_notes list. 2306 */ 2307 static int populate_sdt_note(Elf **elf, const char *data, size_t len, 2308 struct list_head *sdt_notes) 2309 { 2310 const char *provider, *name, *args; 2311 struct sdt_note *tmp = NULL; 2312 GElf_Ehdr ehdr; 2313 GElf_Shdr shdr; 2314 int ret = -EINVAL; 2315 2316 union { 2317 Elf64_Addr a64[NR_ADDR]; 2318 Elf32_Addr a32[NR_ADDR]; 2319 } buf; 2320 2321 Elf_Data dst = { 2322 .d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT, 2323 .d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT), 2324 .d_off = 0, .d_align = 0 2325 }; 2326 Elf_Data src = { 2327 .d_buf = (void *) data, .d_type = ELF_T_ADDR, 2328 .d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0, 2329 .d_align = 0 2330 }; 2331 2332 tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note)); 2333 if (!tmp) { 2334 ret = -ENOMEM; 2335 goto out_err; 2336 } 2337 2338 INIT_LIST_HEAD(&tmp->note_list); 2339 2340 if (len < dst.d_size + 3) 2341 goto out_free_note; 2342 2343 /* Translation from file representation to memory representation */ 2344 if (gelf_xlatetom(*elf, &dst, &src, 2345 elf_getident(*elf, NULL)[EI_DATA]) == NULL) { 2346 pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1)); 2347 goto out_free_note; 2348 } 2349 2350 /* Populate the fields of sdt_note */ 2351 provider = data + dst.d_size; 2352 2353 name = (const char *)memchr(provider, '\0', data + len - provider); 2354 if (name++ == NULL) 2355 goto out_free_note; 2356 2357 tmp->provider = strdup(provider); 2358 if (!tmp->provider) { 2359 ret = -ENOMEM; 2360 goto out_free_note; 2361 } 2362 tmp->name = strdup(name); 2363 if (!tmp->name) { 2364 ret = -ENOMEM; 2365 goto out_free_prov; 2366 } 2367 2368 args = memchr(name, '\0', data + len - name); 2369 2370 /* 2371 * There is no argument if: 2372 * - We reached the end of the note; 2373 * - There is not enough room to hold a potential string; 2374 * - The argument string is empty or just contains ':'. 2375 */ 2376 if (args == NULL || data + len - args < 2 || 2377 args[1] == ':' || args[1] == '\0') 2378 tmp->args = NULL; 2379 else { 2380 tmp->args = strdup(++args); 2381 if (!tmp->args) { 2382 ret = -ENOMEM; 2383 goto out_free_name; 2384 } 2385 } 2386 2387 if (gelf_getclass(*elf) == ELFCLASS32) { 2388 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr)); 2389 tmp->bit32 = true; 2390 } else { 2391 memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr)); 2392 tmp->bit32 = false; 2393 } 2394 2395 if (!gelf_getehdr(*elf, &ehdr)) { 2396 pr_debug("%s : cannot get elf header.\n", __func__); 2397 ret = -EBADF; 2398 goto out_free_args; 2399 } 2400 2401 /* Adjust the prelink effect : 2402 * Find out the .stapsdt.base section. 2403 * This scn will help us to handle prelinking (if present). 2404 * Compare the retrieved file offset of the base section with the 2405 * base address in the description of the SDT note. If its different, 2406 * then accordingly, adjust the note location. 2407 */ 2408 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL)) 2409 sdt_adjust_loc(tmp, shdr.sh_offset); 2410 2411 /* Adjust reference counter offset */ 2412 if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_PROBES_SCN, NULL)) 2413 sdt_adjust_refctr(tmp, shdr.sh_addr, shdr.sh_offset); 2414 2415 list_add_tail(&tmp->note_list, sdt_notes); 2416 return 0; 2417 2418 out_free_args: 2419 zfree(&tmp->args); 2420 out_free_name: 2421 zfree(&tmp->name); 2422 out_free_prov: 2423 zfree(&tmp->provider); 2424 out_free_note: 2425 free(tmp); 2426 out_err: 2427 return ret; 2428 } 2429 2430 /** 2431 * construct_sdt_notes_list : constructs a list of SDT notes 2432 * @elf : elf to look into 2433 * @sdt_notes : empty list_head 2434 * 2435 * Scans the sections in 'elf' for the section 2436 * .note.stapsdt. It, then calls populate_sdt_note to find 2437 * out the SDT events and populates the 'sdt_notes'. 2438 */ 2439 static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes) 2440 { 2441 GElf_Ehdr ehdr; 2442 Elf_Scn *scn = NULL; 2443 Elf_Data *data; 2444 GElf_Shdr shdr; 2445 size_t shstrndx, next; 2446 GElf_Nhdr nhdr; 2447 size_t name_off, desc_off, offset; 2448 int ret = 0; 2449 2450 if (gelf_getehdr(elf, &ehdr) == NULL) { 2451 ret = -EBADF; 2452 goto out_ret; 2453 } 2454 if (elf_getshdrstrndx(elf, &shstrndx) != 0) { 2455 ret = -EBADF; 2456 goto out_ret; 2457 } 2458 2459 /* Look for the required section */ 2460 scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL); 2461 if (!scn) { 2462 ret = -ENOENT; 2463 goto out_ret; 2464 } 2465 2466 if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) { 2467 ret = -ENOENT; 2468 goto out_ret; 2469 } 2470 2471 data = elf_getdata(scn, NULL); 2472 2473 /* Get the SDT notes */ 2474 for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off, 2475 &desc_off)) > 0; offset = next) { 2476 if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) && 2477 !memcmp(data->d_buf + name_off, SDT_NOTE_NAME, 2478 sizeof(SDT_NOTE_NAME))) { 2479 /* Check the type of the note */ 2480 if (nhdr.n_type != SDT_NOTE_TYPE) 2481 goto out_ret; 2482 2483 ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off), 2484 nhdr.n_descsz, sdt_notes); 2485 if (ret < 0) 2486 goto out_ret; 2487 } 2488 } 2489 if (list_empty(sdt_notes)) 2490 ret = -ENOENT; 2491 2492 out_ret: 2493 return ret; 2494 } 2495 2496 /** 2497 * get_sdt_note_list : Wrapper to construct a list of sdt notes 2498 * @head : empty list_head 2499 * @target : file to find SDT notes from 2500 * 2501 * This opens the file, initializes 2502 * the ELF and then calls construct_sdt_notes_list. 2503 */ 2504 int get_sdt_note_list(struct list_head *head, const char *target) 2505 { 2506 Elf *elf; 2507 int fd, ret; 2508 2509 fd = open(target, O_RDONLY); 2510 if (fd < 0) 2511 return -EBADF; 2512 2513 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 2514 if (!elf) { 2515 ret = -EBADF; 2516 goto out_close; 2517 } 2518 ret = construct_sdt_notes_list(elf, head); 2519 elf_end(elf); 2520 out_close: 2521 close(fd); 2522 return ret; 2523 } 2524 2525 /** 2526 * cleanup_sdt_note_list : free the sdt notes' list 2527 * @sdt_notes: sdt notes' list 2528 * 2529 * Free up the SDT notes in @sdt_notes. 2530 * Returns the number of SDT notes free'd. 2531 */ 2532 int cleanup_sdt_note_list(struct list_head *sdt_notes) 2533 { 2534 struct sdt_note *tmp, *pos; 2535 int nr_free = 0; 2536 2537 list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) { 2538 list_del_init(&pos->note_list); 2539 zfree(&pos->args); 2540 zfree(&pos->name); 2541 zfree(&pos->provider); 2542 free(pos); 2543 nr_free++; 2544 } 2545 return nr_free; 2546 } 2547 2548 /** 2549 * sdt_notes__get_count: Counts the number of sdt events 2550 * @start: list_head to sdt_notes list 2551 * 2552 * Returns the number of SDT notes in a list 2553 */ 2554 int sdt_notes__get_count(struct list_head *start) 2555 { 2556 struct sdt_note *sdt_ptr; 2557 int count = 0; 2558 2559 list_for_each_entry(sdt_ptr, start, note_list) 2560 count++; 2561 return count; 2562 } 2563 #endif 2564 2565 void symbol__elf_init(void) 2566 { 2567 elf_version(EV_CURRENT); 2568 } 2569