]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ctfout.c
Daily bump.
[thirdparty/gcc.git] / gcc / ctfout.c
CommitLineData
b7e215a8
IB
1/* Output CTF format from GCC.
2 Copyright (C) 2019,2021 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "target.h"
24#include "output.h"
25#include "dwarf2asm.h"
26#include "debug.h"
27#include "ctfc.h"
28#include "diagnostic-core.h"
29
30static int ctf_label_num;
31
32/* Pointers to various CTF sections. */
33
34static GTY (()) section * ctf_info_section;
35
36/* Section names used to hold CTF debugging information. */
37
38/* CTF debug info section. */
39
40#ifndef CTF_INFO_SECTION_NAME
41#define CTF_INFO_SECTION_NAME ".ctf"
42#endif
43
44/* Section flags for the CTF debug info section. */
45
46#define CTF_INFO_SECTION_FLAGS (SECTION_DEBUG)
47
48/* Maximum size (in bytes) of an artificially generated CTF label. */
49
50#define MAX_CTF_LABEL_BYTES 40
51
52static char ctf_info_section_label[MAX_CTF_LABEL_BYTES];
53
54#ifndef CTF_INFO_SECTION_LABEL
55#define CTF_INFO_SECTION_LABEL "Lctf"
56#endif
57
58/* CTF preprocess callback arguments. */
59
60typedef struct ctf_dtd_preprocess_arg
61{
62 uint64_t dtd_global_func_idx;
63 ctf_container_ref dtd_arg_ctfc;
64} ctf_dtd_preprocess_arg_t;
65
66typedef struct ctf_dvd_preprocess_arg
67{
68 uint64_t dvd_global_obj_idx;
69 ctf_container_ref dvd_arg_ctfc;
70} ctf_dvd_preprocess_arg_t;
71
72/* Compare two CTF variable definition entries. Currently used for sorting
73 by name. */
74
75static int
76ctf_varent_compare (const void * entry1, const void * entry2)
77{
78 int result;
79 const ctf_dvdef_t * e1 = *(const ctf_dvdef_t * const*) entry1;
80 const ctf_dvdef_t * e2 = *(const ctf_dvdef_t * const*) entry2;
81
82 result = strcmp (e1->dvd_name, e2->dvd_name);
83
84 return result;
85}
86
87/* A CTF type record may be followed by variable-length of bytes to encode the
88 CTF type completely. This routine calculates the number of bytes, in the
89 final binary CTF format, which are used to encode information about the type
90 completely.
91
92 This function must always be in sync with the CTF header. */
93
94static uint64_t
95ctf_calc_num_vbytes (ctf_dtdef_ref ctftype)
96{
97 uint32_t size;
98 uint64_t vlen_bytes = 0;
99
100 uint32_t kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
101 uint32_t vlen = CTF_V2_INFO_VLEN (ctftype->dtd_data.ctti_info);
102
103 ctf_dmdef_t * dmd;
104 ctf_func_arg_t * farg;
105 uint32_t size_per_member = 0;
106 unsigned int num_members = 0;
107 unsigned int num_fargs = 0;
108
109 switch (kind)
110 {
111 case CTF_K_FORWARD:
112 case CTF_K_UNKNOWN:
113 case CTF_K_POINTER:
114 case CTF_K_TYPEDEF:
115 case CTF_K_VOLATILE:
116 case CTF_K_CONST:
117 case CTF_K_RESTRICT:
118 /* These types have no vlen data. */
119 break;
120
121 case CTF_K_INTEGER:
122 case CTF_K_FLOAT:
123 /* 4 bytes to represent encoding CTF_INT_DATA, CTF_FP_DATA. */
124 vlen_bytes += sizeof (uint32_t);
125 break;
126 case CTF_K_FUNCTION:
127 /* Sanity check - number of function args must be the same as
128 vlen. */
129 for (farg = ctftype->dtd_u.dtu_argv;
130 farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
131 num_fargs++;
132 gcc_assert (vlen == num_fargs);
133
134 /* FIXME - CTF_PADDING_FOR_ALIGNMENT. */
135 vlen_bytes += (vlen + (vlen & 1)) * sizeof (uint32_t);
136 break;
137 case CTF_K_ARRAY:
138 /* This has a single ctf_array_t. */
139 vlen_bytes += sizeof (ctf_array_t);
140 break;
141 case CTF_K_SLICE:
142 vlen_bytes += sizeof (ctf_slice_t);
143 break;
144 case CTF_K_STRUCT:
145 case CTF_K_UNION:
146 /* Count the number and type of members. */
147 size = ctftype->dtd_data.ctti_size;
148 size_per_member = size >= CTF_LSTRUCT_THRESH
149 ? sizeof (ctf_lmember_t) : sizeof (ctf_member_t);
150
151 /* Sanity check - number of members of struct must be the same as
152 vlen. */
153 for (dmd = ctftype->dtd_u.dtu_members;
154 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
155 num_members++;
156 gcc_assert (vlen == num_members);
157
158 vlen_bytes += (num_members * size_per_member);
159 break;
160 case CTF_K_ENUM:
161 vlen_bytes += vlen * sizeof (ctf_enum_t);
162 break;
163 default :
164 break;
165 }
166 return vlen_bytes;
167}
168
169/* Add a CTF variable to the end of the list. */
170
171static void
172ctf_list_add_ctf_vars (ctf_container_ref ctfc, ctf_dvdef_ref var)
173{
174 /* FIXME - static may not fly with multiple CUs. */
175 static int num_vars_added = 0;
176 ctfc->ctfc_vars_list[num_vars_added++] = var;
177}
178
179/* Initialize the various sections and labels for CTF output. */
180
181void
182init_ctf_sections (void)
183{
184 /* Note : Even in case of LTO, the compiler continues to generate a single
185 CTF section for each compilation unit "early". Unlike other debug
186 sections, CTF sections are non-LTO sections, and do not take the
187 .gnu.debuglto_ prefix. The linker will de-duplicate the types in the CTF
188 sections, in case of LTO or otherwise. */
189 ctf_info_section = get_section (CTF_INFO_SECTION_NAME, CTF_INFO_SECTION_FLAGS,
190 NULL);
191
192 ASM_GENERATE_INTERNAL_LABEL (ctf_info_section_label,
193 CTF_INFO_SECTION_LABEL, ctf_label_num++);
194}
195
196/* Routines for CTF pre-processing. */
197
198static void
199ctf_preprocess_var (ctf_container_ref ctfc, ctf_dvdef_ref var)
200{
201 /* Add it to the list of types. This array of types will be sorted before
202 assembling into output. */
203 ctf_list_add_ctf_vars (ctfc, var);
204}
205
206/* CTF preprocess callback routine for CTF variables. */
207
208int
209ctf_dvd_preprocess_cb (ctf_dvdef_ref * slot, void * arg)
210{
211 ctf_dvd_preprocess_arg_t * dvd_arg = (ctf_dvd_preprocess_arg_t *)arg;
212 ctf_dvdef_ref var = (ctf_dvdef_ref) *slot;
213 ctf_container_ref arg_ctfc = dvd_arg->dvd_arg_ctfc;
214
215 ctf_preprocess_var (arg_ctfc, var);
216
217 /* Keep track of global objts. */
218 arg_ctfc->ctfc_gobjts_list[dvd_arg->dvd_global_obj_idx] = var;
219 dvd_arg->dvd_global_obj_idx++;
220
221 return 1;
222}
223
224/* CTF preprocess callback routine for CTF types. */
225
226int
227ctf_dtd_preprocess_cb (ctf_dtdef_ref * slot, void * arg)
228{
229 uint32_t kind;
230
231 ctf_dtdef_ref ctftype = (ctf_dtdef_ref) *slot;
232 ctf_dtd_preprocess_arg_t * dtd_arg = (ctf_dtd_preprocess_arg_t *)arg;
233 ctf_container_ref arg_ctfc = dtd_arg->dtd_arg_ctfc;
234
235 size_t index = ctftype->dtd_type;
236 gcc_assert (index <= arg_ctfc->ctfc_types->elements ());
237
238 /* CTF types need to be output in the order of their type IDs. In other
239 words, if type A is used to define type B, type ID of type A must
240 appear before type ID of type B. */
241 arg_ctfc->ctfc_types_list[index] = ctftype;
242
243 /* Keep track of the CTF type if it's a function type and the type
244 was generated from a function object. */
245 kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
246 if (kind == CTF_K_FUNCTION && ctftype->from_global_func)
247 {
248 arg_ctfc->ctfc_gfuncs_list[dtd_arg->dtd_global_func_idx] = ctftype;
249 dtd_arg->dtd_global_func_idx++;
250 }
251
252 /* Calculate the vlen bytes. */
253 arg_ctfc->ctfc_num_vlen_bytes += ctf_calc_num_vbytes (ctftype);
254
255 return 1;
256}
257
258/* CTF preprocessing.
259 After the CTF types for the compilation unit have been generated fully, the
260 compiler writes out the asm for the CTF types.
261
262 CTF writeout in the compiler requires two passes over the CTF types. In the
263 first pass, the CTF preprocess pass:
264 1. CTF types are sorted in the order of their type IDs.
265 2. The variable number of bytes after each CTF type record are calculated.
266 This is used to calculate the offsets in the ctf_header_t.
267 3. If the CTF type is of CTF_K_FUNCTION, the number of bytes in the
268 funcinfo sub-section are calculated. This is used to calculate the
269 offsets in the ctf_header_t.
270 4. Keep the list of CTF variables in ASCIIbetical order of their names.
271
272 In the second pass, the CTF writeout pass, asm tags are written out using
273 the compiler's afore-generated internal pre-processed CTF types. */
274
275static void
276ctf_preprocess (ctf_container_ref ctfc)
277{
278 size_t num_ctf_types = ctfc->ctfc_types->elements ();
279
280 /* Initialize an array to keep track of the CTF variables at global
281 scope. */
282 size_t num_global_objts = ctfc->ctfc_num_global_objts;
283 if (num_global_objts)
284 {
285 ctfc->ctfc_gobjts_list = ggc_vec_alloc<ctf_dvdef_t*>(num_global_objts);
286 }
287
288 size_t num_ctf_vars = ctfc->ctfc_vars->elements ();
289 if (num_ctf_vars)
290 {
291 ctf_dvd_preprocess_arg_t dvd_arg;
292 dvd_arg.dvd_global_obj_idx = 0;
293 dvd_arg.dvd_arg_ctfc = ctfc;
294
295 /* Allocate CTF var list. */
296 ctfc->ctfc_vars_list = ggc_vec_alloc<ctf_dvdef_ref>(num_ctf_vars);
297 /* Variables appear in the sort ASCIIbetical order of their names. This
298 permits binary searching in the CTF reader. Add the variables to a
299 list for sorting. */
300 ctfc->ctfc_vars->traverse<void *, ctf_dvd_preprocess_cb> (&dvd_arg);
301 /* Sort the list. */
302 qsort (ctfc->ctfc_vars_list, num_ctf_vars, sizeof (ctf_dvdef_ref),
303 ctf_varent_compare);
304 }
305
306 /* Initialize an array to keep track of the CTF functions types for global
307 functions in the CTF data section. */
308 size_t num_global_funcs = ctfc->ctfc_num_global_funcs;
309 if (num_global_funcs)
310 {
311 ctfc->ctfc_gfuncs_list = ggc_vec_alloc<ctf_dtdef_t*>(num_global_funcs);
312 gcc_assert (num_ctf_types);
313 }
314
315 if (num_ctf_types)
316 {
317 ctf_dtd_preprocess_arg_t dtd_arg;
318 dtd_arg.dtd_global_func_idx = 0;
319 dtd_arg.dtd_arg_ctfc = ctfc;
320 /* Allocate the CTF types list. Add 1 because type ID 0 is never a valid
321 CTF type ID. No CTF type record should appear at that offset, this
322 eases debugging and readability. */
323 ctfc->ctfc_types_list = ggc_vec_alloc<ctf_dtdef_ref>(num_ctf_types + 1);
324 /* Pre-process CTF types. */
325 ctfc->ctfc_types->traverse<void *, ctf_dtd_preprocess_cb> (&dtd_arg);
326
327 gcc_assert (dtd_arg.dtd_global_func_idx == num_global_funcs);
328 }
329}
330
331/* CTF asm helper routines. */
332
333/* Asm'out the CTF preamble. */
334
335static void
336ctf_asm_preamble (ctf_container_ref ctfc)
337{
338 dw2_asm_output_data (2, ctfc->ctfc_magic,
339 "CTF preamble magic number");
340 dw2_asm_output_data (1, ctfc->ctfc_version, "CTF preamble version");
341 dw2_asm_output_data (1, ctfc->ctfc_flags, "CTF preamble flags");
342}
343
344/* Asm'out a CTF type which is represented by ctf_stype_t. */
345
346static void
347ctf_asm_stype (ctf_dtdef_ref type)
348{
349 dw2_asm_output_data (4, type->dtd_data.ctti_name, "ctt_name");
350 dw2_asm_output_data (4, type->dtd_data.ctti_info, "ctt_info");
351 /* union. */
352 dw2_asm_output_data (4, type->dtd_data.ctti_size, "ctt_size or ctt_type");
353}
354
355/* Asm'out a CTF type which is represented by ctf_type_t. */
356
357static void
358ctf_asm_type (ctf_dtdef_ref type)
359{
360 dw2_asm_output_data (4, type->dtd_data.ctti_name, "ctt_name");
361 dw2_asm_output_data (4, type->dtd_data.ctti_info, "ctt_info");
362 /* union. */
363 dw2_asm_output_data (4, type->dtd_data.ctti_size, "ctt_size");
364 dw2_asm_output_data (4, type->dtd_data.ctti_lsizehi, "ctt_lsizehi");
365 dw2_asm_output_data (4, type->dtd_data.ctti_lsizelo, "ctt_lsizelo");
366}
367
368/* Asm'out a CTF type of kind CTF_K_SLICE. */
369
370static void
371ctf_asm_slice (ctf_dtdef_ref type)
372{
373 dw2_asm_output_data (4, type->dtd_u.dtu_slice.cts_type, "cts_type");
374 dw2_asm_output_data (2, type->dtd_u.dtu_slice.cts_offset, "cts_offset");
375 dw2_asm_output_data (2, type->dtd_u.dtu_slice.cts_bits, "cts_bits");
376}
377
378/* Asm'out a CTF type of kind CTF_K_ARRAY. */
379
380static void
381ctf_asm_array (ctf_dtdef_ref dtd)
382{
383 dw2_asm_output_data (4, dtd->dtd_u.dtu_arr.ctr_contents, "cta_contents");
384 dw2_asm_output_data (4, dtd->dtd_u.dtu_arr.ctr_index, "cta_index");
385 dw2_asm_output_data (4, dtd->dtd_u.dtu_arr.ctr_nelems, "cta_nelems");
386}
387
388/* Asm'out a CTF variable. */
389
390static void
391ctf_asm_varent (ctf_dvdef_ref var)
392{
393 /* Output the reference to the name in the string table. */
394 dw2_asm_output_data (4, var->dvd_name_offset, "ctv_name");
395 /* Output the type index. */
396 dw2_asm_output_data (4, var->dvd_type, "ctv_typeidx");
397}
398
399/* Asm'out a member of CTF struct or union, represented by ctf_lmember_t. */
400
401static void
402ctf_asm_sou_lmember (ctf_dmdef_t * dmd)
403{
404 dw2_asm_output_data (4, dmd->dmd_name_offset, "ctlm_name");
405 dw2_asm_output_data (4, CTF_OFFSET_TO_LMEMHI (dmd->dmd_offset),
406 "ctlm_offsethi");
407 dw2_asm_output_data (4, dmd->dmd_type, "ctlm_type");
408 dw2_asm_output_data (4, CTF_OFFSET_TO_LMEMLO (dmd->dmd_offset),
409 "ctlm_offsetlo");
410}
411
412/* Asm'out a member of a CTF sruct or union, represented by ctf_member_t. */
413
414static void
415ctf_asm_sou_member (ctf_dmdef_t * dmd)
416{
417 dw2_asm_output_data (4, dmd->dmd_name_offset, "ctm_name");
418 dw2_asm_output_data (4, dmd->dmd_offset, "ctm_offset");
419 dw2_asm_output_data (4, dmd->dmd_type, "ctm_type");
420}
421
422/* Asm'out an enumerator constant. */
423
424static void
425ctf_asm_enum_const (ctf_dmdef_t * dmd)
426{
427 dw2_asm_output_data (4, dmd->dmd_name_offset, "cte_name");
428 dw2_asm_output_data (4, dmd->dmd_value, "cte_value");
429}
430
431/* Asm'out a function argument. */
432
433static void
434ctf_asm_func_arg (ctf_func_arg_t * farg)
435{
436 dw2_asm_output_data (4, farg->farg_type, "dtu_argv");
437}
438
439/* CTF writeout to asm file. */
440
441static void
442output_ctf_header (ctf_container_ref ctfc)
443{
444 switch_to_section (ctf_info_section);
445 ASM_OUTPUT_LABEL (asm_out_file, ctf_info_section_label);
446
447 ctf_asm_preamble (ctfc);
448
449 /* For a single compilation unit, the parent container's name and label are
450 NULL. */
451 dw2_asm_output_data (4, 0, "cth_parlabel");
452 dw2_asm_output_data (4, 0, "cth_parname");
453 dw2_asm_output_data (4, ctfc->ctfc_cuname_offset, "cth_cuname");
454
455 int typeslen = 0;
456 /* Initialize the offsets. The offsets are from after the CTF header. */
457 uint32_t lbloff = 0;
458 uint32_t objtoff = 0;
459 uint32_t funcoff = 0;
460 uint32_t objtidxoff = 0;
461 uint32_t funcidxoff = 0;
462 uint32_t varoff = 0;
463 uint32_t typeoff = 0;
464 uint32_t stroff = 0;
465
466 if (!ctfc_is_empty_container (ctfc))
467 {
468 gcc_assert (ctfc_get_num_ctf_types (ctfc)
469 == (ctfc->ctfc_num_types + ctfc->ctfc_num_stypes));
470
471 funcoff = objtoff + ctfc->ctfc_num_global_objts * sizeof (uint32_t);
472 /* Object index appears after function info. */
473 objtidxoff = funcoff + ctfc->ctfc_num_global_funcs * sizeof (uint32_t);
474 /* Funxtion index goes next. */
475 funcidxoff = objtidxoff + ctfc->ctfc_num_global_objts * sizeof (uint32_t);
476 /* Vars appear after function index. */
477 varoff = funcidxoff + ctfc->ctfc_num_global_funcs * sizeof (uint32_t);
478 /* CTF types appear after vars. */
479 typeoff = varoff + ctfc_get_num_ctf_vars (ctfc) * sizeof (ctf_varent_t);
480 /* The total number of bytes for CTF types is the sum of the number of
481 times struct ctf_type_t, struct ctf_stype_t are written, plus the
482 amount of variable length data after each one of these. */
483 typeslen = ctfc->ctfc_num_types * sizeof (ctf_type_t)
484 + ctfc->ctfc_num_stypes * (sizeof (ctf_stype_t))
485 + ctfc_get_num_vlen_bytes (ctfc);
486
487 /* Strings appear after types. */
488 stroff = typeoff + typeslen;
489 }
490
491 /* Offset of label section. */
492 dw2_asm_output_data (4, lbloff, "cth_lbloff");
493 /* Offset of object section. */
494 dw2_asm_output_data (4, objtoff, "cth_objtoff");
495 /* Offset of function section. */
496 dw2_asm_output_data (4, funcoff, "cth_funcoff");
497 /* Offset of object index section. */
498 dw2_asm_output_data (4, objtidxoff, "cth_objtidxoff");
499 /* Offset of function index section. */
500 dw2_asm_output_data (4, funcidxoff, "cth_funcidxoff");
501
502 /* Offset of variable section. */
503 dw2_asm_output_data (4, varoff, "cth_varoff");
504 /* Offset of type section. */
505 dw2_asm_output_data (4, typeoff, "cth_typeoff");
506 /* Offset of string section. */
507 dw2_asm_output_data (4, stroff, "cth_stroff");
508 /* Length of string section in bytes. */
509 dw2_asm_output_data (4, ctfc->ctfc_strlen, "cth_strlen");
510}
511
512/* Output the CTF object info section. */
513
514static void
515output_ctf_obj_info (ctf_container_ref ctfc)
516{
517 uint64_t i;
518 ctf_dvdef_ref var;
519
520 if (!ctfc->ctfc_num_global_objts) return;
521
522 /* Compiler spits out the objts (at global scope) in the CTF obj info section.
523 In no specific order. In an object file, the CTF object index section is
524 used to associate the objts to their corresponding names. */
525 for (i = 0; i < ctfc->ctfc_num_global_objts; i++)
526 {
527 var = ctfc->ctfc_gobjts_list[i];
528
529 /* CTF type ID corresponding to the type of the variable. */
530 dw2_asm_output_data (4, var->dvd_type, "objtinfo_var_type");
531 }
532
533}
534
535/* Output the CTF function info section. */
536
537static void
538output_ctf_func_info (ctf_container_ref ctfc)
539{
540 uint64_t i;
541 ctf_dtdef_ref ctftype;
542
543 if (!ctfc->ctfc_num_global_funcs) return;
544
545 /* The CTF funcinfo section is simply an array of CTF_K_FUNCTION type IDs in
546 the type section. In an object file, the CTF function index section is
547 used to associate functions to their corresponding names. */
548 for (i = 0; i < ctfc->ctfc_num_global_funcs; i++)
549 {
550 ctftype = ctfc->ctfc_gfuncs_list[i];
551 dw2_asm_output_data (4, ctftype->dtd_type, "funcinfo_func_type");
552 }
553}
554
555/* Output the CTF object index section. */
556
557static void
558output_ctf_objtidx (ctf_container_ref ctfc)
559{
560 uint64_t i;
561 ctf_dvdef_ref var;
562
563 if (!ctfc->ctfc_num_global_objts) return;
564
565 for (i = 0; i < ctfc->ctfc_num_global_objts; i++)
566 {
567 var = ctfc->ctfc_gobjts_list[i];
568 /* Offset to the name in CTF string table. */
569 dw2_asm_output_data (4, var->dvd_name_offset, "objtinfo_name");
570 }
571}
572
573/* Output the CTF function index section. */
574
575static void
576output_ctf_funcidx (ctf_container_ref ctfc)
577{
578 uint64_t i;
579 ctf_dtdef_ref ctftype;
580
581 if (!ctfc->ctfc_num_global_funcs) return;
582
583 for (i = 0; i < ctfc->ctfc_num_global_funcs; i++)
584 {
585 ctftype = ctfc->ctfc_gfuncs_list[i];
586 /* Offset to the name in CTF string table. */
587 dw2_asm_output_data (4, ctftype->dtd_data.ctti_name, "funcinfo_name");
588 }
589}
590
591/* Output the CTF variables. Variables appear in the sorted ASCIIbetical
592 order of their names. This permits binary searching in the CTF reader. */
593
594static void
595output_ctf_vars (ctf_container_ref ctfc)
596{
597 size_t i;
598 size_t num_ctf_vars = ctfc->ctfc_vars->elements ();
599 if (num_ctf_vars)
600 {
601 /* Iterate over the list of sorted vars and output the asm. */
602 for (i = 0; i < num_ctf_vars; i++)
603 {
604 ctf_asm_varent (ctfc->ctfc_vars_list[i]);
605 /* The type of variable must be a valid one. */
606 gcc_assert (ctfc->ctfc_vars_list[i]->dvd_type != CTF_NULL_TYPEID);
607 }
608 }
609}
610
611/* Output the CTF string records. */
612
613static void
614output_ctf_strs (ctf_container_ref ctfc)
615{
616 ctf_string_t * ctf_string = ctfc->ctfc_strtable.ctstab_head;
617
618 while (ctf_string)
619 {
620 dw2_asm_output_nstring (ctf_string->cts_str, -1, "ctf_string");
621 ctf_string = ctf_string->cts_next;
622 }
623}
624
625/* Output the members of the CTF struct or union. */
626
627static void
628output_asm_ctf_sou_fields (ctf_container_ref ARG_UNUSED (ctfc),
629 ctf_dtdef_ref dtd)
630{
631 ctf_dmdef_t * dmd;
632
633 /* Function pointer to dump struct/union members. */
634 void (*ctf_asm_sou_field_func) (ctf_dmdef_t *);
635
636 uint32_t size = dtd->dtd_data.ctti_size;
637
638 /* The variable length data struct/union CTF types is an array of
639 ctf_member or ctf_lmember, depending on size of the member. */
640 if (size >= CTF_LSTRUCT_THRESH)
641 ctf_asm_sou_field_func = ctf_asm_sou_lmember;
642 else
643 ctf_asm_sou_field_func = ctf_asm_sou_member;
644
645 for (dmd = dtd->dtd_u.dtu_members;
646 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
647 {
648 ctf_asm_sou_field_func (dmd);
649 /* Sanity Check - Unrepresented types appear as explicit types. */
650 gcc_assert (dmd->dmd_type != CTF_NULL_TYPEID);
651 }
652}
653
654/* Output the list of enumerator constants of the CTF enum type. */
655
656static void
657output_asm_ctf_enum_list (ctf_container_ref ARG_UNUSED (ctfc),
658 ctf_dtdef_ref dtd)
659{
660 ctf_dmdef_t * dmd;
661
662 for (dmd = dtd->dtd_u.dtu_members;
663 dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
664 ctf_asm_enum_const (dmd);
665}
666
667/* Output the list of function arguments of the CTF function type. */
668
669static void
670output_asm_func_args_list (ctf_container_ref ARG_UNUSED (ctfc),
671 ctf_dtdef_ref dtd)
672{
673 ctf_func_arg_t * farg;
674
675 for (farg = dtd->dtd_u.dtu_argv;
676 farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
677 ctf_asm_func_arg (farg);
678}
679
680/* Output the variable length portion of the CTF type record. */
681
682static void
683output_asm_ctf_vlen_bytes (ctf_container_ref ctfc, ctf_dtdef_ref ctftype)
684{
685 uint32_t encoding;
686 uint32_t kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
687 uint32_t vlen = CTF_V2_INFO_VLEN (ctftype->dtd_data.ctti_info);
688
689 switch (kind)
690 {
691 case CTF_K_INTEGER:
692 case CTF_K_FLOAT:
693 if (kind == CTF_K_INTEGER)
694 {
695 encoding = CTF_INT_DATA (ctftype->dtd_u.dtu_enc.cte_format,
696 ctftype->dtd_u.dtu_enc.cte_offset,
697 ctftype->dtd_u.dtu_enc.cte_bits);
698 }
699 else
700 {
701 encoding = CTF_FP_DATA (ctftype->dtd_u.dtu_enc.cte_format,
702 ctftype->dtd_u.dtu_enc.cte_offset,
703 ctftype->dtd_u.dtu_enc.cte_bits);
704 }
705 dw2_asm_output_data (4, encoding, "ctf_encoding_data");
706 break;
707 case CTF_K_FUNCTION:
708 {
709 output_asm_func_args_list (ctfc, ctftype);
710 /* FIXME - CTF_PADDING_FOR_ALIGNMENT.
711 libctf expects this padding for alignment reasons. Expected to
712 be redundant in CTF_VERSION_4. */
713 if (vlen & 1)
714 dw2_asm_output_data (4, 0, "dtu_argv_padding");
715
716 break;
717 }
718 case CTF_K_ARRAY:
719 ctf_asm_array (ctftype);
720 break;
721 case CTF_K_SLICE:
722 {
723 ctf_asm_slice (ctftype);
724 /* Type of the slice must be a valid CTF type. */
725 gcc_assert (ctftype->dtd_u.dtu_slice.cts_type != CTF_NULL_TYPEID);
726 break;
727 }
728 case CTF_K_STRUCT:
729 case CTF_K_UNION:
730 output_asm_ctf_sou_fields (ctfc, ctftype);
731 break;
732 case CTF_K_ENUM:
733 output_asm_ctf_enum_list (ctfc, ctftype);
734 break;
735
736 default:
737 /* CTF types of kind CTF_K_VOLATILE, CTF_K_CONST, CTF_K_RESTRICT,
738 etc have no vlen data to write. */
739 break;
740 }
741}
742
743/* Output a CTF Type. */
744
745static void
746output_asm_ctf_type (ctf_container_ref ctfc, ctf_dtdef_ref type)
747{
748 if (type->dtd_data.ctti_size <= CTF_MAX_SIZE)
749 ctf_asm_stype (type);
750 else
751 ctf_asm_type (type);
752 /* Now comes the variable-length portion for defining types completely.
753 E.g., encoding follows CTF_INT_DATA, CTF_FP_DATA types,
754 struct ctf_array_t follows CTF_K_ARRAY types, or a bunch of
755 struct ctf_member / ctf_lmember ctf_enum sit in there for CTF_K_STRUCT or
756 CTF_K_UNION. */
757 output_asm_ctf_vlen_bytes (ctfc, type);
758
759 uint32_t kind = CTF_V2_INFO_KIND (type->dtd_data.ctti_info);
760 /* The underlying type must be a valid CTF type. */
761 if (kind == CTF_K_POINTER || kind == CTF_K_TYPEDEF
762 || kind == CTF_K_VOLATILE || kind == CTF_K_CONST
763 || kind == CTF_K_RESTRICT)
764 gcc_assert (type->dtd_data.ctti_type != CTF_NULL_TYPEID);
765}
766
767/* Output all CTF type records. */
768
769static void
770output_ctf_types (ctf_container_ref ctfc)
771{
772 size_t i;
773 size_t num_ctf_types = ctfc->ctfc_types->elements ();
774 if (num_ctf_types)
775 {
776 /* Type ID = 0 is used as sentinel value; not a valid type. */
777 for (i = 1; i <= num_ctf_types; i++)
778 output_asm_ctf_type (ctfc, ctfc->ctfc_types_list[i]);
779 }
780}
781
782/* CTF routines interfacing to the compiler. */
783
784/* Prepare and output the CTF section. */
785
786void
787ctf_output (const char * filename)
788{
789 if (ctf_debug_info_level == CTFINFO_LEVEL_NONE)
790 return;
791
792 /* Get the CTF container for the current translation unit. */
793 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
794
795 init_ctf_sections ();
796
797 ctf_add_cuname (tu_ctfc, filename);
798
799 /* Pre-process CTF before generating assembly. */
800 ctf_preprocess (tu_ctfc);
801 output_ctf_header (tu_ctfc);
802 output_ctf_obj_info (tu_ctfc);
803 output_ctf_func_info (tu_ctfc);
804 output_ctf_objtidx (tu_ctfc);
805 output_ctf_funcidx (tu_ctfc);
806 output_ctf_vars (tu_ctfc);
807 output_ctf_types (tu_ctfc);
808 output_ctf_strs (tu_ctfc);
809
810 /* The total number of string bytes must be equal to those processed out to
811 the str subsection. */
812 gcc_assert (tu_ctfc->ctfc_strlen
813 == ctfc_get_strtab_len (tu_ctfc, CTF_STRTAB));
814
815}
816
817/* Reset all state for CTF generation so that we can rerun the compiler within
818 the same process. */
819
820void
821ctf_finalize (void)
822{
823 ctf_info_section = NULL;
824
825 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
826 ctfc_delete_container (tu_ctfc);
827 tu_ctfc = NULL;
828}
829
830#include "gt-ctfout.h"