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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * Create and parse buffers containing CTF data.
28 */
29
30 #if HAVE_NBTOOL_CONFIG_H
31 #include "nbtool_config.h"
32 #endif
33
34 #include <sys/types.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <strings.h>
38 #include <ctype.h>
39 #include <zlib.h>
40 #include <elf.h>
41
42 #include "ctf_headers.h"
43 #include "ctftools.h"
44 #include "strtab.h"
45 #include "memory.h"
46
47 /*
48 * Name of the file currently being read, used to print error messages. We
49 * assume that only one file will be read at a time, and thus make no attempt
50 * to allow curfile to be used simultaneously by multiple threads.
51 *
52 * The value is only valid during a call to ctf_load.
53 */
54 static char *curfile;
55
56 #define CTF_BUF_CHUNK_SIZE (64 * 1024)
57 #define RES_BUF_CHUNK_SIZE (64 * 1024)
58
59 struct ctf_buf {
60 strtab_t ctb_strtab; /* string table */
61 caddr_t ctb_base; /* pointer to base of buffer */
62 caddr_t ctb_end; /* pointer to end of buffer */
63 caddr_t ctb_ptr; /* pointer to empty buffer space */
64 size_t ctb_size; /* size of buffer */
65 int nptent; /* number of processed types */
66 int ntholes; /* number of type holes */
67 };
68
69 /*
70 * Macros to reverse byte order
71 */
72 #define BSWAP_8(x) ((x) & 0xff)
73 #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
74 #define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
75
76 #define SWAP_16(x) (x) = BSWAP_16(x)
77 #define SWAP_32(x) (x) = BSWAP_32(x)
78
79 static int target_requires_swap;
80
81 /*PRINTFLIKE1*/
82 static void __printflike(1, 2) __dead
parseterminate(const char * fmt,...)83 parseterminate(const char *fmt, ...)
84 {
85 static char msgbuf[1024]; /* sigh */
86 va_list ap;
87
88 va_start(ap, fmt);
89 vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
90 va_end(ap);
91
92 terminate("%s: %s\n", curfile, msgbuf);
93 }
94
95 static void
ctf_buf_grow(ctf_buf_t * b)96 ctf_buf_grow(ctf_buf_t *b)
97 {
98 off_t ptroff = b->ctb_ptr - b->ctb_base;
99
100 b->ctb_size += CTF_BUF_CHUNK_SIZE;
101 b->ctb_base = xrealloc(b->ctb_base, b->ctb_size);
102 b->ctb_end = b->ctb_base + b->ctb_size;
103 b->ctb_ptr = b->ctb_base + ptroff;
104 }
105
106 static ctf_buf_t *
ctf_buf_new(void)107 ctf_buf_new(void)
108 {
109 ctf_buf_t *b = xcalloc(sizeof (ctf_buf_t));
110
111 strtab_create(&b->ctb_strtab);
112 ctf_buf_grow(b);
113
114 return (b);
115 }
116
117 static void
ctf_buf_free(ctf_buf_t * b)118 ctf_buf_free(ctf_buf_t *b)
119 {
120 strtab_destroy(&b->ctb_strtab);
121 free(b->ctb_base);
122 free(b);
123 }
124
125 static uint_t
ctf_buf_cur(ctf_buf_t * b)126 ctf_buf_cur(ctf_buf_t *b)
127 {
128 return (b->ctb_ptr - b->ctb_base);
129 }
130
131 static void
ctf_buf_write(ctf_buf_t * b,void const * p,size_t n)132 ctf_buf_write(ctf_buf_t *b, void const *p, size_t n)
133 {
134 size_t len;
135
136 while (n != 0) {
137 if (b->ctb_ptr == b->ctb_end)
138 ctf_buf_grow(b);
139
140 len = MIN((size_t)(b->ctb_end - b->ctb_ptr), n);
141 bcopy(p, b->ctb_ptr, len);
142 b->ctb_ptr += len;
143
144 p = (char const *)p + len;
145 n -= len;
146 }
147 }
148
149 static int
write_label(void * arg1,void * arg2)150 write_label(void *arg1, void *arg2)
151 {
152 labelent_t *le = arg1;
153 ctf_buf_t *b = arg2;
154 ctf_lblent_t ctl;
155
156 ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name);
157 ctl.ctl_typeidx = le->le_idx;
158
159 if (target_requires_swap) {
160 SWAP_32(ctl.ctl_label);
161 SWAP_32(ctl.ctl_typeidx);
162 }
163
164 ctf_buf_write(b, &ctl, sizeof (ctl));
165
166 return (1);
167 }
168
169 static void
write_objects(iidesc_t * idp,ctf_buf_t * b)170 write_objects(iidesc_t *idp, ctf_buf_t *b)
171 {
172 ushort_t id = (idp ? idp->ii_dtype->t_id : 0);
173
174 if (target_requires_swap) {
175 SWAP_16(id);
176 }
177
178 ctf_buf_write(b, &id, sizeof (id));
179
180 debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id);
181 }
182
183 static void
write_functions(iidesc_t * idp,ctf_buf_t * b)184 write_functions(iidesc_t *idp, ctf_buf_t *b)
185 {
186 ushort_t fdata[2];
187 ushort_t id;
188 int nargs;
189 int i;
190
191 if (!idp) {
192 fdata[0] = 0;
193 ctf_buf_write(b, &fdata[0], sizeof (fdata[0]));
194
195 debug(3, "Wrote function (null)\n");
196 return;
197 }
198
199 nargs = idp->ii_nargs + (idp->ii_vargs != 0);
200
201 if (nargs > CTF_MAX_VLEN) {
202 terminate("function %s has too many args: %d > %d\n",
203 idp->ii_name, nargs, CTF_MAX_VLEN);
204 }
205
206 fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs);
207 fdata[1] = idp->ii_dtype->t_id;
208
209 if (target_requires_swap) {
210 SWAP_16(fdata[0]);
211 SWAP_16(fdata[1]);
212 }
213
214 ctf_buf_write(b, fdata, sizeof (fdata));
215
216 for (i = 0; i < idp->ii_nargs; i++) {
217 id = idp->ii_args[i]->t_id;
218
219 if (target_requires_swap) {
220 SWAP_16(id);
221 }
222
223 ctf_buf_write(b, &id, sizeof (id));
224 }
225
226 if (idp->ii_vargs) {
227 id = 0;
228 ctf_buf_write(b, &id, sizeof (id));
229 }
230
231 debug(3, "Wrote function %s (%d args)\n", idp->ii_name, nargs);
232 }
233
234 /*
235 * Depending on the size of the type being described, either a ctf_stype_t (for
236 * types with size < CTF_LSTRUCT_THRESH) or a ctf_type_t (all others) will be
237 * written. We isolate the determination here so the rest of the writer code
238 * doesn't need to care.
239 */
240 static void
write_sized_type_rec(ctf_buf_t * b,ctf_type_t * ctt,size_t size)241 write_sized_type_rec(ctf_buf_t *b, ctf_type_t *ctt, size_t size)
242 {
243 if (size > CTF_MAX_SIZE) {
244 ctt->ctt_size = CTF_LSIZE_SENT;
245 ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
246 ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
247 if (target_requires_swap) {
248 SWAP_32(ctt->ctt_name);
249 SWAP_16(ctt->ctt_info);
250 SWAP_16(ctt->ctt_size);
251 SWAP_32(ctt->ctt_lsizehi);
252 SWAP_32(ctt->ctt_lsizelo);
253 }
254 ctf_buf_write(b, ctt, sizeof (*ctt));
255 } else {
256 ctf_stype_t *cts = (ctf_stype_t *)ctt;
257
258 cts->ctt_size = (ushort_t)size;
259
260 if (target_requires_swap) {
261 SWAP_32(cts->ctt_name);
262 SWAP_16(cts->ctt_info);
263 SWAP_16(cts->ctt_size);
264 }
265
266 ctf_buf_write(b, cts, sizeof (*cts));
267 }
268 }
269
270 static void
write_unsized_type_rec(ctf_buf_t * b,ctf_type_t * ctt)271 write_unsized_type_rec(ctf_buf_t *b, ctf_type_t *ctt)
272 {
273 ctf_stype_t *cts = (ctf_stype_t *)ctt;
274
275 if (target_requires_swap) {
276 SWAP_32(cts->ctt_name);
277 SWAP_16(cts->ctt_info);
278 SWAP_16(cts->ctt_size);
279 }
280
281 ctf_buf_write(b, cts, sizeof (*cts));
282 }
283
284 static int
write_type(void * arg1,void * arg2)285 write_type(void *arg1, void *arg2)
286 {
287 tdesc_t *tp = arg1;
288 ctf_buf_t *b = arg2;
289 elist_t *ep;
290 mlist_t *mp;
291 intr_t *ip;
292
293 size_t offset;
294 uint_t encoding;
295 uint_t data;
296 int isroot = tp->t_flags & TDESC_F_ISROOT;
297 int i;
298
299 ctf_type_t ctt;
300 ctf_array_t cta;
301 ctf_member_t ctm;
302 ctf_lmember_t ctlm;
303 ctf_enum_t cte;
304 ushort_t id;
305
306 ctlm.ctlm_pad = 0;
307
308 /*
309 * There shouldn't be any holes in the type list (where a hole is
310 * defined as two consecutive tdescs without consecutive ids), but
311 * check for them just in case. If we do find holes, we need to make
312 * fake entries to fill the holes, or we won't be able to reconstruct
313 * the tree from the written data.
314 */
315 if (++b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
316 debug(2, "genctf: type hole from %d < x < %d\n",
317 b->nptent - 1, CTF_TYPE_TO_INDEX(tp->t_id));
318
319 ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, 0);
320 ctt.ctt_info = CTF_TYPE_INFO(0, 0, 0);
321 while (b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
322 write_sized_type_rec(b, &ctt, 0);
323 b->nptent++;
324 }
325 }
326
327 offset = strtab_insert(&b->ctb_strtab, tp->t_name);
328 ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
329
330 switch (tp->t_type) {
331 case INTRINSIC:
332 ip = tp->t_intr;
333 if (ip->intr_type == INTR_INT)
334 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_INTEGER,
335 isroot, 1);
336 else
337 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FLOAT, isroot, 1);
338 write_sized_type_rec(b, &ctt, tp->t_size);
339
340 encoding = 0;
341
342 if (ip->intr_type == INTR_INT) {
343 if (ip->intr_signed)
344 encoding |= CTF_INT_SIGNED;
345 if (ip->intr_iformat == 'c')
346 encoding |= CTF_INT_CHAR;
347 else if (ip->intr_iformat == 'b')
348 encoding |= CTF_INT_BOOL;
349 else if (ip->intr_iformat == 'v')
350 encoding |= CTF_INT_VARARGS;
351 } else
352 encoding = ip->intr_fformat;
353
354 data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits);
355 if (target_requires_swap) {
356 SWAP_32(data);
357 }
358 ctf_buf_write(b, &data, sizeof (data));
359 break;
360
361 case POINTER:
362 case REFERENCE: /* XXX: */
363 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_POINTER, isroot, 0);
364 ctt.ctt_type = tp->t_tdesc->t_id;
365 write_unsized_type_rec(b, &ctt);
366 break;
367
368 case ARRAY:
369 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, isroot, 1);
370 write_sized_type_rec(b, &ctt, tp->t_size);
371
372 cta.cta_contents = tp->t_ardef->ad_contents->t_id;
373 cta.cta_index = tp->t_ardef->ad_idxtype->t_id;
374 cta.cta_nelems = tp->t_ardef->ad_nelems;
375 if (target_requires_swap) {
376 SWAP_16(cta.cta_contents);
377 SWAP_16(cta.cta_index);
378 SWAP_32(cta.cta_nelems);
379 }
380 ctf_buf_write(b, &cta, sizeof (cta));
381 break;
382
383 case STRUCT:
384 case UNION:
385 case CLASS:
386 for (i = 0, mp = tp->t_members; mp != NULL; mp = mp->ml_next)
387 i++; /* count up struct or union members */
388
389 if (i > CTF_MAX_VLEN) {
390 warning("sou %s has too many members: %d > %d\n",
391 tdesc_name(tp), i, CTF_MAX_VLEN);
392 i = CTF_MAX_VLEN;
393 }
394
395 if (tp->t_type == STRUCT)
396 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, isroot, i);
397 else
398 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, isroot, i);
399
400 write_sized_type_rec(b, &ctt, tp->t_size);
401
402 if (tp->t_size < CTF_LSTRUCT_THRESH) {
403 for (mp = tp->t_members; mp != NULL && i > 0;
404 mp = mp->ml_next) {
405 offset = strtab_insert(&b->ctb_strtab,
406 mp->ml_name);
407
408 ctm.ctm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
409 offset);
410 ctm.ctm_type = mp->ml_type->t_id;
411 ctm.ctm_offset = mp->ml_offset;
412 if (target_requires_swap) {
413 SWAP_32(ctm.ctm_name);
414 SWAP_16(ctm.ctm_type);
415 SWAP_16(ctm.ctm_offset);
416 }
417 ctf_buf_write(b, &ctm, sizeof (ctm));
418 i--;
419 }
420 } else {
421 for (mp = tp->t_members; mp != NULL && i > 0;
422 mp = mp->ml_next) {
423 offset = strtab_insert(&b->ctb_strtab,
424 mp->ml_name);
425
426 ctlm.ctlm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
427 offset);
428 ctlm.ctlm_type = mp->ml_type->t_id;
429 ctlm.ctlm_offsethi =
430 CTF_OFFSET_TO_LMEMHI(mp->ml_offset);
431 ctlm.ctlm_offsetlo =
432 CTF_OFFSET_TO_LMEMLO(mp->ml_offset);
433
434 if (target_requires_swap) {
435 SWAP_32(ctlm.ctlm_name);
436 SWAP_16(ctlm.ctlm_type);
437 SWAP_32(ctlm.ctlm_offsethi);
438 SWAP_32(ctlm.ctlm_offsetlo);
439 }
440
441 ctf_buf_write(b, &ctlm, sizeof (ctlm));
442 i--;
443 }
444 }
445 break;
446
447 case ENUM:
448 for (i = 0, ep = tp->t_emem; ep != NULL; ep = ep->el_next)
449 i++; /* count up enum members */
450
451 if (i > CTF_MAX_VLEN) {
452 warning("enum %s has too many values: %d > %d\n",
453 tdesc_name(tp), i, CTF_MAX_VLEN);
454 i = CTF_MAX_VLEN;
455 }
456
457 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, isroot, i);
458 write_sized_type_rec(b, &ctt, tp->t_size);
459
460 for (ep = tp->t_emem; ep != NULL && i > 0; ep = ep->el_next) {
461 offset = strtab_insert(&b->ctb_strtab, ep->el_name);
462 cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
463 cte.cte_value = ep->el_number;
464
465 if (target_requires_swap) {
466 SWAP_32(cte.cte_name);
467 SWAP_32(cte.cte_value);
468 }
469
470 ctf_buf_write(b, &cte, sizeof (cte));
471 i--;
472 }
473 break;
474
475 case FORWARD:
476 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, isroot, 0);
477 ctt.ctt_type = 0;
478 write_unsized_type_rec(b, &ctt);
479 break;
480
481 case TYPEDEF:
482 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, isroot, 0);
483 ctt.ctt_type = tp->t_tdesc->t_id;
484 write_unsized_type_rec(b, &ctt);
485 break;
486
487 case VOLATILE:
488 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_VOLATILE, isroot, 0);
489 ctt.ctt_type = tp->t_tdesc->t_id;
490 write_unsized_type_rec(b, &ctt);
491 break;
492
493 case CONST:
494 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_CONST, isroot, 0);
495 ctt.ctt_type = tp->t_tdesc->t_id;
496 write_unsized_type_rec(b, &ctt);
497 break;
498
499 case FUNCTION:
500 i = tp->t_fndef->fn_nargs + tp->t_fndef->fn_vargs;
501
502 if (i > CTF_MAX_VLEN) {
503 terminate("function %s has too many args: %d > %d\n",
504 tdesc_name(tp), i, CTF_MAX_VLEN);
505 }
506
507 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, isroot, i);
508 ctt.ctt_type = tp->t_fndef->fn_ret->t_id;
509 write_unsized_type_rec(b, &ctt);
510
511 for (i = 0; i < (int) tp->t_fndef->fn_nargs; i++) {
512 id = tp->t_fndef->fn_args[i]->t_id;
513
514 if (target_requires_swap) {
515 SWAP_16(id);
516 }
517
518 ctf_buf_write(b, &id, sizeof (id));
519 }
520
521 if (tp->t_fndef->fn_vargs) {
522 id = 0;
523 ctf_buf_write(b, &id, sizeof (id));
524 i++;
525 }
526
527 if (i & 1) {
528 id = 0;
529 ctf_buf_write(b, &id, sizeof (id));
530 }
531 break;
532
533 case RESTRICT:
534 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_RESTRICT, isroot, 0);
535 ctt.ctt_type = tp->t_tdesc->t_id;
536 write_unsized_type_rec(b, &ctt);
537 break;
538
539 default:
540 warning("Can't write unknown type %d\n", tp->t_type);
541 }
542
543 debug(3, "Wrote type %d %s\n", tp->t_id, tdesc_name(tp));
544
545 return (1);
546 }
547
548 typedef struct resbuf {
549 caddr_t rb_base;
550 caddr_t rb_ptr;
551 size_t rb_size;
552 z_stream rb_zstr;
553 } resbuf_t;
554
555 static void
rbzs_grow(resbuf_t * rb)556 rbzs_grow(resbuf_t *rb)
557 {
558 off_t ptroff = (caddr_t)rb->rb_zstr.next_out - rb->rb_base;
559
560 rb->rb_size += RES_BUF_CHUNK_SIZE;
561 rb->rb_base = xrealloc(rb->rb_base, rb->rb_size);
562 rb->rb_ptr = rb->rb_base + ptroff;
563 rb->rb_zstr.next_out = (Bytef *)(rb->rb_ptr);
564 rb->rb_zstr.avail_out += RES_BUF_CHUNK_SIZE;
565 }
566
567 static void
compress_start(resbuf_t * rb)568 compress_start(resbuf_t *rb)
569 {
570 int rc;
571
572 rb->rb_zstr.zalloc = (alloc_func)0;
573 rb->rb_zstr.zfree = (free_func)0;
574 rb->rb_zstr.opaque = (voidpf)0;
575
576 if ((rc = deflateInit(&rb->rb_zstr, Z_BEST_COMPRESSION)) != Z_OK)
577 parseterminate("zlib start failed: %s", zError(rc));
578 }
579
580 static ssize_t
compress_buffer(void * buf,size_t n,void * data)581 compress_buffer(void *buf, size_t n, void *data)
582 {
583 resbuf_t *rb = (resbuf_t *)data;
584 int rc;
585
586 rb->rb_zstr.next_out = (Bytef *)rb->rb_ptr;
587 rb->rb_zstr.avail_out = rb->rb_size - (rb->rb_ptr - rb->rb_base);
588 rb->rb_zstr.next_in = buf;
589 rb->rb_zstr.avail_in = n;
590
591 while (rb->rb_zstr.avail_in) {
592 if (rb->rb_zstr.avail_out == 0)
593 rbzs_grow(rb);
594
595 if ((rc = deflate(&rb->rb_zstr, Z_NO_FLUSH)) != Z_OK)
596 parseterminate("zlib deflate failed: %s", zError(rc));
597 }
598 rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
599
600 return (n);
601 }
602
603 static void
compress_flush(resbuf_t * rb,int type)604 compress_flush(resbuf_t *rb, int type)
605 {
606 int rc;
607
608 for (;;) {
609 if (rb->rb_zstr.avail_out == 0)
610 rbzs_grow(rb);
611
612 rc = deflate(&rb->rb_zstr, type);
613 if ((type == Z_FULL_FLUSH && rc == Z_BUF_ERROR) ||
614 (type == Z_FINISH && rc == Z_STREAM_END))
615 break;
616 else if (rc != Z_OK)
617 parseterminate("zlib finish failed: %s", zError(rc));
618 }
619 rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
620 }
621
622 static void
compress_end(resbuf_t * rb)623 compress_end(resbuf_t *rb)
624 {
625 int rc;
626
627 compress_flush(rb, Z_FINISH);
628
629 if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK)
630 parseterminate("zlib end failed: %s", zError(rc));
631 }
632
633 /*
634 * Pad the buffer to a power-of-2 boundary
635 */
636 static void
pad_buffer(ctf_buf_t * buf,int align)637 pad_buffer(ctf_buf_t *buf, int align)
638 {
639 uint_t cur = ctf_buf_cur(buf);
640 ssize_t topad = (align - (cur % align)) % align;
641 static const char pad[8] = { 0 };
642
643 while (topad > 0) {
644 ctf_buf_write(buf, pad, (topad > 8 ? 8 : topad));
645 topad -= 8;
646 }
647 }
648
649 static ssize_t
bcopy_data(void * buf,size_t n,void * data)650 bcopy_data(void *buf, size_t n, void *data)
651 {
652 caddr_t *posp = (caddr_t *)data;
653 bcopy(buf, *posp, n);
654 *posp += n;
655 return (n);
656 }
657
658 static caddr_t
write_buffer(ctf_header_t * h,ctf_buf_t * buf,size_t * resszp)659 write_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
660 {
661 caddr_t outbuf;
662 caddr_t bufpos;
663
664 outbuf = xmalloc(sizeof (ctf_header_t) + (buf->ctb_ptr - buf->ctb_base)
665 + buf->ctb_strtab.str_size);
666
667 bufpos = outbuf;
668 (void) bcopy_data(h, sizeof (ctf_header_t), &bufpos);
669 (void) bcopy_data(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
670 &bufpos);
671 (void) strtab_write(&buf->ctb_strtab, bcopy_data, &bufpos);
672 *resszp = bufpos - outbuf;
673 return (outbuf);
674 }
675
676 /*
677 * Create the compression buffer, and fill it with the CTF and string
678 * table data. We flush the compression state between the two so the
679 * dictionary used for the string tables won't be polluted with values
680 * that made sense for the CTF data.
681 */
682 static caddr_t
write_compressed_buffer(ctf_header_t * h,ctf_buf_t * buf,size_t * resszp)683 write_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
684 {
685 resbuf_t resbuf;
686 resbuf.rb_size = RES_BUF_CHUNK_SIZE;
687 resbuf.rb_base = xmalloc(resbuf.rb_size);
688 bcopy(h, resbuf.rb_base, sizeof (ctf_header_t));
689 resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t);
690
691 compress_start(&resbuf);
692 (void) compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
693 &resbuf);
694 compress_flush(&resbuf, Z_FULL_FLUSH);
695 (void) strtab_write(&buf->ctb_strtab, compress_buffer, &resbuf);
696 compress_end(&resbuf);
697
698 *resszp = (resbuf.rb_ptr - resbuf.rb_base);
699 return (resbuf.rb_base);
700 }
701
702 caddr_t
ctf_gen(iiburst_t * iiburst,size_t * resszp,int do_compress)703 ctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress)
704 {
705 ctf_buf_t *buf = ctf_buf_new();
706 ctf_header_t h;
707 caddr_t outbuf;
708
709 int i;
710
711 target_requires_swap = do_compress & CTF_SWAP_BYTES;
712 do_compress &= ~CTF_SWAP_BYTES;
713
714 /*
715 * Prepare the header, and create the CTF output buffers. The data
716 * object section and function section are both lists of 2-byte
717 * integers; we pad these out to the next 4-byte boundary if needed.
718 */
719 h.cth_magic = CTF_MAGIC;
720 h.cth_version = CTF_VERSION;
721 h.cth_flags = do_compress ? CTF_F_COMPRESS : 0;
722 h.cth_parlabel = strtab_insert(&buf->ctb_strtab,
723 iiburst->iib_td->td_parlabel);
724 h.cth_parname = strtab_insert(&buf->ctb_strtab,
725 iiburst->iib_td->td_parname);
726
727 h.cth_lbloff = 0;
728 (void) list_iter(iiburst->iib_td->td_labels, write_label,
729 buf);
730
731 pad_buffer(buf, 2);
732 h.cth_objtoff = ctf_buf_cur(buf);
733 for (i = 0; i < iiburst->iib_nobjts; i++)
734 write_objects(iiburst->iib_objts[i], buf);
735
736 pad_buffer(buf, 2);
737 h.cth_funcoff = ctf_buf_cur(buf);
738 for (i = 0; i < iiburst->iib_nfuncs; i++)
739 write_functions(iiburst->iib_funcs[i], buf);
740
741 pad_buffer(buf, 4);
742 h.cth_typeoff = ctf_buf_cur(buf);
743 (void) list_iter(iiburst->iib_types, write_type, buf);
744
745 debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types));
746
747 h.cth_stroff = ctf_buf_cur(buf);
748 h.cth_strlen = strtab_size(&buf->ctb_strtab);
749
750 if (target_requires_swap) {
751 SWAP_16(h.cth_preamble.ctp_magic);
752 SWAP_32(h.cth_parlabel);
753 SWAP_32(h.cth_parname);
754 SWAP_32(h.cth_lbloff);
755 SWAP_32(h.cth_objtoff);
756 SWAP_32(h.cth_funcoff);
757 SWAP_32(h.cth_typeoff);
758 SWAP_32(h.cth_stroff);
759 SWAP_32(h.cth_strlen);
760 }
761
762 /*
763 * We only do compression for ctfmerge, as ctfconvert is only
764 * supposed to be used on intermediary build objects. This is
765 * significantly faster.
766 */
767 if (do_compress)
768 outbuf = write_compressed_buffer(&h, buf, resszp);
769 else
770 outbuf = write_buffer(&h, buf, resszp);
771
772 ctf_buf_free(buf);
773 return (outbuf);
774 }
775
776 static void
get_ctt_size(ctf_type_t * ctt,size_t * sizep,size_t * incrementp)777 get_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp)
778 {
779 if (ctt->ctt_size == CTF_LSIZE_SENT) {
780 *sizep = (size_t)CTF_TYPE_LSIZE(ctt);
781 *incrementp = sizeof (ctf_type_t);
782 } else {
783 *sizep = ctt->ctt_size;
784 *incrementp = sizeof (ctf_stype_t);
785 }
786 }
787
788 static int
count_types(ctf_header_t * h,caddr_t data)789 count_types(ctf_header_t *h, caddr_t data)
790 {
791 caddr_t dptr = data + h->cth_typeoff;
792 int count = 0;
793
794 dptr = data + h->cth_typeoff;
795 while (dptr < data + h->cth_stroff) {
796 void *v = (void *) dptr;
797 ctf_type_t *ctt = v;
798 size_t vlen = CTF_INFO_VLEN(ctt->ctt_info);
799 size_t size, increment;
800
801 get_ctt_size(ctt, &size, &increment);
802
803 switch (CTF_INFO_KIND(ctt->ctt_info)) {
804 case CTF_K_INTEGER:
805 case CTF_K_FLOAT:
806 dptr += 4;
807 break;
808 case CTF_K_POINTER:
809 case CTF_K_FORWARD:
810 case CTF_K_TYPEDEF:
811 case CTF_K_VOLATILE:
812 case CTF_K_CONST:
813 case CTF_K_RESTRICT:
814 case CTF_K_FUNCTION:
815 dptr += sizeof (ushort_t) * (vlen + (vlen & 1));
816 break;
817 case CTF_K_ARRAY:
818 dptr += sizeof (ctf_array_t);
819 break;
820 case CTF_K_STRUCT:
821 case CTF_K_UNION:
822 if (size < CTF_LSTRUCT_THRESH)
823 dptr += sizeof (ctf_member_t) * vlen;
824 else
825 dptr += sizeof (ctf_lmember_t) * vlen;
826 break;
827 case CTF_K_ENUM:
828 dptr += sizeof (ctf_enum_t) * vlen;
829 break;
830 case CTF_K_UNKNOWN:
831 break;
832 default:
833 parseterminate("Unknown CTF type %d (#%d) at %#jx",
834 CTF_INFO_KIND(ctt->ctt_info), count,
835 (intmax_t)(dptr - data));
836 }
837
838 dptr += increment;
839 count++;
840 }
841
842 debug(3, "CTF read %d types\n", count);
843
844 return (count);
845 }
846
847 /*
848 * Resurrect the labels stored in the CTF data, returning the index associated
849 * with a label provided by the caller. There are several cases, outlined
850 * below. Note that, given two labels, the one associated with the lesser type
851 * index is considered to be older than the other.
852 *
853 * 1. matchlbl == NULL - return the index of the most recent label.
854 * 2. matchlbl == "BASE" - return the index of the oldest label.
855 * 3. matchlbl != NULL, but doesn't match any labels in the section - warn
856 * the user, and proceed as if matchlbl == "BASE" (for safety).
857 * 4. matchlbl != NULL, and matches one of the labels in the section - return
858 * the type index associated with the label.
859 */
860 static int
resurrect_labels(ctf_header_t * h,tdata_t * td,caddr_t ctfdata,char * matchlbl)861 resurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl)
862 {
863 caddr_t buf = ctfdata + h->cth_lbloff;
864 caddr_t sbuf = ctfdata + h->cth_stroff;
865 size_t bufsz = h->cth_objtoff - h->cth_lbloff;
866 int lastidx = 0, baseidx = -1;
867 char *baselabel = NULL;
868 ctf_lblent_t *ctl;
869 void *v = (void *) buf;
870
871 for (ctl = v; (caddr_t)ctl < buf + bufsz; ctl++) {
872 char *label = sbuf + ctl->ctl_label;
873
874 lastidx = ctl->ctl_typeidx;
875
876 debug(3, "Resurrected label %s type idx %d\n", label, lastidx);
877
878 tdata_label_add(td, label, lastidx);
879
880 if (baseidx == -1) {
881 baseidx = lastidx;
882 baselabel = label;
883 if (matchlbl != NULL && streq(matchlbl, "BASE"))
884 return (lastidx);
885 }
886
887 if (matchlbl != NULL && streq(label, matchlbl))
888 return (lastidx);
889 }
890
891 if (matchlbl != NULL) {
892 /* User provided a label that didn't match */
893 warning("%s: Cannot find label `%s' - using base (%s)\n",
894 curfile, matchlbl, (baselabel ? baselabel : "NONE"));
895
896 tdata_label_free(td);
897 tdata_label_add(td, baselabel, baseidx);
898
899 return (baseidx);
900 }
901
902 return (lastidx);
903 }
904
905 static void
resurrect_objects(ctf_header_t * h,tdata_t * td,tdesc_t ** tdarr,int tdsize,caddr_t ctfdata,symit_data_t * si)906 resurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
907 caddr_t ctfdata, symit_data_t *si)
908 {
909 caddr_t buf = ctfdata + h->cth_objtoff;
910 size_t bufsz = h->cth_funcoff - h->cth_objtoff;
911 caddr_t dptr;
912
913 symit_reset(si);
914 for (dptr = buf; dptr < buf + bufsz; dptr += 2) {
915 void *v = (void *) dptr;
916 ushort_t id = *((ushort_t *)v);
917 iidesc_t *ii;
918 GElf_Sym *sym;
919
920 if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) {
921 parseterminate(
922 "Unexpected end of object symbols at %ju of %zu",
923 (intmax_t)(dptr - buf), bufsz);
924 }
925
926 if (id == 0) {
927 debug(3, "Skipping null object\n");
928 continue;
929 } else if (id >= tdsize) {
930 parseterminate("Reference to invalid type %d", id);
931 }
932
933 ii = iidesc_new(symit_name(si));
934 ii->ii_dtype = tdarr[id];
935 if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
936 ii->ii_type = II_SVAR;
937 ii->ii_owner = xstrdup(symit_curfile(si));
938 } else
939 ii->ii_type = II_GVAR;
940 hash_add(td->td_iihash, ii);
941
942 debug(3, "Resurrected %s object %s (%d) from %s\n",
943 (ii->ii_type == II_GVAR ? "global" : "static"),
944 ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)"));
945 }
946 }
947
948 static void
resurrect_functions(ctf_header_t * h,tdata_t * td,tdesc_t ** tdarr,int tdsize,caddr_t ctfdata,symit_data_t * si)949 resurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
950 caddr_t ctfdata, symit_data_t *si)
951 {
952 caddr_t buf = ctfdata + h->cth_funcoff;
953 size_t bufsz = h->cth_typeoff - h->cth_funcoff;
954 caddr_t dptr = buf;
955 iidesc_t *ii;
956 ushort_t info;
957 ushort_t retid;
958 GElf_Sym *sym;
959 int i;
960
961 symit_reset(si);
962 while (dptr < buf + bufsz) {
963 void *v = (void *) dptr;
964 info = *((ushort_t *)v);
965 dptr += 2;
966
967 if (!(sym = symit_next(si, STT_FUNC)) && info != 0)
968 parseterminate("Unexpected end of function symbols");
969
970 if (info == 0) {
971 debug(3, "Skipping null function (%s)\n",
972 symit_name(si));
973 continue;
974 }
975
976 v = (void *) dptr;
977 retid = *((ushort_t *)v);
978 dptr += 2;
979
980 if (retid >= tdsize)
981 parseterminate("Reference to invalid type %d", retid);
982
983 ii = iidesc_new(symit_name(si));
984 ii->ii_dtype = tdarr[retid];
985 if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
986 ii->ii_type = II_SFUN;
987 ii->ii_owner = xstrdup(symit_curfile(si));
988 } else
989 ii->ii_type = II_GFUN;
990 ii->ii_nargs = CTF_INFO_VLEN(info);
991 if (ii->ii_nargs)
992 ii->ii_args =
993 xmalloc(sizeof (tdesc_t *) * ii->ii_nargs);
994
995 for (i = 0; i < ii->ii_nargs; i++, dptr += 2) {
996 v = (void *) dptr;
997 ushort_t id = *((ushort_t *)v);
998 if (id >= tdsize)
999 parseterminate("Reference to invalid type %d",
1000 id);
1001 ii->ii_args[i] = tdarr[id];
1002 }
1003
1004 if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) {
1005 ii->ii_nargs--;
1006 ii->ii_vargs = 1;
1007 }
1008
1009 hash_add(td->td_iihash, ii);
1010
1011 debug(3, "Resurrected %s function %s (%d, %d args)\n",
1012 (ii->ii_type == II_GFUN ? "global" : "static"),
1013 ii->ii_name, retid, ii->ii_nargs);
1014 }
1015 }
1016
1017 static void
resurrect_types(ctf_header_t * h,tdata_t * td,tdesc_t ** tdarr,int tdsize,caddr_t ctfdata,int maxid)1018 resurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
1019 caddr_t ctfdata, int maxid)
1020 {
1021 caddr_t buf = ctfdata + h->cth_typeoff;
1022 size_t bufsz = h->cth_stroff - h->cth_typeoff;
1023 caddr_t sbuf = ctfdata + h->cth_stroff;
1024 caddr_t dptr = buf;
1025 tdesc_t *tdp;
1026 uint_t data;
1027 uint_t encoding;
1028 size_t size, increment;
1029 int tcnt;
1030 int iicnt = 0;
1031 tid_t tid, argid;
1032 int kind, vlen;
1033 int i;
1034
1035 elist_t **epp;
1036 mlist_t **mpp;
1037 intr_t *ip;
1038
1039 ctf_type_t *ctt;
1040 ctf_array_t *cta;
1041 ctf_enum_t *cte;
1042
1043 /*
1044 * A maxid of zero indicates a request to resurrect all types, so reset
1045 * maxid to the maximum type id.
1046 */
1047 if (maxid == 0)
1048 maxid = CTF_MAX_TYPE;
1049
1050 for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) {
1051 if (tid > maxid)
1052 break;
1053
1054 if (tid >= tdsize)
1055 parseterminate("Reference to invalid type %d", tid);
1056
1057 void *v = (void *) dptr;
1058 ctt = v;
1059
1060 get_ctt_size(ctt, &size, &increment);
1061 dptr += increment;
1062
1063 tdp = tdarr[tid];
1064
1065 if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0)
1066 parseterminate(
1067 "Unable to cope with non-zero strtab id");
1068 if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) {
1069 tdp->t_name =
1070 xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name));
1071 } else
1072 tdp->t_name = NULL;
1073
1074 kind = CTF_INFO_KIND(ctt->ctt_info);
1075 vlen = CTF_INFO_VLEN(ctt->ctt_info);
1076
1077 switch (kind) {
1078 case CTF_K_INTEGER:
1079 tdp->t_type = INTRINSIC;
1080 tdp->t_size = size;
1081
1082 v = (void *) dptr;
1083 data = *((uint_t *)v);
1084 dptr += sizeof (uint_t);
1085 encoding = CTF_INT_ENCODING(data);
1086
1087 ip = xmalloc(sizeof (intr_t));
1088 ip->intr_type = INTR_INT;
1089 ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0;
1090
1091 if (encoding & CTF_INT_CHAR)
1092 ip->intr_iformat = 'c';
1093 else if (encoding & CTF_INT_BOOL)
1094 ip->intr_iformat = 'b';
1095 else if (encoding & CTF_INT_VARARGS)
1096 ip->intr_iformat = 'v';
1097 else
1098 ip->intr_iformat = '\0';
1099
1100 ip->intr_offset = CTF_INT_OFFSET(data);
1101 ip->intr_nbits = CTF_INT_BITS(data);
1102 tdp->t_intr = ip;
1103 break;
1104
1105 case CTF_K_FLOAT:
1106 tdp->t_type = INTRINSIC;
1107 tdp->t_size = size;
1108
1109 v = (void *) dptr;
1110 data = *((uint_t *)v);
1111 dptr += sizeof (uint_t);
1112
1113 ip = xcalloc(sizeof (intr_t));
1114 ip->intr_type = INTR_REAL;
1115 ip->intr_fformat = CTF_FP_ENCODING(data);
1116 ip->intr_offset = CTF_FP_OFFSET(data);
1117 ip->intr_nbits = CTF_FP_BITS(data);
1118 tdp->t_intr = ip;
1119 break;
1120
1121 case CTF_K_POINTER:
1122 tdp->t_type = POINTER;
1123 tdp->t_tdesc = tdarr[ctt->ctt_type];
1124 break;
1125
1126 case CTF_K_ARRAY:
1127 tdp->t_type = ARRAY;
1128 tdp->t_size = size;
1129
1130 v = (void *) dptr;
1131 cta = v;
1132 dptr += sizeof (ctf_array_t);
1133
1134 tdp->t_ardef = xmalloc(sizeof (ardef_t));
1135 tdp->t_ardef->ad_contents = tdarr[cta->cta_contents];
1136 tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index];
1137 tdp->t_ardef->ad_nelems = cta->cta_nelems;
1138 break;
1139
1140 case CTF_K_STRUCT:
1141 case CTF_K_UNION:
1142 tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION);
1143 tdp->t_size = size;
1144
1145 if (size < CTF_LSTRUCT_THRESH) {
1146 for (i = 0, mpp = &tdp->t_members; i < vlen;
1147 i++, mpp = &((*mpp)->ml_next)) {
1148 v = (void *) dptr;
1149 ctf_member_t *ctm = v;
1150 dptr += sizeof (ctf_member_t);
1151
1152 *mpp = xmalloc(sizeof (mlist_t));
1153 (*mpp)->ml_name = xstrdup(sbuf +
1154 ctm->ctm_name);
1155 (*mpp)->ml_type = tdarr[ctm->ctm_type];
1156 (*mpp)->ml_offset = ctm->ctm_offset;
1157 (*mpp)->ml_size = 0;
1158 }
1159 } else {
1160 for (i = 0, mpp = &tdp->t_members; i < vlen;
1161 i++, mpp = &((*mpp)->ml_next)) {
1162 v = (void *) dptr;
1163 ctf_lmember_t *ctlm = v;
1164 dptr += sizeof (ctf_lmember_t);
1165
1166 *mpp = xmalloc(sizeof (mlist_t));
1167 (*mpp)->ml_name = xstrdup(sbuf +
1168 ctlm->ctlm_name);
1169 (*mpp)->ml_type =
1170 tdarr[ctlm->ctlm_type];
1171 (*mpp)->ml_offset =
1172 (int)CTF_LMEM_OFFSET(ctlm);
1173 (*mpp)->ml_size = 0;
1174 }
1175 }
1176
1177 *mpp = NULL;
1178 break;
1179
1180 case CTF_K_ENUM:
1181 tdp->t_type = ENUM;
1182 tdp->t_size = size;
1183
1184 for (i = 0, epp = &tdp->t_emem; i < vlen;
1185 i++, epp = &((*epp)->el_next)) {
1186 v = (void *) dptr;
1187 cte = v;
1188 dptr += sizeof (ctf_enum_t);
1189
1190 *epp = xmalloc(sizeof (elist_t));
1191 (*epp)->el_name = xstrdup(sbuf + cte->cte_name);
1192 (*epp)->el_number = cte->cte_value;
1193 }
1194 *epp = NULL;
1195 break;
1196
1197 case CTF_K_FORWARD:
1198 tdp->t_type = FORWARD;
1199 list_add(&td->td_fwdlist, tdp);
1200 break;
1201
1202 case CTF_K_TYPEDEF:
1203 tdp->t_type = TYPEDEF;
1204 tdp->t_tdesc = tdarr[ctt->ctt_type];
1205 break;
1206
1207 case CTF_K_VOLATILE:
1208 tdp->t_type = VOLATILE;
1209 tdp->t_tdesc = tdarr[ctt->ctt_type];
1210 break;
1211
1212 case CTF_K_CONST:
1213 tdp->t_type = CONST;
1214 tdp->t_tdesc = tdarr[ctt->ctt_type];
1215 break;
1216
1217 case CTF_K_FUNCTION:
1218 tdp->t_type = FUNCTION;
1219 tdp->t_fndef = xcalloc(sizeof (fndef_t));
1220 tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type];
1221
1222 v = (void *) (dptr + (sizeof (ushort_t) * (vlen - 1)));
1223 if (vlen > 0 && *(ushort_t *)v == 0)
1224 tdp->t_fndef->fn_vargs = 1;
1225
1226 tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs;
1227 tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) *
1228 vlen - tdp->t_fndef->fn_vargs);
1229
1230 for (i = 0; i < vlen; i++) {
1231 v = (void *) dptr;
1232 argid = *(ushort_t *)v;
1233 dptr += sizeof (ushort_t);
1234
1235 if (argid != 0)
1236 tdp->t_fndef->fn_args[i] = tdarr[argid];
1237 }
1238
1239 if (vlen & 1)
1240 dptr += sizeof (ushort_t);
1241 break;
1242
1243 case CTF_K_RESTRICT:
1244 tdp->t_type = RESTRICT;
1245 tdp->t_tdesc = tdarr[ctt->ctt_type];
1246 break;
1247
1248 case CTF_K_UNKNOWN:
1249 break;
1250
1251 default:
1252 warning("Can't parse unknown CTF type %d\n", kind);
1253 }
1254
1255 if (CTF_INFO_ISROOT(ctt->ctt_info)) {
1256 iidesc_t *ii = iidesc_new(tdp->t_name);
1257 if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1258 tdp->t_type == ENUM)
1259 ii->ii_type = II_SOU;
1260 else
1261 ii->ii_type = II_TYPE;
1262 ii->ii_dtype = tdp;
1263 hash_add(td->td_iihash, ii);
1264
1265 iicnt++;
1266 }
1267
1268 debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type,
1269 (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""),
1270 tdesc_name(tdp), tdp->t_id);
1271 }
1272
1273 debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt);
1274 }
1275
1276 /*
1277 * For lack of other inspiration, we're going to take the boring route. We
1278 * count the number of types. This lets us malloc that many tdesc structs
1279 * before we start filling them in. This has the advantage of allowing us to
1280 * avoid a merge-esque remap step.
1281 */
1282 static tdata_t *
ctf_parse(ctf_header_t * h,caddr_t buf,symit_data_t * si,char * label)1283 ctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label)
1284 {
1285 tdata_t *td = tdata_new();
1286 tdesc_t **tdarr;
1287 int ntypes = count_types(h, buf);
1288 int idx, i;
1289
1290 /* shudder */
1291 tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1));
1292 tdarr[0] = NULL;
1293 for (i = 1; i <= ntypes; i++) {
1294 tdarr[i] = xcalloc(sizeof (tdesc_t));
1295 tdarr[i]->t_id = i;
1296 }
1297
1298 td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel);
1299
1300 /* we have the technology - we can rebuild them */
1301 idx = resurrect_labels(h, td, buf, label);
1302
1303 resurrect_objects(h, td, tdarr, ntypes + 1, buf, si);
1304 resurrect_functions(h, td, tdarr, ntypes + 1, buf, si);
1305 resurrect_types(h, td, tdarr, ntypes + 1, buf, idx);
1306
1307 free(tdarr);
1308
1309 td->td_nextid = ntypes + 1;
1310
1311 return (td);
1312 }
1313
1314 static size_t
decompress_ctf(caddr_t cbuf,size_t cbufsz,caddr_t dbuf,size_t dbufsz)1315 decompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz)
1316 {
1317 z_stream zstr;
1318 int rc;
1319
1320 zstr.zalloc = (alloc_func)0;
1321 zstr.zfree = (free_func)0;
1322 zstr.opaque = (voidpf)0;
1323
1324 zstr.next_in = (Bytef *)cbuf;
1325 zstr.avail_in = cbufsz;
1326 zstr.next_out = (Bytef *)dbuf;
1327 zstr.avail_out = dbufsz;
1328
1329 if ((rc = inflateInit(&zstr)) != Z_OK ||
1330 (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END ||
1331 (rc = inflateEnd(&zstr)) != Z_OK) {
1332 warning("CTF decompress zlib error %s\n", zError(rc));
1333 return (0);
1334 }
1335
1336 debug(3, "reflated %lu bytes to %lu, pointer at 0x%jx\n",
1337 zstr.total_in, zstr.total_out,
1338 (intmax_t)((caddr_t)zstr.next_in - cbuf));
1339
1340 return (zstr.total_out);
1341 }
1342
1343 /*
1344 * Reconstruct the type tree from a given buffer of CTF data. Only the types
1345 * up to the type associated with the provided label, inclusive, will be
1346 * reconstructed. If a NULL label is provided, all types will be reconstructed.
1347 *
1348 * This function won't work on files that have been uniquified.
1349 */
1350 tdata_t *
ctf_load(char * file,caddr_t buf,size_t bufsz,symit_data_t * si,char * label)1351 ctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label)
1352 {
1353 ctf_header_t *h;
1354 caddr_t ctfdata;
1355 size_t ctfdatasz;
1356 tdata_t *td;
1357
1358 curfile = file;
1359
1360 if (bufsz < sizeof (ctf_header_t))
1361 parseterminate("Corrupt CTF - short header");
1362
1363 void *v = (void *) buf;
1364 h = v;
1365 buf += sizeof (ctf_header_t);
1366 bufsz -= sizeof (ctf_header_t);
1367
1368 if (h->cth_magic != CTF_MAGIC)
1369 parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic);
1370
1371 if (h->cth_version != CTF_VERSION)
1372 parseterminate("Unknown CTF version %d", h->cth_version);
1373
1374 ctfdatasz = h->cth_stroff + h->cth_strlen;
1375 if (h->cth_flags & CTF_F_COMPRESS) {
1376 size_t actual;
1377
1378 ctfdata = xmalloc(ctfdatasz);
1379 if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) !=
1380 ctfdatasz) {
1381 parseterminate("Corrupt CTF - short decompression "
1382 "(was %zu, expecting %zu)", actual, ctfdatasz);
1383 }
1384 } else {
1385 ctfdata = buf;
1386 ctfdatasz = bufsz;
1387 }
1388
1389 td = ctf_parse(h, ctfdata, si, label);
1390
1391 if (h->cth_flags & CTF_F_COMPRESS)
1392 free(ctfdata);
1393
1394 curfile = NULL;
1395
1396 return (td);
1397 }
1398