xref: /aosp_15_r20/external/stg/doc/stg.md (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1*9e3b08aeSAndroid Build Coastguard Worker# `stg`
2*9e3b08aeSAndroid Build Coastguard Worker
3*9e3b08aeSAndroid Build Coastguard Worker`stg` is used to extract and process ABI representations from libabigail XML,
4*9e3b08aeSAndroid Build Coastguard WorkerBTF and ELF/DWARF.
5*9e3b08aeSAndroid Build Coastguard Worker
6*9e3b08aeSAndroid Build Coastguard Worker## Synopsis
7*9e3b08aeSAndroid Build Coastguard Worker
8*9e3b08aeSAndroid Build Coastguard Worker```
9*9e3b08aeSAndroid Build Coastguard Workerstg
10*9e3b08aeSAndroid Build Coastguard Worker  [-m|--metrics]
11*9e3b08aeSAndroid Build Coastguard Worker  [-d|--keep-duplicates]
12*9e3b08aeSAndroid Build Coastguard Worker  [-t|--types]
13*9e3b08aeSAndroid Build Coastguard Worker  [-F|--files|--file-filter <filter>]
14*9e3b08aeSAndroid Build Coastguard Worker  [-S|--symbols|--symbol-filter <filter>]
15*9e3b08aeSAndroid Build Coastguard Worker  [-a|--abi|-b|--btf|-e|--elf|-s|--stg] [file] ...
16*9e3b08aeSAndroid Build Coastguard Worker  [{-o|--output} {filename|-}] ...
17*9e3b08aeSAndroid Build Coastguard Worker  [-A|--annotate]
18*9e3b08aeSAndroid Build Coastguard Workerimplicit defaults: --abi
19*9e3b08aeSAndroid Build Coastguard Workerfilter syntax:
20*9e3b08aeSAndroid Build Coastguard Worker  <filter>   ::= <term>          |  <expression> '|' <term>
21*9e3b08aeSAndroid Build Coastguard Worker  <term>     ::= <factor>        |  <term> '&' <factor>
22*9e3b08aeSAndroid Build Coastguard Worker  <factor>   ::= <atom>          |  '!' <factor>
23*9e3b08aeSAndroid Build Coastguard Worker  <atom>     ::= ':' <filename>  |  <glob>  |  '(' <expression> ')'
24*9e3b08aeSAndroid Build Coastguard Worker  <filename> ::= <string>
25*9e3b08aeSAndroid Build Coastguard Worker  <glob>     ::= <string>
26*9e3b08aeSAndroid Build Coastguard Worker```
27*9e3b08aeSAndroid Build Coastguard Worker
28*9e3b08aeSAndroid Build Coastguard Worker## Input
29*9e3b08aeSAndroid Build Coastguard Worker
30*9e3b08aeSAndroid Build Coastguard WorkerThe tool can be passed any number of inputs to combine into a unified ABI.
31*9e3b08aeSAndroid Build Coastguard Worker
32*9e3b08aeSAndroid Build Coastguard Worker### Formats
33*9e3b08aeSAndroid Build Coastguard Worker
34*9e3b08aeSAndroid Build Coastguard Worker*   `-a|--abi`
35*9e3b08aeSAndroid Build Coastguard Worker
36*9e3b08aeSAndroid Build Coastguard Worker    Read ABI XML representation generated by libabigail's `abidw`. Not all ABI
37*9e3b08aeSAndroid Build Coastguard Worker    XML features are consumed. Some XML "tidying" is performed before parsing:
38*9e3b08aeSAndroid Build Coastguard Worker
39*9e3b08aeSAndroid Build Coastguard Worker    *   types with naming typedefs are re-anonymised
40*9e3b08aeSAndroid Build Coastguard Worker    *   (rare) duplicate data members are removed
41*9e3b08aeSAndroid Build Coastguard Worker    *   (partial and entire) duplicate type definitions are removed
42*9e3b08aeSAndroid Build Coastguard Worker
43*9e3b08aeSAndroid Build Coastguard Worker    After parsing, function parameter and return type qualifiers are removed.
44*9e3b08aeSAndroid Build Coastguard Worker
45*9e3b08aeSAndroid Build Coastguard Worker*   `-b|--btf`
46*9e3b08aeSAndroid Build Coastguard Worker
47*9e3b08aeSAndroid Build Coastguard Worker    Read ABI information from the `.BTF` ELF section. BTF only covers the C type
48*9e3b08aeSAndroid Build Coastguard Worker    system and can be obtained in the following ways:
49*9e3b08aeSAndroid Build Coastguard Worker
50*9e3b08aeSAndroid Build Coastguard Worker    *   `gcc -gbtf` generates BTF instead of DWARF
51*9e3b08aeSAndroid Build Coastguard Worker    *   `clang -c -g -target bpf` works similarly, but only for BPF targets
52*9e3b08aeSAndroid Build Coastguard Worker    *   `pahole -J` reads existing DWARF debug information and adds BTF
53*9e3b08aeSAndroid Build Coastguard Worker
54*9e3b08aeSAndroid Build Coastguard Worker*   `-e|--elf`
55*9e3b08aeSAndroid Build Coastguard Worker
56*9e3b08aeSAndroid Build Coastguard Worker    Read ABI information from ELF symbols and DWARF types.
57*9e3b08aeSAndroid Build Coastguard Worker
58*9e3b08aeSAndroid Build Coastguard Worker*   `-s|--stg`
59*9e3b08aeSAndroid Build Coastguard Worker
60*9e3b08aeSAndroid Build Coastguard Worker    Read ABI information from a `.stg` file.
61*9e3b08aeSAndroid Build Coastguard Worker
62*9e3b08aeSAndroid Build Coastguard Worker### Options
63*9e3b08aeSAndroid Build Coastguard Worker
64*9e3b08aeSAndroid Build Coastguard Worker*   `--types`
65*9e3b08aeSAndroid Build Coastguard Worker
66*9e3b08aeSAndroid Build Coastguard Worker    Captures all named types found in ELF files as interface types, regardless
67*9e3b08aeSAndroid Build Coastguard Worker    of whether those types are reachable by any symbol.
68*9e3b08aeSAndroid Build Coastguard Worker
69*9e3b08aeSAndroid Build Coastguard Worker## Merge
70*9e3b08aeSAndroid Build Coastguard Worker
71*9e3b08aeSAndroid Build Coastguard WorkerIf multiple (or zero) inputs are provided, then ABI roots from all inputs are
72*9e3b08aeSAndroid Build Coastguard Workermerged.
73*9e3b08aeSAndroid Build Coastguard Worker
74*9e3b08aeSAndroid Build Coastguard Worker### Symbols
75*9e3b08aeSAndroid Build Coastguard Worker
76*9e3b08aeSAndroid Build Coastguard WorkerSymbols must be disjoint across all inputs.
77*9e3b08aeSAndroid Build Coastguard Worker
78*9e3b08aeSAndroid Build Coastguard Worker### Types
79*9e3b08aeSAndroid Build Coastguard Worker
80*9e3b08aeSAndroid Build Coastguard WorkerIf duplicate type roots are found during merge, they are unified. If unification
81*9e3b08aeSAndroid Build Coastguard Workerfails, the merge fails.
82*9e3b08aeSAndroid Build Coastguard Worker
83*9e3b08aeSAndroid Build Coastguard WorkerUnification is a process which replaces references to forward declarations of
84*9e3b08aeSAndroid Build Coastguard Workertypes with references to full definitions, if that would result in equal types.
85*9e3b08aeSAndroid Build Coastguard Worker
86*9e3b08aeSAndroid Build Coastguard Worker## Filter
87*9e3b08aeSAndroid Build Coastguard Worker
88*9e3b08aeSAndroid Build Coastguard WorkerThere are two types of filters that can be applied to STG output:
89*9e3b08aeSAndroid Build Coastguard Worker
90*9e3b08aeSAndroid Build Coastguard Worker*   `-F|--files|--file-filter <filter>`
91*9e3b08aeSAndroid Build Coastguard Worker
92*9e3b08aeSAndroid Build Coastguard Worker    Filter type definitions by source location.
93*9e3b08aeSAndroid Build Coastguard Worker
94*9e3b08aeSAndroid Build Coastguard Worker    DWARF information includes type declaration source locations. If a struct,
95*9e3b08aeSAndroid Build Coastguard Worker    union, class or enum is defined in a file whose name does not match the
96*9e3b08aeSAndroid Build Coastguard Worker    filter, then its definition (layout, members etc.) is dropped, leaving only
97*9e3b08aeSAndroid Build Coastguard Worker    a declaration.
98*9e3b08aeSAndroid Build Coastguard Worker
99*9e3b08aeSAndroid Build Coastguard Worker    File filters are only applicable to ELF binary objects containing DWARF with
100*9e3b08aeSAndroid Build Coastguard Worker    source location information; any other kind of input will be unaffected.
101*9e3b08aeSAndroid Build Coastguard Worker
102*9e3b08aeSAndroid Build Coastguard Worker*   `-S|--symbols|--symbol-filter <filter>`
103*9e3b08aeSAndroid Build Coastguard Worker
104*9e3b08aeSAndroid Build Coastguard Worker    Filter ELF symbols by name (which may include a `@version` or `@@version`
105*9e3b08aeSAndroid Build Coastguard Worker    suffix).
106*9e3b08aeSAndroid Build Coastguard Worker
107*9e3b08aeSAndroid Build Coastguard Worker    If a symbol filter is supplied, symbols not matching the filter are dropped.
108*9e3b08aeSAndroid Build Coastguard Worker    Symbol filtering is universal across all input formats as it happens after
109*9e3b08aeSAndroid Build Coastguard Worker    input processing.
110*9e3b08aeSAndroid Build Coastguard Worker
111*9e3b08aeSAndroid Build Coastguard WorkerThe basic syntactical elements are:
112*9e3b08aeSAndroid Build Coastguard Worker
113*9e3b08aeSAndroid Build Coastguard Worker*   `glob` - a **glob**(7) pattern supporting `?`, `*` and `[ ... ]` wilcards
114*9e3b08aeSAndroid Build Coastguard Worker*   `:filename` - the name of a file containing a libabigail format filter list
115*9e3b08aeSAndroid Build Coastguard Worker
116*9e3b08aeSAndroid Build Coastguard WorkerFilter expressions can be combined with infix disjuction (`|`) and conjunction
117*9e3b08aeSAndroid Build Coastguard Worker(`&`) operators and negated with the prefix (`!`) operator; these obey the usual
118*9e3b08aeSAndroid Build Coastguard Workerprecedence rules. Parentheses (`( ... )`) can be used to enclose subexpressions.
119*9e3b08aeSAndroid Build Coastguard WorkerWhitespace is not significant, except as a string delimiter.
120*9e3b08aeSAndroid Build Coastguard Worker
121*9e3b08aeSAndroid Build Coastguard Worker### Examples
122*9e3b08aeSAndroid Build Coastguard Worker
123*9e3b08aeSAndroid Build Coastguard WorkerSymbol filters:
124*9e3b08aeSAndroid Build Coastguard Worker
125*9e3b08aeSAndroid Build Coastguard Worker*   `jiffies |panic` - keep just the symbols `jiffies` and `panic`
126*9e3b08aeSAndroid Build Coastguard Worker*   `str*` - keep symbols beginning with `str` such as `strncpy_from_user`
127*9e3b08aeSAndroid Build Coastguard Worker*   `!(*@* & ! *@@*`) - drop versioned symbols that are not the default versions
128*9e3b08aeSAndroid Build Coastguard Worker*   `!*@*|*@@*` - the same
129*9e3b08aeSAndroid Build Coastguard Worker*   `:include & !:exclude` - keep symbols that are in the symbol list file
130*9e3b08aeSAndroid Build Coastguard Worker    `include` but not in the symbol list file `exclude`
131*9e3b08aeSAndroid Build Coastguard Worker
132*9e3b08aeSAndroid Build Coastguard WorkerFile filters:
133*9e3b08aeSAndroid Build Coastguard Worker
134*9e3b08aeSAndroid Build Coastguard Worker*   `!*.cc` - exclude all type definitions found in source files named `*.cc`
135*9e3b08aeSAndroid Build Coastguard Worker*   `*.h` - include only type definitions found in source files named `*.h`
136*9e3b08aeSAndroid Build Coastguard Worker
137*9e3b08aeSAndroid Build Coastguard Worker## Deduplication
138*9e3b08aeSAndroid Build Coastguard Worker
139*9e3b08aeSAndroid Build Coastguard WorkerABI representations, particularly merged ones, almost always have some sets of
140*9e3b08aeSAndroid Build Coastguard Workernodes that are recursively equal. By default, duplicate nodes are eliminated.
141*9e3b08aeSAndroid Build Coastguard Worker
142*9e3b08aeSAndroid Build Coastguard Worker*   `-d|--keep-duplicates`
143*9e3b08aeSAndroid Build Coastguard Worker
144*9e3b08aeSAndroid Build Coastguard Worker    Skip the deduplication pass.
145*9e3b08aeSAndroid Build Coastguard Worker
146*9e3b08aeSAndroid Build Coastguard Worker## Output
147*9e3b08aeSAndroid Build Coastguard Worker
148*9e3b08aeSAndroid Build Coastguard Worker*   `-o|--output`
149*9e3b08aeSAndroid Build Coastguard Worker
150*9e3b08aeSAndroid Build Coastguard Worker    Zero or more outputs can be requested. The filename `-` is recognised as a
151*9e3b08aeSAndroid Build Coastguard Worker    synonym for stdout.
152*9e3b08aeSAndroid Build Coastguard Worker
153*9e3b08aeSAndroid Build Coastguard Worker    The output will be an ABI representation in STG's native format.
154*9e3b08aeSAndroid Build Coastguard Worker
155*9e3b08aeSAndroid Build Coastguard Worker## Diagnostics
156*9e3b08aeSAndroid Build Coastguard Worker
157*9e3b08aeSAndroid Build Coastguard Worker*   `-m|--metrics`
158*9e3b08aeSAndroid Build Coastguard Worker
159*9e3b08aeSAndroid Build Coastguard Worker    Print various internal timing and other metrics.
160*9e3b08aeSAndroid Build Coastguard Worker
161*9e3b08aeSAndroid Build Coastguard Worker## Annotations
162*9e3b08aeSAndroid Build Coastguard Worker
163*9e3b08aeSAndroid Build Coastguard Worker*   `-A|--annotate`
164*9e3b08aeSAndroid Build Coastguard Worker
165*9e3b08aeSAndroid Build Coastguard Worker    Output node descriptions for edges in the graph as inline textual protobuf
166*9e3b08aeSAndroid Build Coastguard Worker    comments.
167*9e3b08aeSAndroid Build Coastguard Worker
168*9e3b08aeSAndroid Build Coastguard Worker    Sample STG output:
169*9e3b08aeSAndroid Build Coastguard Worker
170*9e3b08aeSAndroid Build Coastguard Worker    ```
171*9e3b08aeSAndroid Build Coastguard Worker    root_id: 0x84ea5130  # interface
172*9e3b08aeSAndroid Build Coastguard Worker    primitive {
173*9e3b08aeSAndroid Build Coastguard Worker      id: 0x6720d32f
174*9e3b08aeSAndroid Build Coastguard Worker      name: "int"
175*9e3b08aeSAndroid Build Coastguard Worker      bytesize: 0x00000004
176*9e3b08aeSAndroid Build Coastguard Worker    }
177*9e3b08aeSAndroid Build Coastguard Worker    struct_union {
178*9e3b08aeSAndroid Build Coastguard Worker      id: 0xf57dfbfc
179*9e3b08aeSAndroid Build Coastguard Worker      kind: STRUCT
180*9e3b08aeSAndroid Build Coastguard Worker      name: "S"
181*9e3b08aeSAndroid Build Coastguard Worker    }
182*9e3b08aeSAndroid Build Coastguard Worker    function {
183*9e3b08aeSAndroid Build Coastguard Worker      id: 0x85d454a8
184*9e3b08aeSAndroid Build Coastguard Worker      return_type_id: 0x6720d32f  # int
185*9e3b08aeSAndroid Build Coastguard Worker      parameter_id: 0x6720d32f  # int
186*9e3b08aeSAndroid Build Coastguard Worker    }
187*9e3b08aeSAndroid Build Coastguard Worker    elf_symbol {
188*9e3b08aeSAndroid Build Coastguard Worker      id: 0x8bf70937
189*9e3b08aeSAndroid Build Coastguard Worker      name: "func"
190*9e3b08aeSAndroid Build Coastguard Worker      symbol_type: FUNCTION
191*9e3b08aeSAndroid Build Coastguard Worker      type_id: 0x85d454a8  # int(int)
192*9e3b08aeSAndroid Build Coastguard Worker    }
193*9e3b08aeSAndroid Build Coastguard Worker    elf_symbol {
194*9e3b08aeSAndroid Build Coastguard Worker      id: 0x3e4f6c44
195*9e3b08aeSAndroid Build Coastguard Worker      name: "s"
196*9e3b08aeSAndroid Build Coastguard Worker      symbol_type: OBJECT
197*9e3b08aeSAndroid Build Coastguard Worker      type_id: 0xf57dfbfc  # struct S
198*9e3b08aeSAndroid Build Coastguard Worker    }
199*9e3b08aeSAndroid Build Coastguard Worker    interface {
200*9e3b08aeSAndroid Build Coastguard Worker      id: 0x84ea5130
201*9e3b08aeSAndroid Build Coastguard Worker      symbol_id: 0x8bf70937  # int func(int)
202*9e3b08aeSAndroid Build Coastguard Worker      symbol_id: 0x3e4f6c44  # struct S s
203*9e3b08aeSAndroid Build Coastguard Worker    }
204*9e3b08aeSAndroid Build Coastguard Worker    ```
205