xref: /netbsd-src/external/cddl/osnet/dist/lib/libctf/common/ctf_lib.c (revision a2d10af9ab477971c1062643776b230b52d84b99)
1a864dc36Sdarran /*
2a864dc36Sdarran  * CDDL HEADER START
3a864dc36Sdarran  *
4a864dc36Sdarran  * The contents of this file are subject to the terms of the
5a864dc36Sdarran  * Common Development and Distribution License, Version 1.0 only
6a864dc36Sdarran  * (the "License").  You may not use this file except in compliance
7a864dc36Sdarran  * with the License.
8a864dc36Sdarran  *
9a864dc36Sdarran  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10a864dc36Sdarran  * or http://www.opensolaris.org/os/licensing.
11a864dc36Sdarran  * See the License for the specific language governing permissions
12a864dc36Sdarran  * and limitations under the License.
13a864dc36Sdarran  *
14a864dc36Sdarran  * When distributing Covered Code, include this CDDL HEADER in each
15a864dc36Sdarran  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16a864dc36Sdarran  * If applicable, add the following below this CDDL HEADER, with the
17a864dc36Sdarran  * fields enclosed by brackets "[]" replaced with your own identifying
18a864dc36Sdarran  * information: Portions Copyright [yyyy] [name of copyright owner]
19a864dc36Sdarran  *
20a864dc36Sdarran  * CDDL HEADER END
21a864dc36Sdarran  */
22a864dc36Sdarran /*
23a864dc36Sdarran  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24a864dc36Sdarran  * Use is subject to license terms.
25a864dc36Sdarran  */
26a864dc36Sdarran 
27a864dc36Sdarran #pragma ident	"%Z%%M%	%I%	%E% SMI"
28a864dc36Sdarran 
2989300bd9Sdarran #if HAVE_NBTOOL_CONFIG_H
3089300bd9Sdarran # include "nbtool_config.h"
3189300bd9Sdarran #endif
3289300bd9Sdarran 
33a864dc36Sdarran #include <sys/types.h>
34a864dc36Sdarran #include <sys/stat.h>
35a864dc36Sdarran #include <sys/mman.h>
36bb8023b5Sdarran #include <sys/zmod.h>
37a864dc36Sdarran #include <ctf_impl.h>
38a864dc36Sdarran #include <unistd.h>
39a864dc36Sdarran #include <fcntl.h>
40a864dc36Sdarran #include <errno.h>
413745d9fbSchristos #ifdef illumos
42a864dc36Sdarran #include <dlfcn.h>
43bb8023b5Sdarran #else
44bb8023b5Sdarran #include <zlib.h>
45bb8023b5Sdarran #endif
46a864dc36Sdarran #include <gelf.h>
47a864dc36Sdarran 
483745d9fbSchristos #ifdef illumos
49a864dc36Sdarran #ifdef _LP64
50a864dc36Sdarran static const char *_libctf_zlib = "/usr/lib/64/libz.so";
51a864dc36Sdarran #else
52a864dc36Sdarran static const char *_libctf_zlib = "/usr/lib/libz.so";
53a864dc36Sdarran #endif
54bb8023b5Sdarran #endif
55a864dc36Sdarran 
56a864dc36Sdarran static struct {
57a864dc36Sdarran 	int (*z_uncompress)(uchar_t *, ulong_t *, const uchar_t *, ulong_t);
58a864dc36Sdarran 	const char *(*z_error)(int);
59a864dc36Sdarran 	void *z_dlp;
60a864dc36Sdarran } zlib;
61a864dc36Sdarran 
62a864dc36Sdarran static size_t _PAGESIZE;
63a864dc36Sdarran static size_t _PAGEMASK;
64a864dc36Sdarran 
653745d9fbSchristos #ifdef illumos
66a864dc36Sdarran #pragma init(_libctf_init)
67bb8023b5Sdarran #else
68bb8023b5Sdarran void    _libctf_init(void) __attribute__ ((constructor));
69bb8023b5Sdarran #endif
70a864dc36Sdarran void
_libctf_init(void)71a864dc36Sdarran _libctf_init(void)
72a864dc36Sdarran {
733745d9fbSchristos #ifdef illumos
74a864dc36Sdarran 	const char *p = getenv("LIBCTF_DECOMPRESSOR");
75a864dc36Sdarran 
76a864dc36Sdarran 	if (p != NULL)
77a864dc36Sdarran 		_libctf_zlib = p; /* use alternate decompression library */
78bb8023b5Sdarran #endif
79a864dc36Sdarran 
80a864dc36Sdarran 	_libctf_debug = getenv("LIBCTF_DEBUG") != NULL;
81a864dc36Sdarran 
82a864dc36Sdarran 	_PAGESIZE = getpagesize();
83a864dc36Sdarran 	_PAGEMASK = ~(_PAGESIZE - 1);
84a864dc36Sdarran }
85a864dc36Sdarran 
86a864dc36Sdarran /*
87a864dc36Sdarran  * Attempt to dlopen the decompression library and locate the symbols of
88a864dc36Sdarran  * interest that we will need to call.  This information in cached so
89a864dc36Sdarran  * that multiple calls to ctf_bufopen() do not need to reopen the library.
90a864dc36Sdarran  */
91a864dc36Sdarran void *
ctf_zopen(int * errp)92a864dc36Sdarran ctf_zopen(int *errp)
93a864dc36Sdarran {
943745d9fbSchristos #ifdef illumos
95a864dc36Sdarran 	ctf_dprintf("decompressing CTF data using %s\n", _libctf_zlib);
96a864dc36Sdarran 
97a864dc36Sdarran 	if (zlib.z_dlp != NULL)
98a864dc36Sdarran 		return (zlib.z_dlp); /* library is already loaded */
99a864dc36Sdarran 
100a864dc36Sdarran 	if (access(_libctf_zlib, R_OK) == -1)
101a864dc36Sdarran 		return (ctf_set_open_errno(errp, ECTF_ZMISSING));
102a864dc36Sdarran 
103a864dc36Sdarran 	if ((zlib.z_dlp = dlopen(_libctf_zlib, RTLD_LAZY | RTLD_LOCAL)) == NULL)
104a864dc36Sdarran 		return (ctf_set_open_errno(errp, ECTF_ZINIT));
105a864dc36Sdarran 
106bb8023b5Sdarran 	zlib.z_uncompress = (int (*)(uchar_t *, ulong_t *, const uchar_t *, ulong_t)) dlsym(zlib.z_dlp, "uncompress");
107bb8023b5Sdarran 	zlib.z_error = (const char *(*)(int)) dlsym(zlib.z_dlp, "zError");
108a864dc36Sdarran 
109a864dc36Sdarran 	if (zlib.z_uncompress == NULL || zlib.z_error == NULL) {
110a864dc36Sdarran 		(void) dlclose(zlib.z_dlp);
111a864dc36Sdarran 		bzero(&zlib, sizeof (zlib));
112a864dc36Sdarran 		return (ctf_set_open_errno(errp, ECTF_ZINIT));
113a864dc36Sdarran 	}
114bb8023b5Sdarran #else
115bb8023b5Sdarran 	zlib.z_uncompress = uncompress;
116bb8023b5Sdarran 	zlib.z_error = zError;
117bb8023b5Sdarran 
118bb8023b5Sdarran 	/* Dummy return variable as 'no error' */
119bb8023b5Sdarran 	zlib.z_dlp = (void *) (uintptr_t) 1;
120bb8023b5Sdarran #endif
121a864dc36Sdarran 
122a864dc36Sdarran 	return (zlib.z_dlp);
123a864dc36Sdarran }
124a864dc36Sdarran 
125a864dc36Sdarran /*
126a864dc36Sdarran  * The ctf_bufopen() routine calls these subroutines, defined by <sys/zmod.h>,
127a864dc36Sdarran  * which we then patch through to the functions in the decompression library.
128a864dc36Sdarran  */
129a864dc36Sdarran int
z_uncompress(void * dst,size_t * dstlen,const void * src,size_t srclen)130a864dc36Sdarran z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
131a864dc36Sdarran {
132a864dc36Sdarran 	return (zlib.z_uncompress(dst, (ulong_t *)dstlen, src, srclen));
133a864dc36Sdarran }
134a864dc36Sdarran 
135a864dc36Sdarran const char *
z_strerror(int err)136a864dc36Sdarran z_strerror(int err)
137a864dc36Sdarran {
138a864dc36Sdarran 	return (zlib.z_error(err));
139a864dc36Sdarran }
140a864dc36Sdarran 
141a864dc36Sdarran /*
142a864dc36Sdarran  * Convert a 32-bit ELF file header into GElf.
143a864dc36Sdarran  */
144a864dc36Sdarran static void
ehdr_to_gelf(const Elf32_Ehdr * src,GElf_Ehdr * dst)145a864dc36Sdarran ehdr_to_gelf(const Elf32_Ehdr *src, GElf_Ehdr *dst)
146a864dc36Sdarran {
147a864dc36Sdarran 	bcopy(src->e_ident, dst->e_ident, EI_NIDENT);
148a864dc36Sdarran 	dst->e_type = src->e_type;
149a864dc36Sdarran 	dst->e_machine = src->e_machine;
150a864dc36Sdarran 	dst->e_version = src->e_version;
151a864dc36Sdarran 	dst->e_entry = (Elf64_Addr)src->e_entry;
152a864dc36Sdarran 	dst->e_phoff = (Elf64_Off)src->e_phoff;
153a864dc36Sdarran 	dst->e_shoff = (Elf64_Off)src->e_shoff;
154a864dc36Sdarran 	dst->e_flags = src->e_flags;
155a864dc36Sdarran 	dst->e_ehsize = src->e_ehsize;
156a864dc36Sdarran 	dst->e_phentsize = src->e_phentsize;
157a864dc36Sdarran 	dst->e_phnum = src->e_phnum;
158a864dc36Sdarran 	dst->e_shentsize = src->e_shentsize;
159a864dc36Sdarran 	dst->e_shnum = src->e_shnum;
160a864dc36Sdarran 	dst->e_shstrndx = src->e_shstrndx;
161a864dc36Sdarran }
162a864dc36Sdarran 
163a864dc36Sdarran /*
164a864dc36Sdarran  * Convert a 32-bit ELF section header into GElf.
165a864dc36Sdarran  */
166a864dc36Sdarran static void
shdr_to_gelf(const Elf32_Shdr * src,GElf_Shdr * dst)167a864dc36Sdarran shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
168a864dc36Sdarran {
169a864dc36Sdarran 	dst->sh_name = src->sh_name;
170a864dc36Sdarran 	dst->sh_type = src->sh_type;
171a864dc36Sdarran 	dst->sh_flags = src->sh_flags;
172a864dc36Sdarran 	dst->sh_addr = src->sh_addr;
173a864dc36Sdarran 	dst->sh_offset = src->sh_offset;
174a864dc36Sdarran 	dst->sh_size = src->sh_size;
175a864dc36Sdarran 	dst->sh_link = src->sh_link;
176a864dc36Sdarran 	dst->sh_info = src->sh_info;
177a864dc36Sdarran 	dst->sh_addralign = src->sh_addralign;
178a864dc36Sdarran 	dst->sh_entsize = src->sh_entsize;
179a864dc36Sdarran }
180a864dc36Sdarran 
181a864dc36Sdarran /*
182a864dc36Sdarran  * In order to mmap a section from the ELF file, we must round down sh_offset
183a864dc36Sdarran  * to the previous page boundary, and mmap the surrounding page.  We store
184a864dc36Sdarran  * the pointer to the start of the actual section data back into sp->cts_data.
185a864dc36Sdarran  */
186a864dc36Sdarran const void *
ctf_sect_mmap(ctf_sect_t * sp,int fd)187a864dc36Sdarran ctf_sect_mmap(ctf_sect_t *sp, int fd)
188a864dc36Sdarran {
189a864dc36Sdarran 	size_t pageoff = sp->cts_offset & ~_PAGEMASK;
190a864dc36Sdarran 
191a864dc36Sdarran 	caddr_t base = mmap64(NULL, sp->cts_size + pageoff, PROT_READ,
192a864dc36Sdarran 	    MAP_PRIVATE, fd, sp->cts_offset & _PAGEMASK);
193a864dc36Sdarran 
194a864dc36Sdarran 	if (base != MAP_FAILED)
195a864dc36Sdarran 		sp->cts_data = base + pageoff;
196a864dc36Sdarran 
197a864dc36Sdarran 	return (base);
198a864dc36Sdarran }
199a864dc36Sdarran 
200a864dc36Sdarran /*
201a864dc36Sdarran  * Since sp->cts_data has the adjusted offset, we have to again round down
202a864dc36Sdarran  * to get the actual mmap address and round up to get the size.
203a864dc36Sdarran  */
204a864dc36Sdarran void
ctf_sect_munmap(const ctf_sect_t * sp)205a864dc36Sdarran ctf_sect_munmap(const ctf_sect_t *sp)
206a864dc36Sdarran {
207a864dc36Sdarran 	uintptr_t addr = (uintptr_t)sp->cts_data;
208a864dc36Sdarran 	uintptr_t pageoff = addr & ~_PAGEMASK;
209a864dc36Sdarran 
210a864dc36Sdarran 	(void) munmap((void *)(addr - pageoff), sp->cts_size + pageoff);
211a864dc36Sdarran }
212a864dc36Sdarran 
213a864dc36Sdarran /*
214a864dc36Sdarran  * Open the specified file descriptor and return a pointer to a CTF container.
215a864dc36Sdarran  * The file can be either an ELF file or raw CTF file.  The caller is
216a864dc36Sdarran  * responsible for closing the file descriptor when it is no longer needed.
217a864dc36Sdarran  */
218a864dc36Sdarran ctf_file_t *
ctf_fdopen(int fd,int * errp)219a864dc36Sdarran ctf_fdopen(int fd, int *errp)
220a864dc36Sdarran {
221a864dc36Sdarran 	ctf_sect_t ctfsect, symsect, strsect;
222a864dc36Sdarran 	ctf_file_t *fp = NULL;
2233745d9fbSchristos 	size_t shstrndx, shnum;
224a864dc36Sdarran 
225a864dc36Sdarran 	struct stat64 st;
226a864dc36Sdarran 	ssize_t nbytes;
227a864dc36Sdarran 
228a864dc36Sdarran 	union {
229a864dc36Sdarran 		ctf_preamble_t ctf;
230a864dc36Sdarran 		Elf32_Ehdr e32;
231a864dc36Sdarran 		GElf_Ehdr e64;
232a864dc36Sdarran 	} hdr;
233a864dc36Sdarran 
234a864dc36Sdarran 	bzero(&ctfsect, sizeof (ctf_sect_t));
235a864dc36Sdarran 	bzero(&symsect, sizeof (ctf_sect_t));
236a864dc36Sdarran 	bzero(&strsect, sizeof (ctf_sect_t));
237*a2d10af9Schristos 	bzero(&hdr, sizeof (hdr));
238a864dc36Sdarran 
239a864dc36Sdarran 	if (fstat64(fd, &st) == -1)
240a864dc36Sdarran 		return (ctf_set_open_errno(errp, errno));
241a864dc36Sdarran 
242a864dc36Sdarran 	if ((nbytes = pread64(fd, &hdr.ctf, sizeof (hdr), 0)) <= 0)
243a864dc36Sdarran 		return (ctf_set_open_errno(errp, nbytes < 0? errno : ECTF_FMT));
244a864dc36Sdarran 
245a864dc36Sdarran 	/*
246a864dc36Sdarran 	 * If we have read enough bytes to form a CTF header and the magic
247a864dc36Sdarran 	 * string matches, attempt to interpret the file as raw CTF.
248a864dc36Sdarran 	 */
249bb8023b5Sdarran 	if (nbytes >= (ssize_t) sizeof (ctf_preamble_t) &&
250a864dc36Sdarran 	    hdr.ctf.ctp_magic == CTF_MAGIC) {
251a864dc36Sdarran 		if (hdr.ctf.ctp_version > CTF_VERSION)
252a864dc36Sdarran 			return (ctf_set_open_errno(errp, ECTF_CTFVERS));
253a864dc36Sdarran 
254a864dc36Sdarran 		ctfsect.cts_data = mmap64(NULL, st.st_size, PROT_READ,
255a864dc36Sdarran 		    MAP_PRIVATE, fd, 0);
256a864dc36Sdarran 
257a864dc36Sdarran 		if (ctfsect.cts_data == MAP_FAILED)
258a864dc36Sdarran 			return (ctf_set_open_errno(errp, errno));
259a864dc36Sdarran 
2603745d9fbSchristos 		ctfsect.cts_name = _CTF_SECTION;
261a864dc36Sdarran 		ctfsect.cts_type = SHT_PROGBITS;
262a864dc36Sdarran 		ctfsect.cts_flags = SHF_ALLOC;
263a864dc36Sdarran 		ctfsect.cts_size = (size_t)st.st_size;
264a864dc36Sdarran 		ctfsect.cts_entsize = 1;
265a864dc36Sdarran 		ctfsect.cts_offset = 0;
266a864dc36Sdarran 
267a864dc36Sdarran 		if ((fp = ctf_bufopen(&ctfsect, NULL, NULL, errp)) == NULL)
268a864dc36Sdarran 			ctf_sect_munmap(&ctfsect);
269a864dc36Sdarran 
270a864dc36Sdarran 		return (fp);
271a864dc36Sdarran 	}
272a864dc36Sdarran 
273a864dc36Sdarran 	/*
274a864dc36Sdarran 	 * If we have read enough bytes to form an ELF header and the magic
275a864dc36Sdarran 	 * string matches, attempt to interpret the file as an ELF file.  We
276a864dc36Sdarran 	 * do our own largefile ELF processing, and convert everything to
277a864dc36Sdarran 	 * GElf structures so that clients can operate on any data model.
278a864dc36Sdarran 	 */
279bb8023b5Sdarran 	if (nbytes >= (ssize_t) sizeof (Elf32_Ehdr) &&
280a864dc36Sdarran 	    bcmp(&hdr.e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0) {
2813745d9fbSchristos #if BYTE_ORDER == _BIG_ENDIAN
282a864dc36Sdarran 		uchar_t order = ELFDATA2MSB;
283a864dc36Sdarran #else
284a864dc36Sdarran 		uchar_t order = ELFDATA2LSB;
285a864dc36Sdarran #endif
286a864dc36Sdarran 		GElf_Shdr *sp;
287a864dc36Sdarran 
288a864dc36Sdarran 		void *strs_map;
2893745d9fbSchristos 		size_t strs_mapsz, i;
290bb8023b5Sdarran 		char *strs;
291a864dc36Sdarran 
292a864dc36Sdarran 		if (hdr.e32.e_ident[EI_DATA] != order)
293a864dc36Sdarran 			return (ctf_set_open_errno(errp, ECTF_ENDIAN));
294a864dc36Sdarran 		if (hdr.e32.e_version != EV_CURRENT)
295a864dc36Sdarran 			return (ctf_set_open_errno(errp, ECTF_ELFVERS));
296a864dc36Sdarran 
297a864dc36Sdarran 		if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS64) {
298bb8023b5Sdarran 			if (nbytes < (ssize_t) sizeof (GElf_Ehdr))
299a864dc36Sdarran 				return (ctf_set_open_errno(errp, ECTF_FMT));
300a864dc36Sdarran 		} else {
301a864dc36Sdarran 			Elf32_Ehdr e32 = hdr.e32;
302a864dc36Sdarran 			ehdr_to_gelf(&e32, &hdr.e64);
303a864dc36Sdarran 		}
304a864dc36Sdarran 
3053745d9fbSchristos 		shnum = hdr.e64.e_shnum;
3063745d9fbSchristos 		shstrndx = hdr.e64.e_shstrndx;
3073745d9fbSchristos 
3083745d9fbSchristos 		/* Extended ELF sections */
3093745d9fbSchristos 		if ((shstrndx == SHN_XINDEX) || (shnum == 0)) {
3103745d9fbSchristos 			if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
3113745d9fbSchristos 				Elf32_Shdr x32;
3123745d9fbSchristos 
3133745d9fbSchristos 				if (pread64(fd, &x32, sizeof (x32),
3143745d9fbSchristos 				    hdr.e64.e_shoff) != sizeof (x32))
3153745d9fbSchristos 					return (ctf_set_open_errno(errp,
3163745d9fbSchristos 					    errno));
3173745d9fbSchristos 
3183745d9fbSchristos 				shnum = x32.sh_size;
3193745d9fbSchristos 				shstrndx = x32.sh_link;
3203745d9fbSchristos 			} else {
3213745d9fbSchristos 				Elf64_Shdr x64;
3223745d9fbSchristos 
3233745d9fbSchristos 				if (pread64(fd, &x64, sizeof (x64),
3243745d9fbSchristos 				    hdr.e64.e_shoff) != sizeof (x64))
3253745d9fbSchristos 					return (ctf_set_open_errno(errp,
3263745d9fbSchristos 					    errno));
3273745d9fbSchristos 
3283745d9fbSchristos 				shnum = x64.sh_size;
3293745d9fbSchristos 				shstrndx = x64.sh_link;
3303745d9fbSchristos 			}
3313745d9fbSchristos 		}
3323745d9fbSchristos 
3333745d9fbSchristos 		if (shstrndx >= shnum)
334a864dc36Sdarran 			return (ctf_set_open_errno(errp, ECTF_CORRUPT));
335a864dc36Sdarran 
3363745d9fbSchristos 		nbytes = sizeof (GElf_Shdr) * shnum;
337a864dc36Sdarran 
338a864dc36Sdarran 		if ((sp = malloc(nbytes)) == NULL)
339a864dc36Sdarran 			return (ctf_set_open_errno(errp, errno));
340a864dc36Sdarran 
341a864dc36Sdarran 		/*
342a864dc36Sdarran 		 * Read in and convert to GElf the array of Shdr structures
343a864dc36Sdarran 		 * from e_shoff so we can locate sections of interest.
344a864dc36Sdarran 		 */
345a864dc36Sdarran 		if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
346a864dc36Sdarran 			Elf32_Shdr *sp32;
347a864dc36Sdarran 
3483745d9fbSchristos 			nbytes = sizeof (Elf32_Shdr) * shnum;
349a864dc36Sdarran 
350a864dc36Sdarran 			if ((sp32 = malloc(nbytes)) == NULL || pread64(fd,
351a864dc36Sdarran 			    sp32, nbytes, hdr.e64.e_shoff) != nbytes) {
352a864dc36Sdarran 				free(sp);
353ba2539a9Schs 				free(sp32);
354a864dc36Sdarran 				return (ctf_set_open_errno(errp, errno));
355a864dc36Sdarran 			}
356a864dc36Sdarran 
3573745d9fbSchristos 			for (i = 0; i < shnum; i++)
358a864dc36Sdarran 				shdr_to_gelf(&sp32[i], &sp[i]);
359a864dc36Sdarran 
360a864dc36Sdarran 			free(sp32);
361a864dc36Sdarran 
362a864dc36Sdarran 		} else if (pread64(fd, sp, nbytes, hdr.e64.e_shoff) != nbytes) {
363a864dc36Sdarran 			free(sp);
364a864dc36Sdarran 			return (ctf_set_open_errno(errp, errno));
365a864dc36Sdarran 		}
366a864dc36Sdarran 
367a864dc36Sdarran 		/*
368a864dc36Sdarran 		 * Now mmap the section header strings section so that we can
369a864dc36Sdarran 		 * perform string comparison on the section names.
370a864dc36Sdarran 		 */
3713745d9fbSchristos 		strs_mapsz = sp[shstrndx].sh_size +
3723745d9fbSchristos 		    (sp[shstrndx].sh_offset & ~_PAGEMASK);
373a864dc36Sdarran 
374a864dc36Sdarran 		strs_map = mmap64(NULL, strs_mapsz, PROT_READ, MAP_PRIVATE,
3753745d9fbSchristos 		    fd, sp[shstrndx].sh_offset & _PAGEMASK);
376a864dc36Sdarran 
37725f11de3Sriastradh 		if (strs_map == MAP_FAILED)
37825f11de3Sriastradh 			return (ctf_set_open_errno(errp, errno));
37925f11de3Sriastradh 
380bb8023b5Sdarran 		strs = (char *)strs_map +
3813745d9fbSchristos 		    (sp[shstrndx].sh_offset & ~_PAGEMASK);
382a864dc36Sdarran 
383a864dc36Sdarran 		if (strs_map == MAP_FAILED) {
384a864dc36Sdarran 			free(sp);
385a864dc36Sdarran 			return (ctf_set_open_errno(errp, ECTF_MMAP));
386a864dc36Sdarran 		}
387a864dc36Sdarran 
388a864dc36Sdarran 		/*
389a864dc36Sdarran 		 * Iterate over the section header array looking for the CTF
390a864dc36Sdarran 		 * section and symbol table.  The strtab is linked to symtab.
391a864dc36Sdarran 		 */
3923745d9fbSchristos 		for (i = 0; i < shnum; i++) {
393a864dc36Sdarran 			const GElf_Shdr *shp = &sp[i];
394a864dc36Sdarran 			const GElf_Shdr *lhp = &sp[shp->sh_link];
395a864dc36Sdarran 
3963745d9fbSchristos 			if (shp->sh_link >= shnum)
397a864dc36Sdarran 				continue; /* corrupt sh_link field */
398a864dc36Sdarran 
3993745d9fbSchristos 			if (shp->sh_name >= sp[shstrndx].sh_size ||
4003745d9fbSchristos 			    lhp->sh_name >= sp[shstrndx].sh_size)
401a864dc36Sdarran 				continue; /* corrupt sh_name field */
402a864dc36Sdarran 
403a864dc36Sdarran 			if (shp->sh_type == SHT_PROGBITS &&
404a864dc36Sdarran 			    strcmp(strs + shp->sh_name, _CTF_SECTION) == 0) {
405a864dc36Sdarran 				ctfsect.cts_name = strs + shp->sh_name;
406a864dc36Sdarran 				ctfsect.cts_type = shp->sh_type;
407a864dc36Sdarran 				ctfsect.cts_flags = shp->sh_flags;
408a864dc36Sdarran 				ctfsect.cts_size = shp->sh_size;
409a864dc36Sdarran 				ctfsect.cts_entsize = shp->sh_entsize;
410a864dc36Sdarran 				ctfsect.cts_offset = (off64_t)shp->sh_offset;
411a864dc36Sdarran 
412a864dc36Sdarran 			} else if (shp->sh_type == SHT_SYMTAB) {
413a864dc36Sdarran 				symsect.cts_name = strs + shp->sh_name;
414a864dc36Sdarran 				symsect.cts_type = shp->sh_type;
415a864dc36Sdarran 				symsect.cts_flags = shp->sh_flags;
416a864dc36Sdarran 				symsect.cts_size = shp->sh_size;
417a864dc36Sdarran 				symsect.cts_entsize = shp->sh_entsize;
418a864dc36Sdarran 				symsect.cts_offset = (off64_t)shp->sh_offset;
419a864dc36Sdarran 
420a864dc36Sdarran 				strsect.cts_name = strs + lhp->sh_name;
421a864dc36Sdarran 				strsect.cts_type = lhp->sh_type;
422a864dc36Sdarran 				strsect.cts_flags = lhp->sh_flags;
423a864dc36Sdarran 				strsect.cts_size = lhp->sh_size;
424a864dc36Sdarran 				strsect.cts_entsize = lhp->sh_entsize;
425a864dc36Sdarran 				strsect.cts_offset = (off64_t)lhp->sh_offset;
426a864dc36Sdarran 			}
427a864dc36Sdarran 		}
428a864dc36Sdarran 
429a864dc36Sdarran 		free(sp); /* free section header array */
430a864dc36Sdarran 
431a864dc36Sdarran 		if (ctfsect.cts_type == SHT_NULL) {
432a864dc36Sdarran 			(void) munmap(strs_map, strs_mapsz);
433a864dc36Sdarran 			return (ctf_set_open_errno(errp, ECTF_NOCTFDATA));
434a864dc36Sdarran 		}
435a864dc36Sdarran 
436a864dc36Sdarran 		/*
437a864dc36Sdarran 		 * Now mmap the CTF data, symtab, and strtab sections and
438a864dc36Sdarran 		 * call ctf_bufopen() to do the rest of the work.
439a864dc36Sdarran 		 */
440a864dc36Sdarran 		if (ctf_sect_mmap(&ctfsect, fd) == MAP_FAILED) {
441a864dc36Sdarran 			(void) munmap(strs_map, strs_mapsz);
442a864dc36Sdarran 			return (ctf_set_open_errno(errp, ECTF_MMAP));
443a864dc36Sdarran 		}
444a864dc36Sdarran 
445a864dc36Sdarran 		if (symsect.cts_type != SHT_NULL &&
446a864dc36Sdarran 		    strsect.cts_type != SHT_NULL) {
447a864dc36Sdarran 			if (ctf_sect_mmap(&symsect, fd) == MAP_FAILED ||
448a864dc36Sdarran 			    ctf_sect_mmap(&strsect, fd) == MAP_FAILED) {
449a864dc36Sdarran 				(void) ctf_set_open_errno(errp, ECTF_MMAP);
450a864dc36Sdarran 				goto bad; /* unmap all and abort */
451a864dc36Sdarran 			}
452a864dc36Sdarran 			fp = ctf_bufopen(&ctfsect, &symsect, &strsect, errp);
453a864dc36Sdarran 		} else
454a864dc36Sdarran 			fp = ctf_bufopen(&ctfsect, NULL, NULL, errp);
455a864dc36Sdarran bad:
456a864dc36Sdarran 		if (fp == NULL) {
457a864dc36Sdarran 			ctf_sect_munmap(&ctfsect);
458a864dc36Sdarran 			ctf_sect_munmap(&symsect);
459a864dc36Sdarran 			ctf_sect_munmap(&strsect);
460a864dc36Sdarran 		} else
461a864dc36Sdarran 			fp->ctf_flags |= LCTF_MMAP;
462a864dc36Sdarran 
463a864dc36Sdarran 		(void) munmap(strs_map, strs_mapsz);
464a864dc36Sdarran 		return (fp);
465a864dc36Sdarran 	}
466a864dc36Sdarran 
467a864dc36Sdarran 	return (ctf_set_open_errno(errp, ECTF_FMT));
468a864dc36Sdarran }
469a864dc36Sdarran 
470a864dc36Sdarran /*
471a864dc36Sdarran  * Open the specified file and return a pointer to a CTF container.  The file
472a864dc36Sdarran  * can be either an ELF file or raw CTF file.  This is just a convenient
473a864dc36Sdarran  * wrapper around ctf_fdopen() for callers.
474a864dc36Sdarran  */
475a864dc36Sdarran ctf_file_t *
ctf_open(const char * filename,int * errp)476a864dc36Sdarran ctf_open(const char *filename, int *errp)
477a864dc36Sdarran {
478a864dc36Sdarran 	ctf_file_t *fp;
479a864dc36Sdarran 	int fd;
480a864dc36Sdarran 
481a864dc36Sdarran 	if ((fd = open64(filename, O_RDONLY)) == -1) {
482a864dc36Sdarran 		if (errp != NULL)
483a864dc36Sdarran 			*errp = errno;
484a864dc36Sdarran 		return (NULL);
485a864dc36Sdarran 	}
486a864dc36Sdarran 
487a864dc36Sdarran 	fp = ctf_fdopen(fd, errp);
488a864dc36Sdarran 	(void) close(fd);
489a864dc36Sdarran 	return (fp);
490a864dc36Sdarran }
491a864dc36Sdarran 
492a864dc36Sdarran /*
493a864dc36Sdarran  * Write the uncompressed CTF data stream to the specified file descriptor.
494a864dc36Sdarran  * This is useful for saving the results of dynamic CTF containers.
495a864dc36Sdarran  */
496a864dc36Sdarran int
ctf_write(ctf_file_t * fp,int fd)497a864dc36Sdarran ctf_write(ctf_file_t *fp, int fd)
498a864dc36Sdarran {
499a864dc36Sdarran 	const uchar_t *buf = fp->ctf_base;
500a864dc36Sdarran 	ssize_t resid = fp->ctf_size;
501a864dc36Sdarran 	ssize_t len;
502a864dc36Sdarran 
503a864dc36Sdarran 	while (resid != 0) {
504a864dc36Sdarran 		if ((len = write(fd, buf, resid)) <= 0)
505a864dc36Sdarran 			return (ctf_set_errno(fp, errno));
506a864dc36Sdarran 		resid -= len;
507a864dc36Sdarran 		buf += len;
508a864dc36Sdarran 	}
509a864dc36Sdarran 
510a864dc36Sdarran 	return (0);
511a864dc36Sdarran }
512a864dc36Sdarran 
513a864dc36Sdarran /*
514a864dc36Sdarran  * Set the CTF library client version to the specified version.  If version is
515a864dc36Sdarran  * zero, we just return the default library version number.
516a864dc36Sdarran  */
517a864dc36Sdarran int
ctf_version(int version)518a864dc36Sdarran ctf_version(int version)
519a864dc36Sdarran {
520a864dc36Sdarran 	if (version < 0) {
521a864dc36Sdarran 		errno = EINVAL;
522a864dc36Sdarran 		return (-1);
523a864dc36Sdarran 	}
524a864dc36Sdarran 
525a864dc36Sdarran 	if (version > 0) {
526a864dc36Sdarran 		if (version > CTF_VERSION) {
527a864dc36Sdarran 			errno = ENOTSUP;
528a864dc36Sdarran 			return (-1);
529a864dc36Sdarran 		}
530a864dc36Sdarran 		ctf_dprintf("ctf_version: client using version %d\n", version);
531a864dc36Sdarran 		_libctf_version = version;
532a864dc36Sdarran 	}
533a864dc36Sdarran 
534a864dc36Sdarran 	return (_libctf_version);
535a864dc36Sdarran }
536