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;
219178479Sjb
220178479Sjb	struct stat64 st;
221178479Sjb	ssize_t nbytes;
222178479Sjb
223178479Sjb	union {
224178479Sjb		ctf_preamble_t ctf;
225178479Sjb		Elf32_Ehdr e32;
226178479Sjb		GElf_Ehdr e64;
227178479Sjb	} hdr;
228178479Sjb
229178479Sjb	bzero(&ctfsect, sizeof (ctf_sect_t));
230178479Sjb	bzero(&symsect, sizeof (ctf_sect_t));
231178479Sjb	bzero(&strsect, sizeof (ctf_sect_t));
232178479Sjb	bzero(&hdr.ctf, sizeof (hdr));
233178479Sjb
234178479Sjb	if (fstat64(fd, &st) == -1)
235178479Sjb		return (ctf_set_open_errno(errp, errno));
236178479Sjb
237178479Sjb	if ((nbytes = pread64(fd, &hdr.ctf, sizeof (hdr), 0)) <= 0)
238178479Sjb		return (ctf_set_open_errno(errp, nbytes < 0? errno : ECTF_FMT));
239178479Sjb
240178479Sjb	/*
241178479Sjb	 * If we have read enough bytes to form a CTF header and the magic
242178479Sjb	 * string matches, attempt to interpret the file as raw CTF.
243178479Sjb	 */
244178548Sjb	if (nbytes >= (ssize_t) sizeof (ctf_preamble_t) &&
245178479Sjb	    hdr.ctf.ctp_magic == CTF_MAGIC) {
246178479Sjb		if (hdr.ctf.ctp_version > CTF_VERSION)
247178479Sjb			return (ctf_set_open_errno(errp, ECTF_CTFVERS));
248178479Sjb
249178479Sjb		ctfsect.cts_data = mmap64(NULL, st.st_size, PROT_READ,
250178479Sjb		    MAP_PRIVATE, fd, 0);
251178479Sjb
252178479Sjb		if (ctfsect.cts_data == MAP_FAILED)
253178479Sjb			return (ctf_set_open_errno(errp, errno));
254178479Sjb
255178479Sjb		ctfsect.cts_name = _CTF_SECTION;
256178479Sjb		ctfsect.cts_type = SHT_PROGBITS;
257178479Sjb		ctfsect.cts_flags = SHF_ALLOC;
258178479Sjb		ctfsect.cts_size = (size_t)st.st_size;
259178479Sjb		ctfsect.cts_entsize = 1;
260178479Sjb		ctfsect.cts_offset = 0;
261178479Sjb
262178479Sjb		if ((fp = ctf_bufopen(&ctfsect, NULL, NULL, errp)) == NULL)
263178479Sjb			ctf_sect_munmap(&ctfsect);
264178479Sjb
265178479Sjb		return (fp);
266178479Sjb	}
267178479Sjb
268178479Sjb	/*
269178479Sjb	 * If we have read enough bytes to form an ELF header and the magic
270178479Sjb	 * string matches, attempt to interpret the file as an ELF file.  We
271178479Sjb	 * do our own largefile ELF processing, and convert everything to
272178479Sjb	 * GElf structures so that clients can operate on any data model.
273178479Sjb	 */
274178548Sjb	if (nbytes >= (ssize_t) sizeof (Elf32_Ehdr) &&
275178479Sjb	    bcmp(&hdr.e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0) {
276178479Sjb#ifdef	_BIG_ENDIAN
277178479Sjb		uchar_t order = ELFDATA2MSB;
278178479Sjb#else
279178479Sjb		uchar_t order = ELFDATA2LSB;
280178479Sjb#endif
281178479Sjb		GElf_Half i, n;
282178479Sjb		GElf_Shdr *sp;
283178479Sjb
284178479Sjb		void *strs_map;
285178479Sjb		size_t strs_mapsz;
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
301178479Sjb		if (hdr.e64.e_shstrndx >= hdr.e64.e_shnum)
302178479Sjb			return (ctf_set_open_errno(errp, ECTF_CORRUPT));
303178479Sjb
304178479Sjb		n = hdr.e64.e_shnum;
305178479Sjb		nbytes = sizeof (GElf_Shdr) * n;
306178479Sjb
307178479Sjb		if ((sp = malloc(nbytes)) == NULL)
308178479Sjb			return (ctf_set_open_errno(errp, errno));
309178479Sjb
310178479Sjb		/*
311178479Sjb		 * Read in and convert to GElf the array of Shdr structures
312178479Sjb		 * from e_shoff so we can locate sections of interest.
313178479Sjb		 */
314178479Sjb		if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
315178479Sjb			Elf32_Shdr *sp32;
316178479Sjb
317178479Sjb			nbytes = sizeof (Elf32_Shdr) * n;
318178479Sjb
319178479Sjb			if ((sp32 = malloc(nbytes)) == NULL || pread64(fd,
320178479Sjb			    sp32, nbytes, hdr.e64.e_shoff) != nbytes) {
321178479Sjb				free(sp);
322178479Sjb				return (ctf_set_open_errno(errp, errno));
323178479Sjb			}
324178479Sjb
325178479Sjb			for (i = 0; i < n; i++)
326178479Sjb				shdr_to_gelf(&sp32[i], &sp[i]);
327178479Sjb
328178479Sjb			free(sp32);
329178479Sjb
330178479Sjb		} else if (pread64(fd, sp, nbytes, hdr.e64.e_shoff) != nbytes) {
331178479Sjb			free(sp);
332178479Sjb			return (ctf_set_open_errno(errp, errno));
333178479Sjb		}
334178479Sjb
335178479Sjb		/*
336178479Sjb		 * Now mmap the section header strings section so that we can
337178479Sjb		 * perform string comparison on the section names.
338178479Sjb		 */
339178479Sjb		strs_mapsz = sp[hdr.e64.e_shstrndx].sh_size +
340178479Sjb		    (sp[hdr.e64.e_shstrndx].sh_offset & ~_PAGEMASK);
341178479Sjb
342178479Sjb		strs_map = mmap64(NULL, strs_mapsz, PROT_READ, MAP_PRIVATE,
343178479Sjb		    fd, sp[hdr.e64.e_shstrndx].sh_offset & _PAGEMASK);
344178479Sjb
345178548Sjb		strs = (char *)strs_map +
346178479Sjb		    (sp[hdr.e64.e_shstrndx].sh_offset & ~_PAGEMASK);
347178479Sjb
348178479Sjb		if (strs_map == MAP_FAILED) {
349178479Sjb			free(sp);
350178479Sjb			return (ctf_set_open_errno(errp, ECTF_MMAP));
351178479Sjb		}
352178479Sjb
353178479Sjb		/*
354178479Sjb		 * Iterate over the section header array looking for the CTF
355178479Sjb		 * section and symbol table.  The strtab is linked to symtab.
356178479Sjb		 */
357178479Sjb		for (i = 0; i < n; i++) {
358178479Sjb			const GElf_Shdr *shp = &sp[i];
359178479Sjb			const GElf_Shdr *lhp = &sp[shp->sh_link];
360178479Sjb
361178479Sjb			if (shp->sh_link >= hdr.e64.e_shnum)
362178479Sjb				continue; /* corrupt sh_link field */
363178479Sjb
364178479Sjb			if (shp->sh_name >= sp[hdr.e64.e_shstrndx].sh_size ||
365178479Sjb			    lhp->sh_name >= sp[hdr.e64.e_shstrndx].sh_size)
366178479Sjb				continue; /* corrupt sh_name field */
367178479Sjb
368178479Sjb			if (shp->sh_type == SHT_PROGBITS &&
369178479Sjb			    strcmp(strs + shp->sh_name, _CTF_SECTION) == 0) {
370178479Sjb				ctfsect.cts_name = strs + shp->sh_name;
371178479Sjb				ctfsect.cts_type = shp->sh_type;
372178479Sjb				ctfsect.cts_flags = shp->sh_flags;
373178479Sjb				ctfsect.cts_size = shp->sh_size;
374178479Sjb				ctfsect.cts_entsize = shp->sh_entsize;
375178479Sjb				ctfsect.cts_offset = (off64_t)shp->sh_offset;
376178479Sjb
377178479Sjb			} else if (shp->sh_type == SHT_SYMTAB) {
378178479Sjb				symsect.cts_name = strs + shp->sh_name;
379178479Sjb				symsect.cts_type = shp->sh_type;
380178479Sjb				symsect.cts_flags = shp->sh_flags;
381178479Sjb				symsect.cts_size = shp->sh_size;
382178479Sjb				symsect.cts_entsize = shp->sh_entsize;
383178479Sjb				symsect.cts_offset = (off64_t)shp->sh_offset;
384178479Sjb
385178479Sjb				strsect.cts_name = strs + lhp->sh_name;
386178479Sjb				strsect.cts_type = lhp->sh_type;
387178479Sjb				strsect.cts_flags = lhp->sh_flags;
388178479Sjb				strsect.cts_size = lhp->sh_size;
389178479Sjb				strsect.cts_entsize = lhp->sh_entsize;
390178479Sjb				strsect.cts_offset = (off64_t)lhp->sh_offset;
391178479Sjb			}
392178479Sjb		}
393178479Sjb
394178479Sjb		free(sp); /* free section header array */
395178479Sjb
396178479Sjb		if (ctfsect.cts_type == SHT_NULL) {
397178479Sjb			(void) munmap(strs_map, strs_mapsz);
398178479Sjb			return (ctf_set_open_errno(errp, ECTF_NOCTFDATA));
399178479Sjb		}
400178479Sjb
401178479Sjb		/*
402178479Sjb		 * Now mmap the CTF data, symtab, and strtab sections and
403178479Sjb		 * call ctf_bufopen() to do the rest of the work.
404178479Sjb		 */
405178479Sjb		if (ctf_sect_mmap(&ctfsect, fd) == MAP_FAILED) {
406178479Sjb			(void) munmap(strs_map, strs_mapsz);
407178479Sjb			return (ctf_set_open_errno(errp, ECTF_MMAP));
408178479Sjb		}
409178479Sjb
410178479Sjb		if (symsect.cts_type != SHT_NULL &&
411178479Sjb		    strsect.cts_type != SHT_NULL) {
412178479Sjb			if (ctf_sect_mmap(&symsect, fd) == MAP_FAILED ||
413178479Sjb			    ctf_sect_mmap(&strsect, fd) == MAP_FAILED) {
414178479Sjb				(void) ctf_set_open_errno(errp, ECTF_MMAP);
415178479Sjb				goto bad; /* unmap all and abort */
416178479Sjb			}
417178479Sjb			fp = ctf_bufopen(&ctfsect, &symsect, &strsect, errp);
418178479Sjb		} else
419178479Sjb			fp = ctf_bufopen(&ctfsect, NULL, NULL, errp);
420178479Sjbbad:
421178479Sjb		if (fp == NULL) {
422178479Sjb			ctf_sect_munmap(&ctfsect);
423178479Sjb			ctf_sect_munmap(&symsect);
424178479Sjb			ctf_sect_munmap(&strsect);
425178479Sjb		} else
426178479Sjb			fp->ctf_flags |= LCTF_MMAP;
427178479Sjb
428178479Sjb		(void) munmap(strs_map, strs_mapsz);
429178479Sjb		return (fp);
430178479Sjb	}
431178479Sjb
432178479Sjb	return (ctf_set_open_errno(errp, ECTF_FMT));
433178479Sjb}
434178479Sjb
435178479Sjb/*
436178479Sjb * Open the specified file and return a pointer to a CTF container.  The file
437178479Sjb * can be either an ELF file or raw CTF file.  This is just a convenient
438178479Sjb * wrapper around ctf_fdopen() for callers.
439178479Sjb */
440178479Sjbctf_file_t *
441178479Sjbctf_open(const char *filename, int *errp)
442178479Sjb{
443178479Sjb	ctf_file_t *fp;
444178479Sjb	int fd;
445178479Sjb
446178479Sjb	if ((fd = open64(filename, O_RDONLY)) == -1) {
447178479Sjb		if (errp != NULL)
448178479Sjb			*errp = errno;
449178479Sjb		return (NULL);
450178479Sjb	}
451178479Sjb
452178479Sjb	fp = ctf_fdopen(fd, errp);
453178479Sjb	(void) close(fd);
454178479Sjb	return (fp);
455178479Sjb}
456178479Sjb
457178479Sjb/*
458178479Sjb * Write the uncompressed CTF data stream to the specified file descriptor.
459178479Sjb * This is useful for saving the results of dynamic CTF containers.
460178479Sjb */
461178479Sjbint
462178479Sjbctf_write(ctf_file_t *fp, int fd)
463178479Sjb{
464178479Sjb	const uchar_t *buf = fp->ctf_base;
465178479Sjb	ssize_t resid = fp->ctf_size;
466178479Sjb	ssize_t len;
467178479Sjb
468178479Sjb	while (resid != 0) {
469178479Sjb		if ((len = write(fd, buf, resid)) <= 0)
470178479Sjb			return (ctf_set_errno(fp, errno));
471178479Sjb		resid -= len;
472178479Sjb		buf += len;
473178479Sjb	}
474178479Sjb
475178479Sjb	return (0);
476178479Sjb}
477178479Sjb
478178479Sjb/*
479178479Sjb * Set the CTF library client version to the specified version.  If version is
480178479Sjb * zero, we just return the default library version number.
481178479Sjb */
482178479Sjbint
483178479Sjbctf_version(int version)
484178479Sjb{
485178479Sjb	if (version < 0) {
486178479Sjb		errno = EINVAL;
487178479Sjb		return (-1);
488178479Sjb	}
489178479Sjb
490178479Sjb	if (version > 0) {
491178479Sjb		if (version > CTF_VERSION) {
492178479Sjb			errno = ENOTSUP;
493178479Sjb			return (-1);
494178479Sjb		}
495178479Sjb		ctf_dprintf("ctf_version: client using version %d\n", version);
496178479Sjb		_libctf_version = version;
497178479Sjb	}
498178479Sjb
499178479Sjb	return (_libctf_version);
500178479Sjb}
501