summaryrefslogtreecommitdiff
path: root/libctf/ctf-types.c
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2021-01-05 13:25:56 +0000
committerNick Alcock <nick.alcock@oracle.com>2021-01-05 14:53:39 +0000
commitffeece6ac2d4e4d2bf28c910b51c74ffc881c963 (patch)
treedbb61a523d3289f68a024ed1c84a4290d75b364b /libctf/ctf-types.c
parent91e7ce2fd7b82f3982b453d1c6f4576621bc1c2d (diff)
downloadbinutils-gdb-ffeece6ac2d4e4d2bf28c910b51c74ffc881c963.tar.gz
libctf, ld: prohibit getting the size or alignment of forwards
C allows you to do only a very few things with entities of incomplete type (as opposed to pointers to them): make pointers to them and give them cv-quals, roughly. In particular you can't sizeof them and you can't get their alignment. We cannot impose all the requirements the standard imposes on CTF users, because the deduplicator can transform any structure type into a forward for the purposes of breaking cycles: so CTF type graphs can easily contain things like arrays of forward type (if you want to figure out their size or alignment, you need to chase down the types this forward might be a forward to in child TU dicts: we will soon add API functions to make doing this much easier). Nonetheless, it is still meaningless to ask for the size or alignment of forwards: but libctf didn't prohibit this and returned nonsense from internal implementation details when you asked (it returned the kind of the pointed-to type as both the size and alignment, because forwards reuse ctt_type as a type kind, and ctt_type and ctt_size overlap). So introduce a new error, ECTF_INCOMPLETE, which is returned when you try to get the size or alignment of forwards: we also return it when you try to do things that require libctf itself to get the size or alignment of a forward, notably using a forward as an array index type (which C should never do in any case) or adding forwards to structures without specifying their offset explicitly. The dumper will not emit size or alignment info for forwards any more. (This should not be an API break since ctf_type_size and ctf_type_align could both return errors before now: any code that isn't expecting error returns is already potentially broken.) include/ChangeLog 2021-01-05 Nick Alcock <nick.alcock@oracle.com> * ctf-api.h (ECTF_INCOMPLETE): New. (ECTF_NERR): Adjust. ld/ChangeLog 2021-01-05 Nick Alcock <nick.alcock@oracle.com> * testsuite/ld-ctf/conflicting-cycle-1.parent.d: Adjust for dumper changes. * testsuite/ld-ctf/cross-tu-cyclic-conflicting.d: Likewise. * testsuite/ld-ctf/forward.c: New test... * testsuite/ld-ctf/forward.d: ... and results. libctf/ChangeLog 2021-01-05 Nick Alcock <nick.alcock@oracle.com> * ctf-types.c (ctf_type_resolve): Improve comment. (ctf_type_size): Yield ECTF_INCOMPLETE when applied to forwards. Emit errors into the right dict. (ctf_type_align): Likewise. * ctf-create.c (ctf_add_member_offset): Yield ECTF_INCOMPLETE when adding a member without explicit offset when this member, or the previous member, is incomplete. * ctf-dump.c (ctf_dump_format_type): Do not try to print the size of forwards. (ctf_dump_member): Do not try to print their alignment.
Diffstat (limited to 'libctf/ctf-types.c')
-rw-r--r--libctf/ctf-types.c28
1 files changed, 20 insertions, 8 deletions
diff --git a/libctf/ctf-types.c b/libctf/ctf-types.c
index 61891d67ba2..3ba635f48bb 100644
--- a/libctf/ctf-types.c
+++ b/libctf/ctf-types.c
@@ -583,7 +583,10 @@ ctf_variable_next (ctf_dict_t *fp, ctf_next_t **it, const char **name)
against infinite loops, we implement simplified cycle detection and check
each link against itself, the previous node, and the topmost node.
- Does not drill down through slices to their contained type. */
+ Does not drill down through slices to their contained type.
+
+ Callers of this function must not presume that a type it returns must have a
+ valid ctt_size: forwards do not, and must be separately handled. */
ctf_id_t
ctf_type_resolve (ctf_dict_t *fp, ctf_id_t type)
@@ -911,6 +914,7 @@ ctf_type_aname_raw (ctf_dict_t *fp, ctf_id_t type)
ssize_t
ctf_type_size (ctf_dict_t *fp, ctf_id_t type)
{
+ ctf_dict_t *ofp = fp;
const ctf_type_t *tp;
ssize_t size;
ctf_arinfo_t ar;
@@ -942,12 +946,16 @@ ctf_type_size (ctf_dict_t *fp, ctf_id_t type)
if ((size = ctf_get_ctt_size (fp, tp, NULL, NULL)) > 0)
return size;
- if (ctf_array_info (fp, type, &ar) < 0
- || (size = ctf_type_size (fp, ar.ctr_contents)) < 0)
+ if (ctf_array_info (ofp, type, &ar) < 0
+ || (size = ctf_type_size (ofp, ar.ctr_contents)) < 0)
return -1; /* errno is set for us. */
return size * ar.ctr_nelems;
+ case CTF_K_FORWARD:
+ /* Forwards do not have a meaningful size. */
+ return (ctf_set_errno (ofp, ECTF_INCOMPLETE));
+
default: /* including slices of enums, etc */
return (ctf_get_ctt_size (fp, tp, NULL, NULL));
}
@@ -981,9 +989,9 @@ ctf_type_align (ctf_dict_t *fp, ctf_id_t type)
case CTF_K_ARRAY:
{
ctf_arinfo_t r;
- if (ctf_array_info (fp, type, &r) < 0)
+ if (ctf_array_info (ofp, type, &r) < 0)
return -1; /* errno is set for us. */
- return (ctf_type_align (fp, r.ctr_contents));
+ return (ctf_type_align (ofp, r.ctr_contents));
}
case CTF_K_STRUCT:
@@ -1009,7 +1017,7 @@ ctf_type_align (ctf_dict_t *fp, ctf_id_t type)
const ctf_member_t *mp = vmp;
for (; n != 0; n--, mp++)
{
- ssize_t am = ctf_type_align (fp, mp->ctm_type);
+ ssize_t am = ctf_type_align (ofp, mp->ctm_type);
align = MAX (align, (size_t) am);
}
}
@@ -1018,7 +1026,7 @@ ctf_type_align (ctf_dict_t *fp, ctf_id_t type)
const ctf_lmember_t *lmp = vmp;
for (; n != 0; n--, lmp++)
{
- ssize_t am = ctf_type_align (fp, lmp->ctlm_type);
+ ssize_t am = ctf_type_align (ofp, lmp->ctlm_type);
align = MAX (align, (size_t) am);
}
}
@@ -1030,7 +1038,7 @@ ctf_type_align (ctf_dict_t *fp, ctf_id_t type)
for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
dmd != NULL; dmd = ctf_list_next (dmd))
{
- ssize_t am = ctf_type_align (fp, dmd->dmd_type);
+ ssize_t am = ctf_type_align (ofp, dmd->dmd_type);
align = MAX (align, (size_t) am);
if (kind == CTF_K_STRUCT)
break;
@@ -1043,6 +1051,10 @@ ctf_type_align (ctf_dict_t *fp, ctf_id_t type)
case CTF_K_ENUM:
return fp->ctf_dmodel->ctd_int;
+ case CTF_K_FORWARD:
+ /* Forwards do not have a meaningful alignment. */
+ return (ctf_set_errno (ofp, ECTF_INCOMPLETE));
+
default: /* including slices of enums, etc */
return (ctf_get_ctt_size (fp, tp, NULL, NULL));
}