Print this page
10812 ctf tools shouldn't add blank labels
10813 ctf symbol mapping needs work
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
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) 2015, Joyent, Inc.
28 + * Copyright (c) 2019, 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);
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 1249 ctf_id_t
1250 1250 ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name)
1251 1251 {
1252 1252 ctf_hash_t *hp = &fp->ctf_enums;
1253 1253 ctf_helem_t *hep = NULL;
1254 1254 ctf_dtdef_t *dtd = NULL;
1255 1255 ctf_id_t type = CTF_ERR;
1256 1256
1257 1257 if (name != NULL)
1258 1258 hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1259 1259
1260 1260 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1261 1261 type = hep->h_type;
1262 1262 dtd = ctf_dtd_lookup(fp, type);
1263 1263 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1264 1264 dtd = NULL;
1265 1265 }
1266 1266
1267 1267 if (dtd == NULL) {
1268 1268 type = ctf_add_generic(fp, flag, name, &dtd);
1269 1269 if (type == CTF_ERR)
1270 1270 return (CTF_ERR); /* errno is set for us */
1271 1271 }
1272 1272
1273 1273 VERIFY(type != CTF_ERR);
1274 1274 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0);
1275 1275 dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
1276 1276
1277 1277 /*
1278 1278 * Always dirty in case we modified a forward.
1279 1279 */
1280 1280 fp->ctf_flags |= LCTF_DIRTY;
1281 1281
1282 1282 return (type);
1283 1283 }
1284 1284
1285 1285 ctf_id_t
1286 1286 ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind)
1287 1287 {
1288 1288 ctf_hash_t *hp;
1289 1289 ctf_helem_t *hep;
1290 1290 ctf_dtdef_t *dtd;
1291 1291 ctf_id_t type;
1292 1292
1293 1293 switch (kind) {
1294 1294 case CTF_K_STRUCT:
1295 1295 hp = &fp->ctf_structs;
1296 1296 break;
1297 1297 case CTF_K_UNION:
1298 1298 hp = &fp->ctf_unions;
1299 1299 break;
1300 1300 case CTF_K_ENUM:
1301 1301 hp = &fp->ctf_enums;
1302 1302 break;
1303 1303 default:
1304 1304 return (ctf_set_errno(fp, ECTF_NOTSUE));
1305 1305 }
1306 1306
1307 1307 /*
1308 1308 * If the type is already defined or exists as a forward tag, just
1309 1309 * return the ctf_id_t of the existing definition.
1310 1310 */
1311 1311 if (name != NULL && (hep = ctf_hash_lookup(hp,
1312 1312 fp, name, strlen(name))) != NULL)
1313 1313 return (hep->h_type);
1314 1314
1315 1315 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1316 1316 return (CTF_ERR); /* errno is set for us */
1317 1317
1318 1318 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0);
1319 1319 dtd->dtd_data.ctt_type = kind;
1320 1320
1321 1321 return (type);
1322 1322 }
1323 1323
1324 1324 ctf_id_t
1325 1325 ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1326 1326 {
1327 1327 ctf_dtdef_t *dtd;
1328 1328 ctf_id_t type;
1329 1329 ctf_file_t *fpd;
1330 1330
1331 1331 fpd = fp;
1332 1332 if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL &&
1333 1333 ctf_dtd_lookup(fp, ref) == NULL))
1334 1334 return (ctf_set_errno(fp, EINVAL));
1335 1335
1336 1336 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1337 1337 return (CTF_ERR); /* errno is set for us */
1338 1338
1339 1339 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0);
1340 1340 dtd->dtd_data.ctt_type = (ushort_t)ref;
1341 1341 ctf_ref_inc(fp, ref);
1342 1342
1343 1343 return (type);
1344 1344 }
1345 1345
1346 1346 ctf_id_t
1347 1347 ctf_add_volatile(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1348 1348 {
1349 1349 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_VOLATILE));
1350 1350 }
1351 1351
1352 1352 ctf_id_t
1353 1353 ctf_add_const(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1354 1354 {
1355 1355 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_CONST));
1356 1356 }
1357 1357
1358 1358 ctf_id_t
1359 1359 ctf_add_restrict(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1360 1360 {
1361 1361 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_RESTRICT));
1362 1362 }
1363 1363
1364 1364 int
1365 1365 ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)
1366 1366 {
1367 1367 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);
1368 1368 ctf_dmdef_t *dmd;
1369 1369
1370 1370 uint_t kind, vlen, root;
1371 1371 char *s;
1372 1372
1373 1373 if (name == NULL)
1374 1374 return (ctf_set_errno(fp, EINVAL));
1375 1375
1376 1376 if (!(fp->ctf_flags & LCTF_RDWR))
1377 1377 return (ctf_set_errno(fp, ECTF_RDONLY));
1378 1378
1379 1379 if (dtd == NULL)
1380 1380 return (ctf_set_errno(fp, ECTF_BADID));
1381 1381
1382 1382 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1383 1383 root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
1384 1384 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
1385 1385
1386 1386 if (kind != CTF_K_ENUM)
1387 1387 return (ctf_set_errno(fp, ECTF_NOTENUM));
1388 1388
1389 1389 if (vlen == CTF_MAX_VLEN)
1390 1390 return (ctf_set_errno(fp, ECTF_DTFULL));
1391 1391
1392 1392 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1393 1393 dmd != NULL; dmd = ctf_list_next(dmd)) {
1394 1394 if (strcmp(dmd->dmd_name, name) == 0) {
1395 1395 ctf_dprintf("encountered duplicate member %s\n", name);
1396 1396 return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1397 1397 }
1398 1398 }
1399 1399
1400 1400 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1401 1401 return (ctf_set_errno(fp, EAGAIN));
1402 1402
1403 1403 if ((s = ctf_strdup(name)) == NULL) {
1404 1404 ctf_free(dmd, sizeof (ctf_dmdef_t));
1405 1405 return (ctf_set_errno(fp, EAGAIN));
1406 1406 }
1407 1407
1408 1408 dmd->dmd_name = s;
1409 1409 dmd->dmd_type = CTF_ERR;
1410 1410 dmd->dmd_offset = 0;
1411 1411 dmd->dmd_value = value;
1412 1412
1413 1413 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
1414 1414 ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1415 1415
1416 1416 fp->ctf_dtstrlen += strlen(s) + 1;
1417 1417 fp->ctf_flags |= LCTF_DIRTY;
1418 1418
1419 1419 return (0);
1420 1420 }
1421 1421
1422 1422 int
1423 1423 ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type,
1424 1424 ulong_t offset)
1425 1425 {
1426 1426 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);
1427 1427 ctf_dmdef_t *dmd;
1428 1428
1429 1429 ulong_t mbitsz;
1430 1430 ssize_t msize, malign, ssize;
1431 1431 uint_t kind, vlen, root;
1432 1432 int mkind;
1433 1433 char *s = NULL;
1434 1434
1435 1435 if (!(fp->ctf_flags & LCTF_RDWR))
1436 1436 return (ctf_set_errno(fp, ECTF_RDONLY));
1437 1437
1438 1438 if (dtd == NULL)
1439 1439 return (ctf_set_errno(fp, ECTF_BADID));
1440 1440
1441 1441 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1442 1442 root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
1443 1443 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
1444 1444
1445 1445 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
1446 1446 return (ctf_set_errno(fp, ECTF_NOTSOU));
1447 1447
1448 1448 if (vlen == CTF_MAX_VLEN)
1449 1449 return (ctf_set_errno(fp, ECTF_DTFULL));
1450 1450
1451 1451 /*
1452 1452 * Structures may have members which are anonymous. If they have two of
1453 1453 * these, then the duplicate member detection would find it due to the
1454 1454 * string of "", so we skip it.
1455 1455 */
1456 1456 if (name != NULL && *name != '\0') {
1457 1457 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1458 1458 dmd != NULL; dmd = ctf_list_next(dmd)) {
1459 1459 if (dmd->dmd_name != NULL &&
1460 1460 strcmp(dmd->dmd_name, name) == 0) {
1461 1461 return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1462 1462 }
1463 1463 }
1464 1464 }
1465 1465
1466 1466 if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||
1467 1467 (malign = ctf_type_align(fp, type)) == CTF_ERR ||
1468 1468 (mkind = ctf_type_kind(fp, type)) == CTF_ERR)
1469 1469 return (CTF_ERR); /* errno is set for us */
1470 1470
1471 1471 /*
1472 1472 * ctf_type_size returns sizes in bytes. However, for bitfields, that
1473 1473 * means that it may misrepresent and actually rounds it up to a power
1474 1474 * of two and store that in bytes. So instead we have to get the
1475 1475 * Integers encoding and rely on that.
1476 1476 */
1477 1477 if (mkind == CTF_K_INTEGER) {
1478 1478 ctf_encoding_t e;
1479 1479
1480 1480 if (ctf_type_encoding(fp, type, &e) == CTF_ERR)
1481 1481 return (CTF_ERR); /* errno is set for us */
1482 1482 mbitsz = e.cte_bits;
1483 1483 } else if (mkind == CTF_K_FORWARD) {
1484 1484 /*
1485 1485 * This is a rather rare case. In general one cannot add a
1486 1486 * forward to a structure. However, the CTF tools traditionally
1487 1487 * tried to add a forward to the struct cpu as the last member.
1488 1488 * Therefore, if we find one here, we're going to verify the
1489 1489 * size and make sure it's zero. It's certainly odd, but that's
1490 1490 * life.
1491 1491 *
1492 1492 * Further, if it's not an absolute position being specified,
1493 1493 * then we refuse to add it.
1494 1494 */
1495 1495 if (offset == ULONG_MAX)
1496 1496 return (ctf_set_errno(fp, EINVAL));
1497 1497 VERIFY(msize == 0);
1498 1498 mbitsz = msize;
1499 1499 } else {
1500 1500 mbitsz = msize * 8;
1501 1501 }
1502 1502
1503 1503 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1504 1504 return (ctf_set_errno(fp, EAGAIN));
1505 1505
1506 1506 if (name != NULL && (s = ctf_strdup(name)) == NULL) {
1507 1507 ctf_free(dmd, sizeof (ctf_dmdef_t));
1508 1508 return (ctf_set_errno(fp, EAGAIN));
1509 1509 }
1510 1510
1511 1511 dmd->dmd_name = s;
1512 1512 dmd->dmd_type = type;
1513 1513 dmd->dmd_value = -1;
1514 1514
1515 1515 if (kind == CTF_K_STRUCT && vlen != 0) {
1516 1516 ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);
1517 1517 ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);
1518 1518 size_t off;
1519 1519
1520 1520 if (offset == ULONG_MAX) {
1521 1521 ctf_encoding_t linfo;
1522 1522 ssize_t lsize;
1523 1523
1524 1524 off = lmd->dmd_offset;
1525 1525 if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)
1526 1526 off += linfo.cte_bits;
1527 1527 else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)
1528 1528 off += lsize * NBBY;
1529 1529
1530 1530 /*
1531 1531 * Round up the offset of the end of the last member to
1532 1532 * the next byte boundary, convert 'off' to bytes, and
1533 1533 * then round it up again to the next multiple of the
1534 1534 * alignment required by the new member. Finally,
1535 1535 * convert back to bits and store the result in
1536 1536 * dmd_offset. Technically we could do more efficient
1537 1537 * packing if the new member is a bit-field, but we're
1538 1538 * the "compiler" and ANSI says we can do as we choose.
1539 1539 */
1540 1540 off = roundup(off, NBBY) / NBBY;
1541 1541 off = roundup(off, MAX(malign, 1));
1542 1542 dmd->dmd_offset = off * NBBY;
1543 1543 ssize = off + msize;
1544 1544 } else {
1545 1545 dmd->dmd_offset = offset;
1546 1546 ssize = (offset + mbitsz) / NBBY;
1547 1547 }
1548 1548 } else {
1549 1549 dmd->dmd_offset = 0;
1550 1550 ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
1551 1551 ssize = MAX(ssize, msize);
1552 1552 }
1553 1553
1554 1554 if (ssize > CTF_MAX_SIZE) {
1555 1555 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1556 1556 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);
1557 1557 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);
1558 1558 } else
1559 1559 dtd->dtd_data.ctt_size = (ushort_t)ssize;
1560 1560
1561 1561 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
1562 1562 ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1563 1563
1564 1564 if (s != NULL)
1565 1565 fp->ctf_dtstrlen += strlen(s) + 1;
1566 1566
1567 1567 ctf_ref_inc(fp, type);
1568 1568 fp->ctf_flags |= LCTF_DIRTY;
1569 1569 return (0);
1570 1570 }
1571 1571
1572 1572 /*
1573 1573 * This removes a type from the dynamic section. This will fail if the type is
1574 1574 * referenced by another type. Note that the CTF ID is never reused currently by
1575 1575 * CTF. Note that if this container is a parent container then we just outright
1576 1576 * refuse to remove the type. There currently is no notion of searching for the
1577 1577 * ctf_dtdef_t in parent containers. If there is, then this constraint could
1578 1578 * become finer grained.
1579 1579 */
1580 1580 int
1581 1581 ctf_delete_type(ctf_file_t *fp, ctf_id_t type)
1582 1582 {
1583 1583 ctf_file_t *fpd;
1584 1584 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
1585 1585
1586 1586 if (!(fp->ctf_flags & LCTF_RDWR))
1587 1587 return (ctf_set_errno(fp, ECTF_RDONLY));
1588 1588
1589 1589 /*
1590 1590 * We want to give as useful an errno as possible. That means that we
1591 1591 * want to distinguish between a type which does not exist and one for
1592 1592 * which the type is not dynamic.
1593 1593 */
1594 1594 fpd = fp;
1595 1595 if (ctf_lookup_by_id(&fpd, type) == NULL &&
1596 1596 ctf_dtd_lookup(fp, type) == NULL)
1597 1597 return (CTF_ERR); /* errno is set for us */
1598 1598
1599 1599 if (dtd == NULL)
1600 1600 return (ctf_set_errno(fp, ECTF_NOTDYN));
1601 1601
1602 1602 if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1)
1603 1603 return (ctf_set_errno(fp, ECTF_REFERENCED));
1604 1604
1605 1605 ctf_dtd_delete(fp, dtd);
1606 1606 fp->ctf_flags |= LCTF_DIRTY;
1607 1607 return (0);
1608 1608 }
1609 1609
1610 1610 static int
1611 1611 enumcmp(const char *name, int value, void *arg)
1612 1612 {
1613 1613 ctf_bundle_t *ctb = arg;
1614 1614 int bvalue;
1615 1615
1616 1616 return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type,
1617 1617 name, &bvalue) == CTF_ERR || value != bvalue);
1618 1618 }
1619 1619
1620 1620 static int
1621 1621 enumadd(const char *name, int value, void *arg)
1622 1622 {
1623 1623 ctf_bundle_t *ctb = arg;
1624 1624
1625 1625 return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type,
1626 1626 name, value) == CTF_ERR);
1627 1627 }
1628 1628
1629 1629 /*ARGSUSED*/
1630 1630 static int
1631 1631 membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg)
1632 1632 {
1633 1633 ctf_bundle_t *ctb = arg;
1634 1634 ctf_membinfo_t ctm;
1635 1635
1636 1636 return (ctf_member_info(ctb->ctb_file, ctb->ctb_type,
1637 1637 name, &ctm) == CTF_ERR || ctm.ctm_offset != offset);
1638 1638 }
1639 1639
1640 1640 static int
1641 1641 membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg)
1642 1642 {
1643 1643 ctf_bundle_t *ctb = arg;
1644 1644 ctf_dmdef_t *dmd;
1645 1645 char *s = NULL;
1646 1646
1647 1647 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1648 1648 return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1649 1649
1650 1650 if (name != NULL && (s = ctf_strdup(name)) == NULL) {
1651 1651 ctf_free(dmd, sizeof (ctf_dmdef_t));
1652 1652 return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1653 1653 }
1654 1654
1655 1655 /*
1656 1656 * For now, dmd_type is copied as the src_fp's type; it is reset to an
1657 1657 * equivalent dst_fp type by a final loop in ctf_add_type(), below.
1658 1658 */
1659 1659 dmd->dmd_name = s;
1660 1660 dmd->dmd_type = type;
1661 1661 dmd->dmd_offset = offset;
1662 1662 dmd->dmd_value = -1;
1663 1663
1664 1664 ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
1665 1665
1666 1666 if (s != NULL)
1667 1667 ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1;
1668 1668
1669 1669 ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
1670 1670 return (0);
1671 1671 }
1672 1672
1673 1673 /*
1674 1674 * The ctf_add_type routine is used to copy a type from a source CTF container
1675 1675 * to a dynamic destination container. This routine operates recursively by
1676 1676 * following the source type's links and embedded member types. If the
1677 1677 * destination container already contains a named type which has the same
1678 1678 * attributes, then we succeed and return this type but no changes occur.
1679 1679 */
1680 1680 ctf_id_t
1681 1681 ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
1682 1682 {
1683 1683 ctf_id_t dst_type = CTF_ERR;
1684 1684 uint_t dst_kind = CTF_K_UNKNOWN;
1685 1685
1686 1686 const ctf_type_t *tp;
1687 1687 const char *name;
1688 1688 uint_t kind, flag, vlen;
1689 1689
1690 1690 ctf_bundle_t src, dst;
1691 1691 ctf_encoding_t src_en, dst_en;
1692 1692 ctf_arinfo_t src_ar, dst_ar;
1693 1693
1694 1694 ctf_dtdef_t *dtd;
1695 1695 ctf_funcinfo_t ctc;
1696 1696 ssize_t size;
1697 1697
1698 1698 ctf_hash_t *hp;
1699 1699 ctf_helem_t *hep;
1700 1700
1701 1701 if (dst_fp == src_fp)
1702 1702 return (src_type);
1703 1703
1704 1704 if (!(dst_fp->ctf_flags & LCTF_RDWR))
1705 1705 return (ctf_set_errno(dst_fp, ECTF_RDONLY));
1706 1706
1707 1707 if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)
1708 1708 return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1709 1709
1710 1710 name = ctf_strptr(src_fp, tp->ctt_name);
1711 1711 kind = LCTF_INFO_KIND(src_fp, tp->ctt_info);
1712 1712 flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info);
1713 1713 vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info);
1714 1714
1715 1715 switch (kind) {
1716 1716 case CTF_K_STRUCT:
1717 1717 hp = &dst_fp->ctf_structs;
1718 1718 break;
1719 1719 case CTF_K_UNION:
1720 1720 hp = &dst_fp->ctf_unions;
1721 1721 break;
1722 1722 case CTF_K_ENUM:
1723 1723 hp = &dst_fp->ctf_enums;
1724 1724 break;
1725 1725 default:
1726 1726 hp = &dst_fp->ctf_names;
1727 1727 break;
1728 1728 }
1729 1729
1730 1730 /*
1731 1731 * If the source type has a name and is a root type (visible at the
1732 1732 * top-level scope), lookup the name in the destination container and
1733 1733 * verify that it is of the same kind before we do anything else.
1734 1734 */
1735 1735 if ((flag & CTF_ADD_ROOT) && name[0] != '\0' &&
1736 1736 (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) {
1737 1737 dst_type = (ctf_id_t)hep->h_type;
1738 1738 dst_kind = ctf_type_kind(dst_fp, dst_type);
1739 1739 }
1740 1740
1741 1741 /*
1742 1742 * If an identically named dst_type exists, fail with ECTF_CONFLICT
1743 1743 * unless dst_type is a forward declaration and src_type is a struct,
1744 1744 * union, or enum (i.e. the definition of the previous forward decl).
1745 1745 */
1746 1746 if (dst_type != CTF_ERR && dst_kind != kind && (
1747 1747 dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM &&
1748 1748 kind != CTF_K_STRUCT && kind != CTF_K_UNION)))
1749 1749 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1750 1750
1751 1751 /*
1752 1752 * If the non-empty name was not found in the appropriate hash, search
1753 1753 * the list of pending dynamic definitions that are not yet committed.
1754 1754 * If a matching name and kind are found, assume this is the type that
1755 1755 * we are looking for. This is necessary to permit ctf_add_type() to
1756 1756 * operate recursively on entities such as a struct that contains a
1757 1757 * pointer member that refers to the same struct type.
1758 1758 */
1759 1759 if (dst_type == CTF_ERR && name[0] != '\0') {
1760 1760 for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL &&
1761 1761 dtd->dtd_type > dst_fp->ctf_dtoldid;
1762 1762 dtd = ctf_list_prev(dtd)) {
1763 1763 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) == kind &&
1764 1764 dtd->dtd_name != NULL &&
1765 1765 strcmp(dtd->dtd_name, name) == 0)
1766 1766 return (dtd->dtd_type);
1767 1767 }
1768 1768 }
1769 1769
1770 1770 src.ctb_file = src_fp;
1771 1771 src.ctb_type = src_type;
1772 1772 src.ctb_dtd = NULL;
1773 1773
1774 1774 dst.ctb_file = dst_fp;
1775 1775 dst.ctb_type = dst_type;
1776 1776 dst.ctb_dtd = NULL;
1777 1777
1778 1778 /*
1779 1779 * Now perform kind-specific processing. If dst_type is CTF_ERR, then
1780 1780 * we add a new type with the same properties as src_type to dst_fp.
1781 1781 * If dst_type is not CTF_ERR, then we verify that dst_type has the
1782 1782 * same attributes as src_type. We recurse for embedded references.
1783 1783 */
1784 1784 switch (kind) {
1785 1785 case CTF_K_INTEGER:
1786 1786 case CTF_K_FLOAT:
1787 1787 if (ctf_type_encoding(src_fp, src_type, &src_en) != 0)
1788 1788 return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1789 1789
1790 1790 if (dst_type != CTF_ERR) {
1791 1791 if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0)
1792 1792 return (CTF_ERR); /* errno is set for us */
1793 1793
1794 1794 if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t)))
1795 1795 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1796 1796
1797 1797 } else if (kind == CTF_K_INTEGER) {
1798 1798 dst_type = ctf_add_integer(dst_fp, flag, name, &src_en);
1799 1799 } else
1800 1800 dst_type = ctf_add_float(dst_fp, flag, name, &src_en);
1801 1801 break;
1802 1802
1803 1803 case CTF_K_POINTER:
1804 1804 case CTF_K_VOLATILE:
1805 1805 case CTF_K_CONST:
1806 1806 case CTF_K_RESTRICT:
1807 1807 src_type = ctf_type_reference(src_fp, src_type);
1808 1808 src_type = ctf_add_type(dst_fp, src_fp, src_type);
1809 1809
1810 1810 if (src_type == CTF_ERR)
1811 1811 return (CTF_ERR); /* errno is set for us */
1812 1812
1813 1813 dst_type = ctf_add_reftype(dst_fp, flag, NULL, src_type, kind);
1814 1814 break;
1815 1815
1816 1816 case CTF_K_ARRAY:
1817 1817 if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR)
1818 1818 return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1819 1819
1820 1820 src_ar.ctr_contents =
1821 1821 ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents);
1822 1822 src_ar.ctr_index =
1823 1823 ctf_add_type(dst_fp, src_fp, src_ar.ctr_index);
1824 1824 src_ar.ctr_nelems = src_ar.ctr_nelems;
1825 1825
1826 1826 if (src_ar.ctr_contents == CTF_ERR ||
1827 1827 src_ar.ctr_index == CTF_ERR)
1828 1828 return (CTF_ERR); /* errno is set for us */
1829 1829
1830 1830 if (dst_type != CTF_ERR) {
1831 1831 if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0)
1832 1832 return (CTF_ERR); /* errno is set for us */
1833 1833
1834 1834 if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
1835 1835 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1836 1836 } else
1837 1837 dst_type = ctf_add_array(dst_fp, flag, &src_ar);
1838 1838 break;
1839 1839
1840 1840 case CTF_K_FUNCTION:
1841 1841 ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type);
1842 1842 ctc.ctc_argc = 0;
1843 1843 ctc.ctc_flags = 0;
1844 1844
1845 1845 if (ctc.ctc_return == CTF_ERR)
1846 1846 return (CTF_ERR); /* errno is set for us */
1847 1847
1848 1848 dst_type = ctf_add_funcptr(dst_fp, flag, &ctc, NULL);
1849 1849 break;
1850 1850
1851 1851 case CTF_K_STRUCT:
1852 1852 case CTF_K_UNION: {
1853 1853 ctf_dmdef_t *dmd;
1854 1854 int errs = 0;
1855 1855
1856 1856 /*
1857 1857 * Technically to match a struct or union we need to check both
1858 1858 * ways (src members vs. dst, dst members vs. src) but we make
1859 1859 * this more optimal by only checking src vs. dst and comparing
1860 1860 * the total size of the structure (which we must do anyway)
1861 1861 * which covers the possibility of dst members not in src.
1862 1862 * This optimization can be defeated for unions, but is so
1863 1863 * pathological as to render it irrelevant for our purposes.
1864 1864 */
1865 1865 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1866 1866 if (ctf_type_size(src_fp, src_type) !=
1867 1867 ctf_type_size(dst_fp, dst_type))
1868 1868 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1869 1869
1870 1870 if (ctf_member_iter(src_fp, src_type, membcmp, &dst))
1871 1871 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1872 1872
1873 1873 break;
1874 1874 }
1875 1875
1876 1876 /*
1877 1877 * Unlike the other cases, copying structs and unions is done
1878 1878 * manually so as to avoid repeated lookups in ctf_add_member
1879 1879 * and to ensure the exact same member offsets as in src_type.
1880 1880 */
1881 1881 dst_type = ctf_add_generic(dst_fp, flag, name, &dtd);
1882 1882 if (dst_type == CTF_ERR)
1883 1883 return (CTF_ERR); /* errno is set for us */
1884 1884
1885 1885 dst.ctb_type = dst_type;
1886 1886 dst.ctb_dtd = dtd;
1887 1887
1888 1888 if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0)
1889 1889 errs++; /* increment errs and fail at bottom of case */
1890 1890
1891 1891 if ((size = ctf_type_size(src_fp, src_type)) > CTF_MAX_SIZE) {
1892 1892 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1893 1893 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
1894 1894 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
1895 1895 } else
1896 1896 dtd->dtd_data.ctt_size = (ushort_t)size;
1897 1897
1898 1898 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen);
1899 1899
1900 1900 /*
1901 1901 * Make a final pass through the members changing each dmd_type
1902 1902 * (a src_fp type) to an equivalent type in dst_fp. We pass
1903 1903 * through all members, leaving any that fail set to CTF_ERR.
1904 1904 */
1905 1905 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1906 1906 dmd != NULL; dmd = ctf_list_next(dmd)) {
1907 1907 if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp,
1908 1908 dmd->dmd_type)) == CTF_ERR)
1909 1909 errs++;
1910 1910 }
1911 1911
1912 1912 if (errs)
1913 1913 return (CTF_ERR); /* errno is set for us */
1914 1914
1915 1915 /*
1916 1916 * Now that we know that we can't fail, we go through and bump
1917 1917 * all the reference counts on the member types.
1918 1918 */
1919 1919 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1920 1920 dmd != NULL; dmd = ctf_list_next(dmd))
1921 1921 ctf_ref_inc(dst_fp, dmd->dmd_type);
1922 1922 break;
1923 1923 }
1924 1924
1925 1925 case CTF_K_ENUM:
1926 1926 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1927 1927 if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) ||
1928 1928 ctf_enum_iter(dst_fp, dst_type, enumcmp, &src))
1929 1929 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1930 1930 } else {
1931 1931 dst_type = ctf_add_enum(dst_fp, flag, name);
1932 1932 if ((dst.ctb_type = dst_type) == CTF_ERR ||
1933 1933 ctf_enum_iter(src_fp, src_type, enumadd, &dst))
1934 1934 return (CTF_ERR); /* errno is set for us */
1935 1935 }
1936 1936 break;
1937 1937
1938 1938 case CTF_K_FORWARD:
1939 1939 if (dst_type == CTF_ERR) {
1940 1940 dst_type = ctf_add_forward(dst_fp,
1941 1941 flag, name, CTF_K_STRUCT); /* assume STRUCT */
1942 1942 }
1943 1943 break;
1944 1944
1945 1945 case CTF_K_TYPEDEF:
1946 1946 src_type = ctf_type_reference(src_fp, src_type);
1947 1947 src_type = ctf_add_type(dst_fp, src_fp, src_type);
1948 1948
1949 1949 if (src_type == CTF_ERR)
1950 1950 return (CTF_ERR); /* errno is set for us */
1951 1951
1952 1952 /*
1953 1953 * If dst_type is not CTF_ERR at this point, we should check if
1954 1954 * ctf_type_reference(dst_fp, dst_type) != src_type and if so
1955 1955 * fail with ECTF_CONFLICT. However, this causes problems with
1956 1956 * <sys/types.h> typedefs that vary based on things like if
1957 1957 * _ILP32x then pid_t is int otherwise long. We therefore omit
1958 1958 * this check and assume that if the identically named typedef
1959 1959 * already exists in dst_fp, it is correct or equivalent.
1960 1960 */
1961 1961 if (dst_type == CTF_ERR) {
1962 1962 dst_type = ctf_add_typedef(dst_fp, flag,
1963 1963 name, src_type);
1964 1964 }
1965 1965 break;
1966 1966
1967 1967 default:
1968 1968 return (ctf_set_errno(dst_fp, ECTF_CORRUPT));
1969 1969 }
1970 1970
1971 1971 return (dst_type);
1972 1972 }
1973 1973
1974 1974 int
1975 1975 ctf_add_function(ctf_file_t *fp, ulong_t idx, const ctf_funcinfo_t *fip,
1976 1976 const ctf_id_t *argc)
1977 1977 {
1978 1978 int i;
1979 1979 ctf_dsdef_t *dsd;
1980 1980 ctf_file_t *afp;
1981 1981 uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
1982 1982
1983 1983 if (!(fp->ctf_flags & LCTF_RDWR))
1984 1984 return (ctf_set_errno(fp, ECTF_RDONLY));
1985 1985
1986 1986 if (ctf_dsd_lookup(fp, idx) != NULL)
1987 1987 return (ctf_set_errno(fp, ECTF_CONFLICT));
1988 1988
1989 1989 if (symbase == (uintptr_t)NULL)
1990 1990 return (ctf_set_errno(fp, ECTF_STRTAB));
1991 1991
1992 1992 if (idx > fp->ctf_nsyms)
1993 1993 return (ctf_set_errno(fp, ECTF_NOTDATA));
1994 1994
1995 1995 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
1996 1996 const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx;
1997 1997 if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC)
1998 1998 return (ctf_set_errno(fp, ECTF_NOTFUNC));
1999 1999 } else {
2000 2000 const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx;
2001 2001 if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC)
2002 2002 return (ctf_set_errno(fp, ECTF_NOTFUNC));
2003 2003 }
2004 2004
2005 2005 afp = fp;
2006 2006 if (ctf_lookup_by_id(&afp, fip->ctc_return) == NULL)
2007 2007 return (CTF_ERR); /* errno is set for us */
2008 2008
2009 2009 for (i = 0; i < fip->ctc_argc; i++) {
2010 2010 afp = fp;
2011 2011 if (ctf_lookup_by_id(&afp, argc[i]) == NULL)
2012 2012 return (CTF_ERR); /* errno is set for us */
2013 2013 }
2014 2014
2015 2015 dsd = ctf_alloc(sizeof (ctf_dsdef_t));
2016 2016 if (dsd == NULL)
2017 2017 return (ctf_set_errno(fp, ENOMEM));
2018 2018 dsd->dsd_nargs = fip->ctc_argc;
2019 2019 if (fip->ctc_flags & CTF_FUNC_VARARG)
2020 2020 dsd->dsd_nargs++;
2021 2021 if (dsd->dsd_nargs != 0) {
2022 2022 dsd->dsd_argc = ctf_alloc(sizeof (ctf_id_t) * dsd->dsd_nargs);
2023 2023 if (dsd->dsd_argc == NULL) {
2024 2024 ctf_free(dsd, sizeof (ctf_dsdef_t));
2025 2025 return (ctf_set_errno(fp, ENOMEM));
2026 2026 }
2027 2027 bcopy(argc, dsd->dsd_argc, sizeof (ctf_id_t) * fip->ctc_argc);
2028 2028 if (fip->ctc_flags & CTF_FUNC_VARARG)
2029 2029 dsd->dsd_argc[fip->ctc_argc] = 0;
2030 2030 }
2031 2031 dsd->dsd_symidx = idx;
2032 2032 dsd->dsd_tid = fip->ctc_return;
2033 2033
2034 2034 ctf_dsd_insert(fp, dsd);
2035 2035 fp->ctf_flags |= LCTF_DIRTY;
2036 2036
2037 2037 return (0);
2038 2038 }
2039 2039
2040 2040 int
2041 2041 ctf_add_object(ctf_file_t *fp, ulong_t idx, ctf_id_t type)
2042 2042 {
2043 2043 ctf_dsdef_t *dsd;
2044 2044 ctf_file_t *afp;
2045 2045 uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
2046 2046
2047 2047 if (!(fp->ctf_flags & LCTF_RDWR))
2048 2048 return (ctf_set_errno(fp, ECTF_RDONLY));
2049 2049
2050 2050 if (!(fp->ctf_flags & LCTF_RDWR))
2051 2051 return (ctf_set_errno(fp, ECTF_RDONLY));
2052 2052
2053 2053 if (ctf_dsd_lookup(fp, idx) != NULL)
2054 2054 return (ctf_set_errno(fp, ECTF_CONFLICT));
2055 2055
2056 2056 if (symbase == (uintptr_t)NULL)
2057 2057 return (ctf_set_errno(fp, ECTF_STRTAB));
2058 2058
2059 2059 if (idx > fp->ctf_nsyms)
2060 2060 return (ctf_set_errno(fp, ECTF_NOTDATA));
2061 2061
2062 2062 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
2063 2063 const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx;
2064 2064 if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT)
2065 2065 return (ctf_set_errno(fp, ECTF_NOTDATA));
2066 2066 } else {
2067 2067 const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx;
2068 2068 if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT)
2069 2069 return (ctf_set_errno(fp, ECTF_NOTDATA));
2070 2070 }
2071 2071
2072 2072 afp = fp;
2073 2073 if (ctf_lookup_by_id(&afp, type) == NULL)
2074 2074 return (CTF_ERR); /* errno is set for us */
2075 2075
2076 2076 dsd = ctf_alloc(sizeof (ctf_dsdef_t));
2077 2077 if (dsd == NULL)
2078 2078 return (ctf_set_errno(fp, ENOMEM));
2079 2079 dsd->dsd_symidx = idx;
2080 2080 dsd->dsd_tid = type;
2081 2081 dsd->dsd_argc = NULL;
2082 2082
2083 2083 ctf_dsd_insert(fp, dsd);
2084 2084 fp->ctf_flags |= LCTF_DIRTY;
2085 2085
2086 2086 return (0);
2087 2087 }
2088 2088
2089 2089 void
2090 2090 ctf_dataptr(ctf_file_t *fp, const void **addrp, size_t *sizep)
2091 2091 {
2092 2092 if (addrp != NULL)
2093 2093 *addrp = fp->ctf_base;
2094 2094 if (sizep != NULL)
2095 2095 *sizep = fp->ctf_size;
2096 2096 }
2097 2097
2098 2098 int
2099 2099 ctf_add_label(ctf_file_t *fp, const char *name, ctf_id_t type, uint_t position)
2100 2100 {
2101 2101 ctf_file_t *fpd;
2102 2102 ctf_dldef_t *dld;
2103 2103
2104 2104 if (name == NULL)
2105 2105 return (ctf_set_errno(fp, EINVAL));
2106 2106
2107 2107 if (!(fp->ctf_flags & LCTF_RDWR))
2108 2108 return (ctf_set_errno(fp, ECTF_RDONLY));
2109 2109
2110 2110 fpd = fp;
2111 2111 if (type != 0 && ctf_lookup_by_id(&fpd, type) == NULL)
2112 2112 return (CTF_ERR); /* errno is set for us */
2113 2113
2114 2114 if (type != 0 && (fp->ctf_flags & LCTF_CHILD) &&
2115 2115 CTF_TYPE_ISPARENT(type))
2116 2116 return (ctf_set_errno(fp, ECTF_NOPARENT));
2117 2117
2118 2118 if (ctf_dld_lookup(fp, name) != NULL)
↓ open down ↓ |
2080 lines elided |
↑ open up ↑ |
2119 2119 return (ctf_set_errno(fp, ECTF_LABELEXISTS));
2120 2120
2121 2121 if ((dld = ctf_alloc(sizeof (ctf_dldef_t))) == NULL)
2122 2122 return (ctf_set_errno(fp, EAGAIN));
2123 2123
2124 2124 if ((dld->dld_name = ctf_strdup(name)) == NULL) {
2125 2125 ctf_free(dld, sizeof (ctf_dldef_t));
2126 2126 return (ctf_set_errno(fp, EAGAIN));
2127 2127 }
2128 2128
2129 + ctf_dprintf("adding label %s, %ld\n", name, type);
2129 2130 dld->dld_type = type;
2130 2131 fp->ctf_dtstrlen += strlen(name) + 1;
2131 2132 ctf_dld_insert(fp, dld, position);
2132 2133 fp->ctf_flags |= LCTF_DIRTY;
2133 2134
2134 2135 return (0);
2135 2136 }
2136 2137
2137 2138 /*
2138 2139 * Update the size of a structure or union. Note that we don't allow this to
2139 2140 * shrink the size of a struct or union, only to increase it. This is useful for
2140 2141 * cases when you have a structure whose actual size is larger than the sum of
2141 2142 * its members due to padding for natural alignment.
2142 2143 */
2143 2144 int
2144 2145 ctf_set_size(ctf_file_t *fp, ctf_id_t id, const ulong_t newsz)
2145 2146 {
2146 2147 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id);
2147 2148 uint_t kind;
2148 2149 size_t oldsz;
2149 2150
2150 2151 if (!(fp->ctf_flags & LCTF_RDWR))
2151 2152 return (ctf_set_errno(fp, ECTF_RDONLY));
2152 2153
2153 2154 if (dtd == NULL)
2154 2155 return (ctf_set_errno(fp, ECTF_BADID));
2155 2156
2156 2157 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2157 2158
2158 2159 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
2159 2160 return (ctf_set_errno(fp, ECTF_NOTSOU));
2160 2161
2161 2162 if ((oldsz = dtd->dtd_data.ctt_size) == CTF_LSIZE_SENT)
2162 2163 oldsz = CTF_TYPE_LSIZE(&dtd->dtd_data);
2163 2164
2164 2165 if (newsz < oldsz)
2165 2166 return (ctf_set_errno(fp, EINVAL));
2166 2167
2167 2168 if (newsz > CTF_MAX_SIZE) {
2168 2169 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
2169 2170 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(newsz);
2170 2171 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(newsz);
2171 2172 } else {
2172 2173 dtd->dtd_data.ctt_size = (ushort_t)newsz;
2173 2174 }
2174 2175
2175 2176 fp->ctf_flags |= LCTF_DIRTY;
2176 2177 return (0);
2177 2178 }
2178 2179
2179 2180 int
2180 2181 ctf_set_root(ctf_file_t *fp, ctf_id_t id, const boolean_t vis)
2181 2182 {
2182 2183 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id);
2183 2184 uint_t kind, vlen;
2184 2185
2185 2186 if (!(fp->ctf_flags & LCTF_RDWR))
2186 2187 return (ctf_set_errno(fp, ECTF_RDONLY));
2187 2188
2188 2189 if (dtd == NULL)
2189 2190 return (ctf_set_errno(fp, ECTF_BADID));
2190 2191
2191 2192 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2192 2193 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
2193 2194
2194 2195 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, vis, vlen);
2195 2196 return (0);
2196 2197 }
↓ open down ↓ |
58 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX