]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
libctf: strings: no external strings in BTF
authorNick Alcock <nick.alcock@oracle.com>
Fri, 25 Apr 2025 11:56:58 +0000 (12:56 +0100)
committerNick Alcock <nick.alcock@oracle.com>
Tue, 9 Dec 2025 13:02:26 +0000 (13:02 +0000)
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.

libctf/ctf-dedup.c
libctf/ctf-impl.h
libctf/ctf-link.c
libctf/ctf-string.c

index b23c3df94e12ef43dd3a077a92dd098ffd02687a..4388633d452a15db790a28ed8e71f3b26341c5c0 100644 (file)
@@ -3311,8 +3311,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)
@@ -3346,7 +3347,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;
@@ -3395,7 +3396,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;
index 2e2c558a5be289583ca825a7736be166215df4fe..f84f5676179dff56c5175c15d66c9653906c9fdf 100644 (file)
@@ -793,8 +793,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 *);
index e1c864782af9a26a8bd8a987f7b6e773b050b166..49d9019cdb40a489a37a6b95d47aa08194e63496 100644 (file)
@@ -1425,6 +1425,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 };
index d60918a73b1720c4bb0ec29fea056e3677f5ae53..56257d91f9a1dbcea438057d96563766be003c52 100644 (file)
@@ -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
        {