1*9e3b08aeSAndroid Build Coastguard Worker# STG 2*9e3b08aeSAndroid Build Coastguard Worker 3*9e3b08aeSAndroid Build Coastguard WorkerSTG stands for Symbol-Type Graph. 4*9e3b08aeSAndroid Build Coastguard Worker 5*9e3b08aeSAndroid Build Coastguard Worker# Overview 6*9e3b08aeSAndroid Build Coastguard Worker 7*9e3b08aeSAndroid Build Coastguard WorkerSTG models Application Binary Interfaces. It supports extraction of ABIs from 8*9e3b08aeSAndroid Build Coastguard WorkerDWARF and ingestion of BTF and libabigail XML into its model. Its primary 9*9e3b08aeSAndroid Build Coastguard Workerpurpose is monitoring an ABI for changes over time and reporting such changes in 10*9e3b08aeSAndroid Build Coastguard Workera comprehensible fashion. 11*9e3b08aeSAndroid Build Coastguard Worker 12*9e3b08aeSAndroid Build Coastguard WorkerSTG captures symbol information, the size and layout of structs, function 13*9e3b08aeSAndroid Build Coastguard Workerargument and return types and much more, in a graph representation. Difference 14*9e3b08aeSAndroid Build Coastguard Workerreporting happens via a graph comparison. 15*9e3b08aeSAndroid Build Coastguard Worker 16*9e3b08aeSAndroid Build Coastguard WorkerCurrently, STG functionality is exposed as two command-line tools, `stg` (for 17*9e3b08aeSAndroid Build Coastguard WorkerABI extraction) and `stgdiff` (for ABI comparison), and a native file format. 18*9e3b08aeSAndroid Build Coastguard Worker 19*9e3b08aeSAndroid Build Coastguard Worker## Model 20*9e3b08aeSAndroid Build Coastguard Worker 21*9e3b08aeSAndroid Build Coastguard WorkerSTG's model is an *abstraction* which does not and cannot capture every possible 22*9e3b08aeSAndroid Build Coastguard Workerinterface property, invariant or behaviour. Conversely, the model includes 23*9e3b08aeSAndroid Build Coastguard Workerdistinctions which are API significant but not ABI significant. 24*9e3b08aeSAndroid Build Coastguard Worker 25*9e3b08aeSAndroid Build Coastguard WorkerConcretely, STG's model is a rooted, connected, directed graph where each kind 26*9e3b08aeSAndroid Build Coastguard Workerof node corresponds to a meaningful ABI entity such as a symbol, function type 27*9e3b08aeSAndroid Build Coastguard Workeror struct member. 28*9e3b08aeSAndroid Build Coastguard Worker 29*9e3b08aeSAndroid Build Coastguard WorkerNodes have specific attributes, such as name or size. Outgoing edges specify 30*9e3b08aeSAndroid Build Coastguard Workerthings like return type. STG's model does not impose any constraints on which 31*9e3b08aeSAndroid Build Coastguard Workernodes may be joined by edges. 32*9e3b08aeSAndroid Build Coastguard Worker 33*9e3b08aeSAndroid Build Coastguard WorkerEach node has an identity. However, for the purpose of comparison, nodes are 34*9e3b08aeSAndroid Build Coastguard Workerconsidered equal if they are of the same kind, have the same attributes and 35*9e3b08aeSAndroid Build Coastguard Workermatching outgoing edges and all nodes reachable via a pair of matching edges are 36*9e3b08aeSAndroid Build Coastguard Worker(recursively) equal. Renumbering nodes, (de)duplicating nodes and 37*9e3b08aeSAndroid Build Coastguard Workeradding/removing unreachable nodes do not affect this relationship. 38*9e3b08aeSAndroid Build Coastguard Worker 39*9e3b08aeSAndroid Build Coastguard Worker### Symbols 40*9e3b08aeSAndroid Build Coastguard Worker 41*9e3b08aeSAndroid Build Coastguard WorkerAs modelled by STG, symbols correspond closely to ELF symbols as seen in 42*9e3b08aeSAndroid Build Coastguard Worker`.dynsym` for shared object files or in `.symtab` for object files. In the case 43*9e3b08aeSAndroid Build Coastguard Workerof the Linux kernel, the `.symtab` is enriched with metadata and the effective 44*9e3b08aeSAndroid Build Coastguard Worker"ksymtab" is actually a subset of the ELF symbols together with CRC and 45*9e3b08aeSAndroid Build Coastguard Workernamespace information. 46*9e3b08aeSAndroid Build Coastguard Worker 47*9e3b08aeSAndroid Build Coastguard WorkerSTG links symbols to their source-level types where these are known. Symbols 48*9e3b08aeSAndroid Build Coastguard Workerdefined purely in assembly language will not have type information. 49*9e3b08aeSAndroid Build Coastguard Worker 50*9e3b08aeSAndroid Build Coastguard WorkerThe symbol table is contained in the root node of the graph, which is an 51*9e3b08aeSAndroid Build Coastguard Worker*Interface* node. 52*9e3b08aeSAndroid Build Coastguard Worker 53*9e3b08aeSAndroid Build Coastguard Worker### Types 54*9e3b08aeSAndroid Build Coastguard Worker 55*9e3b08aeSAndroid Build Coastguard WorkerSTG models the C, C++ and (to a limited extent) Rust type systems. 56*9e3b08aeSAndroid Build Coastguard Worker 57*9e3b08aeSAndroid Build Coastguard WorkerFor example, C++ template value parameters are poorly modelled for the simple 58*9e3b08aeSAndroid Build Coastguard Workerreason that this would require modelling C++ *values* as well as types, 59*9e3b08aeSAndroid Build Coastguard Workersomething that DWARF itself doesn't do to the full extent permitted by C++20. 60*9e3b08aeSAndroid Build Coastguard Worker 61*9e3b08aeSAndroid Build Coastguard WorkerAs type definitions are in general mutually recursive, an STG ABI is in general 62*9e3b08aeSAndroid Build Coastguard Workera cyclic graph. 63*9e3b08aeSAndroid Build Coastguard Worker 64*9e3b08aeSAndroid Build Coastguard WorkerThe root node of the graph can also contain a list of interface types, which may 65*9e3b08aeSAndroid Build Coastguard Workernot necessarily be reachable from the interface symbols. 66*9e3b08aeSAndroid Build Coastguard Worker 67*9e3b08aeSAndroid Build Coastguard Worker## Supported Input Formats, Parsers and Limitations 68*9e3b08aeSAndroid Build Coastguard Worker 69*9e3b08aeSAndroid Build Coastguard WorkerSTG can read its own native format for processing or comparison. It can also 70*9e3b08aeSAndroid Build Coastguard Workerprocess libabigail XML and BTF (`.BTF` ELF sections), with some limitations due 71*9e3b08aeSAndroid Build Coastguard Workerto model, design and implementation differences including missing features. 72*9e3b08aeSAndroid Build Coastguard Worker 73*9e3b08aeSAndroid Build Coastguard Worker### Kinds of Node 74*9e3b08aeSAndroid Build Coastguard Worker 75*9e3b08aeSAndroid Build Coastguard WorkerSTG has the following kinds of node. 76*9e3b08aeSAndroid Build Coastguard Worker 77*9e3b08aeSAndroid Build Coastguard Worker* **Special** - used for `void` and `...` 78*9e3b08aeSAndroid Build Coastguard Worker* **Pointer / Reference** - `*`, `&` and `&&` 79*9e3b08aeSAndroid Build Coastguard Worker* **Pointer to Member** - `foo::*` 80*9e3b08aeSAndroid Build Coastguard Worker* **Typedef** - `typedef` and `using ... = ...` 81*9e3b08aeSAndroid Build Coastguard Worker* **Qualified** - `const` and friends 82*9e3b08aeSAndroid Build Coastguard Worker* **Primitive** - concrete types such as `int` and friends 83*9e3b08aeSAndroid Build Coastguard Worker* **Array** - `foo[N]` - there is no distinction between zero and 84*9e3b08aeSAndroid Build Coastguard Worker indeterminate length in the model 85*9e3b08aeSAndroid Build Coastguard Worker* **Base Class** - inheritance metadata 86*9e3b08aeSAndroid Build Coastguard Worker* **Method** - (only) virtual function 87*9e3b08aeSAndroid Build Coastguard Worker* **Member** - data member 88*9e3b08aeSAndroid Build Coastguard Worker* **Variant Member** - discriminated member 89*9e3b08aeSAndroid Build Coastguard Worker* **Struct / Union** - `struct foo` etc., Rust tuples too 90*9e3b08aeSAndroid Build Coastguard Worker* **Enumeration** - including the underlying value type - only values that are 91*9e3b08aeSAndroid Build Coastguard Worker within the range of signed 64-bit integer are correctly modelled 92*9e3b08aeSAndroid Build Coastguard Worker* **Variant** - for Rust enums holding data 93*9e3b08aeSAndroid Build Coastguard Worker* **Function** - multiple argument, single return type 94*9e3b08aeSAndroid Build Coastguard Worker* **ELF Symbol** - name, version, ELF metadata, Linux kernel metadata 95*9e3b08aeSAndroid Build Coastguard Worker* **Interface** - top-level collection of symbols and types 96*9e3b08aeSAndroid Build Coastguard Worker 97*9e3b08aeSAndroid Build Coastguard WorkerAn STG ABI consists of a rooted, connected graph of such nodes, and *nothing 98*9e3b08aeSAndroid Build Coastguard Workerelse*. STG is blind to anything that cannot be represented by its model. 99*9e3b08aeSAndroid Build Coastguard Worker 100*9e3b08aeSAndroid Build Coastguard Worker### Native Format 101*9e3b08aeSAndroid Build Coastguard Worker 102*9e3b08aeSAndroid Build Coastguard WorkerSTG's native file format is a protocol buffer text format. It is suitable for 103*9e3b08aeSAndroid Build Coastguard Workerrevision control, rather than human consumption. It is effectively described by 104*9e3b08aeSAndroid Build Coastguard Worker[`stg.proto`](../stg.proto). 105*9e3b08aeSAndroid Build Coastguard Worker 106*9e3b08aeSAndroid Build Coastguard WorkerIn this textual serialisation of ABI graphs, external node identifiers and node 107*9e3b08aeSAndroid Build Coastguard Workerorder are chosen to minimise file changes when a small subset of the graph 108*9e3b08aeSAndroid Build Coastguard Workerchanges. 109*9e3b08aeSAndroid Build Coastguard Worker 110*9e3b08aeSAndroid Build Coastguard WorkerAs an example, this is the definition of the **Typedef** node kind: 111*9e3b08aeSAndroid Build Coastguard Worker 112*9e3b08aeSAndroid Build Coastguard Worker```proto 113*9e3b08aeSAndroid Build Coastguard Workermessage Typedef { 114*9e3b08aeSAndroid Build Coastguard Worker fixed32 id = 1; 115*9e3b08aeSAndroid Build Coastguard Worker string name = 2; 116*9e3b08aeSAndroid Build Coastguard Worker fixed32 referred_type_id = 3; 117*9e3b08aeSAndroid Build Coastguard Worker} 118*9e3b08aeSAndroid Build Coastguard Worker``` 119*9e3b08aeSAndroid Build Coastguard Worker 120*9e3b08aeSAndroid Build Coastguard Worker### Abigail (a.k.a. libabigail XML) 121*9e3b08aeSAndroid Build Coastguard Worker 122*9e3b08aeSAndroid Build Coastguard Worker[libabigail](https://sourceware.org/libabigail/) is another project for ABI 123*9e3b08aeSAndroid Build Coastguard Workermonitoring. It uses a format that can be parsed as XML. 124*9e3b08aeSAndroid Build Coastguard Worker 125*9e3b08aeSAndroid Build Coastguard WorkerThis command will transform Abigail into STG: 126*9e3b08aeSAndroid Build Coastguard Worker 127*9e3b08aeSAndroid Build Coastguard Worker```shell 128*9e3b08aeSAndroid Build Coastguard Workerstg --abi library.xml --output library.stg 129*9e3b08aeSAndroid Build Coastguard Worker``` 130*9e3b08aeSAndroid Build Coastguard Worker 131*9e3b08aeSAndroid Build Coastguard WorkerThe main features modelled in Abigail but not STG are: 132*9e3b08aeSAndroid Build Coastguard Worker 133*9e3b08aeSAndroid Build Coastguard Worker* source file, line and column information 134*9e3b08aeSAndroid Build Coastguard Worker* C++ access specifiers (public, protected, private) 135*9e3b08aeSAndroid Build Coastguard Worker 136*9e3b08aeSAndroid Build Coastguard WorkerThe Abigail reader has these distinct phases of operation: 137*9e3b08aeSAndroid Build Coastguard Worker 138*9e3b08aeSAndroid Build Coastguard Worker1. text parsed into an XML tree 139*9e3b08aeSAndroid Build Coastguard Worker2. XML cleaning - whitespace and unused attributes are stripped 140*9e3b08aeSAndroid Build Coastguard Worker3. XML tidying - issues like duplicate nodes are resolved, if possible 141*9e3b08aeSAndroid Build Coastguard Worker4. XML parsed into a graph with symbol information held separately 142*9e3b08aeSAndroid Build Coastguard Worker5. symbols and root node added to the graph 143*9e3b08aeSAndroid Build Coastguard Worker6. useless type qualifiers are stripped in post-processing 144*9e3b08aeSAndroid Build Coastguard Worker 145*9e3b08aeSAndroid Build Coastguard Worker### BTF 146*9e3b08aeSAndroid Build Coastguard Worker 147*9e3b08aeSAndroid Build Coastguard Worker[BTF](https://docs.kernel.org/bpf/btf.html) is typically used for the Linux 148*9e3b08aeSAndroid Build Coastguard Workerkernel where it is generated by `pahole -J` from ELF and DWARF information. It 149*9e3b08aeSAndroid Build Coastguard Workercan also be generated natively instead of DWARF using `gcc -gbtf` and by Clang, 150*9e3b08aeSAndroid Build Coastguard Workerbut only for eBPF targets. 151*9e3b08aeSAndroid Build Coastguard Worker 152*9e3b08aeSAndroid Build Coastguard WorkerThis command will transform BTF into STG: 153*9e3b08aeSAndroid Build Coastguard Worker 154*9e3b08aeSAndroid Build Coastguard Worker```shell 155*9e3b08aeSAndroid Build Coastguard Workerstg --btf vmlinux --output vmlinux.stg 156*9e3b08aeSAndroid Build Coastguard Worker``` 157*9e3b08aeSAndroid Build Coastguard Worker 158*9e3b08aeSAndroid Build Coastguard WorkerSTG has primarily been tested against the `pahole` (libbtf) dialect of BTF and 159*9e3b08aeSAndroid Build Coastguard Workersupport is not complete. 160*9e3b08aeSAndroid Build Coastguard Worker 161*9e3b08aeSAndroid Build Coastguard Worker* split BTF is not supported at all 162*9e3b08aeSAndroid Build Coastguard Worker* any `.BTF.ext` section is just ignored 163*9e3b08aeSAndroid Build Coastguard Worker* some kinds of BTF node are not handled: 164*9e3b08aeSAndroid Build Coastguard Worker * `BTF_KIND_DATASEC` - skip 165*9e3b08aeSAndroid Build Coastguard Worker * `BTF_KIND_DECL_TAG` - abort 166*9e3b08aeSAndroid Build Coastguard Worker * `BTF_KIND_TYPE_TAG` - abort 167*9e3b08aeSAndroid Build Coastguard Worker 168*9e3b08aeSAndroid Build Coastguard WorkerThe BTF reader has these distinct phases of operation: 169*9e3b08aeSAndroid Build Coastguard Worker 170*9e3b08aeSAndroid Build Coastguard Worker1. file is opened as ELF and `.BTF` section data found 171*9e3b08aeSAndroid Build Coastguard Worker2. BTF header processed 172*9e3b08aeSAndroid Build Coastguard Worker3. BTF nodes parsed into a graph with symbol information held separately 173*9e3b08aeSAndroid Build Coastguard Worker4. symbols and root node added to the graph 174*9e3b08aeSAndroid Build Coastguard Worker 175*9e3b08aeSAndroid Build Coastguard Worker### DWARF 176*9e3b08aeSAndroid Build Coastguard Worker 177*9e3b08aeSAndroid Build Coastguard WorkerThe ELF / DWARF reader operates similarly to the other readers at a high level, 178*9e3b08aeSAndroid Build Coastguard Workerbut much more work has to be done to turn ELF symbols and DWARF DIEs into STG 179*9e3b08aeSAndroid Build Coastguard Workernodes. 180*9e3b08aeSAndroid Build Coastguard Worker 181*9e3b08aeSAndroid Build Coastguard Worker1. the ELF file is checked for DWARF - missing DWARF results in a warning 182*9e3b08aeSAndroid Build Coastguard Worker2. the ELF symbols are read (from `.dynsym` in the case of shared object file) 183*9e3b08aeSAndroid Build Coastguard Worker3. the DWARF information is parsed into a partial STG graph 184*9e3b08aeSAndroid Build Coastguard Worker4. the ELF and DWARF information are stitched together, adding symbols and a 185*9e3b08aeSAndroid Build Coastguard Worker root node to the graph 186*9e3b08aeSAndroid Build Coastguard Worker5. useless type qualifiers are stripped in post-processing 187*9e3b08aeSAndroid Build Coastguard Worker 188*9e3b08aeSAndroid Build Coastguard Worker## Output preprocessing 189*9e3b08aeSAndroid Build Coastguard Worker 190*9e3b08aeSAndroid Build Coastguard WorkerBefore `stg` outputs a serialised graph, it performs: 191*9e3b08aeSAndroid Build Coastguard Worker 192*9e3b08aeSAndroid Build Coastguard Worker1. a type normalisation step that unifies overlapping type definitions 193*9e3b08aeSAndroid Build Coastguard Worker2. a final deduplication step to eliminate other redundant nodes 194