1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5178479Sjb * Common Development and Distribution License, Version 1.0 only
6178479Sjb * (the "License").  You may not use this file except in compliance
7178479Sjb * with the License.
8178479Sjb *
9178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10178479Sjb * or http://www.opensolaris.org/os/licensing.
11178479Sjb * See the License for the specific language governing permissions
12178479Sjb * and limitations under the License.
13178479Sjb *
14178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
15178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16178479Sjb * If applicable, add the following below this CDDL HEADER, with the
17178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
18178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
19178479Sjb *
20178479Sjb * CDDL HEADER END
21178479Sjb */
22178479Sjb/*
23178479Sjb * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24178479Sjb * Use is subject to license terms.
25178479Sjb */
26178479Sjb
27178479Sjb#pragma ident	"%Z%%M%	%I%	%E% SMI"
28178479Sjb
29178479Sjb#include <sys/types.h>
30178479Sjb#include <sys/stat.h>
31178479Sjb#include <sys/mman.h>
32178548Sjb#include <sys/zmod.h>
33178479Sjb#include <ctf_impl.h>
34178479Sjb#include <unistd.h>
35178479Sjb#include <fcntl.h>
36178479Sjb#include <errno.h>
37178548Sjb#if defined(sun)
38178479Sjb#include <dlfcn.h>
39178548Sjb#else
40178548Sjb#include <zlib.h>
41178548Sjb#endif
42178479Sjb#include <gelf.h>
43178479Sjb
44178548Sjb#if defined(sun)
45178479Sjb#ifdef _LP64
46178479Sjbstatic const char *_libctf_zlib = "/usr/lib/64/libz.so";
47178479Sjb#else
48178479Sjbstatic const char *_libctf_zlib = "/usr/lib/libz.so";
49178479Sjb#endif
50178548Sjb#endif
51178479Sjb
52178479Sjbstatic struct {
53178479Sjb	int (*z_uncompress)(uchar_t *, ulong_t *, const uchar_t *, ulong_t);
54178479Sjb	const char *(*z_error)(int);
55178479Sjb	void *z_dlp;
56178479Sjb} zlib;
57178479Sjb
58178479Sjbstatic size_t _PAGESIZE;
59178479Sjbstatic size_t _PAGEMASK;
60178479Sjb
61178548Sjb#if defined(sun)
62178479Sjb#pragma init(_libctf_init)
63178548Sjb#else
64178548Sjbvoid    _libctf_init(void) __attribute__ ((constructor));
65178548Sjb#endif
66178479Sjbvoid
67178479Sjb_libctf_init(void)
68178479Sjb{
69178548Sjb#if defined(sun)
70178479Sjb	const char *p = getenv("LIBCTF_DECOMPRESSOR");
71178479Sjb
72178479Sjb	if (p != NULL)
73178479Sjb		_libctf_zlib = p; /* use alternate decompression library */
74178548Sjb#endif
75178479Sjb
76178479Sjb	_libctf_debug = getenv("LIBCTF_DEBUG") != NULL;
77178479Sjb
78178479Sjb	_PAGESIZE = getpagesize();
79178479Sjb	_PAGEMASK = ~(_PAGESIZE - 1);
80178479Sjb}
81178479Sjb
82178479Sjb/*
83178479Sjb * Attempt to dlopen the decompression library and locate the symbols of
84178479Sjb * interest that we will need to call.  This information in cached so
85178479Sjb * that multiple calls to ctf_bufopen() do not need to reopen the library.
86178479Sjb */
87178479Sjbvoid *
88178479Sjbctf_zopen(int *errp)
89178479Sjb{
90178548Sjb#if defined(sun)
91178479Sjb	ctf_dprintf("decompressing CTF data using %s\n", _libctf_zlib);
92178479Sjb
93178479Sjb	if (zlib.z_dlp != NULL)
94178479Sjb		return (zlib.z_dlp); /* library is already loaded */
95178479Sjb
96178479Sjb	if (access(_libctf_zlib, R_OK) == -1)
97178479Sjb		return (ctf_set_open_errno(errp, ECTF_ZMISSING));
98178479Sjb
99178479Sjb	if ((zlib.z_dlp = dlopen(_libctf_zlib, RTLD_LAZY | RTLD_LOCAL)) == NULL)
100178479Sjb		return (ctf_set_open_errno(errp, ECTF_ZINIT));
101178479Sjb
102178548Sjb	zlib.z_uncompress = (int (*)(uchar_t *, ulong_t *, const uchar_t *, ulong_t)) dlsym(zlib.z_dlp, "uncompress");
103178548Sjb	zlib.z_error = (const char *(*)(int)) dlsym(zlib.z_dlp, "zError");
104178479Sjb
105178479Sjb	if (zlib.z_uncompress == NULL || zlib.z_error == NULL) {
106178479Sjb		(void) dlclose(zlib.z_dlp);
107178479Sjb		bzero(&zlib, sizeof (zlib));
108178479Sjb		return (ctf_set_open_errno(errp, ECTF_ZINIT));
109178479Sjb	}
110178548Sjb#else
111178548Sjb	zlib.z_uncompress = uncompress;
112178548Sjb	zlib.z_error = zError;
113178479Sjb
114178548Sjb	/* Dummy return variable as 'no error' */
115178548Sjb	zlib.z_dlp = (void *) (uintptr_t) 1;
116178548Sjb#endif
117178548Sjb
118178479Sjb	return (zlib.z_dlp);
119178479Sjb}
120178479Sjb
121178479Sjb/*
122178479Sjb * The ctf_bufopen() routine calls these subroutines, defined by <sys/zmod.h>,
123178479Sjb * which we then patch through to the functions in the decompression library.
124178479Sjb */
125178479Sjbint
126178479Sjbz_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
127178479Sjb{
128178479Sjb	return (zlib.z_uncompress(dst, (ulong_t *)dstlen, src, srclen));
129178479Sjb}
130178479Sjb
131178479Sjbconst char *
132178479Sjbz_strerror(int err)
133178479Sjb{
134178479Sjb	return (zlib.z_error(err));
135178479Sjb}
136178479Sjb
137178479Sjb/*
138178479Sjb * Convert a 32-bit ELF file header into GElf.
139178479Sjb */
140178479Sjbstatic void
141178479Sjbehdr_to_gelf(const Elf32_Ehdr *src, GElf_Ehdr *dst)
142178479Sjb{
143178479Sjb	bcopy(src->e_ident, dst->e_ident, EI_NIDENT);
144178479Sjb	dst->e_type = src->e_type;
145178479Sjb	dst->e_machine = src->e_machine;
146178479Sjb	dst->e_version = src->e_version;
147178479Sjb	dst->e_entry = (Elf64_Addr)src->e_entry;
148178479Sjb	dst->e_phoff = (Elf64_Off)src->e_phoff;
149178479Sjb	dst->e_shoff = (Elf64_Off)src->e_shoff;
150178479Sjb	dst->e_flags = src->e_flags;
151178479Sjb	dst->e_ehsize = src->e_ehsize;
152178479Sjb	dst->e_phentsize = src->e_phentsize;
153178479Sjb	dst->e_phnum = src->e_phnum;
154178479Sjb	dst->e_shentsize = src->e_shentsize;
155178479Sjb	dst->e_shnum = src->e_shnum;
156178479Sjb	dst->e_shstrndx = src->e_shstrndx;
157178479Sjb}
158178479Sjb
159178479Sjb/*
160178479Sjb * Convert a 32-bit ELF section header into GElf.
161178479Sjb */
162178479Sjbstatic void
163178479Sjbshdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
164178479Sjb{
165178479Sjb	dst->sh_name = src->sh_name;
166178479Sjb	dst->sh_type = src->sh_type;
167178479Sjb	dst->sh_flags = src->sh_flags;
168178479Sjb	dst->sh_addr = src->sh_addr;
169178479Sjb	dst->sh_offset = src->sh_offset;
170178479Sjb	dst->sh_size = src->sh_size;
171178479Sjb	dst->sh_link = src->sh_link;
172178479Sjb	dst->sh_info = src->sh_info;
173178479Sjb	dst->sh_addralign = src->sh_addralign;
174178479Sjb	dst->sh_entsize = src->sh_entsize;
175178479Sjb}
176178479Sjb
177178479Sjb/*
178178479Sjb * In order to mmap a section from the ELF file, we must round down sh_offset
179178479Sjb * to the previous page boundary, and mmap the surrounding page.  We store
180178479Sjb * the pointer to the start of the actual section data back into sp->cts_data.
181178479Sjb */
182178479Sjbconst void *
183178479Sjbctf_sect_mmap(ctf_sect_t *sp, int fd)
184178479Sjb{
185178479Sjb	size_t pageoff = sp->cts_offset & ~_PAGEMASK;
186178479Sjb
187178479Sjb	caddr_t base = mmap64(NULL, sp->cts_size + pageoff, PROT_READ,
188178479Sjb	    MAP_PRIVATE, fd, sp->cts_offset & _PAGEMASK);
189178479Sjb
190178479Sjb	if (base != MAP_FAILED)
191178479Sjb		sp->cts_data = base + pageoff;
192178479Sjb
193178479Sjb	return (base);
194178479Sjb}
195178479Sjb
196178479Sjb/*
197178479Sjb * Since sp->cts_data has the adjusted offset, we have to again round down
198178479Sjb * to get the actual mmap address and round up to get the size.
199178479Sjb */
200178479Sjbvoid
201178479Sjbctf_sect_munmap(const ctf_sect_t *sp)
202178479Sjb{
203178479Sjb	uintptr_t addr = (uintptr_t)sp->cts_data;
204178479Sjb	uintptr_t pageoff = addr & ~_PAGEMASK;
205178479Sjb
206178479Sjb	(void) munmap((void *)(addr - pageoff), sp->cts_size + pageoff);
207178479Sjb}
208178479Sjb
209178479Sjb/*
210178479Sjb * Open the specified file descriptor and return a pointer to a CTF container.
211178479Sjb * The file can be either an ELF file or raw CTF file.  The caller is
212178479Sjb * responsible for closing the file descriptor when it is no longer needed.
213178479Sjb */
214178479Sjbctf_file_t *
215178479Sjbctf_fdopen(int fd, int *errp)
216178479Sjb{
217178479Sjb	ctf_sect_t ctfsect, symsect, strsect;
218178479Sjb	ctf_file_t *fp = NULL;
219254752Sdelphij	size_t shstrndx, shnum;
220178479Sjb
221178479Sjb	struct stat64 st;
222178479Sjb	ssize_t nbytes;
223178479Sjb
224178479Sjb	union {
225178479Sjb		ctf_preamble_t ctf;
226178479Sjb		Elf32_Ehdr e32;
227178479Sjb		GElf_Ehdr e64;
228178479Sjb	} hdr;
229178479Sjb
230178479Sjb	bzero(&ctfsect, sizeof (ctf_sect_t));
231178479Sjb	bzero(&symsect, sizeof (ctf_sect_t));
232178479Sjb	bzero(&strsect, sizeof (ctf_sect_t));
233178479Sjb	bzero(&hdr.ctf, sizeof (hdr));
234178479Sjb
235178479Sjb	if (fstat64(fd, &st) == -1)
236178479Sjb		return (ctf_set_open_errno(errp, errno));
237178479Sjb
238178479Sjb	if ((nbytes = pread64(fd, &hdr.ctf, sizeof (hdr), 0)) <= 0)
239178479Sjb		return (ctf_set_open_errno(errp, nbytes < 0? errno : ECTF_FMT));
240178479Sjb
241178479Sjb	/*
242178479Sjb	 * If we have read enough bytes to form a CTF header and the magic
243178479Sjb	 * string matches, attempt to interpret the file as raw CTF.
244178479Sjb	 */
245178548Sjb	if (nbytes >= (ssize_t) sizeof (ctf_preamble_t) &&
246178479Sjb	    hdr.ctf.ctp_magic == CTF_MAGIC) {
247178479Sjb		if (hdr.ctf.ctp_version > CTF_VERSION)
248178479Sjb			return (ctf_set_open_errno(errp, ECTF_CTFVERS));
249178479Sjb
250178479Sjb		ctfsect.cts_data = mmap64(NULL, st.st_size, PROT_READ,
251178479Sjb		    MAP_PRIVATE, fd, 0);
252178479Sjb
253178479Sjb		if (ctfsect.cts_data == MAP_FAILED)
254178479Sjb			return (ctf_set_open_errno(errp, errno));
255178479Sjb
256178479Sjb		ctfsect.cts_name = _CTF_SECTION;
257178479Sjb		ctfsect.cts_type = SHT_PROGBITS;
258178479Sjb		ctfsect.cts_flags = SHF_ALLOC;
259178479Sjb		ctfsect.cts_size = (size_t)st.st_size;
260178479Sjb		ctfsect.cts_entsize = 1;
261178479Sjb		ctfsect.cts_offset = 0;
262178479Sjb
263178479Sjb		if ((fp = ctf_bufopen(&ctfsect, NULL, NULL, errp)) == NULL)
264178479Sjb			ctf_sect_munmap(&ctfsect);
265178479Sjb
266178479Sjb		return (fp);
267178479Sjb	}
268178479Sjb
269178479Sjb	/*
270178479Sjb	 * If we have read enough bytes to form an ELF header and the magic
271178479Sjb	 * string matches, attempt to interpret the file as an ELF file.  We
272178479Sjb	 * do our own largefile ELF processing, and convert everything to
273178479Sjb	 * GElf structures so that clients can operate on any data model.
274178479Sjb	 */
275178548Sjb	if (nbytes >= (ssize_t) sizeof (Elf32_Ehdr) &&
276178479Sjb	    bcmp(&hdr.e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0) {
277178479Sjb#ifdef	_BIG_ENDIAN
278178479Sjb		uchar_t order = ELFDATA2MSB;
279178479Sjb#else
280178479Sjb		uchar_t order = ELFDATA2LSB;
281178479Sjb#endif
282178479Sjb		GElf_Shdr *sp;
283178479Sjb
284178479Sjb		void *strs_map;
285254752Sdelphij		size_t strs_mapsz, i;
286178548Sjb		char *strs;
287178479Sjb
288178479Sjb		if (hdr.e32.e_ident[EI_DATA] != order)
289178479Sjb			return (ctf_set_open_errno(errp, ECTF_ENDIAN));
290178479Sjb		if (hdr.e32.e_version != EV_CURRENT)
291178479Sjb			return (ctf_set_open_errno(errp, ECTF_ELFVERS));
292178479Sjb
293178479Sjb		if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS64) {
294178548Sjb			if (nbytes < (ssize_t) sizeof (GElf_Ehdr))
295178479Sjb				return (ctf_set_open_errno(errp, ECTF_FMT));
296178479Sjb		} else {
297178479Sjb			Elf32_Ehdr e32 = hdr.e32;
298178479Sjb			ehdr_to_gelf(&e32, &hdr.e64);
299178479Sjb		}
300178479Sjb
301254752Sdelphij		shnum = hdr.e64.e_shnum;
302254752Sdelphij		shstrndx = hdr.e64.e_shstrndx;
303254752Sdelphij
304254752Sdelphij		/* Extended ELF sections */
305254752Sdelphij		if ((shstrndx == SHN_XINDEX) || (shnum == 0)) {
306254752Sdelphij			if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
307254752Sdelphij				Elf32_Shdr x32;
308254752Sdelphij
309254752Sdelphij				if (pread64(fd, &x32, sizeof (x32),
310254752Sdelphij				    hdr.e64.e_shoff) != sizeof (x32))
311254752Sdelphij					return (ctf_set_open_errno(errp,
312254752Sdelphij					    errno));
313254752Sdelphij
314254752Sdelphij				shnum = x32.sh_size;
315254752Sdelphij				shstrndx = x32.sh_link;
316254752Sdelphij			} else {
317254752Sdelphij				Elf64_Shdr x64;
318254752Sdelphij
319254752Sdelphij				if (pread64(fd, &x64, sizeof (x64),
320254752Sdelphij				    hdr.e64.e_shoff) != sizeof (x64))
321254752Sdelphij					return (ctf_set_open_errno(errp,
322254752Sdelphij					    errno));
323254752Sdelphij
324254752Sdelphij				shnum = x64.sh_size;
325254752Sdelphij				shstrndx = x64.sh_link;
326254752Sdelphij			}
327254752Sdelphij		}
328254752Sdelphij
329254752Sdelphij		if (shstrndx >= shnum)
330178479Sjb			return (ctf_set_open_errno(errp, ECTF_CORRUPT));
331178479Sjb
332254752Sdelphij		nbytes = sizeof (GElf_Shdr) * shnum;
333178479Sjb
334178479Sjb		if ((sp = malloc(nbytes)) == NULL)
335178479Sjb			return (ctf_set_open_errno(errp, errno));
336178479Sjb
337178479Sjb		/*
338178479Sjb		 * Read in and convert to GElf the array of Shdr structures
339178479Sjb		 * from e_shoff so we can locate sections of interest.
340178479Sjb		 */
341178479Sjb		if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
342178479Sjb			Elf32_Shdr *sp32;
343178479Sjb
344254752Sdelphij			nbytes = sizeof (Elf32_Shdr) * shnum;
345178479Sjb
346178479Sjb			if ((sp32 = malloc(nbytes)) == NULL || pread64(fd,
347178479Sjb			    sp32, nbytes, hdr.e64.e_shoff) != nbytes) {
348178479Sjb				free(sp);
349178479Sjb				return (ctf_set_open_errno(errp, errno));
350178479Sjb			}
351178479Sjb
352254752Sdelphij			for (i = 0; i < shnum; i++)
353178479Sjb				shdr_to_gelf(&sp32[i], &sp[i]);
354178479Sjb
355178479Sjb			free(sp32);
356178479Sjb
357178479Sjb		} else if (pread64(fd, sp, nbytes, hdr.e64.e_shoff) != nbytes) {
358178479Sjb			free(sp);
359178479Sjb			return (ctf_set_open_errno(errp, errno));
360178479Sjb		}
361178479Sjb
362178479Sjb		/*
363178479Sjb		 * Now mmap the section header strings section so that we can
364178479Sjb		 * perform string comparison on the section names.
365178479Sjb		 */
366254752Sdelphij		strs_mapsz = sp[shstrndx].sh_size +
367254752Sdelphij		    (sp[shstrndx].sh_offset & ~_PAGEMASK);
368178479Sjb
369178479Sjb		strs_map = mmap64(NULL, strs_mapsz, PROT_READ, MAP_PRIVATE,
370254752Sdelphij		    fd, sp[shstrndx].sh_offset & _PAGEMASK);
371178479Sjb
372178548Sjb		strs = (char *)strs_map +
373254752Sdelphij		    (sp[shstrndx].sh_offset & ~_PAGEMASK);
374178479Sjb
375178479Sjb		if (strs_map == MAP_FAILED) {
376178479Sjb			free(sp);
377178479Sjb			return (ctf_set_open_errno(errp, ECTF_MMAP));
378178479Sjb		}
379178479Sjb
380178479Sjb		/*
381178479Sjb		 * Iterate over the section header array looking for the CTF
382178479Sjb		 * section and symbol table.  The strtab is linked to symtab.
383178479Sjb		 */
384254752Sdelphij		for (i = 0; i < shnum; i++) {
385178479Sjb			const GElf_Shdr *shp = &sp[i];
386178479Sjb			const GElf_Shdr *lhp = &sp[shp->sh_link];
387178479Sjb
388254752Sdelphij			if (shp->sh_link >= shnum)
389178479Sjb				continue; /* corrupt sh_link field */
390178479Sjb
391254752Sdelphij			if (shp->sh_name >= sp[shstrndx].sh_size ||
392254752Sdelphij			    lhp->sh_name >= sp[shstrndx].sh_size)
393178479Sjb				continue; /* corrupt sh_name field */
394178479Sjb
395178479Sjb			if (shp->sh_type == SHT_PROGBITS &&
396178479Sjb			    strcmp(strs + shp->sh_name, _CTF_SECTION) == 0) {
397178479Sjb				ctfsect.cts_name = strs + shp->sh_name;
398178479Sjb				ctfsect.cts_type = shp->sh_type;
399178479Sjb				ctfsect.cts_flags = shp->sh_flags;
400178479Sjb				ctfsect.cts_size = shp->sh_size;
401178479Sjb				ctfsect.cts_entsize = shp->sh_entsize;
402178479Sjb				ctfsect.cts_offset = (off64_t)shp->sh_offset;
403178479Sjb
404178479Sjb			} else if (shp->sh_type == SHT_SYMTAB) {
405178479Sjb				symsect.cts_name = strs + shp->sh_name;
406178479Sjb				symsect.cts_type = shp->sh_type;
407178479Sjb				symsect.cts_flags = shp->sh_flags;
408178479Sjb				symsect.cts_size = shp->sh_size;
409178479Sjb				symsect.cts_entsize = shp->sh_entsize;
410178479Sjb				symsect.cts_offset = (off64_t)shp->sh_offset;
411178479Sjb
412178479Sjb				strsect.cts_name = strs + lhp->sh_name;
413178479Sjb				strsect.cts_type = lhp->sh_type;
414178479Sjb				strsect.cts_flags = lhp->sh_flags;
415178479Sjb				strsect.cts_size = lhp->sh_size;
416178479Sjb				strsect.cts_entsize = lhp->sh_entsize;
417178479Sjb				strsect.cts_offset = (off64_t)lhp->sh_offset;
418178479Sjb			}
419178479Sjb		}
420178479Sjb
421178479Sjb		free(sp); /* free section header array */
422178479Sjb
423178479Sjb		if (ctfsect.cts_type == SHT_NULL) {
424178479Sjb			(void) munmap(strs_map, strs_mapsz);
425178479Sjb			return (ctf_set_open_errno(errp, ECTF_NOCTFDATA));
426178479Sjb		}
427178479Sjb
428178479Sjb		/*
429178479Sjb		 * Now mmap the CTF data, symtab, and strtab sections and
430178479Sjb		 * call ctf_bufopen() to do the rest of the work.
431178479Sjb		 */
432178479Sjb		if (ctf_sect_mmap(&ctfsect, fd) == MAP_FAILED) {
433178479Sjb			(void) munmap(strs_map, strs_mapsz);
434178479Sjb			return (ctf_set_open_errno(errp, ECTF_MMAP));
435178479Sjb		}
436178479Sjb
437178479Sjb		if (symsect.cts_type != SHT_NULL &&
438178479Sjb		    strsect.cts_type != SHT_NULL) {
439178479Sjb			if (ctf_sect_mmap(&symsect, fd) == MAP_FAILED ||
440178479Sjb			    ctf_sect_mmap(&strsect, fd) == MAP_FAILED) {
441178479Sjb				(void) ctf_set_open_errno(errp, ECTF_MMAP);
442178479Sjb				goto bad; /* unmap all and abort */
443178479Sjb			}
444178479Sjb			fp = ctf_bufopen(&ctfsect, &symsect, &strsect, errp);
445178479Sjb		} else
446178479Sjb			fp = ctf_bufopen(&ctfsect, NULL, NULL, errp);
447178479Sjbbad:
448178479Sjb		if (fp == NULL) {
449178479Sjb			ctf_sect_munmap(&ctfsect);
450178479Sjb			ctf_sect_munmap(&symsect);
451178479Sjb			ctf_sect_munmap(&strsect);
452178479Sjb		} else
453178479Sjb			fp->ctf_flags |= LCTF_MMAP;
454178479Sjb
455178479Sjb		(void) munmap(strs_map, strs_mapsz);
456178479Sjb		return (fp);
457178479Sjb	}
458178479Sjb
459178479Sjb	return (ctf_set_open_errno(errp, ECTF_FMT));
460178479Sjb}
461178479Sjb
462178479Sjb/*
463178479Sjb * Open the specified file and return a pointer to a CTF container.  The file
464178479Sjb * can be either an ELF file or raw CTF file.  This is just a convenient
465178479Sjb * wrapper around ctf_fdopen() for callers.
466178479Sjb */
467178479Sjbctf_file_t *
468178479Sjbctf_open(const char *filename, int *errp)
469178479Sjb{
470178479Sjb	ctf_file_t *fp;
471178479Sjb	int fd;
472178479Sjb
473178479Sjb	if ((fd = open64(filename, O_RDONLY)) == -1) {
474178479Sjb		if (errp != NULL)
475178479Sjb			*errp = errno;
476178479Sjb		return (NULL);
477178479Sjb	}
478178479Sjb
479178479Sjb	fp = ctf_fdopen(fd, errp);
480178479Sjb	(void) close(fd);
481178479Sjb	return (fp);
482178479Sjb}
483178479Sjb
484178479Sjb/*
485178479Sjb * Write the uncompressed CTF data stream to the specified file descriptor.
486178479Sjb * This is useful for saving the results of dynamic CTF containers.
487178479Sjb */
488178479Sjbint
489178479Sjbctf_write(ctf_file_t *fp, int fd)
490178479Sjb{
491178479Sjb	const uchar_t *buf = fp->ctf_base;
492178479Sjb	ssize_t resid = fp->ctf_size;
493178479Sjb	ssize_t len;
494178479Sjb
495178479Sjb	while (resid != 0) {
496178479Sjb		if ((len = write(fd, buf, resid)) <= 0)
497178479Sjb			return (ctf_set_errno(fp, errno));
498178479Sjb		resid -= len;
499178479Sjb		buf += len;
500178479Sjb	}
501178479Sjb
502178479Sjb	return (0);
503178479Sjb}
504178479Sjb
505178479Sjb/*
506178479Sjb * Set the CTF library client version to the specified version.  If version is
507178479Sjb * zero, we just return the default library version number.
508178479Sjb */
509178479Sjbint
510178479Sjbctf_version(int version)
511178479Sjb{
512178479Sjb	if (version < 0) {
513178479Sjb		errno = EINVAL;
514178479Sjb		return (-1);
515178479Sjb	}
516178479Sjb
517178479Sjb	if (version > 0) {
518178479Sjb		if (version > CTF_VERSION) {
519178479Sjb			errno = ENOTSUP;
520178479Sjb			return (-1);
521178479Sjb		}
522178479Sjb		ctf_dprintf("ctf_version: client using version %d\n", version);
523178479Sjb		_libctf_version = version;
524178479Sjb	}
525178479Sjb
526178479Sjb	return (_libctf_version);
527178479Sjb}
528