]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libctf/ctf-archive.c
x86/testsuite: Refine AVX10.2 rounding testcases
[thirdparty/binutils-gdb.git] / libctf / ctf-archive.c
CommitLineData
9402cc59 1/* CTF archive files.
fd67aa11 2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
9402cc59
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/types.h>
22#include <sys/stat.h>
23#include <elf.h>
a0486bac 24#include "ctf-endian.h"
9402cc59
NA
25#include <errno.h>
26#include <fcntl.h>
27#include <stdio.h>
28#include <string.h>
29#include <unistd.h>
30
31#ifdef HAVE_MMAP
32#include <sys/mman.h>
33#endif
34
139633c3 35static off_t arc_write_one_ctf (ctf_dict_t * f, int fd, size_t threshold);
ae41200b
NA
36static ctf_dict_t *ctf_dict_open_by_offset (const struct ctf_archive *arc,
37 const ctf_sect_t *symsect,
38 const ctf_sect_t *strsect,
53651de8
NA
39 size_t offset, int little_endian,
40 int *errp);
9402cc59
NA
41static int sort_modent_by_name (const void *one, const void *two, void *n);
42static void *arc_mmap_header (int fd, size_t headersz);
43static void *arc_mmap_file (int fd, size_t size);
44static int arc_mmap_writeout (int fd, void *header, size_t headersz,
45 const char **errmsg);
46static int arc_mmap_unmap (void *header, size_t headersz, const char **errmsg);
61914bb6
NA
47static int ctf_arc_import_parent (const ctf_archive_t *arc, ctf_dict_t *fp,
48 int *errp);
2c78e925 49
f4f60336
NA
50/* Flag to indicate "symbol not present" in ctf_archive_internal.ctfi_symdicts
51 and ctfi_symnamedicts. Never initialized. */
2c78e925 52static ctf_dict_t enosym;
9402cc59 53
5537f9b9 54/* Write out a CTF archive to the start of the file referenced by the passed-in
139633c3
NA
55 fd. The entries in CTF_DICTS are referenced by name: the names are passed in
56 the names array, which must have CTF_DICTS entries.
9402cc59
NA
57
58 Returns 0 on success, or an errno, or an ECTF_* value. */
59int
139633c3 60ctf_arc_write_fd (int fd, ctf_dict_t **ctf_dicts, size_t ctf_dict_cnt,
5537f9b9 61 const char **names, size_t threshold)
9402cc59
NA
62{
63 const char *errmsg;
64 struct ctf_archive *archdr;
9402cc59
NA
65 size_t i;
66 char dummy = 0;
67 size_t headersz;
68 ssize_t namesz;
69 size_t ctf_startoffs; /* Start of the section we are working over. */
70 char *nametbl = NULL; /* The name table. */
71 char *np;
72 off_t nameoffs;
73 struct ctf_archive_modent *modent;
74
5537f9b9 75 ctf_dprintf ("Writing CTF archive with %lu files\n",
139633c3 76 (unsigned long) ctf_dict_cnt);
9402cc59 77
9402cc59
NA
78 /* Figure out the size of the mmap()ed header, including the
79 ctf_archive_modent array. We assume that all of this needs no
80 padding: a likely assumption, given that it's all made up of
81 uint64_t's. */
82 headersz = sizeof (struct ctf_archive)
139633c3 83 + (ctf_dict_cnt * sizeof (uint64_t) * 2);
62d8e3b7 84 ctf_dprintf ("headersz is %lu\n", (unsigned long) headersz);
9402cc59
NA
85
86 /* From now on we work in two pieces: an mmap()ed region from zero up to the
87 headersz, and a region updated via write() starting after that, containing
88 all the tables. Platforms that do not support mmap() just use write(). */
89 ctf_startoffs = headersz;
90 if (lseek (fd, ctf_startoffs - 1, SEEK_SET) < 0)
91 {
926c9e76 92 errmsg = N_("ctf_arc_write(): cannot extend file while writing");
5537f9b9 93 goto err;
9402cc59
NA
94 }
95
96 if (write (fd, &dummy, 1) < 0)
97 {
926c9e76 98 errmsg = N_("ctf_arc_write(): cannot extend file while writing");
5537f9b9 99 goto err;
9402cc59
NA
100 }
101
102 if ((archdr = arc_mmap_header (fd, headersz)) == NULL)
103 {
926c9e76 104 errmsg = N_("ctf_arc_write(): cannot mmap");
5537f9b9 105 goto err;
9402cc59
NA
106 }
107
108 /* Fill in everything we can, which is everything other than the name
109 table offset. */
110 archdr->ctfa_magic = htole64 (CTFA_MAGIC);
139633c3 111 archdr->ctfa_ndicts = htole64 (ctf_dict_cnt);
9402cc59
NA
112 archdr->ctfa_ctfs = htole64 (ctf_startoffs);
113
114 /* We could validate that all CTF files have the same data model, but
115 since any reasonable construction process will be building things of
116 only one bitness anyway, this is pretty pointless, so just use the
117 model of the first CTF file for all of them. (It *is* valid to
118 create an empty archive: the value of ctfa_model is irrelevant in
119 this case, but we must be sure not to dereference uninitialized
120 memory.) */
121
139633c3
NA
122 if (ctf_dict_cnt > 0)
123 archdr->ctfa_model = htole64 (ctf_getmodel (ctf_dicts[0]));
9402cc59
NA
124
125 /* Now write out the CTFs: ctf_archive_modent array via the mapping,
126 ctfs via write(). The names themselves have not been written yet: we
127 track them in a local strtab until the time is right, and sort the
128 modents array after construction.
129
130 The name table is not sorted. */
131
139633c3 132 for (i = 0, namesz = 0; i < le64toh (archdr->ctfa_ndicts); i++)
9402cc59
NA
133 namesz += strlen (names[i]) + 1;
134
135 nametbl = malloc (namesz);
136 if (nametbl == NULL)
137 {
926c9e76 138 errmsg = N_("ctf_arc_write(): error writing named CTF to archive");
9402cc59
NA
139 goto err_unmap;
140 }
141
142 for (i = 0, namesz = 0,
143 modent = (ctf_archive_modent_t *) ((char *) archdr
144 + sizeof (struct ctf_archive));
139633c3 145 i < le64toh (archdr->ctfa_ndicts); i++)
9402cc59
NA
146 {
147 off_t off;
148
149 strcpy (&nametbl[namesz], names[i]);
150
139633c3 151 off = arc_write_one_ctf (ctf_dicts[i], fd, threshold);
9402cc59
NA
152 if ((off < 0) && (off > -ECTF_BASE))
153 {
926c9e76
NA
154 errmsg = N_("ctf_arc_write(): cannot determine file "
155 "position while writing to archive");
9402cc59
NA
156 goto err_free;
157 }
158 if (off < 0)
159 {
926c9e76 160 errmsg = N_("ctf_arc_write(): cannot write CTF file to archive");
9402cc59
NA
161 errno = off * -1;
162 goto err_free;
163 }
164
165 modent->name_offset = htole64 (namesz);
166 modent->ctf_offset = htole64 (off - ctf_startoffs);
167 namesz += strlen (names[i]) + 1;
168 modent++;
169 }
170
6b22174f
NA
171 ctf_qsort_r ((ctf_archive_modent_t *) ((char *) archdr
172 + sizeof (struct ctf_archive)),
139633c3 173 le64toh (archdr->ctfa_ndicts),
6b22174f
NA
174 sizeof (struct ctf_archive_modent), sort_modent_by_name,
175 nametbl);
9402cc59
NA
176
177 /* Now the name table. */
178
179 if ((nameoffs = lseek (fd, 0, SEEK_CUR)) < 0)
180 {
926c9e76
NA
181 errmsg = N_("ctf_arc_write(): cannot get current file position "
182 "in archive");
9402cc59
NA
183 goto err_free;
184 }
185 archdr->ctfa_names = htole64 (nameoffs);
186 np = nametbl;
187 while (namesz > 0)
188 {
189 ssize_t len;
190 if ((len = write (fd, np, namesz)) < 0)
191 {
926c9e76 192 errmsg = N_("ctf_arc_write(): cannot write name table to archive");
9402cc59
NA
193 goto err_free;
194 }
195 namesz -= len;
196 np += len;
197 }
198 free (nametbl);
199
200 if (arc_mmap_writeout (fd, archdr, headersz, &errmsg) < 0)
201 goto err_unmap;
202 if (arc_mmap_unmap (archdr, headersz, &errmsg) < 0)
5537f9b9 203 goto err;
9402cc59
NA
204 return 0;
205
206err_free:
207 free (nametbl);
208err_unmap:
209 arc_mmap_unmap (archdr, headersz, NULL);
9402cc59 210err:
926c9e76
NA
211 /* We report errors into the first file in the archive, if any: if this is a
212 zero-file archive, put it in the open-errors stream for lack of anywhere
213 else for it to go. */
139633c3 214 ctf_err_warn (ctf_dict_cnt > 0 ? ctf_dicts[0] : NULL, 0, errno, "%s",
926c9e76 215 gettext (errmsg));
9402cc59
NA
216 return errno;
217}
218
139633c3
NA
219/* Write out a CTF archive. The entries in CTF_DICTS are referenced by name:
220 the names are passed in the names array, which must have CTF_DICTS entries.
5537f9b9
NA
221
222 If the filename is NULL, create a temporary file and return a pointer to it.
223
224 Returns 0 on success, or an errno, or an ECTF_* value. */
225int
139633c3 226ctf_arc_write (const char *file, ctf_dict_t **ctf_dicts, size_t ctf_dict_cnt,
5537f9b9
NA
227 const char **names, size_t threshold)
228{
229 int err;
230 int fd;
231
232 if ((fd = open (file, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0666)) < 0)
233 {
139633c3 234 ctf_err_warn (ctf_dict_cnt > 0 ? ctf_dicts[0] : NULL, 0, errno,
926c9e76 235 _("ctf_arc_write(): cannot create %s"), file);
5537f9b9
NA
236 return errno;
237 }
238
139633c3 239 err = ctf_arc_write_fd (fd, ctf_dicts, ctf_dict_cnt, names, threshold);
5537f9b9 240 if (err)
df16e041 241 goto err_close;
5537f9b9
NA
242
243 if ((err = close (fd)) < 0)
139633c3 244 ctf_err_warn (ctf_dict_cnt > 0 ? ctf_dicts[0] : NULL, 0, errno,
926c9e76 245 _("ctf_arc_write(): cannot close after writing to archive"));
df16e041 246 goto err;
5537f9b9
NA
247
248 err_close:
df16e041
NC
249 (void) close (fd);
250 err:
5537f9b9
NA
251 if (err < 0)
252 unlink (file);
253
254 return err;
255}
256
483546ce 257/* Write one CTF dict out. Return the file position of the written file (or
9402cc59
NA
258 rather, of the file-size uint64_t that precedes it): negative return is a
259 negative errno or ctf_errno value. On error, the file position may no longer
260 be at the end of the file. */
261static off_t
483546ce 262arc_write_one_ctf (ctf_dict_t *f, int fd, size_t threshold)
9402cc59
NA
263{
264 off_t off, end_off;
265 uint64_t ctfsz = 0;
266 char *ctfszp;
267 size_t ctfsz_len;
9402cc59
NA
268
269 if ((off = lseek (fd, 0, SEEK_CUR)) < 0)
270 return errno * -1;
271
9402cc59
NA
272 /* This zero-write turns into the size in a moment. */
273 ctfsz_len = sizeof (ctfsz);
274 ctfszp = (char *) &ctfsz;
275 while (ctfsz_len > 0)
276 {
277 ssize_t writelen = write (fd, ctfszp, ctfsz_len);
278 if (writelen < 0)
279 return errno * -1;
280 ctfsz_len -= writelen;
281 ctfszp += writelen;
282 }
283
36c771b1 284 if (ctf_write_thresholded (f, fd, threshold) != 0)
9402cc59
NA
285 return f->ctf_errno * -1;
286
287 if ((end_off = lseek (fd, 0, SEEK_CUR)) < 0)
288 return errno * -1;
289 ctfsz = htole64 (end_off - off);
290
291 if ((lseek (fd, off, SEEK_SET)) < 0)
292 return errno * -1;
293
294 /* ... here. */
295 ctfsz_len = sizeof (ctfsz);
296 ctfszp = (char *) &ctfsz;
297 while (ctfsz_len > 0)
298 {
299 ssize_t writelen = write (fd, ctfszp, ctfsz_len);
300 if (writelen < 0)
301 return errno * -1;
302 ctfsz_len -= writelen;
303 ctfszp += writelen;
304 }
305
306 end_off = LCTF_ALIGN_OFFS (end_off, 8);
307 if ((lseek (fd, end_off, SEEK_SET)) < 0)
308 return errno * -1;
309
310 return off;
311}
312
313/* qsort() function to sort the array of struct ctf_archive_modents into
314 ascending name order. */
315static int
316sort_modent_by_name (const void *one, const void *two, void *n)
317{
318 const struct ctf_archive_modent *a = one;
319 const struct ctf_archive_modent *b = two;
320 char *nametbl = n;
321
322 return strcmp (&nametbl[le64toh (a->name_offset)],
323 &nametbl[le64toh (b->name_offset)]);
324}
325
2e428e74 326/* bsearch_r() function to search for a given name in the sorted array of struct
9402cc59
NA
327 ctf_archive_modents. */
328static int
2e428e74 329search_modent_by_name (const void *key, const void *ent, void *arg)
9402cc59
NA
330{
331 const char *k = key;
332 const struct ctf_archive_modent *v = ent;
2e428e74 333 const char *search_nametbl = arg;
9402cc59
NA
334
335 return strcmp (k, &search_nametbl[le64toh (v->name_offset)]);
336}
337
2f6ecaed 338/* Make a new struct ctf_archive_internal wrapper for a ctf_archive or a
139633c3 339 ctf_dict. Closes ARC and/or FP on error. Arrange to free the SYMSECT or
601e455b 340 STRSECT, as needed, on close. Possibly do not unmap on close. */
2f6ecaed
NA
341
342struct ctf_archive_internal *
601e455b
NA
343ctf_new_archive_internal (int is_archive, int unmap_on_close,
344 struct ctf_archive *arc,
139633c3 345 ctf_dict_t *fp, const ctf_sect_t *symsect,
2f6ecaed
NA
346 const ctf_sect_t *strsect,
347 int *errp)
9402cc59 348{
2f6ecaed 349 struct ctf_archive_internal *arci;
9402cc59 350
2f6ecaed 351 if ((arci = calloc (1, sizeof (struct ctf_archive_internal))) == NULL)
9402cc59 352 {
2f6ecaed 353 if (is_archive)
601e455b
NA
354 {
355 if (unmap_on_close)
356 ctf_arc_close_internal (arc);
357 }
2f6ecaed 358 else
139633c3 359 ctf_dict_close (fp);
2f6ecaed 360 return (ctf_set_open_errno (errp, errno));
9402cc59 361 }
2f6ecaed
NA
362 arci->ctfi_is_archive = is_archive;
363 if (is_archive)
364 arci->ctfi_archive = arc;
365 else
139633c3 366 arci->ctfi_dict = fp;
2f6ecaed
NA
367 if (symsect)
368 memcpy (&arci->ctfi_symsect, symsect, sizeof (struct ctf_sect));
369 if (strsect)
370 memcpy (&arci->ctfi_strsect, strsect, sizeof (struct ctf_sect));
371 arci->ctfi_free_symsect = 0;
d50c0802 372 arci->ctfi_free_strsect = 0;
601e455b 373 arci->ctfi_unmap_on_close = unmap_on_close;
53651de8 374 arci->ctfi_symsect_little_endian = -1;
2f6ecaed
NA
375
376 return arci;
377}
378
53651de8
NA
379/* Set the symbol-table endianness of an archive (defaulting the symtab
380 endianness of all ctf_file_t's opened from that archive). */
381void
382ctf_arc_symsect_endianness (ctf_archive_t *arc, int little_endian)
383{
384 arc->ctfi_symsect_little_endian = !!little_endian;
385 if (!arc->ctfi_is_archive)
386 ctf_symsect_endianness (arc->ctfi_dict, arc->ctfi_symsect_little_endian);
387}
388
3d16b64e
NA
389/* Get the CTF preamble from data in a buffer, which may be either an archive or
390 a CTF dict. If multiple dicts are present in an archive, the preamble comes
391 from an arbitrary dict. The preamble is a pointer into the ctfsect passed
392 in. */
393
394const ctf_preamble_t *
395ctf_arc_bufpreamble (const ctf_sect_t *ctfsect)
396{
027333da
AM
397 if (ctfsect->cts_data != NULL
398 && ctfsect->cts_size > sizeof (uint64_t)
399 && (le64toh ((*(uint64_t *) ctfsect->cts_data)) == CTFA_MAGIC))
3d16b64e
NA
400 {
401 struct ctf_archive *arc = (struct ctf_archive *) ctfsect->cts_data;
402 return (const ctf_preamble_t *) ((char *) arc + le64toh (arc->ctfa_ctfs)
403 + sizeof (uint64_t));
404 }
405 else
406 return (const ctf_preamble_t *) ctfsect->cts_data;
407}
408
2f6ecaed
NA
409/* Open a CTF archive or dictionary from data in a buffer (which the caller must
410 preserve until ctf_arc_close() time). Returns the archive, or NULL and an
411 error in *err (if not NULL). */
412ctf_archive_t *
413ctf_arc_bufopen (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect,
414 const ctf_sect_t *strsect, int *errp)
415{
416 struct ctf_archive *arc = NULL;
417 int is_archive;
139633c3 418 ctf_dict_t *fp = NULL;
2f6ecaed 419
027333da
AM
420 if (ctfsect->cts_data != NULL
421 && ctfsect->cts_size > sizeof (uint64_t)
422 && (le64toh ((*(uint64_t *) ctfsect->cts_data)) == CTFA_MAGIC))
2f6ecaed 423 {
601e455b
NA
424 /* The archive is mmappable, so this operation is trivial.
425
426 This buffer is nonmodifiable, so the trick involving mmapping only part
427 of it and storing the length in the magic number is not applicable: so
428 record this fact in the archive-wrapper header. (We cannot record it
429 in the archive, because the archive may very well be a read-only
430 mapping.) */
2f6ecaed
NA
431
432 is_archive = 1;
433 arc = (struct ctf_archive *) ctfsect->cts_data;
434 }
435 else
436 {
437 is_archive = 0;
438 if ((fp = ctf_bufopen (ctfsect, symsect, strsect, errp)) == NULL)
439 {
926c9e76 440 ctf_err_warn (NULL, 0, *errp, _("ctf_arc_bufopen(): cannot open CTF"));
2f6ecaed
NA
441 return NULL;
442 }
443 }
601e455b 444 return ctf_new_archive_internal (is_archive, 0, arc, fp, symsect, strsect,
2f6ecaed 445 errp);
9402cc59
NA
446}
447
448/* Open a CTF archive. Returns the archive, or NULL and an error in *err (if
449 not NULL). */
450struct ctf_archive *
451ctf_arc_open_internal (const char *filename, int *errp)
452{
453 const char *errmsg;
454 int fd;
455 struct stat s;
456 struct ctf_archive *arc; /* (Actually the whole file.) */
457
458 libctf_init_debug();
459 if ((fd = open (filename, O_RDONLY)) < 0)
460 {
926c9e76 461 errmsg = N_("ctf_arc_open(): cannot open %s");
9402cc59
NA
462 goto err;
463 }
464 if (fstat (fd, &s) < 0)
465 {
926c9e76 466 errmsg = N_("ctf_arc_open(): cannot stat %s");
9402cc59
NA
467 goto err_close;
468 }
469
470 if ((arc = arc_mmap_file (fd, s.st_size)) == NULL)
471 {
926c9e76 472 errmsg = N_("ctf_arc_open(): cannot read in %s");
9402cc59
NA
473 goto err_close;
474 }
475
476 if (le64toh (arc->ctfa_magic) != CTFA_MAGIC)
477 {
926c9e76 478 errmsg = N_("ctf_arc_open(): %s: invalid magic number");
9402cc59
NA
479 errno = ECTF_FMT;
480 goto err_unmap;
481 }
482
483 /* This horrible hack lets us know how much to unmap when the file is
484 closed. (We no longer need the magic number, and the mapping
485 is private.) */
486 arc->ctfa_magic = s.st_size;
487 close (fd);
488 return arc;
489
490err_unmap:
491 arc_mmap_unmap (arc, s.st_size, NULL);
492err_close:
493 close (fd);
494err:
495 if (errp)
496 *errp = errno;
926c9e76 497 ctf_err_warn (NULL, 0, errno, gettext (errmsg), filename);
9402cc59
NA
498 return NULL;
499}
500
501/* Close an archive. */
502void
503ctf_arc_close_internal (struct ctf_archive *arc)
504{
505 if (arc == NULL)
506 return;
507
508 /* See the comment in ctf_arc_open(). */
509 arc_mmap_unmap (arc, arc->ctfa_magic, NULL);
510}
511
512/* Public entry point: close an archive, or CTF file. */
513void
514ctf_arc_close (ctf_archive_t *arc)
515{
516 if (arc == NULL)
517 return;
518
519 if (arc->ctfi_is_archive)
601e455b
NA
520 {
521 if (arc->ctfi_unmap_on_close)
522 ctf_arc_close_internal (arc->ctfi_archive);
523 }
9402cc59 524 else
139633c3 525 ctf_dict_close (arc->ctfi_dict);
2c78e925 526 free (arc->ctfi_symdicts);
f4f60336 527 free (arc->ctfi_symnamedicts);
2c78e925 528 ctf_dynhash_destroy (arc->ctfi_dicts);
2f6ecaed
NA
529 if (arc->ctfi_free_symsect)
530 free ((void *) arc->ctfi_symsect.cts_data);
d50c0802
NA
531 if (arc->ctfi_free_strsect)
532 free ((void *) arc->ctfi_strsect.cts_data);
9402cc59 533 free (arc->ctfi_data);
f046147d
NA
534 if (arc->ctfi_bfd_close)
535 arc->ctfi_bfd_close (arc);
9402cc59
NA
536 free (arc);
537}
538
139633c3 539/* Return the ctf_dict_t with the given name, or NULL if none, setting 'err' if
9402cc59 540 non-NULL. A name of NULL means to open the default file. */
139633c3 541static ctf_dict_t *
ae41200b
NA
542ctf_dict_open_internal (const struct ctf_archive *arc,
543 const ctf_sect_t *symsect,
544 const ctf_sect_t *strsect,
53651de8
NA
545 const char *name, int little_endian,
546 int *errp)
9402cc59
NA
547{
548 struct ctf_archive_modent *modent;
2e428e74 549 const char *search_nametbl;
9402cc59
NA
550
551 if (name == NULL)
552 name = _CTF_SECTION; /* The default name. */
553
ae41200b 554 ctf_dprintf ("ctf_dict_open_internal(%s): opening\n", name);
9402cc59
NA
555
556 modent = (ctf_archive_modent_t *) ((char *) arc
557 + sizeof (struct ctf_archive));
558
2e428e74 559 search_nametbl = (const char *) arc + le64toh (arc->ctfa_names);
139633c3 560 modent = bsearch_r (name, modent, le64toh (arc->ctfa_ndicts),
2e428e74
NA
561 sizeof (struct ctf_archive_modent),
562 search_modent_by_name, (void *) search_nametbl);
9402cc59
NA
563
564 /* This is actually a common case and normal operation: no error
565 debug output. */
566 if (modent == NULL)
567 {
568 if (errp)
569 *errp = ECTF_ARNNAME;
570 return NULL;
571 }
572
ae41200b 573 return ctf_dict_open_by_offset (arc, symsect, strsect,
53651de8
NA
574 le64toh (modent->ctf_offset),
575 little_endian, errp);
9402cc59
NA
576}
577
139633c3 578/* Return the ctf_dict_t with the given name, or NULL if none, setting 'err' if
9402cc59
NA
579 non-NULL. A name of NULL means to open the default file.
580
581 Use the specified string and symbol table sections.
582
583 Public entry point. */
139633c3 584ctf_dict_t *
ae41200b
NA
585ctf_dict_open_sections (const ctf_archive_t *arc,
586 const ctf_sect_t *symsect,
587 const ctf_sect_t *strsect,
588 const char *name,
589 int *errp)
9402cc59
NA
590{
591 if (arc->ctfi_is_archive)
592 {
139633c3 593 ctf_dict_t *ret;
ae41200b 594 ret = ctf_dict_open_internal (arc->ctfi_archive, symsect, strsect,
53651de8
NA
595 name, arc->ctfi_symsect_little_endian,
596 errp);
9402cc59 597 if (ret)
2c78e925
NA
598 {
599 ret->ctf_archive = (ctf_archive_t *) arc;
61914bb6
NA
600 if (ctf_arc_import_parent (arc, ret, errp) < 0)
601 {
602 ctf_dict_close (ret);
603 return NULL;
604 }
2c78e925 605 }
9402cc59
NA
606 return ret;
607 }
608
609 if ((name != NULL) && (strcmp (name, _CTF_SECTION) != 0))
610 {
611 if (errp)
612 *errp = ECTF_ARNNAME;
613 return NULL;
614 }
139633c3 615 arc->ctfi_dict->ctf_archive = (ctf_archive_t *) arc;
9402cc59 616
139633c3
NA
617 /* Bump the refcount so that the user can ctf_dict_close() it. */
618 arc->ctfi_dict->ctf_refcnt++;
619 return arc->ctfi_dict;
9402cc59
NA
620}
621
139633c3 622/* Return the ctf_dict_t with the given name, or NULL if none, setting 'err' if
9402cc59
NA
623 non-NULL. A name of NULL means to open the default file.
624
625 Public entry point. */
139633c3 626ctf_dict_t *
ae41200b 627ctf_dict_open (const ctf_archive_t *arc, const char *name, int *errp)
9402cc59
NA
628{
629 const ctf_sect_t *symsect = &arc->ctfi_symsect;
630 const ctf_sect_t *strsect = &arc->ctfi_strsect;
631
632 if (symsect->cts_name == NULL)
633 symsect = NULL;
634 if (strsect->cts_name == NULL)
635 strsect = NULL;
636
ae41200b 637 return ctf_dict_open_sections (arc, symsect, strsect, name, errp);
9402cc59
NA
638}
639
2c78e925
NA
640static void
641ctf_cached_dict_close (void *fp)
642{
643 ctf_dict_close ((ctf_dict_t *) fp);
644}
645
f4f60336
NA
646/* Return the ctf_dict_t with the given name and cache it in the archive's
647 ctfi_dicts. If this is the first cached dict, designate it the
648 crossdict_cache. */
2c78e925
NA
649static ctf_dict_t *
650ctf_dict_open_cached (ctf_archive_t *arc, const char *name, int *errp)
651{
652 ctf_dict_t *fp;
653 char *dupname;
654
655 /* Just return from the cache if possible. */
656 if (arc->ctfi_dicts
657 && ((fp = ctf_dynhash_lookup (arc->ctfi_dicts, name)) != NULL))
658 {
659 fp->ctf_refcnt++;
660 return fp;
661 }
662
663 /* Not yet cached: open it. */
664 fp = ctf_dict_open (arc, name, errp);
665 dupname = strdup (name);
666
667 if (!fp || !dupname)
668 goto oom;
669
670 if (arc->ctfi_dicts == NULL)
671 if ((arc->ctfi_dicts
672 = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
673 free, ctf_cached_dict_close)) == NULL)
674 goto oom;
675
676 if (ctf_dynhash_insert (arc->ctfi_dicts, dupname, fp) < 0)
677 goto oom;
678 fp->ctf_refcnt++;
679
f4f60336
NA
680 if (arc->ctfi_crossdict_cache == NULL)
681 arc->ctfi_crossdict_cache = fp;
682
2c78e925
NA
683 return fp;
684
685 oom:
686 ctf_dict_close (fp);
687 free (dupname);
688 if (errp)
689 *errp = ENOMEM;
690 return NULL;
691}
692
693/* Flush any caches the CTF archive may have open. */
694void
695ctf_arc_flush_caches (ctf_archive_t *wrapper)
696{
697 free (wrapper->ctfi_symdicts);
ca019227 698 ctf_dynhash_destroy (wrapper->ctfi_symnamedicts);
2c78e925
NA
699 ctf_dynhash_destroy (wrapper->ctfi_dicts);
700 wrapper->ctfi_symdicts = NULL;
f4f60336 701 wrapper->ctfi_symnamedicts = NULL;
2c78e925 702 wrapper->ctfi_dicts = NULL;
f4f60336 703 wrapper->ctfi_crossdict_cache = NULL;
2c78e925
NA
704}
705
139633c3 706/* Return the ctf_dict_t at the given ctfa_ctfs-relative offset, or NULL if
9402cc59 707 none, setting 'err' if non-NULL. */
139633c3 708static ctf_dict_t *
ae41200b
NA
709ctf_dict_open_by_offset (const struct ctf_archive *arc,
710 const ctf_sect_t *symsect,
711 const ctf_sect_t *strsect, size_t offset,
53651de8 712 int little_endian, int *errp)
9402cc59
NA
713{
714 ctf_sect_t ctfsect;
139633c3 715 ctf_dict_t *fp;
9402cc59 716
ae41200b 717 ctf_dprintf ("ctf_dict_open_by_offset(%lu): opening\n", (unsigned long) offset);
9402cc59 718
a0486bac 719 memset (&ctfsect, 0, sizeof (ctf_sect_t));
9402cc59
NA
720
721 offset += le64toh (arc->ctfa_ctfs);
722
723 ctfsect.cts_name = _CTF_SECTION;
9402cc59
NA
724 ctfsect.cts_size = le64toh (*((uint64_t *) ((char *) arc + offset)));
725 ctfsect.cts_entsize = 1;
9402cc59
NA
726 ctfsect.cts_data = (void *) ((char *) arc + offset + sizeof (uint64_t));
727 fp = ctf_bufopen (&ctfsect, symsect, strsect, errp);
728 if (fp)
53651de8
NA
729 {
730 ctf_setmodel (fp, le64toh (arc->ctfa_model));
731 if (little_endian >= 0)
732 ctf_symsect_endianness (fp, little_endian);
733 }
9402cc59
NA
734 return fp;
735}
736
ae41200b
NA
737/* Backward compatibility. */
738ctf_dict_t *
739ctf_arc_open_by_name (const ctf_archive_t *arc, const char *name,
740 int *errp)
741{
742 return ctf_dict_open (arc, name, errp);
743}
744
745ctf_dict_t *
746ctf_arc_open_by_name_sections (const ctf_archive_t *arc,
747 const ctf_sect_t *symsect,
748 const ctf_sect_t *strsect,
749 const char *name,
750 int *errp)
751{
752 return ctf_dict_open_sections (arc, symsect, strsect, name, errp);
753}
754
2c78e925
NA
755/* Import the parent into a ctf archive, if this is a child, the parent is not
756 already set, and a suitable archive member exists. No error is raised if
757 this is not possible: this is just a best-effort helper operation to give
758 people useful dicts to start with. */
61914bb6
NA
759static int
760ctf_arc_import_parent (const ctf_archive_t *arc, ctf_dict_t *fp, int *errp)
2c78e925
NA
761{
762 if ((fp->ctf_flags & LCTF_CHILD) && fp->ctf_parname && !fp->ctf_parent)
763 {
61914bb6 764 int err;
2c78e925 765 ctf_dict_t *parent = ctf_dict_open_cached ((ctf_archive_t *) arc,
61914bb6
NA
766 fp->ctf_parname, &err);
767 if (errp)
768 *errp = err;
769
2c78e925
NA
770 if (parent)
771 {
772 ctf_import (fp, parent);
773 ctf_dict_close (parent);
774 }
61914bb6
NA
775 else if (err != ECTF_ARNNAME)
776 return -1; /* errno is set for us. */
2c78e925 777 }
61914bb6 778 return 0;
2c78e925
NA
779}
780
9c23dfa5
NA
781/* Return the number of members in an archive. */
782size_t
783ctf_archive_count (const ctf_archive_t *wrapper)
784{
785 if (!wrapper->ctfi_is_archive)
786 return 1;
787
86fd34fd 788 return le64toh (wrapper->ctfi_archive->ctfa_ndicts);
9c23dfa5
NA
789}
790
f4f60336
NA
791/* Look up a symbol in an archive by name or index (if the name is set, a lookup
792 by name is done). Return the dict in the archive that the symbol is found
793 in, and (optionally) the ctf_id_t of the symbol in that dict (so you don't
794 have to look it up yourself). The dict is cached, so repeated lookups are
795 nearly free.
2c78e925
NA
796
797 As usual, you should ctf_dict_close() the returned dict once you are done
798 with it.
799
800 Returns NULL on error, and an error in errp (if set). */
801
f4f60336
NA
802static ctf_dict_t *
803ctf_arc_lookup_sym_or_name (ctf_archive_t *wrapper, unsigned long symidx,
804 const char *symname, ctf_id_t *typep, int *errp)
2c78e925
NA
805{
806 ctf_dict_t *fp;
f4f60336 807 void *fpkey;
2c78e925
NA
808 ctf_id_t type;
809
810 /* The usual non-archive-transparent-wrapper special case. */
811 if (!wrapper->ctfi_is_archive)
812 {
f4f60336 813 if (!symname)
2c78e925 814 {
f4f60336
NA
815 if ((type = ctf_lookup_by_symbol (wrapper->ctfi_dict, symidx)) == CTF_ERR)
816 {
817 if (errp)
818 *errp = ctf_errno (wrapper->ctfi_dict);
819 return NULL;
820 }
821 }
822 else
823 {
824 if ((type = ctf_lookup_by_symbol_name (wrapper->ctfi_dict,
825 symname)) == CTF_ERR)
826 {
827 if (errp)
828 *errp = ctf_errno (wrapper->ctfi_dict);
829 return NULL;
830 }
2c78e925
NA
831 }
832 if (typep)
833 *typep = type;
834 wrapper->ctfi_dict->ctf_refcnt++;
835 return wrapper->ctfi_dict;
836 }
837
838 if (wrapper->ctfi_symsect.cts_name == NULL
839 || wrapper->ctfi_symsect.cts_data == NULL
840 || wrapper->ctfi_symsect.cts_size == 0
841 || wrapper->ctfi_symsect.cts_entsize == 0)
842 {
843 if (errp)
844 *errp = ECTF_NOSYMTAB;
845 return NULL;
846 }
847
f4f60336
NA
848 /* Make enough space for all possible symbol indexes, if not already done. We
849 cache the originating dictionary of all symbols. The dict links are weak,
850 to the dictionaries cached in ctfi_dicts: their refcnts are *not* bumped.
851 We also cache similar mappings for symbol names: these are ordinary
852 dynhashes, with weak links to dicts. */
2c78e925 853
f4f60336 854 if (!wrapper->ctfi_symdicts)
2c78e925 855 {
f4f60336
NA
856 if ((wrapper->ctfi_symdicts = calloc (wrapper->ctfi_symsect.cts_size
857 / wrapper->ctfi_symsect.cts_entsize,
858 sizeof (ctf_dict_t *))) == NULL)
2c78e925
NA
859 {
860 if (errp)
861 *errp = ENOMEM;
862 return NULL;
863 }
864 }
f4f60336 865 if (!wrapper->ctfi_symnamedicts)
2c78e925 866 {
f4f60336
NA
867 if ((wrapper->ctfi_symnamedicts = ctf_dynhash_create (ctf_hash_string,
868 ctf_hash_eq_string,
869 free, NULL)) == NULL)
2c78e925
NA
870 {
871 if (errp)
872 *errp = ENOMEM;
873 return NULL;
874 }
875 }
876
f4f60336
NA
877 /* Perhaps the dict in which we found a previous lookup is cached. If it's
878 supposed to be cached but we don't find it, pretend it was always not
879 found: this should never happen, but shouldn't be allowed to cause trouble
880 if it does. */
881
882 if ((symname && ctf_dynhash_lookup_kv (wrapper->ctfi_symnamedicts,
883 symname, NULL, &fpkey))
884 || (!symname && wrapper->ctfi_symdicts[symidx] != NULL))
2c78e925 885 {
f4f60336
NA
886 if (symname)
887 fp = (ctf_dict_t *) fpkey;
888 else
889 fp = wrapper->ctfi_symdicts[symidx];
890
891 if (fp == &enosym)
892 goto no_sym;
893
894 if (symname)
2c78e925 895 {
f4f60336
NA
896 if ((type = ctf_lookup_by_symbol_name (fp, symname)) == CTF_ERR)
897 goto cache_no_sym;
898 }
899 else
900 {
901 if ((type = ctf_lookup_by_symbol (fp, symidx)) == CTF_ERR)
902 goto cache_no_sym;
2c78e925
NA
903 }
904
905 if (typep)
f4f60336
NA
906 *typep = type;
907 fp->ctf_refcnt++;
908 return fp;
2c78e925
NA
909 }
910
911 /* Not cached: find it and cache it. We must track open errors ourselves even
912 if our caller doesn't, to be able to distinguish no-error end-of-iteration
913 from open errors. */
914
915 int local_err;
916 int *local_errp;
917 ctf_next_t *i = NULL;
918 const char *name;
919
920 if (errp)
921 local_errp = errp;
922 else
923 local_errp = &local_err;
924
925 while ((fp = ctf_archive_next (wrapper, &i, &name, 0, local_errp)) != NULL)
926 {
f4f60336 927 if (!symname)
2c78e925 928 {
f4f60336
NA
929 if ((type = ctf_lookup_by_symbol (fp, symidx)) != CTF_ERR)
930 wrapper->ctfi_symdicts[symidx] = fp;
931 }
932 else
933 {
934 if ((type = ctf_lookup_by_symbol_name (fp, symname)) != CTF_ERR)
935 {
936 char *tmp;
937 /* No error checking, as above. */
938 if ((tmp = strdup (symname)) != NULL)
939 ctf_dynhash_insert (wrapper->ctfi_symnamedicts, tmp, fp);
940 }
941 }
2c78e925 942
f4f60336
NA
943 if (type != CTF_ERR)
944 {
2c78e925
NA
945 if (typep)
946 *typep = type;
f4f60336 947 ctf_next_destroy (i);
2c78e925
NA
948 return fp;
949 }
f4f60336
NA
950 if (ctf_errno (fp) != ECTF_NOTYPEDAT)
951 {
952 if (errp)
953 *errp = ctf_errno (fp);
e3cd5660 954 ctf_dict_close (fp);
f4f60336
NA
955 ctf_next_destroy (i);
956 return NULL; /* errno is set for us. */
957 }
2c78e925
NA
958 ctf_dict_close (fp);
959 }
960 if (*local_errp != ECTF_NEXT_END)
961 {
962 ctf_next_destroy (i);
963 return NULL;
964 }
f4f60336 965
2c78e925
NA
966 /* Don't leak end-of-iteration to the caller. */
967 *local_errp = 0;
968
f4f60336
NA
969 cache_no_sym:
970 if (!symname)
971 wrapper->ctfi_symdicts[symidx] = &enosym;
972 else
973 {
974 char *tmp;
975
976 /* No error checking: if caching fails, there is only a slight performance
977 impact. */
978 if ((tmp = strdup (symname)) != NULL)
979 if (ctf_dynhash_insert (wrapper->ctfi_symnamedicts, tmp, &enosym) < 0)
980 free (tmp);
981 }
2c78e925 982
f4f60336 983 no_sym:
2c78e925
NA
984 if (errp)
985 *errp = ECTF_NOTYPEDAT;
986 if (typep)
987 *typep = CTF_ERR;
988 return NULL;
989}
990
f4f60336
NA
991/* The public API for looking up a symbol by index. */
992ctf_dict_t *
993ctf_arc_lookup_symbol (ctf_archive_t *wrapper, unsigned long symidx,
994 ctf_id_t *typep, int *errp)
995{
996 return ctf_arc_lookup_sym_or_name (wrapper, symidx, NULL, typep, errp);
997}
998
999/* The public API for looking up a symbol by name. */
1000
1001ctf_dict_t *
1002ctf_arc_lookup_symbol_name (ctf_archive_t *wrapper, const char *symname,
1003 ctf_id_t *typep, int *errp)
1004{
1005 return ctf_arc_lookup_sym_or_name (wrapper, 0, symname, typep, errp);
1006}
1007
2fa4b6e6
NA
1008/* Return all enumeration constants with a given NAME across all dicts in an
1009 archive, similar to ctf_lookup_enumerator_next. The DICT is cached, so
1010 opening costs are paid only once, but (unlike ctf_arc_lookup_symbol*
1011 above) the results of the iterations are not cached. dict and errp are
1012 not optional. */
1013
1014ctf_id_t
1015ctf_arc_lookup_enumerator_next (ctf_archive_t *arc, const char *name,
1016 ctf_next_t **it, int64_t *enum_value,
1017 ctf_dict_t **dict, int *errp)
1018{
1019 ctf_next_t *i = *it;
1020 ctf_id_t type;
1021 int opened_this_time = 0;
1022 int err;
1023
1024 /* We have two nested iterators in here: ctn_next tracks archives, while
1025 within it ctn_next_inner tracks enumerators within an archive. We
1026 keep track of the dict by simply reusing the passed-in arg: if it's
1027 changed by the caller, the caller will get an ECTF_WRONGFP error,
1028 so this is quite safe and means we don't have to track the arc and fp
1029 simultaneously in the ctf_next_t. */
1030
1031 if (!i)
1032 {
1033 if ((i = ctf_next_create ()) == NULL)
1034 {
1035 err = ENOMEM;
1036 goto err;
1037 }
1038 i->ctn_iter_fun = (void (*) (void)) ctf_arc_lookup_enumerator_next;
1039 i->cu.ctn_arc = arc;
1040 *it = i;
1041 }
1042
1043 if ((void (*) (void)) ctf_arc_lookup_enumerator_next != i->ctn_iter_fun)
1044 {
1045 err = ECTF_NEXT_WRONGFUN;
1046 goto err;
1047 }
1048
1049 if (arc != i->cu.ctn_arc)
1050 {
1051 err = ECTF_NEXT_WRONGFP;
1052 goto err;
1053 }
1054
1055 /* Prevent any earlier end-of-iteration on this dict from confusing the
1056 test below. */
1057 if (i->ctn_next != NULL)
1058 ctf_set_errno (*dict, 0);
1059
1060 do
1061 {
1062 /* At end of one dict, or not started any iterations yet?
1063 Traverse to next dict. If we never returned this dict to the
1064 caller, close it ourselves: the caller will never see it and cannot
1065 do so. */
1066
1067 if (i->ctn_next == NULL || ctf_errno (*dict) == ECTF_NEXT_END)
1068 {
1069 if (opened_this_time)
1070 {
1071 ctf_dict_close (*dict);
1072 *dict = NULL;
1073 opened_this_time = 0;
1074 }
1075
1076 *dict = ctf_archive_next (arc, &i->ctn_next, NULL, 0, &err);
1077 if (!*dict)
1078 goto err;
1079 opened_this_time = 1;
1080 }
1081
1082 type = ctf_lookup_enumerator_next (*dict, name, &i->ctn_next_inner,
1083 enum_value);
1084 }
1085 while (type == CTF_ERR && ctf_errno (*dict) == ECTF_NEXT_END);
1086
1087 if (type == CTF_ERR)
1088 {
1089 err = ctf_errno (*dict);
1090 goto err;
1091 }
1092
1093 /* If this dict is being reused from the previous iteration, bump its
1094 refcnt: the caller is going to close it and has no idea that we didn't
1095 open it this time round. */
1096 if (!opened_this_time)
1097 ctf_ref (*dict);
1098
1099 return type;
1100
1101 err: /* Also ECTF_NEXT_END. */
1102 if (opened_this_time)
1103 {
1104 ctf_dict_close (*dict);
1105 *dict = NULL;
1106 }
1107
1108 ctf_next_destroy (i);
1109 *it = NULL;
1110 if (errp)
1111 *errp = err;
1112 return CTF_ERR;
1113}
1114
9402cc59
NA
1115/* Raw iteration over all CTF files in an archive. We pass the raw data for all
1116 CTF files in turn to the specified callback function. */
1117static int
1118ctf_archive_raw_iter_internal (const struct ctf_archive *arc,
1119 ctf_archive_raw_member_f *func, void *data)
1120{
1121 int rc;
1122 size_t i;
1123 struct ctf_archive_modent *modent;
1124 const char *nametbl;
1125
1126 modent = (ctf_archive_modent_t *) ((char *) arc
1127 + sizeof (struct ctf_archive));
1128 nametbl = (((const char *) arc) + le64toh (arc->ctfa_names));
1129
139633c3 1130 for (i = 0; i < le64toh (arc->ctfa_ndicts); i++)
9402cc59
NA
1131 {
1132 const char *name;
1133 char *fp;
1134
1135 name = &nametbl[le64toh (modent[i].name_offset)];
1136 fp = ((char *) arc + le64toh (arc->ctfa_ctfs)
1137 + le64toh (modent[i].ctf_offset));
1138
1139 if ((rc = func (name, (void *) (fp + sizeof (uint64_t)),
1140 le64toh (*((uint64_t *) fp)), data)) != 0)
1141 return rc;
1142 }
1143 return 0;
1144}
1145
1146/* Raw iteration over all CTF files in an archive: public entry point.
1147
1148 Returns -EINVAL if not supported for this sort of archive. */
1149int
1150ctf_archive_raw_iter (const ctf_archive_t *arc,
1151 ctf_archive_raw_member_f * func, void *data)
1152{
1153 if (arc->ctfi_is_archive)
1154 return ctf_archive_raw_iter_internal (arc->ctfi_archive, func, data);
1155
1156 return -EINVAL; /* Not supported. */
1157}
1158
ac36e134
NA
1159/* Iterate over all CTF files in an archive: public entry point. We pass all
1160 CTF files in turn to the specified callback function. */
1161int
1162ctf_archive_iter (const ctf_archive_t *arc, ctf_archive_member_f *func,
1163 void *data)
9402cc59 1164{
ac36e134
NA
1165 ctf_next_t *i = NULL;
1166 ctf_dict_t *fp;
1167 const char *name;
2dd3fd0d 1168 int err = 0;
9402cc59 1169
ac36e134 1170 while ((fp = ctf_archive_next (arc, &i, &name, 0, &err)) != NULL)
9402cc59 1171 {
ac36e134 1172 int rc;
9402cc59 1173
ac36e134 1174 if ((rc = func (fp, name, data)) != 0)
9402cc59 1175 {
ac36e134
NA
1176 ctf_dict_close (fp);
1177 ctf_next_destroy (i);
9402cc59
NA
1178 return rc;
1179 }
ac36e134 1180 ctf_dict_close (fp);
9402cc59 1181 }
2dd3fd0d
NA
1182 if (err != ECTF_NEXT_END && err != 0)
1183 {
1184 ctf_next_destroy (i);
1185 return -1;
1186 }
9402cc59
NA
1187 return 0;
1188}
1189
688d28f6 1190/* Iterate over all CTF files in an archive, returning each dict in turn as a
139633c3 1191 ctf_dict_t, and NULL on error or end of iteration. It is the caller's
8769046e 1192 responsibility to close it. Parent dicts may be skipped.
688d28f6 1193
2c78e925
NA
1194 The archive member is cached for rapid return on future calls.
1195
688d28f6
NA
1196 We identify parents by name rather than by flag value: for now, with the
1197 linker only emitting parents named _CTF_SECTION, this works well enough. */
1198
139633c3 1199ctf_dict_t *
688d28f6
NA
1200ctf_archive_next (const ctf_archive_t *wrapper, ctf_next_t **it, const char **name,
1201 int skip_parent, int *errp)
1202{
139633c3 1203 ctf_dict_t *f;
688d28f6
NA
1204 ctf_next_t *i = *it;
1205 struct ctf_archive *arc;
1206 struct ctf_archive_modent *modent;
1207 const char *nametbl;
1208 const char *name_;
1209
1210 if (!i)
1211 {
1212 if ((i = ctf_next_create()) == NULL)
1213 {
1214 if (errp)
1215 *errp = ENOMEM;
1216 return NULL;
1217 }
1218 i->cu.ctn_arc = wrapper;
1219 i->ctn_iter_fun = (void (*) (void)) ctf_archive_next;
1220 *it = i;
1221 }
1222
1223 if ((void (*) (void)) ctf_archive_next != i->ctn_iter_fun)
1224 {
1225 if (errp)
1226 *errp = ECTF_NEXT_WRONGFUN;
1227 return NULL;
1228 }
1229
1230 if (wrapper != i->cu.ctn_arc)
1231 {
1232 if (errp)
1233 *errp = ECTF_NEXT_WRONGFP;
1234 return NULL;
1235 }
1236
139633c3 1237 /* Iteration is made a bit more complex by the need to handle ctf_dict_t's
688d28f6
NA
1238 transparently wrapped in a single-member archive. These are parents: if
1239 skip_parent is on, they are skipped and the iterator terminates
1240 immediately. */
1241
1242 if (!wrapper->ctfi_is_archive && i->ctn_n == 0)
1243 {
1244 i->ctn_n++;
1245 if (!skip_parent)
1246 {
139633c3 1247 wrapper->ctfi_dict->ctf_refcnt++;
eaa2913a
NA
1248 if (name)
1249 *name = _CTF_SECTION;
139633c3 1250 return wrapper->ctfi_dict;
688d28f6
NA
1251 }
1252 }
1253
1254 arc = wrapper->ctfi_archive;
1255
1256 /* The loop keeps going when skip_parent is on as long as the member we find
1257 is the parent (i.e. at most two iterations, but possibly an early return if
1258 *all* we have is a parent). */
1259
688d28f6
NA
1260 do
1261 {
139633c3 1262 if ((!wrapper->ctfi_is_archive) || (i->ctn_n >= le64toh (arc->ctfa_ndicts)))
688d28f6
NA
1263 {
1264 ctf_next_destroy (i);
1265 *it = NULL;
1266 if (errp)
1267 *errp = ECTF_NEXT_END;
1268 return NULL;
1269 }
1270
688d28f6
NA
1271 modent = (ctf_archive_modent_t *) ((char *) arc
1272 + sizeof (struct ctf_archive));
1273 nametbl = (((const char *) arc) + le64toh (arc->ctfa_names));
1274
1275 name_ = &nametbl[le64toh (modent[i->ctn_n].name_offset)];
1276 i->ctn_n++;
eefe721e
NA
1277 }
1278 while (skip_parent && strcmp (name_, _CTF_SECTION) == 0);
688d28f6
NA
1279
1280 if (name)
1281 *name = name_;
1282
2c78e925 1283 f = ctf_dict_open_cached ((ctf_archive_t *) wrapper, name_, errp);
688d28f6
NA
1284 return f;
1285}
1286
9402cc59
NA
1287#ifdef HAVE_MMAP
1288/* Map the header in. Only used on new, empty files. */
1289static void *arc_mmap_header (int fd, size_t headersz)
1290{
1291 void *hdr;
1292 if ((hdr = mmap (NULL, headersz, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1293 0)) == MAP_FAILED)
1294 return NULL;
1295 return hdr;
1296}
1297
1298/* mmap() the whole file, for reading only. (Map it writably, but privately: we
1299 need to modify the region, but don't need anyone else to see the
1300 modifications.) */
1301static void *arc_mmap_file (int fd, size_t size)
1302{
1303 void *arc;
1304 if ((arc = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
1305 fd, 0)) == MAP_FAILED)
1306 return NULL;
1307 return arc;
1308}
1309
1310/* Persist the header to disk. */
1311static int arc_mmap_writeout (int fd _libctf_unused_, void *header,
1312 size_t headersz, const char **errmsg)
1313{
1314 if (msync (header, headersz, MS_ASYNC) < 0)
1315 {
1316 if (errmsg)
926c9e76
NA
1317 *errmsg = N_("arc_mmap_writeout(): cannot sync after writing "
1318 "to %s: %s");
9402cc59
NA
1319 return -1;
1320 }
1321 return 0;
1322}
1323
1324/* Unmap the region. */
1325static int arc_mmap_unmap (void *header, size_t headersz, const char **errmsg)
1326{
1327 if (munmap (header, headersz) < 0)
1328 {
1329 if (errmsg)
926c9e76
NA
1330 *errmsg = N_("arc_mmap_munmap(): cannot unmap after writing "
1331 "to %s: %s");
9402cc59
NA
1332 return -1;
1333 }
1334 return 0;
1335}
1336#else
1337/* Map the header in. Only used on new, empty files. */
f5e73be1 1338static void *arc_mmap_header (int fd _libctf_unused_, size_t headersz)
9402cc59
NA
1339{
1340 void *hdr;
1341 if ((hdr = malloc (headersz)) == NULL)
1342 return NULL;
1343 return hdr;
1344}
1345
1346/* Pull in the whole file, for reading only. We assume the current file
1347 position is at the start of the file. */
1348static void *arc_mmap_file (int fd, size_t size)
1349{
1350 char *data;
1351
1352 if ((data = malloc (size)) == NULL)
1353 return NULL;
1354
1355 if (ctf_pread (fd, data, size, 0) < 0)
1356 {
1357 free (data);
1358 return NULL;
1359 }
1360 return data;
1361}
1362
1363/* Persist the header to disk. */
1364static int arc_mmap_writeout (int fd, void *header, size_t headersz,
1365 const char **errmsg)
1366{
1367 ssize_t len;
9402cc59
NA
1368 char *data = (char *) header;
1369 ssize_t count = headersz;
1370
1371 if ((lseek (fd, 0, SEEK_SET)) < 0)
1372 {
1373 if (errmsg)
926c9e76
NA
1374 *errmsg = N_("arc_mmap_writeout(): cannot seek while writing header to "
1375 "%s: %s");
9402cc59
NA
1376 return -1;
1377 }
1378
1379 while (headersz > 0)
1380 {
1381 if ((len = write (fd, data, count)) < 0)
1382 {
1383 if (errmsg)
926c9e76 1384 *errmsg = N_("arc_mmap_writeout(): cannot write header to %s: %s");
9402cc59
NA
1385 return len;
1386 }
1387 if (len == EINTR)
1388 continue;
1389
9402cc59
NA
1390 if (len == 0) /* EOF. */
1391 break;
1392
1393 count -= len;
1394 data += len;
1395 }
1396 return 0;
1397}
1398
1399/* Unmap the region. */
1400static int arc_mmap_unmap (void *header, size_t headersz _libctf_unused_,
1401 const char **errmsg _libctf_unused_)
1402{
1403 free (header);
1404 return 0;
1405}
1406#endif