blob: e8fb4606e0bdf9713515776cc330af2a13dc8adc [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
Edgar E. Iglesias35b5be72012-10-09 14:22:31 +020059/* The base implementation of backend.h (arch_get_sym_info).
60 * See backend.h for details. */
61int elf_get_sym_info(struct ltelf *lte, const char *filename,
62 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym);
63
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 Machata5b3e26a2012-04-30 20:53:22 +020069int elf_get_section_named(struct ltelf *lte, const char *name,
70 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
Petr Machatae67635d2012-03-21 03:37:39 +010071
72/* Read, respectively, 2, 4, or 8 bytes from Elf data at given OFFSET,
73 * and store it in *RETP. Returns 0 on success or a negative value if
74 * there's not enough data. */
Petr Machata3a01cd72012-04-30 20:50:20 +020075int elf_read_u16(Elf_Data *data, GElf_Xword offset, uint16_t *retp);
76int elf_read_u32(Elf_Data *data, GElf_Xword offset, uint32_t *retp);
77int elf_read_u64(Elf_Data *data, GElf_Xword offset, uint64_t *retp);
Petr Machatae67635d2012-03-21 03:37:39 +010078
Zachary T Welch3ba522f2010-12-14 15:12:47 -080079#if __WORDSIZE == 32
80#define PRI_ELF_ADDR PRIx32
81#define GELF_ADDR_CAST(x) (void *)(uint32_t)(x)
82#else
83#define PRI_ELF_ADDR PRIx64
84#define GELF_ADDR_CAST(x) (void *)(x)
85#endif
86
Juan Cespedes1cd999a2001-07-03 00:46:04 +020087#endif