17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 22e4586ebfSmws 237c478bd9Sstevel@tonic-gate /* 24e4586ebfSmws * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 270a47c91cSRobert Mustacchi /* 28*7fd79137SRobert Mustacchi * Copyright (c) 2015, Joyent, Inc. 290a47c91cSRobert Mustacchi */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 327c478bd9Sstevel@tonic-gate #include <sys/param.h> 337c478bd9Sstevel@tonic-gate #include <sys/mman.h> 347c478bd9Sstevel@tonic-gate #include <ctf_impl.h> 350a47c91cSRobert Mustacchi #include <sys/debug.h> 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * This static string is used as the template for initially populating a 397c478bd9Sstevel@tonic-gate * dynamic container's string table. We always store \0 in the first byte, 407c478bd9Sstevel@tonic-gate * and we use the generic string "PARENT" to mark this container's parent 417c478bd9Sstevel@tonic-gate * if one is associated with the container using ctf_import(). 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT"; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate /* 467c478bd9Sstevel@tonic-gate * To create an empty CTF container, we just declare a zeroed header and call 477c478bd9Sstevel@tonic-gate * ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w 487c478bd9Sstevel@tonic-gate * and initialize the dynamic members. We set dtstrlen to 1 to reserve the 497c478bd9Sstevel@tonic-gate * first byte of the string table for a \0 byte, and we start assigning type 507c478bd9Sstevel@tonic-gate * IDs at 1 because type ID 0 is used as a sentinel. 517c478bd9Sstevel@tonic-gate */ 527c478bd9Sstevel@tonic-gate ctf_file_t * 537c478bd9Sstevel@tonic-gate ctf_create(int *errp) 547c478bd9Sstevel@tonic-gate { 557c478bd9Sstevel@tonic-gate static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } }; 567c478bd9Sstevel@tonic-gate 57e4586ebfSmws const ulong_t hashlen = 128; 58e4586ebfSmws ctf_dtdef_t **hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *)); 597c478bd9Sstevel@tonic-gate ctf_sect_t cts; 607c478bd9Sstevel@tonic-gate ctf_file_t *fp; 617c478bd9Sstevel@tonic-gate 62e4586ebfSmws if (hash == NULL) 63e4586ebfSmws return (ctf_set_open_errno(errp, EAGAIN)); 64e4586ebfSmws 657c478bd9Sstevel@tonic-gate cts.cts_name = _CTF_SECTION; 667c478bd9Sstevel@tonic-gate cts.cts_type = SHT_PROGBITS; 677c478bd9Sstevel@tonic-gate cts.cts_flags = 0; 687c478bd9Sstevel@tonic-gate cts.cts_data = &hdr; 697c478bd9Sstevel@tonic-gate cts.cts_size = sizeof (hdr); 707c478bd9Sstevel@tonic-gate cts.cts_entsize = 1; 717c478bd9Sstevel@tonic-gate cts.cts_offset = 0; 727c478bd9Sstevel@tonic-gate 73e4586ebfSmws if ((fp = ctf_bufopen(&cts, NULL, NULL, errp)) == NULL) { 74e4586ebfSmws ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *)); 75e4586ebfSmws return (NULL); 76e4586ebfSmws } 77e4586ebfSmws 787c478bd9Sstevel@tonic-gate fp->ctf_flags |= LCTF_RDWR; 79e4586ebfSmws fp->ctf_dthashlen = hashlen; 80e4586ebfSmws bzero(hash, hashlen * sizeof (ctf_dtdef_t *)); 81e4586ebfSmws fp->ctf_dthash = hash; 827c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE); 837c478bd9Sstevel@tonic-gate fp->ctf_dtnextid = 1; 847c478bd9Sstevel@tonic-gate fp->ctf_dtoldid = 0; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate return (fp); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 89*7fd79137SRobert Mustacchi ctf_file_t * 90*7fd79137SRobert Mustacchi ctf_fdcreate(int fd, int *errp) 91*7fd79137SRobert Mustacchi { 92*7fd79137SRobert Mustacchi ctf_file_t *fp; 93*7fd79137SRobert Mustacchi static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } }; 94*7fd79137SRobert Mustacchi 95*7fd79137SRobert Mustacchi const ulong_t hashlen = 128; 96*7fd79137SRobert Mustacchi ctf_dtdef_t **hash; 97*7fd79137SRobert Mustacchi ctf_sect_t cts; 98*7fd79137SRobert Mustacchi 99*7fd79137SRobert Mustacchi if (fd == -1) 100*7fd79137SRobert Mustacchi return (ctf_create(errp)); 101*7fd79137SRobert Mustacchi 102*7fd79137SRobert Mustacchi hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *)); 103*7fd79137SRobert Mustacchi 104*7fd79137SRobert Mustacchi if (hash == NULL) 105*7fd79137SRobert Mustacchi return (ctf_set_open_errno(errp, EAGAIN)); 106*7fd79137SRobert Mustacchi 107*7fd79137SRobert Mustacchi cts.cts_name = _CTF_SECTION; 108*7fd79137SRobert Mustacchi cts.cts_type = SHT_PROGBITS; 109*7fd79137SRobert Mustacchi cts.cts_flags = 0; 110*7fd79137SRobert Mustacchi cts.cts_data = &hdr; 111*7fd79137SRobert Mustacchi cts.cts_size = sizeof (hdr); 112*7fd79137SRobert Mustacchi cts.cts_entsize = 1; 113*7fd79137SRobert Mustacchi cts.cts_offset = 0; 114*7fd79137SRobert Mustacchi 115*7fd79137SRobert Mustacchi if ((fp = ctf_fdcreate_int(fd, errp, &cts)) == NULL) { 116*7fd79137SRobert Mustacchi ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *)); 117*7fd79137SRobert Mustacchi return (NULL); 118*7fd79137SRobert Mustacchi } 119*7fd79137SRobert Mustacchi 120*7fd79137SRobert Mustacchi fp->ctf_flags |= LCTF_RDWR; 121*7fd79137SRobert Mustacchi fp->ctf_dthashlen = hashlen; 122*7fd79137SRobert Mustacchi bzero(hash, hashlen * sizeof (ctf_dtdef_t *)); 123*7fd79137SRobert Mustacchi fp->ctf_dthash = hash; 124*7fd79137SRobert Mustacchi fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE); 125*7fd79137SRobert Mustacchi fp->ctf_dtnextid = 1; 126*7fd79137SRobert Mustacchi fp->ctf_dtoldid = 0; 127*7fd79137SRobert Mustacchi 128*7fd79137SRobert Mustacchi return (fp); 129*7fd79137SRobert Mustacchi } 130*7fd79137SRobert Mustacchi 1317c478bd9Sstevel@tonic-gate static uchar_t * 1327c478bd9Sstevel@tonic-gate ctf_copy_smembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1357c478bd9Sstevel@tonic-gate ctf_member_t ctm; 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 1387c478bd9Sstevel@tonic-gate if (dmd->dmd_name) { 1397c478bd9Sstevel@tonic-gate ctm.ctm_name = soff; 1407c478bd9Sstevel@tonic-gate soff += strlen(dmd->dmd_name) + 1; 1417c478bd9Sstevel@tonic-gate } else 1427c478bd9Sstevel@tonic-gate ctm.ctm_name = 0; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate ctm.ctm_type = (ushort_t)dmd->dmd_type; 1457c478bd9Sstevel@tonic-gate ctm.ctm_offset = (ushort_t)dmd->dmd_offset; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate bcopy(&ctm, t, sizeof (ctm)); 1487c478bd9Sstevel@tonic-gate t += sizeof (ctm); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate return (t); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate static uchar_t * 1557c478bd9Sstevel@tonic-gate ctf_copy_lmembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t) 1567c478bd9Sstevel@tonic-gate { 1577c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1587c478bd9Sstevel@tonic-gate ctf_lmember_t ctlm; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 1617c478bd9Sstevel@tonic-gate if (dmd->dmd_name) { 1627c478bd9Sstevel@tonic-gate ctlm.ctlm_name = soff; 1637c478bd9Sstevel@tonic-gate soff += strlen(dmd->dmd_name) + 1; 1647c478bd9Sstevel@tonic-gate } else 1657c478bd9Sstevel@tonic-gate ctlm.ctlm_name = 0; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate ctlm.ctlm_type = (ushort_t)dmd->dmd_type; 1687c478bd9Sstevel@tonic-gate ctlm.ctlm_pad = 0; 1697c478bd9Sstevel@tonic-gate ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset); 1707c478bd9Sstevel@tonic-gate ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset); 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate bcopy(&ctlm, t, sizeof (ctlm)); 1737c478bd9Sstevel@tonic-gate t += sizeof (ctlm); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate return (t); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate static uchar_t * 1807c478bd9Sstevel@tonic-gate ctf_copy_emembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t) 1817c478bd9Sstevel@tonic-gate { 1827c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1837c478bd9Sstevel@tonic-gate ctf_enum_t cte; 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 1867c478bd9Sstevel@tonic-gate cte.cte_name = soff; 1877c478bd9Sstevel@tonic-gate cte.cte_value = dmd->dmd_value; 1887c478bd9Sstevel@tonic-gate soff += strlen(dmd->dmd_name) + 1; 1897c478bd9Sstevel@tonic-gate bcopy(&cte, t, sizeof (cte)); 1907c478bd9Sstevel@tonic-gate t += sizeof (cte); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate return (t); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate static uchar_t * 1977c478bd9Sstevel@tonic-gate ctf_copy_membnames(ctf_dtdef_t *dtd, uchar_t *s) 1987c478bd9Sstevel@tonic-gate { 1997c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 2007c478bd9Sstevel@tonic-gate size_t len; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 2037c478bd9Sstevel@tonic-gate if (dmd->dmd_name == NULL) 2047c478bd9Sstevel@tonic-gate continue; /* skip anonymous members */ 2057c478bd9Sstevel@tonic-gate len = strlen(dmd->dmd_name) + 1; 2067c478bd9Sstevel@tonic-gate bcopy(dmd->dmd_name, s, len); 2077c478bd9Sstevel@tonic-gate s += len; 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate return (s); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* 2140a47c91cSRobert Mustacchi * Only types of dyanmic CTF containers contain reference counts. These 2150a47c91cSRobert Mustacchi * containers are marked RD/WR. Because of that we basically make this a no-op 2160a47c91cSRobert Mustacchi * for compatability with non-dynamic CTF sections. This is also a no-op for 2170a47c91cSRobert Mustacchi * types which are not dynamic types. It is the responsibility of the caller to 2180a47c91cSRobert Mustacchi * make sure it is a valid type. We help that caller out on debug builds. 2190a47c91cSRobert Mustacchi * 2200a47c91cSRobert Mustacchi * Note that the reference counts are not maintained for types that are not 2210a47c91cSRobert Mustacchi * within this container. In other words if we have a type in a parent, that 2220a47c91cSRobert Mustacchi * will not have its reference count increased. On the flip side, the parent 2230a47c91cSRobert Mustacchi * will not be allowed to remove dynamic types if it has children. 2240a47c91cSRobert Mustacchi */ 2250a47c91cSRobert Mustacchi static void 2260a47c91cSRobert Mustacchi ctf_ref_inc(ctf_file_t *fp, ctf_id_t tid) 2270a47c91cSRobert Mustacchi { 2280a47c91cSRobert Mustacchi ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid); 2290a47c91cSRobert Mustacchi 2300a47c91cSRobert Mustacchi if (dtd == NULL) 2310a47c91cSRobert Mustacchi return; 2320a47c91cSRobert Mustacchi 2330a47c91cSRobert Mustacchi if (!(fp->ctf_flags & LCTF_RDWR)) 2340a47c91cSRobert Mustacchi return; 2350a47c91cSRobert Mustacchi 2360a47c91cSRobert Mustacchi dtd->dtd_ref++; 2370a47c91cSRobert Mustacchi } 2380a47c91cSRobert Mustacchi 2390a47c91cSRobert Mustacchi /* 2400a47c91cSRobert Mustacchi * Just as with ctf_ref_inc, this is a no-op on non-writeable containers and the 2410a47c91cSRobert Mustacchi * caller should ensure that this is already a valid type. 2420a47c91cSRobert Mustacchi */ 2430a47c91cSRobert Mustacchi static void 2440a47c91cSRobert Mustacchi ctf_ref_dec(ctf_file_t *fp, ctf_id_t tid) 2450a47c91cSRobert Mustacchi { 2460a47c91cSRobert Mustacchi ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid); 2470a47c91cSRobert Mustacchi 2480a47c91cSRobert Mustacchi if (dtd == NULL) 2490a47c91cSRobert Mustacchi return; 2500a47c91cSRobert Mustacchi 2510a47c91cSRobert Mustacchi if (!(fp->ctf_flags & LCTF_RDWR)) 2520a47c91cSRobert Mustacchi return; 2530a47c91cSRobert Mustacchi 2540a47c91cSRobert Mustacchi ASSERT(dtd->dtd_ref >= 1); 2550a47c91cSRobert Mustacchi dtd->dtd_ref--; 2560a47c91cSRobert Mustacchi } 2570a47c91cSRobert Mustacchi 2580a47c91cSRobert Mustacchi /* 2597c478bd9Sstevel@tonic-gate * If the specified CTF container is writable and has been modified, reload 2607c478bd9Sstevel@tonic-gate * this container with the updated type definitions. In order to make this 2617c478bd9Sstevel@tonic-gate * code and the rest of libctf as simple as possible, we perform updates by 2627c478bd9Sstevel@tonic-gate * taking the dynamic type definitions and creating an in-memory CTF file 2637c478bd9Sstevel@tonic-gate * containing the definitions, and then call ctf_bufopen() on it. This not 2647c478bd9Sstevel@tonic-gate * only leverages ctf_bufopen(), but also avoids having to bifurcate the rest 2657c478bd9Sstevel@tonic-gate * of the library code with different lookup paths for static and dynamic 2667c478bd9Sstevel@tonic-gate * type definitions. We are therefore optimizing greatly for lookup over 2677c478bd9Sstevel@tonic-gate * update, which we assume will be an uncommon operation. We perform one 2687c478bd9Sstevel@tonic-gate * extra trick here for the benefit of callers and to keep our code simple: 2697c478bd9Sstevel@tonic-gate * ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp 2707c478bd9Sstevel@tonic-gate * constant for the caller, so after ctf_bufopen() returns, we use bcopy to 2717c478bd9Sstevel@tonic-gate * swap the interior of the old and new ctf_file_t's, and then free the old. 2720a47c91cSRobert Mustacchi * 2730a47c91cSRobert Mustacchi * Note that the lists of dynamic types stays around and the resulting container 2740a47c91cSRobert Mustacchi * is still writeable. Furthermore, the reference counts that are on the dtd's 2750a47c91cSRobert Mustacchi * are still valid. 2767c478bd9Sstevel@tonic-gate */ 2777c478bd9Sstevel@tonic-gate int 2787c478bd9Sstevel@tonic-gate ctf_update(ctf_file_t *fp) 2797c478bd9Sstevel@tonic-gate { 2807c478bd9Sstevel@tonic-gate ctf_file_t ofp, *nfp; 281*7fd79137SRobert Mustacchi ctf_header_t hdr, *bhdr; 2827c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 283*7fd79137SRobert Mustacchi ctf_dsdef_t *dsd; 284*7fd79137SRobert Mustacchi ctf_dldef_t *dld; 285*7fd79137SRobert Mustacchi ctf_sect_t cts, *symp, *strp; 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate uchar_t *s, *s0, *t; 288*7fd79137SRobert Mustacchi ctf_lblent_t *label; 289*7fd79137SRobert Mustacchi uint16_t *obj, *func; 290*7fd79137SRobert Mustacchi size_t size, objsize, funcsize, labelsize, plen; 2917c478bd9Sstevel@tonic-gate void *buf; 2927c478bd9Sstevel@tonic-gate int err; 293*7fd79137SRobert Mustacchi ulong_t i; 294*7fd79137SRobert Mustacchi const char *plabel; 295*7fd79137SRobert Mustacchi const char *sname; 296*7fd79137SRobert Mustacchi 297*7fd79137SRobert Mustacchi uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data; 298*7fd79137SRobert Mustacchi uintptr_t strbase = (uintptr_t)fp->ctf_strtab.cts_data; 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_RDWR)) 3017c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_RDONLY)); 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_DIRTY)) 3047c478bd9Sstevel@tonic-gate return (0); /* no update required */ 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate /* 3077c478bd9Sstevel@tonic-gate * Fill in an initial CTF header. We will leave the label, object, 3087c478bd9Sstevel@tonic-gate * and function sections empty and only output a header, type section, 3097c478bd9Sstevel@tonic-gate * and string table. The type section begins at a 4-byte aligned 3107c478bd9Sstevel@tonic-gate * boundary past the CTF header itself (at relative offset zero). 3117c478bd9Sstevel@tonic-gate */ 3127c478bd9Sstevel@tonic-gate bzero(&hdr, sizeof (hdr)); 3137c478bd9Sstevel@tonic-gate hdr.cth_magic = CTF_MAGIC; 3147c478bd9Sstevel@tonic-gate hdr.cth_version = CTF_VERSION; 3157c478bd9Sstevel@tonic-gate 316*7fd79137SRobert Mustacchi if (fp->ctf_flags & LCTF_CHILD) { 317*7fd79137SRobert Mustacchi if (fp->ctf_parname == NULL) { 318*7fd79137SRobert Mustacchi plen = 0; 3197c478bd9Sstevel@tonic-gate hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */ 320*7fd79137SRobert Mustacchi plabel = NULL; 321*7fd79137SRobert Mustacchi } else { 322*7fd79137SRobert Mustacchi plen = strlen(fp->ctf_parname) + 1; 323*7fd79137SRobert Mustacchi plabel = ctf_label_topmost(fp->ctf_parent); 324*7fd79137SRobert Mustacchi } 325*7fd79137SRobert Mustacchi } else { 326*7fd79137SRobert Mustacchi plabel = NULL; 327*7fd79137SRobert Mustacchi plen = 0; 328*7fd79137SRobert Mustacchi } 329*7fd79137SRobert Mustacchi 330*7fd79137SRobert Mustacchi /* 331*7fd79137SRobert Mustacchi * Iterate over the labels that we have. 332*7fd79137SRobert Mustacchi */ 333*7fd79137SRobert Mustacchi for (labelsize = 0, dld = ctf_list_next(&fp->ctf_dldefs); 334*7fd79137SRobert Mustacchi dld != NULL; dld = ctf_list_next(dld)) 335*7fd79137SRobert Mustacchi labelsize += sizeof (ctf_lblent_t); 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate /* 3387c478bd9Sstevel@tonic-gate * Iterate through the dynamic type definition list and compute the 3397c478bd9Sstevel@tonic-gate * size of the CTF type section we will need to generate. 3407c478bd9Sstevel@tonic-gate */ 3417c478bd9Sstevel@tonic-gate for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs); 3427c478bd9Sstevel@tonic-gate dtd != NULL; dtd = ctf_list_next(dtd)) { 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 3457c478bd9Sstevel@tonic-gate uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT) 3487c478bd9Sstevel@tonic-gate size += sizeof (ctf_stype_t); 3497c478bd9Sstevel@tonic-gate else 3507c478bd9Sstevel@tonic-gate size += sizeof (ctf_type_t); 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate switch (kind) { 3537c478bd9Sstevel@tonic-gate case CTF_K_INTEGER: 3547c478bd9Sstevel@tonic-gate case CTF_K_FLOAT: 3557c478bd9Sstevel@tonic-gate size += sizeof (uint_t); 3567c478bd9Sstevel@tonic-gate break; 3577c478bd9Sstevel@tonic-gate case CTF_K_ARRAY: 3587c478bd9Sstevel@tonic-gate size += sizeof (ctf_array_t); 3597c478bd9Sstevel@tonic-gate break; 3607c478bd9Sstevel@tonic-gate case CTF_K_FUNCTION: 3617c478bd9Sstevel@tonic-gate size += sizeof (ushort_t) * (vlen + (vlen & 1)); 3627c478bd9Sstevel@tonic-gate break; 3637c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 3647c478bd9Sstevel@tonic-gate case CTF_K_UNION: 3657c478bd9Sstevel@tonic-gate if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH) 3667c478bd9Sstevel@tonic-gate size += sizeof (ctf_member_t) * vlen; 3677c478bd9Sstevel@tonic-gate else 3687c478bd9Sstevel@tonic-gate size += sizeof (ctf_lmember_t) * vlen; 3697c478bd9Sstevel@tonic-gate break; 3707c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 3717c478bd9Sstevel@tonic-gate size += sizeof (ctf_enum_t) * vlen; 3727c478bd9Sstevel@tonic-gate break; 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate /* 377*7fd79137SRobert Mustacchi * An entry for each object must exist in the data section. However, if 378*7fd79137SRobert Mustacchi * the symbol is SHN_UNDEF, then it is skipped. For objects, the storage 379*7fd79137SRobert Mustacchi * is just the size of the 2-byte id. For functions it's always 2 bytes, 380*7fd79137SRobert Mustacchi * plus 2 bytes per argument and the return type. 381*7fd79137SRobert Mustacchi */ 382*7fd79137SRobert Mustacchi dsd = ctf_list_next(&fp->ctf_dsdefs); 383*7fd79137SRobert Mustacchi for (objsize = 0, funcsize = 0, i = 0; i < fp->ctf_nsyms; i++) { 384*7fd79137SRobert Mustacchi int type; 385*7fd79137SRobert Mustacchi 386*7fd79137SRobert Mustacchi if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) { 387*7fd79137SRobert Mustacchi const Elf32_Sym *symp = (Elf32_Sym *)symbase + i; 388*7fd79137SRobert Mustacchi 389*7fd79137SRobert Mustacchi type = ELF32_ST_TYPE(symp->st_info); 390*7fd79137SRobert Mustacchi if (ctf_sym_valid(strbase, type, symp->st_shndx, 391*7fd79137SRobert Mustacchi symp->st_value, symp->st_name) == B_FALSE) 392*7fd79137SRobert Mustacchi continue; 393*7fd79137SRobert Mustacchi } else { 394*7fd79137SRobert Mustacchi const Elf64_Sym *symp = (Elf64_Sym *)symbase + i; 395*7fd79137SRobert Mustacchi 396*7fd79137SRobert Mustacchi type = ELF64_ST_TYPE(symp->st_info); 397*7fd79137SRobert Mustacchi if (ctf_sym_valid(strbase, type, symp->st_shndx, 398*7fd79137SRobert Mustacchi symp->st_value, symp->st_name) == B_FALSE) 399*7fd79137SRobert Mustacchi continue; 400*7fd79137SRobert Mustacchi } 401*7fd79137SRobert Mustacchi 402*7fd79137SRobert Mustacchi while (dsd != NULL && i > dsd->dsd_symidx) 403*7fd79137SRobert Mustacchi dsd = ctf_list_next(dsd); 404*7fd79137SRobert Mustacchi if (type == STT_OBJECT) { 405*7fd79137SRobert Mustacchi objsize += sizeof (uint16_t); 406*7fd79137SRobert Mustacchi } else { 407*7fd79137SRobert Mustacchi /* Every function has a uint16_t info no matter what */ 408*7fd79137SRobert Mustacchi if (dsd == NULL || i < dsd->dsd_symidx) { 409*7fd79137SRobert Mustacchi funcsize += sizeof (uint16_t); 410*7fd79137SRobert Mustacchi } else { 411*7fd79137SRobert Mustacchi funcsize += sizeof (uint16_t) * 412*7fd79137SRobert Mustacchi (dsd->dsd_nargs + 2); 413*7fd79137SRobert Mustacchi } 414*7fd79137SRobert Mustacchi } 415*7fd79137SRobert Mustacchi } 416*7fd79137SRobert Mustacchi 417*7fd79137SRobert Mustacchi /* 418*7fd79137SRobert Mustacchi * The objtoff and funcoffset must be 2-byte aligned. We're guaranteed 419*7fd79137SRobert Mustacchi * that this is always true for the objtoff because labels are always 8 420*7fd79137SRobert Mustacchi * bytes large. Similarly, because objects are always two bytes of data, 421*7fd79137SRobert Mustacchi * this will always be true for funcoff. 422*7fd79137SRobert Mustacchi */ 423*7fd79137SRobert Mustacchi hdr.cth_objtoff = hdr.cth_lbloff + labelsize; 424*7fd79137SRobert Mustacchi hdr.cth_funcoff = hdr.cth_objtoff + objsize; 425*7fd79137SRobert Mustacchi 426*7fd79137SRobert Mustacchi /* 427*7fd79137SRobert Mustacchi * The type offset must be 4 byte aligned. 428*7fd79137SRobert Mustacchi */ 429*7fd79137SRobert Mustacchi hdr.cth_typeoff = hdr.cth_funcoff + funcsize; 430*7fd79137SRobert Mustacchi if (hdr.cth_typeoff & 3) 431*7fd79137SRobert Mustacchi hdr.cth_typeoff += 4 - (hdr.cth_typeoff & 3); 432*7fd79137SRobert Mustacchi ASSERT((hdr.cth_typeoff & 3) == 0); 433*7fd79137SRobert Mustacchi 434*7fd79137SRobert Mustacchi /* 4357c478bd9Sstevel@tonic-gate * Fill in the string table offset and size, compute the size of the 4367c478bd9Sstevel@tonic-gate * entire CTF buffer we need, and then allocate a new buffer and 4377c478bd9Sstevel@tonic-gate * bcopy the finished header to the start of the buffer. 4387c478bd9Sstevel@tonic-gate */ 4397c478bd9Sstevel@tonic-gate hdr.cth_stroff = hdr.cth_typeoff + size; 440*7fd79137SRobert Mustacchi hdr.cth_strlen = fp->ctf_dtstrlen + plen; 4417c478bd9Sstevel@tonic-gate size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen; 442*7fd79137SRobert Mustacchi ctf_dprintf("lbloff: %u\nobjtoff: %u\nfuncoff: %u\n" 443*7fd79137SRobert Mustacchi "typeoff: %u\nstroff: %u\nstrlen: %u\n", 444*7fd79137SRobert Mustacchi hdr.cth_lbloff, hdr.cth_objtoff, hdr.cth_funcoff, 445*7fd79137SRobert Mustacchi hdr.cth_typeoff, hdr.cth_stroff, hdr.cth_strlen); 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate if ((buf = ctf_data_alloc(size)) == MAP_FAILED) 4487c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate bcopy(&hdr, buf, sizeof (ctf_header_t)); 451*7fd79137SRobert Mustacchi bhdr = buf; 452*7fd79137SRobert Mustacchi label = (ctf_lblent_t *)((uintptr_t)buf + sizeof (ctf_header_t)); 453*7fd79137SRobert Mustacchi t = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_typeoff; 4547c478bd9Sstevel@tonic-gate s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff; 455*7fd79137SRobert Mustacchi obj = (uint16_t *)((uintptr_t)buf + sizeof (ctf_header_t) + 456*7fd79137SRobert Mustacchi hdr.cth_objtoff); 457*7fd79137SRobert Mustacchi func = (uint16_t *)((uintptr_t)buf + sizeof (ctf_header_t) + 458*7fd79137SRobert Mustacchi hdr.cth_funcoff); 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE)); 4617c478bd9Sstevel@tonic-gate s += sizeof (_CTF_STRTAB_TEMPLATE); 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate /* 464*7fd79137SRobert Mustacchi * We have an actual parent name and we're a child container, therefore 465*7fd79137SRobert Mustacchi * we should make sure to note our parent's name here. 466*7fd79137SRobert Mustacchi */ 467*7fd79137SRobert Mustacchi if (plen != 0) { 468*7fd79137SRobert Mustacchi VERIFY(s + plen - s0 <= hdr.cth_strlen); 469*7fd79137SRobert Mustacchi bcopy(fp->ctf_parname, s, plen); 470*7fd79137SRobert Mustacchi bhdr->cth_parname = s - s0; 471*7fd79137SRobert Mustacchi s += plen; 472*7fd79137SRobert Mustacchi } 473*7fd79137SRobert Mustacchi 474*7fd79137SRobert Mustacchi /* 475*7fd79137SRobert Mustacchi * First pass over the labels and copy them out. 476*7fd79137SRobert Mustacchi */ 477*7fd79137SRobert Mustacchi for (dld = ctf_list_next(&fp->ctf_dldefs); dld != NULL; 478*7fd79137SRobert Mustacchi dld = ctf_list_next(dld), label++) { 479*7fd79137SRobert Mustacchi size_t len = strlen(dld->dld_name) + 1; 480*7fd79137SRobert Mustacchi 481*7fd79137SRobert Mustacchi VERIFY(s + len - s0 <= hdr.cth_strlen); 482*7fd79137SRobert Mustacchi bcopy(dld->dld_name, s, len); 483*7fd79137SRobert Mustacchi label->ctl_typeidx = dld->dld_type; 484*7fd79137SRobert Mustacchi label->ctl_label = s - s0; 485*7fd79137SRobert Mustacchi s += len; 486*7fd79137SRobert Mustacchi 487*7fd79137SRobert Mustacchi if (plabel != NULL && strcmp(plabel, dld->dld_name) == 0) 488*7fd79137SRobert Mustacchi bhdr->cth_parlabel = label->ctl_label; 489*7fd79137SRobert Mustacchi } 490*7fd79137SRobert Mustacchi 491*7fd79137SRobert Mustacchi /* 4927c478bd9Sstevel@tonic-gate * We now take a final lap through the dynamic type definition list and 4937c478bd9Sstevel@tonic-gate * copy the appropriate type records and strings to the output buffer. 4947c478bd9Sstevel@tonic-gate */ 4957c478bd9Sstevel@tonic-gate for (dtd = ctf_list_next(&fp->ctf_dtdefs); 4967c478bd9Sstevel@tonic-gate dtd != NULL; dtd = ctf_list_next(dtd)) { 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 4997c478bd9Sstevel@tonic-gate uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate ctf_array_t cta; 5027c478bd9Sstevel@tonic-gate uint_t encoding; 5037c478bd9Sstevel@tonic-gate size_t len; 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate if (dtd->dtd_name != NULL) { 5067c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_name = (uint_t)(s - s0); 5077c478bd9Sstevel@tonic-gate len = strlen(dtd->dtd_name) + 1; 508*7fd79137SRobert Mustacchi VERIFY(s + len - s0 <= hdr.cth_strlen); 5097c478bd9Sstevel@tonic-gate bcopy(dtd->dtd_name, s, len); 5107c478bd9Sstevel@tonic-gate s += len; 5117c478bd9Sstevel@tonic-gate } else 5127c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_name = 0; 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT) 5157c478bd9Sstevel@tonic-gate len = sizeof (ctf_stype_t); 5167c478bd9Sstevel@tonic-gate else 5177c478bd9Sstevel@tonic-gate len = sizeof (ctf_type_t); 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate bcopy(&dtd->dtd_data, t, len); 5207c478bd9Sstevel@tonic-gate t += len; 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate switch (kind) { 5237c478bd9Sstevel@tonic-gate case CTF_K_INTEGER: 5247c478bd9Sstevel@tonic-gate case CTF_K_FLOAT: 5257c478bd9Sstevel@tonic-gate if (kind == CTF_K_INTEGER) { 5267c478bd9Sstevel@tonic-gate encoding = CTF_INT_DATA( 5277c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_format, 5287c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_offset, 5297c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_bits); 5307c478bd9Sstevel@tonic-gate } else { 5317c478bd9Sstevel@tonic-gate encoding = CTF_FP_DATA( 5327c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_format, 5337c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_offset, 5347c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_bits); 5357c478bd9Sstevel@tonic-gate } 5367c478bd9Sstevel@tonic-gate bcopy(&encoding, t, sizeof (encoding)); 5377c478bd9Sstevel@tonic-gate t += sizeof (encoding); 5387c478bd9Sstevel@tonic-gate break; 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate case CTF_K_ARRAY: 5417c478bd9Sstevel@tonic-gate cta.cta_contents = (ushort_t) 5427c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_arr.ctr_contents; 5437c478bd9Sstevel@tonic-gate cta.cta_index = (ushort_t) 5447c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_arr.ctr_index; 5457c478bd9Sstevel@tonic-gate cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems; 5467c478bd9Sstevel@tonic-gate bcopy(&cta, t, sizeof (cta)); 5477c478bd9Sstevel@tonic-gate t += sizeof (cta); 5487c478bd9Sstevel@tonic-gate break; 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate case CTF_K_FUNCTION: { 5517c478bd9Sstevel@tonic-gate ushort_t *argv = (ushort_t *)(uintptr_t)t; 5527c478bd9Sstevel@tonic-gate uint_t argc; 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate for (argc = 0; argc < vlen; argc++) 5557c478bd9Sstevel@tonic-gate *argv++ = (ushort_t)dtd->dtd_u.dtu_argv[argc]; 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate if (vlen & 1) 5587c478bd9Sstevel@tonic-gate *argv++ = 0; /* pad to 4-byte boundary */ 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate t = (uchar_t *)argv; 5617c478bd9Sstevel@tonic-gate break; 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 5657c478bd9Sstevel@tonic-gate case CTF_K_UNION: 5667c478bd9Sstevel@tonic-gate if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH) 5677c478bd9Sstevel@tonic-gate t = ctf_copy_smembers(dtd, (uint_t)(s - s0), t); 5687c478bd9Sstevel@tonic-gate else 5697c478bd9Sstevel@tonic-gate t = ctf_copy_lmembers(dtd, (uint_t)(s - s0), t); 5707c478bd9Sstevel@tonic-gate s = ctf_copy_membnames(dtd, s); 5717c478bd9Sstevel@tonic-gate break; 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 5747c478bd9Sstevel@tonic-gate t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t); 5757c478bd9Sstevel@tonic-gate s = ctf_copy_membnames(dtd, s); 5767c478bd9Sstevel@tonic-gate break; 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate } 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate /* 581*7fd79137SRobert Mustacchi * Now we fill in our dynamic data and function sections. We use the 582*7fd79137SRobert Mustacchi * same criteria as above, but also consult the dsd list. 583*7fd79137SRobert Mustacchi */ 584*7fd79137SRobert Mustacchi dsd = ctf_list_next(&fp->ctf_dsdefs); 585*7fd79137SRobert Mustacchi for (i = 0; i < fp->ctf_nsyms; i++) { 586*7fd79137SRobert Mustacchi int type; 587*7fd79137SRobert Mustacchi if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) { 588*7fd79137SRobert Mustacchi const Elf32_Sym *symp = (Elf32_Sym *)symbase + i; 589*7fd79137SRobert Mustacchi type = ELF32_ST_TYPE(symp->st_info); 590*7fd79137SRobert Mustacchi 591*7fd79137SRobert Mustacchi if (ctf_sym_valid(strbase, type, symp->st_shndx, 592*7fd79137SRobert Mustacchi symp->st_value, symp->st_name) == B_FALSE) 593*7fd79137SRobert Mustacchi continue; 594*7fd79137SRobert Mustacchi } else { 595*7fd79137SRobert Mustacchi const Elf64_Sym *symp = (Elf64_Sym *)symbase + i; 596*7fd79137SRobert Mustacchi type = ELF64_ST_TYPE(symp->st_info); 597*7fd79137SRobert Mustacchi if (ctf_sym_valid(strbase, type, symp->st_shndx, 598*7fd79137SRobert Mustacchi symp->st_value, symp->st_name) == B_FALSE) 599*7fd79137SRobert Mustacchi continue; 600*7fd79137SRobert Mustacchi } 601*7fd79137SRobert Mustacchi 602*7fd79137SRobert Mustacchi while (dsd != NULL && i > dsd->dsd_symidx) { 603*7fd79137SRobert Mustacchi dsd = ctf_list_next(dsd); 604*7fd79137SRobert Mustacchi } 605*7fd79137SRobert Mustacchi if (type == STT_OBJECT) { 606*7fd79137SRobert Mustacchi if (dsd == NULL || i < dsd->dsd_symidx) { 607*7fd79137SRobert Mustacchi *obj = 0; 608*7fd79137SRobert Mustacchi } else { 609*7fd79137SRobert Mustacchi *obj = dsd->dsd_tid; 610*7fd79137SRobert Mustacchi } 611*7fd79137SRobert Mustacchi obj++; 612*7fd79137SRobert Mustacchi VERIFY((uintptr_t)obj <= (uintptr_t)func); 613*7fd79137SRobert Mustacchi } else { 614*7fd79137SRobert Mustacchi if (dsd == NULL || i < dsd->dsd_symidx) { 615*7fd79137SRobert Mustacchi ushort_t data = CTF_TYPE_INFO(CTF_K_UNKNOWN, 616*7fd79137SRobert Mustacchi 0, 0); 617*7fd79137SRobert Mustacchi *func = data; 618*7fd79137SRobert Mustacchi func++; 619*7fd79137SRobert Mustacchi } else { 620*7fd79137SRobert Mustacchi int j; 621*7fd79137SRobert Mustacchi ushort_t data = CTF_TYPE_INFO(CTF_K_FUNCTION, 0, 622*7fd79137SRobert Mustacchi dsd->dsd_nargs); 623*7fd79137SRobert Mustacchi 624*7fd79137SRobert Mustacchi *func = data; 625*7fd79137SRobert Mustacchi func++; 626*7fd79137SRobert Mustacchi *func = dsd->dsd_tid; 627*7fd79137SRobert Mustacchi func++; 628*7fd79137SRobert Mustacchi for (j = 0; j < dsd->dsd_nargs; j++) 629*7fd79137SRobert Mustacchi func[j] = dsd->dsd_argc[j]; 630*7fd79137SRobert Mustacchi func += dsd->dsd_nargs; 631*7fd79137SRobert Mustacchi } 632*7fd79137SRobert Mustacchi } 633*7fd79137SRobert Mustacchi } 634*7fd79137SRobert Mustacchi 635*7fd79137SRobert Mustacchi /* 6367c478bd9Sstevel@tonic-gate * Finally, we are ready to ctf_bufopen() the new container. If this 6377c478bd9Sstevel@tonic-gate * is successful, we then switch nfp and fp and free the old container. 6387c478bd9Sstevel@tonic-gate */ 6397c478bd9Sstevel@tonic-gate ctf_data_protect(buf, size); 6407c478bd9Sstevel@tonic-gate cts.cts_name = _CTF_SECTION; 6417c478bd9Sstevel@tonic-gate cts.cts_type = SHT_PROGBITS; 6427c478bd9Sstevel@tonic-gate cts.cts_flags = 0; 6437c478bd9Sstevel@tonic-gate cts.cts_data = buf; 6447c478bd9Sstevel@tonic-gate cts.cts_size = size; 6457c478bd9Sstevel@tonic-gate cts.cts_entsize = 1; 6467c478bd9Sstevel@tonic-gate cts.cts_offset = 0; 6477c478bd9Sstevel@tonic-gate 648*7fd79137SRobert Mustacchi if (fp->ctf_nsyms == 0) { 649*7fd79137SRobert Mustacchi symp = NULL; 650*7fd79137SRobert Mustacchi strp = NULL; 651*7fd79137SRobert Mustacchi } else { 652*7fd79137SRobert Mustacchi symp = &fp->ctf_symtab; 653*7fd79137SRobert Mustacchi strp = &fp->ctf_strtab; 654*7fd79137SRobert Mustacchi } 655*7fd79137SRobert Mustacchi 656*7fd79137SRobert Mustacchi if ((nfp = ctf_bufopen(&cts, symp, strp, &err)) == NULL) { 6577c478bd9Sstevel@tonic-gate ctf_data_free(buf, size); 6587c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, err)); 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate (void) ctf_setmodel(nfp, ctf_getmodel(fp)); 6627c478bd9Sstevel@tonic-gate (void) ctf_import(nfp, fp->ctf_parent); 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate nfp->ctf_refcnt = fp->ctf_refcnt; 6657c478bd9Sstevel@tonic-gate nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY; 666e4586ebfSmws nfp->ctf_dthash = fp->ctf_dthash; 667e4586ebfSmws nfp->ctf_dthashlen = fp->ctf_dthashlen; 6687c478bd9Sstevel@tonic-gate nfp->ctf_dtdefs = fp->ctf_dtdefs; 669*7fd79137SRobert Mustacchi nfp->ctf_dsdefs = fp->ctf_dsdefs; 670*7fd79137SRobert Mustacchi nfp->ctf_dldefs = fp->ctf_dldefs; 6717c478bd9Sstevel@tonic-gate nfp->ctf_dtstrlen = fp->ctf_dtstrlen; 6727c478bd9Sstevel@tonic-gate nfp->ctf_dtnextid = fp->ctf_dtnextid; 6737c478bd9Sstevel@tonic-gate nfp->ctf_dtoldid = fp->ctf_dtnextid - 1; 6747c478bd9Sstevel@tonic-gate nfp->ctf_specific = fp->ctf_specific; 6757c478bd9Sstevel@tonic-gate 676e4586ebfSmws fp->ctf_dthash = NULL; 677e4586ebfSmws fp->ctf_dthashlen = 0; 6787c478bd9Sstevel@tonic-gate bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t)); 679*7fd79137SRobert Mustacchi bzero(&fp->ctf_dsdefs, sizeof (ctf_list_t)); 680*7fd79137SRobert Mustacchi bzero(&fp->ctf_dldefs, sizeof (ctf_list_t)); 681*7fd79137SRobert Mustacchi 682*7fd79137SRobert Mustacchi /* 683*7fd79137SRobert Mustacchi * Because the various containers share the data sections, we don't want 684*7fd79137SRobert Mustacchi * to have ctf_close free it all. However, the name of the section is in 685*7fd79137SRobert Mustacchi * fact unique to the ctf_sect_t. Thus we save the names of the symbol 686*7fd79137SRobert Mustacchi * and string sections around the bzero() and restore them afterwards, 687*7fd79137SRobert Mustacchi * ensuring that we don't result in a memory leak. 688*7fd79137SRobert Mustacchi */ 689*7fd79137SRobert Mustacchi sname = fp->ctf_symtab.cts_name; 690*7fd79137SRobert Mustacchi bzero(&fp->ctf_symtab, sizeof (ctf_sect_t)); 691*7fd79137SRobert Mustacchi fp->ctf_symtab.cts_name = sname; 692*7fd79137SRobert Mustacchi 693*7fd79137SRobert Mustacchi sname = fp->ctf_strtab.cts_name; 694*7fd79137SRobert Mustacchi bzero(&fp->ctf_strtab, sizeof (ctf_sect_t)); 695*7fd79137SRobert Mustacchi fp->ctf_strtab.cts_name = sname; 696e4586ebfSmws 6977c478bd9Sstevel@tonic-gate bcopy(fp, &ofp, sizeof (ctf_file_t)); 6987c478bd9Sstevel@tonic-gate bcopy(nfp, fp, sizeof (ctf_file_t)); 6997c478bd9Sstevel@tonic-gate bcopy(&ofp, nfp, sizeof (ctf_file_t)); 7007c478bd9Sstevel@tonic-gate 7017c478bd9Sstevel@tonic-gate /* 7027c478bd9Sstevel@tonic-gate * Initialize the ctf_lookup_by_name top-level dictionary. We keep an 7037c478bd9Sstevel@tonic-gate * array of type name prefixes and the corresponding ctf_hash to use. 7047c478bd9Sstevel@tonic-gate * NOTE: This code must be kept in sync with the code in ctf_bufopen(). 7057c478bd9Sstevel@tonic-gate */ 7067c478bd9Sstevel@tonic-gate fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs; 7077c478bd9Sstevel@tonic-gate fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions; 7087c478bd9Sstevel@tonic-gate fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums; 7097c478bd9Sstevel@tonic-gate fp->ctf_lookups[3].ctl_hash = &fp->ctf_names; 7107c478bd9Sstevel@tonic-gate 7117c478bd9Sstevel@tonic-gate nfp->ctf_refcnt = 1; /* force nfp to be freed */ 7127c478bd9Sstevel@tonic-gate ctf_close(nfp); 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate return (0); 7157c478bd9Sstevel@tonic-gate } 7167c478bd9Sstevel@tonic-gate 717e4586ebfSmws void 718e4586ebfSmws ctf_dtd_insert(ctf_file_t *fp, ctf_dtdef_t *dtd) 7197c478bd9Sstevel@tonic-gate { 720e4586ebfSmws ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1); 721e4586ebfSmws 722e4586ebfSmws dtd->dtd_hash = fp->ctf_dthash[h]; 723e4586ebfSmws fp->ctf_dthash[h] = dtd; 724e4586ebfSmws ctf_list_append(&fp->ctf_dtdefs, dtd); 725e4586ebfSmws } 726e4586ebfSmws 727e4586ebfSmws void 728e4586ebfSmws ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd) 729e4586ebfSmws { 730e4586ebfSmws ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1); 731e4586ebfSmws ctf_dtdef_t *p, **q = &fp->ctf_dthash[h]; 7327c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd, *nmd; 7337c478bd9Sstevel@tonic-gate size_t len; 7340a47c91cSRobert Mustacchi int kind, i; 7357c478bd9Sstevel@tonic-gate 736e4586ebfSmws for (p = *q; p != NULL; p = p->dtd_hash) { 737e4586ebfSmws if (p != dtd) 738e4586ebfSmws q = &p->dtd_hash; 739e4586ebfSmws else 740e4586ebfSmws break; 741e4586ebfSmws } 7427c478bd9Sstevel@tonic-gate 743e4586ebfSmws if (p != NULL) 744e4586ebfSmws *q = p->dtd_hash; 7457c478bd9Sstevel@tonic-gate 7460a47c91cSRobert Mustacchi kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 7470a47c91cSRobert Mustacchi switch (kind) { 7487c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 7497c478bd9Sstevel@tonic-gate case CTF_K_UNION: 7507c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 7517c478bd9Sstevel@tonic-gate for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 7527c478bd9Sstevel@tonic-gate dmd != NULL; dmd = nmd) { 7537c478bd9Sstevel@tonic-gate if (dmd->dmd_name != NULL) { 7547c478bd9Sstevel@tonic-gate len = strlen(dmd->dmd_name) + 1; 7557c478bd9Sstevel@tonic-gate ctf_free(dmd->dmd_name, len); 7567c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen -= len; 7577c478bd9Sstevel@tonic-gate } 7580a47c91cSRobert Mustacchi if (kind != CTF_K_ENUM) 7590a47c91cSRobert Mustacchi ctf_ref_dec(fp, dmd->dmd_type); 7607c478bd9Sstevel@tonic-gate nmd = ctf_list_next(dmd); 7617c478bd9Sstevel@tonic-gate ctf_free(dmd, sizeof (ctf_dmdef_t)); 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate break; 7647c478bd9Sstevel@tonic-gate case CTF_K_FUNCTION: 7650a47c91cSRobert Mustacchi ctf_ref_dec(fp, dtd->dtd_data.ctt_type); 7660a47c91cSRobert Mustacchi for (i = 0; i < CTF_INFO_VLEN(dtd->dtd_data.ctt_info); i++) 7670a47c91cSRobert Mustacchi if (dtd->dtd_u.dtu_argv[i] != 0) 7680a47c91cSRobert Mustacchi ctf_ref_dec(fp, dtd->dtd_u.dtu_argv[i]); 7697c478bd9Sstevel@tonic-gate ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) * 7707c478bd9Sstevel@tonic-gate CTF_INFO_VLEN(dtd->dtd_data.ctt_info)); 7717c478bd9Sstevel@tonic-gate break; 7720a47c91cSRobert Mustacchi case CTF_K_ARRAY: 7730a47c91cSRobert Mustacchi ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents); 7740a47c91cSRobert Mustacchi ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index); 7750a47c91cSRobert Mustacchi break; 7760a47c91cSRobert Mustacchi case CTF_K_TYPEDEF: 7770a47c91cSRobert Mustacchi ctf_ref_dec(fp, dtd->dtd_data.ctt_type); 7780a47c91cSRobert Mustacchi break; 7790a47c91cSRobert Mustacchi case CTF_K_POINTER: 7800a47c91cSRobert Mustacchi case CTF_K_VOLATILE: 7810a47c91cSRobert Mustacchi case CTF_K_CONST: 7820a47c91cSRobert Mustacchi case CTF_K_RESTRICT: 7830a47c91cSRobert Mustacchi ctf_ref_dec(fp, dtd->dtd_data.ctt_type); 7840a47c91cSRobert Mustacchi break; 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate 7877c478bd9Sstevel@tonic-gate if (dtd->dtd_name) { 7887c478bd9Sstevel@tonic-gate len = strlen(dtd->dtd_name) + 1; 7897c478bd9Sstevel@tonic-gate ctf_free(dtd->dtd_name, len); 7907c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen -= len; 7917c478bd9Sstevel@tonic-gate } 7927c478bd9Sstevel@tonic-gate 7937c478bd9Sstevel@tonic-gate ctf_list_delete(&fp->ctf_dtdefs, dtd); 7947c478bd9Sstevel@tonic-gate ctf_free(dtd, sizeof (ctf_dtdef_t)); 7957c478bd9Sstevel@tonic-gate } 7967c478bd9Sstevel@tonic-gate 797e4586ebfSmws ctf_dtdef_t * 798e4586ebfSmws ctf_dtd_lookup(ctf_file_t *fp, ctf_id_t type) 799e4586ebfSmws { 800e4586ebfSmws ulong_t h = type & (fp->ctf_dthashlen - 1); 801e4586ebfSmws ctf_dtdef_t *dtd; 802e4586ebfSmws 803e4586ebfSmws if (fp->ctf_dthash == NULL) 804e4586ebfSmws return (NULL); 805e4586ebfSmws 806e4586ebfSmws for (dtd = fp->ctf_dthash[h]; dtd != NULL; dtd = dtd->dtd_hash) { 807e4586ebfSmws if (dtd->dtd_type == type) 808e4586ebfSmws break; 809e4586ebfSmws } 810e4586ebfSmws 811e4586ebfSmws return (dtd); 812e4586ebfSmws } 813e4586ebfSmws 814*7fd79137SRobert Mustacchi ctf_dsdef_t * 815*7fd79137SRobert Mustacchi ctf_dsd_lookup(ctf_file_t *fp, ulong_t idx) 816*7fd79137SRobert Mustacchi { 817*7fd79137SRobert Mustacchi ctf_dsdef_t *dsd; 818*7fd79137SRobert Mustacchi 819*7fd79137SRobert Mustacchi for (dsd = ctf_list_next(&fp->ctf_dsdefs); dsd != NULL; 820*7fd79137SRobert Mustacchi dsd = ctf_list_next(dsd)) { 821*7fd79137SRobert Mustacchi if (dsd->dsd_symidx == idx) 822*7fd79137SRobert Mustacchi return (dsd); 823*7fd79137SRobert Mustacchi } 824*7fd79137SRobert Mustacchi 825*7fd79137SRobert Mustacchi return (NULL); 826*7fd79137SRobert Mustacchi } 827*7fd79137SRobert Mustacchi 828*7fd79137SRobert Mustacchi /* 829*7fd79137SRobert Mustacchi * We order the ctf_dsdef_t by symbol index to make things better for updates. 830*7fd79137SRobert Mustacchi */ 831*7fd79137SRobert Mustacchi void 832*7fd79137SRobert Mustacchi ctf_dsd_insert(ctf_file_t *fp, ctf_dsdef_t *dsd) 833*7fd79137SRobert Mustacchi { 834*7fd79137SRobert Mustacchi ctf_dsdef_t *i; 835*7fd79137SRobert Mustacchi 836*7fd79137SRobert Mustacchi for (i = ctf_list_next(&fp->ctf_dsdefs); i != NULL; 837*7fd79137SRobert Mustacchi i = ctf_list_next(i)) { 838*7fd79137SRobert Mustacchi if (i->dsd_symidx > dsd->dsd_symidx) 839*7fd79137SRobert Mustacchi break; 840*7fd79137SRobert Mustacchi } 841*7fd79137SRobert Mustacchi 842*7fd79137SRobert Mustacchi if (i == NULL) { 843*7fd79137SRobert Mustacchi ctf_list_append(&fp->ctf_dsdefs, dsd); 844*7fd79137SRobert Mustacchi return; 845*7fd79137SRobert Mustacchi } 846*7fd79137SRobert Mustacchi 847*7fd79137SRobert Mustacchi ctf_list_insert_before(&fp->ctf_dsdefs, i, dsd); 848*7fd79137SRobert Mustacchi } 849*7fd79137SRobert Mustacchi 850*7fd79137SRobert Mustacchi /* ARGSUSED */ 851*7fd79137SRobert Mustacchi void 852*7fd79137SRobert Mustacchi ctf_dsd_delete(ctf_file_t *fp, ctf_dsdef_t *dsd) 853*7fd79137SRobert Mustacchi { 854*7fd79137SRobert Mustacchi if (dsd->dsd_nargs > 0) 855*7fd79137SRobert Mustacchi ctf_free(dsd->dsd_argc, 856*7fd79137SRobert Mustacchi sizeof (ctf_id_t) * dsd->dsd_nargs); 857*7fd79137SRobert Mustacchi ctf_list_delete(&fp->ctf_dsdefs, dsd); 858*7fd79137SRobert Mustacchi ctf_free(dsd, sizeof (ctf_dsdef_t)); 859*7fd79137SRobert Mustacchi } 860*7fd79137SRobert Mustacchi 861*7fd79137SRobert Mustacchi ctf_dldef_t * 862*7fd79137SRobert Mustacchi ctf_dld_lookup(ctf_file_t *fp, const char *name) 863*7fd79137SRobert Mustacchi { 864*7fd79137SRobert Mustacchi ctf_dldef_t *dld; 865*7fd79137SRobert Mustacchi 866*7fd79137SRobert Mustacchi for (dld = ctf_list_next(&fp->ctf_dldefs); dld != NULL; 867*7fd79137SRobert Mustacchi dld = ctf_list_next(dld)) { 868*7fd79137SRobert Mustacchi if (strcmp(name, dld->dld_name) == 0) 869*7fd79137SRobert Mustacchi return (dld); 870*7fd79137SRobert Mustacchi } 871*7fd79137SRobert Mustacchi 872*7fd79137SRobert Mustacchi return (NULL); 873*7fd79137SRobert Mustacchi } 874*7fd79137SRobert Mustacchi 875*7fd79137SRobert Mustacchi void 876*7fd79137SRobert Mustacchi ctf_dld_insert(ctf_file_t *fp, ctf_dldef_t *dld, uint_t pos) 877*7fd79137SRobert Mustacchi { 878*7fd79137SRobert Mustacchi ctf_dldef_t *l; 879*7fd79137SRobert Mustacchi 880*7fd79137SRobert Mustacchi if (pos == 0) { 881*7fd79137SRobert Mustacchi ctf_list_prepend(&fp->ctf_dldefs, dld); 882*7fd79137SRobert Mustacchi return; 883*7fd79137SRobert Mustacchi } 884*7fd79137SRobert Mustacchi 885*7fd79137SRobert Mustacchi for (l = ctf_list_next(&fp->ctf_dldefs); pos != 0 && dld != NULL; 886*7fd79137SRobert Mustacchi l = ctf_list_next(l), pos--) 887*7fd79137SRobert Mustacchi ; 888*7fd79137SRobert Mustacchi 889*7fd79137SRobert Mustacchi if (l == NULL) 890*7fd79137SRobert Mustacchi ctf_list_append(&fp->ctf_dldefs, dld); 891*7fd79137SRobert Mustacchi else 892*7fd79137SRobert Mustacchi ctf_list_insert_before(&fp->ctf_dsdefs, l, dld); 893*7fd79137SRobert Mustacchi } 894*7fd79137SRobert Mustacchi 895*7fd79137SRobert Mustacchi void 896*7fd79137SRobert Mustacchi ctf_dld_delete(ctf_file_t *fp, ctf_dldef_t *dld) 897*7fd79137SRobert Mustacchi { 898*7fd79137SRobert Mustacchi ctf_list_delete(&fp->ctf_dldefs, dld); 899*7fd79137SRobert Mustacchi 900*7fd79137SRobert Mustacchi if (dld->dld_name != NULL) { 901*7fd79137SRobert Mustacchi size_t len = strlen(dld->dld_name) + 1; 902*7fd79137SRobert Mustacchi ctf_free(dld->dld_name, len); 903*7fd79137SRobert Mustacchi fp->ctf_dtstrlen -= len; 904*7fd79137SRobert Mustacchi } 905*7fd79137SRobert Mustacchi 906*7fd79137SRobert Mustacchi ctf_free(dld, sizeof (ctf_dldef_t)); 907*7fd79137SRobert Mustacchi } 908*7fd79137SRobert Mustacchi 909e4586ebfSmws /* 910e4586ebfSmws * Discard all of the dynamic type definitions that have been added to the 911e4586ebfSmws * container since the last call to ctf_update(). We locate such types by 912e4586ebfSmws * scanning the list and deleting elements that have type IDs greater than 9130a47c91cSRobert Mustacchi * ctf_dtoldid, which is set by ctf_update(), above. Note that to work properly 9140a47c91cSRobert Mustacchi * with our reference counting schemes, we must delete the dynamic list in 9150a47c91cSRobert Mustacchi * reverse. 916e4586ebfSmws */ 917e4586ebfSmws int 918e4586ebfSmws ctf_discard(ctf_file_t *fp) 919e4586ebfSmws { 920e4586ebfSmws ctf_dtdef_t *dtd, *ntd; 921e4586ebfSmws 922e4586ebfSmws if (!(fp->ctf_flags & LCTF_RDWR)) 923e4586ebfSmws return (ctf_set_errno(fp, ECTF_RDONLY)); 924e4586ebfSmws 925e4586ebfSmws if (!(fp->ctf_flags & LCTF_DIRTY)) 926e4586ebfSmws return (0); /* no update required */ 927e4586ebfSmws 9280a47c91cSRobert Mustacchi for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) { 929*7fd79137SRobert Mustacchi ntd = ctf_list_prev(dtd); 930e4586ebfSmws if (dtd->dtd_type <= fp->ctf_dtoldid) 931e4586ebfSmws continue; /* skip types that have been committed */ 932e4586ebfSmws 933e4586ebfSmws ctf_dtd_delete(fp, dtd); 934e4586ebfSmws } 935e4586ebfSmws 9367c478bd9Sstevel@tonic-gate fp->ctf_dtnextid = fp->ctf_dtoldid + 1; 9377c478bd9Sstevel@tonic-gate fp->ctf_flags &= ~LCTF_DIRTY; 9387c478bd9Sstevel@tonic-gate 9397c478bd9Sstevel@tonic-gate return (0); 9407c478bd9Sstevel@tonic-gate } 9417c478bd9Sstevel@tonic-gate 9427c478bd9Sstevel@tonic-gate static ctf_id_t 9437c478bd9Sstevel@tonic-gate ctf_add_generic(ctf_file_t *fp, uint_t flag, const char *name, ctf_dtdef_t **rp) 9447c478bd9Sstevel@tonic-gate { 9457c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 9467c478bd9Sstevel@tonic-gate ctf_id_t type; 9477c478bd9Sstevel@tonic-gate char *s = NULL; 9487c478bd9Sstevel@tonic-gate 9497c478bd9Sstevel@tonic-gate if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT) 9507c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 9517c478bd9Sstevel@tonic-gate 9527c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_RDWR)) 9537c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_RDONLY)); 9547c478bd9Sstevel@tonic-gate 9557c478bd9Sstevel@tonic-gate if (CTF_INDEX_TO_TYPE(fp->ctf_dtnextid, 1) > CTF_MAX_TYPE) 9567c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_FULL)); 9577c478bd9Sstevel@tonic-gate 9587c478bd9Sstevel@tonic-gate if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL) 9597c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 9607c478bd9Sstevel@tonic-gate 9617c478bd9Sstevel@tonic-gate if (name != NULL && (s = ctf_strdup(name)) == NULL) { 9627c478bd9Sstevel@tonic-gate ctf_free(dtd, sizeof (ctf_dtdef_t)); 9637c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 9647c478bd9Sstevel@tonic-gate } 9657c478bd9Sstevel@tonic-gate 9667c478bd9Sstevel@tonic-gate type = fp->ctf_dtnextid++; 9677c478bd9Sstevel@tonic-gate type = CTF_INDEX_TO_TYPE(type, (fp->ctf_flags & LCTF_CHILD)); 9687c478bd9Sstevel@tonic-gate 9697c478bd9Sstevel@tonic-gate bzero(dtd, sizeof (ctf_dtdef_t)); 9707c478bd9Sstevel@tonic-gate dtd->dtd_name = s; 9717c478bd9Sstevel@tonic-gate dtd->dtd_type = type; 9727c478bd9Sstevel@tonic-gate 9737c478bd9Sstevel@tonic-gate if (s != NULL) 9747c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen += strlen(s) + 1; 9757c478bd9Sstevel@tonic-gate 976e4586ebfSmws ctf_dtd_insert(fp, dtd); 9777c478bd9Sstevel@tonic-gate fp->ctf_flags |= LCTF_DIRTY; 9787c478bd9Sstevel@tonic-gate 9797c478bd9Sstevel@tonic-gate *rp = dtd; 9807c478bd9Sstevel@tonic-gate return (type); 9817c478bd9Sstevel@tonic-gate } 9827c478bd9Sstevel@tonic-gate 983*7fd79137SRobert Mustacchi ctf_id_t 9847c478bd9Sstevel@tonic-gate ctf_add_encoded(ctf_file_t *fp, uint_t flag, 9857c478bd9Sstevel@tonic-gate const char *name, const ctf_encoding_t *ep, uint_t kind) 9867c478bd9Sstevel@tonic-gate { 9877c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 9887c478bd9Sstevel@tonic-gate ctf_id_t type; 9897c478bd9Sstevel@tonic-gate 9907c478bd9Sstevel@tonic-gate if (ep == NULL) 9917c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 9947c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 9957c478bd9Sstevel@tonic-gate 9967c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0); 997*7fd79137SRobert Mustacchi 998*7fd79137SRobert Mustacchi /* 999*7fd79137SRobert Mustacchi * If the type's size is not an even number of bytes, then we should 1000*7fd79137SRobert Mustacchi * round up the type size to the nearest byte. 1001*7fd79137SRobert Mustacchi */ 1002*7fd79137SRobert Mustacchi dtd->dtd_data.ctt_size = ep->cte_bits / NBBY; 1003*7fd79137SRobert Mustacchi if ((ep->cte_bits % NBBY) != 0) 1004*7fd79137SRobert Mustacchi dtd->dtd_data.ctt_size++; 10057c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc = *ep; 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate return (type); 10087c478bd9Sstevel@tonic-gate } 10097c478bd9Sstevel@tonic-gate 1010*7fd79137SRobert Mustacchi ctf_id_t 1011*7fd79137SRobert Mustacchi ctf_add_reftype(ctf_file_t *fp, uint_t flag, 1012*7fd79137SRobert Mustacchi const char *name, ctf_id_t ref, uint_t kind) 10137c478bd9Sstevel@tonic-gate { 10147c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 10157c478bd9Sstevel@tonic-gate ctf_id_t type; 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE) 10187c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 10197c478bd9Sstevel@tonic-gate 1020*7fd79137SRobert Mustacchi if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 10217c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 10227c478bd9Sstevel@tonic-gate 10230a47c91cSRobert Mustacchi ctf_ref_inc(fp, ref); 10240a47c91cSRobert Mustacchi 10257c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0); 10267c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_type = (ushort_t)ref; 10277c478bd9Sstevel@tonic-gate 10287c478bd9Sstevel@tonic-gate return (type); 10297c478bd9Sstevel@tonic-gate } 10307c478bd9Sstevel@tonic-gate 10317c478bd9Sstevel@tonic-gate ctf_id_t 10327c478bd9Sstevel@tonic-gate ctf_add_integer(ctf_file_t *fp, uint_t flag, 10337c478bd9Sstevel@tonic-gate const char *name, const ctf_encoding_t *ep) 10347c478bd9Sstevel@tonic-gate { 10357c478bd9Sstevel@tonic-gate return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER)); 10367c478bd9Sstevel@tonic-gate } 10377c478bd9Sstevel@tonic-gate 10387c478bd9Sstevel@tonic-gate ctf_id_t 10397c478bd9Sstevel@tonic-gate ctf_add_float(ctf_file_t *fp, uint_t flag, 10407c478bd9Sstevel@tonic-gate const char *name, const ctf_encoding_t *ep) 10417c478bd9Sstevel@tonic-gate { 10427c478bd9Sstevel@tonic-gate return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT)); 10437c478bd9Sstevel@tonic-gate } 10447c478bd9Sstevel@tonic-gate 10457c478bd9Sstevel@tonic-gate ctf_id_t 1046*7fd79137SRobert Mustacchi ctf_add_pointer(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 10477c478bd9Sstevel@tonic-gate { 1048*7fd79137SRobert Mustacchi return (ctf_add_reftype(fp, flag, name, ref, CTF_K_POINTER)); 10497c478bd9Sstevel@tonic-gate } 10507c478bd9Sstevel@tonic-gate 10517c478bd9Sstevel@tonic-gate ctf_id_t 10527c478bd9Sstevel@tonic-gate ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp) 10537c478bd9Sstevel@tonic-gate { 10547c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 10557c478bd9Sstevel@tonic-gate ctf_id_t type; 10560a47c91cSRobert Mustacchi ctf_file_t *fpd; 10577c478bd9Sstevel@tonic-gate 10587c478bd9Sstevel@tonic-gate if (arp == NULL) 10597c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 10607c478bd9Sstevel@tonic-gate 10610a47c91cSRobert Mustacchi fpd = fp; 10620a47c91cSRobert Mustacchi if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL && 1063*7fd79137SRobert Mustacchi ctf_dtd_lookup(fp, arp->ctr_contents) == NULL) { 1064*7fd79137SRobert Mustacchi ctf_dprintf("bad contents for array: %ld\n", 1065*7fd79137SRobert Mustacchi arp->ctr_contents); 10660a47c91cSRobert Mustacchi return (ctf_set_errno(fp, ECTF_BADID)); 1067*7fd79137SRobert Mustacchi } 10680a47c91cSRobert Mustacchi 10690a47c91cSRobert Mustacchi fpd = fp; 10700a47c91cSRobert Mustacchi if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL && 1071*7fd79137SRobert Mustacchi ctf_dtd_lookup(fp, arp->ctr_index) == NULL) { 1072*7fd79137SRobert Mustacchi ctf_dprintf("bad index for array: %ld\n", arp->ctr_index); 10730a47c91cSRobert Mustacchi return (ctf_set_errno(fp, ECTF_BADID)); 1074*7fd79137SRobert Mustacchi } 10750a47c91cSRobert Mustacchi 10767c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) 10777c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 10787c478bd9Sstevel@tonic-gate 10797c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, flag, 0); 10807c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = 0; 10817c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_arr = *arp; 10820a47c91cSRobert Mustacchi ctf_ref_inc(fp, arp->ctr_contents); 10830a47c91cSRobert Mustacchi ctf_ref_inc(fp, arp->ctr_index); 10847c478bd9Sstevel@tonic-gate 10857c478bd9Sstevel@tonic-gate return (type); 10867c478bd9Sstevel@tonic-gate } 10877c478bd9Sstevel@tonic-gate 10887c478bd9Sstevel@tonic-gate int 10897c478bd9Sstevel@tonic-gate ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp) 10907c478bd9Sstevel@tonic-gate { 10910a47c91cSRobert Mustacchi ctf_file_t *fpd; 1092e4586ebfSmws ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type); 10937c478bd9Sstevel@tonic-gate 10947c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_RDWR)) 10957c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_RDONLY)); 10967c478bd9Sstevel@tonic-gate 10977c478bd9Sstevel@tonic-gate if (dtd == NULL || CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_ARRAY) 10987c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_BADID)); 10997c478bd9Sstevel@tonic-gate 11000a47c91cSRobert Mustacchi fpd = fp; 11010a47c91cSRobert Mustacchi if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL && 11020a47c91cSRobert Mustacchi ctf_dtd_lookup(fp, arp->ctr_contents) == NULL) 11030a47c91cSRobert Mustacchi return (ctf_set_errno(fp, ECTF_BADID)); 11040a47c91cSRobert Mustacchi 11050a47c91cSRobert Mustacchi fpd = fp; 11060a47c91cSRobert Mustacchi if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL && 11070a47c91cSRobert Mustacchi ctf_dtd_lookup(fp, arp->ctr_index) == NULL) 11080a47c91cSRobert Mustacchi return (ctf_set_errno(fp, ECTF_BADID)); 11090a47c91cSRobert Mustacchi 11100a47c91cSRobert Mustacchi ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents); 11110a47c91cSRobert Mustacchi ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index); 11127c478bd9Sstevel@tonic-gate fp->ctf_flags |= LCTF_DIRTY; 11137c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_arr = *arp; 11140a47c91cSRobert Mustacchi ctf_ref_inc(fp, arp->ctr_contents); 11150a47c91cSRobert Mustacchi ctf_ref_inc(fp, arp->ctr_index); 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate return (0); 11187c478bd9Sstevel@tonic-gate } 11197c478bd9Sstevel@tonic-gate 11207c478bd9Sstevel@tonic-gate ctf_id_t 1121*7fd79137SRobert Mustacchi ctf_add_funcptr(ctf_file_t *fp, uint_t flag, 11227c478bd9Sstevel@tonic-gate const ctf_funcinfo_t *ctc, const ctf_id_t *argv) 11237c478bd9Sstevel@tonic-gate { 11247c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 11257c478bd9Sstevel@tonic-gate ctf_id_t type; 11267c478bd9Sstevel@tonic-gate uint_t vlen; 11270a47c91cSRobert Mustacchi int i; 11287c478bd9Sstevel@tonic-gate ctf_id_t *vdat = NULL; 11290a47c91cSRobert Mustacchi ctf_file_t *fpd; 11307c478bd9Sstevel@tonic-gate 11317c478bd9Sstevel@tonic-gate if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 || 11327c478bd9Sstevel@tonic-gate (ctc->ctc_argc != 0 && argv == NULL)) 11337c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 11347c478bd9Sstevel@tonic-gate 11357c478bd9Sstevel@tonic-gate vlen = ctc->ctc_argc; 11367c478bd9Sstevel@tonic-gate if (ctc->ctc_flags & CTF_FUNC_VARARG) 11377c478bd9Sstevel@tonic-gate vlen++; /* add trailing zero to indicate varargs (see below) */ 11387c478bd9Sstevel@tonic-gate 11397c478bd9Sstevel@tonic-gate if (vlen > CTF_MAX_VLEN) 11407c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EOVERFLOW)); 11417c478bd9Sstevel@tonic-gate 11420a47c91cSRobert Mustacchi fpd = fp; 11430a47c91cSRobert Mustacchi if (ctf_lookup_by_id(&fpd, ctc->ctc_return) == NULL && 11440a47c91cSRobert Mustacchi ctf_dtd_lookup(fp, ctc->ctc_return) == NULL) 11450a47c91cSRobert Mustacchi return (ctf_set_errno(fp, ECTF_BADID)); 11460a47c91cSRobert Mustacchi 11470a47c91cSRobert Mustacchi for (i = 0; i < ctc->ctc_argc; i++) { 11480a47c91cSRobert Mustacchi fpd = fp; 11490a47c91cSRobert Mustacchi if (ctf_lookup_by_id(&fpd, argv[i]) == NULL && 11500a47c91cSRobert Mustacchi ctf_dtd_lookup(fp, argv[i]) == NULL) 11510a47c91cSRobert Mustacchi return (ctf_set_errno(fp, ECTF_BADID)); 11520a47c91cSRobert Mustacchi } 11530a47c91cSRobert Mustacchi 11547c478bd9Sstevel@tonic-gate if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL) 11557c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 11567c478bd9Sstevel@tonic-gate 11577c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) { 11587c478bd9Sstevel@tonic-gate ctf_free(vdat, sizeof (ctf_id_t) * vlen); 11597c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, flag, vlen); 11637c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_type = (ushort_t)ctc->ctc_return; 11647c478bd9Sstevel@tonic-gate 11650a47c91cSRobert Mustacchi ctf_ref_inc(fp, ctc->ctc_return); 11660a47c91cSRobert Mustacchi for (i = 0; i < ctc->ctc_argc; i++) 11670a47c91cSRobert Mustacchi ctf_ref_inc(fp, argv[i]); 11680a47c91cSRobert Mustacchi 11697c478bd9Sstevel@tonic-gate bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc); 11707c478bd9Sstevel@tonic-gate if (ctc->ctc_flags & CTF_FUNC_VARARG) 11717c478bd9Sstevel@tonic-gate vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */ 11727c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_argv = vdat; 11737c478bd9Sstevel@tonic-gate 11747c478bd9Sstevel@tonic-gate return (type); 11757c478bd9Sstevel@tonic-gate } 11767c478bd9Sstevel@tonic-gate 11777c478bd9Sstevel@tonic-gate ctf_id_t 11787c478bd9Sstevel@tonic-gate ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name) 11797c478bd9Sstevel@tonic-gate { 1180e4586ebfSmws ctf_hash_t *hp = &fp->ctf_structs; 1181e4586ebfSmws ctf_helem_t *hep = NULL; 1182*7fd79137SRobert Mustacchi ctf_dtdef_t *dtd = NULL; 1183*7fd79137SRobert Mustacchi ctf_id_t type = CTF_ERR; 11847c478bd9Sstevel@tonic-gate 1185e4586ebfSmws if (name != NULL) 1186e4586ebfSmws hep = ctf_hash_lookup(hp, fp, name, strlen(name)); 1187e4586ebfSmws 1188*7fd79137SRobert Mustacchi if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) { 1189*7fd79137SRobert Mustacchi type = hep->h_type; 1190*7fd79137SRobert Mustacchi dtd = ctf_dtd_lookup(fp, type); 1191*7fd79137SRobert Mustacchi if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD) 1192*7fd79137SRobert Mustacchi dtd = NULL; 1193*7fd79137SRobert Mustacchi } 11947c478bd9Sstevel@tonic-gate 1195*7fd79137SRobert Mustacchi if (dtd == NULL) { 1196*7fd79137SRobert Mustacchi type = ctf_add_generic(fp, flag, name, &dtd); 1197*7fd79137SRobert Mustacchi if (type == CTF_ERR) 1198*7fd79137SRobert Mustacchi return (CTF_ERR); /* errno is set for us */ 1199*7fd79137SRobert Mustacchi } 1200*7fd79137SRobert Mustacchi 1201*7fd79137SRobert Mustacchi VERIFY(type != CTF_ERR); 12027c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, flag, 0); 1203e4586ebfSmws dtd->dtd_data.ctt_size = 0; 1204e4586ebfSmws 1205*7fd79137SRobert Mustacchi /* 1206*7fd79137SRobert Mustacchi * Always dirty in case we modified a forward. 1207*7fd79137SRobert Mustacchi */ 1208*7fd79137SRobert Mustacchi fp->ctf_flags |= LCTF_DIRTY; 1209*7fd79137SRobert Mustacchi 12107c478bd9Sstevel@tonic-gate return (type); 12117c478bd9Sstevel@tonic-gate } 12127c478bd9Sstevel@tonic-gate 12137c478bd9Sstevel@tonic-gate ctf_id_t 12147c478bd9Sstevel@tonic-gate ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name) 12157c478bd9Sstevel@tonic-gate { 1216e4586ebfSmws ctf_hash_t *hp = &fp->ctf_unions; 1217e4586ebfSmws ctf_helem_t *hep = NULL; 1218*7fd79137SRobert Mustacchi ctf_dtdef_t *dtd = NULL; 1219*7fd79137SRobert Mustacchi ctf_id_t type = CTF_ERR; 12207c478bd9Sstevel@tonic-gate 1221e4586ebfSmws if (name != NULL) 1222e4586ebfSmws hep = ctf_hash_lookup(hp, fp, name, strlen(name)); 1223e4586ebfSmws 1224*7fd79137SRobert Mustacchi if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) { 1225*7fd79137SRobert Mustacchi type = hep->h_type; 1226*7fd79137SRobert Mustacchi dtd = ctf_dtd_lookup(fp, type); 1227*7fd79137SRobert Mustacchi if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD) 1228*7fd79137SRobert Mustacchi dtd = NULL; 1229*7fd79137SRobert Mustacchi } 12307c478bd9Sstevel@tonic-gate 1231*7fd79137SRobert Mustacchi if (dtd == NULL) { 1232*7fd79137SRobert Mustacchi type = ctf_add_generic(fp, flag, name, &dtd); 1233*7fd79137SRobert Mustacchi if (type == CTF_ERR) 1234*7fd79137SRobert Mustacchi return (CTF_ERR); /* errno is set for us */ 1235*7fd79137SRobert Mustacchi } 1236*7fd79137SRobert Mustacchi 1237*7fd79137SRobert Mustacchi VERIFY(type != CTF_ERR); 12387c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, flag, 0); 1239e4586ebfSmws dtd->dtd_data.ctt_size = 0; 1240e4586ebfSmws 1241*7fd79137SRobert Mustacchi /* 1242*7fd79137SRobert Mustacchi * Always dirty in case we modified a forward. 1243*7fd79137SRobert Mustacchi */ 1244*7fd79137SRobert Mustacchi fp->ctf_flags |= LCTF_DIRTY; 1245*7fd79137SRobert Mustacchi 12467c478bd9Sstevel@tonic-gate return (type); 12477c478bd9Sstevel@tonic-gate } 12487c478bd9Sstevel@tonic-gate 12497c478bd9Sstevel@tonic-gate ctf_id_t 12507c478bd9Sstevel@tonic-gate ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name) 12517c478bd9Sstevel@tonic-gate { 1252e4586ebfSmws ctf_hash_t *hp = &fp->ctf_enums; 1253e4586ebfSmws ctf_helem_t *hep = NULL; 1254*7fd79137SRobert Mustacchi ctf_dtdef_t *dtd = NULL; 1255*7fd79137SRobert Mustacchi ctf_id_t type = CTF_ERR; 12567c478bd9Sstevel@tonic-gate 1257e4586ebfSmws if (name != NULL) 1258e4586ebfSmws hep = ctf_hash_lookup(hp, fp, name, strlen(name)); 1259e4586ebfSmws 1260*7fd79137SRobert Mustacchi if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) { 1261*7fd79137SRobert Mustacchi type = hep->h_type; 1262*7fd79137SRobert Mustacchi dtd = ctf_dtd_lookup(fp, type); 1263*7fd79137SRobert Mustacchi if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD) 1264*7fd79137SRobert Mustacchi dtd = NULL; 1265*7fd79137SRobert Mustacchi } 12667c478bd9Sstevel@tonic-gate 1267*7fd79137SRobert Mustacchi if (dtd == NULL) { 1268*7fd79137SRobert Mustacchi type = ctf_add_generic(fp, flag, name, &dtd); 1269*7fd79137SRobert Mustacchi if (type == CTF_ERR) 1270*7fd79137SRobert Mustacchi return (CTF_ERR); /* errno is set for us */ 1271*7fd79137SRobert Mustacchi } 1272*7fd79137SRobert Mustacchi 1273*7fd79137SRobert Mustacchi VERIFY(type != CTF_ERR); 12747c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0); 12757c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int; 12767c478bd9Sstevel@tonic-gate 1277*7fd79137SRobert Mustacchi /* 1278*7fd79137SRobert Mustacchi * Always dirty in case we modified a forward. 1279*7fd79137SRobert Mustacchi */ 1280*7fd79137SRobert Mustacchi fp->ctf_flags |= LCTF_DIRTY; 1281*7fd79137SRobert Mustacchi 12827c478bd9Sstevel@tonic-gate return (type); 12837c478bd9Sstevel@tonic-gate } 12847c478bd9Sstevel@tonic-gate 12857c478bd9Sstevel@tonic-gate ctf_id_t 12867c478bd9Sstevel@tonic-gate ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind) 12877c478bd9Sstevel@tonic-gate { 1288e4586ebfSmws ctf_hash_t *hp; 1289e4586ebfSmws ctf_helem_t *hep; 12907c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 12917c478bd9Sstevel@tonic-gate ctf_id_t type; 12927c478bd9Sstevel@tonic-gate 1293e4586ebfSmws switch (kind) { 1294e4586ebfSmws case CTF_K_STRUCT: 1295e4586ebfSmws hp = &fp->ctf_structs; 1296e4586ebfSmws break; 1297e4586ebfSmws case CTF_K_UNION: 1298e4586ebfSmws hp = &fp->ctf_unions; 1299e4586ebfSmws break; 1300e4586ebfSmws case CTF_K_ENUM: 1301e4586ebfSmws hp = &fp->ctf_enums; 1302e4586ebfSmws break; 1303e4586ebfSmws default: 13047c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTSUE)); 1305e4586ebfSmws } 1306e4586ebfSmws 1307e4586ebfSmws /* 1308e4586ebfSmws * If the type is already defined or exists as a forward tag, just 1309e4586ebfSmws * return the ctf_id_t of the existing definition. 1310e4586ebfSmws */ 1311e4586ebfSmws if (name != NULL && (hep = ctf_hash_lookup(hp, 1312e4586ebfSmws fp, name, strlen(name))) != NULL) 1313e4586ebfSmws return (hep->h_type); 13147c478bd9Sstevel@tonic-gate 13157c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 13167c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 13177c478bd9Sstevel@tonic-gate 13187c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0); 1319e4586ebfSmws dtd->dtd_data.ctt_type = kind; 1320e4586ebfSmws 13217c478bd9Sstevel@tonic-gate return (type); 13227c478bd9Sstevel@tonic-gate } 13237c478bd9Sstevel@tonic-gate 13247c478bd9Sstevel@tonic-gate ctf_id_t 13257c478bd9Sstevel@tonic-gate ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 13267c478bd9Sstevel@tonic-gate { 13277c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 13287c478bd9Sstevel@tonic-gate ctf_id_t type; 13290a47c91cSRobert Mustacchi ctf_file_t *fpd; 13307c478bd9Sstevel@tonic-gate 13310a47c91cSRobert Mustacchi fpd = fp; 13320a47c91cSRobert Mustacchi if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL && 13330a47c91cSRobert Mustacchi ctf_dtd_lookup(fp, ref) == NULL)) 13347c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 13357c478bd9Sstevel@tonic-gate 13367c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 13377c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 13387c478bd9Sstevel@tonic-gate 13397c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0); 13407c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_type = (ushort_t)ref; 13410a47c91cSRobert Mustacchi ctf_ref_inc(fp, ref); 13427c478bd9Sstevel@tonic-gate 13437c478bd9Sstevel@tonic-gate return (type); 13447c478bd9Sstevel@tonic-gate } 13457c478bd9Sstevel@tonic-gate 13467c478bd9Sstevel@tonic-gate ctf_id_t 1347*7fd79137SRobert Mustacchi ctf_add_volatile(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 13487c478bd9Sstevel@tonic-gate { 1349*7fd79137SRobert Mustacchi return (ctf_add_reftype(fp, flag, name, ref, CTF_K_VOLATILE)); 13507c478bd9Sstevel@tonic-gate } 13517c478bd9Sstevel@tonic-gate 13527c478bd9Sstevel@tonic-gate ctf_id_t 1353*7fd79137SRobert Mustacchi ctf_add_const(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 13547c478bd9Sstevel@tonic-gate { 1355*7fd79137SRobert Mustacchi return (ctf_add_reftype(fp, flag, name, ref, CTF_K_CONST)); 13567c478bd9Sstevel@tonic-gate } 13577c478bd9Sstevel@tonic-gate 13587c478bd9Sstevel@tonic-gate ctf_id_t 1359*7fd79137SRobert Mustacchi ctf_add_restrict(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 13607c478bd9Sstevel@tonic-gate { 1361*7fd79137SRobert Mustacchi return (ctf_add_reftype(fp, flag, name, ref, CTF_K_RESTRICT)); 13627c478bd9Sstevel@tonic-gate } 13637c478bd9Sstevel@tonic-gate 13647c478bd9Sstevel@tonic-gate int 13657c478bd9Sstevel@tonic-gate ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value) 13667c478bd9Sstevel@tonic-gate { 1367e4586ebfSmws ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid); 13687c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd; 13697c478bd9Sstevel@tonic-gate 13707c478bd9Sstevel@tonic-gate uint_t kind, vlen, root; 13717c478bd9Sstevel@tonic-gate char *s; 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate if (name == NULL) 13747c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 13757c478bd9Sstevel@tonic-gate 13767c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_RDWR)) 13777c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_RDONLY)); 13787c478bd9Sstevel@tonic-gate 13797c478bd9Sstevel@tonic-gate if (dtd == NULL) 13807c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_BADID)); 13817c478bd9Sstevel@tonic-gate 13827c478bd9Sstevel@tonic-gate kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 13837c478bd9Sstevel@tonic-gate root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info); 13847c478bd9Sstevel@tonic-gate vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 13857c478bd9Sstevel@tonic-gate 13867c478bd9Sstevel@tonic-gate if (kind != CTF_K_ENUM) 13877c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTENUM)); 13887c478bd9Sstevel@tonic-gate 13897c478bd9Sstevel@tonic-gate if (vlen == CTF_MAX_VLEN) 13907c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_DTFULL)); 13917c478bd9Sstevel@tonic-gate 13927c478bd9Sstevel@tonic-gate for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 13937c478bd9Sstevel@tonic-gate dmd != NULL; dmd = ctf_list_next(dmd)) { 1394*7fd79137SRobert Mustacchi if (strcmp(dmd->dmd_name, name) == 0) { 1395*7fd79137SRobert Mustacchi ctf_dprintf("encountered duplicate member %s\n", name); 13967c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_DUPMEMBER)); 13977c478bd9Sstevel@tonic-gate } 1398*7fd79137SRobert Mustacchi } 13997c478bd9Sstevel@tonic-gate 14007c478bd9Sstevel@tonic-gate if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 14017c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 14027c478bd9Sstevel@tonic-gate 14037c478bd9Sstevel@tonic-gate if ((s = ctf_strdup(name)) == NULL) { 14047c478bd9Sstevel@tonic-gate ctf_free(dmd, sizeof (ctf_dmdef_t)); 14057c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 14067c478bd9Sstevel@tonic-gate } 14077c478bd9Sstevel@tonic-gate 14087c478bd9Sstevel@tonic-gate dmd->dmd_name = s; 14097c478bd9Sstevel@tonic-gate dmd->dmd_type = CTF_ERR; 14107c478bd9Sstevel@tonic-gate dmd->dmd_offset = 0; 14117c478bd9Sstevel@tonic-gate dmd->dmd_value = value; 14127c478bd9Sstevel@tonic-gate 14137c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1); 14147c478bd9Sstevel@tonic-gate ctf_list_append(&dtd->dtd_u.dtu_members, dmd); 14157c478bd9Sstevel@tonic-gate 14167c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen += strlen(s) + 1; 14177c478bd9Sstevel@tonic-gate fp->ctf_flags |= LCTF_DIRTY; 14187c478bd9Sstevel@tonic-gate 14197c478bd9Sstevel@tonic-gate return (0); 14207c478bd9Sstevel@tonic-gate } 14217c478bd9Sstevel@tonic-gate 14227c478bd9Sstevel@tonic-gate int 1423*7fd79137SRobert Mustacchi ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type, 1424*7fd79137SRobert Mustacchi ulong_t offset) 14257c478bd9Sstevel@tonic-gate { 1426e4586ebfSmws ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid); 14277c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd; 14287c478bd9Sstevel@tonic-gate 1429*7fd79137SRobert Mustacchi ulong_t mbitsz; 14307c478bd9Sstevel@tonic-gate ssize_t msize, malign, ssize; 14317c478bd9Sstevel@tonic-gate uint_t kind, vlen, root; 1432*7fd79137SRobert Mustacchi int mkind; 14337c478bd9Sstevel@tonic-gate char *s = NULL; 14347c478bd9Sstevel@tonic-gate 14357c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_RDWR)) 14367c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_RDONLY)); 14377c478bd9Sstevel@tonic-gate 14387c478bd9Sstevel@tonic-gate if (dtd == NULL) 14397c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_BADID)); 14407c478bd9Sstevel@tonic-gate 14417c478bd9Sstevel@tonic-gate kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 14427c478bd9Sstevel@tonic-gate root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info); 14437c478bd9Sstevel@tonic-gate vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 14447c478bd9Sstevel@tonic-gate 14457c478bd9Sstevel@tonic-gate if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) 14467c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTSOU)); 14477c478bd9Sstevel@tonic-gate 14487c478bd9Sstevel@tonic-gate if (vlen == CTF_MAX_VLEN) 14497c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_DTFULL)); 14507c478bd9Sstevel@tonic-gate 1451*7fd79137SRobert Mustacchi /* 1452*7fd79137SRobert Mustacchi * Structures may have members which are anonymous. If they have two of 1453*7fd79137SRobert Mustacchi * these, then the duplicate member detection would find it due to the 1454*7fd79137SRobert Mustacchi * string of "", so we skip it. 1455*7fd79137SRobert Mustacchi */ 1456*7fd79137SRobert Mustacchi if (name != NULL && *name != '\0') { 14577c478bd9Sstevel@tonic-gate for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 14587c478bd9Sstevel@tonic-gate dmd != NULL; dmd = ctf_list_next(dmd)) { 14597c478bd9Sstevel@tonic-gate if (dmd->dmd_name != NULL && 1460*7fd79137SRobert Mustacchi strcmp(dmd->dmd_name, name) == 0) { 14617c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_DUPMEMBER)); 14627c478bd9Sstevel@tonic-gate } 14637c478bd9Sstevel@tonic-gate } 1464*7fd79137SRobert Mustacchi } 14657c478bd9Sstevel@tonic-gate 14667c478bd9Sstevel@tonic-gate if ((msize = ctf_type_size(fp, type)) == CTF_ERR || 1467*7fd79137SRobert Mustacchi (malign = ctf_type_align(fp, type)) == CTF_ERR || 1468*7fd79137SRobert Mustacchi (mkind = ctf_type_kind(fp, type)) == CTF_ERR) 14697c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 14707c478bd9Sstevel@tonic-gate 1471*7fd79137SRobert Mustacchi /* 1472*7fd79137SRobert Mustacchi * ctf_type_size returns sizes in bytes. However, for bitfields, that 1473*7fd79137SRobert Mustacchi * means that it may misrepresent and actually rounds it up to a power 1474*7fd79137SRobert Mustacchi * of two and store that in bytes. So instead we have to get the 1475*7fd79137SRobert Mustacchi * Integers encoding and rely on that. 1476*7fd79137SRobert Mustacchi */ 1477*7fd79137SRobert Mustacchi if (mkind == CTF_K_INTEGER) { 1478*7fd79137SRobert Mustacchi ctf_encoding_t e; 1479*7fd79137SRobert Mustacchi 1480*7fd79137SRobert Mustacchi if (ctf_type_encoding(fp, type, &e) == CTF_ERR) 1481*7fd79137SRobert Mustacchi return (CTF_ERR); /* errno is set for us */ 1482*7fd79137SRobert Mustacchi mbitsz = e.cte_bits; 1483*7fd79137SRobert Mustacchi } else if (mkind == CTF_K_FORWARD) { 1484*7fd79137SRobert Mustacchi /* 1485*7fd79137SRobert Mustacchi * This is a rather rare case. In general one cannot add a 1486*7fd79137SRobert Mustacchi * forward to a structure. However, the CTF tools traditionally 1487*7fd79137SRobert Mustacchi * tried to add a forward to the struct cpu as the last member. 1488*7fd79137SRobert Mustacchi * Therefore, if we find one here, we're going to verify the 1489*7fd79137SRobert Mustacchi * size and make sure it's zero. It's certainly odd, but that's 1490*7fd79137SRobert Mustacchi * life. 1491*7fd79137SRobert Mustacchi * 1492*7fd79137SRobert Mustacchi * Further, if it's not an absolute position being specified, 1493*7fd79137SRobert Mustacchi * then we refuse to add it. 1494*7fd79137SRobert Mustacchi */ 1495*7fd79137SRobert Mustacchi if (offset == ULONG_MAX) 1496*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, EINVAL)); 1497*7fd79137SRobert Mustacchi VERIFY(msize == 0); 1498*7fd79137SRobert Mustacchi mbitsz = msize; 1499*7fd79137SRobert Mustacchi } else { 1500*7fd79137SRobert Mustacchi mbitsz = msize * 8; 1501*7fd79137SRobert Mustacchi } 1502*7fd79137SRobert Mustacchi 15037c478bd9Sstevel@tonic-gate if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 15047c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 15057c478bd9Sstevel@tonic-gate 15067c478bd9Sstevel@tonic-gate if (name != NULL && (s = ctf_strdup(name)) == NULL) { 15077c478bd9Sstevel@tonic-gate ctf_free(dmd, sizeof (ctf_dmdef_t)); 15087c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 15097c478bd9Sstevel@tonic-gate } 15107c478bd9Sstevel@tonic-gate 15117c478bd9Sstevel@tonic-gate dmd->dmd_name = s; 15127c478bd9Sstevel@tonic-gate dmd->dmd_type = type; 15137c478bd9Sstevel@tonic-gate dmd->dmd_value = -1; 15147c478bd9Sstevel@tonic-gate 15157c478bd9Sstevel@tonic-gate if (kind == CTF_K_STRUCT && vlen != 0) { 15167c478bd9Sstevel@tonic-gate ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members); 15177c478bd9Sstevel@tonic-gate ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type); 1518*7fd79137SRobert Mustacchi size_t off; 15197c478bd9Sstevel@tonic-gate 1520*7fd79137SRobert Mustacchi if (offset == ULONG_MAX) { 15217c478bd9Sstevel@tonic-gate ctf_encoding_t linfo; 15227c478bd9Sstevel@tonic-gate ssize_t lsize; 15237c478bd9Sstevel@tonic-gate 1524*7fd79137SRobert Mustacchi off = lmd->dmd_offset; 15257c478bd9Sstevel@tonic-gate if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR) 15267c478bd9Sstevel@tonic-gate off += linfo.cte_bits; 15277c478bd9Sstevel@tonic-gate else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR) 15287c478bd9Sstevel@tonic-gate off += lsize * NBBY; 15297c478bd9Sstevel@tonic-gate 15307c478bd9Sstevel@tonic-gate /* 1531*7fd79137SRobert Mustacchi * Round up the offset of the end of the last member to 1532*7fd79137SRobert Mustacchi * the next byte boundary, convert 'off' to bytes, and 1533*7fd79137SRobert Mustacchi * then round it up again to the next multiple of the 1534*7fd79137SRobert Mustacchi * alignment required by the new member. Finally, 1535*7fd79137SRobert Mustacchi * convert back to bits and store the result in 1536*7fd79137SRobert Mustacchi * dmd_offset. Technically we could do more efficient 1537*7fd79137SRobert Mustacchi * packing if the new member is a bit-field, but we're 1538*7fd79137SRobert Mustacchi * the "compiler" and ANSI says we can do as we choose. 15397c478bd9Sstevel@tonic-gate */ 15407c478bd9Sstevel@tonic-gate off = roundup(off, NBBY) / NBBY; 15417c478bd9Sstevel@tonic-gate off = roundup(off, MAX(malign, 1)); 15427c478bd9Sstevel@tonic-gate dmd->dmd_offset = off * NBBY; 15437c478bd9Sstevel@tonic-gate ssize = off + msize; 15447c478bd9Sstevel@tonic-gate } else { 1545*7fd79137SRobert Mustacchi dmd->dmd_offset = offset; 1546*7fd79137SRobert Mustacchi ssize = (offset + mbitsz) / NBBY; 1547*7fd79137SRobert Mustacchi } 1548*7fd79137SRobert Mustacchi } else { 15497c478bd9Sstevel@tonic-gate dmd->dmd_offset = 0; 15507c478bd9Sstevel@tonic-gate ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL); 15517c478bd9Sstevel@tonic-gate ssize = MAX(ssize, msize); 15527c478bd9Sstevel@tonic-gate } 15537c478bd9Sstevel@tonic-gate 15547c478bd9Sstevel@tonic-gate if (ssize > CTF_MAX_SIZE) { 15557c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; 15567c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize); 15577c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize); 15587c478bd9Sstevel@tonic-gate } else 15597c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = (ushort_t)ssize; 15607c478bd9Sstevel@tonic-gate 15617c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1); 15627c478bd9Sstevel@tonic-gate ctf_list_append(&dtd->dtd_u.dtu_members, dmd); 15637c478bd9Sstevel@tonic-gate 15647c478bd9Sstevel@tonic-gate if (s != NULL) 15657c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen += strlen(s) + 1; 15667c478bd9Sstevel@tonic-gate 15670a47c91cSRobert Mustacchi ctf_ref_inc(fp, type); 15680a47c91cSRobert Mustacchi fp->ctf_flags |= LCTF_DIRTY; 15690a47c91cSRobert Mustacchi return (0); 15700a47c91cSRobert Mustacchi } 15710a47c91cSRobert Mustacchi 15720a47c91cSRobert Mustacchi /* 15730a47c91cSRobert Mustacchi * This removes a type from the dynamic section. This will fail if the type is 15740a47c91cSRobert Mustacchi * referenced by another type. Note that the CTF ID is never reused currently by 15750a47c91cSRobert Mustacchi * CTF. Note that if this container is a parent container then we just outright 15760a47c91cSRobert Mustacchi * refuse to remove the type. There currently is no notion of searching for the 15770a47c91cSRobert Mustacchi * ctf_dtdef_t in parent containers. If there is, then this constraint could 15780a47c91cSRobert Mustacchi * become finer grained. 15790a47c91cSRobert Mustacchi */ 15800a47c91cSRobert Mustacchi int 15810a47c91cSRobert Mustacchi ctf_delete_type(ctf_file_t *fp, ctf_id_t type) 15820a47c91cSRobert Mustacchi { 15830a47c91cSRobert Mustacchi ctf_file_t *fpd; 15840a47c91cSRobert Mustacchi ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type); 15850a47c91cSRobert Mustacchi 15860a47c91cSRobert Mustacchi if (!(fp->ctf_flags & LCTF_RDWR)) 15870a47c91cSRobert Mustacchi return (ctf_set_errno(fp, ECTF_RDONLY)); 15880a47c91cSRobert Mustacchi 15890a47c91cSRobert Mustacchi /* 15900a47c91cSRobert Mustacchi * We want to give as useful an errno as possible. That means that we 15910a47c91cSRobert Mustacchi * want to distinguish between a type which does not exist and one for 15920a47c91cSRobert Mustacchi * which the type is not dynamic. 15930a47c91cSRobert Mustacchi */ 15940a47c91cSRobert Mustacchi fpd = fp; 15950a47c91cSRobert Mustacchi if (ctf_lookup_by_id(&fpd, type) == NULL && 15960a47c91cSRobert Mustacchi ctf_dtd_lookup(fp, type) == NULL) 15970a47c91cSRobert Mustacchi return (CTF_ERR); /* errno is set for us */ 15980a47c91cSRobert Mustacchi 15990a47c91cSRobert Mustacchi if (dtd == NULL) 16000a47c91cSRobert Mustacchi return (ctf_set_errno(fp, ECTF_NOTDYN)); 16010a47c91cSRobert Mustacchi 16020a47c91cSRobert Mustacchi if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1) 16030a47c91cSRobert Mustacchi return (ctf_set_errno(fp, ECTF_REFERENCED)); 16040a47c91cSRobert Mustacchi 16050a47c91cSRobert Mustacchi ctf_dtd_delete(fp, dtd); 16067c478bd9Sstevel@tonic-gate fp->ctf_flags |= LCTF_DIRTY; 16077c478bd9Sstevel@tonic-gate return (0); 16087c478bd9Sstevel@tonic-gate } 16097c478bd9Sstevel@tonic-gate 16107c478bd9Sstevel@tonic-gate static int 16117c478bd9Sstevel@tonic-gate enumcmp(const char *name, int value, void *arg) 16127c478bd9Sstevel@tonic-gate { 16137c478bd9Sstevel@tonic-gate ctf_bundle_t *ctb = arg; 16147c478bd9Sstevel@tonic-gate int bvalue; 16157c478bd9Sstevel@tonic-gate 16167c478bd9Sstevel@tonic-gate return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type, 16177c478bd9Sstevel@tonic-gate name, &bvalue) == CTF_ERR || value != bvalue); 16187c478bd9Sstevel@tonic-gate } 16197c478bd9Sstevel@tonic-gate 16207c478bd9Sstevel@tonic-gate static int 16217c478bd9Sstevel@tonic-gate enumadd(const char *name, int value, void *arg) 16227c478bd9Sstevel@tonic-gate { 16237c478bd9Sstevel@tonic-gate ctf_bundle_t *ctb = arg; 16247c478bd9Sstevel@tonic-gate 16257c478bd9Sstevel@tonic-gate return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type, 16267c478bd9Sstevel@tonic-gate name, value) == CTF_ERR); 16277c478bd9Sstevel@tonic-gate } 16287c478bd9Sstevel@tonic-gate 16297c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 16307c478bd9Sstevel@tonic-gate static int 16317c478bd9Sstevel@tonic-gate membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg) 16327c478bd9Sstevel@tonic-gate { 16337c478bd9Sstevel@tonic-gate ctf_bundle_t *ctb = arg; 16347c478bd9Sstevel@tonic-gate ctf_membinfo_t ctm; 16357c478bd9Sstevel@tonic-gate 16367c478bd9Sstevel@tonic-gate return (ctf_member_info(ctb->ctb_file, ctb->ctb_type, 16377c478bd9Sstevel@tonic-gate name, &ctm) == CTF_ERR || ctm.ctm_offset != offset); 16387c478bd9Sstevel@tonic-gate } 16397c478bd9Sstevel@tonic-gate 16407c478bd9Sstevel@tonic-gate static int 16417c478bd9Sstevel@tonic-gate membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg) 16427c478bd9Sstevel@tonic-gate { 16437c478bd9Sstevel@tonic-gate ctf_bundle_t *ctb = arg; 16447c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd; 16457c478bd9Sstevel@tonic-gate char *s = NULL; 16467c478bd9Sstevel@tonic-gate 16477c478bd9Sstevel@tonic-gate if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 16487c478bd9Sstevel@tonic-gate return (ctf_set_errno(ctb->ctb_file, EAGAIN)); 16497c478bd9Sstevel@tonic-gate 16507c478bd9Sstevel@tonic-gate if (name != NULL && (s = ctf_strdup(name)) == NULL) { 16517c478bd9Sstevel@tonic-gate ctf_free(dmd, sizeof (ctf_dmdef_t)); 16527c478bd9Sstevel@tonic-gate return (ctf_set_errno(ctb->ctb_file, EAGAIN)); 16537c478bd9Sstevel@tonic-gate } 16547c478bd9Sstevel@tonic-gate 16557c478bd9Sstevel@tonic-gate /* 16567c478bd9Sstevel@tonic-gate * For now, dmd_type is copied as the src_fp's type; it is reset to an 16577c478bd9Sstevel@tonic-gate * equivalent dst_fp type by a final loop in ctf_add_type(), below. 16587c478bd9Sstevel@tonic-gate */ 16597c478bd9Sstevel@tonic-gate dmd->dmd_name = s; 16607c478bd9Sstevel@tonic-gate dmd->dmd_type = type; 16617c478bd9Sstevel@tonic-gate dmd->dmd_offset = offset; 16627c478bd9Sstevel@tonic-gate dmd->dmd_value = -1; 16637c478bd9Sstevel@tonic-gate 16647c478bd9Sstevel@tonic-gate ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd); 16657c478bd9Sstevel@tonic-gate 16667c478bd9Sstevel@tonic-gate if (s != NULL) 16677c478bd9Sstevel@tonic-gate ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1; 16687c478bd9Sstevel@tonic-gate 16697c478bd9Sstevel@tonic-gate ctb->ctb_file->ctf_flags |= LCTF_DIRTY; 16707c478bd9Sstevel@tonic-gate return (0); 16717c478bd9Sstevel@tonic-gate } 16727c478bd9Sstevel@tonic-gate 16737c478bd9Sstevel@tonic-gate /* 16747c478bd9Sstevel@tonic-gate * The ctf_add_type routine is used to copy a type from a source CTF container 16757c478bd9Sstevel@tonic-gate * to a dynamic destination container. This routine operates recursively by 16767c478bd9Sstevel@tonic-gate * following the source type's links and embedded member types. If the 16777c478bd9Sstevel@tonic-gate * destination container already contains a named type which has the same 16787c478bd9Sstevel@tonic-gate * attributes, then we succeed and return this type but no changes occur. 16797c478bd9Sstevel@tonic-gate */ 16807c478bd9Sstevel@tonic-gate ctf_id_t 16817c478bd9Sstevel@tonic-gate ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type) 16827c478bd9Sstevel@tonic-gate { 16837c478bd9Sstevel@tonic-gate ctf_id_t dst_type = CTF_ERR; 16847c478bd9Sstevel@tonic-gate uint_t dst_kind = CTF_K_UNKNOWN; 16857c478bd9Sstevel@tonic-gate 16867c478bd9Sstevel@tonic-gate const ctf_type_t *tp; 16877c478bd9Sstevel@tonic-gate const char *name; 16887c478bd9Sstevel@tonic-gate uint_t kind, flag, vlen; 16897c478bd9Sstevel@tonic-gate 16907c478bd9Sstevel@tonic-gate ctf_bundle_t src, dst; 16917c478bd9Sstevel@tonic-gate ctf_encoding_t src_en, dst_en; 16927c478bd9Sstevel@tonic-gate ctf_arinfo_t src_ar, dst_ar; 16937c478bd9Sstevel@tonic-gate 16947c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 16957c478bd9Sstevel@tonic-gate ctf_funcinfo_t ctc; 16967c478bd9Sstevel@tonic-gate ssize_t size; 16977c478bd9Sstevel@tonic-gate 16987c478bd9Sstevel@tonic-gate ctf_hash_t *hp; 16997c478bd9Sstevel@tonic-gate ctf_helem_t *hep; 17007c478bd9Sstevel@tonic-gate 17010a47c91cSRobert Mustacchi if (dst_fp == src_fp) 17020a47c91cSRobert Mustacchi return (src_type); 17030a47c91cSRobert Mustacchi 17047c478bd9Sstevel@tonic-gate if (!(dst_fp->ctf_flags & LCTF_RDWR)) 17057c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_RDONLY)); 17067c478bd9Sstevel@tonic-gate 17077c478bd9Sstevel@tonic-gate if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL) 17087c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 17097c478bd9Sstevel@tonic-gate 17107c478bd9Sstevel@tonic-gate name = ctf_strptr(src_fp, tp->ctt_name); 17117c478bd9Sstevel@tonic-gate kind = LCTF_INFO_KIND(src_fp, tp->ctt_info); 17127c478bd9Sstevel@tonic-gate flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info); 17137c478bd9Sstevel@tonic-gate vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info); 17147c478bd9Sstevel@tonic-gate 17157c478bd9Sstevel@tonic-gate switch (kind) { 17167c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 17177c478bd9Sstevel@tonic-gate hp = &dst_fp->ctf_structs; 17187c478bd9Sstevel@tonic-gate break; 17197c478bd9Sstevel@tonic-gate case CTF_K_UNION: 17207c478bd9Sstevel@tonic-gate hp = &dst_fp->ctf_unions; 17217c478bd9Sstevel@tonic-gate break; 17227c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 17237c478bd9Sstevel@tonic-gate hp = &dst_fp->ctf_enums; 17247c478bd9Sstevel@tonic-gate break; 17257c478bd9Sstevel@tonic-gate default: 17267c478bd9Sstevel@tonic-gate hp = &dst_fp->ctf_names; 17277c478bd9Sstevel@tonic-gate break; 17287c478bd9Sstevel@tonic-gate } 17297c478bd9Sstevel@tonic-gate 17307c478bd9Sstevel@tonic-gate /* 17317c478bd9Sstevel@tonic-gate * If the source type has a name and is a root type (visible at the 17327c478bd9Sstevel@tonic-gate * top-level scope), lookup the name in the destination container and 17337c478bd9Sstevel@tonic-gate * verify that it is of the same kind before we do anything else. 17347c478bd9Sstevel@tonic-gate */ 17357c478bd9Sstevel@tonic-gate if ((flag & CTF_ADD_ROOT) && name[0] != '\0' && 17367c478bd9Sstevel@tonic-gate (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) { 17377c478bd9Sstevel@tonic-gate dst_type = (ctf_id_t)hep->h_type; 17387c478bd9Sstevel@tonic-gate dst_kind = ctf_type_kind(dst_fp, dst_type); 17397c478bd9Sstevel@tonic-gate } 17407c478bd9Sstevel@tonic-gate 17417c478bd9Sstevel@tonic-gate /* 17427c478bd9Sstevel@tonic-gate * If an identically named dst_type exists, fail with ECTF_CONFLICT 17437c478bd9Sstevel@tonic-gate * unless dst_type is a forward declaration and src_type is a struct, 17447c478bd9Sstevel@tonic-gate * union, or enum (i.e. the definition of the previous forward decl). 17457c478bd9Sstevel@tonic-gate */ 17467c478bd9Sstevel@tonic-gate if (dst_type != CTF_ERR && dst_kind != kind && ( 17477c478bd9Sstevel@tonic-gate dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM && 17487c478bd9Sstevel@tonic-gate kind != CTF_K_STRUCT && kind != CTF_K_UNION))) 17497c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 17507c478bd9Sstevel@tonic-gate 17517c478bd9Sstevel@tonic-gate /* 17527c478bd9Sstevel@tonic-gate * If the non-empty name was not found in the appropriate hash, search 17537c478bd9Sstevel@tonic-gate * the list of pending dynamic definitions that are not yet committed. 17547c478bd9Sstevel@tonic-gate * If a matching name and kind are found, assume this is the type that 17557c478bd9Sstevel@tonic-gate * we are looking for. This is necessary to permit ctf_add_type() to 17567c478bd9Sstevel@tonic-gate * operate recursively on entities such as a struct that contains a 17577c478bd9Sstevel@tonic-gate * pointer member that refers to the same struct type. 17587c478bd9Sstevel@tonic-gate */ 17597c478bd9Sstevel@tonic-gate if (dst_type == CTF_ERR && name[0] != '\0') { 17607c478bd9Sstevel@tonic-gate for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL && 17617c478bd9Sstevel@tonic-gate dtd->dtd_type > dst_fp->ctf_dtoldid; 17627c478bd9Sstevel@tonic-gate dtd = ctf_list_prev(dtd)) { 17637c478bd9Sstevel@tonic-gate if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) == kind && 17647c478bd9Sstevel@tonic-gate dtd->dtd_name != NULL && 17657c478bd9Sstevel@tonic-gate strcmp(dtd->dtd_name, name) == 0) 17667c478bd9Sstevel@tonic-gate return (dtd->dtd_type); 17677c478bd9Sstevel@tonic-gate } 17687c478bd9Sstevel@tonic-gate } 17697c478bd9Sstevel@tonic-gate 17707c478bd9Sstevel@tonic-gate src.ctb_file = src_fp; 17717c478bd9Sstevel@tonic-gate src.ctb_type = src_type; 17727c478bd9Sstevel@tonic-gate src.ctb_dtd = NULL; 17737c478bd9Sstevel@tonic-gate 17747c478bd9Sstevel@tonic-gate dst.ctb_file = dst_fp; 17757c478bd9Sstevel@tonic-gate dst.ctb_type = dst_type; 17767c478bd9Sstevel@tonic-gate dst.ctb_dtd = NULL; 17777c478bd9Sstevel@tonic-gate 17787c478bd9Sstevel@tonic-gate /* 17797c478bd9Sstevel@tonic-gate * Now perform kind-specific processing. If dst_type is CTF_ERR, then 17807c478bd9Sstevel@tonic-gate * we add a new type with the same properties as src_type to dst_fp. 17817c478bd9Sstevel@tonic-gate * If dst_type is not CTF_ERR, then we verify that dst_type has the 17827c478bd9Sstevel@tonic-gate * same attributes as src_type. We recurse for embedded references. 17837c478bd9Sstevel@tonic-gate */ 17847c478bd9Sstevel@tonic-gate switch (kind) { 17857c478bd9Sstevel@tonic-gate case CTF_K_INTEGER: 17867c478bd9Sstevel@tonic-gate case CTF_K_FLOAT: 17877c478bd9Sstevel@tonic-gate if (ctf_type_encoding(src_fp, src_type, &src_en) != 0) 17887c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 17897c478bd9Sstevel@tonic-gate 17907c478bd9Sstevel@tonic-gate if (dst_type != CTF_ERR) { 17917c478bd9Sstevel@tonic-gate if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0) 17927c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 17937c478bd9Sstevel@tonic-gate 17947c478bd9Sstevel@tonic-gate if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t))) 17957c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 17967c478bd9Sstevel@tonic-gate 17977c478bd9Sstevel@tonic-gate } else if (kind == CTF_K_INTEGER) { 17987c478bd9Sstevel@tonic-gate dst_type = ctf_add_integer(dst_fp, flag, name, &src_en); 17997c478bd9Sstevel@tonic-gate } else 18007c478bd9Sstevel@tonic-gate dst_type = ctf_add_float(dst_fp, flag, name, &src_en); 18017c478bd9Sstevel@tonic-gate break; 18027c478bd9Sstevel@tonic-gate 18037c478bd9Sstevel@tonic-gate case CTF_K_POINTER: 18047c478bd9Sstevel@tonic-gate case CTF_K_VOLATILE: 18057c478bd9Sstevel@tonic-gate case CTF_K_CONST: 18067c478bd9Sstevel@tonic-gate case CTF_K_RESTRICT: 18077c478bd9Sstevel@tonic-gate src_type = ctf_type_reference(src_fp, src_type); 18087c478bd9Sstevel@tonic-gate src_type = ctf_add_type(dst_fp, src_fp, src_type); 18097c478bd9Sstevel@tonic-gate 18107c478bd9Sstevel@tonic-gate if (src_type == CTF_ERR) 18117c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 18127c478bd9Sstevel@tonic-gate 1813*7fd79137SRobert Mustacchi dst_type = ctf_add_reftype(dst_fp, flag, NULL, src_type, kind); 18147c478bd9Sstevel@tonic-gate break; 18157c478bd9Sstevel@tonic-gate 18167c478bd9Sstevel@tonic-gate case CTF_K_ARRAY: 18177c478bd9Sstevel@tonic-gate if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR) 18187c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 18197c478bd9Sstevel@tonic-gate 18207c478bd9Sstevel@tonic-gate src_ar.ctr_contents = 18217c478bd9Sstevel@tonic-gate ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents); 18227c478bd9Sstevel@tonic-gate src_ar.ctr_index = 18237c478bd9Sstevel@tonic-gate ctf_add_type(dst_fp, src_fp, src_ar.ctr_index); 18247c478bd9Sstevel@tonic-gate src_ar.ctr_nelems = src_ar.ctr_nelems; 18257c478bd9Sstevel@tonic-gate 18267c478bd9Sstevel@tonic-gate if (src_ar.ctr_contents == CTF_ERR || 18277c478bd9Sstevel@tonic-gate src_ar.ctr_index == CTF_ERR) 18287c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 18297c478bd9Sstevel@tonic-gate 18307c478bd9Sstevel@tonic-gate if (dst_type != CTF_ERR) { 18317c478bd9Sstevel@tonic-gate if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0) 18327c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 18337c478bd9Sstevel@tonic-gate 18347c478bd9Sstevel@tonic-gate if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t))) 18357c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 18367c478bd9Sstevel@tonic-gate } else 18377c478bd9Sstevel@tonic-gate dst_type = ctf_add_array(dst_fp, flag, &src_ar); 18387c478bd9Sstevel@tonic-gate break; 18397c478bd9Sstevel@tonic-gate 18407c478bd9Sstevel@tonic-gate case CTF_K_FUNCTION: 18417c478bd9Sstevel@tonic-gate ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type); 18427c478bd9Sstevel@tonic-gate ctc.ctc_argc = 0; 18437c478bd9Sstevel@tonic-gate ctc.ctc_flags = 0; 18447c478bd9Sstevel@tonic-gate 18457c478bd9Sstevel@tonic-gate if (ctc.ctc_return == CTF_ERR) 18467c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 18477c478bd9Sstevel@tonic-gate 1848*7fd79137SRobert Mustacchi dst_type = ctf_add_funcptr(dst_fp, flag, &ctc, NULL); 18497c478bd9Sstevel@tonic-gate break; 18507c478bd9Sstevel@tonic-gate 18517c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 18527c478bd9Sstevel@tonic-gate case CTF_K_UNION: { 18537c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd; 18547c478bd9Sstevel@tonic-gate int errs = 0; 18557c478bd9Sstevel@tonic-gate 18567c478bd9Sstevel@tonic-gate /* 18577c478bd9Sstevel@tonic-gate * Technically to match a struct or union we need to check both 18587c478bd9Sstevel@tonic-gate * ways (src members vs. dst, dst members vs. src) but we make 18597c478bd9Sstevel@tonic-gate * this more optimal by only checking src vs. dst and comparing 18607c478bd9Sstevel@tonic-gate * the total size of the structure (which we must do anyway) 18617c478bd9Sstevel@tonic-gate * which covers the possibility of dst members not in src. 18627c478bd9Sstevel@tonic-gate * This optimization can be defeated for unions, but is so 18637c478bd9Sstevel@tonic-gate * pathological as to render it irrelevant for our purposes. 18647c478bd9Sstevel@tonic-gate */ 18657c478bd9Sstevel@tonic-gate if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) { 18667c478bd9Sstevel@tonic-gate if (ctf_type_size(src_fp, src_type) != 18677c478bd9Sstevel@tonic-gate ctf_type_size(dst_fp, dst_type)) 18687c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 18697c478bd9Sstevel@tonic-gate 18707c478bd9Sstevel@tonic-gate if (ctf_member_iter(src_fp, src_type, membcmp, &dst)) 18717c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 18727c478bd9Sstevel@tonic-gate 18737c478bd9Sstevel@tonic-gate break; 18747c478bd9Sstevel@tonic-gate } 18757c478bd9Sstevel@tonic-gate 18767c478bd9Sstevel@tonic-gate /* 18777c478bd9Sstevel@tonic-gate * Unlike the other cases, copying structs and unions is done 18787c478bd9Sstevel@tonic-gate * manually so as to avoid repeated lookups in ctf_add_member 18797c478bd9Sstevel@tonic-gate * and to ensure the exact same member offsets as in src_type. 18807c478bd9Sstevel@tonic-gate */ 18817c478bd9Sstevel@tonic-gate dst_type = ctf_add_generic(dst_fp, flag, name, &dtd); 18827c478bd9Sstevel@tonic-gate if (dst_type == CTF_ERR) 18837c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 18847c478bd9Sstevel@tonic-gate 18857c478bd9Sstevel@tonic-gate dst.ctb_type = dst_type; 18867c478bd9Sstevel@tonic-gate dst.ctb_dtd = dtd; 18877c478bd9Sstevel@tonic-gate 18887c478bd9Sstevel@tonic-gate if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0) 18897c478bd9Sstevel@tonic-gate errs++; /* increment errs and fail at bottom of case */ 18907c478bd9Sstevel@tonic-gate 18917c478bd9Sstevel@tonic-gate if ((size = ctf_type_size(src_fp, src_type)) > CTF_MAX_SIZE) { 18927c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; 18937c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size); 18947c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size); 18957c478bd9Sstevel@tonic-gate } else 18967c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = (ushort_t)size; 18977c478bd9Sstevel@tonic-gate 18987c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen); 18997c478bd9Sstevel@tonic-gate 19007c478bd9Sstevel@tonic-gate /* 19017c478bd9Sstevel@tonic-gate * Make a final pass through the members changing each dmd_type 19027c478bd9Sstevel@tonic-gate * (a src_fp type) to an equivalent type in dst_fp. We pass 19037c478bd9Sstevel@tonic-gate * through all members, leaving any that fail set to CTF_ERR. 19047c478bd9Sstevel@tonic-gate */ 19057c478bd9Sstevel@tonic-gate for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 19067c478bd9Sstevel@tonic-gate dmd != NULL; dmd = ctf_list_next(dmd)) { 19077c478bd9Sstevel@tonic-gate if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp, 19087c478bd9Sstevel@tonic-gate dmd->dmd_type)) == CTF_ERR) 19097c478bd9Sstevel@tonic-gate errs++; 19107c478bd9Sstevel@tonic-gate } 19117c478bd9Sstevel@tonic-gate 19127c478bd9Sstevel@tonic-gate if (errs) 19137c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 19140a47c91cSRobert Mustacchi 19150a47c91cSRobert Mustacchi /* 19160a47c91cSRobert Mustacchi * Now that we know that we can't fail, we go through and bump 19170a47c91cSRobert Mustacchi * all the reference counts on the member types. 19180a47c91cSRobert Mustacchi */ 19190a47c91cSRobert Mustacchi for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 19200a47c91cSRobert Mustacchi dmd != NULL; dmd = ctf_list_next(dmd)) 19210a47c91cSRobert Mustacchi ctf_ref_inc(dst_fp, dmd->dmd_type); 19227c478bd9Sstevel@tonic-gate break; 19237c478bd9Sstevel@tonic-gate } 19247c478bd9Sstevel@tonic-gate 19257c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 19267c478bd9Sstevel@tonic-gate if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) { 19277c478bd9Sstevel@tonic-gate if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) || 19287c478bd9Sstevel@tonic-gate ctf_enum_iter(dst_fp, dst_type, enumcmp, &src)) 19297c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 19307c478bd9Sstevel@tonic-gate } else { 19317c478bd9Sstevel@tonic-gate dst_type = ctf_add_enum(dst_fp, flag, name); 19327c478bd9Sstevel@tonic-gate if ((dst.ctb_type = dst_type) == CTF_ERR || 19337c478bd9Sstevel@tonic-gate ctf_enum_iter(src_fp, src_type, enumadd, &dst)) 19347c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 19357c478bd9Sstevel@tonic-gate } 19367c478bd9Sstevel@tonic-gate break; 19377c478bd9Sstevel@tonic-gate 19387c478bd9Sstevel@tonic-gate case CTF_K_FORWARD: 19397c478bd9Sstevel@tonic-gate if (dst_type == CTF_ERR) { 19407c478bd9Sstevel@tonic-gate dst_type = ctf_add_forward(dst_fp, 19417c478bd9Sstevel@tonic-gate flag, name, CTF_K_STRUCT); /* assume STRUCT */ 19427c478bd9Sstevel@tonic-gate } 19437c478bd9Sstevel@tonic-gate break; 19447c478bd9Sstevel@tonic-gate 19457c478bd9Sstevel@tonic-gate case CTF_K_TYPEDEF: 19467c478bd9Sstevel@tonic-gate src_type = ctf_type_reference(src_fp, src_type); 19477c478bd9Sstevel@tonic-gate src_type = ctf_add_type(dst_fp, src_fp, src_type); 19487c478bd9Sstevel@tonic-gate 19497c478bd9Sstevel@tonic-gate if (src_type == CTF_ERR) 19507c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 19517c478bd9Sstevel@tonic-gate 19527c478bd9Sstevel@tonic-gate /* 19537c478bd9Sstevel@tonic-gate * If dst_type is not CTF_ERR at this point, we should check if 19547c478bd9Sstevel@tonic-gate * ctf_type_reference(dst_fp, dst_type) != src_type and if so 19557c478bd9Sstevel@tonic-gate * fail with ECTF_CONFLICT. However, this causes problems with 19567c478bd9Sstevel@tonic-gate * <sys/types.h> typedefs that vary based on things like if 19577c478bd9Sstevel@tonic-gate * _ILP32x then pid_t is int otherwise long. We therefore omit 19587c478bd9Sstevel@tonic-gate * this check and assume that if the identically named typedef 19597c478bd9Sstevel@tonic-gate * already exists in dst_fp, it is correct or equivalent. 19607c478bd9Sstevel@tonic-gate */ 19617c478bd9Sstevel@tonic-gate if (dst_type == CTF_ERR) { 19627c478bd9Sstevel@tonic-gate dst_type = ctf_add_typedef(dst_fp, flag, 19637c478bd9Sstevel@tonic-gate name, src_type); 19647c478bd9Sstevel@tonic-gate } 19657c478bd9Sstevel@tonic-gate break; 19667c478bd9Sstevel@tonic-gate 19677c478bd9Sstevel@tonic-gate default: 19687c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CORRUPT)); 19697c478bd9Sstevel@tonic-gate } 19707c478bd9Sstevel@tonic-gate 19717c478bd9Sstevel@tonic-gate return (dst_type); 19727c478bd9Sstevel@tonic-gate } 1973*7fd79137SRobert Mustacchi 1974*7fd79137SRobert Mustacchi int 1975*7fd79137SRobert Mustacchi ctf_add_function(ctf_file_t *fp, ulong_t idx, const ctf_funcinfo_t *fip, 1976*7fd79137SRobert Mustacchi const ctf_id_t *argc) 1977*7fd79137SRobert Mustacchi { 1978*7fd79137SRobert Mustacchi int i; 1979*7fd79137SRobert Mustacchi ctf_dsdef_t *dsd; 1980*7fd79137SRobert Mustacchi ctf_file_t *afp; 1981*7fd79137SRobert Mustacchi uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data; 1982*7fd79137SRobert Mustacchi 1983*7fd79137SRobert Mustacchi if (!(fp->ctf_flags & LCTF_RDWR)) 1984*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_RDONLY)); 1985*7fd79137SRobert Mustacchi 1986*7fd79137SRobert Mustacchi if (ctf_dsd_lookup(fp, idx) != NULL) 1987*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_CONFLICT)); 1988*7fd79137SRobert Mustacchi 1989*7fd79137SRobert Mustacchi if (symbase == NULL) 1990*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_STRTAB)); 1991*7fd79137SRobert Mustacchi 1992*7fd79137SRobert Mustacchi if (idx > fp->ctf_nsyms) 1993*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_NOTDATA)); 1994*7fd79137SRobert Mustacchi 1995*7fd79137SRobert Mustacchi if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) { 1996*7fd79137SRobert Mustacchi const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx; 1997*7fd79137SRobert Mustacchi if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC) 1998*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_NOTFUNC)); 1999*7fd79137SRobert Mustacchi } else { 2000*7fd79137SRobert Mustacchi const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx; 2001*7fd79137SRobert Mustacchi if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC) 2002*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_NOTFUNC)); 2003*7fd79137SRobert Mustacchi } 2004*7fd79137SRobert Mustacchi 2005*7fd79137SRobert Mustacchi afp = fp; 2006*7fd79137SRobert Mustacchi if (ctf_lookup_by_id(&afp, fip->ctc_return) == NULL) 2007*7fd79137SRobert Mustacchi return (CTF_ERR); /* errno is set for us */ 2008*7fd79137SRobert Mustacchi 2009*7fd79137SRobert Mustacchi for (i = 0; i < fip->ctc_argc; i++) { 2010*7fd79137SRobert Mustacchi afp = fp; 2011*7fd79137SRobert Mustacchi if (ctf_lookup_by_id(&afp, argc[i]) == NULL) 2012*7fd79137SRobert Mustacchi return (CTF_ERR); /* errno is set for us */ 2013*7fd79137SRobert Mustacchi } 2014*7fd79137SRobert Mustacchi 2015*7fd79137SRobert Mustacchi dsd = ctf_alloc(sizeof (ctf_dsdef_t)); 2016*7fd79137SRobert Mustacchi if (dsd == NULL) 2017*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ENOMEM)); 2018*7fd79137SRobert Mustacchi dsd->dsd_nargs = fip->ctc_argc; 2019*7fd79137SRobert Mustacchi if (fip->ctc_flags & CTF_FUNC_VARARG) 2020*7fd79137SRobert Mustacchi dsd->dsd_nargs++; 2021*7fd79137SRobert Mustacchi if (dsd->dsd_nargs != 0) { 2022*7fd79137SRobert Mustacchi dsd->dsd_argc = ctf_alloc(sizeof (ctf_id_t) * dsd->dsd_nargs); 2023*7fd79137SRobert Mustacchi if (dsd->dsd_argc == NULL) { 2024*7fd79137SRobert Mustacchi ctf_free(dsd, sizeof (ctf_dsdef_t)); 2025*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ENOMEM)); 2026*7fd79137SRobert Mustacchi } 2027*7fd79137SRobert Mustacchi bcopy(argc, dsd->dsd_argc, sizeof (ctf_id_t) * fip->ctc_argc); 2028*7fd79137SRobert Mustacchi if (fip->ctc_flags & CTF_FUNC_VARARG) 2029*7fd79137SRobert Mustacchi dsd->dsd_argc[fip->ctc_argc] = 0; 2030*7fd79137SRobert Mustacchi } 2031*7fd79137SRobert Mustacchi dsd->dsd_symidx = idx; 2032*7fd79137SRobert Mustacchi dsd->dsd_tid = fip->ctc_return; 2033*7fd79137SRobert Mustacchi 2034*7fd79137SRobert Mustacchi ctf_dsd_insert(fp, dsd); 2035*7fd79137SRobert Mustacchi fp->ctf_flags |= LCTF_DIRTY; 2036*7fd79137SRobert Mustacchi 2037*7fd79137SRobert Mustacchi return (0); 2038*7fd79137SRobert Mustacchi } 2039*7fd79137SRobert Mustacchi 2040*7fd79137SRobert Mustacchi int 2041*7fd79137SRobert Mustacchi ctf_add_object(ctf_file_t *fp, ulong_t idx, ctf_id_t type) 2042*7fd79137SRobert Mustacchi { 2043*7fd79137SRobert Mustacchi ctf_dsdef_t *dsd; 2044*7fd79137SRobert Mustacchi ctf_file_t *afp; 2045*7fd79137SRobert Mustacchi uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data; 2046*7fd79137SRobert Mustacchi 2047*7fd79137SRobert Mustacchi if (!(fp->ctf_flags & LCTF_RDWR)) 2048*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_RDONLY)); 2049*7fd79137SRobert Mustacchi 2050*7fd79137SRobert Mustacchi if (!(fp->ctf_flags & LCTF_RDWR)) 2051*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_RDONLY)); 2052*7fd79137SRobert Mustacchi 2053*7fd79137SRobert Mustacchi if (ctf_dsd_lookup(fp, idx) != NULL) 2054*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_CONFLICT)); 2055*7fd79137SRobert Mustacchi 2056*7fd79137SRobert Mustacchi if (symbase == NULL) 2057*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_STRTAB)); 2058*7fd79137SRobert Mustacchi 2059*7fd79137SRobert Mustacchi if (idx > fp->ctf_nsyms) 2060*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_NOTDATA)); 2061*7fd79137SRobert Mustacchi 2062*7fd79137SRobert Mustacchi if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) { 2063*7fd79137SRobert Mustacchi const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx; 2064*7fd79137SRobert Mustacchi if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT) 2065*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_NOTDATA)); 2066*7fd79137SRobert Mustacchi } else { 2067*7fd79137SRobert Mustacchi const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx; 2068*7fd79137SRobert Mustacchi if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT) 2069*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_NOTDATA)); 2070*7fd79137SRobert Mustacchi } 2071*7fd79137SRobert Mustacchi 2072*7fd79137SRobert Mustacchi afp = fp; 2073*7fd79137SRobert Mustacchi if (ctf_lookup_by_id(&afp, type) == NULL) 2074*7fd79137SRobert Mustacchi return (CTF_ERR); /* errno is set for us */ 2075*7fd79137SRobert Mustacchi 2076*7fd79137SRobert Mustacchi dsd = ctf_alloc(sizeof (ctf_dsdef_t)); 2077*7fd79137SRobert Mustacchi if (dsd == NULL) 2078*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ENOMEM)); 2079*7fd79137SRobert Mustacchi dsd->dsd_symidx = idx; 2080*7fd79137SRobert Mustacchi dsd->dsd_tid = type; 2081*7fd79137SRobert Mustacchi dsd->dsd_argc = NULL; 2082*7fd79137SRobert Mustacchi 2083*7fd79137SRobert Mustacchi ctf_dsd_insert(fp, dsd); 2084*7fd79137SRobert Mustacchi fp->ctf_flags |= LCTF_DIRTY; 2085*7fd79137SRobert Mustacchi 2086*7fd79137SRobert Mustacchi return (0); 2087*7fd79137SRobert Mustacchi } 2088*7fd79137SRobert Mustacchi 2089*7fd79137SRobert Mustacchi void 2090*7fd79137SRobert Mustacchi ctf_dataptr(ctf_file_t *fp, const void **addrp, size_t *sizep) 2091*7fd79137SRobert Mustacchi { 2092*7fd79137SRobert Mustacchi if (addrp != NULL) 2093*7fd79137SRobert Mustacchi *addrp = fp->ctf_base; 2094*7fd79137SRobert Mustacchi if (sizep != NULL) 2095*7fd79137SRobert Mustacchi *sizep = fp->ctf_size; 2096*7fd79137SRobert Mustacchi } 2097*7fd79137SRobert Mustacchi 2098*7fd79137SRobert Mustacchi int 2099*7fd79137SRobert Mustacchi ctf_add_label(ctf_file_t *fp, const char *name, ctf_id_t type, uint_t position) 2100*7fd79137SRobert Mustacchi { 2101*7fd79137SRobert Mustacchi ctf_file_t *fpd; 2102*7fd79137SRobert Mustacchi ctf_dldef_t *dld; 2103*7fd79137SRobert Mustacchi 2104*7fd79137SRobert Mustacchi if (name == NULL) 2105*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, EINVAL)); 2106*7fd79137SRobert Mustacchi 2107*7fd79137SRobert Mustacchi if (!(fp->ctf_flags & LCTF_RDWR)) 2108*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_RDONLY)); 2109*7fd79137SRobert Mustacchi 2110*7fd79137SRobert Mustacchi fpd = fp; 2111*7fd79137SRobert Mustacchi if (type != 0 && ctf_lookup_by_id(&fpd, type) == NULL) 2112*7fd79137SRobert Mustacchi return (CTF_ERR); /* errno is set for us */ 2113*7fd79137SRobert Mustacchi 2114*7fd79137SRobert Mustacchi if (type != 0 && (fp->ctf_flags & LCTF_CHILD) && 2115*7fd79137SRobert Mustacchi CTF_TYPE_ISPARENT(type)) 2116*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_NOPARENT)); 2117*7fd79137SRobert Mustacchi 2118*7fd79137SRobert Mustacchi if (ctf_dld_lookup(fp, name) != NULL) 2119*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_LABELEXISTS)); 2120*7fd79137SRobert Mustacchi 2121*7fd79137SRobert Mustacchi if ((dld = ctf_alloc(sizeof (ctf_dldef_t))) == NULL) 2122*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, EAGAIN)); 2123*7fd79137SRobert Mustacchi 2124*7fd79137SRobert Mustacchi if ((dld->dld_name = ctf_strdup(name)) == NULL) { 2125*7fd79137SRobert Mustacchi ctf_free(dld, sizeof (ctf_dldef_t)); 2126*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, EAGAIN)); 2127*7fd79137SRobert Mustacchi } 2128*7fd79137SRobert Mustacchi 2129*7fd79137SRobert Mustacchi dld->dld_type = type; 2130*7fd79137SRobert Mustacchi fp->ctf_dtstrlen += strlen(name) + 1; 2131*7fd79137SRobert Mustacchi ctf_dld_insert(fp, dld, position); 2132*7fd79137SRobert Mustacchi fp->ctf_flags |= LCTF_DIRTY; 2133*7fd79137SRobert Mustacchi 2134*7fd79137SRobert Mustacchi return (0); 2135*7fd79137SRobert Mustacchi } 2136*7fd79137SRobert Mustacchi 2137*7fd79137SRobert Mustacchi /* 2138*7fd79137SRobert Mustacchi * Update the size of a structure or union. Note that we don't allow this to 2139*7fd79137SRobert Mustacchi * shrink the size of a struct or union, only to increase it. This is useful for 2140*7fd79137SRobert Mustacchi * cases when you have a structure whose actual size is larger than the sum of 2141*7fd79137SRobert Mustacchi * its members due to padding for natural alignment. 2142*7fd79137SRobert Mustacchi */ 2143*7fd79137SRobert Mustacchi int 2144*7fd79137SRobert Mustacchi ctf_set_size(ctf_file_t *fp, ctf_id_t id, const ulong_t newsz) 2145*7fd79137SRobert Mustacchi { 2146*7fd79137SRobert Mustacchi ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id); 2147*7fd79137SRobert Mustacchi uint_t kind; 2148*7fd79137SRobert Mustacchi size_t oldsz; 2149*7fd79137SRobert Mustacchi 2150*7fd79137SRobert Mustacchi if (!(fp->ctf_flags & LCTF_RDWR)) 2151*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_RDONLY)); 2152*7fd79137SRobert Mustacchi 2153*7fd79137SRobert Mustacchi if (dtd == NULL) 2154*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_BADID)); 2155*7fd79137SRobert Mustacchi 2156*7fd79137SRobert Mustacchi kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 2157*7fd79137SRobert Mustacchi 2158*7fd79137SRobert Mustacchi if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) 2159*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_NOTSOU)); 2160*7fd79137SRobert Mustacchi 2161*7fd79137SRobert Mustacchi if ((oldsz = dtd->dtd_data.ctt_size) == CTF_LSIZE_SENT) 2162*7fd79137SRobert Mustacchi oldsz = CTF_TYPE_LSIZE(&dtd->dtd_data); 2163*7fd79137SRobert Mustacchi 2164*7fd79137SRobert Mustacchi if (newsz < oldsz) 2165*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, EINVAL)); 2166*7fd79137SRobert Mustacchi 2167*7fd79137SRobert Mustacchi if (newsz > CTF_MAX_SIZE) { 2168*7fd79137SRobert Mustacchi dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; 2169*7fd79137SRobert Mustacchi dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(newsz); 2170*7fd79137SRobert Mustacchi dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(newsz); 2171*7fd79137SRobert Mustacchi } else { 2172*7fd79137SRobert Mustacchi dtd->dtd_data.ctt_size = (ushort_t)newsz; 2173*7fd79137SRobert Mustacchi } 2174*7fd79137SRobert Mustacchi 2175*7fd79137SRobert Mustacchi fp->ctf_flags |= LCTF_DIRTY; 2176*7fd79137SRobert Mustacchi return (0); 2177*7fd79137SRobert Mustacchi } 2178*7fd79137SRobert Mustacchi 2179*7fd79137SRobert Mustacchi int 2180*7fd79137SRobert Mustacchi ctf_set_root(ctf_file_t *fp, ctf_id_t id, const boolean_t vis) 2181*7fd79137SRobert Mustacchi { 2182*7fd79137SRobert Mustacchi ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id); 2183*7fd79137SRobert Mustacchi uint_t kind, vlen; 2184*7fd79137SRobert Mustacchi 2185*7fd79137SRobert Mustacchi if (!(fp->ctf_flags & LCTF_RDWR)) 2186*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_RDONLY)); 2187*7fd79137SRobert Mustacchi 2188*7fd79137SRobert Mustacchi if (dtd == NULL) 2189*7fd79137SRobert Mustacchi return (ctf_set_errno(fp, ECTF_BADID)); 2190*7fd79137SRobert Mustacchi 2191*7fd79137SRobert Mustacchi kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 2192*7fd79137SRobert Mustacchi vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 2193*7fd79137SRobert Mustacchi 2194*7fd79137SRobert Mustacchi dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, vis, vlen); 2195*7fd79137SRobert Mustacchi return (0); 2196*7fd79137SRobert Mustacchi } 2197