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