Line data Source code
1 : /* ctfc.h - Declarations and definitions related to the CTF container.
2 : Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : /* This file defines the data structures and functions used by the compiler
21 : to generate the CTF debug info. The definitions below are compiler internal
22 : representations and closely reflect the CTF format requirements in <ctf.h>.
23 :
24 : The contents of the CTF container are used eventually for emission of both
25 : CTF (ctfout.cc) and BTF debug info (btfout.cc), as the two type debug formats
26 : are close cousins. */
27 :
28 : #ifndef GCC_CTFC_H
29 : #define GCC_CTFC_H 1
30 :
31 : #include "config.h"
32 : #include "system.h"
33 : #include "tree.h"
34 : #include "fold-const.h"
35 : #include "dwarf2ctf.h"
36 : #include "ctf.h"
37 : #include "btf.h"
38 :
39 : /* Invalid CTF type ID definition. */
40 :
41 : #define CTF_NULL_TYPEID 0
42 :
43 : /* Value to start generating the CTF type ID from. */
44 :
45 : #define CTF_INIT_TYPEID 1
46 :
47 : /* CTF type ID. */
48 :
49 : typedef uint64_t ctf_id_t;
50 :
51 : struct ctf_dtdef;
52 : typedef struct ctf_dtdef ctf_dtdef_t;
53 : typedef ctf_dtdef_t * ctf_dtdef_ref;
54 :
55 : struct ctf_dvdef;
56 : typedef struct ctf_dvdef ctf_dvdef_t;
57 : typedef ctf_dvdef_t * ctf_dvdef_ref;
58 :
59 : /* CTF string table element (list node). */
60 :
61 : typedef struct GTY ((chain_next ("%h.cts_next"))) ctf_string
62 : {
63 : const char * cts_str; /* CTF string. */
64 : struct ctf_string * cts_next; /* A list node. */
65 : } ctf_string_t;
66 :
67 : /* Internal representation of CTF string table. */
68 :
69 : typedef struct GTY (()) ctf_strtable
70 : {
71 : ctf_string_t * ctstab_head; /* Head str ptr. */
72 : ctf_string_t * ctstab_tail; /* Tail. new str appended to tail. */
73 : int ctstab_num; /* Number of strings in the table. */
74 : size_t ctstab_len; /* Size of string table in bytes. */
75 : const char * ctstab_estr; /* Empty string "". */
76 : } ctf_strtable_t;
77 :
78 : /* Encoding information for integers, floating-point values etc. The flags
79 : field will contain values appropriate for the type defined in <ctf.h>. */
80 :
81 : typedef struct GTY (()) ctf_encoding
82 : {
83 : unsigned int cte_format; /* Data format (CTF_INT_* or CTF_FP_* flags). */
84 : unsigned int cte_offset; /* Offset of value in bits. */
85 : unsigned int cte_bits; /* Size of storage in bits. */
86 : } ctf_encoding_t;
87 :
88 : /* Array information for CTF generation. */
89 :
90 : typedef struct GTY (()) ctf_arinfo
91 : {
92 : ctf_dtdef_ref ctr_contents; /* Type of array contents. */
93 : ctf_dtdef_ref ctr_index; /* Type of array index. */
94 : unsigned int ctr_nelems; /* Number of elements. */
95 : } ctf_arinfo_t;
96 :
97 : /* Function information for CTF generation. */
98 :
99 : typedef struct GTY (()) ctf_funcinfo
100 : {
101 : ctf_dtdef_ref ctc_return; /* Function return type. */
102 : unsigned int ctc_argc; /* Number of typed arguments to function. */
103 : unsigned int ctc_flags; /* Function attributes (see below). */
104 : } ctf_funcinfo_t;
105 :
106 : typedef struct GTY (()) ctf_sliceinfo
107 : {
108 : ctf_dtdef_ref cts_type; /* Reference CTF type. */
109 : unsigned short cts_offset; /* Offset in bits of the first bit. */
110 : unsigned short cts_bits; /* Size in bits. */
111 : } ctf_sliceinfo_t;
112 :
113 : /* CTF type representation internal to the compiler. It closely reflects the
114 : ctf_type_t type node in <ctf.h> except the GTY (()) tags. */
115 :
116 : typedef struct GTY (()) ctf_itype
117 : {
118 : uint32_t ctti_name; /* Reference to name in string table. */
119 : uint32_t ctti_info; /* Encoded kind, variant length (see below). */
120 : union GTY ((desc ("0")))
121 : {
122 : uint32_t GTY ((tag ("0"))) _size;/* Size of entire type in bytes. */
123 : uint32_t GTY ((tag ("1"))) _type;/* Reference to another type. */
124 : } _u;
125 : uint32_t ctti_lsizehi; /* High 32 bits of type size in bytes. */
126 : uint32_t ctti_lsizelo; /* Low 32 bits of type size in bytes. */
127 : } ctf_itype_t;
128 :
129 : #define ctti_size _u._size
130 : #define ctti_type _u._type
131 :
132 : /* Function arguments end with varargs. */
133 :
134 : #define CTF_FUNC_VARARG 0x1
135 :
136 : /* Struct/union/enum member definition for CTF generation. */
137 :
138 : typedef struct GTY ((chain_next ("%h.dmd_next"))) ctf_dmdef
139 : {
140 : const char * dmd_name; /* Name of this member. */
141 : ctf_dtdef_ref dmd_type; /* Type of this member (for sou). */
142 : uint32_t dmd_name_offset; /* Offset of the name in str table. */
143 : uint64_t dmd_offset; /* Offset of this member in bits (for sou). */
144 : HOST_WIDE_INT dmd_value; /* Value of this member (for enum). */
145 : struct ctf_dmdef * dmd_next; /* A list node. */
146 : } ctf_dmdef_t;
147 :
148 : #define ctf_dmd_list_next(elem) ((ctf_dmdef_t *)((elem)->dmd_next))
149 :
150 : /* Function Argument. */
151 :
152 : typedef struct GTY (()) ctf_func_arg
153 : {
154 : ctf_dtdef_ref farg_type; /* Type of the argument. */
155 : const char * farg_name; /* Name of the argument. */
156 : uint32_t farg_name_offset; /* Offset of the name in str table. */
157 : struct ctf_func_arg * farg_next;/* A list node. */
158 : } ctf_func_arg_t;
159 :
160 : #define ctf_farg_list_next(elem) ((ctf_func_arg_t *)((elem)->farg_next))
161 :
162 : /* Declaration Tag.
163 : May be used to annotate struct/union members, variable declarations,
164 : function declarations, and function arguments. */
165 :
166 : typedef struct GTY (()) ctf_decl_tag
167 : {
168 : uint32_t component_idx; /* Index of component to which tag applies. */
169 : ctf_dvdef_ref ref_var; /* Non-null iff this tag applies to a variable. */
170 : } ctf_decl_tag_t;
171 :
172 : /* Type definition for CTF generation. */
173 :
174 : struct GTY ((for_user)) ctf_dtdef
175 : {
176 : dw_die_ref dtd_key; /* Type key for hashing. */
177 : const char * dtd_name; /* Name associated with definition (if any). */
178 : ctf_id_t dtd_type; /* Type identifier for this definition. */
179 : ctf_dtdef_ref ref_type; /* Type referred to by this type (if any). */
180 : ctf_itype_t dtd_data; /* Type node. */
181 : uint32_t linkage; /* Used in function types. 0=local, 1=global. */
182 :
183 : /* Whether this type was added from a global function. */
184 : BOOL_BITFIELD from_global_func : 1;
185 : /* Enum signedness. */
186 : BOOL_BITFIELD dtd_enum_unsigned : 1;
187 : /* Lots of spare bits. */
188 :
189 : union GTY ((desc ("ctf_dtu_d_union_selector (&%1)")))
190 : {
191 : /* struct, union, or enum. */
192 : ctf_dmdef_t * GTY ((tag ("CTF_DTU_D_MEMBERS"))) dtu_members;
193 : /* array. */
194 : ctf_arinfo_t GTY ((tag ("CTF_DTU_D_ARRAY"))) dtu_arr;
195 : /* integer or float. */
196 : ctf_encoding_t GTY ((tag ("CTF_DTU_D_ENCODING"))) dtu_enc;
197 : /* function. */
198 : ctf_func_arg_t * GTY ((tag ("CTF_DTU_D_ARGUMENTS"))) dtu_argv;
199 : /* slice. */
200 : ctf_sliceinfo_t GTY ((tag ("CTF_DTU_D_SLICE"))) dtu_slice;
201 : /* decl tag. */
202 : ctf_decl_tag_t GTY ((tag ("CTF_DTU_D_TAG"))) dtu_tag;
203 : } dtd_u;
204 : };
205 :
206 : #define ctf_type_id(dtd) ((uint32_t) dtd->dtd_type)
207 :
208 : /* Variable definition for CTF generation. */
209 :
210 : struct GTY ((for_user)) ctf_dvdef
211 : {
212 : dw_die_ref dvd_key; /* DWARF DIE corresponding to the variable. */
213 : const char * dvd_name; /* Name associated with variable. */
214 : uint32_t dvd_name_offset; /* Offset of the name in str table. */
215 : unsigned int dvd_visibility; /* External visibility. 0=static,1=global. */
216 : ctf_dtdef_ref dvd_type; /* Type of variable. */
217 : ctf_id_t dvd_id; /* ID of this variable. Only used for BTF. */
218 : };
219 :
220 : /* Location information for CTF Types and CTF Variables. */
221 :
222 : typedef struct GTY (()) ctf_srcloc
223 : {
224 : const char * ctsloc_file;
225 : unsigned int ctsloc_line;
226 : unsigned int ctsloc_col;
227 : } ctf_srcloc_t;
228 :
229 : typedef ctf_srcloc_t * ctf_srcloc_ref;
230 :
231 : /* Helper enum and api for the GTY machinery to work on union dtu_d. */
232 :
233 : enum ctf_dtu_d_union_enum {
234 : CTF_DTU_D_MEMBERS,
235 : CTF_DTU_D_ARRAY,
236 : CTF_DTU_D_ENCODING,
237 : CTF_DTU_D_ARGUMENTS,
238 : CTF_DTU_D_SLICE,
239 : CTF_DTU_D_TAG,
240 : };
241 :
242 : enum ctf_dtu_d_union_enum
243 : ctf_dtu_d_union_selector (ctf_dtdef_ref);
244 :
245 : struct ctfc_dtd_hasher : ggc_ptr_hash <ctf_dtdef_t>
246 : {
247 : typedef ctf_dtdef_ref compare_type;
248 :
249 : static hashval_t hash (ctf_dtdef_ref);
250 : static bool equal (ctf_dtdef_ref, ctf_dtdef_ref);
251 : };
252 :
253 : inline hashval_t
254 9221 : ctfc_dtd_hasher::hash (ctf_dtdef_ref dtd)
255 : {
256 9221 : return htab_hash_pointer (dtd->dtd_key);
257 : }
258 :
259 : inline bool
260 3738 : ctfc_dtd_hasher::equal (ctf_dtdef_ref dtd, ctf_dtdef_ref dtd2)
261 : {
262 3738 : return (dtd->dtd_key == dtd2->dtd_key);
263 : }
264 :
265 : struct ctfc_dvd_hasher : ggc_ptr_hash <ctf_dvdef_t>
266 : {
267 : typedef ctf_dvdef_ref compare_type;
268 :
269 : static hashval_t hash (ctf_dvdef_ref);
270 : static bool equal (ctf_dvdef_ref, ctf_dvdef_ref);
271 : };
272 :
273 : inline hashval_t
274 1484 : ctfc_dvd_hasher::hash (ctf_dvdef_ref dvd)
275 : {
276 1484 : return htab_hash_pointer (dvd->dvd_key);
277 : }
278 :
279 : inline bool
280 200 : ctfc_dvd_hasher::equal (ctf_dvdef_ref dvd, ctf_dvdef_ref dvd2)
281 : {
282 200 : return (dvd->dvd_key == dvd2->dvd_key);
283 : }
284 :
285 : /* CTF container structure.
286 : It is the context passed around when generating ctf debug info. There is
287 : one container per translation unit. */
288 :
289 : typedef struct GTY (()) ctf_container
290 : {
291 : /* CTF Preamble. */
292 : unsigned short ctfc_magic;
293 : unsigned char ctfc_version;
294 : unsigned char ctfc_flags;
295 : uint32_t ctfc_cuname_offset;
296 :
297 : /* CTF types. */
298 : hash_table <ctfc_dtd_hasher> * GTY (()) ctfc_types;
299 : /* CTF variables. */
300 : hash_table <ctfc_dvd_hasher> * GTY (()) ctfc_vars;
301 : /* CTF variables to be ignored. */
302 : hash_table <ctfc_dvd_hasher> * GTY (()) ctfc_ignore_vars;
303 :
304 : /* BTF type and decl tags. Not yet represented in CTF. These tags also
305 : uniquely have a one-to-many relation with DIEs, meaning a single DIE
306 : may translate to multiple CTF/BTF records. For both of these reasons,
307 : they cannot be stored in the regular types table. */
308 : vec <ctf_dtdef_ref, va_gc> * GTY (()) ctfc_tags;
309 : /* Type tags logically form part of the type chain similar to cv-quals.
310 : Therefore references to types need to know if the referred-to type has
311 : any type tags, and if so to refer to the outermost type tag. This map
312 : maps a type to the outermost type tag created for it, if any. */
313 : hash_map <ctf_dtdef_ref, ctf_dtdef_ref> * GTY (()) ctfc_type_tags_map;
314 :
315 : /* CTF string table. */
316 : ctf_strtable_t ctfc_strtable;
317 : /* Auxilliary string table. At this time, used for keeping func arg names
318 : for BTF. */
319 : ctf_strtable_t ctfc_aux_strtable;
320 :
321 : uint64_t ctfc_num_types;
322 : uint64_t ctfc_num_stypes;
323 : uint64_t ctfc_num_global_funcs;
324 : uint64_t ctfc_num_global_objts;
325 :
326 : /* Number of vlen bytes - the variable length portion after ctf_type_t and
327 : ctf_stype_t in the CTF section. This is used to calculate the offsets in
328 : the CTF header. */
329 : uint64_t ctfc_num_vlen_bytes;
330 :
331 : /* Next CTF type id to assign. */
332 : ctf_id_t ctfc_nextid;
333 :
334 : /* Specify an explicit length of 0 so that the GC marking routines steer
335 : clear of marking the CTF vars and CTF types twice. These lists below do
336 : not own the pointed to objects, they simply hold references to them. */
337 :
338 : /* List of pre-processed CTF Variables. CTF requires that the variables
339 : appear in the sorted order of their names. */
340 : ctf_dvdef_t ** GTY ((length ("0"))) ctfc_vars_list;
341 : /* Count of pre-processed CTF Variables in the list. */
342 : uint64_t ctfc_vars_list_count;
343 : /* List of pre-processed CTF types. CTF requires that a shared type must
344 : appear before the type that uses it. For the compiler, this means types
345 : are emitted in sorted order of their type IDs. */
346 : ctf_dtdef_t ** GTY ((length ("0"))) ctfc_types_list;
347 : /* List of CTF function types for global functions. The order of global
348 : function entries in the CTF funcinfo section is undefined by the
349 : compiler. */
350 : ctf_dtdef_t ** GTY ((length ("0"))) ctfc_gfuncs_list;
351 : /* List of CTF variables at global scope. The order of global object entries
352 : in the CTF objinfo section is undefined by the compiler. */
353 : ctf_dvdef_t ** GTY ((length ("0"))) ctfc_gobjts_list;
354 :
355 : /* Following members are for debugging only. They do not add functional
356 : value to the task of CTF creation. These can be cleaned up once CTF
357 : generation stabilizes. */
358 :
359 : /* Keep a count of the number of bytes dumped in asm for debugging
360 : purposes. */
361 : uint64_t ctfc_numbytes_asm;
362 : /* Total length of all strings in CTF. */
363 : size_t ctfc_strlen;
364 : /* Total length of all strings in aux string table. */
365 : size_t ctfc_aux_strlen;
366 :
367 : } ctf_container_t;
368 :
369 : /* Markers for which string table from the CTF container to use. */
370 :
371 : #define CTF_STRTAB 0 /* CTF string table. */
372 : #define CTF_AUX_STRTAB 1 /* CTF auxilliary string table. */
373 :
374 : typedef ctf_container_t * ctf_container_ref;
375 :
376 : extern GTY (()) ctf_container_ref tu_ctfc;
377 :
378 : extern void ctfc_delete_container (ctf_container_ref);
379 :
380 : /* If the next ctf type id is still set to the init value, no ctf records to
381 : report. */
382 : extern bool ctfc_is_empty_container (ctf_container_ref);
383 :
384 : /* Get the total number of CTF types in the container. */
385 :
386 : extern unsigned int ctfc_get_num_ctf_types (ctf_container_ref);
387 :
388 : /* Get the total number of CTF variables in the container. */
389 :
390 : extern unsigned int ctfc_get_num_ctf_vars (ctf_container_ref);
391 :
392 : /* Get reference to the CTF string table or the CTF auxilliary
393 : string table. */
394 :
395 : extern ctf_strtable_t * ctfc_get_strtab (ctf_container_ref, int);
396 :
397 : extern void init_ctf_strtable (ctf_strtable_t *);
398 : extern void ctfc_delete_strtab (ctf_strtable_t *);
399 :
400 : /* Get the length of the specified string table in the CTF container. */
401 :
402 : extern size_t ctfc_get_strtab_len (ctf_container_ref, int);
403 :
404 : /* Get the number of bytes to represent the variable length portion of all CTF
405 : types in the CTF container. */
406 :
407 : extern size_t ctfc_get_num_vlen_bytes (ctf_container_ref);
408 :
409 : /* The compiler demarcates whether types are visible at top-level scope or not.
410 : The only example so far of a type not visible at top-level scope is slices.
411 : CTF_ADD_NONROOT is used to indicate the latter. */
412 : #define CTF_ADD_NONROOT 0 /* CTF type only visible in nested scope. */
413 : #define CTF_ADD_ROOT 1 /* CTF type visible at top-level scope. */
414 :
415 : /* These APIs allow to initialize and finalize the CTF machinery and
416 : to add types to the CTF container associated to the current
417 : translation unit. Used in dwarf2ctf.cc. */
418 :
419 : extern void ctf_init (void);
420 : extern void ctf_output (const char * filename);
421 : extern void ctf_finalize (void);
422 :
423 : extern void btf_early_finish (void);
424 : extern void btf_finish (void);
425 : extern void btf_finalize (void);
426 :
427 : extern ctf_container_ref ctf_get_tu_ctfc (void);
428 :
429 : extern bool ctf_type_exists (ctf_container_ref, dw_die_ref, ctf_dtdef_ref *);
430 :
431 : extern void ctf_add_cuname (ctf_container_ref, const char *);
432 :
433 : extern ctf_dtdef_ref ctf_dtd_lookup (const ctf_container_ref ctfc,
434 : dw_die_ref die);
435 : extern ctf_dvdef_ref ctf_dvd_lookup (const ctf_container_ref ctfc,
436 : dw_die_ref die);
437 : extern bool ctf_dvd_ignore_lookup (const ctf_container_ref ctfc,
438 : dw_die_ref die);
439 :
440 : extern const char * ctf_add_string (ctf_container_ref, const char *,
441 : uint32_t *, int);
442 :
443 : extern ctf_dtdef_ref ctf_add_reftype (ctf_container_ref, uint32_t,
444 : ctf_dtdef_ref, uint32_t, dw_die_ref);
445 : extern ctf_dtdef_ref ctf_add_enum (ctf_container_ref, uint32_t, const char *,
446 : HOST_WIDE_INT, bool, dw_die_ref);
447 : extern ctf_dtdef_ref ctf_add_slice (ctf_container_ref, uint32_t, ctf_dtdef_ref,
448 : uint32_t, uint32_t, dw_die_ref);
449 : extern ctf_dtdef_ref ctf_add_float (ctf_container_ref, uint32_t, const char *,
450 : const ctf_encoding_t *, dw_die_ref);
451 : extern ctf_dtdef_ref ctf_add_integer (ctf_container_ref, uint32_t, const char *,
452 : const ctf_encoding_t *, dw_die_ref);
453 : extern ctf_dtdef_ref ctf_add_unknown (ctf_container_ref, uint32_t, const char *,
454 : const ctf_encoding_t *, dw_die_ref);
455 : extern ctf_dtdef_ref ctf_add_pointer (ctf_container_ref, uint32_t,
456 : ctf_dtdef_ref, dw_die_ref);
457 : extern ctf_dtdef_ref ctf_add_array (ctf_container_ref, uint32_t,
458 : const ctf_arinfo_t *, dw_die_ref);
459 : extern ctf_dtdef_ref ctf_add_forward (ctf_container_ref, uint32_t, const char *,
460 : uint32_t, dw_die_ref);
461 : extern ctf_dtdef_ref ctf_add_typedef (ctf_container_ref, uint32_t, const char *,
462 : ctf_dtdef_ref, dw_die_ref);
463 : extern ctf_dtdef_ref ctf_add_function (ctf_container_ref, uint32_t,
464 : const char *, const ctf_funcinfo_t *,
465 : dw_die_ref, bool, int);
466 : extern ctf_dtdef_ref ctf_add_sou (ctf_container_ref, uint32_t, const char *,
467 : uint32_t, unsigned HOST_WIDE_INT, dw_die_ref);
468 : extern ctf_dtdef_ref ctf_add_type_tag (ctf_container_ref, uint32_t,
469 : const char *, ctf_dtdef_ref);
470 : extern ctf_dtdef_ref ctf_add_decl_tag (ctf_container_ref, uint32_t,
471 : const char *, ctf_dtdef_ref, uint32_t);
472 : extern int ctf_add_enumerator (ctf_container_ref, ctf_dtdef_ref, const char *,
473 : HOST_WIDE_INT, dw_die_ref);
474 : extern int ctf_add_member_offset (ctf_container_ref, dw_die_ref, const char *,
475 : ctf_dtdef_ref, uint64_t);
476 : extern int ctf_add_function_arg (ctf_container_ref, dw_die_ref,
477 : const char *, ctf_dtdef_ref);
478 : extern ctf_dvdef_ref ctf_add_variable (ctf_container_ref, const char *,
479 : ctf_dtdef_ref, dw_die_ref, unsigned int,
480 : dw_die_ref);
481 :
482 : extern ctf_dtdef_ref ctf_lookup_tree_type (ctf_container_ref, const tree);
483 :
484 : /* Callback and traversal function for BTF_KIND_FUNC records. Used by BPF
485 : target for BPF CO-RE implementation. */
486 :
487 : typedef bool (*funcs_traverse_callback) (ctf_dtdef_ref, void *);
488 : bool traverse_btf_func_types (funcs_traverse_callback, void *);
489 : extern void btf_mark_type_used (tree);
490 :
491 : /* CTF section does not emit location information; at this time, location
492 : information is needed for BTF CO-RE use-cases. */
493 :
494 : extern int ctfc_get_dtd_srcloc (ctf_dtdef_ref, ctf_srcloc_ref);
495 : extern int ctfc_get_dvd_srcloc (ctf_dvdef_ref, ctf_srcloc_ref);
496 :
497 : extern uint32_t btf_dtd_kind (ctf_dtdef_ref dtd);
498 :
499 : #endif /* GCC_CTFC_H */
|