libctf: creation functions
[deliverable/binutils-gdb.git] / libctf / ctf-lookup.c
CommitLineData
47d546f4
NA
1/* Type lookup.
2 Copyright (C) 2019 Free Software Foundation, Inc.
3
4 This file is part of libctf.
5
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
19
20#include <ctf-impl.h>
21#include <elf.h>
22#include <string.h>
23
24/* Return the pointer to the internal CTF type data corresponding to the
25 given type ID. If the ID is invalid, the function returns NULL.
26 This function is not exported outside of the library. */
27
28const ctf_type_t *
29ctf_lookup_by_id (ctf_file_t **fpp, ctf_id_t type)
30{
31 ctf_file_t *fp = *fpp; /* Caller passes in starting CTF container. */
32 ctf_id_t idx;
33
34 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, type)
35 && (fp = fp->ctf_parent) == NULL)
36 {
37 (void) ctf_set_errno (*fpp, ECTF_NOPARENT);
38 return NULL;
39 }
40
41 idx = LCTF_TYPE_TO_INDEX (fp, type);
42 if (idx > 0 && (unsigned long) idx <= fp->ctf_typemax)
43 {
44 *fpp = fp; /* Function returns ending CTF container. */
45 return (LCTF_INDEX_TO_TYPEPTR (fp, idx));
46 }
47
48 /* If this container is writable, check for a dynamic type. */
49
50 if (fp->ctf_flags & LCTF_RDWR)
51 {
52 ctf_dtdef_t *dtd;
53
54 if ((dtd = ctf_dynamic_type (fp, type)) != NULL)
55 {
56 *fpp = fp;
57 return &dtd->dtd_data;
58 }
59 }
60 (void) ctf_set_errno (*fpp, ECTF_BADID);
61 return NULL;
62}
63
This page took 0.029667 seconds and 4 git commands to generate.