1 /* Output BTF format from GCC.
2 Copyright (C) 2021 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
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
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.c, and the same CTF container objects are
27 #include "coretypes.h"
31 #include "dwarf2asm.h"
34 #include "diagnostic-core.h"
37 #include "dwarf2out.h" /* For lookup_decl_die. */
39 static int btf_label_num
;
41 static GTY (()) section
* btf_info_section
;
43 /* BTF debug info section. */
45 #ifndef BTF_INFO_SECTION_NAME
46 #define BTF_INFO_SECTION_NAME ".BTF"
49 #define BTF_INFO_SECTION_FLAGS (SECTION_DEBUG)
51 /* Maximum size (in bytes) for an artifically generated BTF label. */
53 #define MAX_BTF_LABEL_BYTES 40
55 static char btf_info_section_label
[MAX_BTF_LABEL_BYTES
];
57 #ifndef BTF_INFO_SECTION_LABEL
58 #define BTF_INFO_SECTION_LABEL "Lbtf"
61 /* BTF encodes void as type id 0. */
63 #define BTF_VOID_TYPEID 0
64 #define BTF_INIT_TYPEID 1
66 #define BTF_INVALID_TYPEID 0xFFFFFFFF
68 /* Mapping of CTF variables to the IDs they will be assigned when they are
69 converted to BTF_KIND_VAR type records. Strictly accounts for the index
70 from the start of the variable type entries, does not include the number
71 of types emitted prior to the variable records. */
72 static hash_map
<ctf_dvdef_ref
, unsigned int> *btf_var_ids
;
74 /* Mapping of type IDs from original CTF ID to BTF ID. Types do not map
75 1-to-1 from CTF to BTF. To avoid polluting the CTF container when updating
76 type references-by-ID, we use this map instead. */
77 static ctf_id_t
* btf_id_map
= NULL
;
79 /* Information for creating the BTF_KIND_DATASEC records. */
80 typedef struct btf_datasec
82 const char *name
; /* Section name, e.g. ".bss". */
83 uint32_t name_offset
; /* Offset to name in string table. */
84 vec
<struct btf_var_secinfo
> entries
; /* Variable entries in this section. */
87 /* One BTF_KIND_DATASEC record is created for each output data section which
88 will hold at least one variable. */
89 static vec
<btf_datasec_t
> datasecs
;
91 /* Holes occur for types which are present in the CTF container, but are either
92 non-representable or redundant in BTF. */
93 static vec
<ctf_id_t
> holes
;
95 /* CTF definition(s) of void. Only one definition of void should be generated.
96 We should not encounter more than one definition of void, but use a vector
98 static vec
<ctf_id_t
> voids
;
100 /* Functions in BTF have two separate type records - one for the prototype
101 (BTF_KIND_FUNC_PROTO), as well as a BTF_KIND_FUNC. CTF_K_FUNCTION types
102 map closely to BTF_KIND_FUNC_PROTO, but the BTF_KIND_FUNC records must be
103 created. This vector holds them. */
104 static GTY (()) vec
<ctf_dtdef_ref
, va_gc
> *funcs
;
106 /* The number of BTF variables added to the TU CTF container. */
107 static unsigned int num_vars_added
= 0;
109 /* The number of BTF types added to the TU CTF container. */
110 static unsigned int num_types_added
= 0;
112 /* The number of types synthesized for BTF that do not correspond to
114 static unsigned int num_types_created
= 0;
116 /* Map a CTF type kind to the corresponding BTF type kind. */
119 get_btf_kind (uint32_t ctf_kind
)
121 /* N.B. the values encoding kinds are not in general the same for the
122 same kind between CTF and BTF. e.g. CTF_K_CONST != BTF_KIND_CONST. */
125 case CTF_K_INTEGER
: return BTF_KIND_INT
;
126 case CTF_K_POINTER
: return BTF_KIND_PTR
;
127 case CTF_K_ARRAY
: return BTF_KIND_ARRAY
;
128 case CTF_K_FUNCTION
: return BTF_KIND_FUNC_PROTO
;
129 case CTF_K_STRUCT
: return BTF_KIND_STRUCT
;
130 case CTF_K_UNION
: return BTF_KIND_UNION
;
131 case CTF_K_ENUM
: return BTF_KIND_ENUM
;
132 case CTF_K_FORWARD
: return BTF_KIND_FWD
;
133 case CTF_K_TYPEDEF
: return BTF_KIND_TYPEDEF
;
134 case CTF_K_VOLATILE
: return BTF_KIND_VOLATILE
;
135 case CTF_K_CONST
: return BTF_KIND_CONST
;
136 case CTF_K_RESTRICT
: return BTF_KIND_RESTRICT
;
139 return BTF_KIND_UNKN
;
142 /* Allocate the btf_id_map, and initialize elements to BTF_INVALID_TYPEID. */
145 init_btf_id_map (size_t len
)
147 btf_id_map
= XNEWVEC (ctf_id_t
, len
);
149 btf_id_map
[0] = BTF_VOID_TYPEID
;
150 for (size_t i
= 1; i
< len
; i
++)
151 btf_id_map
[i
] = BTF_INVALID_TYPEID
;
154 /* Return the BTF type ID of CTF type ID KEY, or BTF_INVALID_TYPEID if the CTF
155 type with ID KEY does not map to a BTF type. */
157 static inline ctf_id_t
158 get_btf_id (ctf_id_t key
)
160 return btf_id_map
[key
];
163 /* Set the CTF type ID KEY to map to BTF type ID VAL. */
166 set_btf_id (ctf_id_t key
, ctf_id_t val
)
168 btf_id_map
[key
] = val
;
171 /* Return TRUE iff the given CTF type ID maps to a BTF type which will
174 btf_emit_id_p (ctf_id_t id
)
176 return ((btf_id_map
[id
] != BTF_VOID_TYPEID
)
177 && (btf_id_map
[id
] <= BTF_MAX_TYPE
));
180 /* Each BTF type can be followed additional, variable-length information
181 completing the description of the type. Calculate the number of bytes
182 of variable information required to encode a given type. */
185 btf_calc_num_vbytes (ctf_dtdef_ref dtd
)
187 uint64_t vlen_bytes
= 0;
189 uint32_t kind
= get_btf_kind (CTF_V2_INFO_KIND (dtd
->dtd_data
.ctti_info
));
190 uint32_t vlen
= CTF_V2_INFO_VLEN (dtd
->dtd_data
.ctti_info
);
197 case BTF_KIND_TYPEDEF
:
198 case BTF_KIND_VOLATILE
:
200 case BTF_KIND_RESTRICT
:
202 /* These kinds have no vlen data. */
206 /* Size 0 integers represent redundant definitions of void that will
207 not be emitted. Don't allocate space for them. */
208 if (dtd
->dtd_data
.ctti_size
== 0)
211 vlen_bytes
+= sizeof (uint32_t);
215 vlen_bytes
+= sizeof (struct btf_array
);
218 case BTF_KIND_STRUCT
:
220 vlen_bytes
+= vlen
* sizeof (struct btf_member
);
224 vlen_bytes
+= vlen
* sizeof (struct btf_enum
);
227 case BTF_KIND_FUNC_PROTO
:
228 vlen_bytes
+= vlen
* sizeof (struct btf_param
);
232 vlen_bytes
+= sizeof (struct btf_var
);
235 case BTF_KIND_DATASEC
:
236 vlen_bytes
+= vlen
* sizeof (struct btf_var_secinfo
);
245 /* Initialize BTF section (.BTF) for output. */
248 init_btf_sections (void)
250 btf_info_section
= get_section (BTF_INFO_SECTION_NAME
, BTF_INFO_SECTION_FLAGS
,
253 ASM_GENERATE_INTERNAL_LABEL (btf_info_section_label
,
254 BTF_INFO_SECTION_LABEL
, btf_label_num
++);
257 /* Push a BTF datasec variable entry INFO into the datasec named SECNAME,
258 creating the datasec if it does not already exist. */
261 btf_datasec_push_entry (ctf_container_ref ctfc
, const char *secname
,
262 struct btf_var_secinfo info
)
267 for (size_t i
= 0; i
< datasecs
.length (); i
++)
268 if (strcmp (datasecs
[i
].name
, secname
) == 0)
270 datasecs
[i
].entries
.safe_push (info
);
274 /* If we don't already have a datasec record for secname, make one. */
277 ctf_add_string (ctfc
, secname
, &str_off
, CTF_AUX_STRTAB
);
278 if (strcmp (secname
, ""))
279 ctfc
->ctfc_aux_strlen
+= strlen (secname
) + 1;
283 ds
.name_offset
= str_off
;
285 ds
.entries
.create (0);
286 ds
.entries
.safe_push (info
);
288 datasecs
.safe_push (ds
);
292 /* Construct all BTF_KIND_DATASEC records for CTFC. One such record is created
293 for each non-empty data-containing section in the output. Each record is
294 followed by a variable number of entries describing the variables stored
298 btf_collect_datasec (ctf_container_ref ctfc
)
300 /* See cgraph.h struct symtab_node, which varpool_node extends. */
302 FOR_EACH_VARIABLE (node
)
304 dw_die_ref die
= lookup_decl_die (node
->decl
);
308 ctf_dvdef_ref dvd
= ctf_dvd_lookup (ctfc
, die
);
312 const char *section_name
= node
->get_section ();
314 if (section_name
== NULL
)
316 switch (categorize_decl_for_section (node
->decl
, 0))
319 section_name
= ".bss";
322 section_name
= ".data";
325 section_name
= ".rodata";
332 struct btf_var_secinfo info
;
335 unsigned int *var_id
= btf_var_ids
->get (dvd
);
337 /* +1 for the sentinel type not in the types map. */
338 info
.type
= *var_id
+ num_types_added
+ 1;
343 tree size
= DECL_SIZE_UNIT (node
->decl
);
344 if (tree_fits_uhwi_p (size
))
345 info
.size
= tree_to_uhwi (size
);
347 /* Offset is left as 0 at compile time, to be filled in by loaders such
351 btf_datasec_push_entry (ctfc
, section_name
, info
);
355 /* Return true if the type ID is that of a type which will not be emitted (for
356 example, if it is not representable in BTF). */
359 btf_removed_type_p (ctf_id_t id
)
361 return holes
.contains (id
);
364 /* Adjust the given type ID to account for holes and duplicate definitions of
368 btf_adjust_type_id (ctf_id_t id
)
373 /* Do not adjust invalid type markers. */
374 if (id
== BTF_INVALID_TYPEID
)
377 for (n
= 0; n
< voids
.length (); n
++)
379 return BTF_VOID_TYPEID
;
381 for (n
= 0; n
< holes
.length (); n
++)
385 else if (holes
[n
] == id
)
386 return BTF_VOID_TYPEID
;
392 /* Postprocessing callback routine for types. */
395 btf_dtd_postprocess_cb (ctf_dtdef_ref
*slot
, ctf_container_ref arg_ctfc
)
397 ctf_dtdef_ref ctftype
= (ctf_dtdef_ref
) * slot
;
399 size_t index
= ctftype
->dtd_type
;
400 gcc_assert (index
<= arg_ctfc
->ctfc_types
->elements ());
402 uint32_t ctf_kind
, btf_kind
;
404 ctf_kind
= CTF_V2_INFO_KIND (ctftype
->dtd_data
.ctti_info
);
405 btf_kind
= get_btf_kind (ctf_kind
);
407 if (btf_kind
== BTF_KIND_UNKN
)
408 /* This type is not representable in BTF. Create a hole. */
409 holes
.safe_push (ctftype
->dtd_type
);
411 else if (btf_kind
== BTF_KIND_INT
&& ctftype
->dtd_data
.ctti_size
== 0)
413 /* This is a (redundant) definition of void. */
414 voids
.safe_push (ctftype
->dtd_type
);
415 holes
.safe_push (ctftype
->dtd_type
);
418 arg_ctfc
->ctfc_types_list
[index
] = ctftype
;
423 /* Preprocessing callback routine for variables. */
426 btf_dvd_emit_preprocess_cb (ctf_dvdef_ref
*slot
, ctf_container_ref arg_ctfc
)
428 ctf_dvdef_ref var
= (ctf_dvdef_ref
) * slot
;
430 /* Do not add variables which refer to unsupported types. */
431 if (btf_removed_type_p (var
->dvd_type
))
434 arg_ctfc
->ctfc_vars_list
[num_vars_added
] = var
;
435 btf_var_ids
->put (var
, num_vars_added
);
443 /* Preprocessing callback routine for types. */
446 btf_dtd_emit_preprocess_cb (ctf_container_ref ctfc
, ctf_dtdef_ref dtd
)
448 if (!btf_emit_id_p (dtd
->dtd_type
))
452 = get_btf_kind (CTF_V2_INFO_KIND (dtd
->dtd_data
.ctti_info
));
454 if (btf_kind
== BTF_KIND_FUNC_PROTO
)
456 /* Functions actually get two types: a BTF_KIND_FUNC_PROTO, and
457 also a BTF_KIND_FUNC. But the CTF container only allocates one
458 type per function, which matches closely with BTF_KIND_FUNC_PROTO.
459 For each such function, also allocate a BTF_KIND_FUNC entry.
460 These will be output later. */
461 ctf_dtdef_ref func_dtd
= ggc_cleared_alloc
<ctf_dtdef_t
> ();
462 func_dtd
->dtd_data
= dtd
->dtd_data
;
463 func_dtd
->dtd_data
.ctti_type
= dtd
->dtd_type
;
465 vec_safe_push (funcs
, func_dtd
);
468 /* Only the BTF_KIND_FUNC type actually references the name. The
469 BTF_KIND_FUNC_PROTO is always anonymous. */
470 dtd
->dtd_data
.ctti_name
= 0;
473 ctfc
->ctfc_num_vlen_bytes
+= btf_calc_num_vbytes (dtd
);
476 /* Preprocess the CTF information to prepare for BTF output. BTF is almost a
477 subset of CTF, with many small differences in encoding, and lacking support
478 for some types (notably floating point formats).
480 During the preprocessing pass:
481 - Ascertain that the sorted list of types has been prepared. For the BTF
482 generation process, this is taken care of by the btf_init_postprocess ().
484 - BTF_KIND_FUNC and BTF_KIND_DATASEC records are constructed. These types do
485 not have analogues in CTF (the analogous type to CTF_K_FUNCTION is
486 BTF_KIND_FUNC_PROTO), but can be relatively easily deduced from CTF
489 - Construct BTF_KIND_VAR records, representing variables.
491 - Calculate the total size in bytes of variable-length information following
492 BTF type records. This is used for outputting the BTF header.
494 After preprocessing, all BTF information is ready to be output:
495 - ctfc->ctfc_types_list holdstypes converted from CTF types. This does not
496 include KIND_VAR, KIND_FUNC, nor KIND_DATASEC types. These types have been
497 re-encoded to the appropriate representation in BTF.
498 - ctfc->ctfc_vars_list holds all variables which should be output.
499 Variables of unsupported types are not present in this list.
500 - Vector 'funcs' holds all BTF_KIND_FUNC types, one to match each
502 - Vector 'datasecs' holds all BTF_KIND_DATASEC types. */
505 btf_emit_preprocess (ctf_container_ref ctfc
)
507 size_t num_ctf_types
= ctfc
->ctfc_types
->elements ();
508 size_t num_ctf_vars
= ctfc
->ctfc_vars
->elements ();
513 gcc_assert (ctfc
->ctfc_types_list
);
514 /* Preprocess the types. */
515 for (i
= 1; i
<= num_ctf_types
; i
++)
516 btf_dtd_emit_preprocess_cb (ctfc
, ctfc
->ctfc_types_list
[i
]);
519 btf_var_ids
= hash_map
<ctf_dvdef_ref
, unsigned int>::create_ggc (100);
523 /* Allocate and construct the list of variables. While BTF variables are
524 not distinct from types (in that variables are simply types with
525 BTF_KIND_VAR), it is simpler to maintain a separate list of variables
526 and append them to the types list during output. */
527 ctfc
->ctfc_vars_list
= ggc_vec_alloc
<ctf_dvdef_ref
>(num_ctf_vars
);
528 ctfc
->ctfc_vars
->traverse
<ctf_container_ref
, btf_dvd_emit_preprocess_cb
>
531 ctfc
->ctfc_num_vlen_bytes
+= (num_vars_added
* sizeof (struct btf_var
));
534 btf_collect_datasec (ctfc
);
537 /* Return true iff DMD is a member description of a bit-field which can be
538 validly represented in BTF. */
541 btf_dmd_representable_bitfield_p (ctf_container_ref ctfc
, ctf_dmdef_t
*dmd
)
543 ctf_dtdef_ref ref_type
= ctfc
->ctfc_types_list
[dmd
->dmd_type
];
545 if (CTF_V2_INFO_KIND (ref_type
->dtd_data
.ctti_info
) == CTF_K_SLICE
)
547 unsigned short word_offset
= ref_type
->dtd_u
.dtu_slice
.cts_offset
;
548 unsigned short bits
= ref_type
->dtd_u
.dtu_slice
.cts_bits
;
549 uint64_t sou_offset
= dmd
->dmd_offset
;
551 if ((bits
> 0xff) || ((sou_offset
+ word_offset
) > 0xffffff))
560 /* BTF asm helper routines. */
562 /* Asm'out a BTF type. This routine is responsible for the bulk of the task
563 of converting CTF types to their BTF representation. */
566 btf_asm_type (ctf_container_ref ctfc
, ctf_dtdef_ref dtd
)
568 uint32_t btf_kind
, btf_kflag
, btf_vlen
, btf_size_type
;
569 uint32_t ctf_info
= dtd
->dtd_data
.ctti_info
;
571 btf_kind
= get_btf_kind (CTF_V2_INFO_KIND (ctf_info
));
572 btf_size_type
= dtd
->dtd_data
.ctti_type
;
573 btf_vlen
= CTF_V2_INFO_VLEN (ctf_info
);
575 /* By now any unrepresentable types have been removed. */
576 gcc_assert (btf_kind
!= BTF_KIND_UNKN
);
578 /* Size 0 integers are redundant definitions of void. None should remain
579 in the types list by this point. */
580 gcc_assert (btf_kind
!= BTF_KIND_INT
|| btf_size_type
>= 1);
582 /* Re-encode the ctti_info to BTF. */
583 /* kflag is 1 for structs/unions with a bitfield member.
584 kflag is 1 for forwards to unions.
585 kflag is 0 in all other cases. */
588 if (btf_kind
== BTF_KIND_STRUCT
|| btf_kind
== BTF_KIND_UNION
)
590 /* If a struct/union has ANY bitfield members, set kflag=1.
591 Note that we must also change the encoding of every member to encode
592 both member bitfield size (stealing most-significant 8 bits) and bit
593 offset (LS 24 bits). This is done during preprocessing. */
595 for (dmd
= dtd
->dtd_u
.dtu_members
;
596 dmd
!= NULL
; dmd
= (ctf_dmdef_t
*) ctf_dmd_list_next (dmd
))
598 /* Set kflag if this member is a representable bitfield. */
599 if (btf_dmd_representable_bitfield_p (ctfc
, dmd
))
602 /* Struct members that refer to unsupported types or bitfield formats
603 shall be skipped. These are marked during preprocessing. */
604 else if (!btf_emit_id_p (dmd
->dmd_type
))
609 /* BTF forwards make use of KIND_FLAG to distinguish between forwards to
610 structs and forwards to unions. The dwarf2ctf conversion process stores
611 the kind of the forward in ctti_type, but for BTF this must be 0 for
612 forwards, with only the KIND_FLAG to distinguish.
613 At time of writing, BTF forwards to enums are unspecified. */
614 if (btf_kind
== BTF_KIND_FWD
)
616 if (dtd
->dtd_data
.ctti_type
== CTF_K_UNION
)
622 dw2_asm_output_data (4, dtd
->dtd_data
.ctti_name
, "btt_name");
623 dw2_asm_output_data (4, BTF_TYPE_INFO (btf_kind
, btf_kflag
, btf_vlen
),
624 "btt_info: kind=%u, kflag=%u, vlen=%u",
625 btf_kind
, btf_kflag
, btf_vlen
);
629 case BTF_KIND_STRUCT
:
632 case BTF_KIND_DATASEC
:
633 dw2_asm_output_data (4, dtd
->dtd_data
.ctti_size
, "btt_size: %uB",
634 dtd
->dtd_data
.ctti_size
);
640 dw2_asm_output_data (4, get_btf_id (dtd
->dtd_data
.ctti_type
), "btt_type");
643 /* Asm'out the variable information following a BTF_KIND_ARRAY. */
646 btf_asm_array (ctf_dtdef_ref dtd
)
648 dw2_asm_output_data (4, get_btf_id (dtd
->dtd_u
.dtu_arr
.ctr_contents
),
650 dw2_asm_output_data (4, get_btf_id (dtd
->dtd_u
.dtu_arr
.ctr_index
),
652 dw2_asm_output_data (4, dtd
->dtd_u
.dtu_arr
.ctr_nelems
, "bta_nelems");
655 /* Asm'out a BTF_KIND_VAR. */
658 btf_asm_varent (ctf_dvdef_ref var
)
660 dw2_asm_output_data (4, var
->dvd_name_offset
, "btv_name");
661 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_VAR
, 0, 0), "btv_info");
662 dw2_asm_output_data (4, get_btf_id (var
->dvd_type
), "btv_type");
663 dw2_asm_output_data (4, (var
->dvd_visibility
? 1 : 0), "btv_linkage");
666 /* Asm'out a member description following a BTF_KIND_STRUCT or
670 btf_asm_sou_member (ctf_container_ref ctfc
, ctf_dmdef_t
* dmd
)
672 ctf_dtdef_ref ref_type
= ctfc
->ctfc_types_list
[dmd
->dmd_type
];
674 /* Re-encode bitfields to BTF representation. */
675 if (CTF_V2_INFO_KIND (ref_type
->dtd_data
.ctti_info
) == CTF_K_SLICE
)
677 ctf_id_t base_type
= ref_type
->dtd_u
.dtu_slice
.cts_type
;
678 unsigned short word_offset
= ref_type
->dtd_u
.dtu_slice
.cts_offset
;
679 unsigned short bits
= ref_type
->dtd_u
.dtu_slice
.cts_bits
;
680 uint64_t sou_offset
= dmd
->dmd_offset
;
682 /* Pack the bit offset and bitfield size together. */
683 sou_offset
+= word_offset
;
685 /* If this bitfield cannot be represented, do not output anything.
686 The parent struct/union 'vlen' field has already been updated. */
687 if ((bits
> 0xff) || (sou_offset
> 0xffffff))
690 sou_offset
&= 0x00ffffff;
691 sou_offset
|= ((bits
& 0xff) << 24);
693 /* Refer to the base type of the slice. */
694 dw2_asm_output_data (4, dmd
->dmd_name_offset
, "btm_name_off");
695 dw2_asm_output_data (4, get_btf_id (base_type
), "btm_type");
696 dw2_asm_output_data (4, sou_offset
, "btm_offset");
700 dw2_asm_output_data (4, dmd
->dmd_name_offset
, "btm_name_off");
701 dw2_asm_output_data (4, get_btf_id (dmd
->dmd_type
), "btm_type");
702 dw2_asm_output_data (4, dmd
->dmd_offset
, "btm_offset");
706 /* Asm'out an enum constant following a BTF_KIND_ENUM. */
709 btf_asm_enum_const (ctf_dmdef_t
* dmd
)
711 dw2_asm_output_data (4, dmd
->dmd_name_offset
, "bte_name");
712 dw2_asm_output_data (4, dmd
->dmd_value
, "bte_value");
715 /* Asm'out a function parameter description following a BTF_KIND_FUNC_PROTO. */
718 btf_asm_func_arg (ctf_func_arg_t
* farg
, size_t stroffset
)
720 /* If the function arg does not have a name, refer to the null string at
721 the start of the string table. This ensures correct encoding for varargs
723 if ((farg
->farg_name
!= NULL
) && strcmp (farg
->farg_name
, ""))
724 dw2_asm_output_data (4, farg
->farg_name_offset
+ stroffset
, "farg_name");
726 dw2_asm_output_data (4, 0, "farg_name");
728 dw2_asm_output_data (4, (btf_removed_type_p (farg
->farg_type
)
730 : get_btf_id (farg
->farg_type
)),
734 /* Asm'out a BTF_KIND_FUNC type. */
737 btf_asm_func_type (ctf_dtdef_ref dtd
)
739 dw2_asm_output_data (4, dtd
->dtd_data
.ctti_name
, "btt_name");
740 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC
, 0, 0), "btt_info");
741 dw2_asm_output_data (4, get_btf_id (dtd
->dtd_data
.ctti_type
), "btt_type");
744 /* Asm'out a variable entry following a BTF_KIND_DATASEC. */
747 btf_asm_datasec_entry (struct btf_var_secinfo info
)
749 dw2_asm_output_data (4, info
.type
, "bts_type");
750 dw2_asm_output_data (4, info
.offset
, "bts_offset");
751 dw2_asm_output_data (4, info
.size
, "bts_size");
754 /* Asm'out a whole BTF_KIND_DATASEC, including its variable entries. */
757 btf_asm_datasec_type (btf_datasec_t ds
, size_t stroffset
)
759 dw2_asm_output_data (4, ds
.name_offset
+ stroffset
, "btt_name");
760 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_DATASEC
, 0,
761 ds
.entries
.length ()),
763 /* Note: the "total section size in bytes" is emitted as 0 and patched by
764 loaders such as libbpf. */
765 dw2_asm_output_data (4, 0, "btt_size");
766 for (size_t i
= 0; i
< ds
.entries
.length (); i
++)
767 btf_asm_datasec_entry (ds
.entries
[i
]);
770 /* Compute and output the header information for a .BTF section. */
773 output_btf_header (ctf_container_ref ctfc
)
775 switch_to_section (btf_info_section
);
776 ASM_OUTPUT_LABEL (asm_out_file
, btf_info_section_label
);
778 /* BTF magic number, version, flags, and header length. */
779 dw2_asm_output_data (2, BTF_MAGIC
, "btf_magic");
780 dw2_asm_output_data (1, BTF_VERSION
, "btf_version");
781 dw2_asm_output_data (1, 0, "btf_flags");
782 dw2_asm_output_data (4, sizeof (struct btf_header
), "btf_hdr_len");
784 uint32_t type_off
= 0, type_len
= 0;
785 uint32_t str_off
= 0, str_len
= 0;
786 uint32_t datasec_vlen_bytes
= 0;
788 if (!ctfc_is_empty_container (ctfc
))
790 for (size_t i
= 0; i
< datasecs
.length (); i
++)
792 datasec_vlen_bytes
+= ((datasecs
[i
].entries
.length ())
793 * sizeof (struct btf_var_secinfo
));
796 /* Total length (bytes) of the types section. */
797 type_len
= (num_types_added
* sizeof (struct btf_type
))
798 + (num_types_created
* sizeof (struct btf_type
))
800 + ctfc
->ctfc_num_vlen_bytes
;
802 str_off
= type_off
+ type_len
;
804 str_len
= ctfc
->ctfc_strtable
.ctstab_len
805 + ctfc
->ctfc_aux_strtable
.ctstab_len
;
808 /* Offset of type section. */
809 dw2_asm_output_data (4, type_off
, "type_off");
810 /* Length of type section in bytes. */
811 dw2_asm_output_data (4, type_len
, "type_len");
812 /* Offset of string section. */
813 dw2_asm_output_data (4, str_off
, "str_off");
814 /* Length of string section in bytes. */
815 dw2_asm_output_data (4, str_len
, "str_len");
818 /* Output all BTF_KIND_VARs in CTFC. */
821 output_btf_vars (ctf_container_ref ctfc
)
824 size_t num_ctf_vars
= num_vars_added
;
827 for (i
= 0; i
< num_ctf_vars
; i
++)
828 btf_asm_varent (ctfc
->ctfc_vars_list
[i
]);
832 /* Output BTF string records. The BTF strings section is a concatenation
833 of the standard and auxilliary string tables in the ctf container. */
836 output_btf_strs (ctf_container_ref ctfc
)
838 ctf_string_t
* ctf_string
= ctfc
->ctfc_strtable
.ctstab_head
;
842 dw2_asm_output_nstring (ctf_string
->cts_str
, -1, "btf_string");
843 ctf_string
= ctf_string
->cts_next
;
846 ctf_string
= ctfc
->ctfc_aux_strtable
.ctstab_head
;
849 dw2_asm_output_nstring (ctf_string
->cts_str
, -1, "btf_aux_string");
850 ctf_string
= ctf_string
->cts_next
;
854 /* Output all (representable) members of a BTF_KIND_STRUCT or
855 BTF_KIND_UNION type. */
858 output_asm_btf_sou_fields (ctf_container_ref ctfc
, ctf_dtdef_ref dtd
)
862 for (dmd
= dtd
->dtd_u
.dtu_members
;
863 dmd
!= NULL
; dmd
= (ctf_dmdef_t
*) ctf_dmd_list_next (dmd
))
864 btf_asm_sou_member (ctfc
, dmd
);
867 /* Output all enumerator constants following a BTF_KIND_ENUM. */
870 output_asm_btf_enum_list (ctf_container_ref
ARG_UNUSED (ctfc
),
875 for (dmd
= dtd
->dtd_u
.dtu_members
;
876 dmd
!= NULL
; dmd
= (ctf_dmdef_t
*) ctf_dmd_list_next (dmd
))
877 btf_asm_enum_const (dmd
);
880 /* Output all function arguments following a BTF_KIND_FUNC_PROTO. */
883 output_asm_btf_func_args_list (ctf_container_ref ctfc
,
886 size_t farg_name_offset
= ctfc_get_strtab_len (ctfc
, CTF_STRTAB
);
887 ctf_func_arg_t
* farg
;
888 for (farg
= dtd
->dtd_u
.dtu_argv
;
889 farg
!= NULL
; farg
= (ctf_func_arg_t
*) ctf_farg_list_next (farg
))
890 btf_asm_func_arg (farg
, farg_name_offset
);
893 /* Output the variable portion of a BTF type record. The information depends
894 on the kind of the type. */
897 output_asm_btf_vlen_bytes (ctf_container_ref ctfc
, ctf_dtdef_ref dtd
)
899 uint32_t btf_kind
, encoding
;
901 btf_kind
= get_btf_kind (CTF_V2_INFO_KIND (dtd
->dtd_data
.ctti_info
));
903 if (btf_kind
== BTF_KIND_UNKN
)
909 /* Redundant definitions of void may still be hanging around in the type
910 list as size 0 integers. Skip emitting them. */
911 if (dtd
->dtd_data
.ctti_size
< 1)
914 encoding
= BTF_INT_DATA (dtd
->dtd_u
.dtu_enc
.cte_format
,
915 dtd
->dtd_u
.dtu_enc
.cte_offset
,
916 dtd
->dtd_u
.dtu_enc
.cte_bits
);
918 dw2_asm_output_data (4, encoding
, "bti_encoding");
925 case BTF_KIND_STRUCT
:
927 output_asm_btf_sou_fields (ctfc
, dtd
);
931 output_asm_btf_enum_list (ctfc
, dtd
);
934 case BTF_KIND_FUNC_PROTO
:
935 output_asm_btf_func_args_list (ctfc
, dtd
);
939 /* BTF Variables are handled by output_btf_vars and btf_asm_varent.
940 There should be no BTF_KIND_VAR types at this point. */
943 case BTF_KIND_DATASEC
:
944 /* The BTF_KIND_DATASEC records are handled by output_btf_datasec_types
945 and btf_asm_datasec_type. There should be no BTF_KIND_DATASEC types
950 /* All other BTF type kinds have no variable length data. */
955 /* Output a whole BTF type record for TYPE, including the fixed and variable
959 output_asm_btf_type (ctf_container_ref ctfc
, ctf_dtdef_ref type
)
961 if (btf_emit_id_p (type
->dtd_type
))
963 btf_asm_type (ctfc
, type
);
964 output_asm_btf_vlen_bytes (ctfc
, type
);
968 /* Output all BTF types in the container. This does not include synthesized
969 types: BTF_KIND_VAR, BTF_KIND_FUNC, nor BTF_KIND_DATASEC. */
972 output_btf_types (ctf_container_ref ctfc
)
975 size_t num_types
= ctfc
->ctfc_types
->elements ();
978 for (i
= 1; i
<= num_types
; i
++)
979 output_asm_btf_type (ctfc
, ctfc
->ctfc_types_list
[i
]);
983 /* Output all BTF_KIND_FUNC type records. */
986 output_btf_func_types (void)
988 for (size_t i
= 0; i
< vec_safe_length (funcs
); i
++)
989 btf_asm_func_type ((*funcs
)[i
]);
992 /* Output all BTF_KIND_DATASEC records. */
995 output_btf_datasec_types (ctf_container_ref ctfc
)
997 size_t name_offset
= ctfc_get_strtab_len (ctfc
, CTF_STRTAB
);
999 for (size_t i
= 0; i
< datasecs
.length(); i
++)
1000 btf_asm_datasec_type (datasecs
[i
], name_offset
);
1003 /* Postprocess the CTF debug data post initialization.
1005 During the postprocess pass:
1007 - Prepare the sorted list of BTF types.
1009 The sorted list of BTF types is, firstly, used for lookup (during the BTF
1010 generation process) of CTF/BTF types given a typeID.
1012 Secondly, in the emitted BTF section, BTF Types need to be in the sorted
1013 order of their type IDs. The BTF types section is viewed as an array,
1014 with type IDs used to index into that array. It is essential that every
1015 type be placed at the exact index corresponding to its ID, or else
1016 references to that type from other types will no longer be correct.
1018 - References to void types are converted to reference BTF_VOID_TYPEID. In
1019 CTF, a distinct type is used to encode void.
1021 - Bitfield struct/union members are converted to BTF encoding. CTF uses
1022 slices to encode bitfields, but BTF does not have slices and encodes
1023 bitfield information directly in the variable-length btf_member
1024 descriptions following the struct or union type.
1026 - Unrepresentable types are removed. We cannot have any invalid BTF types
1027 appearing in the output so they must be removed, and type ids of other
1028 types and references adjust accordingly. This also involves ensuring that
1029 BTF descriptions of struct members referring to unrepresentable types are
1030 not emitted, as they would be nonsensical.
1032 - Adjust inner- and inter-type references-by-ID to account for removed
1033 types, and construct the types list. */
1036 btf_init_postprocess (void)
1038 ctf_container_ref tu_ctfc
= ctf_get_tu_ctfc ();
1041 size_t num_ctf_types
= tu_ctfc
->ctfc_types
->elements ();
1046 num_types_added
= 0;
1047 num_types_created
= 0;
1051 init_btf_id_map (num_ctf_types
+ 1);
1053 /* Allocate the types list and traverse all types, placing each type
1054 at the index according to its ID. Add 1 because type ID 0 always
1056 tu_ctfc
->ctfc_types_list
1057 = ggc_vec_alloc
<ctf_dtdef_ref
>(num_ctf_types
+ 1);
1058 tu_ctfc
->ctfc_types
->traverse
<ctf_container_ref
, btf_dtd_postprocess_cb
>
1061 /* Build mapping of CTF type ID -> BTF type ID, and count total number
1062 of valid BTF types added. */
1063 for (i
= 1; i
<= num_ctf_types
; i
++)
1065 ctf_dtdef_ref dtd
= tu_ctfc
->ctfc_types_list
[i
];
1066 ctf_id_t btfid
= btf_adjust_type_id (dtd
->dtd_type
);
1067 set_btf_id (dtd
->dtd_type
, btfid
);
1068 if (btfid
< BTF_MAX_TYPE
&& (btfid
!= BTF_VOID_TYPEID
))
1074 /* Process and output all BTF data. Entry point of btfout. */
1077 btf_output (const char * filename
)
1079 ctf_container_ref tu_ctfc
= ctf_get_tu_ctfc ();
1081 init_btf_sections ();
1083 datasecs
.create (0);
1084 vec_alloc (funcs
, 16);
1086 ctf_add_cuname (tu_ctfc
, filename
);
1088 btf_emit_preprocess (tu_ctfc
);
1090 output_btf_header (tu_ctfc
);
1091 output_btf_types (tu_ctfc
);
1092 output_btf_vars (tu_ctfc
);
1093 output_btf_func_types ();
1094 output_btf_datasec_types (tu_ctfc
);
1095 output_btf_strs (tu_ctfc
);
1098 /* Reset all state for BTF generation so that we can rerun the compiler within
1099 the same process. */
1104 btf_info_section
= NULL
;
1106 /* Clear preprocessing state. */
1108 num_types_added
= 0;
1109 num_types_created
= 0;
1113 for (size_t i
= 0; i
< datasecs
.length (); i
++)
1114 datasecs
[i
].entries
.release ();
1115 datasecs
.release ();
1122 ggc_free (btf_var_ids
);
1125 ctf_container_ref tu_ctfc
= ctf_get_tu_ctfc ();
1126 ctfc_delete_container (tu_ctfc
);
1130 #include "gt-btfout.h"