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