blob: de5538a94bb8c64f22f3593394a6a20dec8b18eb [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>
Petr Machata50b96032012-03-24 04:56:52 +01006#include "sysdep.h"
Petr Machatae67635d2012-03-21 03:37:39 +01007
Petr Machata2b46cfc2012-02-18 11:17:29 +01008struct Process;
9struct library;
10
11/* XXX Ok, the original idea was to separate the low-level ELF data
12 * from the abstract "struct library" object, but we use some of the
13 * following extensively in the back end. Not all though. So what we
14 * use should be move to struct library, and the rest of this
15 * structure maybe could be safely hidden in .c. How to integrate the
16 * arch-specific bits into struct library is unclear as of now. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010017struct ltelf {
18 int fd;
19 Elf *elf;
20 GElf_Ehdr ehdr;
21 Elf_Data *dynsym;
22 size_t dynsym_count;
23 const char *dynstr;
24 GElf_Addr plt_addr;
25 size_t plt_size;
26 Elf_Data *relplt;
Petr Machatae67635d2012-03-21 03:37:39 +010027 Elf_Data *plt_data;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010028 size_t relplt_count;
29 Elf_Data *symtab;
30 const char *strtab;
Petr Machata2b46cfc2012-02-18 11:17:29 +010031 const char *soname;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010032 size_t symtab_count;
33 Elf_Data *opd;
34 GElf_Addr *opd_addr;
35 size_t opd_size;
Paul Gilliam76c61f12006-06-14 06:55:21 +020036 int lte_flags;
Joe Damato87f4f582010-11-08 15:47:36 -080037 GElf_Addr dyn_addr;
38 size_t dyn_sz;
Petr Machatae67635d2012-03-21 03:37:39 +010039 size_t relplt_size;
Petr Machata29add4f2012-02-18 16:38:05 +010040 GElf_Addr bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +010041 GElf_Addr entry_addr;
Petr Machata29add4f2012-02-18 16:38:05 +010042 GElf_Addr base_addr;
Petr Machatae67635d2012-03-21 03:37:39 +010043 struct arch_ltelf_data arch;
Juan Cespedes1cd999a2001-07-03 00:46:04 +020044};
45
Paul Gilliam76c61f12006-06-14 06:55:21 +020046#define LTE_PLT_EXECUTABLE 2
47
Petr Machata2b46cfc2012-02-18 11:17:29 +010048#define PLTS_ARE_EXECUTABLE(lte) (((lte)->lte_flags & LTE_PLT_EXECUTABLE) != 0)
Paul Gilliam76c61f12006-06-14 06:55:21 +020049
Petr Machata2b46cfc2012-02-18 11:17:29 +010050int open_elf(struct ltelf *lte, const char *filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020051
Petr Machata2b46cfc2012-02-18 11:17:29 +010052/* XXX is it possible to put breakpoints in VDSO and VSYSCALL
53 * pseudo-libraries? For now we assume that all libraries can be
54 * opened via a filesystem. BASE is ignored for ET_EXEC files. */
Petr Machatab120fdf2012-03-21 05:05:46 +010055struct library *ltelf_read_library(struct Process *proc,
56 const char *filename, GElf_Addr bias);
Juan Cespedesd914a202004-11-10 00:15:33 +010057
Petr Machata2b46cfc2012-02-18 11:17:29 +010058/* Create a library object representing the main binary. The entry
59 * point address is stored to *ENTRYP. */
60struct library *ltelf_read_main_binary(struct Process *proc, const char *path);
Juan Cespedes96935a91997-08-09 23:45:39 +020061
Petr Machata2b46cfc2012-02-18 11:17:29 +010062GElf_Addr arch_plt_sym_val(struct ltelf *, size_t, GElf_Rela *);
Zachary T Welch3ba522f2010-12-14 15:12:47 -080063
Petr Machatae67635d2012-03-21 03:37:39 +010064Elf_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);
Petr Machataffd5aab2012-03-24 02:03:33 +010067int elf_get_section_type(struct ltelf *lte, GElf_Word type,
68 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
Petr Machatae67635d2012-03-21 03:37:39 +010069
70/* Read, respectively, 2, 4, or 8 bytes from Elf data at given OFFSET,
71 * and store it in *RETP. Returns 0 on success or a negative value if
72 * there's not enough data. */
73int elf_read_u16(Elf_Data *data, size_t offset, uint16_t *retp);
74int elf_read_u32(Elf_Data *data, size_t offset, uint32_t *retp);
75int elf_read_u64(Elf_Data *data, size_t offset, uint64_t *retp);
76
77
Zachary T Welch3ba522f2010-12-14 15:12:47 -080078#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
Juan Cespedes1cd999a2001-07-03 00:46:04 +020086#endif