2019-04-24 01:42:34 +08:00
|
|
|
/* Public API to libctf.
|
|
|
|
Copyright (C) 2019 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
This file is part of libctf.
|
|
|
|
|
|
|
|
libctf is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation; either version 3, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; see the file COPYING. If not see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
/* This header file defines the interfaces available from the CTF debugger
|
|
|
|
library, libctf. This API can be used by a debugger to operate on data in
|
|
|
|
the Compact ANSI-C Type Format (CTF). */
|
|
|
|
|
|
|
|
#ifndef _CTF_API_H
|
|
|
|
#define _CTF_API_H
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ctf.h>
|
libctf: creation functions
The CTF creation process looks roughly like (error handling elided):
int err;
ctf_file_t *foo = ctf_create (&err);
ctf_id_t type = ctf_add_THING (foo, ...);
ctf_update (foo);
ctf_*write (...);
Some ctf_add_THING functions accept other type IDs as arguments,
depending on the type: cv-quals, pointers, and structure and union
members all take other types as arguments. So do 'slices', which
let you take an existing integral type and recast it as a type
with a different bitness or offset within a byte, for bitfields.
One class of THING is not a type: "variables", which are mappings
of names (in the internal string table) to types. These are mostly
useful when encoding variables that do not appear in a symbol table
but which some external user has some other way to figure out the
address of at runtime (dynamic symbol lookup or querying a VM
interpreter or something).
You can snapshot the creation process at any point: rolling back to a
snapshot deletes all types and variables added since that point.
You can make arbitrary type queries on the CTF container during the
creation process, but you must call ctf_update() first, which
translates the growing dynamic container into a static one (this uses
the CTF opening machinery, added in a later commit), which is quite
expensive. This function must also be called after adding types
and before writing the container out.
Because addition of types involves looking up existing types, we add a
little of the type lookup machinery here, as well: only enough to
look up types in dynamic containers under construction.
libctf/
* ctf-create.c: New file.
* ctf-lookup.c: New file.
include/
* ctf-api.h (zlib.h): New include.
(ctf_sect_t): New.
(ctf_sect_names_t): Likewise.
(ctf_encoding_t): Likewise.
(ctf_membinfo_t): Likewise.
(ctf_arinfo_t): Likewise.
(ctf_funcinfo_t): Likewise.
(ctf_lblinfo_t): Likewise.
(ctf_snapshot_id_t): Likewise.
(CTF_FUNC_VARARG): Likewise.
(ctf_simple_open): Likewise.
(ctf_bufopen): Likewise.
(ctf_create): Likewise.
(ctf_add_array): Likewise.
(ctf_add_const): Likewise.
(ctf_add_enum_encoded): Likewise.
(ctf_add_enum): Likewise.
(ctf_add_float): Likewise.
(ctf_add_forward): Likewise.
(ctf_add_function): Likewise.
(ctf_add_integer): Likewise.
(ctf_add_slice): Likewise.
(ctf_add_pointer): Likewise.
(ctf_add_type): Likewise.
(ctf_add_typedef): Likewise.
(ctf_add_restrict): Likewise.
(ctf_add_struct): Likewise.
(ctf_add_union): Likewise.
(ctf_add_struct_sized): Likewise.
(ctf_add_union_sized): Likewise.
(ctf_add_volatile): Likewise.
(ctf_add_enumerator): Likewise.
(ctf_add_member): Likewise.
(ctf_add_member_offset): Likewise.
(ctf_add_member_encoded): Likewise.
(ctf_add_variable): Likewise.
(ctf_set_array): Likewise.
(ctf_update): Likewise.
(ctf_snapshot): Likewise.
(ctf_rollback): Likewise.
(ctf_discard): Likewise.
(ctf_write): Likewise.
(ctf_gzwrite): Likewise.
(ctf_compress_write): Likewise.
2019-04-24 05:45:46 +08:00
|
|
|
#include <zlib.h>
|
2019-04-24 01:42:34 +08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Clients can open one or more CTF containers and obtain a pointer to an
|
|
|
|
opaque ctf_file_t. Types are identified by an opaque ctf_id_t token.
|
|
|
|
They can also open or create read-only archives of CTF containers in a
|
|
|
|
ctf_archive_t.
|
|
|
|
|
|
|
|
These opaque definitions allow libctf to evolve without breaking clients. */
|
|
|
|
|
|
|
|
typedef struct ctf_file ctf_file_t;
|
|
|
|
typedef struct ctf_archive_internal ctf_archive_t;
|
|
|
|
typedef long ctf_id_t;
|
|
|
|
|
libctf: creation functions
The CTF creation process looks roughly like (error handling elided):
int err;
ctf_file_t *foo = ctf_create (&err);
ctf_id_t type = ctf_add_THING (foo, ...);
ctf_update (foo);
ctf_*write (...);
Some ctf_add_THING functions accept other type IDs as arguments,
depending on the type: cv-quals, pointers, and structure and union
members all take other types as arguments. So do 'slices', which
let you take an existing integral type and recast it as a type
with a different bitness or offset within a byte, for bitfields.
One class of THING is not a type: "variables", which are mappings
of names (in the internal string table) to types. These are mostly
useful when encoding variables that do not appear in a symbol table
but which some external user has some other way to figure out the
address of at runtime (dynamic symbol lookup or querying a VM
interpreter or something).
You can snapshot the creation process at any point: rolling back to a
snapshot deletes all types and variables added since that point.
You can make arbitrary type queries on the CTF container during the
creation process, but you must call ctf_update() first, which
translates the growing dynamic container into a static one (this uses
the CTF opening machinery, added in a later commit), which is quite
expensive. This function must also be called after adding types
and before writing the container out.
Because addition of types involves looking up existing types, we add a
little of the type lookup machinery here, as well: only enough to
look up types in dynamic containers under construction.
libctf/
* ctf-create.c: New file.
* ctf-lookup.c: New file.
include/
* ctf-api.h (zlib.h): New include.
(ctf_sect_t): New.
(ctf_sect_names_t): Likewise.
(ctf_encoding_t): Likewise.
(ctf_membinfo_t): Likewise.
(ctf_arinfo_t): Likewise.
(ctf_funcinfo_t): Likewise.
(ctf_lblinfo_t): Likewise.
(ctf_snapshot_id_t): Likewise.
(CTF_FUNC_VARARG): Likewise.
(ctf_simple_open): Likewise.
(ctf_bufopen): Likewise.
(ctf_create): Likewise.
(ctf_add_array): Likewise.
(ctf_add_const): Likewise.
(ctf_add_enum_encoded): Likewise.
(ctf_add_enum): Likewise.
(ctf_add_float): Likewise.
(ctf_add_forward): Likewise.
(ctf_add_function): Likewise.
(ctf_add_integer): Likewise.
(ctf_add_slice): Likewise.
(ctf_add_pointer): Likewise.
(ctf_add_type): Likewise.
(ctf_add_typedef): Likewise.
(ctf_add_restrict): Likewise.
(ctf_add_struct): Likewise.
(ctf_add_union): Likewise.
(ctf_add_struct_sized): Likewise.
(ctf_add_union_sized): Likewise.
(ctf_add_volatile): Likewise.
(ctf_add_enumerator): Likewise.
(ctf_add_member): Likewise.
(ctf_add_member_offset): Likewise.
(ctf_add_member_encoded): Likewise.
(ctf_add_variable): Likewise.
(ctf_set_array): Likewise.
(ctf_update): Likewise.
(ctf_snapshot): Likewise.
(ctf_rollback): Likewise.
(ctf_discard): Likewise.
(ctf_write): Likewise.
(ctf_gzwrite): Likewise.
(ctf_compress_write): Likewise.
2019-04-24 05:45:46 +08:00
|
|
|
/* If the debugger needs to provide the CTF library with a set of raw buffers
|
|
|
|
for use as the CTF data, symbol table, and string table, it can do so by
|
|
|
|
filling in ctf_sect_t structures and passing them to ctf_bufopen().
|
|
|
|
|
|
|
|
The contents of this structure must always be in native endianness (no
|
|
|
|
byteswapping is performed). */
|
|
|
|
|
|
|
|
typedef struct ctf_sect
|
|
|
|
{
|
|
|
|
const char *cts_name; /* Section name (if any). */
|
|
|
|
unsigned long cts_type; /* Section type (ELF SHT_... value). */
|
|
|
|
unsigned long cts_flags; /* Section flags (ELF SHF_... value). */
|
|
|
|
const void *cts_data; /* Pointer to section data. */
|
|
|
|
size_t cts_size; /* Size of data in bytes. */
|
|
|
|
size_t cts_entsize; /* Size of each section entry (symtab only). */
|
|
|
|
off64_t cts_offset; /* File offset of this section (if any). */
|
|
|
|
} ctf_sect_t;
|
|
|
|
|
|
|
|
/* Symbolic names for CTF sections. */
|
|
|
|
|
|
|
|
typedef enum ctf_sect_names
|
|
|
|
{
|
|
|
|
CTF_SECT_HEADER,
|
|
|
|
CTF_SECT_LABEL,
|
|
|
|
CTF_SECT_OBJT,
|
|
|
|
CTF_SECT_FUNC,
|
|
|
|
CTF_SECT_VAR,
|
|
|
|
CTF_SECT_TYPE,
|
|
|
|
CTF_SECT_STR
|
|
|
|
} ctf_sect_names_t;
|
|
|
|
|
|
|
|
/* Encoding information for integers, floating-point values, and certain other
|
|
|
|
intrinsics can be obtained by calling ctf_type_encoding(), below. The flags
|
|
|
|
field will contain values appropriate for the type defined in <ctf.h>. */
|
|
|
|
|
|
|
|
typedef struct ctf_encoding
|
|
|
|
{
|
|
|
|
uint32_t cte_format; /* Data format (CTF_INT_* or CTF_FP_* flags). */
|
|
|
|
uint32_t cte_offset; /* Offset of value in bits. */
|
|
|
|
uint32_t cte_bits; /* Size of storage in bits. */
|
|
|
|
} ctf_encoding_t;
|
|
|
|
|
|
|
|
typedef struct ctf_membinfo
|
|
|
|
{
|
|
|
|
ctf_id_t ctm_type; /* Type of struct or union member. */
|
|
|
|
unsigned long ctm_offset; /* Offset of member in bits. */
|
|
|
|
} ctf_membinfo_t;
|
|
|
|
|
|
|
|
typedef struct ctf_arinfo
|
|
|
|
{
|
|
|
|
ctf_id_t ctr_contents; /* Type of array contents. */
|
|
|
|
ctf_id_t ctr_index; /* Type of array index. */
|
|
|
|
uint32_t ctr_nelems; /* Number of elements. */
|
|
|
|
} ctf_arinfo_t;
|
|
|
|
|
|
|
|
typedef struct ctf_funcinfo
|
|
|
|
{
|
|
|
|
ctf_id_t ctc_return; /* Function return type. */
|
|
|
|
uint32_t ctc_argc; /* Number of typed arguments to function. */
|
|
|
|
uint32_t ctc_flags; /* Function attributes (see below). */
|
|
|
|
} ctf_funcinfo_t;
|
|
|
|
|
|
|
|
typedef struct ctf_lblinfo
|
|
|
|
{
|
|
|
|
ctf_id_t ctb_type; /* Last type associated with the label. */
|
|
|
|
} ctf_lblinfo_t;
|
|
|
|
|
|
|
|
typedef struct ctf_snapshot_id
|
|
|
|
{
|
|
|
|
unsigned long dtd_id; /* Highest DTD ID at time of snapshot. */
|
|
|
|
unsigned long snapshot_id; /* Snapshot id at time of snapshot. */
|
|
|
|
} ctf_snapshot_id_t;
|
|
|
|
|
|
|
|
#define CTF_FUNC_VARARG 0x1 /* Function arguments end with varargs. */
|
|
|
|
|
2019-04-24 01:42:34 +08:00
|
|
|
/* Functions that return integer status or a ctf_id_t use the following value
|
|
|
|
to indicate failure. ctf_errno() can be used to obtain an error code. */
|
|
|
|
#define CTF_ERR (-1L)
|
|
|
|
|
|
|
|
#define ECTF_BASE 1000 /* Base value for libctf errnos. */
|
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ECTF_FMT = ECTF_BASE, /* File is not in CTF or ELF format. */
|
|
|
|
ECTF_BFDERR, /* BFD error. */
|
|
|
|
ECTF_CTFVERS, /* CTF version is more recent than libctf. */
|
|
|
|
ECTF_BFD_AMBIGUOUS, /* Ambiguous BFD target. */
|
|
|
|
ECTF_SYMTAB, /* Symbol table uses invalid entry size. */
|
|
|
|
ECTF_SYMBAD, /* Symbol table data buffer invalid. */
|
|
|
|
ECTF_STRBAD, /* String table data buffer invalid. */
|
|
|
|
ECTF_CORRUPT, /* File data corruption detected. */
|
|
|
|
ECTF_NOCTFDATA, /* ELF file does not contain CTF data. */
|
|
|
|
ECTF_NOCTFBUF, /* Buffer does not contain CTF data. */
|
|
|
|
ECTF_NOSYMTAB, /* Symbol table data is not available. */
|
|
|
|
ECTF_NOPARENT, /* Parent CTF container is not available. */
|
|
|
|
ECTF_DMODEL, /* Data model mismatch. */
|
|
|
|
ECTF_UNUSED, /* Unused error. */
|
|
|
|
ECTF_ZALLOC, /* Failed to allocate (de)compression buffer. */
|
|
|
|
ECTF_DECOMPRESS, /* Failed to decompress CTF data. */
|
|
|
|
ECTF_STRTAB, /* String table for this string is missing. */
|
|
|
|
ECTF_BADNAME, /* String offset is corrupt w.r.t. strtab. */
|
|
|
|
ECTF_BADID, /* Invalid type ID number. */
|
|
|
|
ECTF_NOTSOU, /* Type is not a struct or union. */
|
|
|
|
ECTF_NOTENUM, /* Type is not an enum. */
|
|
|
|
ECTF_NOTSUE, /* Type is not a struct, union, or enum. */
|
|
|
|
ECTF_NOTINTFP, /* Type is not an integer, float, or enum. */
|
|
|
|
ECTF_NOTARRAY, /* Type is not an array. */
|
|
|
|
ECTF_NOTREF, /* Type does not reference another type. */
|
|
|
|
ECTF_NAMELEN, /* Buffer is too small to hold type name. */
|
|
|
|
ECTF_NOTYPE, /* No type found corresponding to name. */
|
|
|
|
ECTF_SYNTAX, /* Syntax error in type name. */
|
|
|
|
ECTF_NOTFUNC, /* Symtab entry does not refer to a function. */
|
|
|
|
ECTF_NOFUNCDAT, /* No func info available for function. */
|
|
|
|
ECTF_NOTDATA, /* Symtab entry does not refer to a data obj. */
|
|
|
|
ECTF_NOTYPEDAT, /* No type info available for object. */
|
|
|
|
ECTF_NOLABEL, /* No label found corresponding to name. */
|
|
|
|
ECTF_NOLABELDATA, /* File does not contain any labels. */
|
|
|
|
ECTF_NOTSUP, /* Feature not supported. */
|
|
|
|
ECTF_NOENUMNAM, /* Enum element name not found. */
|
|
|
|
ECTF_NOMEMBNAM, /* Member name not found. */
|
|
|
|
ECTF_RDONLY, /* CTF container is read-only. */
|
|
|
|
ECTF_DTFULL, /* CTF type is full (no more members allowed). */
|
|
|
|
ECTF_FULL, /* CTF container is full. */
|
|
|
|
ECTF_DUPLICATE, /* Duplicate member or variable name. */
|
|
|
|
ECTF_CONFLICT, /* Conflicting type definition present. */
|
|
|
|
ECTF_OVERROLLBACK, /* Attempt to roll back past a ctf_update. */
|
|
|
|
ECTF_COMPRESS, /* Failed to compress CTF data. */
|
|
|
|
ECTF_ARCREATE, /* Error creating CTF archive. */
|
|
|
|
ECTF_ARNNAME, /* Name not found in CTF archive. */
|
|
|
|
ECTF_SLICEOVERFLOW, /* Overflow of type bitness or offset in slice. */
|
|
|
|
ECTF_DUMPSECTUNKNOWN, /* Unknown section number in dump. */
|
|
|
|
ECTF_DUMPSECTCHANGED /* Section changed in middle of dump. */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The CTF data model is inferred to be the caller's data model or the data
|
|
|
|
model of the given object, unless ctf_setmodel() is explicitly called. */
|
|
|
|
#define CTF_MODEL_ILP32 1 /* Object data model is ILP32. */
|
|
|
|
#define CTF_MODEL_LP64 2 /* Object data model is LP64. */
|
|
|
|
#ifdef _LP64
|
|
|
|
# define CTF_MODEL_NATIVE CTF_MODEL_LP64
|
|
|
|
#else
|
|
|
|
# define CTF_MODEL_NATIVE CTF_MODEL_ILP32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Dynamic CTF containers can be created using ctf_create(). The ctf_add_*
|
|
|
|
routines can be used to add new definitions to the dynamic container.
|
|
|
|
New types are labeled as root or non-root to determine whether they are
|
|
|
|
visible at the top-level program scope when subsequently doing a lookup. */
|
|
|
|
|
|
|
|
#define CTF_ADD_NONROOT 0 /* Type only visible in nested scope. */
|
|
|
|
#define CTF_ADD_ROOT 1 /* Type visible at top-level scope. */
|
|
|
|
|
libctf: opening
This fills in the other half of the opening/creation puzzle: opening of
already-existing CTF files. Such files are always read-only: if you
want to add to a CTF file opened with one of the opening functions in
this file, use ctf_add_type(), in a later commit, to copy appropriate
types into a newly ctf_create()d, writable container.
The lowest-level opening functions are in here: ctf_bufopen(), which
takes ctf_sect_t structures akin to ELF section headers, and
ctf_simple_open(), which can be used if you don't have an entire ELF
section header to work from. Both will malloc() new space for the
buffers only if necessary, will mmap() directly from the file if
requested, and will mprotect() it afterwards to prevent accidental
corruption of the types. These functions are also used by ctf_update()
when converting types in a writable container into read-only types that
can be looked up using the lookup functions (in later commits).
The files are always of the native endianness of the system that created
them: at read time, the endianness of the header magic number is used to
determine whether or not the file needs byte-swapping, and the entire
thing is aggressively byte-swapped.
The agggressive nature of this swapping avoids complicating the rest of
the code with endianness conversions, while the native endianness
introduces no byte-swapping overhead in the common case. (The
endianness-independence code is also much newer than everything else in
this file, and deserves closer scrutiny.)
The accessors at the top of the file are there to transparently support
older versions of the CTF file format, allowing translation from older
formats that have different sizes for the structures in ctf.h:
currently, these older formats are intermingled with the newer ones in
ctf.h: they will probably migrate to a compatibility header in time, to
ease readability. The ctf_set_base() function is split out for the same
reason: when conversion code to a newer format is written, it would need
to malloc() new storage for the entire ctf_file_t if a file format
change causes it to grow, and for that we need ctf_set_base() to be a
separate function.
One pair of linked data structures supported by this file has no
creation code in libctf yet: the data and function object sections read
by init_symtab(). These will probably arrive soon, when the linker comes
to need them. (init_symtab() has hardly been changed since 2009, but if
any code in libctf has rotted over time, this will.)
A few simple accessors are also present that can even be called on
read-only containers because they don't actually modify them, since the
relevant things are not stored in the container but merely change its
operation: ctf_setmodel(), which lets you specify whether a container is
LP64 or not (used to statically determine the sizes of a few types),
ctf_import(), which is the only way to associate a parent container with
a child container, and ctf_setspecific(), which lets the caller
associate an arbitrary pointer with the CTF container for any use. If
the user doesn't call these functions correctly, libctf will misbehave:
this is particularly important for ctf_import(), since a container built
against a given parent container will not be able to resolve types that
depend on types in the parent unless it is ctf_import()ed with a parent
container with the same set of types at the same IDs, or a superset.
Possible future extensions (also noted in the ctf-hash.c file) include
storing a count of things so that we don't need to do one pass over the
CTF file counting everything, and computing a perfect hash at CTF
creation time in some compact form, storing it in the CTF file, and
using it to hash things so we don't need to do a second pass over the
entire CTF file to set up the hashes used to go from names to type IDs.
(There are multiple such hashes, one for each C type namespace: types,
enums, structs, and unions.)
libctf/
* ctf-open.c: New file.
* swap.h: Likewise.
include/
* ctf-api.h (ctf_file_close): New declaration.
(ctf_getdatasect): Likewise.
(ctf_parent_file): Likewise.
(ctf_parent_name): Likewise.
(ctf_parent_name_set): Likewise.
(ctf_import): Likewise.
(ctf_setmodel): Likewise.
(ctf_getmodel): Likewise.
(ctf_setspecific): Likewise.
(ctf_getspecific): Likewise.
2019-04-24 17:17:13 +08:00
|
|
|
extern ctf_sect_t ctf_getdatasect (const ctf_file_t *);
|
libctf: creation functions
The CTF creation process looks roughly like (error handling elided):
int err;
ctf_file_t *foo = ctf_create (&err);
ctf_id_t type = ctf_add_THING (foo, ...);
ctf_update (foo);
ctf_*write (...);
Some ctf_add_THING functions accept other type IDs as arguments,
depending on the type: cv-quals, pointers, and structure and union
members all take other types as arguments. So do 'slices', which
let you take an existing integral type and recast it as a type
with a different bitness or offset within a byte, for bitfields.
One class of THING is not a type: "variables", which are mappings
of names (in the internal string table) to types. These are mostly
useful when encoding variables that do not appear in a symbol table
but which some external user has some other way to figure out the
address of at runtime (dynamic symbol lookup or querying a VM
interpreter or something).
You can snapshot the creation process at any point: rolling back to a
snapshot deletes all types and variables added since that point.
You can make arbitrary type queries on the CTF container during the
creation process, but you must call ctf_update() first, which
translates the growing dynamic container into a static one (this uses
the CTF opening machinery, added in a later commit), which is quite
expensive. This function must also be called after adding types
and before writing the container out.
Because addition of types involves looking up existing types, we add a
little of the type lookup machinery here, as well: only enough to
look up types in dynamic containers under construction.
libctf/
* ctf-create.c: New file.
* ctf-lookup.c: New file.
include/
* ctf-api.h (zlib.h): New include.
(ctf_sect_t): New.
(ctf_sect_names_t): Likewise.
(ctf_encoding_t): Likewise.
(ctf_membinfo_t): Likewise.
(ctf_arinfo_t): Likewise.
(ctf_funcinfo_t): Likewise.
(ctf_lblinfo_t): Likewise.
(ctf_snapshot_id_t): Likewise.
(CTF_FUNC_VARARG): Likewise.
(ctf_simple_open): Likewise.
(ctf_bufopen): Likewise.
(ctf_create): Likewise.
(ctf_add_array): Likewise.
(ctf_add_const): Likewise.
(ctf_add_enum_encoded): Likewise.
(ctf_add_enum): Likewise.
(ctf_add_float): Likewise.
(ctf_add_forward): Likewise.
(ctf_add_function): Likewise.
(ctf_add_integer): Likewise.
(ctf_add_slice): Likewise.
(ctf_add_pointer): Likewise.
(ctf_add_type): Likewise.
(ctf_add_typedef): Likewise.
(ctf_add_restrict): Likewise.
(ctf_add_struct): Likewise.
(ctf_add_union): Likewise.
(ctf_add_struct_sized): Likewise.
(ctf_add_union_sized): Likewise.
(ctf_add_volatile): Likewise.
(ctf_add_enumerator): Likewise.
(ctf_add_member): Likewise.
(ctf_add_member_offset): Likewise.
(ctf_add_member_encoded): Likewise.
(ctf_add_variable): Likewise.
(ctf_set_array): Likewise.
(ctf_update): Likewise.
(ctf_snapshot): Likewise.
(ctf_rollback): Likewise.
(ctf_discard): Likewise.
(ctf_write): Likewise.
(ctf_gzwrite): Likewise.
(ctf_compress_write): Likewise.
2019-04-24 05:45:46 +08:00
|
|
|
extern ctf_file_t *ctf_simple_open (const char *, size_t, const char *, size_t,
|
|
|
|
size_t, const char *, size_t, int *);
|
|
|
|
extern ctf_file_t *ctf_bufopen (const ctf_sect_t *, const ctf_sect_t *,
|
|
|
|
const ctf_sect_t *, int *);
|
libctf: opening
This fills in the other half of the opening/creation puzzle: opening of
already-existing CTF files. Such files are always read-only: if you
want to add to a CTF file opened with one of the opening functions in
this file, use ctf_add_type(), in a later commit, to copy appropriate
types into a newly ctf_create()d, writable container.
The lowest-level opening functions are in here: ctf_bufopen(), which
takes ctf_sect_t structures akin to ELF section headers, and
ctf_simple_open(), which can be used if you don't have an entire ELF
section header to work from. Both will malloc() new space for the
buffers only if necessary, will mmap() directly from the file if
requested, and will mprotect() it afterwards to prevent accidental
corruption of the types. These functions are also used by ctf_update()
when converting types in a writable container into read-only types that
can be looked up using the lookup functions (in later commits).
The files are always of the native endianness of the system that created
them: at read time, the endianness of the header magic number is used to
determine whether or not the file needs byte-swapping, and the entire
thing is aggressively byte-swapped.
The agggressive nature of this swapping avoids complicating the rest of
the code with endianness conversions, while the native endianness
introduces no byte-swapping overhead in the common case. (The
endianness-independence code is also much newer than everything else in
this file, and deserves closer scrutiny.)
The accessors at the top of the file are there to transparently support
older versions of the CTF file format, allowing translation from older
formats that have different sizes for the structures in ctf.h:
currently, these older formats are intermingled with the newer ones in
ctf.h: they will probably migrate to a compatibility header in time, to
ease readability. The ctf_set_base() function is split out for the same
reason: when conversion code to a newer format is written, it would need
to malloc() new storage for the entire ctf_file_t if a file format
change causes it to grow, and for that we need ctf_set_base() to be a
separate function.
One pair of linked data structures supported by this file has no
creation code in libctf yet: the data and function object sections read
by init_symtab(). These will probably arrive soon, when the linker comes
to need them. (init_symtab() has hardly been changed since 2009, but if
any code in libctf has rotted over time, this will.)
A few simple accessors are also present that can even be called on
read-only containers because they don't actually modify them, since the
relevant things are not stored in the container but merely change its
operation: ctf_setmodel(), which lets you specify whether a container is
LP64 or not (used to statically determine the sizes of a few types),
ctf_import(), which is the only way to associate a parent container with
a child container, and ctf_setspecific(), which lets the caller
associate an arbitrary pointer with the CTF container for any use. If
the user doesn't call these functions correctly, libctf will misbehave:
this is particularly important for ctf_import(), since a container built
against a given parent container will not be able to resolve types that
depend on types in the parent unless it is ctf_import()ed with a parent
container with the same set of types at the same IDs, or a superset.
Possible future extensions (also noted in the ctf-hash.c file) include
storing a count of things so that we don't need to do one pass over the
CTF file counting everything, and computing a perfect hash at CTF
creation time in some compact form, storing it in the CTF file, and
using it to hash things so we don't need to do a second pass over the
entire CTF file to set up the hashes used to go from names to type IDs.
(There are multiple such hashes, one for each C type namespace: types,
enums, structs, and unions.)
libctf/
* ctf-open.c: New file.
* swap.h: Likewise.
include/
* ctf-api.h (ctf_file_close): New declaration.
(ctf_getdatasect): Likewise.
(ctf_parent_file): Likewise.
(ctf_parent_name): Likewise.
(ctf_parent_name_set): Likewise.
(ctf_import): Likewise.
(ctf_setmodel): Likewise.
(ctf_getmodel): Likewise.
(ctf_setspecific): Likewise.
(ctf_getspecific): Likewise.
2019-04-24 17:17:13 +08:00
|
|
|
extern void ctf_file_close (ctf_file_t *);
|
|
|
|
|
|
|
|
extern ctf_file_t *ctf_parent_file (ctf_file_t *);
|
|
|
|
extern const char *ctf_parent_name (ctf_file_t *);
|
|
|
|
extern void ctf_parent_name_set (ctf_file_t *, const char *);
|
|
|
|
|
|
|
|
extern int ctf_import (ctf_file_t *, ctf_file_t *);
|
|
|
|
extern int ctf_setmodel (ctf_file_t *, int);
|
|
|
|
extern int ctf_getmodel (ctf_file_t *);
|
|
|
|
|
|
|
|
extern void ctf_setspecific (ctf_file_t *, void *);
|
|
|
|
extern void *ctf_getspecific (ctf_file_t *);
|
libctf: creation functions
The CTF creation process looks roughly like (error handling elided):
int err;
ctf_file_t *foo = ctf_create (&err);
ctf_id_t type = ctf_add_THING (foo, ...);
ctf_update (foo);
ctf_*write (...);
Some ctf_add_THING functions accept other type IDs as arguments,
depending on the type: cv-quals, pointers, and structure and union
members all take other types as arguments. So do 'slices', which
let you take an existing integral type and recast it as a type
with a different bitness or offset within a byte, for bitfields.
One class of THING is not a type: "variables", which are mappings
of names (in the internal string table) to types. These are mostly
useful when encoding variables that do not appear in a symbol table
but which some external user has some other way to figure out the
address of at runtime (dynamic symbol lookup or querying a VM
interpreter or something).
You can snapshot the creation process at any point: rolling back to a
snapshot deletes all types and variables added since that point.
You can make arbitrary type queries on the CTF container during the
creation process, but you must call ctf_update() first, which
translates the growing dynamic container into a static one (this uses
the CTF opening machinery, added in a later commit), which is quite
expensive. This function must also be called after adding types
and before writing the container out.
Because addition of types involves looking up existing types, we add a
little of the type lookup machinery here, as well: only enough to
look up types in dynamic containers under construction.
libctf/
* ctf-create.c: New file.
* ctf-lookup.c: New file.
include/
* ctf-api.h (zlib.h): New include.
(ctf_sect_t): New.
(ctf_sect_names_t): Likewise.
(ctf_encoding_t): Likewise.
(ctf_membinfo_t): Likewise.
(ctf_arinfo_t): Likewise.
(ctf_funcinfo_t): Likewise.
(ctf_lblinfo_t): Likewise.
(ctf_snapshot_id_t): Likewise.
(CTF_FUNC_VARARG): Likewise.
(ctf_simple_open): Likewise.
(ctf_bufopen): Likewise.
(ctf_create): Likewise.
(ctf_add_array): Likewise.
(ctf_add_const): Likewise.
(ctf_add_enum_encoded): Likewise.
(ctf_add_enum): Likewise.
(ctf_add_float): Likewise.
(ctf_add_forward): Likewise.
(ctf_add_function): Likewise.
(ctf_add_integer): Likewise.
(ctf_add_slice): Likewise.
(ctf_add_pointer): Likewise.
(ctf_add_type): Likewise.
(ctf_add_typedef): Likewise.
(ctf_add_restrict): Likewise.
(ctf_add_struct): Likewise.
(ctf_add_union): Likewise.
(ctf_add_struct_sized): Likewise.
(ctf_add_union_sized): Likewise.
(ctf_add_volatile): Likewise.
(ctf_add_enumerator): Likewise.
(ctf_add_member): Likewise.
(ctf_add_member_offset): Likewise.
(ctf_add_member_encoded): Likewise.
(ctf_add_variable): Likewise.
(ctf_set_array): Likewise.
(ctf_update): Likewise.
(ctf_snapshot): Likewise.
(ctf_rollback): Likewise.
(ctf_discard): Likewise.
(ctf_write): Likewise.
(ctf_gzwrite): Likewise.
(ctf_compress_write): Likewise.
2019-04-24 05:45:46 +08:00
|
|
|
|
2019-04-24 05:05:52 +08:00
|
|
|
extern int ctf_errno (ctf_file_t *);
|
|
|
|
extern const char *ctf_errmsg (int);
|
libctf: creation functions
The CTF creation process looks roughly like (error handling elided):
int err;
ctf_file_t *foo = ctf_create (&err);
ctf_id_t type = ctf_add_THING (foo, ...);
ctf_update (foo);
ctf_*write (...);
Some ctf_add_THING functions accept other type IDs as arguments,
depending on the type: cv-quals, pointers, and structure and union
members all take other types as arguments. So do 'slices', which
let you take an existing integral type and recast it as a type
with a different bitness or offset within a byte, for bitfields.
One class of THING is not a type: "variables", which are mappings
of names (in the internal string table) to types. These are mostly
useful when encoding variables that do not appear in a symbol table
but which some external user has some other way to figure out the
address of at runtime (dynamic symbol lookup or querying a VM
interpreter or something).
You can snapshot the creation process at any point: rolling back to a
snapshot deletes all types and variables added since that point.
You can make arbitrary type queries on the CTF container during the
creation process, but you must call ctf_update() first, which
translates the growing dynamic container into a static one (this uses
the CTF opening machinery, added in a later commit), which is quite
expensive. This function must also be called after adding types
and before writing the container out.
Because addition of types involves looking up existing types, we add a
little of the type lookup machinery here, as well: only enough to
look up types in dynamic containers under construction.
libctf/
* ctf-create.c: New file.
* ctf-lookup.c: New file.
include/
* ctf-api.h (zlib.h): New include.
(ctf_sect_t): New.
(ctf_sect_names_t): Likewise.
(ctf_encoding_t): Likewise.
(ctf_membinfo_t): Likewise.
(ctf_arinfo_t): Likewise.
(ctf_funcinfo_t): Likewise.
(ctf_lblinfo_t): Likewise.
(ctf_snapshot_id_t): Likewise.
(CTF_FUNC_VARARG): Likewise.
(ctf_simple_open): Likewise.
(ctf_bufopen): Likewise.
(ctf_create): Likewise.
(ctf_add_array): Likewise.
(ctf_add_const): Likewise.
(ctf_add_enum_encoded): Likewise.
(ctf_add_enum): Likewise.
(ctf_add_float): Likewise.
(ctf_add_forward): Likewise.
(ctf_add_function): Likewise.
(ctf_add_integer): Likewise.
(ctf_add_slice): Likewise.
(ctf_add_pointer): Likewise.
(ctf_add_type): Likewise.
(ctf_add_typedef): Likewise.
(ctf_add_restrict): Likewise.
(ctf_add_struct): Likewise.
(ctf_add_union): Likewise.
(ctf_add_struct_sized): Likewise.
(ctf_add_union_sized): Likewise.
(ctf_add_volatile): Likewise.
(ctf_add_enumerator): Likewise.
(ctf_add_member): Likewise.
(ctf_add_member_offset): Likewise.
(ctf_add_member_encoded): Likewise.
(ctf_add_variable): Likewise.
(ctf_set_array): Likewise.
(ctf_update): Likewise.
(ctf_snapshot): Likewise.
(ctf_rollback): Likewise.
(ctf_discard): Likewise.
(ctf_write): Likewise.
(ctf_gzwrite): Likewise.
(ctf_compress_write): Likewise.
2019-04-24 05:45:46 +08:00
|
|
|
extern ctf_id_t ctf_add_array (ctf_file_t *, uint32_t,
|
|
|
|
const ctf_arinfo_t *);
|
|
|
|
extern ctf_id_t ctf_add_const (ctf_file_t *, uint32_t, ctf_id_t);
|
|
|
|
extern ctf_id_t ctf_add_enum_encoded (ctf_file_t *, uint32_t, const char *,
|
|
|
|
const ctf_encoding_t *);
|
|
|
|
extern ctf_id_t ctf_add_enum (ctf_file_t *, uint32_t, const char *);
|
|
|
|
extern ctf_id_t ctf_add_float (ctf_file_t *, uint32_t,
|
|
|
|
const char *, const ctf_encoding_t *);
|
|
|
|
extern ctf_id_t ctf_add_forward (ctf_file_t *, uint32_t, const char *,
|
|
|
|
uint32_t);
|
|
|
|
extern ctf_id_t ctf_add_function (ctf_file_t *, uint32_t,
|
|
|
|
const ctf_funcinfo_t *, const ctf_id_t *);
|
|
|
|
extern ctf_id_t ctf_add_integer (ctf_file_t *, uint32_t, const char *,
|
|
|
|
const ctf_encoding_t *);
|
|
|
|
extern ctf_id_t ctf_add_slice (ctf_file_t *, uint32_t, ctf_id_t, const ctf_encoding_t *);
|
|
|
|
extern ctf_id_t ctf_add_pointer (ctf_file_t *, uint32_t, ctf_id_t);
|
|
|
|
extern ctf_id_t ctf_add_type (ctf_file_t *, ctf_file_t *, ctf_id_t);
|
|
|
|
extern ctf_id_t ctf_add_typedef (ctf_file_t *, uint32_t, const char *,
|
|
|
|
ctf_id_t);
|
|
|
|
extern ctf_id_t ctf_add_restrict (ctf_file_t *, uint32_t, ctf_id_t);
|
|
|
|
extern ctf_id_t ctf_add_struct (ctf_file_t *, uint32_t, const char *);
|
|
|
|
extern ctf_id_t ctf_add_union (ctf_file_t *, uint32_t, const char *);
|
|
|
|
extern ctf_id_t ctf_add_struct_sized (ctf_file_t *, uint32_t, const char *,
|
|
|
|
size_t);
|
|
|
|
extern ctf_id_t ctf_add_union_sized (ctf_file_t *, uint32_t, const char *,
|
|
|
|
size_t);
|
|
|
|
extern ctf_id_t ctf_add_volatile (ctf_file_t *, uint32_t, ctf_id_t);
|
|
|
|
|
|
|
|
extern int ctf_add_enumerator (ctf_file_t *, ctf_id_t, const char *, int);
|
|
|
|
extern int ctf_add_member (ctf_file_t *, ctf_id_t, const char *, ctf_id_t);
|
|
|
|
extern int ctf_add_member_offset (ctf_file_t *, ctf_id_t, const char *,
|
|
|
|
ctf_id_t, unsigned long);
|
|
|
|
extern int ctf_add_member_encoded (ctf_file_t *, ctf_id_t, const char *,
|
|
|
|
ctf_id_t, unsigned long,
|
|
|
|
const ctf_encoding_t);
|
|
|
|
|
|
|
|
extern int ctf_add_variable (ctf_file_t *, const char *, ctf_id_t);
|
|
|
|
|
|
|
|
extern int ctf_set_array (ctf_file_t *, ctf_id_t, const ctf_arinfo_t *);
|
|
|
|
|
|
|
|
extern ctf_file_t *ctf_create (int *);
|
|
|
|
extern int ctf_update (ctf_file_t *);
|
|
|
|
extern ctf_snapshot_id_t ctf_snapshot (ctf_file_t *);
|
|
|
|
extern int ctf_rollback (ctf_file_t *, ctf_snapshot_id_t);
|
|
|
|
extern int ctf_discard (ctf_file_t *);
|
|
|
|
extern int ctf_write (ctf_file_t *, int);
|
|
|
|
extern int ctf_gzwrite (ctf_file_t * fp, gzFile fd);
|
|
|
|
extern int ctf_compress_write (ctf_file_t * fp, int fd);
|
2019-04-24 01:42:34 +08:00
|
|
|
|
2019-04-24 01:55:27 +08:00
|
|
|
extern void ctf_setdebug (int debug);
|
|
|
|
extern int ctf_getdebug (void);
|
|
|
|
|
2019-04-24 01:42:34 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* _CTF_API_H */
|