Print this page
12259 CTF shouldn't assume enum size
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/common/ctf/ctf_create.c
+++ new/usr/src/common/ctf/ctf_create.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) 2019, Joyent, Inc.
28 + * Copyright 2020 Joyent, Inc.
29 29 */
30 30
31 31 #include <sys/sysmacros.h>
32 32 #include <sys/param.h>
33 33 #include <sys/mman.h>
34 34 #include <ctf_impl.h>
35 35 #include <sys/debug.h>
36 36
37 37 /*
38 38 * This static string is used as the template for initially populating a
39 39 * dynamic container's string table. We always store \0 in the first byte,
40 40 * and we use the generic string "PARENT" to mark this container's parent
41 41 * if one is associated with the container using ctf_import().
42 42 */
43 43 static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT";
44 44
45 45 /*
46 46 * To create an empty CTF container, we just declare a zeroed header and call
47 47 * ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w
48 48 * and initialize the dynamic members. We set dtstrlen to 1 to reserve the
49 49 * first byte of the string table for a \0 byte, and we start assigning type
50 50 * IDs at 1 because type ID 0 is used as a sentinel.
51 51 */
52 52 ctf_file_t *
53 53 ctf_create(int *errp)
54 54 {
55 55 static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } };
56 56
57 57 const ulong_t hashlen = 128;
58 58 ctf_dtdef_t **hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *));
59 59 ctf_sect_t cts;
60 60 ctf_file_t *fp;
61 61
62 62 if (hash == NULL)
63 63 return (ctf_set_open_errno(errp, EAGAIN));
64 64
65 65 cts.cts_name = _CTF_SECTION;
66 66 cts.cts_type = SHT_PROGBITS;
67 67 cts.cts_flags = 0;
68 68 cts.cts_data = &hdr;
69 69 cts.cts_size = sizeof (hdr);
70 70 cts.cts_entsize = 1;
71 71 cts.cts_offset = 0;
72 72
73 73 if ((fp = ctf_bufopen(&cts, NULL, NULL, errp)) == NULL) {
74 74 ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *));
75 75 return (NULL);
76 76 }
77 77
78 78 fp->ctf_flags |= LCTF_RDWR;
79 79 fp->ctf_dthashlen = hashlen;
80 80 bzero(hash, hashlen * sizeof (ctf_dtdef_t *));
81 81 fp->ctf_dthash = hash;
82 82 fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE);
83 83 fp->ctf_dtnextid = 1;
84 84 fp->ctf_dtoldid = 0;
85 85
86 86 return (fp);
87 87 }
88 88
89 89 ctf_file_t *
90 90 ctf_fdcreate(int fd, int *errp)
91 91 {
92 92 ctf_file_t *fp;
93 93 static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } };
94 94
95 95 const ulong_t hashlen = 128;
96 96 ctf_dtdef_t **hash;
97 97 ctf_sect_t cts;
98 98
99 99 if (fd == -1)
100 100 return (ctf_create(errp));
101 101
102 102 hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *));
103 103
104 104 if (hash == NULL)
105 105 return (ctf_set_open_errno(errp, EAGAIN));
106 106
107 107 cts.cts_name = _CTF_SECTION;
108 108 cts.cts_type = SHT_PROGBITS;
109 109 cts.cts_flags = 0;
110 110 cts.cts_data = &hdr;
111 111 cts.cts_size = sizeof (hdr);
112 112 cts.cts_entsize = 1;
113 113 cts.cts_offset = 0;
114 114
115 115 if ((fp = ctf_fdcreate_int(fd, errp, &cts)) == NULL) {
116 116 ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *));
117 117 return (NULL);
118 118 }
119 119
120 120 fp->ctf_flags |= LCTF_RDWR;
121 121 fp->ctf_dthashlen = hashlen;
122 122 bzero(hash, hashlen * sizeof (ctf_dtdef_t *));
123 123 fp->ctf_dthash = hash;
124 124 fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE);
125 125 fp->ctf_dtnextid = 1;
126 126 fp->ctf_dtoldid = 0;
127 127
128 128 return (fp);
129 129 }
130 130
131 131 static uchar_t *
132 132 ctf_copy_smembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
133 133 {
134 134 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
135 135 ctf_member_t ctm;
136 136
137 137 for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
138 138 if (dmd->dmd_name) {
139 139 ctm.ctm_name = soff;
140 140 soff += strlen(dmd->dmd_name) + 1;
141 141 } else
142 142 ctm.ctm_name = 0;
143 143
144 144 ctm.ctm_type = (ushort_t)dmd->dmd_type;
145 145 ctm.ctm_offset = (ushort_t)dmd->dmd_offset;
146 146
147 147 bcopy(&ctm, t, sizeof (ctm));
148 148 t += sizeof (ctm);
149 149 }
150 150
151 151 return (t);
152 152 }
153 153
154 154 static uchar_t *
155 155 ctf_copy_lmembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
156 156 {
157 157 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
158 158 ctf_lmember_t ctlm;
159 159
160 160 for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
161 161 if (dmd->dmd_name) {
162 162 ctlm.ctlm_name = soff;
163 163 soff += strlen(dmd->dmd_name) + 1;
164 164 } else
165 165 ctlm.ctlm_name = 0;
166 166
167 167 ctlm.ctlm_type = (ushort_t)dmd->dmd_type;
168 168 ctlm.ctlm_pad = 0;
169 169 ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset);
170 170 ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset);
171 171
172 172 bcopy(&ctlm, t, sizeof (ctlm));
173 173 t += sizeof (ctlm);
174 174 }
175 175
176 176 return (t);
177 177 }
178 178
179 179 static uchar_t *
180 180 ctf_copy_emembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
181 181 {
182 182 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
183 183 ctf_enum_t cte;
184 184
185 185 for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
186 186 cte.cte_name = soff;
187 187 cte.cte_value = dmd->dmd_value;
188 188 soff += strlen(dmd->dmd_name) + 1;
189 189 bcopy(&cte, t, sizeof (cte));
190 190 t += sizeof (cte);
191 191 }
192 192
193 193 return (t);
194 194 }
195 195
196 196 static uchar_t *
197 197 ctf_copy_membnames(ctf_dtdef_t *dtd, uchar_t *s)
198 198 {
199 199 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
200 200 size_t len;
201 201
202 202 for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
203 203 if (dmd->dmd_name == NULL)
204 204 continue; /* skip anonymous members */
205 205 len = strlen(dmd->dmd_name) + 1;
206 206 bcopy(dmd->dmd_name, s, len);
207 207 s += len;
208 208 }
209 209
210 210 return (s);
211 211 }
212 212
213 213 /*
214 214 * Only types of dyanmic CTF containers contain reference counts. These
215 215 * containers are marked RD/WR. Because of that we basically make this a no-op
216 216 * for compatability with non-dynamic CTF sections. This is also a no-op for
217 217 * types which are not dynamic types. It is the responsibility of the caller to
218 218 * make sure it is a valid type. We help that caller out on debug builds.
219 219 *
220 220 * Note that the reference counts are not maintained for types that are not
221 221 * within this container. In other words if we have a type in a parent, that
222 222 * will not have its reference count increased. On the flip side, the parent
223 223 * will not be allowed to remove dynamic types if it has children.
224 224 */
225 225 static void
226 226 ctf_ref_inc(ctf_file_t *fp, ctf_id_t tid)
227 227 {
228 228 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
229 229
230 230 if (dtd == NULL)
231 231 return;
232 232
233 233 if (!(fp->ctf_flags & LCTF_RDWR))
234 234 return;
235 235
236 236 dtd->dtd_ref++;
237 237 }
238 238
239 239 /*
240 240 * Just as with ctf_ref_inc, this is a no-op on non-writeable containers and the
241 241 * caller should ensure that this is already a valid type.
242 242 */
243 243 static void
244 244 ctf_ref_dec(ctf_file_t *fp, ctf_id_t tid)
245 245 {
246 246 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
247 247
248 248 if (dtd == NULL)
249 249 return;
250 250
251 251 if (!(fp->ctf_flags & LCTF_RDWR))
252 252 return;
253 253
254 254 ASSERT(dtd->dtd_ref >= 1);
255 255 dtd->dtd_ref--;
256 256 }
257 257
258 258 /*
259 259 * If the specified CTF container is writable and has been modified, reload
260 260 * this container with the updated type definitions. In order to make this
261 261 * code and the rest of libctf as simple as possible, we perform updates by
262 262 * taking the dynamic type definitions and creating an in-memory CTF file
263 263 * containing the definitions, and then call ctf_bufopen() on it. This not
264 264 * only leverages ctf_bufopen(), but also avoids having to bifurcate the rest
265 265 * of the library code with different lookup paths for static and dynamic
266 266 * type definitions. We are therefore optimizing greatly for lookup over
267 267 * update, which we assume will be an uncommon operation. We perform one
268 268 * extra trick here for the benefit of callers and to keep our code simple:
269 269 * ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp
270 270 * constant for the caller, so after ctf_bufopen() returns, we use bcopy to
271 271 * swap the interior of the old and new ctf_file_t's, and then free the old.
272 272 *
273 273 * Note that the lists of dynamic types stays around and the resulting container
274 274 * is still writeable. Furthermore, the reference counts that are on the dtd's
275 275 * are still valid.
276 276 */
277 277 int
278 278 ctf_update(ctf_file_t *fp)
279 279 {
280 280 ctf_file_t ofp, *nfp;
281 281 ctf_header_t hdr, *bhdr;
282 282 ctf_dtdef_t *dtd;
283 283 ctf_dsdef_t *dsd;
284 284 ctf_dldef_t *dld;
285 285 ctf_sect_t cts, *symp, *strp;
286 286
287 287 uchar_t *s, *s0, *t;
288 288 ctf_lblent_t *label;
289 289 uint16_t *obj, *func;
290 290 size_t size, objsize, funcsize, labelsize, plen;
291 291 void *buf;
292 292 int err;
293 293 ulong_t i;
294 294 const char *plabel;
295 295 const char *sname;
296 296
297 297 uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
298 298 uintptr_t strbase = (uintptr_t)fp->ctf_strtab.cts_data;
299 299
300 300 if (!(fp->ctf_flags & LCTF_RDWR))
301 301 return (ctf_set_errno(fp, ECTF_RDONLY));
302 302
303 303 if (!(fp->ctf_flags & LCTF_DIRTY))
304 304 return (0); /* no update required */
305 305
306 306 /*
307 307 * Fill in an initial CTF header. We will leave the label, object,
308 308 * and function sections empty and only output a header, type section,
309 309 * and string table. The type section begins at a 4-byte aligned
310 310 * boundary past the CTF header itself (at relative offset zero).
311 311 */
312 312 bzero(&hdr, sizeof (hdr));
313 313 hdr.cth_magic = CTF_MAGIC;
314 314 hdr.cth_version = CTF_VERSION;
315 315
316 316 if (fp->ctf_flags & LCTF_CHILD) {
317 317 if (fp->ctf_parname == NULL) {
318 318 plen = 0;
319 319 hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */
320 320 plabel = NULL;
321 321 } else {
322 322 plen = strlen(fp->ctf_parname) + 1;
323 323 plabel = ctf_label_topmost(fp->ctf_parent);
324 324 }
325 325 } else {
326 326 plabel = NULL;
327 327 plen = 0;
328 328 }
329 329
330 330 /*
331 331 * Iterate over the labels that we have.
332 332 */
333 333 for (labelsize = 0, dld = ctf_list_next(&fp->ctf_dldefs);
334 334 dld != NULL; dld = ctf_list_next(dld))
335 335 labelsize += sizeof (ctf_lblent_t);
336 336
337 337 /*
338 338 * Iterate through the dynamic type definition list and compute the
339 339 * size of the CTF type section we will need to generate.
340 340 */
341 341 for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs);
342 342 dtd != NULL; dtd = ctf_list_next(dtd)) {
343 343
344 344 uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
345 345 uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
346 346
347 347 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
348 348 size += sizeof (ctf_stype_t);
349 349 else
350 350 size += sizeof (ctf_type_t);
351 351
352 352 switch (kind) {
353 353 case CTF_K_INTEGER:
354 354 case CTF_K_FLOAT:
355 355 size += sizeof (uint_t);
356 356 break;
357 357 case CTF_K_ARRAY:
358 358 size += sizeof (ctf_array_t);
359 359 break;
360 360 case CTF_K_FUNCTION:
361 361 size += sizeof (ushort_t) * (vlen + (vlen & 1));
362 362 break;
363 363 case CTF_K_STRUCT:
364 364 case CTF_K_UNION:
365 365 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
366 366 size += sizeof (ctf_member_t) * vlen;
367 367 else
368 368 size += sizeof (ctf_lmember_t) * vlen;
369 369 break;
370 370 case CTF_K_ENUM:
371 371 size += sizeof (ctf_enum_t) * vlen;
372 372 break;
373 373 }
374 374 }
375 375
376 376 /*
377 377 * An entry for each object must exist in the data section. However, if
378 378 * the symbol is SHN_UNDEF, then it is skipped. For objects, the storage
379 379 * is just the size of the 2-byte id. For functions it's always 2 bytes,
380 380 * plus 2 bytes per argument and the return type.
381 381 */
382 382 dsd = ctf_list_next(&fp->ctf_dsdefs);
383 383 for (objsize = 0, funcsize = 0, i = 0; i < fp->ctf_nsyms; i++) {
384 384 int type;
385 385
386 386 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
387 387 const Elf32_Sym *symp = (Elf32_Sym *)symbase + i;
388 388
389 389 type = ELF32_ST_TYPE(symp->st_info);
390 390 if (ctf_sym_valid(strbase, type, symp->st_shndx,
391 391 symp->st_value, symp->st_name) == B_FALSE)
392 392 continue;
393 393 } else {
394 394 const Elf64_Sym *symp = (Elf64_Sym *)symbase + i;
395 395
396 396 type = ELF64_ST_TYPE(symp->st_info);
397 397 if (ctf_sym_valid(strbase, type, symp->st_shndx,
398 398 symp->st_value, symp->st_name) == B_FALSE)
399 399 continue;
400 400 }
401 401
402 402 while (dsd != NULL && i > dsd->dsd_symidx)
403 403 dsd = ctf_list_next(dsd);
404 404 if (type == STT_OBJECT) {
405 405 objsize += sizeof (uint16_t);
406 406 } else {
407 407 /* Every function has a uint16_t info no matter what */
408 408 if (dsd == NULL || i < dsd->dsd_symidx) {
409 409 funcsize += sizeof (uint16_t);
410 410 } else {
411 411 funcsize += sizeof (uint16_t) *
412 412 (dsd->dsd_nargs + 2);
413 413 }
414 414 }
415 415 }
416 416
417 417 /*
418 418 * The objtoff and funcoffset must be 2-byte aligned. We're guaranteed
419 419 * that this is always true for the objtoff because labels are always 8
420 420 * bytes large. Similarly, because objects are always two bytes of data,
421 421 * this will always be true for funcoff.
422 422 */
423 423 hdr.cth_objtoff = hdr.cth_lbloff + labelsize;
424 424 hdr.cth_funcoff = hdr.cth_objtoff + objsize;
425 425
426 426 /*
427 427 * The type offset must be 4 byte aligned.
428 428 */
429 429 hdr.cth_typeoff = hdr.cth_funcoff + funcsize;
430 430 if (hdr.cth_typeoff & 3)
431 431 hdr.cth_typeoff += 4 - (hdr.cth_typeoff & 3);
432 432 ASSERT((hdr.cth_typeoff & 3) == 0);
433 433
434 434 /*
435 435 * Fill in the string table offset and size, compute the size of the
436 436 * entire CTF buffer we need, and then allocate a new buffer and
437 437 * bcopy the finished header to the start of the buffer.
438 438 */
439 439 hdr.cth_stroff = hdr.cth_typeoff + size;
440 440 hdr.cth_strlen = fp->ctf_dtstrlen + plen;
441 441 size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
442 442 ctf_dprintf("lbloff: %u\nobjtoff: %u\nfuncoff: %u\n"
443 443 "typeoff: %u\nstroff: %u\nstrlen: %u\n",
444 444 hdr.cth_lbloff, hdr.cth_objtoff, hdr.cth_funcoff,
445 445 hdr.cth_typeoff, hdr.cth_stroff, hdr.cth_strlen);
446 446
447 447 if ((buf = ctf_data_alloc(size)) == MAP_FAILED)
448 448 return (ctf_set_errno(fp, EAGAIN));
449 449
450 450 bcopy(&hdr, buf, sizeof (ctf_header_t));
451 451 bhdr = buf;
452 452 label = (ctf_lblent_t *)((uintptr_t)buf + sizeof (ctf_header_t));
453 453 t = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_typeoff;
454 454 s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff;
455 455 obj = (uint16_t *)((uintptr_t)buf + sizeof (ctf_header_t) +
456 456 hdr.cth_objtoff);
457 457 func = (uint16_t *)((uintptr_t)buf + sizeof (ctf_header_t) +
458 458 hdr.cth_funcoff);
459 459
460 460 bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE));
461 461 s += sizeof (_CTF_STRTAB_TEMPLATE);
462 462
463 463 /*
464 464 * We have an actual parent name and we're a child container, therefore
465 465 * we should make sure to note our parent's name here.
466 466 */
467 467 if (plen != 0) {
468 468 VERIFY(s + plen - s0 <= hdr.cth_strlen);
469 469 bcopy(fp->ctf_parname, s, plen);
470 470 bhdr->cth_parname = s - s0;
471 471 s += plen;
472 472 }
473 473
474 474 /*
475 475 * First pass over the labels and copy them out.
476 476 */
477 477 for (dld = ctf_list_next(&fp->ctf_dldefs); dld != NULL;
478 478 dld = ctf_list_next(dld), label++) {
479 479 size_t len = strlen(dld->dld_name) + 1;
480 480
481 481 VERIFY(s + len - s0 <= hdr.cth_strlen);
482 482 bcopy(dld->dld_name, s, len);
483 483 label->ctl_typeidx = dld->dld_type;
484 484 label->ctl_label = s - s0;
485 485 s += len;
486 486
487 487 if (plabel != NULL && strcmp(plabel, dld->dld_name) == 0)
488 488 bhdr->cth_parlabel = label->ctl_label;
489 489 }
490 490
491 491 /*
492 492 * We now take a final lap through the dynamic type definition list and
493 493 * copy the appropriate type records and strings to the output buffer.
494 494 */
495 495 for (dtd = ctf_list_next(&fp->ctf_dtdefs);
496 496 dtd != NULL; dtd = ctf_list_next(dtd)) {
497 497
498 498 uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
499 499 uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
500 500
501 501 ctf_array_t cta;
502 502 uint_t encoding;
503 503 size_t len;
504 504
505 505 if (dtd->dtd_name != NULL) {
506 506 dtd->dtd_data.ctt_name = (uint_t)(s - s0);
507 507 len = strlen(dtd->dtd_name) + 1;
508 508 VERIFY(s + len - s0 <= hdr.cth_strlen);
509 509 bcopy(dtd->dtd_name, s, len);
510 510 s += len;
511 511 } else
512 512 dtd->dtd_data.ctt_name = 0;
513 513
514 514 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
515 515 len = sizeof (ctf_stype_t);
516 516 else
517 517 len = sizeof (ctf_type_t);
518 518
519 519 bcopy(&dtd->dtd_data, t, len);
520 520 t += len;
521 521
522 522 switch (kind) {
523 523 case CTF_K_INTEGER:
524 524 case CTF_K_FLOAT:
525 525 if (kind == CTF_K_INTEGER) {
526 526 encoding = CTF_INT_DATA(
527 527 dtd->dtd_u.dtu_enc.cte_format,
528 528 dtd->dtd_u.dtu_enc.cte_offset,
529 529 dtd->dtd_u.dtu_enc.cte_bits);
530 530 } else {
531 531 encoding = CTF_FP_DATA(
532 532 dtd->dtd_u.dtu_enc.cte_format,
533 533 dtd->dtd_u.dtu_enc.cte_offset,
534 534 dtd->dtd_u.dtu_enc.cte_bits);
535 535 }
536 536 bcopy(&encoding, t, sizeof (encoding));
537 537 t += sizeof (encoding);
538 538 break;
539 539
540 540 case CTF_K_ARRAY:
541 541 cta.cta_contents = (ushort_t)
542 542 dtd->dtd_u.dtu_arr.ctr_contents;
543 543 cta.cta_index = (ushort_t)
544 544 dtd->dtd_u.dtu_arr.ctr_index;
545 545 cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
546 546 bcopy(&cta, t, sizeof (cta));
547 547 t += sizeof (cta);
548 548 break;
549 549
550 550 case CTF_K_FUNCTION: {
551 551 ushort_t *argv = (ushort_t *)(uintptr_t)t;
552 552 uint_t argc;
553 553
554 554 for (argc = 0; argc < vlen; argc++)
555 555 *argv++ = (ushort_t)dtd->dtd_u.dtu_argv[argc];
556 556
557 557 if (vlen & 1)
558 558 *argv++ = 0; /* pad to 4-byte boundary */
559 559
560 560 t = (uchar_t *)argv;
561 561 break;
562 562 }
563 563
564 564 case CTF_K_STRUCT:
565 565 case CTF_K_UNION:
566 566 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
567 567 t = ctf_copy_smembers(dtd, (uint_t)(s - s0), t);
568 568 else
569 569 t = ctf_copy_lmembers(dtd, (uint_t)(s - s0), t);
570 570 s = ctf_copy_membnames(dtd, s);
571 571 break;
572 572
573 573 case CTF_K_ENUM:
574 574 t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t);
575 575 s = ctf_copy_membnames(dtd, s);
576 576 break;
577 577 }
578 578 }
579 579
580 580 /*
581 581 * Now we fill in our dynamic data and function sections. We use the
582 582 * same criteria as above, but also consult the dsd list.
583 583 */
584 584 dsd = ctf_list_next(&fp->ctf_dsdefs);
585 585 for (i = 0; i < fp->ctf_nsyms; i++) {
586 586 int type;
587 587 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
588 588 const Elf32_Sym *symp = (Elf32_Sym *)symbase + i;
589 589 type = ELF32_ST_TYPE(symp->st_info);
590 590
591 591 if (ctf_sym_valid(strbase, type, symp->st_shndx,
592 592 symp->st_value, symp->st_name) == B_FALSE)
593 593 continue;
594 594 } else {
595 595 const Elf64_Sym *symp = (Elf64_Sym *)symbase + i;
596 596 type = ELF64_ST_TYPE(symp->st_info);
597 597 if (ctf_sym_valid(strbase, type, symp->st_shndx,
598 598 symp->st_value, symp->st_name) == B_FALSE)
599 599 continue;
600 600 }
601 601
602 602 while (dsd != NULL && i > dsd->dsd_symidx) {
603 603 dsd = ctf_list_next(dsd);
604 604 }
605 605 if (type == STT_OBJECT) {
606 606 if (dsd == NULL || i < dsd->dsd_symidx) {
607 607 *obj = 0;
608 608 } else {
609 609 *obj = dsd->dsd_tid;
610 610 }
611 611 obj++;
612 612 VERIFY((uintptr_t)obj <= (uintptr_t)func);
613 613 } else {
614 614 if (dsd == NULL || i < dsd->dsd_symidx) {
615 615 ushort_t data = CTF_TYPE_INFO(CTF_K_UNKNOWN,
616 616 0, 0);
617 617 *func = data;
618 618 func++;
619 619 } else {
620 620 int j;
621 621 ushort_t data = CTF_TYPE_INFO(CTF_K_FUNCTION, 0,
622 622 dsd->dsd_nargs);
623 623
624 624 *func = data;
625 625 func++;
626 626 *func = dsd->dsd_tid;
627 627 func++;
628 628 for (j = 0; j < dsd->dsd_nargs; j++)
629 629 func[j] = dsd->dsd_argc[j];
630 630 func += dsd->dsd_nargs;
631 631 }
632 632 }
633 633 }
634 634
635 635 /*
636 636 * Finally, we are ready to ctf_bufopen() the new container. If this
637 637 * is successful, we then switch nfp and fp and free the old container.
638 638 */
639 639 ctf_data_protect(buf, size);
640 640 cts.cts_name = _CTF_SECTION;
641 641 cts.cts_type = SHT_PROGBITS;
642 642 cts.cts_flags = 0;
643 643 cts.cts_data = buf;
644 644 cts.cts_size = size;
645 645 cts.cts_entsize = 1;
646 646 cts.cts_offset = 0;
647 647
648 648 if (fp->ctf_nsyms == 0) {
649 649 symp = NULL;
650 650 strp = NULL;
651 651 } else {
652 652 symp = &fp->ctf_symtab;
653 653 strp = &fp->ctf_strtab;
654 654 }
655 655
656 656 if ((nfp = ctf_bufopen(&cts, symp, strp, &err)) == NULL) {
657 657 ctf_data_free(buf, size);
658 658 return (ctf_set_errno(fp, err));
659 659 }
660 660
661 661 (void) ctf_setmodel(nfp, ctf_getmodel(fp));
662 662 (void) ctf_import(nfp, fp->ctf_parent);
663 663
664 664 nfp->ctf_refcnt = fp->ctf_refcnt;
665 665 nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
666 666 nfp->ctf_dthash = fp->ctf_dthash;
667 667 nfp->ctf_dthashlen = fp->ctf_dthashlen;
668 668 nfp->ctf_dtdefs = fp->ctf_dtdefs;
669 669 nfp->ctf_dsdefs = fp->ctf_dsdefs;
670 670 nfp->ctf_dldefs = fp->ctf_dldefs;
671 671 nfp->ctf_dtstrlen = fp->ctf_dtstrlen;
672 672 nfp->ctf_dtnextid = fp->ctf_dtnextid;
673 673 nfp->ctf_dtoldid = fp->ctf_dtnextid - 1;
674 674 nfp->ctf_specific = fp->ctf_specific;
675 675
676 676 fp->ctf_dthash = NULL;
677 677 fp->ctf_dthashlen = 0;
678 678 bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t));
679 679 bzero(&fp->ctf_dsdefs, sizeof (ctf_list_t));
680 680 bzero(&fp->ctf_dldefs, sizeof (ctf_list_t));
681 681
682 682 /*
683 683 * Because the various containers share the data sections, we don't want
684 684 * to have ctf_close free it all. However, the name of the section is in
685 685 * fact unique to the ctf_sect_t. Thus we save the names of the symbol
686 686 * and string sections around the bzero() and restore them afterwards,
687 687 * ensuring that we don't result in a memory leak.
688 688 */
689 689 sname = fp->ctf_symtab.cts_name;
690 690 bzero(&fp->ctf_symtab, sizeof (ctf_sect_t));
691 691 fp->ctf_symtab.cts_name = sname;
692 692
693 693 sname = fp->ctf_strtab.cts_name;
694 694 bzero(&fp->ctf_strtab, sizeof (ctf_sect_t));
695 695 fp->ctf_strtab.cts_name = sname;
696 696
697 697 bcopy(fp, &ofp, sizeof (ctf_file_t));
698 698 bcopy(nfp, fp, sizeof (ctf_file_t));
699 699 bcopy(&ofp, nfp, sizeof (ctf_file_t));
700 700
701 701 /*
702 702 * Initialize the ctf_lookup_by_name top-level dictionary. We keep an
703 703 * array of type name prefixes and the corresponding ctf_hash to use.
704 704 * NOTE: This code must be kept in sync with the code in ctf_bufopen().
705 705 */
706 706 fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
707 707 fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
708 708 fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
709 709 fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
710 710
711 711 nfp->ctf_refcnt = 1; /* force nfp to be freed */
712 712 ctf_close(nfp);
713 713
714 714 return (0);
715 715 }
716 716
717 717 void
718 718 ctf_dtd_insert(ctf_file_t *fp, ctf_dtdef_t *dtd)
719 719 {
720 720 ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
721 721
722 722 dtd->dtd_hash = fp->ctf_dthash[h];
723 723 fp->ctf_dthash[h] = dtd;
724 724 ctf_list_append(&fp->ctf_dtdefs, dtd);
725 725 }
726 726
727 727 void
728 728 ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd)
729 729 {
730 730 ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
731 731 ctf_dtdef_t *p, **q = &fp->ctf_dthash[h];
732 732 ctf_dmdef_t *dmd, *nmd;
733 733 size_t len;
734 734 int kind, i;
735 735
736 736 for (p = *q; p != NULL; p = p->dtd_hash) {
737 737 if (p != dtd)
738 738 q = &p->dtd_hash;
739 739 else
740 740 break;
741 741 }
742 742
743 743 if (p != NULL)
744 744 *q = p->dtd_hash;
745 745
746 746 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
747 747 switch (kind) {
748 748 case CTF_K_STRUCT:
749 749 case CTF_K_UNION:
750 750 case CTF_K_ENUM:
751 751 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
752 752 dmd != NULL; dmd = nmd) {
753 753 if (dmd->dmd_name != NULL) {
754 754 len = strlen(dmd->dmd_name) + 1;
755 755 ctf_free(dmd->dmd_name, len);
756 756 fp->ctf_dtstrlen -= len;
757 757 }
758 758 if (kind != CTF_K_ENUM)
759 759 ctf_ref_dec(fp, dmd->dmd_type);
760 760 nmd = ctf_list_next(dmd);
761 761 ctf_free(dmd, sizeof (ctf_dmdef_t));
762 762 }
763 763 break;
764 764 case CTF_K_FUNCTION:
765 765 ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
766 766 for (i = 0; i < CTF_INFO_VLEN(dtd->dtd_data.ctt_info); i++)
767 767 if (dtd->dtd_u.dtu_argv[i] != 0)
768 768 ctf_ref_dec(fp, dtd->dtd_u.dtu_argv[i]);
769 769 ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *
770 770 CTF_INFO_VLEN(dtd->dtd_data.ctt_info));
771 771 break;
772 772 case CTF_K_ARRAY:
773 773 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
774 774 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
775 775 break;
776 776 case CTF_K_TYPEDEF:
777 777 ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
778 778 break;
779 779 case CTF_K_POINTER:
780 780 case CTF_K_VOLATILE:
781 781 case CTF_K_CONST:
782 782 case CTF_K_RESTRICT:
783 783 ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
784 784 break;
785 785 }
786 786
787 787 if (dtd->dtd_name) {
788 788 len = strlen(dtd->dtd_name) + 1;
789 789 ctf_free(dtd->dtd_name, len);
790 790 fp->ctf_dtstrlen -= len;
791 791 }
792 792
793 793 ctf_list_delete(&fp->ctf_dtdefs, dtd);
794 794 ctf_free(dtd, sizeof (ctf_dtdef_t));
795 795 }
796 796
797 797 ctf_dtdef_t *
798 798 ctf_dtd_lookup(ctf_file_t *fp, ctf_id_t type)
799 799 {
800 800 ulong_t h = type & (fp->ctf_dthashlen - 1);
801 801 ctf_dtdef_t *dtd;
802 802
803 803 if (fp->ctf_dthash == NULL)
804 804 return (NULL);
805 805
806 806 for (dtd = fp->ctf_dthash[h]; dtd != NULL; dtd = dtd->dtd_hash) {
807 807 if (dtd->dtd_type == type)
808 808 break;
809 809 }
810 810
811 811 return (dtd);
812 812 }
813 813
814 814 ctf_dsdef_t *
815 815 ctf_dsd_lookup(ctf_file_t *fp, ulong_t idx)
816 816 {
817 817 ctf_dsdef_t *dsd;
818 818
819 819 for (dsd = ctf_list_next(&fp->ctf_dsdefs); dsd != NULL;
820 820 dsd = ctf_list_next(dsd)) {
821 821 if (dsd->dsd_symidx == idx)
822 822 return (dsd);
823 823 }
824 824
825 825 return (NULL);
826 826 }
827 827
828 828 /*
829 829 * We order the ctf_dsdef_t by symbol index to make things better for updates.
830 830 */
831 831 void
832 832 ctf_dsd_insert(ctf_file_t *fp, ctf_dsdef_t *dsd)
833 833 {
834 834 ctf_dsdef_t *i;
835 835
836 836 for (i = ctf_list_next(&fp->ctf_dsdefs); i != NULL;
837 837 i = ctf_list_next(i)) {
838 838 if (i->dsd_symidx > dsd->dsd_symidx)
839 839 break;
840 840 }
841 841
842 842 if (i == NULL) {
843 843 ctf_list_append(&fp->ctf_dsdefs, dsd);
844 844 return;
845 845 }
846 846
847 847 ctf_list_insert_before(&fp->ctf_dsdefs, i, dsd);
848 848 }
849 849
850 850 /* ARGSUSED */
851 851 void
852 852 ctf_dsd_delete(ctf_file_t *fp, ctf_dsdef_t *dsd)
853 853 {
854 854 if (dsd->dsd_nargs > 0)
855 855 ctf_free(dsd->dsd_argc,
856 856 sizeof (ctf_id_t) * dsd->dsd_nargs);
857 857 ctf_list_delete(&fp->ctf_dsdefs, dsd);
858 858 ctf_free(dsd, sizeof (ctf_dsdef_t));
859 859 }
860 860
861 861 ctf_dldef_t *
862 862 ctf_dld_lookup(ctf_file_t *fp, const char *name)
863 863 {
864 864 ctf_dldef_t *dld;
865 865
866 866 for (dld = ctf_list_next(&fp->ctf_dldefs); dld != NULL;
867 867 dld = ctf_list_next(dld)) {
868 868 if (strcmp(name, dld->dld_name) == 0)
869 869 return (dld);
870 870 }
871 871
872 872 return (NULL);
873 873 }
874 874
875 875 void
876 876 ctf_dld_insert(ctf_file_t *fp, ctf_dldef_t *dld, uint_t pos)
877 877 {
878 878 ctf_dldef_t *l;
879 879
880 880 if (pos == 0) {
881 881 ctf_list_prepend(&fp->ctf_dldefs, dld);
882 882 return;
883 883 }
884 884
885 885 for (l = ctf_list_next(&fp->ctf_dldefs); pos != 0 && dld != NULL;
886 886 l = ctf_list_next(l), pos--)
887 887 ;
888 888
889 889 if (l == NULL)
890 890 ctf_list_append(&fp->ctf_dldefs, dld);
891 891 else
892 892 ctf_list_insert_before(&fp->ctf_dsdefs, l, dld);
893 893 }
894 894
895 895 void
896 896 ctf_dld_delete(ctf_file_t *fp, ctf_dldef_t *dld)
897 897 {
898 898 ctf_list_delete(&fp->ctf_dldefs, dld);
899 899
900 900 if (dld->dld_name != NULL) {
901 901 size_t len = strlen(dld->dld_name) + 1;
902 902 ctf_free(dld->dld_name, len);
903 903 fp->ctf_dtstrlen -= len;
904 904 }
905 905
906 906 ctf_free(dld, sizeof (ctf_dldef_t));
907 907 }
908 908
909 909 /*
910 910 * Discard all of the dynamic type definitions that have been added to the
911 911 * container since the last call to ctf_update(). We locate such types by
912 912 * scanning the list and deleting elements that have type IDs greater than
913 913 * ctf_dtoldid, which is set by ctf_update(), above. Note that to work properly
914 914 * with our reference counting schemes, we must delete the dynamic list in
915 915 * reverse.
916 916 */
917 917 int
918 918 ctf_discard(ctf_file_t *fp)
919 919 {
920 920 ctf_dtdef_t *dtd, *ntd;
921 921
922 922 if (!(fp->ctf_flags & LCTF_RDWR))
923 923 return (ctf_set_errno(fp, ECTF_RDONLY));
924 924
925 925 if (!(fp->ctf_flags & LCTF_DIRTY))
926 926 return (0); /* no update required */
927 927
928 928 for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
929 929 ntd = ctf_list_prev(dtd);
930 930 if (dtd->dtd_type <= fp->ctf_dtoldid)
931 931 continue; /* skip types that have been committed */
932 932
933 933 ctf_dtd_delete(fp, dtd);
934 934 }
935 935
936 936 fp->ctf_dtnextid = fp->ctf_dtoldid + 1;
937 937 fp->ctf_flags &= ~LCTF_DIRTY;
938 938
939 939 return (0);
940 940 }
941 941
942 942 static ctf_id_t
943 943 ctf_add_generic(ctf_file_t *fp, uint_t flag, const char *name, ctf_dtdef_t **rp)
944 944 {
945 945 ctf_dtdef_t *dtd;
946 946 ctf_id_t type;
947 947 char *s = NULL;
948 948
949 949 if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
950 950 return (ctf_set_errno(fp, EINVAL));
951 951
952 952 if (!(fp->ctf_flags & LCTF_RDWR))
953 953 return (ctf_set_errno(fp, ECTF_RDONLY));
954 954
955 955 if (CTF_INDEX_TO_TYPE(fp->ctf_dtnextid, 1) > CTF_MAX_TYPE)
956 956 return (ctf_set_errno(fp, ECTF_FULL));
957 957
958 958 if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL)
959 959 return (ctf_set_errno(fp, EAGAIN));
960 960
961 961 if (name != NULL && (s = ctf_strdup(name)) == NULL) {
962 962 ctf_free(dtd, sizeof (ctf_dtdef_t));
963 963 return (ctf_set_errno(fp, EAGAIN));
964 964 }
965 965
966 966 type = fp->ctf_dtnextid++;
967 967 type = CTF_INDEX_TO_TYPE(type, (fp->ctf_flags & LCTF_CHILD));
968 968
969 969 bzero(dtd, sizeof (ctf_dtdef_t));
970 970 dtd->dtd_name = s;
971 971 dtd->dtd_type = type;
972 972
973 973 if (s != NULL)
974 974 fp->ctf_dtstrlen += strlen(s) + 1;
975 975
976 976 ctf_dtd_insert(fp, dtd);
977 977 fp->ctf_flags |= LCTF_DIRTY;
978 978
979 979 *rp = dtd;
980 980 return (type);
981 981 }
982 982
983 983 ctf_id_t
984 984 ctf_add_encoded(ctf_file_t *fp, uint_t flag,
985 985 const char *name, const ctf_encoding_t *ep, uint_t kind)
986 986 {
987 987 ctf_dtdef_t *dtd;
988 988 ctf_id_t type;
989 989
990 990 if (ep == NULL)
991 991 return (ctf_set_errno(fp, EINVAL));
992 992
993 993 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
994 994 return (CTF_ERR); /* errno is set for us */
995 995
996 996 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
997 997
998 998 /*
999 999 * If the type's size is not an even number of bytes, then we should
1000 1000 * round up the type size to the nearest byte.
1001 1001 */
1002 1002 dtd->dtd_data.ctt_size = ep->cte_bits / NBBY;
1003 1003 if ((ep->cte_bits % NBBY) != 0)
1004 1004 dtd->dtd_data.ctt_size++;
1005 1005 dtd->dtd_u.dtu_enc = *ep;
1006 1006
1007 1007 return (type);
1008 1008 }
1009 1009
1010 1010 ctf_id_t
1011 1011 ctf_add_reftype(ctf_file_t *fp, uint_t flag,
1012 1012 const char *name, ctf_id_t ref, uint_t kind)
1013 1013 {
1014 1014 ctf_dtdef_t *dtd;
1015 1015 ctf_id_t type;
1016 1016
1017 1017 if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE)
1018 1018 return (ctf_set_errno(fp, EINVAL));
1019 1019
1020 1020 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1021 1021 return (CTF_ERR); /* errno is set for us */
1022 1022
1023 1023 ctf_ref_inc(fp, ref);
1024 1024
1025 1025 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
1026 1026 dtd->dtd_data.ctt_type = (ushort_t)ref;
1027 1027
1028 1028 return (type);
1029 1029 }
1030 1030
1031 1031 ctf_id_t
1032 1032 ctf_add_integer(ctf_file_t *fp, uint_t flag,
1033 1033 const char *name, const ctf_encoding_t *ep)
1034 1034 {
1035 1035 return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER));
1036 1036 }
1037 1037
1038 1038 ctf_id_t
1039 1039 ctf_add_float(ctf_file_t *fp, uint_t flag,
1040 1040 const char *name, const ctf_encoding_t *ep)
1041 1041 {
1042 1042 return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT));
1043 1043 }
1044 1044
1045 1045 ctf_id_t
1046 1046 ctf_add_pointer(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1047 1047 {
1048 1048 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_POINTER));
1049 1049 }
1050 1050
1051 1051 ctf_id_t
1052 1052 ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp)
1053 1053 {
1054 1054 ctf_dtdef_t *dtd;
1055 1055 ctf_id_t type;
1056 1056 ctf_file_t *fpd;
1057 1057
1058 1058 if (arp == NULL)
1059 1059 return (ctf_set_errno(fp, EINVAL));
1060 1060
1061 1061 fpd = fp;
1062 1062 if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
1063 1063 ctf_dtd_lookup(fp, arp->ctr_contents) == NULL) {
1064 1064 ctf_dprintf("bad contents for array: %ld\n",
1065 1065 arp->ctr_contents);
1066 1066 return (ctf_set_errno(fp, ECTF_BADID));
1067 1067 }
1068 1068
1069 1069 fpd = fp;
1070 1070 if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
1071 1071 ctf_dtd_lookup(fp, arp->ctr_index) == NULL) {
1072 1072 ctf_dprintf("bad index for array: %ld\n", arp->ctr_index);
1073 1073 return (ctf_set_errno(fp, ECTF_BADID));
1074 1074 }
1075 1075
1076 1076 if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)
1077 1077 return (CTF_ERR); /* errno is set for us */
1078 1078
1079 1079 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, flag, 0);
1080 1080 dtd->dtd_data.ctt_size = 0;
1081 1081 dtd->dtd_u.dtu_arr = *arp;
1082 1082 ctf_ref_inc(fp, arp->ctr_contents);
1083 1083 ctf_ref_inc(fp, arp->ctr_index);
1084 1084
1085 1085 return (type);
1086 1086 }
1087 1087
1088 1088 int
1089 1089 ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
1090 1090 {
1091 1091 ctf_file_t *fpd;
1092 1092 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
1093 1093
1094 1094 if (!(fp->ctf_flags & LCTF_RDWR))
1095 1095 return (ctf_set_errno(fp, ECTF_RDONLY));
1096 1096
1097 1097 if (dtd == NULL || CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
1098 1098 return (ctf_set_errno(fp, ECTF_BADID));
1099 1099
1100 1100 fpd = fp;
1101 1101 if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
1102 1102 ctf_dtd_lookup(fp, arp->ctr_contents) == NULL)
1103 1103 return (ctf_set_errno(fp, ECTF_BADID));
1104 1104
1105 1105 fpd = fp;
1106 1106 if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
1107 1107 ctf_dtd_lookup(fp, arp->ctr_index) == NULL)
1108 1108 return (ctf_set_errno(fp, ECTF_BADID));
1109 1109
1110 1110 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
1111 1111 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
1112 1112 fp->ctf_flags |= LCTF_DIRTY;
1113 1113 dtd->dtd_u.dtu_arr = *arp;
1114 1114 ctf_ref_inc(fp, arp->ctr_contents);
1115 1115 ctf_ref_inc(fp, arp->ctr_index);
1116 1116
1117 1117 return (0);
1118 1118 }
1119 1119
1120 1120 ctf_id_t
1121 1121 ctf_add_funcptr(ctf_file_t *fp, uint_t flag,
1122 1122 const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
1123 1123 {
1124 1124 ctf_dtdef_t *dtd;
1125 1125 ctf_id_t type;
1126 1126 uint_t vlen;
1127 1127 int i;
1128 1128 ctf_id_t *vdat = NULL;
1129 1129 ctf_file_t *fpd;
1130 1130
1131 1131 if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 ||
1132 1132 (ctc->ctc_argc != 0 && argv == NULL))
1133 1133 return (ctf_set_errno(fp, EINVAL));
1134 1134
1135 1135 vlen = ctc->ctc_argc;
1136 1136 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1137 1137 vlen++; /* add trailing zero to indicate varargs (see below) */
1138 1138
1139 1139 if (vlen > CTF_MAX_VLEN)
1140 1140 return (ctf_set_errno(fp, EOVERFLOW));
1141 1141
1142 1142 fpd = fp;
1143 1143 if (ctf_lookup_by_id(&fpd, ctc->ctc_return) == NULL &&
1144 1144 ctf_dtd_lookup(fp, ctc->ctc_return) == NULL)
1145 1145 return (ctf_set_errno(fp, ECTF_BADID));
1146 1146
1147 1147 for (i = 0; i < ctc->ctc_argc; i++) {
1148 1148 fpd = fp;
1149 1149 if (ctf_lookup_by_id(&fpd, argv[i]) == NULL &&
1150 1150 ctf_dtd_lookup(fp, argv[i]) == NULL)
1151 1151 return (ctf_set_errno(fp, ECTF_BADID));
1152 1152 }
1153 1153
1154 1154 if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL)
1155 1155 return (ctf_set_errno(fp, EAGAIN));
1156 1156
1157 1157 if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) {
1158 1158 ctf_free(vdat, sizeof (ctf_id_t) * vlen);
1159 1159 return (CTF_ERR); /* errno is set for us */
1160 1160 }
1161 1161
1162 1162 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, flag, vlen);
1163 1163 dtd->dtd_data.ctt_type = (ushort_t)ctc->ctc_return;
1164 1164
1165 1165 ctf_ref_inc(fp, ctc->ctc_return);
1166 1166 for (i = 0; i < ctc->ctc_argc; i++)
1167 1167 ctf_ref_inc(fp, argv[i]);
1168 1168
1169 1169 bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc);
1170 1170 if (ctc->ctc_flags & CTF_FUNC_VARARG)
1171 1171 vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */
1172 1172 dtd->dtd_u.dtu_argv = vdat;
1173 1173
1174 1174 return (type);
1175 1175 }
1176 1176
1177 1177 ctf_id_t
1178 1178 ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name)
1179 1179 {
1180 1180 ctf_hash_t *hp = &fp->ctf_structs;
1181 1181 ctf_helem_t *hep = NULL;
1182 1182 ctf_dtdef_t *dtd = NULL;
1183 1183 ctf_id_t type = CTF_ERR;
1184 1184
1185 1185 if (name != NULL)
1186 1186 hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1187 1187
1188 1188 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1189 1189 type = hep->h_type;
1190 1190 dtd = ctf_dtd_lookup(fp, type);
1191 1191 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1192 1192 dtd = NULL;
1193 1193 }
1194 1194
1195 1195 if (dtd == NULL) {
1196 1196 type = ctf_add_generic(fp, flag, name, &dtd);
1197 1197 if (type == CTF_ERR)
1198 1198 return (CTF_ERR); /* errno is set for us */
1199 1199 }
1200 1200
1201 1201 VERIFY(type != CTF_ERR);
1202 1202 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, flag, 0);
1203 1203 dtd->dtd_data.ctt_size = 0;
1204 1204
1205 1205 /*
1206 1206 * Always dirty in case we modified a forward.
1207 1207 */
1208 1208 fp->ctf_flags |= LCTF_DIRTY;
1209 1209
1210 1210 return (type);
1211 1211 }
1212 1212
1213 1213 ctf_id_t
1214 1214 ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name)
1215 1215 {
1216 1216 ctf_hash_t *hp = &fp->ctf_unions;
1217 1217 ctf_helem_t *hep = NULL;
1218 1218 ctf_dtdef_t *dtd = NULL;
1219 1219 ctf_id_t type = CTF_ERR;
1220 1220
1221 1221 if (name != NULL)
1222 1222 hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1223 1223
1224 1224 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1225 1225 type = hep->h_type;
1226 1226 dtd = ctf_dtd_lookup(fp, type);
1227 1227 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1228 1228 dtd = NULL;
1229 1229 }
1230 1230
1231 1231 if (dtd == NULL) {
1232 1232 type = ctf_add_generic(fp, flag, name, &dtd);
1233 1233 if (type == CTF_ERR)
1234 1234 return (CTF_ERR); /* errno is set for us */
1235 1235 }
1236 1236
1237 1237 VERIFY(type != CTF_ERR);
1238 1238 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, flag, 0);
↓ open down ↓ |
1200 lines elided |
↑ open up ↑ |
1239 1239 dtd->dtd_data.ctt_size = 0;
1240 1240
1241 1241 /*
1242 1242 * Always dirty in case we modified a forward.
1243 1243 */
1244 1244 fp->ctf_flags |= LCTF_DIRTY;
1245 1245
1246 1246 return (type);
1247 1247 }
1248 1248
1249 +/*
1250 + * If size is 0, we use the standard integer size. This is almost always the
1251 + * case, except for packed enums.
1252 + */
1249 1253 ctf_id_t
1250 -ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name)
1254 +ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name, size_t size)
1251 1255 {
1252 1256 ctf_hash_t *hp = &fp->ctf_enums;
1253 1257 ctf_helem_t *hep = NULL;
1254 1258 ctf_dtdef_t *dtd = NULL;
1255 1259 ctf_id_t type = CTF_ERR;
1256 1260
1257 1261 if (name != NULL)
1258 1262 hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1259 1263
1260 1264 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1261 1265 type = hep->h_type;
1262 1266 dtd = ctf_dtd_lookup(fp, type);
1263 1267 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1264 1268 dtd = NULL;
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
1265 1269 }
1266 1270
1267 1271 if (dtd == NULL) {
1268 1272 type = ctf_add_generic(fp, flag, name, &dtd);
1269 1273 if (type == CTF_ERR)
1270 1274 return (CTF_ERR); /* errno is set for us */
1271 1275 }
1272 1276
1273 1277 VERIFY(type != CTF_ERR);
1274 1278 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0);
1275 - dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
1276 1279
1280 + if (size == 0) {
1281 + dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
1282 + } else {
1283 + if (size > CTF_MAX_SIZE) {
1284 + dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1285 + dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
1286 + dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
1287 + } else
1288 + dtd->dtd_data.ctt_size = size;
1289 + }
1290 +
1277 1291 /*
1278 1292 * Always dirty in case we modified a forward.
1279 1293 */
1280 1294 fp->ctf_flags |= LCTF_DIRTY;
1281 1295
1282 1296 return (type);
1283 1297 }
1284 1298
1285 1299 ctf_id_t
1286 1300 ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind)
1287 1301 {
1288 1302 ctf_hash_t *hp;
1289 1303 ctf_helem_t *hep;
1290 1304 ctf_dtdef_t *dtd;
1291 1305 ctf_id_t type;
1292 1306
1293 1307 switch (kind) {
1294 1308 case CTF_K_STRUCT:
1295 1309 hp = &fp->ctf_structs;
1296 1310 break;
1297 1311 case CTF_K_UNION:
1298 1312 hp = &fp->ctf_unions;
1299 1313 break;
1300 1314 case CTF_K_ENUM:
1301 1315 hp = &fp->ctf_enums;
1302 1316 break;
1303 1317 default:
1304 1318 return (ctf_set_errno(fp, ECTF_NOTSUE));
1305 1319 }
1306 1320
1307 1321 /*
1308 1322 * If the type is already defined or exists as a forward tag, just
1309 1323 * return the ctf_id_t of the existing definition.
1310 1324 */
1311 1325 if (name != NULL && (hep = ctf_hash_lookup(hp,
1312 1326 fp, name, strlen(name))) != NULL)
1313 1327 return (hep->h_type);
1314 1328
1315 1329 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1316 1330 return (CTF_ERR); /* errno is set for us */
1317 1331
1318 1332 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0);
1319 1333 dtd->dtd_data.ctt_type = kind;
1320 1334
1321 1335 return (type);
1322 1336 }
1323 1337
1324 1338 ctf_id_t
1325 1339 ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1326 1340 {
1327 1341 ctf_dtdef_t *dtd;
1328 1342 ctf_id_t type;
1329 1343 ctf_file_t *fpd;
1330 1344
1331 1345 fpd = fp;
1332 1346 if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL &&
1333 1347 ctf_dtd_lookup(fp, ref) == NULL))
1334 1348 return (ctf_set_errno(fp, EINVAL));
1335 1349
1336 1350 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1337 1351 return (CTF_ERR); /* errno is set for us */
1338 1352
1339 1353 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0);
1340 1354 dtd->dtd_data.ctt_type = (ushort_t)ref;
1341 1355 ctf_ref_inc(fp, ref);
1342 1356
1343 1357 return (type);
1344 1358 }
1345 1359
1346 1360 ctf_id_t
1347 1361 ctf_add_volatile(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1348 1362 {
1349 1363 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_VOLATILE));
1350 1364 }
1351 1365
1352 1366 ctf_id_t
1353 1367 ctf_add_const(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1354 1368 {
1355 1369 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_CONST));
1356 1370 }
1357 1371
1358 1372 ctf_id_t
1359 1373 ctf_add_restrict(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1360 1374 {
1361 1375 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_RESTRICT));
1362 1376 }
1363 1377
1364 1378 int
1365 1379 ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)
1366 1380 {
1367 1381 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);
1368 1382 ctf_dmdef_t *dmd;
1369 1383
1370 1384 uint_t kind, vlen, root;
1371 1385 char *s;
1372 1386
1373 1387 if (name == NULL)
1374 1388 return (ctf_set_errno(fp, EINVAL));
1375 1389
1376 1390 if (!(fp->ctf_flags & LCTF_RDWR))
1377 1391 return (ctf_set_errno(fp, ECTF_RDONLY));
1378 1392
1379 1393 if (dtd == NULL)
1380 1394 return (ctf_set_errno(fp, ECTF_BADID));
1381 1395
1382 1396 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1383 1397 root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
1384 1398 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
1385 1399
1386 1400 if (kind != CTF_K_ENUM)
1387 1401 return (ctf_set_errno(fp, ECTF_NOTENUM));
1388 1402
1389 1403 if (vlen == CTF_MAX_VLEN)
1390 1404 return (ctf_set_errno(fp, ECTF_DTFULL));
1391 1405
1392 1406 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1393 1407 dmd != NULL; dmd = ctf_list_next(dmd)) {
1394 1408 if (strcmp(dmd->dmd_name, name) == 0) {
1395 1409 ctf_dprintf("encountered duplicate member %s\n", name);
1396 1410 return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1397 1411 }
1398 1412 }
1399 1413
1400 1414 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1401 1415 return (ctf_set_errno(fp, EAGAIN));
1402 1416
1403 1417 if ((s = ctf_strdup(name)) == NULL) {
1404 1418 ctf_free(dmd, sizeof (ctf_dmdef_t));
1405 1419 return (ctf_set_errno(fp, EAGAIN));
1406 1420 }
1407 1421
1408 1422 dmd->dmd_name = s;
1409 1423 dmd->dmd_type = CTF_ERR;
1410 1424 dmd->dmd_offset = 0;
1411 1425 dmd->dmd_value = value;
1412 1426
1413 1427 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
1414 1428 ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1415 1429
1416 1430 fp->ctf_dtstrlen += strlen(s) + 1;
1417 1431 fp->ctf_flags |= LCTF_DIRTY;
1418 1432
1419 1433 return (0);
1420 1434 }
1421 1435
1422 1436 int
1423 1437 ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type,
1424 1438 ulong_t offset)
1425 1439 {
1426 1440 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);
1427 1441 ctf_dmdef_t *dmd;
1428 1442
1429 1443 ulong_t mbitsz;
1430 1444 ssize_t msize, malign, ssize;
1431 1445 uint_t kind, vlen, root;
1432 1446 int mkind;
1433 1447 char *s = NULL;
1434 1448
1435 1449 if (!(fp->ctf_flags & LCTF_RDWR))
1436 1450 return (ctf_set_errno(fp, ECTF_RDONLY));
1437 1451
1438 1452 if (dtd == NULL)
1439 1453 return (ctf_set_errno(fp, ECTF_BADID));
1440 1454
1441 1455 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1442 1456 root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
1443 1457 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
1444 1458
1445 1459 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
1446 1460 return (ctf_set_errno(fp, ECTF_NOTSOU));
1447 1461
1448 1462 if (vlen == CTF_MAX_VLEN)
1449 1463 return (ctf_set_errno(fp, ECTF_DTFULL));
1450 1464
1451 1465 /*
1452 1466 * Structures may have members which are anonymous. If they have two of
1453 1467 * these, then the duplicate member detection would find it due to the
1454 1468 * string of "", so we skip it.
1455 1469 */
1456 1470 if (name != NULL && *name != '\0') {
1457 1471 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1458 1472 dmd != NULL; dmd = ctf_list_next(dmd)) {
1459 1473 if (dmd->dmd_name != NULL &&
1460 1474 strcmp(dmd->dmd_name, name) == 0) {
1461 1475 return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1462 1476 }
1463 1477 }
1464 1478 }
1465 1479
1466 1480 if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||
1467 1481 (malign = ctf_type_align(fp, type)) == CTF_ERR ||
1468 1482 (mkind = ctf_type_kind(fp, type)) == CTF_ERR)
1469 1483 return (CTF_ERR); /* errno is set for us */
1470 1484
1471 1485 /*
1472 1486 * ctf_type_size returns sizes in bytes. However, for bitfields, that
1473 1487 * means that it may misrepresent and actually rounds it up to a power
1474 1488 * of two and store that in bytes. So instead we have to get the
1475 1489 * Integers encoding and rely on that.
1476 1490 */
1477 1491 if (mkind == CTF_K_INTEGER) {
1478 1492 ctf_encoding_t e;
1479 1493
1480 1494 if (ctf_type_encoding(fp, type, &e) == CTF_ERR)
1481 1495 return (CTF_ERR); /* errno is set for us */
1482 1496 mbitsz = e.cte_bits;
1483 1497 } else if (mkind == CTF_K_FORWARD) {
1484 1498 /*
1485 1499 * This is a rather rare case. In general one cannot add a
1486 1500 * forward to a structure. However, the CTF tools traditionally
1487 1501 * tried to add a forward to the struct cpu as the last member.
1488 1502 * Therefore, if we find one here, we're going to verify the
1489 1503 * size and make sure it's zero. It's certainly odd, but that's
1490 1504 * life.
1491 1505 *
1492 1506 * Further, if it's not an absolute position being specified,
1493 1507 * then we refuse to add it.
1494 1508 */
1495 1509 if (offset == ULONG_MAX)
1496 1510 return (ctf_set_errno(fp, EINVAL));
1497 1511 VERIFY(msize == 0);
1498 1512 mbitsz = msize;
1499 1513 } else {
1500 1514 mbitsz = msize * 8;
1501 1515 }
1502 1516
1503 1517 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1504 1518 return (ctf_set_errno(fp, EAGAIN));
1505 1519
1506 1520 if (name != NULL && (s = ctf_strdup(name)) == NULL) {
1507 1521 ctf_free(dmd, sizeof (ctf_dmdef_t));
1508 1522 return (ctf_set_errno(fp, EAGAIN));
1509 1523 }
1510 1524
1511 1525 dmd->dmd_name = s;
1512 1526 dmd->dmd_type = type;
1513 1527 dmd->dmd_value = -1;
1514 1528
1515 1529 if (kind == CTF_K_STRUCT && vlen != 0) {
1516 1530 ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);
1517 1531 ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);
1518 1532 size_t off;
1519 1533
1520 1534 if (offset == ULONG_MAX) {
1521 1535 ctf_encoding_t linfo;
1522 1536 ssize_t lsize;
1523 1537
1524 1538 off = lmd->dmd_offset;
1525 1539 if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)
1526 1540 off += linfo.cte_bits;
1527 1541 else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)
1528 1542 off += lsize * NBBY;
1529 1543
1530 1544 /*
1531 1545 * Round up the offset of the end of the last member to
1532 1546 * the next byte boundary, convert 'off' to bytes, and
1533 1547 * then round it up again to the next multiple of the
1534 1548 * alignment required by the new member. Finally,
1535 1549 * convert back to bits and store the result in
1536 1550 * dmd_offset. Technically we could do more efficient
1537 1551 * packing if the new member is a bit-field, but we're
1538 1552 * the "compiler" and ANSI says we can do as we choose.
1539 1553 */
1540 1554 off = roundup(off, NBBY) / NBBY;
1541 1555 off = roundup(off, MAX(malign, 1));
1542 1556 dmd->dmd_offset = off * NBBY;
1543 1557 ssize = off + msize;
1544 1558 } else {
1545 1559 dmd->dmd_offset = offset;
1546 1560 ssize = (offset + mbitsz) / NBBY;
1547 1561 }
1548 1562 } else {
1549 1563 dmd->dmd_offset = 0;
1550 1564 ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
1551 1565 ssize = MAX(ssize, msize);
1552 1566 }
1553 1567
1554 1568 if (ssize > CTF_MAX_SIZE) {
1555 1569 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1556 1570 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);
1557 1571 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);
1558 1572 } else
1559 1573 dtd->dtd_data.ctt_size = (ushort_t)ssize;
1560 1574
1561 1575 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
1562 1576 ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1563 1577
1564 1578 if (s != NULL)
1565 1579 fp->ctf_dtstrlen += strlen(s) + 1;
1566 1580
1567 1581 ctf_ref_inc(fp, type);
1568 1582 fp->ctf_flags |= LCTF_DIRTY;
1569 1583 return (0);
1570 1584 }
1571 1585
1572 1586 /*
1573 1587 * This removes a type from the dynamic section. This will fail if the type is
1574 1588 * referenced by another type. Note that the CTF ID is never reused currently by
1575 1589 * CTF. Note that if this container is a parent container then we just outright
1576 1590 * refuse to remove the type. There currently is no notion of searching for the
1577 1591 * ctf_dtdef_t in parent containers. If there is, then this constraint could
1578 1592 * become finer grained.
1579 1593 */
1580 1594 int
1581 1595 ctf_delete_type(ctf_file_t *fp, ctf_id_t type)
1582 1596 {
1583 1597 ctf_file_t *fpd;
1584 1598 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
1585 1599
1586 1600 if (!(fp->ctf_flags & LCTF_RDWR))
1587 1601 return (ctf_set_errno(fp, ECTF_RDONLY));
1588 1602
1589 1603 /*
1590 1604 * We want to give as useful an errno as possible. That means that we
1591 1605 * want to distinguish between a type which does not exist and one for
1592 1606 * which the type is not dynamic.
1593 1607 */
1594 1608 fpd = fp;
1595 1609 if (ctf_lookup_by_id(&fpd, type) == NULL &&
1596 1610 ctf_dtd_lookup(fp, type) == NULL)
1597 1611 return (CTF_ERR); /* errno is set for us */
1598 1612
1599 1613 if (dtd == NULL)
1600 1614 return (ctf_set_errno(fp, ECTF_NOTDYN));
1601 1615
1602 1616 if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1)
1603 1617 return (ctf_set_errno(fp, ECTF_REFERENCED));
1604 1618
1605 1619 ctf_dtd_delete(fp, dtd);
1606 1620 fp->ctf_flags |= LCTF_DIRTY;
1607 1621 return (0);
1608 1622 }
1609 1623
1610 1624 static int
1611 1625 enumcmp(const char *name, int value, void *arg)
1612 1626 {
1613 1627 ctf_bundle_t *ctb = arg;
1614 1628 int bvalue;
1615 1629
1616 1630 return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type,
1617 1631 name, &bvalue) == CTF_ERR || value != bvalue);
1618 1632 }
1619 1633
1620 1634 static int
1621 1635 enumadd(const char *name, int value, void *arg)
1622 1636 {
1623 1637 ctf_bundle_t *ctb = arg;
1624 1638
1625 1639 return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type,
1626 1640 name, value) == CTF_ERR);
1627 1641 }
1628 1642
1629 1643 /*ARGSUSED*/
1630 1644 static int
1631 1645 membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg)
1632 1646 {
1633 1647 ctf_bundle_t *ctb = arg;
1634 1648 ctf_membinfo_t ctm;
1635 1649
1636 1650 return (ctf_member_info(ctb->ctb_file, ctb->ctb_type,
1637 1651 name, &ctm) == CTF_ERR || ctm.ctm_offset != offset);
1638 1652 }
1639 1653
1640 1654 static int
1641 1655 membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg)
1642 1656 {
1643 1657 ctf_bundle_t *ctb = arg;
1644 1658 ctf_dmdef_t *dmd;
1645 1659 char *s = NULL;
1646 1660
1647 1661 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1648 1662 return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1649 1663
1650 1664 if (name != NULL && (s = ctf_strdup(name)) == NULL) {
1651 1665 ctf_free(dmd, sizeof (ctf_dmdef_t));
1652 1666 return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1653 1667 }
1654 1668
1655 1669 /*
1656 1670 * For now, dmd_type is copied as the src_fp's type; it is reset to an
1657 1671 * equivalent dst_fp type by a final loop in ctf_add_type(), below.
1658 1672 */
1659 1673 dmd->dmd_name = s;
1660 1674 dmd->dmd_type = type;
1661 1675 dmd->dmd_offset = offset;
1662 1676 dmd->dmd_value = -1;
1663 1677
1664 1678 ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
1665 1679
1666 1680 if (s != NULL)
1667 1681 ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1;
1668 1682
1669 1683 ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
1670 1684 return (0);
1671 1685 }
1672 1686
1673 1687 /*
1674 1688 * The ctf_add_type routine is used to copy a type from a source CTF container
1675 1689 * to a dynamic destination container. This routine operates recursively by
1676 1690 * following the source type's links and embedded member types. If the
1677 1691 * destination container already contains a named type which has the same
1678 1692 * attributes, then we succeed and return this type but no changes occur.
1679 1693 */
1680 1694 ctf_id_t
1681 1695 ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
1682 1696 {
1683 1697 ctf_id_t dst_type = CTF_ERR;
1684 1698 uint_t dst_kind = CTF_K_UNKNOWN;
1685 1699
1686 1700 const ctf_type_t *tp;
1687 1701 const char *name;
1688 1702 uint_t kind, flag, vlen;
1689 1703
1690 1704 ctf_bundle_t src, dst;
1691 1705 ctf_encoding_t src_en, dst_en;
1692 1706 ctf_arinfo_t src_ar, dst_ar;
1693 1707
1694 1708 ctf_dtdef_t *dtd;
1695 1709 ctf_funcinfo_t ctc;
1696 1710 ssize_t size;
1697 1711
1698 1712 ctf_hash_t *hp;
1699 1713 ctf_helem_t *hep;
1700 1714
1701 1715 if (dst_fp == src_fp)
1702 1716 return (src_type);
1703 1717
1704 1718 if (!(dst_fp->ctf_flags & LCTF_RDWR))
1705 1719 return (ctf_set_errno(dst_fp, ECTF_RDONLY));
1706 1720
1707 1721 if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)
1708 1722 return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1709 1723
1710 1724 name = ctf_strptr(src_fp, tp->ctt_name);
1711 1725 kind = LCTF_INFO_KIND(src_fp, tp->ctt_info);
1712 1726 flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info);
1713 1727 vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info);
1714 1728
1715 1729 switch (kind) {
1716 1730 case CTF_K_STRUCT:
1717 1731 hp = &dst_fp->ctf_structs;
1718 1732 break;
1719 1733 case CTF_K_UNION:
1720 1734 hp = &dst_fp->ctf_unions;
1721 1735 break;
1722 1736 case CTF_K_ENUM:
1723 1737 hp = &dst_fp->ctf_enums;
1724 1738 break;
1725 1739 default:
1726 1740 hp = &dst_fp->ctf_names;
1727 1741 break;
1728 1742 }
1729 1743
1730 1744 /*
1731 1745 * If the source type has a name and is a root type (visible at the
1732 1746 * top-level scope), lookup the name in the destination container and
1733 1747 * verify that it is of the same kind before we do anything else.
1734 1748 */
1735 1749 if ((flag & CTF_ADD_ROOT) && name[0] != '\0' &&
1736 1750 (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) {
1737 1751 dst_type = (ctf_id_t)hep->h_type;
1738 1752 dst_kind = ctf_type_kind(dst_fp, dst_type);
1739 1753 }
1740 1754
1741 1755 /*
1742 1756 * If an identically named dst_type exists, fail with ECTF_CONFLICT
1743 1757 * unless dst_type is a forward declaration and src_type is a struct,
1744 1758 * union, or enum (i.e. the definition of the previous forward decl).
1745 1759 */
1746 1760 if (dst_type != CTF_ERR && dst_kind != kind && (
1747 1761 dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM &&
1748 1762 kind != CTF_K_STRUCT && kind != CTF_K_UNION)))
1749 1763 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1750 1764
1751 1765 /*
1752 1766 * If the non-empty name was not found in the appropriate hash, search
1753 1767 * the list of pending dynamic definitions that are not yet committed.
1754 1768 * If a matching name and kind are found, assume this is the type that
1755 1769 * we are looking for. This is necessary to permit ctf_add_type() to
1756 1770 * operate recursively on entities such as a struct that contains a
1757 1771 * pointer member that refers to the same struct type.
1758 1772 */
1759 1773 if (dst_type == CTF_ERR && name[0] != '\0') {
1760 1774 for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL &&
1761 1775 dtd->dtd_type > dst_fp->ctf_dtoldid;
1762 1776 dtd = ctf_list_prev(dtd)) {
1763 1777 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) == kind &&
1764 1778 dtd->dtd_name != NULL &&
1765 1779 strcmp(dtd->dtd_name, name) == 0)
1766 1780 return (dtd->dtd_type);
1767 1781 }
1768 1782 }
1769 1783
1770 1784 src.ctb_file = src_fp;
1771 1785 src.ctb_type = src_type;
1772 1786 src.ctb_dtd = NULL;
1773 1787
1774 1788 dst.ctb_file = dst_fp;
1775 1789 dst.ctb_type = dst_type;
1776 1790 dst.ctb_dtd = NULL;
1777 1791
1778 1792 /*
1779 1793 * Now perform kind-specific processing. If dst_type is CTF_ERR, then
1780 1794 * we add a new type with the same properties as src_type to dst_fp.
1781 1795 * If dst_type is not CTF_ERR, then we verify that dst_type has the
1782 1796 * same attributes as src_type. We recurse for embedded references.
1783 1797 */
1784 1798 switch (kind) {
1785 1799 case CTF_K_INTEGER:
1786 1800 case CTF_K_FLOAT:
1787 1801 if (ctf_type_encoding(src_fp, src_type, &src_en) != 0)
1788 1802 return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1789 1803
1790 1804 if (dst_type != CTF_ERR) {
1791 1805 if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0)
1792 1806 return (CTF_ERR); /* errno is set for us */
1793 1807
1794 1808 if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t)))
1795 1809 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1796 1810
1797 1811 } else if (kind == CTF_K_INTEGER) {
1798 1812 dst_type = ctf_add_integer(dst_fp, flag, name, &src_en);
1799 1813 } else
1800 1814 dst_type = ctf_add_float(dst_fp, flag, name, &src_en);
1801 1815 break;
1802 1816
1803 1817 case CTF_K_POINTER:
1804 1818 case CTF_K_VOLATILE:
1805 1819 case CTF_K_CONST:
1806 1820 case CTF_K_RESTRICT:
1807 1821 src_type = ctf_type_reference(src_fp, src_type);
1808 1822 src_type = ctf_add_type(dst_fp, src_fp, src_type);
1809 1823
1810 1824 if (src_type == CTF_ERR)
1811 1825 return (CTF_ERR); /* errno is set for us */
1812 1826
1813 1827 dst_type = ctf_add_reftype(dst_fp, flag, NULL, src_type, kind);
1814 1828 break;
1815 1829
1816 1830 case CTF_K_ARRAY:
1817 1831 if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR)
1818 1832 return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1819 1833
1820 1834 src_ar.ctr_contents =
1821 1835 ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents);
1822 1836 src_ar.ctr_index =
1823 1837 ctf_add_type(dst_fp, src_fp, src_ar.ctr_index);
1824 1838 src_ar.ctr_nelems = src_ar.ctr_nelems;
1825 1839
1826 1840 if (src_ar.ctr_contents == CTF_ERR ||
1827 1841 src_ar.ctr_index == CTF_ERR)
1828 1842 return (CTF_ERR); /* errno is set for us */
1829 1843
1830 1844 if (dst_type != CTF_ERR) {
1831 1845 if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0)
1832 1846 return (CTF_ERR); /* errno is set for us */
1833 1847
1834 1848 if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
1835 1849 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1836 1850 } else
1837 1851 dst_type = ctf_add_array(dst_fp, flag, &src_ar);
1838 1852 break;
1839 1853
1840 1854 case CTF_K_FUNCTION:
1841 1855 ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type);
1842 1856 ctc.ctc_argc = 0;
1843 1857 ctc.ctc_flags = 0;
1844 1858
1845 1859 if (ctc.ctc_return == CTF_ERR)
1846 1860 return (CTF_ERR); /* errno is set for us */
1847 1861
1848 1862 dst_type = ctf_add_funcptr(dst_fp, flag, &ctc, NULL);
1849 1863 break;
1850 1864
1851 1865 case CTF_K_STRUCT:
1852 1866 case CTF_K_UNION: {
1853 1867 ctf_dmdef_t *dmd;
1854 1868 int errs = 0;
1855 1869
1856 1870 /*
1857 1871 * Technically to match a struct or union we need to check both
1858 1872 * ways (src members vs. dst, dst members vs. src) but we make
1859 1873 * this more optimal by only checking src vs. dst and comparing
1860 1874 * the total size of the structure (which we must do anyway)
1861 1875 * which covers the possibility of dst members not in src.
1862 1876 * This optimization can be defeated for unions, but is so
1863 1877 * pathological as to render it irrelevant for our purposes.
1864 1878 */
1865 1879 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1866 1880 if (ctf_type_size(src_fp, src_type) !=
1867 1881 ctf_type_size(dst_fp, dst_type))
1868 1882 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1869 1883
1870 1884 if (ctf_member_iter(src_fp, src_type, membcmp, &dst))
1871 1885 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1872 1886
1873 1887 break;
1874 1888 }
1875 1889
1876 1890 /*
1877 1891 * Unlike the other cases, copying structs and unions is done
1878 1892 * manually so as to avoid repeated lookups in ctf_add_member
1879 1893 * and to ensure the exact same member offsets as in src_type.
1880 1894 */
1881 1895 dst_type = ctf_add_generic(dst_fp, flag, name, &dtd);
1882 1896 if (dst_type == CTF_ERR)
1883 1897 return (CTF_ERR); /* errno is set for us */
1884 1898
1885 1899 dst.ctb_type = dst_type;
1886 1900 dst.ctb_dtd = dtd;
1887 1901
1888 1902 if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0)
1889 1903 errs++; /* increment errs and fail at bottom of case */
1890 1904
1891 1905 if ((size = ctf_type_size(src_fp, src_type)) > CTF_MAX_SIZE) {
1892 1906 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1893 1907 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
1894 1908 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
1895 1909 } else
1896 1910 dtd->dtd_data.ctt_size = (ushort_t)size;
1897 1911
1898 1912 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen);
1899 1913
1900 1914 /*
1901 1915 * Make a final pass through the members changing each dmd_type
1902 1916 * (a src_fp type) to an equivalent type in dst_fp. We pass
1903 1917 * through all members, leaving any that fail set to CTF_ERR.
1904 1918 */
1905 1919 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1906 1920 dmd != NULL; dmd = ctf_list_next(dmd)) {
1907 1921 if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp,
1908 1922 dmd->dmd_type)) == CTF_ERR)
1909 1923 errs++;
1910 1924 }
1911 1925
1912 1926 if (errs)
1913 1927 return (CTF_ERR); /* errno is set for us */
1914 1928
1915 1929 /*
1916 1930 * Now that we know that we can't fail, we go through and bump
1917 1931 * all the reference counts on the member types.
1918 1932 */
1919 1933 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1920 1934 dmd != NULL; dmd = ctf_list_next(dmd))
↓ open down ↓ |
634 lines elided |
↑ open up ↑ |
1921 1935 ctf_ref_inc(dst_fp, dmd->dmd_type);
1922 1936 break;
1923 1937 }
1924 1938
1925 1939 case CTF_K_ENUM:
1926 1940 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1927 1941 if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) ||
1928 1942 ctf_enum_iter(dst_fp, dst_type, enumcmp, &src))
1929 1943 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1930 1944 } else {
1931 - dst_type = ctf_add_enum(dst_fp, flag, name);
1945 + size_t size = ctf_type_size(src_fp, src_type);
1946 + dst_type = ctf_add_enum(dst_fp, flag, name, size);
1932 1947 if ((dst.ctb_type = dst_type) == CTF_ERR ||
1933 1948 ctf_enum_iter(src_fp, src_type, enumadd, &dst))
1934 1949 return (CTF_ERR); /* errno is set for us */
1935 1950 }
1936 1951 break;
1937 1952
1938 1953 case CTF_K_FORWARD:
1939 1954 if (dst_type == CTF_ERR) {
1940 1955 dst_type = ctf_add_forward(dst_fp,
1941 1956 flag, name, CTF_K_STRUCT); /* assume STRUCT */
1942 1957 }
1943 1958 break;
1944 1959
1945 1960 case CTF_K_TYPEDEF:
1946 1961 src_type = ctf_type_reference(src_fp, src_type);
1947 1962 src_type = ctf_add_type(dst_fp, src_fp, src_type);
1948 1963
1949 1964 if (src_type == CTF_ERR)
1950 1965 return (CTF_ERR); /* errno is set for us */
1951 1966
1952 1967 /*
1953 1968 * If dst_type is not CTF_ERR at this point, we should check if
1954 1969 * ctf_type_reference(dst_fp, dst_type) != src_type and if so
1955 1970 * fail with ECTF_CONFLICT. However, this causes problems with
1956 1971 * <sys/types.h> typedefs that vary based on things like if
1957 1972 * _ILP32x then pid_t is int otherwise long. We therefore omit
1958 1973 * this check and assume that if the identically named typedef
1959 1974 * already exists in dst_fp, it is correct or equivalent.
1960 1975 */
1961 1976 if (dst_type == CTF_ERR) {
1962 1977 dst_type = ctf_add_typedef(dst_fp, flag,
1963 1978 name, src_type);
1964 1979 }
1965 1980 break;
1966 1981
1967 1982 default:
1968 1983 return (ctf_set_errno(dst_fp, ECTF_CORRUPT));
1969 1984 }
1970 1985
1971 1986 return (dst_type);
1972 1987 }
1973 1988
1974 1989 int
1975 1990 ctf_add_function(ctf_file_t *fp, ulong_t idx, const ctf_funcinfo_t *fip,
1976 1991 const ctf_id_t *argc)
1977 1992 {
1978 1993 int i;
1979 1994 ctf_dsdef_t *dsd;
1980 1995 ctf_file_t *afp;
1981 1996 uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
1982 1997
1983 1998 if (!(fp->ctf_flags & LCTF_RDWR))
1984 1999 return (ctf_set_errno(fp, ECTF_RDONLY));
1985 2000
1986 2001 if (ctf_dsd_lookup(fp, idx) != NULL)
1987 2002 return (ctf_set_errno(fp, ECTF_CONFLICT));
1988 2003
1989 2004 if (symbase == (uintptr_t)NULL)
1990 2005 return (ctf_set_errno(fp, ECTF_STRTAB));
1991 2006
1992 2007 if (idx > fp->ctf_nsyms)
1993 2008 return (ctf_set_errno(fp, ECTF_NOTDATA));
1994 2009
1995 2010 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
1996 2011 const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx;
1997 2012 if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC)
1998 2013 return (ctf_set_errno(fp, ECTF_NOTFUNC));
1999 2014 } else {
2000 2015 const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx;
2001 2016 if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC)
2002 2017 return (ctf_set_errno(fp, ECTF_NOTFUNC));
2003 2018 }
2004 2019
2005 2020 afp = fp;
2006 2021 if (ctf_lookup_by_id(&afp, fip->ctc_return) == NULL)
2007 2022 return (CTF_ERR); /* errno is set for us */
2008 2023
2009 2024 for (i = 0; i < fip->ctc_argc; i++) {
2010 2025 afp = fp;
2011 2026 if (ctf_lookup_by_id(&afp, argc[i]) == NULL)
2012 2027 return (CTF_ERR); /* errno is set for us */
2013 2028 }
2014 2029
2015 2030 dsd = ctf_alloc(sizeof (ctf_dsdef_t));
2016 2031 if (dsd == NULL)
2017 2032 return (ctf_set_errno(fp, ENOMEM));
2018 2033 dsd->dsd_nargs = fip->ctc_argc;
2019 2034 if (fip->ctc_flags & CTF_FUNC_VARARG)
2020 2035 dsd->dsd_nargs++;
2021 2036 if (dsd->dsd_nargs != 0) {
2022 2037 dsd->dsd_argc = ctf_alloc(sizeof (ctf_id_t) * dsd->dsd_nargs);
2023 2038 if (dsd->dsd_argc == NULL) {
2024 2039 ctf_free(dsd, sizeof (ctf_dsdef_t));
2025 2040 return (ctf_set_errno(fp, ENOMEM));
2026 2041 }
2027 2042 bcopy(argc, dsd->dsd_argc, sizeof (ctf_id_t) * fip->ctc_argc);
2028 2043 if (fip->ctc_flags & CTF_FUNC_VARARG)
2029 2044 dsd->dsd_argc[fip->ctc_argc] = 0;
2030 2045 }
2031 2046 dsd->dsd_symidx = idx;
2032 2047 dsd->dsd_tid = fip->ctc_return;
2033 2048
2034 2049 ctf_dsd_insert(fp, dsd);
2035 2050 fp->ctf_flags |= LCTF_DIRTY;
2036 2051
2037 2052 return (0);
2038 2053 }
2039 2054
2040 2055 int
2041 2056 ctf_add_object(ctf_file_t *fp, ulong_t idx, ctf_id_t type)
2042 2057 {
2043 2058 ctf_dsdef_t *dsd;
2044 2059 ctf_file_t *afp;
2045 2060 uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
2046 2061
2047 2062 if (!(fp->ctf_flags & LCTF_RDWR))
2048 2063 return (ctf_set_errno(fp, ECTF_RDONLY));
2049 2064
2050 2065 if (!(fp->ctf_flags & LCTF_RDWR))
2051 2066 return (ctf_set_errno(fp, ECTF_RDONLY));
2052 2067
2053 2068 if (ctf_dsd_lookup(fp, idx) != NULL)
2054 2069 return (ctf_set_errno(fp, ECTF_CONFLICT));
2055 2070
2056 2071 if (symbase == (uintptr_t)NULL)
2057 2072 return (ctf_set_errno(fp, ECTF_STRTAB));
2058 2073
2059 2074 if (idx > fp->ctf_nsyms)
2060 2075 return (ctf_set_errno(fp, ECTF_NOTDATA));
2061 2076
2062 2077 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
2063 2078 const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx;
2064 2079 if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT)
2065 2080 return (ctf_set_errno(fp, ECTF_NOTDATA));
2066 2081 } else {
2067 2082 const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx;
2068 2083 if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT)
2069 2084 return (ctf_set_errno(fp, ECTF_NOTDATA));
2070 2085 }
2071 2086
2072 2087 afp = fp;
2073 2088 if (ctf_lookup_by_id(&afp, type) == NULL)
2074 2089 return (CTF_ERR); /* errno is set for us */
2075 2090
2076 2091 dsd = ctf_alloc(sizeof (ctf_dsdef_t));
2077 2092 if (dsd == NULL)
2078 2093 return (ctf_set_errno(fp, ENOMEM));
2079 2094 dsd->dsd_symidx = idx;
2080 2095 dsd->dsd_tid = type;
2081 2096 dsd->dsd_argc = NULL;
2082 2097
2083 2098 ctf_dsd_insert(fp, dsd);
2084 2099 fp->ctf_flags |= LCTF_DIRTY;
2085 2100
2086 2101 return (0);
2087 2102 }
2088 2103
2089 2104 void
2090 2105 ctf_dataptr(ctf_file_t *fp, const void **addrp, size_t *sizep)
2091 2106 {
2092 2107 if (addrp != NULL)
2093 2108 *addrp = fp->ctf_base;
2094 2109 if (sizep != NULL)
2095 2110 *sizep = fp->ctf_size;
2096 2111 }
2097 2112
2098 2113 int
2099 2114 ctf_add_label(ctf_file_t *fp, const char *name, ctf_id_t type, uint_t position)
2100 2115 {
2101 2116 ctf_file_t *fpd;
2102 2117 ctf_dldef_t *dld;
2103 2118
2104 2119 if (name == NULL)
2105 2120 return (ctf_set_errno(fp, EINVAL));
2106 2121
2107 2122 if (!(fp->ctf_flags & LCTF_RDWR))
2108 2123 return (ctf_set_errno(fp, ECTF_RDONLY));
2109 2124
2110 2125 fpd = fp;
2111 2126 if (type != 0 && ctf_lookup_by_id(&fpd, type) == NULL)
2112 2127 return (CTF_ERR); /* errno is set for us */
2113 2128
2114 2129 if (type != 0 && (fp->ctf_flags & LCTF_CHILD) &&
2115 2130 CTF_TYPE_ISPARENT(type))
2116 2131 return (ctf_set_errno(fp, ECTF_NOPARENT));
2117 2132
2118 2133 if (ctf_dld_lookup(fp, name) != NULL)
2119 2134 return (ctf_set_errno(fp, ECTF_LABELEXISTS));
2120 2135
2121 2136 if ((dld = ctf_alloc(sizeof (ctf_dldef_t))) == NULL)
2122 2137 return (ctf_set_errno(fp, EAGAIN));
2123 2138
2124 2139 if ((dld->dld_name = ctf_strdup(name)) == NULL) {
2125 2140 ctf_free(dld, sizeof (ctf_dldef_t));
2126 2141 return (ctf_set_errno(fp, EAGAIN));
2127 2142 }
2128 2143
2129 2144 ctf_dprintf("adding label %s, %ld\n", name, type);
2130 2145 dld->dld_type = type;
2131 2146 fp->ctf_dtstrlen += strlen(name) + 1;
2132 2147 ctf_dld_insert(fp, dld, position);
2133 2148 fp->ctf_flags |= LCTF_DIRTY;
2134 2149
2135 2150 return (0);
2136 2151 }
2137 2152
2138 2153 /*
2139 2154 * Update the size of a structure or union. Note that we don't allow this to
2140 2155 * shrink the size of a struct or union, only to increase it. This is useful for
2141 2156 * cases when you have a structure whose actual size is larger than the sum of
2142 2157 * its members due to padding for natural alignment.
2143 2158 */
2144 2159 int
2145 2160 ctf_set_size(ctf_file_t *fp, ctf_id_t id, const ulong_t newsz)
2146 2161 {
2147 2162 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id);
2148 2163 uint_t kind;
2149 2164 size_t oldsz;
2150 2165
2151 2166 if (!(fp->ctf_flags & LCTF_RDWR))
2152 2167 return (ctf_set_errno(fp, ECTF_RDONLY));
2153 2168
2154 2169 if (dtd == NULL)
2155 2170 return (ctf_set_errno(fp, ECTF_BADID));
2156 2171
2157 2172 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2158 2173
2159 2174 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
2160 2175 return (ctf_set_errno(fp, ECTF_NOTSOU));
2161 2176
2162 2177 if ((oldsz = dtd->dtd_data.ctt_size) == CTF_LSIZE_SENT)
2163 2178 oldsz = CTF_TYPE_LSIZE(&dtd->dtd_data);
2164 2179
2165 2180 if (newsz < oldsz)
2166 2181 return (ctf_set_errno(fp, EINVAL));
2167 2182
2168 2183 if (newsz > CTF_MAX_SIZE) {
2169 2184 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
2170 2185 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(newsz);
2171 2186 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(newsz);
2172 2187 } else {
2173 2188 dtd->dtd_data.ctt_size = (ushort_t)newsz;
2174 2189 }
2175 2190
2176 2191 fp->ctf_flags |= LCTF_DIRTY;
2177 2192 return (0);
2178 2193 }
2179 2194
2180 2195 int
2181 2196 ctf_set_root(ctf_file_t *fp, ctf_id_t id, const boolean_t vis)
2182 2197 {
2183 2198 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id);
2184 2199 uint_t kind, vlen;
2185 2200
2186 2201 if (!(fp->ctf_flags & LCTF_RDWR))
2187 2202 return (ctf_set_errno(fp, ECTF_RDONLY));
2188 2203
2189 2204 if (dtd == NULL)
2190 2205 return (ctf_set_errno(fp, ECTF_BADID));
2191 2206
2192 2207 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2193 2208 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
2194 2209
2195 2210 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, vis, vlen);
2196 2211 return (0);
2197 2212 }
↓ open down ↓ |
256 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX