git: 1165fc9a5266 - main - ctfconvert: Give bitfield types names distinct from the base type

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Wed, 03 Aug 2022 00:39:12 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=1165fc9a526630487a1feb63daef65c5aee1a583

commit 1165fc9a526630487a1feb63daef65c5aee1a583
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-08-03 00:32:17 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-08-03 00:32:17 +0000

    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
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
---
 cddl/contrib/opensolaris/tools/ctf/common/memory.c | 14 ++++++++++++++
 cddl/contrib/opensolaris/tools/ctf/common/memory.h |  1 +
 cddl/contrib/opensolaris/tools/ctf/cvt/dwarf.c     |  7 ++++---
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/cddl/contrib/opensolaris/tools/ctf/common/memory.c b/cddl/contrib/opensolaris/tools/ctf/common/memory.c
index e16044a8b672..66296c5b114d 100644
--- a/cddl/contrib/opensolaris/tools/ctf/common/memory.c
+++ b/cddl/contrib/opensolaris/tools/ctf/common/memory.c
@@ -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)
 {
diff --git a/cddl/contrib/opensolaris/tools/ctf/common/memory.h b/cddl/contrib/opensolaris/tools/ctf/common/memory.h
index 88ca31bec65a..72706b5f7fdb 100644
--- a/cddl/contrib/opensolaris/tools/ctf/common/memory.h
+++ b/cddl/contrib/opensolaris/tools/ctf/common/memory.h
@@ -39,6 +39,7 @@
 extern "C" {
 #endif
 
+int xasprintf(char **, const char *, ...);
 void *xmalloc(size_t);
 void *xcalloc(size_t);
 char *xstrdup(const char *);
diff --git a/cddl/contrib/opensolaris/tools/ctf/cvt/dwarf.c b/cddl/contrib/opensolaris/tools/ctf/cvt/dwarf.c
index 2d686e53fed1..9c422fb58fa1 100644
--- a/cddl/contrib/opensolaris/tools/ctf/cvt/dwarf.c
+++ b/cddl/contrib/opensolaris/tools/ctf/cvt/dwarf.c
@@ -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");
 		}
 	}