Commit | Line | Data |
---|---|---|
47d546f4 | 1 | /* CTF file creation. |
250d07de | 2 | Copyright (C) 2019-2021 Free Software Foundation, Inc. |
47d546f4 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 <sys/param.h> | |
22 | #include <assert.h> | |
23 | #include <string.h> | |
c1401ecc | 24 | #include <unistd.h> |
47d546f4 NA |
25 | #include <zlib.h> |
26 | ||
1136c379 NA |
27 | #include <elf.h> |
28 | #include "elf-bfd.h" | |
29 | ||
555adca2 EZ |
30 | #ifndef EOVERFLOW |
31 | #define EOVERFLOW ERANGE | |
32 | #endif | |
33 | ||
a0486bac JM |
34 | #ifndef roundup |
35 | #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) | |
36 | #endif | |
37 | ||
676c3ecb NA |
38 | /* Make sure the ptrtab has enough space for at least one more type. |
39 | ||
40 | We start with 4KiB of ptrtab, enough for a thousand types, then grow it 25% | |
41 | at a time. */ | |
42 | ||
43 | static int | |
139633c3 | 44 | ctf_grow_ptrtab (ctf_dict_t *fp) |
676c3ecb NA |
45 | { |
46 | size_t new_ptrtab_len = fp->ctf_ptrtab_len; | |
47 | ||
48 | /* We allocate one more ptrtab entry than we need, for the initial zero, | |
49 | plus one because the caller will probably allocate a new type. */ | |
50 | ||
51 | if (fp->ctf_ptrtab == NULL) | |
52 | new_ptrtab_len = 1024; | |
53 | else if ((fp->ctf_typemax + 2) > fp->ctf_ptrtab_len) | |
54 | new_ptrtab_len = fp->ctf_ptrtab_len * 1.25; | |
55 | ||
56 | if (new_ptrtab_len != fp->ctf_ptrtab_len) | |
57 | { | |
58 | uint32_t *new_ptrtab; | |
59 | ||
60 | if ((new_ptrtab = realloc (fp->ctf_ptrtab, | |
61 | new_ptrtab_len * sizeof (uint32_t))) == NULL) | |
62 | return (ctf_set_errno (fp, ENOMEM)); | |
63 | ||
64 | fp->ctf_ptrtab = new_ptrtab; | |
65 | memset (fp->ctf_ptrtab + fp->ctf_ptrtab_len, 0, | |
66 | (new_ptrtab_len - fp->ctf_ptrtab_len) * sizeof (uint32_t)); | |
67 | fp->ctf_ptrtab_len = new_ptrtab_len; | |
68 | } | |
69 | return 0; | |
70 | } | |
71 | ||
139633c3 NA |
72 | /* To create an empty CTF dict, we just declare a zeroed header and call |
73 | ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new dict r/w and | |
74 | initialize the dynamic members. We start assigning type IDs at 1 because | |
f5e9c9bd | 75 | type ID 0 is used as a sentinel and a not-found indicator. */ |
47d546f4 | 76 | |
139633c3 | 77 | ctf_dict_t * |
47d546f4 NA |
78 | ctf_create (int *errp) |
79 | { | |
80 | static const ctf_header_t hdr = { .cth_preamble = { CTF_MAGIC, CTF_VERSION, 0 } }; | |
81 | ||
82 | ctf_dynhash_t *dthash; | |
83 | ctf_dynhash_t *dvhash; | |
676c3ecb | 84 | ctf_dynhash_t *structs = NULL, *unions = NULL, *enums = NULL, *names = NULL; |
1136c379 | 85 | ctf_dynhash_t *objthash = NULL, *funchash = NULL; |
47d546f4 | 86 | ctf_sect_t cts; |
139633c3 | 87 | ctf_dict_t *fp; |
47d546f4 NA |
88 | |
89 | libctf_init_debug(); | |
90 | dthash = ctf_dynhash_create (ctf_hash_integer, ctf_hash_eq_integer, | |
91 | NULL, NULL); | |
92 | if (dthash == NULL) | |
93 | { | |
94 | ctf_set_open_errno (errp, EAGAIN); | |
95 | goto err; | |
96 | } | |
97 | ||
98 | dvhash = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, | |
99 | NULL, NULL); | |
100 | if (dvhash == NULL) | |
101 | { | |
102 | ctf_set_open_errno (errp, EAGAIN); | |
103 | goto err_dt; | |
104 | } | |
105 | ||
676c3ecb NA |
106 | structs = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, |
107 | NULL, NULL); | |
108 | unions = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, | |
109 | NULL, NULL); | |
110 | enums = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, | |
111 | NULL, NULL); | |
112 | names = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, | |
113 | NULL, NULL); | |
1136c379 NA |
114 | objthash = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, |
115 | free, NULL); | |
116 | funchash = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, | |
117 | free, NULL); | |
676c3ecb | 118 | if (!structs || !unions || !enums || !names) |
47d546f4 NA |
119 | { |
120 | ctf_set_open_errno (errp, EAGAIN); | |
121 | goto err_dv; | |
122 | } | |
123 | ||
124 | cts.cts_name = _CTF_SECTION; | |
47d546f4 NA |
125 | cts.cts_data = &hdr; |
126 | cts.cts_size = sizeof (hdr); | |
127 | cts.cts_entsize = 1; | |
47d546f4 | 128 | |
676c3ecb NA |
129 | if ((fp = ctf_bufopen_internal (&cts, NULL, NULL, NULL, 1, errp)) == NULL) |
130 | goto err_dv; | |
47d546f4 | 131 | |
676c3ecb NA |
132 | fp->ctf_structs.ctn_writable = structs; |
133 | fp->ctf_unions.ctn_writable = unions; | |
134 | fp->ctf_enums.ctn_writable = enums; | |
135 | fp->ctf_names.ctn_writable = names; | |
1136c379 NA |
136 | fp->ctf_objthash = objthash; |
137 | fp->ctf_funchash = funchash; | |
47d546f4 NA |
138 | fp->ctf_dthash = dthash; |
139 | fp->ctf_dvhash = dvhash; | |
47d546f4 | 140 | fp->ctf_dtoldid = 0; |
f57cf0e3 | 141 | fp->ctf_snapshots = 1; |
47d546f4 | 142 | fp->ctf_snapshot_lu = 0; |
dd987f00 | 143 | fp->ctf_flags |= LCTF_DIRTY; |
47d546f4 | 144 | |
676c3ecb NA |
145 | ctf_set_ctl_hashes (fp); |
146 | ctf_setmodel (fp, CTF_MODEL_NATIVE); | |
147 | if (ctf_grow_ptrtab (fp) < 0) | |
148 | { | |
149 | ctf_set_open_errno (errp, ctf_errno (fp)); | |
139633c3 | 150 | ctf_dict_close (fp); |
676c3ecb NA |
151 | return NULL; |
152 | } | |
153 | ||
47d546f4 NA |
154 | return fp; |
155 | ||
47d546f4 | 156 | err_dv: |
676c3ecb NA |
157 | ctf_dynhash_destroy (structs); |
158 | ctf_dynhash_destroy (unions); | |
159 | ctf_dynhash_destroy (enums); | |
160 | ctf_dynhash_destroy (names); | |
1136c379 NA |
161 | ctf_dynhash_destroy (objthash); |
162 | ctf_dynhash_destroy (funchash); | |
47d546f4 NA |
163 | ctf_dynhash_destroy (dvhash); |
164 | err_dt: | |
165 | ctf_dynhash_destroy (dthash); | |
166 | err: | |
167 | return NULL; | |
168 | } | |
169 | ||
1136c379 NA |
170 | /* Delete data symbols that have been assigned names from the variable section. |
171 | Must be called from within ctf_serialize, because that is the only place | |
172 | you can safely delete variables without messing up ctf_rollback. */ | |
173 | ||
174 | static int | |
35a01a04 | 175 | symtypetab_delete_nonstatic_vars (ctf_dict_t *fp, ctf_dict_t *symfp) |
1136c379 NA |
176 | { |
177 | ctf_dvdef_t *dvd, *nvd; | |
178 | ctf_id_t type; | |
179 | ||
180 | for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd) | |
181 | { | |
182 | nvd = ctf_list_next (dvd); | |
183 | ||
184 | if (((type = (ctf_id_t) (uintptr_t) | |
185 | ctf_dynhash_lookup (fp->ctf_objthash, dvd->dvd_name)) > 0) | |
35a01a04 | 186 | && ctf_dynhash_lookup (symfp->ctf_dynsyms, dvd->dvd_name) != NULL |
1136c379 NA |
187 | && type == dvd->dvd_type) |
188 | ctf_dvd_delete (fp, dvd); | |
189 | } | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | /* Determine if a symbol is "skippable" and should never appear in the | |
195 | symtypetab sections. */ | |
196 | ||
197 | int | |
198 | ctf_symtab_skippable (ctf_link_sym_t *sym) | |
199 | { | |
200 | /* Never skip symbols whose name is not yet known. */ | |
201 | if (sym->st_nameidx_set) | |
202 | return 0; | |
203 | ||
204 | return (sym->st_name == NULL || sym->st_name[0] == 0 | |
205 | || sym->st_shndx == SHN_UNDEF | |
206 | || strcmp (sym->st_name, "_START_") == 0 | |
207 | || strcmp (sym->st_name, "_END_") == 0 | |
208 | || (sym->st_type == STT_OBJECT && sym->st_shndx == SHN_EXTABS | |
209 | && sym->st_value == 0)); | |
210 | } | |
211 | ||
212 | /* Symtypetab emission flags. */ | |
213 | ||
214 | #define CTF_SYMTYPETAB_EMIT_FUNCTION 0x1 | |
215 | #define CTF_SYMTYPETAB_EMIT_PAD 0x2 | |
216 | #define CTF_SYMTYPETAB_FORCE_INDEXED 0x4 | |
217 | ||
218 | /* Get the number of symbols in a symbol hash, the count of symbols, the maximum | |
219 | seen, the eventual size, without any padding elements, of the func/data and | |
220 | (if generated) index sections, and the size of accumulated padding elements. | |
35a01a04 NA |
221 | The linker-reported set of symbols is found in SYMFP: it may be NULL if |
222 | symbol filtering is not desired, in which case CTF_SYMTYPETAB_FORCE_INDEXED | |
223 | will always be set in the flags. | |
1136c379 NA |
224 | |
225 | Also figure out if any symbols need to be moved to the variable section, and | |
226 | add them (if not already present). */ | |
227 | ||
35a01a04 | 228 | _libctf_nonnull_ ((1,3,4,5,6,7,8)) |
1136c379 NA |
229 | static int |
230 | symtypetab_density (ctf_dict_t *fp, ctf_dict_t *symfp, ctf_dynhash_t *symhash, | |
231 | size_t *count, size_t *max, size_t *unpadsize, | |
232 | size_t *padsize, size_t *idxsize, int flags) | |
233 | { | |
234 | ctf_next_t *i = NULL; | |
235 | const void *name; | |
236 | const void *ctf_sym; | |
237 | ctf_dynhash_t *linker_known = NULL; | |
238 | int err; | |
239 | int beyond_max = 0; | |
240 | ||
241 | *count = 0; | |
242 | *max = 0; | |
243 | *unpadsize = 0; | |
244 | *idxsize = 0; | |
245 | *padsize = 0; | |
246 | ||
247 | if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED)) | |
248 | { | |
249 | /* Make a dynhash citing only symbols reported by the linker of the | |
250 | appropriate type, then traverse all potential-symbols we know the types | |
251 | of, removing them from linker_known as we go. Once this is done, the | |
252 | only symbols remaining in linker_known are symbols we don't know the | |
253 | types of: we must emit pads for those symbols that are below the | |
35a01a04 NA |
254 | maximum symbol we will emit (any beyond that are simply skipped). |
255 | ||
256 | If there are none, this symtypetab will be empty: just report that. */ | |
257 | ||
258 | if (!symfp->ctf_dynsyms) | |
259 | return 0; | |
1136c379 NA |
260 | |
261 | if ((linker_known = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, | |
262 | NULL, NULL)) == NULL) | |
263 | return (ctf_set_errno (fp, ENOMEM)); | |
264 | ||
265 | while ((err = ctf_dynhash_cnext (symfp->ctf_dynsyms, &i, | |
266 | &name, &ctf_sym)) == 0) | |
267 | { | |
268 | ctf_link_sym_t *sym = (ctf_link_sym_t *) ctf_sym; | |
269 | ||
270 | if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION) | |
271 | && sym->st_type != STT_FUNC) | |
272 | || (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION) | |
273 | && sym->st_type != STT_OBJECT)) | |
274 | continue; | |
275 | ||
276 | if (ctf_symtab_skippable (sym)) | |
277 | continue; | |
278 | ||
279 | /* This should only be true briefly before all the names are | |
280 | finalized, long before we get this far. */ | |
281 | if (!ctf_assert (fp, !sym->st_nameidx_set)) | |
282 | return -1; /* errno is set for us. */ | |
283 | ||
284 | if (ctf_dynhash_cinsert (linker_known, name, ctf_sym) < 0) | |
285 | { | |
286 | ctf_dynhash_destroy (linker_known); | |
287 | return (ctf_set_errno (fp, ENOMEM)); | |
288 | } | |
289 | } | |
290 | if (err != ECTF_NEXT_END) | |
291 | { | |
292 | ctf_err_warn (fp, 0, err, _("iterating over linker-known symbols during " | |
293 | "serialization")); | |
294 | ctf_dynhash_destroy (linker_known); | |
295 | return (ctf_set_errno (fp, err)); | |
296 | } | |
297 | } | |
298 | ||
299 | while ((err = ctf_dynhash_cnext (symhash, &i, &name, NULL)) == 0) | |
300 | { | |
301 | ctf_link_sym_t *sym; | |
302 | ||
303 | if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED)) | |
304 | { | |
305 | /* Linker did not report symbol in symtab. Remove it from the | |
306 | set of known data symbols and continue. */ | |
307 | if ((sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, name)) == NULL) | |
308 | { | |
309 | ctf_dynhash_remove (symhash, name); | |
310 | continue; | |
311 | } | |
312 | ||
313 | /* We don't remove skippable symbols from the symhash because we don't | |
314 | want them to be migrated into variables. */ | |
315 | if (ctf_symtab_skippable (sym)) | |
316 | continue; | |
317 | ||
318 | if ((flags & CTF_SYMTYPETAB_EMIT_FUNCTION) | |
319 | && sym->st_type != STT_FUNC) | |
320 | { | |
321 | ctf_err_warn (fp, 1, 0, _("Symbol %x added to CTF as a function " | |
322 | "but is of type %x\n"), | |
323 | sym->st_symidx, sym->st_type); | |
324 | ctf_dynhash_remove (symhash, name); | |
325 | continue; | |
326 | } | |
327 | else if (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION) | |
328 | && sym->st_type != STT_OBJECT) | |
329 | { | |
330 | ctf_err_warn (fp, 1, 0, _("Symbol %x added to CTF as a data " | |
331 | "object but is of type %x\n"), | |
332 | sym->st_symidx, sym->st_type); | |
333 | ctf_dynhash_remove (symhash, name); | |
334 | continue; | |
335 | } | |
336 | ||
337 | ctf_dynhash_remove (linker_known, name); | |
338 | } | |
339 | *unpadsize += sizeof (uint32_t); | |
340 | (*count)++; | |
341 | ||
342 | if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED)) | |
343 | { | |
344 | if (*max < sym->st_symidx) | |
345 | *max = sym->st_symidx; | |
346 | } | |
347 | else | |
348 | (*max)++; | |
349 | } | |
350 | if (err != ECTF_NEXT_END) | |
351 | { | |
352 | ctf_err_warn (fp, 0, err, _("iterating over CTF symtypetab during " | |
353 | "serialization")); | |
354 | ctf_dynhash_destroy (linker_known); | |
355 | return (ctf_set_errno (fp, err)); | |
356 | } | |
357 | ||
358 | if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED)) | |
359 | { | |
360 | while ((err = ctf_dynhash_cnext (linker_known, &i, NULL, &ctf_sym)) == 0) | |
361 | { | |
362 | ctf_link_sym_t *sym = (ctf_link_sym_t *) ctf_sym; | |
363 | ||
364 | if (sym->st_symidx > *max) | |
365 | beyond_max++; | |
366 | } | |
367 | if (err != ECTF_NEXT_END) | |
368 | { | |
369 | ctf_err_warn (fp, 0, err, _("iterating over linker-known symbols " | |
370 | "during CTF serialization")); | |
371 | ctf_dynhash_destroy (linker_known); | |
372 | return (ctf_set_errno (fp, err)); | |
373 | } | |
374 | } | |
375 | ||
376 | *idxsize = *count * sizeof (uint32_t); | |
377 | if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED)) | |
378 | *padsize = (ctf_dynhash_elements (linker_known) - beyond_max) * sizeof (uint32_t); | |
379 | ||
380 | ctf_dynhash_destroy (linker_known); | |
381 | return 0; | |
382 | } | |
383 | ||
384 | /* Emit an objt or func symtypetab into DP in a particular order defined by an | |
385 | array of ctf_link_sym_t or symbol names passed in. The index has NIDX | |
386 | elements in it: unindexed output would terminate at symbol OUTMAX and is in | |
387 | any case no larger than SIZE bytes. Some index elements are expected to be | |
388 | skipped: see symtypetab_density. The linker-reported set of symbols (if any) | |
389 | is found in SYMFP. */ | |
390 | static int | |
391 | emit_symtypetab (ctf_dict_t *fp, ctf_dict_t *symfp, uint32_t *dp, | |
392 | ctf_link_sym_t **idx, const char **nameidx, uint32_t nidx, | |
393 | uint32_t outmax, int size, int flags) | |
394 | { | |
395 | uint32_t i; | |
396 | uint32_t *dpp = dp; | |
397 | ctf_dynhash_t *symhash; | |
398 | ||
399 | ctf_dprintf ("Emitting table of size %i, outmax %u, %u symtypetab entries, " | |
400 | "flags %i\n", size, outmax, nidx, flags); | |
401 | ||
402 | /* Empty table? Nothing to do. */ | |
403 | if (size == 0) | |
404 | return 0; | |
405 | ||
406 | if (flags & CTF_SYMTYPETAB_EMIT_FUNCTION) | |
407 | symhash = fp->ctf_funchash; | |
408 | else | |
409 | symhash = fp->ctf_objthash; | |
410 | ||
411 | for (i = 0; i < nidx; i++) | |
412 | { | |
413 | const char *sym_name; | |
414 | void *type; | |
415 | ||
416 | /* If we have a linker-reported set of symbols, we may be given that set | |
417 | to work from, or a set of symbol names. In both cases we want to look | |
418 | at the corresponding linker-reported symbol (if any). */ | |
419 | if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED)) | |
420 | { | |
421 | ctf_link_sym_t *this_link_sym; | |
422 | ||
423 | if (idx) | |
424 | this_link_sym = idx[i]; | |
425 | else | |
426 | this_link_sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, nameidx[i]); | |
427 | ||
428 | /* Unreported symbol number. No pad, no nothing. */ | |
429 | if (!this_link_sym) | |
430 | continue; | |
431 | ||
432 | /* Symbol of the wrong type, or skippable? This symbol is not in this | |
433 | table. */ | |
434 | if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION) | |
435 | && this_link_sym->st_type != STT_FUNC) | |
436 | || (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION) | |
437 | && this_link_sym->st_type != STT_OBJECT)) | |
438 | continue; | |
439 | ||
440 | if (ctf_symtab_skippable (this_link_sym)) | |
441 | continue; | |
442 | ||
443 | sym_name = this_link_sym->st_name; | |
444 | ||
445 | /* Linker reports symbol of a different type to the symbol we actually | |
446 | added? Skip the symbol. No pad, since the symbol doesn't actually | |
447 | belong in this table at all. (Warned about in | |
448 | symtypetab_density.) */ | |
449 | if ((this_link_sym->st_type == STT_FUNC) | |
450 | && (ctf_dynhash_lookup (fp->ctf_objthash, sym_name))) | |
451 | continue; | |
452 | ||
453 | if ((this_link_sym->st_type == STT_OBJECT) | |
454 | && (ctf_dynhash_lookup (fp->ctf_funchash, sym_name))) | |
455 | continue; | |
456 | } | |
457 | else | |
458 | sym_name = nameidx[i]; | |
459 | ||
460 | /* Symbol in index but no type set? Silently skip and (optionally) | |
461 | pad. (In force-indexed mode, this is also where we track symbols of | |
462 | the wrong type for this round of insertion.) */ | |
463 | if ((type = ctf_dynhash_lookup (symhash, sym_name)) == NULL) | |
464 | { | |
465 | if (flags & CTF_SYMTYPETAB_EMIT_PAD) | |
466 | *dpp++ = 0; | |
467 | continue; | |
468 | } | |
469 | ||
470 | if (!ctf_assert (fp, (((char *) dpp) - (char *) dp) < size)) | |
471 | return -1; /* errno is set for us. */ | |
472 | ||
473 | *dpp++ = (ctf_id_t) (uintptr_t) type; | |
474 | ||
475 | /* When emitting unindexed output, all later symbols are pads: stop | |
476 | early. */ | |
477 | if ((flags & CTF_SYMTYPETAB_EMIT_PAD) && idx[i]->st_symidx == outmax) | |
478 | break; | |
479 | } | |
480 | ||
481 | return 0; | |
482 | } | |
483 | ||
484 | /* Emit an objt or func symtypetab index into DP in a paticular order defined by | |
485 | an array of symbol names passed in. Stop at NIDX. The linker-reported set | |
486 | of symbols (if any) is found in SYMFP. */ | |
487 | static int | |
488 | emit_symtypetab_index (ctf_dict_t *fp, ctf_dict_t *symfp, uint32_t *dp, | |
489 | const char **idx, uint32_t nidx, int size, int flags) | |
490 | { | |
491 | uint32_t i; | |
492 | uint32_t *dpp = dp; | |
493 | ctf_dynhash_t *symhash; | |
494 | ||
495 | ctf_dprintf ("Emitting index of size %i, %u entries reported by linker, " | |
496 | "flags %i\n", size, nidx, flags); | |
497 | ||
498 | /* Empty table? Nothing to do. */ | |
499 | if (size == 0) | |
500 | return 0; | |
501 | ||
502 | if (flags & CTF_SYMTYPETAB_EMIT_FUNCTION) | |
503 | symhash = fp->ctf_funchash; | |
504 | else | |
505 | symhash = fp->ctf_objthash; | |
506 | ||
507 | /* Indexes should always be unpadded. */ | |
508 | if (!ctf_assert (fp, !(flags & CTF_SYMTYPETAB_EMIT_PAD))) | |
509 | return -1; /* errno is set for us. */ | |
510 | ||
511 | for (i = 0; i < nidx; i++) | |
512 | { | |
513 | const char *sym_name; | |
514 | void *type; | |
515 | ||
516 | if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED)) | |
517 | { | |
518 | ctf_link_sym_t *this_link_sym; | |
519 | ||
520 | this_link_sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, idx[i]); | |
521 | ||
522 | /* This is an index: unreported symbols should never appear in it. */ | |
523 | if (!ctf_assert (fp, this_link_sym != NULL)) | |
524 | return -1; /* errno is set for us. */ | |
525 | ||
526 | /* Symbol of the wrong type, or skippable? This symbol is not in this | |
527 | table. */ | |
528 | if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION) | |
529 | && this_link_sym->st_type != STT_FUNC) | |
530 | || (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION) | |
531 | && this_link_sym->st_type != STT_OBJECT)) | |
532 | continue; | |
533 | ||
534 | if (ctf_symtab_skippable (this_link_sym)) | |
535 | continue; | |
536 | ||
537 | sym_name = this_link_sym->st_name; | |
538 | ||
539 | /* Linker reports symbol of a different type to the symbol we actually | |
540 | added? Skip the symbol. */ | |
541 | if ((this_link_sym->st_type == STT_FUNC) | |
542 | && (ctf_dynhash_lookup (fp->ctf_objthash, sym_name))) | |
543 | continue; | |
544 | ||
545 | if ((this_link_sym->st_type == STT_OBJECT) | |
546 | && (ctf_dynhash_lookup (fp->ctf_funchash, sym_name))) | |
547 | continue; | |
548 | } | |
549 | else | |
550 | sym_name = idx[i]; | |
551 | ||
552 | /* Symbol in index and reported by linker, but no type set? Silently skip | |
553 | and (optionally) pad. (In force-indexed mode, this is also where we | |
554 | track symbols of the wrong type for this round of insertion.) */ | |
555 | if ((type = ctf_dynhash_lookup (symhash, sym_name)) == NULL) | |
556 | continue; | |
557 | ||
558 | ctf_str_add_ref (fp, sym_name, dpp++); | |
559 | ||
560 | if (!ctf_assert (fp, (((char *) dpp) - (char *) dp) <= size)) | |
561 | return -1; /* errno is set for us. */ | |
562 | } | |
563 | ||
564 | return 0; | |
565 | } | |
566 | ||
47d546f4 | 567 | static unsigned char * |
139633c3 | 568 | ctf_copy_smembers (ctf_dict_t *fp, ctf_dtdef_t *dtd, unsigned char *t) |
47d546f4 NA |
569 | { |
570 | ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members); | |
571 | ctf_member_t ctm; | |
572 | ||
573 | for (; dmd != NULL; dmd = ctf_list_next (dmd)) | |
574 | { | |
f5e9c9bd | 575 | ctf_member_t *copied; |
47d546f4 | 576 | |
f5e9c9bd | 577 | ctm.ctm_name = 0; |
47d546f4 NA |
578 | ctm.ctm_type = (uint32_t) dmd->dmd_type; |
579 | ctm.ctm_offset = (uint32_t) dmd->dmd_offset; | |
580 | ||
581 | memcpy (t, &ctm, sizeof (ctm)); | |
f5e9c9bd NA |
582 | copied = (ctf_member_t *) t; |
583 | if (dmd->dmd_name) | |
584 | ctf_str_add_ref (fp, dmd->dmd_name, &copied->ctm_name); | |
585 | ||
47d546f4 NA |
586 | t += sizeof (ctm); |
587 | } | |
588 | ||
589 | return t; | |
590 | } | |
591 | ||
592 | static unsigned char * | |
139633c3 | 593 | ctf_copy_lmembers (ctf_dict_t *fp, ctf_dtdef_t *dtd, unsigned char *t) |
47d546f4 NA |
594 | { |
595 | ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members); | |
596 | ctf_lmember_t ctlm; | |
597 | ||
598 | for (; dmd != NULL; dmd = ctf_list_next (dmd)) | |
599 | { | |
f5e9c9bd | 600 | ctf_lmember_t *copied; |
47d546f4 | 601 | |
f5e9c9bd | 602 | ctlm.ctlm_name = 0; |
47d546f4 NA |
603 | ctlm.ctlm_type = (uint32_t) dmd->dmd_type; |
604 | ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI (dmd->dmd_offset); | |
605 | ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO (dmd->dmd_offset); | |
606 | ||
607 | memcpy (t, &ctlm, sizeof (ctlm)); | |
f5e9c9bd NA |
608 | copied = (ctf_lmember_t *) t; |
609 | if (dmd->dmd_name) | |
610 | ctf_str_add_ref (fp, dmd->dmd_name, &copied->ctlm_name); | |
611 | ||
47d546f4 NA |
612 | t += sizeof (ctlm); |
613 | } | |
614 | ||
615 | return t; | |
616 | } | |
617 | ||
618 | static unsigned char * | |
139633c3 | 619 | ctf_copy_emembers (ctf_dict_t *fp, ctf_dtdef_t *dtd, unsigned char *t) |
47d546f4 NA |
620 | { |
621 | ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members); | |
622 | ctf_enum_t cte; | |
623 | ||
624 | for (; dmd != NULL; dmd = ctf_list_next (dmd)) | |
625 | { | |
f5e9c9bd NA |
626 | ctf_enum_t *copied; |
627 | ||
47d546f4 | 628 | cte.cte_value = dmd->dmd_value; |
47d546f4 | 629 | memcpy (t, &cte, sizeof (cte)); |
f5e9c9bd NA |
630 | copied = (ctf_enum_t *) t; |
631 | ctf_str_add_ref (fp, dmd->dmd_name, &copied->cte_name); | |
47d546f4 NA |
632 | t += sizeof (cte); |
633 | } | |
634 | ||
635 | return t; | |
636 | } | |
637 | ||
47d546f4 NA |
638 | /* Sort a newly-constructed static variable array. */ |
639 | ||
d851ecd3 NA |
640 | typedef struct ctf_sort_var_arg_cb |
641 | { | |
139633c3 | 642 | ctf_dict_t *fp; |
d851ecd3 NA |
643 | ctf_strs_t *strtab; |
644 | } ctf_sort_var_arg_cb_t; | |
645 | ||
47d546f4 | 646 | static int |
d851ecd3 | 647 | ctf_sort_var (const void *one_, const void *two_, void *arg_) |
47d546f4 NA |
648 | { |
649 | const ctf_varent_t *one = one_; | |
650 | const ctf_varent_t *two = two_; | |
d851ecd3 | 651 | ctf_sort_var_arg_cb_t *arg = arg_; |
47d546f4 | 652 | |
d851ecd3 NA |
653 | return (strcmp (ctf_strraw_explicit (arg->fp, one->ctv_name, arg->strtab), |
654 | ctf_strraw_explicit (arg->fp, two->ctv_name, arg->strtab))); | |
47d546f4 NA |
655 | } |
656 | ||
676c3ecb | 657 | /* Compatibility: just update the threshold for ctf_discard. */ |
47d546f4 | 658 | int |
139633c3 | 659 | ctf_update (ctf_dict_t *fp) |
676c3ecb NA |
660 | { |
661 | if (!(fp->ctf_flags & LCTF_RDWR)) | |
662 | return (ctf_set_errno (fp, ECTF_RDONLY)); | |
663 | ||
664 | fp->ctf_dtoldid = fp->ctf_typemax; | |
665 | return 0; | |
666 | } | |
667 | ||
139633c3 NA |
668 | /* If the specified CTF dict is writable and has been modified, reload this dict |
669 | with the updated type definitions, ready for serialization. In order to make | |
670 | this code and the rest of libctf as simple as possible, we perform updates by | |
671 | taking the dynamic type definitions and creating an in-memory CTF dict | |
672 | containing the definitions, and then call ctf_simple_open_internal() on it. | |
673 | We perform one extra trick here for the benefit of callers and to keep our | |
674 | code simple: ctf_simple_open_internal() will return a new ctf_dict_t, but we | |
675 | want to keep the fp constant for the caller, so after | |
676 | ctf_simple_open_internal() returns, we use memcpy to swap the interior of the | |
677 | old and new ctf_dict_t's, and then free the old. */ | |
676c3ecb | 678 | int |
139633c3 | 679 | ctf_serialize (ctf_dict_t *fp) |
47d546f4 | 680 | { |
139633c3 | 681 | ctf_dict_t ofp, *nfp; |
f5e9c9bd | 682 | ctf_header_t hdr, *hdrp; |
47d546f4 NA |
683 | ctf_dtdef_t *dtd; |
684 | ctf_dvdef_t *dvd; | |
685 | ctf_varent_t *dvarents; | |
f5e9c9bd | 686 | ctf_strs_writable_t strtab; |
47d546f4 | 687 | |
f5e9c9bd | 688 | unsigned char *t; |
47d546f4 | 689 | unsigned long i; |
1136c379 NA |
690 | size_t buf_size, type_size, objt_size, func_size; |
691 | size_t objt_unpadsize, func_unpadsize, objt_padsize, func_padsize; | |
692 | size_t funcidx_size, objtidx_size; | |
693 | size_t nvars, nfuncs, nobjts, maxobjt, maxfunc; | |
35a01a04 | 694 | size_t nsymtypes = 0; |
1136c379 NA |
695 | const char **sym_name_order = NULL; |
696 | unsigned char *buf = NULL, *newbuf; | |
47d546f4 NA |
697 | int err; |
698 | ||
35a01a04 NA |
699 | /* Symtab filtering. If filter_syms is true, symfp is set: otherwise, |
700 | CTF_SYMTYPETAB_FORCE_INDEXED is set in symflags. */ | |
701 | int filter_syms = 0; | |
702 | int sort_syms = 1; | |
703 | int symflags = 0; | |
704 | ctf_dict_t *symfp = NULL; | |
705 | ||
47d546f4 NA |
706 | if (!(fp->ctf_flags & LCTF_RDWR)) |
707 | return (ctf_set_errno (fp, ECTF_RDONLY)); | |
708 | ||
709 | /* Update required? */ | |
710 | if (!(fp->ctf_flags & LCTF_DIRTY)) | |
711 | return 0; | |
712 | ||
35a01a04 NA |
713 | /* If doing a writeout as part of linking, and the link flags request it, |
714 | filter out reported symbols from the variable section, and filter out all | |
715 | other symbols from the symtypetab sections. (If we are not linking, the | |
716 | symbols are sorted; if we are linking, don't bother sorting if we are not | |
717 | filtering out reported symbols: this is almost certaily an ld -r and only | |
718 | the linker is likely to consume these symtypetabs again. The linker | |
719 | doesn't care what order the symtypetab entries is in, since it only | |
720 | iterates over symbols and does not use the ctf_lookup_by_symbol* API.) */ | |
721 | ||
722 | if (fp->ctf_flags & LCTF_LINKING) | |
723 | { | |
724 | filter_syms = !(fp->ctf_link_flags & CTF_LINK_NO_FILTER_REPORTED_SYMS); | |
725 | if (!filter_syms) | |
726 | sort_syms = 0; | |
727 | } | |
728 | ||
47d546f4 NA |
729 | /* Fill in an initial CTF header. We will leave the label, object, |
730 | and function sections empty and only output a header, type section, | |
731 | and string table. The type section begins at a 4-byte aligned | |
1136c379 NA |
732 | boundary past the CTF header itself (at relative offset zero). The flag |
733 | indicating a new-style function info section (an array of CTF_K_FUNCTION | |
734 | type IDs in the types section) is flipped on. */ | |
47d546f4 NA |
735 | |
736 | memset (&hdr, 0, sizeof (hdr)); | |
737 | hdr.cth_magic = CTF_MAGIC; | |
738 | hdr.cth_version = CTF_VERSION; | |
739 | ||
1136c379 NA |
740 | /* This is a new-format func info section, and the symtab and strtab come out |
741 | of the dynsym and dynstr these days. */ | |
742 | hdr.cth_flags = (CTF_F_NEWFUNCINFO | CTF_F_DYNSTR); | |
3d16b64e | 743 | |
47d546f4 NA |
744 | /* Iterate through the dynamic type definition list and compute the |
745 | size of the CTF type section we will need to generate. */ | |
746 | ||
747 | for (type_size = 0, dtd = ctf_list_next (&fp->ctf_dtdefs); | |
748 | dtd != NULL; dtd = ctf_list_next (dtd)) | |
749 | { | |
750 | uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); | |
751 | uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info); | |
752 | ||
753 | if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT) | |
754 | type_size += sizeof (ctf_stype_t); | |
755 | else | |
756 | type_size += sizeof (ctf_type_t); | |
757 | ||
758 | switch (kind) | |
759 | { | |
760 | case CTF_K_INTEGER: | |
761 | case CTF_K_FLOAT: | |
762 | type_size += sizeof (uint32_t); | |
763 | break; | |
764 | case CTF_K_ARRAY: | |
765 | type_size += sizeof (ctf_array_t); | |
766 | break; | |
767 | case CTF_K_SLICE: | |
768 | type_size += sizeof (ctf_slice_t); | |
769 | break; | |
770 | case CTF_K_FUNCTION: | |
771 | type_size += sizeof (uint32_t) * (vlen + (vlen & 1)); | |
772 | break; | |
773 | case CTF_K_STRUCT: | |
774 | case CTF_K_UNION: | |
775 | if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH) | |
776 | type_size += sizeof (ctf_member_t) * vlen; | |
777 | else | |
778 | type_size += sizeof (ctf_lmember_t) * vlen; | |
779 | break; | |
780 | case CTF_K_ENUM: | |
781 | type_size += sizeof (ctf_enum_t) * vlen; | |
782 | break; | |
783 | } | |
784 | } | |
785 | ||
35a01a04 | 786 | /* Find the dict to which the linker has reported symbols, if any. */ |
1136c379 | 787 | |
35a01a04 NA |
788 | if (filter_syms) |
789 | { | |
790 | if (!fp->ctf_dynsyms && fp->ctf_parent && fp->ctf_parent->ctf_dynsyms) | |
791 | symfp = fp->ctf_parent; | |
792 | else | |
793 | symfp = fp; | |
794 | } | |
1136c379 | 795 | |
35a01a04 NA |
796 | /* If not filtering, keep all potential symbols in an unsorted, indexed |
797 | dict. */ | |
798 | if (!filter_syms) | |
1136c379 NA |
799 | symflags = CTF_SYMTYPETAB_FORCE_INDEXED; |
800 | else | |
801 | hdr.cth_flags |= CTF_F_IDXSORTED; | |
802 | ||
35a01a04 NA |
803 | if (!ctf_assert (fp, (filter_syms && symfp) |
804 | || (!filter_syms && !symfp | |
805 | && ((symflags & CTF_SYMTYPETAB_FORCE_INDEXED) != 0)))) | |
806 | return -1; | |
807 | ||
1136c379 NA |
808 | /* Work out the sizes of the object and function sections, and work out the |
809 | number of pad (unassigned) symbols in each, and the overall size of the | |
810 | sections. */ | |
811 | ||
812 | if (symtypetab_density (fp, symfp, fp->ctf_objthash, &nobjts, &maxobjt, | |
813 | &objt_unpadsize, &objt_padsize, &objtidx_size, | |
814 | symflags) < 0) | |
815 | return -1; /* errno is set for us. */ | |
816 | ||
817 | ctf_dprintf ("Object symtypetab: %i objects, max %i, unpadded size %i, " | |
818 | "%i bytes of pads, index size %i\n", (int) nobjts, (int) maxobjt, | |
819 | (int) objt_unpadsize, (int) objt_padsize, (int) objtidx_size); | |
820 | ||
821 | if (symtypetab_density (fp, symfp, fp->ctf_funchash, &nfuncs, &maxfunc, | |
822 | &func_unpadsize, &func_padsize, &funcidx_size, | |
823 | symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0) | |
824 | return -1; /* errno is set for us. */ | |
825 | ||
826 | ctf_dprintf ("Function symtypetab: %i functions, max %i, unpadded size %i, " | |
827 | "%i bytes of pads, index size %i\n", (int) nfuncs, (int) maxfunc, | |
828 | (int) func_unpadsize, (int) func_padsize, (int) funcidx_size); | |
829 | ||
35a01a04 NA |
830 | /* If we are filtering symbols out, those symbols that the linker has not |
831 | reported have now been removed from the ctf_objthash and ctf_funchash. | |
832 | Delete entries from the variable section that duplicate newly-added data | |
833 | symbols. There's no need to migrate new ones in, because the compiler | |
834 | always emits both a variable and a data symbol simultaneously, and | |
835 | filtering only happens at final link time. */ | |
1136c379 | 836 | |
35a01a04 NA |
837 | if (filter_syms && symfp->ctf_dynsyms && |
838 | symtypetab_delete_nonstatic_vars (fp, symfp) < 0) | |
1136c379 NA |
839 | return -1; |
840 | ||
841 | /* It is worth indexing each section if it would save space to do so, due to | |
842 | reducing the number of pads sufficiently. A pad is the same size as a | |
843 | single index entry: but index sections compress relatively poorly compared | |
844 | to constant pads, so it takes a lot of contiguous padding to equal one | |
845 | index section entry. It would be nice to be able to *verify* whether we | |
846 | would save space after compression rather than guessing, but this seems | |
847 | difficult, since it would require complete reserialization. Regardless, if | |
848 | the linker has not reported any symbols (e.g. if this is not a final link | |
849 | but just an ld -r), we must emit things in indexed fashion just as the | |
850 | compiler does. */ | |
851 | ||
852 | objt_size = objt_unpadsize; | |
853 | if (!(symflags & CTF_SYMTYPETAB_FORCE_INDEXED) | |
854 | && ((objt_padsize + objt_unpadsize) * CTF_INDEX_PAD_THRESHOLD | |
855 | > objt_padsize)) | |
856 | { | |
857 | objt_size += objt_padsize; | |
858 | objtidx_size = 0; | |
859 | } | |
860 | ||
861 | func_size = func_unpadsize; | |
862 | if (!(symflags & CTF_SYMTYPETAB_FORCE_INDEXED) | |
863 | && ((func_padsize + func_unpadsize) * CTF_INDEX_PAD_THRESHOLD | |
864 | > func_padsize)) | |
865 | { | |
866 | func_size += func_padsize; | |
867 | funcidx_size = 0; | |
868 | } | |
869 | ||
47d546f4 NA |
870 | /* Computing the number of entries in the CTF variable section is much |
871 | simpler. */ | |
872 | ||
873 | for (nvars = 0, dvd = ctf_list_next (&fp->ctf_dvdefs); | |
874 | dvd != NULL; dvd = ctf_list_next (dvd), nvars++); | |
875 | ||
f5e9c9bd NA |
876 | /* Compute the size of the CTF buffer we need, sans only the string table, |
877 | then allocate a new buffer and memcpy the finished header to the start of | |
878 | the buffer. (We will adjust this later with strtab length info.) */ | |
47d546f4 | 879 | |
1136c379 NA |
880 | hdr.cth_lbloff = hdr.cth_objtoff = 0; |
881 | hdr.cth_funcoff = hdr.cth_objtoff + objt_size; | |
882 | hdr.cth_objtidxoff = hdr.cth_funcoff + func_size; | |
883 | hdr.cth_funcidxoff = hdr.cth_objtidxoff + objtidx_size; | |
884 | hdr.cth_varoff = hdr.cth_funcidxoff + funcidx_size; | |
47d546f4 NA |
885 | hdr.cth_typeoff = hdr.cth_varoff + (nvars * sizeof (ctf_varent_t)); |
886 | hdr.cth_stroff = hdr.cth_typeoff + type_size; | |
f5e9c9bd | 887 | hdr.cth_strlen = 0; |
47d546f4 NA |
888 | |
889 | buf_size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen; | |
890 | ||
65365aa8 | 891 | if ((buf = malloc (buf_size)) == NULL) |
47d546f4 NA |
892 | return (ctf_set_errno (fp, EAGAIN)); |
893 | ||
894 | memcpy (buf, &hdr, sizeof (ctf_header_t)); | |
1136c379 | 895 | t = (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_objtoff; |
47d546f4 | 896 | |
f5e9c9bd NA |
897 | hdrp = (ctf_header_t *) buf; |
898 | if ((fp->ctf_flags & LCTF_CHILD) && (fp->ctf_parname != NULL)) | |
899 | ctf_str_add_ref (fp, fp->ctf_parname, &hdrp->cth_parname); | |
fd55eae8 NA |
900 | if (fp->ctf_cuname != NULL) |
901 | ctf_str_add_ref (fp, fp->ctf_cuname, &hdrp->cth_cuname); | |
47d546f4 | 902 | |
35a01a04 | 903 | /* Sort the linker's symbols into name order if need be. */ |
1136c379 NA |
904 | |
905 | if ((objtidx_size != 0) || (funcidx_size != 0)) | |
906 | { | |
907 | ctf_next_t *i = NULL; | |
908 | void *symname; | |
909 | const char **walk; | |
910 | int err; | |
911 | ||
35a01a04 NA |
912 | if (filter_syms) |
913 | { | |
914 | if (symfp->ctf_dynsyms) | |
915 | nsymtypes = ctf_dynhash_elements (symfp->ctf_dynsyms); | |
916 | else | |
917 | nsymtypes = 0; | |
918 | } | |
1136c379 | 919 | else |
35a01a04 NA |
920 | nsymtypes = ctf_dynhash_elements (fp->ctf_objthash) |
921 | + ctf_dynhash_elements (fp->ctf_funchash); | |
1136c379 | 922 | |
35a01a04 | 923 | if ((sym_name_order = calloc (nsymtypes, sizeof (const char *))) == NULL) |
1136c379 NA |
924 | goto oom; |
925 | ||
926 | walk = sym_name_order; | |
927 | ||
35a01a04 | 928 | if (filter_syms) |
1136c379 | 929 | { |
35a01a04 NA |
930 | if (symfp->ctf_dynsyms) |
931 | { | |
932 | while ((err = ctf_dynhash_next_sorted (symfp->ctf_dynsyms, &i, | |
933 | &symname, NULL, | |
934 | ctf_dynhash_sort_by_name, | |
935 | NULL)) == 0) | |
936 | *walk++ = (const char *) symname; | |
937 | if (err != ECTF_NEXT_END) | |
938 | goto symerr; | |
939 | } | |
1136c379 NA |
940 | } |
941 | else | |
942 | { | |
35a01a04 NA |
943 | ctf_hash_sort_f sort_fun = NULL; |
944 | ||
945 | /* Since we partition the set of symbols back into objt and func, | |
946 | we can sort the two independently without harm. */ | |
947 | if (sort_syms) | |
948 | sort_fun = ctf_dynhash_sort_by_name; | |
949 | ||
950 | while ((err = ctf_dynhash_next_sorted (fp->ctf_objthash, &i, &symname, | |
951 | NULL, sort_fun, NULL)) == 0) | |
1136c379 NA |
952 | *walk++ = (const char *) symname; |
953 | if (err != ECTF_NEXT_END) | |
954 | goto symerr; | |
955 | ||
35a01a04 NA |
956 | while ((err = ctf_dynhash_next_sorted (fp->ctf_funchash, &i, &symname, |
957 | NULL, sort_fun, NULL)) == 0) | |
1136c379 NA |
958 | *walk++ = (const char *) symname; |
959 | if (err != ECTF_NEXT_END) | |
960 | goto symerr; | |
961 | } | |
962 | } | |
963 | ||
964 | /* Emit the object and function sections, and if necessary their indexes. | |
965 | Emission is done in symtab order if there is no index, and in index | |
966 | (name) order otherwise. */ | |
967 | ||
35a01a04 | 968 | if ((objtidx_size == 0) && symfp && symfp->ctf_dynsymidx) |
1136c379 NA |
969 | { |
970 | ctf_dprintf ("Emitting unindexed objt symtypetab\n"); | |
971 | if (emit_symtypetab (fp, symfp, (uint32_t *) t, symfp->ctf_dynsymidx, | |
972 | NULL, symfp->ctf_dynsymmax + 1, maxobjt, objt_size, | |
973 | symflags | CTF_SYMTYPETAB_EMIT_PAD) < 0) | |
974 | goto err; /* errno is set for us. */ | |
975 | } | |
976 | else | |
977 | { | |
978 | ctf_dprintf ("Emitting indexed objt symtypetab\n"); | |
979 | if (emit_symtypetab (fp, symfp, (uint32_t *) t, NULL, sym_name_order, | |
35a01a04 | 980 | nsymtypes, maxobjt, objt_size, symflags) < 0) |
1136c379 NA |
981 | goto err; /* errno is set for us. */ |
982 | } | |
983 | ||
984 | t += objt_size; | |
985 | ||
35a01a04 | 986 | if ((funcidx_size == 0) && symfp && symfp->ctf_dynsymidx) |
1136c379 NA |
987 | { |
988 | ctf_dprintf ("Emitting unindexed func symtypetab\n"); | |
989 | if (emit_symtypetab (fp, symfp, (uint32_t *) t, symfp->ctf_dynsymidx, | |
990 | NULL, symfp->ctf_dynsymmax + 1, maxfunc, | |
991 | func_size, symflags | CTF_SYMTYPETAB_EMIT_FUNCTION | |
992 | | CTF_SYMTYPETAB_EMIT_PAD) < 0) | |
993 | goto err; /* errno is set for us. */ | |
994 | } | |
995 | else | |
996 | { | |
997 | ctf_dprintf ("Emitting indexed func symtypetab\n"); | |
998 | if (emit_symtypetab (fp, symfp, (uint32_t *) t, NULL, sym_name_order, | |
35a01a04 | 999 | nsymtypes, maxfunc, func_size, |
1136c379 NA |
1000 | symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0) |
1001 | goto err; /* errno is set for us. */ | |
1002 | } | |
1003 | ||
1004 | t += func_size; | |
1005 | ||
1006 | if (objtidx_size > 0) | |
1007 | if (emit_symtypetab_index (fp, symfp, (uint32_t *) t, sym_name_order, | |
35a01a04 | 1008 | nsymtypes, objtidx_size, symflags) < 0) |
1136c379 NA |
1009 | goto err; |
1010 | ||
1011 | t += objtidx_size; | |
1012 | ||
1013 | if (funcidx_size > 0) | |
1014 | if (emit_symtypetab_index (fp, symfp, (uint32_t *) t, sym_name_order, | |
35a01a04 | 1015 | nsymtypes, funcidx_size, |
1136c379 NA |
1016 | symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0) |
1017 | goto err; | |
1018 | ||
1019 | t += funcidx_size; | |
1020 | free (sym_name_order); | |
1021 | sym_name_order = NULL; | |
1022 | ||
f5e9c9bd NA |
1023 | /* Work over the variable list, translating everything into ctf_varent_t's and |
1024 | prepping the string table. */ | |
47d546f4 NA |
1025 | |
1026 | dvarents = (ctf_varent_t *) t; | |
1027 | for (i = 0, dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; | |
1028 | dvd = ctf_list_next (dvd), i++) | |
1029 | { | |
1030 | ctf_varent_t *var = &dvarents[i]; | |
47d546f4 | 1031 | |
f5e9c9bd | 1032 | ctf_str_add_ref (fp, dvd->dvd_name, &var->ctv_name); |
9943fa3a | 1033 | var->ctv_type = (uint32_t) dvd->dvd_type; |
47d546f4 NA |
1034 | } |
1035 | assert (i == nvars); | |
1036 | ||
47d546f4 NA |
1037 | t += sizeof (ctf_varent_t) * nvars; |
1038 | ||
1039 | assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_typeoff); | |
1040 | ||
f5e9c9bd NA |
1041 | /* We now take a final lap through the dynamic type definition list and copy |
1042 | the appropriate type records to the output buffer, noting down the | |
1043 | strings as we go. */ | |
47d546f4 NA |
1044 | |
1045 | for (dtd = ctf_list_next (&fp->ctf_dtdefs); | |
1046 | dtd != NULL; dtd = ctf_list_next (dtd)) | |
1047 | { | |
47d546f4 NA |
1048 | uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); |
1049 | uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info); | |
1050 | ||
1051 | ctf_array_t cta; | |
1052 | uint32_t encoding; | |
1053 | size_t len; | |
f5e9c9bd | 1054 | ctf_stype_t *copied; |
676c3ecb | 1055 | const char *name; |
47d546f4 NA |
1056 | |
1057 | if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT) | |
1058 | len = sizeof (ctf_stype_t); | |
1059 | else | |
1060 | len = sizeof (ctf_type_t); | |
1061 | ||
1062 | memcpy (t, &dtd->dtd_data, len); | |
f5e9c9bd | 1063 | copied = (ctf_stype_t *) t; /* name is at the start: constant offset. */ |
676c3ecb NA |
1064 | if (copied->ctt_name |
1065 | && (name = ctf_strraw (fp, copied->ctt_name)) != NULL) | |
1066 | ctf_str_add_ref (fp, name, &copied->ctt_name); | |
47d546f4 NA |
1067 | t += len; |
1068 | ||
1069 | switch (kind) | |
1070 | { | |
1071 | case CTF_K_INTEGER: | |
1072 | case CTF_K_FLOAT: | |
1073 | if (kind == CTF_K_INTEGER) | |
1074 | { | |
1075 | encoding = CTF_INT_DATA (dtd->dtd_u.dtu_enc.cte_format, | |
1076 | dtd->dtd_u.dtu_enc.cte_offset, | |
1077 | dtd->dtd_u.dtu_enc.cte_bits); | |
1078 | } | |
1079 | else | |
1080 | { | |
1081 | encoding = CTF_FP_DATA (dtd->dtd_u.dtu_enc.cte_format, | |
1082 | dtd->dtd_u.dtu_enc.cte_offset, | |
1083 | dtd->dtd_u.dtu_enc.cte_bits); | |
1084 | } | |
1085 | memcpy (t, &encoding, sizeof (encoding)); | |
1086 | t += sizeof (encoding); | |
1087 | break; | |
1088 | ||
1089 | case CTF_K_SLICE: | |
1090 | memcpy (t, &dtd->dtd_u.dtu_slice, sizeof (struct ctf_slice)); | |
1091 | t += sizeof (struct ctf_slice); | |
1092 | break; | |
1093 | ||
1094 | case CTF_K_ARRAY: | |
1095 | cta.cta_contents = (uint32_t) dtd->dtd_u.dtu_arr.ctr_contents; | |
1096 | cta.cta_index = (uint32_t) dtd->dtd_u.dtu_arr.ctr_index; | |
1097 | cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems; | |
1098 | memcpy (t, &cta, sizeof (cta)); | |
1099 | t += sizeof (cta); | |
1100 | break; | |
1101 | ||
1102 | case CTF_K_FUNCTION: | |
1103 | { | |
1104 | uint32_t *argv = (uint32_t *) (uintptr_t) t; | |
1105 | uint32_t argc; | |
1106 | ||
1107 | for (argc = 0; argc < vlen; argc++) | |
afd78bd6 | 1108 | *argv++ = dtd->dtd_u.dtu_argv[argc]; |
47d546f4 NA |
1109 | |
1110 | if (vlen & 1) | |
1111 | *argv++ = 0; /* Pad to 4-byte boundary. */ | |
1112 | ||
1113 | t = (unsigned char *) argv; | |
1114 | break; | |
1115 | } | |
1116 | ||
1117 | case CTF_K_STRUCT: | |
1118 | case CTF_K_UNION: | |
1119 | if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH) | |
f5e9c9bd | 1120 | t = ctf_copy_smembers (fp, dtd, t); |
47d546f4 | 1121 | else |
f5e9c9bd | 1122 | t = ctf_copy_lmembers (fp, dtd, t); |
47d546f4 NA |
1123 | break; |
1124 | ||
1125 | case CTF_K_ENUM: | |
f5e9c9bd | 1126 | t = ctf_copy_emembers (fp, dtd, t); |
47d546f4 NA |
1127 | break; |
1128 | } | |
1129 | } | |
1130 | assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_stroff); | |
1131 | ||
f5e9c9bd NA |
1132 | /* Construct the final string table and fill out all the string refs with the |
1133 | final offsets. Then purge the refs list, because we're about to move this | |
1134 | strtab onto the end of the buf, invalidating all the offsets. */ | |
1135 | strtab = ctf_str_write_strtab (fp); | |
1136 | ctf_str_purge_refs (fp); | |
1137 | ||
d851ecd3 | 1138 | if (strtab.cts_strs == NULL) |
1136c379 | 1139 | goto oom; |
d851ecd3 | 1140 | |
f5e9c9bd NA |
1141 | /* Now the string table is constructed, we can sort the buffer of |
1142 | ctf_varent_t's. */ | |
d851ecd3 | 1143 | ctf_sort_var_arg_cb_t sort_var_arg = { fp, (ctf_strs_t *) &strtab }; |
f5e9c9bd | 1144 | ctf_qsort_r (dvarents, nvars, sizeof (ctf_varent_t), ctf_sort_var, |
d851ecd3 | 1145 | &sort_var_arg); |
f5e9c9bd NA |
1146 | |
1147 | if ((newbuf = ctf_realloc (fp, buf, buf_size + strtab.cts_len)) == NULL) | |
1148 | { | |
de07e349 | 1149 | free (strtab.cts_strs); |
1136c379 | 1150 | goto oom; |
f5e9c9bd NA |
1151 | } |
1152 | buf = newbuf; | |
1153 | memcpy (buf + buf_size, strtab.cts_strs, strtab.cts_len); | |
1154 | hdrp = (ctf_header_t *) buf; | |
1155 | hdrp->cth_strlen = strtab.cts_len; | |
1156 | buf_size += hdrp->cth_strlen; | |
de07e349 | 1157 | free (strtab.cts_strs); |
f5e9c9bd | 1158 | |
139633c3 NA |
1159 | /* Finally, we are ready to ctf_simple_open() the new dict. If this is |
1160 | successful, we then switch nfp and fp and free the old dict. */ | |
47d546f4 | 1161 | |
d851ecd3 NA |
1162 | if ((nfp = ctf_simple_open_internal ((char *) buf, buf_size, NULL, 0, |
1163 | 0, NULL, 0, fp->ctf_syn_ext_strtab, | |
676c3ecb | 1164 | 1, &err)) == NULL) |
47d546f4 | 1165 | { |
de07e349 | 1166 | free (buf); |
47d546f4 NA |
1167 | return (ctf_set_errno (fp, err)); |
1168 | } | |
1169 | ||
1170 | (void) ctf_setmodel (nfp, ctf_getmodel (fp)); | |
47d546f4 | 1171 | |
1fa7a0c2 NA |
1172 | nfp->ctf_parent = fp->ctf_parent; |
1173 | nfp->ctf_parent_unreffed = fp->ctf_parent_unreffed; | |
47d546f4 NA |
1174 | nfp->ctf_refcnt = fp->ctf_refcnt; |
1175 | nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY; | |
fd55eae8 NA |
1176 | if (nfp->ctf_dynbase == NULL) |
1177 | nfp->ctf_dynbase = buf; /* Make sure buf is freed on close. */ | |
47d546f4 NA |
1178 | nfp->ctf_dthash = fp->ctf_dthash; |
1179 | nfp->ctf_dtdefs = fp->ctf_dtdefs; | |
47d546f4 NA |
1180 | nfp->ctf_dvhash = fp->ctf_dvhash; |
1181 | nfp->ctf_dvdefs = fp->ctf_dvdefs; | |
676c3ecb | 1182 | nfp->ctf_dtoldid = fp->ctf_dtoldid; |
99dc3ebd | 1183 | nfp->ctf_add_processing = fp->ctf_add_processing; |
47d546f4 NA |
1184 | nfp->ctf_snapshots = fp->ctf_snapshots + 1; |
1185 | nfp->ctf_specific = fp->ctf_specific; | |
1136c379 NA |
1186 | nfp->ctf_nfuncidx = fp->ctf_nfuncidx; |
1187 | nfp->ctf_nobjtidx = fp->ctf_nobjtidx; | |
1188 | nfp->ctf_objthash = fp->ctf_objthash; | |
1189 | nfp->ctf_funchash = fp->ctf_funchash; | |
1190 | nfp->ctf_dynsyms = fp->ctf_dynsyms; | |
676c3ecb | 1191 | nfp->ctf_ptrtab = fp->ctf_ptrtab; |
abe4ca69 | 1192 | nfp->ctf_pptrtab = fp->ctf_pptrtab; |
1136c379 NA |
1193 | nfp->ctf_dynsymidx = fp->ctf_dynsymidx; |
1194 | nfp->ctf_dynsymmax = fp->ctf_dynsymmax; | |
676c3ecb | 1195 | nfp->ctf_ptrtab_len = fp->ctf_ptrtab_len; |
abe4ca69 | 1196 | nfp->ctf_pptrtab_len = fp->ctf_pptrtab_len; |
72c83edd NA |
1197 | nfp->ctf_link_inputs = fp->ctf_link_inputs; |
1198 | nfp->ctf_link_outputs = fp->ctf_link_outputs; | |
8b37e7b6 | 1199 | nfp->ctf_errs_warnings = fp->ctf_errs_warnings; |
1136c379 NA |
1200 | nfp->ctf_funcidx_names = fp->ctf_funcidx_names; |
1201 | nfp->ctf_objtidx_names = fp->ctf_objtidx_names; | |
1202 | nfp->ctf_funcidx_sxlate = fp->ctf_funcidx_sxlate; | |
1203 | nfp->ctf_objtidx_sxlate = fp->ctf_objtidx_sxlate; | |
676c3ecb | 1204 | nfp->ctf_str_prov_offset = fp->ctf_str_prov_offset; |
d851ecd3 | 1205 | nfp->ctf_syn_ext_strtab = fp->ctf_syn_ext_strtab; |
abe4ca69 | 1206 | nfp->ctf_pptrtab_typemax = fp->ctf_pptrtab_typemax; |
1136c379 | 1207 | nfp->ctf_in_flight_dynsyms = fp->ctf_in_flight_dynsyms; |
5f54462c NA |
1208 | nfp->ctf_link_in_cu_mapping = fp->ctf_link_in_cu_mapping; |
1209 | nfp->ctf_link_out_cu_mapping = fp->ctf_link_out_cu_mapping; | |
886453cb | 1210 | nfp->ctf_link_type_mapping = fp->ctf_link_type_mapping; |
49ea9b45 NA |
1211 | nfp->ctf_link_memb_name_changer = fp->ctf_link_memb_name_changer; |
1212 | nfp->ctf_link_memb_name_changer_arg = fp->ctf_link_memb_name_changer_arg; | |
6dd2819f NA |
1213 | nfp->ctf_link_variable_filter = fp->ctf_link_variable_filter; |
1214 | nfp->ctf_link_variable_filter_arg = fp->ctf_link_variable_filter_arg; | |
53651de8 | 1215 | nfp->ctf_symsect_little_endian = fp->ctf_symsect_little_endian; |
8d2229ad | 1216 | nfp->ctf_link_flags = fp->ctf_link_flags; |
0f0c11f7 NA |
1217 | nfp->ctf_dedup_atoms = fp->ctf_dedup_atoms; |
1218 | nfp->ctf_dedup_atoms_alloc = fp->ctf_dedup_atoms_alloc; | |
1219 | memcpy (&nfp->ctf_dedup, &fp->ctf_dedup, sizeof (fp->ctf_dedup)); | |
47d546f4 NA |
1220 | |
1221 | nfp->ctf_snapshot_lu = fp->ctf_snapshots; | |
1222 | ||
676c3ecb NA |
1223 | memcpy (&nfp->ctf_lookups, fp->ctf_lookups, sizeof (fp->ctf_lookups)); |
1224 | nfp->ctf_structs = fp->ctf_structs; | |
1225 | nfp->ctf_unions = fp->ctf_unions; | |
1226 | nfp->ctf_enums = fp->ctf_enums; | |
1227 | nfp->ctf_names = fp->ctf_names; | |
1228 | ||
47d546f4 | 1229 | fp->ctf_dthash = NULL; |
f5e9c9bd NA |
1230 | ctf_str_free_atoms (nfp); |
1231 | nfp->ctf_str_atoms = fp->ctf_str_atoms; | |
676c3ecb | 1232 | nfp->ctf_prov_strtab = fp->ctf_prov_strtab; |
f5e9c9bd | 1233 | fp->ctf_str_atoms = NULL; |
676c3ecb | 1234 | fp->ctf_prov_strtab = NULL; |
47d546f4 | 1235 | memset (&fp->ctf_dtdefs, 0, sizeof (ctf_list_t)); |
8b37e7b6 | 1236 | memset (&fp->ctf_errs_warnings, 0, sizeof (ctf_list_t)); |
99dc3ebd | 1237 | fp->ctf_add_processing = NULL; |
676c3ecb | 1238 | fp->ctf_ptrtab = NULL; |
abe4ca69 | 1239 | fp->ctf_pptrtab = NULL; |
1136c379 NA |
1240 | fp->ctf_funcidx_names = NULL; |
1241 | fp->ctf_objtidx_names = NULL; | |
1242 | fp->ctf_funcidx_sxlate = NULL; | |
1243 | fp->ctf_objtidx_sxlate = NULL; | |
1244 | fp->ctf_objthash = NULL; | |
1245 | fp->ctf_funchash = NULL; | |
1246 | fp->ctf_dynsyms = NULL; | |
1247 | fp->ctf_dynsymidx = NULL; | |
72c83edd NA |
1248 | fp->ctf_link_inputs = NULL; |
1249 | fp->ctf_link_outputs = NULL; | |
d851ecd3 | 1250 | fp->ctf_syn_ext_strtab = NULL; |
5f54462c NA |
1251 | fp->ctf_link_in_cu_mapping = NULL; |
1252 | fp->ctf_link_out_cu_mapping = NULL; | |
886453cb | 1253 | fp->ctf_link_type_mapping = NULL; |
0f0c11f7 NA |
1254 | fp->ctf_dedup_atoms = NULL; |
1255 | fp->ctf_dedup_atoms_alloc = NULL; | |
1fa7a0c2 | 1256 | fp->ctf_parent_unreffed = 1; |
47d546f4 NA |
1257 | |
1258 | fp->ctf_dvhash = NULL; | |
1259 | memset (&fp->ctf_dvdefs, 0, sizeof (ctf_list_t)); | |
676c3ecb | 1260 | memset (fp->ctf_lookups, 0, sizeof (fp->ctf_lookups)); |
1136c379 | 1261 | memset (&fp->ctf_in_flight_dynsyms, 0, sizeof (fp->ctf_in_flight_dynsyms)); |
0f0c11f7 | 1262 | memset (&fp->ctf_dedup, 0, sizeof (fp->ctf_dedup)); |
676c3ecb NA |
1263 | fp->ctf_structs.ctn_writable = NULL; |
1264 | fp->ctf_unions.ctn_writable = NULL; | |
1265 | fp->ctf_enums.ctn_writable = NULL; | |
1266 | fp->ctf_names.ctn_writable = NULL; | |
47d546f4 | 1267 | |
139633c3 NA |
1268 | memcpy (&ofp, fp, sizeof (ctf_dict_t)); |
1269 | memcpy (fp, nfp, sizeof (ctf_dict_t)); | |
1270 | memcpy (nfp, &ofp, sizeof (ctf_dict_t)); | |
47d546f4 | 1271 | |
1136c379 | 1272 | nfp->ctf_refcnt = 1; /* Force nfp to be freed. */ |
139633c3 | 1273 | ctf_dict_close (nfp); |
47d546f4 NA |
1274 | |
1275 | return 0; | |
1136c379 NA |
1276 | |
1277 | symerr: | |
1278 | ctf_err_warn (fp, 0, err, _("error serializing symtypetabs")); | |
1279 | goto err; | |
1280 | oom: | |
1281 | free (buf); | |
1282 | free (sym_name_order); | |
1283 | return (ctf_set_errno (fp, EAGAIN)); | |
1284 | err: | |
1285 | free (buf); | |
1286 | free (sym_name_order); | |
1287 | return -1; /* errno is set for us. */ | |
47d546f4 NA |
1288 | } |
1289 | ||
676c3ecb | 1290 | ctf_names_t * |
139633c3 | 1291 | ctf_name_table (ctf_dict_t *fp, int kind) |
47d546f4 | 1292 | { |
47d546f4 NA |
1293 | switch (kind) |
1294 | { | |
1295 | case CTF_K_STRUCT: | |
676c3ecb | 1296 | return &fp->ctf_structs; |
47d546f4 | 1297 | case CTF_K_UNION: |
676c3ecb | 1298 | return &fp->ctf_unions; |
47d546f4 | 1299 | case CTF_K_ENUM: |
676c3ecb | 1300 | return &fp->ctf_enums; |
47d546f4 | 1301 | default: |
676c3ecb | 1302 | return &fp->ctf_names; |
47d546f4 | 1303 | } |
47d546f4 NA |
1304 | } |
1305 | ||
24865428 | 1306 | int |
139633c3 | 1307 | ctf_dtd_insert (ctf_dict_t *fp, ctf_dtdef_t *dtd, int flag, int kind) |
47d546f4 | 1308 | { |
676c3ecb | 1309 | const char *name; |
8c419a91 NA |
1310 | if (ctf_dynhash_insert (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type, |
1311 | dtd) < 0) | |
8f235c90 NA |
1312 | { |
1313 | ctf_set_errno (fp, ENOMEM); | |
1314 | return -1; | |
1315 | } | |
24865428 | 1316 | |
fe4c2d55 | 1317 | if (flag == CTF_ADD_ROOT && dtd->dtd_data.ctt_name |
676c3ecb | 1318 | && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL) |
47d546f4 | 1319 | { |
676c3ecb | 1320 | if (ctf_dynhash_insert (ctf_name_table (fp, kind)->ctn_writable, |
8c419a91 NA |
1321 | (char *) name, (void *) (uintptr_t) |
1322 | dtd->dtd_type) < 0) | |
676c3ecb | 1323 | { |
8c419a91 NA |
1324 | ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) |
1325 | dtd->dtd_type); | |
8f235c90 | 1326 | ctf_set_errno (fp, ENOMEM); |
676c3ecb NA |
1327 | return -1; |
1328 | } | |
47d546f4 | 1329 | } |
24865428 NA |
1330 | ctf_list_append (&fp->ctf_dtdefs, dtd); |
1331 | return 0; | |
47d546f4 NA |
1332 | } |
1333 | ||
1334 | void | |
139633c3 | 1335 | ctf_dtd_delete (ctf_dict_t *fp, ctf_dtdef_t *dtd) |
47d546f4 NA |
1336 | { |
1337 | ctf_dmdef_t *dmd, *nmd; | |
1338 | int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); | |
8ffcdf18 | 1339 | int name_kind = kind; |
676c3ecb | 1340 | const char *name; |
47d546f4 | 1341 | |
8c419a91 | 1342 | ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type); |
47d546f4 NA |
1343 | |
1344 | switch (kind) | |
1345 | { | |
1346 | case CTF_K_STRUCT: | |
1347 | case CTF_K_UNION: | |
1348 | case CTF_K_ENUM: | |
1349 | for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members); | |
1350 | dmd != NULL; dmd = nmd) | |
1351 | { | |
1352 | if (dmd->dmd_name != NULL) | |
de07e349 | 1353 | free (dmd->dmd_name); |
47d546f4 | 1354 | nmd = ctf_list_next (dmd); |
de07e349 | 1355 | free (dmd); |
47d546f4 NA |
1356 | } |
1357 | break; | |
1358 | case CTF_K_FUNCTION: | |
de07e349 | 1359 | free (dtd->dtd_u.dtu_argv); |
47d546f4 | 1360 | break; |
8ffcdf18 NA |
1361 | case CTF_K_FORWARD: |
1362 | name_kind = dtd->dtd_data.ctt_type; | |
1363 | break; | |
47d546f4 NA |
1364 | } |
1365 | ||
676c3ecb | 1366 | if (dtd->dtd_data.ctt_name |
fe4c2d55 NA |
1367 | && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL |
1368 | && LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info)) | |
47d546f4 | 1369 | { |
8ffcdf18 | 1370 | ctf_dynhash_remove (ctf_name_table (fp, name_kind)->ctn_writable, |
676c3ecb NA |
1371 | name); |
1372 | ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name); | |
47d546f4 NA |
1373 | } |
1374 | ||
1375 | ctf_list_delete (&fp->ctf_dtdefs, dtd); | |
de07e349 | 1376 | free (dtd); |
47d546f4 NA |
1377 | } |
1378 | ||
1379 | ctf_dtdef_t * | |
139633c3 | 1380 | ctf_dtd_lookup (const ctf_dict_t *fp, ctf_id_t type) |
47d546f4 | 1381 | { |
8c419a91 NA |
1382 | return (ctf_dtdef_t *) |
1383 | ctf_dynhash_lookup (fp->ctf_dthash, (void *) (uintptr_t) type); | |
47d546f4 NA |
1384 | } |
1385 | ||
47d546f4 | 1386 | ctf_dtdef_t * |
139633c3 | 1387 | ctf_dynamic_type (const ctf_dict_t *fp, ctf_id_t id) |
47d546f4 NA |
1388 | { |
1389 | ctf_id_t idx; | |
1390 | ||
676c3ecb NA |
1391 | if (!(fp->ctf_flags & LCTF_RDWR)) |
1392 | return NULL; | |
1393 | ||
47d546f4 NA |
1394 | if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, id)) |
1395 | fp = fp->ctf_parent; | |
1396 | ||
1397 | idx = LCTF_TYPE_TO_INDEX(fp, id); | |
1398 | ||
676c3ecb | 1399 | if ((unsigned long) idx <= fp->ctf_typemax) |
47d546f4 NA |
1400 | return ctf_dtd_lookup (fp, id); |
1401 | return NULL; | |
1402 | } | |
1403 | ||
24865428 | 1404 | int |
139633c3 | 1405 | ctf_dvd_insert (ctf_dict_t *fp, ctf_dvdef_t *dvd) |
47d546f4 | 1406 | { |
24865428 | 1407 | if (ctf_dynhash_insert (fp->ctf_dvhash, dvd->dvd_name, dvd) < 0) |
8f235c90 NA |
1408 | { |
1409 | ctf_set_errno (fp, ENOMEM); | |
1410 | return -1; | |
1411 | } | |
47d546f4 | 1412 | ctf_list_append (&fp->ctf_dvdefs, dvd); |
24865428 | 1413 | return 0; |
47d546f4 NA |
1414 | } |
1415 | ||
1416 | void | |
139633c3 | 1417 | ctf_dvd_delete (ctf_dict_t *fp, ctf_dvdef_t *dvd) |
47d546f4 NA |
1418 | { |
1419 | ctf_dynhash_remove (fp->ctf_dvhash, dvd->dvd_name); | |
de07e349 | 1420 | free (dvd->dvd_name); |
47d546f4 NA |
1421 | |
1422 | ctf_list_delete (&fp->ctf_dvdefs, dvd); | |
de07e349 | 1423 | free (dvd); |
47d546f4 NA |
1424 | } |
1425 | ||
1426 | ctf_dvdef_t * | |
139633c3 | 1427 | ctf_dvd_lookup (const ctf_dict_t *fp, const char *name) |
47d546f4 NA |
1428 | { |
1429 | return (ctf_dvdef_t *) ctf_dynhash_lookup (fp->ctf_dvhash, name); | |
1430 | } | |
1431 | ||
1432 | /* Discard all of the dynamic type definitions and variable definitions that | |
139633c3 NA |
1433 | have been added to the dict since the last call to ctf_update(). We locate |
1434 | such types by scanning the dtd list and deleting elements that have type IDs | |
1435 | greater than ctf_dtoldid, which is set by ctf_update(), above, and by | |
1436 | scanning the variable list and deleting elements that have update IDs equal | |
1437 | to the current value of the last-update snapshot count (indicating that they | |
1438 | were added after the most recent call to ctf_update()). */ | |
47d546f4 | 1439 | int |
139633c3 | 1440 | ctf_discard (ctf_dict_t *fp) |
47d546f4 NA |
1441 | { |
1442 | ctf_snapshot_id_t last_update = | |
1443 | { fp->ctf_dtoldid, | |
1444 | fp->ctf_snapshot_lu + 1 }; | |
1445 | ||
1446 | /* Update required? */ | |
1447 | if (!(fp->ctf_flags & LCTF_DIRTY)) | |
1448 | return 0; | |
1449 | ||
1450 | return (ctf_rollback (fp, last_update)); | |
1451 | } | |
1452 | ||
1453 | ctf_snapshot_id_t | |
139633c3 | 1454 | ctf_snapshot (ctf_dict_t *fp) |
47d546f4 NA |
1455 | { |
1456 | ctf_snapshot_id_t snapid; | |
676c3ecb | 1457 | snapid.dtd_id = fp->ctf_typemax; |
47d546f4 NA |
1458 | snapid.snapshot_id = fp->ctf_snapshots++; |
1459 | return snapid; | |
1460 | } | |
1461 | ||
1462 | /* Like ctf_discard(), only discards everything after a particular ID. */ | |
1463 | int | |
139633c3 | 1464 | ctf_rollback (ctf_dict_t *fp, ctf_snapshot_id_t id) |
47d546f4 NA |
1465 | { |
1466 | ctf_dtdef_t *dtd, *ntd; | |
1467 | ctf_dvdef_t *dvd, *nvd; | |
1468 | ||
1469 | if (!(fp->ctf_flags & LCTF_RDWR)) | |
1470 | return (ctf_set_errno (fp, ECTF_RDONLY)); | |
1471 | ||
47d546f4 NA |
1472 | if (fp->ctf_snapshot_lu >= id.snapshot_id) |
1473 | return (ctf_set_errno (fp, ECTF_OVERROLLBACK)); | |
1474 | ||
1475 | for (dtd = ctf_list_next (&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) | |
1476 | { | |
676c3ecb NA |
1477 | int kind; |
1478 | const char *name; | |
1479 | ||
47d546f4 NA |
1480 | ntd = ctf_list_next (dtd); |
1481 | ||
1482 | if (LCTF_TYPE_TO_INDEX (fp, dtd->dtd_type) <= id.dtd_id) | |
1483 | continue; | |
1484 | ||
676c3ecb | 1485 | kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); |
8ffcdf18 NA |
1486 | if (kind == CTF_K_FORWARD) |
1487 | kind = dtd->dtd_data.ctt_type; | |
676c3ecb NA |
1488 | |
1489 | if (dtd->dtd_data.ctt_name | |
fe4c2d55 NA |
1490 | && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL |
1491 | && LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info)) | |
676c3ecb NA |
1492 | { |
1493 | ctf_dynhash_remove (ctf_name_table (fp, kind)->ctn_writable, | |
1494 | name); | |
1495 | ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name); | |
1496 | } | |
1497 | ||
8c419a91 | 1498 | ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type); |
47d546f4 NA |
1499 | ctf_dtd_delete (fp, dtd); |
1500 | } | |
1501 | ||
1502 | for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd) | |
1503 | { | |
1504 | nvd = ctf_list_next (dvd); | |
1505 | ||
1506 | if (dvd->dvd_snapshots <= id.snapshot_id) | |
1507 | continue; | |
1508 | ||
1509 | ctf_dvd_delete (fp, dvd); | |
1510 | } | |
1511 | ||
676c3ecb | 1512 | fp->ctf_typemax = id.dtd_id; |
47d546f4 NA |
1513 | fp->ctf_snapshots = id.snapshot_id; |
1514 | ||
1515 | if (fp->ctf_snapshots == fp->ctf_snapshot_lu) | |
1516 | fp->ctf_flags &= ~LCTF_DIRTY; | |
1517 | ||
1518 | return 0; | |
1519 | } | |
1520 | ||
1521 | static ctf_id_t | |
139633c3 | 1522 | ctf_add_generic (ctf_dict_t *fp, uint32_t flag, const char *name, int kind, |
47d546f4 NA |
1523 | ctf_dtdef_t **rp) |
1524 | { | |
1525 | ctf_dtdef_t *dtd; | |
1526 | ctf_id_t type; | |
47d546f4 NA |
1527 | |
1528 | if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT) | |
1529 | return (ctf_set_errno (fp, EINVAL)); | |
1530 | ||
1531 | if (!(fp->ctf_flags & LCTF_RDWR)) | |
1532 | return (ctf_set_errno (fp, ECTF_RDONLY)); | |
1533 | ||
676c3ecb | 1534 | if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) >= CTF_MAX_TYPE) |
47d546f4 NA |
1535 | return (ctf_set_errno (fp, ECTF_FULL)); |
1536 | ||
676c3ecb | 1537 | if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) == (CTF_MAX_PTYPE - 1)) |
47d546f4 NA |
1538 | return (ctf_set_errno (fp, ECTF_FULL)); |
1539 | ||
676c3ecb NA |
1540 | /* Make sure ptrtab always grows to be big enough for all types. */ |
1541 | if (ctf_grow_ptrtab (fp) < 0) | |
1542 | return CTF_ERR; /* errno is set for us. */ | |
1543 | ||
de07e349 | 1544 | if ((dtd = malloc (sizeof (ctf_dtdef_t))) == NULL) |
47d546f4 NA |
1545 | return (ctf_set_errno (fp, EAGAIN)); |
1546 | ||
676c3ecb | 1547 | type = ++fp->ctf_typemax; |
47d546f4 NA |
1548 | type = LCTF_INDEX_TO_TYPE (fp, type, (fp->ctf_flags & LCTF_CHILD)); |
1549 | ||
1550 | memset (dtd, 0, sizeof (ctf_dtdef_t)); | |
676c3ecb | 1551 | dtd->dtd_data.ctt_name = ctf_str_add_ref (fp, name, &dtd->dtd_data.ctt_name); |
47d546f4 NA |
1552 | dtd->dtd_type = type; |
1553 | ||
676c3ecb NA |
1554 | if (dtd->dtd_data.ctt_name == 0 && name != NULL && name[0] != '\0') |
1555 | { | |
de07e349 | 1556 | free (dtd); |
676c3ecb NA |
1557 | return (ctf_set_errno (fp, EAGAIN)); |
1558 | } | |
1559 | ||
fe4c2d55 | 1560 | if (ctf_dtd_insert (fp, dtd, flag, kind) < 0) |
24865428 | 1561 | { |
de07e349 | 1562 | free (dtd); |
24865428 NA |
1563 | return CTF_ERR; /* errno is set for us. */ |
1564 | } | |
47d546f4 NA |
1565 | fp->ctf_flags |= LCTF_DIRTY; |
1566 | ||
1567 | *rp = dtd; | |
1568 | return type; | |
1569 | } | |
1570 | ||
1571 | /* When encoding integer sizes, we want to convert a byte count in the range | |
1572 | 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function | |
1573 | is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */ | |
1574 | static size_t | |
1575 | clp2 (size_t x) | |
1576 | { | |
1577 | x--; | |
1578 | ||
1579 | x |= (x >> 1); | |
1580 | x |= (x >> 2); | |
1581 | x |= (x >> 4); | |
1582 | x |= (x >> 8); | |
1583 | x |= (x >> 16); | |
1584 | ||
1585 | return (x + 1); | |
1586 | } | |
1587 | ||
0f0c11f7 | 1588 | ctf_id_t |
139633c3 | 1589 | ctf_add_encoded (ctf_dict_t *fp, uint32_t flag, |
47d546f4 NA |
1590 | const char *name, const ctf_encoding_t *ep, uint32_t kind) |
1591 | { | |
1592 | ctf_dtdef_t *dtd; | |
1593 | ctf_id_t type; | |
1594 | ||
1595 | if (ep == NULL) | |
1596 | return (ctf_set_errno (fp, EINVAL)); | |
1597 | ||
caa17049 NA |
1598 | if (name == NULL || name[0] == '\0') |
1599 | return (ctf_set_errno (fp, ECTF_NONAME)); | |
1600 | ||
676c3ecb | 1601 | if ((type = ctf_add_generic (fp, flag, name, kind, &dtd)) == CTF_ERR) |
47d546f4 NA |
1602 | return CTF_ERR; /* errno is set for us. */ |
1603 | ||
1604 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0); | |
76fad999 TT |
1605 | dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT) |
1606 | / CHAR_BIT); | |
47d546f4 NA |
1607 | dtd->dtd_u.dtu_enc = *ep; |
1608 | ||
1609 | return type; | |
1610 | } | |
1611 | ||
0f0c11f7 | 1612 | ctf_id_t |
139633c3 | 1613 | ctf_add_reftype (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref, uint32_t kind) |
47d546f4 NA |
1614 | { |
1615 | ctf_dtdef_t *dtd; | |
1616 | ctf_id_t type; | |
139633c3 | 1617 | ctf_dict_t *tmp = fp; |
676c3ecb | 1618 | int child = fp->ctf_flags & LCTF_CHILD; |
47d546f4 | 1619 | |
a0486bac | 1620 | if (ref == CTF_ERR || ref > CTF_MAX_TYPE) |
47d546f4 NA |
1621 | return (ctf_set_errno (fp, EINVAL)); |
1622 | ||
2361f1c8 | 1623 | if (ref != 0 && ctf_lookup_by_id (&tmp, ref) == NULL) |
47d546f4 NA |
1624 | return CTF_ERR; /* errno is set for us. */ |
1625 | ||
676c3ecb | 1626 | if ((type = ctf_add_generic (fp, flag, NULL, kind, &dtd)) == CTF_ERR) |
47d546f4 NA |
1627 | return CTF_ERR; /* errno is set for us. */ |
1628 | ||
1629 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0); | |
1630 | dtd->dtd_data.ctt_type = (uint32_t) ref; | |
1631 | ||
676c3ecb NA |
1632 | if (kind != CTF_K_POINTER) |
1633 | return type; | |
1634 | ||
78f28b89 NA |
1635 | /* If we are adding a pointer, update the ptrtab, pointing at this type from |
1636 | the type it points to. Note that ctf_typemax is at this point one higher | |
1637 | than we want to check against, because it's just been incremented for the | |
1638 | addition of this type. The pptrtab is lazily-updated as needed, so is not | |
1639 | touched here. */ | |
676c3ecb NA |
1640 | |
1641 | uint32_t type_idx = LCTF_TYPE_TO_INDEX (fp, type); | |
1642 | uint32_t ref_idx = LCTF_TYPE_TO_INDEX (fp, ref); | |
1643 | ||
1644 | if (LCTF_TYPE_ISCHILD (fp, ref) == child | |
1645 | && ref_idx < fp->ctf_typemax) | |
78f28b89 | 1646 | fp->ctf_ptrtab[ref_idx] = type_idx; |
676c3ecb | 1647 | |
47d546f4 NA |
1648 | return type; |
1649 | } | |
1650 | ||
1651 | ctf_id_t | |
139633c3 | 1652 | ctf_add_slice (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref, |
47d546f4 NA |
1653 | const ctf_encoding_t *ep) |
1654 | { | |
1655 | ctf_dtdef_t *dtd; | |
502e838e | 1656 | ctf_id_t resolved_ref = ref; |
47d546f4 NA |
1657 | ctf_id_t type; |
1658 | int kind; | |
1659 | const ctf_type_t *tp; | |
139633c3 | 1660 | ctf_dict_t *tmp = fp; |
47d546f4 NA |
1661 | |
1662 | if (ep == NULL) | |
1663 | return (ctf_set_errno (fp, EINVAL)); | |
1664 | ||
1665 | if ((ep->cte_bits > 255) || (ep->cte_offset > 255)) | |
1666 | return (ctf_set_errno (fp, ECTF_SLICEOVERFLOW)); | |
1667 | ||
a0486bac | 1668 | if (ref == CTF_ERR || ref > CTF_MAX_TYPE) |
47d546f4 NA |
1669 | return (ctf_set_errno (fp, EINVAL)); |
1670 | ||
2361f1c8 | 1671 | if (ref != 0 && ((tp = ctf_lookup_by_id (&tmp, ref)) == NULL)) |
47d546f4 NA |
1672 | return CTF_ERR; /* errno is set for us. */ |
1673 | ||
502e838e NA |
1674 | /* Make sure we ultimately point to an integral type. We also allow slices to |
1675 | point to the unimplemented type, for now, because the compiler can emit | |
1676 | such slices, though they're not very much use. */ | |
1677 | ||
1678 | resolved_ref = ctf_type_resolve_unsliced (tmp, ref); | |
1679 | kind = ctf_type_kind_unsliced (tmp, resolved_ref); | |
1680 | ||
47d546f4 | 1681 | if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) && |
2361f1c8 NA |
1682 | (kind != CTF_K_ENUM) |
1683 | && (ref != 0)) | |
47d546f4 NA |
1684 | return (ctf_set_errno (fp, ECTF_NOTINTFP)); |
1685 | ||
676c3ecb | 1686 | if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_SLICE, &dtd)) == CTF_ERR) |
47d546f4 NA |
1687 | return CTF_ERR; /* errno is set for us. */ |
1688 | ||
1689 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0); | |
76fad999 TT |
1690 | dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT) |
1691 | / CHAR_BIT); | |
9943fa3a | 1692 | dtd->dtd_u.dtu_slice.cts_type = (uint32_t) ref; |
47d546f4 NA |
1693 | dtd->dtd_u.dtu_slice.cts_bits = ep->cte_bits; |
1694 | dtd->dtd_u.dtu_slice.cts_offset = ep->cte_offset; | |
1695 | ||
1696 | return type; | |
1697 | } | |
1698 | ||
1699 | ctf_id_t | |
139633c3 | 1700 | ctf_add_integer (ctf_dict_t *fp, uint32_t flag, |
47d546f4 NA |
1701 | const char *name, const ctf_encoding_t *ep) |
1702 | { | |
1703 | return (ctf_add_encoded (fp, flag, name, ep, CTF_K_INTEGER)); | |
1704 | } | |
1705 | ||
1706 | ctf_id_t | |
139633c3 | 1707 | ctf_add_float (ctf_dict_t *fp, uint32_t flag, |
47d546f4 NA |
1708 | const char *name, const ctf_encoding_t *ep) |
1709 | { | |
1710 | return (ctf_add_encoded (fp, flag, name, ep, CTF_K_FLOAT)); | |
1711 | } | |
1712 | ||
1713 | ctf_id_t | |
139633c3 | 1714 | ctf_add_pointer (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) |
47d546f4 NA |
1715 | { |
1716 | return (ctf_add_reftype (fp, flag, ref, CTF_K_POINTER)); | |
1717 | } | |
1718 | ||
1719 | ctf_id_t | |
139633c3 | 1720 | ctf_add_array (ctf_dict_t *fp, uint32_t flag, const ctf_arinfo_t *arp) |
47d546f4 NA |
1721 | { |
1722 | ctf_dtdef_t *dtd; | |
1723 | ctf_id_t type; | |
139633c3 | 1724 | ctf_dict_t *tmp = fp; |
47d546f4 NA |
1725 | |
1726 | if (arp == NULL) | |
1727 | return (ctf_set_errno (fp, EINVAL)); | |
1728 | ||
2361f1c8 NA |
1729 | if (arp->ctr_contents != 0 |
1730 | && ctf_lookup_by_id (&tmp, arp->ctr_contents) == NULL) | |
47d546f4 NA |
1731 | return CTF_ERR; /* errno is set for us. */ |
1732 | ||
1733 | tmp = fp; | |
1734 | if (ctf_lookup_by_id (&tmp, arp->ctr_index) == NULL) | |
1735 | return CTF_ERR; /* errno is set for us. */ | |
1736 | ||
ffeece6a NA |
1737 | if (ctf_type_kind (fp, arp->ctr_index) == CTF_K_FORWARD) |
1738 | { | |
1739 | ctf_err_warn (fp, 1, ECTF_INCOMPLETE, | |
1740 | _("ctf_add_array: index type %lx is incomplete"), | |
1741 | arp->ctr_contents); | |
1742 | return (ctf_set_errno (fp, ECTF_INCOMPLETE)); | |
1743 | } | |
1744 | ||
676c3ecb | 1745 | if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_ARRAY, &dtd)) == CTF_ERR) |
47d546f4 NA |
1746 | return CTF_ERR; /* errno is set for us. */ |
1747 | ||
1748 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ARRAY, flag, 0); | |
1749 | dtd->dtd_data.ctt_size = 0; | |
1750 | dtd->dtd_u.dtu_arr = *arp; | |
1751 | ||
1752 | return type; | |
1753 | } | |
1754 | ||
1755 | int | |
139633c3 | 1756 | ctf_set_array (ctf_dict_t *fp, ctf_id_t type, const ctf_arinfo_t *arp) |
47d546f4 NA |
1757 | { |
1758 | ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type); | |
1759 | ||
1760 | if (!(fp->ctf_flags & LCTF_RDWR)) | |
1761 | return (ctf_set_errno (fp, ECTF_RDONLY)); | |
1762 | ||
1763 | if (dtd == NULL | |
1764 | || LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) != CTF_K_ARRAY) | |
1765 | return (ctf_set_errno (fp, ECTF_BADID)); | |
1766 | ||
1767 | fp->ctf_flags |= LCTF_DIRTY; | |
1768 | dtd->dtd_u.dtu_arr = *arp; | |
1769 | ||
1770 | return 0; | |
1771 | } | |
1772 | ||
1773 | ctf_id_t | |
139633c3 | 1774 | ctf_add_function (ctf_dict_t *fp, uint32_t flag, |
47d546f4 NA |
1775 | const ctf_funcinfo_t *ctc, const ctf_id_t *argv) |
1776 | { | |
1777 | ctf_dtdef_t *dtd; | |
1778 | ctf_id_t type; | |
1779 | uint32_t vlen; | |
afd78bd6 | 1780 | uint32_t *vdat = NULL; |
139633c3 | 1781 | ctf_dict_t *tmp = fp; |
47d546f4 NA |
1782 | size_t i; |
1783 | ||
8f235c90 NA |
1784 | if (!(fp->ctf_flags & LCTF_RDWR)) |
1785 | return (ctf_set_errno (fp, ECTF_RDONLY)); | |
1786 | ||
47d546f4 NA |
1787 | if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 |
1788 | || (ctc->ctc_argc != 0 && argv == NULL)) | |
1789 | return (ctf_set_errno (fp, EINVAL)); | |
1790 | ||
1791 | vlen = ctc->ctc_argc; | |
1792 | if (ctc->ctc_flags & CTF_FUNC_VARARG) | |
1793 | vlen++; /* Add trailing zero to indicate varargs (see below). */ | |
1794 | ||
2361f1c8 NA |
1795 | if (ctc->ctc_return != 0 |
1796 | && ctf_lookup_by_id (&tmp, ctc->ctc_return) == NULL) | |
47d546f4 NA |
1797 | return CTF_ERR; /* errno is set for us. */ |
1798 | ||
47d546f4 NA |
1799 | if (vlen > CTF_MAX_VLEN) |
1800 | return (ctf_set_errno (fp, EOVERFLOW)); | |
1801 | ||
de07e349 | 1802 | if (vlen != 0 && (vdat = malloc (sizeof (ctf_id_t) * vlen)) == NULL) |
47d546f4 NA |
1803 | return (ctf_set_errno (fp, EAGAIN)); |
1804 | ||
afd78bd6 NA |
1805 | for (i = 0; i < ctc->ctc_argc; i++) |
1806 | { | |
1807 | tmp = fp; | |
1808 | if (argv[i] != 0 && ctf_lookup_by_id (&tmp, argv[i]) == NULL) | |
1809 | { | |
1810 | free (vdat); | |
1811 | return CTF_ERR; /* errno is set for us. */ | |
1812 | } | |
1813 | vdat[i] = (uint32_t) argv[i]; | |
1814 | } | |
1815 | ||
676c3ecb NA |
1816 | if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_FUNCTION, |
1817 | &dtd)) == CTF_ERR) | |
47d546f4 | 1818 | { |
de07e349 | 1819 | free (vdat); |
47d546f4 NA |
1820 | return CTF_ERR; /* errno is set for us. */ |
1821 | } | |
1822 | ||
1823 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FUNCTION, flag, vlen); | |
1824 | dtd->dtd_data.ctt_type = (uint32_t) ctc->ctc_return; | |
1825 | ||
47d546f4 NA |
1826 | if (ctc->ctc_flags & CTF_FUNC_VARARG) |
1827 | vdat[vlen - 1] = 0; /* Add trailing zero to indicate varargs. */ | |
1828 | dtd->dtd_u.dtu_argv = vdat; | |
1829 | ||
1830 | return type; | |
1831 | } | |
1832 | ||
1833 | ctf_id_t | |
139633c3 | 1834 | ctf_add_struct_sized (ctf_dict_t *fp, uint32_t flag, const char *name, |
47d546f4 NA |
1835 | size_t size) |
1836 | { | |
47d546f4 NA |
1837 | ctf_dtdef_t *dtd; |
1838 | ctf_id_t type = 0; | |
1839 | ||
fe4c2d55 | 1840 | /* Promote root-visible forwards to structs. */ |
47d546f4 | 1841 | if (name != NULL) |
676c3ecb | 1842 | type = ctf_lookup_by_rawname (fp, CTF_K_STRUCT, name); |
47d546f4 NA |
1843 | |
1844 | if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD) | |
1845 | dtd = ctf_dtd_lookup (fp, type); | |
676c3ecb NA |
1846 | else if ((type = ctf_add_generic (fp, flag, name, CTF_K_STRUCT, |
1847 | &dtd)) == CTF_ERR) | |
47d546f4 NA |
1848 | return CTF_ERR; /* errno is set for us. */ |
1849 | ||
1850 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_STRUCT, flag, 0); | |
1851 | ||
1852 | if (size > CTF_MAX_SIZE) | |
1853 | { | |
1854 | dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; | |
1855 | dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size); | |
1856 | dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size); | |
1857 | } | |
1858 | else | |
1859 | dtd->dtd_data.ctt_size = (uint32_t) size; | |
1860 | ||
1861 | return type; | |
1862 | } | |
1863 | ||
1864 | ctf_id_t | |
139633c3 | 1865 | ctf_add_struct (ctf_dict_t *fp, uint32_t flag, const char *name) |
47d546f4 NA |
1866 | { |
1867 | return (ctf_add_struct_sized (fp, flag, name, 0)); | |
1868 | } | |
1869 | ||
1870 | ctf_id_t | |
139633c3 | 1871 | ctf_add_union_sized (ctf_dict_t *fp, uint32_t flag, const char *name, |
47d546f4 NA |
1872 | size_t size) |
1873 | { | |
47d546f4 NA |
1874 | ctf_dtdef_t *dtd; |
1875 | ctf_id_t type = 0; | |
1876 | ||
fe4c2d55 | 1877 | /* Promote root-visible forwards to unions. */ |
47d546f4 | 1878 | if (name != NULL) |
676c3ecb | 1879 | type = ctf_lookup_by_rawname (fp, CTF_K_UNION, name); |
47d546f4 NA |
1880 | |
1881 | if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD) | |
1882 | dtd = ctf_dtd_lookup (fp, type); | |
676c3ecb NA |
1883 | else if ((type = ctf_add_generic (fp, flag, name, CTF_K_UNION, |
1884 | &dtd)) == CTF_ERR) | |
47d546f4 NA |
1885 | return CTF_ERR; /* errno is set for us */ |
1886 | ||
1887 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_UNION, flag, 0); | |
1888 | ||
1889 | if (size > CTF_MAX_SIZE) | |
1890 | { | |
1891 | dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; | |
1892 | dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size); | |
1893 | dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size); | |
1894 | } | |
1895 | else | |
1896 | dtd->dtd_data.ctt_size = (uint32_t) size; | |
1897 | ||
1898 | return type; | |
1899 | } | |
1900 | ||
1901 | ctf_id_t | |
139633c3 | 1902 | ctf_add_union (ctf_dict_t *fp, uint32_t flag, const char *name) |
47d546f4 NA |
1903 | { |
1904 | return (ctf_add_union_sized (fp, flag, name, 0)); | |
1905 | } | |
1906 | ||
1907 | ctf_id_t | |
139633c3 | 1908 | ctf_add_enum (ctf_dict_t *fp, uint32_t flag, const char *name) |
47d546f4 | 1909 | { |
47d546f4 NA |
1910 | ctf_dtdef_t *dtd; |
1911 | ctf_id_t type = 0; | |
1912 | ||
fe4c2d55 | 1913 | /* Promote root-visible forwards to enums. */ |
47d546f4 | 1914 | if (name != NULL) |
676c3ecb | 1915 | type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name); |
47d546f4 NA |
1916 | |
1917 | if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD) | |
1918 | dtd = ctf_dtd_lookup (fp, type); | |
676c3ecb NA |
1919 | else if ((type = ctf_add_generic (fp, flag, name, CTF_K_ENUM, |
1920 | &dtd)) == CTF_ERR) | |
47d546f4 NA |
1921 | return CTF_ERR; /* errno is set for us. */ |
1922 | ||
1923 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ENUM, flag, 0); | |
1924 | dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int; | |
1925 | ||
1926 | return type; | |
1927 | } | |
1928 | ||
1929 | ctf_id_t | |
139633c3 | 1930 | ctf_add_enum_encoded (ctf_dict_t *fp, uint32_t flag, const char *name, |
47d546f4 NA |
1931 | const ctf_encoding_t *ep) |
1932 | { | |
47d546f4 NA |
1933 | ctf_id_t type = 0; |
1934 | ||
1935 | /* First, create the enum if need be, using most of the same machinery as | |
1936 | ctf_add_enum(), to ensure that we do not allow things past that are not | |
1937 | enums or forwards to them. (This includes other slices: you cannot slice a | |
1938 | slice, which would be a useless thing to do anyway.) */ | |
1939 | ||
1940 | if (name != NULL) | |
676c3ecb | 1941 | type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name); |
47d546f4 NA |
1942 | |
1943 | if (type != 0) | |
1944 | { | |
1945 | if ((ctf_type_kind (fp, type) != CTF_K_FORWARD) && | |
1946 | (ctf_type_kind_unsliced (fp, type) != CTF_K_ENUM)) | |
1947 | return (ctf_set_errno (fp, ECTF_NOTINTFP)); | |
1948 | } | |
1949 | else if ((type = ctf_add_enum (fp, flag, name)) == CTF_ERR) | |
1950 | return CTF_ERR; /* errno is set for us. */ | |
1951 | ||
1952 | /* Now attach a suitable slice to it. */ | |
1953 | ||
1954 | return ctf_add_slice (fp, flag, type, ep); | |
1955 | } | |
1956 | ||
1957 | ctf_id_t | |
139633c3 | 1958 | ctf_add_forward (ctf_dict_t *fp, uint32_t flag, const char *name, |
47d546f4 NA |
1959 | uint32_t kind) |
1960 | { | |
47d546f4 NA |
1961 | ctf_dtdef_t *dtd; |
1962 | ctf_id_t type = 0; | |
1963 | ||
9850ce4d | 1964 | if (!ctf_forwardable_kind (kind)) |
676c3ecb | 1965 | return (ctf_set_errno (fp, ECTF_NOTSUE)); |
47d546f4 | 1966 | |
caa17049 NA |
1967 | if (name == NULL || name[0] == '\0') |
1968 | return (ctf_set_errno (fp, ECTF_NONAME)); | |
1969 | ||
47d546f4 NA |
1970 | /* If the type is already defined or exists as a forward tag, just |
1971 | return the ctf_id_t of the existing definition. */ | |
1972 | ||
caa17049 | 1973 | type = ctf_lookup_by_rawname (fp, kind, name); |
47d546f4 | 1974 | |
6bbf9da8 NA |
1975 | if (type) |
1976 | return type; | |
1977 | ||
8ffcdf18 | 1978 | if ((type = ctf_add_generic (fp, flag, name, kind, &dtd)) == CTF_ERR) |
47d546f4 NA |
1979 | return CTF_ERR; /* errno is set for us. */ |
1980 | ||
1981 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FORWARD, flag, 0); | |
1982 | dtd->dtd_data.ctt_type = kind; | |
1983 | ||
1984 | return type; | |
1985 | } | |
1986 | ||
1987 | ctf_id_t | |
139633c3 | 1988 | ctf_add_typedef (ctf_dict_t *fp, uint32_t flag, const char *name, |
47d546f4 NA |
1989 | ctf_id_t ref) |
1990 | { | |
1991 | ctf_dtdef_t *dtd; | |
1992 | ctf_id_t type; | |
139633c3 | 1993 | ctf_dict_t *tmp = fp; |
47d546f4 | 1994 | |
a0486bac | 1995 | if (ref == CTF_ERR || ref > CTF_MAX_TYPE) |
47d546f4 NA |
1996 | return (ctf_set_errno (fp, EINVAL)); |
1997 | ||
caa17049 NA |
1998 | if (name == NULL || name[0] == '\0') |
1999 | return (ctf_set_errno (fp, ECTF_NONAME)); | |
2000 | ||
2361f1c8 | 2001 | if (ref != 0 && ctf_lookup_by_id (&tmp, ref) == NULL) |
47d546f4 NA |
2002 | return CTF_ERR; /* errno is set for us. */ |
2003 | ||
676c3ecb NA |
2004 | if ((type = ctf_add_generic (fp, flag, name, CTF_K_TYPEDEF, |
2005 | &dtd)) == CTF_ERR) | |
47d546f4 NA |
2006 | return CTF_ERR; /* errno is set for us. */ |
2007 | ||
2008 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_TYPEDEF, flag, 0); | |
2009 | dtd->dtd_data.ctt_type = (uint32_t) ref; | |
2010 | ||
2011 | return type; | |
2012 | } | |
2013 | ||
2014 | ctf_id_t | |
139633c3 | 2015 | ctf_add_volatile (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) |
47d546f4 NA |
2016 | { |
2017 | return (ctf_add_reftype (fp, flag, ref, CTF_K_VOLATILE)); | |
2018 | } | |
2019 | ||
2020 | ctf_id_t | |
139633c3 | 2021 | ctf_add_const (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) |
47d546f4 NA |
2022 | { |
2023 | return (ctf_add_reftype (fp, flag, ref, CTF_K_CONST)); | |
2024 | } | |
2025 | ||
2026 | ctf_id_t | |
139633c3 | 2027 | ctf_add_restrict (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) |
47d546f4 NA |
2028 | { |
2029 | return (ctf_add_reftype (fp, flag, ref, CTF_K_RESTRICT)); | |
2030 | } | |
2031 | ||
2032 | int | |
139633c3 | 2033 | ctf_add_enumerator (ctf_dict_t *fp, ctf_id_t enid, const char *name, |
47d546f4 NA |
2034 | int value) |
2035 | { | |
2036 | ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, enid); | |
2037 | ctf_dmdef_t *dmd; | |
2038 | ||
2039 | uint32_t kind, vlen, root; | |
2040 | char *s; | |
2041 | ||
2042 | if (name == NULL) | |
2043 | return (ctf_set_errno (fp, EINVAL)); | |
2044 | ||
2045 | if (!(fp->ctf_flags & LCTF_RDWR)) | |
2046 | return (ctf_set_errno (fp, ECTF_RDONLY)); | |
2047 | ||
2048 | if (dtd == NULL) | |
2049 | return (ctf_set_errno (fp, ECTF_BADID)); | |
2050 | ||
2051 | kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); | |
2052 | root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info); | |
2053 | vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info); | |
2054 | ||
2055 | if (kind != CTF_K_ENUM) | |
2056 | return (ctf_set_errno (fp, ECTF_NOTENUM)); | |
2057 | ||
2058 | if (vlen == CTF_MAX_VLEN) | |
2059 | return (ctf_set_errno (fp, ECTF_DTFULL)); | |
2060 | ||
2061 | for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members); | |
2062 | dmd != NULL; dmd = ctf_list_next (dmd)) | |
2063 | { | |
2064 | if (strcmp (dmd->dmd_name, name) == 0) | |
2065 | return (ctf_set_errno (fp, ECTF_DUPLICATE)); | |
2066 | } | |
2067 | ||
de07e349 | 2068 | if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL) |
47d546f4 NA |
2069 | return (ctf_set_errno (fp, EAGAIN)); |
2070 | ||
de07e349 | 2071 | if ((s = strdup (name)) == NULL) |
47d546f4 | 2072 | { |
de07e349 | 2073 | free (dmd); |
47d546f4 NA |
2074 | return (ctf_set_errno (fp, EAGAIN)); |
2075 | } | |
2076 | ||
2077 | dmd->dmd_name = s; | |
2078 | dmd->dmd_type = CTF_ERR; | |
2079 | dmd->dmd_offset = 0; | |
2080 | dmd->dmd_value = value; | |
2081 | ||
2082 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1); | |
2083 | ctf_list_append (&dtd->dtd_u.dtu_members, dmd); | |
2084 | ||
47d546f4 NA |
2085 | fp->ctf_flags |= LCTF_DIRTY; |
2086 | ||
2087 | return 0; | |
2088 | } | |
2089 | ||
2090 | int | |
139633c3 | 2091 | ctf_add_member_offset (ctf_dict_t *fp, ctf_id_t souid, const char *name, |
47d546f4 NA |
2092 | ctf_id_t type, unsigned long bit_offset) |
2093 | { | |
2094 | ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, souid); | |
2095 | ctf_dmdef_t *dmd; | |
2096 | ||
2097 | ssize_t msize, malign, ssize; | |
2098 | uint32_t kind, vlen, root; | |
2099 | char *s = NULL; | |
ffeece6a | 2100 | int is_incomplete = 0; |
47d546f4 NA |
2101 | |
2102 | if (!(fp->ctf_flags & LCTF_RDWR)) | |
2103 | return (ctf_set_errno (fp, ECTF_RDONLY)); | |
2104 | ||
2105 | if (dtd == NULL) | |
2106 | return (ctf_set_errno (fp, ECTF_BADID)); | |
2107 | ||
ab769488 NA |
2108 | if (name != NULL && name[0] == '\0') |
2109 | name = NULL; | |
2110 | ||
47d546f4 NA |
2111 | kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); |
2112 | root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info); | |
2113 | vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info); | |
2114 | ||
2115 | if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) | |
2116 | return (ctf_set_errno (fp, ECTF_NOTSOU)); | |
2117 | ||
2118 | if (vlen == CTF_MAX_VLEN) | |
2119 | return (ctf_set_errno (fp, ECTF_DTFULL)); | |
2120 | ||
2121 | if (name != NULL) | |
2122 | { | |
2123 | for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members); | |
2124 | dmd != NULL; dmd = ctf_list_next (dmd)) | |
2125 | { | |
2126 | if (dmd->dmd_name != NULL && strcmp (dmd->dmd_name, name) == 0) | |
2127 | return (ctf_set_errno (fp, ECTF_DUPLICATE)); | |
2128 | } | |
2129 | } | |
2130 | ||
a0486bac JM |
2131 | if ((msize = ctf_type_size (fp, type)) < 0 || |
2132 | (malign = ctf_type_align (fp, type)) < 0) | |
2361f1c8 NA |
2133 | { |
2134 | /* The unimplemented type, and any type that resolves to it, has no size | |
2135 | and no alignment: it can correspond to any number of compiler-inserted | |
ffeece6a NA |
2136 | types. We allow incomplete types through since they are routinely |
2137 | added to the ends of structures, and can even be added elsewhere in | |
2138 | structures by the deduplicator. They are assumed to be zero-size with | |
2139 | no alignment: this is often wrong, but problems can be avoided in this | |
2140 | case by explicitly specifying the size of the structure via the _sized | |
2141 | functions. The deduplicator always does this. */ | |
2142 | ||
2143 | msize = 0; | |
2144 | malign = 0; | |
2361f1c8 | 2145 | if (ctf_errno (fp) == ECTF_NONREPRESENTABLE) |
ffeece6a NA |
2146 | ctf_set_errno (fp, 0); |
2147 | else if (ctf_errno (fp) == ECTF_INCOMPLETE) | |
2148 | is_incomplete = 1; | |
2361f1c8 NA |
2149 | else |
2150 | return -1; /* errno is set for us. */ | |
2151 | } | |
47d546f4 | 2152 | |
de07e349 | 2153 | if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL) |
47d546f4 NA |
2154 | return (ctf_set_errno (fp, EAGAIN)); |
2155 | ||
de07e349 | 2156 | if (name != NULL && (s = strdup (name)) == NULL) |
47d546f4 | 2157 | { |
de07e349 | 2158 | free (dmd); |
47d546f4 NA |
2159 | return (ctf_set_errno (fp, EAGAIN)); |
2160 | } | |
2161 | ||
2162 | dmd->dmd_name = s; | |
2163 | dmd->dmd_type = type; | |
2164 | dmd->dmd_value = -1; | |
2165 | ||
2166 | if (kind == CTF_K_STRUCT && vlen != 0) | |
2167 | { | |
2168 | if (bit_offset == (unsigned long) - 1) | |
2169 | { | |
2170 | /* Natural alignment. */ | |
2171 | ||
2172 | ctf_dmdef_t *lmd = ctf_list_prev (&dtd->dtd_u.dtu_members); | |
2173 | ctf_id_t ltype = ctf_type_resolve (fp, lmd->dmd_type); | |
2174 | size_t off = lmd->dmd_offset; | |
2175 | ||
2176 | ctf_encoding_t linfo; | |
2177 | ssize_t lsize; | |
2178 | ||
2361f1c8 NA |
2179 | /* Propagate any error from ctf_type_resolve. If the last member was |
2180 | of unimplemented type, this may be -ECTF_NONREPRESENTABLE: we | |
2181 | cannot insert right after such a member without explicit offset | |
2182 | specification, because its alignment and size is not known. */ | |
2183 | if (ltype == CTF_ERR) | |
2184 | { | |
2185 | free (dmd); | |
2186 | return -1; /* errno is set for us. */ | |
2187 | } | |
2188 | ||
ffeece6a NA |
2189 | if (is_incomplete) |
2190 | { | |
2191 | ctf_err_warn (fp, 1, ECTF_INCOMPLETE, | |
2192 | _("ctf_add_member_offset: cannot add member %s of " | |
2193 | "incomplete type %lx to struct %lx without " | |
2194 | "specifying explicit offset\n"), | |
2195 | name ? name : _("(unnamed member)"), type, souid); | |
2196 | return (ctf_set_errno (fp, ECTF_INCOMPLETE)); | |
2197 | } | |
2198 | ||
a0486bac | 2199 | if (ctf_type_encoding (fp, ltype, &linfo) == 0) |
47d546f4 | 2200 | off += linfo.cte_bits; |
a0486bac | 2201 | else if ((lsize = ctf_type_size (fp, ltype)) > 0) |
76fad999 | 2202 | off += lsize * CHAR_BIT; |
ffeece6a NA |
2203 | else if (lsize == -1 && ctf_errno (fp) == ECTF_INCOMPLETE) |
2204 | { | |
2205 | ctf_err_warn (fp, 1, ECTF_INCOMPLETE, | |
2206 | _("ctf_add_member_offset: cannot add member %s of " | |
2207 | "type %lx to struct %lx without specifying " | |
2208 | "explicit offset after member %s of type %lx, " | |
2209 | "which is an incomplete type\n"), | |
2210 | name ? name : _("(unnamed member)"), type, souid, | |
2211 | lmd->dmd_name ? lmd->dmd_name | |
2212 | : _("(unnamed member)"), ltype); | |
2213 | return -1; /* errno is set for us. */ | |
2214 | } | |
47d546f4 NA |
2215 | |
2216 | /* Round up the offset of the end of the last member to | |
2217 | the next byte boundary, convert 'off' to bytes, and | |
2218 | then round it up again to the next multiple of the | |
2219 | alignment required by the new member. Finally, | |
2220 | convert back to bits and store the result in | |
2221 | dmd_offset. Technically we could do more efficient | |
2222 | packing if the new member is a bit-field, but we're | |
2223 | the "compiler" and ANSI says we can do as we choose. */ | |
2224 | ||
76fad999 | 2225 | off = roundup (off, CHAR_BIT) / CHAR_BIT; |
47d546f4 | 2226 | off = roundup (off, MAX (malign, 1)); |
76fad999 | 2227 | dmd->dmd_offset = off * CHAR_BIT; |
47d546f4 NA |
2228 | ssize = off + msize; |
2229 | } | |
2230 | else | |
2231 | { | |
2232 | /* Specified offset in bits. */ | |
2233 | ||
2234 | dmd->dmd_offset = bit_offset; | |
2235 | ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL); | |
76fad999 | 2236 | ssize = MAX (ssize, ((signed) bit_offset / CHAR_BIT) + msize); |
47d546f4 NA |
2237 | } |
2238 | } | |
2239 | else | |
2240 | { | |
2241 | dmd->dmd_offset = 0; | |
2242 | ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL); | |
2243 | ssize = MAX (ssize, msize); | |
2244 | } | |
2245 | ||
a0486bac | 2246 | if ((size_t) ssize > CTF_MAX_SIZE) |
47d546f4 NA |
2247 | { |
2248 | dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; | |
2249 | dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (ssize); | |
2250 | dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (ssize); | |
2251 | } | |
2252 | else | |
2253 | dtd->dtd_data.ctt_size = (uint32_t) ssize; | |
2254 | ||
2255 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1); | |
2256 | ctf_list_append (&dtd->dtd_u.dtu_members, dmd); | |
2257 | ||
47d546f4 NA |
2258 | fp->ctf_flags |= LCTF_DIRTY; |
2259 | return 0; | |
2260 | } | |
2261 | ||
2262 | int | |
139633c3 | 2263 | ctf_add_member_encoded (ctf_dict_t *fp, ctf_id_t souid, const char *name, |
47d546f4 NA |
2264 | ctf_id_t type, unsigned long bit_offset, |
2265 | const ctf_encoding_t encoding) | |
2266 | { | |
2267 | ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type); | |
2268 | int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); | |
2269 | int otype = type; | |
2270 | ||
2271 | if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) && (kind != CTF_K_ENUM)) | |
2272 | return (ctf_set_errno (fp, ECTF_NOTINTFP)); | |
2273 | ||
2274 | if ((type = ctf_add_slice (fp, CTF_ADD_NONROOT, otype, &encoding)) == CTF_ERR) | |
a0486bac | 2275 | return -1; /* errno is set for us. */ |
47d546f4 NA |
2276 | |
2277 | return ctf_add_member_offset (fp, souid, name, type, bit_offset); | |
2278 | } | |
2279 | ||
2280 | int | |
139633c3 | 2281 | ctf_add_member (ctf_dict_t *fp, ctf_id_t souid, const char *name, |
47d546f4 NA |
2282 | ctf_id_t type) |
2283 | { | |
2284 | return ctf_add_member_offset (fp, souid, name, type, (unsigned long) - 1); | |
2285 | } | |
2286 | ||
2287 | int | |
139633c3 | 2288 | ctf_add_variable (ctf_dict_t *fp, const char *name, ctf_id_t ref) |
47d546f4 NA |
2289 | { |
2290 | ctf_dvdef_t *dvd; | |
139633c3 | 2291 | ctf_dict_t *tmp = fp; |
47d546f4 NA |
2292 | |
2293 | if (!(fp->ctf_flags & LCTF_RDWR)) | |
2294 | return (ctf_set_errno (fp, ECTF_RDONLY)); | |
2295 | ||
2296 | if (ctf_dvd_lookup (fp, name) != NULL) | |
2297 | return (ctf_set_errno (fp, ECTF_DUPLICATE)); | |
2298 | ||
2299 | if (ctf_lookup_by_id (&tmp, ref) == NULL) | |
a0486bac | 2300 | return -1; /* errno is set for us. */ |
47d546f4 | 2301 | |
791915db NA |
2302 | /* Make sure this type is representable. */ |
2303 | if ((ctf_type_resolve (fp, ref) == CTF_ERR) | |
2304 | && (ctf_errno (fp) == ECTF_NONREPRESENTABLE)) | |
2305 | return -1; | |
2306 | ||
de07e349 | 2307 | if ((dvd = malloc (sizeof (ctf_dvdef_t))) == NULL) |
47d546f4 NA |
2308 | return (ctf_set_errno (fp, EAGAIN)); |
2309 | ||
de07e349 | 2310 | if (name != NULL && (dvd->dvd_name = strdup (name)) == NULL) |
47d546f4 | 2311 | { |
de07e349 | 2312 | free (dvd); |
47d546f4 NA |
2313 | return (ctf_set_errno (fp, EAGAIN)); |
2314 | } | |
2315 | dvd->dvd_type = ref; | |
2316 | dvd->dvd_snapshots = fp->ctf_snapshots; | |
2317 | ||
24865428 NA |
2318 | if (ctf_dvd_insert (fp, dvd) < 0) |
2319 | { | |
de07e349 NA |
2320 | free (dvd->dvd_name); |
2321 | free (dvd); | |
24865428 NA |
2322 | return -1; /* errno is set for us. */ |
2323 | } | |
47d546f4 | 2324 | |
47d546f4 NA |
2325 | fp->ctf_flags |= LCTF_DIRTY; |
2326 | return 0; | |
2327 | } | |
2328 | ||
1136c379 NA |
2329 | int |
2330 | ctf_add_funcobjt_sym (ctf_dict_t *fp, int is_function, const char *name, ctf_id_t id) | |
2331 | { | |
2332 | ctf_dict_t *tmp = fp; | |
2333 | char *dupname; | |
2334 | ctf_dynhash_t *h = is_function ? fp->ctf_funchash : fp->ctf_objthash; | |
2335 | ||
2336 | if (!(fp->ctf_flags & LCTF_RDWR)) | |
2337 | return (ctf_set_errno (fp, ECTF_RDONLY)); | |
2338 | ||
2339 | if (ctf_dynhash_lookup (fp->ctf_objthash, name) != NULL || | |
2340 | ctf_dynhash_lookup (fp->ctf_funchash, name) != NULL) | |
2341 | return (ctf_set_errno (fp, ECTF_DUPLICATE)); | |
2342 | ||
2343 | if (ctf_lookup_by_id (&tmp, id) == NULL) | |
2344 | return -1; /* errno is set for us. */ | |
2345 | ||
2346 | if (is_function && ctf_type_kind (fp, id) != CTF_K_FUNCTION) | |
2347 | return (ctf_set_errno (fp, ECTF_NOTFUNC)); | |
2348 | ||
2349 | if ((dupname = strdup (name)) == NULL) | |
2350 | return (ctf_set_errno (fp, ENOMEM)); | |
2351 | ||
2352 | if (ctf_dynhash_insert (h, dupname, (void *) (uintptr_t) id) < 0) | |
2353 | { | |
2354 | free (dupname); | |
2355 | return (ctf_set_errno (fp, ENOMEM)); | |
2356 | } | |
2357 | return 0; | |
2358 | } | |
2359 | ||
2360 | int | |
2361 | ctf_add_objt_sym (ctf_dict_t *fp, const char *name, ctf_id_t id) | |
2362 | { | |
2363 | return (ctf_add_funcobjt_sym (fp, 0, name, id)); | |
2364 | } | |
2365 | ||
2366 | int | |
2367 | ctf_add_func_sym (ctf_dict_t *fp, const char *name, ctf_id_t id) | |
2368 | { | |
2369 | return (ctf_add_funcobjt_sym (fp, 1, name, id)); | |
2370 | } | |
2371 | ||
926c9e76 NA |
2372 | typedef struct ctf_bundle |
2373 | { | |
139633c3 | 2374 | ctf_dict_t *ctb_dict; /* CTF dict handle. */ |
926c9e76 NA |
2375 | ctf_id_t ctb_type; /* CTF type identifier. */ |
2376 | ctf_dtdef_t *ctb_dtd; /* CTF dynamic type definition (if any). */ | |
2377 | } ctf_bundle_t; | |
2378 | ||
c499eb68 NA |
2379 | static int |
2380 | enumcmp (const char *name, int value, void *arg) | |
2381 | { | |
2382 | ctf_bundle_t *ctb = arg; | |
2383 | int bvalue; | |
2384 | ||
139633c3 | 2385 | if (ctf_enum_value (ctb->ctb_dict, ctb->ctb_type, name, &bvalue) < 0) |
c499eb68 | 2386 | { |
139633c3 | 2387 | ctf_err_warn (ctb->ctb_dict, 0, 0, |
926c9e76 | 2388 | _("conflict due to enum %s iteration error"), name); |
c499eb68 NA |
2389 | return 1; |
2390 | } | |
2391 | if (value != bvalue) | |
2392 | { | |
139633c3 | 2393 | ctf_err_warn (ctb->ctb_dict, 1, ECTF_CONFLICT, |
926c9e76 NA |
2394 | _("conflict due to enum value change: %i versus %i"), |
2395 | value, bvalue); | |
c499eb68 NA |
2396 | return 1; |
2397 | } | |
2398 | return 0; | |
2399 | } | |
2400 | ||
2401 | static int | |
2402 | enumadd (const char *name, int value, void *arg) | |
2403 | { | |
2404 | ctf_bundle_t *ctb = arg; | |
2405 | ||
139633c3 | 2406 | return (ctf_add_enumerator (ctb->ctb_dict, ctb->ctb_type, |
a0486bac | 2407 | name, value) < 0); |
c499eb68 NA |
2408 | } |
2409 | ||
2410 | static int | |
2411 | membcmp (const char *name, ctf_id_t type _libctf_unused_, unsigned long offset, | |
2412 | void *arg) | |
2413 | { | |
2414 | ctf_bundle_t *ctb = arg; | |
2415 | ctf_membinfo_t ctm; | |
2416 | ||
f47ca311 NA |
2417 | /* Don't check nameless members (e.g. anonymous structs/unions) against each |
2418 | other. */ | |
2419 | if (name[0] == 0) | |
2420 | return 0; | |
2421 | ||
139633c3 | 2422 | if (ctf_member_info (ctb->ctb_dict, ctb->ctb_type, name, &ctm) < 0) |
c499eb68 | 2423 | { |
139633c3 | 2424 | ctf_err_warn (ctb->ctb_dict, 0, 0, |
926c9e76 NA |
2425 | _("conflict due to struct member %s iteration error"), |
2426 | name); | |
c499eb68 NA |
2427 | return 1; |
2428 | } | |
2429 | if (ctm.ctm_offset != offset) | |
2430 | { | |
139633c3 | 2431 | ctf_err_warn (ctb->ctb_dict, 1, ECTF_CONFLICT, |
926c9e76 NA |
2432 | _("conflict due to struct member %s offset change: " |
2433 | "%lx versus %lx"), | |
2434 | name, ctm.ctm_offset, offset); | |
c499eb68 NA |
2435 | return 1; |
2436 | } | |
2437 | return 0; | |
2438 | } | |
2439 | ||
2440 | static int | |
2441 | membadd (const char *name, ctf_id_t type, unsigned long offset, void *arg) | |
2442 | { | |
2443 | ctf_bundle_t *ctb = arg; | |
2444 | ctf_dmdef_t *dmd; | |
2445 | char *s = NULL; | |
2446 | ||
de07e349 | 2447 | if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL) |
139633c3 | 2448 | return (ctf_set_errno (ctb->ctb_dict, EAGAIN)); |
c499eb68 | 2449 | |
26503e2f NA |
2450 | /* Unnamed members in non-dynamic dicts have a name of "", while dynamic dicts |
2451 | use NULL. Adapt. */ | |
2452 | ||
2453 | if (name[0] == 0) | |
2454 | name = NULL; | |
2455 | ||
de07e349 | 2456 | if (name != NULL && (s = strdup (name)) == NULL) |
c499eb68 | 2457 | { |
de07e349 | 2458 | free (dmd); |
139633c3 | 2459 | return (ctf_set_errno (ctb->ctb_dict, EAGAIN)); |
c499eb68 NA |
2460 | } |
2461 | ||
2462 | /* For now, dmd_type is copied as the src_fp's type; it is reset to an | |
2463 | equivalent dst_fp type by a final loop in ctf_add_type(), below. */ | |
2464 | dmd->dmd_name = s; | |
2465 | dmd->dmd_type = type; | |
2466 | dmd->dmd_offset = offset; | |
2467 | dmd->dmd_value = -1; | |
2468 | ||
2469 | ctf_list_append (&ctb->ctb_dtd->dtd_u.dtu_members, dmd); | |
2470 | ||
139633c3 | 2471 | ctb->ctb_dict->ctf_flags |= LCTF_DIRTY; |
c499eb68 NA |
2472 | return 0; |
2473 | } | |
2474 | ||
139633c3 NA |
2475 | /* The ctf_add_type routine is used to copy a type from a source CTF dictionary |
2476 | to a dynamic destination dictionary. This routine operates recursively by | |
c499eb68 | 2477 | following the source type's links and embedded member types. If the |
139633c3 NA |
2478 | destination dict already contains a named type which has the same attributes, |
2479 | then we succeed and return this type but no changes occur. */ | |
99dc3ebd | 2480 | static ctf_id_t |
139633c3 NA |
2481 | ctf_add_type_internal (ctf_dict_t *dst_fp, ctf_dict_t *src_fp, ctf_id_t src_type, |
2482 | ctf_dict_t *proc_tracking_fp) | |
c499eb68 NA |
2483 | { |
2484 | ctf_id_t dst_type = CTF_ERR; | |
2485 | uint32_t dst_kind = CTF_K_UNKNOWN; | |
139633c3 | 2486 | ctf_dict_t *tmp_fp = dst_fp; |
c499eb68 NA |
2487 | ctf_id_t tmp; |
2488 | ||
2489 | const char *name; | |
5de9eada | 2490 | uint32_t kind, forward_kind, flag, vlen; |
c499eb68 NA |
2491 | |
2492 | const ctf_type_t *src_tp, *dst_tp; | |
2493 | ctf_bundle_t src, dst; | |
2494 | ctf_encoding_t src_en, dst_en; | |
2495 | ctf_arinfo_t src_ar, dst_ar; | |
2496 | ||
c499eb68 | 2497 | ctf_funcinfo_t ctc; |
c499eb68 | 2498 | |
886453cb | 2499 | ctf_id_t orig_src_type = src_type; |
c499eb68 NA |
2500 | |
2501 | if (!(dst_fp->ctf_flags & LCTF_RDWR)) | |
2502 | return (ctf_set_errno (dst_fp, ECTF_RDONLY)); | |
2503 | ||
2504 | if ((src_tp = ctf_lookup_by_id (&src_fp, src_type)) == NULL) | |
2505 | return (ctf_set_errno (dst_fp, ctf_errno (src_fp))); | |
2506 | ||
791915db NA |
2507 | if ((ctf_type_resolve (src_fp, src_type) == CTF_ERR) |
2508 | && (ctf_errno (src_fp) == ECTF_NONREPRESENTABLE)) | |
2509 | return (ctf_set_errno (dst_fp, ECTF_NONREPRESENTABLE)); | |
2510 | ||
c499eb68 NA |
2511 | name = ctf_strptr (src_fp, src_tp->ctt_name); |
2512 | kind = LCTF_INFO_KIND (src_fp, src_tp->ctt_info); | |
2513 | flag = LCTF_INFO_ISROOT (src_fp, src_tp->ctt_info); | |
2514 | vlen = LCTF_INFO_VLEN (src_fp, src_tp->ctt_info); | |
2515 | ||
99dc3ebd NA |
2516 | /* If this is a type we are currently in the middle of adding, hand it |
2517 | straight back. (This lets us handle self-referential structures without | |
2518 | considering forwards and empty structures the same as their completed | |
2519 | forms.) */ | |
2520 | ||
2521 | tmp = ctf_type_mapping (src_fp, src_type, &tmp_fp); | |
2522 | ||
2523 | if (tmp != 0) | |
2524 | { | |
2525 | if (ctf_dynhash_lookup (proc_tracking_fp->ctf_add_processing, | |
2526 | (void *) (uintptr_t) src_type)) | |
2527 | return tmp; | |
2528 | ||
139633c3 NA |
2529 | /* If this type has already been added from this dictionary, and is the |
2530 | same kind and (if a struct or union) has the same number of members, | |
2531 | hand it straight back. */ | |
99dc3ebd | 2532 | |
d04a47ac | 2533 | if (ctf_type_kind_unsliced (tmp_fp, tmp) == (int) kind) |
99dc3ebd | 2534 | { |
d04a47ac NA |
2535 | if (kind == CTF_K_STRUCT || kind == CTF_K_UNION |
2536 | || kind == CTF_K_ENUM) | |
2537 | { | |
2538 | if ((dst_tp = ctf_lookup_by_id (&tmp_fp, dst_type)) != NULL) | |
2539 | if (vlen == LCTF_INFO_VLEN (tmp_fp, dst_tp->ctt_info)) | |
2540 | return tmp; | |
2541 | } | |
2542 | else | |
2543 | return tmp; | |
99dc3ebd NA |
2544 | } |
2545 | } | |
2546 | ||
5de9eada NA |
2547 | forward_kind = kind; |
2548 | if (kind == CTF_K_FORWARD) | |
2549 | forward_kind = src_tp->ctt_type; | |
2550 | ||
139633c3 NA |
2551 | /* If the source type has a name and is a root type (visible at the top-level |
2552 | scope), lookup the name in the destination dictionary and verify that it is | |
2553 | of the same kind before we do anything else. */ | |
c499eb68 NA |
2554 | |
2555 | if ((flag & CTF_ADD_ROOT) && name[0] != '\0' | |
676c3ecb | 2556 | && (tmp = ctf_lookup_by_rawname (dst_fp, forward_kind, name)) != 0) |
c499eb68 NA |
2557 | { |
2558 | dst_type = tmp; | |
2559 | dst_kind = ctf_type_kind_unsliced (dst_fp, dst_type); | |
2560 | } | |
2561 | ||
2562 | /* If an identically named dst_type exists, fail with ECTF_CONFLICT | |
2563 | unless dst_type is a forward declaration and src_type is a struct, | |
5de9eada | 2564 | union, or enum (i.e. the definition of the previous forward decl). |
c499eb68 | 2565 | |
5de9eada NA |
2566 | We also allow addition in the opposite order (addition of a forward when a |
2567 | struct, union, or enum already exists), which is a NOP and returns the | |
2568 | already-present struct, union, or enum. */ | |
2569 | ||
2570 | if (dst_type != CTF_ERR && dst_kind != kind) | |
c499eb68 | 2571 | { |
5de9eada NA |
2572 | if (kind == CTF_K_FORWARD |
2573 | && (dst_kind == CTF_K_ENUM || dst_kind == CTF_K_STRUCT | |
2574 | || dst_kind == CTF_K_UNION)) | |
2575 | { | |
2576 | ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type); | |
2577 | return dst_type; | |
2578 | } | |
2579 | ||
2580 | if (dst_kind != CTF_K_FORWARD | |
2581 | || (kind != CTF_K_ENUM && kind != CTF_K_STRUCT | |
2582 | && kind != CTF_K_UNION)) | |
2583 | { | |
926c9e76 | 2584 | ctf_err_warn (dst_fp, 1, ECTF_CONFLICT, |
139633c3 | 2585 | _("ctf_add_type: conflict for type %s: " |
926c9e76 NA |
2586 | "kinds differ, new: %i; old (ID %lx): %i"), |
2587 | name, kind, dst_type, dst_kind); | |
5de9eada NA |
2588 | return (ctf_set_errno (dst_fp, ECTF_CONFLICT)); |
2589 | } | |
c499eb68 NA |
2590 | } |
2591 | ||
2592 | /* We take special action for an integer, float, or slice since it is | |
2593 | described not only by its name but also its encoding. For integers, | |
2594 | bit-fields exploit this degeneracy. */ | |
2595 | ||
2596 | if (kind == CTF_K_INTEGER || kind == CTF_K_FLOAT || kind == CTF_K_SLICE) | |
2597 | { | |
2598 | if (ctf_type_encoding (src_fp, src_type, &src_en) != 0) | |
2599 | return (ctf_set_errno (dst_fp, ctf_errno (src_fp))); | |
2600 | ||
2601 | if (dst_type != CTF_ERR) | |
2602 | { | |
139633c3 | 2603 | ctf_dict_t *fp = dst_fp; |
c499eb68 NA |
2604 | |
2605 | if ((dst_tp = ctf_lookup_by_id (&fp, dst_type)) == NULL) | |
2606 | return CTF_ERR; | |
2607 | ||
99dc3ebd NA |
2608 | if (ctf_type_encoding (dst_fp, dst_type, &dst_en) != 0) |
2609 | return CTF_ERR; /* errno set for us. */ | |
2610 | ||
c499eb68 NA |
2611 | if (LCTF_INFO_ISROOT (fp, dst_tp->ctt_info) & CTF_ADD_ROOT) |
2612 | { | |
2613 | /* The type that we found in the hash is also root-visible. If | |
2614 | the two types match then use the existing one; otherwise, | |
2615 | declare a conflict. Note: slices are not certain to match | |
2616 | even if there is no conflict: we must check the contained type | |
2617 | too. */ | |
2618 | ||
c499eb68 NA |
2619 | if (memcmp (&src_en, &dst_en, sizeof (ctf_encoding_t)) == 0) |
2620 | { | |
2621 | if (kind != CTF_K_SLICE) | |
886453cb NA |
2622 | { |
2623 | ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type); | |
2624 | return dst_type; | |
2625 | } | |
c499eb68 NA |
2626 | } |
2627 | else | |
2628 | { | |
2629 | return (ctf_set_errno (dst_fp, ECTF_CONFLICT)); | |
2630 | } | |
2631 | } | |
2632 | else | |
2633 | { | |
99dc3ebd NA |
2634 | /* We found a non-root-visible type in the hash. If its encoding |
2635 | is the same, we can reuse it, unless it is a slice. */ | |
c499eb68 | 2636 | |
99dc3ebd | 2637 | if (memcmp (&src_en, &dst_en, sizeof (ctf_encoding_t)) == 0) |
c499eb68 NA |
2638 | { |
2639 | if (kind != CTF_K_SLICE) | |
886453cb | 2640 | { |
99dc3ebd NA |
2641 | ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type); |
2642 | return dst_type; | |
886453cb | 2643 | } |
c499eb68 | 2644 | } |
c499eb68 NA |
2645 | } |
2646 | } | |
2647 | } | |
2648 | ||
139633c3 | 2649 | src.ctb_dict = src_fp; |
c499eb68 NA |
2650 | src.ctb_type = src_type; |
2651 | src.ctb_dtd = NULL; | |
2652 | ||
139633c3 | 2653 | dst.ctb_dict = dst_fp; |
c499eb68 NA |
2654 | dst.ctb_type = dst_type; |
2655 | dst.ctb_dtd = NULL; | |
2656 | ||
99dc3ebd NA |
2657 | /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add |
2658 | a new type with the same properties as src_type to dst_fp. If dst_type is | |
2659 | not CTF_ERR, then we verify that dst_type has the same attributes as | |
2660 | src_type. We recurse for embedded references. Before we start, we note | |
2661 | that we are processing this type, to prevent infinite recursion: we do not | |
2662 | re-process any type that appears in this list. The list is emptied | |
2663 | wholesale at the end of processing everything in this recursive stack. */ | |
2664 | ||
2665 | if (ctf_dynhash_insert (proc_tracking_fp->ctf_add_processing, | |
2666 | (void *) (uintptr_t) src_type, (void *) 1) < 0) | |
2667 | return ctf_set_errno (dst_fp, ENOMEM); | |
2668 | ||
c499eb68 NA |
2669 | switch (kind) |
2670 | { | |
2671 | case CTF_K_INTEGER: | |
2672 | /* If we found a match we will have either returned it or declared a | |
2673 | conflict. */ | |
2674 | dst_type = ctf_add_integer (dst_fp, flag, name, &src_en); | |
2675 | break; | |
2676 | ||
2677 | case CTF_K_FLOAT: | |
2678 | /* If we found a match we will have either returned it or declared a | |
2679 | conflict. */ | |
2680 | dst_type = ctf_add_float (dst_fp, flag, name, &src_en); | |
2681 | break; | |
2682 | ||
2683 | case CTF_K_SLICE: | |
2684 | /* We have checked for conflicting encodings: now try to add the | |
2685 | contained type. */ | |
2686 | src_type = ctf_type_reference (src_fp, src_type); | |
99dc3ebd NA |
2687 | src_type = ctf_add_type_internal (dst_fp, src_fp, src_type, |
2688 | proc_tracking_fp); | |
c499eb68 NA |
2689 | |
2690 | if (src_type == CTF_ERR) | |
2691 | return CTF_ERR; /* errno is set for us. */ | |
2692 | ||
2693 | dst_type = ctf_add_slice (dst_fp, flag, src_type, &src_en); | |
2694 | break; | |
2695 | ||
2696 | case CTF_K_POINTER: | |
2697 | case CTF_K_VOLATILE: | |
2698 | case CTF_K_CONST: | |
2699 | case CTF_K_RESTRICT: | |
2700 | src_type = ctf_type_reference (src_fp, src_type); | |
99dc3ebd NA |
2701 | src_type = ctf_add_type_internal (dst_fp, src_fp, src_type, |
2702 | proc_tracking_fp); | |
c499eb68 NA |
2703 | |
2704 | if (src_type == CTF_ERR) | |
2705 | return CTF_ERR; /* errno is set for us. */ | |
2706 | ||
2707 | dst_type = ctf_add_reftype (dst_fp, flag, src_type, kind); | |
2708 | break; | |
2709 | ||
2710 | case CTF_K_ARRAY: | |
a0486bac | 2711 | if (ctf_array_info (src_fp, src_type, &src_ar) != 0) |
c499eb68 NA |
2712 | return (ctf_set_errno (dst_fp, ctf_errno (src_fp))); |
2713 | ||
2714 | src_ar.ctr_contents = | |
99dc3ebd NA |
2715 | ctf_add_type_internal (dst_fp, src_fp, src_ar.ctr_contents, |
2716 | proc_tracking_fp); | |
2717 | src_ar.ctr_index = ctf_add_type_internal (dst_fp, src_fp, | |
2718 | src_ar.ctr_index, | |
2719 | proc_tracking_fp); | |
c499eb68 NA |
2720 | src_ar.ctr_nelems = src_ar.ctr_nelems; |
2721 | ||
2722 | if (src_ar.ctr_contents == CTF_ERR || src_ar.ctr_index == CTF_ERR) | |
2723 | return CTF_ERR; /* errno is set for us. */ | |
2724 | ||
2725 | if (dst_type != CTF_ERR) | |
2726 | { | |
2727 | if (ctf_array_info (dst_fp, dst_type, &dst_ar) != 0) | |
2728 | return CTF_ERR; /* errno is set for us. */ | |
2729 | ||
2730 | if (memcmp (&src_ar, &dst_ar, sizeof (ctf_arinfo_t))) | |
2731 | { | |
926c9e76 NA |
2732 | ctf_err_warn (dst_fp, 1, ECTF_CONFLICT, |
2733 | _("conflict for type %s against ID %lx: array info " | |
2734 | "differs, old %lx/%lx/%x; new: %lx/%lx/%x"), | |
2735 | name, dst_type, src_ar.ctr_contents, | |
2736 | src_ar.ctr_index, src_ar.ctr_nelems, | |
2737 | dst_ar.ctr_contents, dst_ar.ctr_index, | |
2738 | dst_ar.ctr_nelems); | |
c499eb68 NA |
2739 | return (ctf_set_errno (dst_fp, ECTF_CONFLICT)); |
2740 | } | |
2741 | } | |
2742 | else | |
2743 | dst_type = ctf_add_array (dst_fp, flag, &src_ar); | |
2744 | break; | |
2745 | ||
2746 | case CTF_K_FUNCTION: | |
99dc3ebd NA |
2747 | ctc.ctc_return = ctf_add_type_internal (dst_fp, src_fp, |
2748 | src_tp->ctt_type, | |
2749 | proc_tracking_fp); | |
c499eb68 NA |
2750 | ctc.ctc_argc = 0; |
2751 | ctc.ctc_flags = 0; | |
2752 | ||
2753 | if (ctc.ctc_return == CTF_ERR) | |
2754 | return CTF_ERR; /* errno is set for us. */ | |
2755 | ||
2756 | dst_type = ctf_add_function (dst_fp, flag, &ctc, NULL); | |
2757 | break; | |
2758 | ||
2759 | case CTF_K_STRUCT: | |
2760 | case CTF_K_UNION: | |
2761 | { | |
2762 | ctf_dmdef_t *dmd; | |
2763 | int errs = 0; | |
a0486bac JM |
2764 | size_t size; |
2765 | ssize_t ssize; | |
99dc3ebd | 2766 | ctf_dtdef_t *dtd; |
c499eb68 NA |
2767 | |
2768 | /* Technically to match a struct or union we need to check both | |
2769 | ways (src members vs. dst, dst members vs. src) but we make | |
2770 | this more optimal by only checking src vs. dst and comparing | |
2771 | the total size of the structure (which we must do anyway) | |
2772 | which covers the possibility of dst members not in src. | |
2773 | This optimization can be defeated for unions, but is so | |
2774 | pathological as to render it irrelevant for our purposes. */ | |
2775 | ||
99dc3ebd NA |
2776 | if (dst_type != CTF_ERR && kind != CTF_K_FORWARD |
2777 | && dst_kind != CTF_K_FORWARD) | |
c499eb68 NA |
2778 | { |
2779 | if (ctf_type_size (src_fp, src_type) != | |
2780 | ctf_type_size (dst_fp, dst_type)) | |
2781 | { | |
926c9e76 NA |
2782 | ctf_err_warn (dst_fp, 1, ECTF_CONFLICT, |
2783 | _("conflict for type %s against ID %lx: union " | |
2784 | "size differs, old %li, new %li"), name, | |
2785 | dst_type, (long) ctf_type_size (src_fp, src_type), | |
2786 | (long) ctf_type_size (dst_fp, dst_type)); | |
c499eb68 NA |
2787 | return (ctf_set_errno (dst_fp, ECTF_CONFLICT)); |
2788 | } | |
2789 | ||
2790 | if (ctf_member_iter (src_fp, src_type, membcmp, &dst)) | |
2791 | { | |
926c9e76 NA |
2792 | ctf_err_warn (dst_fp, 1, ECTF_CONFLICT, |
2793 | _("conflict for type %s against ID %lx: members " | |
2794 | "differ, see above"), name, dst_type); | |
c499eb68 NA |
2795 | return (ctf_set_errno (dst_fp, ECTF_CONFLICT)); |
2796 | } | |
2797 | ||
2798 | break; | |
2799 | } | |
2800 | ||
2801 | /* Unlike the other cases, copying structs and unions is done | |
2802 | manually so as to avoid repeated lookups in ctf_add_member | |
2803 | and to ensure the exact same member offsets as in src_type. */ | |
2804 | ||
676c3ecb | 2805 | dst_type = ctf_add_generic (dst_fp, flag, name, kind, &dtd); |
c499eb68 NA |
2806 | if (dst_type == CTF_ERR) |
2807 | return CTF_ERR; /* errno is set for us. */ | |
2808 | ||
2809 | dst.ctb_type = dst_type; | |
2810 | dst.ctb_dtd = dtd; | |
2811 | ||
99dc3ebd NA |
2812 | /* Pre-emptively add this struct to the type mapping so that |
2813 | structures that refer to themselves work. */ | |
2814 | ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type); | |
2815 | ||
c499eb68 NA |
2816 | if (ctf_member_iter (src_fp, src_type, membadd, &dst) != 0) |
2817 | errs++; /* Increment errs and fail at bottom of case. */ | |
2818 | ||
a0486bac JM |
2819 | if ((ssize = ctf_type_size (src_fp, src_type)) < 0) |
2820 | return CTF_ERR; /* errno is set for us. */ | |
2821 | ||
2822 | size = (size_t) ssize; | |
2823 | if (size > CTF_MAX_SIZE) | |
c499eb68 NA |
2824 | { |
2825 | dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; | |
2826 | dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size); | |
2827 | dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size); | |
2828 | } | |
2829 | else | |
2830 | dtd->dtd_data.ctt_size = (uint32_t) size; | |
2831 | ||
2832 | dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, vlen); | |
2833 | ||
2834 | /* Make a final pass through the members changing each dmd_type (a | |
2835 | src_fp type) to an equivalent type in dst_fp. We pass through all | |
791915db NA |
2836 | members, leaving any that fail set to CTF_ERR, unless they fail |
2837 | because they are marking a member of type not representable in this | |
2838 | version of CTF, in which case we just want to silently omit them: | |
2839 | no consumer can do anything with them anyway. */ | |
c499eb68 NA |
2840 | for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members); |
2841 | dmd != NULL; dmd = ctf_list_next (dmd)) | |
2842 | { | |
139633c3 | 2843 | ctf_dict_t *dst = dst_fp; |
99dc3ebd NA |
2844 | ctf_id_t memb_type; |
2845 | ||
2846 | memb_type = ctf_type_mapping (src_fp, dmd->dmd_type, &dst); | |
2847 | if (memb_type == 0) | |
791915db | 2848 | { |
99dc3ebd NA |
2849 | if ((dmd->dmd_type = |
2850 | ctf_add_type_internal (dst_fp, src_fp, dmd->dmd_type, | |
2851 | proc_tracking_fp)) == CTF_ERR) | |
2852 | { | |
2853 | if (ctf_errno (dst_fp) != ECTF_NONREPRESENTABLE) | |
2854 | errs++; | |
2855 | } | |
791915db | 2856 | } |
99dc3ebd NA |
2857 | else |
2858 | dmd->dmd_type = memb_type; | |
c499eb68 NA |
2859 | } |
2860 | ||
2861 | if (errs) | |
2862 | return CTF_ERR; /* errno is set for us. */ | |
2863 | break; | |
2864 | } | |
2865 | ||
2866 | case CTF_K_ENUM: | |
99dc3ebd NA |
2867 | if (dst_type != CTF_ERR && kind != CTF_K_FORWARD |
2868 | && dst_kind != CTF_K_FORWARD) | |
c499eb68 NA |
2869 | { |
2870 | if (ctf_enum_iter (src_fp, src_type, enumcmp, &dst) | |
2871 | || ctf_enum_iter (dst_fp, dst_type, enumcmp, &src)) | |
2872 | { | |
926c9e76 NA |
2873 | ctf_err_warn (dst_fp, 1, ECTF_CONFLICT, |
2874 | _("conflict for enum %s against ID %lx: members " | |
2875 | "differ, see above"), name, dst_type); | |
c499eb68 NA |
2876 | return (ctf_set_errno (dst_fp, ECTF_CONFLICT)); |
2877 | } | |
2878 | } | |
2879 | else | |
2880 | { | |
2881 | dst_type = ctf_add_enum (dst_fp, flag, name); | |
2882 | if ((dst.ctb_type = dst_type) == CTF_ERR | |
2883 | || ctf_enum_iter (src_fp, src_type, enumadd, &dst)) | |
2884 | return CTF_ERR; /* errno is set for us */ | |
2885 | } | |
2886 | break; | |
2887 | ||
2888 | case CTF_K_FORWARD: | |
2889 | if (dst_type == CTF_ERR) | |
5de9eada | 2890 | dst_type = ctf_add_forward (dst_fp, flag, name, forward_kind); |
c499eb68 NA |
2891 | break; |
2892 | ||
2893 | case CTF_K_TYPEDEF: | |
2894 | src_type = ctf_type_reference (src_fp, src_type); | |
99dc3ebd NA |
2895 | src_type = ctf_add_type_internal (dst_fp, src_fp, src_type, |
2896 | proc_tracking_fp); | |
c499eb68 NA |
2897 | |
2898 | if (src_type == CTF_ERR) | |
2899 | return CTF_ERR; /* errno is set for us. */ | |
2900 | ||
2901 | /* If dst_type is not CTF_ERR at this point, we should check if | |
2902 | ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with | |
2903 | ECTF_CONFLICT. However, this causes problems with bitness typedefs | |
2904 | that vary based on things like if 32-bit then pid_t is int otherwise | |
2905 | long. We therefore omit this check and assume that if the identically | |
2906 | named typedef already exists in dst_fp, it is correct or | |
2907 | equivalent. */ | |
2908 | ||
2909 | if (dst_type == CTF_ERR) | |
c499eb68 | 2910 | dst_type = ctf_add_typedef (dst_fp, flag, name, src_type); |
99dc3ebd | 2911 | |
c499eb68 NA |
2912 | break; |
2913 | ||
2914 | default: | |
2915 | return (ctf_set_errno (dst_fp, ECTF_CORRUPT)); | |
2916 | } | |
2917 | ||
886453cb NA |
2918 | if (dst_type != CTF_ERR) |
2919 | ctf_add_type_mapping (src_fp, orig_src_type, dst_fp, dst_type); | |
c499eb68 NA |
2920 | return dst_type; |
2921 | } | |
2922 | ||
99dc3ebd | 2923 | ctf_id_t |
139633c3 | 2924 | ctf_add_type (ctf_dict_t *dst_fp, ctf_dict_t *src_fp, ctf_id_t src_type) |
99dc3ebd NA |
2925 | { |
2926 | ctf_id_t id; | |
2927 | ||
2928 | if (!src_fp->ctf_add_processing) | |
2929 | src_fp->ctf_add_processing = ctf_dynhash_create (ctf_hash_integer, | |
2930 | ctf_hash_eq_integer, | |
2931 | NULL, NULL); | |
2932 | ||
2933 | /* We store the hash on the source, because it contains only source type IDs: | |
2934 | but callers will invariably expect errors to appear on the dest. */ | |
2935 | if (!src_fp->ctf_add_processing) | |
2936 | return (ctf_set_errno (dst_fp, ENOMEM)); | |
2937 | ||
2938 | id = ctf_add_type_internal (dst_fp, src_fp, src_type, src_fp); | |
2939 | ctf_dynhash_empty (src_fp->ctf_add_processing); | |
2940 | ||
2941 | return id; | |
2942 | } | |
2943 | ||
fd55eae8 | 2944 | /* Write the compressed CTF data stream to the specified gzFile descriptor. */ |
47d546f4 | 2945 | int |
139633c3 | 2946 | ctf_gzwrite (ctf_dict_t *fp, gzFile fd) |
47d546f4 | 2947 | { |
fd55eae8 NA |
2948 | const unsigned char *buf; |
2949 | ssize_t resid; | |
47d546f4 NA |
2950 | ssize_t len; |
2951 | ||
fd55eae8 NA |
2952 | resid = sizeof (ctf_header_t); |
2953 | buf = (unsigned char *) fp->ctf_header; | |
2954 | while (resid != 0) | |
2955 | { | |
2956 | if ((len = gzwrite (fd, buf, resid)) <= 0) | |
2957 | return (ctf_set_errno (fp, errno)); | |
2958 | resid -= len; | |
2959 | buf += len; | |
2960 | } | |
2961 | ||
2962 | resid = fp->ctf_size; | |
2963 | buf = fp->ctf_buf; | |
47d546f4 NA |
2964 | while (resid != 0) |
2965 | { | |
2966 | if ((len = gzwrite (fd, buf, resid)) <= 0) | |
2967 | return (ctf_set_errno (fp, errno)); | |
2968 | resid -= len; | |
2969 | buf += len; | |
2970 | } | |
2971 | ||
2972 | return 0; | |
2973 | } | |
2974 | ||
2975 | /* Compress the specified CTF data stream and write it to the specified file | |
2976 | descriptor. */ | |
2977 | int | |
139633c3 | 2978 | ctf_compress_write (ctf_dict_t *fp, int fd) |
47d546f4 NA |
2979 | { |
2980 | unsigned char *buf; | |
2981 | unsigned char *bp; | |
2982 | ctf_header_t h; | |
2983 | ctf_header_t *hp = &h; | |
2984 | ssize_t header_len = sizeof (ctf_header_t); | |
2985 | ssize_t compress_len; | |
47d546f4 NA |
2986 | ssize_t len; |
2987 | int rc; | |
2988 | int err = 0; | |
2989 | ||
676c3ecb NA |
2990 | if (ctf_serialize (fp) < 0) |
2991 | return -1; /* errno is set for us. */ | |
2992 | ||
fd55eae8 | 2993 | memcpy (hp, fp->ctf_header, header_len); |
47d546f4 | 2994 | hp->cth_flags |= CTF_F_COMPRESS; |
676c3ecb | 2995 | compress_len = compressBound (fp->ctf_size); |
47d546f4 | 2996 | |
de07e349 | 2997 | if ((buf = malloc (compress_len)) == NULL) |
926c9e76 NA |
2998 | { |
2999 | ctf_err_warn (fp, 0, 0, _("ctf_compress_write: cannot allocate %li bytes"), | |
3000 | (unsigned long) compress_len); | |
3001 | return (ctf_set_errno (fp, ECTF_ZALLOC)); | |
3002 | } | |
47d546f4 | 3003 | |
65365aa8 | 3004 | if ((rc = compress (buf, (uLongf *) &compress_len, |
fd55eae8 | 3005 | fp->ctf_buf, fp->ctf_size)) != Z_OK) |
47d546f4 | 3006 | { |
47d546f4 | 3007 | err = ctf_set_errno (fp, ECTF_COMPRESS); |
926c9e76 | 3008 | ctf_err_warn (fp, 0, 0, _("zlib deflate err: %s"), zError (rc)); |
47d546f4 NA |
3009 | goto ret; |
3010 | } | |
3011 | ||
3012 | while (header_len > 0) | |
3013 | { | |
3014 | if ((len = write (fd, hp, header_len)) < 0) | |
3015 | { | |
3016 | err = ctf_set_errno (fp, errno); | |
926c9e76 | 3017 | ctf_err_warn (fp, 0, 0, _("ctf_compress_write: error writing header")); |
47d546f4 NA |
3018 | goto ret; |
3019 | } | |
3020 | header_len -= len; | |
3021 | hp += len; | |
3022 | } | |
3023 | ||
3024 | bp = buf; | |
3025 | while (compress_len > 0) | |
3026 | { | |
3027 | if ((len = write (fd, bp, compress_len)) < 0) | |
3028 | { | |
3029 | err = ctf_set_errno (fp, errno); | |
926c9e76 | 3030 | ctf_err_warn (fp, 0, 0, _("ctf_compress_write: error writing")); |
47d546f4 NA |
3031 | goto ret; |
3032 | } | |
3033 | compress_len -= len; | |
3034 | bp += len; | |
3035 | } | |
3036 | ||
3037 | ret: | |
de07e349 | 3038 | free (buf); |
47d546f4 NA |
3039 | return err; |
3040 | } | |
3041 | ||
5537f9b9 NA |
3042 | /* Optionally compress the specified CTF data stream and return it as a new |
3043 | dynamically-allocated string. */ | |
3044 | unsigned char * | |
139633c3 | 3045 | ctf_write_mem (ctf_dict_t *fp, size_t *size, size_t threshold) |
5537f9b9 NA |
3046 | { |
3047 | unsigned char *buf; | |
3048 | unsigned char *bp; | |
3049 | ctf_header_t *hp; | |
3050 | ssize_t header_len = sizeof (ctf_header_t); | |
3051 | ssize_t compress_len; | |
5537f9b9 NA |
3052 | int rc; |
3053 | ||
676c3ecb NA |
3054 | if (ctf_serialize (fp) < 0) |
3055 | return NULL; /* errno is set for us. */ | |
3056 | ||
3057 | compress_len = compressBound (fp->ctf_size); | |
5537f9b9 | 3058 | if (fp->ctf_size < threshold) |
676c3ecb NA |
3059 | compress_len = fp->ctf_size; |
3060 | if ((buf = malloc (compress_len | |
5537f9b9 NA |
3061 | + sizeof (struct ctf_header))) == NULL) |
3062 | { | |
3063 | ctf_set_errno (fp, ENOMEM); | |
926c9e76 NA |
3064 | ctf_err_warn (fp, 0, 0, _("ctf_write_mem: cannot allocate %li bytes"), |
3065 | (unsigned long) (compress_len + sizeof (struct ctf_header))); | |
5537f9b9 NA |
3066 | return NULL; |
3067 | } | |
3068 | ||
3069 | hp = (ctf_header_t *) buf; | |
3070 | memcpy (hp, fp->ctf_header, header_len); | |
3071 | bp = buf + sizeof (struct ctf_header); | |
3072 | *size = sizeof (struct ctf_header); | |
3073 | ||
5537f9b9 NA |
3074 | if (fp->ctf_size < threshold) |
3075 | { | |
3076 | hp->cth_flags &= ~CTF_F_COMPRESS; | |
3077 | memcpy (bp, fp->ctf_buf, fp->ctf_size); | |
3078 | *size += fp->ctf_size; | |
3079 | } | |
3080 | else | |
3081 | { | |
3082 | hp->cth_flags |= CTF_F_COMPRESS; | |
3083 | if ((rc = compress (bp, (uLongf *) &compress_len, | |
3084 | fp->ctf_buf, fp->ctf_size)) != Z_OK) | |
3085 | { | |
5537f9b9 | 3086 | ctf_set_errno (fp, ECTF_COMPRESS); |
926c9e76 | 3087 | ctf_err_warn (fp, 0, 0, _("zlib deflate err: %s"), zError (rc)); |
de07e349 | 3088 | free (buf); |
5537f9b9 NA |
3089 | return NULL; |
3090 | } | |
3091 | *size += compress_len; | |
3092 | } | |
3093 | return buf; | |
3094 | } | |
3095 | ||
fd55eae8 | 3096 | /* Write the uncompressed CTF data stream to the specified file descriptor. */ |
47d546f4 | 3097 | int |
139633c3 | 3098 | ctf_write (ctf_dict_t *fp, int fd) |
47d546f4 | 3099 | { |
fd55eae8 NA |
3100 | const unsigned char *buf; |
3101 | ssize_t resid; | |
47d546f4 NA |
3102 | ssize_t len; |
3103 | ||
676c3ecb NA |
3104 | if (ctf_serialize (fp) < 0) |
3105 | return -1; /* errno is set for us. */ | |
3106 | ||
fd55eae8 NA |
3107 | resid = sizeof (ctf_header_t); |
3108 | buf = (unsigned char *) fp->ctf_header; | |
3109 | while (resid != 0) | |
3110 | { | |
3111 | if ((len = write (fd, buf, resid)) <= 0) | |
926c9e76 NA |
3112 | { |
3113 | ctf_err_warn (fp, 0, errno, _("ctf_write: error writing header")); | |
3114 | return (ctf_set_errno (fp, errno)); | |
3115 | } | |
fd55eae8 NA |
3116 | resid -= len; |
3117 | buf += len; | |
3118 | } | |
3119 | ||
3120 | resid = fp->ctf_size; | |
3121 | buf = fp->ctf_buf; | |
47d546f4 NA |
3122 | while (resid != 0) |
3123 | { | |
fd55eae8 | 3124 | if ((len = write (fd, buf, resid)) <= 0) |
926c9e76 NA |
3125 | { |
3126 | ctf_err_warn (fp, 0, errno, _("ctf_write: error writing")); | |
3127 | return (ctf_set_errno (fp, errno)); | |
3128 | } | |
47d546f4 NA |
3129 | resid -= len; |
3130 | buf += len; | |
3131 | } | |
3132 | ||
3133 | return 0; | |
3134 | } |