Remove two unnecessary variables from evaluate_subexp_standard
[deliverable/binutils-gdb.git] / libctf / ctf-util.c
CommitLineData
94585e7f 1/* Miscellaneous utilities.
b3adc24a 2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
94585e7f
NA
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
676c3ecb
NA
83/* Return 1 if the list is empty. */
84
85int
86ctf_list_empty_p (ctf_list_t *lp)
87{
88 return (lp->l_next == NULL && lp->l_prev == NULL);
89}
90
662df3c3
NA
91/* Splice one entire list onto the end of another one. The existing list is
92 emptied. */
93
94void
95ctf_list_splice (ctf_list_t *lp, ctf_list_t *append)
96{
97 if (ctf_list_empty_p (append))
98 return;
99
100 if (lp->l_prev != NULL)
101 lp->l_prev->l_next = append->l_next;
102 else
103 lp->l_next = append->l_next;
104
105 append->l_next->l_prev = lp->l_prev;
106 lp->l_prev = append->l_prev;
107 append->l_next = NULL;
108 append->l_prev = NULL;
109}
110
1136c379 111/* Convert a 32-bit ELF symbol to a ctf_link_sym_t. */
94585e7f 112
1136c379
NA
113ctf_link_sym_t *
114ctf_elf32_to_link_sym (ctf_dict_t *fp, ctf_link_sym_t *dst, const Elf32_Sym *src,
115 uint32_t symidx)
94585e7f 116{
1136c379
NA
117 /* The name must be in the external string table. */
118 if (src->st_name < fp->ctf_str[CTF_STRTAB_1].cts_len)
119 dst->st_name = (const char *) fp->ctf_str[CTF_STRTAB_1].cts_strs + src->st_name;
120 else
121 dst->st_name = _CTF_NULLSTR;
122 dst->st_nameidx_set = 0;
123 dst->st_symidx = symidx;
124 dst->st_shndx = src->st_shndx;
125 dst->st_type = ELF32_ST_TYPE (src->st_info);
94585e7f 126 dst->st_value = src->st_value;
1136c379
NA
127
128 return dst;
129}
130
131/* Convert a 64-bit ELF symbol to a ctf_link_sym_t. */
132
133ctf_link_sym_t *
134ctf_elf64_to_link_sym (ctf_dict_t *fp, ctf_link_sym_t *dst, const Elf64_Sym *src,
135 uint32_t symidx)
136{
137 /* The name must be in the external string table. */
138 if (src->st_name < fp->ctf_str[CTF_STRTAB_1].cts_len)
139 dst->st_name = (const char *) fp->ctf_str[CTF_STRTAB_1].cts_strs + src->st_name;
140 else
141 dst->st_name = _CTF_NULLSTR;
142 dst->st_nameidx_set = 0;
143 dst->st_symidx = symidx;
94585e7f 144 dst->st_shndx = src->st_shndx;
1136c379
NA
145 dst->st_type = ELF32_ST_TYPE (src->st_info);
146
147 /* We only care if the value is zero, so avoid nonzeroes turning into
148 zeroes. */
149 if (_libctf_unlikely_ (src->st_value != 0 && ((uint32_t) src->st_value == 0)))
150 dst->st_value = 1;
151 else
152 dst->st_value = (uint32_t) src->st_value;
94585e7f
NA
153
154 return dst;
155}
156
9323dd86 157/* A string appender working on dynamic strings. Returns NULL on OOM. */
94585e7f
NA
158
159char *
160ctf_str_append (char *s, const char *append)
161{
162 size_t s_len = 0;
163
164 if (append == NULL)
165 return s;
166
167 if (s != NULL)
168 s_len = strlen (s);
169
170 size_t append_len = strlen (append);
171
172 if ((s = realloc (s, s_len + append_len + 1)) == NULL)
173 return NULL;
174
175 memcpy (s + s_len, append, append_len);
176 s[s_len + append_len] = '\0';
177
178 return s;
179}
180
9323dd86
NA
181/* A version of ctf_str_append that returns the old string on OOM. */
182
183char *
184ctf_str_append_noerr (char *s, const char *append)
185{
186 char *new_s;
187
188 new_s = ctf_str_append (s, append);
189 if (!new_s)
190 return s;
191 return new_s;
192}
193
f5e9c9bd
NA
194/* A realloc() that fails noisily if called with any ctf_str_num_users. */
195void *
139633c3 196ctf_realloc (ctf_dict_t *fp, void *ptr, size_t size)
f5e9c9bd
NA
197{
198 if (fp->ctf_str_num_refs > 0)
199 {
200 ctf_dprintf ("%p: attempt to realloc() string table with %lu active refs\n",
201 (void *) fp, (unsigned long) fp->ctf_str_num_refs);
202 return NULL;
203 }
204 return realloc (ptr, size);
205}
206
94585e7f
NA
207/* Store the specified error code into errp if it is non-NULL, and then
208 return NULL for the benefit of the caller. */
209
210void *
211ctf_set_open_errno (int *errp, int error)
212{
213 if (errp != NULL)
214 *errp = error;
215 return NULL;
216}
217
139633c3
NA
218/* Store the specified error code into the CTF dict, and then return CTF_ERR /
219 -1 for the benefit of the caller. */
94585e7f 220
a0486bac 221unsigned long
139633c3 222ctf_set_errno (ctf_dict_t *fp, int err)
94585e7f
NA
223{
224 fp->ctf_errno = err;
225 return CTF_ERR;
226}
688d28f6
NA
227
228/* Create a ctf_next_t. */
229
230ctf_next_t *
231ctf_next_create (void)
232{
233 return calloc (1, sizeof (struct ctf_next));
234}
235
236/* Destroy a ctf_next_t, for early exit from iterators. */
237
238void
239ctf_next_destroy (ctf_next_t *i)
240{
e28591b3
NA
241 if (i == NULL)
242 return;
243
244 if (i->ctn_iter_fun == (void (*) (void)) ctf_dynhash_next_sorted)
245 free (i->u.ctn_sorted_hkv);
1136c379
NA
246 if (i->ctn_iter_fun == (void (*) (void)) ctf_symbol_next
247 && i->cu.ctn_fp->ctf_flags & LCTF_RDWR)
248 ctf_next_destroy (i->u.ctn_next);
688d28f6
NA
249 free (i);
250}
251
252/* Copy a ctf_next_t. */
253
254ctf_next_t *
255ctf_next_copy (ctf_next_t *i)
256{
257 ctf_next_t *i2;
258
259 if ((i2 = ctf_next_create()) == NULL)
260 return NULL;
261 memcpy (i2, i, sizeof (struct ctf_next));
e28591b3
NA
262
263 if (i2->ctn_iter_fun == (void (*) (void)) ctf_dynhash_next_sorted)
264 {
265 size_t els = ctf_dynhash_elements ((ctf_dynhash_t *) i->cu.ctn_h);
266 if ((i2->u.ctn_sorted_hkv = calloc (els, sizeof (ctf_next_hkv_t))) == NULL)
267 {
268 free (i2);
269 return NULL;
270 }
271 memcpy (i2->u.ctn_sorted_hkv, i->u.ctn_sorted_hkv,
272 els * sizeof (ctf_next_hkv_t));
273 }
688d28f6
NA
274 return i2;
275}
This page took 0.104881 seconds and 4 git commands to generate.