summaryrefslogtreecommitdiff
path: root/usr.bin/pmdb/elf_syms.c
blob: 57cc27222eac95fa6224b975361f2fe14eb8ceb4 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*	$OpenBSD: elf_syms.c,v 1.9 2007/07/24 21:08:49 deraadt Exp $	*/
/*
 * Copyright (c) 2002 Artur Grabowski <art@openbsd.org>
 * All rights reserved. 
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met: 
 *
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <err.h>

#include <sys/param.h>
#include <sys/ptrace.h>
#include <sys/mman.h>

#include <nlist.h>
#ifdef __NetBSD__
#include <machine/elf_machdep.h>
#define ELFSIZE ARCH_ELFSIZE
#include <sys/exec_elf.h>
#else
#include <elf_abi.h>
#endif
#include <link.h>

#include "pmdb.h"
#include "symbol.h"
#include "break.h"

struct elf_symbol_handle {
	struct sym_table esh_st;
	int		esh_fd;
	char 		*esh_strtab;
	Elf_Word	esh_strsize;
	Elf_Sym 	*esh_symtab;
	Elf_Word	esh_symsize;
};

#define ESH_TO_ST(esh) (&(esh)->esh_st)
#define ST_TO_ESH(st) ((struct elf_symbol_handle *)(st))

struct sym_table *elf_open(const char *);
void elf_close(struct sym_table *);
char *elf_name_and_off(struct sym_table *, reg, reg *);
int elf_lookup(struct pstate *, const char *, reg *);
void elf_update(struct pstate *);

struct sym_ops elf_sops = {
	elf_open,
	elf_close,
	elf_name_and_off,
	elf_lookup,
	elf_update
};	

int
sym_check_elf(const char *name, struct pstate *ps)
{
	Elf_Ehdr ehdr;
	int error = 0;
	int fd;

	if ((fd = open(name, O_RDONLY)) < 0)
		return (1);

	if (pread(fd, &ehdr, sizeof(Elf_Ehdr), 0) != sizeof(Elf_Ehdr))
		error = 1;

#ifndef __NetBSD__
	if (!error && (!IS_ELF(ehdr) ||
	    ehdr.e_ident[EI_CLASS] != ELF_TARG_CLASS ||
	    ehdr.e_ident[EI_DATA] != ELF_TARG_DATA ||
	    ehdr.e_ident[EI_VERSION] != ELF_TARG_VER ||
	    ehdr.e_machine != ELF_TARG_MACH ||
	    ehdr.e_version != ELF_TARG_VER))
		error = 1;
#endif

	close(fd);

	if (!error)
		ps->ps_sops = &elf_sops;

	return (error);
}

struct sym_table *
elf_open(const char *name)
{
	struct elf_symbol_handle *esh;
	Elf_Off symoff, stroff;
	Elf_Ehdr ehdr;
	Elf_Shdr *shdr;
	int i, fd;

	/* Just a sanity check */
	if (sizeof(reg) != sizeof(Elf_Addr))
		errx(1, "sym_open: sizeof(reg) != sizeof(Elf_Addr)");

	if ((esh = malloc(sizeof(*esh))) == NULL) {
		return (NULL);
	}

	memset(esh, 0, sizeof(*esh));
	esh->esh_fd = -1;

	if ((fd = esh->esh_fd = open(name, O_RDONLY)) < 0) {
		goto fail;
	}

	if (pread(fd, &ehdr, sizeof(Elf_Ehdr), 0) != sizeof(Elf_Ehdr)) {
		goto fail;
	}
#ifndef __NetBSD__
	if (!IS_ELF(ehdr) ||
	    ehdr.e_ident[EI_CLASS] != ELF_TARG_CLASS ||
	    ehdr.e_ident[EI_DATA] != ELF_TARG_DATA ||
	    ehdr.e_ident[EI_VERSION] != ELF_TARG_VER ||
	    ehdr.e_machine != ELF_TARG_MACH ||
	    ehdr.e_version != ELF_TARG_VER) {
		goto fail;
	}
#endif

	if ((shdr = (Elf_Shdr *)mmap(NULL, ehdr.e_shentsize * ehdr.e_shnum,
	    PROT_READ, MAP_SHARED, fd, ehdr.e_shoff)) == MAP_FAILED) {
		goto fail;
	}

	for (i = 0; i < ehdr.e_shnum; i++) {
		if (shdr[i].sh_type == SHT_SYMTAB) {
			symoff = shdr[i].sh_offset;
			esh->esh_symsize = shdr[i].sh_size;
			stroff = shdr[shdr[i].sh_link].sh_offset;
			esh->esh_strsize = shdr[shdr[i].sh_link].sh_size;
			break;
		}
	}

	munmap(shdr, ehdr.e_shentsize * ehdr.e_shnum);

	if (i == ehdr.e_shnum) {
		goto fail;
	}

	if (esh->esh_symsize) {

		if ((esh->esh_strtab = mmap(NULL, esh->esh_strsize, PROT_READ,
		    MAP_SHARED, fd, stroff)) == MAP_FAILED) {
			goto fail;
		}

		if ((esh->esh_symtab = mmap(NULL, esh->esh_symsize, PROT_READ,
		    MAP_SHARED, fd, symoff)) == MAP_FAILED) {
			goto fail;
		}
	}

	return (ESH_TO_ST(esh));
fail:

	elf_close(ESH_TO_ST(esh));
	return (NULL);
}

void
elf_close(struct sym_table *st)
{
	struct elf_symbol_handle *esh = ST_TO_ESH(st);

	if (esh->esh_fd != -1)
		close(esh->esh_fd);

	munmap(esh->esh_strtab, esh->esh_strsize);
	munmap(esh->esh_symtab, esh->esh_symsize);
	free(esh);
}

char *
elf_name_and_off(struct sym_table *st, reg pc, reg *offs)
{
	struct elf_symbol_handle *esh = ST_TO_ESH(st);
	Elf_Sym *s, *bests = NULL;
	Elf_Addr bestoff = 0;
	int nsyms, i;
	char *symn;

	if (esh == NULL)
		return (NULL);

#define SYMVAL(S) (unsigned long)((S)->st_value + st->st_offs)

	nsyms = esh->esh_symsize / sizeof(Elf_Sym);

	bests = NULL;
	for (i = 0; i < nsyms; i++) {
		s = &esh->esh_symtab[i];

		if (s->st_value == 0 ||
		    s->st_shndx == 0 ||
		    (ELF_ST_BIND(s->st_info) != STB_GLOBAL &&
		     ELF_ST_BIND(s->st_info) != STB_WEAK &&
		     ELF_ST_BIND(s->st_info) != STB_LOCAL))
			continue;
		symn = &esh->esh_strtab[s->st_name];
		if (SYMVAL(s) <= pc && SYMVAL(s) > bestoff &&
		    symn[0] != '\0' && strcmp(symn, "gcc2_compiled.")) {
			bests = s;
			bestoff = SYMVAL(s);
		}
	}

	if ((s = bests) == NULL)
		return (NULL);

	*offs = pc - SYMVAL(s);

	return &esh->esh_strtab[s->st_name];
}

static Elf_Sym *
elf_lookup_table(struct elf_symbol_handle *esh, const char *name)
{
	int nsyms, i;
	char *symn;
	Elf_Sym *s = NULL;

	if (esh == NULL)
		return (NULL);

	/* XXX - dumb, doesn't follow the rules (weak, hash, etc.). */
	nsyms = esh->esh_symsize / sizeof(Elf_Sym);
	for (i = 0; i < nsyms; i++) {
		s = &esh->esh_symtab[i];
		symn = &esh->esh_strtab[s->st_name];
		if (strcmp(name, symn) == 0)
			break;
	}
	if (i == nsyms)
		return (NULL);

	return (s);
}

int
elf_lookup(struct pstate *ps, const char *name, reg *res)
{
	struct sym_table *st;
	Elf_Sym *s = NULL;

	TAILQ_FOREACH(st, &ps->ps_syms, st_list) {
		if ((s = elf_lookup_table(ST_TO_ESH(st), name)) != NULL)
			break;
	}

	if (s != NULL) {
		*res = s->st_value + st->st_offs;
		return (0);
	}

	return (-1);
}

#ifndef __NetBSD__
struct elf_object_v1 {
	Elf_Addr load_addr;
	Elf_Addr load_offs;
	char *load_name;
	Elf_Dyn *load_dyn;
	struct elf_object_v1 *next;
	struct elf_object_v1 *prev;
	void *load_list;
	u_int32_t load_size;
	u_long info[DT_NUM + DT_PROCNUM];
	struct elf_object_v1 *dep_next;
	int status;
	Elf_Phdr *phdrp;
	int phdrc;
	int refcount;
	int obj_type;
#define EOBJ1_LDR 1
#define EOBJ1_EXE 2
#define EOBJ1_LIB 3
#define EOBJ1_DLO 4
};
#endif

/*
 * dlopen breakpoint (XXX make this generic?)
 */
int
sym_bkpt(struct pstate *ps, void *arg)
{
	fprintf(stderr, "pmdb: shared lib changed\n");

	sym_update(ps);

	return BKPT_KEEP_CONT;
}

/* Is the ABI really so daft that it doesn't include the linking offset? */
struct xlink_map {
	struct link_map lm;
	Elf_Addr a;
};

/*
 * Called after execution started so that we can load any dynamic symbols.
 */
void
elf_update(struct pstate *ps)
{
#ifndef __NetBSD__
	struct xlink_map xlm;
#define lm xlm.lm
	struct r_debug rdeb;
	reg addr;
	Elf_Dyn dyn;
	static int bkpt_set;
	Elf_Sym *s;

	if ((s = elf_lookup_table(ST_TO_ESH(ps->ps_sym_exe), "_DYNAMIC")) == NULL) {
		warnx("Can't find _DYNAMIC");
		return;
	}
	addr = s->st_value + ps->ps_sym_exe->st_offs;

	do {
		if (process_read(ps, addr, &dyn, sizeof(dyn)) < 0) {
			warnx("Can't read _DYNAMIC");
			return;
		}
		addr += sizeof(dyn);
	} while (dyn.d_tag != 0 && dyn.d_tag != DT_DEBUG);

	if (dyn.d_tag == 0) {
		warnx("Can't find DT_DEBUG");
		return;
	}

	if (process_read(ps, dyn.d_un.d_ptr, &rdeb, sizeof(rdeb)) < 0) {
		warnx("Can't read DT_DEBUG");
		return;
	}

	if (rdeb.r_version != 1) {
		warn("Can't handle debug map version %d", rdeb.r_version);
		return;
	}
	if (rdeb.r_state != RT_CONSISTENT) {
		warn("debug map not consistent: %d", rdeb.r_state);
		return;
	}

	if (!bkpt_set) {
		if (bkpt_add_cb(ps, rdeb.r_brk, sym_bkpt, NULL))
			warn("sym_exec: can't set bkpt");
		bkpt_set = 1;
	}

	addr = (Elf_Addr)rdeb.r_map;
	while (addr != 0 && addr != -1) {
		char fname[MAXPATHLEN];
		int i;

		if (process_read(ps, addr, &xlm, sizeof(xlm)) < 0) {
			warnx("Can't read symbols...");
			return;
		}

		addr = (Elf_Addr)lm.l_next;

		if (lm.l_name == NULL || lm.l_name == (char *)-1)
			continue;
		if (process_read(ps, (Elf_Addr)lm.l_name, fname,
		    sizeof(fname)) < 0) {
			warnx("Can't read symbols...");
			return;
		}

		/* sanity check the file name */
		for (i = 0; i < MAXPATHLEN; i++)
			if (fname[i] == '\0')
				break;
		if (i == MAXPATHLEN)
			continue;

		if (st_open(ps, fname, xlm.a) == NULL)
			warn("symbol loading failed");
	}
#endif
}