xref: /netbsd-src/external/gpl3/gdb/dist/include/ctf-api.h (revision e663ba6e3a60083e70de702e9d54bf486a57b6a7)
18dffb485Schristos /* Public API to libctf.
2*e663ba6eSchristos    Copyright (C) 2019-2024 Free Software Foundation, Inc.
38dffb485Schristos 
48dffb485Schristos    This file is part of libctf.
58dffb485Schristos 
68dffb485Schristos    libctf is free software; you can redistribute it and/or modify it under
78dffb485Schristos    the terms of the GNU General Public License as published by the Free
88dffb485Schristos    Software Foundation; either version 3, or (at your option) any later
98dffb485Schristos    version.
108dffb485Schristos 
118dffb485Schristos    This program is distributed in the hope that it will be useful, but
128dffb485Schristos    WITHOUT ANY WARRANTY; without even the implied warranty of
138dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
148dffb485Schristos    See the GNU General Public License for more details.
158dffb485Schristos 
168dffb485Schristos    You should have received a copy of the GNU General Public License
178dffb485Schristos    along with this program; see the file COPYING.  If not see
188dffb485Schristos    <http://www.gnu.org/licenses/>.  */
198dffb485Schristos 
208dffb485Schristos /* This header file defines the interfaces available from the CTF debugger
218dffb485Schristos    library, libctf.  This API can be used by a debugger to operate on data in
228dffb485Schristos    the Compact ANSI-C Type Format (CTF).  */
238dffb485Schristos 
248dffb485Schristos #ifndef	_CTF_API_H
258dffb485Schristos #define	_CTF_API_H
268dffb485Schristos 
278dffb485Schristos #include <sys/types.h>
288dffb485Schristos #include <ctf.h>
298dffb485Schristos #include <zlib.h>
308dffb485Schristos 
318dffb485Schristos #ifdef	__cplusplus
328dffb485Schristos extern "C"
338dffb485Schristos {
348dffb485Schristos #endif
358dffb485Schristos 
368dffb485Schristos /* Clients can open one or more CTF containers and obtain a pointer to an
374b169a6bSchristos    opaque ctf_dict_t.  Types are identified by an opaque ctf_id_t token.
388dffb485Schristos    They can also open or create read-only archives of CTF containers in a
398dffb485Schristos    ctf_archive_t.
408dffb485Schristos 
418dffb485Schristos    These opaque definitions allow libctf to evolve without breaking clients.  */
428dffb485Schristos 
434b169a6bSchristos typedef struct ctf_dict ctf_dict_t;
448dffb485Schristos typedef struct ctf_archive_internal ctf_archive_t;
458dffb485Schristos typedef unsigned long ctf_id_t;
468dffb485Schristos 
478dffb485Schristos /* This opaque definition allows libctf to accept BFD data structures without
488dffb485Schristos    importing all the BFD noise into users' namespaces.  */
498dffb485Schristos 
508dffb485Schristos struct bfd;
518dffb485Schristos 
528dffb485Schristos /* If the debugger needs to provide the CTF library with a set of raw buffers
538dffb485Schristos    for use as the CTF data, symbol table, and string table, it can do so by
544b169a6bSchristos    filling in ctf_sect_t structures and passing them to ctf_bufopen.
558dffb485Schristos 
564b169a6bSchristos    The contents of this structure must always be in native endianness.  At read
574b169a6bSchristos    time, the symbol table endianness is derived from the BFD target (if BFD is
584b169a6bSchristos    in use): if a BFD target is not in use, please call ctf_symsect_endianness or
594b169a6bSchristos    ctf_arc_symsect_endianness.  */
608dffb485Schristos 
618dffb485Schristos typedef struct ctf_sect
628dffb485Schristos {
638dffb485Schristos   const char *cts_name;		  /* Section name (if any).  */
648dffb485Schristos   const void *cts_data;		  /* Pointer to section data.  */
658dffb485Schristos   size_t cts_size;		  /* Size of data in bytes.  */
668dffb485Schristos   size_t cts_entsize;		  /* Size of each section entry (symtab only).  */
678dffb485Schristos } ctf_sect_t;
688dffb485Schristos 
698dffb485Schristos /* A minimal symbol extracted from a linker's internal symbol table
704b169a6bSchristos    representation.  The symbol name can be given either via st_name or via a
714b169a6bSchristos    strtab offset in st_nameidx, which corresponds to one of the string offsets
724b169a6bSchristos    communicated via the ctf_link_add_strtab callback.   */
738dffb485Schristos 
748dffb485Schristos typedef struct ctf_link_sym
758dffb485Schristos {
764b169a6bSchristos   /* The st_name and st_nameidx will not be accessed outside the call to
774b169a6bSchristos      ctf_link_shuffle_syms.  If you set st_nameidx to offset zero, make sure
784b169a6bSchristos      to set st_nameidx_set as well.  */
798dffb485Schristos 
808dffb485Schristos   const char *st_name;
814b169a6bSchristos   size_t st_nameidx;
824b169a6bSchristos   int st_nameidx_set;
834b169a6bSchristos   uint32_t st_symidx;
848dffb485Schristos   uint32_t st_shndx;
858dffb485Schristos   uint32_t st_type;
868dffb485Schristos   uint32_t st_value;
878dffb485Schristos } ctf_link_sym_t;
888dffb485Schristos 
898dffb485Schristos /* Flags applying to this specific link.  */
908dffb485Schristos 
918dffb485Schristos /* Share all types that are not in conflict.  The default.  */
928dffb485Schristos #define CTF_LINK_SHARE_UNCONFLICTED 0x0
938dffb485Schristos 
948dffb485Schristos /* Share only types that are used by multiple inputs.  */
958dffb485Schristos #define CTF_LINK_SHARE_DUPLICATED 0x1
968dffb485Schristos 
974b169a6bSchristos /* Do a nondeduplicating link, or otherwise deduplicate "less hard", trading off
984b169a6bSchristos    CTF output size for link time.  */
998dffb485Schristos #define CTF_LINK_NONDEDUP 0x2
1008dffb485Schristos 
1018dffb485Schristos /* Create empty outputs for all registered CU mappings even if no types are
1028dffb485Schristos    emitted into them.  */
1038dffb485Schristos #define CTF_LINK_EMPTY_CU_MAPPINGS 0x4
1048dffb485Schristos 
1058dffb485Schristos /* Omit the content of the variables section.  */
1068dffb485Schristos #define CTF_LINK_OMIT_VARIABLES_SECTION 0x8
1078dffb485Schristos 
1084b169a6bSchristos /* If *unset*, filter out entries corresponding to linker-reported symbols
1094b169a6bSchristos    from the variable section, and filter out all entries with no linker-reported
1104b169a6bSchristos    symbols from the data object and function info sections: if set, do no
1114b169a6bSchristos    filtering and leave all entries in place.  (This is a negative-sense flag
1124b169a6bSchristos    because it is rare to want symbols the linker has not reported as present to
1134b169a6bSchristos    stick around in the symtypetab sections nonetheless: relocatable links are
1144b169a6bSchristos    the only likely case.)  */
1154b169a6bSchristos #define CTF_LINK_NO_FILTER_REPORTED_SYMS 0x10
1164b169a6bSchristos 
1178dffb485Schristos /* Symbolic names for CTF sections.  */
1188dffb485Schristos 
1198dffb485Schristos typedef enum ctf_sect_names
1208dffb485Schristos   {
1218dffb485Schristos    CTF_SECT_HEADER,
1228dffb485Schristos    CTF_SECT_LABEL,
1238dffb485Schristos    CTF_SECT_OBJT,
1244b169a6bSchristos    CTF_SECT_OBJTIDX = CTF_SECT_OBJT,
1258dffb485Schristos    CTF_SECT_FUNC,
1264b169a6bSchristos    CTF_SECT_FUNCIDX = CTF_SECT_FUNC,
1278dffb485Schristos    CTF_SECT_VAR,
1288dffb485Schristos    CTF_SECT_TYPE,
1298dffb485Schristos    CTF_SECT_STR
1308dffb485Schristos   } ctf_sect_names_t;
1318dffb485Schristos 
1328dffb485Schristos /* Encoding information for integers, floating-point values, and certain other
1334b169a6bSchristos    intrinsics can be obtained by calling ctf_type_encoding, below.  The flags
1348dffb485Schristos    field will contain values appropriate for the type defined in <ctf.h>.  */
1358dffb485Schristos 
1368dffb485Schristos typedef struct ctf_encoding
1378dffb485Schristos {
1388dffb485Schristos   uint32_t cte_format;		 /* Data format (CTF_INT_* or CTF_FP_* flags).  */
1398dffb485Schristos   uint32_t cte_offset;		 /* Offset of value in bits.  */
1408dffb485Schristos   uint32_t cte_bits;		 /* Size of storage in bits.  */
1418dffb485Schristos } ctf_encoding_t;
1428dffb485Schristos 
1438dffb485Schristos typedef struct ctf_membinfo
1448dffb485Schristos {
1458dffb485Schristos   ctf_id_t ctm_type;		/* Type of struct or union member.  */
1468dffb485Schristos   unsigned long ctm_offset;	/* Offset of member in bits.  */
1478dffb485Schristos } ctf_membinfo_t;
1488dffb485Schristos 
1498dffb485Schristos typedef struct ctf_arinfo
1508dffb485Schristos {
1518dffb485Schristos   ctf_id_t ctr_contents;	/* Type of array contents.  */
1528dffb485Schristos   ctf_id_t ctr_index;		/* Type of array index.  */
1538dffb485Schristos   uint32_t ctr_nelems;		/* Number of elements.  */
1548dffb485Schristos } ctf_arinfo_t;
1558dffb485Schristos 
1568dffb485Schristos typedef struct ctf_funcinfo
1578dffb485Schristos {
1588dffb485Schristos   ctf_id_t ctc_return;		/* Function return type.  */
1598dffb485Schristos   uint32_t ctc_argc;		/* Number of typed arguments to function.  */
1608dffb485Schristos   uint32_t ctc_flags;		/* Function attributes (see below).  */
1618dffb485Schristos } ctf_funcinfo_t;
1628dffb485Schristos 
1638dffb485Schristos typedef struct ctf_lblinfo
1648dffb485Schristos {
1658dffb485Schristos   ctf_id_t ctb_type;		/* Last type associated with the label.  */
1668dffb485Schristos } ctf_lblinfo_t;
1678dffb485Schristos 
1688dffb485Schristos typedef struct ctf_snapshot_id
1698dffb485Schristos {
1708dffb485Schristos   unsigned long dtd_id;		/* Highest DTD ID at time of snapshot.  */
1718dffb485Schristos   unsigned long snapshot_id;	/* Snapshot id at time of snapshot.  */
1728dffb485Schristos } ctf_snapshot_id_t;
1738dffb485Schristos 
1748dffb485Schristos #define	CTF_FUNC_VARARG	0x1	/* Function arguments end with varargs.  */
1758dffb485Schristos 
1768dffb485Schristos /* Functions that return a ctf_id_t use the following value to indicate failure.
1774b169a6bSchristos    ctf_errno can be used to obtain an error code.  Functions that return
1784b169a6bSchristos    a straight integral -1 also use ctf_errno.  */
1798dffb485Schristos #define	CTF_ERR	((ctf_id_t) -1L)
1808dffb485Schristos 
1814b169a6bSchristos /* This macro holds information about all the available ctf errors.
1824b169a6bSchristos    It is used to form both an enum holding all the error constants,
1834b169a6bSchristos    and also the error strings themselves.  To use, define _CTF_FIRST
1844b169a6bSchristos    and _CTF_ITEM to expand as you like, then mention the macro name.
1854b169a6bSchristos    See the enum after this for an example.  */
1864b169a6bSchristos #define _CTF_ERRORS \
1874b169a6bSchristos   _CTF_FIRST (ECTF_FMT, "File is not in CTF or ELF format.")	\
1884b169a6bSchristos   _CTF_ITEM (ECTF_BFDERR, "BFD error.")				\
1894b169a6bSchristos   _CTF_ITEM (ECTF_CTFVERS, "CTF dict version is too new for libctf.") \
1904b169a6bSchristos   _CTF_ITEM (ECTF_BFD_AMBIGUOUS, "Ambiguous BFD target.")	\
1914b169a6bSchristos   _CTF_ITEM (ECTF_SYMTAB, "Symbol table uses invalid entry size.") \
1924b169a6bSchristos   _CTF_ITEM (ECTF_SYMBAD, "Symbol table data buffer is not valid.") \
1934b169a6bSchristos   _CTF_ITEM (ECTF_STRBAD, "String table data buffer is not valid.") \
1944b169a6bSchristos   _CTF_ITEM (ECTF_CORRUPT, "File data structure corruption detected.") \
1954b169a6bSchristos   _CTF_ITEM (ECTF_NOCTFDATA, "File does not contain CTF data.") \
1964b169a6bSchristos   _CTF_ITEM (ECTF_NOCTFBUF, "Buffer does not contain CTF data.") \
1974b169a6bSchristos   _CTF_ITEM (ECTF_NOSYMTAB, "Symbol table information is not available.") \
1984b169a6bSchristos   _CTF_ITEM (ECTF_NOPARENT, "The parent CTF dictionary is unavailable.") \
1994b169a6bSchristos   _CTF_ITEM (ECTF_DMODEL, "Data model mismatch.") \
2004b169a6bSchristos   _CTF_ITEM (ECTF_LINKADDEDLATE, "File added to link too late.") \
2014b169a6bSchristos   _CTF_ITEM (ECTF_ZALLOC, "Failed to allocate (de)compression buffer.") \
2024b169a6bSchristos   _CTF_ITEM (ECTF_DECOMPRESS, "Failed to decompress CTF data.") \
2034b169a6bSchristos   _CTF_ITEM (ECTF_STRTAB, "External string table is not available.") \
2044b169a6bSchristos   _CTF_ITEM (ECTF_BADNAME, "String name offset is corrupt.") \
2054b169a6bSchristos   _CTF_ITEM (ECTF_BADID, "Invalid type identifier.") \
2064b169a6bSchristos   _CTF_ITEM (ECTF_NOTSOU, "Type is not a struct or union.") \
2074b169a6bSchristos   _CTF_ITEM (ECTF_NOTENUM, "Type is not an enum.") \
2084b169a6bSchristos   _CTF_ITEM (ECTF_NOTSUE, "Type is not a struct, union, or enum.") \
2094b169a6bSchristos   _CTF_ITEM (ECTF_NOTINTFP, "Type is not an integer, float, or enum.") \
2104b169a6bSchristos   _CTF_ITEM (ECTF_NOTARRAY, "Type is not an array.") \
2114b169a6bSchristos   _CTF_ITEM (ECTF_NOTREF, "Type does not reference another type.") \
2124b169a6bSchristos   _CTF_ITEM (ECTF_NAMELEN, "Buffer is too small to hold type name.") \
2134b169a6bSchristos   _CTF_ITEM (ECTF_NOTYPE, "No type found corresponding to name.") \
2144b169a6bSchristos   _CTF_ITEM (ECTF_SYNTAX, "Syntax error in type name.") \
2154b169a6bSchristos   _CTF_ITEM (ECTF_NOTFUNC, "Symbol table entry or type is not a function.") \
2164b169a6bSchristos   _CTF_ITEM (ECTF_NOFUNCDAT, "No function information available for function.") \
2174b169a6bSchristos   _CTF_ITEM (ECTF_NOTDATA, "Symbol table entry does not refer to a data object.") \
2184b169a6bSchristos   _CTF_ITEM (ECTF_NOTYPEDAT, "No type information available for symbol.") \
2194b169a6bSchristos   _CTF_ITEM (ECTF_NOLABEL, "No label found corresponding to name.") \
2204b169a6bSchristos   _CTF_ITEM (ECTF_NOLABELDATA, "File does not contain any labels.") \
2214b169a6bSchristos   _CTF_ITEM (ECTF_NOTSUP, "Feature not supported.") \
2224b169a6bSchristos   _CTF_ITEM (ECTF_NOENUMNAM, "Enum element name not found.") \
2234b169a6bSchristos   _CTF_ITEM (ECTF_NOMEMBNAM, "Member name not found.") \
2244b169a6bSchristos   _CTF_ITEM (ECTF_RDONLY, "CTF container is read-only.") \
2254b169a6bSchristos   _CTF_ITEM (ECTF_DTFULL, "CTF type is full (no more members allowed).") \
2264b169a6bSchristos   _CTF_ITEM (ECTF_FULL, "CTF container is full.") \
2274b169a6bSchristos   _CTF_ITEM (ECTF_DUPLICATE, "Duplicate member or variable name.") \
2284b169a6bSchristos   _CTF_ITEM (ECTF_CONFLICT, "Conflicting type is already defined.") \
2294b169a6bSchristos   _CTF_ITEM (ECTF_OVERROLLBACK, "Attempt to roll back past a ctf_update.") \
2304b169a6bSchristos   _CTF_ITEM (ECTF_COMPRESS, "Failed to compress CTF data.") \
2314b169a6bSchristos   _CTF_ITEM (ECTF_ARCREATE, "Error creating CTF archive.") \
2324b169a6bSchristos   _CTF_ITEM (ECTF_ARNNAME, "Name not found in CTF archive.") \
2334b169a6bSchristos   _CTF_ITEM (ECTF_SLICEOVERFLOW, "Overflow of type bitness or offset in slice.") \
2344b169a6bSchristos   _CTF_ITEM (ECTF_DUMPSECTUNKNOWN, "Unknown section number in dump.") \
2354b169a6bSchristos   _CTF_ITEM (ECTF_DUMPSECTCHANGED, "Section changed in middle of dump.") \
2364b169a6bSchristos   _CTF_ITEM (ECTF_NOTYET, "Feature not yet implemented.") \
2374b169a6bSchristos   _CTF_ITEM (ECTF_INTERNAL, "Internal error: assertion failure.") \
2384b169a6bSchristos   _CTF_ITEM (ECTF_NONREPRESENTABLE, "Type not representable in CTF.") \
2394b169a6bSchristos   _CTF_ITEM (ECTF_NEXT_END, "End of iteration.") \
2404b169a6bSchristos   _CTF_ITEM (ECTF_NEXT_WRONGFUN, "Wrong iteration function called.") \
2414b169a6bSchristos   _CTF_ITEM (ECTF_NEXT_WRONGFP, "Iteration entity changed in mid-iterate.") \
2424b169a6bSchristos   _CTF_ITEM (ECTF_FLAGS, "CTF header contains flags unknown to libctf.") \
2434b169a6bSchristos   _CTF_ITEM (ECTF_NEEDSBFD, "This feature needs a libctf with BFD support.") \
2444b169a6bSchristos   _CTF_ITEM (ECTF_INCOMPLETE, "Type is not a complete type.") \
2454b169a6bSchristos   _CTF_ITEM (ECTF_NONAME, "Type name must not be empty.")
2464b169a6bSchristos 
2478dffb485Schristos #define	ECTF_BASE	1000	/* Base value for libctf errnos.  */
2488dffb485Schristos 
2498dffb485Schristos enum
2508dffb485Schristos   {
2514b169a6bSchristos #define _CTF_FIRST(NAME, STR) NAME = ECTF_BASE
2524b169a6bSchristos #define _CTF_ITEM(NAME, STR) , NAME
2534b169a6bSchristos _CTF_ERRORS
2544b169a6bSchristos #undef _CTF_ITEM
2554b169a6bSchristos #undef _CTF_FIRST
2568dffb485Schristos   };
2578dffb485Schristos 
2584b169a6bSchristos #define ECTF_NERR (ECTF_NONAME - ECTF_BASE + 1) /* Count of CTF errors.  */
2598dffb485Schristos 
2608dffb485Schristos /* The CTF data model is inferred to be the caller's data model or the data
2614b169a6bSchristos    model of the given object, unless ctf_setmodel is explicitly called.  */
2628dffb485Schristos #define	CTF_MODEL_ILP32 1	/* Object data model is ILP32.  */
2638dffb485Schristos #define	CTF_MODEL_LP64  2	/* Object data model is LP64.  */
2648dffb485Schristos #ifdef _LP64
2658dffb485Schristos # define CTF_MODEL_NATIVE CTF_MODEL_LP64
2668dffb485Schristos #else
2678dffb485Schristos # define CTF_MODEL_NATIVE CTF_MODEL_ILP32
2688dffb485Schristos #endif
2698dffb485Schristos 
2704b169a6bSchristos /* Dynamic CTF containers can be created using ctf_create.  The ctf_add_*
2718dffb485Schristos    routines can be used to add new definitions to the dynamic container.
2728dffb485Schristos    New types are labeled as root or non-root to determine whether they are
2738dffb485Schristos    visible at the top-level program scope when subsequently doing a lookup.  */
2748dffb485Schristos 
2758dffb485Schristos #define	CTF_ADD_NONROOT	0	/* Type only visible in nested scope.  */
2768dffb485Schristos #define	CTF_ADD_ROOT	1	/* Type visible at top-level scope.  */
2778dffb485Schristos 
2784b169a6bSchristos /* Flags for ctf_member_next.  */
2794b169a6bSchristos 
2804b169a6bSchristos #define CTF_MN_RECURSE 0x1	/* Recurse into unnamed members.  */
2814b169a6bSchristos 
2828dffb485Schristos /* These typedefs are used to define the signature for callback functions that
2838dffb485Schristos    can be used with the iteration and visit functions below.  There is also a
2848dffb485Schristos    family of iteration functions that do not require callbacks.  */
2858dffb485Schristos 
2868dffb485Schristos typedef int ctf_visit_f (const char *name, ctf_id_t type, unsigned long offset,
2878dffb485Schristos 			 int depth, void *arg);
2888dffb485Schristos typedef int ctf_member_f (const char *name, ctf_id_t membtype,
2898dffb485Schristos 			  unsigned long offset, void *arg);
2908dffb485Schristos typedef int ctf_enum_f (const char *name, int val, void *arg);
2918dffb485Schristos typedef int ctf_variable_f (const char *name, ctf_id_t type, void *arg);
2928dffb485Schristos typedef int ctf_type_f (ctf_id_t type, void *arg);
2938dffb485Schristos typedef int ctf_type_all_f (ctf_id_t type, int flag, void *arg);
2948dffb485Schristos typedef int ctf_label_f (const char *name, const ctf_lblinfo_t *info,
2958dffb485Schristos 			 void *arg);
2964b169a6bSchristos typedef int ctf_archive_member_f (ctf_dict_t *fp, const char *name, void *arg);
2978dffb485Schristos typedef int ctf_archive_raw_member_f (const char *name, const void *content,
2988dffb485Schristos 				      size_t len, void *arg);
2998dffb485Schristos typedef char *ctf_dump_decorate_f (ctf_sect_names_t sect,
3008dffb485Schristos 				   char *line, void *arg);
3018dffb485Schristos 
3028dffb485Schristos typedef struct ctf_dump_state ctf_dump_state_t;
3038dffb485Schristos 
3044b169a6bSchristos /* Iteration state for the _next functions, and allocators/copiers/freers for
3058dffb485Schristos    it.  (None of these are needed for the simple case of iterating to the end:
306*e663ba6eSchristos    the _next functions allocate and free the iterators for you.)
307*e663ba6eSchristos 
308*e663ba6eSchristos    The _next iterators all work in similar ways: they take things to query (a
309*e663ba6eSchristos    dict, a name, a type ID, something like that), then a ctf_next_t iterator
310*e663ba6eSchristos    arg which must be the address of a variable whose value is NULL on first
311*e663ba6eSchristos    call, and will be set to NULL again once iteration has completed.
312*e663ba6eSchristos 
313*e663ba6eSchristos    They return something important about the thing being iterated over (often a
314*e663ba6eSchristos    type ID or a name); on end of iteration they instead return return CTF_ERR,
315*e663ba6eSchristos    -1, or NULL and set the error ECTF_NEXT_END on the dict.  They can often
316*e663ba6eSchristos    provide more information too: this is done via pointer parameters (e.g. the
317*e663ba6eSchristos    membname and membtype in ctf_member_next()).  These parameters are always
318*e663ba6eSchristos    optional and can be set to NULL if not needed.
319*e663ba6eSchristos 
320*e663ba6eSchristos    Errors other than end-of-iteration will return CTF_ERR/-1/NULL and set the
321*e663ba6eSchristos    error to something other than ECTF_NEXT_END, and *not* destroy the iterator:
322*e663ba6eSchristos    you should either recover somehow and continue iterating, or call
323*e663ba6eSchristos    ctf_next_destroy() on it.  (You can call ctf_next_destroy() on a NULL
324*e663ba6eSchristos    iterator, so it's safe to just unconditionally do it after iteration has
325*e663ba6eSchristos    completed.)  */
3268dffb485Schristos 
3278dffb485Schristos typedef struct ctf_next ctf_next_t;
3288dffb485Schristos extern ctf_next_t *ctf_next_create (void);
3298dffb485Schristos extern void ctf_next_destroy (ctf_next_t *);
3308dffb485Schristos extern ctf_next_t *ctf_next_copy (ctf_next_t *);
3318dffb485Schristos 
3328dffb485Schristos /* Opening.  These mostly return an abstraction over both CTF files and CTF
3338dffb485Schristos    archives: so they can be used to open both.  CTF files will appear to be an
334*e663ba6eSchristos    archive with one member named '.ctf'.
3354b169a6bSchristos 
336*e663ba6eSchristos    All these functions except for ctf_close use BFD and can open anything BFD
337*e663ba6eSchristos    can open, hunting down the .ctf section for you, so are not available in the
338*e663ba6eSchristos    libctf-nobfd flavour of the library.  If you want to provide the CTF section
339*e663ba6eSchristos    yourself, you can do that with ctf_bfdopen_ctfsect.  */
3408dffb485Schristos 
3418dffb485Schristos extern ctf_archive_t *ctf_bfdopen (struct bfd *, int *);
3428dffb485Schristos extern ctf_archive_t *ctf_bfdopen_ctfsect (struct bfd *, const ctf_sect_t *,
3438dffb485Schristos 					   int *);
3448dffb485Schristos extern ctf_archive_t *ctf_fdopen (int fd, const char *filename,
3458dffb485Schristos 				  const char *target, int *errp);
3468dffb485Schristos extern ctf_archive_t *ctf_open (const char *filename,
3478dffb485Schristos 				const char *target, int *errp);
3488dffb485Schristos extern void ctf_close (ctf_archive_t *);
349*e663ba6eSchristos 
350*e663ba6eSchristos /* Return the data, symbol, or string sections used by a given CTF dict.  */
3514b169a6bSchristos extern ctf_sect_t ctf_getdatasect (const ctf_dict_t *);
3524b169a6bSchristos extern ctf_sect_t ctf_getsymsect (const ctf_dict_t *);
3534b169a6bSchristos extern ctf_sect_t ctf_getstrsect (const ctf_dict_t *);
354*e663ba6eSchristos 
355*e663ba6eSchristos /* Set the endianness of the symbol section, which may be different from
356*e663ba6eSchristos    the endianness of the CTF dict. Done for you by ctf_open and ctf_fdopen,
357*e663ba6eSchristos    but direct calls to ctf_bufopen etc with symbol sections provided must
358*e663ba6eSchristos    do so explicitly.  */
359*e663ba6eSchristos 
3604b169a6bSchristos extern void ctf_symsect_endianness (ctf_dict_t *, int little_endian);
3614b169a6bSchristos extern void ctf_arc_symsect_endianness (ctf_archive_t *, int little_endian);
362*e663ba6eSchristos 
363*e663ba6eSchristos /* Open CTF archives from files or raw section data, and close them again.
364*e663ba6eSchristos    Closing may munmap() the data making up the archive, so should not be
365*e663ba6eSchristos    done until all dicts are finished with and closed themselves.
366*e663ba6eSchristos 
367*e663ba6eSchristos    Almost all functions that open archives will also open raw CTF dicts, which
368*e663ba6eSchristos    are treated as if they were archives with only one member.
369*e663ba6eSchristos 
370*e663ba6eSchristos    Some of these functions take optional raw symtab and strtab section content
371*e663ba6eSchristos    in the form of ctf_sect_t structures.  For CTF in ELF files, the more
372*e663ba6eSchristos    convenient opening functions above extract these .dynsym and its associated
373*e663ba6eSchristos    string table (usually .dynsym) whenever the CTF_F_DYNSTR flag is set in the
374*e663ba6eSchristos    CTF preamble (which it almost always will be for linked objects, but not for
375*e663ba6eSchristos    .o files).  If you use ctf_arc_bufopen and do not specify symbol/string
376*e663ba6eSchristos    tables, the ctf_*_lookuup_symbol functions will fail with ECTF_NOSYMTAB.
377*e663ba6eSchristos 
378*e663ba6eSchristos    Like many other convenient opening functions, ctf_arc_open needs BFD and is
379*e663ba6eSchristos    not available in libctf-nobfd.  */
380*e663ba6eSchristos 
381*e663ba6eSchristos extern ctf_archive_t *ctf_arc_open (const char *, int *);
382*e663ba6eSchristos extern ctf_archive_t *ctf_arc_bufopen (const ctf_sect_t *ctfsect,
383*e663ba6eSchristos 				       const ctf_sect_t *symsect,
384*e663ba6eSchristos 				       const ctf_sect_t *strsect,
385*e663ba6eSchristos 				       int *);
3868dffb485Schristos extern void ctf_arc_close (ctf_archive_t *);
387*e663ba6eSchristos 
388*e663ba6eSchristos /* Get the archive a given dictionary came from (if any).  */
389*e663ba6eSchristos 
390*e663ba6eSchristos extern ctf_archive_t *ctf_get_arc (const ctf_dict_t *);
391*e663ba6eSchristos 
392*e663ba6eSchristos /* Return the number of members in an archive.  */
393*e663ba6eSchristos 
394*e663ba6eSchristos extern size_t ctf_archive_count (const ctf_archive_t *);
395*e663ba6eSchristos 
396*e663ba6eSchristos /* Open a dictionary with a given name, given a CTF archive and
397*e663ba6eSchristos    optionally symbol and string table sections to accompany it (if the
398*e663ba6eSchristos    archive was oriiginally opened from an ELF file via ctf_open*, or
399*e663ba6eSchristos    if string or symbol tables were explicitly passed when the archive
400*e663ba6eSchristos    was opened, this can be used to override that choice).  The dict
401*e663ba6eSchristos    should be closed with ctf_dict_close() when done.
402*e663ba6eSchristos 
403*e663ba6eSchristos    (The low-level functions ctf_simple_open and ctf_bufopen return
404*e663ba6eSchristos    ctf_dict_t's directly, and cannot be used on CTF archives: use these
405*e663ba6eSchristos    functions instead.)  */
406*e663ba6eSchristos 
407*e663ba6eSchristos extern ctf_dict_t *ctf_dict_open (const ctf_archive_t *,
408*e663ba6eSchristos 				  const char *, int *);
409*e663ba6eSchristos extern ctf_dict_t *ctf_dict_open_sections (const ctf_archive_t *,
410*e663ba6eSchristos 					   const ctf_sect_t *symsect,
411*e663ba6eSchristos 					   const ctf_sect_t *strsect,
412*e663ba6eSchristos 					   const char *, int *);
413*e663ba6eSchristos 
414*e663ba6eSchristos /* Look up symbols' types in archives by index or name, returning the dict
415*e663ba6eSchristos    and optionally type ID in which the type is found.  Lookup results are
416*e663ba6eSchristos    cached so future lookups are faster.  Needs symbol tables and (for name
417*e663ba6eSchristos    lookups) string tables to be known for this CTF archive.  */
418*e663ba6eSchristos 
4194b169a6bSchristos extern ctf_dict_t *ctf_arc_lookup_symbol (ctf_archive_t *,
4204b169a6bSchristos 					  unsigned long symidx,
4214b169a6bSchristos 					  ctf_id_t *, int *errp);
4224b169a6bSchristos extern ctf_dict_t *ctf_arc_lookup_symbol_name (ctf_archive_t *,
4234b169a6bSchristos 					       const char *name,
4244b169a6bSchristos 					       ctf_id_t *, int *errp);
4254b169a6bSchristos extern void ctf_arc_flush_caches (ctf_archive_t *);
4268dffb485Schristos 
427*e663ba6eSchristos /* The next functions return or close real CTF files, or write out CTF
428*e663ba6eSchristos    archives, not archives or ELF files containing CTF content.  As with
429*e663ba6eSchristos    ctf_dict_open_sections, they can be passed symbol and string table
430*e663ba6eSchristos    sections.  */
4318dffb485Schristos 
432*e663ba6eSchristos extern ctf_dict_t *ctf_simple_open (const char *ctfsect, size_t ctfsect_size,
433*e663ba6eSchristos 				    const char *symsect, size_t symsect_size,
434*e663ba6eSchristos 				    size_t symsect_entsize,
435*e663ba6eSchristos 				    const char *strsect, size_t strsect_size,
436*e663ba6eSchristos 				    int *errp);
437*e663ba6eSchristos extern ctf_dict_t *ctf_bufopen (const ctf_sect_t *ctfsect,
438*e663ba6eSchristos 				const ctf_sect_t *symsect,
439*e663ba6eSchristos 				const ctf_sect_t *strsect, int *);
4404b169a6bSchristos extern void ctf_ref (ctf_dict_t *);
4414b169a6bSchristos extern void ctf_dict_close (ctf_dict_t *);
4428dffb485Schristos 
443*e663ba6eSchristos /* CTF dicts may be in a parent/child relationship, where the child dicts
444*e663ba6eSchristos    contain the name of their originating compilation unit and the name of
445*e663ba6eSchristos    their parent.  Dicts opened from CTF archives have this relationship set
446*e663ba6eSchristos    up already, but if opening via raw low-level calls, you need to figure
447*e663ba6eSchristos    out which dict is the parent and set it on the child via ctf_import(). */
4488dffb485Schristos 
4494b169a6bSchristos extern const char *ctf_cuname (ctf_dict_t *);
4504b169a6bSchristos extern ctf_dict_t *ctf_parent_dict (ctf_dict_t *);
4514b169a6bSchristos extern const char *ctf_parent_name (ctf_dict_t *);
4524b169a6bSchristos extern int ctf_type_isparent (ctf_dict_t *, ctf_id_t);
4534b169a6bSchristos extern int ctf_type_ischild (ctf_dict_t *, ctf_id_t);
4544b169a6bSchristos extern int ctf_import (ctf_dict_t *, ctf_dict_t *);
455*e663ba6eSchristos 
456*e663ba6eSchristos /* Set these names (used when creating dicts).  */
457*e663ba6eSchristos 
458*e663ba6eSchristos extern int ctf_cuname_set (ctf_dict_t *, const char *);
459*e663ba6eSchristos extern int ctf_parent_name_set (ctf_dict_t *, const char *);
460*e663ba6eSchristos 
461*e663ba6eSchristos /* Set and get the CTF data model (see above).  */
462*e663ba6eSchristos 
4634b169a6bSchristos extern int ctf_setmodel (ctf_dict_t *, int);
4644b169a6bSchristos extern int ctf_getmodel (ctf_dict_t *);
4658dffb485Schristos 
466*e663ba6eSchristos /* CTF dicts can carry a single (in-memory-only) non-persistent pointer to
467*e663ba6eSchristos    arbitrary data.  No meaning is attached to this data and the dict does
468*e663ba6eSchristos    not own it: nothing is done to it when the dict is closed.  */
469*e663ba6eSchristos 
4704b169a6bSchristos extern void ctf_setspecific (ctf_dict_t *, void *);
4714b169a6bSchristos extern void *ctf_getspecific (ctf_dict_t *);
4728dffb485Schristos 
473*e663ba6eSchristos /* Error handling.  ctf dicts carry a system errno value or one of the
474*e663ba6eSchristos    CTF_ERRORS above, which are returned via ctf_errno.  The return value of
475*e663ba6eSchristos    ctf_errno is only meaningful when the immediately preceding CTF function
476*e663ba6eSchristos    call returns an error code.
477*e663ba6eSchristos 
478*e663ba6eSchristos    There are four possible sorts of error return:
479*e663ba6eSchristos 
480*e663ba6eSchristos     - From opening functions, a return value of NULL and the error returned
481*e663ba6eSchristos       via an errp instead of via ctf_errno; all other functions return return
482*e663ba6eSchristos       errors via ctf_errno.
483*e663ba6eSchristos 
484*e663ba6eSchristos     - Functions returning a ctf_id_t are in error if the return value == CTF_ERR
485*e663ba6eSchristos     - Functions returning an int are in error if their return value < 0
486*e663ba6eSchristos     - Functions returning a pointer are in error if their return value ==
487*e663ba6eSchristos       NULL.  */
488*e663ba6eSchristos 
4894b169a6bSchristos extern int ctf_errno (ctf_dict_t *);
4908dffb485Schristos extern const char *ctf_errmsg (int);
491*e663ba6eSchristos 
492*e663ba6eSchristos /* Return the version of CTF dicts written by writeout functions.  The
493*e663ba6eSchristos    argument must currently be zero.  All dicts with versions below the value
494*e663ba6eSchristos    returned by this function can be read by the library.  CTF dicts written
495*e663ba6eSchristos    by other non-GNU CTF libraries (e.g. that in FreeBSD) are not compatible
496*e663ba6eSchristos    and cannot be read by this library.  */
497*e663ba6eSchristos 
4988dffb485Schristos extern int ctf_version (int);
4998dffb485Schristos 
500*e663ba6eSchristos /* Given a symbol table index corresponding to a function symbol, return info on
501*e663ba6eSchristos    the type of a given function's arguments or return value.  Vararg functions
502*e663ba6eSchristos    have a final arg with CTF_FUNC_VARARG on in ctc_flags.  */
503*e663ba6eSchristos 
5044b169a6bSchristos extern int ctf_func_info (ctf_dict_t *, unsigned long, ctf_funcinfo_t *);
5054b169a6bSchristos extern int ctf_func_args (ctf_dict_t *, unsigned long, uint32_t, ctf_id_t *);
506*e663ba6eSchristos 
507*e663ba6eSchristos /* As above, but for CTF_K_FUNCTION types in CTF dicts.  */
508*e663ba6eSchristos 
5094b169a6bSchristos extern int ctf_func_type_info (ctf_dict_t *, ctf_id_t, ctf_funcinfo_t *);
5104b169a6bSchristos extern int ctf_func_type_args (ctf_dict_t *, ctf_id_t, uint32_t, ctf_id_t *);
5118dffb485Schristos 
512*e663ba6eSchristos /* Look up function or data symbols by name and return their CTF type ID,
513*e663ba6eSchristos   if any.  (For both function symbols and data symbols that are function
514*e663ba6eSchristos   pointers, the types are of kind CTF_K_FUNCTION.)  */
515*e663ba6eSchristos 
5164b169a6bSchristos extern ctf_id_t ctf_lookup_by_symbol (ctf_dict_t *, unsigned long);
5174b169a6bSchristos extern ctf_id_t ctf_lookup_by_symbol_name (ctf_dict_t *, const char *);
518*e663ba6eSchristos 
519*e663ba6eSchristos /* Traverse all (function or data) symbols in a dict, one by one, and return the
520*e663ba6eSchristos    type of each and (if NAME is non-NULL) optionally its name.  */
521*e663ba6eSchristos 
5224b169a6bSchristos extern ctf_id_t ctf_symbol_next (ctf_dict_t *, ctf_next_t **,
5234b169a6bSchristos 				 const char **name, int functions);
524*e663ba6eSchristos 
525*e663ba6eSchristos /* Look up a type by name: some simple C type parsing is done, but this is by no
526*e663ba6eSchristos    means comprehensive.  Structures, unions and enums need "struct ", "union "
527*e663ba6eSchristos    or "enum " on the front, as usual in C.  */
528*e663ba6eSchristos 
529*e663ba6eSchristos extern ctf_id_t ctf_lookup_by_name (ctf_dict_t *, const char *);
530*e663ba6eSchristos 
531*e663ba6eSchristos /* Look up a variable, which is a name -> type mapping with no specific
532*e663ba6eSchristos    relationship to a symbol table.  Before linking, everything with types in the
533*e663ba6eSchristos    symbol table will be in the variable table as well; after linking, only those
534*e663ba6eSchristos    typed functions and data objects that are not asssigned to symbols by the
535*e663ba6eSchristos    linker are left in the variable table here.  */
536*e663ba6eSchristos 
5374b169a6bSchristos extern ctf_id_t ctf_lookup_variable (ctf_dict_t *, const char *);
5388dffb485Schristos 
539*e663ba6eSchristos /* Type lookup functions.  */
540*e663ba6eSchristos 
541*e663ba6eSchristos /* Strip qualifiers and typedefs off a type, returning the base type.
542*e663ba6eSchristos 
543*e663ba6eSchristos    Stripping also stops when we hit slices (see ctf_add_slice below), so it is
544*e663ba6eSchristos    possible (given a chain looking like const -> slice -> typedef -> int) to
545*e663ba6eSchristos    still have a typedef after you're done with this, but in that case it is a
546*e663ba6eSchristos    typedef of a type with a *different width* (because this slice has not been
547*e663ba6eSchristos    applied to it).
548*e663ba6eSchristos 
549*e663ba6eSchristos    Most of the time you don't need to call this: the type-querying functions
550*e663ba6eSchristos    will do it for you (as noted below).  */
551*e663ba6eSchristos 
5524b169a6bSchristos extern ctf_id_t ctf_type_resolve (ctf_dict_t *, ctf_id_t);
553*e663ba6eSchristos 
554*e663ba6eSchristos /* Get the name of a type, including any const/volatile/restrict qualifiers
555*e663ba6eSchristos    (cvr-quals), and return it as a new dynamically-allocated string.
556*e663ba6eSchristos    (The 'a' stands for 'a'llocated.) */
557*e663ba6eSchristos 
5584b169a6bSchristos extern char *ctf_type_aname (ctf_dict_t *, ctf_id_t);
559*e663ba6eSchristos 
560*e663ba6eSchristos /* As above, but with no cvr-quals.  */
561*e663ba6eSchristos 
5624b169a6bSchristos extern char *ctf_type_aname_raw (ctf_dict_t *, ctf_id_t);
563*e663ba6eSchristos 
564*e663ba6eSchristos /* A raw name that is owned by the ctf_dict_t and will live as long as it
565*e663ba6eSchristos    does.  Do not change the value this function returns!  */
566*e663ba6eSchristos 
5674b169a6bSchristos extern const char *ctf_type_name_raw (ctf_dict_t *, ctf_id_t);
568*e663ba6eSchristos 
569*e663ba6eSchristos /* Like ctf_type_aname, but print the string into the passed buffer, truncating
570*e663ba6eSchristos    if necessary and setting ECTF_NAMELEN on the errno: return the actual number
571*e663ba6eSchristos    of bytes needed (not including the trailing \0).  Consider using
572*e663ba6eSchristos    ctf_type_aname instead.  */
573*e663ba6eSchristos 
574*e663ba6eSchristos extern ssize_t ctf_type_lname (ctf_dict_t *, ctf_id_t, char *, size_t);
575*e663ba6eSchristos 
576*e663ba6eSchristos /* Like ctf_type_lname, but return the string, or NULL if truncated.
577*e663ba6eSchristos    Consider using ctf_type_aname instead.  */
578*e663ba6eSchristos 
579*e663ba6eSchristos extern char *ctf_type_name (ctf_dict_t *, ctf_id_t, char *, size_t);
580*e663ba6eSchristos 
581*e663ba6eSchristos /* Return the size or alignment of a type.  Types with no meaningful size, like
582*e663ba6eSchristos    function types, return 0 as their size; incomplete types set ECTF_INCOMPLETE.
583*e663ba6eSchristos    The type is resolved for you, so cvr-quals and typedefs can be passsed in.  */
584*e663ba6eSchristos 
5854b169a6bSchristos extern ssize_t ctf_type_size (ctf_dict_t *, ctf_id_t);
5864b169a6bSchristos extern ssize_t ctf_type_align (ctf_dict_t *, ctf_id_t);
587*e663ba6eSchristos 
588*e663ba6eSchristos /* Return the kind of a type (CTF_K_* constant).  Slices are considered to be
589*e663ba6eSchristos    the kind they are a slice of.  Forwards to incomplete structs, etc, return
590*e663ba6eSchristos    CTF_K_FORWARD (but deduplication resolves most forwards to their concrete
591*e663ba6eSchristos    types).  */
592*e663ba6eSchristos 
5934b169a6bSchristos extern int ctf_type_kind (ctf_dict_t *, ctf_id_t);
594*e663ba6eSchristos 
595*e663ba6eSchristos /* Return the kind of a type (CTF_K_* constant).  Slices are considered to be
596*e663ba6eSchristos    the kind they are a slice of; forwards are considered to be the kind they are
597*e663ba6eSchristos    a forward of.  */
598*e663ba6eSchristos 
5994b169a6bSchristos extern int ctf_type_kind_forwarded (ctf_dict_t *, ctf_id_t);
600*e663ba6eSchristos 
601*e663ba6eSchristos /* Return the type a pointer, typedef, cvr-qual, or slice refers to, or return
602*e663ba6eSchristos    an ECTF_NOTREF error otherwise.  ctf_type_kind pretends that slices are
603*e663ba6eSchristos    actually the type they are a slice of: this is usually want you want, but if
604*e663ba6eSchristos    you want to find out if a type was actually a slice of some (usually-wider)
605*e663ba6eSchristos    base type, you can call ctf_type_reference on it: a non-error return means
606*e663ba6eSchristos    it was a slice.  */
607*e663ba6eSchristos 
6084b169a6bSchristos extern ctf_id_t ctf_type_reference (ctf_dict_t *, ctf_id_t);
609*e663ba6eSchristos 
610*e663ba6eSchristos /* Return the encoding of a given type.  No attempt is made to resolve the
611*e663ba6eSchristos    type first, so passing in typedefs etc will yield an error.  */
612*e663ba6eSchristos 
6134b169a6bSchristos extern int ctf_type_encoding (ctf_dict_t *, ctf_id_t, ctf_encoding_t *);
614*e663ba6eSchristos 
615*e663ba6eSchristos /* Given a type, return some other type that is a pointer to this type (if any
616*e663ba6eSchristos    exists), or return ECTF_NOTYPE otherwise.  If non exists, try resolving away
617*e663ba6eSchristos    typedefs and cvr-quals and check again (so if you call this on foo_t, you
618*e663ba6eSchristos    might get back foo *).  No attempt is made to hunt for pointers to qualified
619*e663ba6eSchristos    versions of the type passed in.  */
620*e663ba6eSchristos 
621*e663ba6eSchristos extern ctf_id_t ctf_type_pointer (ctf_dict_t *, ctf_id_t);
622*e663ba6eSchristos 
623*e663ba6eSchristos /* Return 1 if two types are assignment-compatible.  */
624*e663ba6eSchristos 
6254b169a6bSchristos extern int ctf_type_compat (ctf_dict_t *, ctf_id_t, ctf_dict_t *, ctf_id_t);
6268dffb485Schristos 
627*e663ba6eSchristos /* Recursively visit the members of any type, calling the ctf_visit_f for each.  */
628*e663ba6eSchristos 
629*e663ba6eSchristos extern int ctf_type_visit (ctf_dict_t *, ctf_id_t, ctf_visit_f *, void *);
630*e663ba6eSchristos 
631*e663ba6eSchristos /* Comparison function that defines an ordering over types.  If the types are in
632*e663ba6eSchristos    different dicts, the ordering may vary between different openings of the same
633*e663ba6eSchristos    dicts.  */
634*e663ba6eSchristos 
635*e663ba6eSchristos extern int ctf_type_cmp (ctf_dict_t *, ctf_id_t, ctf_dict_t *, ctf_id_t);
636*e663ba6eSchristos 
637*e663ba6eSchristos /* Get the name of an enumerator given its value, or vice versa.  If many
638*e663ba6eSchristos    enumerators have the same value, the first with that value is returned.  */
6398dffb485Schristos 
6404b169a6bSchristos extern const char *ctf_enum_name (ctf_dict_t *, ctf_id_t, int);
6414b169a6bSchristos extern int ctf_enum_value (ctf_dict_t *, ctf_id_t, const char *, int *);
6428dffb485Schristos 
643*e663ba6eSchristos /* Get the size and member type of an array.  */
6448dffb485Schristos 
645*e663ba6eSchristos extern int ctf_array_info (ctf_dict_t *, ctf_id_t, ctf_arinfo_t *);
6468dffb485Schristos 
647*e663ba6eSchristos /* Get info on specific named members of structs or unions, and count the number
648*e663ba6eSchristos    of members in a struct, union, or enum.  */
649*e663ba6eSchristos 
650*e663ba6eSchristos extern int ctf_member_info (ctf_dict_t *, ctf_id_t, const char *,
651*e663ba6eSchristos 			    ctf_membinfo_t *);
6524b169a6bSchristos extern int ctf_member_count (ctf_dict_t *, ctf_id_t);
653*e663ba6eSchristos 
654*e663ba6eSchristos /* Iterators.  */
655*e663ba6eSchristos 
656*e663ba6eSchristos /* ctf_member_next is a _next-style iterator that can additionally traverse into
657*e663ba6eSchristos    the members of unnamed structs nested within this struct as if they were
658*e663ba6eSchristos    direct members, if CTF_MN_RECURSE is passed in the flags.  */
659*e663ba6eSchristos 
6604b169a6bSchristos extern int ctf_member_iter (ctf_dict_t *, ctf_id_t, ctf_member_f *, void *);
6614b169a6bSchristos extern ssize_t ctf_member_next (ctf_dict_t *, ctf_id_t, ctf_next_t **,
6624b169a6bSchristos 				const char **name, ctf_id_t *membtype,
6634b169a6bSchristos 				int flags);
6644b169a6bSchristos extern int ctf_enum_iter (ctf_dict_t *, ctf_id_t, ctf_enum_f *, void *);
6654b169a6bSchristos extern const char *ctf_enum_next (ctf_dict_t *, ctf_id_t, ctf_next_t **,
6668dffb485Schristos 				  int *);
667*e663ba6eSchristos 
668*e663ba6eSchristos /* Iterate over all types in a dict.  ctf_type_iter_all recurses over all types:
669*e663ba6eSchristos    ctf_type_iter recurses only over types with user-visible names (for which
670*e663ba6eSchristos    CTF_ADD_ROOT was passed).  All such types are returned, even if they are
671*e663ba6eSchristos    things like pointers that intrinsically have no name: this is the only effect
672*e663ba6eSchristos    of CTF_ADD_ROOT for such types.  ctf_type_next allows you to choose whether
673*e663ba6eSchristos    to see hidden types or not with the want_hidden arg: if set, the flag (if
674*e663ba6eSchristos    passed) returns the hidden state of each type in turn.  */
675*e663ba6eSchristos 
6764b169a6bSchristos extern int ctf_type_iter (ctf_dict_t *, ctf_type_f *, void *);
6774b169a6bSchristos extern int ctf_type_iter_all (ctf_dict_t *, ctf_type_all_f *, void *);
6784b169a6bSchristos extern ctf_id_t ctf_type_next (ctf_dict_t *, ctf_next_t **,
6798dffb485Schristos 			       int *flag, int want_hidden);
680*e663ba6eSchristos 
6814b169a6bSchristos extern int ctf_variable_iter (ctf_dict_t *, ctf_variable_f *, void *);
6824b169a6bSchristos extern ctf_id_t ctf_variable_next (ctf_dict_t *, ctf_next_t **,
6838dffb485Schristos 				   const char **);
684*e663ba6eSchristos 
685*e663ba6eSchristos /* ctf_archive_iter and ctf_archive_next open each member dict for you,
686*e663ba6eSchristos    automatically importing any parent dict as usual: ctf_archive_iter closes the
687*e663ba6eSchristos    dict on return from ctf_archive_member_f, but for ctf_archive_next the caller
688*e663ba6eSchristos    must close each dict returned.  If skip_parent is set, the parent dict is
689*e663ba6eSchristos    skipped on the basis that it's already been seen in every child dict (but if
690*e663ba6eSchristos    no child dicts exist, this will lead to nothing being returned).
691*e663ba6eSchristos 
692*e663ba6eSchristos    If an open fails, ctf_archive_iter returns -1 early (losing the error), but
693*e663ba6eSchristos    ctf_archive_next both passes back the error in the passed errp and allows you
694*e663ba6eSchristos    to iterate past errors (until the usual ECTF_NEXT_END is returned).  */
695*e663ba6eSchristos 
6968dffb485Schristos extern int ctf_archive_iter (const ctf_archive_t *, ctf_archive_member_f *,
6978dffb485Schristos 			     void *);
6984b169a6bSchristos extern ctf_dict_t *ctf_archive_next (const ctf_archive_t *, ctf_next_t **,
6998dffb485Schristos 				     const char **, int skip_parent, int *errp);
7008dffb485Schristos 
701*e663ba6eSchristos /* Pass the raw content of each archive member in turn to
702*e663ba6eSchristos    ctf_archive_raw_member_f.
703*e663ba6eSchristos 
704*e663ba6eSchristos    This function alone does not currently operate on CTF files masquerading as
705*e663ba6eSchristos    archives, and returns -EINVAL: the raw data is no longer available.  It is
7068dffb485Schristos    expected to be used only by archiving tools, in any case, which have no need
707*e663ba6eSchristos    to deal with non-archives at all.  (There is currently no _next analogue of
708*e663ba6eSchristos    this function.)  */
709*e663ba6eSchristos 
7108dffb485Schristos extern int ctf_archive_raw_iter (const ctf_archive_t *,
7118dffb485Schristos 				 ctf_archive_raw_member_f *, void *);
712*e663ba6eSchristos 
713*e663ba6eSchristos /* Dump the contents of a section in a CTF dict.  STATE is an
714*e663ba6eSchristos    iterator which should be a pointer to a variable set to NULL.  The decorator
715*e663ba6eSchristos    is called with each line in turn and can modify it or allocate and return a
716*e663ba6eSchristos    new one.  ctf_dump accumulates all the results and returns a single giant
717*e663ba6eSchristos    multiline string.  */
718*e663ba6eSchristos 
7194b169a6bSchristos extern char *ctf_dump (ctf_dict_t *, ctf_dump_state_t **state,
7208dffb485Schristos 		       ctf_sect_names_t sect, ctf_dump_decorate_f *,
7218dffb485Schristos 		       void *arg);
7228dffb485Schristos 
7238dffb485Schristos /* Error-warning reporting: an 'iterator' that returns errors and warnings from
7248dffb485Schristos    the error/warning list, in order of emission.  Errors and warnings are popped
7258dffb485Schristos    after return: the caller must free the returned error-text pointer.  */
7264b169a6bSchristos extern char *ctf_errwarning_next (ctf_dict_t *, ctf_next_t **,
7278dffb485Schristos 				  int *is_warning, int *errp);
7288dffb485Schristos 
729*e663ba6eSchristos /* Creation.  */
730*e663ba6eSchristos 
731*e663ba6eSchristos /* Create a new, empty dict.  If creation fails, return NULL and put a CTF error
732*e663ba6eSchristos    code in the passed-in int (if set).  */
733*e663ba6eSchristos extern ctf_dict_t *ctf_create (int *);
734*e663ba6eSchristos 
735*e663ba6eSchristos /* Add specific types to a dict.  You can add new types to any dict, but you can
736*e663ba6eSchristos    only add members to types that have been added since this dict was read in
737*e663ba6eSchristos    (you cannot read in a dict, look up a type in it, then add members to
738*e663ba6eSchristos    it).  All adding functions take a uint32_t CTF_ADD_ROOT / CTF_ADD_NONROOT
739*e663ba6eSchristos    flag to indicate whether this type should be visible to name lookups via
740*e663ba6eSchristos    ctf_lookup_by_name et al.  */
741*e663ba6eSchristos 
7424b169a6bSchristos extern ctf_id_t ctf_add_array (ctf_dict_t *, uint32_t,
7438dffb485Schristos 			       const ctf_arinfo_t *);
7444b169a6bSchristos extern ctf_id_t ctf_add_const (ctf_dict_t *, uint32_t, ctf_id_t);
7454b169a6bSchristos extern ctf_id_t ctf_add_enum_encoded (ctf_dict_t *, uint32_t, const char *,
7468dffb485Schristos 				      const ctf_encoding_t *);
7474b169a6bSchristos extern ctf_id_t ctf_add_enum (ctf_dict_t *, uint32_t, const char *);
7484b169a6bSchristos extern ctf_id_t ctf_add_float (ctf_dict_t *, uint32_t,
7498dffb485Schristos 			       const char *, const ctf_encoding_t *);
7504b169a6bSchristos extern ctf_id_t ctf_add_forward (ctf_dict_t *, uint32_t, const char *,
7518dffb485Schristos 				 uint32_t);
7524b169a6bSchristos extern ctf_id_t ctf_add_function (ctf_dict_t *, uint32_t,
7538dffb485Schristos 				  const ctf_funcinfo_t *, const ctf_id_t *);
7544b169a6bSchristos extern ctf_id_t ctf_add_integer (ctf_dict_t *, uint32_t, const char *,
7558dffb485Schristos 				 const ctf_encoding_t *);
756*e663ba6eSchristos 
757*e663ba6eSchristos /* Add a "slice", which wraps some integral type and changes its encoding
758*e663ba6eSchristos    (useful for bitfields, etc).  In most respects slices are treated the same
759*e663ba6eSchristos    kind as the type they wrap: only ctf_type_reference can see the difference,
760*e663ba6eSchristos    returning the wrapped type.  */
761*e663ba6eSchristos 
7624b169a6bSchristos extern ctf_id_t ctf_add_slice (ctf_dict_t *, uint32_t, ctf_id_t, const ctf_encoding_t *);
7634b169a6bSchristos extern ctf_id_t ctf_add_pointer (ctf_dict_t *, uint32_t, ctf_id_t);
7644b169a6bSchristos extern ctf_id_t ctf_add_type (ctf_dict_t *, ctf_dict_t *, ctf_id_t);
7654b169a6bSchristos extern ctf_id_t ctf_add_typedef (ctf_dict_t *, uint32_t, const char *,
7668dffb485Schristos 				 ctf_id_t);
7674b169a6bSchristos extern ctf_id_t ctf_add_restrict (ctf_dict_t *, uint32_t, ctf_id_t);
768*e663ba6eSchristos 
769*e663ba6eSchristos /* Struct and union addition.  Straight addition uses possibly-confusing rules
770*e663ba6eSchristos    to guess the final size of the struct/union given its members: to explicitly
771*e663ba6eSchristos    state the size of the struct or union (to report compiler-generated padding,
772*e663ba6eSchristos    etc) use the _sized variants.  */
773*e663ba6eSchristos 
7744b169a6bSchristos extern ctf_id_t ctf_add_struct (ctf_dict_t *, uint32_t, const char *);
7754b169a6bSchristos extern ctf_id_t ctf_add_union (ctf_dict_t *, uint32_t, const char *);
7764b169a6bSchristos extern ctf_id_t ctf_add_struct_sized (ctf_dict_t *, uint32_t, const char *,
7778dffb485Schristos 				      size_t);
7784b169a6bSchristos extern ctf_id_t ctf_add_union_sized (ctf_dict_t *, uint32_t, const char *,
7798dffb485Schristos 				     size_t);
780*e663ba6eSchristos 
781*e663ba6eSchristos /* Note that CTF cannot encode a given type.  This usually returns an
782*e663ba6eSchristos    ECTF_NONREPRESENTABLE error when queried.  Mostly useful for struct members,
783*e663ba6eSchristos    variables, etc, to point to.  */
784*e663ba6eSchristos 
7854b169a6bSchristos extern ctf_id_t ctf_add_unknown (ctf_dict_t *, uint32_t, const char *);
7864b169a6bSchristos extern ctf_id_t ctf_add_volatile (ctf_dict_t *, uint32_t, ctf_id_t);
7878dffb485Schristos 
788*e663ba6eSchristos /* Add an enumerator to an enum (the name is a misnomer).  We do not currently
789*e663ba6eSchristos    validate that enumerators have unique names, even though C requires it: in
790*e663ba6eSchristos    future this may change.  */
791*e663ba6eSchristos 
7924b169a6bSchristos extern int ctf_add_enumerator (ctf_dict_t *, ctf_id_t, const char *, int);
793*e663ba6eSchristos 
794*e663ba6eSchristos /* Add a member to a struct or union, either at the next available offset (with
795*e663ba6eSchristos    suitable padding for the alignment) or at a specific offset, and possibly
796*e663ba6eSchristos    with a specific encoding (creating a slice for you).  Offsets need not be
797*e663ba6eSchristos    unique, and need not be added in ascending order.  */
798*e663ba6eSchristos 
7994b169a6bSchristos extern int ctf_add_member (ctf_dict_t *, ctf_id_t, const char *, ctf_id_t);
8004b169a6bSchristos extern int ctf_add_member_offset (ctf_dict_t *, ctf_id_t, const char *,
8018dffb485Schristos 				  ctf_id_t, unsigned long);
8024b169a6bSchristos extern int ctf_add_member_encoded (ctf_dict_t *, ctf_id_t, const char *,
8038dffb485Schristos 				   ctf_id_t, unsigned long,
8048dffb485Schristos 				   const ctf_encoding_t);
8058dffb485Schristos 
8064b169a6bSchristos extern int ctf_add_variable (ctf_dict_t *, const char *, ctf_id_t);
8078dffb485Schristos 
808*e663ba6eSchristos /* Set the size and member and index types of an array.  */
8098dffb485Schristos 
8104b169a6bSchristos extern int ctf_set_array (ctf_dict_t *, ctf_id_t, const ctf_arinfo_t *);
8118dffb485Schristos 
812*e663ba6eSchristos /* Add a function oor object symbol type with a particular name, without saying
813*e663ba6eSchristos    anything about the actual symbol index.  (The linker will then associate them
814*e663ba6eSchristos    with actual symbol indexes using the ctf_link functions below.)  */
815*e663ba6eSchristos 
816*e663ba6eSchristos extern int ctf_add_objt_sym (ctf_dict_t *, const char *, ctf_id_t);
817*e663ba6eSchristos extern int ctf_add_func_sym (ctf_dict_t *, const char *, ctf_id_t);
818*e663ba6eSchristos 
819*e663ba6eSchristos /* Snapshot/rollback.  Call ctf_update to snapshot the state of a dict:
820*e663ba6eSchristos   a later call to ctf_discard then deletes all types added since (but not new
821*e663ba6eSchristos   members, enumerands etc).  Call ctf_snapshot to return a snapshot ID: pass
822*e663ba6eSchristos   one of these IDs to ctf_rollback to discard all types added since the
823*e663ba6eSchristos   corresponding call to ctf_snapshot.  */
824*e663ba6eSchristos 
8254b169a6bSchristos extern int ctf_update (ctf_dict_t *);
8264b169a6bSchristos extern ctf_snapshot_id_t ctf_snapshot (ctf_dict_t *);
8274b169a6bSchristos extern int ctf_rollback (ctf_dict_t *, ctf_snapshot_id_t);
8284b169a6bSchristos extern int ctf_discard (ctf_dict_t *);
829*e663ba6eSchristos 
830*e663ba6eSchristos /* Dict writeout.
831*e663ba6eSchristos 
832*e663ba6eSchristos    ctf_write: write out an uncompressed dict to an fd.
833*e663ba6eSchristos    ctf_compress_write: write out a compressed dict to an fd (currently always
834*e663ba6eSchristos    gzip, but this may change in future).
835*e663ba6eSchristos    ctf_write_mem: write out a dict to a buffer and return it and its size,
836*e663ba6eSchristos    compressing it if its uncompressed size is over THRESHOLD.  */
837*e663ba6eSchristos 
8384b169a6bSchristos extern int ctf_write (ctf_dict_t *, int);
8394b169a6bSchristos extern int ctf_compress_write (ctf_dict_t * fp, int fd);
8404b169a6bSchristos extern unsigned char *ctf_write_mem (ctf_dict_t *, size_t *, size_t threshold);
8414b169a6bSchristos 
842*e663ba6eSchristos /* Create a CTF archive named FILE from CTF_DICTS inputs with NAMES (or write it
843*e663ba6eSchristos    to the passed-in fd).  */
844*e663ba6eSchristos 
845*e663ba6eSchristos extern int ctf_arc_write (const char *file, ctf_dict_t **ctf_dicts, size_t,
846*e663ba6eSchristos 			  const char **names, size_t);
847*e663ba6eSchristos extern int ctf_arc_write_fd (int, ctf_dict_t **, size_t, const char **,
848*e663ba6eSchristos 			     size_t);
849*e663ba6eSchristos 
850*e663ba6eSchristos /* Linking.  These functions are used by ld to link .ctf sections in input
851*e663ba6eSchristos    object files into a single .ctf section which is an archive possibly
852*e663ba6eSchristos    containing members containing types whose names collide across multiple
853*e663ba6eSchristos    compilation units, but they are usable by other programs as well and are not
854*e663ba6eSchristos    private to the linker.  */
855*e663ba6eSchristos 
856*e663ba6eSchristos /* Add a CTF archive to the link with a given NAME (usually the name of the
857*e663ba6eSchristos    containing object file).  The dict added to is usually a new dict created
858*e663ba6eSchristos    with ctf_create which will be filled with types corresponding to the shared
859*e663ba6eSchristos    dict in the output (conflicting types in child dicts in the output archive
860*e663ba6eSchristos    are stored in internal space inside this dict, but are not easily visible
861*e663ba6eSchristos    until after ctf_link_write below).
862*e663ba6eSchristos 
863*e663ba6eSchristos    The NAME need not be unique (but usually is).  */
864*e663ba6eSchristos 
865*e663ba6eSchristos extern int ctf_link_add_ctf (ctf_dict_t *, ctf_archive_t *, const char *name);
866*e663ba6eSchristos 
867*e663ba6eSchristos /* Do the deduplicating link, filling the dict with types.  The FLAGS are the
868*e663ba6eSchristos    CTF_LINK_* flags above.  */
869*e663ba6eSchristos 
8704b169a6bSchristos extern int ctf_link (ctf_dict_t *, int flags);
871*e663ba6eSchristos 
872*e663ba6eSchristos /* Symtab linker handling, called after ctf_link to set up the symbol type
873*e663ba6eSchristos    information used by ctf_*_lookup_symbol.  */
874*e663ba6eSchristos 
875*e663ba6eSchristos /* Add strings to the link from the ELF string table, repeatedly calling
876*e663ba6eSchristos    ADD_STRING to add each string and its corresponding offset in turn.  */
877*e663ba6eSchristos 
8788dffb485Schristos typedef const char *ctf_link_strtab_string_f (uint32_t *offset, void *arg);
879*e663ba6eSchristos extern int ctf_link_add_strtab (ctf_dict_t *,
880*e663ba6eSchristos 				ctf_link_strtab_string_f *add_string, void *);
881*e663ba6eSchristos 
882*e663ba6eSchristos /* Note that a given symbol will be public with a given set of properties.
883*e663ba6eSchristos    If the symbol has been added with that name via ctf_add_{func,objt}_sym,
884*e663ba6eSchristos    this symbol type will end up in the symtypetabs and can be looked up via
885*e663ba6eSchristos    ctf_*_lookup_symbol after the dict is read back in.  */
886*e663ba6eSchristos 
8874b169a6bSchristos extern int ctf_link_add_linker_symbol (ctf_dict_t *, ctf_link_sym_t *);
888*e663ba6eSchristos 
889*e663ba6eSchristos /* Impose an ordering on symbols, as defined by the strtab and symbol
890*e663ba6eSchristos    added by earlier calls to the above two functions.  */
891*e663ba6eSchristos 
8924b169a6bSchristos extern int ctf_link_shuffle_syms (ctf_dict_t *);
893*e663ba6eSchristos 
894*e663ba6eSchristos /* Return the serialized form of this ctf_linked dict as a new
895*e663ba6eSchristos    dynamically-allocated string, compressed if size over THRESHOLD.
896*e663ba6eSchristos 
897*e663ba6eSchristos    May be a CTF dict or a CTF archive (this library mostly papers over the
898*e663ba6eSchristos    differences so you can open both the same way, treat both as ctf_archive_t
899*e663ba6eSchristos    and so on).  */
900*e663ba6eSchristos 
9014b169a6bSchristos extern unsigned char *ctf_link_write (ctf_dict_t *, size_t *size,
9028dffb485Schristos 				      size_t threshold);
9038dffb485Schristos 
9048dffb485Schristos /* Specialist linker functions.  These functions are not used by ld, but can be
9058dffb485Schristos    used by other programs making use of the linker machinery for other purposes
906*e663ba6eSchristos    to customize its output.  Must be called befoore ctf_link. */
907*e663ba6eSchristos 
908*e663ba6eSchristos /* Add an entry to rename a given compilation unit to some other name.  This
909*e663ba6eSchristos    is only used if conflicting types are found in that compilation unit: they
910*e663ba6eSchristos    will instead be placed in the child dict named TO. Many FROMs can map to one
911*e663ba6eSchristos    TO: all the types are placed together in that dict, with any whose names
912*e663ba6eSchristos    collide as a result being marked as non-root types.  */
913*e663ba6eSchristos 
9144b169a6bSchristos extern int ctf_link_add_cu_mapping (ctf_dict_t *, const char *from,
9158dffb485Schristos 				    const char *to);
916*e663ba6eSchristos 
917*e663ba6eSchristos /* Allow CTF archive names to be tweaked at the last minute before writeout.
918*e663ba6eSchristos    Unlike cu-mappings, this cannot transform names so that they collide: it's
919*e663ba6eSchristos    meant for unusual use cases that use names for archive members that are not
920*e663ba6eSchristos    exactly the same as CU names but are modified in some systematic way.  */
9214b169a6bSchristos typedef char *ctf_link_memb_name_changer_f (ctf_dict_t *,
9228dffb485Schristos 					    const char *, void *);
9238dffb485Schristos extern void ctf_link_set_memb_name_changer
9244b169a6bSchristos   (ctf_dict_t *, ctf_link_memb_name_changer_f *, void *);
9258dffb485Schristos 
926*e663ba6eSchristos /* Filter out unwanted variables, which can be very voluminous, and (unlike
927*e663ba6eSchristos    symbols) cause the CTF string table to grow to hold their names.  The
928*e663ba6eSchristos    variable filter should return nonzero if a variable should not appear in the
929*e663ba6eSchristos    output.  */
930*e663ba6eSchristos typedef int ctf_link_variable_filter_f (ctf_dict_t *, const char *, ctf_id_t,
931*e663ba6eSchristos 					void *);
932*e663ba6eSchristos extern int ctf_link_set_variable_filter (ctf_dict_t *,
933*e663ba6eSchristos 					 ctf_link_variable_filter_f *, void *);
934*e663ba6eSchristos 
935*e663ba6eSchristos /* Turn debugging off and on, and get its value.  This is the same as setting
936*e663ba6eSchristos    LIBCTF_DEBUG in the environment.  */
9378dffb485Schristos extern void ctf_setdebug (int debug);
9388dffb485Schristos extern int ctf_getdebug (void);
9398dffb485Schristos 
9404b169a6bSchristos /* Deprecated aliases for existing functions and types.  */
9414b169a6bSchristos 
9424b169a6bSchristos struct ctf_file;
9434b169a6bSchristos typedef struct ctf_dict ctf_file_t;
9444b169a6bSchristos extern void ctf_file_close (ctf_file_t *);
9454b169a6bSchristos extern ctf_dict_t *ctf_parent_file (ctf_dict_t *);
9464b169a6bSchristos extern ctf_dict_t *ctf_arc_open_by_name (const ctf_archive_t *,
9474b169a6bSchristos 					 const char *, int *);
948*e663ba6eSchristos extern ctf_dict_t *ctf_arc_open_by_name_sections (const ctf_archive_t *arc,
949*e663ba6eSchristos 						  const ctf_sect_t *symsect,
950*e663ba6eSchristos 						  const ctf_sect_t *strsect,
951*e663ba6eSchristos 						  const char *name, int *errp);
952*e663ba6eSchristos 
953*e663ba6eSchristos /* Deprecated witeout function to write out a gzip-compressed dict.  Unlike all
954*e663ba6eSchristos    the other writeout functions, this even compresses the header (it has to,
955*e663ba6eSchristos    since it's passed a gzFile), so the caller must also decompress it, since
956*e663ba6eSchristos    ctf_open() etc cannot tell it is a CTF dict or how large it is before
957*e663ba6eSchristos    decompression.  */
958*e663ba6eSchristos 
959*e663ba6eSchristos extern int ctf_gzwrite (ctf_dict_t *fp, gzFile fd);
960*e663ba6eSchristos 
961*e663ba6eSchristos /* Deprecated functions with no current use.  */
962*e663ba6eSchristos 
963*e663ba6eSchristos extern const char *ctf_label_topmost (ctf_dict_t *);
964*e663ba6eSchristos extern int ctf_label_info (ctf_dict_t *, const char *, ctf_lblinfo_t *);
965*e663ba6eSchristos extern int ctf_label_iter (ctf_dict_t *, ctf_label_f *, void *);
966*e663ba6eSchristos extern int ctf_label_next (ctf_dict_t *, ctf_next_t **, const char **); /* TBD */
9674b169a6bSchristos 
9688dffb485Schristos #ifdef	__cplusplus
9698dffb485Schristos }
9708dffb485Schristos #endif
9718dffb485Schristos 
9728dffb485Schristos #endif				/* _CTF_API_H */
973