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