From: Nick Alcock Date: Fri, 25 Apr 2025 10:51:04 +0000 (+0100) Subject: libctf, open: new API for getting the size of CTF/BTF file sections X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f7f72bcca656527fd69b18b485cfe96b8c18dea8;p=thirdparty%2Fbinutils-gdb.git libctf, open: new API for getting the size of CTF/BTF file sections 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); --- diff --git a/include/ctf-api.h b/include/ctf-api.h index a23e1da0ace..27bd4731524 100644 --- a/include/ctf-api.h +++ b/include/ctf-api.h @@ -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. */ diff --git a/libctf/ctf-open.c b/libctf/ctf-open.c index 994372012b3..32f1414d91c 100644 --- a/libctf/ctf-open.c +++ b/libctf/ctf-open.c @@ -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 * diff --git a/libctf/libctf.ver b/libctf/libctf.ver index 24480f37b86..8aa9f356db7 100644 --- a/libctf/libctf.ver +++ b/libctf/libctf.ver @@ -167,6 +167,7 @@ LIBCTF_2.0 { ctf_write_mem; ctf_gzwrite; ctf_compress_write; + ctf_sect_size; ctf_getdatasect; ctf_getsymsect; ctf_getstrsect;