1 /* Output BTF format from GCC.
2    Copyright (C) 2021 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.c, 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 "dwarf2out.h" /* For lookup_decl_die.  */
39 
40 static int btf_label_num;
41 
42 static GTY (()) section * btf_info_section;
43 
44 /* BTF debug info section.  */
45 
46 #ifndef BTF_INFO_SECTION_NAME
47 #define BTF_INFO_SECTION_NAME  ".BTF"
48 #endif
49 
50 #define BTF_INFO_SECTION_FLAGS (SECTION_DEBUG)
51 
52 /* Maximum size (in bytes) for an artifically generated BTF label.  */
53 
54 #define MAX_BTF_LABEL_BYTES 40
55 
56 static char btf_info_section_label[MAX_BTF_LABEL_BYTES];
57 
58 #ifndef BTF_INFO_SECTION_LABEL
59 #define BTF_INFO_SECTION_LABEL  "Lbtf"
60 #endif
61 
62 /* BTF encodes void as type id 0.  */
63 
64 #define BTF_VOID_TYPEID 0
65 #define BTF_INIT_TYPEID 1
66 
67 #define BTF_INVALID_TYPEID 0xFFFFFFFF
68 
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;
74 
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;
79 
80 /* Information for creating the BTF_KIND_DATASEC records.  */
81 typedef struct btf_datasec
82 {
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;
87 
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;
91 
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;
95 
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;
100 
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;
106 
107 /* The number of BTF variables added to the TU CTF container.  */
108 static unsigned int num_vars_added = 0;
109 
110 /* The number of BTF types added to the TU CTF container.  */
111 static unsigned int num_types_added = 0;
112 
113 /* The number of types synthesized for BTF that do not correspond to
114    CTF types.  */
115 static unsigned int num_types_created = 0;
116 
117 /* Map a CTF type kind to the corresponding BTF type kind.  */
118 
119 static uint32_t
get_btf_kind(uint32_t ctf_kind)120 get_btf_kind (uint32_t ctf_kind)
121 {
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)
125     {
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:;
140     }
141   return BTF_KIND_UNKN;
142 }
143 
144 /* Allocate the btf_id_map, and initialize elements to BTF_INVALID_TYPEID.  */
145 
146 static void
init_btf_id_map(size_t len)147 init_btf_id_map (size_t len)
148 {
149   btf_id_map = XNEWVEC (ctf_id_t, len);
150 
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;
154 }
155 
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.  */
158 
159 ctf_id_t
get_btf_id(ctf_id_t key)160 get_btf_id (ctf_id_t key)
161 {
162   return btf_id_map[key];
163 }
164 
165 /* Set the CTF type ID KEY to map to BTF type ID VAL.  */
166 
167 static inline void
set_btf_id(ctf_id_t key,ctf_id_t val)168 set_btf_id (ctf_id_t key, ctf_id_t val)
169 {
170   btf_id_map[key] = val;
171 }
172 
173 /* Return TRUE iff the given CTF type ID maps to a BTF type which will
174    be emitted.  */
175 static inline bool
btf_emit_id_p(ctf_id_t id)176 btf_emit_id_p (ctf_id_t id)
177 {
178   return ((btf_id_map[id] != BTF_VOID_TYPEID)
179 	  && (btf_id_map[id] <= BTF_MAX_TYPE));
180 }
181 
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.  */
185 
186 static uint64_t
btf_calc_num_vbytes(ctf_dtdef_ref dtd)187 btf_calc_num_vbytes (ctf_dtdef_ref dtd)
188 {
189   uint64_t vlen_bytes = 0;
190 
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);
193 
194   switch (kind)
195     {
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;
206 
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;
212 
213       vlen_bytes += sizeof (uint32_t);
214       break;
215 
216     case BTF_KIND_ARRAY:
217       vlen_bytes += sizeof (struct btf_array);
218       break;
219 
220     case BTF_KIND_STRUCT:
221     case BTF_KIND_UNION:
222       vlen_bytes += vlen * sizeof (struct btf_member);
223       break;
224 
225     case BTF_KIND_ENUM:
226       vlen_bytes += vlen * sizeof (struct btf_enum);
227       break;
228 
229     case BTF_KIND_FUNC_PROTO:
230       vlen_bytes += vlen * sizeof (struct btf_param);
231       break;
232 
233     case BTF_KIND_VAR:
234       vlen_bytes += sizeof (struct btf_var);
235       break;
236 
237     case BTF_KIND_DATASEC:
238       vlen_bytes += vlen * sizeof (struct btf_var_secinfo);
239       break;
240 
241     default:
242       break;
243     }
244   return vlen_bytes;
245 }
246 
247 /* Initialize BTF section (.BTF) for output.  */
248 
249 void
init_btf_sections(void)250 init_btf_sections (void)
251 {
252   btf_info_section = get_section (BTF_INFO_SECTION_NAME, BTF_INFO_SECTION_FLAGS,
253 				  NULL);
254 
255   ASM_GENERATE_INTERNAL_LABEL (btf_info_section_label,
256 			       BTF_INFO_SECTION_LABEL, btf_label_num++);
257 }
258 
259 /* Push a BTF datasec variable entry INFO into the datasec named SECNAME,
260    creating the datasec if it does not already exist.  */
261 
262 static void
btf_datasec_push_entry(ctf_container_ref ctfc,const char * secname,struct btf_var_secinfo info)263 btf_datasec_push_entry (ctf_container_ref ctfc, const char *secname,
264 			struct btf_var_secinfo info)
265 {
266   if (secname == NULL)
267     return;
268 
269   for (size_t i = 0; i < datasecs.length (); i++)
270     if (strcmp (datasecs[i].name, secname) == 0)
271       {
272 	datasecs[i].entries.safe_push (info);
273 	return;
274       }
275 
276   /* If we don't already have a datasec record for secname, make one.  */
277 
278   uint32_t str_off;
279   ctf_add_string (ctfc, secname, &str_off, CTF_AUX_STRTAB);
280   if (strcmp (secname, ""))
281     ctfc->ctfc_aux_strlen += strlen (secname) + 1;
282 
283   btf_datasec_t ds;
284   ds.name = secname;
285   ds.name_offset = str_off;
286 
287   ds.entries.create (0);
288   ds.entries.safe_push (info);
289 
290   datasecs.safe_push (ds);
291   num_types_created++;
292 }
293 
294 /* Construct all BTF_KIND_DATASEC records for CTFC. One such record is created
295    for each non-empty data-containing section in the output. Each record is
296    followed by a variable number of entries describing the variables stored
297    in that section.  */
298 
299 static void
btf_collect_datasec(ctf_container_ref ctfc)300 btf_collect_datasec (ctf_container_ref ctfc)
301 {
302   /* See cgraph.h struct symtab_node, which varpool_node extends.  */
303   varpool_node *node;
304   FOR_EACH_VARIABLE (node)
305     {
306       dw_die_ref die = lookup_decl_die (node->decl);
307       if (die == NULL)
308 	continue;
309 
310       ctf_dvdef_ref dvd = ctf_dvd_lookup (ctfc, die);
311       if (dvd == NULL)
312 	continue;
313 
314       const char *section_name = node->get_section ();
315 
316       if (section_name == NULL)
317 	{
318 	  switch (categorize_decl_for_section (node->decl, 0))
319 	    {
320 	    case SECCAT_BSS:
321 	      section_name = ".bss";
322 	      break;
323 	    case SECCAT_DATA:
324 	      section_name = ".data";
325 	      break;
326 	    case SECCAT_RODATA:
327 	      section_name = ".rodata";
328 	      break;
329 	    default:
330 	      continue;
331 	    }
332 	}
333 
334       struct btf_var_secinfo info;
335 
336       info.type = 0;
337       unsigned int *var_id = btf_var_ids->get (dvd);
338       if (var_id)
339 	/* +1 for the sentinel type not in the types map.  */
340 	info.type = *var_id + num_types_added + 1;
341       else
342 	continue;
343 
344       info.size = 0;
345       tree size = DECL_SIZE_UNIT (node->decl);
346       if (tree_fits_uhwi_p (size))
347 	info.size = tree_to_uhwi (size);
348 
349       /* Offset is left as 0 at compile time, to be filled in by loaders such
350 	 as libbpf.  */
351       info.offset = 0;
352 
353       btf_datasec_push_entry (ctfc, section_name, info);
354     }
355 }
356 
357 /* Return true if the type ID is that of a type which will not be emitted (for
358    example, if it is not representable in BTF).  */
359 
360 static bool
btf_removed_type_p(ctf_id_t id)361 btf_removed_type_p (ctf_id_t id)
362 {
363   return holes.contains (id);
364 }
365 
366 /* Adjust the given type ID to account for holes and duplicate definitions of
367    void.  */
368 
369 static ctf_id_t
btf_adjust_type_id(ctf_id_t id)370 btf_adjust_type_id (ctf_id_t id)
371 {
372   size_t n;
373   ctf_id_t i = 0;
374 
375   /* Do not adjust invalid type markers.  */
376   if (id == BTF_INVALID_TYPEID)
377     return id;
378 
379   for (n = 0; n < voids.length (); n++)
380     if (id == voids[n])
381       return BTF_VOID_TYPEID;
382 
383   for (n = 0; n < holes.length (); n++)
384     {
385       if (holes[n] < id)
386 	i++;
387       else if (holes[n] == id)
388 	return BTF_VOID_TYPEID;
389     }
390 
391   return id - i;
392 }
393 
394 /* Postprocessing callback routine for types.  */
395 
396 int
btf_dtd_postprocess_cb(ctf_dtdef_ref * slot,ctf_container_ref arg_ctfc)397 btf_dtd_postprocess_cb (ctf_dtdef_ref *slot, ctf_container_ref arg_ctfc)
398 {
399   ctf_dtdef_ref ctftype = (ctf_dtdef_ref) * slot;
400 
401   size_t index = ctftype->dtd_type;
402   gcc_assert (index <= arg_ctfc->ctfc_types->elements ());
403 
404   uint32_t ctf_kind, btf_kind;
405 
406   ctf_kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
407   btf_kind = get_btf_kind (ctf_kind);
408 
409   if (btf_kind == BTF_KIND_UNKN)
410     /* This type is not representable in BTF. Create a hole.  */
411     holes.safe_push (ctftype->dtd_type);
412 
413   else if (btf_kind == BTF_KIND_INT && ctftype->dtd_data.ctti_size == 0)
414     {
415       /* This is a (redundant) definition of void.  */
416       voids.safe_push (ctftype->dtd_type);
417       holes.safe_push (ctftype->dtd_type);
418     }
419 
420   arg_ctfc->ctfc_types_list[index] = ctftype;
421 
422   return 1;
423 }
424 
425 /* Preprocessing callback routine for variables.  */
426 
427 int
btf_dvd_emit_preprocess_cb(ctf_dvdef_ref * slot,ctf_container_ref arg_ctfc)428 btf_dvd_emit_preprocess_cb (ctf_dvdef_ref *slot, ctf_container_ref arg_ctfc)
429 {
430   ctf_dvdef_ref var = (ctf_dvdef_ref) * slot;
431 
432   /* Do not add variables which refer to unsupported types.  */
433   if (btf_removed_type_p (var->dvd_type))
434     return 1;
435 
436   arg_ctfc->ctfc_vars_list[num_vars_added] = var;
437   btf_var_ids->put (var, num_vars_added);
438 
439   num_vars_added++;
440   num_types_created++;
441 
442   return 1;
443 }
444 
445 /* Preprocessing callback routine for types.  */
446 
447 static void
btf_dtd_emit_preprocess_cb(ctf_container_ref ctfc,ctf_dtdef_ref dtd)448 btf_dtd_emit_preprocess_cb (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
449 {
450   if (!btf_emit_id_p (dtd->dtd_type))
451     return;
452 
453   uint32_t btf_kind
454     = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
455 
456   if (btf_kind == BTF_KIND_FUNC_PROTO)
457     {
458       /* Functions actually get two types: a BTF_KIND_FUNC_PROTO, and
459 	 also a BTF_KIND_FUNC. But the CTF container only allocates one
460 	 type per function, which matches closely with BTF_KIND_FUNC_PROTO.
461 	 For each such function, also allocate a BTF_KIND_FUNC entry.
462 	 These will be output later.  */
463       ctf_dtdef_ref func_dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
464       func_dtd->dtd_data = dtd->dtd_data;
465       func_dtd->dtd_data.ctti_type = dtd->dtd_type;
466 
467       vec_safe_push (funcs, func_dtd);
468       num_types_created++;
469 
470       /* Only the BTF_KIND_FUNC type actually references the name. The
471 	 BTF_KIND_FUNC_PROTO is always anonymous.  */
472       dtd->dtd_data.ctti_name = 0;
473     }
474 
475   ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (dtd);
476 }
477 
478 /* Preprocess the CTF information to prepare for BTF output.  BTF is almost a
479    subset of CTF, with many small differences in encoding, and lacking support
480    for some types (notably floating point formats).
481 
482    During the preprocessing pass:
483    - Ascertain that the sorted list of types has been prepared.  For the BTF
484      generation process, this is taken care of by the btf_init_postprocess ().
485 
486    - BTF_KIND_FUNC and BTF_KIND_DATASEC records are constructed. These types do
487      not have analogues in CTF (the analogous type to CTF_K_FUNCTION is
488      BTF_KIND_FUNC_PROTO), but can be relatively easily deduced from CTF
489      information.
490 
491    - Construct BTF_KIND_VAR records, representing variables.
492 
493    - Calculate the total size in bytes of variable-length information following
494      BTF type records. This is used for outputting the BTF header.
495 
496    After preprocessing, all BTF information is ready to be output:
497    - ctfc->ctfc_types_list holdstypes converted from CTF types. This does not
498      include KIND_VAR, KIND_FUNC, nor KIND_DATASEC types. These types have been
499      re-encoded to the appropriate representation in BTF.
500    - ctfc->ctfc_vars_list holds all variables which should be output.
501      Variables of unsupported types are not present in this list.
502    - Vector 'funcs' holds all BTF_KIND_FUNC types, one to match each
503      BTF_KIND_FUNC_PROTO.
504    - Vector 'datasecs' holds all BTF_KIND_DATASEC types.  */
505 
506 static void
btf_emit_preprocess(ctf_container_ref ctfc)507 btf_emit_preprocess (ctf_container_ref ctfc)
508 {
509   size_t num_ctf_types = ctfc->ctfc_types->elements ();
510   size_t num_ctf_vars = ctfc->ctfc_vars->elements ();
511   size_t i;
512 
513   if (num_ctf_types)
514     {
515       gcc_assert (ctfc->ctfc_types_list);
516       /* Preprocess the types.  */
517       for (i = 1; i <= num_ctf_types; i++)
518 	btf_dtd_emit_preprocess_cb (ctfc, ctfc->ctfc_types_list[i]);
519     }
520 
521   btf_var_ids = hash_map<ctf_dvdef_ref, unsigned int>::create_ggc (100);
522 
523   if (num_ctf_vars)
524     {
525       /* Allocate and construct the list of variables. While BTF variables are
526 	 not distinct from types (in that variables are simply types with
527 	 BTF_KIND_VAR), it is simpler to maintain a separate list of variables
528 	 and append them to the types list during output.  */
529       ctfc->ctfc_vars_list = ggc_vec_alloc<ctf_dvdef_ref>(num_ctf_vars);
530       ctfc->ctfc_vars->traverse<ctf_container_ref, btf_dvd_emit_preprocess_cb>
531 	(ctfc);
532 
533       ctfc->ctfc_num_vlen_bytes += (num_vars_added * sizeof (struct btf_var));
534     }
535 
536   btf_collect_datasec (ctfc);
537 }
538 
539 /* Return true iff DMD is a member description of a bit-field which can be
540    validly represented in BTF.  */
541 
542 static bool
btf_dmd_representable_bitfield_p(ctf_container_ref ctfc,ctf_dmdef_t * dmd)543 btf_dmd_representable_bitfield_p (ctf_container_ref ctfc, ctf_dmdef_t *dmd)
544 {
545   ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[dmd->dmd_type];
546 
547   if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
548     {
549       unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
550       unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
551       uint64_t sou_offset = dmd->dmd_offset;
552 
553       if ((bits > 0xff) || ((sou_offset + word_offset) > 0xffffff))
554 	return false;
555 
556       return true;
557     }
558 
559   return false;
560 }
561 
562 /* BTF asm helper routines.  */
563 
564 /* Asm'out a BTF type. This routine is responsible for the bulk of the task
565    of converting CTF types to their BTF representation.  */
566 
567 static void
btf_asm_type(ctf_container_ref ctfc,ctf_dtdef_ref dtd)568 btf_asm_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
569 {
570   uint32_t btf_kind, btf_kflag, btf_vlen, btf_size_type;
571   uint32_t ctf_info = dtd->dtd_data.ctti_info;
572 
573   btf_kind = get_btf_kind (CTF_V2_INFO_KIND (ctf_info));
574   btf_size_type = dtd->dtd_data.ctti_type;
575   btf_vlen = CTF_V2_INFO_VLEN (ctf_info);
576 
577   /* By now any unrepresentable types have been removed.  */
578   gcc_assert (btf_kind != BTF_KIND_UNKN);
579 
580   /* Size 0 integers are redundant definitions of void. None should remain
581      in the types list by this point.  */
582   gcc_assert (btf_kind != BTF_KIND_INT || btf_size_type >= 1);
583 
584   /* Re-encode the ctti_info to BTF.  */
585   /* kflag is 1 for structs/unions with a bitfield member.
586      kflag is 1 for forwards to unions.
587      kflag is 0 in all other cases.  */
588   btf_kflag = 0;
589 
590   if (btf_kind == BTF_KIND_STRUCT || btf_kind == BTF_KIND_UNION)
591     {
592       /* If a struct/union has ANY bitfield members, set kflag=1.
593 	 Note that we must also change the encoding of every member to encode
594 	 both member bitfield size (stealing most-significant 8 bits) and bit
595 	 offset (LS 24 bits). This is done during preprocessing.  */
596       ctf_dmdef_t *dmd;
597       for (dmd = dtd->dtd_u.dtu_members;
598 	   dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
599 	{
600 	  /* Set kflag if this member is a representable bitfield.  */
601 	  if (btf_dmd_representable_bitfield_p (ctfc, dmd))
602 	    btf_kflag = 1;
603 
604 	  /* Struct members that refer to unsupported types or bitfield formats
605 	     shall be skipped. These are marked during preprocessing.  */
606 	  else if (!btf_emit_id_p (dmd->dmd_type))
607 	    btf_vlen -= 1;
608 	}
609     }
610 
611   /* BTF forwards make use of KIND_FLAG to distinguish between forwards to
612      structs and forwards to unions. The dwarf2ctf conversion process stores
613      the kind of the forward in ctti_type, but for BTF this must be 0 for
614      forwards, with only the KIND_FLAG to distinguish.
615      At time of writing, BTF forwards to enums are unspecified.  */
616   if (btf_kind == BTF_KIND_FWD)
617     {
618       if (dtd->dtd_data.ctti_type == CTF_K_UNION)
619 	btf_kflag = 1;
620 
621       btf_size_type = 0;
622     }
623 
624   dw2_asm_output_data (4, dtd->dtd_data.ctti_name, "btt_name");
625   dw2_asm_output_data (4, BTF_TYPE_INFO (btf_kind, btf_kflag, btf_vlen),
626 		       "btt_info: kind=%u, kflag=%u, vlen=%u",
627 		       btf_kind, btf_kflag, btf_vlen);
628   switch (btf_kind)
629     {
630     case BTF_KIND_INT:
631     case BTF_KIND_FLOAT:
632     case BTF_KIND_STRUCT:
633     case BTF_KIND_UNION:
634     case BTF_KIND_ENUM:
635     case BTF_KIND_DATASEC:
636       dw2_asm_output_data (4, dtd->dtd_data.ctti_size, "btt_size: %uB",
637 			   dtd->dtd_data.ctti_size);
638       return;
639     default:
640       break;
641     }
642 
643   dw2_asm_output_data (4, get_btf_id (dtd->dtd_data.ctti_type), "btt_type");
644 }
645 
646 /* Asm'out the variable information following a BTF_KIND_ARRAY.  */
647 
648 static void
btf_asm_array(ctf_dtdef_ref dtd)649 btf_asm_array (ctf_dtdef_ref dtd)
650 {
651   dw2_asm_output_data (4, get_btf_id (dtd->dtd_u.dtu_arr.ctr_contents),
652 		       "bta_contents");
653   dw2_asm_output_data (4, get_btf_id (dtd->dtd_u.dtu_arr.ctr_index),
654 		       "bta_index");
655   dw2_asm_output_data (4, dtd->dtd_u.dtu_arr.ctr_nelems, "bta_nelems");
656 }
657 
658 /* Asm'out a BTF_KIND_VAR.  */
659 
660 static void
btf_asm_varent(ctf_dvdef_ref var)661 btf_asm_varent (ctf_dvdef_ref var)
662 {
663   dw2_asm_output_data (4, var->dvd_name_offset, "btv_name");
664   dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_VAR, 0, 0), "btv_info");
665   dw2_asm_output_data (4, get_btf_id (var->dvd_type), "btv_type");
666   dw2_asm_output_data (4, (var->dvd_visibility ? 1 : 0), "btv_linkage");
667 }
668 
669 /* Asm'out a member description following a BTF_KIND_STRUCT or
670    BTF_KIND_UNION.  */
671 
672 static void
btf_asm_sou_member(ctf_container_ref ctfc,ctf_dmdef_t * dmd)673 btf_asm_sou_member (ctf_container_ref ctfc, ctf_dmdef_t * dmd)
674 {
675   ctf_dtdef_ref ref_type = ctfc->ctfc_types_list[dmd->dmd_type];
676 
677   /* Re-encode bitfields to BTF representation.  */
678   if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
679     {
680       ctf_id_t base_type = ref_type->dtd_u.dtu_slice.cts_type;
681       unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
682       unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
683       uint64_t sou_offset = dmd->dmd_offset;
684 
685       /* Pack the bit offset and bitfield size together.  */
686       sou_offset += word_offset;
687 
688       /* If this bitfield cannot be represented, do not output anything.
689 	 The parent struct/union 'vlen' field has already been updated.  */
690       if ((bits > 0xff) || (sou_offset > 0xffffff))
691 	return;
692 
693       sou_offset &= 0x00ffffff;
694       sou_offset |= ((bits & 0xff) << 24);
695 
696       /* Refer to the base type of the slice.  */
697       dw2_asm_output_data (4, dmd->dmd_name_offset, "btm_name_off");
698       dw2_asm_output_data (4, get_btf_id (base_type), "btm_type");
699       dw2_asm_output_data (4, sou_offset, "btm_offset");
700     }
701   else
702     {
703       dw2_asm_output_data (4, dmd->dmd_name_offset, "btm_name_off");
704       dw2_asm_output_data (4, get_btf_id (dmd->dmd_type), "btm_type");
705       dw2_asm_output_data (4, dmd->dmd_offset, "btm_offset");
706     }
707 }
708 
709 /* Asm'out an enum constant following a BTF_KIND_ENUM.  */
710 
711 static void
btf_asm_enum_const(ctf_dmdef_t * dmd)712 btf_asm_enum_const (ctf_dmdef_t * dmd)
713 {
714   dw2_asm_output_data (4, dmd->dmd_name_offset, "bte_name");
715   dw2_asm_output_data (4, dmd->dmd_value, "bte_value");
716 }
717 
718 /* Asm'out a function parameter description following a BTF_KIND_FUNC_PROTO.  */
719 
720 static void
btf_asm_func_arg(ctf_func_arg_t * farg,size_t stroffset)721 btf_asm_func_arg (ctf_func_arg_t * farg, size_t stroffset)
722 {
723   /* If the function arg does not have a name, refer to the null string at
724      the start of the string table. This ensures correct encoding for varargs
725      '...' arguments.  */
726   if ((farg->farg_name != NULL) && strcmp (farg->farg_name, ""))
727     dw2_asm_output_data (4, farg->farg_name_offset + stroffset, "farg_name");
728   else
729     dw2_asm_output_data (4, 0, "farg_name");
730 
731   dw2_asm_output_data (4, (btf_removed_type_p (farg->farg_type)
732 			   ? BTF_VOID_TYPEID
733 			   : get_btf_id (farg->farg_type)),
734 		       "farg_type");
735 }
736 
737 /* Asm'out a BTF_KIND_FUNC type.  */
738 
739 static void
btf_asm_func_type(ctf_dtdef_ref dtd)740 btf_asm_func_type (ctf_dtdef_ref dtd)
741 {
742   dw2_asm_output_data (4, dtd->dtd_data.ctti_name, "btt_name");
743   dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC, 0, 0), "btt_info");
744   dw2_asm_output_data (4, get_btf_id (dtd->dtd_data.ctti_type), "btt_type");
745 }
746 
747 /* Asm'out a variable entry following a BTF_KIND_DATASEC.  */
748 
749 static void
btf_asm_datasec_entry(struct btf_var_secinfo info)750 btf_asm_datasec_entry (struct btf_var_secinfo info)
751 {
752   dw2_asm_output_data (4, info.type, "bts_type");
753   dw2_asm_output_data (4, info.offset, "bts_offset");
754   dw2_asm_output_data (4, info.size, "bts_size");
755 }
756 
757 /* Asm'out a whole BTF_KIND_DATASEC, including its variable entries.  */
758 
759 static void
btf_asm_datasec_type(btf_datasec_t ds,size_t stroffset)760 btf_asm_datasec_type (btf_datasec_t ds, size_t stroffset)
761 {
762   dw2_asm_output_data (4, ds.name_offset + stroffset, "btt_name");
763   dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_DATASEC, 0,
764 					 ds.entries.length ()),
765 		       "btt_info");
766   /* Note: the "total section size in bytes" is emitted as 0 and patched by
767      loaders such as libbpf.  */
768   dw2_asm_output_data (4, 0, "btt_size");
769   for (size_t i = 0; i < ds.entries.length (); i++)
770     btf_asm_datasec_entry (ds.entries[i]);
771 }
772 
773 /* Compute and output the header information for a .BTF section.  */
774 
775 static void
output_btf_header(ctf_container_ref ctfc)776 output_btf_header (ctf_container_ref ctfc)
777 {
778    switch_to_section (btf_info_section);
779    ASM_OUTPUT_LABEL (asm_out_file, btf_info_section_label);
780 
781    /* BTF magic number, version, flags, and header length.  */
782    dw2_asm_output_data (2, BTF_MAGIC, "btf_magic");
783    dw2_asm_output_data (1, BTF_VERSION, "btf_version");
784    dw2_asm_output_data (1, 0, "btf_flags");
785    dw2_asm_output_data (4, sizeof (struct btf_header), "btf_hdr_len");
786 
787    uint32_t type_off = 0, type_len = 0;
788    uint32_t str_off = 0, str_len = 0;
789    uint32_t datasec_vlen_bytes = 0;
790 
791    if (!ctfc_is_empty_container (ctfc))
792      {
793        for (size_t i = 0; i < datasecs.length (); i++)
794 	 {
795 	   datasec_vlen_bytes += ((datasecs[i].entries.length ())
796 				  * sizeof (struct btf_var_secinfo));
797 	 }
798 
799        /* Total length (bytes) of the types section.  */
800        type_len = (num_types_added * sizeof (struct btf_type))
801 	 + (num_types_created * sizeof (struct btf_type))
802 	 + datasec_vlen_bytes
803 	 + ctfc->ctfc_num_vlen_bytes;
804 
805        str_off = type_off + type_len;
806 
807        str_len = ctfc->ctfc_strtable.ctstab_len
808 	 + ctfc->ctfc_aux_strtable.ctstab_len;
809      }
810 
811    /* Offset of type section.  */
812    dw2_asm_output_data (4, type_off, "type_off");
813    /* Length of type section in bytes.  */
814    dw2_asm_output_data (4, type_len, "type_len");
815     /* Offset of string section.  */
816    dw2_asm_output_data (4, str_off, "str_off");
817     /* Length of string section in bytes.  */
818    dw2_asm_output_data (4, str_len, "str_len");
819 }
820 
821 /* Output all BTF_KIND_VARs in CTFC.  */
822 
823 static void
output_btf_vars(ctf_container_ref ctfc)824 output_btf_vars (ctf_container_ref ctfc)
825 {
826   size_t i;
827   size_t num_ctf_vars = num_vars_added;
828   if (num_ctf_vars)
829     {
830       for (i = 0; i < num_ctf_vars; i++)
831 	btf_asm_varent (ctfc->ctfc_vars_list[i]);
832     }
833 }
834 
835 /* Output BTF string records. The BTF strings section is a concatenation
836    of the standard and auxilliary string tables in the ctf container.  */
837 
838 static void
output_btf_strs(ctf_container_ref ctfc)839 output_btf_strs (ctf_container_ref ctfc)
840 {
841   ctf_string_t * ctf_string = ctfc->ctfc_strtable.ctstab_head;
842 
843   while (ctf_string)
844     {
845       dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_string");
846       ctf_string = ctf_string->cts_next;
847     }
848 
849   ctf_string = ctfc->ctfc_aux_strtable.ctstab_head;
850   while (ctf_string)
851     {
852       dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_aux_string");
853       ctf_string = ctf_string->cts_next;
854     }
855 }
856 
857 /* Output all (representable) members of a BTF_KIND_STRUCT or
858    BTF_KIND_UNION type.  */
859 
860 static void
output_asm_btf_sou_fields(ctf_container_ref ctfc,ctf_dtdef_ref dtd)861 output_asm_btf_sou_fields (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
862 {
863   ctf_dmdef_t * dmd;
864 
865   for (dmd = dtd->dtd_u.dtu_members;
866        dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
867       btf_asm_sou_member (ctfc, dmd);
868 }
869 
870 /* Output all enumerator constants following a BTF_KIND_ENUM.  */
871 
872 static void
output_asm_btf_enum_list(ctf_container_ref ARG_UNUSED (ctfc),ctf_dtdef_ref dtd)873 output_asm_btf_enum_list (ctf_container_ref ARG_UNUSED (ctfc),
874 			  ctf_dtdef_ref dtd)
875 {
876   ctf_dmdef_t * dmd;
877 
878   for (dmd = dtd->dtd_u.dtu_members;
879        dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
880     btf_asm_enum_const (dmd);
881 }
882 
883 /* Output all function arguments following a BTF_KIND_FUNC_PROTO.  */
884 
885 static void
output_asm_btf_func_args_list(ctf_container_ref ctfc,ctf_dtdef_ref dtd)886 output_asm_btf_func_args_list (ctf_container_ref ctfc,
887 			       ctf_dtdef_ref dtd)
888 {
889   size_t farg_name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
890   ctf_func_arg_t * farg;
891   for (farg = dtd->dtd_u.dtu_argv;
892        farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
893     btf_asm_func_arg (farg, farg_name_offset);
894 }
895 
896 /* Output the variable portion of a BTF type record. The information depends
897    on the kind of the type.  */
898 
899 static void
output_asm_btf_vlen_bytes(ctf_container_ref ctfc,ctf_dtdef_ref dtd)900 output_asm_btf_vlen_bytes (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
901 {
902   uint32_t btf_kind, encoding;
903 
904   btf_kind = get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
905 
906   if (btf_kind == BTF_KIND_UNKN)
907     return;
908 
909   switch (btf_kind)
910     {
911     case BTF_KIND_INT:
912       /* Redundant definitions of void may still be hanging around in the type
913 	 list as size 0 integers. Skip emitting them.  */
914       if (dtd->dtd_data.ctti_size < 1)
915 	break;
916 
917       encoding = BTF_INT_DATA (dtd->dtd_u.dtu_enc.cte_format,
918 			       dtd->dtd_u.dtu_enc.cte_offset,
919 			       dtd->dtd_u.dtu_enc.cte_bits);
920 
921       dw2_asm_output_data (4, encoding, "bti_encoding");
922       break;
923 
924     case BTF_KIND_ARRAY:
925       btf_asm_array (dtd);
926       break;
927 
928     case BTF_KIND_STRUCT:
929     case BTF_KIND_UNION:
930       output_asm_btf_sou_fields (ctfc, dtd);
931       break;
932 
933     case BTF_KIND_ENUM:
934       output_asm_btf_enum_list (ctfc, dtd);
935       break;
936 
937     case BTF_KIND_FUNC_PROTO:
938       output_asm_btf_func_args_list (ctfc, dtd);
939       break;
940 
941     case BTF_KIND_VAR:
942       /* BTF Variables are handled by output_btf_vars and btf_asm_varent.
943 	 There should be no BTF_KIND_VAR types at this point.  */
944       gcc_unreachable ();
945 
946     case BTF_KIND_DATASEC:
947       /* The BTF_KIND_DATASEC records are handled by output_btf_datasec_types
948 	 and btf_asm_datasec_type. There should be no BTF_KIND_DATASEC types
949 	 at this point.  */
950       gcc_unreachable ();
951 
952     default:
953       /* All other BTF type kinds have no variable length data.  */
954       break;
955     }
956 }
957 
958 /* Output a whole BTF type record for TYPE, including the fixed and variable
959    data portions.  */
960 
961 static void
output_asm_btf_type(ctf_container_ref ctfc,ctf_dtdef_ref type)962 output_asm_btf_type (ctf_container_ref ctfc, ctf_dtdef_ref type)
963 {
964   if (btf_emit_id_p (type->dtd_type))
965     {
966       btf_asm_type (ctfc, type);
967       output_asm_btf_vlen_bytes (ctfc, type);
968     }
969 }
970 
971 /* Output all BTF types in the container. This does not include synthesized
972    types: BTF_KIND_VAR, BTF_KIND_FUNC, nor BTF_KIND_DATASEC.  */
973 
974 static void
output_btf_types(ctf_container_ref ctfc)975 output_btf_types (ctf_container_ref ctfc)
976 {
977   size_t i;
978   size_t num_types = ctfc->ctfc_types->elements ();
979   if (num_types)
980     {
981       for (i = 1; i <= num_types; i++)
982 	output_asm_btf_type (ctfc, ctfc->ctfc_types_list[i]);
983     }
984 }
985 
986 /* Output all BTF_KIND_FUNC type records.  */
987 
988 static void
output_btf_func_types(void)989 output_btf_func_types (void)
990 {
991   for (size_t i = 0; i < vec_safe_length (funcs); i++)
992     btf_asm_func_type ((*funcs)[i]);
993 }
994 
995 /* Output all BTF_KIND_DATASEC records.  */
996 
997 static void
output_btf_datasec_types(ctf_container_ref ctfc)998 output_btf_datasec_types (ctf_container_ref ctfc)
999 {
1000   size_t name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
1001 
1002   for (size_t i = 0; i < datasecs.length(); i++)
1003     btf_asm_datasec_type (datasecs[i], name_offset);
1004 }
1005 
1006 /* Postprocess the CTF debug data post initialization.
1007 
1008    During the postprocess pass:
1009 
1010    - Prepare the sorted list of BTF types.
1011 
1012      The sorted list of BTF types is, firstly, used for lookup (during the BTF
1013      generation process) of CTF/BTF types given a typeID.
1014 
1015      Secondly, in the emitted BTF section, BTF Types need to be in the sorted
1016      order of their type IDs.  The BTF types section is viewed as an array,
1017      with type IDs used to index into that array.  It is essential that every
1018      type be placed at the exact index corresponding to its ID, or else
1019      references to that type from other types will no longer be correct.
1020 
1021    - References to void types are converted to reference BTF_VOID_TYPEID. In
1022      CTF, a distinct type is used to encode void.
1023 
1024    - Bitfield struct/union members are converted to BTF encoding. CTF uses
1025      slices to encode bitfields, but BTF does not have slices and encodes
1026      bitfield information directly in the variable-length btf_member
1027      descriptions following the struct or union type.
1028 
1029    - Unrepresentable types are removed. We cannot have any invalid BTF types
1030      appearing in the output so they must be removed, and type ids of other
1031      types and references adjust accordingly. This also involves ensuring that
1032      BTF descriptions of struct members referring to unrepresentable types are
1033      not emitted, as they would be nonsensical.
1034 
1035    - Adjust inner- and inter-type references-by-ID to account for removed
1036      types, and construct the types list.  */
1037 
1038 void
btf_init_postprocess(void)1039 btf_init_postprocess (void)
1040 {
1041   ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1042 
1043   size_t i;
1044   size_t num_ctf_types = tu_ctfc->ctfc_types->elements ();
1045 
1046   holes.create (0);
1047   voids.create (0);
1048 
1049   num_types_added = 0;
1050   num_types_created = 0;
1051 
1052   if (num_ctf_types)
1053     {
1054       init_btf_id_map (num_ctf_types + 1);
1055 
1056       /* Allocate the types list and traverse all types, placing each type
1057 	 at the index according to its ID.  Add 1 because type ID 0 always
1058 	 represents VOID.  */
1059       tu_ctfc->ctfc_types_list
1060 	= ggc_vec_alloc<ctf_dtdef_ref>(num_ctf_types + 1);
1061       tu_ctfc->ctfc_types->traverse<ctf_container_ref, btf_dtd_postprocess_cb>
1062 	(tu_ctfc);
1063 
1064       /* Build mapping of CTF type ID -> BTF type ID, and count total number
1065 	 of valid BTF types added.  */
1066       for (i = 1; i <= num_ctf_types; i++)
1067 	{
1068 	  ctf_dtdef_ref dtd = tu_ctfc->ctfc_types_list[i];
1069 	  ctf_id_t btfid = btf_adjust_type_id (dtd->dtd_type);
1070 	  set_btf_id (dtd->dtd_type, btfid);
1071 	  if (btfid < BTF_MAX_TYPE && (btfid != BTF_VOID_TYPEID))
1072 	    num_types_added ++;
1073 	}
1074     }
1075 }
1076 
1077 /* Process and output all BTF data. Entry point of btfout.  */
1078 
1079 void
btf_output(const char * filename)1080 btf_output (const char * filename)
1081 {
1082   ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1083 
1084   init_btf_sections ();
1085 
1086   datasecs.create (0);
1087   vec_alloc (funcs, 16);
1088 
1089   ctf_add_cuname (tu_ctfc, filename);
1090 
1091   btf_emit_preprocess (tu_ctfc);
1092 
1093   output_btf_header (tu_ctfc);
1094   output_btf_types (tu_ctfc);
1095   output_btf_vars (tu_ctfc);
1096   output_btf_func_types ();
1097   output_btf_datasec_types (tu_ctfc);
1098   output_btf_strs (tu_ctfc);
1099 }
1100 
1101 /* Reset all state for BTF generation so that we can rerun the compiler within
1102    the same process.  */
1103 
1104 void
btf_finalize(void)1105 btf_finalize (void)
1106 {
1107   btf_info_section = NULL;
1108 
1109   /* Clear preprocessing state.  */
1110   num_vars_added = 0;
1111   num_types_added = 0;
1112   num_types_created = 0;
1113 
1114   holes.release ();
1115   voids.release ();
1116   for (size_t i = 0; i < datasecs.length (); i++)
1117     datasecs[i].entries.release ();
1118   datasecs.release ();
1119 
1120   funcs = NULL;
1121 
1122   btf_var_ids->empty ();
1123   btf_var_ids = NULL;
1124 
1125   free (btf_id_map);
1126   btf_id_map = NULL;
1127 
1128   ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
1129   ctfc_delete_container (tu_ctfc);
1130   tu_ctfc = NULL;
1131 }
1132 
1133 #include "gt-btfout.h"
1134