xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_gelf.c (revision 62b628a6)
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
5a576ab5bSrab  * Common Development and Distribution License (the "License").
6a576ab5bSrab  * 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 /*
227e16fca0SAli Bahrami  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
277c478bd9Sstevel@tonic-gate #include <sys/link.h>
287c478bd9Sstevel@tonic-gate #include <strings.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
327c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
337c478bd9Sstevel@tonic-gate #include <mdb/mdb_io_impl.h>
347c478bd9Sstevel@tonic-gate #include <mdb/mdb_gelf.h>
357c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
367c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #define	GST_GROW	2	/* Mutable symbol table growth multiplier */
397c478bd9Sstevel@tonic-gate #define	GST_DEFSZ	16	/* Mutable symbol table initial size */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	GST_NVFLG	(MDB_NV_EXTNAME | MDB_NV_SILENT)
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static const char *gelf_strtab;	/* Active string table for qsort callbacks */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static mdb_gelf_file_t *
gelf_sect_init(mdb_gelf_file_t * gf)467c478bd9Sstevel@tonic-gate gelf_sect_init(mdb_gelf_file_t *gf)
477c478bd9Sstevel@tonic-gate {
4830da1432Sahl 	mdb_gelf_sect_t *gsp, *shstr = &gf->gf_sects[gf->gf_shstrndx];
490e233487SRod Evans 	size_t i;
500e233487SRod Evans 	GElf_Half npbit = 0;
517c478bd9Sstevel@tonic-gate 	GElf_Shdr *shp;
527c478bd9Sstevel@tonic-gate 	GElf_Phdr *gpp;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	if (gf->gf_mode == GF_PROGRAM)
5530da1432Sahl 		gf->gf_shnum = 0; /* Simplifies other code paths */
567c478bd9Sstevel@tonic-gate 
5730da1432Sahl 	if (gf->gf_shnum == 0)
587c478bd9Sstevel@tonic-gate 		return (gf); /* If no section headers we're done here */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if (IOP_SEEK(gf->gf_io, shstr->gs_shdr.sh_offset, SEEK_SET) == -1) {
617c478bd9Sstevel@tonic-gate 		warn("failed to seek %s to shdr strings", IOP_NAME(gf->gf_io));
627c478bd9Sstevel@tonic-gate 		return (NULL);
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	shstr->gs_data = mdb_zalloc(shstr->gs_shdr.sh_size + 1, UM_SLEEP);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	if (IOP_READ(gf->gf_io, shstr->gs_data, shstr->gs_shdr.sh_size) !=
687c478bd9Sstevel@tonic-gate 	    shstr->gs_shdr.sh_size) {
697c478bd9Sstevel@tonic-gate 		warn("failed to read %s shdr strings", IOP_NAME(gf->gf_io));
707c478bd9Sstevel@tonic-gate 		mdb_free(shstr->gs_data, shstr->gs_shdr.sh_size);
717c478bd9Sstevel@tonic-gate 		return (NULL);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
7430da1432Sahl 	for (gsp = gf->gf_sects, i = 0; i < gf->gf_shnum; i++, gsp++) {
757c478bd9Sstevel@tonic-gate 		shp = &gsp->gs_shdr;
767c478bd9Sstevel@tonic-gate 		gsp->gs_name = (const char *)shstr->gs_data + shp->sh_name;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 		if (shp->sh_name >= shstr->gs_shdr.sh_size) {
797c478bd9Sstevel@tonic-gate 			warn("section name for %s:[%u] is corrupt: %u\n",
800e233487SRod Evans 			    IOP_NAME(gf->gf_io), i, shp->sh_name);
817c478bd9Sstevel@tonic-gate 			gsp->gs_name = shstr->gs_data; /* empty string */
827c478bd9Sstevel@tonic-gate 		}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 		if (shp->sh_type == SHT_PROGBITS && (shp->sh_flags & SHF_ALLOC))
857c478bd9Sstevel@tonic-gate 			npbit++; /* Keep count for ET_REL code below */
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * If the file is of type ET_REL, we would still like to provide file
907c478bd9Sstevel@tonic-gate 	 * i/o using the mdb_gelf_rw() function defined below.  To simplify
917c478bd9Sstevel@tonic-gate 	 * things, we forge up a sequence of Phdrs based on Shdrs which have
927c478bd9Sstevel@tonic-gate 	 * been marked SHF_ALLOC and are of type SHT_PROGBITS.  We convert
937c478bd9Sstevel@tonic-gate 	 * relevant Shdr fields to their Phdr equivalents, and then set the
947c478bd9Sstevel@tonic-gate 	 * p_vaddr (virtual base address) to the section's file offset.
957c478bd9Sstevel@tonic-gate 	 * This allows us to relocate a given symbol by simply incrementing
967c478bd9Sstevel@tonic-gate 	 * its st_value by the file offset of the section corresponding to
977c478bd9Sstevel@tonic-gate 	 * its st_shndx, and then perform i/o to read or write the symbol's
987c478bd9Sstevel@tonic-gate 	 * value in the object file.
997c478bd9Sstevel@tonic-gate 	 */
1007c478bd9Sstevel@tonic-gate 	if (gf->gf_ehdr.e_type == ET_REL && npbit != 0) {
1017c478bd9Sstevel@tonic-gate 		gf->gf_phdrs = mdb_zalloc(sizeof (GElf_Phdr) * npbit, UM_SLEEP);
10230da1432Sahl 		gf->gf_phnum = npbit;
1037c478bd9Sstevel@tonic-gate 		gf->gf_npload = npbit;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		gpp = gf->gf_phdrs;
1067c478bd9Sstevel@tonic-gate 		gsp = gf->gf_sects;
1077c478bd9Sstevel@tonic-gate 
10830da1432Sahl 		for (i = 0; i < gf->gf_shnum; i++, gsp++) {
1097c478bd9Sstevel@tonic-gate 			shp = &gsp->gs_shdr;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 			if ((shp->sh_type == SHT_PROGBITS) &&
1127c478bd9Sstevel@tonic-gate 			    (shp->sh_flags & SHF_ALLOC)) {
1137c478bd9Sstevel@tonic-gate 				gpp->p_type = PT_LOAD;
1147c478bd9Sstevel@tonic-gate 				gpp->p_flags = PF_R;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 				if (shp->sh_flags & SHF_EXECINSTR)
1177c478bd9Sstevel@tonic-gate 					gpp->p_flags |= PF_X;
1187c478bd9Sstevel@tonic-gate 				if (shp->sh_flags & SHF_WRITE)
1197c478bd9Sstevel@tonic-gate 					gpp->p_flags |= PF_W;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 				gpp->p_offset = shp->sh_offset;
1227c478bd9Sstevel@tonic-gate 				gpp->p_vaddr = shp->sh_offset;
1237c478bd9Sstevel@tonic-gate 				gpp->p_filesz = shp->sh_size;
1247c478bd9Sstevel@tonic-gate 				gpp->p_memsz = shp->sh_size;
1257c478bd9Sstevel@tonic-gate 				gpp->p_align = shp->sh_addralign;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 				gpp++;
1287c478bd9Sstevel@tonic-gate 			}
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	return (gf);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
135a576ab5bSrab void *
mdb_gelf_sect_load(mdb_gelf_file_t * gf,mdb_gelf_sect_t * gsp)136a576ab5bSrab mdb_gelf_sect_load(mdb_gelf_file_t *gf, mdb_gelf_sect_t *gsp)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	ssize_t nbytes;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (gsp->gs_data != NULL)
1417c478bd9Sstevel@tonic-gate 		return (gsp->gs_data);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_ELF, "loading %s:%s (%lu bytes)\n",
1447c478bd9Sstevel@tonic-gate 	    IOP_NAME(gf->gf_io), gsp->gs_name, (ulong_t)gsp->gs_shdr.sh_size);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	gsp->gs_data = mdb_alloc(gsp->gs_shdr.sh_size, UM_SLEEP);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (IOP_SEEK(gf->gf_io, gsp->gs_shdr.sh_offset, SEEK_SET) == -1) {
1497c478bd9Sstevel@tonic-gate 		warn("failed to seek to start of %s:%s",
1507c478bd9Sstevel@tonic-gate 		    IOP_NAME(gf->gf_io), gsp->gs_name);
1517c478bd9Sstevel@tonic-gate 		goto err;
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	nbytes = IOP_READ(gf->gf_io, gsp->gs_data, gsp->gs_shdr.sh_size);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (nbytes < 0) {
1577c478bd9Sstevel@tonic-gate 		warn("failed to read %s:%s", IOP_NAME(gf->gf_io), gsp->gs_name);
1587c478bd9Sstevel@tonic-gate 		goto err;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (nbytes < gsp->gs_shdr.sh_size) {
1627c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_ELF, "only %ld of %llu bytes of %s:%s "
1637c478bd9Sstevel@tonic-gate 		    "could be read\n", (long)nbytes, (u_longlong_t)
1647c478bd9Sstevel@tonic-gate 		    gsp->gs_shdr.sh_size, IOP_NAME(gf->gf_io), gsp->gs_name);
1657c478bd9Sstevel@tonic-gate 		bzero((char *)gsp->gs_data + nbytes,
1667c478bd9Sstevel@tonic-gate 		    (size_t)gsp->gs_shdr.sh_size - nbytes);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	return (gsp->gs_data);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate err:
1727c478bd9Sstevel@tonic-gate 	mdb_free(gsp->gs_data, sizeof (gsp->gs_shdr.sh_size));
1737c478bd9Sstevel@tonic-gate 	gsp->gs_data = NULL;
1747c478bd9Sstevel@tonic-gate 	return (NULL);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate void
mdb_gelf_ehdr_to_gehdr(Ehdr * src,GElf_Ehdr * dst)1787c478bd9Sstevel@tonic-gate mdb_gelf_ehdr_to_gehdr(Ehdr *src, GElf_Ehdr *dst)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	bcopy(src->e_ident, dst->e_ident, sizeof (dst->e_ident));
1817c478bd9Sstevel@tonic-gate 	dst->e_type = src->e_type;
1827c478bd9Sstevel@tonic-gate 	dst->e_machine = src->e_machine;
1837c478bd9Sstevel@tonic-gate 	dst->e_version = src->e_version;
1847c478bd9Sstevel@tonic-gate 	dst->e_entry = src->e_entry;
1857c478bd9Sstevel@tonic-gate 	dst->e_phoff = src->e_phoff;
1867c478bd9Sstevel@tonic-gate 	dst->e_shoff = src->e_shoff;
1877c478bd9Sstevel@tonic-gate 	dst->e_flags = src->e_flags;
1887c478bd9Sstevel@tonic-gate 	dst->e_ehsize = src->e_ehsize;
1897c478bd9Sstevel@tonic-gate 	dst->e_phentsize = src->e_phentsize;
1907c478bd9Sstevel@tonic-gate 	dst->e_phnum = src->e_phnum;
1917c478bd9Sstevel@tonic-gate 	dst->e_shentsize = src->e_shentsize;
1927c478bd9Sstevel@tonic-gate 	dst->e_shnum = src->e_shnum;
1937c478bd9Sstevel@tonic-gate 	dst->e_shstrndx = src->e_shstrndx;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static GElf_Shdr *
gelf32_to_shdr(const Elf32_Shdr * src,GElf_Shdr * dst)1977c478bd9Sstevel@tonic-gate gelf32_to_shdr(const Elf32_Shdr *src, GElf_Shdr *dst)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	if (src != NULL) {
2007c478bd9Sstevel@tonic-gate 		dst->sh_name = src->sh_name;
2017c478bd9Sstevel@tonic-gate 		dst->sh_type = src->sh_type;
2027c478bd9Sstevel@tonic-gate 		dst->sh_flags = src->sh_flags;
2037c478bd9Sstevel@tonic-gate 		dst->sh_addr = src->sh_addr;
2047c478bd9Sstevel@tonic-gate 		dst->sh_offset = src->sh_offset;
2057c478bd9Sstevel@tonic-gate 		dst->sh_size = src->sh_size;
2067c478bd9Sstevel@tonic-gate 		dst->sh_link = src->sh_link;
2077c478bd9Sstevel@tonic-gate 		dst->sh_info = src->sh_info;
2087c478bd9Sstevel@tonic-gate 		dst->sh_addralign = src->sh_addralign;
2097c478bd9Sstevel@tonic-gate 		dst->sh_entsize = src->sh_entsize;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		return (dst);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	return (NULL);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static GElf_Shdr *
gelf64_to_shdr(const Elf64_Shdr * src,GElf_Shdr * dst)2187c478bd9Sstevel@tonic-gate gelf64_to_shdr(const Elf64_Shdr *src, GElf_Shdr *dst)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	if (src != NULL) {
2217c478bd9Sstevel@tonic-gate 		bcopy(src, dst, sizeof (Elf64_Shdr));
2227c478bd9Sstevel@tonic-gate 		return (dst);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	return (NULL);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate static mdb_gelf_file_t *
gelf_shdrs_init(mdb_gelf_file_t * gf,size_t shdr_size,GElf_Shdr * (* elf2gelf)(const void *,GElf_Shdr *))2297c478bd9Sstevel@tonic-gate gelf_shdrs_init(mdb_gelf_file_t *gf, size_t shdr_size,
2307c478bd9Sstevel@tonic-gate     GElf_Shdr *(*elf2gelf)(const void *, GElf_Shdr *))
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	caddr_t shdrs, shp;
2330e233487SRod Evans 	size_t i;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	mdb_gelf_sect_t *gsp;
2367c478bd9Sstevel@tonic-gate 	size_t nbytes;
2377c478bd9Sstevel@tonic-gate 
2380e233487SRod Evans 	mdb_dprintf(MDB_DBG_ELF, "loading %s section headers (%u entries)\n",
23930da1432Sahl 	    IOP_NAME(gf->gf_io), gf->gf_shnum);
2407c478bd9Sstevel@tonic-gate 
24130da1432Sahl 	if (gf->gf_shnum == 0)
2427c478bd9Sstevel@tonic-gate 		return (gf);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (IOP_SEEK(gf->gf_io, (off64_t)gf->gf_ehdr.e_shoff, SEEK_SET) == -1) {
2457c478bd9Sstevel@tonic-gate 		warn("failed to seek %s to shdrs", IOP_NAME(gf->gf_io));
2467c478bd9Sstevel@tonic-gate 		return (NULL);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
24930da1432Sahl 	nbytes = shdr_size * gf->gf_shnum;
2507c478bd9Sstevel@tonic-gate 	shdrs = mdb_alloc(nbytes, UM_SLEEP);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (IOP_READ(gf->gf_io, shdrs, nbytes) != nbytes) {
2537c478bd9Sstevel@tonic-gate 		warn("failed to read %s section headers", IOP_NAME(gf->gf_io));
2547c478bd9Sstevel@tonic-gate 		mdb_free(shdrs, nbytes);
2557c478bd9Sstevel@tonic-gate 		return (NULL);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
25830da1432Sahl 	gf->gf_sects = mdb_zalloc(sizeof (mdb_gelf_sect_t) * gf->gf_shnum,
25930da1432Sahl 	    UM_SLEEP);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	shp = shdrs;
2627c478bd9Sstevel@tonic-gate 	gsp = gf->gf_sects;
2637c478bd9Sstevel@tonic-gate 
26430da1432Sahl 	for (i = 0; i < gf->gf_shnum; i++, shp += shdr_size, gsp++)
2657c478bd9Sstevel@tonic-gate 		(void) elf2gelf(shp, &gsp->gs_shdr);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	mdb_free(shdrs, nbytes);
2687c478bd9Sstevel@tonic-gate 	return (gf);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate static GElf_Phdr *
gelf32_to_phdr(const Elf32_Phdr * src,GElf_Phdr * dst)2727c478bd9Sstevel@tonic-gate gelf32_to_phdr(const Elf32_Phdr *src, GElf_Phdr *dst)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	if (src != NULL) {
2757c478bd9Sstevel@tonic-gate 		dst->p_type = src->p_type;
2767c478bd9Sstevel@tonic-gate 		dst->p_offset = src->p_offset;
2777c478bd9Sstevel@tonic-gate 		dst->p_vaddr = src->p_vaddr;
2787c478bd9Sstevel@tonic-gate 		dst->p_paddr = src->p_paddr;
2797c478bd9Sstevel@tonic-gate 		dst->p_filesz = src->p_filesz;
2807c478bd9Sstevel@tonic-gate 		dst->p_memsz = src->p_memsz;
2817c478bd9Sstevel@tonic-gate 		dst->p_flags = src->p_flags;
2827c478bd9Sstevel@tonic-gate 		dst->p_align = src->p_align;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		return (dst);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	return (NULL);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate static GElf_Phdr *
gelf64_to_phdr(const Elf64_Phdr * src,GElf_Phdr * dst)2917c478bd9Sstevel@tonic-gate gelf64_to_phdr(const Elf64_Phdr *src, GElf_Phdr *dst)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	if (src != NULL) {
2947c478bd9Sstevel@tonic-gate 		bcopy(src, dst, sizeof (Elf64_Phdr));
2957c478bd9Sstevel@tonic-gate 		return (dst);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	return (NULL);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static int
gelf_phdr_compare(const void * lp,const void * rp)3027c478bd9Sstevel@tonic-gate gelf_phdr_compare(const void *lp, const void *rp)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	GElf_Phdr *lhs = (GElf_Phdr *)lp;
3057c478bd9Sstevel@tonic-gate 	GElf_Phdr *rhs = (GElf_Phdr *)rp;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * If both p_type fields are PT_LOAD, we want to sort by vaddr.
3097c478bd9Sstevel@tonic-gate 	 * Exception is that p_vaddr == 0 means ignore this (put at end).
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	if (lhs->p_type == PT_LOAD && rhs->p_type == PT_LOAD) {
3127c478bd9Sstevel@tonic-gate 		if (lhs->p_vaddr != rhs->p_vaddr) {
3137c478bd9Sstevel@tonic-gate 			if (lhs->p_vaddr == 0)
3147c478bd9Sstevel@tonic-gate 				return (1); /* lhs is "greater" */
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 			if (rhs->p_vaddr == 0)
3177c478bd9Sstevel@tonic-gate 				return (-1); /* rhs is "greater" */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 			return (lhs->p_vaddr > rhs->p_vaddr ? 1 : -1);
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		return (0);
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	/*
3267c478bd9Sstevel@tonic-gate 	 * If the p_type fields don't match, we need to make sure that PT_LOAD
3277c478bd9Sstevel@tonic-gate 	 * entries are considered "less" (i.e. move towards the beginning
3287c478bd9Sstevel@tonic-gate 	 * of the array we are sorting)
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 	if (lhs->p_type != rhs->p_type) {
3317c478bd9Sstevel@tonic-gate 		if (lhs->p_type == PT_LOAD)
3327c478bd9Sstevel@tonic-gate 			return (-1); /* rhs is "greater" */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		if (rhs->p_type == PT_LOAD)
3357c478bd9Sstevel@tonic-gate 			return (1); /* lhs is "greater" */
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		return (lhs->p_type > rhs->p_type ? 1 : -1);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/*
3417c478bd9Sstevel@tonic-gate 	 * If the p_type is the same but neither is PT_LOAD, then
3427c478bd9Sstevel@tonic-gate 	 * just sort by file offset (doesn't really matter)
3437c478bd9Sstevel@tonic-gate 	 */
3447c478bd9Sstevel@tonic-gate 	if (lhs->p_offset != rhs->p_offset)
3457c478bd9Sstevel@tonic-gate 		return (lhs->p_offset > rhs->p_offset ? 1 : -1);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	return (0);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate static mdb_gelf_file_t *
gelf_phdrs_init(mdb_gelf_file_t * gf,size_t phdr_size,GElf_Phdr * (* elf2gelf)(const void *,GElf_Phdr *))3517c478bd9Sstevel@tonic-gate gelf_phdrs_init(mdb_gelf_file_t *gf, size_t phdr_size,
3527c478bd9Sstevel@tonic-gate     GElf_Phdr *(*elf2gelf)(const void *, GElf_Phdr *))
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	caddr_t phdrs, php;
3550e233487SRod Evans 	size_t i;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	GElf_Phdr *gpp;
3587c478bd9Sstevel@tonic-gate 	size_t nbytes;
3597c478bd9Sstevel@tonic-gate 
36030da1432Sahl 	mdb_dprintf(MDB_DBG_ELF, "loading %s program headers (%lu entries)\n",
36130da1432Sahl 	    IOP_NAME(gf->gf_io), gf->gf_phnum);
3627c478bd9Sstevel@tonic-gate 
36330da1432Sahl 	if (gf->gf_phnum == 0)
3647c478bd9Sstevel@tonic-gate 		return (gf);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (IOP_SEEK(gf->gf_io, (off64_t)gf->gf_ehdr.e_phoff, SEEK_SET) == -1) {
3677c478bd9Sstevel@tonic-gate 		warn("failed to seek %s to phdrs", IOP_NAME(gf->gf_io));
3687c478bd9Sstevel@tonic-gate 		return (NULL);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
37130da1432Sahl 	nbytes = phdr_size * gf->gf_phnum;
3727c478bd9Sstevel@tonic-gate 	phdrs = mdb_alloc(nbytes, UM_SLEEP);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (IOP_READ(gf->gf_io, phdrs, nbytes) != nbytes) {
3757c478bd9Sstevel@tonic-gate 		warn("failed to read %s program headers", IOP_NAME(gf->gf_io));
3767c478bd9Sstevel@tonic-gate 		mdb_free(phdrs, nbytes);
3777c478bd9Sstevel@tonic-gate 		return (NULL);
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
38030da1432Sahl 	gf->gf_phdrs = mdb_zalloc(sizeof (GElf_Phdr) * gf->gf_phnum, UM_SLEEP);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	php = phdrs;
3837c478bd9Sstevel@tonic-gate 	gpp = gf->gf_phdrs;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/*
3867c478bd9Sstevel@tonic-gate 	 * Iterate through the list of phdrs locating those that are of type
3877c478bd9Sstevel@tonic-gate 	 * PT_LOAD; increment gf_npload so we know how many are loadable.
3887c478bd9Sstevel@tonic-gate 	 */
38930da1432Sahl 	for (i = 0; i < gf->gf_phnum; i++, php += phdr_size, gpp++) {
3907c478bd9Sstevel@tonic-gate 		(void) elf2gelf(php, gpp);
3917c478bd9Sstevel@tonic-gate 		if (gpp->p_type != PT_LOAD)
3927c478bd9Sstevel@tonic-gate 			continue;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_ELF, "PT_LOAD va=0x%llx flags=0x%x "
3957c478bd9Sstevel@tonic-gate 		    "memsz=%llu filesz=%llu off=%llu\n", (u_longlong_t)
3967c478bd9Sstevel@tonic-gate 		    gpp->p_vaddr, gpp->p_flags, (u_longlong_t)gpp->p_memsz,
3977c478bd9Sstevel@tonic-gate 		    (u_longlong_t)gpp->p_filesz, (u_longlong_t)gpp->p_offset);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		gf->gf_npload++;
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * Now we sort the phdrs array using a comparison routine which
4047c478bd9Sstevel@tonic-gate 	 * arranges for the PT_LOAD phdrs with non-zero virtual addresses
4057c478bd9Sstevel@tonic-gate 	 * to come first sorted by virtual address.  This means that we
4067c478bd9Sstevel@tonic-gate 	 * can access the complete phdr table by examining the array
40730da1432Sahl 	 * gf->gf_phdrs[0 .. gf->gf_phnum - 1], and we can access a sorted
40830da1432Sahl 	 * array of valid PT_LOAD pdhrs by examining the array
4097c478bd9Sstevel@tonic-gate 	 * gf->gf_phdrs[0 .. gf->gf_npload - 1].
4107c478bd9Sstevel@tonic-gate 	 */
41130da1432Sahl 	qsort(gf->gf_phdrs, gf->gf_phnum, sizeof (GElf_Phdr),
4127c478bd9Sstevel@tonic-gate 	    gelf_phdr_compare);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * Locate the PT_DYNAMIC Phdr if one is present; we save this
4167c478bd9Sstevel@tonic-gate 	 * Phdr pointer in gf->gf_dynp for future use.
4177c478bd9Sstevel@tonic-gate 	 */
41830da1432Sahl 	for (gpp = gf->gf_phdrs, i = 0; i < gf->gf_phnum; i++, gpp++) {
4197c478bd9Sstevel@tonic-gate 		if (gpp->p_type == PT_DYNAMIC) {
4207c478bd9Sstevel@tonic-gate 			mdb_dprintf(MDB_DBG_ELF, "PT_DYNAMIC "
4217c478bd9Sstevel@tonic-gate 			    "filesize = %lluULL off=%lluULL\n",
4227c478bd9Sstevel@tonic-gate 			    (u_longlong_t)gpp->p_filesz,
4237c478bd9Sstevel@tonic-gate 			    (u_longlong_t)gpp->p_offset);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 			gf->gf_dynp = gpp;
4267c478bd9Sstevel@tonic-gate 			break;
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	mdb_free(phdrs, nbytes);
4317c478bd9Sstevel@tonic-gate 	return (gf);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate static GElf_Dyn *
gelf32_to_dyn(const Elf32_Dyn * src,GElf_Dyn * dst)4357c478bd9Sstevel@tonic-gate gelf32_to_dyn(const Elf32_Dyn *src, GElf_Dyn *dst)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	if (src != NULL) {
4387c478bd9Sstevel@tonic-gate 		dst->d_tag = (GElf_Xword)(Elf32_Word)src->d_tag;
4397c478bd9Sstevel@tonic-gate 		dst->d_un.d_ptr = src->d_un.d_ptr;
4407c478bd9Sstevel@tonic-gate 		return (dst);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	return (NULL);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate static GElf_Dyn *
gelf64_to_dyn(const Elf64_Dyn * src,GElf_Dyn * dst)4477c478bd9Sstevel@tonic-gate gelf64_to_dyn(const Elf64_Dyn *src, GElf_Dyn *dst)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	if (src != NULL) {
4507c478bd9Sstevel@tonic-gate 		bcopy(src, dst, sizeof (Elf64_Dyn));
4517c478bd9Sstevel@tonic-gate 		return (dst);
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	return (NULL);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate static GElf_Xword
gelf_dyn_lookup(mdb_gelf_file_t * gf,GElf_Xword tag)4587c478bd9Sstevel@tonic-gate gelf_dyn_lookup(mdb_gelf_file_t *gf, GElf_Xword tag)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	size_t i;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	for (i = 0; i < gf->gf_ndyns; i++) {
4637c478bd9Sstevel@tonic-gate 		if (gf->gf_dyns[i].d_tag == tag)
4647c478bd9Sstevel@tonic-gate 			return (gf->gf_dyns[i].d_un.d_val);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	return ((GElf_Xword)-1L);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
47022872efbSedp void
mdb_gelf_dyns_set(mdb_gelf_file_t * gf,void * dyns,size_t dyns_sz)47122872efbSedp mdb_gelf_dyns_set(mdb_gelf_file_t *gf, void *dyns, size_t dyns_sz)
47222872efbSedp {
47322872efbSedp 	size_t ndyns, i, dyn_size;
47422872efbSedp 	caddr_t dp;
47522872efbSedp 	GElf_Dyn *gdp;
47622872efbSedp 
47722872efbSedp 	if (gf->gf_dyns != NULL) {
47822872efbSedp 		/* Free the existing dyn entries */
47922872efbSedp 		free(gf->gf_dyns);
48022872efbSedp 		gf->gf_dyns = NULL;
48122872efbSedp 		gf->gf_ndyns = 0;
48222872efbSedp 	}
48322872efbSedp 
48422872efbSedp 	if (gf->gf_ehdr.e_ident[EI_CLASS] == ELFCLASS32)
48522872efbSedp 		dyn_size = sizeof (Elf32_Dyn);
48622872efbSedp 	else
48722872efbSedp 		dyn_size = sizeof (Elf64_Dyn);
48822872efbSedp 
48922872efbSedp 	ndyns = dyns_sz / dyn_size;
49022872efbSedp 	gf->gf_dyns = mdb_zalloc(sizeof (GElf_Dyn) * ndyns, UM_SLEEP);
49122872efbSedp 	gf->gf_ndyns = ndyns;
49222872efbSedp 
49322872efbSedp 	dp = dyns;
49422872efbSedp 	gdp = gf->gf_dyns;
49522872efbSedp 
49622872efbSedp 	if (gf->gf_ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
49722872efbSedp 		for (i = 0; i < ndyns; i++, dp += dyn_size, gdp++) {
49822872efbSedp 			/* LINTED - alignment */
49922872efbSedp 			(void) gelf32_to_dyn((Elf32_Dyn *)dp, gdp);
50022872efbSedp 		}
50122872efbSedp 	} else {
50222872efbSedp 		for (i = 0; i < ndyns; i++, dp += dyn_size, gdp++) {
50322872efbSedp 			/* LINTED - alignment */
50422872efbSedp 			(void) gelf64_to_dyn((Elf64_Dyn *)dp, gdp);
50522872efbSedp 		}
50622872efbSedp 	}
50722872efbSedp }
50822872efbSedp 
5097c478bd9Sstevel@tonic-gate static GElf_Dyn *
gelf_dyns_init(mdb_gelf_file_t * gf,size_t dyn_size,GElf_Dyn * (* elf2gelf)(const void *,GElf_Dyn *))5107c478bd9Sstevel@tonic-gate gelf_dyns_init(mdb_gelf_file_t *gf, size_t dyn_size,
5117c478bd9Sstevel@tonic-gate     GElf_Dyn *(*elf2gelf)(const void *, GElf_Dyn *))
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	size_t nbytes, ndyns, i;
5147c478bd9Sstevel@tonic-gate 	caddr_t dyns, dp;
5157c478bd9Sstevel@tonic-gate 	GElf_Dyn *gdp;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	off64_t dyn_addr;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if (gf->gf_dyns != NULL)
5207c478bd9Sstevel@tonic-gate 		return (gf->gf_dyns);	/* Already loaded */
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (gf->gf_dynp == NULL)
5237c478bd9Sstevel@tonic-gate 		return (NULL);		/* No PT_DYNAMIC entry was found */
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	nbytes = gf->gf_dynp->p_filesz;
5267c478bd9Sstevel@tonic-gate 	ndyns = nbytes / dyn_size;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	/*
5297c478bd9Sstevel@tonic-gate 	 * If this is an executable in PROGRAM view, then p_vaddr is an
5307c478bd9Sstevel@tonic-gate 	 * absolute address; we need to subtract the virtual base address of
5317c478bd9Sstevel@tonic-gate 	 * the mapping.  In FILE view, dyn_addr is just the file offset.
5327c478bd9Sstevel@tonic-gate 	 */
5337c478bd9Sstevel@tonic-gate 	if (gf->gf_mode == GF_PROGRAM) {
5347c478bd9Sstevel@tonic-gate 		if (gf->gf_ehdr.e_type == ET_EXEC && gf->gf_npload != 0)
5357c478bd9Sstevel@tonic-gate 			dyn_addr = gf->gf_dynp->p_vaddr - gf->gf_phdrs->p_vaddr;
5367c478bd9Sstevel@tonic-gate 		else
5377c478bd9Sstevel@tonic-gate 			dyn_addr = gf->gf_dynp->p_vaddr;
5387c478bd9Sstevel@tonic-gate 	} else {
5397c478bd9Sstevel@tonic-gate 		mdb_gelf_sect_t *gsp = gf->gf_sects;
5407c478bd9Sstevel@tonic-gate 
54130da1432Sahl 		for (i = 0; i < gf->gf_shnum; i++, gsp++) {
5427c478bd9Sstevel@tonic-gate 			if (gsp->gs_shdr.sh_type == SHT_DYNAMIC) {
5437c478bd9Sstevel@tonic-gate 				dyn_addr = gsp->gs_shdr.sh_offset;
5447c478bd9Sstevel@tonic-gate 				break;
5457c478bd9Sstevel@tonic-gate 			}
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 
54830da1432Sahl 		if (i == gf->gf_shnum)
5497c478bd9Sstevel@tonic-gate 			return (NULL); /* No SHT_DYNAMIC entry was found */
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_ELF, "loading _DYNAMIC[] (%lu entries) "
5537c478bd9Sstevel@tonic-gate 	    "from offset %llx\n", (ulong_t)ndyns, (longlong_t)dyn_addr);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (IOP_SEEK(gf->gf_io, dyn_addr, SEEK_SET) == -1) {
5567c478bd9Sstevel@tonic-gate 		warn("failed to seek %s to _DYNAMIC", IOP_NAME(gf->gf_io));
5577c478bd9Sstevel@tonic-gate 		return (NULL);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	dyns = mdb_alloc(nbytes, UM_SLEEP);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if (IOP_READ(gf->gf_io, dyns, nbytes) != nbytes) {
5637c478bd9Sstevel@tonic-gate 		warn("failed to read %s:_DYNAMIC", IOP_NAME(gf->gf_io));
5647c478bd9Sstevel@tonic-gate 		mdb_free(dyns, nbytes);
5657c478bd9Sstevel@tonic-gate 		return (NULL);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	gf->gf_dyns = mdb_zalloc(sizeof (GElf_Dyn) * ndyns, UM_SLEEP);
5697c478bd9Sstevel@tonic-gate 	gf->gf_ndyns = ndyns;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	dp = dyns;
5727c478bd9Sstevel@tonic-gate 	gdp = gf->gf_dyns;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndyns; i++, dp += dyn_size, gdp++)
5757c478bd9Sstevel@tonic-gate 		(void) elf2gelf(dp, gdp);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	mdb_free(dyns, nbytes);
5787c478bd9Sstevel@tonic-gate 	return (gf->gf_dyns);
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate static mdb_gelf_file_t *
gelf32_init(mdb_gelf_file_t * gf,mdb_io_t * io,const Elf32_Ehdr * ehdr)58230da1432Sahl gelf32_init(mdb_gelf_file_t *gf, mdb_io_t *io, const Elf32_Ehdr *ehdr)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * Convert the Elf32_Ehdr to a GElf_Ehdr
5867c478bd9Sstevel@tonic-gate 	 */
5877c478bd9Sstevel@tonic-gate 	bcopy(ehdr->e_ident, gf->gf_ehdr.e_ident, EI_NIDENT);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_type = ehdr->e_type;
5907c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_machine = ehdr->e_machine;
5917c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_version = ehdr->e_version;
5927c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_entry = ehdr->e_entry;
5937c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_phoff = ehdr->e_phoff;
5947c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_shoff = ehdr->e_shoff;
5957c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_flags = ehdr->e_flags;
5967c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_ehsize = ehdr->e_ehsize;
5977c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_phentsize = ehdr->e_phentsize;
5987c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_phnum = ehdr->e_phnum;
5997c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_shentsize = ehdr->e_shentsize;
6007c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_shnum = ehdr->e_shnum;
6017c478bd9Sstevel@tonic-gate 	gf->gf_ehdr.e_shstrndx = ehdr->e_shstrndx;
6027c478bd9Sstevel@tonic-gate 
60330da1432Sahl 	gf->gf_shnum = gf->gf_ehdr.e_shnum;
60430da1432Sahl 	gf->gf_shstrndx = gf->gf_ehdr.e_shstrndx;
60530da1432Sahl 	gf->gf_phnum = gf->gf_ehdr.e_phnum;
60630da1432Sahl 
60730da1432Sahl 	if ((gf->gf_shnum == 0 && ehdr->e_shoff != 0) ||
60830da1432Sahl 	    gf->gf_shstrndx == SHN_XINDEX || gf->gf_phnum == PN_XNUM) {
60930da1432Sahl 		Elf32_Shdr shdr0;
61030da1432Sahl 
61130da1432Sahl 		if (ehdr->e_shoff == 0)
61230da1432Sahl 			return (NULL);
61330da1432Sahl 
61430da1432Sahl 		if (IOP_SEEK(io, (off64_t)ehdr->e_shoff, SEEK_SET) == -1) {
61530da1432Sahl 			warn("failed to seek %s", IOP_NAME(io));
61630da1432Sahl 			return (NULL);
61730da1432Sahl 		}
61830da1432Sahl 
61930da1432Sahl 		if (IOP_READ(io, &shdr0, sizeof (shdr0)) != sizeof (shdr0)) {
62030da1432Sahl 			warn("failed to read extended ELF header from %s",
62130da1432Sahl 			    IOP_NAME(io));
62230da1432Sahl 			return (NULL);
62330da1432Sahl 		}
62430da1432Sahl 
62530da1432Sahl 		if (gf->gf_shnum == 0)
62630da1432Sahl 			gf->gf_shnum = shdr0.sh_size;
62730da1432Sahl 
62830da1432Sahl 		if (gf->gf_shstrndx == SHN_XINDEX)
62930da1432Sahl 			gf->gf_shstrndx = shdr0.sh_link;
63030da1432Sahl 
63130da1432Sahl 		if (gf->gf_phnum == PN_XNUM)
63230da1432Sahl 			gf->gf_phnum = shdr0.sh_info;
63330da1432Sahl 	}
63430da1432Sahl 
6357c478bd9Sstevel@tonic-gate 	/*
6367c478bd9Sstevel@tonic-gate 	 * Initialize the section and program headers.  We skip initializing
6377c478bd9Sstevel@tonic-gate 	 * the section headers if this is a program image because they are
6387c478bd9Sstevel@tonic-gate 	 * not loadable and thus we can't get at them.
6397c478bd9Sstevel@tonic-gate 	 */
6407c478bd9Sstevel@tonic-gate 	if (gf->gf_mode == GF_FILE && gelf_shdrs_init(gf, sizeof (Elf32_Shdr),
6417c478bd9Sstevel@tonic-gate 	    (GElf_Shdr *(*)(const void *, GElf_Shdr *))gelf32_to_shdr) == NULL)
6427c478bd9Sstevel@tonic-gate 		return (NULL);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	if (gelf_phdrs_init(gf, sizeof (Elf32_Phdr),
6457c478bd9Sstevel@tonic-gate 	    (GElf_Phdr *(*)(const void *, GElf_Phdr *))gelf32_to_phdr) == NULL)
6467c478bd9Sstevel@tonic-gate 		return (NULL);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	(void) gelf_dyns_init(gf, sizeof (Elf32_Dyn),
6497c478bd9Sstevel@tonic-gate 	    (GElf_Dyn *(*)(const void *, GElf_Dyn *))gelf32_to_dyn);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	return (gf);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate static mdb_gelf_file_t *
gelf64_init(mdb_gelf_file_t * gf,mdb_io_t * io,Elf64_Ehdr * ehdr)65530da1432Sahl gelf64_init(mdb_gelf_file_t *gf, mdb_io_t *io, Elf64_Ehdr *ehdr)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	/*
6587c478bd9Sstevel@tonic-gate 	 * Save a copy of the ELF file header
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	bcopy(ehdr, &gf->gf_ehdr, sizeof (Elf64_Ehdr));
6617c478bd9Sstevel@tonic-gate 
66230da1432Sahl 	gf->gf_shnum = gf->gf_ehdr.e_shnum;
66330da1432Sahl 	gf->gf_shstrndx = gf->gf_ehdr.e_shstrndx;
66430da1432Sahl 	gf->gf_phnum = gf->gf_ehdr.e_phnum;
66530da1432Sahl 
66630da1432Sahl 	if ((gf->gf_shnum == 0 && ehdr->e_shoff != 0) ||
66730da1432Sahl 	    gf->gf_shstrndx == SHN_XINDEX || gf->gf_phnum == PN_XNUM) {
66830da1432Sahl 		Elf64_Shdr shdr0;
66930da1432Sahl 
67030da1432Sahl 		if (ehdr->e_shoff == 0)
67130da1432Sahl 			return (NULL);
67230da1432Sahl 
67330da1432Sahl 		if (IOP_SEEK(io, (off64_t)ehdr->e_shoff, SEEK_SET) == -1) {
67430da1432Sahl 			warn("failed to seek %s", IOP_NAME(io));
67530da1432Sahl 			return (NULL);
67630da1432Sahl 		}
67730da1432Sahl 
67830da1432Sahl 		if (IOP_READ(io, &shdr0, sizeof (shdr0)) != sizeof (shdr0)) {
67930da1432Sahl 			warn("failed to read extended ELF header from %s",
68030da1432Sahl 			    IOP_NAME(io));
68130da1432Sahl 			return (NULL);
68230da1432Sahl 		}
68330da1432Sahl 
68430da1432Sahl 		if (gf->gf_shnum == 0)
68530da1432Sahl 			gf->gf_shnum = shdr0.sh_size;
68630da1432Sahl 
68730da1432Sahl 		if (gf->gf_shstrndx == SHN_XINDEX)
68830da1432Sahl 			gf->gf_shstrndx = shdr0.sh_link;
68930da1432Sahl 
69030da1432Sahl 		if (gf->gf_phnum == PN_XNUM)
69130da1432Sahl 			gf->gf_phnum = shdr0.sh_info;
69230da1432Sahl 	}
69330da1432Sahl 
6947c478bd9Sstevel@tonic-gate 	/*
6957c478bd9Sstevel@tonic-gate 	 * Initialize the section and program headers.  We skip initializing
6967c478bd9Sstevel@tonic-gate 	 * the section headers if this is a program image because they are
6977c478bd9Sstevel@tonic-gate 	 * not loadable and thus we can't get at them.
6987c478bd9Sstevel@tonic-gate 	 */
6997c478bd9Sstevel@tonic-gate 	if (gf->gf_mode == GF_FILE && gelf_shdrs_init(gf, sizeof (Elf64_Shdr),
7007c478bd9Sstevel@tonic-gate 	    (GElf_Shdr *(*)(const void *, GElf_Shdr *))gelf64_to_shdr) == NULL)
7017c478bd9Sstevel@tonic-gate 		return (NULL);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	if (gelf_phdrs_init(gf, sizeof (Elf64_Phdr),
7047c478bd9Sstevel@tonic-gate 	    (GElf_Phdr *(*)(const void *, GElf_Phdr *))gelf64_to_phdr) == NULL)
7057c478bd9Sstevel@tonic-gate 		return (NULL);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	(void) gelf_dyns_init(gf, sizeof (Elf64_Dyn),
7087c478bd9Sstevel@tonic-gate 	    (GElf_Dyn *(*)(const void *, GElf_Dyn *))gelf64_to_dyn);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	return (gf);
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate int
mdb_gelf_check(mdb_io_t * io,Elf32_Ehdr * ehp,GElf_Half etype)7147c478bd9Sstevel@tonic-gate mdb_gelf_check(mdb_io_t *io, Elf32_Ehdr *ehp, GElf_Half etype)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
7177c478bd9Sstevel@tonic-gate 	uchar_t order = ELFDATA2MSB;
7187c478bd9Sstevel@tonic-gate #else
7197c478bd9Sstevel@tonic-gate 	uchar_t order = ELFDATA2LSB;
7207c478bd9Sstevel@tonic-gate #endif
7217c478bd9Sstevel@tonic-gate 	ssize_t nbytes;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	(void) IOP_SEEK(io, (off64_t)0L, SEEK_SET);
7247c478bd9Sstevel@tonic-gate 	nbytes = IOP_READ(io, ehp, sizeof (Elf32_Ehdr));
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (nbytes == -1) {
7277c478bd9Sstevel@tonic-gate 		if (etype != ET_NONE)
7287c478bd9Sstevel@tonic-gate 			warn("failed to read ELF header from %s", IOP_NAME(io));
7297c478bd9Sstevel@tonic-gate 		return (-1);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	if (nbytes != sizeof (Elf32_Ehdr) ||
7337c478bd9Sstevel@tonic-gate 	    bcmp(&ehp->e_ident[EI_MAG0], ELFMAG, SELFMAG) != 0) {
7347c478bd9Sstevel@tonic-gate 		if (etype != ET_NONE)
7357c478bd9Sstevel@tonic-gate 			warn("%s is not an ELF file\n", IOP_NAME(io));
7367c478bd9Sstevel@tonic-gate 		return (-1);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (ehp->e_ident[EI_DATA] != order) {
7407c478bd9Sstevel@tonic-gate 		warn("ELF file %s has different endianness from debugger\n",
7417c478bd9Sstevel@tonic-gate 		    IOP_NAME(io));
7427c478bd9Sstevel@tonic-gate 		return (-1);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	if (ehp->e_version != EV_CURRENT) {
7467c478bd9Sstevel@tonic-gate 		warn("ELF file %s uses different ELF version (%lu) than "
7477c478bd9Sstevel@tonic-gate 		    "debugger (%u)\n", IOP_NAME(io),
7487c478bd9Sstevel@tonic-gate 		    (ulong_t)ehp->e_version, EV_CURRENT);
7497c478bd9Sstevel@tonic-gate 		return (-1);
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	if (etype != ET_NONE && ehp->e_type != etype) {
7537c478bd9Sstevel@tonic-gate 		warn("ELF file %s is not of the expected type\n", IOP_NAME(io));
7547c478bd9Sstevel@tonic-gate 		return (-1);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	return (0);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate mdb_gelf_file_t *
mdb_gelf_create(mdb_io_t * io,GElf_Half etype,int mode)7617c478bd9Sstevel@tonic-gate mdb_gelf_create(mdb_io_t *io, GElf_Half etype, int mode)
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	union {
7647c478bd9Sstevel@tonic-gate 		Elf32_Ehdr h32;
7657c478bd9Sstevel@tonic-gate 		Elf64_Ehdr h64;
7667c478bd9Sstevel@tonic-gate 	} ehdr;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	mdb_gelf_file_t *gf = mdb_zalloc(sizeof (mdb_gelf_file_t), UM_SLEEP);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	ASSERT(mode == GF_FILE || mode == GF_PROGRAM);
7717c478bd9Sstevel@tonic-gate 	gf->gf_mode = mode;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	/*
7747c478bd9Sstevel@tonic-gate 	 * Assign the i/o backend now, but don't hold it until we're sure
7757c478bd9Sstevel@tonic-gate 	 * we're going to succeed; otherwise the caller will be responsible
7767c478bd9Sstevel@tonic-gate 	 * for mdb_io_destroy()ing it.
7777c478bd9Sstevel@tonic-gate 	 */
7787c478bd9Sstevel@tonic-gate 	gf->gf_io = io;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	if (mdb_gelf_check(io, &ehdr.h32, etype) == -1)
7817c478bd9Sstevel@tonic-gate 		goto err;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	switch (ehdr.h32.e_ident[EI_CLASS]) {
7847c478bd9Sstevel@tonic-gate 	case ELFCLASS32:
78530da1432Sahl 		gf = gelf32_init(gf, io, &ehdr.h32);
7867c478bd9Sstevel@tonic-gate 		break;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	case ELFCLASS64:
7897c478bd9Sstevel@tonic-gate 		if (IOP_SEEK(io, (off64_t)0L, SEEK_SET) == -1) {
7907c478bd9Sstevel@tonic-gate 			warn("failed to seek %s", IOP_NAME(io));
7917c478bd9Sstevel@tonic-gate 			goto err;
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		if (IOP_READ(io, &ehdr.h64, sizeof (ehdr.h64)) !=
7957c478bd9Sstevel@tonic-gate 		    sizeof (ehdr.h64)) {
7967c478bd9Sstevel@tonic-gate 			warn("failed to read ELF header from %s", IOP_NAME(io));
7977c478bd9Sstevel@tonic-gate 			goto err;
7987c478bd9Sstevel@tonic-gate 		}
7997c478bd9Sstevel@tonic-gate 
80030da1432Sahl 		gf = gelf64_init(gf, io, &ehdr.h64);
8017c478bd9Sstevel@tonic-gate 		break;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	default:
8047c478bd9Sstevel@tonic-gate 		warn("%s is an unsupported ELF class: %u\n",
8057c478bd9Sstevel@tonic-gate 		    IOP_NAME(io), ehdr.h32.e_ident[EI_CLASS]);
8067c478bd9Sstevel@tonic-gate 		goto err;
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	if (gf != NULL && gelf_sect_init(gf) != NULL) {
8107c478bd9Sstevel@tonic-gate 		gf->gf_io = mdb_io_hold(io);
8117c478bd9Sstevel@tonic-gate 		return (gf);
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate err:
8157c478bd9Sstevel@tonic-gate 	if (gf != NULL) {
8167c478bd9Sstevel@tonic-gate 		if (gf->gf_sects != NULL) {
81730da1432Sahl 			mdb_free(gf->gf_sects, gf->gf_shnum *
8187c478bd9Sstevel@tonic-gate 			    sizeof (mdb_gelf_sect_t));
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 		mdb_free(gf, sizeof (mdb_gelf_file_t));
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 	return (NULL);
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate void
mdb_gelf_destroy(mdb_gelf_file_t * gf)8267c478bd9Sstevel@tonic-gate mdb_gelf_destroy(mdb_gelf_file_t *gf)
8277c478bd9Sstevel@tonic-gate {
8287c478bd9Sstevel@tonic-gate 	mdb_gelf_sect_t *gsp;
8290e233487SRod Evans 	size_t i;
8307c478bd9Sstevel@tonic-gate 
83130da1432Sahl 	for (gsp = gf->gf_sects, i = 0; i < gf->gf_shnum; i++, gsp++) {
8327c478bd9Sstevel@tonic-gate 		if (gsp->gs_data != NULL)
8337c478bd9Sstevel@tonic-gate 			mdb_free(gsp->gs_data, gsp->gs_shdr.sh_size);
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	mdb_free(gf->gf_sects,
83730da1432Sahl 	    gf->gf_shnum * sizeof (mdb_gelf_sect_t));
8387c478bd9Sstevel@tonic-gate 
83930da1432Sahl 	mdb_free(gf->gf_phdrs, gf->gf_phnum * sizeof (GElf_Phdr));
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	mdb_io_rele(gf->gf_io);
8427c478bd9Sstevel@tonic-gate 	mdb_free(gf, sizeof (mdb_gelf_file_t));
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate /*
8467c478bd9Sstevel@tonic-gate  * Sort comparison function for 32-bit symbol address-to-name lookups.  We sort
8477c478bd9Sstevel@tonic-gate  * symbols by value.  If values are equal, we prefer the symbol that is
8487c478bd9Sstevel@tonic-gate  * non-zero sized, typed, not weak, or lexically first, in that order.
8497c478bd9Sstevel@tonic-gate  */
8507c478bd9Sstevel@tonic-gate static int
gelf32_sym_compare(const void * lp,const void * rp)8517c478bd9Sstevel@tonic-gate gelf32_sym_compare(const void *lp, const void *rp)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	Elf32_Sym *lhs = *((Elf32_Sym **)lp);
8547c478bd9Sstevel@tonic-gate 	Elf32_Sym *rhs = *((Elf32_Sym **)rp);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	if (lhs->st_value != rhs->st_value)
8577c478bd9Sstevel@tonic-gate 		return (lhs->st_value > rhs->st_value ? 1 : -1);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	if ((lhs->st_size == 0) != (rhs->st_size == 0))
8607c478bd9Sstevel@tonic-gate 		return (lhs->st_size == 0 ? 1 : -1);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
8637c478bd9Sstevel@tonic-gate 	    (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
8647c478bd9Sstevel@tonic-gate 		return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
8677c478bd9Sstevel@tonic-gate 	    (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
8687c478bd9Sstevel@tonic-gate 		return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	return (strcmp(gelf_strtab + lhs->st_name, gelf_strtab + rhs->st_name));
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate /*
8747c478bd9Sstevel@tonic-gate  * Sort comparison function for 64-bit symbol address-to-name lookups.  We sort
8757c478bd9Sstevel@tonic-gate  * symbols by value.  If values are equal, we prefer the symbol that is
8767c478bd9Sstevel@tonic-gate  * non-zero sized, typed, not weak, or lexically first, in that order.
8777c478bd9Sstevel@tonic-gate  */
8787c478bd9Sstevel@tonic-gate static int
gelf64_sym_compare(const void * lp,const void * rp)8797c478bd9Sstevel@tonic-gate gelf64_sym_compare(const void *lp, const void *rp)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	Elf64_Sym *lhs = *((Elf64_Sym **)lp);
8827c478bd9Sstevel@tonic-gate 	Elf64_Sym *rhs = *((Elf64_Sym **)rp);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	if (lhs->st_value != rhs->st_value)
8857c478bd9Sstevel@tonic-gate 		return (lhs->st_value > rhs->st_value ? 1 : -1);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	if ((lhs->st_size == 0) != (rhs->st_size == 0))
8887c478bd9Sstevel@tonic-gate 		return (lhs->st_size == 0 ? 1 : -1);
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
8917c478bd9Sstevel@tonic-gate 	    (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
8927c478bd9Sstevel@tonic-gate 		return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
8957c478bd9Sstevel@tonic-gate 	    (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
8967c478bd9Sstevel@tonic-gate 		return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	return (strcmp(gelf_strtab + lhs->st_name, gelf_strtab + rhs->st_name));
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate static void
gelf32_symtab_sort(mdb_gelf_symtab_t * gst)9027c478bd9Sstevel@tonic-gate gelf32_symtab_sort(mdb_gelf_symtab_t *gst)
9037c478bd9Sstevel@tonic-gate {
9047c478bd9Sstevel@tonic-gate 	Elf32_Sym **sympp = (Elf32_Sym **)gst->gst_asmap;
9057c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	mdb_nv_rewind(&gst->gst_nv);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	while ((v = mdb_nv_advance(&gst->gst_nv)) != NULL) {
9107c478bd9Sstevel@tonic-gate 		Elf32_Sym *sym = MDB_NV_COOKIE(v);
9117c478bd9Sstevel@tonic-gate 		if (sym->st_value != 0 &&
9127c478bd9Sstevel@tonic-gate 		    (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
9137c478bd9Sstevel@tonic-gate 			*sympp++ = sym;
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	gst->gst_aslen = (size_t)(sympp - (Elf32_Sym **)gst->gst_asmap);
9177c478bd9Sstevel@tonic-gate 	ASSERT(gst->gst_aslen <= gst->gst_asrsv);
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	gelf_strtab = gst->gst_ssect ? gst->gst_ssect->gs_data : NULL;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	qsort(gst->gst_asmap, gst->gst_aslen,
9227c478bd9Sstevel@tonic-gate 	    sizeof (Elf32_Sym *), gelf32_sym_compare);
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	gelf_strtab = NULL;
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate static void
gelf32_symtab_init(mdb_gelf_symtab_t * gst)9287c478bd9Sstevel@tonic-gate gelf32_symtab_init(mdb_gelf_symtab_t *gst)
9297c478bd9Sstevel@tonic-gate {
930*62b628a6SAli Bahrami #if STT_NUM != (STT_TLS + 1)
9317e16fca0SAli Bahrami #error "STT_NUM has grown. update gelf32_symtab_init()"
9327e16fca0SAli Bahrami #endif
9337e16fca0SAli Bahrami 
9347c478bd9Sstevel@tonic-gate 	const char *base = (const char *)gst->gst_ssect->gs_data;
9357c478bd9Sstevel@tonic-gate 	Elf32_Sym *sym = gst->gst_dsect->gs_data;
9367c478bd9Sstevel@tonic-gate 	mdb_nv_t *nv = &gst->gst_nv;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	Elf32_Word ss_size = gst->gst_ssect->gs_shdr.sh_size;
9397c478bd9Sstevel@tonic-gate 	size_t asrsv = 0;
9407c478bd9Sstevel@tonic-gate 	GElf_Word i, n;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	if (gst->gst_dsect->gs_shdr.sh_entsize != sizeof (Elf32_Sym)) {
9437c478bd9Sstevel@tonic-gate 		warn("%s sh_entsize %llu != sizeof (Elf32_Sym); "
9447c478bd9Sstevel@tonic-gate 		    "using %u instead\n", gst->gst_dsect->gs_name,
9457c478bd9Sstevel@tonic-gate 		    (u_longlong_t)gst->gst_dsect->gs_shdr.sh_entsize,
9467c478bd9Sstevel@tonic-gate 		    (uint_t)sizeof (Elf32_Sym));
9477c478bd9Sstevel@tonic-gate 		gst->gst_dsect->gs_shdr.sh_entsize = sizeof (Elf32_Sym);
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	n = gst->gst_dsect->gs_shdr.sh_size /
9517c478bd9Sstevel@tonic-gate 	    gst->gst_dsect->gs_shdr.sh_entsize;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++, sym++) {
9547c478bd9Sstevel@tonic-gate 		const char *name = base + sym->st_name;
9557c478bd9Sstevel@tonic-gate 		uchar_t type = ELF32_ST_TYPE(sym->st_info);
9567c478bd9Sstevel@tonic-gate 
957*62b628a6SAli Bahrami 		if (type >= STT_NUM || type == STT_SECTION)
9587c478bd9Sstevel@tonic-gate 			continue; /* skip sections and unknown types */
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 		if (sym->st_name >= ss_size || name[0] < '!' || name[0] > '~') {
9617c478bd9Sstevel@tonic-gate 			if (sym->st_name >= ss_size || name[0] != '\0') {
9627c478bd9Sstevel@tonic-gate 				warn("ignoring %s symbol [%u]: invalid name\n",
9637c478bd9Sstevel@tonic-gate 				    gst->gst_dsect->gs_name, i);
9647c478bd9Sstevel@tonic-gate 				sym->st_name = 0;
9657c478bd9Sstevel@tonic-gate 			}
9667c478bd9Sstevel@tonic-gate 			continue; /* skip corrupt or empty names */
9677c478bd9Sstevel@tonic-gate 		}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 		(void) mdb_nv_insert(nv, name, NULL, (uintptr_t)sym, GST_NVFLG);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 		if (sym->st_value != 0 &&
9727c478bd9Sstevel@tonic-gate 		    (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
9737c478bd9Sstevel@tonic-gate 			asrsv++; /* reserve space in the address map */
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	if (gst->gst_ehdr->e_type == ET_REL && gst->gst_file != NULL) {
97730da1432Sahl 		GElf_Word smax = gst->gst_file->gf_shnum;
9787c478bd9Sstevel@tonic-gate 		mdb_gelf_sect_t *gsp;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 		for (sym = gst->gst_dsect->gs_data, i = 0; i < n; i++, sym++) {
9817c478bd9Sstevel@tonic-gate 			if (sym->st_shndx > SHN_UNDEF && sym->st_shndx < smax) {
9827c478bd9Sstevel@tonic-gate 				gsp = &gst->gst_file->gf_sects[sym->st_shndx];
9837c478bd9Sstevel@tonic-gate 				sym->st_value += gsp->gs_shdr.sh_offset;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 				if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL ||
9867c478bd9Sstevel@tonic-gate 				    sym->st_size != 0)
9877c478bd9Sstevel@tonic-gate 					asrsv++; /* reserve space in asmap */
9887c478bd9Sstevel@tonic-gate 			}
9897c478bd9Sstevel@tonic-gate 		}
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	gst->gst_asmap = mdb_alloc(sizeof (Elf32_Sym *) * asrsv, UM_SLEEP);
9937c478bd9Sstevel@tonic-gate 	gst->gst_asrsv = asrsv;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	gelf32_symtab_sort(gst);
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate static void
gelf64_symtab_sort(mdb_gelf_symtab_t * gst)9997c478bd9Sstevel@tonic-gate gelf64_symtab_sort(mdb_gelf_symtab_t *gst)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	Elf64_Sym **sympp = (Elf64_Sym **)gst->gst_asmap;
10027c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	mdb_nv_rewind(&gst->gst_nv);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	while ((v = mdb_nv_advance(&gst->gst_nv)) != NULL) {
10077c478bd9Sstevel@tonic-gate 		Elf64_Sym *sym = MDB_NV_COOKIE(v);
10087c478bd9Sstevel@tonic-gate 		if (sym->st_value != 0 &&
10097c478bd9Sstevel@tonic-gate 		    (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
10107c478bd9Sstevel@tonic-gate 			*sympp++ = sym;
1011