Debian Codegraph @

gcc/ctfc.h

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