ltrace-elf.h revision 3a01cd7a2fcf200cedd0770e137a28764f679c3c
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
59GElf_Addr arch_plt_sym_val(struct ltelf *, size_t, GElf_Rela *);
60
61Elf_Data *elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr);
62int elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
63			     Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
64int elf_get_section_type(struct ltelf *lte, GElf_Word type,
65			 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
66
67/* Read, respectively, 2, 4, or 8 bytes from Elf data at given OFFSET,
68 * and store it in *RETP.  Returns 0 on success or a negative value if
69 * there's not enough data.  */
70int elf_read_u16(Elf_Data *data, GElf_Xword offset, uint16_t *retp);
71int elf_read_u32(Elf_Data *data, GElf_Xword offset, uint32_t *retp);
72int elf_read_u64(Elf_Data *data, GElf_Xword offset, uint64_t *retp);
73
74int default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
75			      const char *a_name, GElf_Rela *rela, size_t ndx,
76			      struct library_symbol **ret);
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