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