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