blob: b8ad53f32e38cee35e42c03a192abc7c5c370e00 [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;
Petr Machata18c801c2012-04-07 01:24:08 +020025 GElf_Word plt_flags;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010026 size_t plt_size;
27 Elf_Data *relplt;
Petr Machatae67635d2012-03-21 03:37:39 +010028 Elf_Data *plt_data;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010029 size_t relplt_count;
30 Elf_Data *symtab;
31 const char *strtab;
Petr Machata2b46cfc2012-02-18 11:17:29 +010032 const char *soname;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010033 size_t symtab_count;
34 Elf_Data *opd;
35 GElf_Addr *opd_addr;
36 size_t opd_size;
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
Petr Machata2b46cfc2012-02-18 11:17:29 +010046int open_elf(struct ltelf *lte, const char *filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020047
Petr Machata2b46cfc2012-02-18 11:17:29 +010048/* XXX is it possible to put breakpoints in VDSO and VSYSCALL
49 * pseudo-libraries? For now we assume that all libraries can be
50 * opened via a filesystem. BASE is ignored for ET_EXEC files. */
Petr Machatab5f80ac2012-04-04 01:46:18 +020051int ltelf_read_library(struct library *lib, struct Process *proc,
52 const char *filename, GElf_Addr bias);
Juan Cespedesd914a202004-11-10 00:15:33 +010053
Petr Machata2b46cfc2012-02-18 11:17:29 +010054/* Create a library object representing the main binary. The entry
55 * point address is stored to *ENTRYP. */
56struct library *ltelf_read_main_binary(struct Process *proc, const char *path);
Juan Cespedes96935a91997-08-09 23:45:39 +020057
Petr Machata2b46cfc2012-02-18 11:17:29 +010058GElf_Addr arch_plt_sym_val(struct ltelf *, size_t, GElf_Rela *);
Zachary T Welch3ba522f2010-12-14 15:12:47 -080059
Petr Machatae67635d2012-03-21 03:37:39 +010060Elf_Data *elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr);
61int elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
62 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
Petr Machataffd5aab2012-03-24 02:03:33 +010063int elf_get_section_type(struct ltelf *lte, GElf_Word type,
64 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
Petr Machatae67635d2012-03-21 03:37:39 +010065
66/* Read, respectively, 2, 4, or 8 bytes from Elf data at given OFFSET,
67 * and store it in *RETP. Returns 0 on success or a negative value if
68 * there's not enough data. */
69int elf_read_u16(Elf_Data *data, size_t offset, uint16_t *retp);
70int elf_read_u32(Elf_Data *data, size_t offset, uint32_t *retp);
71int elf_read_u64(Elf_Data *data, size_t offset, uint64_t *retp);
72
Petr Machatacf26d592012-04-17 05:18:31 +020073int default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
74 const char *a_name, GElf_Rela *rela, size_t ndx,
75 struct library_symbol **ret);
Petr Machatae67635d2012-03-21 03:37:39 +010076
Zachary T Welch3ba522f2010-12-14 15:12:47 -080077#if __WORDSIZE == 32
78#define PRI_ELF_ADDR PRIx32
79#define GELF_ADDR_CAST(x) (void *)(uint32_t)(x)
80#else
81#define PRI_ELF_ADDR PRIx64
82#define GELF_ADDR_CAST(x) (void *)(x)
83#endif
84
Juan Cespedes1cd999a2001-07-03 00:46:04 +020085#endif