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
23 #ifdef HAVE_NBTOOL_CONFIG_H
24 #include "nbtool_config.h"
25 #endif
26 /*
27 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30
31 #pragma ident "%Z%%M% %I% %E% SMI"
32
33 #include <sys/sysmacros.h>
34 #include <ctf_impl.h>
35
36 /*
37 * Compare the given input string and length against a table of known C storage
38 * qualifier keywords. We just ignore these in ctf_lookup_by_name, below. To
39 * do this quickly, we use a pre-computed Perfect Hash Function similar to the
40 * technique originally described in the classic paper:
41 *
42 * R.J. Cichelli, "Minimal Perfect Hash Functions Made Simple",
43 * Communications of the ACM, Volume 23, Issue 1, January 1980, pp. 17-19.
44 *
45 * For an input string S of length N, we use hash H = S[N - 1] + N - 105, which
46 * for the current set of qualifiers yields a unique H in the range [0 .. 20].
47 * The hash can be modified when the keyword set changes as necessary. We also
48 * store the length of each keyword and check it prior to the final strcmp().
49 */
50 static int
isqualifier(const char * s,size_t len)51 isqualifier(const char *s, size_t len)
52 {
53 static const struct qual {
54 const char *q_name;
55 size_t q_len;
56 } qhash[] = {
57 { "static", 6 }, { "", 0 }, { "", 0 }, { "", 0 },
58 { "volatile", 8 }, { "", 0 }, { "", 0 }, { "", 0 }, { "", 0 },
59 { "", 0 }, { "auto", 4 }, { "extern", 6 }, { "", 0 }, { "", 0 },
60 { "", 0 }, { "", 0 }, { "const", 5 }, { "register", 8 },
61 { "", 0 }, { "restrict", 8 }, { "_Restrict", 9 }
62 };
63
64 int h = s[len - 1] + (int)len - 105;
65 const struct qual *qp;
66
67 if (h < 0 || h >= sizeof (qhash) / sizeof (qhash[0]))
68 return (0);
69 qp = &qhash[h];
70 return (len == qp->q_len && strncmp(qp->q_name, s, qp->q_len) == 0);
71 }
72
73 /*
74 * Attempt to convert the given C type name into the corresponding CTF type ID.
75 * It is not possible to do complete and proper conversion of type names
76 * without implementing a more full-fledged parser, which is necessary to
77 * handle things like types that are function pointers to functions that
78 * have arguments that are function pointers, and fun stuff like that.
79 * Instead, this function implements a very simple conversion algorithm that
80 * finds the things that we actually care about: structs, unions, enums,
81 * integers, floats, typedefs, and pointers to any of these named types.
82 */
83 ctf_id_t
ctf_lookup_by_name(ctf_file_t * fp,const char * name)84 ctf_lookup_by_name(ctf_file_t *fp, const char *name)
85 {
86 static const char delimiters[] = " \t\n\r\v\f*";
87
88 const ctf_lookup_t *lp;
89 const ctf_helem_t *hp;
90 const char *p, *q, *end;
91 ctf_id_t type = 0;
92 ctf_id_t ntype, ptype;
93
94 if (name == NULL)
95 return (ctf_set_errno(fp, EINVAL));
96
97 for (p = name, end = name + strlen(name); *p != '\0'; p = q) {
98 while (isspace((unsigned char)*p))
99 p++; /* skip leading ws */
100
101 if (p == end)
102 break;
103
104 if ((q = strpbrk(p + 1, delimiters)) == NULL)
105 q = end; /* compare until end */
106
107 if (*p == '*') {
108 /*
109 * Find a pointer to type by looking in fp->ctf_ptrtab.
110 * If we can't find a pointer to the given type, see if
111 * we can compute a pointer to the type resulting from
112 * resolving the type down to its base type and use
113 * that instead. This helps with cases where the CTF
114 * data includes "struct foo *" but not "foo_t *" and
115 * the user tries to access "foo_t *" in the debugger.
116 */
117 ntype = fp->ctf_ptrtab[LCTF_TYPE_TO_INDEX(fp, type)];
118 if (ntype == 0) {
119 ntype = ctf_type_resolve(fp, type);
120 if (ntype == CTF_ERR || (ntype = fp->ctf_ptrtab[
121 LCTF_TYPE_TO_INDEX(fp, ntype)]) == 0) {
122 (void) ctf_set_errno(fp, ECTF_NOTYPE);
123 goto err;
124 }
125 }
126
127 type = LCTF_INDEX_TO_TYPE(fp, ntype,
128 (fp->ctf_flags & LCTF_CHILD));
129
130 q = p + 1;
131 continue;
132 }
133
134 if (isqualifier(p, (size_t)(q - p)))
135 continue; /* skip qualifier keyword */
136
137 for (lp = fp->ctf_lookups; lp->ctl_prefix != NULL; lp++) {
138 if (lp->ctl_prefix[0] == '\0' ||
139 ((size_t)(q - p) >= lp->ctl_len && strncmp(p,
140 lp->ctl_prefix, (size_t)(q - p)) == 0)) {
141 for (p += lp->ctl_len; isspace((unsigned char)*p); p++)
142 continue; /* skip prefix and next ws */
143
144 if ((q = strchr(p, '*')) == NULL)
145 q = end; /* compare until end */
146
147 while (isspace((unsigned char)q[-1]))
148 q--; /* exclude trailing ws */
149
150 if ((hp = ctf_hash_lookup(lp->ctl_hash, fp, p,
151 (size_t)(q - p))) == NULL) {
152 (void) ctf_set_errno(fp, ECTF_NOTYPE);
153 goto err;
154 }
155
156 type = hp->h_type;
157 break;
158 }
159 }
160
161 if (lp->ctl_prefix == NULL) {
162 (void) ctf_set_errno(fp, ECTF_NOTYPE);
163 goto err;
164 }
165 }
166
167 if (*p != '\0' || type == 0)
168 return (ctf_set_errno(fp, ECTF_SYNTAX));
169
170 return (type);
171
172 err:
173 if (fp->ctf_parent != NULL &&
174 (ptype = ctf_lookup_by_name(fp->ctf_parent, name)) != CTF_ERR)
175 return (ptype);
176
177 return (CTF_ERR);
178 }
179
180 /*
181 * Given a symbol table index, return the type of the data object described
182 * by the corresponding entry in the symbol table.
183 */
184 ctf_id_t
ctf_lookup_by_symbol(ctf_file_t * fp,ulong_t symidx)185 ctf_lookup_by_symbol(ctf_file_t *fp, ulong_t symidx)
186 {
187 const ctf_sect_t *sp = &fp->ctf_symtab;
188 ctf_id_t type;
189
190 if (sp->cts_data == NULL)
191 return (ctf_set_errno(fp, ECTF_NOSYMTAB));
192
193 if (symidx >= fp->ctf_nsyms)
194 return (ctf_set_errno(fp, EINVAL));
195
196 if (sp->cts_entsize == sizeof (Elf32_Sym)) {
197 const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;
198 if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT)
199 return (ctf_set_errno(fp, ECTF_NOTDATA));
200 } else {
201 const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;
202 if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT)
203 return (ctf_set_errno(fp, ECTF_NOTDATA));
204 }
205
206 if (fp->ctf_sxlate[symidx] == -1u)
207 return (ctf_set_errno(fp, ECTF_NOTYPEDAT));
208
209 type = *(uint_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);
210 if (type == 0)
211 return (ctf_set_errno(fp, ECTF_NOTYPEDAT));
212
213 return (type);
214 }
215
216 /*
217 * Return the pointer to the internal CTF type data corresponding to the
218 * given type ID. If the ID is invalid, the function returns NULL.
219 * This function is not exported outside of the library.
220 */
221 const void *
ctf_lookup_by_id(ctf_file_t ** fpp,ctf_id_t type)222 ctf_lookup_by_id(ctf_file_t **fpp, ctf_id_t type)
223 {
224 ctf_file_t *fp = *fpp; /* caller passes in starting CTF container */
225
226 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT(fp, type)) {
227 if (fp->ctf_parent == NULL) {
228 (void) ctf_set_errno(*fpp, ECTF_NOPARENT);
229 return (NULL);
230 }
231
232 /* The parent may be using a different CTF version. */
233 type = LCTF_TYPE_TO_INDEX(fp, type);
234 fp = fp->ctf_parent;
235 } else {
236 type = LCTF_TYPE_TO_INDEX(fp, type);
237 }
238
239 if (type > 0 && type <= fp->ctf_typemax) {
240 *fpp = fp; /* function returns ending CTF container */
241 return (LCTF_INDEX_TO_TYPEPTR(fp, type));
242 }
243
244 (void) ctf_set_errno(fp, ECTF_BADID);
245 return (NULL);
246 }
247
248 /*
249 * Given a symbol table index, return the info for the function described
250 * by the corresponding entry in the symbol table.
251 */
252 int
ctf_func_info(ctf_file_t * fp,ulong_t symidx,ctf_funcinfo_t * fip)253 ctf_func_info(ctf_file_t *fp, ulong_t symidx, ctf_funcinfo_t *fip)
254 {
255 const ctf_sect_t *sp = &fp->ctf_symtab;
256 const uint_t *dp;
257 uint_t info, kind, n;
258
259 if (sp->cts_data == NULL)
260 return (ctf_set_errno(fp, ECTF_NOSYMTAB));
261
262 if (symidx >= fp->ctf_nsyms)
263 return (ctf_set_errno(fp, EINVAL));
264
265 if (sp->cts_entsize == sizeof (Elf32_Sym)) {
266 const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;
267 if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC)
268 return (ctf_set_errno(fp, ECTF_NOTFUNC));
269 } else {
270 const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;
271 if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC)
272 return (ctf_set_errno(fp, ECTF_NOTFUNC));
273 }
274
275 if (fp->ctf_sxlate[symidx] == -1u)
276 return (ctf_set_errno(fp, ECTF_NOFUNCDAT));
277
278 dp = (uint_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);
279
280 info = *dp++;
281 kind = LCTF_INFO_KIND(fp, info);
282 n = LCTF_INFO_VLEN(fp, info);
283
284 if (kind == CTF_K_UNKNOWN && n == 0)
285 return (ctf_set_errno(fp, ECTF_NOFUNCDAT));
286
287 if (kind != CTF_K_FUNCTION)
288 return (ctf_set_errno(fp, ECTF_CORRUPT));
289
290 fip->ctc_return = *dp++;
291 fip->ctc_argc = n;
292 fip->ctc_flags = 0;
293
294 if (n != 0 && dp[n - 1] == 0) {
295 fip->ctc_flags |= CTF_FUNC_VARARG;
296 fip->ctc_argc--;
297 }
298
299 return (0);
300 }
301
302 /*
303 * Given a symbol table index, return the arguments for the function described
304 * by the corresponding entry in the symbol table.
305 */
306 int
ctf_func_args(ctf_file_t * fp,ulong_t symidx,uint_t argc,ctf_id_t * argv)307 ctf_func_args(ctf_file_t *fp, ulong_t symidx, uint_t argc, ctf_id_t *argv)
308 {
309 const uint_t *dp;
310 ctf_funcinfo_t f;
311
312 if (ctf_func_info(fp, symidx, &f) == CTF_ERR)
313 return (CTF_ERR); /* errno is set for us */
314
315 /*
316 * The argument data is two uint_t's past the translation table
317 * offset: one for the function info, and one for the return type.
318 */
319 dp = (uint_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]) + 2;
320
321 for (argc = MIN(argc, f.ctc_argc); argc != 0; argc--)
322 *argv++ = *dp++;
323
324 return (0);
325 }
326