ltrace-elf.c revision 8eb0d9349614219cec5c3bb164f1376d83988525
1#include "config.h" 2 3#include <assert.h> 4#include <endian.h> 5#include <errno.h> 6#include <error.h> 7#include <fcntl.h> 8#include <gelf.h> 9#include <inttypes.h> 10#include <search.h> 11#include <stdint.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, size_t offset, size_t 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, size_t 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 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename); 202 203 if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) 204 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"", 205 filename); 206 207 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) 208 error(EXIT_FAILURE, 0, 209 "\"%s\" is not an ELF executable nor shared library", 210 filename); 211 212 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS 213 || lte->ehdr.e_machine != LT_ELF_MACHINE) 214#ifdef LT_ELF_MACHINE2 215 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 216 || lte->ehdr.e_machine != LT_ELF_MACHINE2) 217#endif 218#ifdef LT_ELF_MACHINE3 219 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 220 || lte->ehdr.e_machine != LT_ELF_MACHINE3) 221#endif 222 ) 223 error(EXIT_FAILURE, 0, 224 "\"%s\" is ELF from incompatible architecture", filename); 225 226 return 0; 227} 228 229static int 230do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias) 231{ 232 int i; 233 GElf_Addr relplt_addr = 0; 234 GElf_Addr soname_offset = 0; 235 236 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); 237 debug(1, "Reading ELF from %s...", filename); 238 239 if (open_elf(lte, filename) < 0) 240 return -1; 241 242 /* Find out the base address. */ 243 { 244 GElf_Phdr phdr; 245 for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) { 246 if (phdr.p_type == PT_LOAD) { 247 lte->base_addr = phdr.p_vaddr + bias; 248 break; 249 } 250 } 251 } 252 253 if (lte->base_addr == 0) { 254 fprintf(stderr, "Couldn't determine base address of %s\n", 255 filename); 256 return -1; 257 } 258 259 lte->bias = bias; 260 lte->entry_addr = lte->ehdr.e_entry + lte->bias; 261 262 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 263 Elf_Scn *scn; 264 GElf_Shdr shdr; 265 const char *name; 266 267 scn = elf_getscn(lte->elf, i); 268 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) 269 error(EXIT_FAILURE, 0, 270 "Couldn't get section header from \"%s\"", 271 filename); 272 273 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); 274 if (name == NULL) 275 error(EXIT_FAILURE, 0, 276 "Couldn't get section header from \"%s\"", 277 filename); 278 279 if (shdr.sh_type == SHT_SYMTAB) { 280 Elf_Data *data; 281 282 lte->symtab = elf_getdata(scn, NULL); 283 lte->symtab_count = shdr.sh_size / shdr.sh_entsize; 284 if ((lte->symtab == NULL 285 || elf_getdata(scn, lte->symtab) != NULL) 286 && options.static_filter != NULL) 287 error(EXIT_FAILURE, 0, 288 "Couldn't get .symtab data from \"%s\"", 289 filename); 290 291 scn = elf_getscn(lte->elf, shdr.sh_link); 292 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) 293 error(EXIT_FAILURE, 0, 294 "Couldn't get section header from \"%s\"", 295 filename); 296 297 data = elf_getdata(scn, NULL); 298 if (data == NULL || elf_getdata(scn, data) != NULL 299 || shdr.sh_size != data->d_size || data->d_off) 300 error(EXIT_FAILURE, 0, 301 "Couldn't get .strtab data from \"%s\"", 302 filename); 303 304 lte->strtab = data->d_buf; 305 } else if (shdr.sh_type == SHT_DYNSYM) { 306 Elf_Data *data; 307 308 lte->dynsym = elf_getdata(scn, NULL); 309 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize; 310 if (lte->dynsym == NULL 311 || elf_getdata(scn, lte->dynsym) != NULL) 312 error(EXIT_FAILURE, 0, 313 "Couldn't get .dynsym data from \"%s\"", 314 filename); 315 316 scn = elf_getscn(lte->elf, shdr.sh_link); 317 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) 318 error(EXIT_FAILURE, 0, 319 "Couldn't get section header from \"%s\"", 320 filename); 321 322 data = elf_getdata(scn, NULL); 323 if (data == NULL || elf_getdata(scn, data) != NULL 324 || shdr.sh_size != data->d_size || data->d_off) 325 error(EXIT_FAILURE, 0, 326 "Couldn't get .dynstr data from \"%s\"", 327 filename); 328 329 lte->dynstr = data->d_buf; 330 } else if (shdr.sh_type == SHT_DYNAMIC) { 331 Elf_Data *data; 332 size_t j; 333 334 lte->dyn_addr = shdr.sh_addr; 335 lte->dyn_sz = shdr.sh_size; 336 337 data = elf_getdata(scn, NULL); 338 if (data == NULL || elf_getdata(scn, data) != NULL) 339 error(EXIT_FAILURE, 0, 340 "Couldn't get .dynamic data from \"%s\"", 341 filename); 342 343 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { 344 GElf_Dyn dyn; 345 346 if (gelf_getdyn(data, j, &dyn) == NULL) 347 error(EXIT_FAILURE, 0, 348 "Couldn't get .dynamic data from \"%s\"", 349 filename); 350 if (dyn.d_tag == DT_JMPREL) 351 relplt_addr = dyn.d_un.d_ptr; 352 else if (dyn.d_tag == DT_PLTRELSZ) 353 lte->relplt_size = dyn.d_un.d_val; 354 else if (dyn.d_tag == DT_SONAME) 355 soname_offset = dyn.d_un.d_val; 356 } 357 } else if (shdr.sh_type == SHT_PROGBITS 358 || shdr.sh_type == SHT_NOBITS) { 359 if (strcmp(name, ".plt") == 0) { 360 lte->plt_addr = shdr.sh_addr; 361 lte->plt_size = shdr.sh_size; 362 lte->plt_data = elf_loaddata(scn, &shdr); 363 if (lte->plt_data == NULL) 364 fprintf(stderr, 365 "Can't load .plt data\n"); 366 lte->plt_flags = shdr.sh_flags; 367 } 368#ifdef ARCH_SUPPORTS_OPD 369 else if (strcmp(name, ".opd") == 0) { 370 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; 371 lte->opd_size = shdr.sh_size; 372 lte->opd = elf_rawdata(scn, NULL); 373 } 374#endif 375 } 376 } 377 378 if (lte->dynsym == NULL || lte->dynstr == NULL) 379 error(EXIT_FAILURE, 0, 380 "Couldn't find .dynsym or .dynstr in \"%s\"", filename); 381 382 if (!relplt_addr || !lte->plt_addr) { 383 debug(1, "%s has no PLT relocations", filename); 384 lte->relplt = NULL; 385 lte->relplt_count = 0; 386 } else if (lte->relplt_size == 0) { 387 debug(1, "%s has unknown PLT size", filename); 388 lte->relplt = NULL; 389 lte->relplt_count = 0; 390 } else { 391 392 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 393 Elf_Scn *scn; 394 GElf_Shdr shdr; 395 396 scn = elf_getscn(lte->elf, i); 397 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) 398 error(EXIT_FAILURE, 0, 399 "Couldn't get section header from \"%s\"", 400 filename); 401 if (shdr.sh_addr == relplt_addr 402 && shdr.sh_size == lte->relplt_size) { 403 lte->relplt = elf_getdata(scn, NULL); 404 lte->relplt_count = 405 shdr.sh_size / shdr.sh_entsize; 406 if (lte->relplt == NULL 407 || elf_getdata(scn, lte->relplt) != NULL) 408 error(EXIT_FAILURE, 0, 409 "Couldn't get .rel*.plt data from \"%s\"", 410 filename); 411 break; 412 } 413 } 414 415 if (i == lte->ehdr.e_shnum) 416 error(EXIT_FAILURE, 0, 417 "Couldn't find .rel*.plt section in \"%s\"", 418 filename); 419 420 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); 421 } 422 423 if (soname_offset != 0) 424 lte->soname = lte->dynstr + soname_offset; 425 426 return 0; 427} 428 429/* XXX temporarily non-static */ 430void 431do_close_elf(struct ltelf *lte) { 432 debug(DEBUG_FUNCTION, "do_close_elf()"); 433 arch_elf_destroy(lte); 434 elf_end(lte->elf); 435 close(lte->fd); 436} 437 438static int 439populate_plt(struct Process *proc, const char *filename, 440 struct ltelf *lte, struct library *lib) 441{ 442 size_t i; 443 for (i = 0; i < lte->relplt_count; ++i) { 444 GElf_Rel rel; 445 GElf_Rela rela; 446 GElf_Sym sym; 447 void *ret; 448 449 if (lte->relplt->d_type == ELF_T_REL) { 450 ret = gelf_getrel(lte->relplt, i, &rel); 451 rela.r_offset = rel.r_offset; 452 rela.r_info = rel.r_info; 453 rela.r_addend = 0; 454 } else { 455 ret = gelf_getrela(lte->relplt, i, &rela); 456 } 457 458 if (ret == NULL 459 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count 460 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info), 461 &sym) == NULL) 462 error(EXIT_FAILURE, 0, 463 "Couldn't get relocation from \"%s\"", 464 filename); 465 466 char const *name = lte->dynstr + sym.st_name; 467 468 if (!filter_matches_symbol(options.plt_filter, name, lib)) 469 continue; 470 471 struct library_symbol *libsym = NULL; 472 switch (arch_elf_add_plt_entry(proc, lte, name, 473 &rela, i, &libsym)) { 474 case plt_default: 475 if (default_elf_add_plt_entry(proc, lte, name, 476 &rela, i, &libsym) < 0) 477 /* fall-through */ 478 case plt_fail: 479 return -1; 480 /* fall-through */ 481 case plt_ok: 482 if (libsym != NULL) 483 library_add_symbol(lib, libsym); 484 } 485 } 486 return 0; 487} 488 489/* When -x rules result in request to trace several aliases, we only 490 * want to add such symbol once. The only way that those symbols 491 * differ in is their name, e.g. in glibc you have __GI___libc_free, 492 * __cfree, __free, __libc_free, cfree and free all defined on the 493 * same address. So instead we keep this unique symbol struct for 494 * each address, and replace name in libsym with a shorter variant if 495 * we find it. */ 496struct unique_symbol { 497 target_address_t addr; 498 struct library_symbol *libsym; 499}; 500 501static int 502unique_symbol_cmp(const void *key, const void *val) 503{ 504 const struct unique_symbol *sym_key = key; 505 const struct unique_symbol *sym_val = val; 506 return sym_key->addr != sym_val->addr; 507} 508 509static int 510populate_this_symtab(struct Process *proc, const char *filename, 511 struct ltelf *lte, struct library *lib, 512 Elf_Data *symtab, const char *strtab, size_t size) 513{ 514 /* Using sorted array would be arguably better, but this 515 * should be well enough for the number of symbols that we 516 * typically deal with. */ 517 size_t num_symbols = 0; 518 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size); 519 if (symbols == NULL) { 520 error(0, errno, "couldn't insert symbols for -x"); 521 return -1; 522 } 523 524 GElf_Word secflags[lte->ehdr.e_shnum]; 525 size_t i; 526 for (i = 1; i < lte->ehdr.e_shnum; ++i) { 527 Elf_Scn *scn = elf_getscn(lte->elf, i); 528 if (scn == NULL) 529 continue; 530 GElf_Shdr shdr; 531 if (gelf_getshdr(scn, &shdr) == NULL) 532 continue; 533 secflags[i] = shdr.sh_flags; 534 } 535 536 size_t lib_len = strlen(lib->soname); 537 for (i = 0; i < size; ++i) { 538 GElf_Sym sym; 539 if (gelf_getsym(symtab, i, &sym) == NULL) { 540 fail: 541 error(0, errno, "couldn't get symbol #%zd from %s: %s", 542 i, filename, elf_errmsg(-1)); 543 continue; 544 } 545 546 /* XXX support IFUNC as well. */ 547 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC 548 || sym.st_value == 0) 549 continue; 550 551 const char *name = strtab + sym.st_name; 552 if (!filter_matches_symbol(options.static_filter, name, lib)) 553 continue; 554 555 target_address_t addr = (target_address_t) 556 (uintptr_t)(sym.st_value + lte->bias); 557 target_address_t naddr; 558 559 /* On arches that support OPD, the value of typical 560 * function symbol will be a pointer to .opd, but some 561 * will point directly to .text. We don't want to 562 * translate those. */ 563 if (secflags[sym.st_shndx] & SHF_EXECINSTR) { 564 naddr = addr; 565 } else if (arch_translate_address(proc, addr, &naddr) < 0) { 566 error(0, errno, "couldn't translate address of %s@%s", 567 name, lib->soname); 568 continue; 569 } 570 571 /* If the translation actually took place, and wasn't 572 * a no-op, then bias again. XXX We shouldn't apply 573 * second bias for libraries that were open at the 574 * time that we attached. In fact what we should do 575 * is look at each translated address, whether it 576 * falls into a SHF_EXECINSTR section. If it does, 577 * it's most likely already translated. */ 578 if (addr != naddr) 579 naddr += lte->bias; 580 581 char *full_name; 582 if (lib->type != LT_LIBTYPE_MAIN) { 583 full_name = malloc(strlen(name) + 1 + lib_len + 1); 584 if (full_name == NULL) 585 goto fail; 586 sprintf(full_name, "%s@%s", name, lib->soname); 587 } else { 588 full_name = strdup(name); 589 if (full_name == NULL) 590 goto fail; 591 } 592 593 /* Look whether we already have a symbol for this 594 * address. If not, add this one. */ 595 struct unique_symbol key = { naddr, NULL }; 596 struct unique_symbol *unique 597 = lsearch(&key, symbols, &num_symbols, 598 sizeof(*symbols), &unique_symbol_cmp); 599 600 if (unique->libsym == NULL) { 601 struct library_symbol *libsym = malloc(sizeof(*libsym)); 602 if (libsym == NULL 603 || library_symbol_init(libsym, naddr, full_name, 604 1, LS_TOPLT_NONE) < 0) { 605 --num_symbols; 606 goto fail; 607 } 608 unique->libsym = libsym; 609 unique->addr = naddr; 610 611 } else if (strlen(full_name) < strlen(unique->libsym->name)) { 612 library_symbol_set_name(unique->libsym, full_name, 1); 613 614 } else { 615 free(full_name); 616 } 617 } 618 619 for (i = 0; i < num_symbols; ++i) { 620 assert(symbols[i].libsym != NULL); 621 library_add_symbol(lib, symbols[i].libsym); 622 } 623 624 free(symbols); 625 626 return 0; 627} 628 629static int 630populate_symtab(struct Process *proc, const char *filename, 631 struct ltelf *lte, struct library *lib) 632{ 633 if (lte->symtab != NULL && lte->strtab != NULL) 634 return populate_this_symtab(proc, filename, lte, lib, 635 lte->symtab, lte->strtab, 636 lte->symtab_count); 637 else 638 return populate_this_symtab(proc, filename, lte, lib, 639 lte->dynsym, lte->dynstr, 640 lte->dynsym_count); 641} 642 643int 644ltelf_read_library(struct library *lib, struct Process *proc, 645 const char *filename, GElf_Addr bias) 646{ 647 struct ltelf lte = {}; 648 if (do_init_elf(<e, filename, bias) < 0) 649 return -1; 650 if (arch_elf_init(<e, lib) < 0) { 651 fprintf(stderr, "Backend initialization failed.\n"); 652 return -1; 653 } 654 655 proc->e_machine = lte.ehdr.e_machine; 656 657 int status = 0; 658 if (lib == NULL) 659 goto fail; 660 661 /* Note that we set soname and pathname as soon as they are 662 * allocated, so in case of further errors, this get released 663 * when LIB is release, which should happen in the caller when 664 * we return error. */ 665 666 if (lib->pathname == NULL) { 667 char *pathname = strdup(filename); 668 if (pathname == NULL) 669 goto fail; 670 library_set_pathname(lib, pathname, 1); 671 } 672 673 if (lte.soname != NULL) { 674 char *soname = strdup(lte.soname); 675 if (soname == NULL) 676 goto fail; 677 library_set_soname(lib, soname, 1); 678 } else { 679 const char *soname = rindex(lib->pathname, '/') + 1; 680 if (soname == NULL) 681 soname = lib->pathname; 682 library_set_soname(lib, soname, 0); 683 } 684 685 /* XXX The double cast should be removed when 686 * target_address_t becomes integral type. */ 687 target_address_t entry = (target_address_t)(uintptr_t)lte.entry_addr; 688 if (arch_translate_address(proc, entry, &entry) < 0) 689 goto fail; 690 691 /* XXX The double cast should be removed when 692 * target_address_t becomes integral type. */ 693 lib->base = (target_address_t)(uintptr_t)lte.base_addr; 694 lib->entry = entry; 695 /* XXX The double cast should be removed when 696 * target_address_t becomes integral type. */ 697 lib->dyn_addr = (target_address_t)(uintptr_t)lte.dyn_addr; 698 699 if (filter_matches_library(options.plt_filter, lib) 700 && populate_plt(proc, filename, <e, lib) < 0) 701 goto fail; 702 703 if (filter_matches_library(options.static_filter, lib) 704 && populate_symtab(proc, filename, <e, lib) < 0) 705 goto fail; 706 707done: 708 do_close_elf(<e); 709 return status; 710 711fail: 712 status = -1; 713 goto done; 714} 715 716struct library * 717ltelf_read_main_binary(struct Process *proc, const char *path) 718{ 719 struct library *lib = malloc(sizeof(*lib)); 720 if (lib == NULL) 721 return NULL; 722 library_init(lib, LT_LIBTYPE_MAIN); 723 library_set_pathname(lib, path, 0); 724 725 /* There is a race between running the process and reading its 726 * binary for internal consumption. So open the binary from 727 * the /proc filesystem. XXX Note that there is similar race 728 * for libraries, but there we don't have a nice answer like 729 * that. Presumably we could read the DSOs from the process 730 * memory image, but that's not currently done. */ 731 char *fname = pid2name(proc->pid); 732 if (ltelf_read_library(lib, proc, fname, 0) < 0) { 733 library_destroy(lib); 734 free(lib); 735 return NULL; 736 } 737 738 return lib; 739} 740