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