Debian Codegraph @

gcc/btfout.cc

raw
1 /* Output BTF format from GCC.
2 Copyright (C) 2021-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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. */
24
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. */
41
42 static int btf_label_num;
43
44 static GTY (()) section * btf_info_section;
45
46 /* BTF debug info section. */
47
48 #ifndef BTF_INFO_SECTION_NAME
49 #define BTF_INFO_SECTION_NAME ".BTF"
50 #endif
51
52 #define BTF_INFO_SECTION_FLAGS (SECTION_DEBUG)
53
54 /* Maximum size (in bytes) for an artifically generated BTF label. */
55
56 #define MAX_BTF_LABEL_BYTES 40
57
58 static char btf_info_section_label[MAX_BTF_LABEL_BYTES];
59
60 #ifndef BTF_INFO_SECTION_LABEL
61 #define BTF_INFO_SECTION_LABEL "Lbtf"
62 #endif
63
64 #define BTF_INVALID_TYPEID 0xFFFFFFFF
65
66 /* Internal representation of an entry in a BTF_KIND_DATASEC record. */
67 struct btf_datasec_entry
68 {
69 union {
70 ctf_dvdef_ref dvd; /* Reference to the underlying variable represented. */
71 ctf_dtdef_ref dtd; /* Reference to the underlying type represented. */
72 };
73 bool is_var; /* True iff this entry represents a variable. */
74 uint32_t size; /* Size of variable or function, in bytes.
75 For functions, always zero at compile time. */
76 };
77
78 /* Internal representation of a BTF_KIND_DATASEC record. */
79 typedef struct btf_datasec
80 {
81 ctf_id_t id; /* BTF type ID of this record. */
82 const char *name; /* Section name, e.g. ".bss". */
83 uint32_t name_offset; /* Offset to name in string table. */
84 vec<struct btf_datasec_entry> entries; /* Entries in this section. */
85 } btf_datasec_t;
86
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;
90
91 /* Functions in BTF have two separate type records - one for the prototype
92 (BTF_KIND_FUNC_PROTO), as well as a BTF_KIND_FUNC. CTF_K_FUNCTION types
93 map closely to BTF_KIND_FUNC_PROTO, but the BTF_KIND_FUNC records must be
94 created. This vector holds them. */
95 static GTY (()) vec<ctf_dtdef_ref, va_gc> *funcs;
96
97 /* Maps BTF_KIND_FUNC_PROTO to the BTF_KIND_FUNC record for it. Used when
98 creating DATASEC entries. */
99 static GTY (()) hash_map<ctf_dtdef_ref, ctf_dtdef_ref> *func_map;
100
101 /* Highest BTF ID assigned to any regular type translated from CTF.
102 Does not include BTF_KIND_{VAR,FUNC,DATASEC} types. */
103 static ctf_id_t max_translated_id = 0;
104
105 /* Name strings for BTF kinds.
106 Note: the indices here must match the type defines in btf.h. */
107 static const char *const btf_kind_names[] =
108 {
109 "UNKN", "INT", "PTR", "ARRAY", "STRUCT", "UNION", "ENUM", "FWD",
110 "TYPEDEF", "VOLATILE", "CONST", "RESTRICT", "FUNC", "FUNC_PROTO",
111 "VAR", "DATASEC", "FLOAT", "DECL_TAG", "TYPE_TAG", "ENUM64"
112 };
113
114 /* Return a name string for the given BTF_KIND. */
115
116 static const char *
117 btf_kind_name (uint32_t btf_kind)
118 {
119 return btf_kind_names[btf_kind];
120 }
121
122 /* Map a CTF type kind to the corresponding BTF type kind. */
123
124 static uint32_t
125 get_btf_kind (uint32_t ctf_kind)
126 {
127 /* N.B. the values encoding kinds are not in general the same for the
128 same kind between CTF and BTF. e.g. CTF_K_CONST != BTF_KIND_CONST. */
129 switch (ctf_kind)
130 {
131 case CTF_K_INTEGER: return BTF_KIND_INT;
132 case CTF_K_FLOAT: return BTF_KIND_FLOAT;
133 case CTF_K_POINTER: return BTF_KIND_PTR;
134 case CTF_K_ARRAY: return BTF_KIND_ARRAY;
135 case CTF_K_FUNCTION: return BTF_KIND_FUNC_PROTO;
136 case CTF_K_STRUCT: return BTF_KIND_STRUCT;
137 case CTF_K_UNION: return BTF_KIND_UNION;
138 case CTF_K_ENUM: return BTF_KIND_ENUM;
139 case CTF_K_FORWARD: return BTF_KIND_FWD;
140 case CTF_K_TYPEDEF: return BTF_KIND_TYPEDEF;
141 case CTF_K_VOLATILE: return BTF_KIND_VOLATILE;
142 case CTF_K_CONST: return BTF_KIND_CONST;
143 case CTF_K_RESTRICT: return BTF_KIND_RESTRICT;
144 default:;
145 }
146 return BTF_KIND_UNKN;
147 }
148
149 /* Convenience wrapper around get_btf_kind for the common case. */
150
151 uint32_t
152 btf_dtd_kind (ctf_dtdef_ref dtd)
153 {
154 if (!dtd)
155 return BTF_KIND_UNKN;
156 return get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
157 }
158
159 /* Some BTF types, like BTF_KIND_FUNC_PROTO, are anonymous. The machinery
160 in btfout to emit BTF, may reset dtd_data->ctti_name, but does not update
161 the name in the ctf_dtdef_ref type object (deliberate choice). This
162 interface helps abstract out that state of affairs, while giving access to
163 the name of the type as intended. */
164
165 static const char *
166 get_btf_type_name (ctf_dtdef_ref dtd)
167 {
168 const char *anon = "";
169 return (dtd->dtd_data.ctti_name) ? dtd->dtd_name : anon;
170 }
171
172 static bool
173 btf_emit_type_p (ctf_dtdef_ref dtd)
174 {
175 uint32_t kind = btf_dtd_kind (dtd);
176
177 if (kind == BTF_KIND_UNKN)
178 /* This type is not representable in BTF. */
179 return false;
180
181 if (kind == BTF_KIND_INT && dtd->dtd_data.ctti_size == 0)
182 /* This is a (redundant) definition of void. */
183 return false;
184
185 return true;
186 }
187
188 /* Return true if DTD is a forward-declared enum. The BTF representation
189 of forward declared enums is not formally defined. */
190
191 static bool
192 btf_fwd_to_enum_p (ctf_dtdef_ref dtd)
193 {
194 uint32_t kind = btf_dtd_kind (dtd);
195 return (kind == BTF_KIND_FWD && dtd->dtd_data.ctti_type == CTF_K_ENUM);
196 }
197
198 /* Each BTF type can be followed additional, variable-length information
199 completing the description of the type. Calculate the number of bytes
200 of variable information required to encode a given type. */
201
202 static uint64_t
203 btf_calc_num_vbytes (ctf_dtdef_ref dtd)
204 {
205 uint64_t vlen_bytes = 0;
206
207 uint32_t kind = btf_dtd_kind (dtd);
208 uint32_t vlen = CTF_V2_INFO_VLEN (dtd->dtd_data.ctti_info);
209
210 switch (kind)
211 {
212 case BTF_KIND_UNKN:
213 case BTF_KIND_PTR:
214 case BTF_KIND_FWD:
215 case BTF_KIND_TYPEDEF:
216 case BTF_KIND_VOLATILE:
217 case BTF_KIND_CONST:
218 case BTF_KIND_RESTRICT:
219 case BTF_KIND_FUNC:
220 /* These kinds have no vlen data. */
221 break;
222
223 case BTF_KIND_INT:
224 /* Size 0 integers represent redundant definitions of void that will
225 not be emitted. Don't allocate space for them. */
226 if (dtd->dtd_data.ctti_size == 0)
227 break;
228
229 vlen_bytes += sizeof (uint32_t);
230 break;
231
232 case BTF_KIND_ARRAY:
233 vlen_bytes += sizeof (struct btf_array);
234 break;
235
236 case BTF_KIND_STRUCT:
237 case BTF_KIND_UNION:
238 vlen_bytes += vlen * sizeof (struct btf_member);
239 break;
240
241 case BTF_KIND_ENUM:
242 vlen_bytes += (dtd->dtd_data.ctti_size > 4)
243 ? vlen * sizeof (struct btf_enum64)
244 : vlen * sizeof (struct btf_enum);
245 break;
246
247 case BTF_KIND_FUNC_PROTO:
248 vlen_bytes += vlen * sizeof (struct btf_param);
249 break;
250
251 case BTF_KIND_VAR:
252 vlen_bytes += sizeof (struct btf_var);
253 break;
254
255 case BTF_KIND_DATASEC:
256 vlen_bytes += vlen * sizeof (struct btf_var_secinfo);
257 break;
258
259 default:
260 break;
261 }
262 return vlen_bytes;
263 }
264
265 /* Initialize BTF section (.BTF) for output. */
266
267 void
268 init_btf_sections (void)
269 {
270 btf_info_section = get_section (BTF_INFO_SECTION_NAME, BTF_INFO_SECTION_FLAGS,
271 NULL);
272
273 ASM_GENERATE_INTERNAL_LABEL (btf_info_section_label,
274 BTF_INFO_SECTION_LABEL, btf_label_num++);
275 }
276
277 /* Return the section name, as of interest to btf_collect_datasec, for the
278 given symtab node. Note that this deliberately returns NULL for objects
279 which do not go in a section btf_collect_datasec cares about. */
280 static const char *
281 get_section_name (symtab_node *node)
282 {
283 const char *section_name = node->get_section ();
284
285 if (section_name == NULL)
286 {
287 switch (categorize_decl_for_section (node->decl, 0))
288 {
289 case SECCAT_BSS:
290 section_name = ".bss";
291 break;
292 case SECCAT_DATA:
293 section_name = ".data";
294 break;
295 case SECCAT_RODATA:
296 section_name = ".rodata";
297 break;
298 default:;
299 }
300 }
301
302 return section_name;
303 }
304
305 /* Return true iff DMD is a member description of a bit-field which can be
306 validly represented in BTF. */
307
308 static bool
309 btf_dmd_representable_bitfield_p (ctf_dmdef_t *dmd)
310 {
311 ctf_dtdef_ref ref_type = dmd->dmd_type;
312 if (!ref_type)
313 return false;
314
315 if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
316 {
317 unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
318 unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
319 uint64_t sou_offset = dmd->dmd_offset;
320
321 if ((bits > 0xff) || ((sou_offset + word_offset) > 0xffffff))
322 return false;
323
324 return true;
325 }
326
327 return false;
328 }
329
330 /* BTF asm helper routines. */
331
332 /* Asm'out a reference to another BTF type. */
333
334 static void
335 btf_asm_type_ref (const char *prefix, ctf_dtdef_ref dtd)
336 {
337 if (!dtd || !btf_emit_type_p (dtd))
338 dw2_asm_output_data (4, BTF_VOID_TYPEID, "%s: void", prefix);
339 else
340 {
341 uint32_t kind = btf_dtd_kind (dtd);
342 if (btf_fwd_to_enum_p (dtd))
343 kind = BTF_KIND_ENUM;
344 else if (kind == BTF_KIND_FUNC_PROTO && dtd->dtd_type > max_translated_id)
345 kind = BTF_KIND_FUNC;
346
347 dw2_asm_output_data (4, dtd->dtd_type, "%s: (BTF_KIND_%s '%s')",
348 prefix, btf_kind_name (kind),
349 get_btf_type_name (dtd));
350 }
351 }
352
353 /* Asm'out a BTF type. This routine is responsible for the bulk of the task
354 of converting CTF types to their BTF representation. */
355
356 static void
357 btf_asm_type (ctf_dtdef_ref dtd)
358 {
359 uint32_t btf_kind, btf_kflag, btf_vlen, btf_size;
360 uint32_t ctf_info = dtd->dtd_data.ctti_info;
361
362 btf_kind = btf_dtd_kind (dtd);
363 btf_size = dtd->dtd_data.ctti_size;
364 btf_vlen = CTF_V2_INFO_VLEN (ctf_info);
365
366 /* By now any unrepresentable types have been removed. */
367 gcc_assert (btf_kind != BTF_KIND_UNKN);
368
369 /* Size 0 integers are redundant definitions of void. None should remain
370 in the types list by this point. */
371 gcc_assert (btf_kind != BTF_KIND_INT || btf_size >= 1);
372
373 /* Re-encode the ctti_info to BTF. */
374 /* kflag is 1 for structs/unions with a bitfield member.
375 kflag is 1 for forwards to unions.
376 kflag is 0 in all other cases. */
377 btf_kflag = 0;
378
379 if (btf_kind == BTF_KIND_STRUCT || btf_kind == BTF_KIND_UNION)
380 {
381 /* If a struct/union has ANY bitfield members, set kflag=1. */
382 ctf_dmdef_t *dmd;
383 for (dmd = dtd->dtd_u.dtu_members;
384 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
385 {
386 /* Set kflag if this member is a representable bitfield. */
387 if (btf_dmd_representable_bitfield_p (dmd))
388 {
389 btf_kflag = 1;
390 break;
391 }
392 }
393 }
394
395 /* BTF forwards make use of KIND_FLAG to distinguish between forwards to
396 structs and forwards to unions. The dwarf2ctf conversion process stores
397 the kind of the forward in ctti_type, but for BTF this must be 0 for
398 forwards, with only the KIND_FLAG to distinguish.
399 Forwards to enum types are special-cased below. */
400 else if (btf_kind == BTF_KIND_FWD)
401 {
402 if (dtd->dtd_data.ctti_type == CTF_K_UNION)
403 btf_kflag = 1;
404
405 /* PR debug/111735. Encode foward-declared enums as BTF_KIND_ENUM
406 with vlen=0. A representation for these is not formally defined;
407 this is the de-facto standard used by other tools like clang
408 and pahole. */
409 else if (dtd->dtd_data.ctti_type == CTF_K_ENUM)
410 {
411 btf_kind = BTF_KIND_ENUM;
412 btf_vlen = 0;
413 }
414
415 btf_size = 0;
416 }
417
418 else if (btf_kind == BTF_KIND_ENUM)
419 {
420 btf_kflag = dtd->dtd_enum_unsigned
421 ? BTF_KF_ENUM_UNSIGNED
422 : BTF_KF_ENUM_SIGNED;
423 if (dtd->dtd_data.ctti_size == 0x8)
424 btf_kind = BTF_KIND_ENUM64;
425 }
426
427 /* PR debug/112656. BTF_KIND_FUNC_PROTO is always anonymous. */
428 else if (btf_kind == BTF_KIND_FUNC_PROTO)
429 dtd->dtd_data.ctti_name = 0;
430
431 dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
432 "TYPE %" PRIu64 " BTF_KIND_%s '%s'",
433 dtd->dtd_type, btf_kind_name (btf_kind),
434 get_btf_type_name (dtd));
435 dw2_asm_output_data (4, BTF_TYPE_INFO (btf_kind, btf_kflag, btf_vlen),
436 "btt_info: kind=%u, kflag=%u, vlen=%u",
437 btf_kind, btf_kflag, btf_vlen);
438 switch (btf_kind)
439 {
440 case BTF_KIND_INT:
441 case BTF_KIND_FLOAT:
442 case BTF_KIND_STRUCT:
443 case BTF_KIND_UNION:
444 case BTF_KIND_ENUM:
445 case BTF_KIND_DATASEC:
446 case BTF_KIND_ENUM64:
447 dw2_asm_output_data (4, btf_size, "btt_size: %uB", btf_size);
448 return;
449 case BTF_KIND_ARRAY:
450 case BTF_KIND_FWD:
451 /* These types do not encode any information in the size/type field
452 and should write 0. */
453 dw2_asm_output_data (4, 0, "(unused)");
454 return;
455 default:
456 break;
457 }
458
459 btf_asm_type_ref ("btt_type", dtd->ref_type);
460 }
461
462 /* Asm'out the variable information following a BTF_KIND_ARRAY. */
463
464 static void
465 btf_asm_array (ctf_arinfo_t arr)
466 {
467 btf_asm_type_ref ("bta_elem_type", arr.ctr_contents);
468 btf_asm_type_ref ("bta_index_type", arr.ctr_index);
469 dw2_asm_output_data (4, arr.ctr_nelems, "bta_nelems");
470 }
471
472 /* Asm'out a BTF_KIND_VAR. */
473
474 static void
475 btf_asm_varent (ctf_dvdef_ref var)
476 {
477 dw2_asm_output_data (4, var->dvd_name_offset,
478 "TYPE %" PRIu64 " BTF_KIND_VAR '%s'",
479 var->dvd_id, var->dvd_name);
480 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_VAR, 0, 0), "btv_info");
481 btf_asm_type_ref ("btv_type", var->dvd_type);
482 dw2_asm_output_data (4, var->dvd_visibility, "btv_linkage");
483 }
484
485 /* Asm'out a member description following a BTF_KIND_STRUCT or
486 BTF_KIND_UNION. */
487
488 static void
489 btf_asm_sou_member (ctf_dmdef_t * dmd, unsigned int idx)
490 {
491 ctf_dtdef_ref base_type = dmd->dmd_type;
492 uint64_t sou_offset = dmd->dmd_offset;
493
494 dw2_asm_output_data (4, dmd->dmd_name_offset,
495 "MEMBER '%s' idx=%u",
496 dmd->dmd_name, idx);
497
498 if (base_type
499 && CTF_V2_INFO_KIND (base_type->dtd_data.ctti_info) == CTF_K_SLICE)
500 {
501 if (btf_dmd_representable_bitfield_p (dmd))
502 {
503 unsigned short word_offset = base_type->dtd_u.dtu_slice.cts_offset;
504 unsigned short bits = base_type->dtd_u.dtu_slice.cts_bits;
505
506 /* Pack the bit offset and bitfield size together. */
507 sou_offset += word_offset;
508 sou_offset &= 0x00ffffff;
509 sou_offset |= ((bits & 0xff) << 24);
510
511 /* Refer to the base type of the slice. */
512 base_type = base_type->dtd_u.dtu_slice.cts_type;
513 }
514 else
515 {
516 /* Bitfield cannot be represented in BTF. Emit the member as having
517 'void' type. */
518 base_type = NULL;
519 }
520 }
521
522 btf_asm_type_ref ("btm_type", base_type);
523 dw2_asm_output_data (4, sou_offset, "btm_offset");
524 }
525
526 /* Asm'out an enum constant following a BTF_KIND_ENUM{,64}. */
527
528 static void
529 btf_asm_enum_const (unsigned int size, ctf_dmdef_t * dmd, unsigned int idx)
530 {
531 dw2_asm_output_data (4, dmd->dmd_name_offset, "ENUM_CONST '%s' idx=%u",
532 dmd->dmd_name, idx);
533 if (size <= 4)
534 dw2_asm_output_data (size < 4 ? 4 : size, dmd->dmd_value, "bte_value");
535 else
536 {
537 dw2_asm_output_data (4, dmd->dmd_value & 0xffffffff, "bte_value_lo32");
538 dw2_asm_output_data (4, (dmd->dmd_value >> 32) & 0xffffffff, "bte_value_hi32");
539 }
540 }
541
542 /* Asm'out a function parameter description following a BTF_KIND_FUNC_PROTO. */
543
544 static void
545 btf_asm_func_arg (ctf_func_arg_t * farg, size_t stroffset)
546 {
547 /* If the function arg does not have a name, refer to the null string at
548 the start of the string table. This ensures correct encoding for varargs
549 '...' arguments. */
550 if ((farg->farg_name != NULL) && strcmp (farg->farg_name, ""))
551 dw2_asm_output_data (4, farg->farg_name_offset + stroffset,
552 "farg_name '%s'", farg->farg_name);
553 else
554 dw2_asm_output_data (4, 0, "farg_name ''");
555
556 btf_asm_type_ref ("farg_type", farg->farg_type);
557 }
558
559 /* Asm'out a BTF_KIND_FUNC type. */
560
561 static void
562 btf_asm_func_type (ctf_dtdef_ref dtd)
563 {
564 dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
565 "TYPE %" PRIu64 " BTF_KIND_FUNC '%s'",
566 dtd->dtd_type, get_btf_type_name (dtd));
567 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC, 0, dtd->linkage),
568 "btt_info: kind=%u, kflag=%u, linkage=%u",
569 BTF_KIND_FUNC, 0, dtd->linkage);
570 btf_asm_type_ref ("btt_type", dtd->ref_type);
571 }
572
573 /* Asm'out a variable entry following a BTF_KIND_DATASEC. */
574
575 static void
576 btf_asm_datasec_entry (struct btf_datasec_entry entry)
577 {
578 const char *symbol_name = NULL;
579 if (entry.is_var)
580 {
581 symbol_name = entry.dvd->dvd_name;
582 dw2_asm_output_data (4, entry.dvd->dvd_id,
583 "bts_type: (BTF_KIND_VAR '%s')", symbol_name);
584 }
585 else
586 {
587 symbol_name = entry.dtd->dtd_name;
588 btf_asm_type_ref ("bts_type", entry.dtd);
589 }
590
591 if (!btf_with_core_debuginfo_p () || symbol_name == NULL)
592 dw2_asm_output_data (4, 0, "bts_offset");
593 else
594 dw2_asm_output_offset (4, symbol_name, NULL, "bts_offset");
595
596 dw2_asm_output_data (4, entry.size, "bts_size");
597 }
598
599 /* Asm'out a whole BTF_KIND_DATASEC, including its variable entries. */
600
601 static void
602 btf_asm_datasec_type (btf_datasec_t ds)
603 {
604 dw2_asm_output_data (4, ds.name_offset,
605 "TYPE %" PRIu64 " BTF_KIND_DATASEC '%s'",
606 ds.id, ds.name);
607 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_DATASEC, 0,
608 ds.entries.length ()),
609 "btt_info: n_entries=%u", ds.entries.length ());
610 /* Note: the "total section size in bytes" is emitted as 0 and patched by
611 loaders such as libbpf. */
612 dw2_asm_output_data (4, 0, "btt_size");
613 for (size_t i = 0; i < ds.entries.length (); i++)
614 btf_asm_datasec_entry (ds.entries[i]);
615 }
616
617 /* Compute and output the header information for a .BTF section. */
618
619 static void
620 output_btf_header (ctf_container_ref ctfc)
621 {
622 switch_to_section (btf_info_section);
623 ASM_OUTPUT_LABEL (asm_out_file, btf_info_section_label);
624
625 /* BTF magic number, version, flags, and header length. */
626 dw2_asm_output_data (2, BTF_MAGIC, "btf_magic");
627 dw2_asm_output_data (1, BTF_VERSION, "btf_version");
628 dw2_asm_output_data (1, 0, "btf_flags");
629 dw2_asm_output_data (4, sizeof (struct btf_header), "btf_hdr_len");
630
631 uint32_t type_off = 0, type_len = 0;
632 uint32_t str_off = 0, str_len = 0;
633
634 if (!ctfc_is_empty_container (ctfc))
635 {
636 /* Total length (bytes) of the types section. */
637 type_len = ctfc->ctfc_num_types * sizeof (struct btf_type)
638 + ctfc->ctfc_num_vlen_bytes;
639
640 str_off = type_off + type_len;
641
642 str_len = ctfc->ctfc_strtable.ctstab_len
643 + ctfc->ctfc_aux_strtable.ctstab_len;
644 }
645
646 /* Offset of type section. */
647 dw2_asm_output_data (4, type_off, "type_off");
648 /* Length of type section in bytes. */
649 dw2_asm_output_data (4, type_len, "type_len: ntypes=%u, vlen=%u",
650 (uint32_t) ctfc->ctfc_num_types,
651 (uint32_t) ctfc->ctfc_num_vlen_bytes);
652 /* Offset of string section. */
653 dw2_asm_output_data (4, str_off, "str_off");
654 /* Length of string section in bytes. */
655 dw2_asm_output_data (4, str_len, "str_len");
656 }
657
658 /* Output all BTF_KIND_VARs in CTFC. */
659
660 static void
661 output_btf_vars (ctf_container_ref ctfc)
662 {
663 size_t i;
664 size_t num_ctf_vars = ctfc->ctfc_vars_list_count;
665 if (num_ctf_vars)
666 {
667 for (i = 0; i < num_ctf_vars; i++)
668 btf_asm_varent (ctfc->ctfc_vars_list[i]);
669 }
670 }
671
672 /* Output BTF string records. The BTF strings section is a concatenation
673 of the standard and auxilliary string tables in the ctf container. */
674
675 static void
676 output_btf_strs (ctf_container_ref ctfc)
677 {
678 ctf_string_t * ctf_string = ctfc->ctfc_strtable.ctstab_head;
679 static int str_pos = 0;
680
681 while (ctf_string)
682 {
683 dw2_asm_output_nstring (ctf_string->cts_str, -1,
684 "btf_string, str_pos = 0x%x", str_pos);
685 str_pos += strlen(ctf_string->cts_str) + 1;
686 ctf_string = ctf_string->cts_next;
687 }
688
689 ctf_string = ctfc->ctfc_aux_strtable.ctstab_head;
690 while (ctf_string)
691 {
692 dw2_asm_output_nstring (ctf_string->cts_str, -1,
693 "btf_aux_string, str_pos = 0x%x", str_pos);
694 str_pos += strlen(ctf_string->cts_str) + 1;
695 ctf_string = ctf_string->cts_next;
696 }
697 }
698
699 /* Output all (representable) members of a BTF_KIND_STRUCT or
700 BTF_KIND_UNION type. */
701
702 static void
703 output_asm_btf_sou_fields (ctf_dtdef_ref dtd)
704 {
705 ctf_dmdef_t * dmd;
706
707 unsigned idx = 0;
708 for (dmd = dtd->dtd_u.dtu_members;
709 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
710 {
711 btf_asm_sou_member (dmd, idx);
712 idx++;
713 }
714 }
715
716 /* Output all enumerator constants following a BTF_KIND_ENUM{,64}. */
717
718 static void
719 output_asm_btf_enum_list (ctf_dtdef_ref dtd)
720 {
721 ctf_dmdef_t * dmd;
722
723 unsigned idx = 0;
724 for (dmd = dtd->dtd_u.dtu_members;
725 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
726 {
727 btf_asm_enum_const (dtd->dtd_data.ctti_size, dmd, idx);
728 idx++;
729 }
730 }
731
732 /* Output all function arguments following a BTF_KIND_FUNC_PROTO. */
733
734 static void
735 output_asm_btf_func_args_list (ctf_container_ref ctfc,
736 ctf_dtdef_ref dtd)
737 {
738 size_t farg_name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
739 ctf_func_arg_t * farg;
740 for (farg = dtd->dtd_u.dtu_argv;
741 farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
742 btf_asm_func_arg (farg, farg_name_offset);
743 }
744
745 /* Output the variable portion of a BTF type record. The information depends
746 on the kind of the type. */
747
748 static void
749 output_asm_btf_vlen_bytes (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
750 {
751 uint32_t btf_kind, encoding;
752
753 btf_kind = btf_dtd_kind (dtd);
754
755 if (btf_kind == BTF_KIND_UNKN)
756 return;
757
758 switch (btf_kind)
759 {
760 case BTF_KIND_INT:
761 /* Redundant definitions of void may still be hanging around in the type
762 list as size 0 integers. Skip emitting them. */
763 if (dtd->dtd_data.ctti_size < 1)
764 break;
765
766 /* In BTF the CHAR `encoding' seems to not be used, so clear it here. */
767 dtd->dtd_u.dtu_enc.cte_format &= ~BTF_INT_CHAR;
768
769 encoding = BTF_INT_DATA (dtd->dtd_u.dtu_enc.cte_format,
770 dtd->dtd_u.dtu_enc.cte_offset,
771 dtd->dtd_u.dtu_enc.cte_bits);
772
773 dw2_asm_output_data (4, encoding, "bti_encoding");
774 break;
775
776 case BTF_KIND_ARRAY:
777 btf_asm_array (dtd->dtd_u.dtu_arr);
778 break;
779
780 case BTF_KIND_STRUCT:
781 case BTF_KIND_UNION:
782 output_asm_btf_sou_fields (dtd);
783 break;
784
785 case BTF_KIND_ENUM:
786 output_asm_btf_enum_list (dtd);
787 break;
788
789 case BTF_KIND_FUNC_PROTO:
790 output_asm_btf_func_args_list (ctfc, dtd);
791 break;
792
793 case BTF_KIND_VAR:
794 /* BTF Variables are handled by output_btf_vars and btf_asm_varent.
795 There should be no BTF_KIND_VAR types at this point. */
796 gcc_unreachable ();
797
798 case BTF_KIND_DATASEC:
799 /* The BTF_KIND_DATASEC records are handled by output_btf_datasec_types
800 and btf_asm_datasec_type. There should be no BTF_KIND_DATASEC types
801 at this point. */
802 gcc_unreachable ();
803
804 default:
805 /* All other BTF type kinds have no variable length data. */
806 break;
807 }
808 }
809
810 /* Output a whole BTF type record for TYPE, including the fixed and variable
811 data portions. */
812
813 static void
814 output_asm_btf_type (ctf_container_ref ctfc, ctf_dtdef_ref type)
815 {
816 if (btf_emit_type_p (type))
817 {
818 btf_asm_type (type);
819 output_asm_btf_vlen_bytes (ctfc, type);
820 }
821 }
822
823 /* Output all BTF types in the container. This does not include synthesized
824 types: BTF_KIND_VAR, BTF_KIND_FUNC, nor BTF_KIND_DATASEC. */
825
826 static void
827 output_btf_types (ctf_container_ref ctfc)
828 {
829 size_t i;
830 size_t num_types;
831 if (debug_prune_btf)
832 num_types = max_translated_id;
833 else
834 num_types = ctfc->ctfc_types->elements ();
835
836 if (num_types)
837 {
838 for (i = 1; i <= num_types; i++)
839 output_asm_btf_type (ctfc, ctfc->ctfc_types_list[i]);
840 }
841 }
842
843 /* Output all BTF_KIND_FUNC type records. */
844
845 static void
846 output_btf_func_types (void)
847 {
848 ctf_dtdef_ref ref;
849 unsigned i;
850 FOR_EACH_VEC_ELT (*funcs, i, ref)
851 btf_asm_func_type (ref);
852 }
853
854 /* Output all BTF_KIND_DATASEC records. */
855
856 static void
857 output_btf_datasec_types (void)
858 {
859 for (size_t i = 0; i < datasecs.length (); i++)
860 btf_asm_datasec_type (datasecs[i]);
861 }
862
863 /* Write out all BTF debug info. */
864
865 void
866 btf_output (ctf_container_ref ctfc)
867 {
868 output_btf_header (ctfc);
869 output_btf_types (ctfc);
870 output_btf_vars (ctfc);
871 output_btf_func_types ();
872 output_btf_datasec_types ();
873 output_btf_strs (ctfc);
874 }
875
876 /* Workaround for 'const void' variables. These variables are sometimes used
877 in eBPF programs to address kernel symbols. DWARF does not generate const
878 qualifier on void type, so we would incorrectly emit these variables
879 without the const qualifier. Find any such variables, and update them to
880 refer to a new 'const' modifier type for void. */
881
882 static void
883 btf_add_const_void (ctf_container_ref ctfc)
884 {
885 ctf_dtdef_ref constvoid_dtd = NULL;
886 varpool_node *var;
887 FOR_EACH_VARIABLE (var)
888 {
889 if (!var->decl)
890 continue;
891
892 tree type = TREE_TYPE (var->decl);
893 if (type && VOID_TYPE_P (type) && TYPE_READONLY (type))
894 {
895 dw_die_ref die = lookup_decl_die (var->decl);
896 if (die == NULL)
897 continue;
898
899 ctf_dvdef_ref dvd = ctf_dvd_lookup (ctfc, die);
900 if (dvd == NULL)
901 continue;
902
903 /* Create the 'const' modifier type for void. */
904 if (constvoid_dtd == NULL)
905 constvoid_dtd = ctf_add_reftype (ctfc, CTF_ADD_ROOT,
906 dvd->dvd_type, CTF_K_CONST, NULL);
907 dvd->dvd_type = constvoid_dtd;
908 }
909 }
910 }
911
912 /* Functions actually get two type records: a BTF_KIND_FUNC_PROTO, and also a
913 BTF_KIND_FUNC. But the CTF container only allocates one type per function,
914 which matches closely with BTF_KIND_FUNC_PROTO. For each such function,
915 construct a BTF_KIND_FUNC entry. This is done early, because we want FUNC
916 records even for functions which are later inlined by optimizations. */
917
918 static void
919 btf_add_func_records (ctf_container_ref ctfc)
920 {
921 cgraph_node *func;
922 FOR_EACH_FUNCTION (func)
923 {
924 dw_die_ref die = lookup_decl_die (func->decl);
925 if (die == NULL)
926 continue;
927
928 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
929 if (dtd == NULL)
930 continue;
931
932 /* Do not add FUNC records for kernel helpers. */
933 if (DECL_EXTERNAL (func->decl)
934 && (lookup_attribute ("kernel_helper",
935 DECL_ATTRIBUTES (func->decl))) != NULL_TREE)
936 continue;
937
938 ctf_dtdef_ref func_dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
939 func_dtd->dtd_data = dtd->dtd_data;
940 func_dtd->dtd_data.ctti_type = dtd->dtd_type;
941 func_dtd->ref_type = dtd;
942 func_dtd->linkage = dtd->linkage;
943 func_dtd->dtd_name = dtd->dtd_name;
944 /* Type ID will be assigned just before output. */
945
946 /* Only the BTF_KIND_FUNC type actually references the name.
947 The BTF_KIND_FUNC_PROTO is always anonymous. */
948 dtd->dtd_data.ctti_name = 0;
949
950 /* Mark 'extern' funcs. */
951 if (DECL_EXTERNAL (func->decl))
952 func_dtd->linkage = BTF_FUNC_EXTERN;
953
954 /* Buffer newly created FUNC records. We cannot simply insert them
955 into the types map, because types are keyed by their DWARF DIE,
956 and we have no unique DIE to use as a key since the FUNC_PROTOs
957 are already present in the map. */
958 vec_safe_push (funcs, func_dtd);
959 func_map->put (dtd, func_dtd);
960 }
961 }
962
963 /* The set of types used directly in the source program, and any types manually
964 marked as used. This is the set of types which will be emitted when
965 flag_prune_btf is set. */
966 static GTY (()) hash_set<ctf_dtdef_ref> *btf_used_types;
967
968 /* Fixup used to avoid unnecessary pointer chasing for types. A fixup is
969 created when a structure or union member is a pointer to another struct
970 or union type. In such cases, avoid emitting full type information for
971 the pointee struct or union type (which may be quite large), unless that
972 type is used directly elsewhere. */
973 struct btf_fixup
974 {
975 ctf_dtdef_ref pointer_dtd; /* Type node to which the fixup is applied. */
976 ctf_dtdef_ref pointee_dtd; /* Original type node referred to by pointer_dtd.
977 If this concrete type is not otherwise used,
978 then a forward is created. */
979 };
980
981 /* Stores fixups while processing types. */
982 static vec<struct btf_fixup> fixups;
983
984 /* For fixups where the underlying type is not used in the end, a BTF_KIND_FWD
985 is created and emitted. This vector stores them. */
986 static GTY (()) vec<ctf_dtdef_ref, va_gc> *forwards;
987
988 /* Recursively add type DTD and any types it references to the used set.
989 Return a type that should be used for references to DTD - usually DTD itself,
990 but may be NULL if DTD corresponds to a type which will not be emitted.
991 CHECK_PTR is true if one of the predecessors in recursive calls is a struct
992 or union member. SEEN_PTR is true if CHECK_PTR is true AND one of the
993 predecessors was a pointer type. These two flags are used to avoid chasing
994 pointers to struct/union only used from pointer members. For such types, we
995 will emit a forward instead of the full type information, unless
996 CREATE_FIXUPS is false. */
997
998 static ctf_dtdef_ref
999 btf_add_used_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd,
1000 bool check_ptr, bool seen_ptr, bool create_fixups)
1001 {
1002 if (dtd == NULL)
1003 return NULL;
1004
1005 uint32_t ctf_kind = CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info);
1006 uint32_t kind = get_btf_kind (ctf_kind);
1007
1008 /* Check whether the type has already been added. */
1009 if (btf_used_types->contains (dtd))
1010 {
1011 /* It's possible the type was already added as a fixup, but that we now
1012 have a concrete use of it. */
1013 switch (kind)
1014 {
1015 case BTF_KIND_PTR:
1016 case BTF_KIND_TYPEDEF:
1017 case BTF_KIND_CONST:
1018 case BTF_KIND_VOLATILE:
1019 case BTF_KIND_RESTRICT:
1020 if (check_ptr)
1021 /* Type was previously added as a fixup, and that's OK. */
1022 return dtd;
1023 else
1024 {
1025 /* The type was previously added as a fixup, but now we have
1026 a concrete use of it. Remove the fixup. */
1027 for (size_t i = 0; i < fixups.length (); i++)
1028 if (fixups[i].pointer_dtd == dtd)
1029 fixups.unordered_remove (i);
1030
1031 /* Add the concrete base type. */
1032 dtd->ref_type = btf_add_used_type (ctfc, dtd->ref_type, check_ptr,
1033 seen_ptr, create_fixups);
1034 return dtd;
1035 }
1036 default:
1037 return dtd;
1038 }
1039 }
1040
1041 if (ctf_kind == CTF_K_SLICE)
1042 {
1043 /* Bitfield. Add the underlying type to the used set, but leave
1044 the reference to the bitfield. The slice type won't be emitted,
1045 but we need the information in it when writing out the bitfield
1046 encoding. */
1047 btf_add_used_type (ctfc, dtd->dtd_u.dtu_slice.cts_type,
1048 check_ptr, seen_ptr, create_fixups);
1049 return dtd;
1050 }
1051
1052 /* Skip redundant definitions of void and types with no BTF encoding. */
1053 if ((kind == BTF_KIND_INT && dtd->dtd_data.ctti_size == 0)
1054 || (kind == BTF_KIND_UNKN))
1055 return NULL;
1056
1057 /* Add the type itself, and assign its id.
1058 Do this before recursing to handle things like linked list structures. */
1059 gcc_assert (ctfc->ctfc_nextid <= BTF_MAX_TYPE);
1060 dtd->dtd_type = ctfc->ctfc_nextid++;
1061 btf_used_types->add (dtd);
1062 ctf_add_string (ctfc, dtd->dtd_name, &(dtd->dtd_data.ctti_name), CTF_STRTAB);
1063 ctfc->ctfc_num_types++;
1064 ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (dtd);
1065
1066 /* Recursively add types referenced by this type. */
1067 switch (kind)
1068 {
1069 case BTF_KIND_INT:
1070 case BTF_KIND_FLOAT:
1071 case BTF_KIND_FWD:
1072 /* Leaf kinds which do not refer to any other types. */
1073 break;
1074
1075 case BTF_KIND_FUNC:
1076 case BTF_KIND_VAR:
1077 /* Root kinds; no type we are visiting may refer to these. */
1078 gcc_unreachable ();
1079
1080 case BTF_KIND_PTR:
1081 case BTF_KIND_TYPEDEF:
1082 case BTF_KIND_CONST:
1083 case BTF_KIND_VOLATILE:
1084 case BTF_KIND_RESTRICT:
1085 {
1086 /* These type kinds refer to exactly one other type. */
1087 if (check_ptr && !seen_ptr)
1088 seen_ptr = (kind == BTF_KIND_PTR);
1089
1090 /* Try to avoid chasing pointers to struct/union types if the
1091 underlying type isn't used. */
1092 if (check_ptr && seen_ptr && create_fixups)
1093 {
1094 ctf_dtdef_ref ref = dtd->ref_type;
1095 uint32_t ref_kind = btf_dtd_kind (ref);
1096
1097 if ((ref_kind == BTF_KIND_STRUCT || ref_kind == BTF_KIND_UNION)
1098 && !btf_used_types->contains (ref))
1099 {
1100 struct btf_fixup fixup;
1101 fixup.pointer_dtd = dtd;
1102 fixup.pointee_dtd = ref;
1103 fixups.safe_push (fixup);
1104 break;
1105 }
1106 }
1107
1108 /* Add the type to which this type refers. */
1109 dtd->ref_type = btf_add_used_type (ctfc, dtd->ref_type, check_ptr,
1110 seen_ptr, create_fixups);
1111 break;
1112 }
1113 case BTF_KIND_ARRAY:
1114 {
1115 /* Add element and index types. */
1116 ctf_arinfo_t *arr = &(dtd->dtd_u.dtu_arr);
1117 arr->ctr_contents = btf_add_used_type (ctfc, arr->ctr_contents, false,
1118 false, create_fixups);
1119 arr->ctr_index = btf_add_used_type (ctfc, arr->ctr_index, false, false,
1120 create_fixups);
1121 break;
1122 }
1123 case BTF_KIND_STRUCT:
1124 case BTF_KIND_UNION:
1125 case BTF_KIND_ENUM:
1126 case BTF_KIND_ENUM64:
1127 {
1128 /* Add members. */
1129 ctf_dmdef_t *dmd;
1130 for (dmd = dtd->dtd_u.dtu_members;
1131 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
1132 {
1133 /* Add member type for struct/union members. For enums, only the
1134 enumerator names are needed. */
1135 if (kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION)
1136 dmd->dmd_type = btf_add_used_type (ctfc, dmd->dmd_type, true,
1137 false, create_fixups);
1138 ctf_add_string (ctfc, dmd->dmd_name, &(dmd->dmd_name_offset),
1139 CTF_STRTAB);
1140 }
1141 break;
1142 }
1143 case BTF_KIND_FUNC_PROTO:
1144 {
1145 /* Add return type. */
1146 dtd->ref_type = btf_add_used_type (ctfc, dtd->ref_type, false, false,
1147 create_fixups);
1148
1149 /* Add arg types. */
1150 ctf_func_arg_t * farg;
1151 for (farg = dtd->dtd_u.dtu_argv;
1152 farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
1153 {
1154 farg->farg_type = btf_add_used_type (ctfc, farg->farg_type, false,
1155 false, create_fixups);
1156 /* Note: argument names are stored in the auxilliary string table,
1157 since CTF does not include arg names. That table has not been
1158 cleared, so no need to re-add argument names here. */
1159 }
1160 break;
1161 }
1162 default:
1163 return NULL;
1164 }
1165
1166 return dtd;
1167 }
1168
1169 /* Initial entry point of BTF generation, called at early_finish () after
1170 CTF information has possibly been output. Translate all CTF information
1171 to BTF, and do any processing that must be done early, such as creating
1172 BTF_KIND_FUNC records. */
1173
1174 void
1175 btf_early_finish (void)
1176 {
1177 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1178
1179 vec_alloc (funcs, 16);
1180 func_map = hash_map<ctf_dtdef_ref, ctf_dtdef_ref>::create_ggc (16);
1181
1182 /* Note: from this point on, destructive changes are made to the TU CTFC to
1183 translate CTF to BTF. If CTF debug info has also been requested, it must
1184 be emitted before starting the translation to BTF. */
1185 btf_add_const_void (tu_ctfc);
1186 btf_add_func_records (tu_ctfc);
1187
1188 /* These fields are reset to count BTF types etc. */
1189 tu_ctfc->ctfc_num_types = 0;
1190 tu_ctfc->ctfc_num_vlen_bytes = 0;
1191 tu_ctfc->ctfc_vars_list_count = 0;
1192
1193 if (debug_prune_btf)
1194 {
1195 btf_used_types
1196 = hash_set<ctf_dtdef_ref>::create_ggc (tu_ctfc->ctfc_types->elements ());
1197 tu_ctfc->ctfc_nextid = 1;
1198 fixups.create (1);
1199
1200 /* Empty the string table, which was already populated with strings for
1201 all types translated from DWARF. We may only need a very small subset
1202 of these strings; those will be re-added below. */
1203 ctfc_delete_strtab (&tu_ctfc->ctfc_strtable);
1204 init_ctf_strtable (&tu_ctfc->ctfc_strtable);
1205 tu_ctfc->ctfc_strlen++;
1206 }
1207 }
1208
1209 /* Push a BTF datasec entry ENTRY into the datasec named SECNAME,
1210 creating the datasec record if it does not already exist. */
1211
1212 static void
1213 btf_datasec_push_entry (ctf_container_ref ctfc, const char *secname,
1214 struct btf_datasec_entry entry)
1215 {
1216 if (secname == NULL)
1217 return;
1218
1219 /* If we already have a datasec record for the appropriate section,
1220 append the new entry to it. */
1221 for (size_t i = 0; i < datasecs.length (); i++)
1222 if (strcmp (datasecs[i].name, secname) == 0)
1223 {
1224 datasecs[i].entries.safe_push (entry);
1225 return;
1226 }
1227
1228 /* If we don't already have a datasec record for secname, make one. */
1229 uint32_t str_off;
1230 ctf_add_string (ctfc, secname, &str_off, CTF_AUX_STRTAB);
1231 if (strcmp (secname, ""))
1232 ctfc->ctfc_aux_strlen += strlen (secname) + 1;
1233
1234 /* Note: ID will be assigned just before output. */
1235 btf_datasec_t ds;
1236 ds.name = secname;
1237 ds.name_offset = str_off;
1238
1239 /* Insert the entry into the new datasec record. */
1240 ds.entries.create (1);
1241 ds.entries.quick_push (entry);
1242
1243 /* Insert the datasec record itself. */
1244 datasecs.safe_push (ds);
1245 }
1246
1247 /* Create a datasec entry for a function, and insert it into the datasec
1248 record for the appropriate section. Create the record if it does not
1249 yet exist. */
1250
1251 static void
1252 btf_datasec_add_func (ctf_container_ref ctfc, cgraph_node *func,
1253 ctf_dtdef_ref func_dtd)
1254 {
1255 const char *section_name = get_section_name (func);
1256
1257 /* Note: get_section_name () returns NULL for functions in text
1258 section. This is intentional, since we do not want to generate
1259 DATASEC entries for them. */
1260 if (section_name == NULL)
1261 return;
1262
1263 struct btf_datasec_entry entry;
1264 gcc_assert (func_dtd);
1265 entry.dtd = func_dtd;
1266 entry.is_var = false;
1267
1268 /* Size is left as zero at compile time, to be filled in by loaders
1269 such as libbpf. */
1270 entry.size = 0;
1271
1272 btf_datasec_push_entry (ctfc, section_name, entry);
1273 }
1274
1275 /* Create a datasec entry for a variable, and insert it into the datasec
1276 record for the appropriate section. Create the record if it does not
1277 yet exist. */
1278
1279 static void
1280 btf_datasec_add_var (ctf_container_ref ctfc, varpool_node *var,
1281 ctf_dvdef_ref dvd)
1282 {
1283 /* PR112849: avoid assuming a section for extern decls without
1284 an explicit section, which would result in incorrectly
1285 emitting a BTF_KIND_DATASEC entry for them. */
1286 if (DECL_EXTERNAL (var->decl) && var->get_section () == NULL)
1287 return;
1288
1289 const char *section_name = get_section_name (var);
1290 if (section_name == NULL)
1291 return;
1292
1293 gcc_assert (dvd);
1294 struct btf_datasec_entry entry;
1295 entry.dvd = dvd;
1296 entry.is_var = true;
1297 entry.size = 0;
1298
1299 tree size = DECL_SIZE_UNIT (var->decl);
1300 if (tree_fits_uhwi_p (size))
1301 entry.size = tree_to_uhwi (size);
1302 else if (VOID_TYPE_P (TREE_TYPE (var->decl)))
1303 entry.size = 1;
1304
1305 btf_datasec_push_entry (ctfc, section_name, entry);
1306 }
1307
1308 /* Add datasec entries for functions to CTFC. */
1309
1310 static void
1311 btf_add_func_datasec_entries (ctf_container_ref ctfc)
1312 {
1313 /* We need to create FUNC records at early_finish, so that we have them
1314 even for functions which are later inlined by optimization passes.
1315 But on the other hand, we do not want datasec entries for such functions,
1316 so only create the datasec entries for them late. This loop will not
1317 hit functions which have already been inlined. */
1318 cgraph_node *func;
1319 FOR_EACH_FUNCTION (func)
1320 {
1321 dw_die_ref die = lookup_decl_die (func->decl);
1322 if (die == NULL)
1323 continue;
1324
1325 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
1326 if (dtd == NULL)
1327 continue;
1328
1329 ctf_dtdef_ref *pdtd = func_map->get (dtd);
1330 if (pdtd && DECL_EXTERNAL (func->decl))
1331 btf_datasec_add_func (ctfc, func, *pdtd);
1332 }
1333 }
1334
1335 /* Helper function used to determine whether or not a BTF_KIND_VAR record
1336 for the variable VAR shall be emitted. */
1337
1338 static bool
1339 btf_emit_variable_p (ctf_container_ref ctfc, varpool_node *var,
1340 ctf_dvdef_ref *pdvd)
1341 {
1342 dw_die_ref die = lookup_decl_die (var->decl);
1343 if (die == NULL)
1344 return false;
1345
1346 ctf_dvdef_ref dvd = ctf_dvd_lookup (ctfc, die);
1347 if (dvd == NULL)
1348 return false;
1349
1350 /* If this is an extern variable declaration with a defining declaration
1351 later, skip it so that only the defining declaration is emitted.
1352 This is the same case, fix and reasoning as in CTF; see PR105089. */
1353 if (ctf_dvd_ignore_lookup (ctfc, dvd->dvd_key))
1354 return false;
1355
1356 /* Skip variables with unrepresentable types. */
1357 if (!btf_emit_type_p (dvd->dvd_type))
1358 return false;
1359
1360 *pdvd = dvd;
1361 return true;
1362 }
1363
1364 /* Add BTF_KIND_VAR records for variables. */
1365
1366 static void
1367 btf_add_vars (ctf_container_ref ctfc)
1368 {
1369 size_t num_ctf_vars = ctfc->ctfc_vars->elements ();
1370
1371 ctfc->ctfc_vars_list = ggc_vec_alloc<ctf_dvdef_ref>(num_ctf_vars);
1372
1373 varpool_node *var;
1374 ctf_dvdef_ref dvd;
1375 FOR_EACH_VARIABLE (var)
1376 {
1377 if (!btf_emit_variable_p (ctfc, var, &dvd))
1378 continue;
1379
1380 /* Mark 'extern' variables. */
1381 if (DECL_EXTERNAL (var->decl))
1382 dvd->dvd_visibility = BTF_VAR_GLOBAL_EXTERN;
1383
1384 /* Add the variable to the vars list. */
1385 ctfc->ctfc_vars_list[ctfc->ctfc_vars_list_count++] = dvd;
1386
1387 /* Add a BTF_KIND_DATASEC entry for the variable. */
1388 btf_datasec_add_var (ctfc, var, dvd);
1389
1390 const char *section = var->get_section ();
1391 if (section && (strcmp (section, ".maps") == 0) && debug_prune_btf)
1392 {
1393 /* The .maps section has special meaning in BTF: it is used for BPF
1394 map definitions. These definitions should be structs. We must
1395 collect pointee types used in map members as though they are used
1396 directly, effectively ignoring (from the pruning perspective) that
1397 they are struct members. */
1398 ctf_dtdef_ref dtd = dvd->dvd_type;
1399 uint32_t kind = btf_dtd_kind (dvd->dvd_type);
1400 if (kind == BTF_KIND_STRUCT)
1401 {
1402 ctf_dmdef_t *dmd;
1403 for (dmd = dtd->dtd_u.dtu_members;
1404 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
1405 btf_add_used_type (ctfc, dmd->dmd_type, false, false, true);
1406 }
1407 }
1408 }
1409 }
1410
1411 /* Callback used by btf_assign_type_ids to insert types into their initial
1412 positions in the type list. */
1413
1414 static int
1415 btf_type_list_cb (ctf_dtdef_ref *slot, ctf_container_ref ctfc)
1416 {
1417 ctf_dtdef_ref dtd = *slot;
1418 ctfc->ctfc_types_list[dtd->dtd_type] = dtd;
1419 return 1;
1420 }
1421
1422 /* Construct the initial type list and assign BTF IDs for all types translated
1423 from CTF. */
1424
1425 static void
1426 btf_collect_translated_types (ctf_container_ref ctfc)
1427 {
1428 size_t num_ctf_types = ctfc->ctfc_types->elements ();
1429
1430 /* First, place each type at its CTF-assigned index in the list.
1431 The '+1' here and below is to account for the implicit void type with
1432 ID 0. There is no real type at index 0 in the list. */
1433 ctfc->ctfc_types_list = ggc_vec_alloc<ctf_dtdef_ref>(num_ctf_types + 1);
1434 ctfc->ctfc_types->traverse<ctf_container_ref, btf_type_list_cb> (ctfc);
1435
1436 /* Now, pass through the list and adjust IDs to account for types which will
1437 not be emitted. This results in each type that will be emitted in BTF
1438 being assigned an appropriate ID. Note that types which will not be
1439 emitted remain in the list; they are skipped at output time. */
1440 unsigned int skip = 0;
1441 for (size_t i = 1; i <= num_ctf_types; i++)
1442 {
1443 ctf_dtdef_ref dtd = ctfc->ctfc_types_list[i];
1444 if (!btf_emit_type_p (dtd))
1445 {
1446 dtd->dtd_type = BTF_INVALID_TYPEID;
1447 skip += 1;
1448 continue;
1449 }
1450
1451 dtd->dtd_type -= skip;
1452 ctfc->ctfc_num_types++;
1453 ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (dtd);
1454 }
1455
1456 max_translated_id = ctfc->ctfc_num_types;
1457 ctfc->ctfc_nextid = ctfc->ctfc_num_types + 1;
1458 }
1459
1460 /* Assign BTF IDs for FUNC records and account for their size. */
1461
1462 static void
1463 btf_assign_func_ids (ctf_container_ref ctfc)
1464 {
1465 ctf_dtdef_ref dtd;
1466 unsigned int i;
1467 FOR_EACH_VEC_ELT (*funcs, i, dtd)
1468 {
1469 dtd->dtd_type = ctfc->ctfc_nextid++;
1470 ctfc->ctfc_num_types++;
1471 }
1472 }
1473
1474 /* Assign BTF IDs for variables and account for their size. */
1475
1476 static void
1477 btf_assign_var_ids (ctf_container_ref ctfc)
1478 {
1479 for (size_t i = 0; i < ctfc->ctfc_vars_list_count; i++)
1480 {
1481 ctf_dvdef_ref dvd = ctfc->ctfc_vars_list[i];
1482 ctf_id_t id = ctfc->ctfc_nextid++;
1483 gcc_assert (id <= BTF_MAX_TYPE);
1484 dvd->dvd_id = id;
1485
1486 ctfc->ctfc_num_types++;
1487 ctfc->ctfc_num_vlen_bytes += sizeof (struct btf_var);
1488 }
1489 }
1490
1491 /* Assign BTF IDs for datasec records and account for their size. */
1492
1493 static void
1494 btf_assign_datasec_ids (ctf_container_ref ctfc)
1495 {
1496 for (size_t i = 0; i < datasecs.length (); i++)
1497 {
1498 datasecs[i].id = ctfc->ctfc_nextid++;
1499 datasecs[i].name_offset += ctfc_get_strtab_len (ctfc, CTF_STRTAB);
1500 ctfc->ctfc_num_types++;
1501 ctfc->ctfc_num_vlen_bytes += (datasecs[i].entries.length ()
1502 * sizeof (struct btf_var_secinfo));
1503 }
1504 }
1505
1506
1507 /* Manually mark that type T is used to ensure it will not be pruned.
1508 Used by the BPF backend when generating BPF CO-RE to mark types used
1509 in CO-RE relocations. */
1510
1511 void
1512 btf_mark_type_used (tree t)
1513 {
1514 /* If we are not going to prune anyway, this is a no-op. */
1515 if (!debug_prune_btf)
1516 return;
1517
1518 gcc_assert (TYPE_P (t));
1519 ctf_container_ref ctfc = ctf_get_tu_ctfc ();
1520 ctf_dtdef_ref dtd = ctf_lookup_tree_type (ctfc, t);
1521
1522 if (!dtd)
1523 return;
1524
1525 btf_add_used_type (ctfc, dtd, false, false, true);
1526 }
1527
1528 /* Callback used for assembling the only-used-types list. Note that this is
1529 the same as btf_type_list_cb above, but the hash_set traverse requires a
1530 different function signature. */
1531
1532 static bool
1533 btf_used_type_list_cb (const ctf_dtdef_ref& dtd, ctf_container_ref ctfc)
1534 {
1535 ctfc->ctfc_types_list[dtd->dtd_type] = dtd;
1536 return true;
1537 }
1538
1539 /* Collect the set of types reachable from global variables and functions.
1540 This is the minimal set of types, used when generating pruned BTF. */
1541
1542 static void
1543 btf_collect_pruned_types (ctf_container_ref ctfc)
1544 {
1545 vec_alloc (forwards, 1);
1546
1547 /* Add types used from functions. */
1548 ctf_dtdef_ref dtd;
1549 size_t i;
1550 FOR_EACH_VEC_ELT (*funcs, i, dtd)
1551 {
1552 btf_add_used_type (ctfc, dtd->ref_type, false, false, true);
1553 ctf_add_string (ctfc, dtd->dtd_name, &(dtd->dtd_data.ctti_name),
1554 CTF_STRTAB);
1555 }
1556
1557 /* Add types used from global variables. */
1558 for (i = 0; i < ctfc->ctfc_vars_list_count; i++)
1559 {
1560 ctf_dvdef_ref dvd = ctfc->ctfc_vars_list[i];
1561 btf_add_used_type (ctfc, dvd->dvd_type, false, false, true);
1562 ctf_add_string (ctfc, dvd->dvd_name, &(dvd->dvd_name_offset), CTF_STRTAB);
1563 }
1564
1565 /* Process fixups. If the base type was never added, create a forward for it
1566 and adjust the reference to point to that. If it was added, then nothing
1567 needs to change. */
1568 for (i = 0; i < fixups.length (); i++)
1569 {
1570 struct btf_fixup *fx = &fixups[i];
1571 if (!btf_used_types->contains (fx->pointee_dtd))
1572 {
1573 /* The underlying type is not used. Create a forward. */
1574 ctf_dtdef_ref fwd = ggc_cleared_alloc<ctf_dtdef_t> ();
1575 ctf_id_t id = ctfc->ctfc_nextid++;
1576 gcc_assert (id <= BTF_MAX_TYPE);
1577
1578 bool union_p = (btf_dtd_kind (fx->pointee_dtd) == BTF_KIND_UNION);
1579
1580 fwd->dtd_name = fx->pointee_dtd->dtd_name;
1581 fwd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_FORWARD, union_p, 0);
1582 fwd->dtd_type = id;
1583 ctfc->ctfc_num_types++;
1584 ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (fwd);
1585 ctf_add_string (ctfc, fwd->dtd_name, &(fwd->dtd_data.ctti_name),
1586 CTF_STRTAB);
1587
1588 /* Update the pointer to point to the forward. */
1589 fx->pointer_dtd->ref_type = fwd;
1590 vec_safe_push (forwards, fwd);
1591 }
1592 }
1593
1594 /* Construct the resulting pruned type list. */
1595 ctfc->ctfc_types_list
1596 = ggc_vec_alloc<ctf_dtdef_ref> (btf_used_types->elements () + 1
1597 + vec_safe_length (forwards));
1598
1599 btf_used_types->traverse<ctf_container_ref, btf_used_type_list_cb> (ctfc);
1600
1601 /* Insert the newly created forwards into the regular types list too. */
1602 FOR_EACH_VEC_ELT (*forwards, i, dtd)
1603 ctfc->ctfc_types_list[dtd->dtd_type] = dtd;
1604
1605 max_translated_id = btf_used_types->elements () + vec_safe_length (forwards);
1606 }
1607
1608 /* Late entry point for BTF generation, called from dwarf2out_finish ().
1609 Complete and emit BTF information. */
1610
1611 void
1612 btf_finish (void)
1613 {
1614 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1615 init_btf_sections ();
1616
1617 datasecs.create (0);
1618
1619 btf_add_vars (tu_ctfc);
1620 if (debug_prune_btf)
1621 {
1622 /* Collect pruned set of BTF types and prepare for emission.
1623 This includes only types directly used in file-scope variables and
1624 function return/argument types. */
1625 btf_collect_pruned_types (tu_ctfc);
1626 }
1627 else
1628 {
1629 /* Collect all BTF types and prepare for emission.
1630 This includes all types translated from DWARF. */
1631 btf_collect_translated_types (tu_ctfc);
1632 }
1633 btf_add_func_datasec_entries (tu_ctfc);
1634
1635 btf_assign_var_ids (tu_ctfc);
1636 btf_assign_func_ids (tu_ctfc);
1637 btf_assign_datasec_ids (tu_ctfc);
1638
1639 /* Finally, write out the complete .BTF section. */
1640 btf_output (tu_ctfc);
1641
1642 /* If compiling for BPF with CO-RE info, we cannot deallocate until after the
1643 contents of the .BTF.ext section are finalized, which happens very late in
1644 BPF backend. Therefore, the deallocation (i.e. btf_finalize ()) is delayed
1645 until TARGET_ASM_FILE_END for BPF CO-RE. */
1646 if (!btf_with_core_debuginfo_p ())
1647 btf_finalize ();
1648 }
1649
1650 /* Reset all state for BTF generation so that we can rerun the compiler within
1651 the same process. */
1652
1653 void
1654 btf_finalize (void)
1655 {
1656 btf_info_section = NULL;
1657 max_translated_id = 0;
1658
1659 for (size_t i = 0; i < datasecs.length (); i++)
1660 datasecs[i].entries.release ();
1661 datasecs.release ();
1662
1663 funcs = NULL;
1664 if (func_map)
1665 {
1666 func_map->empty ();
1667 func_map = NULL;
1668 }
1669
1670 if (debug_prune_btf)
1671 {
1672 if (btf_used_types)
1673 {
1674 btf_used_types->empty ();
1675 btf_used_types = NULL;
1676 }
1677
1678 fixups.release ();
1679 forwards = NULL;
1680 }
1681
1682 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1683 ctfc_delete_container (tu_ctfc);
1684 tu_ctfc = NULL;
1685 }
1686
1687 /* Traversal function for all BTF_KIND_FUNC type records. */
1688
1689 bool
1690 traverse_btf_func_types (funcs_traverse_callback callback, void *data)
1691 {
1692 ctf_dtdef_ref ref;
1693 unsigned i;
1694 FOR_EACH_VEC_ELT (*funcs, i, ref)
1695 {
1696 bool stop = callback (ref, data);
1697 if (stop == true)
1698 return true;
1699 }
1700 return false;
1701 }
1702
1703 #include "gt-btfout.h"