xref: /onnv-gate/usr/src/ucblib/libucb/port/gen/nlist.c (revision 10754:bc532f9d3ee0)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*10754SGarrett.Damore@Sun.COM  * Common Development and Distribution License (the "License").
6*10754SGarrett.Damore@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*10754SGarrett.Damore@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*LINTLIBRARY*/
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include "libelf.h"
430Sstevel@tonic-gate #include <stdlib.h>
440Sstevel@tonic-gate #include <unistd.h>
45*10754SGarrett.Damore@Sun.COM #include <sys/fcntl.h>
460Sstevel@tonic-gate #include <nlist.h>
470Sstevel@tonic-gate #include <sys/file.h>
480Sstevel@tonic-gate #include <string.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #if COFF_NLIST_SUPPORTED
510Sstevel@tonic-gate #include "aouthdr.h"
520Sstevel@tonic-gate #include "filehdr.h"
530Sstevel@tonic-gate #include "scnhdr.h"
540Sstevel@tonic-gate #include "reloc.h"
550Sstevel@tonic-gate #endif /* COFF_NLIST_SUPPORTED */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include "linenum.h"
580Sstevel@tonic-gate #include "syms.h"
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #undef  BADMAG
610Sstevel@tonic-gate #define	BADMAG(x)	(!ISCOFF(x))
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #ifndef FLEXNAMES
640Sstevel@tonic-gate #define	FLEXNAMES 1
650Sstevel@tonic-gate #endif
660Sstevel@tonic-gate #undef n_name		/* this patch causes problems here */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #define	SPACE 100		/* number of symbols read at a time */
690Sstevel@tonic-gate #define	ISELF (strncmp(magic_buf, ELFMAG, SELFMAG) == 0)
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #if COFF_NLIST_SUPPORTED
720Sstevel@tonic-gate static char sym_buf[SPACE * SYMESZ];
730Sstevel@tonic-gate static int num_in_buf = 0;
740Sstevel@tonic-gate static char *next_entry = (char *)0;
750Sstevel@tonic-gate #endif /* COFF_NLIST_SUPPORTED */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static unsigned encode;		/* data encoding if an ELF file */
780Sstevel@tonic-gate static unsigned fvers;		/* object file version if an ELF file */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /* forward declarations */
810Sstevel@tonic-gate static int _elf_nlist(int, struct nlist *);
820Sstevel@tonic-gate static int end_elf_job(int);
830Sstevel@tonic-gate static Elf_Data *elf_read(int, long, size_t, size_t, Elf_Type);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #if COFF_NLIST_SUPPORTED
860Sstevel@tonic-gate static int _coff_nlist(int, struct nlist *);
870Sstevel@tonic-gate static void sym_close(int);
880Sstevel@tonic-gate static int sym_read(int, struct syment *, int);
890Sstevel@tonic-gate static int fill_sym_buf(int, int);
900Sstevel@tonic-gate #endif /* COFF_NLIST_SUPPORTED */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate int
nlist(const char * name,struct nlist * list)930Sstevel@tonic-gate nlist(const char *name, struct nlist *list)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	struct nlist *p;
960Sstevel@tonic-gate 	char magic_buf[EI_NIDENT+1];
970Sstevel@tonic-gate 	int fd;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	for (p = list; p->n_name && p->n_name[0]; p++) { /* n_name can be ptr */
1000Sstevel@tonic-gate 		p->n_type = 0;
1010Sstevel@tonic-gate 		p->n_value = 0L;
1020Sstevel@tonic-gate 		p->n_scnum = 0;
1030Sstevel@tonic-gate 		p->n_sclass = 0;
1040Sstevel@tonic-gate 		p->n_numaux = 0;
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if ((fd = open(name, 0)) < 0)
1080Sstevel@tonic-gate 		return (-1);
1090Sstevel@tonic-gate 	if (read(fd, magic_buf, (size_t)EI_NIDENT) == -1) {
1100Sstevel@tonic-gate 		(void) close(fd);
1110Sstevel@tonic-gate 		return (-1);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	magic_buf[EI_NIDENT] = '\0';
1140Sstevel@tonic-gate 	if (lseek(fd, 0L, 0) == -1L) { /* rewind to beginning of object file */
1150Sstevel@tonic-gate 		(void) close(fd);
1160Sstevel@tonic-gate 		return (-1);
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (ISELF) {
1200Sstevel@tonic-gate 		/*
1210Sstevel@tonic-gate 		 * right now can only handle 32-bit architectures
1220Sstevel@tonic-gate 		 * XX64 work to come?  ELFCLASS64?
1230Sstevel@tonic-gate 		 */
1240Sstevel@tonic-gate 		if (magic_buf[EI_CLASS] != ELFCLASS32) {
1250Sstevel@tonic-gate 			(void) close(fd);
1260Sstevel@tonic-gate 			return (-1);
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate 		encode = (unsigned)magic_buf[EI_DATA];
1290Sstevel@tonic-gate 		fvers = (unsigned)magic_buf[EI_VERSION];
1300Sstevel@tonic-gate 		return (_elf_nlist(fd, list));
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 	else
1330Sstevel@tonic-gate #if COFF_NLIST_SUPPORTED
1340Sstevel@tonic-gate 		return (_coff_nlist(fd, list));
1350Sstevel@tonic-gate #else /* COFF_NLIST_SUPPORTED */
1360Sstevel@tonic-gate 		return (-1);
1370Sstevel@tonic-gate #endif /* COFF_NLIST_SUPPORTED */
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static int
_elf_nlist(int fd,struct nlist * list)1410Sstevel@tonic-gate _elf_nlist(int fd, struct nlist *list)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	Elf_Data   *symdata;	/* buffer points to symbol table */
1440Sstevel@tonic-gate 	Elf_Data   *strdata;	/* buffer points to string table */
1450Sstevel@tonic-gate 	Elf_Data   *secdata;	/* buffer points to section table */
1460Sstevel@tonic-gate 	Elf32_Shdr *symhdr;	/* section table entry for symtab */
1470Sstevel@tonic-gate 	Elf32_Shdr *strhdr;	/* section table entry for strtab */
1480Sstevel@tonic-gate 	Elf32_Sym  *sym;	/* buffer storing one symbol information */
1490Sstevel@tonic-gate 	Elf32_Sym  *sym_end;	/* end of symbol table */
1500Sstevel@tonic-gate 	Elf32_Ehdr *ehdr;	/* file header */
1510Sstevel@tonic-gate 	Elf_Data   *edata;	/* data buffer for ehdr */
1520Sstevel@tonic-gate 	int	i;
1530Sstevel@tonic-gate 	int	nreq;
1540Sstevel@tonic-gate 	struct  nlist *inl;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE)
1570Sstevel@tonic-gate 		return (end_elf_job(fd));
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/* count the number of symbols requested */
1600Sstevel@tonic-gate 	for (inl = list, nreq = 0; inl->n_name && inl->n_name[0]; ++inl, nreq++)
1610Sstevel@tonic-gate 		;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	/* read file header and section header table */
1640Sstevel@tonic-gate 	if ((edata = elf_read(fd, 0L, elf32_fsize(ELF_T_EHDR, 1, fvers),
1650Sstevel@tonic-gate 		sizeof (Elf32_Ehdr), ELF_T_EHDR)) == 0)
1660Sstevel@tonic-gate 		return (end_elf_job(fd));
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	ehdr = (Elf32_Ehdr *)edata->d_buf;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (ehdr->e_shoff == 0) {
1710Sstevel@tonic-gate 		free(edata->d_buf);
1720Sstevel@tonic-gate 		free(edata);
1730Sstevel@tonic-gate 		return (end_elf_job(fd));
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	if ((secdata = elf_read(fd, (long)ehdr->e_shoff,
1770Sstevel@tonic-gate 		(size_t)(ehdr->e_shentsize * ehdr->e_shnum),
1780Sstevel@tonic-gate 		(size_t)(ehdr->e_shnum * sizeof (Elf32_Ehdr)),
1790Sstevel@tonic-gate 		ELF_T_SHDR)) == 0) {
1800Sstevel@tonic-gate 		free(edata->d_buf);
1810Sstevel@tonic-gate 		free(edata);
1820Sstevel@tonic-gate 		return (end_elf_job(fd));
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/* find symbol table section */
1860Sstevel@tonic-gate 	symhdr = (Elf32_Shdr *)secdata->d_buf;
1870Sstevel@tonic-gate 	for (i = 0; i < (Elf32_Word)ehdr->e_shnum; i++, symhdr++)
1880Sstevel@tonic-gate 		if (symhdr->sh_type == SHT_SYMTAB)
1890Sstevel@tonic-gate 			break;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if ((symhdr->sh_type != SHT_SYMTAB) ||
1920Sstevel@tonic-gate 		(symhdr->sh_link >= ehdr->e_shnum)) {
1930Sstevel@tonic-gate 		free(secdata->d_buf);
1940Sstevel@tonic-gate 		free(secdata);
1950Sstevel@tonic-gate 		free(edata->d_buf);
1960Sstevel@tonic-gate 		free(edata);
1970Sstevel@tonic-gate 		return (end_elf_job(fd));
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	if ((symdata = elf_read(fd, (long)symhdr->sh_offset,
2010Sstevel@tonic-gate 		(size_t)symhdr->sh_size,
2020Sstevel@tonic-gate 		(size_t)((symhdr->sh_size / symhdr->sh_entsize) *
2030Sstevel@tonic-gate 		sizeof (Elf32_Sym)), ELF_T_SYM)) == 0) {
2040Sstevel@tonic-gate 		free(secdata->d_buf);
2050Sstevel@tonic-gate 		free(secdata);
2060Sstevel@tonic-gate 		free(edata->d_buf);
2070Sstevel@tonic-gate 		free(edata);
2080Sstevel@tonic-gate 		return (end_elf_job(fd));
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/* read string table */
2120Sstevel@tonic-gate 	strhdr = (Elf32_Shdr *)secdata->d_buf;
2130Sstevel@tonic-gate 	strhdr = strhdr + symhdr->sh_link;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if (strhdr->sh_type != SHT_STRTAB) {
2160Sstevel@tonic-gate 		free(symdata->d_buf);
2170Sstevel@tonic-gate 		free(symdata);
2180Sstevel@tonic-gate 		free(secdata->d_buf);
2190Sstevel@tonic-gate 		free(secdata);
2200Sstevel@tonic-gate 		free(edata->d_buf);
2210Sstevel@tonic-gate 		free(edata);
2220Sstevel@tonic-gate 		return (end_elf_job(fd));
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if ((strdata = elf_read(fd, strhdr->sh_offset, strhdr->sh_size,
2260Sstevel@tonic-gate 		strhdr->sh_size, ELF_T_BYTE)) == 0) {
2270Sstevel@tonic-gate 		free(symdata->d_buf);
2280Sstevel@tonic-gate 		free(symdata);
2290Sstevel@tonic-gate 		free(secdata->d_buf);
2300Sstevel@tonic-gate 		free(secdata);
2310Sstevel@tonic-gate 		free(edata->d_buf);
2320Sstevel@tonic-gate 		free(edata);
2330Sstevel@tonic-gate 		return (end_elf_job(fd));
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	((char *)strdata->d_buf)[0] = '\0';
2370Sstevel@tonic-gate 	((char *)strdata->d_buf)[strhdr->sh_size-1] = '\0';
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	sym = (Elf32_Sym *) (symdata->d_buf);
2400Sstevel@tonic-gate 	sym_end = sym + symdata->d_size / sizeof (Elf32_Sym);
2410Sstevel@tonic-gate 	for (; sym < sym_end; ++sym) {
2420Sstevel@tonic-gate 		struct nlist *p;
2430Sstevel@tonic-gate 		char *name;
2440Sstevel@tonic-gate 		if (sym->st_name > strhdr->sh_size) {
2450Sstevel@tonic-gate 			free(strdata->d_buf);
2460Sstevel@tonic-gate 			free(strdata);
2470Sstevel@tonic-gate 			free(symdata->d_buf);
2480Sstevel@tonic-gate 			free(symdata);
2490Sstevel@tonic-gate 			free(secdata->d_buf);
2500Sstevel@tonic-gate 			free(secdata);
2510Sstevel@tonic-gate 			free(edata->d_buf);
2520Sstevel@tonic-gate 			free(edata);
2530Sstevel@tonic-gate 			return (end_elf_job(fd));
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 		name = (char *)strdata->d_buf + sym->st_name;
2560Sstevel@tonic-gate 		if (name == 0)
2570Sstevel@tonic-gate 			continue;
2580Sstevel@tonic-gate 		for (p = list; p->n_name && p->n_name[0]; ++p) {
2590Sstevel@tonic-gate 			if (strcmp(p->n_name, name)) {
2600Sstevel@tonic-gate 				continue;
2610Sstevel@tonic-gate 			}
2620Sstevel@tonic-gate 			p->n_value = sym->st_value;
2630Sstevel@tonic-gate 			p->n_type = ELF32_ST_TYPE(sym->st_info);
2640Sstevel@tonic-gate 			p->n_scnum = sym->st_shndx;
2650Sstevel@tonic-gate 			nreq--;
2660Sstevel@tonic-gate 			break;
2670Sstevel@tonic-gate 		}
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 	/*
2700Sstevel@tonic-gate 	 * Currently there is only one symbol table section
2710Sstevel@tonic-gate 	 * in an object file, but this restriction may be
2720Sstevel@tonic-gate 	 * relaxed in the future.
2730Sstevel@tonic-gate 	 */
2740Sstevel@tonic-gate 	(void) close(fd);
2750Sstevel@tonic-gate 	free(secdata->d_buf);
2760Sstevel@tonic-gate 	free(strdata->d_buf);
2770Sstevel@tonic-gate 	free(symdata->d_buf);
2780Sstevel@tonic-gate 	free(edata->d_buf);
2790Sstevel@tonic-gate 	free(secdata);
2800Sstevel@tonic-gate 	free(strdata);
2810Sstevel@tonic-gate 	free(symdata);
2820Sstevel@tonic-gate 	free(edata);
2830Sstevel@tonic-gate 	return (nreq);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate  * allocate memory of size memsize and read size bytes
2880Sstevel@tonic-gate  * starting at offset from fd - the file data are
2890Sstevel@tonic-gate  * translated in place using the low-level libelf
2900Sstevel@tonic-gate  * translation routines
2910Sstevel@tonic-gate  */
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate static Elf_Data *
elf_read(int fd,long offset,size_t size,size_t memsize,Elf_Type dtype)2940Sstevel@tonic-gate elf_read(int fd, long offset, size_t size, size_t memsize, Elf_Type dtype)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	Elf_Data *dsrc, *ddst;
2970Sstevel@tonic-gate 	Elf_Data srcdata;
2980Sstevel@tonic-gate 	size_t maxsize;
2990Sstevel@tonic-gate 	char *p;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	dsrc = &srcdata;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (size == 0)
3040Sstevel@tonic-gate 		return (0);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if ((maxsize = memsize) < size)
3070Sstevel@tonic-gate 		maxsize = size;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if ((ddst = (Elf_Data *)malloc(sizeof (Elf_Data))) == 0)
3110Sstevel@tonic-gate 		return (0);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	if ((p = malloc(maxsize)) == 0) {
3140Sstevel@tonic-gate 		free(ddst);
3150Sstevel@tonic-gate 		return (0);
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (lseek(fd, offset, 0L) == -1) {
3190Sstevel@tonic-gate 		free(ddst);
3200Sstevel@tonic-gate 		free(p);
3210Sstevel@tonic-gate 		return (0);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (read(fd, p, size) != size) {
3250Sstevel@tonic-gate 		free(ddst);
3260Sstevel@tonic-gate 		free(p);
3270Sstevel@tonic-gate 		return (0);
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	dsrc->d_buf = p;
3310Sstevel@tonic-gate 	dsrc->d_type = dtype;
3320Sstevel@tonic-gate 	dsrc->d_size = size;
3330Sstevel@tonic-gate 	dsrc->d_version = fvers;
3340Sstevel@tonic-gate 	ddst->d_buf = p;
3350Sstevel@tonic-gate 	ddst->d_size = memsize;
3360Sstevel@tonic-gate 	ddst->d_version = EV_CURRENT;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	if (elf32_xlatetom(ddst, dsrc, encode) != ddst) {
3390Sstevel@tonic-gate 		free(ddst);
3400Sstevel@tonic-gate 		free(p);
3410Sstevel@tonic-gate 		return (0);
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	return (ddst);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate static int
end_elf_job(int fd)3480Sstevel@tonic-gate end_elf_job(int fd)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	(void) close(fd);
3510Sstevel@tonic-gate 	return (-1);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate #if COFF_NLIST_SUPPORTED
3550Sstevel@tonic-gate static int
_coff_nlist(int fd,struct nlist * list)3560Sstevel@tonic-gate _coff_nlist(int fd, struct nlist *list)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	struct	filehdr	buf;
3590Sstevel@tonic-gate 	struct	syment	sym;
3600Sstevel@tonic-gate 	long n;
3610Sstevel@tonic-gate 	int bufsiz = FILHSZ;
3620Sstevel@tonic-gate #if FLEXNAMES
3630Sstevel@tonic-gate 	char *strtab = (char *)0;
3640Sstevel@tonic-gate 	long strtablen;
3650Sstevel@tonic-gate #endif
3660Sstevel@tonic-gate 	struct nlist *p, *inl;
3670Sstevel@tonic-gate 	struct syment *q;
3680Sstevel@tonic-gate 	long	sa;
3690Sstevel@tonic-gate 	int 	nreq;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	if (read(fd, (char *)&buf, bufsiz) == -1) {
3720Sstevel@tonic-gate 		(void) close(fd);
3730Sstevel@tonic-gate 		return (-1);
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	if (BADMAG(buf.f_magic)) {
3770Sstevel@tonic-gate 		(void) close(fd);
3780Sstevel@tonic-gate 		return (-1);
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 	sa = buf.f_symptr;	/* direct pointer to sym tab */
3810Sstevel@tonic-gate 	if (lseek(fd, (long)sa, 0) == -1L) {
3820Sstevel@tonic-gate 		(void) close(fd);
3830Sstevel@tonic-gate 		return (-1);
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 	q = &sym;
3860Sstevel@tonic-gate 	n = buf.f_nsyms;	/* num. of sym tab entries */
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	/* count the number of symbols requested */
3890Sstevel@tonic-gate 	for (inl = list, nreq = 0; inl->n_name && inl->n_name[0]; ++inl, nreq++)
3900Sstevel@tonic-gate 		;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	while (n) {
3930Sstevel@tonic-gate 		if (sym_read(fd, &sym, SYMESZ) == -1) {
3940Sstevel@tonic-gate 			sym_close(fd);
3950Sstevel@tonic-gate 			return (-1);
3960Sstevel@tonic-gate 		}
3970Sstevel@tonic-gate 		n -= (q->n_numaux + 1L);
3980Sstevel@tonic-gate 		for (p = list; p->n_name && p->n_name[0]; ++p) {
3990Sstevel@tonic-gate 			if (p->n_value != 0L && p->n_sclass == C_EXT)
4000Sstevel@tonic-gate 				continue;
4010Sstevel@tonic-gate 			/*
4020Sstevel@tonic-gate 			 * For 6.0, the name in an object file is
4030Sstevel@tonic-gate 			 * either stored in the eight long character
4040Sstevel@tonic-gate 			 * array, or in a string table at the end
4050Sstevel@tonic-gate 			 * of the object file.  If the name is in the
4060Sstevel@tonic-gate 			 * string table, the eight characters are
4070Sstevel@tonic-gate 			 * thought of as a pair of longs, (n_zeroes
4080Sstevel@tonic-gate 			 * and n_offset) the first of which is zero
4090Sstevel@tonic-gate 			 * and the second is the offset of the name
4100Sstevel@tonic-gate 			 * in the string table.
4110Sstevel@tonic-gate 			 */
4120Sstevel@tonic-gate #if FLEXNAMES
4130Sstevel@tonic-gate 			if (q->n_zeroes == 0L)	/* in string table */
4140Sstevel@tonic-gate 			{
4150Sstevel@tonic-gate 				if (strtab == (char *)0) /* need it */
4160Sstevel@tonic-gate 				{
4170Sstevel@tonic-gate 					long home = lseek(fd, 0L, 1);
4180Sstevel@tonic-gate 					if (home == -1L) {
4190Sstevel@tonic-gate 						sym_close(fd);
4200Sstevel@tonic-gate 						return (-1);
4210Sstevel@tonic-gate 					}
4220Sstevel@tonic-gate 					if (lseek(fd, buf.f_symptr +
4230Sstevel@tonic-gate 					    buf.f_nsyms * SYMESZ, 0) == -1 ||
4240Sstevel@tonic-gate 					    read(fd, (char *)&strtablen,
4250Sstevel@tonic-gate 					    sizeof (long)) != sizeof (long) ||
4260Sstevel@tonic-gate 					    (strtab = (char *)malloc(
4270Sstevel@tonic-gate 					    (unsigned)strtablen)) ==
4280Sstevel@tonic-gate 					    (char *)0 ||
4290Sstevel@tonic-gate 					    read(fd, strtab + sizeof (long),
4300Sstevel@tonic-gate 					    strtablen - sizeof (long)) !=
4310Sstevel@tonic-gate 					    strtablen - sizeof (long) ||
4320Sstevel@tonic-gate 					    strtab[strtablen - 1] != '\0' ||
4330Sstevel@tonic-gate 					    lseek(fd, home, 0) == -1) {
4340Sstevel@tonic-gate 						(void) lseek(fd, home, 0);
4350Sstevel@tonic-gate 						sym_close(fd);
4360Sstevel@tonic-gate 						if (strtab != (char *)0)
4370Sstevel@tonic-gate 							free(strtab);
4380Sstevel@tonic-gate 						return (-1);
4390Sstevel@tonic-gate 					}
4400Sstevel@tonic-gate 				}
4410Sstevel@tonic-gate 				if (q->n_offset < sizeof (long) ||
4420Sstevel@tonic-gate 					q->n_offset >= strtablen) {
4430Sstevel@tonic-gate 					sym_close(fd);
4440Sstevel@tonic-gate 					if (strtab != (char *)0)
4450Sstevel@tonic-gate 						free(strtab);
4460Sstevel@tonic-gate 					return (-1);
4470Sstevel@tonic-gate 				}
4480Sstevel@tonic-gate 				if (strcmp(&strtab[q->n_offset],
4490Sstevel@tonic-gate 					p->n_name)) {
4500Sstevel@tonic-gate 					continue;
4510Sstevel@tonic-gate 				}
4520Sstevel@tonic-gate 			}
4530Sstevel@tonic-gate 			else
4540Sstevel@tonic-gate #endif /* FLEXNAMES */
4550Sstevel@tonic-gate 			{
4560Sstevel@tonic-gate 				if (strncmp(q->_n._n_name,
4570Sstevel@tonic-gate 					p->n_name, SYMNMLEN)) {
4580Sstevel@tonic-gate 					continue;
4590Sstevel@tonic-gate 				}
4600Sstevel@tonic-gate 			}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 			p->n_value = q->n_value;
4630Sstevel@tonic-gate 			p->n_type = q->n_type;
4640Sstevel@tonic-gate 			p->n_scnum = q->n_scnum;
4650Sstevel@tonic-gate 			p->n_sclass = q->n_sclass;
4660Sstevel@tonic-gate 			nreq--;
4670Sstevel@tonic-gate 			break;
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate #if FLEXNAMES
4710Sstevel@tonic-gate 	sym_close(fd);
4720Sstevel@tonic-gate 	if (strtab != (char *)0)
4730Sstevel@tonic-gate 		free(strtab);
4740Sstevel@tonic-gate #endif
4750Sstevel@tonic-gate 	return (nreq);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate static void
sym_close(int fd)4790Sstevel@tonic-gate sym_close(int fd)
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate 	num_in_buf = 0;
4820Sstevel@tonic-gate 	next_entry = (char *)0;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	(void) close(fd);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate /* buffered read of symbol table */
4880Sstevel@tonic-gate static int
sym_read(int fd,struct syment * sym,int size)4890Sstevel@tonic-gate sym_read(int fd, struct syment *sym, int size)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	long where = 0L;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if ((where = lseek(fd, 0L, 1)) == -1L) {
4940Sstevel@tonic-gate 		sym_close(fd);
4950Sstevel@tonic-gate 		return (-1);
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	if (!num_in_buf) {
4990Sstevel@tonic-gate 		if (fill_sym_buf(fd, size) == -1)
5000Sstevel@tonic-gate 			return (-1);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 	(void) memcpy((char *)sym, next_entry, size);
5030Sstevel@tonic-gate 	num_in_buf--;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	if (sym->n_numaux && !num_in_buf) {
5060Sstevel@tonic-gate 		if (fill_sym_buf(fd, size) == -1)
5070Sstevel@tonic-gate 			return (-1);
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 	if ((lseek(fd, where + SYMESZ + (AUXESZ * sym->n_numaux), 0)) == -1L) {
5100Sstevel@tonic-gate 		sym_close(fd);
5110Sstevel@tonic-gate 		return (-1);
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 	size += AUXESZ * sym->n_numaux;
5140Sstevel@tonic-gate 	num_in_buf--;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	next_entry += size;
5170Sstevel@tonic-gate 	return (0);
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate static int
fill_sym_buf(int fd,int size)5210Sstevel@tonic-gate fill_sym_buf(int fd, int size)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate 	if ((num_in_buf = read(fd, sym_buf, size * SPACE)) == -1)
5240Sstevel@tonic-gate 		return (-1);
5250Sstevel@tonic-gate 	num_in_buf /= size;
5260Sstevel@tonic-gate 	next_entry = &(sym_buf[0]);
5270Sstevel@tonic-gate 	return (0);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate #endif /* COFF_NLIST_SUPPORTED */
530