ltrace-elf.c revision e80caced59a9f0cdd7b70a5ad3b0e4103a477f2b
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2006,2010,2011,2012 Petr Machata, Red Hat Inc. 4 * Copyright (C) 2010 Zachary T Welch, CodeSourcery 5 * Copyright (C) 2010 Joe Damato 6 * Copyright (C) 1997,1998,2001,2004,2007,2008,2009 Juan Cespedes 7 * Copyright (C) 2006 Olaf Hering, SUSE Linux GmbH 8 * Copyright (C) 2006 Eric Vaitl, Cisco Systems, Inc. 9 * Copyright (C) 2006 Paul Gilliam, IBM Corporation 10 * Copyright (C) 2006 Ian Wienand 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License as 14 * published by the Free Software Foundation; either version 2 of the 15 * License, or (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, but 18 * WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 * General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 25 * 02110-1301 USA 26 */ 27 28#include "config.h" 29 30#include <assert.h> 31#ifdef __linux__ 32#include <endian.h> 33#endif 34#include <errno.h> 35#include <fcntl.h> 36#include <gelf.h> 37#include <inttypes.h> 38#include <search.h> 39#include <stdint.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <string.h> 43#include <unistd.h> 44 45#include "backend.h" 46#include "filter.h" 47#include "library.h" 48#include "ltrace-elf.h" 49#include "proc.h" 50#include "debug.h" 51#include "options.h" 52 53#ifndef ARCH_HAVE_LTELF_DATA 54int 55arch_elf_init(struct ltelf *lte, struct library *lib) 56{ 57 return 0; 58} 59 60void 61arch_elf_destroy(struct ltelf *lte) 62{ 63} 64#endif 65 66static int 67default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte, 68 const char *a_name, GElf_Rela *rela, size_t ndx, 69 struct library_symbol **ret) 70{ 71 char *name = strdup(a_name); 72 if (name == NULL) { 73 fail: 74 free(name); 75 return -1; 76 } 77 78 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela); 79 80 struct library_symbol *libsym = malloc(sizeof(*libsym)); 81 if (libsym == NULL) 82 goto fail; 83 84 /* XXX The double cast should be removed when 85 * arch_addr_t becomes integral type. */ 86 arch_addr_t taddr = (arch_addr_t) 87 (uintptr_t)(addr + lte->bias); 88 89 if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) { 90 free(libsym); 91 goto fail; 92 } 93 94 libsym->next = *ret; 95 *ret = libsym; 96 return 0; 97} 98 99#ifndef ARCH_HAVE_ADD_PLT_ENTRY 100enum plt_status 101arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte, 102 const char *a_name, GElf_Rela *rela, size_t ndx, 103 struct library_symbol **ret) 104{ 105 return plt_default; 106} 107#endif 108 109Elf_Data * 110elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr) 111{ 112 Elf_Data *data = elf_getdata(scn, NULL); 113 if (data == NULL || elf_getdata(scn, data) != NULL 114 || data->d_off || data->d_size != shdr->sh_size) 115 return NULL; 116 return data; 117} 118 119static int 120elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr, 121 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data), 122 void *data) 123{ 124 int i; 125 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 126 Elf_Scn *scn; 127 GElf_Shdr shdr; 128 129 scn = elf_getscn(lte->elf, i); 130 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { 131 debug(1, "Couldn't read section or header."); 132 return -1; 133 } 134 if (predicate(scn, &shdr, data)) { 135 *tgt_sec = scn; 136 *tgt_shdr = shdr; 137 return 0; 138 } 139 } 140 return -1; 141 142} 143 144static int 145inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data) 146{ 147 GElf_Addr addr = *(GElf_Addr *)data; 148 return addr >= shdr->sh_addr 149 && addr < shdr->sh_addr + shdr->sh_size; 150} 151 152int 153elf_get_section_covering(struct ltelf *lte, GElf_Addr addr, 154 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) 155{ 156 return elf_get_section_if(lte, tgt_sec, tgt_shdr, 157 &inside_p, &addr); 158} 159 160static int 161type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data) 162{ 163 GElf_Word type = *(GElf_Word *)data; 164 return shdr->sh_type == type; 165} 166 167int 168elf_get_section_type(struct ltelf *lte, GElf_Word type, 169 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) 170{ 171 return elf_get_section_if(lte, tgt_sec, tgt_shdr, 172 &type_p, &type); 173} 174 175struct section_named_data { 176 struct ltelf *lte; 177 const char *name; 178}; 179 180static int 181name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d) 182{ 183 struct section_named_data *data = d; 184 const char *name = elf_strptr(data->lte->elf, 185 data->lte->ehdr.e_shstrndx, 186 shdr->sh_name); 187 return strcmp(name, data->name) == 0; 188} 189 190int 191elf_get_section_named(struct ltelf *lte, const char *name, 192 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) 193{ 194 struct section_named_data data = { 195 .lte = lte, 196 .name = name, 197 }; 198 return elf_get_section_if(lte, tgt_sec, tgt_shdr, 199 &name_p, &data); 200} 201 202static int 203need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size) 204{ 205 assert(data != NULL); 206 if (data->d_size < size || offset > data->d_size - size) { 207 debug(1, "Not enough data to read %"PRId64"-byte value" 208 " at offset %"PRId64".", size, offset); 209 return -1; 210 } 211 return 0; 212} 213 214#define DEF_READER(NAME, SIZE) \ 215 int \ 216 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \ 217 { \ 218 if (!need_data(data, offset, SIZE / 8) < 0) \ 219 return -1; \ 220 \ 221 if (data->d_buf == NULL) /* NODATA section */ { \ 222 *retp = 0; \ 223 return 0; \ 224 } \ 225 \ 226 union { \ 227 uint##SIZE##_t dst; \ 228 char buf[0]; \ 229 } u; \ 230 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \ 231 *retp = u.dst; \ 232 return 0; \ 233 } 234 235DEF_READER(elf_read_u16, 16) 236DEF_READER(elf_read_u32, 32) 237DEF_READER(elf_read_u64, 64) 238 239#undef DEF_READER 240 241int 242open_elf(struct ltelf *lte, const char *filename) 243{ 244 lte->fd = open(filename, O_RDONLY); 245 if (lte->fd == -1) 246 return 1; 247 248 elf_version(EV_CURRENT); 249 250#ifdef HAVE_ELF_C_READ_MMAP 251 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); 252#else 253 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); 254#endif 255 256 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) { 257 fprintf(stderr, "\"%s\" is not an ELF file\n", filename); 258 exit(EXIT_FAILURE); 259 } 260 261 if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) { 262 fprintf(stderr, "can't read ELF header of \"%s\": %s\n", 263 filename, elf_errmsg(-1)); 264 exit(EXIT_FAILURE); 265 } 266 267 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) { 268 fprintf(stderr, "\"%s\" is neither an ELF executable" 269 " nor a shared library\n", filename); 270 exit(EXIT_FAILURE); 271 } 272 273 if (1 274#ifdef LT_ELF_MACHINE 275 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS 276 || lte->ehdr.e_machine != LT_ELF_MACHINE) 277#endif 278#ifdef LT_ELF_MACHINE2 279 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 280 || lte->ehdr.e_machine != LT_ELF_MACHINE2) 281#endif 282#ifdef LT_ELF_MACHINE3 283 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 284 || lte->ehdr.e_machine != LT_ELF_MACHINE3) 285#endif 286 ) { 287 fprintf(stderr, 288 "\"%s\" is ELF from incompatible architecture\n", 289 filename); 290 exit(EXIT_FAILURE); 291 } 292 293 return 0; 294} 295 296static void 297read_symbol_table(struct ltelf *lte, const char *filename, 298 Elf_Scn *scn, GElf_Shdr *shdr, const char *name, 299 Elf_Data **datap, size_t *countp, const char **strsp) 300{ 301 *datap = elf_getdata(scn, NULL); 302 *countp = shdr->sh_size / shdr->sh_entsize; 303 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL) 304 && options.static_filter != NULL) { 305 fprintf(stderr, "Couldn't get data of section" 306 " %s from \"%s\": %s\n", 307 name, filename, elf_errmsg(-1)); 308 exit(EXIT_FAILURE); 309 } 310 311 scn = elf_getscn(lte->elf, shdr->sh_link); 312 GElf_Shdr shdr2; 313 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) { 314 fprintf(stderr, "Couldn't get header of section" 315 " #%d from \"%s\": %s\n", 316 shdr2.sh_link, filename, elf_errmsg(-1)); 317 exit(EXIT_FAILURE); 318 } 319 320 Elf_Data *data = elf_getdata(scn, NULL); 321 if (data == NULL || elf_getdata(scn, data) != NULL 322 || shdr2.sh_size != data->d_size || data->d_off) { 323 fprintf(stderr, "Couldn't get data of section" 324 " #%d from \"%s\": %s\n", 325 shdr2.sh_link, filename, elf_errmsg(-1)); 326 exit(EXIT_FAILURE); 327 } 328 329 *strsp = data->d_buf; 330} 331 332static int 333do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias) 334{ 335 int i; 336 GElf_Addr relplt_addr = 0; 337 GElf_Addr soname_offset = 0; 338 339 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); 340 debug(1, "Reading ELF from %s...", filename); 341 342 if (open_elf(lte, filename) < 0) 343 return -1; 344 345 /* Find out the base address. */ 346 { 347 GElf_Phdr phdr; 348 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) { 349 if (phdr.p_type == PT_LOAD) { 350 lte->base_addr = phdr.p_vaddr + bias; 351 break; 352 } 353 } 354 } 355 356 if (lte->base_addr == 0) { 357 fprintf(stderr, "Couldn't determine base address of %s\n", 358 filename); 359 return -1; 360 } 361 362 lte->bias = bias; 363 lte->entry_addr = lte->ehdr.e_entry + lte->bias; 364 365 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 366 Elf_Scn *scn; 367 GElf_Shdr shdr; 368 const char *name; 369 370 scn = elf_getscn(lte->elf, i); 371 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { 372 fprintf(stderr, "Couldn't get section #%d from" 373 " \"%s\": %s\n", i, filename, elf_errmsg(-1)); 374 exit(EXIT_FAILURE); 375 } 376 377 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); 378 if (name == NULL) { 379 fprintf(stderr, "Couldn't get name of section #%d from" 380 " \"%s\": %s\n", i, filename, elf_errmsg(-1)); 381 exit(EXIT_FAILURE); 382 } 383 384 if (shdr.sh_type == SHT_SYMTAB) { 385 read_symbol_table(lte, filename, 386 scn, &shdr, name, <e->symtab, 387 <e->symtab_count, <e->strtab); 388 389 } else if (shdr.sh_type == SHT_DYNSYM) { 390 read_symbol_table(lte, filename, 391 scn, &shdr, name, <e->dynsym, 392 <e->dynsym_count, <e->dynstr); 393 394 } else if (shdr.sh_type == SHT_DYNAMIC) { 395 Elf_Data *data; 396 size_t j; 397 398 lte->dyn_addr = shdr.sh_addr; 399 lte->dyn_sz = shdr.sh_size; 400 401 data = elf_getdata(scn, NULL); 402 if (data == NULL || elf_getdata(scn, data) != NULL) { 403 fprintf(stderr, "Couldn't get .dynamic data" 404 " from \"%s\": %s\n", 405 filename, strerror(errno)); 406 exit(EXIT_FAILURE); 407 } 408 409 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { 410 GElf_Dyn dyn; 411 412 if (gelf_getdyn(data, j, &dyn) == NULL) { 413 fprintf(stderr, "Couldn't get .dynamic" 414 " data from \"%s\": %s\n", 415 filename, strerror(errno)); 416 exit(EXIT_FAILURE); 417 } 418 if (dyn.d_tag == DT_JMPREL) 419 relplt_addr = dyn.d_un.d_ptr; 420 else if (dyn.d_tag == DT_PLTRELSZ) 421 lte->relplt_size = dyn.d_un.d_val; 422 else if (dyn.d_tag == DT_SONAME) 423 soname_offset = dyn.d_un.d_val; 424 } 425 } else if (shdr.sh_type == SHT_PROGBITS 426 || shdr.sh_type == SHT_NOBITS) { 427 if (strcmp(name, ".plt") == 0) { 428 lte->plt_addr = shdr.sh_addr; 429 lte->plt_size = shdr.sh_size; 430 lte->plt_data = elf_loaddata(scn, &shdr); 431 if (lte->plt_data == NULL) 432 fprintf(stderr, 433 "Can't load .plt data\n"); 434 lte->plt_flags = shdr.sh_flags; 435 } 436#ifdef ARCH_SUPPORTS_OPD 437 else if (strcmp(name, ".opd") == 0) { 438 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; 439 lte->opd_size = shdr.sh_size; 440 lte->opd = elf_rawdata(scn, NULL); 441 } 442#endif 443 } 444 } 445 446 if (lte->dynsym == NULL || lte->dynstr == NULL) { 447 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n", 448 filename); 449 exit(EXIT_FAILURE); 450 } 451 452 if (!relplt_addr || !lte->plt_addr) { 453 debug(1, "%s has no PLT relocations", filename); 454 lte->relplt = NULL; 455 lte->relplt_count = 0; 456 } else if (lte->relplt_size == 0) { 457 debug(1, "%s has unknown PLT size", filename); 458 lte->relplt = NULL; 459 lte->relplt_count = 0; 460 } else { 461 462 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 463 Elf_Scn *scn; 464 GElf_Shdr shdr; 465 466 scn = elf_getscn(lte->elf, i); 467 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { 468 fprintf(stderr, "Couldn't get section header" 469 " from \"%s\": %s\n", 470 filename, elf_errmsg(-1)); 471 exit(EXIT_FAILURE); 472 } 473 if (shdr.sh_addr == relplt_addr 474 && shdr.sh_size == lte->relplt_size) { 475 lte->relplt = elf_getdata(scn, NULL); 476 lte->relplt_count = 477 shdr.sh_size / shdr.sh_entsize; 478 if (lte->relplt == NULL 479 || elf_getdata(scn, lte->relplt) != NULL) { 480 fprintf(stderr, "Couldn't get .rel*.plt" 481 " data from \"%s\": %s\n", 482 filename, elf_errmsg(-1)); 483 exit(EXIT_FAILURE); 484 } 485 break; 486 } 487 } 488 489 if (i == lte->ehdr.e_shnum) { 490 fprintf(stderr, 491 "Couldn't find .rel*.plt section in \"%s\"\n", 492 filename); 493 exit(EXIT_FAILURE); 494 } 495 496 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); 497 } 498 499 if (soname_offset != 0) 500 lte->soname = lte->dynstr + soname_offset; 501 502 return 0; 503} 504 505/* XXX temporarily non-static */ 506void 507do_close_elf(struct ltelf *lte) { 508 debug(DEBUG_FUNCTION, "do_close_elf()"); 509 arch_elf_destroy(lte); 510 elf_end(lte->elf); 511 close(lte->fd); 512} 513 514#ifndef ARCH_HAVE_GET_SYMINFO 515int 516arch_get_sym_info(struct ltelf *lte, const char *filename, 517 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym) 518{ 519 int i = sym_index; 520 GElf_Rel rel; 521 void *ret; 522 523 if (lte->relplt->d_type == ELF_T_REL) { 524 ret = gelf_getrel(lte->relplt, i, &rel); 525 rela->r_offset = rel.r_offset; 526 rela->r_info = rel.r_info; 527 rela->r_addend = 0; 528 } else { 529 ret = gelf_getrela(lte->relplt, i, rela); 530 } 531 532 if (ret == NULL 533 || ELF64_R_SYM(rela->r_info) >= lte->dynsym_count 534 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela->r_info), 535 sym) == NULL) { 536 fprintf(stderr, 537 "Couldn't get relocation from \"%s\": %s\n", 538 filename, elf_errmsg(-1)); 539 exit(EXIT_FAILURE); 540 } 541 542 return 0; 543} 544#endif 545 546static void 547mark_chain_latent(struct library_symbol *libsym) 548{ 549 for (; libsym != NULL; libsym = libsym->next) { 550 debug(DEBUG_FUNCTION, "marking %s latent", libsym->name); 551 libsym->latent = 1; 552 } 553} 554 555static int 556populate_plt(struct Process *proc, const char *filename, 557 struct ltelf *lte, struct library *lib, 558 int latent_plts) 559{ 560 size_t i; 561 for (i = 0; i < lte->relplt_count; ++i) { 562 GElf_Rela rela; 563 GElf_Sym sym; 564 565 if (arch_get_sym_info(lte, filename, i, &rela, &sym) < 0) 566 continue; /* Skip this entry. */ 567 568 char const *name = lte->dynstr + sym.st_name; 569 570 /* If the symbol wasn't matched, reject it, unless we 571 * need to keep latent PLT breakpoints for tracing 572 * exports. */ 573 int matched = filter_matches_symbol(options.plt_filter, 574 name, lib); 575 if (!matched && !latent_plts) 576 continue; 577 578 struct library_symbol *libsym = NULL; 579 switch (arch_elf_add_plt_entry(proc, lte, name, 580 &rela, i, &libsym)) { 581 case plt_default: 582 if (default_elf_add_plt_entry(proc, lte, name, 583 &rela, i, &libsym) < 0) 584 /* fall-through */ 585 case plt_fail: 586 return -1; 587 /* fall-through */ 588 case plt_ok: 589 if (libsym != NULL) { 590 /* If we are adding those symbols just 591 * for tracing exports, mark them all 592 * latent. */ 593 if (!matched) 594 mark_chain_latent(libsym); 595 library_add_symbol(lib, libsym); 596 } 597 } 598 } 599 return 0; 600} 601 602/* When -x rules result in request to trace several aliases, we only 603 * want to add such symbol once. The only way that those symbols 604 * differ in is their name, e.g. in glibc you have __GI___libc_free, 605 * __cfree, __free, __libc_free, cfree and free all defined on the 606 * same address. So instead we keep this unique symbol struct for 607 * each address, and replace name in libsym with a shorter variant if 608 * we find it. */ 609struct unique_symbol { 610 arch_addr_t addr; 611 struct library_symbol *libsym; 612}; 613 614static int 615unique_symbol_cmp(const void *key, const void *val) 616{ 617 const struct unique_symbol *sym_key = key; 618 const struct unique_symbol *sym_val = val; 619 return sym_key->addr != sym_val->addr; 620} 621 622static int 623populate_this_symtab(struct Process *proc, const char *filename, 624 struct ltelf *lte, struct library *lib, 625 Elf_Data *symtab, const char *strtab, size_t size, 626 struct library_exported_name **names) 627{ 628 /* If a valid NAMES is passed, we pass in *NAMES a list of 629 * symbol names that this library exports. */ 630 if (names != NULL) 631 *names = NULL; 632 633 /* Using sorted array would be arguably better, but this 634 * should be well enough for the number of symbols that we 635 * typically deal with. */ 636 size_t num_symbols = 0; 637 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size); 638 if (symbols == NULL) { 639 fprintf(stderr, "couldn't insert symbols for -x: %s\n", 640 strerror(errno)); 641 return -1; 642 } 643 644 GElf_Word secflags[lte->ehdr.e_shnum]; 645 size_t i; 646 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 647 Elf_Scn *scn = elf_getscn(lte->elf, i); 648 if (scn == NULL) 649 continue; 650 GElf_Shdr shdr; 651 if (gelf_getshdr(scn, &shdr) == NULL) 652 continue; 653 secflags[i] = shdr.sh_flags; 654 } 655 656 size_t lib_len = strlen(lib->soname); 657 for (i = 0; i < size; ++i) { 658 GElf_Sym sym; 659 if (gelf_getsym(symtab, i, &sym) == NULL) { 660 fail: 661 fprintf(stderr, 662 "couldn't get symbol #%zd from %s: %s\n", 663 i, filename, elf_errmsg(-1)); 664 continue; 665 } 666 667 /* XXX support IFUNC as well. */ 668 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC 669 || sym.st_value == 0 670 || sym.st_shndx == STN_UNDEF) 671 continue; 672 673 /* Find symbol name and snip version. */ 674 const char *orig_name = strtab + sym.st_name; 675 const char *version = strchr(orig_name, '@'); 676 size_t len = version != NULL ? (assert(version > orig_name), 677 (size_t)(version - orig_name)) 678 : strlen(orig_name); 679 char name[len + 1]; 680 memcpy(name, orig_name, len); 681 name[len] = 0; 682 683 /* If we are interested in exports, store this name. */ 684 char *name_copy = NULL; 685 if (names != NULL) { 686 struct library_exported_name *export = NULL; 687 name_copy = strdup(name); 688 689 if (name_copy == NULL 690 || (export = malloc(sizeof(*export))) == NULL) { 691 free(name_copy); 692 fprintf(stderr, "Couldn't store symbol %s. " 693 "Tracing may be incomplete.\n", name); 694 } else { 695 export->name = name_copy; 696 export->own_name = 1; 697 export->next = *names; 698 *names = export; 699 } 700 } 701 702 /* If the symbol is not matched, skip it. We already 703 * stored it to export list above. */ 704 if (!filter_matches_symbol(options.static_filter, name, lib)) 705 continue; 706 707 arch_addr_t addr = (arch_addr_t) 708 (uintptr_t)(sym.st_value + lte->bias); 709 arch_addr_t naddr; 710 711 /* On arches that support OPD, the value of typical 712 * function symbol will be a pointer to .opd, but some 713 * will point directly to .text. We don't want to 714 * translate those. */ 715 if (secflags[sym.st_shndx] & SHF_EXECINSTR) { 716 naddr = addr; 717 } else if (arch_translate_address(lte, addr, &naddr) < 0) { 718 fprintf(stderr, 719 "couldn't translate address of %s@%s: %s\n", 720 name, lib->soname, strerror(errno)); 721 continue; 722 } 723 724 char *full_name; 725 int own_full_name = 1; 726 if (lib->type != LT_LIBTYPE_MAIN) { 727 full_name = malloc(strlen(name) + 1 + lib_len + 1); 728 if (full_name == NULL) 729 goto fail; 730 sprintf(full_name, "%s@%s", name, lib->soname); 731 } else { 732 if (name_copy == NULL) { 733 full_name = strdup(name); 734 if (full_name == NULL) 735 goto fail; 736 } else { 737 full_name = name_copy; 738 own_full_name = 0; 739 } 740 } 741 742 /* Look whether we already have a symbol for this 743 * address. If not, add this one. */ 744 struct unique_symbol key = { naddr, NULL }; 745 struct unique_symbol *unique 746 = lsearch(&key, symbols, &num_symbols, 747 sizeof(*symbols), &unique_symbol_cmp); 748 749 if (unique->libsym == NULL) { 750 struct library_symbol *libsym = malloc(sizeof(*libsym)); 751 if (libsym == NULL 752 || library_symbol_init(libsym, naddr, 753 full_name, own_full_name, 754 LS_TOPLT_NONE) < 0) { 755 --num_symbols; 756 goto fail; 757 } 758 unique->libsym = libsym; 759 unique->addr = naddr; 760 761 } else if (strlen(full_name) < strlen(unique->libsym->name)) { 762 library_symbol_set_name(unique->libsym, 763 full_name, own_full_name); 764 765 } else if (own_full_name) { 766 free(full_name); 767 } 768 } 769 770 for (i = 0; i < num_symbols; ++i) { 771 assert(symbols[i].libsym != NULL); 772 library_add_symbol(lib, symbols[i].libsym); 773 } 774 775 free(symbols); 776 777 return 0; 778} 779 780static int 781populate_symtab(struct Process *proc, const char *filename, 782 struct ltelf *lte, struct library *lib, 783 int symtabs, int exports) 784{ 785 int status; 786 if (symtabs && lte->symtab != NULL && lte->strtab != NULL 787 && (status = populate_this_symtab(proc, filename, lte, lib, 788 lte->symtab, lte->strtab, 789 lte->symtab_count, NULL)) < 0) 790 return status; 791 792 /* Check whether we want to trace symbols implemented by this 793 * library (-l). */ 794 struct library_exported_name **names = NULL; 795 if (exports) { 796 debug(DEBUG_FUNCTION, "-l matches %s", lib->soname); 797 names = &lib->exported_names; 798 } 799 800 return populate_this_symtab(proc, filename, lte, lib, 801 lte->dynsym, lte->dynstr, 802 lte->dynsym_count, names); 803} 804 805int 806ltelf_read_library(struct library *lib, struct Process *proc, 807 const char *filename, GElf_Addr bias) 808{ 809 struct ltelf lte = {}; 810 if (do_init_elf(<e, filename, bias) < 0) 811 return -1; 812 if (arch_elf_init(<e, lib) < 0) { 813 fprintf(stderr, "Backend initialization failed.\n"); 814 return -1; 815 } 816 817 proc->e_machine = lte.ehdr.e_machine; 818 proc->e_class = lte.ehdr.e_ident[EI_CLASS]; 819 820 int status = 0; 821 if (lib == NULL) 822 goto fail; 823 824 /* Note that we set soname and pathname as soon as they are 825 * allocated, so in case of further errors, this get released 826 * when LIB is release, which should happen in the caller when 827 * we return error. */ 828 829 if (lib->pathname == NULL) { 830 char *pathname = strdup(filename); 831 if (pathname == NULL) 832 goto fail; 833 library_set_pathname(lib, pathname, 1); 834 } 835 836 if (lte.soname != NULL) { 837 char *soname = strdup(lte.soname); 838 if (soname == NULL) 839 goto fail; 840 library_set_soname(lib, soname, 1); 841 } else { 842 const char *soname = rindex(lib->pathname, '/') + 1; 843 if (soname == NULL) 844 soname = lib->pathname; 845 library_set_soname(lib, soname, 0); 846 } 847 848 /* XXX The double cast should be removed when 849 * arch_addr_t becomes integral type. */ 850 arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr; 851 if (arch_translate_address(<e, entry, &entry) < 0) 852 goto fail; 853 854 /* XXX The double cast should be removed when 855 * arch_addr_t becomes integral type. */ 856 lib->base = (arch_addr_t)(uintptr_t)lte.base_addr; 857 lib->entry = entry; 858 /* XXX The double cast should be removed when 859 * arch_addr_t becomes integral type. */ 860 lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr; 861 862 /* There are two reasons that we need to inspect symbol tables 863 * or populate PLT entries. Either the user requested 864 * corresponding tracing features (respectively -x and -e), or 865 * they requested tracing exported symbols (-l). 866 * 867 * In the latter case we need to keep even those PLT slots 868 * that are not requested by -e (but we keep them latent). We 869 * also need to inspect .dynsym to find what exports this 870 * library provide, to turn on existing latent PLT 871 * entries. */ 872 873 int plts = filter_matches_library(options.plt_filter, lib); 874 if ((plts || options.export_filter != NULL) 875 && populate_plt(proc, filename, <e, lib, 876 options.export_filter != NULL) < 0) 877 goto fail; 878 879 int exports = filter_matches_library(options.export_filter, lib); 880 int symtabs = filter_matches_library(options.static_filter, lib); 881 if ((symtabs || exports) 882 && populate_symtab(proc, filename, <e, lib, 883 symtabs, exports) < 0) 884 goto fail; 885 886done: 887 do_close_elf(<e); 888 return status; 889 890fail: 891 status = -1; 892 goto done; 893} 894 895struct library * 896ltelf_read_main_binary(struct Process *proc, const char *path) 897{ 898 struct library *lib = malloc(sizeof(*lib)); 899 if (lib == NULL) 900 return NULL; 901 library_init(lib, LT_LIBTYPE_MAIN); 902 library_set_pathname(lib, path, 0); 903 904 /* There is a race between running the process and reading its 905 * binary for internal consumption. So open the binary from 906 * the /proc filesystem. XXX Note that there is similar race 907 * for libraries, but there we don't have a nice answer like 908 * that. Presumably we could read the DSOs from the process 909 * memory image, but that's not currently done. */ 910 char *fname = pid2name(proc->pid); 911 if (ltelf_read_library(lib, proc, fname, 0) < 0) { 912 library_destroy(lib); 913 free(lib); 914 return NULL; 915 } 916 917 return lib; 918} 919