ltrace-elf.h revision 439ab5bfac8588e52c77e22c96fb397787512d0e
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2006,2010,2012,2013 Petr Machata, Red Hat Inc. 4 * Copyright (C) 2010 Zachary T Welch 5 * Copyright (C) 2001,2004,2007,2009 Juan Cespedes 6 * Copyright (C) 2006 Ian Wienand 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as 10 * published by the Free Software Foundation; either version 2 of the 11 * License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 21 * 02110-1301 USA 22 */ 23 24#ifndef LTRACE_ELF_H 25#define LTRACE_ELF_H 26 27#include <gelf.h> 28#include <stdlib.h> 29 30#include "forward.h" 31#include "sysdep.h" 32 33/* XXX Ok, the original idea was to separate the low-level ELF data 34 * from the abstract "struct library" object, but we use some of the 35 * following extensively in the back end. Not all though. So what we 36 * use should be move to struct library, and the rest of this 37 * structure maybe could be safely hidden in .c. How to integrate the 38 * arch-specific bits into struct library is unclear as of now. */ 39struct ltelf { 40 int fd; 41 Elf *elf; 42 GElf_Ehdr ehdr; 43 Elf_Data *dynsym; 44 size_t dynsym_count; 45 const char *dynstr; 46 GElf_Addr plt_addr; 47 GElf_Word plt_flags; 48 size_t plt_size; 49 Elf_Data *relplt; 50 Elf_Data *plt_data; 51 size_t relplt_count; 52 Elf_Data *symtab; 53 const char *strtab; 54 const char *soname; 55 size_t symtab_count; 56 Elf_Data *opd; 57 GElf_Addr *opd_addr; 58 size_t opd_size; 59 GElf_Addr dyn_addr; 60 size_t dyn_sz; 61 size_t relplt_size; 62 GElf_Addr bias; 63 GElf_Addr entry_addr; 64 GElf_Addr base_addr; 65 struct arch_ltelf_data arch; 66}; 67 68int open_elf(struct ltelf *lte, const char *filename); 69void do_close_elf(struct ltelf *lte); 70 71/* XXX is it possible to put breakpoints in VDSO and VSYSCALL 72 * pseudo-libraries? For now we assume that all libraries can be 73 * opened via a filesystem. BASE is ignored for ET_EXEC files. */ 74int ltelf_read_library(struct library *lib, struct process *proc, 75 const char *filename, GElf_Addr bias); 76 77/* Create a library object representing the main binary. The entry 78 * point address is stored to *ENTRYP. */ 79struct library *ltelf_read_main_binary(struct process *proc, const char *path); 80 81/* Create a default PLT entry. This can be used instead (or in 82 * addition to) returning PLT_DEFAULT from arch_elf_add_plt_entry. 83 * RET shall be initialized, the created symbol will be added to the 84 * beginning of the linked list at *RET. This function doesn't add 85 * the symbol to LTE. arch_elf_add_plt_entry has the chance to adjust 86 * symbol internals to its liking, and then return either PLT_DEFAULT 87 * or PLT_OK. */ 88int default_elf_add_plt_entry(struct process *proc, struct ltelf *lte, 89 const char *a_name, GElf_Rela *rela, size_t ndx, 90 struct library_symbol **ret); 91 92/* The base implementation of backend.h (arch_get_sym_info). 93 * See backend.h for details. */ 94int elf_get_sym_info(struct ltelf *lte, const char *filename, 95 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym); 96 97Elf_Data *elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr); 98 99/* The following three look for sections based on various criteria. 100 * They return 0 if there was no error, or a negative value if there 101 * was. If the section was found, it is returned in *TGT_SEC, and the 102 * header is stored te TGT_SHDR. If it wasn't found, *TGT_SEC is set 103 * to NULL. */ 104int elf_get_section_covering(struct ltelf *lte, GElf_Addr addr, 105 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr); 106int elf_get_section_type(struct ltelf *lte, GElf_Word type, 107 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr); 108int elf_get_section_named(struct ltelf *lte, const char *name, 109 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr); 110 111/* Read, respectively, 1, 2, 4, or 8 bytes from Elf data at given 112 * OFFSET, and store it in *RETP. Returns 0 on success or a negative 113 * value if there's not enough data. */ 114int elf_read_u8(Elf_Data *data, GElf_Xword offset, uint8_t *retp); 115int elf_read_u16(Elf_Data *data, GElf_Xword offset, uint16_t *retp); 116int elf_read_u32(Elf_Data *data, GElf_Xword offset, uint32_t *retp); 117int elf_read_u64(Elf_Data *data, GElf_Xword offset, uint64_t *retp); 118 119/* These are same as above, but update *OFFSET with the width 120 * of read datum. */ 121int elf_read_next_u8(Elf_Data *data, GElf_Xword *offset, uint8_t *retp); 122int elf_read_next_u16(Elf_Data *data, GElf_Xword *offset, uint16_t *retp); 123int elf_read_next_u32(Elf_Data *data, GElf_Xword *offset, uint32_t *retp); 124int elf_read_next_u64(Elf_Data *data, GElf_Xword *offset, uint64_t *retp); 125 126#if __WORDSIZE == 32 127#define PRI_ELF_ADDR PRIx32 128#define GELF_ADDR_CAST(x) (void *)(uint32_t)(x) 129#else 130#define PRI_ELF_ADDR PRIx64 131#define GELF_ADDR_CAST(x) (void *)(x) 132#endif 133 134#endif 135