xref: /netbsd-src/external/gpl3/binutils/dist/libctf/ctf-impl.h (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
16f4ced0bSchristos /* Implementation header.
2*cb63e24eSchristos    Copyright (C) 2019-2024 Free Software Foundation, Inc.
36f4ced0bSchristos 
46f4ced0bSchristos    This file is part of libctf.
56f4ced0bSchristos 
66f4ced0bSchristos    libctf is free software; you can redistribute it and/or modify it under
76f4ced0bSchristos    the terms of the GNU General Public License as published by the Free
86f4ced0bSchristos    Software Foundation; either version 3, or (at your option) any later
96f4ced0bSchristos    version.
106f4ced0bSchristos 
116f4ced0bSchristos    This program is distributed in the hope that it will be useful, but
126f4ced0bSchristos    WITHOUT ANY WARRANTY; without even the implied warranty of
136f4ced0bSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
146f4ced0bSchristos    See the GNU General Public License for more details.
156f4ced0bSchristos 
166f4ced0bSchristos    You should have received a copy of the GNU General Public License
176f4ced0bSchristos    along with this program; see the file COPYING.  If not see
186f4ced0bSchristos    <http://www.gnu.org/licenses/>.  */
196f4ced0bSchristos 
206f4ced0bSchristos #ifndef	_CTF_IMPL_H
216f4ced0bSchristos #define	_CTF_IMPL_H
226f4ced0bSchristos 
236f4ced0bSchristos #include "config.h"
246f4ced0bSchristos #include <errno.h>
256f4ced0bSchristos #include <sys/param.h>
266f4ced0bSchristos #include "ctf-decls.h"
276f4ced0bSchristos #include <ctf-api.h>
284f645668Schristos #include "ctf-sha1.h"
296f4ced0bSchristos #include <sys/types.h>
306f4ced0bSchristos #include <stdlib.h>
316f4ced0bSchristos #include <stdarg.h>
324f645668Schristos #include <stddef.h>
336f4ced0bSchristos #include <stdio.h>
346f4ced0bSchristos #include <stdint.h>
354f645668Schristos #include <string.h>
366f4ced0bSchristos #include <limits.h>
376f4ced0bSchristos #include <ctype.h>
386f4ced0bSchristos #include <elf.h>
396f4ced0bSchristos #include <bfd.h>
404f645668Schristos #include "hashtab.h"
414f645668Schristos #include "ctf-intl.h"
426f4ced0bSchristos 
436f4ced0bSchristos #ifdef	__cplusplus
446f4ced0bSchristos extern "C"
456f4ced0bSchristos {
466f4ced0bSchristos #endif
476f4ced0bSchristos 
484f645668Schristos /* Tuning.  */
494f645668Schristos 
504f645668Schristos /* The proportion of symtypetab entries which must be pads before we consider it
514f645668Schristos    worthwhile to emit a symtypetab section as an index.  Indexes cost time to
524f645668Schristos    look up, but save space all told.  Do not set to 1, since this will cause
534f645668Schristos    indexes to be eschewed completely, even in child dicts, at considerable space
544f645668Schristos    cost.  */
554f645668Schristos #define CTF_INDEX_PAD_THRESHOLD .75
564f645668Schristos 
576f4ced0bSchristos /* Compiler attributes.  */
586f4ced0bSchristos 
596f4ced0bSchristos #if defined (__GNUC__)
606f4ced0bSchristos 
616f4ced0bSchristos /* GCC.  We assume that all compilers claiming to be GCC support sufficiently
626f4ced0bSchristos    many GCC attributes that the code below works.  If some non-GCC compilers
636f4ced0bSchristos    masquerading as GCC in fact do not implement these attributes, version checks
646f4ced0bSchristos    may be required.  */
656f4ced0bSchristos 
666f4ced0bSchristos /* We use the _libctf_*_ pattern to avoid clashes with any future attribute
676f4ced0bSchristos    macros glibc may introduce, which have names of the pattern
686f4ced0bSchristos    __attribute_blah__.  */
696f4ced0bSchristos 
706f4ced0bSchristos #define _libctf_printflike_(string_index,first_to_check) \
716f4ced0bSchristos     __attribute__ ((__format__ (__printf__, (string_index), (first_to_check))))
726f4ced0bSchristos #define _libctf_unlikely_(x) __builtin_expect ((x), 0)
736f4ced0bSchristos #define _libctf_unused_ __attribute__ ((__unused__))
746f4ced0bSchristos #define _libctf_malloc_ __attribute__((__malloc__))
754f645668Schristos #define _libctf_nonnull_(params) __attribute__((__nonnull__ params))
766f4ced0bSchristos 
774f645668Schristos #else
784f645668Schristos 
794f645668Schristos #define _libctf_printflike_(string_index,first_to_check)
804f645668Schristos #define _libctf_unlikely_(x) (x)
814f645668Schristos #define _libctf_unused_
824f645668Schristos #define _libctf_malloc_
834f645668Schristos #define _libctf_nonnull_(params)
844f645668Schristos #define __extension__
854f645668Schristos 
864f645668Schristos #endif
874f645668Schristos 
884f645668Schristos #if defined (ENABLE_LIBCTF_HASH_DEBUGGING) && !defined (NDEBUG)
894f645668Schristos #include <assert.h>
904f645668Schristos #define ctf_assert(fp, expr) (assert (expr), 1)
914f645668Schristos #else
924f645668Schristos #define ctf_assert(fp, expr)						\
934f645668Schristos   _libctf_unlikely_ (ctf_assert_internal (fp, __FILE__, __LINE__,	\
944f645668Schristos 					  #expr, !!(expr)))
956f4ced0bSchristos #endif
966f4ced0bSchristos 
976f4ced0bSchristos /* libctf in-memory state.  */
986f4ced0bSchristos 
996f4ced0bSchristos typedef struct ctf_fixed_hash ctf_hash_t; /* Private to ctf-hash.c.  */
1006f4ced0bSchristos typedef struct ctf_dynhash ctf_dynhash_t; /* Private to ctf-hash.c.  */
1014f645668Schristos typedef struct ctf_dynset ctf_dynset_t;   /* Private to ctf-hash.c.  */
1026f4ced0bSchristos 
1036f4ced0bSchristos typedef struct ctf_strs
1046f4ced0bSchristos {
1056f4ced0bSchristos   const char *cts_strs;		/* Base address of string table.  */
1066f4ced0bSchristos   size_t cts_len;		/* Size of string table in bytes.  */
1076f4ced0bSchristos } ctf_strs_t;
1086f4ced0bSchristos 
1096f4ced0bSchristos typedef struct ctf_strs_writable
1106f4ced0bSchristos {
1116f4ced0bSchristos   char *cts_strs;		/* Base address of string table.  */
1126f4ced0bSchristos   size_t cts_len;		/* Size of string table in bytes.  */
1136f4ced0bSchristos } ctf_strs_writable_t;
1146f4ced0bSchristos 
1156f4ced0bSchristos typedef struct ctf_dmodel
1166f4ced0bSchristos {
1176f4ced0bSchristos   const char *ctd_name;		/* Data model name.  */
1186f4ced0bSchristos   int ctd_code;			/* Data model code.  */
1196f4ced0bSchristos   size_t ctd_pointer;		/* Size of void * in bytes.  */
1206f4ced0bSchristos   size_t ctd_char;		/* Size of char in bytes.  */
1216f4ced0bSchristos   size_t ctd_short;		/* Size of short in bytes.  */
1226f4ced0bSchristos   size_t ctd_int;		/* Size of int in bytes.  */
1236f4ced0bSchristos   size_t ctd_long;		/* Size of long in bytes.  */
1246f4ced0bSchristos } ctf_dmodel_t;
1256f4ced0bSchristos 
1266f4ced0bSchristos typedef struct ctf_names
1276f4ced0bSchristos {
1286f4ced0bSchristos   ctf_hash_t *ctn_readonly;	/* Hash table when readonly.  */
1296f4ced0bSchristos   ctf_dynhash_t *ctn_writable;	/* Hash table when writable.  */
1306f4ced0bSchristos } ctf_names_t;
1316f4ced0bSchristos 
1326f4ced0bSchristos typedef struct ctf_lookup
1336f4ced0bSchristos {
1346f4ced0bSchristos   const char *ctl_prefix;	/* String prefix for this lookup.  */
1356f4ced0bSchristos   size_t ctl_len;		/* Length of prefix string in bytes.  */
1366f4ced0bSchristos   ctf_names_t *ctl_hash;	/* Pointer to hash table for lookup.  */
1376f4ced0bSchristos } ctf_lookup_t;
1386f4ced0bSchristos 
1394f645668Schristos typedef struct ctf_dictops
1406f4ced0bSchristos {
1416f4ced0bSchristos   uint32_t (*ctfo_get_kind) (uint32_t);
1426f4ced0bSchristos   uint32_t (*ctfo_get_root) (uint32_t);
1436f4ced0bSchristos   uint32_t (*ctfo_get_vlen) (uint32_t);
1444f645668Schristos   ssize_t (*ctfo_get_ctt_size) (const ctf_dict_t *, const ctf_type_t *,
1456f4ced0bSchristos 				ssize_t *, ssize_t *);
1464f645668Schristos   ssize_t (*ctfo_get_vbytes) (ctf_dict_t *, unsigned short, ssize_t, size_t);
1474f645668Schristos } ctf_dictops_t;
1486f4ced0bSchristos 
1496f4ced0bSchristos typedef struct ctf_list
1506f4ced0bSchristos {
1516f4ced0bSchristos   struct ctf_list *l_prev;	/* Previous pointer or tail pointer.  */
1526f4ced0bSchristos   struct ctf_list *l_next;	/* Next pointer or head pointer.  */
1536f4ced0bSchristos } ctf_list_t;
1546f4ced0bSchristos 
1556f4ced0bSchristos typedef enum
1566f4ced0bSchristos   {
1576f4ced0bSchristos    CTF_PREC_BASE,
1586f4ced0bSchristos    CTF_PREC_POINTER,
1596f4ced0bSchristos    CTF_PREC_ARRAY,
1606f4ced0bSchristos    CTF_PREC_FUNCTION,
1616f4ced0bSchristos    CTF_PREC_MAX
1626f4ced0bSchristos   } ctf_decl_prec_t;
1636f4ced0bSchristos 
1646f4ced0bSchristos typedef struct ctf_decl_node
1656f4ced0bSchristos {
1666f4ced0bSchristos   ctf_list_t cd_list;		/* Linked list pointers.  */
1676f4ced0bSchristos   ctf_id_t cd_type;		/* Type identifier.  */
1686f4ced0bSchristos   uint32_t cd_kind;		/* Type kind.  */
1696f4ced0bSchristos   uint32_t cd_n;		/* Type dimension if array.  */
1706f4ced0bSchristos } ctf_decl_node_t;
1716f4ced0bSchristos 
1726f4ced0bSchristos typedef struct ctf_decl
1736f4ced0bSchristos {
1746f4ced0bSchristos   ctf_list_t cd_nodes[CTF_PREC_MAX]; /* Declaration node stacks.  */
1756f4ced0bSchristos   int cd_order[CTF_PREC_MAX];	     /* Storage order of decls.  */
1766f4ced0bSchristos   ctf_decl_prec_t cd_qualp;	     /* Qualifier precision.  */
1776f4ced0bSchristos   ctf_decl_prec_t cd_ordp;	     /* Ordered precision.  */
1786f4ced0bSchristos   char *cd_buf;			     /* Buffer for output.  */
1796f4ced0bSchristos   int cd_err;			     /* Saved error value.  */
1806f4ced0bSchristos   int cd_enomem;		     /* Nonzero if OOM during printing.  */
1816f4ced0bSchristos } ctf_decl_t;
1826f4ced0bSchristos 
1836f4ced0bSchristos typedef struct ctf_dtdef
1846f4ced0bSchristos {
1856f4ced0bSchristos   ctf_list_t dtd_list;		/* List forward/back pointers.  */
1866f4ced0bSchristos   ctf_id_t dtd_type;		/* Type identifier for this definition.  */
1876f4ced0bSchristos   ctf_type_t dtd_data;		/* Type node, including name.  */
1884f645668Schristos   size_t dtd_vlen_alloc;	/* Total vlen space allocated.  */
1894f645668Schristos   unsigned char *dtd_vlen;	/* Variable-length data for this type.  */
1906f4ced0bSchristos } ctf_dtdef_t;
1916f4ced0bSchristos 
1926f4ced0bSchristos typedef struct ctf_dvdef
1936f4ced0bSchristos {
1946f4ced0bSchristos   ctf_list_t dvd_list;		/* List forward/back pointers.  */
1956f4ced0bSchristos   char *dvd_name;		/* Name associated with variable.  */
1966f4ced0bSchristos   ctf_id_t dvd_type;		/* Type of variable.  */
1976f4ced0bSchristos   unsigned long dvd_snapshots;	/* Snapshot count when inserted.  */
1986f4ced0bSchristos } ctf_dvdef_t;
1996f4ced0bSchristos 
2004f645668Schristos typedef struct ctf_err_warning
2016f4ced0bSchristos {
2024f645668Schristos   ctf_list_t cew_list;		/* List forward/back pointers.  */
2034f645668Schristos   int cew_is_warning;		/* 1 if warning, 0 if error.  */
2044f645668Schristos   char *cew_text;		/* Error/warning text.  */
2054f645668Schristos } ctf_err_warning_t;
2066f4ced0bSchristos 
2076f4ced0bSchristos /* Atoms associate strings with a list of the CTF items that reference that
2086f4ced0bSchristos    string, so that ctf_update() can instantiate all the strings using the
2096f4ced0bSchristos    ctf_str_atoms and then reassociate them with the real string later.
2106f4ced0bSchristos 
2116f4ced0bSchristos    Strings can be interned into ctf_str_atom without having refs associated
2126f4ced0bSchristos    with them, for values that are returned to callers, etc.  Items are only
2136f4ced0bSchristos    removed from this table on ctf_close(), but on every ctf_update(), all the
2146f4ced0bSchristos    csa_refs in all entries are purged.  */
2156f4ced0bSchristos 
2166f4ced0bSchristos typedef struct ctf_str_atom
2176f4ced0bSchristos {
2186f4ced0bSchristos   const char *csa_str;		/* Backpointer to string (hash key).  */
2196f4ced0bSchristos   ctf_list_t csa_refs;		/* This string's refs.  */
2206f4ced0bSchristos   uint32_t csa_offset;		/* Strtab offset, if any.  */
2216f4ced0bSchristos   uint32_t csa_external_offset;	/* External strtab offset, if any.  */
2226f4ced0bSchristos   unsigned long csa_snapshot_id; /* Snapshot ID at time of creation.  */
2236f4ced0bSchristos } ctf_str_atom_t;
2246f4ced0bSchristos 
2256f4ced0bSchristos /* The refs of a single string in the atoms table.  */
2266f4ced0bSchristos 
2276f4ced0bSchristos typedef struct ctf_str_atom_ref
2286f4ced0bSchristos {
2296f4ced0bSchristos   ctf_list_t caf_list;		/* List forward/back pointers.  */
2306f4ced0bSchristos   uint32_t *caf_ref;		/* A single ref to this string.  */
2316f4ced0bSchristos } ctf_str_atom_ref_t;
2326f4ced0bSchristos 
2334f645668Schristos /* A single linker-provided symbol, during symbol addition, possibly before we
2344f645668Schristos    have been given external strtab refs.  */
2354f645668Schristos typedef struct ctf_in_flight_dynsym
2366f4ced0bSchristos {
2374f645668Schristos   ctf_list_t cid_list;		/* List forward/back pointers.  */
2384f645668Schristos   ctf_link_sym_t cid_sym;	/* The linker-known symbol.  */
2394f645668Schristos } ctf_in_flight_dynsym_t;
2406f4ced0bSchristos 
2414f645668Schristos /* The structure used as the key in a ctf_link_type_mapping.  The value is a
2424f645668Schristos    type index, not a type ID.  */
2434f645668Schristos 
2444f645668Schristos typedef struct ctf_link_type_key
2454f645668Schristos {
2464f645668Schristos   ctf_dict_t *cltk_fp;
2474f645668Schristos   ctf_id_t cltk_idx;
2484f645668Schristos } ctf_link_type_key_t;
2494f645668Schristos 
2504f645668Schristos /* The structure used as the key in a cd_id_to_dict_t on 32-bit platforms.  */
2514f645668Schristos typedef struct ctf_type_id_key
2524f645668Schristos {
2534f645668Schristos   int ctii_input_num;
2544f645668Schristos   ctf_id_t ctii_type;
2554f645668Schristos } ctf_type_id_key_t;
2564f645668Schristos 
2574f645668Schristos /* Deduplicator state.
2584f645668Schristos 
2594f645668Schristos    The dedup state below uses three terms consistently. A "hash" is a
2604f645668Schristos    ctf_dynhash_t; a "hash value" is the hash value of a type as returned by
2614f645668Schristos    ctf_dedup_hash_type; a "global type ID" or "global ID" is a packed-together
2624f645668Schristos    reference to a single ctf_dict_t (by array index in an array of inputs) and
2634f645668Schristos    ctf_id_t, i.e. a single instance of some hash value in some input.
2644f645668Schristos 
2654f645668Schristos    The deduplication algorithm takes a bunch of inputs and yields a single
2664f645668Schristos    shared "output" and possibly many outputs corresponding to individual inputs
2674f645668Schristos    that still contain types after sharing of unconflicted types.  Almost all
2684f645668Schristos    deduplicator state is stored in the struct ctf_dedup in the output, though a
2694f645668Schristos    (very) few things are stored in inputs for simplicity's sake, usually if they
2704f645668Schristos    are linking together things within the scope of a single TU.
2714f645668Schristos 
2724f645668Schristos    Flushed at the end of every ctf_dedup run.  */
2734f645668Schristos 
2744f645668Schristos typedef struct ctf_dedup
2754f645668Schristos {
2764f645668Schristos   /* The CTF linker flags in force for this dedup run.  */
2774f645668Schristos   int cd_link_flags;
2784f645668Schristos 
2794f645668Schristos   /* On 32-bit platforms only, a hash of global type IDs, in the form of
2804f645668Schristos      a ctf_link_type_id_key_t.  */
2814f645668Schristos   ctf_dynhash_t *cd_id_to_dict_t;
2824f645668Schristos 
2834f645668Schristos   /* Atoms tables of decorated names: maps undecorated name to decorated name.
2844f645668Schristos      (The actual allocations are in the CTF dict for the former and the real
2854f645668Schristos      atoms table for the latter).  Uses the same namespaces as ctf_lookups,
2864f645668Schristos      below, but has no need for null-termination.  */
2874f645668Schristos   ctf_dynhash_t *cd_decorated_names[4];
2884f645668Schristos 
2894f645668Schristos   /* Map type names to a hash from type hash value -> number of times each value
2904f645668Schristos      has appeared.  */
2914f645668Schristos   ctf_dynhash_t *cd_name_counts;
2924f645668Schristos 
2934f645668Schristos   /* Map global type IDs to type hash values.  Used to determine if types are
2944f645668Schristos      already hashed without having to recompute their hash values again, and to
2954f645668Schristos      link types together at later stages.  Forwards that are peeked through to
2964f645668Schristos      structs and unions are not represented in here, so lookups that might be
2974f645668Schristos      such a type (in practice, all lookups) must go via cd_replaced_types first
2984f645668Schristos      to take this into account.  Discarded before each rehashing.  */
2994f645668Schristos   ctf_dynhash_t *cd_type_hashes;
3004f645668Schristos 
3014f645668Schristos   /* Maps from the names of structs/unions/enums to a a single GID which is the
3024f645668Schristos      only appearance of that type in any input: if it appears in more than one
3034f645668Schristos      input, a value which is a GID with an input_num of -1 appears.  Used in
3044f645668Schristos      share-duplicated link mode link modes to determine whether structs/unions
3054f645668Schristos      can be cited from multiple TUs.  Only populated in that link mode.  */
3064f645668Schristos   ctf_dynhash_t *cd_struct_origin;
3074f645668Schristos 
3084f645668Schristos   /* Maps type hash values to a set of hash values of the types that cite them:
3094f645668Schristos      i.e., pointing backwards up the type graph.  Used for recursive conflict
3104f645668Schristos      marking.  Citations from tagged structures, unions, and forwards do not
3114f645668Schristos      appear in this graph.  */
3124f645668Schristos   ctf_dynhash_t *cd_citers;
3134f645668Schristos 
3144f645668Schristos   /* Maps type hash values to input global type IDs.  The value is a set (a
3154f645668Schristos      hash) of global type IDs.  Discarded before each rehashing.  The result of
3164f645668Schristos      the ctf_dedup function.  */
3174f645668Schristos   ctf_dynhash_t *cd_output_mapping;
3184f645668Schristos 
3194f645668Schristos   /* A map giving the GID of the first appearance of each type for each type
3204f645668Schristos      hash value.  */
3214f645668Schristos   ctf_dynhash_t *cd_output_first_gid;
3224f645668Schristos 
3234f645668Schristos   /* Used to ensure that we never try to map a single type ID to more than one
3244f645668Schristos      hash.  */
3254f645668Schristos   ctf_dynhash_t *cd_output_mapping_guard;
3264f645668Schristos 
3274f645668Schristos   /* Maps the global type IDs of structures in input TUs whose members still
3284f645668Schristos      need emission to the global type ID of the already-emitted target type
3294f645668Schristos      (which has no members yet) in the appropriate target.  Uniquely, the latter
3304f645668Schristos      ID represents a *target* ID (i.e. the cd_output_mapping of some specified
3314f645668Schristos      input): we encode the shared (parent) dict with an ID of -1.  */
3324f645668Schristos   ctf_dynhash_t *cd_emission_struct_members;
3334f645668Schristos 
3344f645668Schristos   /* A set (a hash) of hash values of conflicting types.  */
3354f645668Schristos   ctf_dynset_t *cd_conflicting_types;
3364f645668Schristos 
3374f645668Schristos   /* A hash mapping fp *'s of inputs to their input_nums.  Used only by
3384f645668Schristos      functions outside the core ctf_dedup / ctf_dedup_emit machinery which do
3394f645668Schristos      not take an inputs array.  */
3404f645668Schristos   ctf_dynhash_t *cd_input_nums;
3414f645668Schristos 
3424f645668Schristos   /* Maps type hashes to ctf_id_t's in this dictionary.  Populated only at
3434f645668Schristos      emission time, in the dictionary where emission is taking place.  */
3444f645668Schristos   ctf_dynhash_t *cd_output_emission_hashes;
3454f645668Schristos 
3464f645668Schristos   /* Maps the decorated names of conflicted cross-TU forwards that were forcibly
3474f645668Schristos      emitted in this TU to their emitted ctf_id_ts.  Populated only at emission
3484f645668Schristos      time, in the dictionary where emission is taking place. */
3494f645668Schristos   ctf_dynhash_t *cd_output_emission_conflicted_forwards;
3504f645668Schristos 
3514f645668Schristos   /* Points to the output counterpart of this input dictionary, at emission
3524f645668Schristos      time.  */
3534f645668Schristos   ctf_dict_t *cd_output;
3544f645668Schristos } ctf_dedup_t;
3554f645668Schristos 
3564f645668Schristos /* The ctf_dict is the structure used to represent a CTF dictionary to library
3576f4ced0bSchristos    clients, who see it only as an opaque pointer.  Modifications can therefore
3586f4ced0bSchristos    be made freely to this structure without regard to client versioning.  The
3594f645668Schristos    ctf_dict_t typedef appears in <ctf-api.h> and declares a forward tag.
3604f645668Schristos    (A ctf_file_t typedef also appears there, for historical reasons.)
3616f4ced0bSchristos 
3624f645668Schristos    NOTE: ctf_serialize requires that everything inside of ctf_dict either be an
3636f4ced0bSchristos    immediate value, a pointer to dynamically allocated data *outside* of the
3644f645668Schristos    ctf_dict itself, a pointer to statically allocated data, or specially handled
3654f645668Schristos    in ctf_serialize.  If you add a pointer to ctf_dict that points to something
3664f645668Schristos    within the ctf_dict itself, you must make corresponding changes to
3674f645668Schristos    ctf_serialize.  */
3686f4ced0bSchristos 
3694f645668Schristos struct ctf_dict
3706f4ced0bSchristos {
3714f645668Schristos   const ctf_dictops_t *ctf_dictops; /* Version-specific dict operations.  */
3724f645668Schristos   struct ctf_header *ctf_header;    /* The header from this CTF dict.  */
3734f645668Schristos   unsigned char ctf_openflags;	    /* Flags the dict had when opened.  */
3746f4ced0bSchristos   ctf_sect_t ctf_data;		    /* CTF data from object file.  */
3756f4ced0bSchristos   ctf_sect_t ctf_symtab;	    /* Symbol table from object file.  */
3766f4ced0bSchristos   ctf_sect_t ctf_strtab;	    /* String table from object file.  */
3774f645668Schristos   int ctf_symsect_little_endian;    /* Endianness of the ctf_symtab.  */
3784f645668Schristos   ctf_dynhash_t *ctf_symhash;       /* (partial) hash, symsect name -> idx. */
3794f645668Schristos   size_t ctf_symhash_latest;	    /* Amount of symsect scanned so far.  */
3806f4ced0bSchristos   ctf_dynhash_t *ctf_prov_strtab;   /* Maps provisional-strtab offsets
3816f4ced0bSchristos 				       to names.  */
3826f4ced0bSchristos   ctf_dynhash_t *ctf_syn_ext_strtab; /* Maps ext-strtab offsets to names.  */
3836f4ced0bSchristos   void *ctf_data_mmapped;	    /* CTF data we mmapped, to free later.  */
3846f4ced0bSchristos   size_t ctf_data_mmapped_len;	    /* Length of CTF data we mmapped.  */
3856f4ced0bSchristos   ctf_names_t ctf_structs;	    /* Hash table of struct types.  */
3866f4ced0bSchristos   ctf_names_t ctf_unions;	    /* Hash table of union types.  */
3876f4ced0bSchristos   ctf_names_t ctf_enums;	    /* Hash table of enum types.  */
3886f4ced0bSchristos   ctf_names_t ctf_names;	    /* Hash table of remaining type names.  */
3896f4ced0bSchristos   ctf_lookup_t ctf_lookups[5];	    /* Pointers to nametabs for name lookup.  */
3906f4ced0bSchristos   ctf_strs_t ctf_str[2];	    /* Array of string table base and bounds.  */
3916f4ced0bSchristos   ctf_dynhash_t *ctf_str_atoms;	    /* Hash table of ctf_str_atoms_t.  */
3924f645668Schristos   ctf_dynset_t *ctf_str_pending_ref; /* Locations awaiting ref addition.  */
3936f4ced0bSchristos   uint64_t ctf_str_num_refs;	  /* Number of refs to cts_str_atoms.  */
3946f4ced0bSchristos   uint32_t ctf_str_prov_offset;	  /* Latest provisional offset assigned so far.  */
3956f4ced0bSchristos   unsigned char *ctf_base;	  /* CTF file pointer.  */
3966f4ced0bSchristos   unsigned char *ctf_dynbase;	  /* Freeable CTF file pointer. */
3976f4ced0bSchristos   unsigned char *ctf_buf;	  /* Uncompressed CTF data buffer.  */
3986f4ced0bSchristos   size_t ctf_size;		  /* Size of CTF header + uncompressed data.  */
3994f645668Schristos   uint32_t *ctf_sxlate;		  /* Translation table for unindexed symtypetab
4004f645668Schristos 				     entries.  */
4016f4ced0bSchristos   unsigned long ctf_nsyms;	  /* Number of entries in symtab xlate table.  */
4026f4ced0bSchristos   uint32_t *ctf_txlate;		  /* Translation table for type IDs.  */
4036f4ced0bSchristos   uint32_t *ctf_ptrtab;		  /* Translation table for pointer-to lookups.  */
4046f4ced0bSchristos   size_t ctf_ptrtab_len;	  /* Num types storable in ptrtab currently.  */
4054f645668Schristos   uint32_t *ctf_pptrtab;	  /* Parent types pointed to by child dicts.  */
4064f645668Schristos   size_t ctf_pptrtab_len;	  /* Num types storable in pptrtab currently.  */
4074f645668Schristos   uint32_t ctf_pptrtab_typemax;	  /* Max child type when pptrtab last updated.  */
4084f645668Schristos   uint32_t *ctf_funcidx_names;	  /* Name of each function symbol in symtypetab
4094f645668Schristos 				     (if indexed).  */
4104f645668Schristos   uint32_t *ctf_objtidx_names;	  /* Likewise, for object symbols.  */
4114f645668Schristos   size_t ctf_nfuncidx;		  /* Number of funcidx entries.  */
4124f645668Schristos   uint32_t *ctf_funcidx_sxlate;	  /* Offsets into funcinfo for a given funcidx.  */
4134f645668Schristos   uint32_t *ctf_objtidx_sxlate;	  /* Likewise, for ctf_objtidx.  */
4144f645668Schristos   size_t ctf_nobjtidx;		  /* Number of objtidx entries.  */
4154f645668Schristos   ctf_dynhash_t *ctf_objthash;	  /* name -> type ID.  */
4164f645668Schristos   ctf_dynhash_t *ctf_funchash;	  /* name -> CTF_K_FUNCTION type ID.  */
4174f645668Schristos 
4184f645668Schristos   /* The next three are linker-derived state found in ctf_link targets only.  */
4194f645668Schristos 
4204f645668Schristos   ctf_dynhash_t *ctf_dynsyms;	  /* Symbol info from ctf_link_shuffle_syms.  */
4214f645668Schristos   ctf_link_sym_t **ctf_dynsymidx;  /* Indexes ctf_dynsyms by symidx.  */
4224f645668Schristos   uint32_t ctf_dynsymmax;	  /* Maximum ctf_dynsym index.  */
4234f645668Schristos   ctf_list_t ctf_in_flight_dynsyms; /* Dynsyms during accumulation.  */
4246f4ced0bSchristos   struct ctf_varent *ctf_vars;	  /* Sorted variable->type mapping.  */
4256f4ced0bSchristos   unsigned long ctf_nvars;	  /* Number of variables in ctf_vars.  */
4266f4ced0bSchristos   unsigned long ctf_typemax;	  /* Maximum valid type ID number.  */
4276f4ced0bSchristos   const ctf_dmodel_t *ctf_dmodel; /* Data model pointer (see above).  */
4286f4ced0bSchristos   const char *ctf_cuname;	  /* Compilation unit name (if any).  */
4296f4ced0bSchristos   char *ctf_dyncuname;		  /* Dynamically allocated name of CU.  */
4304f645668Schristos   struct ctf_dict *ctf_parent;	  /* Parent CTF dict (if any).  */
4314f645668Schristos   int ctf_parent_unreffed;	  /* Parent set by ctf_import_unref?  */
4324f645668Schristos   const char *ctf_parlabel;	  /* Label in parent dict (if any).  */
4336f4ced0bSchristos   const char *ctf_parname;	  /* Basename of parent (if any).  */
4346f4ced0bSchristos   char *ctf_dynparname;		  /* Dynamically allocated name of parent.  */
4356f4ced0bSchristos   uint32_t ctf_parmax;		  /* Highest type ID of a parent type.  */
4366f4ced0bSchristos   uint32_t ctf_refcnt;		  /* Reference count (for parent links).  */
4376f4ced0bSchristos   uint32_t ctf_flags;		  /* Libctf flags (see below).  */
4386f4ced0bSchristos   int ctf_errno;		  /* Error code for most recent error.  */
4396f4ced0bSchristos   int ctf_version;		  /* CTF data version.  */
4406f4ced0bSchristos   ctf_dynhash_t *ctf_dthash;	  /* Hash of dynamic type definitions.  */
4416f4ced0bSchristos   ctf_list_t ctf_dtdefs;	  /* List of dynamic type definitions.  */
4426f4ced0bSchristos   ctf_dynhash_t *ctf_dvhash;	  /* Hash of dynamic variable mappings.  */
4436f4ced0bSchristos   ctf_list_t ctf_dvdefs;	  /* List of dynamic variable definitions.  */
4446f4ced0bSchristos   unsigned long ctf_dtoldid;	  /* Oldest id that has been committed.  */
4456f4ced0bSchristos   unsigned long ctf_snapshots;	  /* ctf_snapshot() plus ctf_update() count.  */
4466f4ced0bSchristos   unsigned long ctf_snapshot_lu;  /* ctf_snapshot() call count at last update.  */
4474f645668Schristos   ctf_archive_t *ctf_archive;	  /* Archive this ctf_dict_t came from.  */
4484f645668Schristos   ctf_list_t ctf_errs_warnings;	  /* CTF errors and warnings.  */
4496f4ced0bSchristos   ctf_dynhash_t *ctf_link_inputs; /* Inputs to this link.  */
4506f4ced0bSchristos   ctf_dynhash_t *ctf_link_outputs; /* Additional outputs from this link.  */
4514f645668Schristos 
4524f645668Schristos   /* If a link input CU, points at the corresponding per-CU output (if any);
4534f645668Schristos      if an output, points at the input (if any).  */
4544f645668Schristos   ctf_dict_t *ctf_link_in_out;
4554f645668Schristos 
4564f645668Schristos   /* Map input types to output types for ctf_add_type.  Key is a
4574f645668Schristos      ctf_link_type_key_t: value is a type ID.  */
4584f645668Schristos   ctf_dynhash_t *ctf_link_type_mapping;
4594f645668Schristos 
4604f645668Schristos   /* Map input CU names to output CTF dict names: populated in the top-level
4614f645668Schristos      output dict.
4624f645668Schristos 
4634f645668Schristos      Key and value are dynamically-allocated strings.  */
4644f645668Schristos   ctf_dynhash_t *ctf_link_in_cu_mapping;
4654f645668Schristos 
4664f645668Schristos   /* Map output CTF dict names to input CU names: populated in the top-level
4674f645668Schristos      output dict.  A hash of string to hash (set) of strings.  Key and
4684f645668Schristos      individual value members are shared with ctf_link_in_cu_mapping.  */
4694f645668Schristos   ctf_dynhash_t *ctf_link_out_cu_mapping;
4704f645668Schristos 
4714f645668Schristos   /* CTF linker flags.  Set on the parent output dict (the one passed to
4724f645668Schristos      ctf_link).  Only respected when LCTF_LINKING set in ctf_flags.  */
4734f645668Schristos   int ctf_link_flags;
4744f645668Schristos 
4754f645668Schristos   /* Allow the caller to change the name of link archive members.  */
4766f4ced0bSchristos   ctf_link_memb_name_changer_f *ctf_link_memb_name_changer;
4776f4ced0bSchristos   void *ctf_link_memb_name_changer_arg;         /* Argument for it.  */
4784f645668Schristos 
4794f645668Schristos   /* Allow the caller to filter out variables they don't care about.  */
4804f645668Schristos   ctf_link_variable_filter_f *ctf_link_variable_filter;
4814f645668Schristos   void *ctf_link_variable_filter_arg;           /* Argument for it. */
4824f645668Schristos 
4836f4ced0bSchristos   ctf_dynhash_t *ctf_add_processing; /* Types ctf_add_type is working on now.  */
4844f645668Schristos 
4854f645668Schristos   /* Atoms table for dedup string storage.  All strings in the ctf_dedup_t are
4864f645668Schristos      stored here.  Only the _alloc copy is allocated or freed: the
4874f645668Schristos      ctf_dedup_atoms may be pointed to some other CTF dict, to share its atoms.
4884f645668Schristos      We keep the atoms table outside the ctf_dedup so that atoms can be
4894f645668Schristos      preserved across multiple similar links, such as when doing cu-mapped
4904f645668Schristos      links.  */
4914f645668Schristos   ctf_dynset_t *ctf_dedup_atoms;
4924f645668Schristos   ctf_dynset_t *ctf_dedup_atoms_alloc;
4934f645668Schristos 
4944f645668Schristos   ctf_dedup_t ctf_dedup;	  /* Deduplicator state.  */
4954f645668Schristos 
4966f4ced0bSchristos   char *ctf_tmp_typeslice;	  /* Storage for slicing up type names.  */
4976f4ced0bSchristos   size_t ctf_tmp_typeslicelen;	  /* Size of the typeslice.  */
4986f4ced0bSchristos   void *ctf_specific;		  /* Data for ctf_get/setspecific().  */
4996f4ced0bSchristos };
5006f4ced0bSchristos 
5014f645668Schristos /* An abstraction over both a ctf_dict_t and a ctf_archive_t.  */
5026f4ced0bSchristos 
5036f4ced0bSchristos struct ctf_archive_internal
5046f4ced0bSchristos {
5056f4ced0bSchristos   int ctfi_is_archive;
5064f645668Schristos   int ctfi_unmap_on_close;
5074f645668Schristos   ctf_dict_t *ctfi_dict;
5086f4ced0bSchristos   struct ctf_archive *ctfi_archive;
5094f645668Schristos   ctf_dynhash_t *ctfi_dicts;	  /* Dicts we have opened and cached.  */
5104f645668Schristos   ctf_dict_t *ctfi_crossdict_cache; /* Cross-dict caching.  */
5114f645668Schristos   ctf_dict_t **ctfi_symdicts;	  /* Array of index -> ctf_dict_t *.  */
5124f645668Schristos   ctf_dynhash_t *ctfi_symnamedicts; /* Hash of name -> ctf_dict_t *.  */
5136f4ced0bSchristos   ctf_sect_t ctfi_symsect;
5144f645668Schristos   int ctfi_symsect_little_endian; /* -1 for unknown / do not set.  */
5156f4ced0bSchristos   ctf_sect_t ctfi_strsect;
5164f645668Schristos   int ctfi_free_symsect;
5174f645668Schristos   int ctfi_free_strsect;
5186f4ced0bSchristos   void *ctfi_data;
5196f4ced0bSchristos   bfd *ctfi_abfd;		  /* Optional source of section data.  */
5206f4ced0bSchristos   void (*ctfi_bfd_close) (struct ctf_archive_internal *);
5216f4ced0bSchristos };
5226f4ced0bSchristos 
5234f645668Schristos /* Iterator state for the *_next() functions.  */
5244f645668Schristos 
5254f645668Schristos /* A single hash key/value pair.  */
5264f645668Schristos typedef struct ctf_next_hkv
5274f645668Schristos {
5284f645668Schristos   void *hkv_key;
5294f645668Schristos   void *hkv_value;
5304f645668Schristos } ctf_next_hkv_t;
5314f645668Schristos 
5324f645668Schristos struct ctf_next
5334f645668Schristos {
5344f645668Schristos   void (*ctn_iter_fun) (void);
5354f645668Schristos   ctf_id_t ctn_type;
5364f645668Schristos   ssize_t ctn_size;
5374f645668Schristos   ssize_t ctn_increment;
5384f645668Schristos   const ctf_type_t *ctn_tp;
5394f645668Schristos   uint32_t ctn_n;
5404f645668Schristos 
5414f645668Schristos   /* Some iterators contain other iterators, in addition to their other
5424f645668Schristos      state.  */
5434f645668Schristos   ctf_next_t *ctn_next;
5444f645668Schristos 
5454f645668Schristos   /* We can save space on this side of things by noting that a dictionary is
5464f645668Schristos      either dynamic or not, as a whole, and a given iterator can only iterate
5474f645668Schristos      over one kind of thing at once: so we can overlap the DTD and non-DTD
5484f645668Schristos      members, and the structure, variable and enum members, etc.  */
5494f645668Schristos   union
5504f645668Schristos   {
5514f645668Schristos     unsigned char *ctn_vlen;
5524f645668Schristos     const ctf_enum_t *ctn_en;
5534f645668Schristos     const ctf_dvdef_t *ctn_dvd;
5544f645668Schristos     ctf_next_hkv_t *ctn_sorted_hkv;
5554f645668Schristos     void **ctn_hash_slot;
5564f645668Schristos   } u;
5574f645668Schristos 
5584f645668Schristos   /* This union is of various sorts of dict we can iterate over:
5594f645668Schristos      currently dictionaries and archives, dynhashes, and dynsets.  */
5604f645668Schristos   union
5614f645668Schristos   {
5624f645668Schristos     const ctf_dict_t *ctn_fp;
5634f645668Schristos     const ctf_archive_t *ctn_arc;
5644f645668Schristos     const ctf_dynhash_t *ctn_h;
5654f645668Schristos     const ctf_dynset_t *ctn_s;
5664f645668Schristos   } cu;
5674f645668Schristos };
5684f645668Schristos 
5696f4ced0bSchristos /* Return x rounded up to an alignment boundary.
5706f4ced0bSchristos    eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
5716f4ced0bSchristos    eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)  */
5726f4ced0bSchristos #define P2ROUNDUP(x, align)		(-(-(x) & -(align)))
5736f4ced0bSchristos 
5746f4ced0bSchristos /* * If an offs is not aligned already then round it up and align it. */
5756f4ced0bSchristos #define LCTF_ALIGN_OFFS(offs, align) ((offs + (align - 1)) & ~(align - 1))
5766f4ced0bSchristos 
5776f4ced0bSchristos #define LCTF_TYPE_ISPARENT(fp, id) ((id) <= fp->ctf_parmax)
5786f4ced0bSchristos #define LCTF_TYPE_ISCHILD(fp, id) ((id) > fp->ctf_parmax)
5796f4ced0bSchristos #define LCTF_TYPE_TO_INDEX(fp, id) ((id) & (fp->ctf_parmax))
5806f4ced0bSchristos #define LCTF_INDEX_TO_TYPE(fp, id, child) (child ? ((id) | (fp->ctf_parmax+1)) : \
5816f4ced0bSchristos 					   (id))
5826f4ced0bSchristos 
5836f4ced0bSchristos #define LCTF_INDEX_TO_TYPEPTR(fp, i) \
5846f4ced0bSchristos     ((fp->ctf_flags & LCTF_RDWR) ?					\
5856f4ced0bSchristos      &(ctf_dtd_lookup (fp, LCTF_INDEX_TO_TYPE				\
5866f4ced0bSchristos 		       (fp, i, fp->ctf_flags & LCTF_CHILD))->dtd_data) : \
5876f4ced0bSchristos      (ctf_type_t *)((uintptr_t)(fp)->ctf_buf + (fp)->ctf_txlate[(i)]))
5886f4ced0bSchristos 
5894f645668Schristos #define LCTF_INFO_KIND(fp, info)	((fp)->ctf_dictops->ctfo_get_kind(info))
5904f645668Schristos #define LCTF_INFO_ISROOT(fp, info)	((fp)->ctf_dictops->ctfo_get_root(info))
5914f645668Schristos #define LCTF_INFO_VLEN(fp, info)	((fp)->ctf_dictops->ctfo_get_vlen(info))
5926f4ced0bSchristos #define LCTF_VBYTES(fp, kind, size, vlen) \
5934f645668Schristos   ((fp)->ctf_dictops->ctfo_get_vbytes(fp, kind, size, vlen))
5946f4ced0bSchristos 
5954f645668Schristos #define LCTF_CHILD	0x0001	/* CTF dict is a child.  */
5964f645668Schristos #define LCTF_RDWR	0x0002	/* CTF dict is writable.  */
5974f645668Schristos #define LCTF_DIRTY	0x0004	/* CTF dict has been modified.  */
5984f645668Schristos #define LCTF_LINKING	0x0008  /* CTF link is underway: respect ctf_link_flags.  */
5996f4ced0bSchristos 
6004f645668Schristos extern ctf_names_t *ctf_name_table (ctf_dict_t *, int);
6014f645668Schristos extern const ctf_type_t *ctf_lookup_by_id (ctf_dict_t **, ctf_id_t);
6024f645668Schristos extern ctf_id_t ctf_lookup_by_rawname (ctf_dict_t *, int, const char *);
6034f645668Schristos extern ctf_id_t ctf_lookup_by_rawhash (ctf_dict_t *, ctf_names_t *, const char *);
6044f645668Schristos extern void ctf_set_ctl_hashes (ctf_dict_t *);
6056f4ced0bSchristos 
6064f645668Schristos extern int ctf_symtab_skippable (ctf_link_sym_t *sym);
6074f645668Schristos extern int ctf_add_funcobjt_sym (ctf_dict_t *, int is_function,
6084f645668Schristos 				 const char *, ctf_id_t);
6094f645668Schristos 
6104f645668Schristos extern ctf_dict_t *ctf_get_dict (ctf_dict_t *fp, ctf_id_t type);
6116f4ced0bSchristos 
6126f4ced0bSchristos typedef unsigned int (*ctf_hash_fun) (const void *ptr);
6136f4ced0bSchristos extern unsigned int ctf_hash_integer (const void *ptr);
6146f4ced0bSchristos extern unsigned int ctf_hash_string (const void *ptr);
6154f645668Schristos extern unsigned int ctf_hash_type_key (const void *ptr);
6164f645668Schristos extern unsigned int ctf_hash_type_id_key (const void *ptr);
6176f4ced0bSchristos 
6186f4ced0bSchristos typedef int (*ctf_hash_eq_fun) (const void *, const void *);
6196f4ced0bSchristos extern int ctf_hash_eq_integer (const void *, const void *);
6206f4ced0bSchristos extern int ctf_hash_eq_string (const void *, const void *);
6214f645668Schristos extern int ctf_hash_eq_type_key (const void *, const void *);
6224f645668Schristos extern int ctf_hash_eq_type_id_key (const void *, const void *);
6236f4ced0bSchristos 
6246f4ced0bSchristos typedef void (*ctf_hash_free_fun) (void *);
6256f4ced0bSchristos 
6266f4ced0bSchristos typedef void (*ctf_hash_iter_f) (void *key, void *value, void *arg);
6276f4ced0bSchristos typedef int (*ctf_hash_iter_remove_f) (void *key, void *value, void *arg);
6284f645668Schristos typedef int (*ctf_hash_iter_find_f) (void *key, void *value, void *arg);
6294f645668Schristos typedef int (*ctf_hash_sort_f) (const ctf_next_hkv_t *, const ctf_next_hkv_t *,
6304f645668Schristos 				void *arg);
6316f4ced0bSchristos 
6326f4ced0bSchristos extern ctf_hash_t *ctf_hash_create (unsigned long, ctf_hash_fun, ctf_hash_eq_fun);
6334f645668Schristos extern int ctf_hash_insert_type (ctf_hash_t *, ctf_dict_t *, uint32_t, uint32_t);
6344f645668Schristos extern int ctf_hash_define_type (ctf_hash_t *, ctf_dict_t *, uint32_t, uint32_t);
6354f645668Schristos extern ctf_id_t ctf_hash_lookup_type (ctf_hash_t *, ctf_dict_t *, const char *);
6366f4ced0bSchristos extern uint32_t ctf_hash_size (const ctf_hash_t *);
6376f4ced0bSchristos extern void ctf_hash_destroy (ctf_hash_t *);
6386f4ced0bSchristos 
6396f4ced0bSchristos extern ctf_dynhash_t *ctf_dynhash_create (ctf_hash_fun, ctf_hash_eq_fun,
6406f4ced0bSchristos 					  ctf_hash_free_fun, ctf_hash_free_fun);
6416f4ced0bSchristos extern int ctf_dynhash_insert (ctf_dynhash_t *, void *, void *);
6426f4ced0bSchristos extern void ctf_dynhash_remove (ctf_dynhash_t *, const void *);
6434f645668Schristos extern size_t ctf_dynhash_elements (ctf_dynhash_t *);
6446f4ced0bSchristos extern void ctf_dynhash_empty (ctf_dynhash_t *);
6456f4ced0bSchristos extern void *ctf_dynhash_lookup (ctf_dynhash_t *, const void *);
6464f645668Schristos extern int ctf_dynhash_lookup_kv (ctf_dynhash_t *, const void *key,
6474f645668Schristos 				  const void **orig_key, void **value);
6486f4ced0bSchristos extern void ctf_dynhash_destroy (ctf_dynhash_t *);
6496f4ced0bSchristos extern void ctf_dynhash_iter (ctf_dynhash_t *, ctf_hash_iter_f, void *);
6506f4ced0bSchristos extern void ctf_dynhash_iter_remove (ctf_dynhash_t *, ctf_hash_iter_remove_f,
6516f4ced0bSchristos 				     void *);
6524f645668Schristos extern void *ctf_dynhash_iter_find (ctf_dynhash_t *, ctf_hash_iter_find_f,
6534f645668Schristos 				    void *);
6544f645668Schristos extern int ctf_dynhash_sort_by_name (const ctf_next_hkv_t *,
6554f645668Schristos 				     const ctf_next_hkv_t *,
6564f645668Schristos 				     void * _libctf_unused_);
6574f645668Schristos extern int ctf_dynhash_next (ctf_dynhash_t *, ctf_next_t **,
6584f645668Schristos 			     void **key, void **value);
6594f645668Schristos extern int ctf_dynhash_next_sorted (ctf_dynhash_t *, ctf_next_t **,
6604f645668Schristos 				    void **key, void **value, ctf_hash_sort_f,
6614f645668Schristos 				    void *);
6624f645668Schristos 
6634f645668Schristos extern ctf_dynset_t *ctf_dynset_create (htab_hash, htab_eq, ctf_hash_free_fun);
6644f645668Schristos extern int ctf_dynset_insert (ctf_dynset_t *, void *);
6654f645668Schristos extern void ctf_dynset_remove (ctf_dynset_t *, const void *);
6664f645668Schristos extern void ctf_dynset_destroy (ctf_dynset_t *);
6674f645668Schristos extern void *ctf_dynset_lookup (ctf_dynset_t *, const void *);
6684f645668Schristos extern size_t ctf_dynset_elements (ctf_dynset_t *);
6694f645668Schristos extern int ctf_dynset_exists (ctf_dynset_t *, const void *key,
6704f645668Schristos 			      const void **orig_key);
6714f645668Schristos extern int ctf_dynset_next (ctf_dynset_t *, ctf_next_t **, void **key);
6724f645668Schristos extern void *ctf_dynset_lookup_any (ctf_dynset_t *);
6734f645668Schristos 
6744f645668Schristos extern void ctf_sha1_init (ctf_sha1_t *);
6754f645668Schristos extern void ctf_sha1_add (ctf_sha1_t *, const void *, size_t);
6764f645668Schristos extern char *ctf_sha1_fini (ctf_sha1_t *, char *);
6776f4ced0bSchristos 
6786f4ced0bSchristos #define	ctf_list_prev(elem)	((void *)(((ctf_list_t *)(elem))->l_prev))
6796f4ced0bSchristos #define	ctf_list_next(elem)	((void *)(((ctf_list_t *)(elem))->l_next))
6806f4ced0bSchristos 
6816f4ced0bSchristos extern void ctf_list_append (ctf_list_t *, void *);
6826f4ced0bSchristos extern void ctf_list_prepend (ctf_list_t *, void *);
6836f4ced0bSchristos extern void ctf_list_delete (ctf_list_t *, void *);
6844f645668Schristos extern void ctf_list_splice (ctf_list_t *, ctf_list_t *);
6856f4ced0bSchristos extern int ctf_list_empty_p (ctf_list_t *lp);
6866f4ced0bSchristos 
6874f645668Schristos extern int ctf_dtd_insert (ctf_dict_t *, ctf_dtdef_t *, int flag, int kind);
6884f645668Schristos extern void ctf_dtd_delete (ctf_dict_t *, ctf_dtdef_t *);
6894f645668Schristos extern ctf_dtdef_t *ctf_dtd_lookup (const ctf_dict_t *, ctf_id_t);
6904f645668Schristos extern ctf_dtdef_t *ctf_dynamic_type (const ctf_dict_t *, ctf_id_t);
6916f4ced0bSchristos 
6924f645668Schristos extern int ctf_dvd_insert (ctf_dict_t *, ctf_dvdef_t *);
6934f645668Schristos extern void ctf_dvd_delete (ctf_dict_t *, ctf_dvdef_t *);
6944f645668Schristos extern ctf_dvdef_t *ctf_dvd_lookup (const ctf_dict_t *, const char *);
6956f4ced0bSchristos 
6964f645668Schristos extern ctf_id_t ctf_add_encoded (ctf_dict_t *, uint32_t, const char *,
6974f645668Schristos 				 const ctf_encoding_t *, uint32_t kind);
6984f645668Schristos extern ctf_id_t ctf_add_reftype (ctf_dict_t *, uint32_t, ctf_id_t,
6994f645668Schristos 				 uint32_t kind);
7004f645668Schristos 
7014f645668Schristos extern int ctf_dedup_atoms_init (ctf_dict_t *);
7024f645668Schristos extern int ctf_dedup (ctf_dict_t *, ctf_dict_t **, uint32_t ninputs,
7034f645668Schristos 		      uint32_t *parents, int cu_mapped);
7044f645668Schristos extern void ctf_dedup_fini (ctf_dict_t *, ctf_dict_t **, uint32_t);
7054f645668Schristos extern ctf_dict_t **ctf_dedup_emit (ctf_dict_t *, ctf_dict_t **,
7064f645668Schristos 				    uint32_t ninputs, uint32_t *parents,
7074f645668Schristos 				    uint32_t *noutputs, int cu_mapped);
7084f645668Schristos extern ctf_id_t ctf_dedup_type_mapping (ctf_dict_t *fp, ctf_dict_t *src_fp,
7094f645668Schristos 					ctf_id_t src_type);
7106f4ced0bSchristos 
7116f4ced0bSchristos extern void ctf_decl_init (ctf_decl_t *);
7126f4ced0bSchristos extern void ctf_decl_fini (ctf_decl_t *);
7134f645668Schristos extern void ctf_decl_push (ctf_decl_t *, ctf_dict_t *, ctf_id_t);
7146f4ced0bSchristos 
7156f4ced0bSchristos _libctf_printflike_ (2, 3)
7166f4ced0bSchristos extern void ctf_decl_sprintf (ctf_decl_t *, const char *, ...);
7176f4ced0bSchristos extern char *ctf_decl_buf (ctf_decl_t *cd);
7186f4ced0bSchristos 
7194f645668Schristos extern const char *ctf_strptr (ctf_dict_t *, uint32_t);
7204f645668Schristos extern const char *ctf_strraw (ctf_dict_t *, uint32_t);
7214f645668Schristos extern const char *ctf_strraw_explicit (ctf_dict_t *, uint32_t,
7226f4ced0bSchristos 					ctf_strs_t *);
7234f645668Schristos extern int ctf_str_create_atoms (ctf_dict_t *);
7244f645668Schristos extern void ctf_str_free_atoms (ctf_dict_t *);
7254f645668Schristos extern uint32_t ctf_str_add (ctf_dict_t *, const char *);
7264f645668Schristos extern uint32_t ctf_str_add_ref (ctf_dict_t *, const char *, uint32_t *ref);
7274f645668Schristos extern uint32_t ctf_str_add_pending (ctf_dict_t *, const char *, uint32_t *);
7284f645668Schristos extern int ctf_str_move_pending (ctf_dict_t *, uint32_t *, ptrdiff_t);
7294f645668Schristos extern int ctf_str_add_external (ctf_dict_t *, const char *, uint32_t offset);
7304f645668Schristos extern void ctf_str_remove_ref (ctf_dict_t *, const char *, uint32_t *ref);
7314f645668Schristos extern void ctf_str_rollback (ctf_dict_t *, ctf_snapshot_id_t);
7324f645668Schristos extern void ctf_str_purge_refs (ctf_dict_t *);
7334f645668Schristos extern ctf_strs_writable_t ctf_str_write_strtab (ctf_dict_t *);
7346f4ced0bSchristos 
7354f645668Schristos extern struct ctf_archive_internal *
7364f645668Schristos ctf_new_archive_internal (int is_archive, int unmap_on_close,
7374f645668Schristos 			  struct ctf_archive *, ctf_dict_t *,
7384f645668Schristos 			  const ctf_sect_t *symsect,
7394f645668Schristos 			  const ctf_sect_t *strsect, int *errp);
7406f4ced0bSchristos extern struct ctf_archive *ctf_arc_open_internal (const char *, int *);
7416f4ced0bSchristos extern void ctf_arc_close_internal (struct ctf_archive *);
7424f645668Schristos extern const ctf_preamble_t *ctf_arc_bufpreamble (const ctf_sect_t *);
7436f4ced0bSchristos extern void *ctf_set_open_errno (int *, int);
7444f645668Schristos extern void ctf_flip_header (ctf_header_t *);
7454f645668Schristos extern int ctf_flip (ctf_dict_t *, ctf_header_t *, unsigned char *, int);
7466f4ced0bSchristos 
7474f645668Schristos extern ctf_dict_t *ctf_simple_open_internal (const char *, size_t, const char *,
7486f4ced0bSchristos 					     size_t, size_t,
7496f4ced0bSchristos 					     const char *, size_t,
7506f4ced0bSchristos 					     ctf_dynhash_t *, int, int *);
7514f645668Schristos extern ctf_dict_t *ctf_bufopen_internal (const ctf_sect_t *, const ctf_sect_t *,
7526f4ced0bSchristos 					 const ctf_sect_t *, ctf_dynhash_t *,
7536f4ced0bSchristos 					 int, int *);
7544f645668Schristos extern int ctf_import_unref (ctf_dict_t *fp, ctf_dict_t *pfp);
7554f645668Schristos extern int ctf_serialize (ctf_dict_t *);
7566f4ced0bSchristos 
7576f4ced0bSchristos _libctf_malloc_
7586f4ced0bSchristos extern void *ctf_mmap (size_t length, size_t offset, int fd);
7596f4ced0bSchristos extern void ctf_munmap (void *, size_t);
7606f4ced0bSchristos extern ssize_t ctf_pread (int fd, void *buf, ssize_t count, off_t offset);
7616f4ced0bSchristos 
7624f645668Schristos extern void *ctf_realloc (ctf_dict_t *, void *, size_t);
7636f4ced0bSchristos extern char *ctf_str_append (char *, const char *);
7646f4ced0bSchristos extern char *ctf_str_append_noerr (char *, const char *);
7656f4ced0bSchristos 
7664f645668Schristos extern ctf_id_t ctf_type_resolve_unsliced (ctf_dict_t *, ctf_id_t);
7674f645668Schristos extern int ctf_type_kind_unsliced (ctf_dict_t *, ctf_id_t);
7686f4ced0bSchristos 
7696f4ced0bSchristos _libctf_printflike_ (1, 2)
7706f4ced0bSchristos extern void ctf_dprintf (const char *, ...);
7716f4ced0bSchristos extern void libctf_init_debug (void);
7726f4ced0bSchristos 
7734f645668Schristos _libctf_printflike_ (4, 5)
7744f645668Schristos extern void ctf_err_warn (ctf_dict_t *, int is_warning, int err,
7754f645668Schristos 			  const char *, ...);
7764f645668Schristos extern void ctf_err_warn_to_open (ctf_dict_t *);
7774f645668Schristos extern void ctf_assert_fail_internal (ctf_dict_t *, const char *,
7784f645668Schristos 				      size_t, const char *);
7794f645668Schristos extern const char *ctf_link_input_name (ctf_dict_t *);
7804f645668Schristos 
7814f645668Schristos extern ctf_link_sym_t *ctf_elf32_to_link_sym (ctf_dict_t *fp, ctf_link_sym_t *dst,
7824f645668Schristos 					      const Elf32_Sym *src, uint32_t symidx);
7834f645668Schristos extern ctf_link_sym_t *ctf_elf64_to_link_sym (ctf_dict_t *fp, ctf_link_sym_t *dst,
7844f645668Schristos 					      const Elf64_Sym *src, uint32_t symidx);
7856f4ced0bSchristos 
7866f4ced0bSchristos /* Variables, all underscore-prepended. */
7876f4ced0bSchristos 
7886f4ced0bSchristos extern const char _CTF_SECTION[];	/* name of CTF ELF section */
7896f4ced0bSchristos extern const char _CTF_NULLSTR[];	/* empty string */
7906f4ced0bSchristos 
7916f4ced0bSchristos extern int _libctf_version;	/* library client version */
7926f4ced0bSchristos extern int _libctf_debug;	/* debugging messages enabled */
7936f4ced0bSchristos 
7944f645668Schristos #include "ctf-inlines.h"
7954f645668Schristos 
7966f4ced0bSchristos #ifdef	__cplusplus
7976f4ced0bSchristos }
7986f4ced0bSchristos #endif
7996f4ced0bSchristos 
8006f4ced0bSchristos #endif /* _CTF_IMPL_H */
801