d: Merge upstream dmd, druntime 2bbf64907c, phobos b64bfbf91
[official-gcc.git] / gcc / btfout.cc
blobdb4f1084f85cde05fca843da37c61be7702fe7c1
1 /* Output BTF format from GCC.
2 Copyright (C) 2021-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 /* This file contains routines to output the BPF Type Format (BTF). The BTF
21 debug format is very similar to CTF; as a result, the structure of this file
22 closely resembles that of ctfout.cc, and the same CTF container objects are
23 used. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "target.h"
29 #include "memmodel.h"
30 #include "tm_p.h"
31 #include "output.h"
32 #include "dwarf2asm.h"
33 #include "debug.h"
34 #include "ctfc.h"
35 #include "diagnostic-core.h"
36 #include "cgraph.h"
37 #include "varasm.h"
38 #include "dwarf2out.h" /* For lookup_decl_die. */
40 static int btf_label_num;
42 static GTY (()) section * btf_info_section;
44 /* BTF debug info section. */
46 #ifndef BTF_INFO_SECTION_NAME
47 #define BTF_INFO_SECTION_NAME ".BTF"
48 #endif
50 #define BTF_INFO_SECTION_FLAGS (SECTION_DEBUG)
52 /* Maximum size (in bytes) for an artifically generated BTF label. */
54 #define MAX_BTF_LABEL_BYTES 40
56 static char btf_info_section_label[MAX_BTF_LABEL_BYTES];
58 #ifndef BTF_INFO_SECTION_LABEL
59 #define BTF_INFO_SECTION_LABEL "Lbtf"
60 #endif
62 /* BTF encodes void as type id 0. */
64 #define BTF_VOID_TYPEID 0
65 #define BTF_INIT_TYPEID 1
67 #define BTF_INVALID_TYPEID 0xFFFFFFFF
69 /* Mapping of CTF variables to the IDs they will be assigned when they are
70 converted to BTF_KIND_VAR type records. Strictly accounts for the index
71 from the start of the variable type entries, does not include the number
72 of types emitted prior to the variable records. */
73 static GTY (()) hash_map <ctf_dvdef_ref, unsigned> *btf_var_ids;
75 /* Mapping of type IDs from original CTF ID to BTF ID. Types do not map
76 1-to-1 from CTF to BTF. To avoid polluting the CTF container when updating
77 type references-by-ID, we use this map instead. */
78 static ctf_id_t * btf_id_map = NULL;
80 /* Information for creating the BTF_KIND_DATASEC records. */
81 typedef struct btf_datasec
83 const char *name; /* Section name, e.g. ".bss". */
84 uint32_t name_offset; /* Offset to name in string table. */
85 vec<struct btf_var_secinfo> entries; /* Variable entries in this section. */
86 } btf_datasec_t;
88 /* One BTF_KIND_DATASEC record is created for each output data section which
89 will hold at least one variable. */
90 static vec<btf_datasec_t> datasecs;
92 /* Holes occur for types which are present in the CTF container, but are either
93 non-representable or redundant in BTF. */
94 static vec<ctf_id_t> holes;
96 /* CTF definition(s) of void. Only one definition of void should be generated.
97 We should not encounter more than one definition of void, but use a vector
98 to be safe. */
99 static vec<ctf_id_t> voids;
101 /* Functions in BTF have two separate type records - one for the prototype
102 (BTF_KIND_FUNC_PROTO), as well as a BTF_KIND_FUNC. CTF_K_FUNCTION types
103 map closely to BTF_KIND_FUNC_PROTO, but the BTF_KIND_FUNC records must be
104 created. This vector holds them. */
105 static GTY (()) vec<ctf_dtdef_ref, va_gc> *funcs;
107 /* The number of BTF variables added to the TU CTF container. */
108 static unsigned int num_vars_added = 0;
110 /* The number of BTF types added to the TU CTF container. */
111 static unsigned int num_types_added = 0;
113 /* The number of types synthesized for BTF that do not correspond to
114 CTF types. */
115 static unsigned int num_types_created = 0;
117 /* Name strings for BTF kinds.
118 Note: the indices here must match the type defines in btf.h. */
119 static const char *const btf_kind_names[] =
121 "UNKN", "INT", "PTR", "ARRAY", "STRUCT", "UNION", "ENUM", "FWD",
122 "TYPEDEF", "VOLATILE", "CONST", "RESTRICT", "FUNC", "FUNC_PROTO",
123 "VAR", "DATASEC", "FLOAT", "DECL_TAG", "TYPE_TAG", "ENUM64"
126 /* Return a name string for the given BTF_KIND. */
128 static const char *
129 btf_kind_name (uint32_t btf_kind)
131 return btf_kind_names[btf_kind];
134 /* Map a CTF type kind to the corresponding BTF type kind. */
136 static uint32_t
137 get_btf_kind (uint32_t ctf_kind)
139 /* N.B. the values encoding kinds are not in general the same for the
140 same kind between CTF and BTF. e.g. CTF_K_CONST != BTF_KIND_CONST. */
141 switch (ctf_kind)
143 case CTF_K_INTEGER: return BTF_KIND_INT;
144 case CTF_K_FLOAT: return BTF_KIND_FLOAT;
145 case CTF_K_POINTER: return BTF_KIND_PTR;
146 case CTF_K_ARRAY: return BTF_KIND_ARRAY;
147 case CTF_K_FUNCTION: return BTF_KIND_FUNC_PROTO;
148 case CTF_K_STRUCT: return BTF_KIND_STRUCT;
149 case CTF_K_UNION: return BTF_KIND_UNION;
150 case CTF_K_ENUM: return BTF_KIND_ENUM;
151 case CTF_K_FORWARD: return BTF_KIND_FWD;
152 case CTF_K_TYPEDEF: return BTF_KIND_TYPEDEF;
153 case CTF_K_VOLATILE: return BTF_KIND_VOLATILE;
154 case CTF_K_CONST: return BTF_KIND_CONST;
155 case CTF_K_RESTRICT: return BTF_KIND_RESTRICT;
156 default:;
158 return BTF_KIND_UNKN;
161 /* Some BTF types, like BTF_KIND_FUNC_PROTO, are anonymous. The machinery
162 in btfout to emit BTF, may reset dtd_data->ctti_name, but does not update
163 the name in the ctf_dtdef_ref type object (deliberate choice). This
164 interface helps abstract out that state of affairs, while giving access to
165 the name of the type as intended. */
167 static const char *
168 get_btf_type_name (ctf_dtdef_ref dtd)
170 const char *anon = "";
171 return (dtd->dtd_data.ctti_name) ? dtd->dtd_name : anon;
174 /* Helper routines to map between 'relative' and 'absolute' IDs.
176 In BTF all records (including variables) are output in one long list, and all
177 inter-type references are via index into that list. But internally since we
178 a) translate from CTF, which separates variable records from regular types
179 and b) create some additional types after the fact, things like VAR and FUNC
180 records are stored in separate vectors with their own indices. These
181 functions map between the 'relative' IDs (i.e. indices in their respective
182 containers) and 'absolute' IDs (i.e. indices in the final contiguous
183 output list), which goes in order:
184 all normal type records translated from CTF
185 all BTF_KIND_VAR records
186 all BTF_KIND_FUNC records (synthesized split function records)
187 all BTF_KIND_DATASEC records (synthesized)
189 The extra '+ 1's below are to account for the implicit "void" record, which
190 has index 0 but isn't actually contained in the type list. */
192 /* Return the final BTF ID of the variable at relative index REL. */
194 static ctf_id_t
195 btf_absolute_var_id (ctf_id_t rel)
197 return rel + (num_types_added + 1);
200 /* Return the relative index of the variable with final BTF ID ABS. */
202 static ctf_id_t
203 btf_relative_var_id (ctf_id_t abs)
205 return abs - (num_types_added + 1);
208 /* Return the final BTF ID of the func record at relative index REL. */
210 static ctf_id_t
211 btf_absolute_func_id (ctf_id_t rel)
213 return rel + (num_types_added + 1) + num_vars_added;
216 /* Return the relative index of the func record with final BTF ID ABS. */
218 static ctf_id_t
219 btf_relative_func_id (ctf_id_t abs)
221 return abs - ((num_types_added + 1) + num_vars_added);
224 /* Return the final BTF ID of the datasec record at relative index REL. */
226 static ctf_id_t
227 btf_absolute_datasec_id (ctf_id_t rel)
229 return rel + (num_types_added + 1) + num_vars_added + funcs->length ();
233 /* Allocate the btf_id_map, and initialize elements to BTF_INVALID_TYPEID. */
235 static void
236 init_btf_id_map (size_t len)
238 btf_id_map = XNEWVEC (ctf_id_t, len);
240 btf_id_map[0] = BTF_VOID_TYPEID;
241 for (size_t i = 1; i < len; i++)
242 btf_id_map[i] = BTF_INVALID_TYPEID;
245 /* Return the BTF type ID of CTF type ID KEY, or BTF_INVALID_TYPEID if the CTF
246 type with ID KEY does not map to a BTF type. */
248 ctf_id_t
249 get_btf_id (ctf_id_t key)
251 return btf_id_map[key];
254 /* Set the CTF type ID KEY to map to BTF type ID VAL. */
256 static inline void
257 set_btf_id (ctf_id_t key, ctf_id_t val)
259 btf_id_map[key] = val;
262 /* Return TRUE iff the given CTF type ID maps to a BTF type which will
263 be emitted. */
264 static inline bool
265 btf_emit_id_p (ctf_id_t id)
267 return ((btf_id_map[id] != BTF_VOID_TYPEID)
268 && (btf_id_map[id] <= BTF_MAX_TYPE));
271 /* Each BTF type can be followed additional, variable-length information
272 completing the description of the type. Calculate the number of bytes
273 of variable information required to encode a given type. */
275 static uint64_t
276 btf_calc_num_vbytes (ctf_dtdef_ref dtd)
278 uint64_t vlen_bytes = 0;
280 uint32_t kind = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
281 uint32_t vlen = CTF_V2_INFO_VLEN (dtd->dtd_data.ctti_info);
283 switch (kind)
285 case BTF_KIND_UNKN:
286 case BTF_KIND_PTR:
287 case BTF_KIND_FWD:
288 case BTF_KIND_TYPEDEF:
289 case BTF_KIND_VOLATILE:
290 case BTF_KIND_CONST:
291 case BTF_KIND_RESTRICT:
292 case BTF_KIND_FUNC:
293 /* These kinds have no vlen data. */
294 break;
296 case BTF_KIND_INT:
297 /* Size 0 integers represent redundant definitions of void that will
298 not be emitted. Don't allocate space for them. */
299 if (dtd->dtd_data.ctti_size == 0)
300 break;
302 vlen_bytes += sizeof (uint32_t);
303 break;
305 case BTF_KIND_ARRAY:
306 vlen_bytes += sizeof (struct btf_array);
307 break;
309 case BTF_KIND_STRUCT:
310 case BTF_KIND_UNION:
311 vlen_bytes += vlen * sizeof (struct btf_member);
312 break;
314 case BTF_KIND_ENUM:
315 vlen_bytes += (dtd->dtd_data.ctti_size > 4)
316 ? vlen * sizeof (struct btf_enum64)
317 : vlen * sizeof (struct btf_enum);
318 break;
320 case BTF_KIND_FUNC_PROTO:
321 vlen_bytes += vlen * sizeof (struct btf_param);
322 break;
324 case BTF_KIND_VAR:
325 vlen_bytes += sizeof (struct btf_var);
326 break;
328 case BTF_KIND_DATASEC:
329 vlen_bytes += vlen * sizeof (struct btf_var_secinfo);
330 break;
332 default:
333 break;
335 return vlen_bytes;
338 /* Initialize BTF section (.BTF) for output. */
340 void
341 init_btf_sections (void)
343 btf_info_section = get_section (BTF_INFO_SECTION_NAME, BTF_INFO_SECTION_FLAGS,
344 NULL);
346 ASM_GENERATE_INTERNAL_LABEL (btf_info_section_label,
347 BTF_INFO_SECTION_LABEL, btf_label_num++);
350 /* Push a BTF datasec variable entry INFO into the datasec named SECNAME,
351 creating the datasec if it does not already exist. */
353 static void
354 btf_datasec_push_entry (ctf_container_ref ctfc, const char *secname,
355 struct btf_var_secinfo info)
357 if (secname == NULL)
358 return;
360 for (size_t i = 0; i < datasecs.length (); i++)
361 if (strcmp (datasecs[i].name, secname) == 0)
363 datasecs[i].entries.safe_push (info);
364 return;
367 /* If we don't already have a datasec record for secname, make one. */
369 uint32_t str_off;
370 ctf_add_string (ctfc, secname, &str_off, CTF_AUX_STRTAB);
371 if (strcmp (secname, ""))
372 ctfc->ctfc_aux_strlen += strlen (secname) + 1;
374 btf_datasec_t ds;
375 ds.name = secname;
376 ds.name_offset = str_off;
378 ds.entries.create (0);
379 ds.entries.safe_push (info);
381 datasecs.safe_push (ds);
385 /* Return the section name, as of interest to btf_collect_datasec, for the
386 given symtab node. Note that this deliberately returns NULL for objects
387 which do not go in a section btf_collect_datasec cares about. */
388 static const char *
389 get_section_name (symtab_node *node)
391 const char *section_name = node->get_section ();
393 if (section_name == NULL)
395 switch (categorize_decl_for_section (node->decl, 0))
397 case SECCAT_BSS:
398 section_name = ".bss";
399 break;
400 case SECCAT_DATA:
401 section_name = ".data";
402 break;
403 case SECCAT_RODATA:
404 section_name = ".rodata";
405 break;
406 default:;
410 return section_name;
413 /* Construct all BTF_KIND_DATASEC records for CTFC. One such record is created
414 for each non-empty data-containing section in the output. Each record is
415 followed by a variable number of entries describing the variables stored
416 in that section. */
418 static void
419 btf_collect_datasec (ctf_container_ref ctfc)
421 cgraph_node *func;
422 FOR_EACH_FUNCTION (func)
424 dw_die_ref die = lookup_decl_die (func->decl);
425 if (die == NULL)
426 continue;
428 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
429 if (dtd == NULL)
430 continue;
432 /* Functions actually get two types: a BTF_KIND_FUNC_PROTO, and
433 also a BTF_KIND_FUNC. But the CTF container only allocates one
434 type per function, which matches closely with BTF_KIND_FUNC_PROTO.
435 For each such function, also allocate a BTF_KIND_FUNC entry.
436 These will be output later. */
437 ctf_dtdef_ref func_dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
438 func_dtd->dtd_data = dtd->dtd_data;
439 func_dtd->dtd_data.ctti_type = dtd->dtd_type;
440 func_dtd->linkage = dtd->linkage;
441 func_dtd->dtd_name = dtd->dtd_name;
442 func_dtd->dtd_type = num_types_added + num_types_created;
444 /* Only the BTF_KIND_FUNC type actually references the name. The
445 BTF_KIND_FUNC_PROTO is always anonymous. */
446 dtd->dtd_data.ctti_name = 0;
448 vec_safe_push (funcs, func_dtd);
449 num_types_created++;
451 /* Mark any 'extern' funcs and add DATASEC entries for them. */
452 if (DECL_EXTERNAL (func->decl))
454 func_dtd->linkage = BTF_FUNC_EXTERN;
456 const char *section_name = get_section_name (func);
457 /* Note: get_section_name () returns NULL for functions in text
458 section. This is intentional, since we do not want to generate
459 DATASEC entries for them. */
460 if (section_name == NULL)
461 continue;
463 struct btf_var_secinfo info;
465 /* +1 for the sentinel type not in the types map. */
466 info.type = func_dtd->dtd_type + 1;
468 /* Both zero at compile time. */
469 info.size = 0;
470 info.offset = 0;
472 btf_datasec_push_entry (ctfc, section_name, info);
476 varpool_node *node;
477 FOR_EACH_VARIABLE (node)
479 dw_die_ref die = lookup_decl_die (node->decl);
480 if (die == NULL)
481 continue;
483 ctf_dvdef_ref dvd = ctf_dvd_lookup (ctfc, die);
484 if (dvd == NULL)
485 continue;
487 /* Mark extern variables. */
488 if (DECL_EXTERNAL (node->decl))
490 dvd->dvd_visibility = BTF_VAR_GLOBAL_EXTERN;
492 /* PR112849: avoid assuming a section for extern decls without
493 an explicit section, which would result in incorrectly
494 emitting a BTF_KIND_DATASEC entry for them. */
495 if (node->get_section () == NULL)
496 continue;
499 const char *section_name = get_section_name (node);
500 if (section_name == NULL)
501 continue;
503 struct btf_var_secinfo info;
505 info.type = 0;
506 unsigned int *var_id = btf_var_ids->get (dvd);
507 if (var_id)
508 info.type = btf_absolute_var_id (*var_id);
509 else
510 continue;
512 info.size = 0;
513 tree size = DECL_SIZE_UNIT (node->decl);
514 if (tree_fits_uhwi_p (size))
515 info.size = tree_to_uhwi (size);
516 else if (VOID_TYPE_P (TREE_TYPE (node->decl)))
517 info.size = 1;
519 /* Offset is left as 0 at compile time, to be filled in by loaders such
520 as libbpf. */
521 info.offset = 0;
523 btf_datasec_push_entry (ctfc, section_name, info);
526 num_types_created += datasecs.length ();
529 /* Return true if the type ID is that of a type which will not be emitted (for
530 example, if it is not representable in BTF). */
532 static bool
533 btf_removed_type_p (ctf_id_t id)
535 return holes.contains (id);
538 /* Adjust the given type ID to account for holes and duplicate definitions of
539 void. */
541 static ctf_id_t
542 btf_adjust_type_id (ctf_id_t id)
544 size_t n;
545 ctf_id_t i = 0;
547 /* Do not adjust invalid type markers. */
548 if (id == BTF_INVALID_TYPEID)
549 return id;
551 for (n = 0; n < voids.length (); n++)
552 if (id == voids[n])
553 return BTF_VOID_TYPEID;
555 for (n = 0; n < holes.length (); n++)
557 if (holes[n] < id)
558 i++;
559 else if (holes[n] == id)
560 return BTF_VOID_TYPEID;
563 return id - i;
566 /* Postprocessing callback routine for types. */
569 btf_dtd_postprocess_cb (ctf_dtdef_ref *slot, ctf_container_ref arg_ctfc)
571 ctf_dtdef_ref ctftype = (ctf_dtdef_ref) * slot;
573 size_t index = ctftype->dtd_type;
574 gcc_assert (index <= arg_ctfc->ctfc_types->elements ());
576 uint32_t ctf_kind, btf_kind;
578 ctf_kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
579 btf_kind = get_btf_kind (ctf_kind);
581 if (btf_kind == BTF_KIND_UNKN)
582 /* This type is not representable in BTF. Create a hole. */
583 holes.safe_push (ctftype->dtd_type);
585 else if (btf_kind == BTF_KIND_INT && ctftype->dtd_data.ctti_size == 0)
587 /* This is a (redundant) definition of void. */
588 voids.safe_push (ctftype->dtd_type);
589 holes.safe_push (ctftype->dtd_type);
592 arg_ctfc->ctfc_types_list[index] = ctftype;
594 return 1;
597 /* Preprocessing callback routine for variables. */
600 btf_dvd_emit_preprocess_cb (ctf_dvdef_ref *slot, ctf_container_ref arg_ctfc)
602 ctf_dvdef_ref var = (ctf_dvdef_ref) * slot;
604 /* If this is an extern variable declaration with a defining declaration
605 later, skip it so that only the defining declaration is emitted.
606 This is the same case, fix and reasoning as in CTF; see PR105089. */
607 if (ctf_dvd_ignore_lookup (arg_ctfc, var->dvd_key))
608 return 1;
610 /* Do not add variables which refer to unsupported types. */
611 if (!voids.contains (var->dvd_type) && btf_removed_type_p (var->dvd_type))
612 return 1;
614 arg_ctfc->ctfc_vars_list[num_vars_added] = var;
615 btf_var_ids->put (var, num_vars_added);
617 num_vars_added++;
618 num_types_created++;
620 return 1;
623 /* Preprocessing callback routine for types. */
625 static void
626 btf_dtd_emit_preprocess_cb (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
628 if (!btf_emit_id_p (dtd->dtd_type))
629 return;
631 ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (dtd);
634 /* Preprocess the CTF information to prepare for BTF output. BTF is almost a
635 subset of CTF, with many small differences in encoding, and lacking support
636 for some types (notably floating point formats).
638 During the preprocessing pass:
639 - Ascertain that the sorted list of types has been prepared. For the BTF
640 generation process, this is taken care of by the btf_init_postprocess ().
642 - BTF_KIND_FUNC and BTF_KIND_DATASEC records are constructed. These types do
643 not have analogues in CTF (the analogous type to CTF_K_FUNCTION is
644 BTF_KIND_FUNC_PROTO), but can be relatively easily deduced from CTF
645 information.
647 - Construct BTF_KIND_VAR records, representing variables.
649 - Calculate the total size in bytes of variable-length information following
650 BTF type records. This is used for outputting the BTF header.
652 After preprocessing, all BTF information is ready to be output:
653 - ctfc->ctfc_types_list holdstypes converted from CTF types. This does not
654 include KIND_VAR, KIND_FUNC, nor KIND_DATASEC types. These types have been
655 re-encoded to the appropriate representation in BTF.
656 - ctfc->ctfc_vars_list holds all variables which should be output.
657 Variables of unsupported types are not present in this list.
658 - Vector 'funcs' holds all BTF_KIND_FUNC types, one to match each
659 BTF_KIND_FUNC_PROTO.
660 - Vector 'datasecs' holds all BTF_KIND_DATASEC types. */
662 static void
663 btf_emit_preprocess (ctf_container_ref ctfc)
665 size_t num_ctf_types = ctfc->ctfc_types->elements ();
666 size_t num_ctf_vars = ctfc->ctfc_vars->elements ();
667 size_t i;
669 if (num_ctf_types)
671 gcc_assert (ctfc->ctfc_types_list);
672 /* Preprocess the types. */
673 for (i = 1; i <= num_ctf_types; i++)
674 btf_dtd_emit_preprocess_cb (ctfc, ctfc->ctfc_types_list[i]);
677 btf_var_ids = hash_map<ctf_dvdef_ref, unsigned int>::create_ggc (100);
679 if (num_ctf_vars)
681 /* Allocate and construct the list of variables. While BTF variables are
682 not distinct from types (in that variables are simply types with
683 BTF_KIND_VAR), it is simpler to maintain a separate list of variables
684 and append them to the types list during output. */
685 ctfc->ctfc_vars_list = ggc_vec_alloc<ctf_dvdef_ref>(num_ctf_vars);
686 ctfc->ctfc_vars->traverse<ctf_container_ref, btf_dvd_emit_preprocess_cb>
687 (ctfc);
689 ctfc->ctfc_num_vlen_bytes += (num_vars_added * sizeof (struct btf_var));
692 btf_collect_datasec (ctfc);
695 /* Return true iff DMD is a member description of a bit-field which can be
696 validly represented in BTF. */
698 static bool
699 btf_dmd_representable_bitfield_p (ctf_container_ref ctfc, ctf_dmdef_t *dmd)
701 ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[dmd->dmd_type];
703 if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
705 unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
706 unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
707 uint64_t sou_offset = dmd->dmd_offset;
709 if ((bits > 0xff) || ((sou_offset + word_offset) > 0xffffff))
710 return false;
712 return true;
715 return false;
718 /* BTF asm helper routines. */
720 /* Asm'out a reference to another BTF type. */
722 static void
723 btf_asm_type_ref (const char *prefix, ctf_container_ref ctfc, ctf_id_t ref_id)
725 if (ref_id == BTF_VOID_TYPEID || ref_id == BTF_INVALID_TYPEID)
727 /* There is no explicit void type.
728 Also handle any invalid refs that made it this far, just in case. */
729 dw2_asm_output_data (4, ref_id, "%s: void", prefix);
731 else if (ref_id >= num_types_added + 1
732 && ref_id < num_types_added + num_vars_added + 1)
734 /* Ref to a variable. Should only appear in DATASEC entries. */
735 ctf_id_t var_id = btf_relative_var_id (ref_id);
736 ctf_dvdef_ref dvd = ctfc->ctfc_vars_list[var_id];
737 dw2_asm_output_data (4, ref_id, "%s: (BTF_KIND_VAR '%s')",
738 prefix, dvd->dvd_name);
741 else if (ref_id >= num_types_added + num_vars_added + 1)
743 /* Ref to a FUNC record. */
744 size_t func_id = btf_relative_func_id (ref_id);
745 ctf_dtdef_ref ref_type = (*funcs)[func_id];
746 dw2_asm_output_data (4, ref_id, "%s: (BTF_KIND_FUNC '%s')",
747 prefix, get_btf_type_name (ref_type));
749 else
751 /* Ref to a standard type in the types list. */
752 ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[ref_id];
753 uint32_t ref_kind
754 = get_btf_kind (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info));
756 dw2_asm_output_data (4, ref_id, "%s: (BTF_KIND_%s '%s')",
757 prefix, btf_kind_name (ref_kind),
758 get_btf_type_name (ref_type));
762 /* Asm'out a BTF type. This routine is responsible for the bulk of the task
763 of converting CTF types to their BTF representation. */
765 static void
766 btf_asm_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
768 uint32_t btf_kind, btf_kflag, btf_vlen, btf_size_type;
769 uint32_t ctf_info = dtd->dtd_data.ctti_info;
771 btf_kind = get_btf_kind (CTF_V2_INFO_KIND (ctf_info));
772 btf_size_type = dtd->dtd_data.ctti_type;
773 btf_vlen = CTF_V2_INFO_VLEN (ctf_info);
775 /* By now any unrepresentable types have been removed. */
776 gcc_assert (btf_kind != BTF_KIND_UNKN);
778 /* Size 0 integers are redundant definitions of void. None should remain
779 in the types list by this point. */
780 gcc_assert (btf_kind != BTF_KIND_INT || btf_size_type >= 1);
782 /* Re-encode the ctti_info to BTF. */
783 /* kflag is 1 for structs/unions with a bitfield member.
784 kflag is 1 for forwards to unions.
785 kflag is 0 in all other cases. */
786 btf_kflag = 0;
788 if (btf_kind == BTF_KIND_STRUCT || btf_kind == BTF_KIND_UNION)
790 /* If a struct/union has ANY bitfield members, set kflag=1.
791 Note that we must also change the encoding of every member to encode
792 both member bitfield size (stealing most-significant 8 bits) and bit
793 offset (LS 24 bits). This is done during preprocessing. */
794 ctf_dmdef_t *dmd;
795 for (dmd = dtd->dtd_u.dtu_members;
796 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
798 /* Set kflag if this member is a representable bitfield. */
799 if (btf_dmd_representable_bitfield_p (ctfc, dmd))
800 btf_kflag = 1;
802 /* Struct members that refer to unsupported types or bitfield formats
803 shall be skipped. These are marked during preprocessing. */
804 else if (!btf_emit_id_p (dmd->dmd_type))
805 btf_vlen -= 1;
809 /* BTF forwards make use of KIND_FLAG to distinguish between forwards to
810 structs and forwards to unions. The dwarf2ctf conversion process stores
811 the kind of the forward in ctti_type, but for BTF this must be 0 for
812 forwards, with only the KIND_FLAG to distinguish.
813 At time of writing, BTF forwards to enums are unspecified. */
814 if (btf_kind == BTF_KIND_FWD)
816 if (dtd->dtd_data.ctti_type == CTF_K_UNION)
817 btf_kflag = 1;
819 btf_size_type = 0;
822 if (btf_kind == BTF_KIND_ENUM)
824 btf_kflag = dtd->dtd_enum_unsigned
825 ? BTF_KF_ENUM_UNSIGNED
826 : BTF_KF_ENUM_SIGNED;
827 if (dtd->dtd_data.ctti_size == 0x8)
828 btf_kind = BTF_KIND_ENUM64;
831 /* PR debug/112656. BTF_KIND_FUNC_PROTO is always anonymous. */
832 if (btf_kind == BTF_KIND_FUNC_PROTO)
833 dtd->dtd_data.ctti_name = 0;
835 dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
836 "TYPE %" PRIu64 " BTF_KIND_%s '%s'",
837 get_btf_id (dtd->dtd_type), btf_kind_name (btf_kind),
838 get_btf_type_name (dtd));
839 dw2_asm_output_data (4, BTF_TYPE_INFO (btf_kind, btf_kflag, btf_vlen),
840 "btt_info: kind=%u, kflag=%u, vlen=%u",
841 btf_kind, btf_kflag, btf_vlen);
842 switch (btf_kind)
844 case BTF_KIND_INT:
845 case BTF_KIND_FLOAT:
846 case BTF_KIND_STRUCT:
847 case BTF_KIND_UNION:
848 case BTF_KIND_ENUM:
849 case BTF_KIND_DATASEC:
850 case BTF_KIND_ENUM64:
851 dw2_asm_output_data (4, dtd->dtd_data.ctti_size, "btt_size: %uB",
852 dtd->dtd_data.ctti_size);
853 return;
854 case BTF_KIND_ARRAY:
855 case BTF_KIND_FWD:
856 /* These types do not encode any information in the size/type field
857 and should write 0. */
858 dw2_asm_output_data (4, 0, "(unused)");
859 return;
860 default:
861 break;
864 ctf_id_t ref_id = get_btf_id (dtd->dtd_data.ctti_type);
865 btf_asm_type_ref ("btt_type", ctfc, ref_id);
868 /* Asm'out the variable information following a BTF_KIND_ARRAY. */
870 static void
871 btf_asm_array (ctf_container_ref ctfc, ctf_arinfo_t arr)
873 btf_asm_type_ref ("bta_elem_type", ctfc, get_btf_id (arr.ctr_contents));
874 btf_asm_type_ref ("bta_index_type", ctfc, get_btf_id (arr.ctr_index));
875 dw2_asm_output_data (4, arr.ctr_nelems, "bta_nelems");
878 /* Asm'out a BTF_KIND_VAR. */
880 static void
881 btf_asm_varent (ctf_container_ref ctfc, ctf_dvdef_ref var)
883 ctf_id_t ref_id = get_btf_id (var->dvd_type);
884 dw2_asm_output_data (4, var->dvd_name_offset, "TYPE %u BTF_KIND_VAR '%s'",
885 (*(btf_var_ids->get (var)) + num_types_added + 1),
886 var->dvd_name);
887 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_VAR, 0, 0), "btv_info");
888 btf_asm_type_ref ("btv_type", ctfc, ref_id);
889 dw2_asm_output_data (4, var->dvd_visibility, "btv_linkage");
892 /* Asm'out a member description following a BTF_KIND_STRUCT or
893 BTF_KIND_UNION. */
895 static void
896 btf_asm_sou_member (ctf_container_ref ctfc, ctf_dmdef_t * dmd, unsigned int idx)
898 ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[dmd->dmd_type];
900 /* Re-encode bitfields to BTF representation. */
901 if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
903 ctf_id_t base_type = ref_type->dtd_u.dtu_slice.cts_type;
904 unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
905 unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
906 uint64_t sou_offset = dmd->dmd_offset;
908 /* Pack the bit offset and bitfield size together. */
909 sou_offset += word_offset;
911 /* If this bitfield cannot be represented, do not output anything.
912 The parent struct/union 'vlen' field has already been updated. */
913 if ((bits > 0xff) || (sou_offset > 0xffffff))
914 return;
916 sou_offset &= 0x00ffffff;
917 sou_offset |= ((bits & 0xff) << 24);
919 dw2_asm_output_data (4, dmd->dmd_name_offset,
920 "MEMBER '%s' idx=%u",
921 dmd->dmd_name, idx);
922 /* Refer to the base type of the slice. */
923 btf_asm_type_ref ("btm_type", ctfc, get_btf_id (base_type));
924 dw2_asm_output_data (4, sou_offset, "btm_offset");
926 else
928 dw2_asm_output_data (4, dmd->dmd_name_offset,
929 "MEMBER '%s' idx=%u",
930 dmd->dmd_name, idx);
931 btf_asm_type_ref ("btm_type", ctfc, get_btf_id (dmd->dmd_type));
932 dw2_asm_output_data (4, dmd->dmd_offset, "btm_offset");
936 /* Asm'out an enum constant following a BTF_KIND_ENUM{,64}. */
938 static void
939 btf_asm_enum_const (unsigned int size, ctf_dmdef_t * dmd, unsigned int idx)
941 dw2_asm_output_data (4, dmd->dmd_name_offset, "ENUM_CONST '%s' idx=%u",
942 dmd->dmd_name, idx);
943 if (size <= 4)
944 dw2_asm_output_data (size < 4 ? 4 : size, dmd->dmd_value, "bte_value");
945 else
947 dw2_asm_output_data (4, dmd->dmd_value & 0xffffffff, "bte_value_lo32");
948 dw2_asm_output_data (4, (dmd->dmd_value >> 32) & 0xffffffff, "bte_value_hi32");
952 /* Asm'out a function parameter description following a BTF_KIND_FUNC_PROTO. */
954 static void
955 btf_asm_func_arg (ctf_container_ref ctfc, ctf_func_arg_t * farg,
956 size_t stroffset)
958 /* If the function arg does not have a name, refer to the null string at
959 the start of the string table. This ensures correct encoding for varargs
960 '...' arguments. */
961 if ((farg->farg_name != NULL) && strcmp (farg->farg_name, ""))
962 dw2_asm_output_data (4, farg->farg_name_offset + stroffset, "farg_name");
963 else
964 dw2_asm_output_data (4, 0, "farg_name");
966 btf_asm_type_ref ("farg_type", ctfc, (btf_removed_type_p (farg->farg_type)
967 ? BTF_VOID_TYPEID
968 : get_btf_id (farg->farg_type)));
971 /* Asm'out a BTF_KIND_FUNC type. */
973 static void
974 btf_asm_func_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd, ctf_id_t id)
976 ctf_id_t ref_id = dtd->dtd_data.ctti_type;
977 dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
978 "TYPE %" PRIu64 " BTF_KIND_FUNC '%s'",
979 btf_absolute_func_id (id), get_btf_type_name (dtd));
980 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC, 0, dtd->linkage),
981 "btt_info: kind=%u, kflag=%u, linkage=%u",
982 BTF_KIND_FUNC, 0, dtd->linkage);
983 btf_asm_type_ref ("btt_type", ctfc, get_btf_id (ref_id));
986 /* Asm'out a variable entry following a BTF_KIND_DATASEC. */
988 static void
989 btf_asm_datasec_entry (ctf_container_ref ctfc, struct btf_var_secinfo info)
991 btf_asm_type_ref ("bts_type", ctfc, info.type);
992 dw2_asm_output_data (4, info.offset, "bts_offset");
993 dw2_asm_output_data (4, info.size, "bts_size");
996 /* Asm'out a whole BTF_KIND_DATASEC, including its variable entries. */
998 static void
999 btf_asm_datasec_type (ctf_container_ref ctfc, btf_datasec_t ds, ctf_id_t id,
1000 size_t stroffset)
1002 dw2_asm_output_data (4, ds.name_offset + stroffset,
1003 "TYPE %" PRIu64 " BTF_KIND_DATASEC '%s'",
1004 btf_absolute_datasec_id (id), ds.name);
1005 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_DATASEC, 0,
1006 ds.entries.length ()),
1007 "btt_info: n_entries=%u", ds.entries.length ());
1008 /* Note: the "total section size in bytes" is emitted as 0 and patched by
1009 loaders such as libbpf. */
1010 dw2_asm_output_data (4, 0, "btt_size");
1011 for (size_t i = 0; i < ds.entries.length (); i++)
1012 btf_asm_datasec_entry (ctfc, ds.entries[i]);
1015 /* Compute and output the header information for a .BTF section. */
1017 static void
1018 output_btf_header (ctf_container_ref ctfc)
1020 switch_to_section (btf_info_section);
1021 ASM_OUTPUT_LABEL (asm_out_file, btf_info_section_label);
1023 /* BTF magic number, version, flags, and header length. */
1024 dw2_asm_output_data (2, BTF_MAGIC, "btf_magic");
1025 dw2_asm_output_data (1, BTF_VERSION, "btf_version");
1026 dw2_asm_output_data (1, 0, "btf_flags");
1027 dw2_asm_output_data (4, sizeof (struct btf_header), "btf_hdr_len");
1029 uint32_t type_off = 0, type_len = 0;
1030 uint32_t str_off = 0, str_len = 0;
1031 uint32_t datasec_vlen_bytes = 0;
1033 if (!ctfc_is_empty_container (ctfc))
1035 for (size_t i = 0; i < datasecs.length (); i++)
1037 datasec_vlen_bytes += ((datasecs[i].entries.length ())
1038 * sizeof (struct btf_var_secinfo));
1041 /* Total length (bytes) of the types section. */
1042 type_len = (num_types_added * sizeof (struct btf_type))
1043 + (num_types_created * sizeof (struct btf_type))
1044 + datasec_vlen_bytes
1045 + ctfc->ctfc_num_vlen_bytes;
1047 str_off = type_off + type_len;
1049 str_len = ctfc->ctfc_strtable.ctstab_len
1050 + ctfc->ctfc_aux_strtable.ctstab_len;
1053 /* Offset of type section. */
1054 dw2_asm_output_data (4, type_off, "type_off");
1055 /* Length of type section in bytes. */
1056 dw2_asm_output_data (4, type_len, "type_len");
1057 /* Offset of string section. */
1058 dw2_asm_output_data (4, str_off, "str_off");
1059 /* Length of string section in bytes. */
1060 dw2_asm_output_data (4, str_len, "str_len");
1063 /* Output all BTF_KIND_VARs in CTFC. */
1065 static void
1066 output_btf_vars (ctf_container_ref ctfc)
1068 size_t i;
1069 size_t num_ctf_vars = num_vars_added;
1070 if (num_ctf_vars)
1072 for (i = 0; i < num_ctf_vars; i++)
1073 btf_asm_varent (ctfc, ctfc->ctfc_vars_list[i]);
1077 /* Output BTF string records. The BTF strings section is a concatenation
1078 of the standard and auxilliary string tables in the ctf container. */
1080 static void
1081 output_btf_strs (ctf_container_ref ctfc)
1083 ctf_string_t * ctf_string = ctfc->ctfc_strtable.ctstab_head;
1085 while (ctf_string)
1087 dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_string");
1088 ctf_string = ctf_string->cts_next;
1091 ctf_string = ctfc->ctfc_aux_strtable.ctstab_head;
1092 while (ctf_string)
1094 dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_aux_string");
1095 ctf_string = ctf_string->cts_next;
1099 /* Output all (representable) members of a BTF_KIND_STRUCT or
1100 BTF_KIND_UNION type. */
1102 static void
1103 output_asm_btf_sou_fields (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
1105 ctf_dmdef_t * dmd;
1107 unsigned idx = 0;
1108 for (dmd = dtd->dtd_u.dtu_members;
1109 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
1111 btf_asm_sou_member (ctfc, dmd, idx);
1112 idx++;
1116 /* Output all enumerator constants following a BTF_KIND_ENUM{,64}. */
1118 static void
1119 output_asm_btf_enum_list (ctf_container_ref ARG_UNUSED (ctfc),
1120 ctf_dtdef_ref dtd)
1122 ctf_dmdef_t * dmd;
1124 unsigned idx = 0;
1125 for (dmd = dtd->dtd_u.dtu_members;
1126 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
1128 btf_asm_enum_const (dtd->dtd_data.ctti_size, dmd, idx);
1129 idx++;
1133 /* Output all function arguments following a BTF_KIND_FUNC_PROTO. */
1135 static void
1136 output_asm_btf_func_args_list (ctf_container_ref ctfc,
1137 ctf_dtdef_ref dtd)
1139 size_t farg_name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
1140 ctf_func_arg_t * farg;
1141 for (farg = dtd->dtd_u.dtu_argv;
1142 farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
1143 btf_asm_func_arg (ctfc, farg, farg_name_offset);
1146 /* Output the variable portion of a BTF type record. The information depends
1147 on the kind of the type. */
1149 static void
1150 output_asm_btf_vlen_bytes (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
1152 uint32_t btf_kind, encoding;
1154 btf_kind = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
1156 if (btf_kind == BTF_KIND_UNKN)
1157 return;
1159 switch (btf_kind)
1161 case BTF_KIND_INT:
1162 /* Redundant definitions of void may still be hanging around in the type
1163 list as size 0 integers. Skip emitting them. */
1164 if (dtd->dtd_data.ctti_size < 1)
1165 break;
1167 /* In BTF the CHAR `encoding' seems to not be used, so clear it
1168 here. */
1169 dtd->dtd_u.dtu_enc.cte_format &= ~BTF_INT_CHAR;
1171 encoding = BTF_INT_DATA (dtd->dtd_u.dtu_enc.cte_format,
1172 dtd->dtd_u.dtu_enc.cte_offset,
1173 dtd->dtd_u.dtu_enc.cte_bits);
1175 dw2_asm_output_data (4, encoding, "bti_encoding");
1176 break;
1178 case BTF_KIND_ARRAY:
1179 btf_asm_array (ctfc, dtd->dtd_u.dtu_arr);
1180 break;
1182 case BTF_KIND_STRUCT:
1183 case BTF_KIND_UNION:
1184 output_asm_btf_sou_fields (ctfc, dtd);
1185 break;
1187 case BTF_KIND_ENUM:
1188 output_asm_btf_enum_list (ctfc, dtd);
1189 break;
1191 case BTF_KIND_FUNC_PROTO:
1192 output_asm_btf_func_args_list (ctfc, dtd);
1193 break;
1195 case BTF_KIND_VAR:
1196 /* BTF Variables are handled by output_btf_vars and btf_asm_varent.
1197 There should be no BTF_KIND_VAR types at this point. */
1198 gcc_unreachable ();
1200 case BTF_KIND_DATASEC:
1201 /* The BTF_KIND_DATASEC records are handled by output_btf_datasec_types
1202 and btf_asm_datasec_type. There should be no BTF_KIND_DATASEC types
1203 at this point. */
1204 gcc_unreachable ();
1206 default:
1207 /* All other BTF type kinds have no variable length data. */
1208 break;
1212 /* Output a whole BTF type record for TYPE, including the fixed and variable
1213 data portions. */
1215 static void
1216 output_asm_btf_type (ctf_container_ref ctfc, ctf_dtdef_ref type)
1218 if (btf_emit_id_p (type->dtd_type))
1220 btf_asm_type (ctfc, type);
1221 output_asm_btf_vlen_bytes (ctfc, type);
1225 /* Output all BTF types in the container. This does not include synthesized
1226 types: BTF_KIND_VAR, BTF_KIND_FUNC, nor BTF_KIND_DATASEC. */
1228 static void
1229 output_btf_types (ctf_container_ref ctfc)
1231 size_t i;
1232 size_t num_types = ctfc->ctfc_types->elements ();
1233 if (num_types)
1235 for (i = 1; i <= num_types; i++)
1236 output_asm_btf_type (ctfc, ctfc->ctfc_types_list[i]);
1240 /* Output all BTF_KIND_FUNC type records. */
1242 static void
1243 output_btf_func_types (ctf_container_ref ctfc)
1245 for (size_t i = 0; i < vec_safe_length (funcs); i++)
1246 btf_asm_func_type (ctfc, (*funcs)[i], i);
1249 /* Output all BTF_KIND_DATASEC records. */
1251 static void
1252 output_btf_datasec_types (ctf_container_ref ctfc)
1254 size_t name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
1256 for (size_t i = 0; i < datasecs.length(); i++)
1257 btf_asm_datasec_type (ctfc, datasecs[i], i, name_offset);
1260 /* Postprocess the CTF debug data post initialization.
1262 During the postprocess pass:
1264 - Prepare the sorted list of BTF types.
1266 The sorted list of BTF types is, firstly, used for lookup (during the BTF
1267 generation process) of CTF/BTF types given a typeID.
1269 Secondly, in the emitted BTF section, BTF Types need to be in the sorted
1270 order of their type IDs. The BTF types section is viewed as an array,
1271 with type IDs used to index into that array. It is essential that every
1272 type be placed at the exact index corresponding to its ID, or else
1273 references to that type from other types will no longer be correct.
1275 - References to void types are converted to reference BTF_VOID_TYPEID. In
1276 CTF, a distinct type is used to encode void.
1278 - Bitfield struct/union members are converted to BTF encoding. CTF uses
1279 slices to encode bitfields, but BTF does not have slices and encodes
1280 bitfield information directly in the variable-length btf_member
1281 descriptions following the struct or union type.
1283 - Unrepresentable types are removed. We cannot have any invalid BTF types
1284 appearing in the output so they must be removed, and type ids of other
1285 types and references adjust accordingly. This also involves ensuring that
1286 BTF descriptions of struct members referring to unrepresentable types are
1287 not emitted, as they would be nonsensical.
1289 - Adjust inner- and inter-type references-by-ID to account for removed
1290 types, and construct the types list. */
1292 void
1293 btf_init_postprocess (void)
1295 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1297 holes.create (0);
1298 voids.create (0);
1300 num_types_added = 0;
1301 num_types_created = 0;
1303 /* Workaround for 'const void' variables. These variables are sometimes used
1304 in eBPF programs to address kernel symbols. DWARF does not generate const
1305 qualifier on void type, so we would incorrectly emit these variables
1306 without the const qualifier.
1307 Unfortunately we need the TREE node to know it was const, and we need
1308 to create the const modifier type (if needed) now, before making the types
1309 list. So we can't avoid iterating with FOR_EACH_VARIABLE here, and then
1310 again when creating the DATASEC entries. */
1311 ctf_id_t constvoid_id = CTF_NULL_TYPEID;
1312 varpool_node *var;
1313 FOR_EACH_VARIABLE (var)
1315 if (!var->decl)
1316 continue;
1318 tree type = TREE_TYPE (var->decl);
1319 if (type && VOID_TYPE_P (type) && TYPE_READONLY (type))
1321 dw_die_ref die = lookup_decl_die (var->decl);
1322 if (die == NULL)
1323 continue;
1325 ctf_dvdef_ref dvd = ctf_dvd_lookup (tu_ctfc, die);
1326 if (dvd == NULL)
1327 continue;
1329 /* Create the 'const' modifier type for void. */
1330 if (constvoid_id == CTF_NULL_TYPEID)
1331 constvoid_id = ctf_add_reftype (tu_ctfc, CTF_ADD_ROOT,
1332 dvd->dvd_type, CTF_K_CONST, NULL);
1333 dvd->dvd_type = constvoid_id;
1337 size_t i;
1338 size_t num_ctf_types = tu_ctfc->ctfc_types->elements ();
1340 if (num_ctf_types)
1342 init_btf_id_map (num_ctf_types + 1);
1344 /* Allocate the types list and traverse all types, placing each type
1345 at the index according to its ID. Add 1 because type ID 0 always
1346 represents VOID. */
1347 tu_ctfc->ctfc_types_list
1348 = ggc_vec_alloc<ctf_dtdef_ref>(num_ctf_types + 1);
1349 tu_ctfc->ctfc_types->traverse<ctf_container_ref, btf_dtd_postprocess_cb>
1350 (tu_ctfc);
1352 /* Build mapping of CTF type ID -> BTF type ID, and count total number
1353 of valid BTF types added. */
1354 for (i = 1; i <= num_ctf_types; i++)
1356 ctf_dtdef_ref dtd = tu_ctfc->ctfc_types_list[i];
1357 ctf_id_t btfid = btf_adjust_type_id (dtd->dtd_type);
1358 set_btf_id (dtd->dtd_type, btfid);
1359 if (btfid < BTF_MAX_TYPE && (btfid != BTF_VOID_TYPEID))
1360 num_types_added ++;
1365 /* Process and output all BTF data. Entry point of btfout. */
1367 void
1368 btf_output (const char * filename)
1370 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1372 init_btf_sections ();
1374 datasecs.create (0);
1375 vec_alloc (funcs, 16);
1377 ctf_add_cuname (tu_ctfc, filename);
1379 btf_emit_preprocess (tu_ctfc);
1381 output_btf_header (tu_ctfc);
1382 output_btf_types (tu_ctfc);
1383 output_btf_vars (tu_ctfc);
1384 output_btf_func_types (tu_ctfc);
1385 output_btf_datasec_types (tu_ctfc);
1386 output_btf_strs (tu_ctfc);
1389 /* Reset all state for BTF generation so that we can rerun the compiler within
1390 the same process. */
1392 void
1393 btf_finalize (void)
1395 btf_info_section = NULL;
1397 /* Clear preprocessing state. */
1398 num_vars_added = 0;
1399 num_types_added = 0;
1400 num_types_created = 0;
1402 holes.release ();
1403 voids.release ();
1404 for (size_t i = 0; i < datasecs.length (); i++)
1405 datasecs[i].entries.release ();
1406 datasecs.release ();
1408 funcs = NULL;
1410 btf_var_ids->empty ();
1411 btf_var_ids = NULL;
1413 free (btf_id_map);
1414 btf_id_map = NULL;
1416 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1417 ctfc_delete_container (tu_ctfc);
1418 tu_ctfc = NULL;
1421 #include "gt-btfout.h"