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