From: Nick Alcock Date: Fri, 25 Apr 2025 11:56:58 +0000 (+0100) Subject: libctf: strings: no external strings in BTF X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5ec23dfb74b1962e4a306031776a07c92d18c18a;p=thirdparty%2Fbinutils-gdb.git libctf: strings: no external strings in BTF One of the things BTF doesn't have is the concept of external strings which can be shared with the ELF strtab. Therefore, even if the linker has reported strings which the dict is reusing, when we generate the strtab for a BTF dict we should emit those strings into it (and we should certainly not cause the presence of external strings to prevent BTF emission!) Note that since already-written strtab entries are never erased, writing a dict as BTF and then CTF will cause external strings to be emitted even for the CTF. This sort of repeated writing in different formats seems to be very rare: in any case, the problem can be avoided by simply doing the CTF writeout first (the following BTF writeout will spot the missing external- in-CTF strings and add them). We also throw away the internal-only function ctf_strraw_explicit(), which was used to add strings with a hardwired strtab: it was only ever used to write out the variable section, which is gone in v4. --- diff --git a/libctf/ctf-dedup.c b/libctf/ctf-dedup.c index 9404e196584..a35f026d37c 100644 --- a/libctf/ctf-dedup.c +++ b/libctf/ctf-dedup.c @@ -3156,8 +3156,9 @@ ctf_dedup_emit (ctf_dict_t *output, ctf_dict_t **inputs, uint32_t ninputs, return outputs; } -/* Deduplicate strings. This must be done after parent serialization. - The child dict ctf_parent_strlen is not updated yet. */ +/* Deduplicate strings. This must be done after parent serialization and child + preserialization. The child dict ctf_parent_strlen is not updated yet. + (ctf_arc_write_*() does the right thing.) */ int ctf_dedup_strings (ctf_dict_t *fp) @@ -3191,7 +3192,7 @@ ctf_dedup_strings (ctf_dict_t *fp) if (ctf_list_empty_p (&atom->csa_refs)) continue; - if (atom->csa_external_offset + if ((!fp->ctf_serialize.cs_is_btf && atom->csa_external_offset) || atom->csa_str[0] == '\0' || atom->csa_flags & CTF_STR_ATOM_NO_DEDUP) continue; @@ -3240,7 +3241,7 @@ ctf_dedup_strings (ctf_dict_t *fp) if (ctf_list_empty_p (&atom->csa_refs)) continue; - if (atom->csa_external_offset + if ((!fp->ctf_serialize.cs_is_btf && atom->csa_external_offset) || atom->csa_str[0] == '\0' || atom->csa_flags & CTF_STR_ATOM_NO_DEDUP) continue; diff --git a/libctf/ctf-impl.h b/libctf/ctf-impl.h index 67ddc1801b2..d029db52f7e 100644 --- a/libctf/ctf-impl.h +++ b/libctf/ctf-impl.h @@ -783,8 +783,6 @@ extern char *ctf_decl_buf (ctf_decl_t *cd); extern const char *ctf_strptr (ctf_dict_t *, uint32_t); extern const char *ctf_strraw (ctf_dict_t *, uint32_t); -extern const char *ctf_strraw_explicit (ctf_dict_t *, uint32_t, - ctf_strs_t *); extern const char *ctf_strptr_validate (ctf_dict_t *, uint32_t); extern int ctf_str_create_atoms (ctf_dict_t *); extern void ctf_str_free_atoms (ctf_dict_t *); diff --git a/libctf/ctf-link.c b/libctf/ctf-link.c index e0cbc3f3d68..b3092fce465 100644 --- a/libctf/ctf-link.c +++ b/libctf/ctf-link.c @@ -1447,6 +1447,11 @@ ctf_link_add_strtab (ctf_dict_t *fp, ctf_link_strtab_string_f *add_string, if (fp->ctf_stypes > 0) return ctf_set_errno (fp, ECTF_RDONLY); + /* If emitting BTF, there is no external string table. */ + + if (fp->ctf_serialize.cs_is_btf) + return 0; + while ((str = add_string (&offset, arg)) != NULL) { ctf_link_out_string_cb_arg_t iter_arg = { str, offset, 0 }; diff --git a/libctf/ctf-string.c b/libctf/ctf-string.c index d60918a73b1..56257d91f9a 100644 --- a/libctf/ctf-string.c +++ b/libctf/ctf-string.c @@ -46,11 +46,10 @@ set_prov_offset (ctf_dict_t *fp, uint32_t prov_offset) fp->ctf_str_prov_offset = prov_offset; } -/* Convert an encoded CTF string name into a pointer to a C string, possibly - using an explicit internal provisional strtab rather than the fp-based - one. */ +/* Convert an encoded CTF string name into a pointer to a C string by looking + up the appropriate string table buffer and then adding the offset. */ const char * -ctf_strraw_explicit (ctf_dict_t *fp, uint32_t name, ctf_strs_t *strtab) +ctf_strraw (ctf_dict_t *fp, uint32_t name) { int stid_tab = CTF_NAME_STID (name); ctf_strs_t *ctsp = &fp->ctf_str[stid_tab]; @@ -129,20 +128,20 @@ ctf_strraw_explicit (ctf_dict_t *fp, uint32_t name, ctf_strs_t *strtab) else { name -= fp->ctf_header->cth_parent_strlen; - - if (strtab != NULL) - ctsp = strtab; - else - ctsp = &fp->ctf_str[CTF_STRTAB_0]; + ctsp = &fp->ctf_str[CTF_STRTAB_0]; } } /* If this name is in the external strtab, and there is a synthetic strtab, - use it in preference. (This is used to add the set of strings -- symbol - names, etc -- the linker knows about before the strtab is written out. - The set is added to every dict, so we don't need to scan the parent.) */ - - if (stid_tab == CTF_STRTAB_1 && fp->ctf_syn_ext_strtab != NULL) + and we are not serializing BTF right now, use it in preference. (This is + used to add the set of strings -- symbol names, etc -- the linker knows + about before the strtab is written out. The set is added to every dict, so + we don't need to scan the parent. Preventing this from operating during + BTF serialization is sufficient to prevent external refs from appearing in + BTF, because every string gets rescanned at that stage.) */ + + if (stid_tab == CTF_STRTAB_1 && fp->ctf_syn_ext_strtab != NULL + && !fp->ctf_serialize.cs_is_btf) return ctf_dynhash_lookup (fp->ctf_syn_ext_strtab, (void *) (uintptr_t) name); @@ -156,14 +155,6 @@ ctf_strraw_explicit (ctf_dict_t *fp, uint32_t name, ctf_strs_t *strtab) return NULL; } -/* Convert an encoded CTF string name into a pointer to a C string by looking - up the appropriate string table buffer and then adding the offset. */ -const char * -ctf_strraw (ctf_dict_t *fp, uint32_t name) -{ - return ctf_strraw_explicit (fp, name, NULL); -} - /* Return a guaranteed-non-NULL pointer to the string with the given CTF name. */ const char * @@ -342,11 +333,11 @@ ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str, atom->csa_flags |= CTF_STR_ATOM_NO_DEDUP; if (atom->csa_offset < get_prov_offset (fp) - || atom->csa_external_offset != 0) + || (!fp->ctf_serialize.cs_is_btf && atom->csa_external_offset)) { if (flags & CTF_STR_ADD_REF) { - if (atom->csa_external_offset) + if (!fp->ctf_serialize.cs_is_btf && atom->csa_external_offset) *ref = atom->csa_external_offset; else *ref = atom->csa_offset + lookup_fp->ctf_header->cth_parent_strlen; @@ -428,8 +419,7 @@ ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str, Special-case "" again: it gets a real offset of zero, not a high provisional one. This atom's offset is never returned (see the special - case in ctf_strraw_explicit) and mostly exists for the sake of the - deduplicator. */ + case in ctf_strraw) and mostly exists for the sake of the deduplicator. */ if (flags & CTF_STR_PROVISIONAL) { @@ -479,7 +469,7 @@ ctf_str_add_flagged (ctf_dict_t *fp, const char *str, uint32_t *ref, if (!atom) return 0; - if (atom->csa_external_offset) + if (!fp->ctf_serialize.cs_is_btf && atom->csa_external_offset) offset = atom->csa_external_offset; else offset = atom->csa_offset; @@ -702,10 +692,11 @@ ctf_str_write_strtab (ctf_dict_t *fp) /* The strtab contains the existing string table at its start: figure out how many new strings we need to add. We only need to add new strings that have - no external offset, that have refs, and that are found in the provisional - strtab. If the existing strtab is empty and has no parent strings, we also - need to add the null string at its start. (Dicts promoted from CTFv3 and - below always have no parent strings in this sense.) */ + no external offset (or are BTF, for which external offsets are ignored), + that have refs, and that are found in the provisional strtab. If the + existing strtab is empty and has no parent strings, we also need to add the + null string at its start. (Dicts promoted from CTFv3 and below always have + no parent strings in this sense.) */ strtab->cts_len = fp->ctf_str[CTF_STRTAB_0].cts_len; @@ -730,7 +721,8 @@ ctf_str_write_strtab (ctf_dict_t *fp) if (!ctf_assert (fp, atom)) goto err_strtab; - if (atom->csa_str[0] == 0 || atom->csa_external_offset + if (atom->csa_str[0] == 0 + || (!fp->ctf_serialize.cs_is_btf && atom->csa_external_offset) || atom->csa_offset < prov_offset || ctf_list_empty_p (&atom->csa_refs)) continue; @@ -767,7 +759,8 @@ ctf_str_write_strtab (ctf_dict_t *fp) if (!ctf_assert (fp, atom)) goto err_sorttab; - if (atom->csa_str[0] == 0 || atom->csa_external_offset + if (atom->csa_str[0] == 0 + || (!fp->ctf_serialize.cs_is_btf && atom->csa_external_offset) || atom->csa_offset < prov_offset || ctf_list_empty_p (&atom->csa_refs)) continue; @@ -821,7 +814,7 @@ ctf_str_write_strtab (ctf_dict_t *fp) if (ctf_list_empty_p (&atom->csa_refs)) continue; - if (atom->csa_external_offset) + if (!fp->ctf_serialize.cs_is_btf && atom->csa_external_offset) offset = atom->csa_external_offset; else {