ltrace-elf.c revision 439ab5bfac8588e52c77e22c96fb397787512d0e
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_u8, 8) 241DEF_READER(elf_read_u16, 16) 242DEF_READER(elf_read_u32, 32) 243DEF_READER(elf_read_u64, 64) 244 245#undef DEF_READER 246 247#define DEF_READER(NAME, SIZE) \ 248 int \ 249 NAME(Elf_Data *data, GElf_Xword *offset, uint##SIZE##_t *retp) \ 250 { \ 251 int rc = elf_read_u##SIZE(data, *offset, retp); \ 252 if (rc < 0) \ 253 return rc; \ 254 *offset += SIZE / 8; \ 255 return 0; \ 256 } 257 258DEF_READER(elf_read_next_u8, 8) 259DEF_READER(elf_read_next_u16, 16) 260DEF_READER(elf_read_next_u32, 32) 261DEF_READER(elf_read_next_u64, 64) 262 263#undef DEF_READER 264 265int 266open_elf(struct ltelf *lte, const char *filename) 267{ 268 lte->fd = open(filename, O_RDONLY); 269 if (lte->fd == -1) 270 return 1; 271 272 elf_version(EV_CURRENT); 273 274#ifdef HAVE_ELF_C_READ_MMAP 275 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); 276#else 277 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); 278#endif 279 280 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) { 281 fprintf(stderr, "\"%s\" is not an ELF file\n", filename); 282 exit(EXIT_FAILURE); 283 } 284 285 if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) { 286 fprintf(stderr, "can't read ELF header of \"%s\": %s\n", 287 filename, elf_errmsg(-1)); 288 exit(EXIT_FAILURE); 289 } 290 291 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) { 292 fprintf(stderr, "\"%s\" is neither an ELF executable" 293 " nor a shared library\n", filename); 294 exit(EXIT_FAILURE); 295 } 296 297 if (1 298#ifdef LT_ELF_MACHINE 299 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS 300 || lte->ehdr.e_machine != LT_ELF_MACHINE) 301#endif 302#ifdef LT_ELF_MACHINE2 303 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 304 || lte->ehdr.e_machine != LT_ELF_MACHINE2) 305#endif 306#ifdef LT_ELF_MACHINE3 307 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 308 || lte->ehdr.e_machine != LT_ELF_MACHINE3) 309#endif 310 ) { 311 fprintf(stderr, 312 "\"%s\" is ELF from incompatible architecture\n", 313 filename); 314 exit(EXIT_FAILURE); 315 } 316 317 return 0; 318} 319 320static void 321read_symbol_table(struct ltelf *lte, const char *filename, 322 Elf_Scn *scn, GElf_Shdr *shdr, const char *name, 323 Elf_Data **datap, size_t *countp, const char **strsp) 324{ 325 *datap = elf_getdata(scn, NULL); 326 *countp = shdr->sh_size / shdr->sh_entsize; 327 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL) 328 && options.static_filter != NULL) { 329 fprintf(stderr, "Couldn't get data of section" 330 " %s from \"%s\": %s\n", 331 name, filename, elf_errmsg(-1)); 332 exit(EXIT_FAILURE); 333 } 334 335 scn = elf_getscn(lte->elf, shdr->sh_link); 336 GElf_Shdr shdr2; 337 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) { 338 fprintf(stderr, "Couldn't get header of section" 339 " #%d from \"%s\": %s\n", 340 shdr->sh_link, filename, elf_errmsg(-1)); 341 exit(EXIT_FAILURE); 342 } 343 344 Elf_Data *data = elf_getdata(scn, NULL); 345 if (data == NULL || elf_getdata(scn, data) != NULL 346 || shdr2.sh_size != data->d_size || data->d_off) { 347 fprintf(stderr, "Couldn't get data of section" 348 " #%d from \"%s\": %s\n", 349 shdr2.sh_link, filename, elf_errmsg(-1)); 350 exit(EXIT_FAILURE); 351 } 352 353 *strsp = data->d_buf; 354} 355 356static int 357do_init_elf(struct ltelf *lte, const char *filename) 358{ 359 int i; 360 GElf_Addr relplt_addr = 0; 361 GElf_Addr soname_offset = 0; 362 363 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); 364 debug(1, "Reading ELF from %s...", filename); 365 366 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 367 Elf_Scn *scn; 368 GElf_Shdr shdr; 369 const char *name; 370 371 scn = elf_getscn(lte->elf, i); 372 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { 373 fprintf(stderr, "Couldn't get section #%d from" 374 " \"%s\": %s\n", i, filename, elf_errmsg(-1)); 375 exit(EXIT_FAILURE); 376 } 377 378 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); 379 if (name == NULL) { 380 fprintf(stderr, "Couldn't get name of section #%d from" 381 " \"%s\": %s\n", i, filename, elf_errmsg(-1)); 382 exit(EXIT_FAILURE); 383 } 384 385 if (shdr.sh_type == SHT_SYMTAB) { 386 read_symbol_table(lte, filename, 387 scn, &shdr, name, <e->symtab, 388 <e->symtab_count, <e->strtab); 389 390 } else if (shdr.sh_type == SHT_DYNSYM) { 391 read_symbol_table(lte, filename, 392 scn, &shdr, name, <e->dynsym, 393 <e->dynsym_count, <e->dynstr); 394 395 } else if (shdr.sh_type == SHT_DYNAMIC) { 396 Elf_Data *data; 397 size_t j; 398 399 lte->dyn_addr = shdr.sh_addr + lte->bias; 400 lte->dyn_sz = shdr.sh_size; 401 402 data = elf_getdata(scn, NULL); 403 if (data == NULL || elf_getdata(scn, data) != NULL) { 404 fprintf(stderr, "Couldn't get .dynamic data" 405 " from \"%s\": %s\n", 406 filename, strerror(errno)); 407 exit(EXIT_FAILURE); 408 } 409 410 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { 411 GElf_Dyn dyn; 412 413 if (gelf_getdyn(data, j, &dyn) == NULL) { 414 fprintf(stderr, "Couldn't get .dynamic" 415 " data from \"%s\": %s\n", 416 filename, strerror(errno)); 417 exit(EXIT_FAILURE); 418 } 419 if (dyn.d_tag == DT_JMPREL) 420 relplt_addr = dyn.d_un.d_ptr; 421 else if (dyn.d_tag == DT_PLTRELSZ) 422 lte->relplt_size = dyn.d_un.d_val; 423 else if (dyn.d_tag == DT_SONAME) 424 soname_offset = dyn.d_un.d_val; 425 } 426 } else if (shdr.sh_type == SHT_PROGBITS 427 || shdr.sh_type == SHT_NOBITS) { 428 if (strcmp(name, ".plt") == 0) { 429 lte->plt_addr = shdr.sh_addr; 430 lte->plt_size = shdr.sh_size; 431 lte->plt_data = elf_loaddata(scn, &shdr); 432 if (lte->plt_data == NULL) 433 fprintf(stderr, 434 "Can't load .plt data\n"); 435 lte->plt_flags = shdr.sh_flags; 436 } 437#ifdef ARCH_SUPPORTS_OPD 438 else if (strcmp(name, ".opd") == 0) { 439 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; 440 lte->opd_size = shdr.sh_size; 441 lte->opd = elf_rawdata(scn, NULL); 442 } 443#endif 444 } 445 } 446 447 if (lte->dynsym == NULL || lte->dynstr == NULL) { 448 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n", 449 filename); 450 exit(EXIT_FAILURE); 451 } 452 453 if (!relplt_addr || !lte->plt_addr) { 454 debug(1, "%s has no PLT relocations", filename); 455 lte->relplt = NULL; 456 lte->relplt_count = 0; 457 } else if (lte->relplt_size == 0) { 458 debug(1, "%s has unknown PLT size", filename); 459 lte->relplt = NULL; 460 lte->relplt_count = 0; 461 } else { 462 463 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 464 Elf_Scn *scn; 465 GElf_Shdr shdr; 466 467 scn = elf_getscn(lte->elf, i); 468 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { 469 fprintf(stderr, "Couldn't get section header" 470 " from \"%s\": %s\n", 471 filename, elf_errmsg(-1)); 472 exit(EXIT_FAILURE); 473 } 474 if (shdr.sh_addr == relplt_addr 475 && shdr.sh_size == lte->relplt_size) { 476 lte->relplt = elf_getdata(scn, NULL); 477 lte->relplt_count = 478 shdr.sh_size / shdr.sh_entsize; 479 if (lte->relplt == NULL 480 || elf_getdata(scn, lte->relplt) != NULL) { 481 fprintf(stderr, "Couldn't get .rel*.plt" 482 " data from \"%s\": %s\n", 483 filename, elf_errmsg(-1)); 484 exit(EXIT_FAILURE); 485 } 486 break; 487 } 488 } 489 490 if (i == lte->ehdr.e_shnum) { 491 fprintf(stderr, 492 "Couldn't find .rel*.plt section in \"%s\"\n", 493 filename); 494 exit(EXIT_FAILURE); 495 } 496 497 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); 498 } 499 500 if (soname_offset != 0) 501 lte->soname = lte->dynstr + soname_offset; 502 503 return 0; 504} 505 506void 507do_close_elf(struct ltelf *lte) 508{ 509 debug(DEBUG_FUNCTION, "do_close_elf()"); 510 arch_elf_destroy(lte); 511 elf_end(lte->elf); 512 close(lte->fd); 513} 514 515int 516elf_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 545#ifndef ARCH_HAVE_GET_SYMINFO 546int 547arch_get_sym_info(struct ltelf *lte, const char *filename, 548 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym) 549{ 550 return elf_get_sym_info(lte, filename, sym_index, rela, sym); 551} 552#endif 553 554static void 555mark_chain_latent(struct library_symbol *libsym) 556{ 557 for (; libsym != NULL; libsym = libsym->next) { 558 debug(DEBUG_FUNCTION, "marking %s latent", libsym->name); 559 libsym->latent = 1; 560 } 561} 562 563static int 564populate_plt(struct process *proc, const char *filename, 565 struct ltelf *lte, struct library *lib, 566 int latent_plts) 567{ 568 size_t i; 569 for (i = 0; i < lte->relplt_count; ++i) { 570 GElf_Rela rela; 571 GElf_Sym sym; 572 573 if (arch_get_sym_info(lte, filename, i, &rela, &sym) < 0) 574 continue; /* Skip this entry. */ 575 576 char const *name = lte->dynstr + sym.st_name; 577 578 /* If the symbol wasn't matched, reject it, unless we 579 * need to keep latent PLT breakpoints for tracing 580 * exports. */ 581 int matched = filter_matches_symbol(options.plt_filter, 582 name, lib); 583 if (!matched && !latent_plts) 584 continue; 585 586 struct library_symbol *libsym = NULL; 587 switch (arch_elf_add_plt_entry(proc, lte, name, 588 &rela, i, &libsym)) { 589 case PLT_DEFAULT: 590 if (default_elf_add_plt_entry(proc, lte, name, 591 &rela, i, &libsym) < 0) 592 /* fall-through */ 593 case PLT_FAIL: 594 return -1; 595 /* fall-through */ 596 case PLT_OK: 597 if (libsym != NULL) { 598 /* If we are adding those symbols just 599 * for tracing exports, mark them all 600 * latent. */ 601 if (!matched) 602 mark_chain_latent(libsym); 603 library_add_symbol(lib, libsym); 604 } 605 } 606 } 607 return 0; 608} 609 610/* When -x rules result in request to trace several aliases, we only 611 * want to add such symbol once. The only way that those symbols 612 * differ in is their name, e.g. in glibc you have __GI___libc_free, 613 * __cfree, __free, __libc_free, cfree and free all defined on the 614 * same address. So instead we keep this unique symbol struct for 615 * each address, and replace name in libsym with a shorter variant if 616 * we find it. */ 617struct unique_symbol { 618 arch_addr_t addr; 619 struct library_symbol *libsym; 620}; 621 622static int 623unique_symbol_cmp(const void *key, const void *val) 624{ 625 const struct unique_symbol *sym_key = key; 626 const struct unique_symbol *sym_val = val; 627 return sym_key->addr != sym_val->addr; 628} 629 630static enum callback_status 631symbol_with_address(struct library_symbol *sym, void *addrptr) 632{ 633 return sym->enter_addr == *(arch_addr_t *)addrptr 634 ? CBS_STOP : CBS_CONT; 635} 636 637static int 638populate_this_symtab(struct process *proc, const char *filename, 639 struct ltelf *lte, struct library *lib, 640 Elf_Data *symtab, const char *strtab, size_t size, 641 struct library_exported_name **names) 642{ 643 /* If a valid NAMES is passed, we pass in *NAMES a list of 644 * symbol names that this library exports. */ 645 if (names != NULL) 646 *names = NULL; 647 648 /* Using sorted array would be arguably better, but this 649 * should be well enough for the number of symbols that we 650 * typically deal with. */ 651 size_t num_symbols = 0; 652 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size); 653 if (symbols == NULL) { 654 fprintf(stderr, "couldn't insert symbols for -x: %s\n", 655 strerror(errno)); 656 return -1; 657 } 658 659 GElf_Word secflags[lte->ehdr.e_shnum]; 660 size_t i; 661 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 662 Elf_Scn *scn = elf_getscn(lte->elf, i); 663 GElf_Shdr shdr; 664 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) 665 secflags[i] = 0; 666 else 667 secflags[i] = shdr.sh_flags; 668 } 669 670 for (i = 0; i < size; ++i) { 671 GElf_Sym sym; 672 if (gelf_getsym(symtab, i, &sym) == NULL) { 673 fail: 674 fprintf(stderr, 675 "couldn't get symbol #%zd from %s: %s\n", 676 i, filename, elf_errmsg(-1)); 677 continue; 678 } 679 680 /* XXX support IFUNC as well. */ 681 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC 682 || sym.st_value == 0 683 || sym.st_shndx == STN_UNDEF) 684 continue; 685 686 /* Find symbol name and snip version. */ 687 const char *orig_name = strtab + sym.st_name; 688 const char *version = strchr(orig_name, '@'); 689 size_t len = version != NULL ? (assert(version > orig_name), 690 (size_t)(version - orig_name)) 691 : strlen(orig_name); 692 char name[len + 1]; 693 memcpy(name, orig_name, len); 694 name[len] = 0; 695 696 /* If we are interested in exports, store this name. */ 697 char *name_copy = NULL; 698 if (names != NULL) { 699 struct library_exported_name *export = NULL; 700 name_copy = strdup(name); 701 702 if (name_copy == NULL 703 || (export = malloc(sizeof(*export))) == NULL) { 704 free(name_copy); 705 fprintf(stderr, "Couldn't store symbol %s. " 706 "Tracing may be incomplete.\n", name); 707 } else { 708 export->name = name_copy; 709 export->own_name = 1; 710 export->next = *names; 711 *names = export; 712 } 713 } 714 715 /* If the symbol is not matched, skip it. We already 716 * stored it to export list above. */ 717 if (!filter_matches_symbol(options.static_filter, name, lib)) 718 continue; 719 720 arch_addr_t addr = (arch_addr_t) 721 (uintptr_t)(sym.st_value + lte->bias); 722 arch_addr_t naddr; 723 724 /* On arches that support OPD, the value of typical 725 * function symbol will be a pointer to .opd, but some 726 * will point directly to .text. We don't want to 727 * translate those. */ 728 if (secflags[sym.st_shndx] & SHF_EXECINSTR) { 729 naddr = addr; 730 } else if (arch_translate_address(lte, addr, &naddr) < 0) { 731 fprintf(stderr, 732 "couldn't translate address of %s@%s: %s\n", 733 name, lib->soname, strerror(errno)); 734 continue; 735 } 736 737 char *full_name; 738 int own_full_name = 1; 739 if (name_copy == NULL) { 740 full_name = strdup(name); 741 if (full_name == NULL) 742 goto fail; 743 } else { 744 full_name = name_copy; 745 own_full_name = 0; 746 } 747 748 /* Look whether we already have a symbol for this 749 * address. If not, add this one. */ 750 struct unique_symbol key = { naddr, NULL }; 751 struct unique_symbol *unique 752 = lsearch(&key, symbols, &num_symbols, 753 sizeof(*symbols), &unique_symbol_cmp); 754 755 if (unique->libsym == NULL) { 756 struct library_symbol *libsym = malloc(sizeof(*libsym)); 757 if (libsym == NULL 758 || library_symbol_init(libsym, naddr, 759 full_name, own_full_name, 760 LS_TOPLT_NONE) < 0) { 761 --num_symbols; 762 goto fail; 763 } 764 unique->libsym = libsym; 765 unique->addr = naddr; 766 767 } else if (strlen(full_name) < strlen(unique->libsym->name)) { 768 library_symbol_set_name(unique->libsym, 769 full_name, own_full_name); 770 771 } else if (own_full_name) { 772 free(full_name); 773 } 774 } 775 776 /* Now we do the union of this set of unique symbols with 777 * what's already in the library. */ 778 for (i = 0; i < num_symbols; ++i) { 779 struct library_symbol *this_sym = symbols[i].libsym; 780 assert(this_sym != NULL); 781 struct library_symbol *other 782 = library_each_symbol(lib, NULL, symbol_with_address, 783 &this_sym->enter_addr); 784 if (other != NULL) { 785 library_symbol_destroy(this_sym); 786 free(this_sym); 787 symbols[i].libsym = NULL; 788 } 789 } 790 791 for (i = 0; i < num_symbols; ++i) 792 if (symbols[i].libsym != NULL) 793 library_add_symbol(lib, symbols[i].libsym); 794 795 free(symbols); 796 return 0; 797} 798 799static int 800populate_symtab(struct process *proc, const char *filename, 801 struct ltelf *lte, struct library *lib, 802 int symtabs, int exports) 803{ 804 int status; 805 if (symtabs && lte->symtab != NULL && lte->strtab != NULL 806 && (status = populate_this_symtab(proc, filename, lte, lib, 807 lte->symtab, lte->strtab, 808 lte->symtab_count, NULL)) < 0) 809 return status; 810 811 /* Check whether we want to trace symbols implemented by this 812 * library (-l). */ 813 struct library_exported_name **names = NULL; 814 if (exports) { 815 debug(DEBUG_FUNCTION, "-l matches %s", lib->soname); 816 names = &lib->exported_names; 817 } 818 819 return populate_this_symtab(proc, filename, lte, lib, 820 lte->dynsym, lte->dynstr, 821 lte->dynsym_count, names); 822} 823 824static int 825read_module(struct library *lib, struct process *proc, 826 const char *filename, GElf_Addr bias, int main) 827{ 828 struct ltelf lte = {}; 829 if (open_elf(<e, filename) < 0) 830 return -1; 831 832 /* XXX When we abstract ABI into a module, this should instead 833 * become something like 834 * 835 * proc->abi = arch_get_abi(lte.ehdr); 836 * 837 * The code in open_elf needs to be replaced by this logic. 838 * Be warned that libltrace.c calls open_elf as well to 839 * determine whether ABI is supported. This is to get 840 * reasonable error messages when trying to run 64-bit binary 841 * with 32-bit ltrace. It is desirable to preserve this. */ 842 proc->e_machine = lte.ehdr.e_machine; 843 proc->e_class = lte.ehdr.e_ident[EI_CLASS]; 844 get_arch_dep(proc); 845 846 /* Find out the base address. For PIE main binaries we look 847 * into auxv, otherwise we scan phdrs. */ 848 if (main && lte.ehdr.e_type == ET_DYN) { 849 arch_addr_t entry; 850 if (process_get_entry(proc, &entry, NULL) < 0) { 851 fprintf(stderr, "Couldn't find entry of PIE %s\n", 852 filename); 853 return -1; 854 } 855 /* XXX The double cast should be removed when 856 * arch_addr_t becomes integral type. */ 857 lte.entry_addr = (GElf_Addr)(uintptr_t)entry; 858 lte.bias = (GElf_Addr)(uintptr_t)entry - lte.ehdr.e_entry; 859 860 } else { 861 GElf_Phdr phdr; 862 size_t i; 863 for (i = 0; gelf_getphdr (lte.elf, i, &phdr) != NULL; ++i) { 864 if (phdr.p_type == PT_LOAD) { 865 lte.base_addr = phdr.p_vaddr + bias; 866 break; 867 } 868 } 869 870 lte.bias = bias; 871 lte.entry_addr = lte.ehdr.e_entry + lte.bias; 872 873 if (lte.base_addr == 0) { 874 fprintf(stderr, 875 "Couldn't determine base address of %s\n", 876 filename); 877 return -1; 878 } 879 } 880 881 if (do_init_elf(<e, filename) < 0) 882 return -1; 883 884 if (arch_elf_init(<e, lib) < 0) { 885 fprintf(stderr, "Backend initialization failed.\n"); 886 return -1; 887 } 888 889 int status = 0; 890 if (lib == NULL) 891 goto fail; 892 893 /* Note that we set soname and pathname as soon as they are 894 * allocated, so in case of further errors, this get released 895 * when LIB is released, which should happen in the caller 896 * when we return error. */ 897 898 if (lib->pathname == NULL) { 899 char *pathname = strdup(filename); 900 if (pathname == NULL) 901 goto fail; 902 library_set_pathname(lib, pathname, 1); 903 } 904 905 if (lte.soname != NULL) { 906 char *soname = strdup(lte.soname); 907 if (soname == NULL) 908 goto fail; 909 library_set_soname(lib, soname, 1); 910 } else { 911 const char *soname = rindex(lib->pathname, '/'); 912 if (soname != NULL) 913 soname += 1; 914 else 915 soname = lib->pathname; 916 library_set_soname(lib, soname, 0); 917 } 918 919 /* XXX The double cast should be removed when 920 * arch_addr_t becomes integral type. */ 921 arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr; 922 if (arch_translate_address(<e, entry, &entry) < 0) 923 goto fail; 924 925 /* XXX The double cast should be removed when 926 * arch_addr_t becomes integral type. */ 927 lib->base = (arch_addr_t)(uintptr_t)lte.base_addr; 928 lib->entry = entry; 929 /* XXX The double cast should be removed when 930 * arch_addr_t becomes integral type. */ 931 lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr; 932 933 /* There are two reasons that we need to inspect symbol tables 934 * or populate PLT entries. Either the user requested 935 * corresponding tracing features (respectively -x and -e), or 936 * they requested tracing exported symbols (-l). 937 * 938 * In the latter case we need to keep even those PLT slots 939 * that are not requested by -e (but we keep them latent). We 940 * also need to inspect .dynsym to find what exports this 941 * library provide, to turn on existing latent PLT 942 * entries. */ 943 944 int plts = filter_matches_library(options.plt_filter, lib); 945 if ((plts || options.export_filter != NULL) 946 && populate_plt(proc, filename, <e, lib, 947 options.export_filter != NULL) < 0) 948 goto fail; 949 950 int exports = filter_matches_library(options.export_filter, lib); 951 int symtabs = filter_matches_library(options.static_filter, lib); 952 if ((symtabs || exports) 953 && populate_symtab(proc, filename, <e, lib, 954 symtabs, exports) < 0) 955 goto fail; 956 957done: 958 do_close_elf(<e); 959 return status; 960 961fail: 962 status = -1; 963 goto done; 964} 965 966int 967ltelf_read_library(struct library *lib, struct process *proc, 968 const char *filename, GElf_Addr bias) 969{ 970 return read_module(lib, proc, filename, bias, 0); 971} 972 973 974struct library * 975ltelf_read_main_binary(struct process *proc, const char *path) 976{ 977 struct library *lib = malloc(sizeof(*lib)); 978 if (lib == NULL) 979 return NULL; 980 library_init(lib, LT_LIBTYPE_MAIN); 981 library_set_pathname(lib, path, 0); 982 983 /* There is a race between running the process and reading its 984 * binary for internal consumption. So open the binary from 985 * the /proc filesystem. XXX Note that there is similar race 986 * for libraries, but there we don't have a nice answer like 987 * that. Presumably we could read the DSOs from the process 988 * memory image, but that's not currently done. */ 989 char *fname = pid2name(proc->pid); 990 if (fname == NULL 991 || read_module(lib, proc, fname, 0, 1) < 0) { 992 library_destroy(lib); 993 free(lib); 994 lib = NULL; 995 } 996 997 free(fname); 998 return lib; 999} 1000