1d876124dSJohn Birrell /*
2d876124dSJohn Birrell * CDDL HEADER START
3d876124dSJohn Birrell *
4d876124dSJohn Birrell * The contents of this file are subject to the terms of the
5d876124dSJohn Birrell * Common Development and Distribution License, Version 1.0 only
6d876124dSJohn Birrell * (the "License"). You may not use this file except in compliance
7d876124dSJohn Birrell * with the License.
8d876124dSJohn Birrell *
9d876124dSJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10d876124dSJohn Birrell * or http://www.opensolaris.org/os/licensing.
11d876124dSJohn Birrell * See the License for the specific language governing permissions
12d876124dSJohn Birrell * and limitations under the License.
13d876124dSJohn Birrell *
14d876124dSJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each
15d876124dSJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16d876124dSJohn Birrell * If applicable, add the following below this CDDL HEADER, with the
17d876124dSJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying
18d876124dSJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner]
19d876124dSJohn Birrell *
20d876124dSJohn Birrell * CDDL HEADER END
21d876124dSJohn Birrell */
22d876124dSJohn Birrell
23d876124dSJohn Birrell /*
24d876124dSJohn Birrell * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25d876124dSJohn Birrell * Use is subject to license terms.
26d876124dSJohn Birrell */
273f0164abSXin LI /*
283f0164abSXin LI * Copyright (c) 2013, Joyent, Inc. All rights reserved.
293f0164abSXin LI */
30d876124dSJohn Birrell
31d876124dSJohn Birrell #include <sys/sysmacros.h>
32d876124dSJohn Birrell #include <sys/param.h>
33d876124dSJohn Birrell #include <sys/mman.h>
34d876124dSJohn Birrell #include <ctf_impl.h>
353f0164abSXin LI #include <sys/debug.h>
36d876124dSJohn Birrell
37d876124dSJohn Birrell /*
38d876124dSJohn Birrell * This static string is used as the template for initially populating a
39d876124dSJohn Birrell * dynamic container's string table. We always store \0 in the first byte,
40d876124dSJohn Birrell * and we use the generic string "PARENT" to mark this container's parent
41d876124dSJohn Birrell * if one is associated with the container using ctf_import().
42d876124dSJohn Birrell */
43d876124dSJohn Birrell static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT";
44d876124dSJohn Birrell
45d876124dSJohn Birrell /*
46d876124dSJohn Birrell * To create an empty CTF container, we just declare a zeroed header and call
47d876124dSJohn Birrell * ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w
48d876124dSJohn Birrell * and initialize the dynamic members. We set dtstrlen to 1 to reserve the
49d876124dSJohn Birrell * first byte of the string table for a \0 byte, and we start assigning type
50d876124dSJohn Birrell * IDs at 1 because type ID 0 is used as a sentinel.
51d876124dSJohn Birrell */
52d876124dSJohn Birrell ctf_file_t *
ctf_create(int * errp)53d876124dSJohn Birrell ctf_create(int *errp)
54d876124dSJohn Birrell {
55d876124dSJohn Birrell static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } };
56d876124dSJohn Birrell
57d876124dSJohn Birrell const ulong_t hashlen = 128;
58d876124dSJohn Birrell ctf_dtdef_t **hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *));
59d876124dSJohn Birrell ctf_sect_t cts;
60d876124dSJohn Birrell ctf_file_t *fp;
61d876124dSJohn Birrell
62d876124dSJohn Birrell if (hash == NULL)
63d876124dSJohn Birrell return (ctf_set_open_errno(errp, EAGAIN));
64d876124dSJohn Birrell
65d876124dSJohn Birrell cts.cts_name = _CTF_SECTION;
66d876124dSJohn Birrell cts.cts_type = SHT_PROGBITS;
67d876124dSJohn Birrell cts.cts_flags = 0;
6809d32567SSean Bruno cts.cts_data = (void *)&hdr;
69d876124dSJohn Birrell cts.cts_size = sizeof (hdr);
70d876124dSJohn Birrell cts.cts_entsize = 1;
71d876124dSJohn Birrell cts.cts_offset = 0;
72d876124dSJohn Birrell
73d876124dSJohn Birrell if ((fp = ctf_bufopen(&cts, NULL, NULL, errp)) == NULL) {
74d876124dSJohn Birrell ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *));
75d876124dSJohn Birrell return (NULL);
76d876124dSJohn Birrell }
77d876124dSJohn Birrell
78d876124dSJohn Birrell fp->ctf_flags |= LCTF_RDWR;
79d876124dSJohn Birrell fp->ctf_dthashlen = hashlen;
80d876124dSJohn Birrell bzero(hash, hashlen * sizeof (ctf_dtdef_t *));
81d876124dSJohn Birrell fp->ctf_dthash = hash;
82d876124dSJohn Birrell fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE);
83d876124dSJohn Birrell fp->ctf_dtnextid = 1;
84d876124dSJohn Birrell fp->ctf_dtoldid = 0;
85d876124dSJohn Birrell
86d876124dSJohn Birrell return (fp);
87d876124dSJohn Birrell }
88d876124dSJohn Birrell
89d876124dSJohn Birrell static uchar_t *
ctf_copy_smembers(const ctf_file_t * fp,ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)90*a6fb8691SMark Johnston ctf_copy_smembers(const ctf_file_t *fp, ctf_dtdef_t *dtd, uint_t soff,
91*a6fb8691SMark Johnston uchar_t *t)
92d876124dSJohn Birrell {
93d876124dSJohn Birrell ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
94*a6fb8691SMark Johnston size_t sz;
95*a6fb8691SMark Johnston uint_t name;
96d876124dSJohn Birrell
97d876124dSJohn Birrell for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
98d876124dSJohn Birrell if (dmd->dmd_name) {
99*a6fb8691SMark Johnston name = soff;
100d876124dSJohn Birrell soff += strlen(dmd->dmd_name) + 1;
101d876124dSJohn Birrell } else
102*a6fb8691SMark Johnston name = 0;
103d876124dSJohn Birrell
104*a6fb8691SMark Johnston if (fp->ctf_version == CTF_VERSION_2) {
105*a6fb8691SMark Johnston struct ctf_member_v2 ctm;
106*a6fb8691SMark Johnston
107*a6fb8691SMark Johnston ctm.ctm_name = name;
108d876124dSJohn Birrell ctm.ctm_type = (ushort_t)dmd->dmd_type;
109d876124dSJohn Birrell ctm.ctm_offset = (ushort_t)dmd->dmd_offset;
110d876124dSJohn Birrell
111*a6fb8691SMark Johnston sz = sizeof (ctm);
112*a6fb8691SMark Johnston bcopy(&ctm, t, sz);
113*a6fb8691SMark Johnston t += sz;
114*a6fb8691SMark Johnston } else {
115*a6fb8691SMark Johnston struct ctf_member_v3 ctm;
116*a6fb8691SMark Johnston
117*a6fb8691SMark Johnston ctm.ctm_name = name;
118*a6fb8691SMark Johnston ctm.ctm_type = dmd->dmd_type;
119*a6fb8691SMark Johnston ctm.ctm_offset = dmd->dmd_offset;
120*a6fb8691SMark Johnston
121*a6fb8691SMark Johnston sz = sizeof (ctm);
122*a6fb8691SMark Johnston bcopy(&ctm, t, sz);
123*a6fb8691SMark Johnston t += sz;
124*a6fb8691SMark Johnston }
125d876124dSJohn Birrell }
126d876124dSJohn Birrell
127d876124dSJohn Birrell return (t);
128d876124dSJohn Birrell }
129d876124dSJohn Birrell
130d876124dSJohn Birrell static uchar_t *
ctf_copy_lmembers(const ctf_file_t * fp,ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)131*a6fb8691SMark Johnston ctf_copy_lmembers(const ctf_file_t *fp, ctf_dtdef_t *dtd, uint_t soff,
132*a6fb8691SMark Johnston uchar_t *t)
133d876124dSJohn Birrell {
134d876124dSJohn Birrell ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
135*a6fb8691SMark Johnston size_t sz;
136*a6fb8691SMark Johnston uint_t name;
137d876124dSJohn Birrell
138d876124dSJohn Birrell for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
139d876124dSJohn Birrell if (dmd->dmd_name) {
140*a6fb8691SMark Johnston name = soff;
141d876124dSJohn Birrell soff += strlen(dmd->dmd_name) + 1;
142d876124dSJohn Birrell } else
143*a6fb8691SMark Johnston name = 0;
144d876124dSJohn Birrell
145*a6fb8691SMark Johnston if (fp->ctf_version == CTF_VERSION_2) {
146*a6fb8691SMark Johnston struct ctf_lmember_v2 ctlm;
147*a6fb8691SMark Johnston
148*a6fb8691SMark Johnston ctlm.ctlm_name = name;
149d876124dSJohn Birrell ctlm.ctlm_type = (ushort_t)dmd->dmd_type;
150d876124dSJohn Birrell ctlm.ctlm_pad = 0;
151d876124dSJohn Birrell ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset);
152d876124dSJohn Birrell ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset);
153d876124dSJohn Birrell
154*a6fb8691SMark Johnston sz = sizeof (ctlm);
155*a6fb8691SMark Johnston bcopy(&ctlm, t, sz);
156*a6fb8691SMark Johnston t += sz;
157*a6fb8691SMark Johnston } else {
158*a6fb8691SMark Johnston struct ctf_lmember_v3 ctlm;
159*a6fb8691SMark Johnston
160*a6fb8691SMark Johnston ctlm.ctlm_name = name;
161*a6fb8691SMark Johnston ctlm.ctlm_type = dmd->dmd_type;
162*a6fb8691SMark Johnston ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset);
163*a6fb8691SMark Johnston ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset);
164*a6fb8691SMark Johnston
165*a6fb8691SMark Johnston sz = sizeof (ctlm);
166*a6fb8691SMark Johnston bcopy(&ctlm, t, sz);
167*a6fb8691SMark Johnston t += sz;
168*a6fb8691SMark Johnston }
169d876124dSJohn Birrell }
170d876124dSJohn Birrell
171d876124dSJohn Birrell return (t);
172d876124dSJohn Birrell }
173d876124dSJohn Birrell
174d876124dSJohn Birrell static uchar_t *
ctf_copy_emembers(ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)175d876124dSJohn Birrell ctf_copy_emembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
176d876124dSJohn Birrell {
177d876124dSJohn Birrell ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
178d876124dSJohn Birrell ctf_enum_t cte;
179d876124dSJohn Birrell
180d876124dSJohn Birrell for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
181d876124dSJohn Birrell cte.cte_name = soff;
182d876124dSJohn Birrell cte.cte_value = dmd->dmd_value;
183d876124dSJohn Birrell soff += strlen(dmd->dmd_name) + 1;
184d876124dSJohn Birrell bcopy(&cte, t, sizeof (cte));
185d876124dSJohn Birrell t += sizeof (cte);
186d876124dSJohn Birrell }
187d876124dSJohn Birrell
188d876124dSJohn Birrell return (t);
189d876124dSJohn Birrell }
190d876124dSJohn Birrell
191d876124dSJohn Birrell static uchar_t *
ctf_copy_membnames(ctf_dtdef_t * dtd,uchar_t * s)192d876124dSJohn Birrell ctf_copy_membnames(ctf_dtdef_t *dtd, uchar_t *s)
193d876124dSJohn Birrell {
194d876124dSJohn Birrell ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
195d876124dSJohn Birrell size_t len;
196d876124dSJohn Birrell
197d876124dSJohn Birrell for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
198d876124dSJohn Birrell if (dmd->dmd_name == NULL)
199d876124dSJohn Birrell continue; /* skip anonymous members */
200d876124dSJohn Birrell len = strlen(dmd->dmd_name) + 1;
201d876124dSJohn Birrell bcopy(dmd->dmd_name, s, len);
202d876124dSJohn Birrell s += len;
203d876124dSJohn Birrell }
204d876124dSJohn Birrell
205d876124dSJohn Birrell return (s);
206d876124dSJohn Birrell }
207d876124dSJohn Birrell
208d876124dSJohn Birrell /*
2093f0164abSXin LI * Only types of dyanmic CTF containers contain reference counts. These
2103f0164abSXin LI * containers are marked RD/WR. Because of that we basically make this a no-op
2113f0164abSXin LI * for compatability with non-dynamic CTF sections. This is also a no-op for
2123f0164abSXin LI * types which are not dynamic types. It is the responsibility of the caller to
2133f0164abSXin LI * make sure it is a valid type. We help that caller out on debug builds.
2143f0164abSXin LI *
2153f0164abSXin LI * Note that the reference counts are not maintained for types that are not
2163f0164abSXin LI * within this container. In other words if we have a type in a parent, that
2173f0164abSXin LI * will not have its reference count increased. On the flip side, the parent
2183f0164abSXin LI * will not be allowed to remove dynamic types if it has children.
2193f0164abSXin LI */
2203f0164abSXin LI static void
ctf_ref_inc(ctf_file_t * fp,ctf_id_t tid)2213f0164abSXin LI ctf_ref_inc(ctf_file_t *fp, ctf_id_t tid)
2223f0164abSXin LI {
2233f0164abSXin LI ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
2243f0164abSXin LI
2253f0164abSXin LI if (dtd == NULL)
2263f0164abSXin LI return;
2273f0164abSXin LI
2283f0164abSXin LI if (!(fp->ctf_flags & LCTF_RDWR))
2293f0164abSXin LI return;
2303f0164abSXin LI
2313f0164abSXin LI dtd->dtd_ref++;
2323f0164abSXin LI }
2333f0164abSXin LI
2343f0164abSXin LI /*
2353f0164abSXin LI * Just as with ctf_ref_inc, this is a no-op on non-writeable containers and the
2363f0164abSXin LI * caller should ensure that this is already a valid type.
2373f0164abSXin LI */
2383f0164abSXin LI static void
ctf_ref_dec(ctf_file_t * fp,ctf_id_t tid)2393f0164abSXin LI ctf_ref_dec(ctf_file_t *fp, ctf_id_t tid)
2403f0164abSXin LI {
2413f0164abSXin LI ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
2423f0164abSXin LI
2433f0164abSXin LI if (dtd == NULL)
2443f0164abSXin LI return;
2453f0164abSXin LI
2463f0164abSXin LI if (!(fp->ctf_flags & LCTF_RDWR))
2473f0164abSXin LI return;
2483f0164abSXin LI
2493f0164abSXin LI ASSERT(dtd->dtd_ref >= 1);
2503f0164abSXin LI dtd->dtd_ref--;
2513f0164abSXin LI }
2523f0164abSXin LI
2533f0164abSXin LI /*
254d876124dSJohn Birrell * If the specified CTF container is writable and has been modified, reload
255d876124dSJohn Birrell * this container with the updated type definitions. In order to make this
256d876124dSJohn Birrell * code and the rest of libctf as simple as possible, we perform updates by
257d876124dSJohn Birrell * taking the dynamic type definitions and creating an in-memory CTF file
258d876124dSJohn Birrell * containing the definitions, and then call ctf_bufopen() on it. This not
259d876124dSJohn Birrell * only leverages ctf_bufopen(), but also avoids having to bifurcate the rest
260d876124dSJohn Birrell * of the library code with different lookup paths for static and dynamic
261d876124dSJohn Birrell * type definitions. We are therefore optimizing greatly for lookup over
262d876124dSJohn Birrell * update, which we assume will be an uncommon operation. We perform one
263d876124dSJohn Birrell * extra trick here for the benefit of callers and to keep our code simple:
264d876124dSJohn Birrell * ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp
265d876124dSJohn Birrell * constant for the caller, so after ctf_bufopen() returns, we use bcopy to
266d876124dSJohn Birrell * swap the interior of the old and new ctf_file_t's, and then free the old.
2673f0164abSXin LI *
2683f0164abSXin LI * Note that the lists of dynamic types stays around and the resulting container
2693f0164abSXin LI * is still writeable. Furthermore, the reference counts that are on the dtd's
2703f0164abSXin LI * are still valid.
271d876124dSJohn Birrell */
272d876124dSJohn Birrell int
ctf_update(ctf_file_t * fp)273d876124dSJohn Birrell ctf_update(ctf_file_t *fp)
274d876124dSJohn Birrell {
275d876124dSJohn Birrell ctf_file_t ofp, *nfp;
276d876124dSJohn Birrell ctf_header_t hdr;
277d876124dSJohn Birrell ctf_dtdef_t *dtd;
278d876124dSJohn Birrell ctf_sect_t cts;
279d876124dSJohn Birrell
280d876124dSJohn Birrell uchar_t *s, *s0, *t;
281d876124dSJohn Birrell size_t size;
282d876124dSJohn Birrell void *buf;
283d876124dSJohn Birrell int err;
284d876124dSJohn Birrell
285d876124dSJohn Birrell if (!(fp->ctf_flags & LCTF_RDWR))
286d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_RDONLY));
287d876124dSJohn Birrell
288d876124dSJohn Birrell if (!(fp->ctf_flags & LCTF_DIRTY))
289d876124dSJohn Birrell return (0); /* no update required */
290d876124dSJohn Birrell
291d876124dSJohn Birrell /*
292d876124dSJohn Birrell * Fill in an initial CTF header. We will leave the label, object,
293d876124dSJohn Birrell * and function sections empty and only output a header, type section,
294d876124dSJohn Birrell * and string table. The type section begins at a 4-byte aligned
295d876124dSJohn Birrell * boundary past the CTF header itself (at relative offset zero).
296d876124dSJohn Birrell */
297d876124dSJohn Birrell bzero(&hdr, sizeof (hdr));
298d876124dSJohn Birrell hdr.cth_magic = CTF_MAGIC;
299*a6fb8691SMark Johnston hdr.cth_version = fp->ctf_version;
300d876124dSJohn Birrell
301d876124dSJohn Birrell if (fp->ctf_flags & LCTF_CHILD)
302d876124dSJohn Birrell hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */
303d876124dSJohn Birrell
304d876124dSJohn Birrell /*
305d876124dSJohn Birrell * Iterate through the dynamic type definition list and compute the
306d876124dSJohn Birrell * size of the CTF type section we will need to generate.
307d876124dSJohn Birrell */
308d876124dSJohn Birrell for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs);
309d876124dSJohn Birrell dtd != NULL; dtd = ctf_list_next(dtd)) {
310d876124dSJohn Birrell
311*a6fb8691SMark Johnston uint_t kind = LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info);
312*a6fb8691SMark Johnston uint_t vlen = LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info);
313d876124dSJohn Birrell
314*a6fb8691SMark Johnston if (fp->ctf_version == CTF_VERSION_2) {
315*a6fb8691SMark Johnston if (dtd->dtd_data.ctt_size != CTF_V2_LSIZE_SENT)
316*a6fb8691SMark Johnston size += sizeof (struct ctf_stype_v2);
317d876124dSJohn Birrell else
318*a6fb8691SMark Johnston size += sizeof (struct ctf_type_v2);
319*a6fb8691SMark Johnston } else {
320*a6fb8691SMark Johnston if (dtd->dtd_data.ctt_size != LCTF_LSIZE_SENT(fp))
321*a6fb8691SMark Johnston size += sizeof (struct ctf_stype_v3);
322*a6fb8691SMark Johnston else
323*a6fb8691SMark Johnston size += sizeof (struct ctf_type_v3);
324*a6fb8691SMark Johnston }
325d876124dSJohn Birrell
326d876124dSJohn Birrell switch (kind) {
327d876124dSJohn Birrell case CTF_K_INTEGER:
328d876124dSJohn Birrell case CTF_K_FLOAT:
329d876124dSJohn Birrell size += sizeof (uint_t);
330d876124dSJohn Birrell break;
331d876124dSJohn Birrell case CTF_K_ARRAY:
332*a6fb8691SMark Johnston size += fp->ctf_version == CTF_VERSION_2 ?
333*a6fb8691SMark Johnston sizeof (struct ctf_array_v2) :
334*a6fb8691SMark Johnston sizeof (struct ctf_array_v3);
335d876124dSJohn Birrell break;
336d876124dSJohn Birrell case CTF_K_FUNCTION:
337*a6fb8691SMark Johnston size += roundup2(fp->ctf_idwidth * vlen, 4);
338d876124dSJohn Birrell break;
339d876124dSJohn Birrell case CTF_K_STRUCT:
340d876124dSJohn Birrell case CTF_K_UNION:
341*a6fb8691SMark Johnston if (fp->ctf_version == CTF_VERSION_2) {
342*a6fb8691SMark Johnston if (dtd->dtd_data.ctt_size <
343*a6fb8691SMark Johnston LCTF_LSTRUCT_THRESH(fp))
344*a6fb8691SMark Johnston size += sizeof (struct ctf_member_v2) *
345*a6fb8691SMark Johnston vlen;
346d876124dSJohn Birrell else
347*a6fb8691SMark Johnston size += sizeof (struct ctf_lmember_v2) *
348*a6fb8691SMark Johnston vlen;
349*a6fb8691SMark Johnston } else {
350*a6fb8691SMark Johnston if (dtd->dtd_data.ctt_size <
351*a6fb8691SMark Johnston LCTF_LSTRUCT_THRESH(fp))
352*a6fb8691SMark Johnston size += sizeof (struct ctf_member_v3) *
353*a6fb8691SMark Johnston vlen;
354*a6fb8691SMark Johnston else
355*a6fb8691SMark Johnston size += sizeof (struct ctf_lmember_v3) *
356*a6fb8691SMark Johnston vlen;
357*a6fb8691SMark Johnston }
358d876124dSJohn Birrell break;
359d876124dSJohn Birrell case CTF_K_ENUM:
360d876124dSJohn Birrell size += sizeof (ctf_enum_t) * vlen;
361d876124dSJohn Birrell break;
362d876124dSJohn Birrell }
363d876124dSJohn Birrell }
364d876124dSJohn Birrell
365d876124dSJohn Birrell /*
366d876124dSJohn Birrell * Fill in the string table offset and size, compute the size of the
367d876124dSJohn Birrell * entire CTF buffer we need, and then allocate a new buffer and
368d876124dSJohn Birrell * bcopy the finished header to the start of the buffer.
369d876124dSJohn Birrell */
370d876124dSJohn Birrell hdr.cth_stroff = hdr.cth_typeoff + size;
371d876124dSJohn Birrell hdr.cth_strlen = fp->ctf_dtstrlen;
372d876124dSJohn Birrell size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
373d876124dSJohn Birrell
374d876124dSJohn Birrell if ((buf = ctf_data_alloc(size)) == MAP_FAILED)
375d876124dSJohn Birrell return (ctf_set_errno(fp, EAGAIN));
376d876124dSJohn Birrell
377d876124dSJohn Birrell bcopy(&hdr, buf, sizeof (ctf_header_t));
378d876124dSJohn Birrell t = (uchar_t *)buf + sizeof (ctf_header_t);
379d876124dSJohn Birrell s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff;
380d876124dSJohn Birrell
381d876124dSJohn Birrell bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE));
382d876124dSJohn Birrell s += sizeof (_CTF_STRTAB_TEMPLATE);
383d876124dSJohn Birrell
384d876124dSJohn Birrell /*
385d876124dSJohn Birrell * We now take a final lap through the dynamic type definition list and
386d876124dSJohn Birrell * copy the appropriate type records and strings to the output buffer.
387d876124dSJohn Birrell */
388d876124dSJohn Birrell for (dtd = ctf_list_next(&fp->ctf_dtdefs);
389d876124dSJohn Birrell dtd != NULL; dtd = ctf_list_next(dtd)) {
390*a6fb8691SMark Johnston void *tp;
391*a6fb8691SMark Johnston uint_t kind = LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info);
392*a6fb8691SMark Johnston uint_t vlen = LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info);
393*a6fb8691SMark Johnston struct ctf_type_v2 ctt;
394d876124dSJohn Birrell
395d876124dSJohn Birrell uint_t encoding;
396d876124dSJohn Birrell size_t len;
397d876124dSJohn Birrell
398d876124dSJohn Birrell if (dtd->dtd_name != NULL) {
399d876124dSJohn Birrell dtd->dtd_data.ctt_name = (uint_t)(s - s0);
400d876124dSJohn Birrell len = strlen(dtd->dtd_name) + 1;
401d876124dSJohn Birrell bcopy(dtd->dtd_name, s, len);
402d876124dSJohn Birrell s += len;
403d876124dSJohn Birrell } else
404d876124dSJohn Birrell dtd->dtd_data.ctt_name = 0;
405d876124dSJohn Birrell
406*a6fb8691SMark Johnston if (fp->ctf_version == CTF_VERSION_2) {
407*a6fb8691SMark Johnston ctt.ctt_name = dtd->dtd_data.ctt_name;
408*a6fb8691SMark Johnston ctt.ctt_info = (ushort_t)dtd->dtd_data.ctt_info;
409*a6fb8691SMark Johnston ctt.ctt_size = (ushort_t)dtd->dtd_data.ctt_size;
410*a6fb8691SMark Johnston if (dtd->dtd_data.ctt_size != CTF_V2_LSIZE_SENT)
411*a6fb8691SMark Johnston len = sizeof (struct ctf_stype_v2);
412*a6fb8691SMark Johnston else {
413*a6fb8691SMark Johnston len = sizeof (struct ctf_type_v2);
414*a6fb8691SMark Johnston ctt.ctt_lsizehi = dtd->dtd_data.ctt_lsizehi;
415*a6fb8691SMark Johnston ctt.ctt_lsizelo = dtd->dtd_data.ctt_lsizelo;
416*a6fb8691SMark Johnston }
417*a6fb8691SMark Johnston tp = &ctt;
418*a6fb8691SMark Johnston } else {
419*a6fb8691SMark Johnston if (dtd->dtd_data.ctt_size != LCTF_LSIZE_SENT(fp))
420*a6fb8691SMark Johnston len = sizeof (struct ctf_stype_v3);
421d876124dSJohn Birrell else
422*a6fb8691SMark Johnston len = sizeof (struct ctf_type_v3);
423*a6fb8691SMark Johnston tp = &dtd->dtd_data;
424*a6fb8691SMark Johnston }
425d876124dSJohn Birrell
426*a6fb8691SMark Johnston bcopy(tp, t, len);
427d876124dSJohn Birrell t += len;
428d876124dSJohn Birrell
429d876124dSJohn Birrell switch (kind) {
430d876124dSJohn Birrell case CTF_K_INTEGER:
431d876124dSJohn Birrell case CTF_K_FLOAT:
432d876124dSJohn Birrell if (kind == CTF_K_INTEGER) {
433d876124dSJohn Birrell encoding = CTF_INT_DATA(
434d876124dSJohn Birrell dtd->dtd_u.dtu_enc.cte_format,
435d876124dSJohn Birrell dtd->dtd_u.dtu_enc.cte_offset,
436d876124dSJohn Birrell dtd->dtd_u.dtu_enc.cte_bits);
437d876124dSJohn Birrell } else {
438d876124dSJohn Birrell encoding = CTF_FP_DATA(
439d876124dSJohn Birrell dtd->dtd_u.dtu_enc.cte_format,
440d876124dSJohn Birrell dtd->dtd_u.dtu_enc.cte_offset,
441d876124dSJohn Birrell dtd->dtd_u.dtu_enc.cte_bits);
442d876124dSJohn Birrell }
443d876124dSJohn Birrell bcopy(&encoding, t, sizeof (encoding));
444d876124dSJohn Birrell t += sizeof (encoding);
445d876124dSJohn Birrell break;
446d876124dSJohn Birrell
447d876124dSJohn Birrell case CTF_K_ARRAY:
448*a6fb8691SMark Johnston if (fp->ctf_version == CTF_VERSION_2) {
449*a6fb8691SMark Johnston struct ctf_array_v2 cta;
450*a6fb8691SMark Johnston
451*a6fb8691SMark Johnston cta.cta_contents =
452*a6fb8691SMark Johnston (uint16_t)dtd->dtd_u.dtu_arr.ctr_contents;
453*a6fb8691SMark Johnston cta.cta_index =
454*a6fb8691SMark Johnston (uint16_t)dtd->dtd_u.dtu_arr.ctr_index;
455d876124dSJohn Birrell cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
456*a6fb8691SMark Johnston
457d876124dSJohn Birrell bcopy(&cta, t, sizeof (cta));
458d876124dSJohn Birrell t += sizeof (cta);
459*a6fb8691SMark Johnston } else {
460*a6fb8691SMark Johnston struct ctf_array_v3 cta;
461*a6fb8691SMark Johnston
462*a6fb8691SMark Johnston cta.cta_contents =
463*a6fb8691SMark Johnston dtd->dtd_u.dtu_arr.ctr_contents;
464*a6fb8691SMark Johnston cta.cta_index = dtd->dtd_u.dtu_arr.ctr_index;
465*a6fb8691SMark Johnston cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
466*a6fb8691SMark Johnston
467*a6fb8691SMark Johnston bcopy(&cta, t, sizeof (cta));
468*a6fb8691SMark Johnston t += sizeof (cta);
469*a6fb8691SMark Johnston }
470d876124dSJohn Birrell break;
471d876124dSJohn Birrell
472d876124dSJohn Birrell case CTF_K_FUNCTION: {
473*a6fb8691SMark Johnston char *argv = (char *)(uintptr_t)t;
474d876124dSJohn Birrell uint_t argc;
475d876124dSJohn Birrell
476*a6fb8691SMark Johnston if (fp->ctf_version == CTF_VERSION_2) {
477*a6fb8691SMark Johnston ushort_t arg;
478d876124dSJohn Birrell
479*a6fb8691SMark Johnston for (argc = 0; argc < vlen;
480*a6fb8691SMark Johnston argc++, argv += sizeof(arg)) {
481*a6fb8691SMark Johnston arg =
482*a6fb8691SMark Johnston (ushort_t)dtd->dtd_u.dtu_argv[argc];
483*a6fb8691SMark Johnston memcpy(argv, &arg, sizeof(arg));
484*a6fb8691SMark Johnston }
485*a6fb8691SMark Johnston } else {
486*a6fb8691SMark Johnston uint_t arg;
487*a6fb8691SMark Johnston
488*a6fb8691SMark Johnston for (argc = 0; argc < vlen;
489*a6fb8691SMark Johnston argc++, argv += sizeof(arg)) {
490*a6fb8691SMark Johnston arg = (uint_t)dtd->dtd_u.dtu_argv[argc];
491*a6fb8691SMark Johnston memcpy(argv, &arg, sizeof(arg));
492*a6fb8691SMark Johnston }
493*a6fb8691SMark Johnston }
494d876124dSJohn Birrell
495d876124dSJohn Birrell t = (uchar_t *)argv;
496d876124dSJohn Birrell break;
497d876124dSJohn Birrell }
498d876124dSJohn Birrell
499d876124dSJohn Birrell case CTF_K_STRUCT:
500d876124dSJohn Birrell case CTF_K_UNION:
501*a6fb8691SMark Johnston if (dtd->dtd_data.ctt_size < LCTF_LSTRUCT_THRESH(fp))
502*a6fb8691SMark Johnston t = ctf_copy_smembers(fp, dtd, (uint_t)(s - s0),
503*a6fb8691SMark Johnston t);
504d876124dSJohn Birrell else
505*a6fb8691SMark Johnston t = ctf_copy_lmembers(fp, dtd, (uint_t)(s - s0),
506*a6fb8691SMark Johnston t);
507d876124dSJohn Birrell s = ctf_copy_membnames(dtd, s);
508d876124dSJohn Birrell break;
509d876124dSJohn Birrell
510d876124dSJohn Birrell case CTF_K_ENUM:
511d876124dSJohn Birrell t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t);
512d876124dSJohn Birrell s = ctf_copy_membnames(dtd, s);
513d876124dSJohn Birrell break;
514d876124dSJohn Birrell }
515d876124dSJohn Birrell }
516d876124dSJohn Birrell
517d876124dSJohn Birrell /*
518d876124dSJohn Birrell * Finally, we are ready to ctf_bufopen() the new container. If this
519d876124dSJohn Birrell * is successful, we then switch nfp and fp and free the old container.
520d876124dSJohn Birrell */
521d876124dSJohn Birrell ctf_data_protect(buf, size);
522d876124dSJohn Birrell cts.cts_name = _CTF_SECTION;
523d876124dSJohn Birrell cts.cts_type = SHT_PROGBITS;
524d876124dSJohn Birrell cts.cts_flags = 0;
525d876124dSJohn Birrell cts.cts_data = buf;
526d876124dSJohn Birrell cts.cts_size = size;
527d876124dSJohn Birrell cts.cts_entsize = 1;
528d876124dSJohn Birrell cts.cts_offset = 0;
529d876124dSJohn Birrell
530d876124dSJohn Birrell if ((nfp = ctf_bufopen(&cts, NULL, NULL, &err)) == NULL) {
531d876124dSJohn Birrell ctf_data_free(buf, size);
532d876124dSJohn Birrell return (ctf_set_errno(fp, err));
533d876124dSJohn Birrell }
534d876124dSJohn Birrell
535d876124dSJohn Birrell (void) ctf_setmodel(nfp, ctf_getmodel(fp));
536d876124dSJohn Birrell (void) ctf_import(nfp, fp->ctf_parent);
537d876124dSJohn Birrell
538d876124dSJohn Birrell nfp->ctf_refcnt = fp->ctf_refcnt;
539d876124dSJohn Birrell nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
540d876124dSJohn Birrell nfp->ctf_data.cts_data = NULL; /* force ctf_data_free() on close */
541d876124dSJohn Birrell nfp->ctf_dthash = fp->ctf_dthash;
542d876124dSJohn Birrell nfp->ctf_dthashlen = fp->ctf_dthashlen;
543d876124dSJohn Birrell nfp->ctf_dtdefs = fp->ctf_dtdefs;
544d876124dSJohn Birrell nfp->ctf_dtstrlen = fp->ctf_dtstrlen;
545d876124dSJohn Birrell nfp->ctf_dtnextid = fp->ctf_dtnextid;
546d876124dSJohn Birrell nfp->ctf_dtoldid = fp->ctf_dtnextid - 1;
547d876124dSJohn Birrell nfp->ctf_specific = fp->ctf_specific;
548d876124dSJohn Birrell
549d876124dSJohn Birrell fp->ctf_dthash = NULL;
550d876124dSJohn Birrell fp->ctf_dthashlen = 0;
551d876124dSJohn Birrell bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t));
552d876124dSJohn Birrell
553d876124dSJohn Birrell bcopy(fp, &ofp, sizeof (ctf_file_t));
554d876124dSJohn Birrell bcopy(nfp, fp, sizeof (ctf_file_t));
555d876124dSJohn Birrell bcopy(&ofp, nfp, sizeof (ctf_file_t));
556d876124dSJohn Birrell
557d876124dSJohn Birrell /*
558d876124dSJohn Birrell * Initialize the ctf_lookup_by_name top-level dictionary. We keep an
559d876124dSJohn Birrell * array of type name prefixes and the corresponding ctf_hash to use.
560d876124dSJohn Birrell * NOTE: This code must be kept in sync with the code in ctf_bufopen().
561d876124dSJohn Birrell */
562d876124dSJohn Birrell fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
563d876124dSJohn Birrell fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
564d876124dSJohn Birrell fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
565d876124dSJohn Birrell fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
566d876124dSJohn Birrell
567d876124dSJohn Birrell nfp->ctf_refcnt = 1; /* force nfp to be freed */
568d876124dSJohn Birrell ctf_close(nfp);
569d876124dSJohn Birrell
570d876124dSJohn Birrell return (0);
571d876124dSJohn Birrell }
572d876124dSJohn Birrell
573d876124dSJohn Birrell void
ctf_dtd_insert(ctf_file_t * fp,ctf_dtdef_t * dtd)574d876124dSJohn Birrell ctf_dtd_insert(ctf_file_t *fp, ctf_dtdef_t *dtd)
575d876124dSJohn Birrell {
576d876124dSJohn Birrell ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
577d876124dSJohn Birrell
578d876124dSJohn Birrell dtd->dtd_hash = fp->ctf_dthash[h];
579d876124dSJohn Birrell fp->ctf_dthash[h] = dtd;
580d876124dSJohn Birrell ctf_list_append(&fp->ctf_dtdefs, dtd);
581d876124dSJohn Birrell }
582d876124dSJohn Birrell
583d876124dSJohn Birrell void
ctf_dtd_delete(ctf_file_t * fp,ctf_dtdef_t * dtd)584d876124dSJohn Birrell ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd)
585d876124dSJohn Birrell {
586d876124dSJohn Birrell ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
587d876124dSJohn Birrell ctf_dtdef_t *p, **q = &fp->ctf_dthash[h];
588d876124dSJohn Birrell ctf_dmdef_t *dmd, *nmd;
589d876124dSJohn Birrell size_t len;
5903f0164abSXin LI int kind, i;
591d876124dSJohn Birrell
592d876124dSJohn Birrell for (p = *q; p != NULL; p = p->dtd_hash) {
593d876124dSJohn Birrell if (p != dtd)
594d876124dSJohn Birrell q = &p->dtd_hash;
595d876124dSJohn Birrell else
596d876124dSJohn Birrell break;
597d876124dSJohn Birrell }
598d876124dSJohn Birrell
599d876124dSJohn Birrell if (p != NULL)
600d876124dSJohn Birrell *q = p->dtd_hash;
601d876124dSJohn Birrell
602*a6fb8691SMark Johnston kind = LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info);
6033f0164abSXin LI switch (kind) {
604d876124dSJohn Birrell case CTF_K_STRUCT:
605d876124dSJohn Birrell case CTF_K_UNION:
606d876124dSJohn Birrell case CTF_K_ENUM:
607d876124dSJohn Birrell for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
608d876124dSJohn Birrell dmd != NULL; dmd = nmd) {
609d876124dSJohn Birrell if (dmd->dmd_name != NULL) {
610d876124dSJohn Birrell len = strlen(dmd->dmd_name) + 1;
611d876124dSJohn Birrell ctf_free(dmd->dmd_name, len);
612d876124dSJohn Birrell fp->ctf_dtstrlen -= len;
613d876124dSJohn Birrell }
6143f0164abSXin LI if (kind != CTF_K_ENUM)
6153f0164abSXin LI ctf_ref_dec(fp, dmd->dmd_type);
616d876124dSJohn Birrell nmd = ctf_list_next(dmd);
617d876124dSJohn Birrell ctf_free(dmd, sizeof (ctf_dmdef_t));
618d876124dSJohn Birrell }
619d876124dSJohn Birrell break;
620d876124dSJohn Birrell case CTF_K_FUNCTION:
6213f0164abSXin LI ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
622*a6fb8691SMark Johnston for (i = 0; i < LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info); i++)
6233f0164abSXin LI if (dtd->dtd_u.dtu_argv[i] != 0)
6243f0164abSXin LI ctf_ref_dec(fp, dtd->dtd_u.dtu_argv[i]);
625d876124dSJohn Birrell ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *
626*a6fb8691SMark Johnston LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info));
627d876124dSJohn Birrell break;
6283f0164abSXin LI case CTF_K_ARRAY:
6293f0164abSXin LI ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
6303f0164abSXin LI ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
6313f0164abSXin LI break;
6323f0164abSXin LI case CTF_K_TYPEDEF:
6333f0164abSXin LI ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
6343f0164abSXin LI break;
6353f0164abSXin LI case CTF_K_POINTER:
6363f0164abSXin LI case CTF_K_VOLATILE:
6373f0164abSXin LI case CTF_K_CONST:
6383f0164abSXin LI case CTF_K_RESTRICT:
6393f0164abSXin LI ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
6403f0164abSXin LI break;
641d876124dSJohn Birrell }
642d876124dSJohn Birrell
643d876124dSJohn Birrell if (dtd->dtd_name) {
644d876124dSJohn Birrell len = strlen(dtd->dtd_name) + 1;
645d876124dSJohn Birrell ctf_free(dtd->dtd_name, len);
646d876124dSJohn Birrell fp->ctf_dtstrlen -= len;
647d876124dSJohn Birrell }
648d876124dSJohn Birrell
649d876124dSJohn Birrell ctf_list_delete(&fp->ctf_dtdefs, dtd);
650d876124dSJohn Birrell ctf_free(dtd, sizeof (ctf_dtdef_t));
651d876124dSJohn Birrell }
652d876124dSJohn Birrell
653d876124dSJohn Birrell ctf_dtdef_t *
ctf_dtd_lookup(ctf_file_t * fp,ctf_id_t type)654d876124dSJohn Birrell ctf_dtd_lookup(ctf_file_t *fp, ctf_id_t type)
655d876124dSJohn Birrell {
656d876124dSJohn Birrell ulong_t h = type & (fp->ctf_dthashlen - 1);
657d876124dSJohn Birrell ctf_dtdef_t *dtd;
658d876124dSJohn Birrell
659d876124dSJohn Birrell if (fp->ctf_dthash == NULL)
660d876124dSJohn Birrell return (NULL);
661d876124dSJohn Birrell
662d876124dSJohn Birrell for (dtd = fp->ctf_dthash[h]; dtd != NULL; dtd = dtd->dtd_hash) {
663d876124dSJohn Birrell if (dtd->dtd_type == type)
664d876124dSJohn Birrell break;
665d876124dSJohn Birrell }
666d876124dSJohn Birrell
667d876124dSJohn Birrell return (dtd);
668d876124dSJohn Birrell }
669d876124dSJohn Birrell
670d876124dSJohn Birrell /*
671d876124dSJohn Birrell * Discard all of the dynamic type definitions that have been added to the
672d876124dSJohn Birrell * container since the last call to ctf_update(). We locate such types by
673d876124dSJohn Birrell * scanning the list and deleting elements that have type IDs greater than
6743f0164abSXin LI * ctf_dtoldid, which is set by ctf_update(), above. Note that to work properly
6753f0164abSXin LI * with our reference counting schemes, we must delete the dynamic list in
6763f0164abSXin LI * reverse.
677d876124dSJohn Birrell */
678d876124dSJohn Birrell int
ctf_discard(ctf_file_t * fp)679d876124dSJohn Birrell ctf_discard(ctf_file_t *fp)
680d876124dSJohn Birrell {
681d876124dSJohn Birrell ctf_dtdef_t *dtd, *ntd;
682d876124dSJohn Birrell
683d876124dSJohn Birrell if (!(fp->ctf_flags & LCTF_RDWR))
684d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_RDONLY));
685d876124dSJohn Birrell
686d876124dSJohn Birrell if (!(fp->ctf_flags & LCTF_DIRTY))
687d876124dSJohn Birrell return (0); /* no update required */
688d876124dSJohn Birrell
6893f0164abSXin LI for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
690ba8d15d3SMark Johnston ntd = ctf_list_prev(dtd);
691*a6fb8691SMark Johnston if (LCTF_TYPE_TO_INDEX(fp, dtd->dtd_type) <= fp->ctf_dtoldid)
692d876124dSJohn Birrell continue; /* skip types that have been committed */
693d876124dSJohn Birrell
694d876124dSJohn Birrell ctf_dtd_delete(fp, dtd);
695d876124dSJohn Birrell }
696d876124dSJohn Birrell
697d876124dSJohn Birrell fp->ctf_dtnextid = fp->ctf_dtoldid + 1;
698d876124dSJohn Birrell fp->ctf_flags &= ~LCTF_DIRTY;
699d876124dSJohn Birrell
700d876124dSJohn Birrell return (0);
701d876124dSJohn Birrell }
702d876124dSJohn Birrell
703d876124dSJohn Birrell static ctf_id_t
ctf_add_generic(ctf_file_t * fp,uint_t flag,const char * name,ctf_dtdef_t ** rp)704d876124dSJohn Birrell ctf_add_generic(ctf_file_t *fp, uint_t flag, const char *name, ctf_dtdef_t **rp)
705d876124dSJohn Birrell {
706d876124dSJohn Birrell ctf_dtdef_t *dtd;
707d876124dSJohn Birrell ctf_id_t type;
708d876124dSJohn Birrell char *s = NULL;
709d876124dSJohn Birrell
710d876124dSJohn Birrell if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
711d876124dSJohn Birrell return (ctf_set_errno(fp, EINVAL));
712d876124dSJohn Birrell
713d876124dSJohn Birrell if (!(fp->ctf_flags & LCTF_RDWR))
714d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_RDONLY));
715d876124dSJohn Birrell
716*a6fb8691SMark Johnston if (LCTF_INDEX_TO_TYPE(fp, fp->ctf_dtnextid, 1) > LCTF_MAX_TYPE(fp))
717d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_FULL));
718d876124dSJohn Birrell
719d876124dSJohn Birrell if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL)
720d876124dSJohn Birrell return (ctf_set_errno(fp, EAGAIN));
721d876124dSJohn Birrell
72296fbe519SJonathan T. Looney if (name != NULL && *name != '\0' && (s = ctf_strdup(name)) == NULL) {
723d876124dSJohn Birrell ctf_free(dtd, sizeof (ctf_dtdef_t));
724d876124dSJohn Birrell return (ctf_set_errno(fp, EAGAIN));
725d876124dSJohn Birrell }
726d876124dSJohn Birrell
727d876124dSJohn Birrell type = fp->ctf_dtnextid++;
728*a6fb8691SMark Johnston type = LCTF_INDEX_TO_TYPE(fp, type, (fp->ctf_flags & LCTF_CHILD));
729d876124dSJohn Birrell
730d876124dSJohn Birrell bzero(dtd, sizeof (ctf_dtdef_t));
731d876124dSJohn Birrell dtd->dtd_name = s;
732d876124dSJohn Birrell dtd->dtd_type = type;
733d876124dSJohn Birrell
734d876124dSJohn Birrell if (s != NULL)
735d876124dSJohn Birrell fp->ctf_dtstrlen += strlen(s) + 1;
736d876124dSJohn Birrell
737d876124dSJohn Birrell ctf_dtd_insert(fp, dtd);
738d876124dSJohn Birrell fp->ctf_flags |= LCTF_DIRTY;
739d876124dSJohn Birrell
740d876124dSJohn Birrell *rp = dtd;
741d876124dSJohn Birrell return (type);
742d876124dSJohn Birrell }
743d876124dSJohn Birrell
744d876124dSJohn Birrell /*
745d876124dSJohn Birrell * When encoding integer sizes, we want to convert a byte count in the range
746d876124dSJohn Birrell * 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
747d876124dSJohn Birrell * is a clever implementation from "Hacker's Delight" by Henry Warren, Jr.
748d876124dSJohn Birrell */
749d876124dSJohn Birrell static size_t
clp2(size_t x)750d876124dSJohn Birrell clp2(size_t x)
751d876124dSJohn Birrell {
752d876124dSJohn Birrell x--;
753d876124dSJohn Birrell
754d876124dSJohn Birrell x |= (x >> 1);
755d876124dSJohn Birrell x |= (x >> 2);
756d876124dSJohn Birrell x |= (x >> 4);
757d876124dSJohn Birrell x |= (x >> 8);
758d876124dSJohn Birrell x |= (x >> 16);
759d876124dSJohn Birrell
760d876124dSJohn Birrell return (x + 1);
761d876124dSJohn Birrell }
762d876124dSJohn Birrell
763d876124dSJohn Birrell static ctf_id_t
ctf_add_encoded(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep,uint_t kind)764d876124dSJohn Birrell ctf_add_encoded(ctf_file_t *fp, uint_t flag,
765d876124dSJohn Birrell const char *name, const ctf_encoding_t *ep, uint_t kind)
766d876124dSJohn Birrell {
767d876124dSJohn Birrell ctf_dtdef_t *dtd;
768d876124dSJohn Birrell ctf_id_t type;
769d876124dSJohn Birrell
770d876124dSJohn Birrell if (ep == NULL)
771d876124dSJohn Birrell return (ctf_set_errno(fp, EINVAL));
772d876124dSJohn Birrell
773d876124dSJohn Birrell if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
774d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
775d876124dSJohn Birrell
776*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, kind, flag, 0);
777d876124dSJohn Birrell dtd->dtd_data.ctt_size = clp2(P2ROUNDUP(ep->cte_bits, NBBY) / NBBY);
778d876124dSJohn Birrell dtd->dtd_u.dtu_enc = *ep;
779d876124dSJohn Birrell
780d876124dSJohn Birrell return (type);
781d876124dSJohn Birrell }
782d876124dSJohn Birrell
783d876124dSJohn Birrell static ctf_id_t
ctf_add_reftype(ctf_file_t * fp,uint_t flag,ctf_id_t ref,uint_t kind)784d876124dSJohn Birrell ctf_add_reftype(ctf_file_t *fp, uint_t flag, ctf_id_t ref, uint_t kind)
785d876124dSJohn Birrell {
786d876124dSJohn Birrell ctf_dtdef_t *dtd;
787d876124dSJohn Birrell ctf_id_t type;
788d876124dSJohn Birrell
789*a6fb8691SMark Johnston if (ref == CTF_ERR || ref > LCTF_MAX_TYPE(fp))
790d876124dSJohn Birrell return (ctf_set_errno(fp, EINVAL));
791d876124dSJohn Birrell
792d876124dSJohn Birrell if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)
793d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
794d876124dSJohn Birrell
7953f0164abSXin LI ctf_ref_inc(fp, ref);
7963f0164abSXin LI
797*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, kind, flag, 0);
798*a6fb8691SMark Johnston dtd->dtd_data.ctt_type = (uint_t)ref;
799d876124dSJohn Birrell
800d876124dSJohn Birrell return (type);
801d876124dSJohn Birrell }
802d876124dSJohn Birrell
803d876124dSJohn Birrell ctf_id_t
ctf_add_integer(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep)804d876124dSJohn Birrell ctf_add_integer(ctf_file_t *fp, uint_t flag,
805d876124dSJohn Birrell const char *name, const ctf_encoding_t *ep)
806d876124dSJohn Birrell {
807d876124dSJohn Birrell return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER));
808d876124dSJohn Birrell }
809d876124dSJohn Birrell
810d876124dSJohn Birrell ctf_id_t
ctf_add_float(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep)811d876124dSJohn Birrell ctf_add_float(ctf_file_t *fp, uint_t flag,
812d876124dSJohn Birrell const char *name, const ctf_encoding_t *ep)
813d876124dSJohn Birrell {
814d876124dSJohn Birrell return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT));
815d876124dSJohn Birrell }
816d876124dSJohn Birrell
817d876124dSJohn Birrell ctf_id_t
ctf_add_pointer(ctf_file_t * fp,uint_t flag,ctf_id_t ref)818d876124dSJohn Birrell ctf_add_pointer(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
819d876124dSJohn Birrell {
820d876124dSJohn Birrell return (ctf_add_reftype(fp, flag, ref, CTF_K_POINTER));
821d876124dSJohn Birrell }
822d876124dSJohn Birrell
823d876124dSJohn Birrell ctf_id_t
ctf_add_array(ctf_file_t * fp,uint_t flag,const ctf_arinfo_t * arp)824d876124dSJohn Birrell ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp)
825d876124dSJohn Birrell {
826d876124dSJohn Birrell ctf_dtdef_t *dtd;
827d876124dSJohn Birrell ctf_id_t type;
8283f0164abSXin LI ctf_file_t *fpd;
829d876124dSJohn Birrell
830d876124dSJohn Birrell if (arp == NULL)
831d876124dSJohn Birrell return (ctf_set_errno(fp, EINVAL));
832d876124dSJohn Birrell
8333f0164abSXin LI fpd = fp;
8343f0164abSXin LI if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
8353f0164abSXin LI ctf_dtd_lookup(fp, arp->ctr_contents) == NULL)
8363f0164abSXin LI return (ctf_set_errno(fp, ECTF_BADID));
8373f0164abSXin LI
8383f0164abSXin LI fpd = fp;
8393f0164abSXin LI if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
8403f0164abSXin LI ctf_dtd_lookup(fp, arp->ctr_index) == NULL)
8413f0164abSXin LI return (ctf_set_errno(fp, ECTF_BADID));
8423f0164abSXin LI
843d876124dSJohn Birrell if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)
844d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
845d876124dSJohn Birrell
846*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_ARRAY, flag, 0);
847d876124dSJohn Birrell dtd->dtd_data.ctt_size = 0;
848d876124dSJohn Birrell dtd->dtd_u.dtu_arr = *arp;
8493f0164abSXin LI ctf_ref_inc(fp, arp->ctr_contents);
8503f0164abSXin LI ctf_ref_inc(fp, arp->ctr_index);
851d876124dSJohn Birrell
852d876124dSJohn Birrell return (type);
853d876124dSJohn Birrell }
854d876124dSJohn Birrell
855d876124dSJohn Birrell int
ctf_set_array(ctf_file_t * fp,ctf_id_t type,const ctf_arinfo_t * arp)856d876124dSJohn Birrell ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
857d876124dSJohn Birrell {
8583f0164abSXin LI ctf_file_t *fpd;
859d876124dSJohn Birrell ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
860d876124dSJohn Birrell
861d876124dSJohn Birrell if (!(fp->ctf_flags & LCTF_RDWR))
862d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_RDONLY));
863d876124dSJohn Birrell
864*a6fb8691SMark Johnston if (dtd == NULL ||
865*a6fb8691SMark Johnston LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
866d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_BADID));
867d876124dSJohn Birrell
8683f0164abSXin LI fpd = fp;
8693f0164abSXin LI if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
8703f0164abSXin LI ctf_dtd_lookup(fp, arp->ctr_contents) == NULL)
8713f0164abSXin LI return (ctf_set_errno(fp, ECTF_BADID));
8723f0164abSXin LI
8733f0164abSXin LI fpd = fp;
8743f0164abSXin LI if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
8753f0164abSXin LI ctf_dtd_lookup(fp, arp->ctr_index) == NULL)
8763f0164abSXin LI return (ctf_set_errno(fp, ECTF_BADID));
8773f0164abSXin LI
8783f0164abSXin LI ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
8793f0164abSXin LI ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
880d876124dSJohn Birrell fp->ctf_flags |= LCTF_DIRTY;
881d876124dSJohn Birrell dtd->dtd_u.dtu_arr = *arp;
8823f0164abSXin LI ctf_ref_inc(fp, arp->ctr_contents);
8833f0164abSXin LI ctf_ref_inc(fp, arp->ctr_index);
884d876124dSJohn Birrell
885d876124dSJohn Birrell return (0);
886d876124dSJohn Birrell }
887d876124dSJohn Birrell
888d876124dSJohn Birrell ctf_id_t
ctf_add_function(ctf_file_t * fp,uint_t flag,const ctf_funcinfo_t * ctc,const ctf_id_t * argv)889d876124dSJohn Birrell ctf_add_function(ctf_file_t *fp, uint_t flag,
890d876124dSJohn Birrell const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
891d876124dSJohn Birrell {
892d876124dSJohn Birrell ctf_dtdef_t *dtd;
893d876124dSJohn Birrell ctf_id_t type;
894d876124dSJohn Birrell uint_t vlen;
8953f0164abSXin LI int i;
896d876124dSJohn Birrell ctf_id_t *vdat = NULL;
8973f0164abSXin LI ctf_file_t *fpd;
898d876124dSJohn Birrell
899d876124dSJohn Birrell if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 ||
900d876124dSJohn Birrell (ctc->ctc_argc != 0 && argv == NULL))
901d876124dSJohn Birrell return (ctf_set_errno(fp, EINVAL));
902d876124dSJohn Birrell
903d876124dSJohn Birrell vlen = ctc->ctc_argc;
904d876124dSJohn Birrell if (ctc->ctc_flags & CTF_FUNC_VARARG)
905d876124dSJohn Birrell vlen++; /* add trailing zero to indicate varargs (see below) */
906d876124dSJohn Birrell
907*a6fb8691SMark Johnston if (vlen > LCTF_MAX_VLEN(fp))
908d876124dSJohn Birrell return (ctf_set_errno(fp, EOVERFLOW));
909d876124dSJohn Birrell
9103f0164abSXin LI fpd = fp;
9113f0164abSXin LI if (ctf_lookup_by_id(&fpd, ctc->ctc_return) == NULL &&
9123f0164abSXin LI ctf_dtd_lookup(fp, ctc->ctc_return) == NULL)
9133f0164abSXin LI return (ctf_set_errno(fp, ECTF_BADID));
9143f0164abSXin LI
9153f0164abSXin LI for (i = 0; i < ctc->ctc_argc; i++) {
9163f0164abSXin LI fpd = fp;
9173f0164abSXin LI if (ctf_lookup_by_id(&fpd, argv[i]) == NULL &&
9183f0164abSXin LI ctf_dtd_lookup(fp, argv[i]) == NULL)
9193f0164abSXin LI return (ctf_set_errno(fp, ECTF_BADID));
9203f0164abSXin LI }
9213f0164abSXin LI
922d876124dSJohn Birrell if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL)
923d876124dSJohn Birrell return (ctf_set_errno(fp, EAGAIN));
924d876124dSJohn Birrell
925d876124dSJohn Birrell if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) {
926d876124dSJohn Birrell ctf_free(vdat, sizeof (ctf_id_t) * vlen);
927d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
928d876124dSJohn Birrell }
929d876124dSJohn Birrell
930*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_FUNCTION, flag, vlen);
931*a6fb8691SMark Johnston dtd->dtd_data.ctt_type = ctc->ctc_return;
932d876124dSJohn Birrell
9333f0164abSXin LI ctf_ref_inc(fp, ctc->ctc_return);
9343f0164abSXin LI for (i = 0; i < ctc->ctc_argc; i++)
9353f0164abSXin LI ctf_ref_inc(fp, argv[i]);
9363f0164abSXin LI
937d876124dSJohn Birrell bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc);
938d876124dSJohn Birrell if (ctc->ctc_flags & CTF_FUNC_VARARG)
939d876124dSJohn Birrell vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */
940d876124dSJohn Birrell dtd->dtd_u.dtu_argv = vdat;
941d876124dSJohn Birrell
942d876124dSJohn Birrell return (type);
943d876124dSJohn Birrell }
944d876124dSJohn Birrell
945d876124dSJohn Birrell ctf_id_t
ctf_add_struct(ctf_file_t * fp,uint_t flag,const char * name)946d876124dSJohn Birrell ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name)
947d876124dSJohn Birrell {
948d876124dSJohn Birrell ctf_hash_t *hp = &fp->ctf_structs;
949d876124dSJohn Birrell ctf_helem_t *hep = NULL;
950d876124dSJohn Birrell ctf_dtdef_t *dtd;
951d876124dSJohn Birrell ctf_id_t type;
952d876124dSJohn Birrell
953d876124dSJohn Birrell if (name != NULL)
954d876124dSJohn Birrell hep = ctf_hash_lookup(hp, fp, name, strlen(name));
955d876124dSJohn Birrell
956d876124dSJohn Birrell if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
957d876124dSJohn Birrell dtd = ctf_dtd_lookup(fp, type = hep->h_type);
958d876124dSJohn Birrell else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
959d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
960d876124dSJohn Birrell
961*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_STRUCT, flag, 0);
962d876124dSJohn Birrell dtd->dtd_data.ctt_size = 0;
963d876124dSJohn Birrell
964d876124dSJohn Birrell return (type);
965d876124dSJohn Birrell }
966d876124dSJohn Birrell
967d876124dSJohn Birrell ctf_id_t
ctf_add_union(ctf_file_t * fp,uint_t flag,const char * name)968d876124dSJohn Birrell ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name)
969d876124dSJohn Birrell {
970d876124dSJohn Birrell ctf_hash_t *hp = &fp->ctf_unions;
971d876124dSJohn Birrell ctf_helem_t *hep = NULL;
972d876124dSJohn Birrell ctf_dtdef_t *dtd;
973d876124dSJohn Birrell ctf_id_t type;
974d876124dSJohn Birrell
975d876124dSJohn Birrell if (name != NULL)
976d876124dSJohn Birrell hep = ctf_hash_lookup(hp, fp, name, strlen(name));
977d876124dSJohn Birrell
978d876124dSJohn Birrell if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
979d876124dSJohn Birrell dtd = ctf_dtd_lookup(fp, type = hep->h_type);
980d876124dSJohn Birrell else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
981d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
982d876124dSJohn Birrell
983*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_UNION, flag, 0);
984d876124dSJohn Birrell dtd->dtd_data.ctt_size = 0;
985d876124dSJohn Birrell
986d876124dSJohn Birrell return (type);
987d876124dSJohn Birrell }
988d876124dSJohn Birrell
989d876124dSJohn Birrell ctf_id_t
ctf_add_enum(ctf_file_t * fp,uint_t flag,const char * name)990d876124dSJohn Birrell ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name)
991d876124dSJohn Birrell {
992d876124dSJohn Birrell ctf_hash_t *hp = &fp->ctf_enums;
993d876124dSJohn Birrell ctf_helem_t *hep = NULL;
994d876124dSJohn Birrell ctf_dtdef_t *dtd;
995d876124dSJohn Birrell ctf_id_t type;
996d876124dSJohn Birrell
997d876124dSJohn Birrell if (name != NULL)
998d876124dSJohn Birrell hep = ctf_hash_lookup(hp, fp, name, strlen(name));
999d876124dSJohn Birrell
1000d876124dSJohn Birrell if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
1001d876124dSJohn Birrell dtd = ctf_dtd_lookup(fp, type = hep->h_type);
1002d876124dSJohn Birrell else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1003d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1004d876124dSJohn Birrell
1005*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_ENUM, flag, 0);
1006d876124dSJohn Birrell dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
1007d876124dSJohn Birrell
1008d876124dSJohn Birrell return (type);
1009d876124dSJohn Birrell }
1010d876124dSJohn Birrell
1011d876124dSJohn Birrell ctf_id_t
ctf_add_forward(ctf_file_t * fp,uint_t flag,const char * name,uint_t kind)1012d876124dSJohn Birrell ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind)
1013d876124dSJohn Birrell {
1014d876124dSJohn Birrell ctf_hash_t *hp;
1015d876124dSJohn Birrell ctf_helem_t *hep;
1016d876124dSJohn Birrell ctf_dtdef_t *dtd;
1017d876124dSJohn Birrell ctf_id_t type;
1018d876124dSJohn Birrell
1019d876124dSJohn Birrell switch (kind) {
1020d876124dSJohn Birrell case CTF_K_STRUCT:
1021d876124dSJohn Birrell hp = &fp->ctf_structs;
1022d876124dSJohn Birrell break;
1023d876124dSJohn Birrell case CTF_K_UNION:
1024d876124dSJohn Birrell hp = &fp->ctf_unions;
1025d876124dSJohn Birrell break;
1026d876124dSJohn Birrell case CTF_K_ENUM:
1027d876124dSJohn Birrell hp = &fp->ctf_enums;
1028d876124dSJohn Birrell break;
1029d876124dSJohn Birrell default:
1030d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_NOTSUE));
1031d876124dSJohn Birrell }
1032d876124dSJohn Birrell
1033d876124dSJohn Birrell /*
1034d876124dSJohn Birrell * If the type is already defined or exists as a forward tag, just
1035d876124dSJohn Birrell * return the ctf_id_t of the existing definition.
1036d876124dSJohn Birrell */
1037d876124dSJohn Birrell if (name != NULL && (hep = ctf_hash_lookup(hp,
1038d876124dSJohn Birrell fp, name, strlen(name))) != NULL)
1039d876124dSJohn Birrell return (hep->h_type);
1040d876124dSJohn Birrell
1041d876124dSJohn Birrell if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1042d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1043d876124dSJohn Birrell
1044*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_FORWARD, flag, 0);
1045d876124dSJohn Birrell dtd->dtd_data.ctt_type = kind;
1046d876124dSJohn Birrell
1047d876124dSJohn Birrell return (type);
1048d876124dSJohn Birrell }
1049d876124dSJohn Birrell
1050d876124dSJohn Birrell ctf_id_t
ctf_add_typedef(ctf_file_t * fp,uint_t flag,const char * name,ctf_id_t ref)1051d876124dSJohn Birrell ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1052d876124dSJohn Birrell {
1053d876124dSJohn Birrell ctf_dtdef_t *dtd;
1054d876124dSJohn Birrell ctf_id_t type;
10553f0164abSXin LI ctf_file_t *fpd;
1056d876124dSJohn Birrell
10573f0164abSXin LI fpd = fp;
10583f0164abSXin LI if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL &&
10593f0164abSXin LI ctf_dtd_lookup(fp, ref) == NULL))
1060d876124dSJohn Birrell return (ctf_set_errno(fp, EINVAL));
1061d876124dSJohn Birrell
1062d876124dSJohn Birrell if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1063d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1064d876124dSJohn Birrell
1065*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_TYPEDEF, flag, 0);
1066*a6fb8691SMark Johnston dtd->dtd_data.ctt_type = ref;
10673f0164abSXin LI ctf_ref_inc(fp, ref);
1068d876124dSJohn Birrell
1069d876124dSJohn Birrell return (type);
1070d876124dSJohn Birrell }
1071d876124dSJohn Birrell
1072d876124dSJohn Birrell ctf_id_t
ctf_add_volatile(ctf_file_t * fp,uint_t flag,ctf_id_t ref)1073d876124dSJohn Birrell ctf_add_volatile(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
1074d876124dSJohn Birrell {
1075d876124dSJohn Birrell return (ctf_add_reftype(fp, flag, ref, CTF_K_VOLATILE));
1076d876124dSJohn Birrell }
1077d876124dSJohn Birrell
1078d876124dSJohn Birrell ctf_id_t
ctf_add_const(ctf_file_t * fp,uint_t flag,ctf_id_t ref)1079d876124dSJohn Birrell ctf_add_const(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
1080d876124dSJohn Birrell {
1081d876124dSJohn Birrell return (ctf_add_reftype(fp, flag, ref, CTF_K_CONST));
1082d876124dSJohn Birrell }
1083d876124dSJohn Birrell
1084d876124dSJohn Birrell ctf_id_t
ctf_add_restrict(ctf_file_t * fp,uint_t flag,ctf_id_t ref)1085d876124dSJohn Birrell ctf_add_restrict(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
1086d876124dSJohn Birrell {
1087d876124dSJohn Birrell return (ctf_add_reftype(fp, flag, ref, CTF_K_RESTRICT));
1088d876124dSJohn Birrell }
1089d876124dSJohn Birrell
1090d876124dSJohn Birrell int
ctf_add_enumerator(ctf_file_t * fp,ctf_id_t enid,const char * name,int value)1091d876124dSJohn Birrell ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)
1092d876124dSJohn Birrell {
1093d876124dSJohn Birrell ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);
1094d876124dSJohn Birrell ctf_dmdef_t *dmd;
1095d876124dSJohn Birrell
1096d876124dSJohn Birrell uint_t kind, vlen, root;
1097d876124dSJohn Birrell char *s;
1098d876124dSJohn Birrell
1099d876124dSJohn Birrell if (name == NULL)
1100d876124dSJohn Birrell return (ctf_set_errno(fp, EINVAL));
1101d876124dSJohn Birrell
1102d876124dSJohn Birrell if (!(fp->ctf_flags & LCTF_RDWR))
1103d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_RDONLY));
1104d876124dSJohn Birrell
1105d876124dSJohn Birrell if (dtd == NULL)
1106d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_BADID));
1107d876124dSJohn Birrell
1108*a6fb8691SMark Johnston kind = LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info);
1109*a6fb8691SMark Johnston root = LCTF_INFO_ROOT(fp, dtd->dtd_data.ctt_info);
1110*a6fb8691SMark Johnston vlen = LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info);
1111d876124dSJohn Birrell
1112d876124dSJohn Birrell if (kind != CTF_K_ENUM)
1113d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_NOTENUM));
1114d876124dSJohn Birrell
1115*a6fb8691SMark Johnston if (vlen > LCTF_MAX_VLEN(fp))
1116d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_DTFULL));
1117d876124dSJohn Birrell
1118d876124dSJohn Birrell for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1119d876124dSJohn Birrell dmd != NULL; dmd = ctf_list_next(dmd)) {
1120d876124dSJohn Birrell if (strcmp(dmd->dmd_name, name) == 0)
1121d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1122d876124dSJohn Birrell }
1123d876124dSJohn Birrell
1124d876124dSJohn Birrell if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1125d876124dSJohn Birrell return (ctf_set_errno(fp, EAGAIN));
1126d876124dSJohn Birrell
1127d876124dSJohn Birrell if ((s = ctf_strdup(name)) == NULL) {
1128d876124dSJohn Birrell ctf_free(dmd, sizeof (ctf_dmdef_t));
1129d876124dSJohn Birrell return (ctf_set_errno(fp, EAGAIN));
1130d876124dSJohn Birrell }
1131d876124dSJohn Birrell
1132d876124dSJohn Birrell dmd->dmd_name = s;
1133d876124dSJohn Birrell dmd->dmd_type = CTF_ERR;
1134d876124dSJohn Birrell dmd->dmd_offset = 0;
1135d876124dSJohn Birrell dmd->dmd_value = value;
1136d876124dSJohn Birrell
1137*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, kind, root, vlen + 1);
1138d876124dSJohn Birrell ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1139d876124dSJohn Birrell
1140d876124dSJohn Birrell fp->ctf_dtstrlen += strlen(s) + 1;
1141d876124dSJohn Birrell fp->ctf_flags |= LCTF_DIRTY;
1142d876124dSJohn Birrell
1143d876124dSJohn Birrell return (0);
1144d876124dSJohn Birrell }
1145d876124dSJohn Birrell
1146d876124dSJohn Birrell int
ctf_add_member(ctf_file_t * fp,ctf_id_t souid,const char * name,ctf_id_t type)1147d876124dSJohn Birrell ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type)
1148d876124dSJohn Birrell {
1149d876124dSJohn Birrell ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);
1150d876124dSJohn Birrell ctf_dmdef_t *dmd;
1151d876124dSJohn Birrell
1152d876124dSJohn Birrell ssize_t msize, malign, ssize;
1153d876124dSJohn Birrell uint_t kind, vlen, root;
1154d876124dSJohn Birrell char *s = NULL;
1155d876124dSJohn Birrell
1156d876124dSJohn Birrell if (!(fp->ctf_flags & LCTF_RDWR))
1157d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_RDONLY));
1158d876124dSJohn Birrell
1159d876124dSJohn Birrell if (dtd == NULL)
1160d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_BADID));
1161d876124dSJohn Birrell
1162*a6fb8691SMark Johnston kind = LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info);
1163*a6fb8691SMark Johnston root = LCTF_INFO_ROOT(fp, dtd->dtd_data.ctt_info);
1164*a6fb8691SMark Johnston vlen = LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info);
1165d876124dSJohn Birrell
1166d876124dSJohn Birrell if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
1167d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_NOTSOU));
1168d876124dSJohn Birrell
1169*a6fb8691SMark Johnston if (vlen > LCTF_MAX_VLEN(fp))
1170d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_DTFULL));
1171d876124dSJohn Birrell
1172d876124dSJohn Birrell if (name != NULL) {
1173d876124dSJohn Birrell for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1174d876124dSJohn Birrell dmd != NULL; dmd = ctf_list_next(dmd)) {
1175d876124dSJohn Birrell if (dmd->dmd_name != NULL &&
1176d876124dSJohn Birrell strcmp(dmd->dmd_name, name) == 0)
1177d876124dSJohn Birrell return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1178d876124dSJohn Birrell }
1179d876124dSJohn Birrell }
1180d876124dSJohn Birrell
1181d876124dSJohn Birrell if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||
1182d876124dSJohn Birrell (malign = ctf_type_align(fp, type)) == CTF_ERR)
1183d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1184d876124dSJohn Birrell
1185d876124dSJohn Birrell if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1186d876124dSJohn Birrell return (ctf_set_errno(fp, EAGAIN));
1187d876124dSJohn Birrell
1188d876124dSJohn Birrell if (name != NULL && (s = ctf_strdup(name)) == NULL) {
1189d876124dSJohn Birrell ctf_free(dmd, sizeof (ctf_dmdef_t));
1190d876124dSJohn Birrell return (ctf_set_errno(fp, EAGAIN));
1191d876124dSJohn Birrell }
1192d876124dSJohn Birrell
1193d876124dSJohn Birrell dmd->dmd_name = s;
1194d876124dSJohn Birrell dmd->dmd_type = type;
1195d876124dSJohn Birrell dmd->dmd_value = -1;
1196d876124dSJohn Birrell
1197d876124dSJohn Birrell if (kind == CTF_K_STRUCT && vlen != 0) {
1198d876124dSJohn Birrell ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);
1199d876124dSJohn Birrell ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);
1200d876124dSJohn Birrell size_t off = lmd->dmd_offset;
1201d876124dSJohn Birrell
1202d876124dSJohn Birrell ctf_encoding_t linfo;
1203d876124dSJohn Birrell ssize_t lsize;
1204d876124dSJohn Birrell
1205d876124dSJohn Birrell if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)
1206d876124dSJohn Birrell off += linfo.cte_bits;
1207d876124dSJohn Birrell else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)
1208d876124dSJohn Birrell off += lsize * NBBY;
1209d876124dSJohn Birrell
1210d876124dSJohn Birrell /*
1211d876124dSJohn Birrell * Round up the offset of the end of the last member to the
1212d876124dSJohn Birrell * next byte boundary, convert 'off' to bytes, and then round
1213d876124dSJohn Birrell * it up again to the next multiple of the alignment required
1214d876124dSJohn Birrell * by the new member. Finally, convert back to bits and store
1215d876124dSJohn Birrell * the result in dmd_offset. Technically we could do more
1216d876124dSJohn Birrell * efficient packing if the new member is a bit-field, but
1217d876124dSJohn Birrell * we're the "compiler" and ANSI says we can do as we choose.
1218d876124dSJohn Birrell */
1219d876124dSJohn Birrell off = roundup(off, NBBY) / NBBY;
1220d876124dSJohn Birrell off = roundup(off, MAX(malign, 1));
1221d876124dSJohn Birrell dmd->dmd_offset = off * NBBY;
1222d876124dSJohn Birrell ssize = off + msize;
1223d876124dSJohn Birrell } else {
1224d876124dSJohn Birrell dmd->dmd_offset = 0;
1225d876124dSJohn Birrell ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
1226d876124dSJohn Birrell ssize = MAX(ssize, msize);
1227d876124dSJohn Birrell }
1228d876124dSJohn Birrell
1229*a6fb8691SMark Johnston if (ssize > LCTF_MAX_SIZE(fp)) {
1230*a6fb8691SMark Johnston dtd->dtd_data.ctt_size = LCTF_LSIZE_SENT(fp);
1231d876124dSJohn Birrell dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);
1232d876124dSJohn Birrell dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);
1233d876124dSJohn Birrell } else
1234*a6fb8691SMark Johnston dtd->dtd_data.ctt_size = ssize;
1235d876124dSJohn Birrell
1236*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, kind, root, vlen + 1);
1237d876124dSJohn Birrell ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1238d876124dSJohn Birrell
1239d876124dSJohn Birrell if (s != NULL)
1240d876124dSJohn Birrell fp->ctf_dtstrlen += strlen(s) + 1;
1241d876124dSJohn Birrell
12423f0164abSXin LI ctf_ref_inc(fp, type);
12433f0164abSXin LI fp->ctf_flags |= LCTF_DIRTY;
12443f0164abSXin LI return (0);
12453f0164abSXin LI }
12463f0164abSXin LI
12473f0164abSXin LI /*
12483f0164abSXin LI * This removes a type from the dynamic section. This will fail if the type is
12493f0164abSXin LI * referenced by another type. Note that the CTF ID is never reused currently by
12503f0164abSXin LI * CTF. Note that if this container is a parent container then we just outright
12513f0164abSXin LI * refuse to remove the type. There currently is no notion of searching for the
12523f0164abSXin LI * ctf_dtdef_t in parent containers. If there is, then this constraint could
12533f0164abSXin LI * become finer grained.
12543f0164abSXin LI */
12553f0164abSXin LI int
ctf_delete_type(ctf_file_t * fp,ctf_id_t type)12563f0164abSXin LI ctf_delete_type(ctf_file_t *fp, ctf_id_t type)
12573f0164abSXin LI {
12583f0164abSXin LI ctf_file_t *fpd;
12593f0164abSXin LI ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
12603f0164abSXin LI
12613f0164abSXin LI if (!(fp->ctf_flags & LCTF_RDWR))
12623f0164abSXin LI return (ctf_set_errno(fp, ECTF_RDONLY));
12633f0164abSXin LI
12643f0164abSXin LI /*
12653f0164abSXin LI * We want to give as useful an errno as possible. That means that we
12663f0164abSXin LI * want to distinguish between a type which does not exist and one for
12673f0164abSXin LI * which the type is not dynamic.
12683f0164abSXin LI */
12693f0164abSXin LI fpd = fp;
12703f0164abSXin LI if (ctf_lookup_by_id(&fpd, type) == NULL &&
12713f0164abSXin LI ctf_dtd_lookup(fp, type) == NULL)
12723f0164abSXin LI return (CTF_ERR); /* errno is set for us */
12733f0164abSXin LI
12743f0164abSXin LI if (dtd == NULL)
12753f0164abSXin LI return (ctf_set_errno(fp, ECTF_NOTDYN));
12763f0164abSXin LI
12773f0164abSXin LI if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1)
12783f0164abSXin LI return (ctf_set_errno(fp, ECTF_REFERENCED));
12793f0164abSXin LI
12803f0164abSXin LI ctf_dtd_delete(fp, dtd);
1281d876124dSJohn Birrell fp->ctf_flags |= LCTF_DIRTY;
1282d876124dSJohn Birrell return (0);
1283d876124dSJohn Birrell }
1284d876124dSJohn Birrell
1285d876124dSJohn Birrell static int
enumcmp(const char * name,int value,void * arg)1286d876124dSJohn Birrell enumcmp(const char *name, int value, void *arg)
1287d876124dSJohn Birrell {
1288d876124dSJohn Birrell ctf_bundle_t *ctb = arg;
1289d876124dSJohn Birrell int bvalue;
1290d876124dSJohn Birrell
1291d876124dSJohn Birrell return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type,
1292d876124dSJohn Birrell name, &bvalue) == CTF_ERR || value != bvalue);
1293d876124dSJohn Birrell }
1294d876124dSJohn Birrell
1295d876124dSJohn Birrell static int
enumadd(const char * name,int value,void * arg)1296d876124dSJohn Birrell enumadd(const char *name, int value, void *arg)
1297d876124dSJohn Birrell {
1298d876124dSJohn Birrell ctf_bundle_t *ctb = arg;
1299d876124dSJohn Birrell
1300d876124dSJohn Birrell return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type,
1301d876124dSJohn Birrell name, value) == CTF_ERR);
1302d876124dSJohn Birrell }
1303d876124dSJohn Birrell
1304d876124dSJohn Birrell static int
membadd(const char * name,ctf_id_t type,ulong_t offset,void * arg)1305d876124dSJohn Birrell membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg)
1306d876124dSJohn Birrell {
1307d876124dSJohn Birrell ctf_bundle_t *ctb = arg;
1308d876124dSJohn Birrell ctf_dmdef_t *dmd;
1309d876124dSJohn Birrell char *s = NULL;
1310d876124dSJohn Birrell
1311d876124dSJohn Birrell if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1312d876124dSJohn Birrell return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1313d876124dSJohn Birrell
131496fbe519SJonathan T. Looney if (name != NULL && *name != '\0' && (s = ctf_strdup(name)) == NULL) {
1315d876124dSJohn Birrell ctf_free(dmd, sizeof (ctf_dmdef_t));
1316d876124dSJohn Birrell return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1317d876124dSJohn Birrell }
1318d876124dSJohn Birrell
1319d876124dSJohn Birrell /*
1320d876124dSJohn Birrell * For now, dmd_type is copied as the src_fp's type; it is reset to an
1321d876124dSJohn Birrell * equivalent dst_fp type by a final loop in ctf_add_type(), below.
1322d876124dSJohn Birrell */
1323d876124dSJohn Birrell dmd->dmd_name = s;
1324d876124dSJohn Birrell dmd->dmd_type = type;
1325d876124dSJohn Birrell dmd->dmd_offset = offset;
1326d876124dSJohn Birrell dmd->dmd_value = -1;
1327d876124dSJohn Birrell
1328d876124dSJohn Birrell ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
1329d876124dSJohn Birrell
1330d876124dSJohn Birrell if (s != NULL)
1331d876124dSJohn Birrell ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1;
1332d876124dSJohn Birrell
1333d876124dSJohn Birrell ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
1334d876124dSJohn Birrell return (0);
1335d876124dSJohn Birrell }
1336d876124dSJohn Birrell
1337105fd928SMark Johnston static long
soucmp(ctf_file_t * src_fp,ctf_id_t src_type,ctf_file_t * dst_fp,ctf_id_t dst_type)1338105fd928SMark Johnston soucmp(ctf_file_t *src_fp, ctf_id_t src_type, ctf_file_t *dst_fp,
1339105fd928SMark Johnston ctf_id_t dst_type)
1340105fd928SMark Johnston {
1341*a6fb8691SMark Johnston const void *src_tp, *dst_tp;
1342105fd928SMark Johnston const char *src_name, *dst_name;
1343105fd928SMark Johnston ssize_t src_sz, dst_sz, src_inc, dst_inc;
1344*a6fb8691SMark Johnston uint_t dst_kind, dst_vlen, src_kind, src_vlen, n;
1345105fd928SMark Johnston
1346105fd928SMark Johnston if ((src_type = ctf_type_resolve(src_fp, src_type)) == CTF_ERR)
1347105fd928SMark Johnston return (CTF_ERR);
1348105fd928SMark Johnston if ((dst_type = ctf_type_resolve(dst_fp, dst_type)) == CTF_ERR)
1349105fd928SMark Johnston return (CTF_ERR);
1350105fd928SMark Johnston
1351105fd928SMark Johnston if ((src_tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)
1352105fd928SMark Johnston return (CTF_ERR);
1353105fd928SMark Johnston if ((dst_tp = ctf_lookup_by_id(&dst_fp, dst_type)) == NULL)
1354105fd928SMark Johnston return (CTF_ERR);
1355105fd928SMark Johnston
1356*a6fb8691SMark Johnston ctf_get_ctt_info(src_fp, src_tp, &src_kind, &src_vlen, NULL);
1357*a6fb8691SMark Johnston ctf_get_ctt_info(dst_fp, dst_tp, &dst_kind, &dst_vlen, NULL);
1358*a6fb8691SMark Johnston
1359*a6fb8691SMark Johnston if (src_kind != dst_kind)
1360105fd928SMark Johnston return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1361*a6fb8691SMark Johnston if (src_kind != CTF_K_STRUCT && src_kind != CTF_K_UNION)
1362105fd928SMark Johnston return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1363*a6fb8691SMark Johnston if (src_vlen != dst_vlen)
1364105fd928SMark Johnston return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1365105fd928SMark Johnston
1366105fd928SMark Johnston (void) ctf_get_ctt_size(src_fp, src_tp, &src_sz, &src_inc);
1367105fd928SMark Johnston (void) ctf_get_ctt_size(dst_fp, dst_tp, &dst_sz, &dst_inc);
1368*a6fb8691SMark Johnston if (src_sz != dst_sz)
1369105fd928SMark Johnston return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1370105fd928SMark Johnston
1371*a6fb8691SMark Johnston const char *src_mp, *dst_mp;
1372*a6fb8691SMark Johnston ulong_t src_offset, dst_offset;
1373105fd928SMark Johnston
1374*a6fb8691SMark Johnston src_mp = (const char *)src_tp + src_inc;
1375*a6fb8691SMark Johnston dst_mp = (const char *)dst_tp + dst_inc;
1376*a6fb8691SMark Johnston for (n = src_vlen; n != 0;
1377*a6fb8691SMark Johnston n--, src_mp += src_inc, dst_mp += dst_inc) {
1378*a6fb8691SMark Johnston ctf_get_ctm_info(src_fp, src_mp, src_sz, &src_inc, NULL,
1379*a6fb8691SMark Johnston &src_offset, &src_name);
1380*a6fb8691SMark Johnston ctf_get_ctm_info(dst_fp, dst_mp, dst_sz, &dst_inc, NULL,
1381*a6fb8691SMark Johnston &dst_offset, &dst_name);
1382*a6fb8691SMark Johnston
1383*a6fb8691SMark Johnston if (src_offset != dst_offset)
1384105fd928SMark Johnston return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1385105fd928SMark Johnston if (strcmp(src_name, dst_name) != 0)
1386105fd928SMark Johnston return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1387105fd928SMark Johnston }
1388105fd928SMark Johnston
1389105fd928SMark Johnston return (0);
1390105fd928SMark Johnston }
1391105fd928SMark Johnston
1392d876124dSJohn Birrell /*
1393d876124dSJohn Birrell * The ctf_add_type routine is used to copy a type from a source CTF container
1394d876124dSJohn Birrell * to a dynamic destination container. This routine operates recursively by
1395d876124dSJohn Birrell * following the source type's links and embedded member types. If the
1396d876124dSJohn Birrell * destination container already contains a named type which has the same
1397d876124dSJohn Birrell * attributes, then we succeed and return this type but no changes occur.
1398d876124dSJohn Birrell */
1399d876124dSJohn Birrell ctf_id_t
ctf_add_type(ctf_file_t * dst_fp,ctf_file_t * src_fp,ctf_id_t src_type)1400d876124dSJohn Birrell ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
1401d876124dSJohn Birrell {
1402d876124dSJohn Birrell ctf_id_t dst_type = CTF_ERR;
1403d876124dSJohn Birrell uint_t dst_kind = CTF_K_UNKNOWN;
1404d876124dSJohn Birrell
1405*a6fb8691SMark Johnston const void *tp;
1406d876124dSJohn Birrell const char *name;
1407*a6fb8691SMark Johnston uint_t type, kind, flag, vlen;
1408d876124dSJohn Birrell
1409d876124dSJohn Birrell ctf_bundle_t src, dst;
14103cbb4cc2SJonathan T. Looney ctf_encoding_t src_en, main_en, dst_en;
1411d876124dSJohn Birrell ctf_arinfo_t src_ar, dst_ar;
1412d876124dSJohn Birrell
1413d876124dSJohn Birrell ctf_dtdef_t *dtd;
1414d876124dSJohn Birrell ctf_funcinfo_t ctc;
1415d876124dSJohn Birrell ssize_t size;
1416d876124dSJohn Birrell
1417d876124dSJohn Birrell ctf_hash_t *hp;
1418d876124dSJohn Birrell ctf_helem_t *hep;
1419d876124dSJohn Birrell
14203f0164abSXin LI if (dst_fp == src_fp)
14213f0164abSXin LI return (src_type);
14223f0164abSXin LI
1423d876124dSJohn Birrell if (!(dst_fp->ctf_flags & LCTF_RDWR))
1424d876124dSJohn Birrell return (ctf_set_errno(dst_fp, ECTF_RDONLY));
1425d876124dSJohn Birrell
1426d876124dSJohn Birrell if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)
1427d876124dSJohn Birrell return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1428d876124dSJohn Birrell
1429*a6fb8691SMark Johnston name = ctf_type_rname(src_fp, tp);
1430*a6fb8691SMark Johnston
1431*a6fb8691SMark Johnston ctf_get_ctt_info(src_fp, tp, &kind, &vlen, &flag);
1432d876124dSJohn Birrell
1433d876124dSJohn Birrell switch (kind) {
1434d876124dSJohn Birrell case CTF_K_STRUCT:
1435d876124dSJohn Birrell hp = &dst_fp->ctf_structs;
1436d876124dSJohn Birrell break;
1437d876124dSJohn Birrell case CTF_K_UNION:
1438d876124dSJohn Birrell hp = &dst_fp->ctf_unions;
1439d876124dSJohn Birrell break;
1440d876124dSJohn Birrell case CTF_K_ENUM:
1441d876124dSJohn Birrell hp = &dst_fp->ctf_enums;
1442d876124dSJohn Birrell break;
1443d876124dSJohn Birrell default:
1444d876124dSJohn Birrell hp = &dst_fp->ctf_names;
1445d876124dSJohn Birrell break;
1446d876124dSJohn Birrell }
1447d876124dSJohn Birrell
1448d876124dSJohn Birrell /*
1449d876124dSJohn Birrell * If the source type has a name and is a root type (visible at the
1450d876124dSJohn Birrell * top-level scope), lookup the name in the destination container and
1451d876124dSJohn Birrell * verify that it is of the same kind before we do anything else.
1452d876124dSJohn Birrell */
1453d876124dSJohn Birrell if ((flag & CTF_ADD_ROOT) && name[0] != '\0' &&
1454d876124dSJohn Birrell (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) {
1455d876124dSJohn Birrell dst_type = (ctf_id_t)hep->h_type;
1456d876124dSJohn Birrell dst_kind = ctf_type_kind(dst_fp, dst_type);
1457d876124dSJohn Birrell }
1458d876124dSJohn Birrell
1459d876124dSJohn Birrell /*
1460d876124dSJohn Birrell * If an identically named dst_type exists, fail with ECTF_CONFLICT
1461d876124dSJohn Birrell * unless dst_type is a forward declaration and src_type is a struct,
1462d876124dSJohn Birrell * union, or enum (i.e. the definition of the previous forward decl).
1463d876124dSJohn Birrell */
1464f810bf0eSMark Johnston if (dst_type != CTF_ERR && dst_kind != kind) {
1465f810bf0eSMark Johnston if (dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM &&
1466f810bf0eSMark Johnston kind != CTF_K_STRUCT && kind != CTF_K_UNION))
1467d876124dSJohn Birrell return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1468f810bf0eSMark Johnston else
1469f810bf0eSMark Johnston dst_type = CTF_ERR;
1470f810bf0eSMark Johnston }
1471d876124dSJohn Birrell
1472d876124dSJohn Birrell /*
1473d876124dSJohn Birrell * If the non-empty name was not found in the appropriate hash, search
1474d876124dSJohn Birrell * the list of pending dynamic definitions that are not yet committed.
1475d876124dSJohn Birrell * If a matching name and kind are found, assume this is the type that
1476d876124dSJohn Birrell * we are looking for. This is necessary to permit ctf_add_type() to
1477d876124dSJohn Birrell * operate recursively on entities such as a struct that contains a
1478d876124dSJohn Birrell * pointer member that refers to the same struct type.
14793e5645b7SMark Johnston *
14803e5645b7SMark Johnston * In the case of integer and floating point types, we match using the
14813e5645b7SMark Johnston * type encoding as well - else we may incorrectly return a bitfield
14823e5645b7SMark Johnston * type, for instance.
1483d876124dSJohn Birrell */
1484d876124dSJohn Birrell if (dst_type == CTF_ERR && name[0] != '\0') {
1485d876124dSJohn Birrell for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL &&
1486*a6fb8691SMark Johnston LCTF_TYPE_TO_INDEX(dst_fp, dtd->dtd_type) >
1487*a6fb8691SMark Johnston dst_fp->ctf_dtoldid; dtd = ctf_list_prev(dtd)) {
1488*a6fb8691SMark Johnston if (LCTF_INFO_KIND(dst_fp, dtd->dtd_data.ctt_info) !=
1489*a6fb8691SMark Johnston kind || dtd->dtd_name == NULL ||
14903e5645b7SMark Johnston strcmp(dtd->dtd_name, name) != 0)
14913e5645b7SMark Johnston continue;
14923e5645b7SMark Johnston if (kind == CTF_K_INTEGER || kind == CTF_K_FLOAT) {
14933e5645b7SMark Johnston if (ctf_type_encoding(src_fp, src_type,
14943e5645b7SMark Johnston &src_en) != 0)
14953e5645b7SMark Johnston continue;
14963e5645b7SMark Johnston if (bcmp(&src_en, &dtd->dtd_u.dtu_enc,
14973e5645b7SMark Johnston sizeof (ctf_encoding_t)) != 0)
14983e5645b7SMark Johnston continue;
14993e5645b7SMark Johnston }
1500d876124dSJohn Birrell return (dtd->dtd_type);
1501d876124dSJohn Birrell }
1502d876124dSJohn Birrell }
1503d876124dSJohn Birrell
1504d876124dSJohn Birrell src.ctb_file = src_fp;
1505d876124dSJohn Birrell src.ctb_type = src_type;
1506d876124dSJohn Birrell src.ctb_dtd = NULL;
1507d876124dSJohn Birrell
1508d876124dSJohn Birrell dst.ctb_file = dst_fp;
1509d876124dSJohn Birrell dst.ctb_type = dst_type;
1510d876124dSJohn Birrell dst.ctb_dtd = NULL;
1511d876124dSJohn Birrell
1512d876124dSJohn Birrell /*
1513d876124dSJohn Birrell * Now perform kind-specific processing. If dst_type is CTF_ERR, then
1514d876124dSJohn Birrell * we add a new type with the same properties as src_type to dst_fp.
1515d876124dSJohn Birrell * If dst_type is not CTF_ERR, then we verify that dst_type has the
1516d876124dSJohn Birrell * same attributes as src_type. We recurse for embedded references.
1517d876124dSJohn Birrell */
1518d876124dSJohn Birrell switch (kind) {
1519d876124dSJohn Birrell case CTF_K_INTEGER:
1520d876124dSJohn Birrell case CTF_K_FLOAT:
1521d876124dSJohn Birrell if (ctf_type_encoding(src_fp, src_type, &src_en) != 0)
1522d876124dSJohn Birrell return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1523d876124dSJohn Birrell
15243cbb4cc2SJonathan T. Looney /*
15253cbb4cc2SJonathan T. Looney * This could be a bitfield, and the CTF library assumes
15263cbb4cc2SJonathan T. Looney * intrinsics will appear before bitfields. Therefore,
15273cbb4cc2SJonathan T. Looney * try to copy over the intrinsic prior to copying the
15283cbb4cc2SJonathan T. Looney * bitfield.
15293cbb4cc2SJonathan T. Looney */
15303cbb4cc2SJonathan T. Looney if (dst_type == CTF_ERR && name[0] != '\0' &&
15313cbb4cc2SJonathan T. Looney (hep = ctf_hash_lookup(&src_fp->ctf_names, src_fp, name,
15323cbb4cc2SJonathan T. Looney strlen(name))) != NULL &&
15333cbb4cc2SJonathan T. Looney src_type != (ctf_id_t)hep->h_type) {
15343cbb4cc2SJonathan T. Looney if (ctf_type_encoding(src_fp, (ctf_id_t)hep->h_type,
15353cbb4cc2SJonathan T. Looney &main_en) != 0) {
15363cbb4cc2SJonathan T. Looney return (ctf_set_errno(dst_fp,
15373cbb4cc2SJonathan T. Looney ctf_errno(src_fp)));
15383cbb4cc2SJonathan T. Looney }
15393cbb4cc2SJonathan T. Looney if (bcmp(&src_en, &main_en, sizeof (ctf_encoding_t)) &&
15403cbb4cc2SJonathan T. Looney ctf_add_type(dst_fp, src_fp,
15413cbb4cc2SJonathan T. Looney (ctf_id_t)hep->h_type) == CTF_ERR)
15423cbb4cc2SJonathan T. Looney return (CTF_ERR); /* errno is set for us */
15433cbb4cc2SJonathan T. Looney }
15443cbb4cc2SJonathan T. Looney
1545d876124dSJohn Birrell if (dst_type != CTF_ERR) {
1546d876124dSJohn Birrell if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0)
1547d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1548d876124dSJohn Birrell
1549d876124dSJohn Birrell if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t)))
1550d876124dSJohn Birrell return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1551d876124dSJohn Birrell
1552d876124dSJohn Birrell } else if (kind == CTF_K_INTEGER) {
1553d876124dSJohn Birrell dst_type = ctf_add_integer(dst_fp, flag, name, &src_en);
1554d876124dSJohn Birrell } else
1555d876124dSJohn Birrell dst_type = ctf_add_float(dst_fp, flag, name, &src_en);
1556d876124dSJohn Birrell break;
1557d876124dSJohn Birrell
1558d876124dSJohn Birrell case CTF_K_POINTER:
1559d876124dSJohn Birrell case CTF_K_VOLATILE:
1560d876124dSJohn Birrell case CTF_K_CONST:
1561d876124dSJohn Birrell case CTF_K_RESTRICT:
1562d876124dSJohn Birrell src_type = ctf_type_reference(src_fp, src_type);
1563d876124dSJohn Birrell src_type = ctf_add_type(dst_fp, src_fp, src_type);
1564d876124dSJohn Birrell
1565d876124dSJohn Birrell if (src_type == CTF_ERR)
1566d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1567d876124dSJohn Birrell
1568d876124dSJohn Birrell dst_type = ctf_add_reftype(dst_fp, flag, src_type, kind);
1569d876124dSJohn Birrell break;
1570d876124dSJohn Birrell
1571d876124dSJohn Birrell case CTF_K_ARRAY:
1572d876124dSJohn Birrell if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR)
1573d876124dSJohn Birrell return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1574d876124dSJohn Birrell
1575d876124dSJohn Birrell src_ar.ctr_contents =
1576d876124dSJohn Birrell ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents);
1577d876124dSJohn Birrell src_ar.ctr_index =
1578d876124dSJohn Birrell ctf_add_type(dst_fp, src_fp, src_ar.ctr_index);
1579d876124dSJohn Birrell src_ar.ctr_nelems = src_ar.ctr_nelems;
1580d876124dSJohn Birrell
1581d876124dSJohn Birrell if (src_ar.ctr_contents == CTF_ERR ||
1582d876124dSJohn Birrell src_ar.ctr_index == CTF_ERR)
1583d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1584d876124dSJohn Birrell
1585d876124dSJohn Birrell if (dst_type != CTF_ERR) {
1586d876124dSJohn Birrell if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0)
1587d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1588d876124dSJohn Birrell
1589d876124dSJohn Birrell if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
1590d876124dSJohn Birrell return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1591d876124dSJohn Birrell } else
1592d876124dSJohn Birrell dst_type = ctf_add_array(dst_fp, flag, &src_ar);
1593d876124dSJohn Birrell break;
1594d876124dSJohn Birrell
1595d876124dSJohn Birrell case CTF_K_FUNCTION:
1596*a6fb8691SMark Johnston ctf_get_ctt_index(src_fp, tp, NULL, &type, NULL);
1597*a6fb8691SMark Johnston ctc.ctc_return = ctf_add_type(dst_fp, src_fp, type);
1598d876124dSJohn Birrell ctc.ctc_argc = 0;
1599d876124dSJohn Birrell ctc.ctc_flags = 0;
1600d876124dSJohn Birrell
1601d876124dSJohn Birrell if (ctc.ctc_return == CTF_ERR)
1602d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1603d876124dSJohn Birrell
1604d876124dSJohn Birrell dst_type = ctf_add_function(dst_fp, flag, &ctc, NULL);
1605d876124dSJohn Birrell break;
1606d876124dSJohn Birrell
1607d876124dSJohn Birrell case CTF_K_STRUCT:
1608d876124dSJohn Birrell case CTF_K_UNION: {
1609d876124dSJohn Birrell ctf_dmdef_t *dmd;
1610d876124dSJohn Birrell int errs = 0;
1611d876124dSJohn Birrell
1612d876124dSJohn Birrell if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1613105fd928SMark Johnston /*
1614105fd928SMark Johnston * Compare the sizes and fields of the two types.
1615105fd928SMark Johnston * The field comparisons only check the names and
1616105fd928SMark Johnston * offsets, so this is not perfect but is good enough
1617105fd928SMark Johnston * for scenarios that we care about.
1618105fd928SMark Johnston */
1619105fd928SMark Johnston if (soucmp(src_fp, src_type, dst_fp, dst_type) != 0)
1620105fd928SMark Johnston return (CTF_ERR); /* errno is set for us */
1621d876124dSJohn Birrell break;
1622d876124dSJohn Birrell }
1623d876124dSJohn Birrell
1624d876124dSJohn Birrell /*
1625d876124dSJohn Birrell * Unlike the other cases, copying structs and unions is done
1626d876124dSJohn Birrell * manually so as to avoid repeated lookups in ctf_add_member
1627d876124dSJohn Birrell * and to ensure the exact same member offsets as in src_type.
1628d876124dSJohn Birrell */
1629d876124dSJohn Birrell dst_type = ctf_add_generic(dst_fp, flag, name, &dtd);
1630d876124dSJohn Birrell if (dst_type == CTF_ERR)
1631d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1632d876124dSJohn Birrell
1633d876124dSJohn Birrell dst.ctb_type = dst_type;
1634d876124dSJohn Birrell dst.ctb_dtd = dtd;
1635d876124dSJohn Birrell
1636d876124dSJohn Birrell if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0)
1637d876124dSJohn Birrell errs++; /* increment errs and fail at bottom of case */
1638d876124dSJohn Birrell
1639*a6fb8691SMark Johnston if ((size = ctf_type_size(src_fp, src_type)) >
1640*a6fb8691SMark Johnston LCTF_MAX_SIZE(src_fp)) {
1641*a6fb8691SMark Johnston dtd->dtd_data.ctt_size = LCTF_LSIZE_SENT(dst_fp);
1642d876124dSJohn Birrell dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
1643d876124dSJohn Birrell dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
1644d876124dSJohn Birrell } else
1645*a6fb8691SMark Johnston dtd->dtd_data.ctt_size = size;
1646d876124dSJohn Birrell
1647*a6fb8691SMark Johnston dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(dst_fp, kind, flag,
1648*a6fb8691SMark Johnston vlen);
1649d876124dSJohn Birrell
1650d876124dSJohn Birrell /*
1651d876124dSJohn Birrell * Make a final pass through the members changing each dmd_type
1652d876124dSJohn Birrell * (a src_fp type) to an equivalent type in dst_fp. We pass
1653d876124dSJohn Birrell * through all members, leaving any that fail set to CTF_ERR.
1654d876124dSJohn Birrell */
1655d876124dSJohn Birrell for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1656d876124dSJohn Birrell dmd != NULL; dmd = ctf_list_next(dmd)) {
1657d876124dSJohn Birrell if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp,
1658d876124dSJohn Birrell dmd->dmd_type)) == CTF_ERR)
1659d876124dSJohn Birrell errs++;
1660d876124dSJohn Birrell }
1661d876124dSJohn Birrell
1662d876124dSJohn Birrell if (errs)
1663d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
16643f0164abSXin LI
16653f0164abSXin LI /*
16663f0164abSXin LI * Now that we know that we can't fail, we go through and bump
16673f0164abSXin LI * all the reference counts on the member types.
16683f0164abSXin LI */
16693f0164abSXin LI for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
16703f0164abSXin LI dmd != NULL; dmd = ctf_list_next(dmd))
16713f0164abSXin LI ctf_ref_inc(dst_fp, dmd->dmd_type);
1672d876124dSJohn Birrell break;
1673d876124dSJohn Birrell }
1674d876124dSJohn Birrell
1675d876124dSJohn Birrell case CTF_K_ENUM:
1676d876124dSJohn Birrell if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1677d876124dSJohn Birrell if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) ||
1678d876124dSJohn Birrell ctf_enum_iter(dst_fp, dst_type, enumcmp, &src))
1679d876124dSJohn Birrell return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1680d876124dSJohn Birrell } else {
1681d876124dSJohn Birrell dst_type = ctf_add_enum(dst_fp, flag, name);
1682d876124dSJohn Birrell if ((dst.ctb_type = dst_type) == CTF_ERR ||
1683d876124dSJohn Birrell ctf_enum_iter(src_fp, src_type, enumadd, &dst))
1684d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1685d876124dSJohn Birrell }
1686d876124dSJohn Birrell break;
1687d876124dSJohn Birrell
1688d876124dSJohn Birrell case CTF_K_FORWARD:
1689d876124dSJohn Birrell if (dst_type == CTF_ERR) {
1690d876124dSJohn Birrell dst_type = ctf_add_forward(dst_fp,
1691d876124dSJohn Birrell flag, name, CTF_K_STRUCT); /* assume STRUCT */
1692d876124dSJohn Birrell }
1693d876124dSJohn Birrell break;
1694d876124dSJohn Birrell
1695d876124dSJohn Birrell case CTF_K_TYPEDEF:
1696d876124dSJohn Birrell src_type = ctf_type_reference(src_fp, src_type);
1697d876124dSJohn Birrell src_type = ctf_add_type(dst_fp, src_fp, src_type);
1698d876124dSJohn Birrell
1699d876124dSJohn Birrell if (src_type == CTF_ERR)
1700d876124dSJohn Birrell return (CTF_ERR); /* errno is set for us */
1701d876124dSJohn Birrell
1702d876124dSJohn Birrell /*
1703d876124dSJohn Birrell * If dst_type is not CTF_ERR at this point, we should check if
1704d876124dSJohn Birrell * ctf_type_reference(dst_fp, dst_type) != src_type and if so
1705d876124dSJohn Birrell * fail with ECTF_CONFLICT. However, this causes problems with
1706d876124dSJohn Birrell * <sys/types.h> typedefs that vary based on things like if
1707d876124dSJohn Birrell * _ILP32x then pid_t is int otherwise long. We therefore omit
1708d876124dSJohn Birrell * this check and assume that if the identically named typedef
1709d876124dSJohn Birrell * already exists in dst_fp, it is correct or equivalent.
1710d876124dSJohn Birrell */
1711d876124dSJohn Birrell if (dst_type == CTF_ERR) {
1712d876124dSJohn Birrell dst_type = ctf_add_typedef(dst_fp, flag,
1713d876124dSJohn Birrell name, src_type);
1714d876124dSJohn Birrell }
1715d876124dSJohn Birrell break;
1716d876124dSJohn Birrell
1717d876124dSJohn Birrell default:
1718d876124dSJohn Birrell return (ctf_set_errno(dst_fp, ECTF_CORRUPT));
1719d876124dSJohn Birrell }
1720d876124dSJohn Birrell
1721d876124dSJohn Birrell return (dst_type);
1722d876124dSJohn Birrell }
1723