blob: dec072bba49f61cc30c831b6976ba21a21ddc4b4 [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;
Petr Machata16fabd02012-04-20 02:37:40 +020010struct library_symbol;
Petr Machata2b46cfc2012-02-18 11:17:29 +010011
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. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010018struct 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;
Petr Machata18c801c2012-04-07 01:24:08 +020026 GElf_Word plt_flags;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010027 size_t plt_size;
28 Elf_Data *relplt;
Petr Machatae67635d2012-03-21 03:37:39 +010029 Elf_Data *plt_data;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010030 size_t relplt_count;
31 Elf_Data *symtab;
32 const char *strtab;
Petr Machata2b46cfc2012-02-18 11:17:29 +010033 const char *soname;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010034 size_t symtab_count;
35 Elf_Data *opd;
36 GElf_Addr *opd_addr;
37 size_t opd_size;
Joe Damato87f4f582010-11-08 15:47:36 -080038 GElf_Addr dyn_addr;
39 size_t dyn_sz;
Petr Machatae67635d2012-03-21 03:37:39 +010040 size_t relplt_size;
Petr Machata29add4f2012-02-18 16:38:05 +010041 GElf_Addr bias;
Petr Machata2b46cfc2012-02-18 11:17:29 +010042 GElf_Addr entry_addr;
Petr Machata29add4f2012-02-18 16:38:05 +010043 GElf_Addr base_addr;
Petr Machatae67635d2012-03-21 03:37:39 +010044 struct arch_ltelf_data arch;
Juan Cespedes1cd999a2001-07-03 00:46:04 +020045};
46
Petr Machata2b46cfc2012-02-18 11:17:29 +010047int open_elf(struct ltelf *lte, const char *filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020048
Petr Machata2b46cfc2012-02-18 11:17:29 +010049/* 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. */
Petr Machatab5f80ac2012-04-04 01:46:18 +020052int ltelf_read_library(struct library *lib, struct Process *proc,
53 const char *filename, GElf_Addr bias);
Juan Cespedesd914a202004-11-10 00:15:33 +010054
Petr Machata2b46cfc2012-02-18 11:17:29 +010055/* 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);
Juan Cespedes96935a91997-08-09 23:45:39 +020058
Petr Machata2b46cfc2012-02-18 11:17:29 +010059GElf_Addr arch_plt_sym_val(struct ltelf *, size_t, GElf_Rela *);
Zachary T Welch3ba522f2010-12-14 15:12:47 -080060
Petr Machatae67635d2012-03-21 03:37:39 +010061Elf_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);
Petr Machataffd5aab2012-03-24 02:03:33 +010064int elf_get_section_type(struct ltelf *lte, GElf_Word type,
65 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
Petr Machatae67635d2012-03-21 03:37:39 +010066
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, size_t offset, uint16_t *retp);
71int elf_read_u32(Elf_Data *data, size_t offset, uint32_t *retp);
72int elf_read_u64(Elf_Data *data, size_t offset, uint64_t *retp);
73
Petr Machatacf26d592012-04-17 05:18:31 +020074int 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);
Petr Machatae67635d2012-03-21 03:37:39 +010077
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