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