1a864dc36Sdarran /*
2a864dc36Sdarran * CDDL HEADER START
3a864dc36Sdarran *
4a864dc36Sdarran * The contents of this file are subject to the terms of the
5a864dc36Sdarran * Common Development and Distribution License, Version 1.0 only
6a864dc36Sdarran * (the "License"). You may not use this file except in compliance
7a864dc36Sdarran * with the License.
8a864dc36Sdarran *
9a864dc36Sdarran * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10a864dc36Sdarran * or http://www.opensolaris.org/os/licensing.
11a864dc36Sdarran * See the License for the specific language governing permissions
12a864dc36Sdarran * and limitations under the License.
13a864dc36Sdarran *
14a864dc36Sdarran * When distributing Covered Code, include this CDDL HEADER in each
15a864dc36Sdarran * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16a864dc36Sdarran * If applicable, add the following below this CDDL HEADER, with the
17a864dc36Sdarran * fields enclosed by brackets "[]" replaced with your own identifying
18a864dc36Sdarran * information: Portions Copyright [yyyy] [name of copyright owner]
19a864dc36Sdarran *
20a864dc36Sdarran * CDDL HEADER END
21a864dc36Sdarran */
22*81dbb529Schristos #ifdef HAVE_NBTOOL_CONFIG_H
23*81dbb529Schristos #include "nbtool_config.h"
24*81dbb529Schristos #endif
25a864dc36Sdarran /*
26a864dc36Sdarran * Copyright 2002-2003 Sun Microsystems, Inc. All rights reserved.
27a864dc36Sdarran * Use is subject to license terms.
28a864dc36Sdarran */
29a864dc36Sdarran
30a864dc36Sdarran #pragma ident "%Z%%M% %I% %E% SMI"
31a864dc36Sdarran
32a864dc36Sdarran #include <ctf_impl.h>
33a864dc36Sdarran
34a864dc36Sdarran static int
extract_label_info(ctf_file_t * fp,const ctf_lblent_t ** ctl,uint_t * num_labels)35a864dc36Sdarran extract_label_info(ctf_file_t *fp, const ctf_lblent_t **ctl, uint_t *num_labels)
36a864dc36Sdarran {
37a864dc36Sdarran const ctf_header_t *h;
38a864dc36Sdarran
39a864dc36Sdarran /*
40a864dc36Sdarran * Labels are only supported in V2 or later
41a864dc36Sdarran */
42a864dc36Sdarran if (fp->ctf_version < CTF_VERSION_2)
43a864dc36Sdarran return (ctf_set_errno(fp, ECTF_NOTSUP));
44a864dc36Sdarran
45a864dc36Sdarran h = (const ctf_header_t *)fp->ctf_data.cts_data;
46a864dc36Sdarran
47a864dc36Sdarran /* LINTED - pointer alignment */
48a864dc36Sdarran *ctl = (const ctf_lblent_t *)(fp->ctf_buf + h->cth_lbloff);
49a864dc36Sdarran *num_labels = (h->cth_objtoff - h->cth_lbloff) / sizeof (ctf_lblent_t);
50a864dc36Sdarran
51a864dc36Sdarran return (0);
52a864dc36Sdarran }
53a864dc36Sdarran
54a864dc36Sdarran /*
55a864dc36Sdarran * Returns the topmost label, or NULL if any errors are encountered
56a864dc36Sdarran */
57a864dc36Sdarran const char *
ctf_label_topmost(ctf_file_t * fp)58a864dc36Sdarran ctf_label_topmost(ctf_file_t *fp)
59a864dc36Sdarran {
6074f0118dSchristos const ctf_lblent_t *ctlp = NULL; // XXX: gcc
61a864dc36Sdarran const char *s;
6274f0118dSchristos uint_t num_labels = 0; // XXX: gcc
63a864dc36Sdarran
64a864dc36Sdarran if (extract_label_info(fp, &ctlp, &num_labels) == CTF_ERR)
65a864dc36Sdarran return (NULL); /* errno is set */
66a864dc36Sdarran
67a864dc36Sdarran if (num_labels == 0) {
68a864dc36Sdarran (void) ctf_set_errno(fp, ECTF_NOLABELDATA);
69a864dc36Sdarran return (NULL);
70a864dc36Sdarran }
71a864dc36Sdarran
72a864dc36Sdarran if ((s = ctf_strraw(fp, (ctlp + num_labels - 1)->ctl_label)) == NULL)
73a864dc36Sdarran (void) ctf_set_errno(fp, ECTF_CORRUPT);
74a864dc36Sdarran
75a864dc36Sdarran return (s);
76a864dc36Sdarran }
77a864dc36Sdarran
78a864dc36Sdarran /*
79a864dc36Sdarran * Iterate over all labels. We pass the label string and the lblinfo_t struct
80a864dc36Sdarran * to the specified callback function.
81a864dc36Sdarran */
82a864dc36Sdarran int
ctf_label_iter(ctf_file_t * fp,ctf_label_f * func,void * arg)83a864dc36Sdarran ctf_label_iter(ctf_file_t *fp, ctf_label_f *func, void *arg)
84a864dc36Sdarran {
8574f0118dSchristos const ctf_lblent_t *ctlp = NULL; // XXX: gcc
8674f0118dSchristos uint_t i, num_labels = 0; // XXX: gcc
87a864dc36Sdarran ctf_lblinfo_t linfo;
88a864dc36Sdarran const char *lname;
89a864dc36Sdarran int rc;
90a864dc36Sdarran
91a864dc36Sdarran if (extract_label_info(fp, &ctlp, &num_labels) == CTF_ERR)
92a864dc36Sdarran return (CTF_ERR); /* errno is set */
93a864dc36Sdarran
94a864dc36Sdarran if (num_labels == 0)
95a864dc36Sdarran return (ctf_set_errno(fp, ECTF_NOLABELDATA));
96a864dc36Sdarran
97a864dc36Sdarran for (i = 0; i < num_labels; i++, ctlp++) {
98a864dc36Sdarran if ((lname = ctf_strraw(fp, ctlp->ctl_label)) == NULL) {
99a864dc36Sdarran ctf_dprintf("failed to decode label %u with "
100a864dc36Sdarran "typeidx %u\n", ctlp->ctl_label, ctlp->ctl_typeidx);
101a864dc36Sdarran return (ctf_set_errno(fp, ECTF_CORRUPT));
102a864dc36Sdarran }
103a864dc36Sdarran
104a864dc36Sdarran linfo.ctb_typeidx = ctlp->ctl_typeidx;
105a864dc36Sdarran if ((rc = func(lname, &linfo, arg)) != 0)
106a864dc36Sdarran return (rc);
107a864dc36Sdarran }
108a864dc36Sdarran
109a864dc36Sdarran return (0);
110a864dc36Sdarran }
111a864dc36Sdarran
112a864dc36Sdarran typedef struct linfo_cb_arg {
113a864dc36Sdarran const char *lca_name; /* Label we want to retrieve info for */
114a864dc36Sdarran ctf_lblinfo_t *lca_info; /* Where to store the info about the label */
115a864dc36Sdarran } linfo_cb_arg_t;
116a864dc36Sdarran
117a864dc36Sdarran static int
label_info_cb(const char * lname,const ctf_lblinfo_t * linfo,void * arg)118a864dc36Sdarran label_info_cb(const char *lname, const ctf_lblinfo_t *linfo, void *arg)
119a864dc36Sdarran {
120a864dc36Sdarran /*
121a864dc36Sdarran * If lname matches the label we are looking for, copy the
122a864dc36Sdarran * lblinfo_t struct for the caller.
123a864dc36Sdarran */
124a864dc36Sdarran if (strcmp(lname, ((linfo_cb_arg_t *)arg)->lca_name) == 0) {
125a864dc36Sdarran /*
126a864dc36Sdarran * Allow caller not to allocate storage to test if label exists
127a864dc36Sdarran */
128a864dc36Sdarran if (((linfo_cb_arg_t *)arg)->lca_info != NULL)
129a864dc36Sdarran bcopy(linfo, ((linfo_cb_arg_t *)arg)->lca_info,
130a864dc36Sdarran sizeof (ctf_lblinfo_t));
131a864dc36Sdarran return (1); /* Indicate we found a match */
132a864dc36Sdarran }
133a864dc36Sdarran
134a864dc36Sdarran return (0);
135a864dc36Sdarran }
136a864dc36Sdarran
137a864dc36Sdarran /*
138a864dc36Sdarran * Retrieve information about the label with name "lname"
139a864dc36Sdarran */
140a864dc36Sdarran int
ctf_label_info(ctf_file_t * fp,const char * lname,ctf_lblinfo_t * linfo)141a864dc36Sdarran ctf_label_info(ctf_file_t *fp, const char *lname, ctf_lblinfo_t *linfo)
142a864dc36Sdarran {
143a864dc36Sdarran linfo_cb_arg_t cb_arg;
144a864dc36Sdarran int rc;
145a864dc36Sdarran
146a864dc36Sdarran cb_arg.lca_name = lname;
147a864dc36Sdarran cb_arg.lca_info = linfo;
148a864dc36Sdarran
149a864dc36Sdarran if ((rc = ctf_label_iter(fp, label_info_cb, &cb_arg)) == CTF_ERR)
150a864dc36Sdarran return (rc);
151a864dc36Sdarran
152a864dc36Sdarran if (rc != 1)
153a864dc36Sdarran return (ctf_set_errno(fp, ECTF_NOLABEL));
154a864dc36Sdarran
155a864dc36Sdarran return (0);
156a864dc36Sdarran }
157