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