Debian Codegraph @

gcc/dwarf2ctf.cc

raw
1 /* Generate CTF types and objects from the GCC DWARF.
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 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "dwarf2out.h"
25 #include "dwarf2out.h"
26
27 #include "dwarf2ctf.h"
28 #include "ctfc.h"
29
30 /* Forward declarations for some routines defined in this file. */
31
32 static ctf_dtdef_ref
33 gen_ctf_type (ctf_container_ref, dw_die_ref);
34
35 /* All the DIE structures we handle come from the DWARF information
36 generated by GCC. However, there are three situations where we need
37 to create our own created DIE structures because GCC doesn't
38 provide them.
39
40 The DWARF spec suggests using a DIE with DW_TAG_unspecified_type
41 and name "void" in order to denote the void type. But GCC doesn't
42 follow this advice. Still we need a DIE to act as a key for void
43 types, we use ctf_void_die.
44
45 Also, if a subrange type corresponding to an array index does not
46 specify a type then we assume it is `int'.
47
48 Finally, for types unrepresentable in CTF, we need a DIE to anchor
49 them to a CTF type of kind unknown.
50
51 The variables below are initialized in ctf_debug_init and hold
52 references to the proper DIEs. */
53
54 static GTY (()) dw_die_ref ctf_void_die;
55 static GTY (()) dw_die_ref ctf_array_index_die;
56 static GTY (()) dw_die_ref ctf_unknown_die;
57
58 /* Some DIEs have a type description attribute, stored in a DW_AT_type
59 attribute. However, GCC generates no attribute to signify a `void'
60 type.
61
62 This can happen in many contexts (return type of a function,
63 pointed or qualified type, etc) so we use the `ctf_get_AT_type'
64 function below abstracts this. */
65
66 static dw_die_ref
67 ctf_get_AT_type (dw_die_ref die)
68 {
69 dw_die_ref type_die = get_AT_ref (die, DW_AT_type);
70 return (type_die ? type_die : ctf_void_die);
71 }
72
73 /* Some data member DIEs have location specified as a DWARF expression
74 (specifically in DWARF2). Luckily, the expression is a simple
75 DW_OP_plus_uconst with one operand set to zero.
76
77 Sometimes the data member location may also be negative. In yet some other
78 cases (specifically union data members), the data member location is
79 non-existent. Handle all these scenarios here to abstract this. */
80
81 static HOST_WIDE_INT
82 ctf_get_AT_data_member_location (dw_die_ref die)
83 {
84 HOST_WIDE_INT field_location = 0;
85 dw_attr_node * attr;
86
87 /* The field location (in bits) can be determined from
88 either a DW_AT_data_member_location attribute or a
89 DW_AT_data_bit_offset attribute. */
90 if (get_AT (die, DW_AT_data_bit_offset))
91 field_location = get_AT_unsigned (die, DW_AT_data_bit_offset);
92 else
93 {
94 attr = get_AT (die, DW_AT_data_member_location);
95 if (attr && AT_class (attr) == dw_val_class_loc)
96 {
97 dw_loc_descr_ref descr = AT_loc (attr);
98 /* Operand 2 must be zero; the structure is assumed to be on the
99 stack in DWARF2. */
100 gcc_assert (!descr->dw_loc_oprnd2.v.val_unsigned);
101 gcc_assert (descr->dw_loc_oprnd2.val_class
102 == dw_val_class_unsigned_const);
103 field_location = descr->dw_loc_oprnd1.v.val_unsigned * 8;
104 }
105 else
106 {
107 attr = get_AT (die, DW_AT_data_member_location);
108 if (attr && AT_class (attr) == dw_val_class_const)
109 field_location = AT_int (attr) * 8;
110 else
111 field_location = (get_AT_unsigned (die,
112 DW_AT_data_member_location)
113 * 8);
114 }
115 }
116
117 return field_location;
118 }
119
120 /* CTF Types' and CTF Variables' Location Information. CTF section does not
121 emit location information, this is provided for BTF CO-RE use-cases. These
122 functions fetch information from DWARF Die directly, as such the location
123 information is not buffered in the CTF container. */
124
125 const char *
126 ctf_get_die_loc_file (dw_die_ref die)
127 {
128 if (!die)
129 return NULL;
130
131 struct dwarf_file_data * file;
132 file = get_AT_file (die, DW_AT_decl_file);
133 if (!file)
134 return NULL;
135
136 return file->filename;
137 }
138
139 unsigned int
140 ctf_get_die_loc_line (dw_die_ref die)
141 {
142 if (!die)
143 return 0;
144
145 return get_AT_unsigned (die, DW_AT_decl_line);
146 }
147
148 unsigned int
149 ctf_get_die_loc_col (dw_die_ref die)
150 {
151 if (!die)
152 return 0;
153
154 return get_AT_unsigned (die, DW_AT_decl_column);
155 }
156
157 /* Generate CTF for the void type. */
158
159 static ctf_dtdef_ref
160 gen_ctf_void_type (ctf_container_ref ctfc)
161 {
162 ctf_encoding_t ctf_encoding = {0, 0, 0};
163
164 /* In CTF the void type is encoded as a 0-byte signed integer
165 type. */
166
167 ctf_encoding.cte_bits = 0;
168 ctf_encoding.cte_format = CTF_INT_SIGNED;
169
170 gcc_assert (ctf_void_die != NULL);
171 return ctf_add_integer (ctfc, CTF_ADD_ROOT, "void",
172 &ctf_encoding, ctf_void_die);
173 }
174
175 /* Generate CTF type of unknown kind. */
176
177 static ctf_dtdef_ref
178 gen_ctf_unknown_type (ctf_container_ref ctfc)
179 {
180 ctf_dtdef_ref dtd;
181
182 /* In CTF, the unknown type is encoded as a 0 byte sized type with kind
183 CTF_K_UNKNOWN. Create an encoding object merely to reuse the underlying
184 ctf_add_encoded interface; the CTF encoding object is not 'used' any more
185 than just the generation of size from. */
186 ctf_encoding_t ctf_encoding = {0, 0, 0};
187
188 gcc_assert (ctf_unknown_die != NULL);
189 /* Type de-duplication. */
190 if (!ctf_type_exists (ctfc, ctf_unknown_die, &dtd))
191 dtd = ctf_add_unknown (ctfc, CTF_ADD_ROOT, "unknown",
192 &ctf_encoding, ctf_unknown_die);
193
194 return dtd;
195 }
196
197 /* Sizes of entities can be given in bytes or bits. This function
198 abstracts this by returning the size in bits of the given entity.
199 If no DW_AT_byte_size nor DW_AT_bit_size are defined, this function
200 returns 0. */
201
202 static uint32_t
203 ctf_die_bitsize (dw_die_ref die)
204 {
205 dw_attr_node *attr_byte_size = get_AT (die, DW_AT_byte_size);
206 dw_attr_node *attr_bit_size = get_AT (die, DW_AT_bit_size);
207
208 if (attr_bit_size)
209 return AT_unsigned (attr_bit_size);
210 else if (attr_byte_size)
211 return (AT_unsigned (attr_byte_size) * 8);
212 else
213 return 0;
214 }
215
216 /* Generate CTF for base type (integer, boolean, real, fixed point and complex).
217 Important: the caller of this API must make sure that duplicate types are
218 not added. */
219
220 static ctf_dtdef_ref
221 gen_ctf_base_type (ctf_container_ref ctfc, dw_die_ref type)
222 {
223 ctf_dtdef_ref dtd = NULL;
224
225 ctf_encoding_t ctf_encoding = {0, 0, 0};
226
227 unsigned int encoding = get_AT_unsigned (type, DW_AT_encoding);
228 unsigned int bit_size = ctf_die_bitsize (type);
229 const char * name_string = get_AT_string (type, DW_AT_name);
230
231 switch (encoding)
232 {
233 case DW_ATE_void:
234
235 ctf_encoding.cte_format = CTF_INT_SIGNED;
236 ctf_encoding.cte_bits = 0;
237
238 gcc_assert (name_string);
239 dtd = ctf_add_integer (ctfc, CTF_ADD_ROOT, name_string,
240 &ctf_encoding, type);
241
242 break;
243 case DW_ATE_boolean:
244
245 ctf_encoding.cte_format = CTF_INT_BOOL;
246 ctf_encoding.cte_bits = bit_size;
247
248 gcc_assert (name_string);
249 dtd = ctf_add_integer (ctfc, CTF_ADD_ROOT, name_string,
250 &ctf_encoding, type);
251 break;
252 case DW_ATE_float:
253 {
254 unsigned int float_bit_size
255 = tree_to_uhwi (TYPE_SIZE (float_type_node));
256 unsigned int double_bit_size
257 = tree_to_uhwi (TYPE_SIZE (double_type_node));
258 unsigned int long_double_bit_size
259 = tree_to_uhwi (TYPE_SIZE (long_double_type_node));
260
261 if (bit_size == float_bit_size)
262 ctf_encoding.cte_format = CTF_FP_SINGLE;
263 else if (bit_size == double_bit_size)
264 ctf_encoding.cte_format = CTF_FP_DOUBLE;
265 else if (bit_size == long_double_bit_size)
266 ctf_encoding.cte_format = CTF_FP_LDOUBLE;
267 else
268 /* CTF does not have representation for other types. Skip them. */
269 break;
270
271 ctf_encoding.cte_bits = bit_size;
272 dtd = ctf_add_float (ctfc, CTF_ADD_ROOT, name_string,
273 &ctf_encoding, type);
274
275 break;
276 }
277 case DW_ATE_signed_char:
278 /* FALLTHROUGH */
279 case DW_ATE_unsigned_char:
280 /* FALLTHROUGH */
281 case DW_ATE_signed:
282 /* FALLTHROUGH */
283 case DW_ATE_unsigned:
284
285 if (encoding == DW_ATE_signed_char
286 || encoding == DW_ATE_unsigned_char)
287 ctf_encoding.cte_format |= CTF_INT_CHAR;
288
289 if (encoding == DW_ATE_signed
290 || encoding == DW_ATE_signed_char)
291 ctf_encoding.cte_format |= CTF_INT_SIGNED;
292
293 ctf_encoding.cte_bits = bit_size;
294 dtd = ctf_add_integer (ctfc, CTF_ADD_ROOT, name_string,
295 &ctf_encoding, type);
296 break;
297
298 case DW_ATE_complex_float:
299 {
300 unsigned int float_bit_size
301 = tree_to_uhwi (TYPE_SIZE (float_type_node));
302 unsigned int double_bit_size
303 = tree_to_uhwi (TYPE_SIZE (double_type_node));
304 unsigned int long_double_bit_size
305 = tree_to_uhwi (TYPE_SIZE (long_double_type_node));
306
307 if (bit_size == float_bit_size * 2)
308 ctf_encoding.cte_format = CTF_FP_CPLX;
309 else if (bit_size == double_bit_size * 2)
310 ctf_encoding.cte_format = CTF_FP_DCPLX;
311 else if (bit_size == long_double_bit_size * 2)
312 ctf_encoding.cte_format = CTF_FP_LDCPLX;
313 else
314 /* CTF does not have representation for other types. Skip them. */
315 break;
316
317 ctf_encoding.cte_bits = bit_size;
318 dtd = ctf_add_float (ctfc, CTF_ADD_ROOT, name_string,
319 &ctf_encoding, type);
320 break;
321 }
322 default:
323 /* Ignore. */
324 break;
325 }
326
327 return dtd;
328 }
329
330 /* Generate CTF for a pointer type. */
331
332 static ctf_dtdef_ref
333 gen_ctf_pointer_type (ctf_container_ref ctfc, dw_die_ref ptr_type)
334 {
335 ctf_dtdef_ref pointed_dtd, pointer_dtd;
336 dw_die_ref pointed_type_die = ctf_get_AT_type (ptr_type);
337
338 pointed_dtd = gen_ctf_type (ctfc, pointed_type_die);
339
340 /* Type de-duplication.
341 Consult the ctfc_types hash again before adding the CTF pointer type
342 because there can be cases where a pointer type may have been added by
343 the gen_ctf_type call above. */
344 if (!ctf_type_exists (ctfc, ptr_type, &pointer_dtd))
345 pointer_dtd = ctf_add_pointer (ctfc, CTF_ADD_ROOT, pointed_dtd, ptr_type);
346
347 return pointer_dtd;
348 }
349
350 /* Recursively generate CTF for array dimensions starting at DIE C (of type
351 DW_TAG_subrange_type) until DIE LAST (of type DW_TAG_subrange_type) is
352 reached. ARRAY_ELEMS_TYPE is the CTF type object for the type of the
353 array elements. */
354
355 static ctf_dtdef_ref
356 gen_ctf_subrange_type (ctf_container_ref ctfc, ctf_dtdef_ref array_elems_type,
357 dw_die_ref c, dw_die_ref last)
358 {
359 ctf_arinfo_t arinfo;
360 ctf_dtdef_ref array_dtd;
361
362 dw_attr_node *upper_bound_at;
363 dw_die_ref array_index_type;
364 uint32_t array_num_elements;
365
366 if (dw_get_die_tag (c) == DW_TAG_subrange_type)
367 {
368 /* When DW_AT_upper_bound is used to specify the size of an
369 array in DWARF, it is usually an unsigned constant
370 specifying the upper bound index of the array. However,
371 for unsized arrays, such as foo[] or bar[0],
372 DW_AT_upper_bound is a signed integer constant
373 instead. */
374
375 upper_bound_at = get_AT (c, DW_AT_upper_bound);
376 if (upper_bound_at
377 && AT_class (upper_bound_at) == dw_val_class_unsigned_const)
378 /* This is the upper bound index. */
379 array_num_elements = get_AT_unsigned (c, DW_AT_upper_bound) + 1;
380 else if (get_AT (c, DW_AT_count))
381 array_num_elements = get_AT_unsigned (c, DW_AT_count);
382 else
383 {
384 /* This is a VLA of some kind. */
385 array_num_elements = 0;
386 }
387 }
388 else
389 gcc_unreachable ();
390
391 /* Ok, mount and register the array type. Note how the array
392 type we register here is the type of the elements in
393 subsequent "dimensions", if there are any. */
394 arinfo.ctr_nelems = array_num_elements;
395
396 array_index_type = ctf_get_AT_type (c);
397 arinfo.ctr_index = gen_ctf_type (ctfc, array_index_type);
398
399 if (c == last)
400 arinfo.ctr_contents = array_elems_type;
401 else
402 arinfo.ctr_contents = gen_ctf_subrange_type (ctfc, array_elems_type,
403 dw_get_die_sib (c), last);
404
405 if (!ctf_type_exists (ctfc, c, &array_dtd))
406 array_dtd = ctf_add_array (ctfc, CTF_ADD_ROOT, &arinfo, c);
407
408 return array_dtd;
409 }
410
411 /* Generate CTF for an ARRAY_TYPE. */
412
413 static ctf_dtdef_ref
414 gen_ctf_array_type (ctf_container_ref ctfc,
415 dw_die_ref array_type)
416 {
417 dw_die_ref first, last, array_elems_type;
418 ctf_dtdef_ref array_dtd, elem_dtd;
419
420 int vector_type_p = get_AT_flag (array_type, DW_AT_GNU_vector);
421 if (vector_type_p)
422 return NULL;
423
424 /* Find the first and last array dimension DIEs. */
425 last = dw_get_die_child (array_type);
426 first = dw_get_die_sib (last);
427
428 /* Type de-duplication.
429 Consult the ctfc_types before adding CTF type for the first dimension. */
430 if (!ctf_type_exists (ctfc, first, &array_dtd))
431 {
432 array_elems_type = ctf_get_AT_type (array_type);
433 /* First, register the type of the array elements if needed. */
434 elem_dtd = gen_ctf_type (ctfc, array_elems_type);
435
436 array_dtd = gen_ctf_subrange_type (ctfc, elem_dtd, first, last);
437 }
438
439 return array_dtd;
440 }
441
442 /* Generate CTF for a typedef. */
443
444 static ctf_dtdef_ref
445 gen_ctf_typedef (ctf_container_ref ctfc, dw_die_ref tdef)
446 {
447 ctf_dtdef_ref tdef_dtd, dtd;
448 const char *tdef_name = get_AT_string (tdef, DW_AT_name);
449 dw_die_ref tdef_type = ctf_get_AT_type (tdef);
450
451 dtd = gen_ctf_type (ctfc, tdef_type);
452
453 /* Type de-duplication.
454 This is necessary because the ctf for the typedef may have been already
455 added due to the gen_ctf_type call above. */
456 if (!ctf_type_exists (ctfc, tdef, &tdef_dtd))
457 tdef_dtd = ctf_add_typedef (ctfc, CTF_ADD_ROOT, tdef_name, dtd, tdef);
458
459 return tdef_dtd;
460 }
461
462 /* Generate CTF for a type modifier.
463
464 If the given DIE contains a valid C modifier (like _Atomic), which is not
465 supported by CTF, then this function skips the modifier die and continues
466 with the underlying type.
467
468 If the modifier is supported by CTF, then this function constructs and
469 returns an appropate CTF type representing the modifier.
470
471 For all other cases, this function returns NULL. */
472
473 static ctf_dtdef_ref
474 gen_ctf_modifier_type (ctf_container_ref ctfc, dw_die_ref modifier)
475 {
476 uint32_t kind = CTF_K_MAX;
477 ctf_dtdef_ref dtd, modifier_dtd;
478 dw_die_ref qual_type = ctf_get_AT_type (modifier);
479
480 switch (dw_get_die_tag (modifier))
481 {
482 case DW_TAG_const_type: kind = CTF_K_CONST; break;
483 case DW_TAG_volatile_type: kind = CTF_K_VOLATILE; break;
484 case DW_TAG_restrict_type: kind = CTF_K_RESTRICT; break;
485 case DW_TAG_atomic_type: break;
486 default:
487 return NULL;
488 }
489
490 /* Register the type for which this modifier applies. */
491 dtd = gen_ctf_type (ctfc, qual_type);
492
493 /* Skip generating a CTF modifier record for _Atomic as there is no
494 representation for it. */
495 if (dw_get_die_tag (modifier) == DW_TAG_atomic_type)
496 return dtd;
497
498 gcc_assert (kind != CTF_K_MAX);
499 /* Now register the modifier itself. */
500 if (!ctf_type_exists (ctfc, modifier, &modifier_dtd))
501 modifier_dtd = ctf_add_reftype (ctfc, CTF_ADD_ROOT, dtd, kind, modifier);
502
503 return modifier_dtd;
504 }
505
506 /* Generate CTF for a struct type. */
507
508 static ctf_dtdef_ref
509 gen_ctf_sou_type (ctf_container_ref ctfc, dw_die_ref sou, uint32_t kind)
510 {
511 uint32_t bit_size = ctf_die_bitsize (sou);
512 int declaration_p = get_AT_flag (sou, DW_AT_declaration);
513 const char *sou_name = get_AT_string (sou, DW_AT_name);
514
515 ctf_dtdef_ref sou_dtd;
516
517 /* An incomplete structure or union type is represented in DWARF by
518 a structure or union DIE that does not have a size attribute and
519 that has a DW_AT_declaration attribute. This corresponds to a
520 CTF forward type with kind CTF_K_STRUCT. */
521 if (bit_size == 0 && declaration_p)
522 return ctf_add_forward (ctfc, CTF_ADD_ROOT,
523 sou_name, kind, sou);
524
525 /* This is a complete struct or union type. Generate a CTF type for
526 it if it doesn't exist already. */
527 if (!ctf_type_exists (ctfc, sou, &sou_dtd))
528 sou_dtd = ctf_add_sou (ctfc, CTF_ADD_ROOT,
529 sou_name, kind, bit_size / 8,
530 sou);
531
532 /* Now process the struct members. */
533 {
534 dw_die_ref c;
535
536 c = dw_get_die_child (sou);
537 if (c)
538 do
539 {
540 const char *field_name;
541 dw_die_ref field_type;
542 HOST_WIDE_INT field_location;
543 ctf_dtdef_ref field_dtd;
544
545 c = dw_get_die_sib (c);
546
547 field_name = get_AT_string (c, DW_AT_name);
548 field_type = ctf_get_AT_type (c);
549 field_location = ctf_get_AT_data_member_location (c);
550
551 /* Generate the field type. */
552 field_dtd = gen_ctf_type (ctfc, field_type);
553
554 /* If this is a bit-field, then wrap the field type
555 generated above with a CTF slice. */
556 if (get_AT (c, DW_AT_bit_offset)
557 || get_AT (c, DW_AT_data_bit_offset))
558 {
559 dw_attr_node *attr;
560 HOST_WIDE_INT bitpos = 0;
561 HOST_WIDE_INT bitsize = ctf_die_bitsize (c);
562 HOST_WIDE_INT bit_offset;
563
564 /* The bit offset is given in bits and it may be
565 negative. */
566 attr = get_AT (c, DW_AT_bit_offset);
567 if (attr)
568 {
569 if (AT_class (attr) == dw_val_class_unsigned_const)
570 bit_offset = AT_unsigned (attr);
571 else
572 bit_offset = AT_int (attr);
573
574 if (BYTES_BIG_ENDIAN)
575 bitpos = field_location + bit_offset;
576 else
577 {
578 HOST_WIDE_INT bit_size;
579
580 attr = get_AT (c, DW_AT_byte_size);
581 if (attr)
582 /* Explicit size given in bytes. */
583 bit_size = AT_unsigned (attr) * 8;
584 else
585 /* Infer the size from the member type. */
586 bit_size = ctf_die_bitsize (field_type);
587
588 bitpos = (field_location
589 + bit_size
590 - bit_offset
591 - bitsize);
592 }
593 }
594
595 /* In DWARF5 a data_bit_offset attribute is given with
596 the offset of the data from the beginning of the
597 struct. Acknowledge it if present. */
598 attr = get_AT (c, DW_AT_data_bit_offset);
599 if (attr)
600 bitpos += AT_unsigned (attr);
601
602 /* This is not precisely a TBD_CTF_REPRESENTATION_LIMIT, but
603 surely something to look at for the next format version bump
604 for CTF. */
605 if (bitsize <= 255 && (bitpos - field_location) <= 255)
606 field_dtd = ctf_add_slice (ctfc, CTF_ADD_NONROOT,
607 field_dtd,
608 bitpos - field_location,
609 bitsize, c);
610 else
611 field_dtd = gen_ctf_unknown_type (ctfc);
612 }
613
614 /* Add the field type to the struct or union type. */
615 ctf_add_member_offset (ctfc, sou,
616 field_name,
617 field_dtd,
618 field_location);
619 }
620 while (c != dw_get_die_child (sou));
621 }
622
623 return sou_dtd;
624 }
625
626 /* Generate CTF for a function type. */
627
628 static ctf_dtdef_ref
629 gen_ctf_function_type (ctf_container_ref ctfc, dw_die_ref function,
630 bool from_global_func)
631 {
632 const char *function_name = get_AT_string (function, DW_AT_name);
633 dw_die_ref return_type = ctf_get_AT_type (function);
634
635 ctf_funcinfo_t func_info;
636 uint32_t num_args = 0;
637 int linkage = get_AT_flag (function, DW_AT_external);
638
639 ctf_dtdef_ref return_dtd, function_dtd;
640
641 /* First, add the return type. */
642 return_dtd = gen_ctf_type (ctfc, return_type);
643 func_info.ctc_return = return_dtd;
644
645 /* Type de-duplication.
646 Consult the ctfc_types hash before adding the CTF function type. */
647 if (ctf_type_exists (ctfc, function, &function_dtd))
648 return function_dtd;
649
650 /* Do a first pass on the formals to determine the number of
651 arguments, and whether the function type gets a varargs. */
652 {
653 dw_die_ref c;
654
655 c = dw_get_die_child (function);
656 if (c)
657 do
658 {
659 c = dw_get_die_sib (c);
660
661 if (dw_get_die_tag (c) == DW_TAG_formal_parameter)
662 num_args += 1;
663 else if (dw_get_die_tag (c) == DW_TAG_unspecified_parameters)
664 {
665 func_info.ctc_flags |= CTF_FUNC_VARARG;
666 num_args += 1;
667 }
668 }
669 while (c != dw_get_die_child (function));
670 }
671
672 /* Note the number of typed arguments _includes_ the vararg. */
673 func_info.ctc_argc = num_args;
674
675 /* Type de-duplication has already been performed by now. */
676 function_dtd = ctf_add_function (ctfc, CTF_ADD_ROOT,
677 function_name,
678 (const ctf_funcinfo_t *)&func_info,
679 function,
680 from_global_func,
681 linkage);
682
683 /* Second pass on formals: generate the CTF types corresponding to
684 them and add them as CTF function args. */
685 {
686 dw_die_ref c;
687 unsigned int i = 0;
688 const char *arg_name;
689 ctf_dtdef_ref arg_type;
690
691 c = dw_get_die_child (function);
692 if (c)
693 do
694 {
695 c = dw_get_die_sib (c);
696
697 if (dw_get_die_tag (c) == DW_TAG_unspecified_parameters)
698 {
699 gcc_assert (i == num_args - 1);
700 /* Add an argument with type 0 and no name. */
701 ctf_add_function_arg (ctfc, function, "", NULL);
702 }
703 else if (dw_get_die_tag (c) == DW_TAG_formal_parameter)
704 {
705 i++;
706 arg_name = get_AT_string (c, DW_AT_name);
707 arg_type = gen_ctf_type (ctfc, ctf_get_AT_type (c));
708 /* Add the argument to the existing CTF function type. */
709 ctf_add_function_arg (ctfc, function, arg_name, arg_type);
710 }
711 else
712 /* This is a local variable. Ignore. */
713 continue;
714 }
715 while (c != dw_get_die_child (function));
716 }
717
718 return function_dtd;
719 }
720
721 /* Generate CTF for an enumeration type. */
722
723 static ctf_dtdef_ref
724 gen_ctf_enumeration_type (ctf_container_ref ctfc, dw_die_ref enumeration)
725 {
726 const char *enum_name = get_AT_string (enumeration, DW_AT_name);
727 unsigned int bit_size = ctf_die_bitsize (enumeration);
728 unsigned int signedness = get_AT_unsigned (enumeration, DW_AT_encoding);
729 int declaration_p = get_AT_flag (enumeration, DW_AT_declaration);
730
731 ctf_dtdef_ref enum_dtd;
732
733 /* If this is an incomplete enum, generate a CTF forward for it and
734 be done. */
735 if (declaration_p)
736 {
737 gcc_assert (enum_name);
738 return ctf_add_forward (ctfc, CTF_ADD_ROOT, enum_name,
739 CTF_K_ENUM, enumeration);
740 }
741
742 /* If the size the enumerators is not specified then use the size of
743 the underlying type, which must be a base type. */
744 if (bit_size == 0)
745 {
746 dw_die_ref type = ctf_get_AT_type (enumeration);
747 bit_size = ctf_die_bitsize (type);
748 }
749
750 /* Generate a CTF type for the enumeration. */
751 enum_dtd = ctf_add_enum (ctfc, CTF_ADD_ROOT,
752 enum_name, bit_size / 8,
753 (signedness == DW_ATE_unsigned),
754 enumeration);
755
756 /* Process the enumerators. */
757 {
758 dw_die_ref c;
759
760 c = dw_get_die_child (enumeration);
761 if (c)
762 do
763 {
764 const char *enumerator_name;
765 dw_attr_node *enumerator_value;
766 HOST_WIDE_INT value_wide_int;
767
768 c = dw_get_die_sib (c);
769
770 enumerator_name = get_AT_string (c, DW_AT_name);
771 enumerator_value = get_AT (c, DW_AT_const_value);
772
773 /* enumerator_value can be either a signed or an unsigned
774 constant value. */
775 if (AT_class (enumerator_value) == dw_val_class_unsigned_const
776 || (AT_class (enumerator_value)
777 == dw_val_class_unsigned_const_implicit))
778 value_wide_int = AT_unsigned (enumerator_value);
779 else
780 value_wide_int = AT_int (enumerator_value);
781
782 ctf_add_enumerator (ctfc, enum_dtd,
783 enumerator_name, value_wide_int, enumeration);
784 }
785 while (c != dw_get_die_child (enumeration));
786 }
787
788 return enum_dtd;
789 }
790
791 /* Add a CTF variable record for the given input DWARF DIE. */
792
793 static void
794 gen_ctf_variable (ctf_container_ref ctfc, dw_die_ref die)
795 {
796 const char *var_name = get_AT_string (die, DW_AT_name);
797 dw_die_ref var_type = ctf_get_AT_type (die);
798 unsigned int external_vis = get_AT_flag (die, DW_AT_external);
799 ctf_dtdef_ref var_dtd;
800
801 /* Avoid duplicates. */
802 if (ctf_dvd_lookup (ctfc, die))
803 return;
804
805 /* Do not generate CTF variable records for non-defining incomplete
806 declarations. Such declarations can be known via the DWARF
807 DW_AT_specification attribute. */
808 if (ctf_dvd_ignore_lookup (ctfc, die))
809 return;
810
811 /* The value of the DW_AT_specification attribute, if present, is a
812 reference to the debugging information entry representing the
813 non-defining declaration. */
814 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
815
816 /* Add the type of the variable. */
817 var_dtd = gen_ctf_type (ctfc, var_type);
818
819 /* Generate the new CTF variable and update global counter. */
820 (void) ctf_add_variable (ctfc, var_name, var_dtd, die, external_vis, decl);
821 /* Skip updating the number of global objects at this time. This is updated
822 later after pre-processing as some CTF variable records although
823 generated now, will not be emitted later. [PR105089]. */
824 }
825
826 /* Add a CTF function record for the given input DWARF DIE. */
827
828 static void
829 gen_ctf_function (ctf_container_ref ctfc, dw_die_ref die)
830 {
831 ctf_dtdef_ref function_dtd;
832 /* Type de-duplication.
833 Consult the ctfc_types hash before adding the CTF function type. */
834 if (ctf_type_exists (ctfc, die, &function_dtd))
835 return;
836
837 /* Add the type of the function and update the global functions
838 counter. Note that DWARF encodes function types in both
839 DW_TAG_subroutine_type and DW_TAG_subprogram in exactly the same
840 way. */
841 (void) gen_ctf_function_type (ctfc, die, true /* from_global_func */);
842 ctfc->ctfc_num_global_funcs += 1;
843 }
844
845 /* Add CTF type record(s) for the given input DWARF DIE and return its type id.
846
847 If there is already a CTF type corresponding to the given DIE, then
848 this function returns the type id of the existing type.
849
850 If the given DIE is not recognized as a type, then this function
851 returns NULL. */
852
853 static ctf_dtdef_ref
854 gen_ctf_type (ctf_container_ref ctfc, dw_die_ref die)
855 {
856 ctf_dtdef_ref dtd = NULL;
857 int unrecog_die = false;
858
859 if (ctf_type_exists (ctfc, die, &dtd))
860 return dtd;
861
862 switch (dw_get_die_tag (die))
863 {
864 case DW_TAG_base_type:
865 dtd = gen_ctf_base_type (ctfc, die);
866 break;
867 case DW_TAG_pointer_type:
868 dtd = gen_ctf_pointer_type (ctfc, die);
869 break;
870 case DW_TAG_typedef:
871 dtd = gen_ctf_typedef (ctfc, die);
872 break;
873 case DW_TAG_array_type:
874 dtd = gen_ctf_array_type (ctfc, die);
875 break;
876 case DW_TAG_structure_type:
877 dtd = gen_ctf_sou_type (ctfc, die, CTF_K_STRUCT);
878 break;
879 case DW_TAG_union_type:
880 dtd = gen_ctf_sou_type (ctfc, die, CTF_K_UNION);
881 break;
882 case DW_TAG_subroutine_type:
883 dtd = gen_ctf_function_type (ctfc, die,
884 false /* from_global_func */);
885 break;
886 case DW_TAG_enumeration_type:
887 dtd = gen_ctf_enumeration_type (ctfc, die);
888 break;
889 case DW_TAG_atomic_type:
890 /* FALLTHROUGH */
891 case DW_TAG_const_type:
892 /* FALLTHROUGH */
893 case DW_TAG_restrict_type:
894 /* FALLTHROUGH */
895 case DW_TAG_volatile_type:
896 dtd = gen_ctf_modifier_type (ctfc, die);
897 break;
898 case DW_TAG_unspecified_type:
899 {
900 const char *name = get_AT_string (die, DW_AT_name);
901
902 if (name && strcmp (name, "void") == 0)
903 dtd = gen_ctf_void_type (ctfc);
904 else
905 dtd = NULL;
906
907 break;
908 }
909 case DW_TAG_reference_type:
910 dtd = NULL;
911 break;
912 default:
913 /* Unrecognized DIE. */
914 unrecog_die = true;
915 dtd = NULL;
916 break;
917 }
918
919 /* For all types unrepresented in CTF, use an explicit CTF type of kind
920 CTF_K_UNKNOWN. */
921 if ((dtd == NULL) && (!unrecog_die))
922 dtd = gen_ctf_unknown_type (ctfc);
923
924 return dtd;
925 }
926
927 bool
928 ctf_do_die (dw_die_ref die)
929 {
930 ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
931
932 /* Note how we tell the caller to continue traversing children DIEs
933 if this DIE didn't result in CTF records being added. */
934 if (dw_get_die_tag (die) == DW_TAG_variable)
935 {
936 gen_ctf_variable (tu_ctfc, die);
937 return false;
938 }
939 else if (dw_get_die_tag (die) == DW_TAG_subprogram)
940 {
941 gen_ctf_function (tu_ctfc, die);
942 return false;
943 }
944 else
945 return (gen_ctf_type (tu_ctfc, die) == NULL);
946 }
947
948 /* Initialize CTF subsystem for CTF debug info generation. */
949
950 void
951 ctf_debug_init (void)
952 {
953 /* First, initialize the CTF subsystem. */
954 ctf_init ();
955
956 /* Create a couple of DIE structures that we may need. */
957 ctf_void_die = new_die_raw (DW_TAG_unspecified_type);
958 add_name_attribute (ctf_void_die, "void");
959 ctf_array_index_die
960 = base_type_die (integer_type_node, 0 /* reverse */);
961 add_name_attribute (ctf_array_index_die, "int");
962 ctf_unknown_die = new_die_raw (DW_TAG_unspecified_type);
963 add_name_attribute (ctf_unknown_die, "unknown");
964 }
965
966 /* Early finish CTF/BTF debug info. */
967
968 void
969 ctf_debug_early_finish (const char * filename)
970 {
971 /* Emit the collected CTF information. */
972 if (ctf_debug_info_level > CTFINFO_LEVEL_NONE)
973 ctf_output (filename);
974
975 /* If emitting BTF, start translation to BTF. */
976 if (btf_debuginfo_p ())
977 {
978 btf_early_finish ();
979
980 /* For LTO builds, also emit BTF now. */
981 if (flag_lto && !in_lto_p)
982 btf_finish ();
983 }
984 else
985 /* Otherwise, done with the CTF container. */
986 ctf_finalize ();
987 }
988
989 /* Finish CTF/BTF debug info emission. */
990
991 void
992 ctf_debug_finish ()
993 {
994 /* Emit BTF late, unless this is an LTO build in which case it was
995 already done early. */
996 if (btf_debuginfo_p () && !flag_lto)
997 btf_finish ();
998 }
999
1000 #include "gt-dwarf2ctf.h"