2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
4 This file is part of libctf.
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
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.
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/>. */
21 #include <sys/types.h>
24 #include "ctf-endian.h"
35 static off_t
arc_write_one_ctf (ctf_dict_t
* f
, int fd
, size_t threshold
);
36 static 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
,
39 size_t offset
, int little_endian
,
41 static int sort_modent_by_name (const void *one
, const void *two
, void *n
);
42 static void *arc_mmap_header (int fd
, size_t headersz
);
43 static void *arc_mmap_file (int fd
, size_t size
);
44 static int arc_mmap_writeout (int fd
, void *header
, size_t headersz
,
46 static int arc_mmap_unmap (void *header
, size_t headersz
, const char **errmsg
);
47 static void ctf_arc_import_parent (const ctf_archive_t
*arc
, ctf_dict_t
*fp
);
49 /* Flag to indicate "symbol not present" in ctf_archive_internal.ctfi_symdicts
50 and ctfi_symnamedicts. Never initialized. */
51 static ctf_dict_t enosym
;
53 /* Write out a CTF archive to the start of the file referenced by the passed-in
54 fd. The entries in CTF_DICTS are referenced by name: the names are passed in
55 the names array, which must have CTF_DICTS entries.
57 Returns 0 on success, or an errno, or an ECTF_* value. */
59 ctf_arc_write_fd (int fd
, ctf_dict_t
**ctf_dicts
, size_t ctf_dict_cnt
,
60 const char **names
, size_t threshold
)
63 struct ctf_archive
*archdr
;
68 size_t ctf_startoffs
; /* Start of the section we are working over. */
69 char *nametbl
= NULL
; /* The name table. */
72 struct ctf_archive_modent
*modent
;
74 ctf_dprintf ("Writing CTF archive with %lu files\n",
75 (unsigned long) ctf_dict_cnt
);
77 /* Figure out the size of the mmap()ed header, including the
78 ctf_archive_modent array. We assume that all of this needs no
79 padding: a likely assumption, given that it's all made up of
81 headersz
= sizeof (struct ctf_archive
)
82 + (ctf_dict_cnt
* sizeof (uint64_t) * 2);
83 ctf_dprintf ("headersz is %lu\n", (unsigned long) headersz
);
85 /* From now on we work in two pieces: an mmap()ed region from zero up to the
86 headersz, and a region updated via write() starting after that, containing
87 all the tables. Platforms that do not support mmap() just use write(). */
88 ctf_startoffs
= headersz
;
89 if (lseek (fd
, ctf_startoffs
- 1, SEEK_SET
) < 0)
91 errmsg
= N_("ctf_arc_write(): cannot extend file while writing");
95 if (write (fd
, &dummy
, 1) < 0)
97 errmsg
= N_("ctf_arc_write(): cannot extend file while writing");
101 if ((archdr
= arc_mmap_header (fd
, headersz
)) == NULL
)
103 errmsg
= N_("ctf_arc_write(): cannot mmap");
107 /* Fill in everything we can, which is everything other than the name
109 archdr
->ctfa_magic
= htole64 (CTFA_MAGIC
);
110 archdr
->ctfa_ndicts
= htole64 (ctf_dict_cnt
);
111 archdr
->ctfa_ctfs
= htole64 (ctf_startoffs
);
113 /* We could validate that all CTF files have the same data model, but
114 since any reasonable construction process will be building things of
115 only one bitness anyway, this is pretty pointless, so just use the
116 model of the first CTF file for all of them. (It *is* valid to
117 create an empty archive: the value of ctfa_model is irrelevant in
118 this case, but we must be sure not to dereference uninitialized
121 if (ctf_dict_cnt
> 0)
122 archdr
->ctfa_model
= htole64 (ctf_getmodel (ctf_dicts
[0]));
124 /* Now write out the CTFs: ctf_archive_modent array via the mapping,
125 ctfs via write(). The names themselves have not been written yet: we
126 track them in a local strtab until the time is right, and sort the
127 modents array after construction.
129 The name table is not sorted. */
131 for (i
= 0, namesz
= 0; i
< le64toh (archdr
->ctfa_ndicts
); i
++)
132 namesz
+= strlen (names
[i
]) + 1;
134 nametbl
= malloc (namesz
);
137 errmsg
= N_("ctf_arc_write(): error writing named CTF to archive");
141 for (i
= 0, namesz
= 0,
142 modent
= (ctf_archive_modent_t
*) ((char *) archdr
143 + sizeof (struct ctf_archive
));
144 i
< le64toh (archdr
->ctfa_ndicts
); i
++)
148 strcpy (&nametbl
[namesz
], names
[i
]);
150 off
= arc_write_one_ctf (ctf_dicts
[i
], fd
, threshold
);
151 if ((off
< 0) && (off
> -ECTF_BASE
))
153 errmsg
= N_("ctf_arc_write(): cannot determine file "
154 "position while writing to archive");
159 errmsg
= N_("ctf_arc_write(): cannot write CTF file to archive");
164 modent
->name_offset
= htole64 (namesz
);
165 modent
->ctf_offset
= htole64 (off
- ctf_startoffs
);
166 namesz
+= strlen (names
[i
]) + 1;
170 ctf_qsort_r ((ctf_archive_modent_t
*) ((char *) archdr
171 + sizeof (struct ctf_archive
)),
172 le64toh (archdr
->ctfa_ndicts
),
173 sizeof (struct ctf_archive_modent
), sort_modent_by_name
,
176 /* Now the name table. */
178 if ((nameoffs
= lseek (fd
, 0, SEEK_CUR
)) < 0)
180 errmsg
= N_("ctf_arc_write(): cannot get current file position "
184 archdr
->ctfa_names
= htole64 (nameoffs
);
189 if ((len
= write (fd
, np
, namesz
)) < 0)
191 errmsg
= N_("ctf_arc_write(): cannot write name table to archive");
199 if (arc_mmap_writeout (fd
, archdr
, headersz
, &errmsg
) < 0)
201 if (arc_mmap_unmap (archdr
, headersz
, &errmsg
) < 0)
208 arc_mmap_unmap (archdr
, headersz
, NULL
);
210 /* We report errors into the first file in the archive, if any: if this is a
211 zero-file archive, put it in the open-errors stream for lack of anywhere
212 else for it to go. */
213 ctf_err_warn (ctf_dict_cnt
> 0 ? ctf_dicts
[0] : NULL
, 0, errno
, "%s",
218 /* Write out a CTF archive. The entries in CTF_DICTS are referenced by name:
219 the names are passed in the names array, which must have CTF_DICTS entries.
221 If the filename is NULL, create a temporary file and return a pointer to it.
223 Returns 0 on success, or an errno, or an ECTF_* value. */
225 ctf_arc_write (const char *file
, ctf_dict_t
**ctf_dicts
, size_t ctf_dict_cnt
,
226 const char **names
, size_t threshold
)
231 if ((fd
= open (file
, O_RDWR
| O_CREAT
| O_TRUNC
| O_CLOEXEC
, 0666)) < 0)
233 ctf_err_warn (ctf_dict_cnt
> 0 ? ctf_dicts
[0] : NULL
, 0, errno
,
234 _("ctf_arc_write(): cannot create %s"), file
);
238 err
= ctf_arc_write_fd (fd
, ctf_dicts
, ctf_dict_cnt
, names
, threshold
);
242 if ((err
= close (fd
)) < 0)
243 ctf_err_warn (ctf_dict_cnt
> 0 ? ctf_dicts
[0] : NULL
, 0, errno
,
244 _("ctf_arc_write(): cannot close after writing to archive"));
256 /* Write one CTF file out. Return the file position of the written file (or
257 rather, of the file-size uint64_t that precedes it): negative return is a
258 negative errno or ctf_errno value. On error, the file position may no longer
259 be at the end of the file. */
261 arc_write_one_ctf (ctf_dict_t
* f
, int fd
, size_t threshold
)
267 int (*writefn
) (ctf_dict_t
* fp
, int fd
);
269 if (ctf_serialize (f
) < 0)
270 return f
->ctf_errno
* -1;
272 if ((off
= lseek (fd
, 0, SEEK_CUR
)) < 0)
275 if (f
->ctf_size
> threshold
)
276 writefn
= ctf_compress_write
;
280 /* This zero-write turns into the size in a moment. */
281 ctfsz_len
= sizeof (ctfsz
);
282 ctfszp
= (char *) &ctfsz
;
283 while (ctfsz_len
> 0)
285 ssize_t writelen
= write (fd
, ctfszp
, ctfsz_len
);
288 ctfsz_len
-= writelen
;
292 if (writefn (f
, fd
) != 0)
293 return f
->ctf_errno
* -1;
295 if ((end_off
= lseek (fd
, 0, SEEK_CUR
)) < 0)
297 ctfsz
= htole64 (end_off
- off
);
299 if ((lseek (fd
, off
, SEEK_SET
)) < 0)
303 ctfsz_len
= sizeof (ctfsz
);
304 ctfszp
= (char *) &ctfsz
;
305 while (ctfsz_len
> 0)
307 ssize_t writelen
= write (fd
, ctfszp
, ctfsz_len
);
310 ctfsz_len
-= writelen
;
314 end_off
= LCTF_ALIGN_OFFS (end_off
, 8);
315 if ((lseek (fd
, end_off
, SEEK_SET
)) < 0)
321 /* qsort() function to sort the array of struct ctf_archive_modents into
322 ascending name order. */
324 sort_modent_by_name (const void *one
, const void *two
, void *n
)
326 const struct ctf_archive_modent
*a
= one
;
327 const struct ctf_archive_modent
*b
= two
;
330 return strcmp (&nametbl
[le64toh (a
->name_offset
)],
331 &nametbl
[le64toh (b
->name_offset
)]);
334 /* bsearch_r() function to search for a given name in the sorted array of struct
335 ctf_archive_modents. */
337 search_modent_by_name (const void *key
, const void *ent
, void *arg
)
340 const struct ctf_archive_modent
*v
= ent
;
341 const char *search_nametbl
= arg
;
343 return strcmp (k
, &search_nametbl
[le64toh (v
->name_offset
)]);
346 /* Make a new struct ctf_archive_internal wrapper for a ctf_archive or a
347 ctf_dict. Closes ARC and/or FP on error. Arrange to free the SYMSECT or
348 STRSECT, as needed, on close. Possibly do not unmap on close. */
350 struct ctf_archive_internal
*
351 ctf_new_archive_internal (int is_archive
, int unmap_on_close
,
352 struct ctf_archive
*arc
,
353 ctf_dict_t
*fp
, const ctf_sect_t
*symsect
,
354 const ctf_sect_t
*strsect
,
357 struct ctf_archive_internal
*arci
;
359 if ((arci
= calloc (1, sizeof (struct ctf_archive_internal
))) == NULL
)
364 ctf_arc_close_internal (arc
);
368 return (ctf_set_open_errno (errp
, errno
));
370 arci
->ctfi_is_archive
= is_archive
;
372 arci
->ctfi_archive
= arc
;
374 arci
->ctfi_dict
= fp
;
376 memcpy (&arci
->ctfi_symsect
, symsect
, sizeof (struct ctf_sect
));
378 memcpy (&arci
->ctfi_strsect
, strsect
, sizeof (struct ctf_sect
));
379 arci
->ctfi_free_symsect
= 0;
380 arci
->ctfi_free_strsect
= 0;
381 arci
->ctfi_unmap_on_close
= unmap_on_close
;
382 arci
->ctfi_symsect_little_endian
= -1;
387 /* Set the symbol-table endianness of an archive (defaulting the symtab
388 endianness of all ctf_file_t's opened from that archive). */
390 ctf_arc_symsect_endianness (ctf_archive_t
*arc
, int little_endian
)
392 arc
->ctfi_symsect_little_endian
= !!little_endian
;
393 if (!arc
->ctfi_is_archive
)
394 ctf_symsect_endianness (arc
->ctfi_dict
, arc
->ctfi_symsect_little_endian
);
397 /* Get the CTF preamble from data in a buffer, which may be either an archive or
398 a CTF dict. If multiple dicts are present in an archive, the preamble comes
399 from an arbitrary dict. The preamble is a pointer into the ctfsect passed
402 const ctf_preamble_t
*
403 ctf_arc_bufpreamble (const ctf_sect_t
*ctfsect
)
405 if (ctfsect
->cts_data
!= NULL
406 && ctfsect
->cts_size
> sizeof (uint64_t)
407 && (le64toh ((*(uint64_t *) ctfsect
->cts_data
)) == CTFA_MAGIC
))
409 struct ctf_archive
*arc
= (struct ctf_archive
*) ctfsect
->cts_data
;
410 return (const ctf_preamble_t
*) ((char *) arc
+ le64toh (arc
->ctfa_ctfs
)
411 + sizeof (uint64_t));
414 return (const ctf_preamble_t
*) ctfsect
->cts_data
;
417 /* Open a CTF archive or dictionary from data in a buffer (which the caller must
418 preserve until ctf_arc_close() time). Returns the archive, or NULL and an
419 error in *err (if not NULL). */
421 ctf_arc_bufopen (const ctf_sect_t
*ctfsect
, const ctf_sect_t
*symsect
,
422 const ctf_sect_t
*strsect
, int *errp
)
424 struct ctf_archive
*arc
= NULL
;
426 ctf_dict_t
*fp
= NULL
;
428 if (ctfsect
->cts_data
!= NULL
429 && ctfsect
->cts_size
> sizeof (uint64_t)
430 && (le64toh ((*(uint64_t *) ctfsect
->cts_data
)) == CTFA_MAGIC
))
432 /* The archive is mmappable, so this operation is trivial.
434 This buffer is nonmodifiable, so the trick involving mmapping only part
435 of it and storing the length in the magic number is not applicable: so
436 record this fact in the archive-wrapper header. (We cannot record it
437 in the archive, because the archive may very well be a read-only
441 arc
= (struct ctf_archive
*) ctfsect
->cts_data
;
446 if ((fp
= ctf_bufopen (ctfsect
, symsect
, strsect
, errp
)) == NULL
)
448 ctf_err_warn (NULL
, 0, *errp
, _("ctf_arc_bufopen(): cannot open CTF"));
452 return ctf_new_archive_internal (is_archive
, 0, arc
, fp
, symsect
, strsect
,
456 /* Open a CTF archive. Returns the archive, or NULL and an error in *err (if
459 ctf_arc_open_internal (const char *filename
, int *errp
)
464 struct ctf_archive
*arc
; /* (Actually the whole file.) */
467 if ((fd
= open (filename
, O_RDONLY
)) < 0)
469 errmsg
= N_("ctf_arc_open(): cannot open %s");
472 if (fstat (fd
, &s
) < 0)
474 errmsg
= N_("ctf_arc_open(): cannot stat %s");
478 if ((arc
= arc_mmap_file (fd
, s
.st_size
)) == NULL
)
480 errmsg
= N_("ctf_arc_open(): cannot read in %s");
484 if (le64toh (arc
->ctfa_magic
) != CTFA_MAGIC
)
486 errmsg
= N_("ctf_arc_open(): %s: invalid magic number");
491 /* This horrible hack lets us know how much to unmap when the file is
492 closed. (We no longer need the magic number, and the mapping
494 arc
->ctfa_magic
= s
.st_size
;
499 arc_mmap_unmap (arc
, s
.st_size
, NULL
);
505 ctf_err_warn (NULL
, 0, errno
, gettext (errmsg
), filename
);
509 /* Close an archive. */
511 ctf_arc_close_internal (struct ctf_archive
*arc
)
516 /* See the comment in ctf_arc_open(). */
517 arc_mmap_unmap (arc
, arc
->ctfa_magic
, NULL
);
520 /* Public entry point: close an archive, or CTF file. */
522 ctf_arc_close (ctf_archive_t
*arc
)
527 if (arc
->ctfi_is_archive
)
529 if (arc
->ctfi_unmap_on_close
)
530 ctf_arc_close_internal (arc
->ctfi_archive
);
533 ctf_dict_close (arc
->ctfi_dict
);
534 free (arc
->ctfi_symdicts
);
535 free (arc
->ctfi_symnamedicts
);
536 ctf_dynhash_destroy (arc
->ctfi_dicts
);
537 if (arc
->ctfi_free_symsect
)
538 free ((void *) arc
->ctfi_symsect
.cts_data
);
539 if (arc
->ctfi_free_strsect
)
540 free ((void *) arc
->ctfi_strsect
.cts_data
);
541 free (arc
->ctfi_data
);
542 if (arc
->ctfi_bfd_close
)
543 arc
->ctfi_bfd_close (arc
);
547 /* Return the ctf_dict_t with the given name, or NULL if none, setting 'err' if
548 non-NULL. A name of NULL means to open the default file. */
550 ctf_dict_open_internal (const struct ctf_archive
*arc
,
551 const ctf_sect_t
*symsect
,
552 const ctf_sect_t
*strsect
,
553 const char *name
, int little_endian
,
556 struct ctf_archive_modent
*modent
;
557 const char *search_nametbl
;
560 name
= _CTF_SECTION
; /* The default name. */
562 ctf_dprintf ("ctf_dict_open_internal(%s): opening\n", name
);
564 modent
= (ctf_archive_modent_t
*) ((char *) arc
565 + sizeof (struct ctf_archive
));
567 search_nametbl
= (const char *) arc
+ le64toh (arc
->ctfa_names
);
568 modent
= bsearch_r (name
, modent
, le64toh (arc
->ctfa_ndicts
),
569 sizeof (struct ctf_archive_modent
),
570 search_modent_by_name
, (void *) search_nametbl
);
572 /* This is actually a common case and normal operation: no error
577 *errp
= ECTF_ARNNAME
;
581 return ctf_dict_open_by_offset (arc
, symsect
, strsect
,
582 le64toh (modent
->ctf_offset
),
583 little_endian
, errp
);
586 /* Return the ctf_dict_t with the given name, or NULL if none, setting 'err' if
587 non-NULL. A name of NULL means to open the default file.
589 Use the specified string and symbol table sections.
591 Public entry point. */
593 ctf_dict_open_sections (const ctf_archive_t
*arc
,
594 const ctf_sect_t
*symsect
,
595 const ctf_sect_t
*strsect
,
599 if (arc
->ctfi_is_archive
)
602 ret
= ctf_dict_open_internal (arc
->ctfi_archive
, symsect
, strsect
,
603 name
, arc
->ctfi_symsect_little_endian
,
607 ret
->ctf_archive
= (ctf_archive_t
*) arc
;
608 ctf_arc_import_parent (arc
, ret
);
613 if ((name
!= NULL
) && (strcmp (name
, _CTF_SECTION
) != 0))
616 *errp
= ECTF_ARNNAME
;
619 arc
->ctfi_dict
->ctf_archive
= (ctf_archive_t
*) arc
;
621 /* Bump the refcount so that the user can ctf_dict_close() it. */
622 arc
->ctfi_dict
->ctf_refcnt
++;
623 return arc
->ctfi_dict
;
626 /* Return the ctf_dict_t with the given name, or NULL if none, setting 'err' if
627 non-NULL. A name of NULL means to open the default file.
629 Public entry point. */
631 ctf_dict_open (const ctf_archive_t
*arc
, const char *name
, int *errp
)
633 const ctf_sect_t
*symsect
= &arc
->ctfi_symsect
;
634 const ctf_sect_t
*strsect
= &arc
->ctfi_strsect
;
636 if (symsect
->cts_name
== NULL
)
638 if (strsect
->cts_name
== NULL
)
641 return ctf_dict_open_sections (arc
, symsect
, strsect
, name
, errp
);
645 ctf_cached_dict_close (void *fp
)
647 ctf_dict_close ((ctf_dict_t
*) fp
);
650 /* Return the ctf_dict_t with the given name and cache it in the archive's
651 ctfi_dicts. If this is the first cached dict, designate it the
654 ctf_dict_open_cached (ctf_archive_t
*arc
, const char *name
, int *errp
)
659 /* Just return from the cache if possible. */
661 && ((fp
= ctf_dynhash_lookup (arc
->ctfi_dicts
, name
)) != NULL
))
667 /* Not yet cached: open it. */
668 fp
= ctf_dict_open (arc
, name
, errp
);
669 dupname
= strdup (name
);
674 if (arc
->ctfi_dicts
== NULL
)
676 = ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
677 free
, ctf_cached_dict_close
)) == NULL
)
680 if (ctf_dynhash_insert (arc
->ctfi_dicts
, dupname
, fp
) < 0)
684 if (arc
->ctfi_crossdict_cache
== NULL
)
685 arc
->ctfi_crossdict_cache
= fp
;
697 /* Flush any caches the CTF archive may have open. */
699 ctf_arc_flush_caches (ctf_archive_t
*wrapper
)
701 free (wrapper
->ctfi_symdicts
);
702 free (wrapper
->ctfi_symnamedicts
);
703 ctf_dynhash_destroy (wrapper
->ctfi_dicts
);
704 wrapper
->ctfi_symdicts
= NULL
;
705 wrapper
->ctfi_symnamedicts
= NULL
;
706 wrapper
->ctfi_dicts
= NULL
;
707 wrapper
->ctfi_crossdict_cache
= NULL
;
710 /* Return the ctf_dict_t at the given ctfa_ctfs-relative offset, or NULL if
711 none, setting 'err' if non-NULL. */
713 ctf_dict_open_by_offset (const struct ctf_archive
*arc
,
714 const ctf_sect_t
*symsect
,
715 const ctf_sect_t
*strsect
, size_t offset
,
716 int little_endian
, int *errp
)
721 ctf_dprintf ("ctf_dict_open_by_offset(%lu): opening\n", (unsigned long) offset
);
723 memset (&ctfsect
, 0, sizeof (ctf_sect_t
));
725 offset
+= le64toh (arc
->ctfa_ctfs
);
727 ctfsect
.cts_name
= _CTF_SECTION
;
728 ctfsect
.cts_size
= le64toh (*((uint64_t *) ((char *) arc
+ offset
)));
729 ctfsect
.cts_entsize
= 1;
730 ctfsect
.cts_data
= (void *) ((char *) arc
+ offset
+ sizeof (uint64_t));
731 fp
= ctf_bufopen (&ctfsect
, symsect
, strsect
, errp
);
734 ctf_setmodel (fp
, le64toh (arc
->ctfa_model
));
735 if (little_endian
>= 0)
736 ctf_symsect_endianness (fp
, little_endian
);
741 /* Backward compatibility. */
743 ctf_arc_open_by_name (const ctf_archive_t
*arc
, const char *name
,
746 return ctf_dict_open (arc
, name
, errp
);
750 ctf_arc_open_by_name_sections (const ctf_archive_t
*arc
,
751 const ctf_sect_t
*symsect
,
752 const ctf_sect_t
*strsect
,
756 return ctf_dict_open_sections (arc
, symsect
, strsect
, name
, errp
);
759 /* Import the parent into a ctf archive, if this is a child, the parent is not
760 already set, and a suitable archive member exists. No error is raised if
761 this is not possible: this is just a best-effort helper operation to give
762 people useful dicts to start with. */
764 ctf_arc_import_parent (const ctf_archive_t
*arc
, ctf_dict_t
*fp
)
766 if ((fp
->ctf_flags
& LCTF_CHILD
) && fp
->ctf_parname
&& !fp
->ctf_parent
)
768 ctf_dict_t
*parent
= ctf_dict_open_cached ((ctf_archive_t
*) arc
,
769 fp
->ctf_parname
, NULL
);
772 ctf_import (fp
, parent
);
773 ctf_dict_close (parent
);
778 /* Return the number of members in an archive. */
780 ctf_archive_count (const ctf_archive_t
*wrapper
)
782 if (!wrapper
->ctfi_is_archive
)
785 return wrapper
->ctfi_archive
->ctfa_ndicts
;
788 /* Look up a symbol in an archive by name or index (if the name is set, a lookup
789 by name is done). Return the dict in the archive that the symbol is found
790 in, and (optionally) the ctf_id_t of the symbol in that dict (so you don't
791 have to look it up yourself). The dict is cached, so repeated lookups are
794 As usual, you should ctf_dict_close() the returned dict once you are done
797 Returns NULL on error, and an error in errp (if set). */
800 ctf_arc_lookup_sym_or_name (ctf_archive_t
*wrapper
, unsigned long symidx
,
801 const char *symname
, ctf_id_t
*typep
, int *errp
)
807 /* The usual non-archive-transparent-wrapper special case. */
808 if (!wrapper
->ctfi_is_archive
)
812 if ((type
= ctf_lookup_by_symbol (wrapper
->ctfi_dict
, symidx
)) == CTF_ERR
)
815 *errp
= ctf_errno (wrapper
->ctfi_dict
);
821 if ((type
= ctf_lookup_by_symbol_name (wrapper
->ctfi_dict
,
822 symname
)) == CTF_ERR
)
825 *errp
= ctf_errno (wrapper
->ctfi_dict
);
831 wrapper
->ctfi_dict
->ctf_refcnt
++;
832 return wrapper
->ctfi_dict
;
835 if (wrapper
->ctfi_symsect
.cts_name
== NULL
836 || wrapper
->ctfi_symsect
.cts_data
== NULL
837 || wrapper
->ctfi_symsect
.cts_size
== 0
838 || wrapper
->ctfi_symsect
.cts_entsize
== 0)
841 *errp
= ECTF_NOSYMTAB
;
845 /* Make enough space for all possible symbol indexes, if not already done. We
846 cache the originating dictionary of all symbols. The dict links are weak,
847 to the dictionaries cached in ctfi_dicts: their refcnts are *not* bumped.
848 We also cache similar mappings for symbol names: these are ordinary
849 dynhashes, with weak links to dicts. */
851 if (!wrapper
->ctfi_symdicts
)
853 if ((wrapper
->ctfi_symdicts
= calloc (wrapper
->ctfi_symsect
.cts_size
854 / wrapper
->ctfi_symsect
.cts_entsize
,
855 sizeof (ctf_dict_t
*))) == NULL
)
862 if (!wrapper
->ctfi_symnamedicts
)
864 if ((wrapper
->ctfi_symnamedicts
= ctf_dynhash_create (ctf_hash_string
,
866 free
, NULL
)) == NULL
)
874 /* Perhaps the dict in which we found a previous lookup is cached. If it's
875 supposed to be cached but we don't find it, pretend it was always not
876 found: this should never happen, but shouldn't be allowed to cause trouble
879 if ((symname
&& ctf_dynhash_lookup_kv (wrapper
->ctfi_symnamedicts
,
880 symname
, NULL
, &fpkey
))
881 || (!symname
&& wrapper
->ctfi_symdicts
[symidx
] != NULL
))
884 fp
= (ctf_dict_t
*) fpkey
;
886 fp
= wrapper
->ctfi_symdicts
[symidx
];
893 if ((type
= ctf_lookup_by_symbol_name (fp
, symname
)) == CTF_ERR
)
898 if ((type
= ctf_lookup_by_symbol (fp
, symidx
)) == CTF_ERR
)
908 /* Not cached: find it and cache it. We must track open errors ourselves even
909 if our caller doesn't, to be able to distinguish no-error end-of-iteration
914 ctf_next_t
*i
= NULL
;
920 local_errp
= &local_err
;
922 while ((fp
= ctf_archive_next (wrapper
, &i
, &name
, 0, local_errp
)) != NULL
)
926 if ((type
= ctf_lookup_by_symbol (fp
, symidx
)) != CTF_ERR
)
927 wrapper
->ctfi_symdicts
[symidx
] = fp
;
931 if ((type
= ctf_lookup_by_symbol_name (fp
, symname
)) != CTF_ERR
)
934 /* No error checking, as above. */
935 if ((tmp
= strdup (symname
)) != NULL
)
936 ctf_dynhash_insert (wrapper
->ctfi_symnamedicts
, tmp
, fp
);
944 ctf_next_destroy (i
);
947 if (ctf_errno (fp
) != ECTF_NOTYPEDAT
)
950 *errp
= ctf_errno (fp
);
951 ctf_next_destroy (i
);
952 return NULL
; /* errno is set for us. */
956 if (*local_errp
!= ECTF_NEXT_END
)
958 ctf_next_destroy (i
);
962 /* Don't leak end-of-iteration to the caller. */
967 wrapper
->ctfi_symdicts
[symidx
] = &enosym
;
972 /* No error checking: if caching fails, there is only a slight performance
974 if ((tmp
= strdup (symname
)) != NULL
)
975 if (ctf_dynhash_insert (wrapper
->ctfi_symnamedicts
, tmp
, &enosym
) < 0)
981 *errp
= ECTF_NOTYPEDAT
;
987 /* The public API for looking up a symbol by index. */
989 ctf_arc_lookup_symbol (ctf_archive_t
*wrapper
, unsigned long symidx
,
990 ctf_id_t
*typep
, int *errp
)
992 return ctf_arc_lookup_sym_or_name (wrapper
, symidx
, NULL
, typep
, errp
);
995 /* The public API for looking up a symbol by name. */
998 ctf_arc_lookup_symbol_name (ctf_archive_t
*wrapper
, const char *symname
,
999 ctf_id_t
*typep
, int *errp
)
1001 return ctf_arc_lookup_sym_or_name (wrapper
, 0, symname
, typep
, errp
);
1004 /* Raw iteration over all CTF files in an archive. We pass the raw data for all
1005 CTF files in turn to the specified callback function. */
1007 ctf_archive_raw_iter_internal (const struct ctf_archive
*arc
,
1008 ctf_archive_raw_member_f
*func
, void *data
)
1012 struct ctf_archive_modent
*modent
;
1013 const char *nametbl
;
1015 modent
= (ctf_archive_modent_t
*) ((char *) arc
1016 + sizeof (struct ctf_archive
));
1017 nametbl
= (((const char *) arc
) + le64toh (arc
->ctfa_names
));
1019 for (i
= 0; i
< le64toh (arc
->ctfa_ndicts
); i
++)
1024 name
= &nametbl
[le64toh (modent
[i
].name_offset
)];
1025 fp
= ((char *) arc
+ le64toh (arc
->ctfa_ctfs
)
1026 + le64toh (modent
[i
].ctf_offset
));
1028 if ((rc
= func (name
, (void *) (fp
+ sizeof (uint64_t)),
1029 le64toh (*((uint64_t *) fp
)), data
)) != 0)
1035 /* Raw iteration over all CTF files in an archive: public entry point.
1037 Returns -EINVAL if not supported for this sort of archive. */
1039 ctf_archive_raw_iter (const ctf_archive_t
*arc
,
1040 ctf_archive_raw_member_f
* func
, void *data
)
1042 if (arc
->ctfi_is_archive
)
1043 return ctf_archive_raw_iter_internal (arc
->ctfi_archive
, func
, data
);
1045 return -EINVAL
; /* Not supported. */
1048 /* Iterate over all CTF files in an archive: public entry point. We pass all
1049 CTF files in turn to the specified callback function. */
1051 ctf_archive_iter (const ctf_archive_t
*arc
, ctf_archive_member_f
*func
,
1054 ctf_next_t
*i
= NULL
;
1059 while ((fp
= ctf_archive_next (arc
, &i
, &name
, 0, &err
)) != NULL
)
1063 if ((rc
= func (fp
, name
, data
)) != 0)
1065 ctf_dict_close (fp
);
1066 ctf_next_destroy (i
);
1069 ctf_dict_close (fp
);
1074 /* Iterate over all CTF files in an archive, returning each dict in turn as a
1075 ctf_dict_t, and NULL on error or end of iteration. It is the caller's
1076 responsibility to close it. Parent dicts may be skipped.
1078 The archive member is cached for rapid return on future calls.
1080 We identify parents by name rather than by flag value: for now, with the
1081 linker only emitting parents named _CTF_SECTION, this works well enough. */
1084 ctf_archive_next (const ctf_archive_t
*wrapper
, ctf_next_t
**it
, const char **name
,
1085 int skip_parent
, int *errp
)
1088 ctf_next_t
*i
= *it
;
1089 struct ctf_archive
*arc
;
1090 struct ctf_archive_modent
*modent
;
1091 const char *nametbl
;
1096 if ((i
= ctf_next_create()) == NULL
)
1102 i
->cu
.ctn_arc
= wrapper
;
1103 i
->ctn_iter_fun
= (void (*) (void)) ctf_archive_next
;
1107 if ((void (*) (void)) ctf_archive_next
!= i
->ctn_iter_fun
)
1110 *errp
= ECTF_NEXT_WRONGFUN
;
1114 if (wrapper
!= i
->cu
.ctn_arc
)
1117 *errp
= ECTF_NEXT_WRONGFP
;
1121 /* Iteration is made a bit more complex by the need to handle ctf_dict_t's
1122 transparently wrapped in a single-member archive. These are parents: if
1123 skip_parent is on, they are skipped and the iterator terminates
1126 if (!wrapper
->ctfi_is_archive
&& i
->ctn_n
== 0)
1131 wrapper
->ctfi_dict
->ctf_refcnt
++;
1133 *name
= _CTF_SECTION
;
1134 return wrapper
->ctfi_dict
;
1138 arc
= wrapper
->ctfi_archive
;
1140 /* The loop keeps going when skip_parent is on as long as the member we find
1141 is the parent (i.e. at most two iterations, but possibly an early return if
1142 *all* we have is a parent). */
1146 if ((!wrapper
->ctfi_is_archive
) || (i
->ctn_n
>= le64toh (arc
->ctfa_ndicts
)))
1148 ctf_next_destroy (i
);
1151 *errp
= ECTF_NEXT_END
;
1155 modent
= (ctf_archive_modent_t
*) ((char *) arc
1156 + sizeof (struct ctf_archive
));
1157 nametbl
= (((const char *) arc
) + le64toh (arc
->ctfa_names
));
1159 name_
= &nametbl
[le64toh (modent
[i
->ctn_n
].name_offset
)];
1162 while (skip_parent
&& strcmp (name_
, _CTF_SECTION
) == 0);
1167 f
= ctf_dict_open_cached ((ctf_archive_t
*) wrapper
, name_
, errp
);
1172 /* Map the header in. Only used on new, empty files. */
1173 static void *arc_mmap_header (int fd
, size_t headersz
)
1176 if ((hdr
= mmap (NULL
, headersz
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
,
1182 /* mmap() the whole file, for reading only. (Map it writably, but privately: we
1183 need to modify the region, but don't need anyone else to see the
1185 static void *arc_mmap_file (int fd
, size_t size
)
1188 if ((arc
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
1189 fd
, 0)) == MAP_FAILED
)
1194 /* Persist the header to disk. */
1195 static int arc_mmap_writeout (int fd _libctf_unused_
, void *header
,
1196 size_t headersz
, const char **errmsg
)
1198 if (msync (header
, headersz
, MS_ASYNC
) < 0)
1201 *errmsg
= N_("arc_mmap_writeout(): cannot sync after writing "
1208 /* Unmap the region. */
1209 static int arc_mmap_unmap (void *header
, size_t headersz
, const char **errmsg
)
1211 if (munmap (header
, headersz
) < 0)
1214 *errmsg
= N_("arc_mmap_munmap(): cannot unmap after writing "
1221 /* Map the header in. Only used on new, empty files. */
1222 static void *arc_mmap_header (int fd _libctf_unused_
, size_t headersz
)
1225 if ((hdr
= malloc (headersz
)) == NULL
)
1230 /* Pull in the whole file, for reading only. We assume the current file
1231 position is at the start of the file. */
1232 static void *arc_mmap_file (int fd
, size_t size
)
1236 if ((data
= malloc (size
)) == NULL
)
1239 if (ctf_pread (fd
, data
, size
, 0) < 0)
1247 /* Persist the header to disk. */
1248 static int arc_mmap_writeout (int fd
, void *header
, size_t headersz
,
1249 const char **errmsg
)
1252 char *data
= (char *) header
;
1253 ssize_t count
= headersz
;
1255 if ((lseek (fd
, 0, SEEK_SET
)) < 0)
1258 *errmsg
= N_("arc_mmap_writeout(): cannot seek while writing header to "
1263 while (headersz
> 0)
1265 if ((len
= write (fd
, data
, count
)) < 0)
1268 *errmsg
= N_("arc_mmap_writeout(): cannot write header to %s: %s");
1274 if (len
== 0) /* EOF. */
1283 /* Unmap the region. */
1284 static int arc_mmap_unmap (void *header
, size_t headersz _libctf_unused_
,
1285 const char **errmsg _libctf_unused_
)