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