ltrace-elf.h revision b5f80ac8982c40f79915ce1e1cb9bf8650ac5fe7
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 size_t plt_size; 26 Elf_Data *relplt; 27 Elf_Data *plt_data; 28 size_t relplt_count; 29 Elf_Data *symtab; 30 const char *strtab; 31 const char *soname; 32 size_t symtab_count; 33 Elf_Data *opd; 34 GElf_Addr *opd_addr; 35 size_t opd_size; 36 int lte_flags; 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 46#define LTE_PLT_EXECUTABLE 2 47 48#define PLTS_ARE_EXECUTABLE(lte) (((lte)->lte_flags & LTE_PLT_EXECUTABLE) != 0) 49 50int open_elf(struct ltelf *lte, const char *filename); 51 52/* XXX is it possible to put breakpoints in VDSO and VSYSCALL 53 * pseudo-libraries? For now we assume that all libraries can be 54 * opened via a filesystem. BASE is ignored for ET_EXEC files. */ 55int ltelf_read_library(struct library *lib, struct Process *proc, 56 const char *filename, GElf_Addr bias); 57 58/* Create a library object representing the main binary. The entry 59 * point address is stored to *ENTRYP. */ 60struct library *ltelf_read_main_binary(struct Process *proc, const char *path); 61 62GElf_Addr arch_plt_sym_val(struct ltelf *, size_t, GElf_Rela *); 63 64Elf_Data *elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr); 65int elf_get_section_covering(struct ltelf *lte, GElf_Addr addr, 66 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr); 67int elf_get_section_type(struct ltelf *lte, GElf_Word type, 68 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr); 69 70/* Read, respectively, 2, 4, or 8 bytes from Elf data at given OFFSET, 71 * and store it in *RETP. Returns 0 on success or a negative value if 72 * there's not enough data. */ 73int elf_read_u16(Elf_Data *data, size_t offset, uint16_t *retp); 74int elf_read_u32(Elf_Data *data, size_t offset, uint32_t *retp); 75int elf_read_u64(Elf_Data *data, size_t offset, uint64_t *retp); 76 77 78#if __WORDSIZE == 32 79#define PRI_ELF_ADDR PRIx32 80#define GELF_ADDR_CAST(x) (void *)(uint32_t)(x) 81#else 82#define PRI_ELF_ADDR PRIx64 83#define GELF_ADDR_CAST(x) (void *)(x) 84#endif 85 86#endif 87