ltrace-elf.c revision 8bda1ee2e92a3c211c01041f183b4e2bf2ebf9d2
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 *ret = libsym; 95 return 0; 96} 97 98#ifndef ARCH_HAVE_ADD_PLT_ENTRY 99enum plt_status 100arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte, 101 const char *a_name, GElf_Rela *rela, size_t ndx, 102 struct library_symbol **ret) 103{ 104 return plt_default; 105} 106#endif 107 108Elf_Data * 109elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr) 110{ 111 Elf_Data *data = elf_getdata(scn, NULL); 112 if (data == NULL || elf_getdata(scn, data) != NULL 113 || data->d_off || data->d_size != shdr->sh_size) 114 return NULL; 115 return data; 116} 117 118static int 119elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr, 120 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data), 121 void *data) 122{ 123 int i; 124 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 125 Elf_Scn *scn; 126 GElf_Shdr shdr; 127 128 scn = elf_getscn(lte->elf, i); 129 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { 130 debug(1, "Couldn't read section or header."); 131 return -1; 132 } 133 if (predicate(scn, &shdr, data)) { 134 *tgt_sec = scn; 135 *tgt_shdr = shdr; 136 return 0; 137 } 138 } 139 return -1; 140 141} 142 143static int 144inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data) 145{ 146 GElf_Addr addr = *(GElf_Addr *)data; 147 return addr >= shdr->sh_addr 148 && addr < shdr->sh_addr + shdr->sh_size; 149} 150 151int 152elf_get_section_covering(struct ltelf *lte, GElf_Addr addr, 153 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) 154{ 155 return elf_get_section_if(lte, tgt_sec, tgt_shdr, 156 &inside_p, &addr); 157} 158 159static int 160type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data) 161{ 162 GElf_Word type = *(GElf_Word *)data; 163 return shdr->sh_type == type; 164} 165 166int 167elf_get_section_type(struct ltelf *lte, GElf_Word type, 168 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) 169{ 170 return elf_get_section_if(lte, tgt_sec, tgt_shdr, 171 &type_p, &type); 172} 173 174struct section_named_data { 175 struct ltelf *lte; 176 const char *name; 177}; 178 179static int 180name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d) 181{ 182 struct section_named_data *data = d; 183 const char *name = elf_strptr(data->lte->elf, 184 data->lte->ehdr.e_shstrndx, 185 shdr->sh_name); 186 return strcmp(name, data->name) == 0; 187} 188 189int 190elf_get_section_named(struct ltelf *lte, const char *name, 191 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) 192{ 193 struct section_named_data data = { 194 .lte = lte, 195 .name = name, 196 }; 197 return elf_get_section_if(lte, tgt_sec, tgt_shdr, 198 &name_p, &data); 199} 200 201static int 202need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size) 203{ 204 assert(data != NULL); 205 if (data->d_size < size || offset > data->d_size - size) { 206 debug(1, "Not enough data to read %"PRId64"-byte value" 207 " at offset %"PRId64".", size, offset); 208 return -1; 209 } 210 return 0; 211} 212 213#define DEF_READER(NAME, SIZE) \ 214 int \ 215 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \ 216 { \ 217 if (!need_data(data, offset, SIZE / 8) < 0) \ 218 return -1; \ 219 \ 220 if (data->d_buf == NULL) /* NODATA section */ { \ 221 *retp = 0; \ 222 return 0; \ 223 } \ 224 \ 225 union { \ 226 uint##SIZE##_t dst; \ 227 char buf[0]; \ 228 } u; \ 229 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \ 230 *retp = u.dst; \ 231 return 0; \ 232 } 233 234DEF_READER(elf_read_u16, 16) 235DEF_READER(elf_read_u32, 32) 236DEF_READER(elf_read_u64, 64) 237 238#undef DEF_READER 239 240int 241open_elf(struct ltelf *lte, const char *filename) 242{ 243 lte->fd = open(filename, O_RDONLY); 244 if (lte->fd == -1) 245 return 1; 246 247 elf_version(EV_CURRENT); 248 249#ifdef HAVE_ELF_C_READ_MMAP 250 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); 251#else 252 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); 253#endif 254 255 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) { 256 fprintf(stderr, "\"%s\" is not an ELF file\n", filename); 257 exit(EXIT_FAILURE); 258 } 259 260 if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) { 261 fprintf(stderr, "can't read ELF header of \"%s\": %s\n", 262 filename, elf_errmsg(-1)); 263 exit(EXIT_FAILURE); 264 } 265 266 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) { 267 fprintf(stderr, "\"%s\" is neither an ELF executable" 268 " nor a shared library\n", filename); 269 exit(EXIT_FAILURE); 270 } 271 272 if (1 273#ifdef LT_ELF_MACHINE 274 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS 275 || lte->ehdr.e_machine != LT_ELF_MACHINE) 276#endif 277#ifdef LT_ELF_MACHINE2 278 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 279 || lte->ehdr.e_machine != LT_ELF_MACHINE2) 280#endif 281#ifdef LT_ELF_MACHINE3 282 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 283 || lte->ehdr.e_machine != LT_ELF_MACHINE3) 284#endif 285 ) { 286 fprintf(stderr, 287 "\"%s\" is ELF from incompatible architecture\n", 288 filename); 289 exit(EXIT_FAILURE); 290 } 291 292 return 0; 293} 294 295static void 296read_symbol_table(struct ltelf *lte, const char *filename, 297 Elf_Scn *scn, GElf_Shdr *shdr, const char *name, 298 Elf_Data **datap, size_t *countp, const char **strsp) 299{ 300 *datap = elf_getdata(scn, NULL); 301 *countp = shdr->sh_size / shdr->sh_entsize; 302 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL) 303 && options.static_filter != NULL) { 304 fprintf(stderr, "Couldn't get data of section" 305 " %s from \"%s\": %s\n", 306 name, filename, elf_errmsg(-1)); 307 exit(EXIT_FAILURE); 308 } 309 310 scn = elf_getscn(lte->elf, shdr->sh_link); 311 GElf_Shdr shdr2; 312 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) { 313 fprintf(stderr, "Couldn't get header of section" 314 " #%d from \"%s\": %s\n", 315 shdr2.sh_link, filename, elf_errmsg(-1)); 316 exit(EXIT_FAILURE); 317 } 318 319 Elf_Data *data = elf_getdata(scn, NULL); 320 if (data == NULL || elf_getdata(scn, data) != NULL 321 || shdr2.sh_size != data->d_size || data->d_off) { 322 fprintf(stderr, "Couldn't get data of section" 323 " #%d from \"%s\": %s\n", 324 shdr2.sh_link, filename, elf_errmsg(-1)); 325 exit(EXIT_FAILURE); 326 } 327 328 *strsp = data->d_buf; 329} 330 331static int 332do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias) 333{ 334 int i; 335 GElf_Addr relplt_addr = 0; 336 GElf_Addr soname_offset = 0; 337 338 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); 339 debug(1, "Reading ELF from %s...", filename); 340 341 if (open_elf(lte, filename) < 0) 342 return -1; 343 344 /* Find out the base address. */ 345 { 346 GElf_Phdr phdr; 347 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) { 348 if (phdr.p_type == PT_LOAD) { 349 lte->base_addr = phdr.p_vaddr + bias; 350 break; 351 } 352 } 353 } 354 355 if (lte->base_addr == 0) { 356 fprintf(stderr, "Couldn't determine base address of %s\n", 357 filename); 358 return -1; 359 } 360 361 lte->bias = bias; 362 lte->entry_addr = lte->ehdr.e_entry + lte->bias; 363 364 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 365 Elf_Scn *scn; 366 GElf_Shdr shdr; 367 const char *name; 368 369 scn = elf_getscn(lte->elf, i); 370 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { 371 fprintf(stderr, "Couldn't get section #%d from" 372 " \"%s\": %s\n", i, filename, elf_errmsg(-1)); 373 exit(EXIT_FAILURE); 374 } 375 376 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); 377 if (name == NULL) { 378 fprintf(stderr, "Couldn't get name of section #%d from" 379 " \"%s\": %s\n", i, filename, elf_errmsg(-1)); 380 exit(EXIT_FAILURE); 381 } 382 383 if (shdr.sh_type == SHT_SYMTAB) { 384 read_symbol_table(lte, filename, 385 scn, &shdr, name, <e->symtab, 386 <e->symtab_count, <e->strtab); 387 388 } else if (shdr.sh_type == SHT_DYNSYM) { 389 read_symbol_table(lte, filename, 390 scn, &shdr, name, <e->dynsym, 391 <e->dynsym_count, <e->dynstr); 392 393 } else if (shdr.sh_type == SHT_DYNAMIC) { 394 Elf_Data *data; 395 size_t j; 396 397 lte->dyn_addr = shdr.sh_addr; 398 lte->dyn_sz = shdr.sh_size; 399 400 data = elf_getdata(scn, NULL); 401 if (data == NULL || elf_getdata(scn, data) != NULL) { 402 fprintf(stderr, "Couldn't get .dynamic data" 403 " from \"%s\": %s\n", 404 filename, strerror(errno)); 405 exit(EXIT_FAILURE); 406 } 407 408 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { 409 GElf_Dyn dyn; 410 411 if (gelf_getdyn(data, j, &dyn) == NULL) { 412 fprintf(stderr, "Couldn't get .dynamic" 413 " data from \"%s\": %s\n", 414 filename, strerror(errno)); 415 exit(EXIT_FAILURE); 416 } 417 if (dyn.d_tag == DT_JMPREL) 418 relplt_addr = dyn.d_un.d_ptr; 419 else if (dyn.d_tag == DT_PLTRELSZ) 420 lte->relplt_size = dyn.d_un.d_val; 421 else if (dyn.d_tag == DT_SONAME) 422 soname_offset = dyn.d_un.d_val; 423 } 424 } else if (shdr.sh_type == SHT_PROGBITS 425 || shdr.sh_type == SHT_NOBITS) { 426 if (strcmp(name, ".plt") == 0) { 427 lte->plt_addr = shdr.sh_addr; 428 lte->plt_size = shdr.sh_size; 429 lte->plt_data = elf_loaddata(scn, &shdr); 430 if (lte->plt_data == NULL) 431 fprintf(stderr, 432 "Can't load .plt data\n"); 433 lte->plt_flags = shdr.sh_flags; 434 } 435#ifdef ARCH_SUPPORTS_OPD 436 else if (strcmp(name, ".opd") == 0) { 437 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; 438 lte->opd_size = shdr.sh_size; 439 lte->opd = elf_rawdata(scn, NULL); 440 } 441#endif 442 } 443 } 444 445 if (lte->dynsym == NULL || lte->dynstr == NULL) { 446 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n", 447 filename); 448 exit(EXIT_FAILURE); 449 } 450 451 if (!relplt_addr || !lte->plt_addr) { 452 debug(1, "%s has no PLT relocations", filename); 453 lte->relplt = NULL; 454 lte->relplt_count = 0; 455 } else if (lte->relplt_size == 0) { 456 debug(1, "%s has unknown PLT size", filename); 457 lte->relplt = NULL; 458 lte->relplt_count = 0; 459 } else { 460 461 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 462 Elf_Scn *scn; 463 GElf_Shdr shdr; 464 465 scn = elf_getscn(lte->elf, i); 466 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { 467 fprintf(stderr, "Couldn't get section header" 468 " from \"%s\": %s\n", 469 filename, elf_errmsg(-1)); 470 exit(EXIT_FAILURE); 471 } 472 if (shdr.sh_addr == relplt_addr 473 && shdr.sh_size == lte->relplt_size) { 474 lte->relplt = elf_getdata(scn, NULL); 475 lte->relplt_count = 476 shdr.sh_size / shdr.sh_entsize; 477 if (lte->relplt == NULL 478 || elf_getdata(scn, lte->relplt) != NULL) { 479 fprintf(stderr, "Couldn't get .rel*.plt" 480 " data from \"%s\": %s\n", 481 filename, elf_errmsg(-1)); 482 exit(EXIT_FAILURE); 483 } 484 break; 485 } 486 } 487 488 if (i == lte->ehdr.e_shnum) { 489 fprintf(stderr, 490 "Couldn't find .rel*.plt section in \"%s\"\n", 491 filename); 492 exit(EXIT_FAILURE); 493 } 494 495 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); 496 } 497 498 if (soname_offset != 0) 499 lte->soname = lte->dynstr + soname_offset; 500 501 return 0; 502} 503 504/* XXX temporarily non-static */ 505void 506do_close_elf(struct ltelf *lte) { 507 debug(DEBUG_FUNCTION, "do_close_elf()"); 508 arch_elf_destroy(lte); 509 elf_end(lte->elf); 510 close(lte->fd); 511} 512 513#ifndef ARCH_HAVE_GET_SYMINFO 514int 515arch_get_sym_info(struct ltelf *lte, const char *filename, 516 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym) 517{ 518 int i = sym_index; 519 GElf_Rel rel; 520 void *ret; 521 522 if (lte->relplt->d_type == ELF_T_REL) { 523 ret = gelf_getrel(lte->relplt, i, &rel); 524 rela->r_offset = rel.r_offset; 525 rela->r_info = rel.r_info; 526 rela->r_addend = 0; 527 } else { 528 ret = gelf_getrela(lte->relplt, i, rela); 529 } 530 531 if (ret == NULL 532 || ELF64_R_SYM(rela->r_info) >= lte->dynsym_count 533 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela->r_info), 534 sym) == NULL) { 535 fprintf(stderr, 536 "Couldn't get relocation from \"%s\": %s\n", 537 filename, elf_errmsg(-1)); 538 exit(EXIT_FAILURE); 539 } 540 541 return 0; 542} 543#endif 544 545static int 546populate_plt(struct Process *proc, const char *filename, 547 struct ltelf *lte, struct library *lib) 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 (!filter_matches_symbol(options.plt_filter, name, lib)) 560 continue; 561 562 struct library_symbol *libsym = NULL; 563 switch (arch_elf_add_plt_entry(proc, lte, name, 564 &rela, i, &libsym)) { 565 case plt_default: 566 if (default_elf_add_plt_entry(proc, lte, name, 567 &rela, i, &libsym) < 0) 568 /* fall-through */ 569 case plt_fail: 570 return -1; 571 /* fall-through */ 572 case plt_ok: 573 if (libsym != NULL) 574 library_add_symbol(lib, libsym); 575 } 576 } 577 return 0; 578} 579 580/* When -x rules result in request to trace several aliases, we only 581 * want to add such symbol once. The only way that those symbols 582 * differ in is their name, e.g. in glibc you have __GI___libc_free, 583 * __cfree, __free, __libc_free, cfree and free all defined on the 584 * same address. So instead we keep this unique symbol struct for 585 * each address, and replace name in libsym with a shorter variant if 586 * we find it. */ 587struct unique_symbol { 588 arch_addr_t addr; 589 struct library_symbol *libsym; 590}; 591 592static int 593unique_symbol_cmp(const void *key, const void *val) 594{ 595 const struct unique_symbol *sym_key = key; 596 const struct unique_symbol *sym_val = val; 597 return sym_key->addr != sym_val->addr; 598} 599 600static int 601populate_this_symtab(struct Process *proc, const char *filename, 602 struct ltelf *lte, struct library *lib, 603 Elf_Data *symtab, const char *strtab, size_t size) 604{ 605 /* Using sorted array would be arguably better, but this 606 * should be well enough for the number of symbols that we 607 * typically deal with. */ 608 size_t num_symbols = 0; 609 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size); 610 if (symbols == NULL) { 611 fprintf(stderr, "couldn't insert symbols for -x: %s\n", 612 strerror(errno)); 613 return -1; 614 } 615 616 GElf_Word secflags[lte->ehdr.e_shnum]; 617 size_t i; 618 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 619 Elf_Scn *scn = elf_getscn(lte->elf, i); 620 if (scn == NULL) 621 continue; 622 GElf_Shdr shdr; 623 if (gelf_getshdr(scn, &shdr) == NULL) 624 continue; 625 secflags[i] = shdr.sh_flags; 626 } 627 628 size_t lib_len = strlen(lib->soname); 629 for (i = 0; i < size; ++i) { 630 GElf_Sym sym; 631 if (gelf_getsym(symtab, i, &sym) == NULL) { 632 fail: 633 fprintf(stderr, 634 "couldn't get symbol #%zd from %s: %s\n", 635 i, filename, elf_errmsg(-1)); 636 continue; 637 } 638 639 /* XXX support IFUNC as well. */ 640 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC 641 || sym.st_value == 0 642 || sym.st_shndx == STN_UNDEF) 643 continue; 644 645 const char *orig_name = strtab + sym.st_name; 646 const char *version = strchr(orig_name, '@'); 647 size_t len = version != NULL ? (assert(version > orig_name), 648 (size_t)(version - orig_name)) 649 : strlen(orig_name); 650 char name[len + 1]; 651 memcpy(name, orig_name, len); 652 name[len] = 0; 653 654 if (!filter_matches_symbol(options.static_filter, name, lib)) 655 continue; 656 657 arch_addr_t addr = (arch_addr_t) 658 (uintptr_t)(sym.st_value + lte->bias); 659 arch_addr_t naddr; 660 661 /* On arches that support OPD, the value of typical 662 * function symbol will be a pointer to .opd, but some 663 * will point directly to .text. We don't want to 664 * translate those. */ 665 if (secflags[sym.st_shndx] & SHF_EXECINSTR) { 666 naddr = addr; 667 } else if (arch_translate_address(lte, addr, &naddr) < 0) { 668 fprintf(stderr, 669 "couldn't translate address of %s@%s: %s\n", 670 name, lib->soname, strerror(errno)); 671 continue; 672 } 673 674 char *full_name; 675 if (lib->type != LT_LIBTYPE_MAIN) { 676 full_name = malloc(strlen(name) + 1 + lib_len + 1); 677 if (full_name == NULL) 678 goto fail; 679 sprintf(full_name, "%s@%s", name, lib->soname); 680 } else { 681 full_name = strdup(name); 682 if (full_name == NULL) 683 goto fail; 684 } 685 686 /* Look whether we already have a symbol for this 687 * address. If not, add this one. */ 688 struct unique_symbol key = { naddr, NULL }; 689 struct unique_symbol *unique 690 = lsearch(&key, symbols, &num_symbols, 691 sizeof(*symbols), &unique_symbol_cmp); 692 693 if (unique->libsym == NULL) { 694 struct library_symbol *libsym = malloc(sizeof(*libsym)); 695 if (libsym == NULL 696 || library_symbol_init(libsym, naddr, full_name, 697 1, LS_TOPLT_NONE) < 0) { 698 --num_symbols; 699 goto fail; 700 } 701 unique->libsym = libsym; 702 unique->addr = naddr; 703 704 } else if (strlen(full_name) < strlen(unique->libsym->name)) { 705 library_symbol_set_name(unique->libsym, full_name, 1); 706 707 } else { 708 free(full_name); 709 } 710 } 711 712 for (i = 0; i < num_symbols; ++i) { 713 assert(symbols[i].libsym != NULL); 714 library_add_symbol(lib, symbols[i].libsym); 715 } 716 717 free(symbols); 718 719 return 0; 720} 721 722static int 723populate_symtab(struct Process *proc, const char *filename, 724 struct ltelf *lte, struct library *lib) 725{ 726 if (lte->symtab != NULL && lte->strtab != NULL) 727 return populate_this_symtab(proc, filename, lte, lib, 728 lte->symtab, lte->strtab, 729 lte->symtab_count); 730 else 731 return populate_this_symtab(proc, filename, lte, lib, 732 lte->dynsym, lte->dynstr, 733 lte->dynsym_count); 734} 735 736int 737ltelf_read_library(struct library *lib, struct Process *proc, 738 const char *filename, GElf_Addr bias) 739{ 740 struct ltelf lte = {}; 741 if (do_init_elf(<e, filename, bias) < 0) 742 return -1; 743 if (arch_elf_init(<e, lib) < 0) { 744 fprintf(stderr, "Backend initialization failed.\n"); 745 return -1; 746 } 747 748 proc->e_machine = lte.ehdr.e_machine; 749 proc->e_class = lte.ehdr.e_ident[EI_CLASS]; 750 751 int status = 0; 752 if (lib == NULL) 753 goto fail; 754 755 /* Note that we set soname and pathname as soon as they are 756 * allocated, so in case of further errors, this get released 757 * when LIB is release, which should happen in the caller when 758 * we return error. */ 759 760 if (lib->pathname == NULL) { 761 char *pathname = strdup(filename); 762 if (pathname == NULL) 763 goto fail; 764 library_set_pathname(lib, pathname, 1); 765 } 766 767 if (lte.soname != NULL) { 768 char *soname = strdup(lte.soname); 769 if (soname == NULL) 770 goto fail; 771 library_set_soname(lib, soname, 1); 772 } else { 773 const char *soname = rindex(lib->pathname, '/') + 1; 774 if (soname == NULL) 775 soname = lib->pathname; 776 library_set_soname(lib, soname, 0); 777 } 778 779 /* XXX The double cast should be removed when 780 * arch_addr_t becomes integral type. */ 781 arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr; 782 if (arch_translate_address(<e, entry, &entry) < 0) 783 goto fail; 784 785 /* XXX The double cast should be removed when 786 * arch_addr_t becomes integral type. */ 787 lib->base = (arch_addr_t)(uintptr_t)lte.base_addr; 788 lib->entry = entry; 789 /* XXX The double cast should be removed when 790 * arch_addr_t becomes integral type. */ 791 lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr; 792 793 if (filter_matches_library(options.plt_filter, lib) 794 && populate_plt(proc, filename, <e, lib) < 0) 795 goto fail; 796 797 if (filter_matches_library(options.static_filter, lib) 798 && populate_symtab(proc, filename, <e, lib) < 0) 799 goto fail; 800 801done: 802 do_close_elf(<e); 803 return status; 804 805fail: 806 status = -1; 807 goto done; 808} 809 810struct library * 811ltelf_read_main_binary(struct Process *proc, const char *path) 812{ 813 struct library *lib = malloc(sizeof(*lib)); 814 if (lib == NULL) 815 return NULL; 816 library_init(lib, LT_LIBTYPE_MAIN); 817 library_set_pathname(lib, path, 0); 818 819 /* There is a race between running the process and reading its 820 * binary for internal consumption. So open the binary from 821 * the /proc filesystem. XXX Note that there is similar race 822 * for libraries, but there we don't have a nice answer like 823 * that. Presumably we could read the DSOs from the process 824 * memory image, but that's not currently done. */ 825 char *fname = pid2name(proc->pid); 826 if (ltelf_read_library(lib, proc, fname, 0) < 0) { 827 library_destroy(lib); 828 free(lib); 829 return NULL; 830 } 831 832 return lib; 833} 834