libctf: explicitly cast more size_t types used in printf()s
[deliverable/binutils-gdb.git] / libctf / ctf-util.c
CommitLineData
94585e7f
NA
1/* Miscellaneous utilities.
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 <string.h>
22
23/* Simple doubly-linked list append routine. This implementation assumes that
24 each list element contains an embedded ctf_list_t as the first member.
25 An additional ctf_list_t is used to store the head (l_next) and tail
26 (l_prev) pointers. The current head and tail list elements have their
27 previous and next pointers set to NULL, respectively. */
28
29void
30ctf_list_append (ctf_list_t *lp, void *newp)
31{
32 ctf_list_t *p = lp->l_prev; /* p = tail list element. */
33 ctf_list_t *q = newp; /* q = new list element. */
34
35 lp->l_prev = q;
36 q->l_prev = p;
37 q->l_next = NULL;
38
39 if (p != NULL)
40 p->l_next = q;
41 else
42 lp->l_next = q;
43}
44
45/* Prepend the specified existing element to the given ctf_list_t. The
46 existing pointer should be pointing at a struct with embedded ctf_list_t. */
47
48void
49ctf_list_prepend (ctf_list_t * lp, void *newp)
50{
51 ctf_list_t *p = newp; /* p = new list element. */
52 ctf_list_t *q = lp->l_next; /* q = head list element. */
53
54 lp->l_next = p;
55 p->l_prev = NULL;
56 p->l_next = q;
57
58 if (q != NULL)
59 q->l_prev = p;
60 else
61 lp->l_prev = p;
62}
63
64/* Delete the specified existing element from the given ctf_list_t. The
65 existing pointer should be pointing at a struct with embedded ctf_list_t. */
66
67void
68ctf_list_delete (ctf_list_t *lp, void *existing)
69{
70 ctf_list_t *p = existing;
71
72 if (p->l_prev != NULL)
73 p->l_prev->l_next = p->l_next;
74 else
75 lp->l_next = p->l_next;
76
77 if (p->l_next != NULL)
78 p->l_next->l_prev = p->l_prev;
79 else
80 lp->l_prev = p->l_prev;
81}
82
83/* Convert a 32-bit ELF symbol into Elf64 and return a pointer to it. */
84
85Elf64_Sym *
86ctf_sym_to_elf64 (const Elf32_Sym *src, Elf64_Sym *dst)
87{
88 dst->st_name = src->st_name;
89 dst->st_value = src->st_value;
90 dst->st_size = src->st_size;
91 dst->st_info = src->st_info;
92 dst->st_other = src->st_other;
93 dst->st_shndx = src->st_shndx;
94
95 return dst;
96}
97
98/* Convert an encoded CTF string name into a pointer to a C string by looking
99 up the appropriate string table buffer and then adding the offset. */
100
101const char *
102ctf_strraw (ctf_file_t *fp, uint32_t name)
103{
104 ctf_strs_t *ctsp = &fp->ctf_str[CTF_NAME_STID (name)];
105
106 if (ctsp->cts_strs != NULL && CTF_NAME_OFFSET (name) < ctsp->cts_len)
107 return (ctsp->cts_strs + CTF_NAME_OFFSET (name));
108
109 /* String table not loaded or corrupt offset. */
110 return NULL;
111}
112
113const char *
114ctf_strptr (ctf_file_t *fp, uint32_t name)
115{
116 const char *s = ctf_strraw (fp, name);
117 return (s != NULL ? s : "(?)");
118}
119
120/* Same as strdup(3C), but use ctf_alloc() to do the memory allocation. */
121
122_libctf_malloc_ char *
123ctf_strdup (const char *s1)
124{
125 char *s2 = ctf_alloc (strlen (s1) + 1);
126
127 if (s2 != NULL)
128 (void) strcpy (s2, s1);
129
130 return s2;
131}
132
133/* A string appender working on dynamic strings. */
134
135char *
136ctf_str_append (char *s, const char *append)
137{
138 size_t s_len = 0;
139
140 if (append == NULL)
141 return s;
142
143 if (s != NULL)
144 s_len = strlen (s);
145
146 size_t append_len = strlen (append);
147
148 if ((s = realloc (s, s_len + append_len + 1)) == NULL)
149 return NULL;
150
151 memcpy (s + s_len, append, append_len);
152 s[s_len + append_len] = '\0';
153
154 return s;
155}
156
157/* Store the specified error code into errp if it is non-NULL, and then
158 return NULL for the benefit of the caller. */
159
160void *
161ctf_set_open_errno (int *errp, int error)
162{
163 if (errp != NULL)
164 *errp = error;
165 return NULL;
166}
167
168/* Store the specified error code into the CTF container, and then return
a0486bac 169 CTF_ERR / -1 for the benefit of the caller. */
94585e7f 170
a0486bac 171unsigned long
94585e7f
NA
172ctf_set_errno (ctf_file_t * fp, int err)
173{
174 fp->ctf_errno = err;
175 return CTF_ERR;
176}
This page took 0.028708 seconds and 4 git commands to generate.