VirtualBox

source: vbox/trunk/src/VBox/ExtPacks/VBoxDTrace/onnv/common/ctf/ctf_open.c@ 53716

Last change on this file since 53716 was 53657, checked in by vboxsync, 10 years ago

VBoxDTrace: CTF compiles and links. (r33)

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette