From: Nick Alcock Date: Fri, 25 Apr 2025 17:26:45 +0000 (+0100) Subject: libctf: open-bfd: open BTF dicts X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=27d5d0ccc73858336cdca094b80cfefded3d1061;p=thirdparty%2Fbinutils-gdb.git libctf: open-bfd: open BTF dicts Teaching ctf_open and ctf_fdopen to open BTF dicts if passed is quite simple: we just need to check the magic number and allow BTF dicts into the lower-level ctf_simple_open machinery (which ultimately calls ctf_bufopen). --- diff --git a/libctf/ctf-open-bfd.c b/libctf/ctf-open-bfd.c index 7241de70709..d1210116f77 100644 --- a/libctf/ctf-open-bfd.c +++ b/libctf/ctf-open-bfd.c @@ -56,7 +56,8 @@ ctf_bfdopen (struct bfd *abfd, int *errp) libctf_init_debug(); - if ((ctf_asect = bfd_get_section_by_name (abfd, _CTF_SECTION)) == NULL) + if (((ctf_asect = bfd_get_section_by_name (abfd, _CTF_SECTION)) == NULL) + && ((ctf_asect = bfd_get_section_by_name (abfd, ".BTF")) == NULL)) { return (ctf_set_open_errno (errp, ECTF_NOCTFDATA)); } @@ -257,26 +258,32 @@ ctf_fdopen (int fd, const char *filename, const char *target, int *errp) struct stat st; ssize_t nbytes; - ctf_preamble_t ctfhdr; + ctf_preamble_v3_t *ctfhdr; + ctf_btf_preamble_t btfhdr; uint64_t arc_magic; - memset (&ctfhdr, 0, sizeof (ctfhdr)); + memset (&btfhdr, 0, sizeof (btfhdr)); libctf_init_debug(); if (fstat (fd, &st) == -1) return (ctf_set_open_errno (errp, errno)); - if ((nbytes = ctf_pread (fd, &ctfhdr, sizeof (ctfhdr), 0)) <= 0) + if ((nbytes = ctf_pread (fd, &btfhdr, sizeof (btfhdr) > sizeof (ctfhdr) + ? sizeof (btfhdr) : sizeof (ctfhdr), 0)) <= 0) return (ctf_set_open_errno (errp, nbytes < 0 ? errno : ECTF_FMT)); - - /* If we have read enough bytes to form a CTF header and the magic string - matches, in either endianness, attempt to interpret the file as raw - CTF. */ - - if ((size_t) nbytes >= sizeof (ctf_preamble_t) - && (ctfhdr.ctp_magic == CTF_MAGIC - || ctfhdr.ctp_magic == bswap_16 (CTF_MAGIC))) + ctfhdr = (ctf_preamble_v3_t *) &btfhdr; + + /* If we have read enough bytes to form a CTF or BTF header and the magic + string matches, in either endianness, attempt to interpret the file as raw + CTF/BTF. */ + + if (((size_t) nbytes >= sizeof (ctf_preamble_v3_t) + && (ctfhdr->ctp_magic == CTF_MAGIC + || ctfhdr->ctp_magic == bswap_16 (CTF_MAGIC))) + || ((size_t) nbytes >= sizeof (ctf_btf_preamble_t) + && (btfhdr.btf_magic == CTF_BTF_MAGIC + || btfhdr.btf_magic == bswap_16 (CTF_BTF_MAGIC)))) { ctf_dict_t *fp = NULL; void *data;