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
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.
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.
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
48 #include "ltrace-elf.h"
53 #ifndef ARCH_HAVE_LTELF_DATA
55 arch_elf_init(struct ltelf *lte, struct library *lib)
61 arch_elf_destroy(struct ltelf *lte)
67 default_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)
71 char *name = strdup(a_name);
74 fprintf(stderr, "Couldn't create symbol for PLT entry: %s\n",
81 GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
83 struct library_symbol *libsym = malloc(sizeof(*libsym));
87 /* XXX The double cast should be removed when
88 * arch_addr_t becomes integral type. */
89 arch_addr_t taddr = (arch_addr_t)
90 (uintptr_t)(addr + lte->bias);
92 if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) {
102 #ifndef ARCH_HAVE_ADD_PLT_ENTRY
104 arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
105 const char *a_name, GElf_Rela *rela, size_t ndx,
106 struct library_symbol **ret)
113 elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
115 Elf_Data *data = elf_getdata(scn, NULL);
116 if (data == NULL || elf_getdata(scn, data) != NULL
117 || data->d_off || data->d_size != shdr->sh_size)
123 elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
124 int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
128 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
132 scn = elf_getscn(lte->elf, i);
133 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
134 debug(1, "Couldn't read section or header.");
137 if (predicate(scn, &shdr, data)) {
148 inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
150 GElf_Addr addr = *(GElf_Addr *)data;
151 return addr >= shdr->sh_addr
152 && addr < shdr->sh_addr + shdr->sh_size;
156 elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
157 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
159 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
164 type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
166 GElf_Word type = *(GElf_Word *)data;
167 return shdr->sh_type == type;
171 elf_get_section_type(struct ltelf *lte, GElf_Word type,
172 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
174 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
178 struct section_named_data {
184 name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d)
186 struct section_named_data *data = d;
187 const char *name = elf_strptr(data->lte->elf,
188 data->lte->ehdr.e_shstrndx,
190 return strcmp(name, data->name) == 0;
194 elf_get_section_named(struct ltelf *lte, const char *name,
195 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
197 struct section_named_data data = {
201 return elf_get_section_if(lte, tgt_sec, tgt_shdr,
206 need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
208 assert(data != NULL);
209 if (data->d_size < size || offset > data->d_size - size) {
210 debug(1, "Not enough data to read %"PRId64"-byte value"
211 " at offset %"PRId64".", size, offset);
217 #define DEF_READER(NAME, SIZE) \
219 NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp) \
221 if (!need_data(data, offset, SIZE / 8) < 0) \
224 if (data->d_buf == NULL) /* NODATA section */ { \
230 uint##SIZE##_t dst; \
233 memcpy(u.buf, data->d_buf + offset, sizeof(u.dst)); \
238 DEF_READER(elf_read_u16, 16)
239 DEF_READER(elf_read_u32, 32)
240 DEF_READER(elf_read_u64, 64)
245 open_elf(struct ltelf *lte, const char *filename)
247 lte->fd = open(filename, O_RDONLY);
251 elf_version(EV_CURRENT);
253 #ifdef HAVE_ELF_C_READ_MMAP
254 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
256 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
259 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) {
260 fprintf(stderr, "\"%s\" is not an ELF file\n", filename);
264 if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) {
265 fprintf(stderr, "can't read ELF header of \"%s\": %s\n",
266 filename, elf_errmsg(-1));
270 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) {
271 fprintf(stderr, "\"%s\" is neither an ELF executable"
272 " nor a shared library\n", filename);
277 #ifdef LT_ELF_MACHINE
278 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
279 || lte->ehdr.e_machine != LT_ELF_MACHINE)
281 #ifdef LT_ELF_MACHINE2
282 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
283 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
285 #ifdef LT_ELF_MACHINE3
286 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
287 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
291 "\"%s\" is ELF from incompatible architecture\n",
300 read_symbol_table(struct ltelf *lte, const char *filename,
301 Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
302 Elf_Data **datap, size_t *countp, const char **strsp)
304 *datap = elf_getdata(scn, NULL);
305 *countp = shdr->sh_size / shdr->sh_entsize;
306 if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
307 && options.static_filter != NULL) {
308 fprintf(stderr, "Couldn't get data of section"
309 " %s from \"%s\": %s\n",
310 name, filename, elf_errmsg(-1));
314 scn = elf_getscn(lte->elf, shdr->sh_link);
316 if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
317 fprintf(stderr, "Couldn't get header of section"
318 " #%d from \"%s\": %s\n",
319 shdr2.sh_link, filename, elf_errmsg(-1));
323 Elf_Data *data = elf_getdata(scn, NULL);
324 if (data == NULL || elf_getdata(scn, data) != NULL
325 || shdr2.sh_size != data->d_size || data->d_off) {
326 fprintf(stderr, "Couldn't get data of section"
327 " #%d from \"%s\": %s\n",
328 shdr2.sh_link, filename, elf_errmsg(-1));
332 *strsp = data->d_buf;
336 do_init_elf(struct ltelf *lte, const char *filename)
339 GElf_Addr relplt_addr = 0;
340 GElf_Addr soname_offset = 0;
342 debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
343 debug(1, "Reading ELF from %s...", filename);
345 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
350 scn = elf_getscn(lte->elf, i);
351 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
352 fprintf(stderr, "Couldn't get section #%d from"
353 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
357 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
359 fprintf(stderr, "Couldn't get name of section #%d from"
360 " \"%s\": %s\n", i, filename, elf_errmsg(-1));
364 if (shdr.sh_type == SHT_SYMTAB) {
365 read_symbol_table(lte, filename,
366 scn, &shdr, name, <e->symtab,
367 <e->symtab_count, <e->strtab);
369 } else if (shdr.sh_type == SHT_DYNSYM) {
370 read_symbol_table(lte, filename,
371 scn, &shdr, name, <e->dynsym,
372 <e->dynsym_count, <e->dynstr);
374 } else if (shdr.sh_type == SHT_DYNAMIC) {
378 lte->dyn_addr = shdr.sh_addr + lte->bias;
379 lte->dyn_sz = shdr.sh_size;
381 data = elf_getdata(scn, NULL);
382 if (data == NULL || elf_getdata(scn, data) != NULL) {
383 fprintf(stderr, "Couldn't get .dynamic data"
384 " from \"%s\": %s\n",
385 filename, strerror(errno));
389 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
392 if (gelf_getdyn(data, j, &dyn) == NULL) {
393 fprintf(stderr, "Couldn't get .dynamic"
394 " data from \"%s\": %s\n",
395 filename, strerror(errno));
398 if (dyn.d_tag == DT_JMPREL)
399 relplt_addr = dyn.d_un.d_ptr;
400 else if (dyn.d_tag == DT_PLTRELSZ)
401 lte->relplt_size = dyn.d_un.d_val;
402 else if (dyn.d_tag == DT_SONAME)
403 soname_offset = dyn.d_un.d_val;
405 } else if (shdr.sh_type == SHT_PROGBITS
406 || shdr.sh_type == SHT_NOBITS) {
407 if (strcmp(name, ".plt") == 0) {
408 lte->plt_addr = shdr.sh_addr;
409 lte->plt_size = shdr.sh_size;
410 lte->plt_data = elf_loaddata(scn, &shdr);
411 if (lte->plt_data == NULL)
413 "Can't load .plt data\n");
414 lte->plt_flags = shdr.sh_flags;
416 #ifdef ARCH_SUPPORTS_OPD
417 else if (strcmp(name, ".opd") == 0) {
418 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
419 lte->opd_size = shdr.sh_size;
420 lte->opd = elf_rawdata(scn, NULL);
426 if (lte->dynsym == NULL || lte->dynstr == NULL) {
427 fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
432 if (!relplt_addr || !lte->plt_addr) {
433 debug(1, "%s has no PLT relocations", filename);
435 lte->relplt_count = 0;
436 } else if (lte->relplt_size == 0) {
437 debug(1, "%s has unknown PLT size", filename);
439 lte->relplt_count = 0;
442 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
446 scn = elf_getscn(lte->elf, i);
447 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
448 fprintf(stderr, "Couldn't get section header"
449 " from \"%s\": %s\n",
450 filename, elf_errmsg(-1));
453 if (shdr.sh_addr == relplt_addr
454 && shdr.sh_size == lte->relplt_size) {
455 lte->relplt = elf_getdata(scn, NULL);
457 shdr.sh_size / shdr.sh_entsize;
458 if (lte->relplt == NULL
459 || elf_getdata(scn, lte->relplt) != NULL) {
460 fprintf(stderr, "Couldn't get .rel*.plt"
461 " data from \"%s\": %s\n",
462 filename, elf_errmsg(-1));
469 if (i == lte->ehdr.e_shnum) {
471 "Couldn't find .rel*.plt section in \"%s\"\n",
476 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
479 if (soname_offset != 0)
480 lte->soname = lte->dynstr + soname_offset;
486 do_close_elf(struct ltelf *lte)
488 debug(DEBUG_FUNCTION, "do_close_elf()");
489 arch_elf_destroy(lte);
495 elf_get_sym_info(struct ltelf *lte, const char *filename,
496 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
502 if (lte->relplt->d_type == ELF_T_REL) {
503 ret = gelf_getrel(lte->relplt, i, &rel);
504 rela->r_offset = rel.r_offset;
505 rela->r_info = rel.r_info;
508 ret = gelf_getrela(lte->relplt, i, rela);
512 || ELF64_R_SYM(rela->r_info) >= lte->dynsym_count
513 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela->r_info),
516 "Couldn't get relocation from \"%s\": %s\n",
517 filename, elf_errmsg(-1));
524 #ifndef ARCH_HAVE_GET_SYMINFO
526 arch_get_sym_info(struct ltelf *lte, const char *filename,
527 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
529 return elf_get_sym_info(lte, filename, sym_index, rela, sym);
534 mark_chain_latent(struct library_symbol *libsym)
536 for (; libsym != NULL; libsym = libsym->next) {
537 debug(DEBUG_FUNCTION, "marking %s latent", libsym->name);
543 populate_plt(struct Process *proc, const char *filename,
544 struct ltelf *lte, struct library *lib,
548 for (i = 0; i < lte->relplt_count; ++i) {
552 if (arch_get_sym_info(lte, filename, i, &rela, &sym) < 0)
553 continue; /* Skip this entry. */
555 char const *name = lte->dynstr + sym.st_name;
557 /* If the symbol wasn't matched, reject it, unless we
558 * need to keep latent PLT breakpoints for tracing
560 int matched = filter_matches_symbol(options.plt_filter,
562 if (!matched && !latent_plts)
565 struct library_symbol *libsym = NULL;
566 switch (arch_elf_add_plt_entry(proc, lte, name,
567 &rela, i, &libsym)) {
569 if (default_elf_add_plt_entry(proc, lte, name,
570 &rela, i, &libsym) < 0)
576 if (libsym != NULL) {
577 /* If we are adding those symbols just
578 * for tracing exports, mark them all
581 mark_chain_latent(libsym);
582 library_add_symbol(lib, libsym);
589 /* When -x rules result in request to trace several aliases, we only
590 * want to add such symbol once. The only way that those symbols
591 * differ in is their name, e.g. in glibc you have __GI___libc_free,
592 * __cfree, __free, __libc_free, cfree and free all defined on the
593 * same address. So instead we keep this unique symbol struct for
594 * each address, and replace name in libsym with a shorter variant if
596 struct unique_symbol {
598 struct library_symbol *libsym;
602 unique_symbol_cmp(const void *key, const void *val)
604 const struct unique_symbol *sym_key = key;
605 const struct unique_symbol *sym_val = val;
606 return sym_key->addr != sym_val->addr;
609 static enum callback_status
610 symbol_with_address(struct library_symbol *sym, void *addrptr)
612 return sym->enter_addr == *(arch_addr_t *)addrptr
613 ? CBS_STOP : CBS_CONT;
617 populate_this_symtab(struct Process *proc, const char *filename,
618 struct ltelf *lte, struct library *lib,
619 Elf_Data *symtab, const char *strtab, size_t size,
620 struct library_exported_name **names)
622 /* If a valid NAMES is passed, we pass in *NAMES a list of
623 * symbol names that this library exports. */
627 /* Using sorted array would be arguably better, but this
628 * should be well enough for the number of symbols that we
629 * typically deal with. */
630 size_t num_symbols = 0;
631 struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
632 if (symbols == NULL) {
633 fprintf(stderr, "couldn't insert symbols for -x: %s\n",
638 GElf_Word secflags[lte->ehdr.e_shnum];
640 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
641 Elf_Scn *scn = elf_getscn(lte->elf, i);
645 if (gelf_getshdr(scn, &shdr) == NULL)
647 secflags[i] = shdr.sh_flags;
650 for (i = 0; i < size; ++i) {
652 if (gelf_getsym(symtab, i, &sym) == NULL) {
655 "couldn't get symbol #%zd from %s: %s\n",
656 i, filename, elf_errmsg(-1));
660 /* XXX support IFUNC as well. */
661 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
663 || sym.st_shndx == STN_UNDEF)
666 /* Find symbol name and snip version. */
667 const char *orig_name = strtab + sym.st_name;
668 const char *version = strchr(orig_name, '@');
669 size_t len = version != NULL ? (assert(version > orig_name),
670 (size_t)(version - orig_name))
673 memcpy(name, orig_name, len);
676 /* If we are interested in exports, store this name. */
677 char *name_copy = NULL;
679 struct library_exported_name *export = NULL;
680 name_copy = strdup(name);
682 if (name_copy == NULL
683 || (export = malloc(sizeof(*export))) == NULL) {
685 fprintf(stderr, "Couldn't store symbol %s. "
686 "Tracing may be incomplete.\n", name);
688 export->name = name_copy;
689 export->own_name = 1;
690 export->next = *names;
695 /* If the symbol is not matched, skip it. We already
696 * stored it to export list above. */
697 if (!filter_matches_symbol(options.static_filter, name, lib))
700 arch_addr_t addr = (arch_addr_t)
701 (uintptr_t)(sym.st_value + lte->bias);
704 /* On arches that support OPD, the value of typical
705 * function symbol will be a pointer to .opd, but some
706 * will point directly to .text. We don't want to
707 * translate those. */
708 if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
710 } else if (arch_translate_address(lte, addr, &naddr) < 0) {
712 "couldn't translate address of %s@%s: %s\n",
713 name, lib->soname, strerror(errno));
718 int own_full_name = 1;
719 if (name_copy == NULL) {
720 full_name = strdup(name);
721 if (full_name == NULL)
724 full_name = name_copy;
728 /* Look whether we already have a symbol for this
729 * address. If not, add this one. */
730 struct unique_symbol key = { naddr, NULL };
731 struct unique_symbol *unique
732 = lsearch(&key, symbols, &num_symbols,
733 sizeof(*symbols), &unique_symbol_cmp);
735 if (unique->libsym == NULL) {
736 struct library_symbol *libsym = malloc(sizeof(*libsym));
738 || library_symbol_init(libsym, naddr,
739 full_name, own_full_name,
740 LS_TOPLT_NONE) < 0) {
744 unique->libsym = libsym;
745 unique->addr = naddr;
747 } else if (strlen(full_name) < strlen(unique->libsym->name)) {
748 library_symbol_set_name(unique->libsym,
749 full_name, own_full_name);
751 } else if (own_full_name) {
756 /* Now we do the union of this set of unique symbols with
757 * what's already in the library. */
758 for (i = 0; i < num_symbols; ++i) {
759 struct library_symbol *this_sym = symbols[i].libsym;
760 assert(this_sym != NULL);
761 struct library_symbol *other
762 = library_each_symbol(lib, NULL, symbol_with_address,
763 &this_sym->enter_addr);
765 library_symbol_destroy(this_sym);
767 symbols[i].libsym = NULL;
771 for (i = 0; i < num_symbols; ++i)
772 if (symbols[i].libsym != NULL)
773 library_add_symbol(lib, symbols[i].libsym);
780 populate_symtab(struct Process *proc, const char *filename,
781 struct ltelf *lte, struct library *lib,
782 int symtabs, int exports)
785 if (symtabs && lte->symtab != NULL && lte->strtab != NULL
786 && (status = populate_this_symtab(proc, filename, lte, lib,
787 lte->symtab, lte->strtab,
788 lte->symtab_count, NULL)) < 0)
791 /* Check whether we want to trace symbols implemented by this
793 struct library_exported_name **names = NULL;
795 debug(DEBUG_FUNCTION, "-l matches %s", lib->soname);
796 names = &lib->exported_names;
799 return populate_this_symtab(proc, filename, lte, lib,
800 lte->dynsym, lte->dynstr,
801 lte->dynsym_count, names);
805 read_module(struct library *lib, struct Process *proc,
806 const char *filename, GElf_Addr bias, int main)
808 struct ltelf lte = {};
809 if (open_elf(<e, filename) < 0)
812 /* XXX When we abstract ABI into a module, this should instead
813 * become something like
815 * proc->abi = arch_get_abi(lte.ehdr);
817 * The code in open_elf needs to be replaced by this logic.
818 * Be warned that libltrace.c calls open_elf as well to
819 * determine whether ABI is supported. This is to get
820 * reasonable error messages when trying to run 64-bit binary
821 * with 32-bit ltrace. It is desirable to preserve this. */
822 proc->e_machine = lte.ehdr.e_machine;
823 proc->e_class = lte.ehdr.e_ident[EI_CLASS];
826 /* Find out the base address. For PIE main binaries we look
827 * into auxv, otherwise we scan phdrs. */
828 if (main && lte.ehdr.e_type == ET_DYN) {
830 if (process_get_entry(proc, &entry, NULL) < 0) {
831 fprintf(stderr, "Couldn't find entry of PIE %s\n",
835 /* XXX The double cast should be removed when
836 * arch_addr_t becomes integral type. */
837 lte.entry_addr = (GElf_Addr)(uintptr_t)entry;
838 lte.bias = (GElf_Addr)(uintptr_t)entry - lte.ehdr.e_entry;
843 for (i = 0; gelf_getphdr (lte.elf, i, &phdr) != NULL; ++i) {
844 if (phdr.p_type == PT_LOAD) {
845 lte.base_addr = phdr.p_vaddr + bias;
851 lte.entry_addr = lte.ehdr.e_entry + lte.bias;
853 if (lte.base_addr == 0) {
855 "Couldn't determine base address of %s\n",
861 if (do_init_elf(<e, filename) < 0)
864 if (arch_elf_init(<e, lib) < 0) {
865 fprintf(stderr, "Backend initialization failed.\n");
873 /* Note that we set soname and pathname as soon as they are
874 * allocated, so in case of further errors, this get released
875 * when LIB is release, which should happen in the caller when
876 * we return error. */
878 if (lib->pathname == NULL) {
879 char *pathname = strdup(filename);
880 if (pathname == NULL)
882 library_set_pathname(lib, pathname, 1);
885 if (lte.soname != NULL) {
886 char *soname = strdup(lte.soname);
889 library_set_soname(lib, soname, 1);
891 const char *soname = rindex(lib->pathname, '/') + 1;
893 soname = lib->pathname;
894 library_set_soname(lib, soname, 0);
897 /* XXX The double cast should be removed when
898 * arch_addr_t becomes integral type. */
899 arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr;
900 if (arch_translate_address(<e, entry, &entry) < 0)
903 /* XXX The double cast should be removed when
904 * arch_addr_t becomes integral type. */
905 lib->base = (arch_addr_t)(uintptr_t)lte.base_addr;
907 /* XXX The double cast should be removed when
908 * arch_addr_t becomes integral type. */
909 lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr;
911 /* There are two reasons that we need to inspect symbol tables
912 * or populate PLT entries. Either the user requested
913 * corresponding tracing features (respectively -x and -e), or
914 * they requested tracing exported symbols (-l).
916 * In the latter case we need to keep even those PLT slots
917 * that are not requested by -e (but we keep them latent). We
918 * also need to inspect .dynsym to find what exports this
919 * library provide, to turn on existing latent PLT
922 int plts = filter_matches_library(options.plt_filter, lib);
923 if ((plts || options.export_filter != NULL)
924 && populate_plt(proc, filename, <e, lib,
925 options.export_filter != NULL) < 0)
928 int exports = filter_matches_library(options.export_filter, lib);
929 int symtabs = filter_matches_library(options.static_filter, lib);
930 if ((symtabs || exports)
931 && populate_symtab(proc, filename, <e, lib,
932 symtabs, exports) < 0)
945 ltelf_read_library(struct library *lib, struct Process *proc,
946 const char *filename, GElf_Addr bias)
948 return read_module(lib, proc, filename, bias, 0);
953 ltelf_read_main_binary(struct Process *proc, const char *path)
955 struct library *lib = malloc(sizeof(*lib));
958 library_init(lib, LT_LIBTYPE_MAIN);
959 library_set_pathname(lib, path, 0);
961 /* There is a race between running the process and reading its
962 * binary for internal consumption. So open the binary from
963 * the /proc filesystem. XXX Note that there is similar race
964 * for libraries, but there we don't have a nice answer like
965 * that. Presumably we could read the DSOs from the process
966 * memory image, but that's not currently done. */
967 char *fname = pid2name(proc->pid);
970 if (read_module(lib, proc, fname, 0, 1) < 0) {
971 library_destroy(lib);