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