xref: /titanic_52/usr/src/tools/ctf/cvt/input.c (revision c168da277d64ace98b478c694fd808f783ce7d2e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54d232658Sjohnlev  * Common Development and Distribution License (the "License").
64d232658Sjohnlev  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
224d232658Sjohnlev  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Routines for retrieving CTF data from a .SUNW_ctf ELF section
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <gelf.h>
377c478bd9Sstevel@tonic-gate #include <strings.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "ctftools.h"
417c478bd9Sstevel@tonic-gate #include "memory.h"
424d232658Sjohnlev #include "symbol.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate typedef int read_cb_f(tdata_t *, char *, void *);
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Return the source types that the object was generated from.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate source_types_t
507c478bd9Sstevel@tonic-gate built_source_types(Elf *elf, char const *file)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	source_types_t types = SOURCE_NONE;
537c478bd9Sstevel@tonic-gate 	symit_data_t *si;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	if ((si = symit_new(elf, file)) == NULL)
567c478bd9Sstevel@tonic-gate 		return (SOURCE_NONE);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	while (symit_next(si, STT_FILE) != NULL) {
597c478bd9Sstevel@tonic-gate 		char *name = symit_name(si);
607c478bd9Sstevel@tonic-gate 		size_t len = strlen(name);
617c478bd9Sstevel@tonic-gate 		if (len < 2 || name[len - 2] != '.') {
627c478bd9Sstevel@tonic-gate 			types |= SOURCE_UNKNOWN;
637c478bd9Sstevel@tonic-gate 			continue;
647c478bd9Sstevel@tonic-gate 		}
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 		switch (name[len - 1]) {
677c478bd9Sstevel@tonic-gate 		case 'c':
687c478bd9Sstevel@tonic-gate 			types |= SOURCE_C;
697c478bd9Sstevel@tonic-gate 			break;
707c478bd9Sstevel@tonic-gate 		case 'h':
717c478bd9Sstevel@tonic-gate 			/* ignore */
727c478bd9Sstevel@tonic-gate 			break;
737c478bd9Sstevel@tonic-gate 		case 's':
747c478bd9Sstevel@tonic-gate 			types |= SOURCE_S;
757c478bd9Sstevel@tonic-gate 			break;
767c478bd9Sstevel@tonic-gate 		default:
777c478bd9Sstevel@tonic-gate 			types |= SOURCE_UNKNOWN;
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	symit_free(si);
827c478bd9Sstevel@tonic-gate 	return (types);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static int
867c478bd9Sstevel@tonic-gate read_file(Elf *elf, char *file, char *label, read_cb_f *func, void *arg,
877c478bd9Sstevel@tonic-gate     int require_ctf)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	Elf_Scn *ctfscn;
907c478bd9Sstevel@tonic-gate 	Elf_Data *ctfdata;
917c478bd9Sstevel@tonic-gate 	symit_data_t *si = NULL;
927c478bd9Sstevel@tonic-gate 	int ctfscnidx;
937c478bd9Sstevel@tonic-gate 	tdata_t *td;
947c478bd9Sstevel@tonic-gate 
95*c168da27Sjohnlev 	if ((ctfscnidx = findelfsecidx(elf, file, ".SUNW_ctf")) < 0) {
967c478bd9Sstevel@tonic-gate 		if (require_ctf &&
977c478bd9Sstevel@tonic-gate 		    (built_source_types(elf, file) & SOURCE_C)) {
987c478bd9Sstevel@tonic-gate 			terminate("Input file %s was partially built from "
997c478bd9Sstevel@tonic-gate 			    "C sources, but no CTF data was present\n", file);
1007c478bd9Sstevel@tonic-gate 		}
1017c478bd9Sstevel@tonic-gate 		return (0);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if ((ctfscn = elf_getscn(elf, ctfscnidx)) == NULL ||
1057c478bd9Sstevel@tonic-gate 	    (ctfdata = elf_getdata(ctfscn, NULL)) == NULL)
1067c478bd9Sstevel@tonic-gate 		elfterminate(file, "Cannot read CTF section");
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/* Reconstruction of type tree */
1097c478bd9Sstevel@tonic-gate 	if ((si = symit_new(elf, file)) == NULL) {
1107c478bd9Sstevel@tonic-gate 		warning("%s has no symbol table - skipping", file);
1117c478bd9Sstevel@tonic-gate 		return (0);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	td = ctf_load(file, ctfdata->d_buf, ctfdata->d_size, si, label);
1157c478bd9Sstevel@tonic-gate 	tdata_build_hashes(td);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	symit_free(si);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (td != NULL) {
1207c478bd9Sstevel@tonic-gate 		if (func(td, file, arg) < 0)
1217c478bd9Sstevel@tonic-gate 			return (-1);
1227c478bd9Sstevel@tonic-gate 		else
1237c478bd9Sstevel@tonic-gate 			return (1);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	return (0);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static int
1297c478bd9Sstevel@tonic-gate read_archive(int fd, Elf *elf, char *file, char *label, read_cb_f *func,
1307c478bd9Sstevel@tonic-gate     void *arg, int require_ctf)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	Elf *melf;
1337c478bd9Sstevel@tonic-gate 	Elf_Cmd cmd = ELF_C_READ;
1347c478bd9Sstevel@tonic-gate 	Elf_Arhdr *arh;
1357c478bd9Sstevel@tonic-gate 	int secnum = 1, found = 0;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	while ((melf = elf_begin(fd, cmd, elf)) != NULL) {
1387c478bd9Sstevel@tonic-gate 		int rc = 0;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 		if ((arh = elf_getarhdr(melf)) == NULL) {
1417c478bd9Sstevel@tonic-gate 			elfterminate(file, "Can't get archive header for "
1427c478bd9Sstevel@tonic-gate 			    "member %d", secnum);
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		/* skip special sections - their names begin with "/" */
1467c478bd9Sstevel@tonic-gate 		if (*arh->ar_name != '/') {
1477c478bd9Sstevel@tonic-gate 			size_t memlen = strlen(file) + 1 +
1487c478bd9Sstevel@tonic-gate 			    strlen(arh->ar_name) + 1 + 1;
1497c478bd9Sstevel@tonic-gate 			char *memname = xmalloc(memlen);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 			snprintf(memname, memlen, "%s(%s)", file, arh->ar_name);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 			switch (elf_kind(melf)) {
1547c478bd9Sstevel@tonic-gate 			case ELF_K_AR:
1557c478bd9Sstevel@tonic-gate 				rc = read_archive(fd, melf, memname, label,
1567c478bd9Sstevel@tonic-gate 				    func, arg, require_ctf);
1577c478bd9Sstevel@tonic-gate 				break;
1587c478bd9Sstevel@tonic-gate 			case ELF_K_ELF:
1597c478bd9Sstevel@tonic-gate 				rc = read_file(melf, memname, label,
1607c478bd9Sstevel@tonic-gate 				    func, arg, require_ctf);
1617c478bd9Sstevel@tonic-gate 				break;
1627c478bd9Sstevel@tonic-gate 			default:
1637c478bd9Sstevel@tonic-gate 				terminate("%s: Unknown elf kind %d\n",
1647c478bd9Sstevel@tonic-gate 				    memname, elf_kind(melf));
1657c478bd9Sstevel@tonic-gate 			}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 			free(memname);
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 		cmd = elf_next(melf);
1717c478bd9Sstevel@tonic-gate 		(void) elf_end(melf);
1727c478bd9Sstevel@tonic-gate 		secnum++;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		if (rc < 0)
1757c478bd9Sstevel@tonic-gate 			return (rc);
1767c478bd9Sstevel@tonic-gate 		else
1777c478bd9Sstevel@tonic-gate 			found += rc;
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	return (found);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static int
1847c478bd9Sstevel@tonic-gate read_ctf_common(char *file, char *label, read_cb_f *func, void *arg,
1857c478bd9Sstevel@tonic-gate     int require_ctf)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	Elf *elf;
1887c478bd9Sstevel@tonic-gate 	int found = 0;
1897c478bd9Sstevel@tonic-gate 	int fd;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	debug(3, "Reading %s (label %s)\n", file, (label ? label : "NONE"));
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	(void) elf_version(EV_CURRENT);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if ((fd = open(file, O_RDONLY)) < 0)
1967c478bd9Sstevel@tonic-gate 		terminate("%s: Cannot open for reading", file);
1977c478bd9Sstevel@tonic-gate 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
1987c478bd9Sstevel@tonic-gate 		elfterminate(file, "Cannot read");
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	switch (elf_kind(elf)) {
2017c478bd9Sstevel@tonic-gate 	case ELF_K_AR:
2027c478bd9Sstevel@tonic-gate 		found = read_archive(fd, elf, file, label,
2037c478bd9Sstevel@tonic-gate 		    func, arg, require_ctf);
2047c478bd9Sstevel@tonic-gate 		break;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	case ELF_K_ELF:
2077c478bd9Sstevel@tonic-gate 		found = read_file(elf, file, label,
2087c478bd9Sstevel@tonic-gate 		    func, arg, require_ctf);
2097c478bd9Sstevel@tonic-gate 		break;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	default:
2127c478bd9Sstevel@tonic-gate 		terminate("%s: Unknown elf kind %d\n", file, elf_kind(elf));
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	(void) elf_end(elf);
2167c478bd9Sstevel@tonic-gate 	(void) close(fd);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	return (found);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2227c478bd9Sstevel@tonic-gate int
2237c478bd9Sstevel@tonic-gate read_ctf_save_cb(tdata_t *td, char *name, void *retp)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	tdata_t **tdp = retp;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	*tdp = td;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	return (1);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate int
2337c478bd9Sstevel@tonic-gate read_ctf(char **files, int n, char *label, read_cb_f *func, void *private,
2347c478bd9Sstevel@tonic-gate     int require_ctf)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	int found;
2377c478bd9Sstevel@tonic-gate 	int i, rc;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	for (i = 0, found = 0; i < n; i++) {
2407c478bd9Sstevel@tonic-gate 		if ((rc = read_ctf_common(files[i], label, func,
2417c478bd9Sstevel@tonic-gate 		    private, require_ctf)) < 0)
2427c478bd9Sstevel@tonic-gate 			return (rc);
2437c478bd9Sstevel@tonic-gate 		found += rc;
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	return (found);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static int
2507c478bd9Sstevel@tonic-gate count_archive(int fd, Elf *elf, char *file)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	Elf *melf;
2537c478bd9Sstevel@tonic-gate 	Elf_Cmd cmd = ELF_C_READ;
2547c478bd9Sstevel@tonic-gate 	Elf_Arhdr *arh;
2557c478bd9Sstevel@tonic-gate 	int nfiles = 0, err = 0;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	while ((melf = elf_begin(fd, cmd, elf)) != NULL) {
2587c478bd9Sstevel@tonic-gate 		if ((arh = elf_getarhdr(melf)) == NULL) {
2597c478bd9Sstevel@tonic-gate 			warning("Can't process input archive %s\n",
2607c478bd9Sstevel@tonic-gate 			    file);
2617c478bd9Sstevel@tonic-gate 			err++;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		if (*arh->ar_name != '/')
2657c478bd9Sstevel@tonic-gate 			nfiles++;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		cmd = elf_next(melf);
2687c478bd9Sstevel@tonic-gate 		(void) elf_end(melf);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (err > 0)
2727c478bd9Sstevel@tonic-gate 		return (-1);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	return (nfiles);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate int
2787c478bd9Sstevel@tonic-gate count_files(char **files, int n)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	int nfiles = 0, err = 0;
2817c478bd9Sstevel@tonic-gate 	Elf *elf;
2827c478bd9Sstevel@tonic-gate 	int fd, rc, i;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	(void) elf_version(EV_CURRENT);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
2877c478bd9Sstevel@tonic-gate 		char *file = files[i];
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		if ((fd = open(file, O_RDONLY)) < 0) {
2907c478bd9Sstevel@tonic-gate 			warning("Can't read input file %s", file);
2917c478bd9Sstevel@tonic-gate 			err++;
2927c478bd9Sstevel@tonic-gate 			continue;
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
2967c478bd9Sstevel@tonic-gate 			warning("Can't open input file %s: %s\n", file,
297*c168da27Sjohnlev 			    elf_errmsg(-1));
2987c478bd9Sstevel@tonic-gate 			err++;
2997c478bd9Sstevel@tonic-gate 			(void) close(fd);
3007c478bd9Sstevel@tonic-gate 			continue;
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		switch (elf_kind(elf)) {
3047c478bd9Sstevel@tonic-gate 		case ELF_K_AR:
3057c478bd9Sstevel@tonic-gate 			if ((rc = count_archive(fd, elf, file)) < 0)
3067c478bd9Sstevel@tonic-gate 				err++;
3077c478bd9Sstevel@tonic-gate 			else
3087c478bd9Sstevel@tonic-gate 				nfiles += rc;
3097c478bd9Sstevel@tonic-gate 			break;
3107c478bd9Sstevel@tonic-gate 		case ELF_K_ELF:
3117c478bd9Sstevel@tonic-gate 			nfiles++;
3127c478bd9Sstevel@tonic-gate 			break;
3137c478bd9Sstevel@tonic-gate 		default:
3147c478bd9Sstevel@tonic-gate 			warning("Input file %s is corrupt\n", file);
3157c478bd9Sstevel@tonic-gate 			err++;
3167c478bd9Sstevel@tonic-gate 		}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		(void) elf_end(elf);
3197c478bd9Sstevel@tonic-gate 		(void) close(fd);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (err > 0)
3237c478bd9Sstevel@tonic-gate 		return (-1);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	debug(2, "Found %d files in %d input files\n", nfiles, n);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	return (nfiles);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate struct symit_data {
3317c478bd9Sstevel@tonic-gate 	GElf_Shdr si_shdr;
3327c478bd9Sstevel@tonic-gate 	Elf_Data *si_symd;
3337c478bd9Sstevel@tonic-gate 	Elf_Data *si_strd;
3347c478bd9Sstevel@tonic-gate 	GElf_Sym si_cursym;
3357c478bd9Sstevel@tonic-gate 	char *si_curname;
3367c478bd9Sstevel@tonic-gate 	char *si_curfile;
3377c478bd9Sstevel@tonic-gate 	int si_nument;
3387c478bd9Sstevel@tonic-gate 	int si_next;
3397c478bd9Sstevel@tonic-gate };
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate symit_data_t *
3427c478bd9Sstevel@tonic-gate symit_new(Elf *elf, const char *file)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	symit_data_t *si;
3457c478bd9Sstevel@tonic-gate 	Elf_Scn *scn;
3467c478bd9Sstevel@tonic-gate 	int symtabidx;
3477c478bd9Sstevel@tonic-gate 
348*c168da27Sjohnlev 	if ((symtabidx = findelfsecidx(elf, file, ".symtab")) < 0)
3497c478bd9Sstevel@tonic-gate 		return (NULL);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	si = xcalloc(sizeof (symit_data_t));
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if ((scn = elf_getscn(elf, symtabidx)) == NULL ||
3547c478bd9Sstevel@tonic-gate 	    gelf_getshdr(scn, &si->si_shdr) == NULL ||
3557c478bd9Sstevel@tonic-gate 	    (si->si_symd = elf_getdata(scn, NULL)) == NULL)
3567c478bd9Sstevel@tonic-gate 		elfterminate(file, "Cannot read .symtab");
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	if ((scn = elf_getscn(elf, si->si_shdr.sh_link)) == NULL ||
3597c478bd9Sstevel@tonic-gate 	    (si->si_strd = elf_getdata(scn, NULL)) == NULL)
3607c478bd9Sstevel@tonic-gate 		elfterminate(file, "Cannot read strings for .symtab");
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	si->si_nument = si->si_shdr.sh_size / si->si_shdr.sh_entsize;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	return (si);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate void
3687c478bd9Sstevel@tonic-gate symit_free(symit_data_t *si)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	free(si);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate void
3747c478bd9Sstevel@tonic-gate symit_reset(symit_data_t *si)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	si->si_next = 0;
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate char *
3807c478bd9Sstevel@tonic-gate symit_curfile(symit_data_t *si)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	return (si->si_curfile);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate GElf_Sym *
3867c478bd9Sstevel@tonic-gate symit_next(symit_data_t *si, int type)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
3894d232658Sjohnlev 	int check_sym = (type == STT_OBJECT || type == STT_FUNC);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	for (; si->si_next < si->si_nument; si->si_next++) {
3927c478bd9Sstevel@tonic-gate 		gelf_getsym(si->si_symd, si->si_next, &si->si_cursym);
3937c478bd9Sstevel@tonic-gate 		gelf_getsym(si->si_symd, si->si_next, &sym);
3947c478bd9Sstevel@tonic-gate 		si->si_curname = (caddr_t)si->si_strd->d_buf + sym.st_name;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		if (GELF_ST_TYPE(sym.st_info) == STT_FILE)
3977c478bd9Sstevel@tonic-gate 			si->si_curfile = si->si_curname;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		if (GELF_ST_TYPE(sym.st_info) != type ||
4007c478bd9Sstevel@tonic-gate 		    sym.st_shndx == SHN_UNDEF)
4017c478bd9Sstevel@tonic-gate 			continue;
4027c478bd9Sstevel@tonic-gate 
4034d232658Sjohnlev 		if (check_sym && ignore_symbol(&sym, si->si_curname))
4044d232658Sjohnlev 			continue;
4054d232658Sjohnlev 
4067c478bd9Sstevel@tonic-gate 		si->si_next++;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		return (&si->si_cursym);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	return (NULL);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate char *
4157c478bd9Sstevel@tonic-gate symit_name(symit_data_t *si)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	return (si->si_curname);
4187c478bd9Sstevel@tonic-gate }
419