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