xref: /netbsd-src/external/gpl3/binutils.old/dist/libctf/ctf-util.c (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1867d70fcSchristos /* Miscellaneous utilities.
2*c42dbd0eSchristos    Copyright (C) 2019-2022 Free Software Foundation, Inc.
3867d70fcSchristos 
4867d70fcSchristos    This file is part of libctf.
5867d70fcSchristos 
6867d70fcSchristos    libctf is free software; you can redistribute it and/or modify it under
7867d70fcSchristos    the terms of the GNU General Public License as published by the Free
8867d70fcSchristos    Software Foundation; either version 3, or (at your option) any later
9867d70fcSchristos    version.
10867d70fcSchristos 
11867d70fcSchristos    This program is distributed in the hope that it will be useful, but
12867d70fcSchristos    WITHOUT ANY WARRANTY; without even the implied warranty of
13867d70fcSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14867d70fcSchristos    See the GNU General Public License for more details.
15867d70fcSchristos 
16867d70fcSchristos    You should have received a copy of the GNU General Public License
17867d70fcSchristos    along with this program; see the file COPYING.  If not see
18867d70fcSchristos    <http://www.gnu.org/licenses/>.  */
19867d70fcSchristos 
20867d70fcSchristos #include <ctf-impl.h>
21867d70fcSchristos #include <string.h>
22*c42dbd0eSchristos #include "ctf-endian.h"
23867d70fcSchristos 
24867d70fcSchristos /* Simple doubly-linked list append routine.  This implementation assumes that
25867d70fcSchristos    each list element contains an embedded ctf_list_t as the first member.
26867d70fcSchristos    An additional ctf_list_t is used to store the head (l_next) and tail
27867d70fcSchristos    (l_prev) pointers.  The current head and tail list elements have their
28867d70fcSchristos    previous and next pointers set to NULL, respectively.  */
29867d70fcSchristos 
30867d70fcSchristos void
ctf_list_append(ctf_list_t * lp,void * newp)31867d70fcSchristos ctf_list_append (ctf_list_t *lp, void *newp)
32867d70fcSchristos {
33867d70fcSchristos   ctf_list_t *p = lp->l_prev;	/* p = tail list element.  */
34867d70fcSchristos   ctf_list_t *q = newp;		/* q = new list element.  */
35867d70fcSchristos 
36867d70fcSchristos   lp->l_prev = q;
37867d70fcSchristos   q->l_prev = p;
38867d70fcSchristos   q->l_next = NULL;
39867d70fcSchristos 
40867d70fcSchristos   if (p != NULL)
41867d70fcSchristos     p->l_next = q;
42867d70fcSchristos   else
43867d70fcSchristos     lp->l_next = q;
44867d70fcSchristos }
45867d70fcSchristos 
46867d70fcSchristos /* Prepend the specified existing element to the given ctf_list_t.  The
47867d70fcSchristos    existing pointer should be pointing at a struct with embedded ctf_list_t.  */
48867d70fcSchristos 
49867d70fcSchristos void
ctf_list_prepend(ctf_list_t * lp,void * newp)50867d70fcSchristos ctf_list_prepend (ctf_list_t * lp, void *newp)
51867d70fcSchristos {
52867d70fcSchristos   ctf_list_t *p = newp;		/* p = new list element.  */
53867d70fcSchristos   ctf_list_t *q = lp->l_next;	/* q = head list element.  */
54867d70fcSchristos 
55867d70fcSchristos   lp->l_next = p;
56867d70fcSchristos   p->l_prev = NULL;
57867d70fcSchristos   p->l_next = q;
58867d70fcSchristos 
59867d70fcSchristos   if (q != NULL)
60867d70fcSchristos     q->l_prev = p;
61867d70fcSchristos   else
62867d70fcSchristos     lp->l_prev = p;
63867d70fcSchristos }
64867d70fcSchristos 
65867d70fcSchristos /* Delete the specified existing element from the given ctf_list_t.  The
66867d70fcSchristos    existing pointer should be pointing at a struct with embedded ctf_list_t.  */
67867d70fcSchristos 
68867d70fcSchristos void
ctf_list_delete(ctf_list_t * lp,void * existing)69867d70fcSchristos ctf_list_delete (ctf_list_t *lp, void *existing)
70867d70fcSchristos {
71867d70fcSchristos   ctf_list_t *p = existing;
72867d70fcSchristos 
73867d70fcSchristos   if (p->l_prev != NULL)
74867d70fcSchristos     p->l_prev->l_next = p->l_next;
75867d70fcSchristos   else
76867d70fcSchristos     lp->l_next = p->l_next;
77867d70fcSchristos 
78867d70fcSchristos   if (p->l_next != NULL)
79867d70fcSchristos     p->l_next->l_prev = p->l_prev;
80867d70fcSchristos   else
81867d70fcSchristos     lp->l_prev = p->l_prev;
82867d70fcSchristos }
83867d70fcSchristos 
84867d70fcSchristos /* Return 1 if the list is empty.  */
85867d70fcSchristos 
86867d70fcSchristos int
ctf_list_empty_p(ctf_list_t * lp)87867d70fcSchristos ctf_list_empty_p (ctf_list_t *lp)
88867d70fcSchristos {
89867d70fcSchristos   return (lp->l_next == NULL && lp->l_prev == NULL);
90867d70fcSchristos }
91867d70fcSchristos 
92*c42dbd0eSchristos /* Splice one entire list onto the end of another one.  The existing list is
93*c42dbd0eSchristos    emptied.  */
94867d70fcSchristos 
95*c42dbd0eSchristos void
ctf_list_splice(ctf_list_t * lp,ctf_list_t * append)96*c42dbd0eSchristos ctf_list_splice (ctf_list_t *lp, ctf_list_t *append)
97867d70fcSchristos {
98*c42dbd0eSchristos   if (ctf_list_empty_p (append))
99*c42dbd0eSchristos     return;
100*c42dbd0eSchristos 
101*c42dbd0eSchristos   if (lp->l_prev != NULL)
102*c42dbd0eSchristos     lp->l_prev->l_next = append->l_next;
103*c42dbd0eSchristos   else
104*c42dbd0eSchristos     lp->l_next = append->l_next;
105*c42dbd0eSchristos 
106*c42dbd0eSchristos   append->l_next->l_prev = lp->l_prev;
107*c42dbd0eSchristos   lp->l_prev = append->l_prev;
108*c42dbd0eSchristos   append->l_next = NULL;
109*c42dbd0eSchristos   append->l_prev = NULL;
110*c42dbd0eSchristos }
111*c42dbd0eSchristos 
112*c42dbd0eSchristos /* Convert a 32-bit ELF symbol to a ctf_link_sym_t.  */
113*c42dbd0eSchristos 
114*c42dbd0eSchristos ctf_link_sym_t *
ctf_elf32_to_link_sym(ctf_dict_t * fp,ctf_link_sym_t * dst,const Elf32_Sym * src,uint32_t symidx)115*c42dbd0eSchristos ctf_elf32_to_link_sym (ctf_dict_t *fp, ctf_link_sym_t *dst, const Elf32_Sym *src,
116*c42dbd0eSchristos 		       uint32_t symidx)
117*c42dbd0eSchristos {
118*c42dbd0eSchristos   Elf32_Sym tmp;
119*c42dbd0eSchristos   int needs_flipping = 0;
120*c42dbd0eSchristos 
121*c42dbd0eSchristos #ifdef WORDS_BIGENDIAN
122*c42dbd0eSchristos   if (fp->ctf_symsect_little_endian)
123*c42dbd0eSchristos     needs_flipping = 1;
124*c42dbd0eSchristos #else
125*c42dbd0eSchristos   if (!fp->ctf_symsect_little_endian)
126*c42dbd0eSchristos     needs_flipping = 1;
127*c42dbd0eSchristos #endif
128*c42dbd0eSchristos 
129*c42dbd0eSchristos   memcpy (&tmp, src, sizeof (Elf32_Sym));
130*c42dbd0eSchristos   if (needs_flipping)
131*c42dbd0eSchristos     {
132*c42dbd0eSchristos       swap_thing (tmp.st_name);
133*c42dbd0eSchristos       swap_thing (tmp.st_size);
134*c42dbd0eSchristos       swap_thing (tmp.st_shndx);
135*c42dbd0eSchristos       swap_thing (tmp.st_value);
136*c42dbd0eSchristos     }
137*c42dbd0eSchristos   /* The name must be in the external string table.  */
138*c42dbd0eSchristos   if (tmp.st_name < fp->ctf_str[CTF_STRTAB_1].cts_len)
139*c42dbd0eSchristos     dst->st_name = (const char *) fp->ctf_str[CTF_STRTAB_1].cts_strs + tmp.st_name;
140*c42dbd0eSchristos   else
141*c42dbd0eSchristos     dst->st_name = _CTF_NULLSTR;
142*c42dbd0eSchristos   dst->st_nameidx_set = 0;
143*c42dbd0eSchristos   dst->st_symidx = symidx;
144*c42dbd0eSchristos   dst->st_shndx = tmp.st_shndx;
145*c42dbd0eSchristos   dst->st_type = ELF32_ST_TYPE (tmp.st_info);
146*c42dbd0eSchristos   dst->st_value = tmp.st_value;
147*c42dbd0eSchristos 
148*c42dbd0eSchristos   return dst;
149*c42dbd0eSchristos }
150*c42dbd0eSchristos 
151*c42dbd0eSchristos /* Convert a 64-bit ELF symbol to a ctf_link_sym_t.  */
152*c42dbd0eSchristos 
153*c42dbd0eSchristos ctf_link_sym_t *
ctf_elf64_to_link_sym(ctf_dict_t * fp,ctf_link_sym_t * dst,const Elf64_Sym * src,uint32_t symidx)154*c42dbd0eSchristos ctf_elf64_to_link_sym (ctf_dict_t *fp, ctf_link_sym_t *dst, const Elf64_Sym *src,
155*c42dbd0eSchristos 		       uint32_t symidx)
156*c42dbd0eSchristos {
157*c42dbd0eSchristos   Elf64_Sym tmp;
158*c42dbd0eSchristos   int needs_flipping = 0;
159*c42dbd0eSchristos 
160*c42dbd0eSchristos #ifdef WORDS_BIGENDIAN
161*c42dbd0eSchristos   if (fp->ctf_symsect_little_endian)
162*c42dbd0eSchristos     needs_flipping = 1;
163*c42dbd0eSchristos #else
164*c42dbd0eSchristos   if (!fp->ctf_symsect_little_endian)
165*c42dbd0eSchristos     needs_flipping = 1;
166*c42dbd0eSchristos #endif
167*c42dbd0eSchristos 
168*c42dbd0eSchristos   memcpy (&tmp, src, sizeof (Elf64_Sym));
169*c42dbd0eSchristos   if (needs_flipping)
170*c42dbd0eSchristos     {
171*c42dbd0eSchristos       swap_thing (tmp.st_name);
172*c42dbd0eSchristos       swap_thing (tmp.st_size);
173*c42dbd0eSchristos       swap_thing (tmp.st_shndx);
174*c42dbd0eSchristos       swap_thing (tmp.st_value);
175*c42dbd0eSchristos     }
176*c42dbd0eSchristos 
177*c42dbd0eSchristos   /* The name must be in the external string table.  */
178*c42dbd0eSchristos   if (tmp.st_name < fp->ctf_str[CTF_STRTAB_1].cts_len)
179*c42dbd0eSchristos     dst->st_name = (const char *) fp->ctf_str[CTF_STRTAB_1].cts_strs + tmp.st_name;
180*c42dbd0eSchristos   else
181*c42dbd0eSchristos     dst->st_name = _CTF_NULLSTR;
182*c42dbd0eSchristos   dst->st_nameidx_set = 0;
183*c42dbd0eSchristos   dst->st_symidx = symidx;
184*c42dbd0eSchristos   dst->st_shndx = tmp.st_shndx;
185*c42dbd0eSchristos   dst->st_type = ELF32_ST_TYPE (tmp.st_info);
186*c42dbd0eSchristos 
187*c42dbd0eSchristos   /* We only care if the value is zero, so avoid nonzeroes turning into
188*c42dbd0eSchristos      zeroes.  */
189*c42dbd0eSchristos   if (_libctf_unlikely_ (tmp.st_value != 0 && ((uint32_t) tmp.st_value == 0)))
190*c42dbd0eSchristos     dst->st_value = 1;
191*c42dbd0eSchristos   else
192*c42dbd0eSchristos     dst->st_value = (uint32_t) tmp.st_value;
193867d70fcSchristos 
194867d70fcSchristos   return dst;
195867d70fcSchristos }
196867d70fcSchristos 
197867d70fcSchristos /* A string appender working on dynamic strings.  Returns NULL on OOM.  */
198867d70fcSchristos 
199867d70fcSchristos char *
ctf_str_append(char * s,const char * append)200867d70fcSchristos ctf_str_append (char *s, const char *append)
201867d70fcSchristos {
202867d70fcSchristos   size_t s_len = 0;
203867d70fcSchristos 
204867d70fcSchristos   if (append == NULL)
205867d70fcSchristos     return s;
206867d70fcSchristos 
207867d70fcSchristos   if (s != NULL)
208867d70fcSchristos     s_len = strlen (s);
209867d70fcSchristos 
210867d70fcSchristos   size_t append_len = strlen (append);
211867d70fcSchristos 
212867d70fcSchristos   if ((s = realloc (s, s_len + append_len + 1)) == NULL)
213867d70fcSchristos     return NULL;
214867d70fcSchristos 
215867d70fcSchristos   memcpy (s + s_len, append, append_len);
216867d70fcSchristos   s[s_len + append_len] = '\0';
217867d70fcSchristos 
218867d70fcSchristos   return s;
219867d70fcSchristos }
220867d70fcSchristos 
221867d70fcSchristos /* A version of ctf_str_append that returns the old string on OOM.  */
222867d70fcSchristos 
223867d70fcSchristos char *
ctf_str_append_noerr(char * s,const char * append)224867d70fcSchristos ctf_str_append_noerr (char *s, const char *append)
225867d70fcSchristos {
226867d70fcSchristos   char *new_s;
227867d70fcSchristos 
228867d70fcSchristos   new_s = ctf_str_append (s, append);
229867d70fcSchristos   if (!new_s)
230867d70fcSchristos     return s;
231867d70fcSchristos   return new_s;
232867d70fcSchristos }
233867d70fcSchristos 
234867d70fcSchristos /* A realloc() that fails noisily if called with any ctf_str_num_users.  */
235867d70fcSchristos void *
ctf_realloc(ctf_dict_t * fp,void * ptr,size_t size)236*c42dbd0eSchristos ctf_realloc (ctf_dict_t *fp, void *ptr, size_t size)
237867d70fcSchristos {
238867d70fcSchristos   if (fp->ctf_str_num_refs > 0)
239867d70fcSchristos     {
240867d70fcSchristos       ctf_dprintf ("%p: attempt to realloc() string table with %lu active refs\n",
241867d70fcSchristos 		   (void *) fp, (unsigned long) fp->ctf_str_num_refs);
242867d70fcSchristos       return NULL;
243867d70fcSchristos     }
244867d70fcSchristos   return realloc (ptr, size);
245867d70fcSchristos }
246867d70fcSchristos 
247867d70fcSchristos /* Store the specified error code into errp if it is non-NULL, and then
248867d70fcSchristos    return NULL for the benefit of the caller.  */
249867d70fcSchristos 
250867d70fcSchristos void *
ctf_set_open_errno(int * errp,int error)251867d70fcSchristos ctf_set_open_errno (int *errp, int error)
252867d70fcSchristos {
253867d70fcSchristos   if (errp != NULL)
254867d70fcSchristos     *errp = error;
255867d70fcSchristos   return NULL;
256867d70fcSchristos }
257867d70fcSchristos 
258*c42dbd0eSchristos /* Store the specified error code into the CTF dict, and then return CTF_ERR /
259*c42dbd0eSchristos    -1 for the benefit of the caller. */
260867d70fcSchristos 
261867d70fcSchristos unsigned long
ctf_set_errno(ctf_dict_t * fp,int err)262*c42dbd0eSchristos ctf_set_errno (ctf_dict_t *fp, int err)
263867d70fcSchristos {
264867d70fcSchristos   fp->ctf_errno = err;
265867d70fcSchristos   return CTF_ERR;
266867d70fcSchristos }
267*c42dbd0eSchristos 
268*c42dbd0eSchristos /* Create a ctf_next_t.  */
269*c42dbd0eSchristos 
270*c42dbd0eSchristos ctf_next_t *
ctf_next_create(void)271*c42dbd0eSchristos ctf_next_create (void)
272*c42dbd0eSchristos {
273*c42dbd0eSchristos   return calloc (1, sizeof (struct ctf_next));
274*c42dbd0eSchristos }
275*c42dbd0eSchristos 
276*c42dbd0eSchristos /* Destroy a ctf_next_t, for early exit from iterators.  */
277*c42dbd0eSchristos 
278*c42dbd0eSchristos void
ctf_next_destroy(ctf_next_t * i)279*c42dbd0eSchristos ctf_next_destroy (ctf_next_t *i)
280*c42dbd0eSchristos {
281*c42dbd0eSchristos   if (i == NULL)
282*c42dbd0eSchristos     return;
283*c42dbd0eSchristos 
284*c42dbd0eSchristos   if (i->ctn_iter_fun == (void (*) (void)) ctf_dynhash_next_sorted)
285*c42dbd0eSchristos     free (i->u.ctn_sorted_hkv);
286*c42dbd0eSchristos   if (i->ctn_next)
287*c42dbd0eSchristos     ctf_next_destroy (i->ctn_next);
288*c42dbd0eSchristos   free (i);
289*c42dbd0eSchristos }
290*c42dbd0eSchristos 
291*c42dbd0eSchristos /* Copy a ctf_next_t.  */
292*c42dbd0eSchristos 
293*c42dbd0eSchristos ctf_next_t *
ctf_next_copy(ctf_next_t * i)294*c42dbd0eSchristos ctf_next_copy (ctf_next_t *i)
295*c42dbd0eSchristos {
296*c42dbd0eSchristos   ctf_next_t *i2;
297*c42dbd0eSchristos 
298*c42dbd0eSchristos   if ((i2 = ctf_next_create()) == NULL)
299*c42dbd0eSchristos     return NULL;
300*c42dbd0eSchristos   memcpy (i2, i, sizeof (struct ctf_next));
301*c42dbd0eSchristos 
302*c42dbd0eSchristos   if (i2->ctn_iter_fun == (void (*) (void)) ctf_dynhash_next_sorted)
303*c42dbd0eSchristos     {
304*c42dbd0eSchristos       size_t els = ctf_dynhash_elements ((ctf_dynhash_t *) i->cu.ctn_h);
305*c42dbd0eSchristos       if ((i2->u.ctn_sorted_hkv = calloc (els, sizeof (ctf_next_hkv_t))) == NULL)
306*c42dbd0eSchristos 	{
307*c42dbd0eSchristos 	  free (i2);
308*c42dbd0eSchristos 	  return NULL;
309*c42dbd0eSchristos 	}
310*c42dbd0eSchristos       memcpy (i2->u.ctn_sorted_hkv, i->u.ctn_sorted_hkv,
311*c42dbd0eSchristos 	      els * sizeof (ctf_next_hkv_t));
312*c42dbd0eSchristos     }
313*c42dbd0eSchristos   return i2;
314*c42dbd0eSchristos }
315