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