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