1 /* Symbol, variable and name lookup.
2 Copyright (C) 2019 Free Software Foundation, Inc.
4 This file is part of libctf.
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
24 /* Compare the given input string and length against a table of known C storage
25 qualifier keywords. We just ignore these in ctf_lookup_by_name, below. To
26 do this quickly, we use a pre-computed Perfect Hash Function similar to the
27 technique originally described in the classic paper:
29 R.J. Cichelli, "Minimal Perfect Hash Functions Made Simple",
30 Communications of the ACM, Volume 23, Issue 1, January 1980, pp. 17-19.
32 For an input string S of length N, we use hash H = S[N - 1] + N - 105, which
33 for the current set of qualifiers yields a unique H in the range [0 .. 20].
34 The hash can be modified when the keyword set changes as necessary. We also
35 store the length of each keyword and check it prior to the final strcmp().
37 TODO: just use gperf. */
40 isqualifier (const char *s
, size_t len
)
42 static const struct qual
47 {"static", 6}, {"", 0}, {"", 0}, {"", 0},
48 {"volatile", 8}, {"", 0}, {"", 0}, {"", 0}, {"", 0},
49 {"", 0}, {"auto", 4}, {"extern", 6}, {"", 0}, {"", 0},
50 {"", 0}, {"", 0}, {"const", 5}, {"register", 8},
51 {"", 0}, {"restrict", 8}, {"_Restrict", 9}
54 int h
= s
[len
- 1] + (int) len
- 105;
55 const struct qual
*qp
= &qhash
[h
];
57 return (h
>= 0 && (size_t) h
< sizeof (qhash
) / sizeof (qhash
[0])
58 && (size_t) len
== qp
->q_len
&&
59 strncmp (qp
->q_name
, s
, qp
->q_len
) == 0);
62 /* Attempt to convert the given C type name into the corresponding CTF type ID.
63 It is not possible to do complete and proper conversion of type names
64 without implementing a more full-fledged parser, which is necessary to
65 handle things like types that are function pointers to functions that
66 have arguments that are function pointers, and fun stuff like that.
67 Instead, this function implements a very simple conversion algorithm that
68 finds the things that we actually care about: structs, unions, enums,
69 integers, floats, typedefs, and pointers to any of these named types. */
72 ctf_lookup_by_name (ctf_file_t
*fp
, const char *name
)
74 static const char delimiters
[] = " \t\n\r\v\f*";
76 const ctf_lookup_t
*lp
;
77 const char *p
, *q
, *end
;
79 ctf_id_t ntype
, ptype
;
82 return (ctf_set_errno (fp
, EINVAL
));
84 for (p
= name
, end
= name
+ strlen (name
); *p
!= '\0'; p
= q
)
87 p
++; /* Skip leading whitespace. */
92 if ((q
= strpbrk (p
+ 1, delimiters
)) == NULL
)
93 q
= end
; /* Compare until end. */
97 /* Find a pointer to type by looking in fp->ctf_ptrtab.
98 If we can't find a pointer to the given type, see if
99 we can compute a pointer to the type resulting from
100 resolving the type down to its base type and use
101 that instead. This helps with cases where the CTF
102 data includes "struct foo *" but not "foo_t *" and
103 the user tries to access "foo_t *" in the debugger.
105 TODO need to handle parent containers too. */
107 ntype
= fp
->ctf_ptrtab
[LCTF_TYPE_TO_INDEX (fp
, type
)];
110 ntype
= ctf_type_resolve_unsliced (fp
, type
);
113 fp
->ctf_ptrtab
[LCTF_TYPE_TO_INDEX (fp
, ntype
)]) == 0)
115 (void) ctf_set_errno (fp
, ECTF_NOTYPE
);
120 type
= LCTF_INDEX_TO_TYPE (fp
, ntype
, (fp
->ctf_flags
& LCTF_CHILD
));
126 if (isqualifier (p
, (size_t) (q
- p
)))
127 continue; /* Skip qualifier keyword. */
129 for (lp
= fp
->ctf_lookups
; lp
->ctl_prefix
!= NULL
; lp
++)
131 /* TODO: This is not MT-safe. */
132 if ((lp
->ctl_prefix
[0] == '\0' ||
133 strncmp (p
, lp
->ctl_prefix
, (size_t) (q
- p
)) == 0) &&
134 (size_t) (q
- p
) >= lp
->ctl_len
)
136 for (p
+= lp
->ctl_len
; isspace (*p
); p
++)
137 continue; /* Skip prefix and next whitespace. */
139 if ((q
= strchr (p
, '*')) == NULL
)
140 q
= end
; /* Compare until end. */
142 while (isspace (q
[-1]))
143 q
--; /* Exclude trailing whitespace. */
145 /* Expand and/or allocate storage for a slice of the name, then
148 if (fp
->ctf_tmp_typeslicelen
>= (size_t) (q
- p
) + 1)
150 memcpy (fp
->ctf_tmp_typeslice
, p
, (size_t) (q
- p
));
151 fp
->ctf_tmp_typeslice
[(size_t) (q
- p
)] = '\0';
155 free (fp
->ctf_tmp_typeslice
);
156 fp
->ctf_tmp_typeslice
= xstrndup (p
, (size_t) (q
- p
));
157 if (fp
->ctf_tmp_typeslice
== NULL
)
159 (void) ctf_set_errno (fp
, ENOMEM
);
164 if ((type
= ctf_hash_lookup_type (lp
->ctl_hash
, fp
,
165 fp
->ctf_tmp_typeslice
)) == 0)
167 (void) ctf_set_errno (fp
, ECTF_NOTYPE
);
175 if (lp
->ctl_prefix
== NULL
)
177 (void) ctf_set_errno (fp
, ECTF_NOTYPE
);
182 if (*p
!= '\0' || type
== 0)
183 return (ctf_set_errno (fp
, ECTF_SYNTAX
));
188 if (fp
->ctf_parent
!= NULL
189 && (ptype
= ctf_lookup_by_name (fp
->ctf_parent
, name
)) != CTF_ERR
)
195 typedef struct ctf_lookup_var_key
198 const char *clvk_name
;
199 } ctf_lookup_var_key_t
;
201 /* A bsearch function for variable names. */
204 ctf_lookup_var (const void *key_
, const void *memb_
)
206 const ctf_lookup_var_key_t
*key
= key_
;
207 const ctf_varent_t
*memb
= memb_
;
209 return (strcmp (key
->clvk_name
, ctf_strptr (key
->clvk_fp
, memb
->ctv_name
)));
212 /* Given a variable name, return the type of the variable with that name. */
215 ctf_lookup_variable (ctf_file_t
*fp
, const char *name
)
218 ctf_lookup_var_key_t key
= { fp
, name
};
220 /* This array is sorted, so we can bsearch for it. */
222 ent
= bsearch (&key
, fp
->ctf_vars
, fp
->ctf_nvars
, sizeof (ctf_varent_t
),
227 if (fp
->ctf_parent
!= NULL
)
228 return ctf_lookup_variable (fp
->ctf_parent
, name
);
230 return (ctf_set_errno (fp
, ECTF_NOTYPEDAT
));
233 return ent
->ctv_type
;
236 /* Given a symbol table index, return the name of that symbol from the secondary
237 string table, or the null string (never NULL). */
239 ctf_lookup_symbol_name (ctf_file_t
*fp
, unsigned long symidx
)
241 const ctf_sect_t
*sp
= &fp
->ctf_symtab
;
244 if (sp
->cts_data
== NULL
)
246 ctf_set_errno (fp
, ECTF_NOSYMTAB
);
250 if (symidx
>= fp
->ctf_nsyms
)
252 ctf_set_errno (fp
, EINVAL
);
256 if (sp
->cts_entsize
== sizeof (Elf32_Sym
))
258 const Elf32_Sym
*symp
= (Elf32_Sym
*) sp
->cts_data
+ symidx
;
259 gsp
= ctf_sym_to_elf64 (symp
, &sym
);
262 gsp
= (Elf64_Sym
*) sp
->cts_data
+ symidx
;
264 if (gsp
->st_name
< fp
->ctf_str
[CTF_STRTAB_1
].cts_len
)
265 return (const char *) fp
->ctf_str
[CTF_STRTAB_1
].cts_strs
+ gsp
->st_name
;
270 /* Given a symbol table index, return the type of the data object described
271 by the corresponding entry in the symbol table. */
274 ctf_lookup_by_symbol (ctf_file_t
*fp
, unsigned long symidx
)
276 const ctf_sect_t
*sp
= &fp
->ctf_symtab
;
279 if (sp
->cts_data
== NULL
)
280 return (ctf_set_errno (fp
, ECTF_NOSYMTAB
));
282 if (symidx
>= fp
->ctf_nsyms
)
283 return (ctf_set_errno (fp
, EINVAL
));
285 if (sp
->cts_entsize
== sizeof (Elf32_Sym
))
287 const Elf32_Sym
*symp
= (Elf32_Sym
*) sp
->cts_data
+ symidx
;
288 if (ELF32_ST_TYPE (symp
->st_info
) != STT_OBJECT
)
289 return (ctf_set_errno (fp
, ECTF_NOTDATA
));
293 const Elf64_Sym
*symp
= (Elf64_Sym
*) sp
->cts_data
+ symidx
;
294 if (ELF64_ST_TYPE (symp
->st_info
) != STT_OBJECT
)
295 return (ctf_set_errno (fp
, ECTF_NOTDATA
));
298 if (fp
->ctf_sxlate
[symidx
] == -1u)
299 return (ctf_set_errno (fp
, ECTF_NOTYPEDAT
));
301 type
= *(uint32_t *) ((uintptr_t) fp
->ctf_buf
+ fp
->ctf_sxlate
[symidx
]);
303 return (ctf_set_errno (fp
, ECTF_NOTYPEDAT
));
308 /* Return the pointer to the internal CTF type data corresponding to the
309 given type ID. If the ID is invalid, the function returns NULL.
310 This function is not exported outside of the library. */
313 ctf_lookup_by_id (ctf_file_t
**fpp
, ctf_id_t type
)
315 ctf_file_t
*fp
= *fpp
; /* Caller passes in starting CTF container. */
318 if ((fp
->ctf_flags
& LCTF_CHILD
) && LCTF_TYPE_ISPARENT (fp
, type
)
319 && (fp
= fp
->ctf_parent
) == NULL
)
321 (void) ctf_set_errno (*fpp
, ECTF_NOPARENT
);
325 idx
= LCTF_TYPE_TO_INDEX (fp
, type
);
326 if (idx
> 0 && (unsigned long) idx
<= fp
->ctf_typemax
)
328 *fpp
= fp
; /* Function returns ending CTF container. */
329 return (LCTF_INDEX_TO_TYPEPTR (fp
, idx
));
332 /* If this container is writable, check for a dynamic type. */
334 if (fp
->ctf_flags
& LCTF_RDWR
)
338 if ((dtd
= ctf_dynamic_type (fp
, type
)) != NULL
)
341 return &dtd
->dtd_data
;
344 (void) ctf_set_errno (*fpp
, ECTF_BADID
);
348 /* Given a symbol table index, return the info for the function described
349 by the corresponding entry in the symbol table. */
352 ctf_func_info (ctf_file_t
*fp
, unsigned long symidx
, ctf_funcinfo_t
*fip
)
354 const ctf_sect_t
*sp
= &fp
->ctf_symtab
;
356 uint32_t info
, kind
, n
;
358 if (sp
->cts_data
== NULL
)
359 return (ctf_set_errno (fp
, ECTF_NOSYMTAB
));
361 if (symidx
>= fp
->ctf_nsyms
)
362 return (ctf_set_errno (fp
, EINVAL
));
364 if (sp
->cts_entsize
== sizeof (Elf32_Sym
))
366 const Elf32_Sym
*symp
= (Elf32_Sym
*) sp
->cts_data
+ symidx
;
367 if (ELF32_ST_TYPE (symp
->st_info
) != STT_FUNC
)
368 return (ctf_set_errno (fp
, ECTF_NOTFUNC
));
372 const Elf64_Sym
*symp
= (Elf64_Sym
*) sp
->cts_data
+ symidx
;
373 if (ELF64_ST_TYPE (symp
->st_info
) != STT_FUNC
)
374 return (ctf_set_errno (fp
, ECTF_NOTFUNC
));
377 if (fp
->ctf_sxlate
[symidx
] == -1u)
378 return (ctf_set_errno (fp
, ECTF_NOFUNCDAT
));
380 dp
= (uint32_t *) ((uintptr_t) fp
->ctf_buf
+ fp
->ctf_sxlate
[symidx
]);
383 kind
= LCTF_INFO_KIND (fp
, info
);
384 n
= LCTF_INFO_VLEN (fp
, info
);
386 if (kind
== CTF_K_UNKNOWN
&& n
== 0)
387 return (ctf_set_errno (fp
, ECTF_NOFUNCDAT
));
389 if (kind
!= CTF_K_FUNCTION
)
390 return (ctf_set_errno (fp
, ECTF_CORRUPT
));
392 fip
->ctc_return
= *dp
++;
396 if (n
!= 0 && dp
[n
- 1] == 0)
398 fip
->ctc_flags
|= CTF_FUNC_VARARG
;
405 /* Given a symbol table index, return the arguments for the function described
406 by the corresponding entry in the symbol table. */
409 ctf_func_args (ctf_file_t
* fp
, unsigned long symidx
, uint32_t argc
,
415 if (ctf_func_info (fp
, symidx
, &f
) < 0)
416 return -1; /* errno is set for us. */
418 /* The argument data is two uint32_t's past the translation table
419 offset: one for the function info, and one for the return type. */
421 dp
= (uint32_t *) ((uintptr_t) fp
->ctf_buf
+ fp
->ctf_sxlate
[symidx
]) + 2;
423 for (argc
= MIN (argc
, f
.ctc_argc
); argc
!= 0; argc
--)
This page took 0.054718 seconds and 4 git commands to generate.