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