xref: /dflybsd-src/contrib/binutils-2.34/libctf/ctf-util.c (revision b52ef7118d1621abed722c5bbbd542210290ecef)
1*fae548d3Szrj /* Miscellaneous utilities.
2*fae548d3Szrj    Copyright (C) 2019-2020 Free Software Foundation, Inc.
3*fae548d3Szrj 
4*fae548d3Szrj    This file is part of libctf.
5*fae548d3Szrj 
6*fae548d3Szrj    libctf is free software; you can redistribute it and/or modify it under
7*fae548d3Szrj    the terms of the GNU General Public License as published by the Free
8*fae548d3Szrj    Software Foundation; either version 3, or (at your option) any later
9*fae548d3Szrj    version.
10*fae548d3Szrj 
11*fae548d3Szrj    This program is distributed in the hope that it will be useful, but
12*fae548d3Szrj    WITHOUT ANY WARRANTY; without even the implied warranty of
13*fae548d3Szrj    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14*fae548d3Szrj    See the GNU General Public License for more details.
15*fae548d3Szrj 
16*fae548d3Szrj    You should have received a copy of the GNU General Public License
17*fae548d3Szrj    along with this program; see the file COPYING.  If not see
18*fae548d3Szrj    <http://www.gnu.org/licenses/>.  */
19*fae548d3Szrj 
20*fae548d3Szrj #include <ctf-impl.h>
21*fae548d3Szrj #include <string.h>
22*fae548d3Szrj 
23*fae548d3Szrj /* Simple doubly-linked list append routine.  This implementation assumes that
24*fae548d3Szrj    each list element contains an embedded ctf_list_t as the first member.
25*fae548d3Szrj    An additional ctf_list_t is used to store the head (l_next) and tail
26*fae548d3Szrj    (l_prev) pointers.  The current head and tail list elements have their
27*fae548d3Szrj    previous and next pointers set to NULL, respectively.  */
28*fae548d3Szrj 
29*fae548d3Szrj void
ctf_list_append(ctf_list_t * lp,void * newp)30*fae548d3Szrj ctf_list_append (ctf_list_t *lp, void *newp)
31*fae548d3Szrj {
32*fae548d3Szrj   ctf_list_t *p = lp->l_prev;	/* p = tail list element.  */
33*fae548d3Szrj   ctf_list_t *q = newp;		/* q = new list element.  */
34*fae548d3Szrj 
35*fae548d3Szrj   lp->l_prev = q;
36*fae548d3Szrj   q->l_prev = p;
37*fae548d3Szrj   q->l_next = NULL;
38*fae548d3Szrj 
39*fae548d3Szrj   if (p != NULL)
40*fae548d3Szrj     p->l_next = q;
41*fae548d3Szrj   else
42*fae548d3Szrj     lp->l_next = q;
43*fae548d3Szrj }
44*fae548d3Szrj 
45*fae548d3Szrj /* Prepend the specified existing element to the given ctf_list_t.  The
46*fae548d3Szrj    existing pointer should be pointing at a struct with embedded ctf_list_t.  */
47*fae548d3Szrj 
48*fae548d3Szrj void
ctf_list_prepend(ctf_list_t * lp,void * newp)49*fae548d3Szrj ctf_list_prepend (ctf_list_t * lp, void *newp)
50*fae548d3Szrj {
51*fae548d3Szrj   ctf_list_t *p = newp;		/* p = new list element.  */
52*fae548d3Szrj   ctf_list_t *q = lp->l_next;	/* q = head list element.  */
53*fae548d3Szrj 
54*fae548d3Szrj   lp->l_next = p;
55*fae548d3Szrj   p->l_prev = NULL;
56*fae548d3Szrj   p->l_next = q;
57*fae548d3Szrj 
58*fae548d3Szrj   if (q != NULL)
59*fae548d3Szrj     q->l_prev = p;
60*fae548d3Szrj   else
61*fae548d3Szrj     lp->l_prev = p;
62*fae548d3Szrj }
63*fae548d3Szrj 
64*fae548d3Szrj /* Delete the specified existing element from the given ctf_list_t.  The
65*fae548d3Szrj    existing pointer should be pointing at a struct with embedded ctf_list_t.  */
66*fae548d3Szrj 
67*fae548d3Szrj void
ctf_list_delete(ctf_list_t * lp,void * existing)68*fae548d3Szrj ctf_list_delete (ctf_list_t *lp, void *existing)
69*fae548d3Szrj {
70*fae548d3Szrj   ctf_list_t *p = existing;
71*fae548d3Szrj 
72*fae548d3Szrj   if (p->l_prev != NULL)
73*fae548d3Szrj     p->l_prev->l_next = p->l_next;
74*fae548d3Szrj   else
75*fae548d3Szrj     lp->l_next = p->l_next;
76*fae548d3Szrj 
77*fae548d3Szrj   if (p->l_next != NULL)
78*fae548d3Szrj     p->l_next->l_prev = p->l_prev;
79*fae548d3Szrj   else
80*fae548d3Szrj     lp->l_prev = p->l_prev;
81*fae548d3Szrj }
82*fae548d3Szrj 
83*fae548d3Szrj /* Return 1 if the list is empty.  */
84*fae548d3Szrj 
85*fae548d3Szrj int
ctf_list_empty_p(ctf_list_t * lp)86*fae548d3Szrj ctf_list_empty_p (ctf_list_t *lp)
87*fae548d3Szrj {
88*fae548d3Szrj   return (lp->l_next == NULL && lp->l_prev == NULL);
89*fae548d3Szrj }
90*fae548d3Szrj 
91*fae548d3Szrj /* Convert a 32-bit ELF symbol into Elf64 and return a pointer to it.  */
92*fae548d3Szrj 
93*fae548d3Szrj Elf64_Sym *
ctf_sym_to_elf64(const Elf32_Sym * src,Elf64_Sym * dst)94*fae548d3Szrj ctf_sym_to_elf64 (const Elf32_Sym *src, Elf64_Sym *dst)
95*fae548d3Szrj {
96*fae548d3Szrj   dst->st_name = src->st_name;
97*fae548d3Szrj   dst->st_value = src->st_value;
98*fae548d3Szrj   dst->st_size = src->st_size;
99*fae548d3Szrj   dst->st_info = src->st_info;
100*fae548d3Szrj   dst->st_other = src->st_other;
101*fae548d3Szrj   dst->st_shndx = src->st_shndx;
102*fae548d3Szrj 
103*fae548d3Szrj   return dst;
104*fae548d3Szrj }
105*fae548d3Szrj 
106*fae548d3Szrj /* A string appender working on dynamic strings.  Returns NULL on OOM.  */
107*fae548d3Szrj 
108*fae548d3Szrj char *
ctf_str_append(char * s,const char * append)109*fae548d3Szrj ctf_str_append (char *s, const char *append)
110*fae548d3Szrj {
111*fae548d3Szrj   size_t s_len = 0;
112*fae548d3Szrj 
113*fae548d3Szrj   if (append == NULL)
114*fae548d3Szrj     return s;
115*fae548d3Szrj 
116*fae548d3Szrj   if (s != NULL)
117*fae548d3Szrj     s_len = strlen (s);
118*fae548d3Szrj 
119*fae548d3Szrj   size_t append_len = strlen (append);
120*fae548d3Szrj 
121*fae548d3Szrj   if ((s = realloc (s, s_len + append_len + 1)) == NULL)
122*fae548d3Szrj     return NULL;
123*fae548d3Szrj 
124*fae548d3Szrj   memcpy (s + s_len, append, append_len);
125*fae548d3Szrj   s[s_len + append_len] = '\0';
126*fae548d3Szrj 
127*fae548d3Szrj   return s;
128*fae548d3Szrj }
129*fae548d3Szrj 
130*fae548d3Szrj /* A version of ctf_str_append that returns the old string on OOM.  */
131*fae548d3Szrj 
132*fae548d3Szrj char *
ctf_str_append_noerr(char * s,const char * append)133*fae548d3Szrj ctf_str_append_noerr (char *s, const char *append)
134*fae548d3Szrj {
135*fae548d3Szrj   char *new_s;
136*fae548d3Szrj 
137*fae548d3Szrj   new_s = ctf_str_append (s, append);
138*fae548d3Szrj   if (!new_s)
139*fae548d3Szrj     return s;
140*fae548d3Szrj   return new_s;
141*fae548d3Szrj }
142*fae548d3Szrj 
143*fae548d3Szrj /* A realloc() that fails noisily if called with any ctf_str_num_users.  */
144*fae548d3Szrj void *
ctf_realloc(ctf_file_t * fp,void * ptr,size_t size)145*fae548d3Szrj ctf_realloc (ctf_file_t *fp, void *ptr, size_t size)
146*fae548d3Szrj {
147*fae548d3Szrj   if (fp->ctf_str_num_refs > 0)
148*fae548d3Szrj     {
149*fae548d3Szrj       ctf_dprintf ("%p: attempt to realloc() string table with %lu active refs\n",
150*fae548d3Szrj 		   (void *) fp, (unsigned long) fp->ctf_str_num_refs);
151*fae548d3Szrj       return NULL;
152*fae548d3Szrj     }
153*fae548d3Szrj   return realloc (ptr, size);
154*fae548d3Szrj }
155*fae548d3Szrj 
156*fae548d3Szrj /* Store the specified error code into errp if it is non-NULL, and then
157*fae548d3Szrj    return NULL for the benefit of the caller.  */
158*fae548d3Szrj 
159*fae548d3Szrj void *
ctf_set_open_errno(int * errp,int error)160*fae548d3Szrj ctf_set_open_errno (int *errp, int error)
161*fae548d3Szrj {
162*fae548d3Szrj   if (errp != NULL)
163*fae548d3Szrj     *errp = error;
164*fae548d3Szrj   return NULL;
165*fae548d3Szrj }
166*fae548d3Szrj 
167*fae548d3Szrj /* Store the specified error code into the CTF container, and then return
168*fae548d3Szrj    CTF_ERR / -1 for the benefit of the caller. */
169*fae548d3Szrj 
170*fae548d3Szrj unsigned long
ctf_set_errno(ctf_file_t * fp,int err)171*fae548d3Szrj ctf_set_errno (ctf_file_t * fp, int err)
172*fae548d3Szrj {
173*fae548d3Szrj   fp->ctf_errno = err;
174*fae548d3Szrj   return CTF_ERR;
175*fae548d3Szrj }
176