xref: /titanic_44/usr/src/common/ctf/ctf_open.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <ctf_impl.h>
30 #include <sys/mman.h>
31 #include <sys/zmod.h>
32 
33 static const ctf_dmodel_t _libctf_models[] = {
34 	{ "ILP32", CTF_MODEL_ILP32, 4, 1, 2, 4, 4 },
35 	{ "LP64", CTF_MODEL_LP64, 8, 1, 2, 4, 8 },
36 	{ NULL, 0, 0, 0, 0, 0, 0 }
37 };
38 
39 const char _CTF_SECTION[] = ".SUNW_ctf";
40 const char _CTF_NULLSTR[] = "";
41 
42 int _libctf_version = CTF_VERSION;	/* library client version */
43 int _libctf_debug = 0;			/* debugging messages enabled */
44 
45 static ushort_t
46 get_kind_v1(ushort_t info)
47 {
48 	return (CTF_INFO_KIND_V1(info));
49 }
50 
51 static ushort_t
52 get_kind_v2(ushort_t info)
53 {
54 	return (CTF_INFO_KIND(info));
55 }
56 
57 static ushort_t
58 get_root_v1(ushort_t info)
59 {
60 	return (CTF_INFO_ISROOT_V1(info));
61 }
62 
63 static ushort_t
64 get_root_v2(ushort_t info)
65 {
66 	return (CTF_INFO_ISROOT(info));
67 }
68 
69 static ushort_t
70 get_vlen_v1(ushort_t info)
71 {
72 	return (CTF_INFO_VLEN_V1(info));
73 }
74 
75 static ushort_t
76 get_vlen_v2(ushort_t info)
77 {
78 	return (CTF_INFO_VLEN(info));
79 }
80 
81 static const ctf_fileops_t ctf_fileops[] = {
82 	{ NULL, NULL },
83 	{ get_kind_v1, get_root_v1, get_vlen_v1 },
84 	{ get_kind_v2, get_root_v2, get_vlen_v2 },
85 };
86 
87 /*
88  * Convert a 32-bit ELF symbol into GElf (Elf64) and return a pointer to it.
89  */
90 static Elf64_Sym *
91 sym_to_gelf(const Elf32_Sym *src, Elf64_Sym *dst)
92 {
93 	dst->st_name = src->st_name;
94 	dst->st_value = src->st_value;
95 	dst->st_size = src->st_size;
96 	dst->st_info = src->st_info;
97 	dst->st_other = src->st_other;
98 	dst->st_shndx = src->st_shndx;
99 
100 	return (dst);
101 }
102 
103 /*
104  * Initialize the symtab translation table by filling each entry with the
105  * offset of the CTF type or function data corresponding to each STT_FUNC or
106  * STT_OBJECT entry in the symbol table.
107  */
108 static int
109 init_symtab(ctf_file_t *fp, const ctf_header_t *hp,
110     const ctf_sect_t *sp, const ctf_sect_t *strp)
111 {
112 	const uchar_t *symp = sp->cts_data;
113 	uint_t *xp = fp->ctf_sxlate;
114 	uint_t *xend = xp + fp->ctf_nsyms;
115 
116 	uint_t objtoff = hp->cth_objtoff;
117 	uint_t funcoff = hp->cth_funcoff;
118 
119 	ushort_t info, vlen;
120 	Elf64_Sym sym, *gsp;
121 	const char *name;
122 
123 	/*
124 	 * The CTF data object and function type sections are ordered to match
125 	 * the relative order of the respective symbol types in the symtab.
126 	 * If no type information is available for a symbol table entry, a
127 	 * pad is inserted in the CTF section.  As a further optimization,
128 	 * anonymous or undefined symbols are omitted from the CTF data.
129 	 */
130 	for (; xp < xend; xp++, symp += sp->cts_entsize) {
131 		if (sp->cts_entsize == sizeof (Elf32_Sym))
132 			gsp = sym_to_gelf((Elf32_Sym *)(uintptr_t)symp, &sym);
133 		else
134 			gsp = (Elf64_Sym *)(uintptr_t)symp;
135 
136 		if (gsp->st_name < strp->cts_size)
137 			name = (const char *)strp->cts_data + gsp->st_name;
138 		else
139 			name = _CTF_NULLSTR;
140 
141 		if (gsp->st_name == 0 || gsp->st_shndx == SHN_UNDEF ||
142 		    strcmp(name, "_START_") == 0 ||
143 		    strcmp(name, "_END_") == 0) {
144 			*xp = -1u;
145 			continue;
146 		}
147 
148 		switch (ELF64_ST_TYPE(gsp->st_info)) {
149 		case STT_OBJECT:
150 			if (objtoff >= hp->cth_funcoff ||
151 			    (gsp->st_shndx == SHN_ABS && gsp->st_value == 0)) {
152 				*xp = -1u;
153 				break;
154 			}
155 
156 			*xp = objtoff;
157 			objtoff += sizeof (ushort_t);
158 			break;
159 
160 		case STT_FUNC:
161 			if (funcoff >= hp->cth_typeoff) {
162 				*xp = -1u;
163 				break;
164 			}
165 
166 			*xp = funcoff;
167 
168 			info = *(ushort_t *)((uintptr_t)fp->ctf_buf + funcoff);
169 			vlen = LCTF_INFO_VLEN(fp, info);
170 
171 			/*
172 			 * If we encounter a zero pad at the end, just skip it.
173 			 * Otherwise skip over the function and its return type
174 			 * (+2) and the argument list (vlen).
175 			 */
176 			if (LCTF_INFO_KIND(fp, info) == CTF_K_UNKNOWN &&
177 			    vlen == 0)
178 				funcoff += sizeof (ushort_t); /* skip pad */
179 			else
180 				funcoff += sizeof (ushort_t) * (vlen + 2);
181 			break;
182 
183 		default:
184 			*xp = -1u;
185 			break;
186 		}
187 	}
188 
189 	ctf_dprintf("loaded %lu symtab entries\n", fp->ctf_nsyms);
190 	return (0);
191 }
192 
193 /*
194  * Initialize the type ID translation table with the byte offset of each type,
195  * and initialize the hash tables of each named type.
196  */
197 static int
198 init_types(ctf_file_t *fp, const ctf_header_t *hp)
199 {
200 	/* LINTED - pointer alignment */
201 	const ctf_type_t *tbuf = (ctf_type_t *)(fp->ctf_buf + hp->cth_typeoff);
202 	/* LINTED - pointer alignment */
203 	const ctf_type_t *tend = (ctf_type_t *)(fp->ctf_buf + hp->cth_stroff);
204 
205 	ulong_t pop[CTF_K_MAX + 1] = { 0 };
206 	const ctf_type_t *tp;
207 	ushort_t id, dst;
208 	uint_t *xp;
209 
210 	/*
211 	 * We initially determine whether the container is a child or a parent
212 	 * based on the value of cth_parname.  To support containers that pre-
213 	 * date cth_parname, we also scan the types themselves for references
214 	 * to values in the range reserved for child types in our first pass.
215 	 */
216 	int child = hp->cth_parname != 0;
217 	int nlstructs = 0, nlunions = 0;
218 	int err;
219 
220 	/*
221 	 * We make two passes through the entire type section.  In this first
222 	 * pass, we count the number of each type and the total number of types.
223 	 */
224 	for (tp = tbuf; tp < tend; fp->ctf_typemax++) {
225 		ushort_t kind = LCTF_INFO_KIND(fp, tp->ctt_info);
226 		ulong_t vlen = LCTF_INFO_VLEN(fp, tp->ctt_info);
227 		ssize_t size, increment;
228 
229 		size_t vbytes;
230 		uint_t n;
231 
232 		(void) ctf_get_ctt_size(fp, tp, &size, &increment);
233 
234 		switch (kind) {
235 		case CTF_K_INTEGER:
236 		case CTF_K_FLOAT:
237 			vbytes = sizeof (uint_t);
238 			break;
239 		case CTF_K_ARRAY:
240 			vbytes = sizeof (ctf_array_t);
241 			break;
242 		case CTF_K_FUNCTION:
243 			vbytes = sizeof (ushort_t) * (vlen + (vlen & 1));
244 			break;
245 		case CTF_K_STRUCT:
246 		case CTF_K_UNION:
247 			if (fp->ctf_version == CTF_VERSION_1 ||
248 			    size < CTF_LSTRUCT_THRESH) {
249 				ctf_member_t *mp = (ctf_member_t *)
250 				    ((uintptr_t)tp + increment);
251 
252 				vbytes = sizeof (ctf_member_t) * vlen;
253 				for (n = vlen; n != 0; n--, mp++)
254 					child |= CTF_TYPE_ISCHILD(mp->ctm_type);
255 			} else {
256 				ctf_lmember_t *lmp = (ctf_lmember_t *)
257 				    ((uintptr_t)tp + increment);
258 
259 				vbytes = sizeof (ctf_lmember_t) * vlen;
260 				for (n = vlen; n != 0; n--, lmp++)
261 					child |=
262 					    CTF_TYPE_ISCHILD(lmp->ctlm_type);
263 			}
264 			break;
265 		case CTF_K_ENUM:
266 			vbytes = sizeof (ctf_enum_t) * vlen;
267 			break;
268 		case CTF_K_FORWARD:
269 		case CTF_K_UNKNOWN:
270 			vbytes = 0;
271 			break;
272 		case CTF_K_POINTER:
273 		case CTF_K_TYPEDEF:
274 		case CTF_K_VOLATILE:
275 		case CTF_K_CONST:
276 		case CTF_K_RESTRICT:
277 			child |= CTF_TYPE_ISCHILD(tp->ctt_type);
278 			vbytes = 0;
279 			break;
280 		default:
281 			ctf_dprintf("detected invalid CTF kind -- %u\n", kind);
282 			return (ECTF_CORRUPT);
283 		}
284 		tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes);
285 		pop[kind]++;
286 	}
287 
288 	/*
289 	 * If we detected a reference to a child type ID, then we know this
290 	 * container is a child and may have a parent's types imported later.
291 	 */
292 	if (child) {
293 		ctf_dprintf("CTF container %p is a child\n", (void *)fp);
294 		fp->ctf_flags |= LCTF_CHILD;
295 	} else
296 		ctf_dprintf("CTF container %p is a parent\n", (void *)fp);
297 
298 	/*
299 	 * Now that we've counted up the number of each type, we can allocate
300 	 * the hash tables, type translation table, and pointer table.
301 	 */
302 	if ((err = ctf_hash_create(&fp->ctf_structs,
303 	    pop[CTF_K_STRUCT] + pop[CTF_K_FORWARD])) != 0)
304 		return (err);
305 
306 	if ((err = ctf_hash_create(&fp->ctf_unions, pop[CTF_K_UNION])) != 0)
307 		return (err);
308 
309 	if ((err = ctf_hash_create(&fp->ctf_enums, pop[CTF_K_ENUM])) != 0)
310 		return (err);
311 
312 	if ((err = ctf_hash_create(&fp->ctf_names,
313 	    pop[CTF_K_INTEGER] + pop[CTF_K_FLOAT] + pop[CTF_K_FUNCTION] +
314 	    pop[CTF_K_TYPEDEF] + pop[CTF_K_POINTER] + pop[CTF_K_VOLATILE] +
315 	    pop[CTF_K_CONST] + pop[CTF_K_RESTRICT])) != 0)
316 		return (err);
317 
318 	fp->ctf_txlate = ctf_alloc(sizeof (uint_t) * (fp->ctf_typemax + 1));
319 	fp->ctf_ptrtab = ctf_alloc(sizeof (ushort_t) * (fp->ctf_typemax + 1));
320 
321 	if (fp->ctf_txlate == NULL || fp->ctf_ptrtab == NULL)
322 		return (EAGAIN); /* memory allocation failed */
323 
324 	xp = fp->ctf_txlate;
325 	*xp++ = 0; /* type id 0 is used as a sentinel value */
326 
327 	bzero(fp->ctf_txlate, sizeof (uint_t) * (fp->ctf_typemax + 1));
328 	bzero(fp->ctf_ptrtab, sizeof (ushort_t) * (fp->ctf_typemax + 1));
329 
330 	/*
331 	 * In the second pass through the types, we fill in each entry of the
332 	 * type and pointer tables and add names to the appropriate hashes.
333 	 */
334 	for (id = 1, tp = tbuf; tp < tend; xp++, id++) {
335 		ushort_t kind = LCTF_INFO_KIND(fp, tp->ctt_info);
336 		ulong_t vlen = LCTF_INFO_VLEN(fp, tp->ctt_info);
337 		ssize_t size, increment;
338 
339 		const char *name;
340 		size_t vbytes;
341 		ctf_helem_t *hep;
342 		ctf_encoding_t cte;
343 
344 		(void) ctf_get_ctt_size(fp, tp, &size, &increment);
345 		name = ctf_strptr(fp, tp->ctt_name);
346 
347 		switch (kind) {
348 		case CTF_K_INTEGER:
349 		case CTF_K_FLOAT:
350 			/*
351 			 * Only insert a new integer base type definition if
352 			 * this type name has not been defined yet.  We re-use
353 			 * the names with different encodings for bit-fields.
354 			 */
355 			if ((hep = ctf_hash_lookup(&fp->ctf_names, fp,
356 			    name, strlen(name))) == NULL) {
357 				err = ctf_hash_insert(&fp->ctf_names, fp,
358 				    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
359 				if (err != 0 && err != ECTF_STRTAB)
360 					return (err);
361 			} else if (ctf_type_encoding(fp, hep->h_type,
362 			    &cte) == 0 && cte.cte_bits == 0) {
363 				/*
364 				 * Work-around SOS8 stabs bug: replace existing
365 				 * intrinsic w/ same name if it was zero bits.
366 				 */
367 				hep->h_type = CTF_INDEX_TO_TYPE(id, child);
368 			}
369 			vbytes = sizeof (uint_t);
370 			break;
371 
372 		case CTF_K_ARRAY:
373 			vbytes = sizeof (ctf_array_t);
374 			break;
375 
376 		case CTF_K_FUNCTION:
377 			err = ctf_hash_insert(&fp->ctf_names, fp,
378 			    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
379 			if (err != 0 && err != ECTF_STRTAB)
380 				return (err);
381 			vbytes = sizeof (ushort_t) * (vlen + (vlen & 1));
382 			break;
383 
384 		case CTF_K_STRUCT:
385 			/*
386 			 * If a struct's name is already present as a forward
387 			 * tag, then replace the tag with the struct definition.
388 			 */
389 			if ((hep = ctf_hash_lookup(&fp->ctf_structs, fp,
390 			    name, strlen(name))) == NULL) {
391 				err = ctf_hash_insert(&fp->ctf_structs, fp,
392 				    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
393 				if (err != 0 && err != ECTF_STRTAB)
394 					return (err);
395 			} else
396 				hep->h_type = CTF_INDEX_TO_TYPE(id, child);
397 
398 			if (fp->ctf_version == CTF_VERSION_1 ||
399 			    size < CTF_LSTRUCT_THRESH)
400 				vbytes = sizeof (ctf_member_t) * vlen;
401 			else {
402 				vbytes = sizeof (ctf_lmember_t) * vlen;
403 				nlstructs++;
404 			}
405 			break;
406 
407 		case CTF_K_UNION:
408 			err = ctf_hash_insert(&fp->ctf_unions, fp,
409 			    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
410 			if (err != 0 && err != ECTF_STRTAB)
411 				return (err);
412 
413 			if (fp->ctf_version == CTF_VERSION_1 ||
414 			    size < CTF_LSTRUCT_THRESH)
415 				vbytes = sizeof (ctf_member_t) * vlen;
416 			else {
417 				vbytes = sizeof (ctf_lmember_t) * vlen;
418 				nlunions++;
419 			}
420 			break;
421 
422 		case CTF_K_ENUM:
423 			err = ctf_hash_insert(&fp->ctf_enums, fp,
424 			    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
425 			if (err != 0 && err != ECTF_STRTAB)
426 				return (err);
427 			vbytes = sizeof (ctf_enum_t) * vlen;
428 			break;
429 
430 		case CTF_K_TYPEDEF:
431 			err = ctf_hash_insert(&fp->ctf_names, fp,
432 			    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
433 			if (err != 0 && err != ECTF_STRTAB)
434 				return (err);
435 			vbytes = 0;
436 			break;
437 
438 		case CTF_K_FORWARD:
439 			/*
440 			 * Only insert forward tags into the struct hash if the
441 			 * struct or tag name is not already present.
442 			 */
443 			if (ctf_hash_lookup(&fp->ctf_structs, fp,
444 			    name, strlen(name)) == NULL) {
445 				err = ctf_hash_insert(&fp->ctf_structs, fp,
446 				    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
447 				if (err != 0 && err != ECTF_STRTAB)
448 					return (err);
449 			}
450 			vbytes = 0;
451 			break;
452 
453 		case CTF_K_POINTER:
454 			/*
455 			 * If the type referenced by the pointer is in this CTF
456 			 * container, then store the index of the pointer type
457 			 * in fp->ctf_ptrtab[ index of referenced type ].
458 			 */
459 			if (CTF_TYPE_ISCHILD(tp->ctt_type) == child &&
460 			    CTF_TYPE_TO_INDEX(tp->ctt_type) <= fp->ctf_typemax)
461 				fp->ctf_ptrtab[
462 				    CTF_TYPE_TO_INDEX(tp->ctt_type)] = id;
463 			/*FALLTHRU*/
464 
465 		case CTF_K_VOLATILE:
466 		case CTF_K_CONST:
467 		case CTF_K_RESTRICT:
468 			err = ctf_hash_insert(&fp->ctf_names, fp,
469 			    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
470 			if (err != 0 && err != ECTF_STRTAB)
471 				return (err);
472 			/*FALLTHRU*/
473 
474 		default:
475 			vbytes = 0;
476 			break;
477 		}
478 
479 		*xp = (uint_t)((uintptr_t)tp - (uintptr_t)fp->ctf_buf);
480 		tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes);
481 	}
482 
483 	ctf_dprintf("%lu total types processed\n", fp->ctf_typemax);
484 	ctf_dprintf("%u enum names hashed\n", ctf_hash_size(&fp->ctf_enums));
485 	ctf_dprintf("%u struct names hashed (%d long)\n",
486 	    ctf_hash_size(&fp->ctf_structs), nlstructs);
487 	ctf_dprintf("%u union names hashed (%d long)\n",
488 	    ctf_hash_size(&fp->ctf_unions), nlunions);
489 	ctf_dprintf("%u base type names hashed\n",
490 	    ctf_hash_size(&fp->ctf_names));
491 
492 	/*
493 	 * Make an additional pass through the pointer table to find pointers
494 	 * that point to anonymous typedef nodes.  If we find one, modify the
495 	 * pointer table so that the pointer is also known to point to the
496 	 * node that is referenced by the anonymous typedef node.
497 	 */
498 	for (id = 1; id <= fp->ctf_typemax; id++) {
499 		if ((dst = fp->ctf_ptrtab[id]) != 0) {
500 			tp = LCTF_INDEX_TO_TYPEPTR(fp, id);
501 
502 			if (LCTF_INFO_KIND(fp, tp->ctt_info) == CTF_K_TYPEDEF &&
503 			    strcmp(ctf_strptr(fp, tp->ctt_name), "") == 0 &&
504 			    CTF_TYPE_ISCHILD(tp->ctt_type) == child &&
505 			    CTF_TYPE_TO_INDEX(tp->ctt_type) <= fp->ctf_typemax)
506 				fp->ctf_ptrtab[
507 				    CTF_TYPE_TO_INDEX(tp->ctt_type)] = dst;
508 		}
509 	}
510 
511 	return (0);
512 }
513 
514 /*
515  * Decode the specified CTF buffer and optional symbol table and create a new
516  * CTF container representing the symbolic debugging information.  This code
517  * can be used directly by the debugger, or it can be used as the engine for
518  * ctf_fdopen() or ctf_open(), below.
519  */
520 ctf_file_t *
521 ctf_bufopen(const ctf_sect_t *ctfsect, const ctf_sect_t *symsect,
522     const ctf_sect_t *strsect, int *errp)
523 {
524 	const ctf_preamble_t *pp;
525 	ctf_header_t hp;
526 	ctf_file_t *fp;
527 	void *buf, *base;
528 	size_t size, hdrsz;
529 	int err;
530 
531 	if (ctfsect == NULL || ((symsect == NULL) != (strsect == NULL)))
532 		return (ctf_set_open_errno(errp, EINVAL));
533 
534 	if (symsect != NULL && symsect->cts_entsize != sizeof (Elf32_Sym) &&
535 	    symsect->cts_entsize != sizeof (Elf64_Sym))
536 		return (ctf_set_open_errno(errp, ECTF_SYMTAB));
537 
538 	if (symsect != NULL && symsect->cts_data == NULL)
539 		return (ctf_set_open_errno(errp, ECTF_SYMBAD));
540 
541 	if (strsect != NULL && strsect->cts_data == NULL)
542 		return (ctf_set_open_errno(errp, ECTF_STRBAD));
543 
544 	if (ctfsect->cts_size < sizeof (ctf_preamble_t))
545 		return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
546 
547 	pp = (const ctf_preamble_t *)ctfsect->cts_data;
548 
549 	ctf_dprintf("ctf_bufopen: magic=0x%x version=%u\n",
550 	    pp->ctp_magic, pp->ctp_version);
551 
552 	/*
553 	 * Validate each part of the CTF header (either V1 or V2).
554 	 * First, we validate the preamble (common to all versions).  At that
555 	 * point, we know specific header version, and can validate the
556 	 * version-specific parts including section offsets and alignments.
557 	 */
558 	if (pp->ctp_magic != CTF_MAGIC)
559 		return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
560 
561 	if (pp->ctp_version == CTF_VERSION_2) {
562 		if (ctfsect->cts_size < sizeof (ctf_header_t))
563 			return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
564 
565 		bcopy(ctfsect->cts_data, &hp, sizeof (hp));
566 		hdrsz = sizeof (ctf_header_t);
567 
568 	} else if (pp->ctp_version == CTF_VERSION_1) {
569 		const ctf_header_v1_t *h1p =
570 		    (const ctf_header_v1_t *)ctfsect->cts_data;
571 
572 		if (ctfsect->cts_size < sizeof (ctf_header_v1_t))
573 			return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
574 
575 		bzero(&hp, sizeof (hp));
576 		hp.cth_preamble = h1p->cth_preamble;
577 		hp.cth_objtoff = h1p->cth_objtoff;
578 		hp.cth_funcoff = h1p->cth_funcoff;
579 		hp.cth_typeoff = h1p->cth_typeoff;
580 		hp.cth_stroff = h1p->cth_stroff;
581 		hp.cth_strlen = h1p->cth_strlen;
582 
583 		hdrsz = sizeof (ctf_header_v1_t);
584 	} else
585 		return (ctf_set_open_errno(errp, ECTF_CTFVERS));
586 
587 	size = hp.cth_stroff + hp.cth_strlen;
588 
589 	ctf_dprintf("ctf_bufopen: uncompressed size=%lu\n", (ulong_t)size);
590 
591 	if (hp.cth_lbloff > size || hp.cth_objtoff > size ||
592 	    hp.cth_funcoff > size || hp.cth_typeoff > size ||
593 	    hp.cth_stroff > size)
594 		return (ctf_set_open_errno(errp, ECTF_CORRUPT));
595 
596 	if (hp.cth_lbloff > hp.cth_objtoff ||
597 	    hp.cth_objtoff > hp.cth_funcoff ||
598 	    hp.cth_funcoff > hp.cth_typeoff ||
599 	    hp.cth_typeoff > hp.cth_stroff)
600 		return (ctf_set_open_errno(errp, ECTF_CORRUPT));
601 
602 	if ((hp.cth_lbloff & 3) || (hp.cth_objtoff & 1) ||
603 	    (hp.cth_funcoff & 1) || (hp.cth_typeoff & 3))
604 		return (ctf_set_open_errno(errp, ECTF_CORRUPT));
605 
606 	/*
607 	 * Once everything is determined to be valid, attempt to decompress
608 	 * the CTF data buffer if it is compressed.  Otherwise we just put
609 	 * the data section's buffer pointer into ctf_buf, below.
610 	 */
611 	if (hp.cth_flags & CTF_F_COMPRESS) {
612 		size_t srclen, dstlen;
613 		const void *src;
614 		int rc = Z_OK;
615 
616 		if (ctf_zopen(errp) == NULL)
617 			return (NULL); /* errp is set for us */
618 
619 		if ((base = ctf_data_alloc(size + hdrsz)) == MAP_FAILED)
620 			return (ctf_set_open_errno(errp, ECTF_ZALLOC));
621 
622 		bcopy(ctfsect->cts_data, base, hdrsz);
623 		((ctf_preamble_t *)base)->ctp_flags &= ~CTF_F_COMPRESS;
624 		buf = (uchar_t *)base + hdrsz;
625 
626 		src = (uchar_t *)ctfsect->cts_data + hdrsz;
627 		srclen = ctfsect->cts_size - hdrsz;
628 		dstlen = size;
629 
630 		if ((rc = z_uncompress(buf, &dstlen, src, srclen)) != Z_OK) {
631 			ctf_dprintf("zlib inflate err: %s\n", z_strerror(rc));
632 			ctf_data_free(base, size + hdrsz);
633 			return (ctf_set_open_errno(errp, ECTF_DECOMPRESS));
634 		}
635 
636 		if (dstlen != size) {
637 			ctf_dprintf("zlib inflate short -- got %lu of %lu "
638 			    "bytes\n", (ulong_t)dstlen, (ulong_t)size);
639 			ctf_data_free(base, size + hdrsz);
640 			return (ctf_set_open_errno(errp, ECTF_CORRUPT));
641 		}
642 
643 		ctf_data_protect(base, size + hdrsz);
644 
645 	} else {
646 		base = (void *)ctfsect->cts_data;
647 		buf = (uchar_t *)base + hdrsz;
648 	}
649 
650 	/*
651 	 * Once we have uncompressed and validated the CTF data buffer, we can
652 	 * proceed with allocating a ctf_file_t and initializing it.
653 	 */
654 	if ((fp = ctf_alloc(sizeof (ctf_file_t))) == NULL)
655 		return (ctf_set_open_errno(errp, EAGAIN));
656 
657 	bzero(fp, sizeof (ctf_file_t));
658 	fp->ctf_version = hp.cth_version;
659 	fp->ctf_fileops = &ctf_fileops[hp.cth_version];
660 	bcopy(ctfsect, &fp->ctf_data, sizeof (ctf_sect_t));
661 
662 	if (symsect != NULL) {
663 		bcopy(symsect, &fp->ctf_symtab, sizeof (ctf_sect_t));
664 		bcopy(strsect, &fp->ctf_strtab, sizeof (ctf_sect_t));
665 	}
666 
667 	if (fp->ctf_data.cts_name != NULL)
668 		fp->ctf_data.cts_name = ctf_strdup(fp->ctf_data.cts_name);
669 	if (fp->ctf_symtab.cts_name != NULL)
670 		fp->ctf_symtab.cts_name = ctf_strdup(fp->ctf_symtab.cts_name);
671 	if (fp->ctf_strtab.cts_name != NULL)
672 		fp->ctf_strtab.cts_name = ctf_strdup(fp->ctf_strtab.cts_name);
673 
674 	if (fp->ctf_data.cts_name == NULL)
675 		fp->ctf_data.cts_name = _CTF_NULLSTR;
676 	if (fp->ctf_symtab.cts_name == NULL)
677 		fp->ctf_symtab.cts_name = _CTF_NULLSTR;
678 	if (fp->ctf_strtab.cts_name == NULL)
679 		fp->ctf_strtab.cts_name = _CTF_NULLSTR;
680 
681 	fp->ctf_str[CTF_STRTAB_0].cts_strs = (const char *)buf + hp.cth_stroff;
682 	fp->ctf_str[CTF_STRTAB_0].cts_len = hp.cth_strlen;
683 
684 	if (strsect != NULL) {
685 		fp->ctf_str[CTF_STRTAB_1].cts_strs = strsect->cts_data;
686 		fp->ctf_str[CTF_STRTAB_1].cts_len = strsect->cts_size;
687 	}
688 
689 	fp->ctf_base = base;
690 	fp->ctf_buf = buf;
691 	fp->ctf_size = size + hdrsz;
692 
693 	/*
694 	 * If we have a parent container name and label, store the relocated
695 	 * string pointers in the CTF container for easy access later.
696 	 */
697 	if (hp.cth_parlabel != 0)
698 		fp->ctf_parlabel = ctf_strptr(fp, hp.cth_parlabel);
699 	if (hp.cth_parname != 0)
700 		fp->ctf_parname = ctf_strptr(fp, hp.cth_parname);
701 
702 	ctf_dprintf("ctf_bufopen: parent name %s (label %s)\n",
703 	    fp->ctf_parname ? fp->ctf_parname : "<NULL>",
704 	    fp->ctf_parlabel ? fp->ctf_parlabel : "<NULL>");
705 
706 	/*
707 	 * If we have a symbol table section, allocate and initialize
708 	 * the symtab translation table, pointed to by ctf_sxlate.
709 	 */
710 	if (symsect != NULL) {
711 		fp->ctf_nsyms = symsect->cts_size / symsect->cts_entsize;
712 		fp->ctf_sxlate = ctf_alloc(fp->ctf_nsyms * sizeof (uint_t));
713 
714 		if (fp->ctf_sxlate == NULL) {
715 			(void) ctf_set_open_errno(errp, EAGAIN);
716 			goto bad;
717 		}
718 
719 		if ((err = init_symtab(fp, &hp, symsect, strsect)) != 0) {
720 			(void) ctf_set_open_errno(errp, err);
721 			goto bad;
722 		}
723 	}
724 
725 	if ((err = init_types(fp, &hp)) != 0) {
726 		(void) ctf_set_open_errno(errp, err);
727 		goto bad;
728 	}
729 
730 	/*
731 	 * Initialize the ctf_lookup_by_name top-level dictionary.  We keep an
732 	 * array of type name prefixes and the corresponding ctf_hash to use.
733 	 * NOTE: This code must be kept in sync with the code in ctf_update().
734 	 */
735 	fp->ctf_lookups[0].ctl_prefix = "struct";
736 	fp->ctf_lookups[0].ctl_len = strlen(fp->ctf_lookups[0].ctl_prefix);
737 	fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
738 	fp->ctf_lookups[1].ctl_prefix = "union";
739 	fp->ctf_lookups[1].ctl_len = strlen(fp->ctf_lookups[1].ctl_prefix);
740 	fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
741 	fp->ctf_lookups[2].ctl_prefix = "enum";
742 	fp->ctf_lookups[2].ctl_len = strlen(fp->ctf_lookups[2].ctl_prefix);
743 	fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
744 	fp->ctf_lookups[3].ctl_prefix = _CTF_NULLSTR;
745 	fp->ctf_lookups[3].ctl_len = strlen(fp->ctf_lookups[3].ctl_prefix);
746 	fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
747 	fp->ctf_lookups[4].ctl_prefix = NULL;
748 	fp->ctf_lookups[4].ctl_len = 0;
749 	fp->ctf_lookups[4].ctl_hash = NULL;
750 
751 	if (symsect != NULL) {
752 		if (symsect->cts_entsize == sizeof (Elf64_Sym))
753 			(void) ctf_setmodel(fp, CTF_MODEL_LP64);
754 		else
755 			(void) ctf_setmodel(fp, CTF_MODEL_ILP32);
756 	} else
757 		(void) ctf_setmodel(fp, CTF_MODEL_NATIVE);
758 
759 	fp->ctf_refcnt = 1;
760 	return (fp);
761 
762 bad:
763 	ctf_close(fp);
764 	return (NULL);
765 }
766 
767 /*
768  * Close the specified CTF container and free associated data structures.  Note
769  * that ctf_close() is a reference counted operation: if the specified file is
770  * the parent of other active containers, its reference count will be greater
771  * than one and it will be freed later when no active children exist.
772  */
773 void
774 ctf_close(ctf_file_t *fp)
775 {
776 	ctf_dtdef_t *dtd, *ntd;
777 	ctf_dmdef_t *dmd, *nmd;
778 
779 	if (fp == NULL)
780 		return; /* allow ctf_close(NULL) to simplify caller code */
781 
782 	ctf_dprintf("ctf_close(%p) refcnt=%u\n", (void *)fp, fp->ctf_refcnt);
783 
784 	if (fp->ctf_refcnt > 1) {
785 		fp->ctf_refcnt--;
786 		return;
787 	}
788 
789 	for (dtd = ctf_list_next(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
790 		switch (CTF_INFO_KIND(dtd->dtd_data.ctt_info)) {
791 		case CTF_K_STRUCT:
792 		case CTF_K_UNION:
793 		case CTF_K_ENUM:
794 			for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
795 			    dmd != NULL; dmd = nmd) {
796 				if (dmd->dmd_name != NULL) {
797 					ctf_free(dmd->dmd_name,
798 					    strlen(dmd->dmd_name) + 1);
799 				}
800 				nmd = ctf_list_next(dmd);
801 				ctf_free(dmd, sizeof (ctf_dmdef_t));
802 			}
803 			break;
804 		case CTF_K_FUNCTION:
805 			ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *
806 			    CTF_INFO_VLEN(dtd->dtd_data.ctt_info));
807 			break;
808 		}
809 
810 		if (dtd->dtd_name != NULL)
811 			ctf_free(dtd->dtd_name, strlen(dtd->dtd_name) + 1);
812 
813 		ntd = ctf_list_next(dtd);
814 		ctf_free(dtd, sizeof (ctf_dtdef_t));
815 	}
816 
817 	if (fp->ctf_parent != NULL)
818 		ctf_close(fp->ctf_parent);
819 
820 	if (fp->ctf_flags & LCTF_MMAP) {
821 		if (fp->ctf_data.cts_data != NULL)
822 			ctf_sect_munmap(&fp->ctf_data);
823 		if (fp->ctf_symtab.cts_data != NULL)
824 			ctf_sect_munmap(&fp->ctf_symtab);
825 		if (fp->ctf_strtab.cts_data != NULL)
826 			ctf_sect_munmap(&fp->ctf_strtab);
827 	}
828 
829 	if (fp->ctf_data.cts_name != _CTF_NULLSTR &&
830 	    fp->ctf_data.cts_name != NULL) {
831 		ctf_free((char *)fp->ctf_data.cts_name,
832 		    strlen(fp->ctf_data.cts_name) + 1);
833 	}
834 
835 	if (fp->ctf_symtab.cts_name != _CTF_NULLSTR &&
836 	    fp->ctf_symtab.cts_name != NULL) {
837 		ctf_free((char *)fp->ctf_symtab.cts_name,
838 		    strlen(fp->ctf_symtab.cts_name) + 1);
839 	}
840 
841 	if (fp->ctf_strtab.cts_name != _CTF_NULLSTR &&
842 	    fp->ctf_strtab.cts_name != NULL) {
843 		ctf_free((char *)fp->ctf_strtab.cts_name,
844 		    strlen(fp->ctf_strtab.cts_name) + 1);
845 	}
846 
847 	if (fp->ctf_base != fp->ctf_data.cts_data && fp->ctf_base != NULL)
848 		ctf_data_free((void *)fp->ctf_base, fp->ctf_size);
849 
850 	if (fp->ctf_sxlate != NULL)
851 		ctf_free(fp->ctf_sxlate, sizeof (uint_t) * fp->ctf_nsyms);
852 
853 	if (fp->ctf_txlate != NULL) {
854 		ctf_free(fp->ctf_txlate,
855 		    sizeof (uint_t) * (fp->ctf_typemax + 1));
856 	}
857 
858 	if (fp->ctf_ptrtab != NULL) {
859 		ctf_free(fp->ctf_ptrtab,
860 		    sizeof (ushort_t) * (fp->ctf_typemax + 1));
861 	}
862 
863 	ctf_hash_destroy(&fp->ctf_structs);
864 	ctf_hash_destroy(&fp->ctf_unions);
865 	ctf_hash_destroy(&fp->ctf_enums);
866 	ctf_hash_destroy(&fp->ctf_names);
867 
868 	ctf_free(fp, sizeof (ctf_file_t));
869 }
870 
871 /*
872  * Return the CTF handle for the parent CTF container, if one exists.
873  * Otherwise return NULL to indicate this container has no imported parent.
874  */
875 ctf_file_t *
876 ctf_parent_file(ctf_file_t *fp)
877 {
878 	return (fp->ctf_parent);
879 }
880 
881 /*
882  * Return the name of the parent CTF container, if one exists.  Otherwise
883  * return NULL to indicate this container is a root container.
884  */
885 const char *
886 ctf_parent_name(ctf_file_t *fp)
887 {
888 	return (fp->ctf_parname);
889 }
890 
891 /*
892  * Import the types from the specified parent container by storing a pointer
893  * to it in ctf_parent and incrementing its reference count.  Only one parent
894  * is allowed: if a parent already exists, it is replaced by the new parent.
895  */
896 int
897 ctf_import(ctf_file_t *fp, ctf_file_t *pfp)
898 {
899 	if (fp == NULL || fp == pfp || (pfp != NULL && pfp->ctf_refcnt == 0))
900 		return (ctf_set_errno(fp, EINVAL));
901 
902 	if (pfp != NULL && pfp->ctf_dmodel != fp->ctf_dmodel)
903 		return (ctf_set_errno(fp, ECTF_DMODEL));
904 
905 	if (fp->ctf_parent != NULL)
906 		ctf_close(fp->ctf_parent);
907 
908 	if (pfp != NULL) {
909 		fp->ctf_flags |= LCTF_CHILD;
910 		pfp->ctf_refcnt++;
911 	}
912 
913 	fp->ctf_parent = pfp;
914 	return (0);
915 }
916 
917 /*
918  * Set the data model constant for the CTF container.
919  */
920 int
921 ctf_setmodel(ctf_file_t *fp, int model)
922 {
923 	const ctf_dmodel_t *dp;
924 
925 	for (dp = _libctf_models; dp->ctd_name != NULL; dp++) {
926 		if (dp->ctd_code == model) {
927 			fp->ctf_dmodel = dp;
928 			return (0);
929 		}
930 	}
931 
932 	return (ctf_set_errno(fp, EINVAL));
933 }
934 
935 /*
936  * Return the data model constant for the CTF container.
937  */
938 int
939 ctf_getmodel(ctf_file_t *fp)
940 {
941 	return (fp->ctf_dmodel->ctd_code);
942 }
943 
944 void
945 ctf_setspecific(ctf_file_t *fp, void *data)
946 {
947 	fp->ctf_specific = data;
948 }
949 
950 void *
951 ctf_getspecific(ctf_file_t *fp)
952 {
953 	return (fp->ctf_specific);
954 }
955