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