]>
Commit | Line | Data |
---|---|---|
94585e7f | 1 | /* Miscellaneous utilities. |
d87bef3a | 2 | Copyright (C) 2019-2023 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> | |
53651de8 | 22 | #include "ctf-endian.h" |
94585e7f NA |
23 | |
24 | /* Simple doubly-linked list append routine. This implementation assumes that | |
25 | each list element contains an embedded ctf_list_t as the first member. | |
26 | An additional ctf_list_t is used to store the head (l_next) and tail | |
27 | (l_prev) pointers. The current head and tail list elements have their | |
28 | previous and next pointers set to NULL, respectively. */ | |
29 | ||
30 | void | |
31 | ctf_list_append (ctf_list_t *lp, void *newp) | |
32 | { | |
33 | ctf_list_t *p = lp->l_prev; /* p = tail list element. */ | |
34 | ctf_list_t *q = newp; /* q = new list element. */ | |
35 | ||
36 | lp->l_prev = q; | |
37 | q->l_prev = p; | |
38 | q->l_next = NULL; | |
39 | ||
40 | if (p != NULL) | |
41 | p->l_next = q; | |
42 | else | |
43 | lp->l_next = q; | |
44 | } | |
45 | ||
46 | /* Prepend the specified existing element to the given ctf_list_t. The | |
47 | existing pointer should be pointing at a struct with embedded ctf_list_t. */ | |
48 | ||
49 | void | |
50 | ctf_list_prepend (ctf_list_t * lp, void *newp) | |
51 | { | |
52 | ctf_list_t *p = newp; /* p = new list element. */ | |
53 | ctf_list_t *q = lp->l_next; /* q = head list element. */ | |
54 | ||
55 | lp->l_next = p; | |
56 | p->l_prev = NULL; | |
57 | p->l_next = q; | |
58 | ||
59 | if (q != NULL) | |
60 | q->l_prev = p; | |
61 | else | |
62 | lp->l_prev = p; | |
63 | } | |
64 | ||
65 | /* Delete the specified existing element from the given ctf_list_t. The | |
66 | existing pointer should be pointing at a struct with embedded ctf_list_t. */ | |
67 | ||
68 | void | |
69 | ctf_list_delete (ctf_list_t *lp, void *existing) | |
70 | { | |
71 | ctf_list_t *p = existing; | |
72 | ||
73 | if (p->l_prev != NULL) | |
74 | p->l_prev->l_next = p->l_next; | |
75 | else | |
76 | lp->l_next = p->l_next; | |
77 | ||
78 | if (p->l_next != NULL) | |
79 | p->l_next->l_prev = p->l_prev; | |
80 | else | |
81 | lp->l_prev = p->l_prev; | |
82 | } | |
83 | ||
676c3ecb NA |
84 | /* Return 1 if the list is empty. */ |
85 | ||
86 | int | |
87 | ctf_list_empty_p (ctf_list_t *lp) | |
88 | { | |
89 | return (lp->l_next == NULL && lp->l_prev == NULL); | |
90 | } | |
91 | ||
662df3c3 NA |
92 | /* Splice one entire list onto the end of another one. The existing list is |
93 | emptied. */ | |
94 | ||
95 | void | |
96 | ctf_list_splice (ctf_list_t *lp, ctf_list_t *append) | |
97 | { | |
98 | if (ctf_list_empty_p (append)) | |
99 | return; | |
100 | ||
101 | if (lp->l_prev != NULL) | |
102 | lp->l_prev->l_next = append->l_next; | |
103 | else | |
104 | lp->l_next = append->l_next; | |
105 | ||
106 | append->l_next->l_prev = lp->l_prev; | |
107 | lp->l_prev = append->l_prev; | |
108 | append->l_next = NULL; | |
109 | append->l_prev = NULL; | |
110 | } | |
111 | ||
1136c379 | 112 | /* Convert a 32-bit ELF symbol to a ctf_link_sym_t. */ |
94585e7f | 113 | |
1136c379 NA |
114 | ctf_link_sym_t * |
115 | ctf_elf32_to_link_sym (ctf_dict_t *fp, ctf_link_sym_t *dst, const Elf32_Sym *src, | |
116 | uint32_t symidx) | |
94585e7f | 117 | { |
53651de8 NA |
118 | Elf32_Sym tmp; |
119 | int needs_flipping = 0; | |
120 | ||
121 | #ifdef WORDS_BIGENDIAN | |
122 | if (fp->ctf_symsect_little_endian) | |
123 | needs_flipping = 1; | |
124 | #else | |
125 | if (!fp->ctf_symsect_little_endian) | |
126 | needs_flipping = 1; | |
127 | #endif | |
128 | ||
129 | memcpy (&tmp, src, sizeof (Elf32_Sym)); | |
130 | if (needs_flipping) | |
131 | { | |
132 | swap_thing (tmp.st_name); | |
133 | swap_thing (tmp.st_size); | |
134 | swap_thing (tmp.st_shndx); | |
135 | swap_thing (tmp.st_value); | |
136 | } | |
1136c379 | 137 | /* The name must be in the external string table. */ |
53651de8 NA |
138 | if (tmp.st_name < fp->ctf_str[CTF_STRTAB_1].cts_len) |
139 | dst->st_name = (const char *) fp->ctf_str[CTF_STRTAB_1].cts_strs + tmp.st_name; | |
1136c379 NA |
140 | else |
141 | dst->st_name = _CTF_NULLSTR; | |
142 | dst->st_nameidx_set = 0; | |
143 | dst->st_symidx = symidx; | |
53651de8 NA |
144 | dst->st_shndx = tmp.st_shndx; |
145 | dst->st_type = ELF32_ST_TYPE (tmp.st_info); | |
146 | dst->st_value = tmp.st_value; | |
1136c379 NA |
147 | |
148 | return dst; | |
149 | } | |
150 | ||
151 | /* Convert a 64-bit ELF symbol to a ctf_link_sym_t. */ | |
152 | ||
153 | ctf_link_sym_t * | |
154 | ctf_elf64_to_link_sym (ctf_dict_t *fp, ctf_link_sym_t *dst, const Elf64_Sym *src, | |
155 | uint32_t symidx) | |
156 | { | |
53651de8 NA |
157 | Elf64_Sym tmp; |
158 | int needs_flipping = 0; | |
159 | ||
160 | #ifdef WORDS_BIGENDIAN | |
161 | if (fp->ctf_symsect_little_endian) | |
162 | needs_flipping = 1; | |
163 | #else | |
164 | if (!fp->ctf_symsect_little_endian) | |
165 | needs_flipping = 1; | |
166 | #endif | |
167 | ||
168 | memcpy (&tmp, src, sizeof (Elf64_Sym)); | |
169 | if (needs_flipping) | |
170 | { | |
171 | swap_thing (tmp.st_name); | |
172 | swap_thing (tmp.st_size); | |
173 | swap_thing (tmp.st_shndx); | |
174 | swap_thing (tmp.st_value); | |
175 | } | |
176 | ||
1136c379 | 177 | /* The name must be in the external string table. */ |
53651de8 NA |
178 | if (tmp.st_name < fp->ctf_str[CTF_STRTAB_1].cts_len) |
179 | dst->st_name = (const char *) fp->ctf_str[CTF_STRTAB_1].cts_strs + tmp.st_name; | |
1136c379 NA |
180 | else |
181 | dst->st_name = _CTF_NULLSTR; | |
182 | dst->st_nameidx_set = 0; | |
183 | dst->st_symidx = symidx; | |
53651de8 NA |
184 | dst->st_shndx = tmp.st_shndx; |
185 | dst->st_type = ELF32_ST_TYPE (tmp.st_info); | |
1136c379 NA |
186 | |
187 | /* We only care if the value is zero, so avoid nonzeroes turning into | |
188 | zeroes. */ | |
53651de8 | 189 | if (_libctf_unlikely_ (tmp.st_value != 0 && ((uint32_t) tmp.st_value == 0))) |
1136c379 NA |
190 | dst->st_value = 1; |
191 | else | |
53651de8 | 192 | dst->st_value = (uint32_t) tmp.st_value; |
94585e7f NA |
193 | |
194 | return dst; | |
195 | } | |
196 | ||
9323dd86 | 197 | /* A string appender working on dynamic strings. Returns NULL on OOM. */ |
94585e7f NA |
198 | |
199 | char * | |
200 | ctf_str_append (char *s, const char *append) | |
201 | { | |
202 | size_t s_len = 0; | |
203 | ||
204 | if (append == NULL) | |
205 | return s; | |
206 | ||
207 | if (s != NULL) | |
208 | s_len = strlen (s); | |
209 | ||
210 | size_t append_len = strlen (append); | |
211 | ||
212 | if ((s = realloc (s, s_len + append_len + 1)) == NULL) | |
213 | return NULL; | |
214 | ||
215 | memcpy (s + s_len, append, append_len); | |
216 | s[s_len + append_len] = '\0'; | |
217 | ||
218 | return s; | |
219 | } | |
220 | ||
9323dd86 NA |
221 | /* A version of ctf_str_append that returns the old string on OOM. */ |
222 | ||
223 | char * | |
224 | ctf_str_append_noerr (char *s, const char *append) | |
225 | { | |
226 | char *new_s; | |
227 | ||
228 | new_s = ctf_str_append (s, append); | |
229 | if (!new_s) | |
230 | return s; | |
231 | return new_s; | |
232 | } | |
233 | ||
f5e9c9bd NA |
234 | /* A realloc() that fails noisily if called with any ctf_str_num_users. */ |
235 | void * | |
139633c3 | 236 | ctf_realloc (ctf_dict_t *fp, void *ptr, size_t size) |
f5e9c9bd NA |
237 | { |
238 | if (fp->ctf_str_num_refs > 0) | |
239 | { | |
240 | ctf_dprintf ("%p: attempt to realloc() string table with %lu active refs\n", | |
241 | (void *) fp, (unsigned long) fp->ctf_str_num_refs); | |
242 | return NULL; | |
243 | } | |
244 | return realloc (ptr, size); | |
245 | } | |
246 | ||
94585e7f NA |
247 | /* Store the specified error code into errp if it is non-NULL, and then |
248 | return NULL for the benefit of the caller. */ | |
249 | ||
250 | void * | |
251 | ctf_set_open_errno (int *errp, int error) | |
252 | { | |
253 | if (errp != NULL) | |
254 | *errp = error; | |
255 | return NULL; | |
256 | } | |
257 | ||
688d28f6 NA |
258 | /* Create a ctf_next_t. */ |
259 | ||
260 | ctf_next_t * | |
261 | ctf_next_create (void) | |
262 | { | |
263 | return calloc (1, sizeof (struct ctf_next)); | |
264 | } | |
265 | ||
266 | /* Destroy a ctf_next_t, for early exit from iterators. */ | |
267 | ||
268 | void | |
269 | ctf_next_destroy (ctf_next_t *i) | |
270 | { | |
e28591b3 NA |
271 | if (i == NULL) |
272 | return; | |
273 | ||
274 | if (i->ctn_iter_fun == (void (*) (void)) ctf_dynhash_next_sorted) | |
275 | free (i->u.ctn_sorted_hkv); | |
6c3a3877 NA |
276 | if (i->ctn_next) |
277 | ctf_next_destroy (i->ctn_next); | |
688d28f6 NA |
278 | free (i); |
279 | } | |
280 | ||
281 | /* Copy a ctf_next_t. */ | |
282 | ||
283 | ctf_next_t * | |
284 | ctf_next_copy (ctf_next_t *i) | |
285 | { | |
286 | ctf_next_t *i2; | |
287 | ||
288 | if ((i2 = ctf_next_create()) == NULL) | |
289 | return NULL; | |
290 | memcpy (i2, i, sizeof (struct ctf_next)); | |
e28591b3 NA |
291 | |
292 | if (i2->ctn_iter_fun == (void (*) (void)) ctf_dynhash_next_sorted) | |
293 | { | |
294 | size_t els = ctf_dynhash_elements ((ctf_dynhash_t *) i->cu.ctn_h); | |
295 | if ((i2->u.ctn_sorted_hkv = calloc (els, sizeof (ctf_next_hkv_t))) == NULL) | |
296 | { | |
297 | free (i2); | |
298 | return NULL; | |
299 | } | |
300 | memcpy (i2->u.ctn_sorted_hkv, i->u.ctn_sorted_hkv, | |
301 | els * sizeof (ctf_next_hkv_t)); | |
302 | } | |
688d28f6 NA |
303 | return i2; |
304 | } |