xfail scan-tree-dump-not throw in g++.dg/pr99966.C on hppa*64*-*-*
[official-gcc.git] / gcc / btfout.cc
blobdcf751f8fe0de6ea8dfd0ce4396fc3568cd95f75
1 /* Output BTF format from GCC.
2 Copyright (C) 2021-2024 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 "stringpool.h" /* For lookup_attribute. */
39 #include "attribs.h" /* For lookup_attribute. */
40 #include "dwarf2out.h" /* For lookup_decl_die. */
42 static int btf_label_num;
44 static GTY (()) section * btf_info_section;
46 /* BTF debug info section. */
48 #ifndef BTF_INFO_SECTION_NAME
49 #define BTF_INFO_SECTION_NAME ".BTF"
50 #endif
52 #define BTF_INFO_SECTION_FLAGS (SECTION_DEBUG)
54 /* Maximum size (in bytes) for an artifically generated BTF label. */
56 #define MAX_BTF_LABEL_BYTES 40
58 static char btf_info_section_label[MAX_BTF_LABEL_BYTES];
60 #ifndef BTF_INFO_SECTION_LABEL
61 #define BTF_INFO_SECTION_LABEL "Lbtf"
62 #endif
64 /* BTF encodes void as type id 0. */
66 #define BTF_VOID_TYPEID 0
67 #define BTF_INIT_TYPEID 1
69 #define BTF_INVALID_TYPEID 0xFFFFFFFF
71 /* Mapping of CTF variables to the IDs they will be assigned when they are
72 converted to BTF_KIND_VAR type records. Strictly accounts for the index
73 from the start of the variable type entries, does not include the number
74 of types emitted prior to the variable records. */
75 static GTY (()) hash_map <ctf_dvdef_ref, unsigned> *btf_var_ids;
77 /* Mapping of type IDs from original CTF ID to BTF ID. Types do not map
78 1-to-1 from CTF to BTF. To avoid polluting the CTF container when updating
79 type references-by-ID, we use this map instead. */
80 static ctf_id_t * btf_id_map = NULL;
82 /* Information for creating the BTF_KIND_DATASEC records. */
83 typedef struct btf_datasec
85 const char *name; /* Section name, e.g. ".bss". */
86 uint32_t name_offset; /* Offset to name in string table. */
87 vec<struct btf_var_secinfo> entries; /* Variable entries in this section. */
88 } btf_datasec_t;
90 /* One BTF_KIND_DATASEC record is created for each output data section which
91 will hold at least one variable. */
92 static vec<btf_datasec_t> datasecs;
94 /* Holes occur for types which are present in the CTF container, but are either
95 non-representable or redundant in BTF. */
96 static vec<ctf_id_t> holes;
98 /* CTF definition(s) of void. Only one definition of void should be generated.
99 We should not encounter more than one definition of void, but use a vector
100 to be safe. */
101 static vec<ctf_id_t> voids;
103 /* Functions in BTF have two separate type records - one for the prototype
104 (BTF_KIND_FUNC_PROTO), as well as a BTF_KIND_FUNC. CTF_K_FUNCTION types
105 map closely to BTF_KIND_FUNC_PROTO, but the BTF_KIND_FUNC records must be
106 created. This vector holds them. */
107 static GTY (()) vec<ctf_dtdef_ref, va_gc> *funcs;
109 /* The number of BTF variables added to the TU CTF container. */
110 static unsigned int num_vars_added = 0;
112 /* The number of BTF types added to the TU CTF container. */
113 static unsigned int num_types_added = 0;
115 /* The number of types synthesized for BTF that do not correspond to
116 CTF types. */
117 static unsigned int num_types_created = 0;
119 /* Name strings for BTF kinds.
120 Note: the indices here must match the type defines in btf.h. */
121 static const char *const btf_kind_names[] =
123 "UNKN", "INT", "PTR", "ARRAY", "STRUCT", "UNION", "ENUM", "FWD",
124 "TYPEDEF", "VOLATILE", "CONST", "RESTRICT", "FUNC", "FUNC_PROTO",
125 "VAR", "DATASEC", "FLOAT", "DECL_TAG", "TYPE_TAG", "ENUM64"
128 /* Return a name string for the given BTF_KIND. */
130 static const char *
131 btf_kind_name (uint32_t btf_kind)
133 return btf_kind_names[btf_kind];
136 /* Map a CTF type kind to the corresponding BTF type kind. */
138 static uint32_t
139 get_btf_kind (uint32_t ctf_kind)
141 /* N.B. the values encoding kinds are not in general the same for the
142 same kind between CTF and BTF. e.g. CTF_K_CONST != BTF_KIND_CONST. */
143 switch (ctf_kind)
145 case CTF_K_INTEGER: return BTF_KIND_INT;
146 case CTF_K_FLOAT: return BTF_KIND_FLOAT;
147 case CTF_K_POINTER: return BTF_KIND_PTR;
148 case CTF_K_ARRAY: return BTF_KIND_ARRAY;
149 case CTF_K_FUNCTION: return BTF_KIND_FUNC_PROTO;
150 case CTF_K_STRUCT: return BTF_KIND_STRUCT;
151 case CTF_K_UNION: return BTF_KIND_UNION;
152 case CTF_K_ENUM: return BTF_KIND_ENUM;
153 case CTF_K_FORWARD: return BTF_KIND_FWD;
154 case CTF_K_TYPEDEF: return BTF_KIND_TYPEDEF;
155 case CTF_K_VOLATILE: return BTF_KIND_VOLATILE;
156 case CTF_K_CONST: return BTF_KIND_CONST;
157 case CTF_K_RESTRICT: return BTF_KIND_RESTRICT;
158 default:;
160 return BTF_KIND_UNKN;
163 /* Some BTF types, like BTF_KIND_FUNC_PROTO, are anonymous. The machinery
164 in btfout to emit BTF, may reset dtd_data->ctti_name, but does not update
165 the name in the ctf_dtdef_ref type object (deliberate choice). This
166 interface helps abstract out that state of affairs, while giving access to
167 the name of the type as intended. */
169 static const char *
170 get_btf_type_name (ctf_dtdef_ref dtd)
172 const char *anon = "";
173 return (dtd->dtd_data.ctti_name) ? dtd->dtd_name : anon;
176 /* Helper routines to map between 'relative' and 'absolute' IDs.
178 In BTF all records (including variables) are output in one long list, and all
179 inter-type references are via index into that list. But internally since we
180 a) translate from CTF, which separates variable records from regular types
181 and b) create some additional types after the fact, things like VAR and FUNC
182 records are stored in separate vectors with their own indices. These
183 functions map between the 'relative' IDs (i.e. indices in their respective
184 containers) and 'absolute' IDs (i.e. indices in the final contiguous
185 output list), which goes in order:
186 all normal type records translated from CTF
187 all BTF_KIND_VAR records
188 all BTF_KIND_FUNC records (synthesized split function records)
189 all BTF_KIND_DATASEC records (synthesized)
191 The extra '+ 1's below are to account for the implicit "void" record, which
192 has index 0 but isn't actually contained in the type list. */
194 /* Return the final BTF ID of the variable at relative index REL. */
196 static ctf_id_t
197 btf_absolute_var_id (ctf_id_t rel)
199 return rel + (num_types_added + 1);
202 /* Return the relative index of the variable with final BTF ID ABS. */
204 static ctf_id_t
205 btf_relative_var_id (ctf_id_t abs)
207 return abs - (num_types_added + 1);
210 /* Return the final BTF ID of the func record at relative index REL. */
212 static ctf_id_t
213 btf_absolute_func_id (ctf_id_t rel)
215 return rel + (num_types_added + 1) + num_vars_added;
218 /* Return the relative index of the func record with final BTF ID ABS. */
220 static ctf_id_t
221 btf_relative_func_id (ctf_id_t abs)
223 return abs - ((num_types_added + 1) + num_vars_added);
226 /* Return the final BTF ID of the datasec record at relative index REL. */
228 static ctf_id_t
229 btf_absolute_datasec_id (ctf_id_t rel)
231 return rel + (num_types_added + 1) + num_vars_added + funcs->length ();
235 /* Allocate the btf_id_map, and initialize elements to BTF_INVALID_TYPEID. */
237 static void
238 init_btf_id_map (size_t len)
240 btf_id_map = XNEWVEC (ctf_id_t, len);
242 btf_id_map[0] = BTF_VOID_TYPEID;
243 for (size_t i = 1; i < len; i++)
244 btf_id_map[i] = BTF_INVALID_TYPEID;
247 /* Return the BTF type ID of CTF type ID KEY, or BTF_INVALID_TYPEID if the CTF
248 type with ID KEY does not map to a BTF type. */
250 ctf_id_t
251 get_btf_id (ctf_id_t key)
253 return btf_id_map[key];
256 /* Set the CTF type ID KEY to map to BTF type ID VAL. */
258 static inline void
259 set_btf_id (ctf_id_t key, ctf_id_t val)
261 btf_id_map[key] = val;
264 /* Return TRUE iff the given CTF type ID maps to a BTF type which will
265 be emitted. */
266 static inline bool
267 btf_emit_id_p (ctf_id_t id)
269 return ((btf_id_map[id] != BTF_VOID_TYPEID)
270 && (btf_id_map[id] <= BTF_MAX_TYPE));
273 /* Return true if DTD is a forward-declared enum. The BTF representation
274 of forward declared enums is not formally defined. */
276 static bool
277 btf_fwd_to_enum_p (ctf_dtdef_ref dtd)
279 uint32_t btf_kind = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
281 return (btf_kind == BTF_KIND_FWD && dtd->dtd_data.ctti_type == CTF_K_ENUM);
284 /* Each BTF type can be followed additional, variable-length information
285 completing the description of the type. Calculate the number of bytes
286 of variable information required to encode a given type. */
288 static uint64_t
289 btf_calc_num_vbytes (ctf_dtdef_ref dtd)
291 uint64_t vlen_bytes = 0;
293 uint32_t kind = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
294 uint32_t vlen = CTF_V2_INFO_VLEN (dtd->dtd_data.ctti_info);
296 switch (kind)
298 case BTF_KIND_UNKN:
299 case BTF_KIND_PTR:
300 case BTF_KIND_FWD:
301 case BTF_KIND_TYPEDEF:
302 case BTF_KIND_VOLATILE:
303 case BTF_KIND_CONST:
304 case BTF_KIND_RESTRICT:
305 case BTF_KIND_FUNC:
306 /* These kinds have no vlen data. */
307 break;
309 case BTF_KIND_INT:
310 /* Size 0 integers represent redundant definitions of void that will
311 not be emitted. Don't allocate space for them. */
312 if (dtd->dtd_data.ctti_size == 0)
313 break;
315 vlen_bytes += sizeof (uint32_t);
316 break;
318 case BTF_KIND_ARRAY:
319 vlen_bytes += sizeof (struct btf_array);
320 break;
322 case BTF_KIND_STRUCT:
323 case BTF_KIND_UNION:
324 vlen_bytes += vlen * sizeof (struct btf_member);
325 break;
327 case BTF_KIND_ENUM:
328 vlen_bytes += (dtd->dtd_data.ctti_size > 4)
329 ? vlen * sizeof (struct btf_enum64)
330 : vlen * sizeof (struct btf_enum);
331 break;
333 case BTF_KIND_FUNC_PROTO:
334 vlen_bytes += vlen * sizeof (struct btf_param);
335 break;
337 case BTF_KIND_VAR:
338 vlen_bytes += sizeof (struct btf_var);
339 break;
341 case BTF_KIND_DATASEC:
342 vlen_bytes += vlen * sizeof (struct btf_var_secinfo);
343 break;
345 default:
346 break;
348 return vlen_bytes;
351 /* Initialize BTF section (.BTF) for output. */
353 void
354 init_btf_sections (void)
356 btf_info_section = get_section (BTF_INFO_SECTION_NAME, BTF_INFO_SECTION_FLAGS,
357 NULL);
359 ASM_GENERATE_INTERNAL_LABEL (btf_info_section_label,
360 BTF_INFO_SECTION_LABEL, btf_label_num++);
363 /* Push a BTF datasec variable entry INFO into the datasec named SECNAME,
364 creating the datasec if it does not already exist. */
366 static void
367 btf_datasec_push_entry (ctf_container_ref ctfc, const char *secname,
368 struct btf_var_secinfo info)
370 if (secname == NULL)
371 return;
373 for (size_t i = 0; i < datasecs.length (); i++)
374 if (strcmp (datasecs[i].name, secname) == 0)
376 datasecs[i].entries.safe_push (info);
377 return;
380 /* If we don't already have a datasec record for secname, make one. */
382 uint32_t str_off;
383 ctf_add_string (ctfc, secname, &str_off, CTF_AUX_STRTAB);
384 if (strcmp (secname, ""))
385 ctfc->ctfc_aux_strlen += strlen (secname) + 1;
387 btf_datasec_t ds;
388 ds.name = secname;
389 ds.name_offset = str_off;
391 ds.entries.create (0);
392 ds.entries.safe_push (info);
394 datasecs.safe_push (ds);
398 /* Return the section name, as of interest to btf_collect_datasec, for the
399 given symtab node. Note that this deliberately returns NULL for objects
400 which do not go in a section btf_collect_datasec cares about. */
401 static const char *
402 get_section_name (symtab_node *node)
404 const char *section_name = node->get_section ();
406 if (section_name == NULL)
408 switch (categorize_decl_for_section (node->decl, 0))
410 case SECCAT_BSS:
411 section_name = ".bss";
412 break;
413 case SECCAT_DATA:
414 section_name = ".data";
415 break;
416 case SECCAT_RODATA:
417 section_name = ".rodata";
418 break;
419 default:;
423 return section_name;
426 /* Construct all BTF_KIND_DATASEC records for CTFC. One such record is created
427 for each non-empty data-containing section in the output. Each record is
428 followed by a variable number of entries describing the variables stored
429 in that section. */
431 static void
432 btf_collect_datasec (ctf_container_ref ctfc)
434 cgraph_node *func;
435 FOR_EACH_FUNCTION (func)
437 dw_die_ref die = lookup_decl_die (func->decl);
438 if (die == NULL)
439 continue;
441 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
442 if (dtd == NULL)
443 continue;
445 if (DECL_EXTERNAL (func->decl)
446 && (lookup_attribute ("kernel_helper",
447 DECL_ATTRIBUTES (func->decl))) != NULL_TREE)
448 continue;
450 /* Functions actually get two types: a BTF_KIND_FUNC_PROTO, and
451 also a BTF_KIND_FUNC. But the CTF container only allocates one
452 type per function, which matches closely with BTF_KIND_FUNC_PROTO.
453 For each such function, also allocate a BTF_KIND_FUNC entry.
454 These will be output later. */
455 ctf_dtdef_ref func_dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
456 func_dtd->dtd_data = dtd->dtd_data;
457 func_dtd->dtd_data.ctti_type = dtd->dtd_type;
458 func_dtd->linkage = dtd->linkage;
459 func_dtd->dtd_name = dtd->dtd_name;
460 func_dtd->dtd_type = num_types_added + num_types_created;
462 /* Only the BTF_KIND_FUNC type actually references the name. The
463 BTF_KIND_FUNC_PROTO is always anonymous. */
464 dtd->dtd_data.ctti_name = 0;
466 vec_safe_push (funcs, func_dtd);
467 num_types_created++;
469 /* Mark any 'extern' funcs and add DATASEC entries for them. */
470 if (DECL_EXTERNAL (func->decl))
472 func_dtd->linkage = BTF_FUNC_EXTERN;
474 const char *section_name = get_section_name (func);
475 /* Note: get_section_name () returns NULL for functions in text
476 section. This is intentional, since we do not want to generate
477 DATASEC entries for them. */
478 if (section_name == NULL)
479 continue;
481 struct btf_var_secinfo info;
483 /* +1 for the sentinel type not in the types map. */
484 info.type = func_dtd->dtd_type + 1;
486 /* Both zero at compile time. */
487 info.size = 0;
488 info.offset = 0;
490 btf_datasec_push_entry (ctfc, section_name, info);
494 varpool_node *node;
495 FOR_EACH_VARIABLE (node)
497 dw_die_ref die = lookup_decl_die (node->decl);
498 if (die == NULL)
499 continue;
501 ctf_dvdef_ref dvd = ctf_dvd_lookup (ctfc, die);
502 if (dvd == NULL)
503 continue;
505 /* Mark extern variables. */
506 if (DECL_EXTERNAL (node->decl))
508 dvd->dvd_visibility = BTF_VAR_GLOBAL_EXTERN;
510 /* PR112849: avoid assuming a section for extern decls without
511 an explicit section, which would result in incorrectly
512 emitting a BTF_KIND_DATASEC entry for them. */
513 if (node->get_section () == NULL)
514 continue;
517 const char *section_name = get_section_name (node);
518 if (section_name == NULL)
519 continue;
521 struct btf_var_secinfo info;
523 info.type = 0;
524 unsigned int *var_id = btf_var_ids->get (dvd);
525 if (var_id)
526 info.type = btf_absolute_var_id (*var_id);
527 else
528 continue;
530 info.size = 0;
531 tree size = DECL_SIZE_UNIT (node->decl);
532 if (tree_fits_uhwi_p (size))
533 info.size = tree_to_uhwi (size);
534 else if (VOID_TYPE_P (TREE_TYPE (node->decl)))
535 info.size = 1;
537 /* Offset is left as 0 at compile time, to be filled in by loaders such
538 as libbpf. */
539 info.offset = 0;
541 btf_datasec_push_entry (ctfc, section_name, info);
544 num_types_created += datasecs.length ();
547 /* Return true if the type ID is that of a type which will not be emitted (for
548 example, if it is not representable in BTF). */
550 static bool
551 btf_removed_type_p (ctf_id_t id)
553 return holes.contains (id);
556 /* Adjust the given type ID to account for holes and duplicate definitions of
557 void. */
559 static ctf_id_t
560 btf_adjust_type_id (ctf_id_t id)
562 size_t n;
563 ctf_id_t i = 0;
565 /* Do not adjust invalid type markers. */
566 if (id == BTF_INVALID_TYPEID)
567 return id;
569 for (n = 0; n < voids.length (); n++)
570 if (id == voids[n])
571 return BTF_VOID_TYPEID;
573 for (n = 0; n < holes.length (); n++)
575 if (holes[n] < id)
576 i++;
577 else if (holes[n] == id)
578 return BTF_VOID_TYPEID;
581 return id - i;
584 /* Postprocessing callback routine for types. */
587 btf_dtd_postprocess_cb (ctf_dtdef_ref *slot, ctf_container_ref arg_ctfc)
589 ctf_dtdef_ref ctftype = (ctf_dtdef_ref) * slot;
591 size_t index = ctftype->dtd_type;
592 gcc_assert (index <= arg_ctfc->ctfc_types->elements ());
594 uint32_t ctf_kind, btf_kind;
596 ctf_kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
597 btf_kind = get_btf_kind (ctf_kind);
599 if (btf_kind == BTF_KIND_UNKN)
600 /* This type is not representable in BTF. Create a hole. */
601 holes.safe_push (ctftype->dtd_type);
603 else if (btf_kind == BTF_KIND_INT && ctftype->dtd_data.ctti_size == 0)
605 /* This is a (redundant) definition of void. */
606 voids.safe_push (ctftype->dtd_type);
607 holes.safe_push (ctftype->dtd_type);
610 arg_ctfc->ctfc_types_list[index] = ctftype;
612 return 1;
615 /* Preprocessing callback routine for variables. */
618 btf_dvd_emit_preprocess_cb (ctf_dvdef_ref *slot, ctf_container_ref arg_ctfc)
620 ctf_dvdef_ref var = (ctf_dvdef_ref) * slot;
622 /* If this is an extern variable declaration with a defining declaration
623 later, skip it so that only the defining declaration is emitted.
624 This is the same case, fix and reasoning as in CTF; see PR105089. */
625 if (ctf_dvd_ignore_lookup (arg_ctfc, var->dvd_key))
626 return 1;
628 /* Do not add variables which refer to unsupported types. */
629 if (!voids.contains (var->dvd_type) && btf_removed_type_p (var->dvd_type))
630 return 1;
632 arg_ctfc->ctfc_vars_list[num_vars_added] = var;
633 btf_var_ids->put (var, num_vars_added);
635 num_vars_added++;
636 num_types_created++;
638 return 1;
641 /* Preprocessing callback routine for types. */
643 static void
644 btf_dtd_emit_preprocess_cb (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
646 if (!btf_emit_id_p (dtd->dtd_type))
647 return;
649 ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (dtd);
652 /* Preprocess the CTF information to prepare for BTF output. BTF is almost a
653 subset of CTF, with many small differences in encoding, and lacking support
654 for some types (notably floating point formats).
656 During the preprocessing pass:
657 - Ascertain that the sorted list of types has been prepared. For the BTF
658 generation process, this is taken care of by the btf_init_postprocess ().
660 - BTF_KIND_FUNC and BTF_KIND_DATASEC records are constructed. These types do
661 not have analogues in CTF (the analogous type to CTF_K_FUNCTION is
662 BTF_KIND_FUNC_PROTO), but can be relatively easily deduced from CTF
663 information.
665 - Construct BTF_KIND_VAR records, representing variables.
667 - Calculate the total size in bytes of variable-length information following
668 BTF type records. This is used for outputting the BTF header.
670 After preprocessing, all BTF information is ready to be output:
671 - ctfc->ctfc_types_list holdstypes converted from CTF types. This does not
672 include KIND_VAR, KIND_FUNC, nor KIND_DATASEC types. These types have been
673 re-encoded to the appropriate representation in BTF.
674 - ctfc->ctfc_vars_list holds all variables which should be output.
675 Variables of unsupported types are not present in this list.
676 - Vector 'funcs' holds all BTF_KIND_FUNC types, one to match each
677 BTF_KIND_FUNC_PROTO.
678 - Vector 'datasecs' holds all BTF_KIND_DATASEC types. */
680 static void
681 btf_emit_preprocess (ctf_container_ref ctfc)
683 size_t num_ctf_types = ctfc->ctfc_types->elements ();
684 size_t num_ctf_vars = ctfc->ctfc_vars->elements ();
685 size_t i;
687 if (num_ctf_types)
689 gcc_assert (ctfc->ctfc_types_list);
690 /* Preprocess the types. */
691 for (i = 1; i <= num_ctf_types; i++)
692 btf_dtd_emit_preprocess_cb (ctfc, ctfc->ctfc_types_list[i]);
695 btf_var_ids = hash_map<ctf_dvdef_ref, unsigned int>::create_ggc (100);
697 if (num_ctf_vars)
699 /* Allocate and construct the list of variables. While BTF variables are
700 not distinct from types (in that variables are simply types with
701 BTF_KIND_VAR), it is simpler to maintain a separate list of variables
702 and append them to the types list during output. */
703 ctfc->ctfc_vars_list = ggc_vec_alloc<ctf_dvdef_ref>(num_ctf_vars);
704 ctfc->ctfc_vars->traverse<ctf_container_ref, btf_dvd_emit_preprocess_cb>
705 (ctfc);
707 ctfc->ctfc_num_vlen_bytes += (num_vars_added * sizeof (struct btf_var));
710 btf_collect_datasec (ctfc);
713 /* Return true iff DMD is a member description of a bit-field which can be
714 validly represented in BTF. */
716 static bool
717 btf_dmd_representable_bitfield_p (ctf_container_ref ctfc, ctf_dmdef_t *dmd)
719 ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[dmd->dmd_type];
721 if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
723 unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
724 unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
725 uint64_t sou_offset = dmd->dmd_offset;
727 if ((bits > 0xff) || ((sou_offset + word_offset) > 0xffffff))
728 return false;
730 return true;
733 return false;
736 /* BTF asm helper routines. */
738 /* Asm'out a reference to another BTF type. */
740 static void
741 btf_asm_type_ref (const char *prefix, ctf_container_ref ctfc, ctf_id_t ref_id)
743 if (ref_id == BTF_VOID_TYPEID || ref_id == BTF_INVALID_TYPEID)
745 /* There is no explicit void type.
746 Also handle any invalid refs that made it this far, just in case. */
747 dw2_asm_output_data (4, ref_id, "%s: void", prefix);
749 else if (ref_id >= num_types_added + 1
750 && ref_id < num_types_added + num_vars_added + 1)
752 /* Ref to a variable. Should only appear in DATASEC entries. */
753 ctf_id_t var_id = btf_relative_var_id (ref_id);
754 ctf_dvdef_ref dvd = ctfc->ctfc_vars_list[var_id];
755 dw2_asm_output_data (4, ref_id, "%s: (BTF_KIND_VAR '%s')",
756 prefix, dvd->dvd_name);
759 else if (ref_id >= num_types_added + num_vars_added + 1)
761 /* Ref to a FUNC record. */
762 size_t func_id = btf_relative_func_id (ref_id);
763 ctf_dtdef_ref ref_type = (*funcs)[func_id];
764 dw2_asm_output_data (4, ref_id, "%s: (BTF_KIND_FUNC '%s')",
765 prefix, get_btf_type_name (ref_type));
767 else
769 /* Ref to a standard type in the types list. */
770 ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[ref_id];
771 uint32_t ref_kind
772 = get_btf_kind (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info));
774 const char *kind_name = btf_fwd_to_enum_p (ref_type)
775 ? btf_kind_name (BTF_KIND_ENUM)
776 : btf_kind_name (ref_kind);
778 dw2_asm_output_data (4, ref_id, "%s: (BTF_KIND_%s '%s')",
779 prefix, kind_name,
780 get_btf_type_name (ref_type));
784 /* Asm'out a BTF type. This routine is responsible for the bulk of the task
785 of converting CTF types to their BTF representation. */
787 static void
788 btf_asm_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
790 uint32_t btf_kind, btf_kflag, btf_vlen, btf_size;
791 uint32_t ctf_info = dtd->dtd_data.ctti_info;
793 btf_kind = get_btf_kind (CTF_V2_INFO_KIND (ctf_info));
794 btf_size = dtd->dtd_data.ctti_size;
795 btf_vlen = CTF_V2_INFO_VLEN (ctf_info);
797 /* By now any unrepresentable types have been removed. */
798 gcc_assert (btf_kind != BTF_KIND_UNKN);
800 /* Size 0 integers are redundant definitions of void. None should remain
801 in the types list by this point. */
802 gcc_assert (btf_kind != BTF_KIND_INT || btf_size >= 1);
804 /* Re-encode the ctti_info to BTF. */
805 /* kflag is 1 for structs/unions with a bitfield member.
806 kflag is 1 for forwards to unions.
807 kflag is 0 in all other cases. */
808 btf_kflag = 0;
810 if (btf_kind == BTF_KIND_STRUCT || btf_kind == BTF_KIND_UNION)
812 /* If a struct/union has ANY bitfield members, set kflag=1.
813 Note that we must also change the encoding of every member to encode
814 both member bitfield size (stealing most-significant 8 bits) and bit
815 offset (LS 24 bits). This is done during preprocessing. */
816 ctf_dmdef_t *dmd;
817 for (dmd = dtd->dtd_u.dtu_members;
818 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
820 /* Set kflag if this member is a representable bitfield. */
821 if (btf_dmd_representable_bitfield_p (ctfc, dmd))
822 btf_kflag = 1;
824 /* Struct members that refer to unsupported types or bitfield formats
825 shall be skipped. These are marked during preprocessing. */
826 else if (!btf_emit_id_p (dmd->dmd_type))
827 btf_vlen -= 1;
831 /* BTF forwards make use of KIND_FLAG to distinguish between forwards to
832 structs and forwards to unions. The dwarf2ctf conversion process stores
833 the kind of the forward in ctti_type, but for BTF this must be 0 for
834 forwards, with only the KIND_FLAG to distinguish.
835 Forwards to enum types are special-cased below. */
836 else if (btf_kind == BTF_KIND_FWD)
838 if (dtd->dtd_data.ctti_type == CTF_K_UNION)
839 btf_kflag = 1;
841 /* PR debug/111735. Encode foward-declared enums as BTF_KIND_ENUM
842 with vlen=0. A representation for these is not formally defined;
843 this is the de-facto standard used by other tools like clang
844 and pahole. */
845 else if (dtd->dtd_data.ctti_type == CTF_K_ENUM)
847 btf_kind = BTF_KIND_ENUM;
848 btf_vlen = 0;
851 btf_size = 0;
854 else if (btf_kind == BTF_KIND_ENUM)
856 btf_kflag = dtd->dtd_enum_unsigned
857 ? BTF_KF_ENUM_UNSIGNED
858 : BTF_KF_ENUM_SIGNED;
859 if (dtd->dtd_data.ctti_size == 0x8)
860 btf_kind = BTF_KIND_ENUM64;
863 /* PR debug/112656. BTF_KIND_FUNC_PROTO is always anonymous. */
864 else if (btf_kind == BTF_KIND_FUNC_PROTO)
865 dtd->dtd_data.ctti_name = 0;
867 dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
868 "TYPE %" PRIu64 " BTF_KIND_%s '%s'",
869 get_btf_id (dtd->dtd_type), btf_kind_name (btf_kind),
870 get_btf_type_name (dtd));
871 dw2_asm_output_data (4, BTF_TYPE_INFO (btf_kind, btf_kflag, btf_vlen),
872 "btt_info: kind=%u, kflag=%u, vlen=%u",
873 btf_kind, btf_kflag, btf_vlen);
874 switch (btf_kind)
876 case BTF_KIND_INT:
877 case BTF_KIND_FLOAT:
878 case BTF_KIND_STRUCT:
879 case BTF_KIND_UNION:
880 case BTF_KIND_ENUM:
881 case BTF_KIND_DATASEC:
882 case BTF_KIND_ENUM64:
883 dw2_asm_output_data (4, btf_size, "btt_size: %uB", btf_size);
884 return;
885 case BTF_KIND_ARRAY:
886 case BTF_KIND_FWD:
887 /* These types do not encode any information in the size/type field
888 and should write 0. */
889 dw2_asm_output_data (4, 0, "(unused)");
890 return;
891 default:
892 break;
895 ctf_id_t ref_id = get_btf_id (dtd->dtd_data.ctti_type);
896 btf_asm_type_ref ("btt_type", ctfc, ref_id);
899 /* Asm'out the variable information following a BTF_KIND_ARRAY. */
901 static void
902 btf_asm_array (ctf_container_ref ctfc, ctf_arinfo_t arr)
904 btf_asm_type_ref ("bta_elem_type", ctfc, get_btf_id (arr.ctr_contents));
905 btf_asm_type_ref ("bta_index_type", ctfc, get_btf_id (arr.ctr_index));
906 dw2_asm_output_data (4, arr.ctr_nelems, "bta_nelems");
909 /* Asm'out a BTF_KIND_VAR. */
911 static void
912 btf_asm_varent (ctf_container_ref ctfc, ctf_dvdef_ref var)
914 ctf_id_t ref_id = get_btf_id (var->dvd_type);
915 dw2_asm_output_data (4, var->dvd_name_offset, "TYPE %u BTF_KIND_VAR '%s'",
916 (*(btf_var_ids->get (var)) + num_types_added + 1),
917 var->dvd_name);
918 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_VAR, 0, 0), "btv_info");
919 btf_asm_type_ref ("btv_type", ctfc, ref_id);
920 dw2_asm_output_data (4, var->dvd_visibility, "btv_linkage");
923 /* Asm'out a member description following a BTF_KIND_STRUCT or
924 BTF_KIND_UNION. */
926 static void
927 btf_asm_sou_member (ctf_container_ref ctfc, ctf_dmdef_t * dmd, unsigned int idx)
929 ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[dmd->dmd_type];
931 /* Re-encode bitfields to BTF representation. */
932 if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
934 ctf_id_t base_type = ref_type->dtd_u.dtu_slice.cts_type;
935 unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
936 unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
937 uint64_t sou_offset = dmd->dmd_offset;
939 /* Pack the bit offset and bitfield size together. */
940 sou_offset += word_offset;
942 /* If this bitfield cannot be represented, do not output anything.
943 The parent struct/union 'vlen' field has already been updated. */
944 if ((bits > 0xff) || (sou_offset > 0xffffff))
945 return;
947 sou_offset &= 0x00ffffff;
948 sou_offset |= ((bits & 0xff) << 24);
950 dw2_asm_output_data (4, dmd->dmd_name_offset,
951 "MEMBER '%s' idx=%u",
952 dmd->dmd_name, idx);
953 /* Refer to the base type of the slice. */
954 btf_asm_type_ref ("btm_type", ctfc, get_btf_id (base_type));
955 dw2_asm_output_data (4, sou_offset, "btm_offset");
957 else
959 dw2_asm_output_data (4, dmd->dmd_name_offset,
960 "MEMBER '%s' idx=%u",
961 dmd->dmd_name, idx);
962 btf_asm_type_ref ("btm_type", ctfc, get_btf_id (dmd->dmd_type));
963 dw2_asm_output_data (4, dmd->dmd_offset, "btm_offset");
967 /* Asm'out an enum constant following a BTF_KIND_ENUM{,64}. */
969 static void
970 btf_asm_enum_const (unsigned int size, ctf_dmdef_t * dmd, unsigned int idx)
972 dw2_asm_output_data (4, dmd->dmd_name_offset, "ENUM_CONST '%s' idx=%u",
973 dmd->dmd_name, idx);
974 if (size <= 4)
975 dw2_asm_output_data (size < 4 ? 4 : size, dmd->dmd_value, "bte_value");
976 else
978 dw2_asm_output_data (4, dmd->dmd_value & 0xffffffff, "bte_value_lo32");
979 dw2_asm_output_data (4, (dmd->dmd_value >> 32) & 0xffffffff, "bte_value_hi32");
983 /* Asm'out a function parameter description following a BTF_KIND_FUNC_PROTO. */
985 static void
986 btf_asm_func_arg (ctf_container_ref ctfc, ctf_func_arg_t * farg,
987 size_t stroffset)
989 /* If the function arg does not have a name, refer to the null string at
990 the start of the string table. This ensures correct encoding for varargs
991 '...' arguments. */
992 if ((farg->farg_name != NULL) && strcmp (farg->farg_name, ""))
993 dw2_asm_output_data (4, farg->farg_name_offset + stroffset, "farg_name");
994 else
995 dw2_asm_output_data (4, 0, "farg_name");
997 btf_asm_type_ref ("farg_type", ctfc, (btf_removed_type_p (farg->farg_type)
998 ? BTF_VOID_TYPEID
999 : get_btf_id (farg->farg_type)));
1002 /* Asm'out a BTF_KIND_FUNC type. */
1004 static void
1005 btf_asm_func_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd, ctf_id_t id)
1007 ctf_id_t ref_id = dtd->dtd_data.ctti_type;
1008 dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
1009 "TYPE %" PRIu64 " BTF_KIND_FUNC '%s'",
1010 btf_absolute_func_id (id), get_btf_type_name (dtd));
1011 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC, 0, dtd->linkage),
1012 "btt_info: kind=%u, kflag=%u, linkage=%u",
1013 BTF_KIND_FUNC, 0, dtd->linkage);
1014 btf_asm_type_ref ("btt_type", ctfc, get_btf_id (ref_id));
1017 /* Asm'out a variable entry following a BTF_KIND_DATASEC. */
1019 static void
1020 btf_asm_datasec_entry (ctf_container_ref ctfc, struct btf_var_secinfo info)
1022 btf_asm_type_ref ("bts_type", ctfc, info.type);
1023 dw2_asm_output_data (4, info.offset, "bts_offset");
1024 dw2_asm_output_data (4, info.size, "bts_size");
1027 /* Asm'out a whole BTF_KIND_DATASEC, including its variable entries. */
1029 static void
1030 btf_asm_datasec_type (ctf_container_ref ctfc, btf_datasec_t ds, ctf_id_t id,
1031 size_t stroffset)
1033 dw2_asm_output_data (4, ds.name_offset + stroffset,
1034 "TYPE %" PRIu64 " BTF_KIND_DATASEC '%s'",
1035 btf_absolute_datasec_id (id), ds.name);
1036 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_DATASEC, 0,
1037 ds.entries.length ()),
1038 "btt_info: n_entries=%u", ds.entries.length ());
1039 /* Note: the "total section size in bytes" is emitted as 0 and patched by
1040 loaders such as libbpf. */
1041 dw2_asm_output_data (4, 0, "btt_size");
1042 for (size_t i = 0; i < ds.entries.length (); i++)
1043 btf_asm_datasec_entry (ctfc, ds.entries[i]);
1046 /* Compute and output the header information for a .BTF section. */
1048 static void
1049 output_btf_header (ctf_container_ref ctfc)
1051 switch_to_section (btf_info_section);
1052 ASM_OUTPUT_LABEL (asm_out_file, btf_info_section_label);
1054 /* BTF magic number, version, flags, and header length. */
1055 dw2_asm_output_data (2, BTF_MAGIC, "btf_magic");
1056 dw2_asm_output_data (1, BTF_VERSION, "btf_version");
1057 dw2_asm_output_data (1, 0, "btf_flags");
1058 dw2_asm_output_data (4, sizeof (struct btf_header), "btf_hdr_len");
1060 uint32_t type_off = 0, type_len = 0;
1061 uint32_t str_off = 0, str_len = 0;
1062 uint32_t datasec_vlen_bytes = 0;
1064 if (!ctfc_is_empty_container (ctfc))
1066 for (size_t i = 0; i < datasecs.length (); i++)
1068 datasec_vlen_bytes += ((datasecs[i].entries.length ())
1069 * sizeof (struct btf_var_secinfo));
1072 /* Total length (bytes) of the types section. */
1073 type_len = (num_types_added * sizeof (struct btf_type))
1074 + (num_types_created * sizeof (struct btf_type))
1075 + datasec_vlen_bytes
1076 + ctfc->ctfc_num_vlen_bytes;
1078 str_off = type_off + type_len;
1080 str_len = ctfc->ctfc_strtable.ctstab_len
1081 + ctfc->ctfc_aux_strtable.ctstab_len;
1084 /* Offset of type section. */
1085 dw2_asm_output_data (4, type_off, "type_off");
1086 /* Length of type section in bytes. */
1087 dw2_asm_output_data (4, type_len, "type_len");
1088 /* Offset of string section. */
1089 dw2_asm_output_data (4, str_off, "str_off");
1090 /* Length of string section in bytes. */
1091 dw2_asm_output_data (4, str_len, "str_len");
1094 /* Output all BTF_KIND_VARs in CTFC. */
1096 static void
1097 output_btf_vars (ctf_container_ref ctfc)
1099 size_t i;
1100 size_t num_ctf_vars = num_vars_added;
1101 if (num_ctf_vars)
1103 for (i = 0; i < num_ctf_vars; i++)
1104 btf_asm_varent (ctfc, ctfc->ctfc_vars_list[i]);
1108 /* Output BTF string records. The BTF strings section is a concatenation
1109 of the standard and auxilliary string tables in the ctf container. */
1111 static void
1112 output_btf_strs (ctf_container_ref ctfc)
1114 ctf_string_t * ctf_string = ctfc->ctfc_strtable.ctstab_head;
1115 static int str_pos = 0;
1117 while (ctf_string)
1119 dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_string, str_pos = 0x%x", str_pos);
1120 str_pos += strlen(ctf_string->cts_str) + 1;
1121 ctf_string = ctf_string->cts_next;
1124 ctf_string = ctfc->ctfc_aux_strtable.ctstab_head;
1125 while (ctf_string)
1127 dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_aux_string, str_pos = 0x%x", str_pos);
1128 str_pos += strlen(ctf_string->cts_str) + 1;
1129 ctf_string = ctf_string->cts_next;
1133 /* Output all (representable) members of a BTF_KIND_STRUCT or
1134 BTF_KIND_UNION type. */
1136 static void
1137 output_asm_btf_sou_fields (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
1139 ctf_dmdef_t * dmd;
1141 unsigned idx = 0;
1142 for (dmd = dtd->dtd_u.dtu_members;
1143 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
1145 btf_asm_sou_member (ctfc, dmd, idx);
1146 idx++;
1150 /* Output all enumerator constants following a BTF_KIND_ENUM{,64}. */
1152 static void
1153 output_asm_btf_enum_list (ctf_container_ref ARG_UNUSED (ctfc),
1154 ctf_dtdef_ref dtd)
1156 ctf_dmdef_t * dmd;
1158 unsigned idx = 0;
1159 for (dmd = dtd->dtd_u.dtu_members;
1160 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
1162 btf_asm_enum_const (dtd->dtd_data.ctti_size, dmd, idx);
1163 idx++;
1167 /* Output all function arguments following a BTF_KIND_FUNC_PROTO. */
1169 static void
1170 output_asm_btf_func_args_list (ctf_container_ref ctfc,
1171 ctf_dtdef_ref dtd)
1173 size_t farg_name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
1174 ctf_func_arg_t * farg;
1175 for (farg = dtd->dtd_u.dtu_argv;
1176 farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
1177 btf_asm_func_arg (ctfc, farg, farg_name_offset);
1180 /* Output the variable portion of a BTF type record. The information depends
1181 on the kind of the type. */
1183 static void
1184 output_asm_btf_vlen_bytes (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
1186 uint32_t btf_kind, encoding;
1188 btf_kind = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
1190 if (btf_kind == BTF_KIND_UNKN)
1191 return;
1193 switch (btf_kind)
1195 case BTF_KIND_INT:
1196 /* Redundant definitions of void may still be hanging around in the type
1197 list as size 0 integers. Skip emitting them. */
1198 if (dtd->dtd_data.ctti_size < 1)
1199 break;
1201 /* In BTF the CHAR `encoding' seems to not be used, so clear it
1202 here. */
1203 dtd->dtd_u.dtu_enc.cte_format &= ~BTF_INT_CHAR;
1205 encoding = BTF_INT_DATA (dtd->dtd_u.dtu_enc.cte_format,
1206 dtd->dtd_u.dtu_enc.cte_offset,
1207 dtd->dtd_u.dtu_enc.cte_bits);
1209 dw2_asm_output_data (4, encoding, "bti_encoding");
1210 break;
1212 case BTF_KIND_ARRAY:
1213 btf_asm_array (ctfc, dtd->dtd_u.dtu_arr);
1214 break;
1216 case BTF_KIND_STRUCT:
1217 case BTF_KIND_UNION:
1218 output_asm_btf_sou_fields (ctfc, dtd);
1219 break;
1221 case BTF_KIND_ENUM:
1222 output_asm_btf_enum_list (ctfc, dtd);
1223 break;
1225 case BTF_KIND_FUNC_PROTO:
1226 output_asm_btf_func_args_list (ctfc, dtd);
1227 break;
1229 case BTF_KIND_VAR:
1230 /* BTF Variables are handled by output_btf_vars and btf_asm_varent.
1231 There should be no BTF_KIND_VAR types at this point. */
1232 gcc_unreachable ();
1234 case BTF_KIND_DATASEC:
1235 /* The BTF_KIND_DATASEC records are handled by output_btf_datasec_types
1236 and btf_asm_datasec_type. There should be no BTF_KIND_DATASEC types
1237 at this point. */
1238 gcc_unreachable ();
1240 default:
1241 /* All other BTF type kinds have no variable length data. */
1242 break;
1246 /* Output a whole BTF type record for TYPE, including the fixed and variable
1247 data portions. */
1249 static void
1250 output_asm_btf_type (ctf_container_ref ctfc, ctf_dtdef_ref type)
1252 if (btf_emit_id_p (type->dtd_type))
1254 btf_asm_type (ctfc, type);
1255 output_asm_btf_vlen_bytes (ctfc, type);
1259 /* Output all BTF types in the container. This does not include synthesized
1260 types: BTF_KIND_VAR, BTF_KIND_FUNC, nor BTF_KIND_DATASEC. */
1262 static void
1263 output_btf_types (ctf_container_ref ctfc)
1265 size_t i;
1266 size_t num_types = ctfc->ctfc_types->elements ();
1267 if (num_types)
1269 for (i = 1; i <= num_types; i++)
1270 output_asm_btf_type (ctfc, ctfc->ctfc_types_list[i]);
1274 /* Output all BTF_KIND_FUNC type records. */
1276 static void
1277 output_btf_func_types (ctf_container_ref ctfc)
1279 for (size_t i = 0; i < vec_safe_length (funcs); i++)
1280 btf_asm_func_type (ctfc, (*funcs)[i], i);
1283 /* Output all BTF_KIND_DATASEC records. */
1285 static void
1286 output_btf_datasec_types (ctf_container_ref ctfc)
1288 size_t name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
1290 for (size_t i = 0; i < datasecs.length(); i++)
1291 btf_asm_datasec_type (ctfc, datasecs[i], i, name_offset);
1294 /* Postprocess the CTF debug data post initialization.
1296 During the postprocess pass:
1298 - Prepare the sorted list of BTF types.
1300 The sorted list of BTF types is, firstly, used for lookup (during the BTF
1301 generation process) of CTF/BTF types given a typeID.
1303 Secondly, in the emitted BTF section, BTF Types need to be in the sorted
1304 order of their type IDs. The BTF types section is viewed as an array,
1305 with type IDs used to index into that array. It is essential that every
1306 type be placed at the exact index corresponding to its ID, or else
1307 references to that type from other types will no longer be correct.
1309 - References to void types are converted to reference BTF_VOID_TYPEID. In
1310 CTF, a distinct type is used to encode void.
1312 - Bitfield struct/union members are converted to BTF encoding. CTF uses
1313 slices to encode bitfields, but BTF does not have slices and encodes
1314 bitfield information directly in the variable-length btf_member
1315 descriptions following the struct or union type.
1317 - Unrepresentable types are removed. We cannot have any invalid BTF types
1318 appearing in the output so they must be removed, and type ids of other
1319 types and references adjust accordingly. This also involves ensuring that
1320 BTF descriptions of struct members referring to unrepresentable types are
1321 not emitted, as they would be nonsensical.
1323 - Adjust inner- and inter-type references-by-ID to account for removed
1324 types, and construct the types list. */
1326 void
1327 btf_init_postprocess (void)
1329 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1331 holes.create (0);
1332 voids.create (0);
1334 num_types_added = 0;
1335 num_types_created = 0;
1337 /* Workaround for 'const void' variables. These variables are sometimes used
1338 in eBPF programs to address kernel symbols. DWARF does not generate const
1339 qualifier on void type, so we would incorrectly emit these variables
1340 without the const qualifier.
1341 Unfortunately we need the TREE node to know it was const, and we need
1342 to create the const modifier type (if needed) now, before making the types
1343 list. So we can't avoid iterating with FOR_EACH_VARIABLE here, and then
1344 again when creating the DATASEC entries. */
1345 ctf_id_t constvoid_id = CTF_NULL_TYPEID;
1346 varpool_node *var;
1347 FOR_EACH_VARIABLE (var)
1349 if (!var->decl)
1350 continue;
1352 tree type = TREE_TYPE (var->decl);
1353 if (type && VOID_TYPE_P (type) && TYPE_READONLY (type))
1355 dw_die_ref die = lookup_decl_die (var->decl);
1356 if (die == NULL)
1357 continue;
1359 ctf_dvdef_ref dvd = ctf_dvd_lookup (tu_ctfc, die);
1360 if (dvd == NULL)
1361 continue;
1363 /* Create the 'const' modifier type for void. */
1364 if (constvoid_id == CTF_NULL_TYPEID)
1365 constvoid_id = ctf_add_reftype (tu_ctfc, CTF_ADD_ROOT,
1366 dvd->dvd_type, CTF_K_CONST, NULL);
1367 dvd->dvd_type = constvoid_id;
1371 size_t i;
1372 size_t num_ctf_types = tu_ctfc->ctfc_types->elements ();
1374 if (num_ctf_types)
1376 init_btf_id_map (num_ctf_types + 1);
1378 /* Allocate the types list and traverse all types, placing each type
1379 at the index according to its ID. Add 1 because type ID 0 always
1380 represents VOID. */
1381 tu_ctfc->ctfc_types_list
1382 = ggc_vec_alloc<ctf_dtdef_ref>(num_ctf_types + 1);
1383 tu_ctfc->ctfc_types->traverse<ctf_container_ref, btf_dtd_postprocess_cb>
1384 (tu_ctfc);
1386 /* Build mapping of CTF type ID -> BTF type ID, and count total number
1387 of valid BTF types added. */
1388 for (i = 1; i <= num_ctf_types; i++)
1390 ctf_dtdef_ref dtd = tu_ctfc->ctfc_types_list[i];
1391 ctf_id_t btfid = btf_adjust_type_id (dtd->dtd_type);
1392 set_btf_id (dtd->dtd_type, btfid);
1393 if (btfid < BTF_MAX_TYPE && (btfid != BTF_VOID_TYPEID))
1394 num_types_added ++;
1399 /* Process and output all BTF data. Entry point of btfout. */
1401 void
1402 btf_output (const char * filename)
1404 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1406 init_btf_sections ();
1408 datasecs.create (0);
1409 vec_alloc (funcs, 16);
1411 ctf_add_cuname (tu_ctfc, filename);
1413 btf_emit_preprocess (tu_ctfc);
1415 output_btf_header (tu_ctfc);
1416 output_btf_types (tu_ctfc);
1417 output_btf_vars (tu_ctfc);
1418 output_btf_func_types (tu_ctfc);
1419 output_btf_datasec_types (tu_ctfc);
1420 output_btf_strs (tu_ctfc);
1423 /* Reset all state for BTF generation so that we can rerun the compiler within
1424 the same process. */
1426 void
1427 btf_finalize (void)
1429 btf_info_section = NULL;
1431 /* Clear preprocessing state. */
1432 num_vars_added = 0;
1433 num_types_added = 0;
1434 num_types_created = 0;
1436 holes.release ();
1437 voids.release ();
1438 for (size_t i = 0; i < datasecs.length (); i++)
1439 datasecs[i].entries.release ();
1440 datasecs.release ();
1442 funcs = NULL;
1444 btf_var_ids->empty ();
1445 btf_var_ids = NULL;
1447 free (btf_id_map);
1448 btf_id_map = NULL;
1450 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1451 ctfc_delete_container (tu_ctfc);
1452 tu_ctfc = NULL;
1455 #include "gt-btfout.h"