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