ltrace-elf.h revision e67635d6dcecb0f44448a5329d69fd0de74ebaba
1#ifndef LTRACE_ELF_H 2#define LTRACE_ELF_H 3 4#include <gelf.h> 5#include <stdlib.h> 6 7#define DEFINING_LTELF 8#include "arch.h" 9#undef DEFINING_LTELF 10 11#ifndef ARCH_HAVE_LTELF_DATA 12struct arch_ltelf_data { 13}; 14#endif 15 16struct Process; 17struct library; 18 19/* XXX Ok, the original idea was to separate the low-level ELF data 20 * from the abstract "struct library" object, but we use some of the 21 * following extensively in the back end. Not all though. So what we 22 * use should be move to struct library, and the rest of this 23 * structure maybe could be safely hidden in .c. How to integrate the 24 * arch-specific bits into struct library is unclear as of now. */ 25struct ltelf { 26 int fd; 27 Elf *elf; 28 GElf_Ehdr ehdr; 29 Elf_Data *dynsym; 30 size_t dynsym_count; 31 const char *dynstr; 32 GElf_Addr plt_addr; 33 size_t plt_size; 34 Elf_Data *relplt; 35 Elf_Data *plt_data; 36 size_t relplt_count; 37 Elf_Data *symtab; 38 const char *strtab; 39 const char *soname; 40 size_t symtab_count; 41 Elf_Data *opd; 42 GElf_Addr *opd_addr; 43 size_t opd_size; 44 int lte_flags; 45 GElf_Addr dyn_addr; 46 size_t dyn_sz; 47 size_t relplt_size; 48 GElf_Addr bias; 49 GElf_Addr entry_addr; 50 GElf_Addr base_addr; 51 struct arch_ltelf_data arch; 52}; 53 54#define ELF_MAX_SEGMENTS 50 55#define LTE_PLT_EXECUTABLE 2 56 57#define PLTS_ARE_EXECUTABLE(lte) (((lte)->lte_flags & LTE_PLT_EXECUTABLE) != 0) 58 59int open_elf(struct ltelf *lte, const char *filename); 60 61/* XXX is it possible to put breakpoints in VDSO and VSYSCALL 62 * pseudo-libraries? For now we assume that all libraries can be 63 * opened via a filesystem. BASE is ignored for ET_EXEC files. */ 64struct library *ltelf_read_library(const char *filename, GElf_Addr base); 65 66/* Create a library object representing the main binary. The entry 67 * point address is stored to *ENTRYP. */ 68struct library *ltelf_read_main_binary(struct Process *proc, const char *path); 69 70GElf_Addr arch_plt_sym_val(struct ltelf *, size_t, GElf_Rela *); 71 72Elf_Data *elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr); 73int elf_get_section_covering(struct ltelf *lte, GElf_Addr addr, 74 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr); 75 76/* Read, respectively, 2, 4, or 8 bytes from Elf data at given OFFSET, 77 * and store it in *RETP. Returns 0 on success or a negative value if 78 * there's not enough data. */ 79int elf_read_u16(Elf_Data *data, size_t offset, uint16_t *retp); 80int elf_read_u32(Elf_Data *data, size_t offset, uint32_t *retp); 81int elf_read_u64(Elf_Data *data, size_t offset, uint64_t *retp); 82 83 84#if __WORDSIZE == 32 85#define PRI_ELF_ADDR PRIx32 86#define GELF_ADDR_CAST(x) (void *)(uint32_t)(x) 87#else 88#define PRI_ELF_ADDR PRIx64 89#define GELF_ADDR_CAST(x) (void *)(x) 90#endif 91 92#endif 93