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