ltrace-elf.h revision 673ff510953b65b844a58478aa434120f457c014
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2006,2010,2012,2013 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Zachary T Welch
5 * Copyright (C) 2001,2004,2007,2009 Juan Cespedes
6 * Copyright (C) 2006 Ian Wienand
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24#ifndef LTRACE_ELF_H
25#define LTRACE_ELF_H
26
27#include <gelf.h>
28#include <stdlib.h>
29#include <callback.h>
30
31#include "forward.h"
32#include "sysdep.h"
33#include "vect.h"
34
35/* XXX Ok, the original idea was to separate the low-level ELF data
36 * from the abstract "struct library" object, but we use some of the
37 * following extensively in the back end.  Not all though.  So what we
38 * use should be move to struct library, and the rest of this
39 * structure maybe could be safely hidden in .c.  How to integrate the
40 * arch-specific bits into struct library is unclear as of now.  */
41struct ltelf {
42	int fd;
43	Elf *elf;
44	GElf_Ehdr ehdr;
45	Elf_Data *dynsym;
46	size_t dynsym_count;
47	const char *dynstr;
48	GElf_Addr plt_addr;
49	GElf_Word plt_flags;
50	size_t plt_size;
51	Elf_Data *plt_data;
52
53	/* Vector of GElf_Rela with PLT relocations.  */
54	struct vect plt_relocs;
55
56	Elf_Data *symtab;
57	const char *strtab;
58	const char *soname;
59	size_t symtab_count;
60	Elf_Data *opd;
61	GElf_Addr *opd_addr;
62	size_t opd_size;
63	GElf_Addr dyn_addr;
64	size_t dyn_sz;
65	GElf_Addr bias;
66	GElf_Addr entry_addr;
67	GElf_Addr base_addr;
68	struct arch_ltelf_data arch;
69};
70
71int ltelf_init(struct ltelf *lte, const char *filename);
72void ltelf_destroy(struct ltelf *lte);
73
74/* XXX is it possible to put breakpoints in VDSO and VSYSCALL
75 * pseudo-libraries?  For now we assume that all libraries can be
76 * opened via a filesystem.  BASE is ignored for ET_EXEC files.  */
77int ltelf_read_library(struct library *lib, struct process *proc,
78		       const char *filename, GElf_Addr bias);
79
80/* Create a library object representing the main binary.  The entry
81 * point address is stored to *ENTRYP.  */
82struct library *ltelf_read_main_binary(struct process *proc, const char *path);
83
84/* Create a default PLT entry.  This can be used instead (or in
85 * addition to) returning PLT_DEFAULT from arch_elf_add_plt_entry.
86 * RET shall be initialized, the created symbol will be added to the
87 * beginning of the linked list at *RET.  This function doesn't add
88 * the symbol to LTE.  arch_elf_add_plt_entry has the chance to adjust
89 * symbol internals to its liking, and then return either PLT_DEFAULT
90 * or PLT_OK.  */
91int default_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
92			      const char *a_name, GElf_Rela *rela, size_t ndx,
93			      struct library_symbol **ret);
94
95Elf_Data *elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr);
96
97/* The following three look for sections based on various criteria.
98 * They return 0 if there was no error, or a negative value if there
99 * was.  If the section was found, it is returned in *TGT_SEC, and the
100 * header is stored to TGT_SHDR.  If it wasn't found, *TGT_SEC is set
101 * to NULL.  */
102int elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
103			     Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
104int elf_get_section_type(struct ltelf *lte, GElf_Word type,
105			 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
106int elf_get_section_named(struct ltelf *lte, const char *name,
107			  Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr);
108
109/* Iterate through all symbols in LTE.  See callback.h for notes on
110 * iteration interfaces.  START_AFTER is 0 in initial call.  */
111struct elf_each_symbol_t {
112	unsigned restart;
113	int status;
114} elf_each_symbol(struct ltelf *lte, unsigned start_after,
115		  enum callback_status (*cb)(GElf_Sym *symbol,
116					     const char *name,
117					     void *data),
118		  void *data);
119
120/* Read, respectively, 1, 2, 4, or 8 bytes from Elf data at given
121 * OFFSET, and store it in *RETP.  Returns 0 on success or a negative
122 * value if there's not enough data.  */
123int elf_read_u8(Elf_Data *data, GElf_Xword offset, uint8_t *retp);
124int elf_read_u16(Elf_Data *data, GElf_Xword offset, uint16_t *retp);
125int elf_read_u32(Elf_Data *data, GElf_Xword offset, uint32_t *retp);
126int elf_read_u64(Elf_Data *data, GElf_Xword offset, uint64_t *retp);
127
128/* Read at most 64-bit quantity recorded in an ULEB128 variable-length
129 * encoding.  */
130int elf_read_uleb128(Elf_Data *data, GElf_Xword offset, uint64_t *retp);
131
132/* These are same as above, but update *OFFSET with the width
133 * of read datum.  */
134int elf_read_next_u8(Elf_Data *data, GElf_Xword *offset, uint8_t *retp);
135int elf_read_next_u16(Elf_Data *data, GElf_Xword *offset, uint16_t *retp);
136int elf_read_next_u32(Elf_Data *data, GElf_Xword *offset, uint32_t *retp);
137int elf_read_next_u64(Elf_Data *data, GElf_Xword *offset, uint64_t *retp);
138int elf_read_next_uleb128(Elf_Data *data, GElf_Xword *offset, uint64_t *retp);
139
140/* Return whether there's AMOUNT more bytes after OFFSET in DATA.  */
141int elf_can_read_next(Elf_Data *data, GElf_Xword offset, GElf_Xword amount);
142
143#if __WORDSIZE == 32
144#define PRI_ELF_ADDR		PRIx32
145#define GELF_ADDR_CAST(x)	(void *)(uint32_t)(x)
146#else
147#define PRI_ELF_ADDR		PRIx64
148#define GELF_ADDR_CAST(x)	(void *)(x)
149#endif
150
151#endif
152