18dffb485Schristos /* Labelled ranges of type IDs. 2*12989c96Schristos Copyright (C) 2019-2024 Free Software Foundation, Inc. 38dffb485Schristos 48dffb485Schristos This file is part of libctf. 58dffb485Schristos 68dffb485Schristos libctf is free software; you can redistribute it and/or modify it under 78dffb485Schristos the terms of the GNU General Public License as published by the Free 88dffb485Schristos Software Foundation; either version 3, or (at your option) any later 98dffb485Schristos version. 108dffb485Schristos 118dffb485Schristos This program is distributed in the hope that it will be useful, but 128dffb485Schristos WITHOUT ANY WARRANTY; without even the implied warranty of 138dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 148dffb485Schristos See the GNU General Public License for more details. 158dffb485Schristos 168dffb485Schristos You should have received a copy of the GNU General Public License 178dffb485Schristos along with this program; see the file COPYING. If not see 188dffb485Schristos <http://www.gnu.org/licenses/>. */ 198dffb485Schristos 208dffb485Schristos #include <ctf-impl.h> 218dffb485Schristos #include <string.h> 228dffb485Schristos 238dffb485Schristos static int 244b169a6bSchristos extract_label_info (ctf_dict_t *fp, const ctf_lblent_t **ctl, 258dffb485Schristos uint32_t *num_labels) 268dffb485Schristos { 278dffb485Schristos const ctf_header_t *h; 288dffb485Schristos 298dffb485Schristos h = (const ctf_header_t *) fp->ctf_data.cts_data; 308dffb485Schristos 318dffb485Schristos *ctl = (const ctf_lblent_t *) (fp->ctf_buf + h->cth_lbloff); 328dffb485Schristos *num_labels = (h->cth_objtoff - h->cth_lbloff) / sizeof (ctf_lblent_t); 338dffb485Schristos 348dffb485Schristos return 0; 358dffb485Schristos } 368dffb485Schristos 378dffb485Schristos /* Returns the topmost label, or NULL if any errors are encountered. */ 388dffb485Schristos 398dffb485Schristos const char * 404b169a6bSchristos ctf_label_topmost (ctf_dict_t *fp) 418dffb485Schristos { 428dffb485Schristos const ctf_lblent_t *ctlp = NULL; 438dffb485Schristos const char *s; 448dffb485Schristos uint32_t num_labels = 0; 458dffb485Schristos 468dffb485Schristos if (extract_label_info (fp, &ctlp, &num_labels) < 0) 478dffb485Schristos return NULL; /* errno is set for us. */ 488dffb485Schristos 498dffb485Schristos if (num_labels == 0) 508dffb485Schristos { 518dffb485Schristos (void) ctf_set_errno (fp, ECTF_NOLABELDATA); 528dffb485Schristos return NULL; 538dffb485Schristos } 548dffb485Schristos 558dffb485Schristos if ((s = ctf_strraw (fp, (ctlp + num_labels - 1)->ctl_label)) == NULL) 568dffb485Schristos (void) ctf_set_errno (fp, ECTF_CORRUPT); 578dffb485Schristos 588dffb485Schristos return s; 598dffb485Schristos } 608dffb485Schristos 618dffb485Schristos /* Iterate over all labels. We pass the label string and the lblinfo_t struct 628dffb485Schristos to the specified callback function. */ 638dffb485Schristos int 644b169a6bSchristos ctf_label_iter (ctf_dict_t *fp, ctf_label_f *func, void *arg) 658dffb485Schristos { 668dffb485Schristos const ctf_lblent_t *ctlp = NULL; 678dffb485Schristos uint32_t i; 688dffb485Schristos uint32_t num_labels = 0; 698dffb485Schristos ctf_lblinfo_t linfo; 708dffb485Schristos const char *lname; 718dffb485Schristos int rc; 728dffb485Schristos 738dffb485Schristos if (extract_label_info (fp, &ctlp, &num_labels) < 0) 748dffb485Schristos return -1; /* errno is set for us. */ 758dffb485Schristos 768dffb485Schristos if (num_labels == 0) 778dffb485Schristos return (ctf_set_errno (fp, ECTF_NOLABELDATA)); 788dffb485Schristos 798dffb485Schristos for (i = 0; i < num_labels; i++, ctlp++) 808dffb485Schristos { 818dffb485Schristos if ((lname = ctf_strraw (fp, ctlp->ctl_label)) == NULL) 828dffb485Schristos { 838dffb485Schristos /* Not marked for translation: label code not used yet. */ 848dffb485Schristos ctf_err_warn (fp, 0, ECTF_CORRUPT, 858dffb485Schristos "failed to decode label %u with type %u", 868dffb485Schristos ctlp->ctl_label, ctlp->ctl_type); 878dffb485Schristos return (ctf_set_errno (fp, ECTF_CORRUPT)); 888dffb485Schristos } 898dffb485Schristos 908dffb485Schristos linfo.ctb_type = ctlp->ctl_type; 918dffb485Schristos if ((rc = func (lname, &linfo, arg)) != 0) 928dffb485Schristos return rc; 938dffb485Schristos } 948dffb485Schristos 958dffb485Schristos return 0; 968dffb485Schristos } 978dffb485Schristos 988dffb485Schristos typedef struct linfo_cb_arg 998dffb485Schristos { 1008dffb485Schristos const char *lca_name; /* Label we want to retrieve info for. */ 1018dffb485Schristos ctf_lblinfo_t *lca_info; /* Where to store the info about the label. */ 1028dffb485Schristos } linfo_cb_arg_t; 1038dffb485Schristos 1048dffb485Schristos static int 1058dffb485Schristos label_info_cb (const char *lname, const ctf_lblinfo_t *linfo, void *arg) 1068dffb485Schristos { 1078dffb485Schristos /* If lname matches the label we are looking for, copy the 1088dffb485Schristos lblinfo_t struct for the caller. */ 1098dffb485Schristos 1108dffb485Schristos if (strcmp (lname, ((linfo_cb_arg_t *) arg)->lca_name) == 0) 1118dffb485Schristos { 1128dffb485Schristos /* * Allow caller not to allocate storage to test if label exists. */ 1138dffb485Schristos 1148dffb485Schristos if (((linfo_cb_arg_t *) arg)->lca_info != NULL) 1158dffb485Schristos memcpy (((linfo_cb_arg_t *) arg)->lca_info, linfo, 1168dffb485Schristos sizeof (ctf_lblinfo_t)); 1178dffb485Schristos return 1; /* Indicate we found a match. */ 1188dffb485Schristos } 1198dffb485Schristos 1208dffb485Schristos return 0; 1218dffb485Schristos } 1228dffb485Schristos 1238dffb485Schristos /* Retrieve information about the label with name "lname". */ 1248dffb485Schristos int 1254b169a6bSchristos ctf_label_info (ctf_dict_t *fp, const char *lname, ctf_lblinfo_t *linfo) 1268dffb485Schristos { 1278dffb485Schristos linfo_cb_arg_t cb_arg; 1288dffb485Schristos int rc; 1298dffb485Schristos 1308dffb485Schristos cb_arg.lca_name = lname; 1318dffb485Schristos cb_arg.lca_info = linfo; 1328dffb485Schristos 1338dffb485Schristos if ((rc = ctf_label_iter (fp, label_info_cb, &cb_arg)) < 0) 1348dffb485Schristos return rc; 1358dffb485Schristos 1368dffb485Schristos if (rc != 1) 1378dffb485Schristos return (ctf_set_errno (fp, ECTF_NOLABEL)); 1388dffb485Schristos 1398dffb485Schristos return 0; 1408dffb485Schristos } 141