aboutsummaryrefslogtreecommitdiff
path: root/elf.c
blob: d1fb6ca03ce5420842cec90aa06a0e4c5875a970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#if HAVE_CONFIG_H
#include "config.h"
#endif

/*
 * This file contains functions specific to ELF binaries
 *
 * Silvio Cesare <silvio@big.net.au>
 *
 */

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <unistd.h>

#include "ltrace.h"
#include "elf.h"
#include "debug.h"

static void do_init_elf(struct ltelf *lte, const char *filename);
static void do_close_elf(struct ltelf *lte);
static void do_load_elf_symtab(struct ltelf *lte);
static void do_init_load_libraries(void);
static void do_close_load_libraries(void);
static int in_load_libraries(const char *func);
static void add_library_symbol(
	struct ltelf *lte,
	int i,
	struct library_symbol **library_symbolspp
);

static struct ltelf library_lte[MAX_LIBRARY];

static void
do_init_elf(struct ltelf *lte, const char *filename) {
	struct stat sbuf;

	debug(1, "Reading ELF from %s...", filename);

	lte->fd = open(filename, O_RDONLY);
	if (lte->fd == -1) {
		fprintf(
			stderr,
			"Can't open \"%s\": %s\n",
			filename,
			strerror(errno)
		);
		exit(1);
	}
	if (fstat(lte->fd, &sbuf) == -1) {
		fprintf(
			stderr,
			"Can't stat \"%s\": %s\n",
			filename,
			strerror(errno)
		);
		exit(1);
	}
	if (sbuf.st_size < sizeof(Elf32_Ehdr)) {
		fprintf(
			stderr,
			"\"%s\" is not an ELF binary object\n",
			filename
		);
		exit(1);
	}
	lte->maddr = mmap(
		NULL, sbuf.st_size, PROT_READ, MAP_SHARED, lte->fd, 0
	);
	if (lte->maddr == (void*)-1) {
		fprintf(
			stderr,
			"Can't mmap \"%s\": %s\n",
			filename,
			strerror(errno)
		);
		exit(1);
	}

	lte->ehdr = lte->maddr;

	if (strncmp(lte->ehdr->e_ident, ELFMAG, SELFMAG)) {
		fprintf(
			stderr,
			"\"%s\" is not an ELF binary object\n",
			filename
		);
		exit(1);
	}

/*
	more ELF checks should go here - the e_arch/e_machine fields in the
	ELF header are specific to each architecture. perhaps move some code
	into sysdeps (have check_ehdr_arch) - silvio
*/

	lte->strtab = NULL;

	lte->symtab = NULL;
	lte->symtab_len = 0;
}

static void
do_close_elf(struct ltelf *lte) {
	close(lte->fd);
}

static void
do_load_elf_symtab(struct ltelf *lte) {
	void *maddr = lte->maddr;
	Elf32_Ehdr *ehdr = lte->ehdr;
	Elf32_Shdr *shdr = (Elf32_Shdr *)(maddr + ehdr->e_shoff);
	int i;

/*
	an ELF object should only ever one dynamic symbol section (DYNSYM), but
	can have multiple string tables.  the sh_link entry from DYNSYM points
	to the correct STRTAB section - silvio
*/

	for(i = 0; i < ehdr->e_shnum; i++) {
		if (shdr[i].sh_type == SHT_DYNSYM) {
			lte->symtab = (Elf32_Sym *)(maddr + shdr[i].sh_offset);
			lte->symtab_len = shdr[i].sh_size;
			lte->strtab = (char *)(
				maddr + shdr[shdr[i].sh_link].sh_offset
			);
		}
	}

	debug(2, "symtab: 0x%08x", (unsigned)lte->symtab);
	debug(2, "symtab_len: %lu", lte->symtab_len);
	debug(2, "strtab: 0x%08x", (unsigned)lte->strtab);
}

static void
add_library_symbol(
		struct ltelf *lte,
		int i,
		struct library_symbol **library_symbolspp) {
	struct library_symbol *tmp = *library_symbolspp;
	struct library_symbol *library_symbols;

	*library_symbolspp = (struct library_symbol *)malloc(
		sizeof(struct library_symbol)
	);
	library_symbols = *library_symbolspp;
	if (library_symbols == NULL) {
		perror("ltrace: malloc");
		exit(1);
	}

	library_symbols->enter_addr = (void *)lte->symtab[i].st_value;
	library_symbols->name = &lte->strtab[lte->symtab[i].st_name];
	library_symbols->next = tmp;

	debug(2, "addr: 0x%08x, symbol: \"%s\"",
			(unsigned)lte->symtab[i].st_value,
			&lte->strtab[lte->symtab[i].st_name]);
}

/*
	this is all pretty slow. perhaps using .hash would be faster, or
	even just a custum built hash table. its all initialization though,
	so its not that bad - silvio
*/

static void
do_init_load_libraries(void) {
	int i;

	for (i = 0; i < library_num; i++) {
		do_init_elf(&library_lte[i], library[i]);
		do_load_elf_symtab(&library_lte[i]);
	}
}

static void
do_close_load_libraries(void) {
	int i;

	for (i = 0; i < library_num; i++) {
		do_close_elf(&library_lte[i]);
	}
}

static int
in_load_libraries(const char *func) {
	int i, j;
/*
	if no libraries are specified, assume we want all
*/
	if (library_num == 0) return 1;

	for (i = 0; i < library_num; i++) {
		Elf32_Sym *symtab = library_lte[i].symtab;
		char *strtab = library_lte[i].strtab;

		for(
			j = 0;
			j < library_lte[i].symtab_len / sizeof(Elf32_Sym);
			j++
		) {
			if (
				symtab[j].st_value &&
				!strcmp(func, &strtab[symtab[j].st_name])
			) return 1;
		}
	}
	return 0;
}

/*
	this is the main function
*/

struct library_symbol *
read_elf(const char *filename) {
	struct library_symbol *library_symbols = NULL;
	struct ltelf lte;
	int i;

	do_init_elf(&lte, filename);
	do_load_elf_symtab(&lte);
	do_init_load_libraries();

	for(i = 0; i < lte.symtab_len / sizeof(Elf32_Sym); i++) {
		Elf32_Sym *symtab = lte.symtab;
		char *strtab = lte.strtab;

		if (!symtab[i].st_shndx && symtab[i].st_value) {
			if (in_load_libraries(&strtab[symtab[i].st_name])) {
				add_library_symbol(&lte, i, &library_symbols);
			}
		}
	}

	do_close_load_libraries();
	do_close_elf(&lte);

	return library_symbols;
}