]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
libctf, open: new API for getting the size of CTF/BTF file sections
authorNick Alcock <nick.alcock@oracle.com>
Fri, 25 Apr 2025 10:51:04 +0000 (11:51 +0100)
committerNick Alcock <nick.alcock@oracle.com>
Fri, 25 Apr 2025 17:07:43 +0000 (18:07 +0100)
I wrote this for BTF type size querying programs, but it might be
of more general use and it's impossible to get this info in any
other way, so we might want to keep it.

New API:
+size_t ctf_sect_size (ctf_dict_t *, ctf_sect_names_t sect);

include/ctf-api.h
libctf/ctf-open.c
libctf/libctf.ver

index a23e1da0acef1e66742c505e481876344ec42e8e..27bd4731524d93b78913c8ef286d46db836d8e1d 100644 (file)
@@ -894,6 +894,10 @@ extern char *ctf_dump (ctf_dict_t *, ctf_dump_state_t **state,
                       ctf_sect_names_t sect, ctf_dump_decorate_f *,
                       void *arg);
 
+/* Return the size in bytes of a given CTF section, or 0 if none.  */
+
+extern size_t ctf_sect_size (ctf_dict_t *, ctf_sect_names_t sect);
+
 /* Error-warning reporting: an 'iterator' that returns errors and warnings from
    the error/warning list, in order of emission.  Errors and warnings are popped
    after return: the caller must free the returned error-text pointer.  */
index 994372012b3c76559438e82740208f795d2b880e..32f1414d91c6a279f86d64f8905bec88a4061aa2 100644 (file)
@@ -2505,6 +2505,50 @@ ctf_symsect_endianness (ctf_dict_t *fp, int little_endian)
     assert (init_symtab (fp, fp->ctf_header, &fp->ctf_ext_symtab) == 0);
 }
 
+/* Return the size in bytes of a given CTF section, or 0 if none.  */
+
+size_t
+ctf_sect_size (ctf_dict_t *fp, ctf_sect_names_t sect)
+{
+  switch (sect)
+    {
+    case CTF_SECT_HEADER:
+      switch (CTH_VERSION (fp->ctf_header))
+       {
+       case CTF_VERSION_1:
+       case CTF_VERSION_1_UPGRADED_3:
+       case CTF_VERSION_2:
+         return sizeof (ctf_header_v2_t);
+
+       case CTF_VERSION_3:
+         return sizeof (ctf_header_v3_t);
+
+       case CTF_VERSION_4:
+       default:
+         if (fp->ctf_opened_btf)
+           return sizeof (ctf_btf_header_t);
+         else
+           return sizeof (ctf_header_t);
+       }
+    case CTF_SECT_OBJT:
+      return fp->ctf_header->cth_objtidx_len + fp->ctf_header->cth_objt_len;
+    case CTF_SECT_FUNC:
+      return fp->ctf_header->cth_funcidx_len + fp->ctf_header->cth_func_len;
+    case CTF_SECT_VAR:
+      if (fp->ctf_v3_header)
+       return fp->ctf_v3_header->cth_typeoff - fp->ctf_v3_header->cth_varoff;
+      else
+       return 0;
+    case CTF_SECT_TYPE:
+      return fp->ctf_header->btf.bth_type_len;
+    case CTF_SECT_STR:
+      return fp->ctf_header->btf.bth_str_len;
+    default:
+      ctf_set_errno (fp, ECTF_DUMPSECTUNKNOWN);
+      return 0;
+    }
+}
+
 /* Return the CTF handle for the parent CTF dict, if one exists.  Otherwise
    return NULL to indicate this dict has no imported parent.  */
 ctf_dict_t *
index 24480f37b861ef8df1e5903cdb0b5af4bc1277cb..8aa9f356db76e871c20fb0c524660fad3e435394 100644 (file)
@@ -167,6 +167,7 @@ LIBCTF_2.0 {
        ctf_write_mem;
        ctf_gzwrite;
        ctf_compress_write;
+       ctf_sect_size;
        ctf_getdatasect;
        ctf_getsymsect;
        ctf_getstrsect;