xref: /titanic_44/usr/src/lib/libctf/common/ctf_lib.c (revision c894effd15e1470607e79c76f273f4565720d0f6)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/stat.h>
297c478bd9Sstevel@tonic-gate #include <sys/mman.h>
307c478bd9Sstevel@tonic-gate #include <ctf_impl.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <dlfcn.h>
357c478bd9Sstevel@tonic-gate #include <gelf.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifdef _LP64
382f970cc1SKeith M Wesolowski static const char *_libctf_zlib = "/usr/lib/64/libz.so.1";
397c478bd9Sstevel@tonic-gate #else
402f970cc1SKeith M Wesolowski static const char *_libctf_zlib = "/usr/lib/libz.so.1";
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static struct {
447c478bd9Sstevel@tonic-gate 	int (*z_uncompress)(uchar_t *, ulong_t *, const uchar_t *, ulong_t);
457c478bd9Sstevel@tonic-gate 	const char *(*z_error)(int);
467c478bd9Sstevel@tonic-gate 	void *z_dlp;
477c478bd9Sstevel@tonic-gate } zlib;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static size_t _PAGESIZE;
507c478bd9Sstevel@tonic-gate static size_t _PAGEMASK;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #pragma init(_libctf_init)
537c478bd9Sstevel@tonic-gate void
_libctf_init(void)547c478bd9Sstevel@tonic-gate _libctf_init(void)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	const char *p = getenv("LIBCTF_DECOMPRESSOR");
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	if (p != NULL)
597c478bd9Sstevel@tonic-gate 		_libctf_zlib = p; /* use alternate decompression library */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	_libctf_debug = getenv("LIBCTF_DEBUG") != NULL;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	_PAGESIZE = getpagesize();
647c478bd9Sstevel@tonic-gate 	_PAGEMASK = ~(_PAGESIZE - 1);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Attempt to dlopen the decompression library and locate the symbols of
697c478bd9Sstevel@tonic-gate  * interest that we will need to call.  This information in cached so
707c478bd9Sstevel@tonic-gate  * that multiple calls to ctf_bufopen() do not need to reopen the library.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate void *
ctf_zopen(int * errp)737c478bd9Sstevel@tonic-gate ctf_zopen(int *errp)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	ctf_dprintf("decompressing CTF data using %s\n", _libctf_zlib);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (zlib.z_dlp != NULL)
787c478bd9Sstevel@tonic-gate 		return (zlib.z_dlp); /* library is already loaded */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (access(_libctf_zlib, R_OK) == -1)
817c478bd9Sstevel@tonic-gate 		return (ctf_set_open_errno(errp, ECTF_ZMISSING));
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if ((zlib.z_dlp = dlopen(_libctf_zlib, RTLD_LAZY | RTLD_LOCAL)) == NULL)
847c478bd9Sstevel@tonic-gate 		return (ctf_set_open_errno(errp, ECTF_ZINIT));
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	zlib.z_uncompress = (int (*)()) dlsym(zlib.z_dlp, "uncompress");
877c478bd9Sstevel@tonic-gate 	zlib.z_error = (const char *(*)()) dlsym(zlib.z_dlp, "zError");
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (zlib.z_uncompress == NULL || zlib.z_error == NULL) {
907c478bd9Sstevel@tonic-gate 		(void) dlclose(zlib.z_dlp);
917c478bd9Sstevel@tonic-gate 		bzero(&zlib, sizeof (zlib));
927c478bd9Sstevel@tonic-gate 		return (ctf_set_open_errno(errp, ECTF_ZINIT));
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	return (zlib.z_dlp);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * The ctf_bufopen() routine calls these subroutines, defined by <sys/zmod.h>,
1007c478bd9Sstevel@tonic-gate  * which we then patch through to the functions in the decompression library.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate int
z_uncompress(void * dst,size_t * dstlen,const void * src,size_t srclen)1037c478bd9Sstevel@tonic-gate z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	return (zlib.z_uncompress(dst, (ulong_t *)dstlen, src, srclen));
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate const char *
z_strerror(int err)1097c478bd9Sstevel@tonic-gate z_strerror(int err)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	return (zlib.z_error(err));
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Convert a 32-bit ELF file header into GElf.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate static void
ehdr_to_gelf(const Elf32_Ehdr * src,GElf_Ehdr * dst)1187c478bd9Sstevel@tonic-gate ehdr_to_gelf(const Elf32_Ehdr *src, GElf_Ehdr *dst)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	bcopy(src->e_ident, dst->e_ident, EI_NIDENT);
1217c478bd9Sstevel@tonic-gate 	dst->e_type = src->e_type;
1227c478bd9Sstevel@tonic-gate 	dst->e_machine = src->e_machine;
1237c478bd9Sstevel@tonic-gate 	dst->e_version = src->e_version;
1247c478bd9Sstevel@tonic-gate 	dst->e_entry = (Elf64_Addr)src->e_entry;
1257c478bd9Sstevel@tonic-gate 	dst->e_phoff = (Elf64_Off)src->e_phoff;
1267c478bd9Sstevel@tonic-gate 	dst->e_shoff = (Elf64_Off)src->e_shoff;
1277c478bd9Sstevel@tonic-gate 	dst->e_flags = src->e_flags;
1287c478bd9Sstevel@tonic-gate 	dst->e_ehsize = src->e_ehsize;
1297c478bd9Sstevel@tonic-gate 	dst->e_phentsize = src->e_phentsize;
1307c478bd9Sstevel@tonic-gate 	dst->e_phnum = src->e_phnum;
1317c478bd9Sstevel@tonic-gate 	dst->e_shentsize = src->e_shentsize;
1327c478bd9Sstevel@tonic-gate 	dst->e_shnum = src->e_shnum;
1337c478bd9Sstevel@tonic-gate 	dst->e_shstrndx = src->e_shstrndx;
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Convert a 32-bit ELF section header into GElf.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate static void
shdr_to_gelf(const Elf32_Shdr * src,GElf_Shdr * dst)1407c478bd9Sstevel@tonic-gate shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	dst->sh_name = src->sh_name;
1437c478bd9Sstevel@tonic-gate 	dst->sh_type = src->sh_type;
1447c478bd9Sstevel@tonic-gate 	dst->sh_flags = src->sh_flags;
1457c478bd9Sstevel@tonic-gate 	dst->sh_addr = src->sh_addr;
1467c478bd9Sstevel@tonic-gate 	dst->sh_offset = src->sh_offset;
1477c478bd9Sstevel@tonic-gate 	dst->sh_size = src->sh_size;
1487c478bd9Sstevel@tonic-gate 	dst->sh_link = src->sh_link;
1497c478bd9Sstevel@tonic-gate 	dst->sh_info = src->sh_info;
1507c478bd9Sstevel@tonic-gate 	dst->sh_addralign = src->sh_addralign;
1517c478bd9Sstevel@tonic-gate 	dst->sh_entsize = src->sh_entsize;
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * In order to mmap a section from the ELF file, we must round down sh_offset
1567c478bd9Sstevel@tonic-gate  * to the previous page boundary, and mmap the surrounding page.  We store
1577c478bd9Sstevel@tonic-gate  * the pointer to the start of the actual section data back into sp->cts_data.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate const void *
ctf_sect_mmap(ctf_sect_t * sp,int fd)1607c478bd9Sstevel@tonic-gate ctf_sect_mmap(ctf_sect_t *sp, int fd)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	size_t pageoff = sp->cts_offset & ~_PAGEMASK;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	caddr_t base = mmap64(NULL, sp->cts_size + pageoff, PROT_READ,
1657c478bd9Sstevel@tonic-gate 	    MAP_PRIVATE, fd, sp->cts_offset & _PAGEMASK);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (base != MAP_FAILED)
1687c478bd9Sstevel@tonic-gate 		sp->cts_data = base + pageoff;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	return (base);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * Since sp->cts_data has the adjusted offset, we have to again round down
1757c478bd9Sstevel@tonic-gate  * to get the actual mmap address and round up to get the size.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate void
ctf_sect_munmap(const ctf_sect_t * sp)1787c478bd9Sstevel@tonic-gate ctf_sect_munmap(const ctf_sect_t *sp)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	uintptr_t addr = (uintptr_t)sp->cts_data;
1817c478bd9Sstevel@tonic-gate 	uintptr_t pageoff = addr & ~_PAGEMASK;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	(void) munmap((void *)(addr - pageoff), sp->cts_size + pageoff);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * Open the specified file descriptor and return a pointer to a CTF container.
1887c478bd9Sstevel@tonic-gate  * The file can be either an ELF file or raw CTF file.  The caller is
1897c478bd9Sstevel@tonic-gate  * responsible for closing the file descriptor when it is no longer needed.
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate ctf_file_t *
ctf_fdopen(int fd,int * errp)1927c478bd9Sstevel@tonic-gate ctf_fdopen(int fd, int *errp)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	ctf_sect_t ctfsect, symsect, strsect;
1957c478bd9Sstevel@tonic-gate 	ctf_file_t *fp = NULL;
196*c894effdSRichard Lowe 	size_t shstrndx, shnum;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	struct stat64 st;
1997c478bd9Sstevel@tonic-gate 	ssize_t nbytes;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	union {
2027c478bd9Sstevel@tonic-gate 		ctf_preamble_t ctf;
2037c478bd9Sstevel@tonic-gate 		Elf32_Ehdr e32;
2047c478bd9Sstevel@tonic-gate 		GElf_Ehdr e64;
2057c478bd9Sstevel@tonic-gate 	} hdr;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	bzero(&ctfsect, sizeof (ctf_sect_t));
2087c478bd9Sstevel@tonic-gate 	bzero(&symsect, sizeof (ctf_sect_t));
2097c478bd9Sstevel@tonic-gate 	bzero(&strsect, sizeof (ctf_sect_t));
2107c478bd9Sstevel@tonic-gate 	bzero(&hdr.ctf, sizeof (hdr));
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if (fstat64(fd, &st) == -1)
2137c478bd9Sstevel@tonic-gate 		return (ctf_set_open_errno(errp, errno));
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if ((nbytes = pread64(fd, &hdr.ctf, sizeof (hdr), 0)) <= 0)
2167c478bd9Sstevel@tonic-gate 		return (ctf_set_open_errno(errp, nbytes < 0? errno : ECTF_FMT));
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * If we have read enough bytes to form a CTF header and the magic
2207c478bd9Sstevel@tonic-gate 	 * string matches, attempt to interpret the file as raw CTF.
2217c478bd9Sstevel@tonic-gate 	 */
2227c478bd9Sstevel@tonic-gate 	if (nbytes >= sizeof (ctf_preamble_t) &&
2237c478bd9Sstevel@tonic-gate 	    hdr.ctf.ctp_magic == CTF_MAGIC) {
2247c478bd9Sstevel@tonic-gate 		if (hdr.ctf.ctp_version > CTF_VERSION)
2257c478bd9Sstevel@tonic-gate 			return (ctf_set_open_errno(errp, ECTF_CTFVERS));
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		ctfsect.cts_data = mmap64(NULL, st.st_size, PROT_READ,
2287c478bd9Sstevel@tonic-gate 		    MAP_PRIVATE, fd, 0);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		if (ctfsect.cts_data == MAP_FAILED)
2317c478bd9Sstevel@tonic-gate 			return (ctf_set_open_errno(errp, errno));
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		ctfsect.cts_name = _CTF_SECTION;
2347c478bd9Sstevel@tonic-gate 		ctfsect.cts_type = SHT_PROGBITS;
2357c478bd9Sstevel@tonic-gate 		ctfsect.cts_flags = SHF_ALLOC;
2367c478bd9Sstevel@tonic-gate 		ctfsect.cts_size = (size_t)st.st_size;
2377c478bd9Sstevel@tonic-gate 		ctfsect.cts_entsize = 1;
2387c478bd9Sstevel@tonic-gate 		ctfsect.cts_offset = 0;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		if ((fp = ctf_bufopen(&ctfsect, NULL, NULL, errp)) == NULL)
2417c478bd9Sstevel@tonic-gate 			ctf_sect_munmap(&ctfsect);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		return (fp);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/*
2477c478bd9Sstevel@tonic-gate 	 * If we have read enough bytes to form an ELF header and the magic
2487c478bd9Sstevel@tonic-gate 	 * string matches, attempt to interpret the file as an ELF file.  We
2497c478bd9Sstevel@tonic-gate 	 * do our own largefile ELF processing, and convert everything to
2507c478bd9Sstevel@tonic-gate 	 * GElf structures so that clients can operate on any data model.
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 	if (nbytes >= sizeof (Elf32_Ehdr) &&
2537c478bd9Sstevel@tonic-gate 	    bcmp(&hdr.e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0) {
2547c478bd9Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
2557c478bd9Sstevel@tonic-gate 		uchar_t order = ELFDATA2MSB;
2567c478bd9Sstevel@tonic-gate #else
2577c478bd9Sstevel@tonic-gate 		uchar_t order = ELFDATA2LSB;
2587c478bd9Sstevel@tonic-gate #endif
2597c478bd9Sstevel@tonic-gate 		GElf_Shdr *sp;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		void *strs_map;
262*c894effdSRichard Lowe 		size_t strs_mapsz, i;
2637c478bd9Sstevel@tonic-gate 		const char *strs;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		if (hdr.e32.e_ident[EI_DATA] != order)
2667c478bd9Sstevel@tonic-gate 			return (ctf_set_open_errno(errp, ECTF_ENDIAN));
2677c478bd9Sstevel@tonic-gate 		if (hdr.e32.e_version != EV_CURRENT)
2687c478bd9Sstevel@tonic-gate 			return (ctf_set_open_errno(errp, ECTF_ELFVERS));
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS64) {
2717c478bd9Sstevel@tonic-gate 			if (nbytes < sizeof (GElf_Ehdr))
2727c478bd9Sstevel@tonic-gate 				return (ctf_set_open_errno(errp, ECTF_FMT));
2737c478bd9Sstevel@tonic-gate 		} else {
2747c478bd9Sstevel@tonic-gate 			Elf32_Ehdr e32 = hdr.e32;
2757c478bd9Sstevel@tonic-gate 			ehdr_to_gelf(&e32, &hdr.e64);
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 
278*c894effdSRichard Lowe 		shnum = hdr.e64.e_shnum;
279*c894effdSRichard Lowe 		shstrndx = hdr.e64.e_shstrndx;
280*c894effdSRichard Lowe 
281*c894effdSRichard Lowe 		/* Extended ELF sections */
282*c894effdSRichard Lowe 		if ((shstrndx == SHN_XINDEX) || (shnum == 0)) {
283*c894effdSRichard Lowe 			if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
284*c894effdSRichard Lowe 				Elf32_Shdr x32;
285*c894effdSRichard Lowe 
286*c894effdSRichard Lowe 				if (pread64(fd, &x32, sizeof (x32),
287*c894effdSRichard Lowe 				    hdr.e64.e_shoff) != sizeof (x32))
288*c894effdSRichard Lowe 					return (ctf_set_open_errno(errp,
289*c894effdSRichard Lowe 					    errno));
290*c894effdSRichard Lowe 
291*c894effdSRichard Lowe 				shnum = x32.sh_size;
292*c894effdSRichard Lowe 				shstrndx = x32.sh_link;
293*c894effdSRichard Lowe 			} else {
294*c894effdSRichard Lowe 				Elf64_Shdr x64;
295*c894effdSRichard Lowe 
296*c894effdSRichard Lowe 				if (pread64(fd, &x64, sizeof (x64),
297*c894effdSRichard Lowe 				    hdr.e64.e_shoff) != sizeof (x64))
298*c894effdSRichard Lowe 					return (ctf_set_open_errno(errp,
299*c894effdSRichard Lowe 					    errno));
300*c894effdSRichard Lowe 
301*c894effdSRichard Lowe 				shnum = x64.sh_size;
302*c894effdSRichard Lowe 				shstrndx = x64.sh_link;
303*c894effdSRichard Lowe 			}
304*c894effdSRichard Lowe 		}
305*c894effdSRichard Lowe 
306*c894effdSRichard Lowe 		if (shstrndx >= shnum)
3077c478bd9Sstevel@tonic-gate 			return (ctf_set_open_errno(errp, ECTF_CORRUPT));
3087c478bd9Sstevel@tonic-gate 
309*c894effdSRichard Lowe 		nbytes = sizeof (GElf_Shdr) * shnum;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		if ((sp = malloc(nbytes)) == NULL)
3127c478bd9Sstevel@tonic-gate 			return (ctf_set_open_errno(errp, errno));
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		/*
3157c478bd9Sstevel@tonic-gate 		 * Read in and convert to GElf the array of Shdr structures
3167c478bd9Sstevel@tonic-gate 		 * from e_shoff so we can locate sections of interest.
3177c478bd9Sstevel@tonic-gate 		 */
3187c478bd9Sstevel@tonic-gate 		if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
3197c478bd9Sstevel@tonic-gate 			Elf32_Shdr *sp32;
3207c478bd9Sstevel@tonic-gate 
321*c894effdSRichard Lowe 			nbytes = sizeof (Elf32_Shdr) * shnum;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 			if ((sp32 = malloc(nbytes)) == NULL || pread64(fd,
3247c478bd9Sstevel@tonic-gate 			    sp32, nbytes, hdr.e64.e_shoff) != nbytes) {
3257c478bd9Sstevel@tonic-gate 				free(sp);
3267c478bd9Sstevel@tonic-gate 				return (ctf_set_open_errno(errp, errno));
3277c478bd9Sstevel@tonic-gate 			}
3287c478bd9Sstevel@tonic-gate 
329*c894effdSRichard Lowe 			for (i = 0; i < shnum; i++)
3307c478bd9Sstevel@tonic-gate 				shdr_to_gelf(&sp32[i], &sp[i]);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 			free(sp32);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		} else if (pread64(fd, sp, nbytes, hdr.e64.e_shoff) != nbytes) {
3357c478bd9Sstevel@tonic-gate 			free(sp);
3367c478bd9Sstevel@tonic-gate 			return (ctf_set_open_errno(errp, errno));
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		/*
3407c478bd9Sstevel@tonic-gate 		 * Now mmap the section header strings section so that we can
3417c478bd9Sstevel@tonic-gate 		 * perform string comparison on the section names.
3427c478bd9Sstevel@tonic-gate 		 */
343*c894effdSRichard Lowe 		strs_mapsz = sp[shstrndx].sh_size +
344*c894effdSRichard Lowe 		    (sp[shstrndx].sh_offset & ~_PAGEMASK);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		strs_map = mmap64(NULL, strs_mapsz, PROT_READ, MAP_PRIVATE,
347*c894effdSRichard Lowe 		    fd, sp[shstrndx].sh_offset & _PAGEMASK);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		strs = (const char *)strs_map +
350*c894effdSRichard Lowe 		    (sp[shstrndx].sh_offset & ~_PAGEMASK);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 		if (strs_map == MAP_FAILED) {
3537c478bd9Sstevel@tonic-gate 			free(sp);
3547c478bd9Sstevel@tonic-gate 			return (ctf_set_open_errno(errp, ECTF_MMAP));
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 		/*
3587c478bd9Sstevel@tonic-gate 		 * Iterate over the section header array looking for the CTF
3597c478bd9Sstevel@tonic-gate 		 * section and symbol table.  The strtab is linked to symtab.
3607c478bd9Sstevel@tonic-gate 		 */
361*c894effdSRichard Lowe 		for (i = 0; i < shnum; i++) {
3627c478bd9Sstevel@tonic-gate 			const GElf_Shdr *shp = &sp[i];
3637c478bd9Sstevel@tonic-gate 			const GElf_Shdr *lhp = &sp[shp->sh_link];
3647c478bd9Sstevel@tonic-gate 
365*c894effdSRichard Lowe 			if (shp->sh_link >= shnum)
3667c478bd9Sstevel@tonic-gate 				continue; /* corrupt sh_link field */
3677c478bd9Sstevel@tonic-gate 
368*c894effdSRichard Lowe 			if (shp->sh_name >= sp[shstrndx].sh_size ||
369*c894effdSRichard Lowe 			    lhp->sh_name >= sp[shstrndx].sh_size)
3707c478bd9Sstevel@tonic-gate 				continue; /* corrupt sh_name field */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 			if (shp->sh_type == SHT_PROGBITS &&
3737c478bd9Sstevel@tonic-gate 			    strcmp(strs + shp->sh_name, _CTF_SECTION) == 0) {
3747c478bd9Sstevel@tonic-gate 				ctfsect.cts_name = strs + shp->sh_name;
3757c478bd9Sstevel@tonic-gate 				ctfsect.cts_type = shp->sh_type;
3767c478bd9Sstevel@tonic-gate 				ctfsect.cts_flags = shp->sh_flags;
3777c478bd9Sstevel@tonic-gate 				ctfsect.cts_size = shp->sh_size;
3787c478bd9Sstevel@tonic-gate 				ctfsect.cts_entsize = shp->sh_entsize;
3797c478bd9Sstevel@tonic-gate 				ctfsect.cts_offset = (off64_t)shp->sh_offset;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 			} else if (shp->sh_type == SHT_SYMTAB) {
3827c478bd9Sstevel@tonic-gate 				symsect.cts_name = strs + shp->sh_name;
3837c478bd9Sstevel@tonic-gate 				symsect.cts_type = shp->sh_type;
3847c478bd9Sstevel@tonic-gate 				symsect.cts_flags = shp->sh_flags;
3857c478bd9Sstevel@tonic-gate 				symsect.cts_size = shp->sh_size;
3867c478bd9Sstevel@tonic-gate 				symsect.cts_entsize = shp->sh_entsize;
3877c478bd9Sstevel@tonic-gate 				symsect.cts_offset = (off64_t)shp->sh_offset;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 				strsect.cts_name = strs + lhp->sh_name;
3907c478bd9Sstevel@tonic-gate 				strsect.cts_type = lhp->sh_type;
3917c478bd9Sstevel@tonic-gate 				strsect.cts_flags = lhp->sh_flags;
3927c478bd9Sstevel@tonic-gate 				strsect.cts_size = lhp->sh_size;
3937c478bd9Sstevel@tonic-gate 				strsect.cts_entsize = lhp->sh_entsize;
3947c478bd9Sstevel@tonic-gate 				strsect.cts_offset = (off64_t)lhp->sh_offset;
3957c478bd9Sstevel@tonic-gate 			}
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		free(sp); /* free section header array */
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		if (ctfsect.cts_type == SHT_NULL) {
4017c478bd9Sstevel@tonic-gate 			(void) munmap(strs_map, strs_mapsz);
4027c478bd9Sstevel@tonic-gate 			return (ctf_set_open_errno(errp, ECTF_NOCTFDATA));
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		/*
4067c478bd9Sstevel@tonic-gate 		 * Now mmap the CTF data, symtab, and strtab sections and
4077c478bd9Sstevel@tonic-gate 		 * call ctf_bufopen() to do the rest of the work.
4087c478bd9Sstevel@tonic-gate 		 */
4097c478bd9Sstevel@tonic-gate 		if (ctf_sect_mmap(&ctfsect, fd) == MAP_FAILED) {
4107c478bd9Sstevel@tonic-gate 			(void) munmap(strs_map, strs_mapsz);
4117c478bd9Sstevel@tonic-gate 			return (ctf_set_open_errno(errp, ECTF_MMAP));
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		if (symsect.cts_type != SHT_NULL &&
4157c478bd9Sstevel@tonic-gate 		    strsect.cts_type != SHT_NULL) {
4167c478bd9Sstevel@tonic-gate 			if (ctf_sect_mmap(&symsect, fd) == MAP_FAILED ||
4177c478bd9Sstevel@tonic-gate 			    ctf_sect_mmap(&strsect, fd) == MAP_FAILED) {
4187c478bd9Sstevel@tonic-gate 				(void) ctf_set_open_errno(errp, ECTF_MMAP);
4197c478bd9Sstevel@tonic-gate 				goto bad; /* unmap all and abort */
4207c478bd9Sstevel@tonic-gate 			}
4217c478bd9Sstevel@tonic-gate 			fp = ctf_bufopen(&ctfsect, &symsect, &strsect, errp);
4227c478bd9Sstevel@tonic-gate 		} else
4237c478bd9Sstevel@tonic-gate 			fp = ctf_bufopen(&ctfsect, NULL, NULL, errp);
4247c478bd9Sstevel@tonic-gate bad:
4257c478bd9Sstevel@tonic-gate 		if (fp == NULL) {
4267c478bd9Sstevel@tonic-gate 			ctf_sect_munmap(&ctfsect);
4277c478bd9Sstevel@tonic-gate 			ctf_sect_munmap(&symsect);
4287c478bd9Sstevel@tonic-gate 			ctf_sect_munmap(&strsect);
4297c478bd9Sstevel@tonic-gate 		} else
4307c478bd9Sstevel@tonic-gate 			fp->ctf_flags |= LCTF_MMAP;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 		(void) munmap(strs_map, strs_mapsz);
4337c478bd9Sstevel@tonic-gate 		return (fp);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	return (ctf_set_open_errno(errp, ECTF_FMT));
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate  * Open the specified file and return a pointer to a CTF container.  The file
4417c478bd9Sstevel@tonic-gate  * can be either an ELF file or raw CTF file.  This is just a convenient
4427c478bd9Sstevel@tonic-gate  * wrapper around ctf_fdopen() for callers.
4437c478bd9Sstevel@tonic-gate  */
4447c478bd9Sstevel@tonic-gate ctf_file_t *
ctf_open(const char * filename,int * errp)4457c478bd9Sstevel@tonic-gate ctf_open(const char *filename, int *errp)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	ctf_file_t *fp;
4487c478bd9Sstevel@tonic-gate 	int fd;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	if ((fd = open64(filename, O_RDONLY)) == -1) {
4517c478bd9Sstevel@tonic-gate 		if (errp != NULL)
4527c478bd9Sstevel@tonic-gate 			*errp = errno;
4537c478bd9Sstevel@tonic-gate 		return (NULL);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	fp = ctf_fdopen(fd, errp);
4577c478bd9Sstevel@tonic-gate 	(void) close(fd);
4587c478bd9Sstevel@tonic-gate 	return (fp);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate /*
4627c478bd9Sstevel@tonic-gate  * Write the uncompressed CTF data stream to the specified file descriptor.
4637c478bd9Sstevel@tonic-gate  * This is useful for saving the results of dynamic CTF containers.
4647c478bd9Sstevel@tonic-gate  */
4657c478bd9Sstevel@tonic-gate int
ctf_write(ctf_file_t * fp,int fd)4667c478bd9Sstevel@tonic-gate ctf_write(ctf_file_t *fp, int fd)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate 	const uchar_t *buf = fp->ctf_base;
4697c478bd9Sstevel@tonic-gate 	ssize_t resid = fp->ctf_size;
4707c478bd9Sstevel@tonic-gate 	ssize_t len;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	while (resid != 0) {
4737c478bd9Sstevel@tonic-gate 		if ((len = write(fd, buf, resid)) <= 0)
4747c478bd9Sstevel@tonic-gate 			return (ctf_set_errno(fp, errno));
4757c478bd9Sstevel@tonic-gate 		resid -= len;
4767c478bd9Sstevel@tonic-gate 		buf += len;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	return (0);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate  * Set the CTF library client version to the specified version.  If version is
4847c478bd9Sstevel@tonic-gate  * zero, we just return the default library version number.
4857c478bd9Sstevel@tonic-gate  */
4867c478bd9Sstevel@tonic-gate int
ctf_version(int version)4877c478bd9Sstevel@tonic-gate ctf_version(int version)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	if (version < 0) {
4907c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4917c478bd9Sstevel@tonic-gate 		return (-1);
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	if (version > 0) {
4957c478bd9Sstevel@tonic-gate 		if (version > CTF_VERSION) {
4967c478bd9Sstevel@tonic-gate 			errno = ENOTSUP;
4977c478bd9Sstevel@tonic-gate 			return (-1);
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		ctf_dprintf("ctf_version: client using version %d\n", version);
5007c478bd9Sstevel@tonic-gate 		_libctf_version = version;
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	return (_libctf_version);
5047c478bd9Sstevel@tonic-gate }
505