ltrace-elf.h revision b120fdfda45e49d082ab2aa5caadf030f50cb4ed
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(struct Process *proc,
65				   const char *filename, GElf_Addr bias);
66
67/* Create a library object representing the main binary.  The entry
68 * point address is stored to *ENTRYP.  */
69struct library *ltelf_read_main_binary(struct Process *proc, const char *path);
70
71GElf_Addr arch_plt_sym_val(struct ltelf *, size_t, GElf_Rela *);
72
73Elf_Data *elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr);
74int elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
75			     Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
76
77/* Read, respectively, 2, 4, or 8 bytes from Elf data at given OFFSET,
78 * and store it in *RETP.  Returns 0 on success or a negative value if
79 * there's not enough data.  */
80int elf_read_u16(Elf_Data *data, size_t offset, uint16_t *retp);
81int elf_read_u32(Elf_Data *data, size_t offset, uint32_t *retp);
82int elf_read_u64(Elf_Data *data, size_t offset, uint64_t *retp);
83
84
85#if __WORDSIZE == 32
86#define PRI_ELF_ADDR		PRIx32
87#define GELF_ADDR_CAST(x)	(void *)(uint32_t)(x)
88#else
89#define PRI_ELF_ADDR		PRIx64
90#define GELF_ADDR_CAST(x)	(void *)(x)
91#endif
92
93#endif
94