hppa: Fix pr110279-1.c on hppa
[official-gcc.git] / gcc / btfout.cc
blob3ec938874b625559847b27c53caa90f7e4cac559
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 /* Return true if DTD is a forward-declared enum. The BTF representation
272 of forward declared enums is not formally defined. */
274 static bool
275 btf_fwd_to_enum_p (ctf_dtdef_ref dtd)
277 uint32_t btf_kind = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
279 return (btf_kind == BTF_KIND_FWD && dtd->dtd_data.ctti_type == CTF_K_ENUM);
282 /* Each BTF type can be followed additional, variable-length information
283 completing the description of the type. Calculate the number of bytes
284 of variable information required to encode a given type. */
286 static uint64_t
287 btf_calc_num_vbytes (ctf_dtdef_ref dtd)
289 uint64_t vlen_bytes = 0;
291 uint32_t kind = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
292 uint32_t vlen = CTF_V2_INFO_VLEN (dtd->dtd_data.ctti_info);
294 switch (kind)
296 case BTF_KIND_UNKN:
297 case BTF_KIND_PTR:
298 case BTF_KIND_FWD:
299 case BTF_KIND_TYPEDEF:
300 case BTF_KIND_VOLATILE:
301 case BTF_KIND_CONST:
302 case BTF_KIND_RESTRICT:
303 case BTF_KIND_FUNC:
304 /* These kinds have no vlen data. */
305 break;
307 case BTF_KIND_INT:
308 /* Size 0 integers represent redundant definitions of void that will
309 not be emitted. Don't allocate space for them. */
310 if (dtd->dtd_data.ctti_size == 0)
311 break;
313 vlen_bytes += sizeof (uint32_t);
314 break;
316 case BTF_KIND_ARRAY:
317 vlen_bytes += sizeof (struct btf_array);
318 break;
320 case BTF_KIND_STRUCT:
321 case BTF_KIND_UNION:
322 vlen_bytes += vlen * sizeof (struct btf_member);
323 break;
325 case BTF_KIND_ENUM:
326 vlen_bytes += (dtd->dtd_data.ctti_size > 4)
327 ? vlen * sizeof (struct btf_enum64)
328 : vlen * sizeof (struct btf_enum);
329 break;
331 case BTF_KIND_FUNC_PROTO:
332 vlen_bytes += vlen * sizeof (struct btf_param);
333 break;
335 case BTF_KIND_VAR:
336 vlen_bytes += sizeof (struct btf_var);
337 break;
339 case BTF_KIND_DATASEC:
340 vlen_bytes += vlen * sizeof (struct btf_var_secinfo);
341 break;
343 default:
344 break;
346 return vlen_bytes;
349 /* Initialize BTF section (.BTF) for output. */
351 void
352 init_btf_sections (void)
354 btf_info_section = get_section (BTF_INFO_SECTION_NAME, BTF_INFO_SECTION_FLAGS,
355 NULL);
357 ASM_GENERATE_INTERNAL_LABEL (btf_info_section_label,
358 BTF_INFO_SECTION_LABEL, btf_label_num++);
361 /* Push a BTF datasec variable entry INFO into the datasec named SECNAME,
362 creating the datasec if it does not already exist. */
364 static void
365 btf_datasec_push_entry (ctf_container_ref ctfc, const char *secname,
366 struct btf_var_secinfo info)
368 if (secname == NULL)
369 return;
371 for (size_t i = 0; i < datasecs.length (); i++)
372 if (strcmp (datasecs[i].name, secname) == 0)
374 datasecs[i].entries.safe_push (info);
375 return;
378 /* If we don't already have a datasec record for secname, make one. */
380 uint32_t str_off;
381 ctf_add_string (ctfc, secname, &str_off, CTF_AUX_STRTAB);
382 if (strcmp (secname, ""))
383 ctfc->ctfc_aux_strlen += strlen (secname) + 1;
385 btf_datasec_t ds;
386 ds.name = secname;
387 ds.name_offset = str_off;
389 ds.entries.create (0);
390 ds.entries.safe_push (info);
392 datasecs.safe_push (ds);
396 /* Return the section name, as of interest to btf_collect_datasec, for the
397 given symtab node. Note that this deliberately returns NULL for objects
398 which do not go in a section btf_collect_datasec cares about. */
399 static const char *
400 get_section_name (symtab_node *node)
402 const char *section_name = node->get_section ();
404 if (section_name == NULL)
406 switch (categorize_decl_for_section (node->decl, 0))
408 case SECCAT_BSS:
409 section_name = ".bss";
410 break;
411 case SECCAT_DATA:
412 section_name = ".data";
413 break;
414 case SECCAT_RODATA:
415 section_name = ".rodata";
416 break;
417 default:;
421 return section_name;
424 /* Construct all BTF_KIND_DATASEC records for CTFC. One such record is created
425 for each non-empty data-containing section in the output. Each record is
426 followed by a variable number of entries describing the variables stored
427 in that section. */
429 static void
430 btf_collect_datasec (ctf_container_ref ctfc)
432 cgraph_node *func;
433 FOR_EACH_FUNCTION (func)
435 dw_die_ref die = lookup_decl_die (func->decl);
436 if (die == NULL)
437 continue;
439 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
440 if (dtd == NULL)
441 continue;
443 /* Functions actually get two types: a BTF_KIND_FUNC_PROTO, and
444 also a BTF_KIND_FUNC. But the CTF container only allocates one
445 type per function, which matches closely with BTF_KIND_FUNC_PROTO.
446 For each such function, also allocate a BTF_KIND_FUNC entry.
447 These will be output later. */
448 ctf_dtdef_ref func_dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
449 func_dtd->dtd_data = dtd->dtd_data;
450 func_dtd->dtd_data.ctti_type = dtd->dtd_type;
451 func_dtd->linkage = dtd->linkage;
452 func_dtd->dtd_name = dtd->dtd_name;
453 func_dtd->dtd_type = num_types_added + num_types_created;
455 /* Only the BTF_KIND_FUNC type actually references the name. The
456 BTF_KIND_FUNC_PROTO is always anonymous. */
457 dtd->dtd_data.ctti_name = 0;
459 vec_safe_push (funcs, func_dtd);
460 num_types_created++;
462 /* Mark any 'extern' funcs and add DATASEC entries for them. */
463 if (DECL_EXTERNAL (func->decl))
465 func_dtd->linkage = BTF_FUNC_EXTERN;
467 const char *section_name = get_section_name (func);
468 /* Note: get_section_name () returns NULL for functions in text
469 section. This is intentional, since we do not want to generate
470 DATASEC entries for them. */
471 if (section_name == NULL)
472 continue;
474 struct btf_var_secinfo info;
476 /* +1 for the sentinel type not in the types map. */
477 info.type = func_dtd->dtd_type + 1;
479 /* Both zero at compile time. */
480 info.size = 0;
481 info.offset = 0;
483 btf_datasec_push_entry (ctfc, section_name, info);
487 varpool_node *node;
488 FOR_EACH_VARIABLE (node)
490 dw_die_ref die = lookup_decl_die (node->decl);
491 if (die == NULL)
492 continue;
494 ctf_dvdef_ref dvd = ctf_dvd_lookup (ctfc, die);
495 if (dvd == NULL)
496 continue;
498 /* Mark extern variables. */
499 if (DECL_EXTERNAL (node->decl))
501 dvd->dvd_visibility = BTF_VAR_GLOBAL_EXTERN;
503 /* PR112849: avoid assuming a section for extern decls without
504 an explicit section, which would result in incorrectly
505 emitting a BTF_KIND_DATASEC entry for them. */
506 if (node->get_section () == NULL)
507 continue;
510 const char *section_name = get_section_name (node);
511 if (section_name == NULL)
512 continue;
514 struct btf_var_secinfo info;
516 info.type = 0;
517 unsigned int *var_id = btf_var_ids->get (dvd);
518 if (var_id)
519 info.type = btf_absolute_var_id (*var_id);
520 else
521 continue;
523 info.size = 0;
524 tree size = DECL_SIZE_UNIT (node->decl);
525 if (tree_fits_uhwi_p (size))
526 info.size = tree_to_uhwi (size);
527 else if (VOID_TYPE_P (TREE_TYPE (node->decl)))
528 info.size = 1;
530 /* Offset is left as 0 at compile time, to be filled in by loaders such
531 as libbpf. */
532 info.offset = 0;
534 btf_datasec_push_entry (ctfc, section_name, info);
537 num_types_created += datasecs.length ();
540 /* Return true if the type ID is that of a type which will not be emitted (for
541 example, if it is not representable in BTF). */
543 static bool
544 btf_removed_type_p (ctf_id_t id)
546 return holes.contains (id);
549 /* Adjust the given type ID to account for holes and duplicate definitions of
550 void. */
552 static ctf_id_t
553 btf_adjust_type_id (ctf_id_t id)
555 size_t n;
556 ctf_id_t i = 0;
558 /* Do not adjust invalid type markers. */
559 if (id == BTF_INVALID_TYPEID)
560 return id;
562 for (n = 0; n < voids.length (); n++)
563 if (id == voids[n])
564 return BTF_VOID_TYPEID;
566 for (n = 0; n < holes.length (); n++)
568 if (holes[n] < id)
569 i++;
570 else if (holes[n] == id)
571 return BTF_VOID_TYPEID;
574 return id - i;
577 /* Postprocessing callback routine for types. */
580 btf_dtd_postprocess_cb (ctf_dtdef_ref *slot, ctf_container_ref arg_ctfc)
582 ctf_dtdef_ref ctftype = (ctf_dtdef_ref) * slot;
584 size_t index = ctftype->dtd_type;
585 gcc_assert (index <= arg_ctfc->ctfc_types->elements ());
587 uint32_t ctf_kind, btf_kind;
589 ctf_kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
590 btf_kind = get_btf_kind (ctf_kind);
592 if (btf_kind == BTF_KIND_UNKN)
593 /* This type is not representable in BTF. Create a hole. */
594 holes.safe_push (ctftype->dtd_type);
596 else if (btf_kind == BTF_KIND_INT && ctftype->dtd_data.ctti_size == 0)
598 /* This is a (redundant) definition of void. */
599 voids.safe_push (ctftype->dtd_type);
600 holes.safe_push (ctftype->dtd_type);
603 arg_ctfc->ctfc_types_list[index] = ctftype;
605 return 1;
608 /* Preprocessing callback routine for variables. */
611 btf_dvd_emit_preprocess_cb (ctf_dvdef_ref *slot, ctf_container_ref arg_ctfc)
613 ctf_dvdef_ref var = (ctf_dvdef_ref) * slot;
615 /* If this is an extern variable declaration with a defining declaration
616 later, skip it so that only the defining declaration is emitted.
617 This is the same case, fix and reasoning as in CTF; see PR105089. */
618 if (ctf_dvd_ignore_lookup (arg_ctfc, var->dvd_key))
619 return 1;
621 /* Do not add variables which refer to unsupported types. */
622 if (!voids.contains (var->dvd_type) && btf_removed_type_p (var->dvd_type))
623 return 1;
625 arg_ctfc->ctfc_vars_list[num_vars_added] = var;
626 btf_var_ids->put (var, num_vars_added);
628 num_vars_added++;
629 num_types_created++;
631 return 1;
634 /* Preprocessing callback routine for types. */
636 static void
637 btf_dtd_emit_preprocess_cb (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
639 if (!btf_emit_id_p (dtd->dtd_type))
640 return;
642 ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (dtd);
645 /* Preprocess the CTF information to prepare for BTF output. BTF is almost a
646 subset of CTF, with many small differences in encoding, and lacking support
647 for some types (notably floating point formats).
649 During the preprocessing pass:
650 - Ascertain that the sorted list of types has been prepared. For the BTF
651 generation process, this is taken care of by the btf_init_postprocess ().
653 - BTF_KIND_FUNC and BTF_KIND_DATASEC records are constructed. These types do
654 not have analogues in CTF (the analogous type to CTF_K_FUNCTION is
655 BTF_KIND_FUNC_PROTO), but can be relatively easily deduced from CTF
656 information.
658 - Construct BTF_KIND_VAR records, representing variables.
660 - Calculate the total size in bytes of variable-length information following
661 BTF type records. This is used for outputting the BTF header.
663 After preprocessing, all BTF information is ready to be output:
664 - ctfc->ctfc_types_list holdstypes converted from CTF types. This does not
665 include KIND_VAR, KIND_FUNC, nor KIND_DATASEC types. These types have been
666 re-encoded to the appropriate representation in BTF.
667 - ctfc->ctfc_vars_list holds all variables which should be output.
668 Variables of unsupported types are not present in this list.
669 - Vector 'funcs' holds all BTF_KIND_FUNC types, one to match each
670 BTF_KIND_FUNC_PROTO.
671 - Vector 'datasecs' holds all BTF_KIND_DATASEC types. */
673 static void
674 btf_emit_preprocess (ctf_container_ref ctfc)
676 size_t num_ctf_types = ctfc->ctfc_types->elements ();
677 size_t num_ctf_vars = ctfc->ctfc_vars->elements ();
678 size_t i;
680 if (num_ctf_types)
682 gcc_assert (ctfc->ctfc_types_list);
683 /* Preprocess the types. */
684 for (i = 1; i <= num_ctf_types; i++)
685 btf_dtd_emit_preprocess_cb (ctfc, ctfc->ctfc_types_list[i]);
688 btf_var_ids = hash_map<ctf_dvdef_ref, unsigned int>::create_ggc (100);
690 if (num_ctf_vars)
692 /* Allocate and construct the list of variables. While BTF variables are
693 not distinct from types (in that variables are simply types with
694 BTF_KIND_VAR), it is simpler to maintain a separate list of variables
695 and append them to the types list during output. */
696 ctfc->ctfc_vars_list = ggc_vec_alloc<ctf_dvdef_ref>(num_ctf_vars);
697 ctfc->ctfc_vars->traverse<ctf_container_ref, btf_dvd_emit_preprocess_cb>
698 (ctfc);
700 ctfc->ctfc_num_vlen_bytes += (num_vars_added * sizeof (struct btf_var));
703 btf_collect_datasec (ctfc);
706 /* Return true iff DMD is a member description of a bit-field which can be
707 validly represented in BTF. */
709 static bool
710 btf_dmd_representable_bitfield_p (ctf_container_ref ctfc, ctf_dmdef_t *dmd)
712 ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[dmd->dmd_type];
714 if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
716 unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
717 unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
718 uint64_t sou_offset = dmd->dmd_offset;
720 if ((bits > 0xff) || ((sou_offset + word_offset) > 0xffffff))
721 return false;
723 return true;
726 return false;
729 /* BTF asm helper routines. */
731 /* Asm'out a reference to another BTF type. */
733 static void
734 btf_asm_type_ref (const char *prefix, ctf_container_ref ctfc, ctf_id_t ref_id)
736 if (ref_id == BTF_VOID_TYPEID || ref_id == BTF_INVALID_TYPEID)
738 /* There is no explicit void type.
739 Also handle any invalid refs that made it this far, just in case. */
740 dw2_asm_output_data (4, ref_id, "%s: void", prefix);
742 else if (ref_id >= num_types_added + 1
743 && ref_id < num_types_added + num_vars_added + 1)
745 /* Ref to a variable. Should only appear in DATASEC entries. */
746 ctf_id_t var_id = btf_relative_var_id (ref_id);
747 ctf_dvdef_ref dvd = ctfc->ctfc_vars_list[var_id];
748 dw2_asm_output_data (4, ref_id, "%s: (BTF_KIND_VAR '%s')",
749 prefix, dvd->dvd_name);
752 else if (ref_id >= num_types_added + num_vars_added + 1)
754 /* Ref to a FUNC record. */
755 size_t func_id = btf_relative_func_id (ref_id);
756 ctf_dtdef_ref ref_type = (*funcs)[func_id];
757 dw2_asm_output_data (4, ref_id, "%s: (BTF_KIND_FUNC '%s')",
758 prefix, get_btf_type_name (ref_type));
760 else
762 /* Ref to a standard type in the types list. */
763 ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[ref_id];
764 uint32_t ref_kind
765 = get_btf_kind (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info));
767 const char *kind_name = btf_fwd_to_enum_p (ref_type)
768 ? btf_kind_name (BTF_KIND_ENUM)
769 : btf_kind_name (ref_kind);
771 dw2_asm_output_data (4, ref_id, "%s: (BTF_KIND_%s '%s')",
772 prefix, kind_name,
773 get_btf_type_name (ref_type));
777 /* Asm'out a BTF type. This routine is responsible for the bulk of the task
778 of converting CTF types to their BTF representation. */
780 static void
781 btf_asm_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
783 uint32_t btf_kind, btf_kflag, btf_vlen, btf_size;
784 uint32_t ctf_info = dtd->dtd_data.ctti_info;
786 btf_kind = get_btf_kind (CTF_V2_INFO_KIND (ctf_info));
787 btf_size = dtd->dtd_data.ctti_size;
788 btf_vlen = CTF_V2_INFO_VLEN (ctf_info);
790 /* By now any unrepresentable types have been removed. */
791 gcc_assert (btf_kind != BTF_KIND_UNKN);
793 /* Size 0 integers are redundant definitions of void. None should remain
794 in the types list by this point. */
795 gcc_assert (btf_kind != BTF_KIND_INT || btf_size >= 1);
797 /* Re-encode the ctti_info to BTF. */
798 /* kflag is 1 for structs/unions with a bitfield member.
799 kflag is 1 for forwards to unions.
800 kflag is 0 in all other cases. */
801 btf_kflag = 0;
803 if (btf_kind == BTF_KIND_STRUCT || btf_kind == BTF_KIND_UNION)
805 /* If a struct/union has ANY bitfield members, set kflag=1.
806 Note that we must also change the encoding of every member to encode
807 both member bitfield size (stealing most-significant 8 bits) and bit
808 offset (LS 24 bits). This is done during preprocessing. */
809 ctf_dmdef_t *dmd;
810 for (dmd = dtd->dtd_u.dtu_members;
811 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
813 /* Set kflag if this member is a representable bitfield. */
814 if (btf_dmd_representable_bitfield_p (ctfc, dmd))
815 btf_kflag = 1;
817 /* Struct members that refer to unsupported types or bitfield formats
818 shall be skipped. These are marked during preprocessing. */
819 else if (!btf_emit_id_p (dmd->dmd_type))
820 btf_vlen -= 1;
824 /* BTF forwards make use of KIND_FLAG to distinguish between forwards to
825 structs and forwards to unions. The dwarf2ctf conversion process stores
826 the kind of the forward in ctti_type, but for BTF this must be 0 for
827 forwards, with only the KIND_FLAG to distinguish.
828 Forwards to enum types are special-cased below. */
829 else if (btf_kind == BTF_KIND_FWD)
831 if (dtd->dtd_data.ctti_type == CTF_K_UNION)
832 btf_kflag = 1;
834 /* PR debug/111735. Encode foward-declared enums as BTF_KIND_ENUM
835 with vlen=0. A representation for these is not formally defined;
836 this is the de-facto standard used by other tools like clang
837 and pahole. */
838 else if (dtd->dtd_data.ctti_type == CTF_K_ENUM)
840 btf_kind = BTF_KIND_ENUM;
841 btf_vlen = 0;
844 btf_size = 0;
847 else if (btf_kind == BTF_KIND_ENUM)
849 btf_kflag = dtd->dtd_enum_unsigned
850 ? BTF_KF_ENUM_UNSIGNED
851 : BTF_KF_ENUM_SIGNED;
852 if (dtd->dtd_data.ctti_size == 0x8)
853 btf_kind = BTF_KIND_ENUM64;
856 /* PR debug/112656. BTF_KIND_FUNC_PROTO is always anonymous. */
857 else if (btf_kind == BTF_KIND_FUNC_PROTO)
858 dtd->dtd_data.ctti_name = 0;
860 dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
861 "TYPE %" PRIu64 " BTF_KIND_%s '%s'",
862 get_btf_id (dtd->dtd_type), btf_kind_name (btf_kind),
863 get_btf_type_name (dtd));
864 dw2_asm_output_data (4, BTF_TYPE_INFO (btf_kind, btf_kflag, btf_vlen),
865 "btt_info: kind=%u, kflag=%u, vlen=%u",
866 btf_kind, btf_kflag, btf_vlen);
867 switch (btf_kind)
869 case BTF_KIND_INT:
870 case BTF_KIND_FLOAT:
871 case BTF_KIND_STRUCT:
872 case BTF_KIND_UNION:
873 case BTF_KIND_ENUM:
874 case BTF_KIND_DATASEC:
875 case BTF_KIND_ENUM64:
876 dw2_asm_output_data (4, btf_size, "btt_size: %uB", btf_size);
877 return;
878 case BTF_KIND_ARRAY:
879 case BTF_KIND_FWD:
880 /* These types do not encode any information in the size/type field
881 and should write 0. */
882 dw2_asm_output_data (4, 0, "(unused)");
883 return;
884 default:
885 break;
888 ctf_id_t ref_id = get_btf_id (dtd->dtd_data.ctti_type);
889 btf_asm_type_ref ("btt_type", ctfc, ref_id);
892 /* Asm'out the variable information following a BTF_KIND_ARRAY. */
894 static void
895 btf_asm_array (ctf_container_ref ctfc, ctf_arinfo_t arr)
897 btf_asm_type_ref ("bta_elem_type", ctfc, get_btf_id (arr.ctr_contents));
898 btf_asm_type_ref ("bta_index_type", ctfc, get_btf_id (arr.ctr_index));
899 dw2_asm_output_data (4, arr.ctr_nelems, "bta_nelems");
902 /* Asm'out a BTF_KIND_VAR. */
904 static void
905 btf_asm_varent (ctf_container_ref ctfc, ctf_dvdef_ref var)
907 ctf_id_t ref_id = get_btf_id (var->dvd_type);
908 dw2_asm_output_data (4, var->dvd_name_offset, "TYPE %u BTF_KIND_VAR '%s'",
909 (*(btf_var_ids->get (var)) + num_types_added + 1),
910 var->dvd_name);
911 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_VAR, 0, 0), "btv_info");
912 btf_asm_type_ref ("btv_type", ctfc, ref_id);
913 dw2_asm_output_data (4, var->dvd_visibility, "btv_linkage");
916 /* Asm'out a member description following a BTF_KIND_STRUCT or
917 BTF_KIND_UNION. */
919 static void
920 btf_asm_sou_member (ctf_container_ref ctfc, ctf_dmdef_t * dmd, unsigned int idx)
922 ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[dmd->dmd_type];
924 /* Re-encode bitfields to BTF representation. */
925 if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
927 ctf_id_t base_type = ref_type->dtd_u.dtu_slice.cts_type;
928 unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
929 unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
930 uint64_t sou_offset = dmd->dmd_offset;
932 /* Pack the bit offset and bitfield size together. */
933 sou_offset += word_offset;
935 /* If this bitfield cannot be represented, do not output anything.
936 The parent struct/union 'vlen' field has already been updated. */
937 if ((bits > 0xff) || (sou_offset > 0xffffff))
938 return;
940 sou_offset &= 0x00ffffff;
941 sou_offset |= ((bits & 0xff) << 24);
943 dw2_asm_output_data (4, dmd->dmd_name_offset,
944 "MEMBER '%s' idx=%u",
945 dmd->dmd_name, idx);
946 /* Refer to the base type of the slice. */
947 btf_asm_type_ref ("btm_type", ctfc, get_btf_id (base_type));
948 dw2_asm_output_data (4, sou_offset, "btm_offset");
950 else
952 dw2_asm_output_data (4, dmd->dmd_name_offset,
953 "MEMBER '%s' idx=%u",
954 dmd->dmd_name, idx);
955 btf_asm_type_ref ("btm_type", ctfc, get_btf_id (dmd->dmd_type));
956 dw2_asm_output_data (4, dmd->dmd_offset, "btm_offset");
960 /* Asm'out an enum constant following a BTF_KIND_ENUM{,64}. */
962 static void
963 btf_asm_enum_const (unsigned int size, ctf_dmdef_t * dmd, unsigned int idx)
965 dw2_asm_output_data (4, dmd->dmd_name_offset, "ENUM_CONST '%s' idx=%u",
966 dmd->dmd_name, idx);
967 if (size <= 4)
968 dw2_asm_output_data (size < 4 ? 4 : size, dmd->dmd_value, "bte_value");
969 else
971 dw2_asm_output_data (4, dmd->dmd_value & 0xffffffff, "bte_value_lo32");
972 dw2_asm_output_data (4, (dmd->dmd_value >> 32) & 0xffffffff, "bte_value_hi32");
976 /* Asm'out a function parameter description following a BTF_KIND_FUNC_PROTO. */
978 static void
979 btf_asm_func_arg (ctf_container_ref ctfc, ctf_func_arg_t * farg,
980 size_t stroffset)
982 /* If the function arg does not have a name, refer to the null string at
983 the start of the string table. This ensures correct encoding for varargs
984 '...' arguments. */
985 if ((farg->farg_name != NULL) && strcmp (farg->farg_name, ""))
986 dw2_asm_output_data (4, farg->farg_name_offset + stroffset, "farg_name");
987 else
988 dw2_asm_output_data (4, 0, "farg_name");
990 btf_asm_type_ref ("farg_type", ctfc, (btf_removed_type_p (farg->farg_type)
991 ? BTF_VOID_TYPEID
992 : get_btf_id (farg->farg_type)));
995 /* Asm'out a BTF_KIND_FUNC type. */
997 static void
998 btf_asm_func_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd, ctf_id_t id)
1000 ctf_id_t ref_id = dtd->dtd_data.ctti_type;
1001 dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
1002 "TYPE %" PRIu64 " BTF_KIND_FUNC '%s'",
1003 btf_absolute_func_id (id), get_btf_type_name (dtd));
1004 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC, 0, dtd->linkage),
1005 "btt_info: kind=%u, kflag=%u, linkage=%u",
1006 BTF_KIND_FUNC, 0, dtd->linkage);
1007 btf_asm_type_ref ("btt_type", ctfc, get_btf_id (ref_id));
1010 /* Asm'out a variable entry following a BTF_KIND_DATASEC. */
1012 static void
1013 btf_asm_datasec_entry (ctf_container_ref ctfc, struct btf_var_secinfo info)
1015 btf_asm_type_ref ("bts_type", ctfc, info.type);
1016 dw2_asm_output_data (4, info.offset, "bts_offset");
1017 dw2_asm_output_data (4, info.size, "bts_size");
1020 /* Asm'out a whole BTF_KIND_DATASEC, including its variable entries. */
1022 static void
1023 btf_asm_datasec_type (ctf_container_ref ctfc, btf_datasec_t ds, ctf_id_t id,
1024 size_t stroffset)
1026 dw2_asm_output_data (4, ds.name_offset + stroffset,
1027 "TYPE %" PRIu64 " BTF_KIND_DATASEC '%s'",
1028 btf_absolute_datasec_id (id), ds.name);
1029 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_DATASEC, 0,
1030 ds.entries.length ()),
1031 "btt_info: n_entries=%u", ds.entries.length ());
1032 /* Note: the "total section size in bytes" is emitted as 0 and patched by
1033 loaders such as libbpf. */
1034 dw2_asm_output_data (4, 0, "btt_size");
1035 for (size_t i = 0; i < ds.entries.length (); i++)
1036 btf_asm_datasec_entry (ctfc, ds.entries[i]);
1039 /* Compute and output the header information for a .BTF section. */
1041 static void
1042 output_btf_header (ctf_container_ref ctfc)
1044 switch_to_section (btf_info_section);
1045 ASM_OUTPUT_LABEL (asm_out_file, btf_info_section_label);
1047 /* BTF magic number, version, flags, and header length. */
1048 dw2_asm_output_data (2, BTF_MAGIC, "btf_magic");
1049 dw2_asm_output_data (1, BTF_VERSION, "btf_version");
1050 dw2_asm_output_data (1, 0, "btf_flags");
1051 dw2_asm_output_data (4, sizeof (struct btf_header), "btf_hdr_len");
1053 uint32_t type_off = 0, type_len = 0;
1054 uint32_t str_off = 0, str_len = 0;
1055 uint32_t datasec_vlen_bytes = 0;
1057 if (!ctfc_is_empty_container (ctfc))
1059 for (size_t i = 0; i < datasecs.length (); i++)
1061 datasec_vlen_bytes += ((datasecs[i].entries.length ())
1062 * sizeof (struct btf_var_secinfo));
1065 /* Total length (bytes) of the types section. */
1066 type_len = (num_types_added * sizeof (struct btf_type))
1067 + (num_types_created * sizeof (struct btf_type))
1068 + datasec_vlen_bytes
1069 + ctfc->ctfc_num_vlen_bytes;
1071 str_off = type_off + type_len;
1073 str_len = ctfc->ctfc_strtable.ctstab_len
1074 + ctfc->ctfc_aux_strtable.ctstab_len;
1077 /* Offset of type section. */
1078 dw2_asm_output_data (4, type_off, "type_off");
1079 /* Length of type section in bytes. */
1080 dw2_asm_output_data (4, type_len, "type_len");
1081 /* Offset of string section. */
1082 dw2_asm_output_data (4, str_off, "str_off");
1083 /* Length of string section in bytes. */
1084 dw2_asm_output_data (4, str_len, "str_len");
1087 /* Output all BTF_KIND_VARs in CTFC. */
1089 static void
1090 output_btf_vars (ctf_container_ref ctfc)
1092 size_t i;
1093 size_t num_ctf_vars = num_vars_added;
1094 if (num_ctf_vars)
1096 for (i = 0; i < num_ctf_vars; i++)
1097 btf_asm_varent (ctfc, ctfc->ctfc_vars_list[i]);
1101 /* Output BTF string records. The BTF strings section is a concatenation
1102 of the standard and auxilliary string tables in the ctf container. */
1104 static void
1105 output_btf_strs (ctf_container_ref ctfc)
1107 ctf_string_t * ctf_string = ctfc->ctfc_strtable.ctstab_head;
1109 while (ctf_string)
1111 dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_string");
1112 ctf_string = ctf_string->cts_next;
1115 ctf_string = ctfc->ctfc_aux_strtable.ctstab_head;
1116 while (ctf_string)
1118 dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_aux_string");
1119 ctf_string = ctf_string->cts_next;
1123 /* Output all (representable) members of a BTF_KIND_STRUCT or
1124 BTF_KIND_UNION type. */
1126 static void
1127 output_asm_btf_sou_fields (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
1129 ctf_dmdef_t * dmd;
1131 unsigned idx = 0;
1132 for (dmd = dtd->dtd_u.dtu_members;
1133 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
1135 btf_asm_sou_member (ctfc, dmd, idx);
1136 idx++;
1140 /* Output all enumerator constants following a BTF_KIND_ENUM{,64}. */
1142 static void
1143 output_asm_btf_enum_list (ctf_container_ref ARG_UNUSED (ctfc),
1144 ctf_dtdef_ref dtd)
1146 ctf_dmdef_t * dmd;
1148 unsigned idx = 0;
1149 for (dmd = dtd->dtd_u.dtu_members;
1150 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
1152 btf_asm_enum_const (dtd->dtd_data.ctti_size, dmd, idx);
1153 idx++;
1157 /* Output all function arguments following a BTF_KIND_FUNC_PROTO. */
1159 static void
1160 output_asm_btf_func_args_list (ctf_container_ref ctfc,
1161 ctf_dtdef_ref dtd)
1163 size_t farg_name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
1164 ctf_func_arg_t * farg;
1165 for (farg = dtd->dtd_u.dtu_argv;
1166 farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
1167 btf_asm_func_arg (ctfc, farg, farg_name_offset);
1170 /* Output the variable portion of a BTF type record. The information depends
1171 on the kind of the type. */
1173 static void
1174 output_asm_btf_vlen_bytes (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
1176 uint32_t btf_kind, encoding;
1178 btf_kind = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
1180 if (btf_kind == BTF_KIND_UNKN)
1181 return;
1183 switch (btf_kind)
1185 case BTF_KIND_INT:
1186 /* Redundant definitions of void may still be hanging around in the type
1187 list as size 0 integers. Skip emitting them. */
1188 if (dtd->dtd_data.ctti_size < 1)
1189 break;
1191 /* In BTF the CHAR `encoding' seems to not be used, so clear it
1192 here. */
1193 dtd->dtd_u.dtu_enc.cte_format &= ~BTF_INT_CHAR;
1195 encoding = BTF_INT_DATA (dtd->dtd_u.dtu_enc.cte_format,
1196 dtd->dtd_u.dtu_enc.cte_offset,
1197 dtd->dtd_u.dtu_enc.cte_bits);
1199 dw2_asm_output_data (4, encoding, "bti_encoding");
1200 break;
1202 case BTF_KIND_ARRAY:
1203 btf_asm_array (ctfc, dtd->dtd_u.dtu_arr);
1204 break;
1206 case BTF_KIND_STRUCT:
1207 case BTF_KIND_UNION:
1208 output_asm_btf_sou_fields (ctfc, dtd);
1209 break;
1211 case BTF_KIND_ENUM:
1212 output_asm_btf_enum_list (ctfc, dtd);
1213 break;
1215 case BTF_KIND_FUNC_PROTO:
1216 output_asm_btf_func_args_list (ctfc, dtd);
1217 break;
1219 case BTF_KIND_VAR:
1220 /* BTF Variables are handled by output_btf_vars and btf_asm_varent.
1221 There should be no BTF_KIND_VAR types at this point. */
1222 gcc_unreachable ();
1224 case BTF_KIND_DATASEC:
1225 /* The BTF_KIND_DATASEC records are handled by output_btf_datasec_types
1226 and btf_asm_datasec_type. There should be no BTF_KIND_DATASEC types
1227 at this point. */
1228 gcc_unreachable ();
1230 default:
1231 /* All other BTF type kinds have no variable length data. */
1232 break;
1236 /* Output a whole BTF type record for TYPE, including the fixed and variable
1237 data portions. */
1239 static void
1240 output_asm_btf_type (ctf_container_ref ctfc, ctf_dtdef_ref type)
1242 if (btf_emit_id_p (type->dtd_type))
1244 btf_asm_type (ctfc, type);
1245 output_asm_btf_vlen_bytes (ctfc, type);
1249 /* Output all BTF types in the container. This does not include synthesized
1250 types: BTF_KIND_VAR, BTF_KIND_FUNC, nor BTF_KIND_DATASEC. */
1252 static void
1253 output_btf_types (ctf_container_ref ctfc)
1255 size_t i;
1256 size_t num_types = ctfc->ctfc_types->elements ();
1257 if (num_types)
1259 for (i = 1; i <= num_types; i++)
1260 output_asm_btf_type (ctfc, ctfc->ctfc_types_list[i]);
1264 /* Output all BTF_KIND_FUNC type records. */
1266 static void
1267 output_btf_func_types (ctf_container_ref ctfc)
1269 for (size_t i = 0; i < vec_safe_length (funcs); i++)
1270 btf_asm_func_type (ctfc, (*funcs)[i], i);
1273 /* Output all BTF_KIND_DATASEC records. */
1275 static void
1276 output_btf_datasec_types (ctf_container_ref ctfc)
1278 size_t name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
1280 for (size_t i = 0; i < datasecs.length(); i++)
1281 btf_asm_datasec_type (ctfc, datasecs[i], i, name_offset);
1284 /* Postprocess the CTF debug data post initialization.
1286 During the postprocess pass:
1288 - Prepare the sorted list of BTF types.
1290 The sorted list of BTF types is, firstly, used for lookup (during the BTF
1291 generation process) of CTF/BTF types given a typeID.
1293 Secondly, in the emitted BTF section, BTF Types need to be in the sorted
1294 order of their type IDs. The BTF types section is viewed as an array,
1295 with type IDs used to index into that array. It is essential that every
1296 type be placed at the exact index corresponding to its ID, or else
1297 references to that type from other types will no longer be correct.
1299 - References to void types are converted to reference BTF_VOID_TYPEID. In
1300 CTF, a distinct type is used to encode void.
1302 - Bitfield struct/union members are converted to BTF encoding. CTF uses
1303 slices to encode bitfields, but BTF does not have slices and encodes
1304 bitfield information directly in the variable-length btf_member
1305 descriptions following the struct or union type.
1307 - Unrepresentable types are removed. We cannot have any invalid BTF types
1308 appearing in the output so they must be removed, and type ids of other
1309 types and references adjust accordingly. This also involves ensuring that
1310 BTF descriptions of struct members referring to unrepresentable types are
1311 not emitted, as they would be nonsensical.
1313 - Adjust inner- and inter-type references-by-ID to account for removed
1314 types, and construct the types list. */
1316 void
1317 btf_init_postprocess (void)
1319 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1321 holes.create (0);
1322 voids.create (0);
1324 num_types_added = 0;
1325 num_types_created = 0;
1327 /* Workaround for 'const void' variables. These variables are sometimes used
1328 in eBPF programs to address kernel symbols. DWARF does not generate const
1329 qualifier on void type, so we would incorrectly emit these variables
1330 without the const qualifier.
1331 Unfortunately we need the TREE node to know it was const, and we need
1332 to create the const modifier type (if needed) now, before making the types
1333 list. So we can't avoid iterating with FOR_EACH_VARIABLE here, and then
1334 again when creating the DATASEC entries. */
1335 ctf_id_t constvoid_id = CTF_NULL_TYPEID;
1336 varpool_node *var;
1337 FOR_EACH_VARIABLE (var)
1339 if (!var->decl)
1340 continue;
1342 tree type = TREE_TYPE (var->decl);
1343 if (type && VOID_TYPE_P (type) && TYPE_READONLY (type))
1345 dw_die_ref die = lookup_decl_die (var->decl);
1346 if (die == NULL)
1347 continue;
1349 ctf_dvdef_ref dvd = ctf_dvd_lookup (tu_ctfc, die);
1350 if (dvd == NULL)
1351 continue;
1353 /* Create the 'const' modifier type for void. */
1354 if (constvoid_id == CTF_NULL_TYPEID)
1355 constvoid_id = ctf_add_reftype (tu_ctfc, CTF_ADD_ROOT,
1356 dvd->dvd_type, CTF_K_CONST, NULL);
1357 dvd->dvd_type = constvoid_id;
1361 size_t i;
1362 size_t num_ctf_types = tu_ctfc->ctfc_types->elements ();
1364 if (num_ctf_types)
1366 init_btf_id_map (num_ctf_types + 1);
1368 /* Allocate the types list and traverse all types, placing each type
1369 at the index according to its ID. Add 1 because type ID 0 always
1370 represents VOID. */
1371 tu_ctfc->ctfc_types_list
1372 = ggc_vec_alloc<ctf_dtdef_ref>(num_ctf_types + 1);
1373 tu_ctfc->ctfc_types->traverse<ctf_container_ref, btf_dtd_postprocess_cb>
1374 (tu_ctfc);
1376 /* Build mapping of CTF type ID -> BTF type ID, and count total number
1377 of valid BTF types added. */
1378 for (i = 1; i <= num_ctf_types; i++)
1380 ctf_dtdef_ref dtd = tu_ctfc->ctfc_types_list[i];
1381 ctf_id_t btfid = btf_adjust_type_id (dtd->dtd_type);
1382 set_btf_id (dtd->dtd_type, btfid);
1383 if (btfid < BTF_MAX_TYPE && (btfid != BTF_VOID_TYPEID))
1384 num_types_added ++;
1389 /* Process and output all BTF data. Entry point of btfout. */
1391 void
1392 btf_output (const char * filename)
1394 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1396 init_btf_sections ();
1398 datasecs.create (0);
1399 vec_alloc (funcs, 16);
1401 ctf_add_cuname (tu_ctfc, filename);
1403 btf_emit_preprocess (tu_ctfc);
1405 output_btf_header (tu_ctfc);
1406 output_btf_types (tu_ctfc);
1407 output_btf_vars (tu_ctfc);
1408 output_btf_func_types (tu_ctfc);
1409 output_btf_datasec_types (tu_ctfc);
1410 output_btf_strs (tu_ctfc);
1413 /* Reset all state for BTF generation so that we can rerun the compiler within
1414 the same process. */
1416 void
1417 btf_finalize (void)
1419 btf_info_section = NULL;
1421 /* Clear preprocessing state. */
1422 num_vars_added = 0;
1423 num_types_added = 0;
1424 num_types_created = 0;
1426 holes.release ();
1427 voids.release ();
1428 for (size_t i = 0; i < datasecs.length (); i++)
1429 datasecs[i].entries.release ();
1430 datasecs.release ();
1432 funcs = NULL;
1434 btf_var_ids->empty ();
1435 btf_var_ids = NULL;
1437 free (btf_id_map);
1438 btf_id_map = NULL;
1440 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1441 ctfc_delete_container (tu_ctfc);
1442 tu_ctfc = NULL;
1445 #include "gt-btfout.h"