blob: d907e5ff00c5777c1e25432d8e11c6595a99c4f6 [file] [log] [blame]
Juan Cespedes1cd999a2001-07-03 00:46:04 +02001#ifndef LTRACE_ELF_H
2#define LTRACE_ELF_H
3
Juan Cespedesd914a202004-11-10 00:15:33 +01004#include <gelf.h>
5#include <stdlib.h>
6
Petr Machatae67635d2012-03-21 03:37:39 +01007#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
Petr Machata2b46cfc2012-02-18 11:17:29 +010016struct 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. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010025struct 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;
Petr Machatae67635d2012-03-21 03:37:39 +010035 Elf_Data *plt_data;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010036 size_t relplt_count;
37 Elf_Data *symtab;
38 const char *strtab;
Petr Machata2b46cfc2012-02-18 11:17:29 +010039 const char *soname;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010040 size_t symtab_count;
41 Elf_Data *opd;
42 GElf_Addr *opd_addr;
43 size_t opd_size;
Paul Gilliam76c61f12006-06-14 06:55:21 +020044 int lte_flags;
Joe Damato87f4f582010-11-08 15:47:36 -080045 GElf_Addr dyn_addr;
46 size_t dyn_sz;
Petr Machatae67635d2012-03-21 03:37:39 +010047 size_t relplt_size;
Petr Machata29add4f2012-02-18 16:38:05 +010048 GElf_Addr bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +010049 GElf_Addr entry_addr;
Petr Machata29add4f2012-02-18 16:38:05 +010050 GElf_Addr base_addr;
Petr Machatae67635d2012-03-21 03:37:39 +010051 struct arch_ltelf_data arch;
Juan Cespedes1cd999a2001-07-03 00:46:04 +020052};
53
Joe Damato47cae1e2010-11-08 15:47:39 -080054#define ELF_MAX_SEGMENTS 50
Paul Gilliam76c61f12006-06-14 06:55:21 +020055#define LTE_PLT_EXECUTABLE 2
56
Petr Machata2b46cfc2012-02-18 11:17:29 +010057#define PLTS_ARE_EXECUTABLE(lte) (((lte)->lte_flags & LTE_PLT_EXECUTABLE) != 0)
Paul Gilliam76c61f12006-06-14 06:55:21 +020058
Petr Machata2b46cfc2012-02-18 11:17:29 +010059int open_elf(struct ltelf *lte, const char *filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020060
Petr Machata2b46cfc2012-02-18 11:17:29 +010061/* 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. */
Petr Machatab120fdf2012-03-21 05:05:46 +010064struct library *ltelf_read_library(struct Process *proc,
65 const char *filename, GElf_Addr bias);
Juan Cespedesd914a202004-11-10 00:15:33 +010066
Petr Machata2b46cfc2012-02-18 11:17:29 +010067/* 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);
Juan Cespedes96935a91997-08-09 23:45:39 +020070
Petr Machata2b46cfc2012-02-18 11:17:29 +010071GElf_Addr arch_plt_sym_val(struct ltelf *, size_t, GElf_Rela *);
Zachary T Welch3ba522f2010-12-14 15:12:47 -080072
Petr Machatae67635d2012-03-21 03:37:39 +010073Elf_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
Zachary T Welch3ba522f2010-12-14 15:12:47 -080085#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
Juan Cespedes1cd999a2001-07-03 00:46:04 +020093#endif