ctfconvert: Give bitfield types names distinct from the base type

CTF integers have an explicit width and so can be used to represent
bitfields.  Bitfield types emitted by ctfconvert(1) share the name of
the base integer type, so a struct field with type "unsigned int : 15"
will have a type named "unsigned int".

To avoid ambiguity when looking up types by name, add a suffix to names
of bitfield types to distinguish them from the base type.  Then, if
ctfmerge happens to order bitfield types before the corresponding base
type in a CTF file, a name lookup will return the base type, which is
always going to be the desired behaviour.

PR:		265403
Reported by:	cy
Sponsored by:	The FreeBSD Foundation

(cherry picked from commit 1165fc9a52)
This commit is contained in:
Mark Johnston 2022-08-02 20:32:17 -04:00
parent cf63676e08
commit a853b4c425
3 changed files with 19 additions and 3 deletions

View file

@ -44,6 +44,20 @@ memory_bailout(void)
exit(1);
}
int
xasprintf(char **s, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vasprintf(s, fmt, ap);
va_end(ap);
if (ret == -1)
memory_bailout();
return (ret);
}
void *
xmalloc(size_t size)
{

View file

@ -39,6 +39,7 @@
extern "C" {
#endif
int xasprintf(char **, const char *, ...);
void *xmalloc(size_t);
void *xcalloc(size_t);
char *xstrdup(const char *);

View file

@ -618,7 +618,7 @@ tdesc_intr_long(dwarf_t *dw)
* caller can then use the copy as the type for a bitfield structure member.
*/
static tdesc_t *
tdesc_intr_clone(dwarf_t *dw, tdesc_t *old, size_t bitsz)
tdesc_intr_clone(dwarf_t *dw, tdesc_t *old, size_t bitsz, const char *suffix)
{
tdesc_t *new = xcalloc(sizeof (tdesc_t));
@ -627,7 +627,7 @@ tdesc_intr_clone(dwarf_t *dw, tdesc_t *old, size_t bitsz)
"unresolved type\n", old->t_id);
}
new->t_name = xstrdup(old->t_name);
asprintf(&new->t_name, "%s %s", old->t_name, suffix);
new->t_size = old->t_size;
new->t_id = mfgtid_next(dw);
new->t_type = INTRINSIC;
@ -1158,7 +1158,8 @@ die_sou_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
debug(3, "tdp %u: creating bitfield for %d bits\n",
tdp->t_id, ml->ml_size);
ml->ml_type = tdesc_intr_clone(dw, mt, ml->ml_size);
ml->ml_type = tdesc_intr_clone(dw, mt, ml->ml_size,
"bitfield");
}
}