summaryrefslogtreecommitdiff
path: root/usr.bin/pmdb/aout_syms.c
blob: ef9f3e4d4796b49abb2b9df0c7030c251c04331a (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
/*	$OpenBSD: aout_syms.c,v 1.11 2007/07/24 21:11:02 deraadt Exp $	*/
/*
 * Copyright (c) 2002 Federico Schwindt <fgsch@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 <sys/param.h>
#include <sys/ptrace.h>
#include <sys/mman.h>
#include <sys/stat.h>

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

#include <a.out.h>
#include <link.h>

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

struct aout_symbol_handle {
	struct sym_table	ash_st;
	int		ash_fd;
	char	       *ash_strtab;
	u_int32_t	ash_strsize;
	struct nlist   *ash_symtab;
	int		ash_symsize;
};

#define ASH_TO_ST(ash) (&(ash)->ash_st)
#define ST_TO_ASH(st) ((struct aout_symbol_handle *)(st))

struct sym_table *aout_open(const char *);
void aout_close(struct sym_table *);
char *aout_name_and_off(struct sym_table *, reg, reg *);
int aout_lookup(struct pstate *, const char *, reg *);
void aout_update(struct pstate *);

struct sym_ops aout_sops = {
	aout_open,
	aout_close,
	aout_name_and_off,
	aout_lookup,
	aout_update
};	

int
sym_check_aout(const char *name, struct pstate *ps)
{
	struct exec ahdr;
	int error = 0;
	int fd;

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

	if (pread(fd, &ahdr, sizeof(ahdr), 0) != sizeof(ahdr)) {
		error = 1;
	}

	if (!error && N_BADMAG(ahdr)) {
		error = 1;
	}

	close(fd);

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

	return (error);
}

struct sym_table *
aout_open(const char *name)
{
	struct aout_symbol_handle *ash;
	struct stat sb;
	u_int32_t symoff, stroff;
	struct exec ahdr;

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

	memset(ash, 0, sizeof(*ash));
	ash->ash_fd = -1;

	if ((ash->ash_fd = open(name, O_RDONLY)) < 0) {
		warn("open(%s)", name);
		goto fail;
	}

	if (pread(ash->ash_fd, &ahdr, sizeof(ahdr), 0) != sizeof(ahdr)) {
		warn("pread(header)");
		goto fail;
	}

	if (N_BADMAG(ahdr)) {
		warnx("Bad magic.");
		goto fail;
	}

	/* Don't go further for stripped files. */
	if (fstat(ash->ash_fd, &sb) < 0 || N_SYMOFF(ahdr) == sb.st_size ||
	    N_STROFF(ahdr) == sb.st_size)
		goto fail;

	symoff = N_SYMOFF(ahdr);
	ash->ash_symsize = ahdr.a_syms;
	stroff = N_STROFF(ahdr);

	if (ahdr.a_syms == 0) {
		warnx("No symbol table");
		goto fail;
	}

	if (pread(ash->ash_fd, &ash->ash_strsize, sizeof(u_int32_t),
	    stroff) != sizeof(u_int32_t)) {
		warn("pread(strsize)");
		goto fail;
	}

	if ((ash->ash_strtab = mmap(NULL, ash->ash_strsize, PROT_READ,
	    MAP_SHARED, ash->ash_fd, stroff)) == MAP_FAILED) {
		warn("mmap(strtab)");
		goto fail;
	}

	if ((ash->ash_symtab = mmap(NULL, ash->ash_symsize, PROT_READ,
	    MAP_SHARED, ash->ash_fd, symoff)) == MAP_FAILED) {
		warn("mmap(symtab)");
		goto fail;
	}

	return (ASH_TO_ST(ash));
fail:

	aout_close(ASH_TO_ST(ash));
	return (NULL);
}

void
aout_close(struct sym_table *st)
{
	struct aout_symbol_handle *ash = ST_TO_ASH(st);

	if (ash->ash_fd != -1)
		close(ash->ash_fd);

	munmap(ash->ash_strtab, ash->ash_strsize);
	munmap(ash->ash_symtab, ash->ash_symsize);
	free(ash);
}

char *
aout_name_and_off(struct sym_table *st, reg pc, reg *offs)
{
	struct aout_symbol_handle *ash = ST_TO_ASH(st);
	struct nlist *s, *bests = NULL;
	int bestoff = 0;
	int nsyms, i;
	char *symn;

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

	nsyms = ash->ash_symsize / sizeof(struct nlist);

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

		if (s->n_value == 0 ||
		    s->n_un.n_strx == 0)
			continue;

		symn = &ash->ash_strtab[s->n_un.n_strx];
		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 &ash->ash_strtab[s->n_un.n_strx];
}

static struct nlist *
aout_lookup_table(struct aout_symbol_handle *ash, const char *name)
{
	int nsyms, i;
	char *symn;
	struct nlist *s = NULL;

	nsyms = ash->ash_symsize / sizeof(struct nlist);
	for (i = 0; i < nsyms; i++) {
		s = &ash->ash_symtab[i];
		symn = &ash->ash_strtab[s->n_un.n_strx];
		if (strcmp(name, symn) == 0)
			break;
	}

	if (i == nsyms)
		return (NULL);

	return (s);
}

int
aout_lookup(struct pstate *ps, const char *name, reg *res)
{
	struct sym_table *st;
	struct nlist *s = NULL;
	int first = 1;
	char *sname = (char *)name;

restart:
	TAILQ_FOREACH(st, &ps->ps_syms, st_list) {
		if ((s = aout_lookup_table(ST_TO_ASH(st), sname)) != NULL)
			break;
	}

	if (!first)
		free(sname);

	if (s == NULL) {
		if (first) {
			if (asprintf(&sname, "_%s", sname) != -1) {
				first = 0;
				goto restart;
			}
		}

		return (-1);
	}

	*res = s->n_value + st->st_offs;
	return (0);
}

/*
 * Called after execution started so that we can load any dynamic symbols.
 */
void
aout_update(struct pstate *ps)
{
	struct _dynamic dyn;
	struct so_debug sdeb;
	struct section_dispatch_table sdt;
	struct so_map som;
	off_t somp;
	reg addr;
	struct nlist *s;

	if ((s = aout_lookup_table(ST_TO_ASH(ps->ps_sym_exe), "__DYNAMIC")) == NULL) {
		warnx("Can't find __DYNAMIC");
		return;
	}
	addr = s->n_value + ps->ps_sym_exe->st_offs;

	if (process_read(ps, addr, &dyn, sizeof(dyn)) < 0) {
		warn("Can't read __DYNAMIC");
		return;
	}

	if (dyn.d_version != LD_VERSION_BSD) {
		warn("Can't handle __DYNAMIC version %d", dyn.d_version);
		return;
	}

	if (process_read(ps, (off_t)(reg)dyn.d_debug, &sdeb, sizeof(sdeb)) < 0) {
		warn("Can't read __DYNAMIC.d_debug");
		return;
	}

	if (sdeb.dd_version != 0) {
		warn("Can't handle so_debug version %d", sdeb.dd_version);
		return;
	}

	if (process_read(ps, (off_t)(reg)dyn.d_un.d_sdt, &sdt, sizeof(sdt)) < 0) {
		warn("Can't read section dispatch table");
		return;
	}

	somp = (off_t)(reg)sdt.sdt_loaded;
	while (somp) {
		char fname[MAXPATHLEN];
		int i;

		if (process_read(ps, somp, &som, sizeof(som)) < 0) {
			warn("Can't read so_map");
			break;
		}
		somp = (off_t)(reg)som.som_next;
		if (process_read(ps, (off_t)(reg)som.som_path, fname,
		    sizeof(fname)) < 0) {
			warn("Can't read so filename");
			continue;
		}
		
		/* sanity check the file name */
		for (i = 0; i < MAXPATHLEN; i++)
			if (fname[i] == '\0')
				break;
		if (i == MAXPATHLEN) {
			warnx("so filename invalid");
			continue;
		}

		if (st_open(ps, fname, (reg)som.som_addr) == NULL)
			warn("symbol loading failed");
	}
}