summaryrefslogtreecommitdiff
path: root/usr/src/ucblib/libucb/port/gen/nlist.c
blob: b99967e0ecc57366dacb2025b9ac817342c2327f (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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * University Copyright- Copyright (c) 1982, 1986, 1988
 * The Regents of the University of California
 * All Rights Reserved
 *
 * University Acknowledgment- Portions of this document are derived from
 * software developed by the University of California, Berkeley, and its
 * contributors.
 */

/*LINTLIBRARY*/

#include <sys/types.h>
#include "libelf.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <nlist.h>
#include <sys/file.h>
#include <string.h>

#if COFF_NLIST_SUPPORTED
#include "aouthdr.h"
#include "filehdr.h"
#include "scnhdr.h"
#include "reloc.h"
#endif /* COFF_NLIST_SUPPORTED */

#include "linenum.h"
#include "syms.h"

#undef  BADMAG
#define	BADMAG(x)	(!ISCOFF(x))

#ifndef FLEXNAMES
#define	FLEXNAMES 1
#endif
#undef n_name		/* this patch causes problems here */

#define	SPACE 100		/* number of symbols read at a time */
#define	ISELF (strncmp(magic_buf, ELFMAG, SELFMAG) == 0)

#if COFF_NLIST_SUPPORTED
static char sym_buf[SPACE * SYMESZ];
static int num_in_buf = 0;
static char *next_entry = (char *)0;
#endif /* COFF_NLIST_SUPPORTED */

static unsigned encode;		/* data encoding if an ELF file */
static unsigned fvers;		/* object file version if an ELF file */

/* forward declarations */
static int _elf_nlist(int, struct nlist *);
static int end_elf_job(int);
static Elf_Data *elf_read(int, long, size_t, size_t, Elf_Type);

#if COFF_NLIST_SUPPORTED
static int _coff_nlist(int, struct nlist *);
static void sym_close(int);
static int sym_read(int, struct syment *, int);
static int fill_sym_buf(int, int);
#endif /* COFF_NLIST_SUPPORTED */

int
nlist(const char *name, struct nlist *list)
{
	struct nlist *p;
	char magic_buf[EI_NIDENT+1];
	int fd;

	for (p = list; p->n_name && p->n_name[0]; p++) { /* n_name can be ptr */
		p->n_type = 0;
		p->n_value = 0L;
		p->n_scnum = 0;
		p->n_sclass = 0;
		p->n_numaux = 0;
	}

	if ((fd = open(name, 0)) < 0)
		return (-1);
	if (read(fd, magic_buf, (size_t)EI_NIDENT) == -1) {
		(void) close(fd);
		return (-1);
	}
	magic_buf[EI_NIDENT] = '\0';
	if (lseek(fd, 0L, 0) == -1L) { /* rewind to beginning of object file */
		(void) close(fd);
		return (-1);
	}

	if (ISELF) {
		/*
		 * right now can only handle 32-bit architectures
		 * XX64 work to come?  ELFCLASS64?
		 */
		if (magic_buf[EI_CLASS] != ELFCLASS32) {
			(void) close(fd);
			return (-1);
		}
		encode = (unsigned)magic_buf[EI_DATA];
		fvers = (unsigned)magic_buf[EI_VERSION];
		return (_elf_nlist(fd, list));
	}
	else
#if COFF_NLIST_SUPPORTED
		return (_coff_nlist(fd, list));
#else /* COFF_NLIST_SUPPORTED */
		return (-1);
#endif /* COFF_NLIST_SUPPORTED */
}

static int
_elf_nlist(int fd, struct nlist *list)
{
	Elf_Data   *symdata;	/* buffer points to symbol table */
	Elf_Data   *strdata;	/* buffer points to string table */
	Elf_Data   *secdata;	/* buffer points to section table */
	Elf32_Shdr *symhdr;	/* section table entry for symtab */
	Elf32_Shdr *strhdr;	/* section table entry for strtab */
	Elf32_Sym  *sym;	/* buffer storing one symbol information */
	Elf32_Sym  *sym_end;	/* end of symbol table */
	Elf32_Ehdr *ehdr;	/* file header */
	Elf_Data   *edata;	/* data buffer for ehdr */
	int	i;
	int	nreq;
	struct  nlist *inl;

	if (elf_version(EV_CURRENT) == EV_NONE)
		return (end_elf_job(fd));

	/* count the number of symbols requested */
	for (inl = list, nreq = 0; inl->n_name && inl->n_name[0]; ++inl, nreq++)
		;

	/* read file header and section header table */
	if ((edata = elf_read(fd, 0L, elf32_fsize(ELF_T_EHDR, 1, fvers),
		sizeof (Elf32_Ehdr), ELF_T_EHDR)) == 0)
		return (end_elf_job(fd));

	ehdr = (Elf32_Ehdr *)edata->d_buf;

	if (ehdr->e_shoff == 0) {
		free(edata->d_buf);
		free(edata);
		return (end_elf_job(fd));
	}

	if ((secdata = elf_read(fd, (long)ehdr->e_shoff,
		(size_t)(ehdr->e_shentsize * ehdr->e_shnum),
		(size_t)(ehdr->e_shnum * sizeof (Elf32_Ehdr)),
		ELF_T_SHDR)) == 0) {
		free(edata->d_buf);
		free(edata);
		return (end_elf_job(fd));
	}

	/* find symbol table section */
	symhdr = (Elf32_Shdr *)secdata->d_buf;
	for (i = 0; i < (Elf32_Word)ehdr->e_shnum; i++, symhdr++)
		if (symhdr->sh_type == SHT_SYMTAB)
			break;

	if ((symhdr->sh_type != SHT_SYMTAB) ||
		(symhdr->sh_link >= ehdr->e_shnum)) {
		free(secdata->d_buf);
		free(secdata);
		free(edata->d_buf);
		free(edata);
		return (end_elf_job(fd));
	}

	if ((symdata = elf_read(fd, (long)symhdr->sh_offset,
		(size_t)symhdr->sh_size,
		(size_t)((symhdr->sh_size / symhdr->sh_entsize) *
		sizeof (Elf32_Sym)), ELF_T_SYM)) == 0) {
		free(secdata->d_buf);
		free(secdata);
		free(edata->d_buf);
		free(edata);
		return (end_elf_job(fd));
	}

	/* read string table */
	strhdr = (Elf32_Shdr *)secdata->d_buf;
	strhdr = strhdr + symhdr->sh_link;

	if (strhdr->sh_type != SHT_STRTAB) {
		free(symdata->d_buf);
		free(symdata);
		free(secdata->d_buf);
		free(secdata);
		free(edata->d_buf);
		free(edata);
		return (end_elf_job(fd));
	}

	if ((strdata = elf_read(fd, strhdr->sh_offset, strhdr->sh_size,
		strhdr->sh_size, ELF_T_BYTE)) == 0) {
		free(symdata->d_buf);
		free(symdata);
		free(secdata->d_buf);
		free(secdata);
		free(edata->d_buf);
		free(edata);
		return (end_elf_job(fd));
	}

	((char *)strdata->d_buf)[0] = '\0';
	((char *)strdata->d_buf)[strhdr->sh_size-1] = '\0';

	sym = (Elf32_Sym *) (symdata->d_buf);
	sym_end = sym + symdata->d_size / sizeof (Elf32_Sym);
	for (; sym < sym_end; ++sym) {
		struct nlist *p;
		char *name;
		if (sym->st_name > strhdr->sh_size) {
			free(strdata->d_buf);
			free(strdata);
			free(symdata->d_buf);
			free(symdata);
			free(secdata->d_buf);
			free(secdata);
			free(edata->d_buf);
			free(edata);
			return (end_elf_job(fd));
		}
		name = (char *)strdata->d_buf + sym->st_name;
		if (name == 0)
			continue;
		for (p = list; p->n_name && p->n_name[0]; ++p) {
			if (strcmp(p->n_name, name)) {
				continue;
			}
			p->n_value = sym->st_value;
			p->n_type = ELF32_ST_TYPE(sym->st_info);
			p->n_scnum = sym->st_shndx;
			nreq--;
			break;
		}
	}
	/*
	 * Currently there is only one symbol table section
	 * in an object file, but this restriction may be
	 * relaxed in the future.
	 */
	(void) close(fd);
	free(secdata->d_buf);
	free(strdata->d_buf);
	free(symdata->d_buf);
	free(edata->d_buf);
	free(secdata);
	free(strdata);
	free(symdata);
	free(edata);
	return (nreq);
}

/*
 * allocate memory of size memsize and read size bytes
 * starting at offset from fd - the file data are
 * translated in place using the low-level libelf
 * translation routines
 */

static Elf_Data *
elf_read(int fd, long offset, size_t size, size_t memsize, Elf_Type dtype)
{
	Elf_Data *dsrc, *ddst;
	Elf_Data srcdata;
	size_t maxsize;
	char *p;

	dsrc = &srcdata;

	if (size == 0)
		return (0);

	if ((maxsize = memsize) < size)
		maxsize = size;


	if ((ddst = (Elf_Data *)malloc(sizeof (Elf_Data))) == 0)
		return (0);

	if ((p = malloc(maxsize)) == 0) {
		free(ddst);
		return (0);
	}

	if (lseek(fd, offset, 0L) == -1) {
		free(ddst);
		free(p);
		return (0);
	}

	if (read(fd, p, size) != size) {
		free(ddst);
		free(p);
		return (0);
	}

	dsrc->d_buf = p;
	dsrc->d_type = dtype;
	dsrc->d_size = size;
	dsrc->d_version = fvers;
	ddst->d_buf = p;
	ddst->d_size = memsize;
	ddst->d_version = EV_CURRENT;

	if (elf32_xlatetom(ddst, dsrc, encode) != ddst) {
		free(ddst);
		free(p);
		return (0);
	}

	return (ddst);
}

static int
end_elf_job(int fd)
{
	(void) close(fd);
	return (-1);
}

#if COFF_NLIST_SUPPORTED
static int
_coff_nlist(int fd, struct nlist *list)
{
	struct	filehdr	buf;
	struct	syment	sym;
	long n;
	int bufsiz = FILHSZ;
#if FLEXNAMES
	char *strtab = (char *)0;
	long strtablen;
#endif
	struct nlist *p, *inl;
	struct syment *q;
	long	sa;
	int 	nreq;

	if (read(fd, (char *)&buf, bufsiz) == -1) {
		(void) close(fd);
		return (-1);
	}

	if (BADMAG(buf.f_magic)) {
		(void) close(fd);
		return (-1);
	}
	sa = buf.f_symptr;	/* direct pointer to sym tab */
	if (lseek(fd, (long)sa, 0) == -1L) {
		(void) close(fd);
		return (-1);
	}
	q = &sym;
	n = buf.f_nsyms;	/* num. of sym tab entries */

	/* count the number of symbols requested */
	for (inl = list, nreq = 0; inl->n_name && inl->n_name[0]; ++inl, nreq++)
		;

	while (n) {
		if (sym_read(fd, &sym, SYMESZ) == -1) {
			sym_close(fd);
			return (-1);
		}
		n -= (q->n_numaux + 1L);
		for (p = list; p->n_name && p->n_name[0]; ++p) {
			if (p->n_value != 0L && p->n_sclass == C_EXT)
				continue;
			/*
			 * For 6.0, the name in an object file is
			 * either stored in the eight long character
			 * array, or in a string table at the end
			 * of the object file.  If the name is in the
			 * string table, the eight characters are
			 * thought of as a pair of longs, (n_zeroes
			 * and n_offset) the first of which is zero
			 * and the second is the offset of the name
			 * in the string table.
			 */
#if FLEXNAMES
			if (q->n_zeroes == 0L)	/* in string table */
			{
				if (strtab == (char *)0) /* need it */
				{
					long home = lseek(fd, 0L, 1);
					if (home == -1L) {
						sym_close(fd);
						return (-1);
					}
					if (lseek(fd, buf.f_symptr +
					    buf.f_nsyms * SYMESZ, 0) == -1 ||
					    read(fd, (char *)&strtablen,
					    sizeof (long)) != sizeof (long) ||
					    (strtab = (char *)malloc(
					    (unsigned)strtablen)) ==
					    (char *)0 ||
					    read(fd, strtab + sizeof (long),
					    strtablen - sizeof (long)) !=
					    strtablen - sizeof (long) ||
					    strtab[strtablen - 1] != '\0' ||
					    lseek(fd, home, 0) == -1) {
						(void) lseek(fd, home, 0);
						sym_close(fd);
						if (strtab != (char *)0)
							free(strtab);
						return (-1);
					}
				}
				if (q->n_offset < sizeof (long) ||
					q->n_offset >= strtablen) {
					sym_close(fd);
					if (strtab != (char *)0)
						free(strtab);
					return (-1);
				}
				if (strcmp(&strtab[q->n_offset],
					p->n_name)) {
					continue;
				}
			}
			else
#endif /* FLEXNAMES */
			{
				if (strncmp(q->_n._n_name,
					p->n_name, SYMNMLEN)) {
					continue;
				}
			}

			p->n_value = q->n_value;
			p->n_type = q->n_type;
			p->n_scnum = q->n_scnum;
			p->n_sclass = q->n_sclass;
			nreq--;
			break;
		}
	}
#if FLEXNAMES
	sym_close(fd);
	if (strtab != (char *)0)
		free(strtab);
#endif
	return (nreq);
}

static void
sym_close(int fd)
{
	num_in_buf = 0;
	next_entry = (char *)0;

	(void) close(fd);
}

/* buffered read of symbol table */
static int
sym_read(int fd, struct syment *sym, int size)
{
	long where = 0L;

	if ((where = lseek(fd, 0L, 1)) == -1L) {
		sym_close(fd);
		return (-1);
	}

	if (!num_in_buf) {
		if (fill_sym_buf(fd, size) == -1)
			return (-1);
	}
	(void) memcpy((char *)sym, next_entry, size);
	num_in_buf--;

	if (sym->n_numaux && !num_in_buf) {
		if (fill_sym_buf(fd, size) == -1)
			return (-1);
	}
	if ((lseek(fd, where + SYMESZ + (AUXESZ * sym->n_numaux), 0)) == -1L) {
		sym_close(fd);
		return (-1);
	}
	size += AUXESZ * sym->n_numaux;
	num_in_buf--;

	next_entry += size;
	return (0);
}

static int
fill_sym_buf(int fd, int size)
{
	if ((num_in_buf = read(fd, sym_buf, size * SPACE)) == -1)
		return (-1);
	num_in_buf /= size;
	next_entry = &(sym_buf[0]);
	return (0);
}
#endif /* COFF_NLIST_SUPPORTED */