libgo: bump major version
[official-gcc.git] / gcc / btfout.cc
blobaef9fd70a283c2a2804c8e2c1ab5489600e562d1
1 /* Output BTF format from GCC.
2 Copyright (C) 2021-2022 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 /* Map a CTF type kind to the corresponding BTF type kind. */
119 static uint32_t
120 get_btf_kind (uint32_t ctf_kind)
122 /* N.B. the values encoding kinds are not in general the same for the
123 same kind between CTF and BTF. e.g. CTF_K_CONST != BTF_KIND_CONST. */
124 switch (ctf_kind)
126 case CTF_K_INTEGER: return BTF_KIND_INT;
127 case CTF_K_FLOAT: return BTF_KIND_FLOAT;
128 case CTF_K_POINTER: return BTF_KIND_PTR;
129 case CTF_K_ARRAY: return BTF_KIND_ARRAY;
130 case CTF_K_FUNCTION: return BTF_KIND_FUNC_PROTO;
131 case CTF_K_STRUCT: return BTF_KIND_STRUCT;
132 case CTF_K_UNION: return BTF_KIND_UNION;
133 case CTF_K_ENUM: return BTF_KIND_ENUM;
134 case CTF_K_FORWARD: return BTF_KIND_FWD;
135 case CTF_K_TYPEDEF: return BTF_KIND_TYPEDEF;
136 case CTF_K_VOLATILE: return BTF_KIND_VOLATILE;
137 case CTF_K_CONST: return BTF_KIND_CONST;
138 case CTF_K_RESTRICT: return BTF_KIND_RESTRICT;
139 default:;
141 return BTF_KIND_UNKN;
144 /* Allocate the btf_id_map, and initialize elements to BTF_INVALID_TYPEID. */
146 static void
147 init_btf_id_map (size_t len)
149 btf_id_map = XNEWVEC (ctf_id_t, len);
151 btf_id_map[0] = BTF_VOID_TYPEID;
152 for (size_t i = 1; i < len; i++)
153 btf_id_map[i] = BTF_INVALID_TYPEID;
156 /* Return the BTF type ID of CTF type ID KEY, or BTF_INVALID_TYPEID if the CTF
157 type with ID KEY does not map to a BTF type. */
159 ctf_id_t
160 get_btf_id (ctf_id_t key)
162 return btf_id_map[key];
165 /* Set the CTF type ID KEY to map to BTF type ID VAL. */
167 static inline void
168 set_btf_id (ctf_id_t key, ctf_id_t val)
170 btf_id_map[key] = val;
173 /* Return TRUE iff the given CTF type ID maps to a BTF type which will
174 be emitted. */
175 static inline bool
176 btf_emit_id_p (ctf_id_t id)
178 return ((btf_id_map[id] != BTF_VOID_TYPEID)
179 && (btf_id_map[id] <= BTF_MAX_TYPE));
182 /* Each BTF type can be followed additional, variable-length information
183 completing the description of the type. Calculate the number of bytes
184 of variable information required to encode a given type. */
186 static uint64_t
187 btf_calc_num_vbytes (ctf_dtdef_ref dtd)
189 uint64_t vlen_bytes = 0;
191 uint32_t kind = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
192 uint32_t vlen = CTF_V2_INFO_VLEN (dtd->dtd_data.ctti_info);
194 switch (kind)
196 case BTF_KIND_UNKN:
197 case BTF_KIND_PTR:
198 case BTF_KIND_FWD:
199 case BTF_KIND_TYPEDEF:
200 case BTF_KIND_VOLATILE:
201 case BTF_KIND_CONST:
202 case BTF_KIND_RESTRICT:
203 case BTF_KIND_FUNC:
204 /* These kinds have no vlen data. */
205 break;
207 case BTF_KIND_INT:
208 /* Size 0 integers represent redundant definitions of void that will
209 not be emitted. Don't allocate space for them. */
210 if (dtd->dtd_data.ctti_size == 0)
211 break;
213 vlen_bytes += sizeof (uint32_t);
214 break;
216 case BTF_KIND_ARRAY:
217 vlen_bytes += sizeof (struct btf_array);
218 break;
220 case BTF_KIND_STRUCT:
221 case BTF_KIND_UNION:
222 vlen_bytes += vlen * sizeof (struct btf_member);
223 break;
225 case BTF_KIND_ENUM:
226 vlen_bytes += (dtd->dtd_data.ctti_size == 0x8)
227 ? vlen * sizeof (struct btf_enum64)
228 : vlen * sizeof (struct btf_enum);
229 break;
231 case BTF_KIND_FUNC_PROTO:
232 vlen_bytes += vlen * sizeof (struct btf_param);
233 break;
235 case BTF_KIND_VAR:
236 vlen_bytes += sizeof (struct btf_var);
237 break;
239 case BTF_KIND_DATASEC:
240 vlen_bytes += vlen * sizeof (struct btf_var_secinfo);
241 break;
243 default:
244 break;
246 return vlen_bytes;
249 /* Initialize BTF section (.BTF) for output. */
251 void
252 init_btf_sections (void)
254 btf_info_section = get_section (BTF_INFO_SECTION_NAME, BTF_INFO_SECTION_FLAGS,
255 NULL);
257 ASM_GENERATE_INTERNAL_LABEL (btf_info_section_label,
258 BTF_INFO_SECTION_LABEL, btf_label_num++);
261 /* Push a BTF datasec variable entry INFO into the datasec named SECNAME,
262 creating the datasec if it does not already exist. */
264 static void
265 btf_datasec_push_entry (ctf_container_ref ctfc, const char *secname,
266 struct btf_var_secinfo info)
268 if (secname == NULL)
269 return;
271 for (size_t i = 0; i < datasecs.length (); i++)
272 if (strcmp (datasecs[i].name, secname) == 0)
274 datasecs[i].entries.safe_push (info);
275 return;
278 /* If we don't already have a datasec record for secname, make one. */
280 uint32_t str_off;
281 ctf_add_string (ctfc, secname, &str_off, CTF_AUX_STRTAB);
282 if (strcmp (secname, ""))
283 ctfc->ctfc_aux_strlen += strlen (secname) + 1;
285 btf_datasec_t ds;
286 ds.name = secname;
287 ds.name_offset = str_off;
289 ds.entries.create (0);
290 ds.entries.safe_push (info);
292 datasecs.safe_push (ds);
293 num_types_created++;
296 /* Construct all BTF_KIND_DATASEC records for CTFC. One such record is created
297 for each non-empty data-containing section in the output. Each record is
298 followed by a variable number of entries describing the variables stored
299 in that section. */
301 static void
302 btf_collect_datasec (ctf_container_ref ctfc)
304 /* See cgraph.h struct symtab_node, which varpool_node extends. */
305 varpool_node *node;
306 FOR_EACH_VARIABLE (node)
308 dw_die_ref die = lookup_decl_die (node->decl);
309 if (die == NULL)
310 continue;
312 ctf_dvdef_ref dvd = ctf_dvd_lookup (ctfc, die);
313 if (dvd == NULL)
314 continue;
316 const char *section_name = node->get_section ();
318 if (section_name == NULL)
320 switch (categorize_decl_for_section (node->decl, 0))
322 case SECCAT_BSS:
323 section_name = ".bss";
324 break;
325 case SECCAT_DATA:
326 section_name = ".data";
327 break;
328 case SECCAT_RODATA:
329 section_name = ".rodata";
330 break;
331 default:
332 continue;
336 struct btf_var_secinfo info;
338 info.type = 0;
339 unsigned int *var_id = btf_var_ids->get (dvd);
340 if (var_id)
341 /* +1 for the sentinel type not in the types map. */
342 info.type = *var_id + num_types_added + 1;
343 else
344 continue;
346 info.size = 0;
347 tree size = DECL_SIZE_UNIT (node->decl);
348 if (tree_fits_uhwi_p (size))
349 info.size = tree_to_uhwi (size);
351 /* Offset is left as 0 at compile time, to be filled in by loaders such
352 as libbpf. */
353 info.offset = 0;
355 btf_datasec_push_entry (ctfc, section_name, info);
359 /* Return true if the type ID is that of a type which will not be emitted (for
360 example, if it is not representable in BTF). */
362 static bool
363 btf_removed_type_p (ctf_id_t id)
365 return holes.contains (id);
368 /* Adjust the given type ID to account for holes and duplicate definitions of
369 void. */
371 static ctf_id_t
372 btf_adjust_type_id (ctf_id_t id)
374 size_t n;
375 ctf_id_t i = 0;
377 /* Do not adjust invalid type markers. */
378 if (id == BTF_INVALID_TYPEID)
379 return id;
381 for (n = 0; n < voids.length (); n++)
382 if (id == voids[n])
383 return BTF_VOID_TYPEID;
385 for (n = 0; n < holes.length (); n++)
387 if (holes[n] < id)
388 i++;
389 else if (holes[n] == id)
390 return BTF_VOID_TYPEID;
393 return id - i;
396 /* Postprocessing callback routine for types. */
399 btf_dtd_postprocess_cb (ctf_dtdef_ref *slot, ctf_container_ref arg_ctfc)
401 ctf_dtdef_ref ctftype = (ctf_dtdef_ref) * slot;
403 size_t index = ctftype->dtd_type;
404 gcc_assert (index <= arg_ctfc->ctfc_types->elements ());
406 uint32_t ctf_kind, btf_kind;
408 ctf_kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
409 btf_kind = get_btf_kind (ctf_kind);
411 if (btf_kind == BTF_KIND_UNKN)
412 /* This type is not representable in BTF. Create a hole. */
413 holes.safe_push (ctftype->dtd_type);
415 else if (btf_kind == BTF_KIND_INT && ctftype->dtd_data.ctti_size == 0)
417 /* This is a (redundant) definition of void. */
418 voids.safe_push (ctftype->dtd_type);
419 holes.safe_push (ctftype->dtd_type);
422 arg_ctfc->ctfc_types_list[index] = ctftype;
424 return 1;
427 /* Preprocessing callback routine for variables. */
430 btf_dvd_emit_preprocess_cb (ctf_dvdef_ref *slot, ctf_container_ref arg_ctfc)
432 ctf_dvdef_ref var = (ctf_dvdef_ref) * slot;
434 /* Do not add variables which refer to unsupported types. */
435 if (btf_removed_type_p (var->dvd_type))
436 return 1;
438 arg_ctfc->ctfc_vars_list[num_vars_added] = var;
439 btf_var_ids->put (var, num_vars_added);
441 num_vars_added++;
442 num_types_created++;
444 return 1;
447 /* Preprocessing callback routine for types. */
449 static void
450 btf_dtd_emit_preprocess_cb (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
452 if (!btf_emit_id_p (dtd->dtd_type))
453 return;
455 uint32_t btf_kind
456 = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
458 if (btf_kind == BTF_KIND_FUNC_PROTO)
460 /* Functions actually get two types: a BTF_KIND_FUNC_PROTO, and
461 also a BTF_KIND_FUNC. But the CTF container only allocates one
462 type per function, which matches closely with BTF_KIND_FUNC_PROTO.
463 For each such function, also allocate a BTF_KIND_FUNC entry.
464 These will be output later. */
465 ctf_dtdef_ref func_dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
466 func_dtd->dtd_data = dtd->dtd_data;
467 func_dtd->dtd_data.ctti_type = dtd->dtd_type;
468 func_dtd->linkage = dtd->linkage;
470 vec_safe_push (funcs, func_dtd);
471 num_types_created++;
473 /* Only the BTF_KIND_FUNC type actually references the name. The
474 BTF_KIND_FUNC_PROTO is always anonymous. */
475 dtd->dtd_data.ctti_name = 0;
478 ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (dtd);
481 /* Preprocess the CTF information to prepare for BTF output. BTF is almost a
482 subset of CTF, with many small differences in encoding, and lacking support
483 for some types (notably floating point formats).
485 During the preprocessing pass:
486 - Ascertain that the sorted list of types has been prepared. For the BTF
487 generation process, this is taken care of by the btf_init_postprocess ().
489 - BTF_KIND_FUNC and BTF_KIND_DATASEC records are constructed. These types do
490 not have analogues in CTF (the analogous type to CTF_K_FUNCTION is
491 BTF_KIND_FUNC_PROTO), but can be relatively easily deduced from CTF
492 information.
494 - Construct BTF_KIND_VAR records, representing variables.
496 - Calculate the total size in bytes of variable-length information following
497 BTF type records. This is used for outputting the BTF header.
499 After preprocessing, all BTF information is ready to be output:
500 - ctfc->ctfc_types_list holdstypes converted from CTF types. This does not
501 include KIND_VAR, KIND_FUNC, nor KIND_DATASEC types. These types have been
502 re-encoded to the appropriate representation in BTF.
503 - ctfc->ctfc_vars_list holds all variables which should be output.
504 Variables of unsupported types are not present in this list.
505 - Vector 'funcs' holds all BTF_KIND_FUNC types, one to match each
506 BTF_KIND_FUNC_PROTO.
507 - Vector 'datasecs' holds all BTF_KIND_DATASEC types. */
509 static void
510 btf_emit_preprocess (ctf_container_ref ctfc)
512 size_t num_ctf_types = ctfc->ctfc_types->elements ();
513 size_t num_ctf_vars = ctfc->ctfc_vars->elements ();
514 size_t i;
516 if (num_ctf_types)
518 gcc_assert (ctfc->ctfc_types_list);
519 /* Preprocess the types. */
520 for (i = 1; i <= num_ctf_types; i++)
521 btf_dtd_emit_preprocess_cb (ctfc, ctfc->ctfc_types_list[i]);
524 btf_var_ids = hash_map<ctf_dvdef_ref, unsigned int>::create_ggc (100);
526 if (num_ctf_vars)
528 /* Allocate and construct the list of variables. While BTF variables are
529 not distinct from types (in that variables are simply types with
530 BTF_KIND_VAR), it is simpler to maintain a separate list of variables
531 and append them to the types list during output. */
532 ctfc->ctfc_vars_list = ggc_vec_alloc<ctf_dvdef_ref>(num_ctf_vars);
533 ctfc->ctfc_vars->traverse<ctf_container_ref, btf_dvd_emit_preprocess_cb>
534 (ctfc);
536 ctfc->ctfc_num_vlen_bytes += (num_vars_added * sizeof (struct btf_var));
539 btf_collect_datasec (ctfc);
542 /* Return true iff DMD is a member description of a bit-field which can be
543 validly represented in BTF. */
545 static bool
546 btf_dmd_representable_bitfield_p (ctf_container_ref ctfc, ctf_dmdef_t *dmd)
548 ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[dmd->dmd_type];
550 if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
552 unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
553 unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
554 uint64_t sou_offset = dmd->dmd_offset;
556 if ((bits > 0xff) || ((sou_offset + word_offset) > 0xffffff))
557 return false;
559 return true;
562 return false;
565 /* BTF asm helper routines. */
567 /* Asm'out a BTF type. This routine is responsible for the bulk of the task
568 of converting CTF types to their BTF representation. */
570 static void
571 btf_asm_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
573 uint32_t btf_kind, btf_kflag, btf_vlen, btf_size_type;
574 uint32_t ctf_info = dtd->dtd_data.ctti_info;
576 btf_kind = get_btf_kind (CTF_V2_INFO_KIND (ctf_info));
577 btf_size_type = dtd->dtd_data.ctti_type;
578 btf_vlen = CTF_V2_INFO_VLEN (ctf_info);
580 /* By now any unrepresentable types have been removed. */
581 gcc_assert (btf_kind != BTF_KIND_UNKN);
583 /* Size 0 integers are redundant definitions of void. None should remain
584 in the types list by this point. */
585 gcc_assert (btf_kind != BTF_KIND_INT || btf_size_type >= 1);
587 /* Re-encode the ctti_info to BTF. */
588 /* kflag is 1 for structs/unions with a bitfield member.
589 kflag is 1 for forwards to unions.
590 kflag is 0 in all other cases. */
591 btf_kflag = 0;
593 if (btf_kind == BTF_KIND_STRUCT || btf_kind == BTF_KIND_UNION)
595 /* If a struct/union has ANY bitfield members, set kflag=1.
596 Note that we must also change the encoding of every member to encode
597 both member bitfield size (stealing most-significant 8 bits) and bit
598 offset (LS 24 bits). This is done during preprocessing. */
599 ctf_dmdef_t *dmd;
600 for (dmd = dtd->dtd_u.dtu_members;
601 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
603 /* Set kflag if this member is a representable bitfield. */
604 if (btf_dmd_representable_bitfield_p (ctfc, dmd))
605 btf_kflag = 1;
607 /* Struct members that refer to unsupported types or bitfield formats
608 shall be skipped. These are marked during preprocessing. */
609 else if (!btf_emit_id_p (dmd->dmd_type))
610 btf_vlen -= 1;
614 /* BTF forwards make use of KIND_FLAG to distinguish between forwards to
615 structs and forwards to unions. The dwarf2ctf conversion process stores
616 the kind of the forward in ctti_type, but for BTF this must be 0 for
617 forwards, with only the KIND_FLAG to distinguish.
618 At time of writing, BTF forwards to enums are unspecified. */
619 if (btf_kind == BTF_KIND_FWD)
621 if (dtd->dtd_data.ctti_type == CTF_K_UNION)
622 btf_kflag = 1;
624 btf_size_type = 0;
627 if (btf_kind == BTF_KIND_ENUM)
629 btf_kflag = dtd->dtd_enum_unsigned
630 ? BTF_KF_ENUM_UNSIGNED
631 : BTF_KF_ENUM_SIGNED;
632 if (dtd->dtd_data.ctti_size == 0x8)
633 btf_kind = BTF_KIND_ENUM64;
636 dw2_asm_output_data (4, dtd->dtd_data.ctti_name, "btt_name");
637 dw2_asm_output_data (4, BTF_TYPE_INFO (btf_kind, btf_kflag, btf_vlen),
638 "btt_info: kind=%u, kflag=%u, vlen=%u",
639 btf_kind, btf_kflag, btf_vlen);
640 switch (btf_kind)
642 case BTF_KIND_INT:
643 case BTF_KIND_FLOAT:
644 case BTF_KIND_STRUCT:
645 case BTF_KIND_UNION:
646 case BTF_KIND_ENUM:
647 case BTF_KIND_DATASEC:
648 case BTF_KIND_ENUM64:
649 dw2_asm_output_data (4, dtd->dtd_data.ctti_size, "btt_size: %uB",
650 dtd->dtd_data.ctti_size);
651 return;
652 default:
653 break;
656 dw2_asm_output_data (4, get_btf_id (dtd->dtd_data.ctti_type), "btt_type");
659 /* Asm'out the variable information following a BTF_KIND_ARRAY. */
661 static void
662 btf_asm_array (ctf_dtdef_ref dtd)
664 dw2_asm_output_data (4, get_btf_id (dtd->dtd_u.dtu_arr.ctr_contents),
665 "bta_contents");
666 dw2_asm_output_data (4, get_btf_id (dtd->dtd_u.dtu_arr.ctr_index),
667 "bta_index");
668 dw2_asm_output_data (4, dtd->dtd_u.dtu_arr.ctr_nelems, "bta_nelems");
671 /* Asm'out a BTF_KIND_VAR. */
673 static void
674 btf_asm_varent (ctf_dvdef_ref var)
676 dw2_asm_output_data (4, var->dvd_name_offset, "btv_name");
677 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_VAR, 0, 0), "btv_info");
678 dw2_asm_output_data (4, get_btf_id (var->dvd_type), "btv_type");
679 dw2_asm_output_data (4, (var->dvd_visibility ? 1 : 0), "btv_linkage");
682 /* Asm'out a member description following a BTF_KIND_STRUCT or
683 BTF_KIND_UNION. */
685 static void
686 btf_asm_sou_member (ctf_container_ref ctfc, ctf_dmdef_t * dmd)
688 ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[dmd->dmd_type];
690 /* Re-encode bitfields to BTF representation. */
691 if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
693 ctf_id_t base_type = ref_type->dtd_u.dtu_slice.cts_type;
694 unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
695 unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
696 uint64_t sou_offset = dmd->dmd_offset;
698 /* Pack the bit offset and bitfield size together. */
699 sou_offset += word_offset;
701 /* If this bitfield cannot be represented, do not output anything.
702 The parent struct/union 'vlen' field has already been updated. */
703 if ((bits > 0xff) || (sou_offset > 0xffffff))
704 return;
706 sou_offset &= 0x00ffffff;
707 sou_offset |= ((bits & 0xff) << 24);
709 /* Refer to the base type of the slice. */
710 dw2_asm_output_data (4, dmd->dmd_name_offset, "btm_name_off");
711 dw2_asm_output_data (4, get_btf_id (base_type), "btm_type");
712 dw2_asm_output_data (4, sou_offset, "btm_offset");
714 else
716 dw2_asm_output_data (4, dmd->dmd_name_offset, "btm_name_off");
717 dw2_asm_output_data (4, get_btf_id (dmd->dmd_type), "btm_type");
718 dw2_asm_output_data (4, dmd->dmd_offset, "btm_offset");
722 /* Asm'out an enum constant following a BTF_KIND_ENUM{,64}. */
724 static void
725 btf_asm_enum_const (unsigned int size, ctf_dmdef_t * dmd)
727 dw2_asm_output_data (4, dmd->dmd_name_offset, "bte_name");
728 if (size == 4)
729 dw2_asm_output_data (size, dmd->dmd_value, "bte_value");
730 else
732 dw2_asm_output_data (4, dmd->dmd_value & 0xffffffff, "bte_value_lo32");
733 dw2_asm_output_data (4, (dmd->dmd_value >> 32) & 0xffffffff, "bte_value_hi32");
737 /* Asm'out a function parameter description following a BTF_KIND_FUNC_PROTO. */
739 static void
740 btf_asm_func_arg (ctf_func_arg_t * farg, size_t stroffset)
742 /* If the function arg does not have a name, refer to the null string at
743 the start of the string table. This ensures correct encoding for varargs
744 '...' arguments. */
745 if ((farg->farg_name != NULL) && strcmp (farg->farg_name, ""))
746 dw2_asm_output_data (4, farg->farg_name_offset + stroffset, "farg_name");
747 else
748 dw2_asm_output_data (4, 0, "farg_name");
750 dw2_asm_output_data (4, (btf_removed_type_p (farg->farg_type)
751 ? BTF_VOID_TYPEID
752 : get_btf_id (farg->farg_type)),
753 "farg_type");
756 /* Asm'out a BTF_KIND_FUNC type. */
758 static void
759 btf_asm_func_type (ctf_dtdef_ref dtd)
761 dw2_asm_output_data (4, dtd->dtd_data.ctti_name, "btt_name");
762 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC, 0,
763 dtd->linkage),
764 "btt_info: kind=%u, kflag=%u, linkage=%u",
765 BTF_KIND_FUNC, 0, dtd->linkage);
766 dw2_asm_output_data (4, get_btf_id (dtd->dtd_data.ctti_type), "btt_type");
769 /* Asm'out a variable entry following a BTF_KIND_DATASEC. */
771 static void
772 btf_asm_datasec_entry (struct btf_var_secinfo info)
774 dw2_asm_output_data (4, info.type, "bts_type");
775 dw2_asm_output_data (4, info.offset, "bts_offset");
776 dw2_asm_output_data (4, info.size, "bts_size");
779 /* Asm'out a whole BTF_KIND_DATASEC, including its variable entries. */
781 static void
782 btf_asm_datasec_type (btf_datasec_t ds, size_t stroffset)
784 dw2_asm_output_data (4, ds.name_offset + stroffset, "btt_name");
785 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_DATASEC, 0,
786 ds.entries.length ()),
787 "btt_info");
788 /* Note: the "total section size in bytes" is emitted as 0 and patched by
789 loaders such as libbpf. */
790 dw2_asm_output_data (4, 0, "btt_size");
791 for (size_t i = 0; i < ds.entries.length (); i++)
792 btf_asm_datasec_entry (ds.entries[i]);
795 /* Compute and output the header information for a .BTF section. */
797 static void
798 output_btf_header (ctf_container_ref ctfc)
800 switch_to_section (btf_info_section);
801 ASM_OUTPUT_LABEL (asm_out_file, btf_info_section_label);
803 /* BTF magic number, version, flags, and header length. */
804 dw2_asm_output_data (2, BTF_MAGIC, "btf_magic");
805 dw2_asm_output_data (1, BTF_VERSION, "btf_version");
806 dw2_asm_output_data (1, 0, "btf_flags");
807 dw2_asm_output_data (4, sizeof (struct btf_header), "btf_hdr_len");
809 uint32_t type_off = 0, type_len = 0;
810 uint32_t str_off = 0, str_len = 0;
811 uint32_t datasec_vlen_bytes = 0;
813 if (!ctfc_is_empty_container (ctfc))
815 for (size_t i = 0; i < datasecs.length (); i++)
817 datasec_vlen_bytes += ((datasecs[i].entries.length ())
818 * sizeof (struct btf_var_secinfo));
821 /* Total length (bytes) of the types section. */
822 type_len = (num_types_added * sizeof (struct btf_type))
823 + (num_types_created * sizeof (struct btf_type))
824 + datasec_vlen_bytes
825 + ctfc->ctfc_num_vlen_bytes;
827 str_off = type_off + type_len;
829 str_len = ctfc->ctfc_strtable.ctstab_len
830 + ctfc->ctfc_aux_strtable.ctstab_len;
833 /* Offset of type section. */
834 dw2_asm_output_data (4, type_off, "type_off");
835 /* Length of type section in bytes. */
836 dw2_asm_output_data (4, type_len, "type_len");
837 /* Offset of string section. */
838 dw2_asm_output_data (4, str_off, "str_off");
839 /* Length of string section in bytes. */
840 dw2_asm_output_data (4, str_len, "str_len");
843 /* Output all BTF_KIND_VARs in CTFC. */
845 static void
846 output_btf_vars (ctf_container_ref ctfc)
848 size_t i;
849 size_t num_ctf_vars = num_vars_added;
850 if (num_ctf_vars)
852 for (i = 0; i < num_ctf_vars; i++)
853 btf_asm_varent (ctfc->ctfc_vars_list[i]);
857 /* Output BTF string records. The BTF strings section is a concatenation
858 of the standard and auxilliary string tables in the ctf container. */
860 static void
861 output_btf_strs (ctf_container_ref ctfc)
863 ctf_string_t * ctf_string = ctfc->ctfc_strtable.ctstab_head;
865 while (ctf_string)
867 dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_string");
868 ctf_string = ctf_string->cts_next;
871 ctf_string = ctfc->ctfc_aux_strtable.ctstab_head;
872 while (ctf_string)
874 dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_aux_string");
875 ctf_string = ctf_string->cts_next;
879 /* Output all (representable) members of a BTF_KIND_STRUCT or
880 BTF_KIND_UNION type. */
882 static void
883 output_asm_btf_sou_fields (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
885 ctf_dmdef_t * dmd;
887 for (dmd = dtd->dtd_u.dtu_members;
888 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
889 btf_asm_sou_member (ctfc, dmd);
892 /* Output all enumerator constants following a BTF_KIND_ENUM{,64}. */
894 static void
895 output_asm_btf_enum_list (ctf_container_ref ARG_UNUSED (ctfc),
896 ctf_dtdef_ref dtd)
898 ctf_dmdef_t * dmd;
900 for (dmd = dtd->dtd_u.dtu_members;
901 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
902 btf_asm_enum_const (dtd->dtd_data.ctti_size, dmd);
905 /* Output all function arguments following a BTF_KIND_FUNC_PROTO. */
907 static void
908 output_asm_btf_func_args_list (ctf_container_ref ctfc,
909 ctf_dtdef_ref dtd)
911 size_t farg_name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
912 ctf_func_arg_t * farg;
913 for (farg = dtd->dtd_u.dtu_argv;
914 farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
915 btf_asm_func_arg (farg, farg_name_offset);
918 /* Output the variable portion of a BTF type record. The information depends
919 on the kind of the type. */
921 static void
922 output_asm_btf_vlen_bytes (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
924 uint32_t btf_kind, encoding;
926 btf_kind = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
928 if (btf_kind == BTF_KIND_UNKN)
929 return;
931 switch (btf_kind)
933 case BTF_KIND_INT:
934 /* Redundant definitions of void may still be hanging around in the type
935 list as size 0 integers. Skip emitting them. */
936 if (dtd->dtd_data.ctti_size < 1)
937 break;
939 /* In BTF the CHAR `encoding' seems to not be used, so clear it
940 here. */
941 dtd->dtd_u.dtu_enc.cte_format &= ~BTF_INT_CHAR;
943 encoding = BTF_INT_DATA (dtd->dtd_u.dtu_enc.cte_format,
944 dtd->dtd_u.dtu_enc.cte_offset,
945 dtd->dtd_u.dtu_enc.cte_bits);
947 dw2_asm_output_data (4, encoding, "bti_encoding");
948 break;
950 case BTF_KIND_ARRAY:
951 btf_asm_array (dtd);
952 break;
954 case BTF_KIND_STRUCT:
955 case BTF_KIND_UNION:
956 output_asm_btf_sou_fields (ctfc, dtd);
957 break;
959 case BTF_KIND_ENUM:
960 output_asm_btf_enum_list (ctfc, dtd);
961 break;
963 case BTF_KIND_FUNC_PROTO:
964 output_asm_btf_func_args_list (ctfc, dtd);
965 break;
967 case BTF_KIND_VAR:
968 /* BTF Variables are handled by output_btf_vars and btf_asm_varent.
969 There should be no BTF_KIND_VAR types at this point. */
970 gcc_unreachable ();
972 case BTF_KIND_DATASEC:
973 /* The BTF_KIND_DATASEC records are handled by output_btf_datasec_types
974 and btf_asm_datasec_type. There should be no BTF_KIND_DATASEC types
975 at this point. */
976 gcc_unreachable ();
978 default:
979 /* All other BTF type kinds have no variable length data. */
980 break;
984 /* Output a whole BTF type record for TYPE, including the fixed and variable
985 data portions. */
987 static void
988 output_asm_btf_type (ctf_container_ref ctfc, ctf_dtdef_ref type)
990 if (btf_emit_id_p (type->dtd_type))
992 btf_asm_type (ctfc, type);
993 output_asm_btf_vlen_bytes (ctfc, type);
997 /* Output all BTF types in the container. This does not include synthesized
998 types: BTF_KIND_VAR, BTF_KIND_FUNC, nor BTF_KIND_DATASEC. */
1000 static void
1001 output_btf_types (ctf_container_ref ctfc)
1003 size_t i;
1004 size_t num_types = ctfc->ctfc_types->elements ();
1005 if (num_types)
1007 for (i = 1; i <= num_types; i++)
1008 output_asm_btf_type (ctfc, ctfc->ctfc_types_list[i]);
1012 /* Output all BTF_KIND_FUNC type records. */
1014 static void
1015 output_btf_func_types (void)
1017 for (size_t i = 0; i < vec_safe_length (funcs); i++)
1018 btf_asm_func_type ((*funcs)[i]);
1021 /* Output all BTF_KIND_DATASEC records. */
1023 static void
1024 output_btf_datasec_types (ctf_container_ref ctfc)
1026 size_t name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
1028 for (size_t i = 0; i < datasecs.length(); i++)
1029 btf_asm_datasec_type (datasecs[i], name_offset);
1032 /* Postprocess the CTF debug data post initialization.
1034 During the postprocess pass:
1036 - Prepare the sorted list of BTF types.
1038 The sorted list of BTF types is, firstly, used for lookup (during the BTF
1039 generation process) of CTF/BTF types given a typeID.
1041 Secondly, in the emitted BTF section, BTF Types need to be in the sorted
1042 order of their type IDs. The BTF types section is viewed as an array,
1043 with type IDs used to index into that array. It is essential that every
1044 type be placed at the exact index corresponding to its ID, or else
1045 references to that type from other types will no longer be correct.
1047 - References to void types are converted to reference BTF_VOID_TYPEID. In
1048 CTF, a distinct type is used to encode void.
1050 - Bitfield struct/union members are converted to BTF encoding. CTF uses
1051 slices to encode bitfields, but BTF does not have slices and encodes
1052 bitfield information directly in the variable-length btf_member
1053 descriptions following the struct or union type.
1055 - Unrepresentable types are removed. We cannot have any invalid BTF types
1056 appearing in the output so they must be removed, and type ids of other
1057 types and references adjust accordingly. This also involves ensuring that
1058 BTF descriptions of struct members referring to unrepresentable types are
1059 not emitted, as they would be nonsensical.
1061 - Adjust inner- and inter-type references-by-ID to account for removed
1062 types, and construct the types list. */
1064 void
1065 btf_init_postprocess (void)
1067 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1069 size_t i;
1070 size_t num_ctf_types = tu_ctfc->ctfc_types->elements ();
1072 holes.create (0);
1073 voids.create (0);
1075 num_types_added = 0;
1076 num_types_created = 0;
1078 if (num_ctf_types)
1080 init_btf_id_map (num_ctf_types + 1);
1082 /* Allocate the types list and traverse all types, placing each type
1083 at the index according to its ID. Add 1 because type ID 0 always
1084 represents VOID. */
1085 tu_ctfc->ctfc_types_list
1086 = ggc_vec_alloc<ctf_dtdef_ref>(num_ctf_types + 1);
1087 tu_ctfc->ctfc_types->traverse<ctf_container_ref, btf_dtd_postprocess_cb>
1088 (tu_ctfc);
1090 /* Build mapping of CTF type ID -> BTF type ID, and count total number
1091 of valid BTF types added. */
1092 for (i = 1; i <= num_ctf_types; i++)
1094 ctf_dtdef_ref dtd = tu_ctfc->ctfc_types_list[i];
1095 ctf_id_t btfid = btf_adjust_type_id (dtd->dtd_type);
1096 set_btf_id (dtd->dtd_type, btfid);
1097 if (btfid < BTF_MAX_TYPE && (btfid != BTF_VOID_TYPEID))
1098 num_types_added ++;
1103 /* Process and output all BTF data. Entry point of btfout. */
1105 void
1106 btf_output (const char * filename)
1108 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1110 init_btf_sections ();
1112 datasecs.create (0);
1113 vec_alloc (funcs, 16);
1115 ctf_add_cuname (tu_ctfc, filename);
1117 btf_emit_preprocess (tu_ctfc);
1119 output_btf_header (tu_ctfc);
1120 output_btf_types (tu_ctfc);
1121 output_btf_vars (tu_ctfc);
1122 output_btf_func_types ();
1123 output_btf_datasec_types (tu_ctfc);
1124 output_btf_strs (tu_ctfc);
1127 /* Reset all state for BTF generation so that we can rerun the compiler within
1128 the same process. */
1130 void
1131 btf_finalize (void)
1133 btf_info_section = NULL;
1135 /* Clear preprocessing state. */
1136 num_vars_added = 0;
1137 num_types_added = 0;
1138 num_types_created = 0;
1140 holes.release ();
1141 voids.release ();
1142 for (size_t i = 0; i < datasecs.length (); i++)
1143 datasecs[i].entries.release ();
1144 datasecs.release ();
1146 funcs = NULL;
1148 btf_var_ids->empty ();
1149 btf_var_ids = NULL;
1151 free (btf_id_map);
1152 btf_id_map = NULL;
1154 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1155 ctfc_delete_container (tu_ctfc);
1156 tu_ctfc = NULL;
1159 #include "gt-btfout.h"