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