ltrace-elf.c revision aafb00b7d7751049b99cac3953b5021e4f474ac4
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 return -1; 145 146} 147 148static int 149inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data) 150{ 151 GElf_Addr addr = *(GElf_Addr *)data; 152 return addr >= shdr->sh_addr 153 && addr < shdr->sh_addr + shdr->sh_size; 154} 155 156int 157elf_get_section_covering(struct ltelf *lte, GElf_Addr addr, 158 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) 159{ 160 return elf_get_section_if(lte, tgt_sec, tgt_shdr, 161 &inside_p, &addr); 162} 163 164static int 165type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data) 166{ 167 GElf_Word type = *(GElf_Word *)data; 168 return shdr->sh_type == type; 169} 170 171int 172elf_get_section_type(struct ltelf *lte, GElf_Word type, 173 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) 174{ 175 return elf_get_section_if(lte, tgt_sec, tgt_shdr, 176 &type_p, &type); 177} 178 179struct section_named_data { 180 struct ltelf *lte; 181 const char *name; 182}; 183 184static int 185name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d) 186{ 187 struct section_named_data *data = d; 188 const char *name = elf_strptr(data->lte->elf, 189 data->lte->ehdr.e_shstrndx, 190 shdr->sh_name); 191 return strcmp(name, data->name) == 0; 192} 193 194int 195elf_get_section_named(struct ltelf *lte, const char *name, 196 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) 197{ 198 struct section_named_data data = { 199 .lte = lte, 200 .name = name, 201 }; 202 return elf_get_section_if(lte, tgt_sec, tgt_shdr, 203 &name_p, &data); 204} 205 206static int 207need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size) 208{ 209 assert(data != NULL); 210 if (data->d_size < size || offset > data->d_size - size) { 211 debug(1, "Not enough data to read %"PRId64"-byte value" 212 " at offset %"PRId64".", size, offset); 213 return -1; 214 } 215 return 0; 216} 217 218#define DEF_READER(NAME, SIZE) \ 219 int \ 220 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \ 221 { \ 222 if (!need_data(data, offset, SIZE / 8) < 0) \ 223 return -1; \ 224 \ 225 if (data->d_buf == NULL) /* NODATA section */ { \ 226 *retp = 0; \ 227 return 0; \ 228 } \ 229 \ 230 union { \ 231 uint##SIZE##_t dst; \ 232 char buf[0]; \ 233 } u; \ 234 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \ 235 *retp = u.dst; \ 236 return 0; \ 237 } 238 239DEF_READER(elf_read_u16, 16) 240DEF_READER(elf_read_u32, 32) 241DEF_READER(elf_read_u64, 64) 242 243#undef DEF_READER 244 245int 246open_elf(struct ltelf *lte, const char *filename) 247{ 248 lte->fd = open(filename, O_RDONLY); 249 if (lte->fd == -1) 250 return 1; 251 252 elf_version(EV_CURRENT); 253 254#ifdef HAVE_ELF_C_READ_MMAP 255 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); 256#else 257 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); 258#endif 259 260 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) { 261 fprintf(stderr, "\"%s\" is not an ELF file\n", filename); 262 exit(EXIT_FAILURE); 263 } 264 265 if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) { 266 fprintf(stderr, "can't read ELF header of \"%s\": %s\n", 267 filename, elf_errmsg(-1)); 268 exit(EXIT_FAILURE); 269 } 270 271 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) { 272 fprintf(stderr, "\"%s\" is neither an ELF executable" 273 " nor a shared library\n", filename); 274 exit(EXIT_FAILURE); 275 } 276 277 if (1 278#ifdef LT_ELF_MACHINE 279 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS 280 || lte->ehdr.e_machine != LT_ELF_MACHINE) 281#endif 282#ifdef LT_ELF_MACHINE2 283 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 284 || lte->ehdr.e_machine != LT_ELF_MACHINE2) 285#endif 286#ifdef LT_ELF_MACHINE3 287 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 288 || lte->ehdr.e_machine != LT_ELF_MACHINE3) 289#endif 290 ) { 291 fprintf(stderr, 292 "\"%s\" is ELF from incompatible architecture\n", 293 filename); 294 exit(EXIT_FAILURE); 295 } 296 297 return 0; 298} 299 300static void 301read_symbol_table(struct ltelf *lte, const char *filename, 302 Elf_Scn *scn, GElf_Shdr *shdr, const char *name, 303 Elf_Data **datap, size_t *countp, const char **strsp) 304{ 305 *datap = elf_getdata(scn, NULL); 306 *countp = shdr->sh_size / shdr->sh_entsize; 307 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL) 308 && options.static_filter != NULL) { 309 fprintf(stderr, "Couldn't get data of section" 310 " %s from \"%s\": %s\n", 311 name, filename, elf_errmsg(-1)); 312 exit(EXIT_FAILURE); 313 } 314 315 scn = elf_getscn(lte->elf, shdr->sh_link); 316 GElf_Shdr shdr2; 317 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) { 318 fprintf(stderr, "Couldn't get header of section" 319 " #%d from \"%s\": %s\n", 320 shdr2.sh_link, filename, elf_errmsg(-1)); 321 exit(EXIT_FAILURE); 322 } 323 324 Elf_Data *data = elf_getdata(scn, NULL); 325 if (data == NULL || elf_getdata(scn, data) != NULL 326 || shdr2.sh_size != data->d_size || data->d_off) { 327 fprintf(stderr, "Couldn't get data of section" 328 " #%d from \"%s\": %s\n", 329 shdr2.sh_link, filename, elf_errmsg(-1)); 330 exit(EXIT_FAILURE); 331 } 332 333 *strsp = data->d_buf; 334} 335 336static int 337do_init_elf(struct ltelf *lte, const char *filename) 338{ 339 int i; 340 GElf_Addr relplt_addr = 0; 341 GElf_Addr soname_offset = 0; 342 343 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); 344 debug(1, "Reading ELF from %s...", filename); 345 346 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 347 Elf_Scn *scn; 348 GElf_Shdr shdr; 349 const char *name; 350 351 scn = elf_getscn(lte->elf, i); 352 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { 353 fprintf(stderr, "Couldn't get section #%d from" 354 " \"%s\": %s\n", i, filename, elf_errmsg(-1)); 355 exit(EXIT_FAILURE); 356 } 357 358 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); 359 if (name == NULL) { 360 fprintf(stderr, "Couldn't get name of section #%d from" 361 " \"%s\": %s\n", i, filename, elf_errmsg(-1)); 362 exit(EXIT_FAILURE); 363 } 364 365 if (shdr.sh_type == SHT_SYMTAB) { 366 read_symbol_table(lte, filename, 367 scn, &shdr, name, <e->symtab, 368 <e->symtab_count, <e->strtab); 369 370 } else if (shdr.sh_type == SHT_DYNSYM) { 371 read_symbol_table(lte, filename, 372 scn, &shdr, name, <e->dynsym, 373 <e->dynsym_count, <e->dynstr); 374 375 } else if (shdr.sh_type == SHT_DYNAMIC) { 376 Elf_Data *data; 377 size_t j; 378 379 lte->dyn_addr = shdr.sh_addr + lte->bias; 380 lte->dyn_sz = shdr.sh_size; 381 382 data = elf_getdata(scn, NULL); 383 if (data == NULL || elf_getdata(scn, data) != NULL) { 384 fprintf(stderr, "Couldn't get .dynamic data" 385 " from \"%s\": %s\n", 386 filename, strerror(errno)); 387 exit(EXIT_FAILURE); 388 } 389 390 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { 391 GElf_Dyn dyn; 392 393 if (gelf_getdyn(data, j, &dyn) == NULL) { 394 fprintf(stderr, "Couldn't get .dynamic" 395 " data from \"%s\": %s\n", 396 filename, strerror(errno)); 397 exit(EXIT_FAILURE); 398 } 399 if (dyn.d_tag == DT_JMPREL) 400 relplt_addr = dyn.d_un.d_ptr; 401 else if (dyn.d_tag == DT_PLTRELSZ) 402 lte->relplt_size = dyn.d_un.d_val; 403 else if (dyn.d_tag == DT_SONAME) 404 soname_offset = dyn.d_un.d_val; 405 } 406 } else if (shdr.sh_type == SHT_PROGBITS 407 || shdr.sh_type == SHT_NOBITS) { 408 if (strcmp(name, ".plt") == 0) { 409 lte->plt_addr = shdr.sh_addr; 410 lte->plt_size = shdr.sh_size; 411 lte->plt_data = elf_loaddata(scn, &shdr); 412 if (lte->plt_data == NULL) 413 fprintf(stderr, 414 "Can't load .plt data\n"); 415 lte->plt_flags = shdr.sh_flags; 416 } 417#ifdef ARCH_SUPPORTS_OPD 418 else if (strcmp(name, ".opd") == 0) { 419 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; 420 lte->opd_size = shdr.sh_size; 421 lte->opd = elf_rawdata(scn, NULL); 422 } 423#endif 424 } 425 } 426 427 if (lte->dynsym == NULL || lte->dynstr == NULL) { 428 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n", 429 filename); 430 exit(EXIT_FAILURE); 431 } 432 433 if (!relplt_addr || !lte->plt_addr) { 434 debug(1, "%s has no PLT relocations", filename); 435 lte->relplt = NULL; 436 lte->relplt_count = 0; 437 } else if (lte->relplt_size == 0) { 438 debug(1, "%s has unknown PLT size", filename); 439 lte->relplt = NULL; 440 lte->relplt_count = 0; 441 } else { 442 443 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 444 Elf_Scn *scn; 445 GElf_Shdr shdr; 446 447 scn = elf_getscn(lte->elf, i); 448 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { 449 fprintf(stderr, "Couldn't get section header" 450 " from \"%s\": %s\n", 451 filename, elf_errmsg(-1)); 452 exit(EXIT_FAILURE); 453 } 454 if (shdr.sh_addr == relplt_addr 455 && shdr.sh_size == lte->relplt_size) { 456 lte->relplt = elf_getdata(scn, NULL); 457 lte->relplt_count = 458 shdr.sh_size / shdr.sh_entsize; 459 if (lte->relplt == NULL 460 || elf_getdata(scn, lte->relplt) != NULL) { 461 fprintf(stderr, "Couldn't get .rel*.plt" 462 " data from \"%s\": %s\n", 463 filename, elf_errmsg(-1)); 464 exit(EXIT_FAILURE); 465 } 466 break; 467 } 468 } 469 470 if (i == lte->ehdr.e_shnum) { 471 fprintf(stderr, 472 "Couldn't find .rel*.plt section in \"%s\"\n", 473 filename); 474 exit(EXIT_FAILURE); 475 } 476 477 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); 478 } 479 480 if (soname_offset != 0) 481 lte->soname = lte->dynstr + soname_offset; 482 483 return 0; 484} 485 486void 487do_close_elf(struct ltelf *lte) 488{ 489 debug(DEBUG_FUNCTION, "do_close_elf()"); 490 arch_elf_destroy(lte); 491 elf_end(lte->elf); 492 close(lte->fd); 493} 494 495int 496elf_get_sym_info(struct ltelf *lte, const char *filename, 497 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym) 498{ 499 int i = sym_index; 500 GElf_Rel rel; 501 void *ret; 502 503 if (lte->relplt->d_type == ELF_T_REL) { 504 ret = gelf_getrel(lte->relplt, i, &rel); 505 rela->r_offset = rel.r_offset; 506 rela->r_info = rel.r_info; 507 rela->r_addend = 0; 508 } else { 509 ret = gelf_getrela(lte->relplt, i, rela); 510 } 511 512 if (ret == NULL 513 || ELF64_R_SYM(rela->r_info) >= lte->dynsym_count 514 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela->r_info), 515 sym) == NULL) { 516 fprintf(stderr, 517 "Couldn't get relocation from \"%s\": %s\n", 518 filename, elf_errmsg(-1)); 519 exit(EXIT_FAILURE); 520 } 521 522 return 0; 523} 524 525#ifndef ARCH_HAVE_GET_SYMINFO 526int 527arch_get_sym_info(struct ltelf *lte, const char *filename, 528 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym) 529{ 530 return elf_get_sym_info(lte, filename, sym_index, rela, sym); 531} 532#endif 533 534static void 535mark_chain_latent(struct library_symbol *libsym) 536{ 537 for (; libsym != NULL; libsym = libsym->next) { 538 debug(DEBUG_FUNCTION, "marking %s latent", libsym->name); 539 libsym->latent = 1; 540 } 541} 542 543static int 544populate_plt(struct process *proc, const char *filename, 545 struct ltelf *lte, struct library *lib, 546 int latent_plts) 547{ 548 size_t i; 549 for (i = 0; i < lte->relplt_count; ++i) { 550 GElf_Rela rela; 551 GElf_Sym sym; 552 553 if (arch_get_sym_info(lte, filename, i, &rela, &sym) < 0) 554 continue; /* Skip this entry. */ 555 556 char const *name = lte->dynstr + sym.st_name; 557 558 /* If the symbol wasn't matched, reject it, unless we 559 * need to keep latent PLT breakpoints for tracing 560 * exports. */ 561 int matched = filter_matches_symbol(options.plt_filter, 562 name, lib); 563 if (!matched && !latent_plts) 564 continue; 565 566 struct library_symbol *libsym = NULL; 567 switch (arch_elf_add_plt_entry(proc, lte, name, 568 &rela, i, &libsym)) { 569 case PLT_DEFAULT: 570 if (default_elf_add_plt_entry(proc, lte, name, 571 &rela, i, &libsym) < 0) 572 /* fall-through */ 573 case PLT_FAIL: 574 return -1; 575 /* fall-through */ 576 case PLT_OK: 577 if (libsym != NULL) { 578 /* If we are adding those symbols just 579 * for tracing exports, mark them all 580 * latent. */ 581 if (!matched) 582 mark_chain_latent(libsym); 583 library_add_symbol(lib, libsym); 584 } 585 } 586 } 587 return 0; 588} 589 590/* When -x rules result in request to trace several aliases, we only 591 * want to add such symbol once. The only way that those symbols 592 * differ in is their name, e.g. in glibc you have __GI___libc_free, 593 * __cfree, __free, __libc_free, cfree and free all defined on the 594 * same address. So instead we keep this unique symbol struct for 595 * each address, and replace name in libsym with a shorter variant if 596 * we find it. */ 597struct unique_symbol { 598 arch_addr_t addr; 599 struct library_symbol *libsym; 600}; 601 602static int 603unique_symbol_cmp(const void *key, const void *val) 604{ 605 const struct unique_symbol *sym_key = key; 606 const struct unique_symbol *sym_val = val; 607 return sym_key->addr != sym_val->addr; 608} 609 610static enum callback_status 611symbol_with_address(struct library_symbol *sym, void *addrptr) 612{ 613 return sym->enter_addr == *(arch_addr_t *)addrptr 614 ? CBS_STOP : CBS_CONT; 615} 616 617static int 618populate_this_symtab(struct process *proc, const char *filename, 619 struct ltelf *lte, struct library *lib, 620 Elf_Data *symtab, const char *strtab, size_t size, 621 struct library_exported_name **names) 622{ 623 /* If a valid NAMES is passed, we pass in *NAMES a list of 624 * symbol names that this library exports. */ 625 if (names != NULL) 626 *names = NULL; 627 628 /* Using sorted array would be arguably better, but this 629 * should be well enough for the number of symbols that we 630 * typically deal with. */ 631 size_t num_symbols = 0; 632 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size); 633 if (symbols == NULL) { 634 fprintf(stderr, "couldn't insert symbols for -x: %s\n", 635 strerror(errno)); 636 return -1; 637 } 638 639 GElf_Word secflags[lte->ehdr.e_shnum]; 640 size_t i; 641 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 642 Elf_Scn *scn = elf_getscn(lte->elf, i); 643 if (scn == NULL) 644 continue; 645 GElf_Shdr shdr; 646 if (gelf_getshdr(scn, &shdr) == NULL) 647 continue; 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 release, which should happen in the caller when 877 * 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 return NULL; 973 if (read_module(lib, proc, fname, 0, 1) < 0) { 974 library_destroy(lib); 975 free(lib); 976 return NULL; 977 } 978 free(fname); 979 980 return lib; 981} 982