From: Nick Alcock Date: Tue, 4 Feb 2025 12:57:17 +0000 (+0000) Subject: libctf: de-macroize LCTF_TYPE_TO_INDEX / LCTF_INDEX_TO_TYPE X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dc93d01ff286fd5711be25a534d30aacc6164c81;p=thirdparty%2Fbinutils-gdb.git libctf: de-macroize LCTF_TYPE_TO_INDEX / LCTF_INDEX_TO_TYPE Making these functions is unnecessary right now, but will become much clearer shortly. While we're at it, we can drop the third child argument to LCTF_INDEX_TO_TYPE: it's only used for nontrivial purposes that aren't literally the same as getting the result from the fp in one place, in ctf_lookup_by_name_internal, and that place is easily fixed by just looking in the right dictionary in the first place. --- diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c index 5a131f2c205..4b96efc2efb 100644 --- a/libctf/ctf-create.c +++ b/libctf/ctf-create.c @@ -285,7 +285,7 @@ ctf_dynamic_type (const ctf_dict_t *fp, ctf_id_t type) fp = ctf_get_dict (fp, type); - idx = LCTF_TYPE_TO_INDEX(fp, type); + idx = ctf_type_to_index (fp, type); if ((unsigned long) idx > fp->ctf_stypes) return ctf_dtd_lookup (fp, type); @@ -299,7 +299,7 @@ ctf_static_type (const ctf_dict_t *fp, ctf_id_t type) fp = ctf_get_dict (fp, type); - idx = LCTF_TYPE_TO_INDEX(fp, type); + idx = ctf_type_to_index (fp, type); return ((unsigned long) idx <= fp->ctf_stypes); } @@ -378,7 +378,7 @@ ctf_rollback (ctf_dict_t *fp, ctf_snapshot_id_t id) ntd = ctf_list_next (dtd); - if (LCTF_TYPE_TO_INDEX (fp, dtd->dtd_type) <= id.dtd_id) + if (ctf_type_to_index (fp, dtd->dtd_type) <= id.dtd_id) continue; kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); @@ -426,10 +426,10 @@ ctf_add_generic (ctf_dict_t *fp, uint32_t flag, const char *name, int kind, if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT) return (ctf_set_typed_errno (fp, EINVAL)); - if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) >= CTF_MAX_TYPE) + if (ctf_index_to_type (fp, fp->ctf_typemax) >= CTF_MAX_TYPE) return (ctf_set_typed_errno (fp, ECTF_FULL)); - if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) == (CTF_MAX_PTYPE - 1)) + if (ctf_index_to_type (fp, fp->ctf_typemax) == (CTF_MAX_PTYPE - 1)) return (ctf_set_typed_errno (fp, ECTF_FULL)); if (fp->ctf_flags & LCTF_NO_STR) @@ -465,7 +465,7 @@ ctf_add_generic (ctf_dict_t *fp, uint32_t flag, const char *name, int kind, dtd->dtd_vlen = NULL; type = ++fp->ctf_typemax; - type = LCTF_INDEX_TO_TYPE (fp, type, (fp->ctf_flags & LCTF_CHILD)); + type = ctf_index_to_type (fp, type); dtd->dtd_data.ctt_name = ctf_str_add_ref (fp, name, &dtd->dtd_data.ctt_name); dtd->dtd_type = type; @@ -575,8 +575,8 @@ ctf_add_reftype (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref, uint32_t kind) addition of this type. The pptrtab is lazily-updated as needed, so is not touched here. */ - uint32_t type_idx = LCTF_TYPE_TO_INDEX (fp, type); - uint32_t ref_idx = LCTF_TYPE_TO_INDEX (fp, ref); + uint32_t type_idx = ctf_type_to_index (fp, type); + uint32_t ref_idx = ctf_type_to_index (fp, ref); if (ctf_type_ischild (fp, ref) == child && ref_idx < fp->ctf_typemax) @@ -1589,9 +1589,9 @@ ctf_add_type_mapping (ctf_dict_t *src_fp, ctf_id_t src_type, if (src_fp == NULL || dst_fp == NULL) return; - src_type = LCTF_TYPE_TO_INDEX(src_fp, src_type); + src_type = ctf_type_to_index (src_fp, src_type); - dst_type = LCTF_TYPE_TO_INDEX(dst_fp, dst_type); + dst_type = ctf_type_to_index (dst_fp, dst_type); if (dst_fp->ctf_link_type_mapping == NULL) { @@ -1633,7 +1633,7 @@ ctf_type_mapping (ctf_dict_t *src_fp, ctf_id_t src_type, ctf_dict_t **dst_fp) if (src_fp == NULL) return 0; - src_type = LCTF_TYPE_TO_INDEX(src_fp, src_type); + src_type = ctf_type_to_index (src_fp, src_type); key.cltk_fp = src_fp; key.cltk_idx = src_type; @@ -1643,8 +1643,7 @@ ctf_type_mapping (ctf_dict_t *src_fp, ctf_id_t src_type, ctf_dict_t **dst_fp) if (dst_type != 0) { - dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type, - target_fp->ctf_parent != NULL); + dst_type = ctf_index_to_type (target_fp, dst_type); *dst_fp = target_fp; return dst_type; } @@ -1659,8 +1658,7 @@ ctf_type_mapping (ctf_dict_t *src_fp, ctf_id_t src_type, ctf_dict_t **dst_fp) &key); if (dst_type) - dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type, - target_fp->ctf_parent != NULL); + dst_type = ctf_index_to_type (target_fp, dst_type); *dst_fp = target_fp; return dst_type; diff --git a/libctf/ctf-impl.h b/libctf/ctf-impl.h index 918aa127aa5..be682d74e02 100644 --- a/libctf/ctf-impl.h +++ b/libctf/ctf-impl.h @@ -579,6 +579,9 @@ struct ctf_next } cu; }; +extern uint32_t ctf_type_to_index (const ctf_dict_t *, ctf_id_t); +extern ctf_id_t ctf_index_to_type (const ctf_dict_t *, uint32_t); + /* Return x rounded up to an alignment boundary. eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align) eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align) */ @@ -587,15 +590,10 @@ struct ctf_next /* * If an offs is not aligned already then round it up and align it. */ #define LCTF_ALIGN_OFFS(offs, align) ((offs + (align - 1)) & ~(align - 1)) -#define LCTF_TYPE_TO_INDEX(fp, id) ((id) & (fp->ctf_parmax)) -#define LCTF_INDEX_TO_TYPE(fp, id, child) (child ? ((id) | (fp->ctf_parmax+1)) : \ - (id)) - -#define LCTF_INDEX_TO_TYPEPTR(fp, i) \ - ((i > fp->ctf_stypes) ? \ - &(ctf_dtd_lookup (fp, LCTF_INDEX_TO_TYPE \ - (fp, i, fp->ctf_flags & LCTF_CHILD))->dtd_data) : \ - (ctf_type_t *)((uintptr_t)(fp)->ctf_buf + (fp)->ctf_txlate[(i)])) +#define LCTF_INDEX_TO_TYPEPTR(fp, i) \ + ((i > fp->ctf_stypes) ? \ + &(ctf_dtd_lookup (fp, ctf_index_to_type (fp, i))->dtd_data) : \ + (ctf_type_t *)((uintptr_t)(fp)->ctf_buf + (fp)->ctf_txlate[(i)])) #define LCTF_INFO_KIND(fp, info) ((fp)->ctf_dictops->ctfo_get_kind(info)) #define LCTF_INFO_ISROOT(fp, info) ((fp)->ctf_dictops->ctfo_get_root(info)) diff --git a/libctf/ctf-lookup.c b/libctf/ctf-lookup.c index 604a4fbf36b..6ee717a77d7 100644 --- a/libctf/ctf-lookup.c +++ b/libctf/ctf-lookup.c @@ -49,7 +49,7 @@ refresh_pptrtab (ctf_dict_t *fp, ctf_dict_t *pfp) uint32_t i; for (i = fp->ctf_pptrtab_typemax; i <= fp->ctf_typemax; i++) { - ctf_id_t type = LCTF_INDEX_TO_TYPE (fp, i, 1); + ctf_id_t type = ctf_index_to_type (fp, i); ctf_id_t reffed_type; if (ctf_type_kind (fp, type) != CTF_K_POINTER) @@ -59,7 +59,7 @@ refresh_pptrtab (ctf_dict_t *fp, ctf_dict_t *pfp) if (ctf_type_isparent (fp, reffed_type)) { - uint32_t idx = LCTF_TYPE_TO_INDEX (fp, reffed_type); + uint32_t idx = ctf_type_to_index (fp, reffed_type); /* Guard against references to invalid types. No need to consider the CTF dict corrupt in this case: this pointer just can't be a @@ -172,7 +172,7 @@ ctf_lookup_by_name_internal (ctf_dict_t *fp, ctf_dict_t *child, ctf_lookup_by_name. (Pointers to types are never of type 0, so this is unambiguous, just fiddly to deal with.) */ - uint32_t idx = LCTF_TYPE_TO_INDEX (fp, type); + uint32_t idx = ctf_type_to_index (fp, type); int in_child = 0; ntype = CTF_ERR; @@ -203,7 +203,7 @@ ctf_lookup_by_name_internal (ctf_dict_t *fp, ctf_dict_t *child, if (ntype == CTF_ERR) goto notype; - idx = LCTF_TYPE_TO_INDEX (fp, ntype); + idx = ctf_type_to_index (fp, ntype); ntype = CTF_ERR; if (child && idx < child->ctf_pptrtab_len) @@ -225,8 +225,10 @@ ctf_lookup_by_name_internal (ctf_dict_t *fp, ctf_dict_t *child, goto notype; } - type = LCTF_INDEX_TO_TYPE (fp, ntype, (fp->ctf_flags & LCTF_CHILD) - || in_child); + if (in_child) + type = ctf_index_to_type (child, ntype); + else + type = ctf_index_to_type (fp, ntype); /* We are looking up a type in the parent, but the pointed-to type is in the child. Switch to looking in the child: if we need to go @@ -341,7 +343,7 @@ ctf_lookup_by_id (ctf_dict_t **fpp, ctf_id_t type) return NULL; } - idx = LCTF_TYPE_TO_INDEX (fp, type); + idx = ctf_type_to_index (fp, type); if (idx > 0 && (unsigned long) idx <= fp->ctf_typemax) { *fpp = fp; /* Possibly the parent CTF dict. */ diff --git a/libctf/ctf-open.c b/libctf/ctf-open.c index 030f95fd6e1..ce7b575a550 100644 --- a/libctf/ctf-open.c +++ b/libctf/ctf-open.c @@ -966,13 +966,13 @@ init_static_types_names_internal (ctf_dict_t *fp, ctf_header_t *cth, if (((existing = ctf_dynhash_lookup_type (fp->ctf_names, name)) == 0) || ctf_type_encoding (fp, existing, &existing_en) != 0 - || (ctf_type_encoding (fp, LCTF_INDEX_TO_TYPE (fp, id, child), &this_en) == 0 + || (ctf_type_encoding (fp, ctf_index_to_type (fp, id), &this_en) == 0 && this_en.cte_offset == 0 && (existing_en.cte_offset != 0 || existing_en.cte_bits < this_en.cte_bits))) { err = ctf_dynhash_insert_type (fp, fp->ctf_names, - LCTF_INDEX_TO_TYPE (fp, id, child), + ctf_index_to_type (fp, id), tp->ctt_name); if (err != 0) return err * -1; @@ -991,7 +991,7 @@ init_static_types_names_internal (ctf_dict_t *fp, ctf_header_t *cth, break; err = ctf_dynhash_insert_type (fp, fp->ctf_names, - LCTF_INDEX_TO_TYPE (fp, id, child), + ctf_index_to_type (fp, id), tp->ctt_name); if (err != 0) return err * -1; @@ -1005,7 +1005,7 @@ init_static_types_names_internal (ctf_dict_t *fp, ctf_header_t *cth, break; err = ctf_dynhash_insert_type (fp, fp->ctf_structs, - LCTF_INDEX_TO_TYPE (fp, id, child), + ctf_index_to_type (fp, id), tp->ctt_name); if (err != 0) @@ -1021,7 +1021,7 @@ init_static_types_names_internal (ctf_dict_t *fp, ctf_header_t *cth, break; err = ctf_dynhash_insert_type (fp, fp->ctf_unions, - LCTF_INDEX_TO_TYPE (fp, id, child), + ctf_index_to_type (fp, id), tp->ctt_name); if (err != 0) @@ -1034,7 +1034,7 @@ init_static_types_names_internal (ctf_dict_t *fp, ctf_header_t *cth, break; err = ctf_dynhash_insert_type (fp, fp->ctf_enums, - LCTF_INDEX_TO_TYPE (fp, id, child), + ctf_index_to_type (fp, id), tp->ctt_name); if (err != 0) @@ -1043,7 +1043,7 @@ init_static_types_names_internal (ctf_dict_t *fp, ctf_header_t *cth, /* Remember all enums for later rescanning. */ err = ctf_dynset_insert (all_enums, (void *) (ptrdiff_t) - LCTF_INDEX_TO_TYPE (fp, id, child)); + ctf_index_to_type (fp, id)); if (err != 0) return err * -1; break; @@ -1054,7 +1054,7 @@ init_static_types_names_internal (ctf_dict_t *fp, ctf_header_t *cth, break; err = ctf_dynhash_insert_type (fp, fp->ctf_names, - LCTF_INDEX_TO_TYPE (fp, id, child), + ctf_index_to_type (fp, id), tp->ctt_name); if (err != 0) return err * -1; @@ -1071,7 +1071,7 @@ init_static_types_names_internal (ctf_dict_t *fp, ctf_header_t *cth, name is not already present. */ if (ctf_dynhash_lookup_type (h, name) == 0) { - err = ctf_dynhash_insert_type (fp, h, LCTF_INDEX_TO_TYPE (fp, id, child), + err = ctf_dynhash_insert_type (fp, h, ctf_index_to_type (fp, id), tp->ctt_name); if (err != 0) return err * -1; @@ -1085,8 +1085,8 @@ init_static_types_names_internal (ctf_dict_t *fp, ctf_header_t *cth, referenced type ]. */ if (ctf_type_ischild (fp, tp->ctt_type) == child - && LCTF_TYPE_TO_INDEX (fp, tp->ctt_type) <= fp->ctf_typemax) - fp->ctf_ptrtab[LCTF_TYPE_TO_INDEX (fp, tp->ctt_type)] = id; + && ctf_type_to_index (fp, tp->ctt_type) <= fp->ctf_typemax) + fp->ctf_ptrtab[ctf_type_to_index (fp, tp->ctt_type)] = id; /*FALLTHRU*/ case CTF_K_VOLATILE: @@ -1096,7 +1096,7 @@ init_static_types_names_internal (ctf_dict_t *fp, ctf_header_t *cth, break; err = ctf_dynhash_insert_type (fp, fp->ctf_names, - LCTF_INDEX_TO_TYPE (fp, id, child), + ctf_index_to_type (fp, id), tp->ctt_name); if (err != 0) return err * -1; diff --git a/libctf/ctf-types.c b/libctf/ctf-types.c index a234a81db63..25eda5495c0 100644 --- a/libctf/ctf-types.c +++ b/libctf/ctf-types.c @@ -35,6 +35,24 @@ ctf_type_ischild (const ctf_dict_t *fp, ctf_id_t id) return (!ctf_type_isparent (fp, id)); } +/* Get the index in the internal type array (or otherwise) for a given type ID. + Only ever called on the right dictionary for the type and can fail otherwise. + If called on an invalid type, may return an index that does not correspond to + any type (such as -1). */ + +uint32_t +ctf_type_to_index (const ctf_dict_t *fp, ctf_id_t type) +{ + return type & fp->ctf_parmax; +} + +/* The inverse of ctf_type_to_index. */ +ctf_id_t +ctf_index_to_type (const ctf_dict_t *fp, uint32_t idx) +{ + return (fp->ctf_flags & LCTF_CHILD) ? ((idx) | (fp->ctf_parmax+1)) : idx; +} + /* Expand a structure element into the passed-in ctf_lmember_t. */ static int @@ -461,7 +479,7 @@ ctf_type_next (ctf_dict_t *fp, ctf_next_t **it, int *flag, int want_hidden) if (flag) *flag = LCTF_INFO_ISROOT (fp, tp->ctt_info); - return LCTF_INDEX_TO_TYPE (fp, i->ctn_type++, fp->ctf_flags & LCTF_CHILD); + return ctf_index_to_type (fp, i->ctn_type++); } ctf_next_destroy (i); *it = NULL; @@ -1164,8 +1182,8 @@ ctf_type_pointer (ctf_dict_t *fp, ctf_id_t type) if (ctf_lookup_by_id (&fp, type) == NULL) return CTF_ERR; /* errno is set for us. */ - if ((ntype = fp->ctf_ptrtab[LCTF_TYPE_TO_INDEX (fp, type)]) != 0) - return (LCTF_INDEX_TO_TYPE (fp, ntype, (fp->ctf_flags & LCTF_CHILD))); + if ((ntype = fp->ctf_ptrtab[ctf_type_to_index (fp, type)]) != 0) + return (ctf_index_to_type (fp, ntype)); if ((type = ctf_type_resolve (fp, type)) == CTF_ERR) return (ctf_set_typed_errno (ofp, ECTF_NOTYPE)); @@ -1173,8 +1191,8 @@ ctf_type_pointer (ctf_dict_t *fp, ctf_id_t type) if (ctf_lookup_by_id (&fp, type) == NULL) return (ctf_set_typed_errno (ofp, ECTF_NOTYPE)); - if ((ntype = fp->ctf_ptrtab[LCTF_TYPE_TO_INDEX (fp, type)]) != 0) - return (LCTF_INDEX_TO_TYPE (fp, ntype, (fp->ctf_flags & LCTF_CHILD))); + if ((ntype = fp->ctf_ptrtab[ctf_type_to_index (fp, type)]) != 0) + return (ctf_index_to_type (fp, ntype)); return (ctf_set_typed_errno (ofp, ECTF_NOTYPE)); }