Debian Codegraph @

gcc/ctfc.cc

raw
1 /* Generate CTF.
2 Copyright (C) 2019-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 "toplev.h"
25 #include "ctfc.h"
26 #include "diagnostic-core.h"
27
28 /* A CTF container object - one per translation unit. */
29
30 ctf_container_ref tu_ctfc;
31
32 ctf_container_ref
33 ctf_get_tu_ctfc (void)
34 {
35 return tu_ctfc;
36 }
37
38 /* If the next ctf type id is still set to the init value, no ctf records to
39 report. */
40 bool
41 ctfc_is_empty_container (ctf_container_ref ctfc)
42 {
43 return ((ctfc)->ctfc_nextid == CTF_INIT_TYPEID);
44 }
45
46 /* Get the total number of CTF types in the container. */
47
48 unsigned int
49 ctfc_get_num_ctf_types (ctf_container_ref ctfc)
50 {
51 return ctfc->ctfc_types->elements ();
52 }
53
54 /* Get the total number of CTF variables in the container. */
55
56 unsigned int ctfc_get_num_ctf_vars (ctf_container_ref ctfc)
57 {
58 return ctfc->ctfc_vars->elements ();
59 }
60
61 /* Get reference to the CTF string table or the CTF auxilliary
62 string table. */
63
64 ctf_strtable_t *
65 ctfc_get_strtab (ctf_container_ref ctfc, int aux)
66 {
67 return aux ? &(ctfc)->ctfc_aux_strtable : &(ctfc->ctfc_strtable);
68 }
69
70 /* Get the length of the specified string table of the CTF container. */
71
72 size_t
73 ctfc_get_strtab_len (ctf_container_ref ctfc, int aux)
74 {
75 ctf_strtable_t * strtab = ctfc_get_strtab (ctfc, aux);
76 return strtab->ctstab_len;
77 }
78
79 /* Get the number of bytes to represent the variable length portion of all CTF
80 types in the CTF container. */
81
82 size_t ctfc_get_num_vlen_bytes (ctf_container_ref ctfc)
83 {
84 return ctfc->ctfc_num_vlen_bytes;
85 }
86
87 /* Return which member of the union is used in CTFTYPE. Used for garbage
88 collection. */
89
90 enum ctf_dtu_d_union_enum
91 ctf_dtu_d_union_selector (ctf_dtdef_ref ctftype)
92 {
93 uint32_t kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
94 switch (kind)
95 {
96 case CTF_K_UNKNOWN:
97 case CTF_K_INTEGER:
98 case CTF_K_FLOAT:
99 return CTF_DTU_D_ENCODING;
100 case CTF_K_STRUCT:
101 case CTF_K_UNION:
102 case CTF_K_ENUM:
103 return CTF_DTU_D_MEMBERS;
104 case CTF_K_ARRAY:
105 return CTF_DTU_D_ARRAY;
106 case CTF_K_FUNCTION:
107 return CTF_DTU_D_ARGUMENTS;
108 case CTF_K_SLICE:
109 return CTF_DTU_D_SLICE;
110 default:
111 /* The largest member as default. */
112 return CTF_DTU_D_ARRAY;
113 }
114 }
115
116 /* Insert CTF type into the CTF container. */
117
118 static void
119 ctf_dtd_insert (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
120 {
121 bool existed = false;
122 ctf_dtdef_ref entry = dtd;
123
124 ctf_dtdef_ref * item = ctfc->ctfc_types->find_slot (entry, INSERT);
125 if (*item == NULL)
126 *item = dtd;
127 else
128 existed = true;
129 /* Duplicate CTF type records not expected to be inserted. */
130 gcc_assert (!existed);
131 }
132
133 /* Lookup CTF type given a DWARF die for the type. */
134
135 ctf_dtdef_ref
136 ctf_dtd_lookup (const ctf_container_ref ctfc, const dw_die_ref type)
137 {
138 ctf_dtdef_t entry;
139 entry.dtd_key = type;
140
141 ctf_dtdef_ref * slot = ctfc->ctfc_types->find_slot (&entry, NO_INSERT);
142
143 if (slot)
144 return (ctf_dtdef_ref)*slot;
145
146 return NULL;
147 }
148
149 /* Insert CTF variable into the CTF container. */
150
151 static void
152 ctf_dvd_insert (ctf_container_ref ctfc, ctf_dvdef_ref dvd)
153 {
154 bool existed = false;
155 ctf_dvdef_ref entry = dvd;
156
157 ctf_dvdef_ref * item = ctfc->ctfc_vars->find_slot (entry, INSERT);
158 if (*item == NULL)
159 *item = dvd;
160 else
161 existed = true;
162 /* Duplicate variable records not expected to be inserted. */
163 gcc_assert (!existed);
164 }
165
166 /* Lookup CTF variable given a DWARF die for the decl. */
167
168 ctf_dvdef_ref
169 ctf_dvd_lookup (const ctf_container_ref ctfc, dw_die_ref die)
170 {
171 ctf_dvdef_t entry;
172 entry.dvd_key = die;
173
174 ctf_dvdef_ref * slot = ctfc->ctfc_vars->find_slot (&entry, NO_INSERT);
175
176 if (slot)
177 return (ctf_dvdef_ref)*slot;
178
179 return NULL;
180 }
181
182 /* Insert a dummy CTF variable into the list of variables to be ignored. */
183
184 static void
185 ctf_dvd_ignore_insert (ctf_container_ref ctfc, ctf_dvdef_ref dvd)
186 {
187 bool existed = false;
188 ctf_dvdef_ref entry = dvd;
189
190 ctf_dvdef_ref * item = ctfc->ctfc_ignore_vars->find_slot (entry, INSERT);
191 if (*item == NULL)
192 *item = dvd;
193 else
194 existed = true;
195 /* Duplicate variable records not expected to be inserted. */
196 gcc_assert (!existed);
197 }
198
199 /* Lookup the dummy CTF variable given the DWARF die for the non-defining
200 decl to be ignored. */
201
202 bool
203 ctf_dvd_ignore_lookup (const ctf_container_ref ctfc, dw_die_ref die)
204 {
205 ctf_dvdef_t entry;
206 entry.dvd_key = die;
207
208 ctf_dvdef_ref * slot = ctfc->ctfc_ignore_vars->find_slot (&entry, NO_INSERT);
209
210 if (slot)
211 return true;
212
213 return false;
214 }
215
216 /* Append member definition to the list. Member list is a singly-linked list
217 with list start pointing to the head. */
218
219 static void
220 ctf_dmd_list_append (ctf_dmdef_t ** dmd, ctf_dmdef_t * elem)
221 {
222 ctf_dmdef_t * tail = (dmd && *dmd) ? *dmd : NULL;
223 if (tail)
224 {
225 while (tail->dmd_next)
226 tail = tail->dmd_next;
227
228 tail->dmd_next = elem;
229 }
230 else
231 *dmd = elem;
232
233 elem->dmd_next = NULL;
234 }
235
236 /* Append function argument to the list. Member list is a singly-linked list
237 with list start pointing to the head. */
238
239 static void
240 ctf_farg_list_append (ctf_func_arg_t ** farg, ctf_func_arg_t * elem)
241 {
242 ctf_func_arg_t * tail = (farg && *farg) ? *farg : NULL;
243 if (tail)
244 {
245 while (tail->farg_next)
246 tail = tail->farg_next;
247
248 tail->farg_next = elem;
249 }
250 else
251 *farg = elem;
252
253 elem->farg_next = NULL;
254 }
255
256 /* Append str to the CTF string table. */
257
258 static void
259 ctfc_strtable_append_str (ctf_strtable_t * str_table, const char * str)
260 {
261 ctf_string_t * ctf_string = ggc_cleared_alloc<ctf_string_t> ();
262 /* Keep a reference to the input STR. */
263 ctf_string->cts_str = str;
264 ctf_string->cts_next = NULL;
265
266 if (!str_table->ctstab_head)
267 str_table->ctstab_head = ctf_string;
268
269 /* Append to the end of the list. */
270 if (str_table->ctstab_tail)
271 str_table->ctstab_tail->cts_next = ctf_string;
272
273 str_table->ctstab_tail = ctf_string;
274 }
275
276 /* Wrapper function to add str to the CTF string table. No de-duplication of
277 CTF strings is done by the compiler. */
278
279 static const char *
280 ctfc_strtable_add_str (ctf_strtable_t * str_table, const char * name,
281 uint32_t * name_offset)
282 {
283 size_t len;
284 char * ctf_string;
285 /* Return value is the offset to the string in the string table. */
286 uint32_t str_offset = str_table->ctstab_len;
287
288 /* Add empty string only once at the beginning of the string table. Also, do
289 not add null strings, return the offset to the empty string for them. */
290 if ((!name || (name != NULL && !strcmp (name, ""))) && str_offset)
291 {
292 ctf_string = CONST_CAST (char *, str_table->ctstab_estr);
293 str_offset = 0;
294 }
295 else
296 {
297 gcc_assert (name);
298 /* Add null-terminated strings to the string table. */
299 len = strlen (name) + 1;
300 ctf_string = CONST_CAST (char *, ggc_strdup (name));
301
302 ctfc_strtable_append_str (str_table, ctf_string);
303 /* Add string to the string table. Keep number of strings updated. */
304 str_table->ctstab_num++;
305 /* Keep the number of bytes contained in the string table updated. */
306 str_table->ctstab_len += len;
307 }
308
309 *name_offset = str_offset;
310
311 return (const char *) ctf_string;
312
313 }
314
315 /* Add string to the appropriate string table in the CTF container. */
316
317 const char *
318 ctf_add_string (ctf_container_ref ctfc, const char * name,
319 uint32_t * name_offset, int aux_str = CTF_STRTAB)
320 {
321 /* Get the CTF string table or the CTF auxilliary string table,
322 as applicable. */
323 ctf_strtable_t *str_table = ctfc_get_strtab (ctfc, aux_str);
324 return ctfc_strtable_add_str (str_table, name, name_offset);
325 }
326
327 /* Add the compilation unit (CU) name string to the CTF string table. The
328 CU name has a prepended pwd string if it is a relative path. Also set the
329 CU name offset in the CTF container. */
330
331 void
332 ctf_add_cuname (ctf_container_ref ctfc, const char * filename)
333 {
334 char * cuname = NULL;
335
336 /* (filename at this point of compilation cannot be null). */
337
338 if (!IS_DIR_SEPARATOR (filename[0]))
339 {
340 /* Filename is a relative path. */
341 const char * cu_pwd = get_src_pwd ();
342 const int cu_pwd_len = strlen (cu_pwd);
343
344 /* Add a DIR_SEPARATOR char before the filename. */
345 const int len = cu_pwd_len + 2 + strlen (filename);
346
347 cuname = (char *) ggc_alloc_atomic (len);
348 memset (cuname, 0, len);
349
350 strcpy (cuname, cu_pwd);
351 cuname[cu_pwd_len] = DIR_SEPARATOR;
352 cuname[cu_pwd_len+1] = 0;
353 strcat (cuname, filename);
354 }
355 else
356 /* Filename is an absolute path. */
357 cuname = CONST_CAST (char *, ggc_strdup (filename));
358
359 ctf_add_string (ctfc, cuname, &(ctfc->ctfc_cuname_offset));
360 /* Add 1 as CTF strings in the CTF string table are null-terminated
361 strings. */
362 ctfc->ctfc_strlen += strlen (cuname) + 1;
363
364 /* Mark cuname for garbage collection. */
365 cuname = NULL;
366 }
367
368 /* Functions to create CTF types.
369
370 These functions perform the task of adding CTF types to the CTF container.
371 No de-duplication is done by them; the onus is on the calling function to do
372 so. The caller must first do a lookup via ctf_dtd_lookup or
373 ctf_dvd_lookup, as applicable, to ascertain that the CTF type or the CTF
374 variable respectively does not already exist, and then add it. */
375
376 static ctf_dtdef_ref
377 ctf_add_generic (ctf_container_ref ctfc, uint32_t flag, const char * name,
378 dw_die_ref die)
379 {
380 ctf_dtdef_ref dtd;
381 ctf_id_t type;
382
383 gcc_assert (flag == CTF_ADD_NONROOT || flag == CTF_ADD_ROOT);
384
385 dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
386
387 type = ctfc->ctfc_nextid++;
388 gcc_assert (type < CTF_MAX_TYPE); /* CTF type ID overflow. */
389
390 /* Buffer the strings in the CTF string table. */
391 dtd->dtd_name = ctf_add_string (ctfc, name, &(dtd->dtd_data.ctti_name));
392 dtd->dtd_type = type;
393 dtd->dtd_key = die;
394
395 if ((name != NULL) && strcmp (name, ""))
396 ctfc->ctfc_strlen += strlen (name) + 1;
397
398 ctf_dtd_insert (ctfc, dtd);
399
400 return dtd;
401 }
402
403 static ctf_dtdef_ref
404 ctf_add_encoded (ctf_container_ref ctfc, uint32_t flag, const char * name,
405 const ctf_encoding_t * ep, uint32_t kind, dw_die_ref die)
406 {
407 ctf_dtdef_ref dtd;
408
409 dtd = ctf_add_generic (ctfc, flag, name, die);
410
411 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, flag, 0);
412
413 uint32_t roundup_nbytes = (ROUND_UP (ep->cte_bits, BITS_PER_UNIT)
414 / BITS_PER_UNIT);
415
416 /* FIXME, stay close to what libctf does. But by getting next power of two,
417 aren't we conveying less precise information. E.g. floating point mode
418 XF has a size of 12 bytes. */
419 dtd->dtd_data.ctti_size = roundup_nbytes ? (1 << ceil_log2 (roundup_nbytes))
420 : roundup_nbytes;
421 dtd->dtd_u.dtu_enc = *ep;
422
423 ctfc->ctfc_num_stypes++;
424
425 return dtd;
426 }
427
428 ctf_dtdef_ref
429 ctf_add_reftype (ctf_container_ref ctfc, uint32_t flag, ctf_dtdef_ref ref,
430 uint32_t kind, dw_die_ref die)
431 {
432 ctf_dtdef_ref dtd;
433
434 gcc_assert (ref != NULL);
435
436 dtd = ctf_add_generic (ctfc, flag, NULL, die);
437 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, flag, 0);
438 /* Caller of this API must guarantee that a CTF type with id = ref already
439 exists. This will also be validated for us at link-time. */
440 dtd->dtd_data.ctti_type = (uint32_t) ref->dtd_type;
441 dtd->ref_type = ref;
442
443 ctfc->ctfc_num_stypes++;
444
445 return dtd;
446 }
447
448 ctf_dtdef_ref
449 ctf_add_forward (ctf_container_ref ctfc, uint32_t flag, const char * name,
450 uint32_t kind, dw_die_ref die)
451 {
452 ctf_dtdef_ref dtd;
453
454 dtd = ctf_add_generic (ctfc, flag, name, die);
455
456 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_FORWARD, flag, 0);
457 dtd->dtd_data.ctti_type = kind;
458
459 ctfc->ctfc_num_stypes++;
460
461 return dtd;
462 }
463
464 ctf_dtdef_ref
465 ctf_add_typedef (ctf_container_ref ctfc, uint32_t flag, const char * name,
466 ctf_dtdef_ref ref, dw_die_ref die)
467 {
468 ctf_dtdef_ref dtd;
469
470 gcc_assert (ref != NULL);
471 /* Nameless Typedefs are not expected. */
472 gcc_assert ((name != NULL) && strcmp (name, ""));
473
474 dtd = ctf_add_generic (ctfc, flag, name, die);
475 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_TYPEDEF, flag, 0);
476 dtd->dtd_data.ctti_type = (uint32_t) ref->dtd_type;
477 dtd->ref_type = ref;
478
479 gcc_assert (dtd->dtd_type != dtd->dtd_data.ctti_type);
480
481 ctfc->ctfc_num_stypes++;
482
483 return dtd;
484 }
485
486 ctf_dtdef_ref
487 ctf_add_slice (ctf_container_ref ctfc, uint32_t flag, ctf_dtdef_ref ref,
488 uint32_t bit_offset, uint32_t bit_size, dw_die_ref die)
489 {
490 ctf_dtdef_ref dtd;
491 uint32_t roundup_nbytes;
492
493 gcc_assert ((bit_size <= 255) && (bit_offset <= 255));
494
495 gcc_assert (ref != NULL);
496
497 dtd = ctf_add_generic (ctfc, flag, NULL, die);
498
499 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0);
500
501 roundup_nbytes = (ROUND_UP (bit_size, BITS_PER_UNIT) / BITS_PER_UNIT);
502 /* FIXME, stay close to what libctf does. But by getting next power of two,
503 aren't we conveying less precise information, especially for bitfields.
504 For example, cte_bits = 33, roundup_nbytes = 5, ctti_size = 8 in the
505 implementation below. */
506 dtd->dtd_data.ctti_size = roundup_nbytes ? (1 << ceil_log2 (roundup_nbytes))
507 : 0;
508
509 dtd->dtd_u.dtu_slice.cts_type = ref;
510 dtd->dtd_u.dtu_slice.cts_bits = bit_size;
511 dtd->dtd_u.dtu_slice.cts_offset = bit_offset;
512
513 ctfc->ctfc_num_stypes++;
514
515 return dtd;
516 }
517
518 ctf_dtdef_ref
519 ctf_add_float (ctf_container_ref ctfc, uint32_t flag,
520 const char * name, const ctf_encoding_t * ep, dw_die_ref die)
521 {
522 return (ctf_add_encoded (ctfc, flag, name, ep, CTF_K_FLOAT, die));
523 }
524
525 ctf_dtdef_ref
526 ctf_add_integer (ctf_container_ref ctfc, uint32_t flag,
527 const char * name, const ctf_encoding_t * ep, dw_die_ref die)
528 {
529 return (ctf_add_encoded (ctfc, flag, name, ep, CTF_K_INTEGER, die));
530 }
531
532 ctf_dtdef_ref
533 ctf_add_unknown (ctf_container_ref ctfc, uint32_t flag,
534 const char * name, const ctf_encoding_t * ep, dw_die_ref die)
535 {
536 return (ctf_add_encoded (ctfc, flag, name, ep, CTF_K_UNKNOWN, die));
537 }
538
539 ctf_dtdef_ref
540 ctf_add_pointer (ctf_container_ref ctfc, uint32_t flag, ctf_dtdef_ref ref,
541 dw_die_ref die)
542 {
543 return (ctf_add_reftype (ctfc, flag, ref, CTF_K_POINTER, die));
544 }
545
546 ctf_dtdef_ref
547 ctf_add_array (ctf_container_ref ctfc, uint32_t flag, const ctf_arinfo_t * arp,
548 dw_die_ref die)
549 {
550 ctf_dtdef_ref dtd;
551
552 gcc_assert (arp);
553
554 /* Caller of this API must make sure CTF type for arp->ctr_contents and
555 arp->ctr_index are already added. This will also be validated for us at
556 link-time. */
557
558 dtd = ctf_add_generic (ctfc, flag, NULL, die);
559
560 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_ARRAY, flag, 0);
561 dtd->dtd_data.ctti_size = 0;
562 dtd->dtd_u.dtu_arr = *arp;
563
564 ctfc->ctfc_num_stypes++;
565
566 return dtd;
567 }
568
569 ctf_dtdef_ref
570 ctf_add_enum (ctf_container_ref ctfc, uint32_t flag, const char * name,
571 HOST_WIDE_INT size, bool eunsigned, dw_die_ref die)
572 {
573 ctf_dtdef_ref dtd;
574
575 /* In the compiler, no need to handle the case of promoting forwards to
576 enums. This comment is simply to note a divergence from libctf. */
577
578 /* The compiler does, however, update any previously existing forward types
579 to non-root. CTF does not allow existence of two root types with the same
580 name. */
581 ctf_dtdef_ref enum_fwd_type = ctf_dtd_lookup (ctfc, die);
582 if (enum_fwd_type)
583 {
584 enum_fwd_type->dtd_data.ctti_info
585 = CTF_TYPE_INFO (CTF_K_FORWARD, CTF_ADD_NONROOT, 0);
586 }
587
588 dtd = ctf_add_generic (ctfc, flag, name, die);
589
590 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_ENUM, flag, 0);
591
592 /* Size in bytes should always fit, of course.
593 TBD WARN - warn instead? */
594 gcc_assert (size <= CTF_MAX_SIZE);
595
596 dtd->dtd_data.ctti_size = size;
597 dtd->dtd_enum_unsigned = eunsigned;
598
599 ctfc->ctfc_num_stypes++;
600
601 return dtd;
602 }
603
604 int
605 ctf_add_enumerator (ctf_container_ref ctfc, ctf_dtdef_ref enum_dtd,
606 const char * name, HOST_WIDE_INT value, dw_die_ref die)
607 {
608 ctf_dmdef_t * dmd;
609 uint32_t kind, vlen, root;
610
611 /* The associated CTF type of kind CTF_K_ENUM must already exist.
612 This will also be validated for us at link-time. */
613 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
614 gcc_assert (dtd);
615 gcc_assert (dtd == enum_dtd);
616 gcc_assert (name);
617
618 kind = CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info);
619 root = CTF_V2_INFO_ISROOT (dtd->dtd_data.ctti_info);
620 vlen = CTF_V2_INFO_VLEN (dtd->dtd_data.ctti_info);
621
622 gcc_assert (kind == CTF_K_ENUM && vlen < CTF_MAX_VLEN);
623
624 /* Enum value is of type HOST_WIDE_INT in the compiler, CTF enumerators
625 values in ctf_enum_t is limited to int32_t, BTF supports signed and
626 unsigned enumerators values of 32 and 64 bits, for both debug formats
627 we use ctf_dmdef_t.dmd_value entry of HOST_WIDE_INT type. So check
628 CTF bounds and skip adding this enum value if out of bounds. */
629 if (!btf_debuginfo_p() && ((value > INT_MAX) || (value < INT_MIN)))
630 {
631 /* FIXME - Note this TBD_CTF_REPRESENTATION_LIMIT. */
632 return (1);
633 }
634
635 dmd = ggc_cleared_alloc<ctf_dmdef_t> ();
636
637 /* Buffer the strings in the CTF string table. */
638 dmd->dmd_name = ctf_add_string (ctfc, name, &(dmd->dmd_name_offset));
639 dmd->dmd_type = NULL;
640 dmd->dmd_offset = 0;
641
642 dmd->dmd_value = value;
643
644 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, root, vlen + 1);
645 ctf_dmd_list_append (&dtd->dtd_u.dtu_members, dmd);
646
647 if ((name != NULL) && strcmp (name, ""))
648 ctfc->ctfc_strlen += strlen (name) + 1;
649
650 return (0);
651 }
652
653 int
654 ctf_add_member_offset (ctf_container_ref ctfc, dw_die_ref sou,
655 const char * name, ctf_dtdef_ref type,
656 uint64_t bit_offset)
657 {
658 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, sou);
659 ctf_dmdef_t * dmd;
660
661 uint32_t kind, vlen, root;
662
663 /* The type of the member being added must already exist. */
664 gcc_assert (dtd);
665
666 kind = CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info);
667 root = CTF_V2_INFO_ISROOT (dtd->dtd_data.ctti_info);
668 vlen = CTF_V2_INFO_VLEN (dtd->dtd_data.ctti_info);
669
670 gcc_assert (kind == CTF_K_STRUCT || kind == CTF_K_UNION);
671 gcc_assert (vlen < CTF_MAX_VLEN);
672
673 dmd = ggc_cleared_alloc<ctf_dmdef_t> ();
674
675 /* Buffer the strings in the CTF string table. */
676 dmd->dmd_name = ctf_add_string (ctfc, name, &(dmd->dmd_name_offset));
677 dmd->dmd_type = type;
678 dmd->dmd_value = -1;
679
680 if (kind == CTF_K_STRUCT && vlen != 0)
681 dmd->dmd_offset = bit_offset;
682 else
683 dmd->dmd_offset = 0;
684
685 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, root, vlen + 1);
686 ctf_dmd_list_append (&dtd->dtd_u.dtu_members, dmd);
687
688 if ((name != NULL) && strcmp (name, ""))
689 ctfc->ctfc_strlen += strlen (name) + 1;
690
691 return 0;
692 }
693
694 int
695 ctf_add_variable (ctf_container_ref ctfc, const char * name, ctf_dtdef_ref ref,
696 dw_die_ref die, unsigned int external_vis,
697 dw_die_ref die_var_decl)
698 {
699 ctf_dvdef_ref dvd, dvd_ignore;
700
701 gcc_assert (name);
702
703 if (name != NULL)
704 {
705 dvd = ggc_cleared_alloc<ctf_dvdef_t> ();
706 dvd->dvd_key = die;
707 /* Buffer the strings in the CTF string table. */
708 dvd->dvd_name = ctf_add_string (ctfc, name, &(dvd->dvd_name_offset));
709 dvd->dvd_visibility = external_vis;
710 dvd->dvd_type = ref;
711
712 /* If DW_AT_specification attribute exists, keep track of it as this is
713 the non-defining declaration corresponding to the variable. We will
714 skip emitting CTF variable for such incomplete, non-defining
715 declarations.
716 There could be some non-defining declarations, however, for which a
717 defining declaration does not show up in the same CU. For such
718 cases, the compiler continues to emit CTF variable record as
719 usual. */
720 if (die_var_decl)
721 {
722 dvd_ignore = ggc_cleared_alloc<ctf_dvdef_t> ();
723 dvd_ignore->dvd_key = die_var_decl;
724 /* It's alright to leave other fields as zero. No valid CTF
725 variable will be added for these DW_TAG_variable DIEs. */
726 ctf_dvd_ignore_insert (ctfc, dvd_ignore);
727 }
728
729 ctf_dvd_insert (ctfc, dvd);
730
731 if (strcmp (name, ""))
732 ctfc->ctfc_strlen += strlen (name) + 1;
733 }
734
735 return 0;
736 }
737
738 int
739 ctf_add_function_arg (ctf_container_ref ctfc, dw_die_ref func,
740 const char * name, ctf_dtdef_ref arg_dtd)
741 {
742 ctf_dtdef_ref func_dtd = ctf_dtd_lookup (ctfc, func);
743 ctf_func_arg_t * farg;
744 uint32_t vlen;
745
746 /* The function to which argument is being added must already exist. */
747 gcc_assert (func_dtd);
748 /* The number of args must have been non-zero. */
749 vlen = CTF_V2_INFO_VLEN (func_dtd->dtd_data.ctti_info);
750 gcc_assert (vlen);
751
752 farg = ggc_cleared_alloc<ctf_func_arg_t> ();
753
754 /* Buffer the strings in the auxilliary string table. CTF V3 format does not
755 require function argument names. Use auxilliary string table to keep
756 these strings to avoid unnecessary bloat in CTF section in CTF V3. */
757 farg->farg_name = ctf_add_string (ctfc, name, &(farg->farg_name_offset),
758 CTF_AUX_STRTAB);
759 farg->farg_type = arg_dtd;
760
761 ctf_farg_list_append (&func_dtd->dtd_u.dtu_argv, farg);
762
763 /* For aux_str, keep ctfc_aux_strlen updated for debugging. */
764 if ((name != NULL) && strcmp (name, ""))
765 ctfc->ctfc_aux_strlen += strlen (name) + 1;
766
767 return 0;
768 }
769
770 ctf_dtdef_ref
771 ctf_add_function (ctf_container_ref ctfc, uint32_t flag, const char * name,
772 const ctf_funcinfo_t * ctc, dw_die_ref die,
773 bool from_global_func, int linkage)
774 {
775 ctf_dtdef_ref dtd;
776 uint32_t vlen;
777
778 gcc_assert (ctc);
779
780 vlen = ctc->ctc_argc;
781 gcc_assert (vlen <= CTF_MAX_VLEN);
782
783 dtd = ctf_add_generic (ctfc, flag, name, die);
784
785 dtd->from_global_func = from_global_func;
786 dtd->linkage = linkage;
787 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_FUNCTION, flag, vlen);
788 dtd->ref_type = ctc->ctc_return;
789 /* Caller must make sure CTF types for ctc->ctc_return are already added. */
790 dtd->dtd_data.ctti_type = (uint32_t) ctc->ctc_return->dtd_type;
791 /* Caller must make sure CTF types for function arguments are already added
792 via ctf_add_function_arg () API. */
793
794 ctfc->ctfc_num_stypes++;
795
796 return dtd;
797 }
798
799 ctf_dtdef_ref
800 ctf_add_sou (ctf_container_ref ctfc, uint32_t flag, const char * name,
801 uint32_t kind, size_t size, dw_die_ref die)
802 {
803 ctf_dtdef_ref dtd;
804
805 gcc_assert ((kind == CTF_K_STRUCT) || (kind == CTF_K_UNION));
806
807 /* In the compiler, no need to handle the case of promoting forwards to
808 structs. This comment is simply to note a divergence from libctf. */
809
810 /* The compiler does, however, update any previously existing forward types
811 to non-root. CTF does not allow existence of two root types with the same
812 name. */
813 ctf_dtdef_ref sou_fwd_type = ctf_dtd_lookup (ctfc, die);
814 if (sou_fwd_type)
815 {
816 sou_fwd_type->dtd_data.ctti_info
817 = CTF_TYPE_INFO (CTF_K_FORWARD, CTF_ADD_NONROOT, 0);
818 }
819
820 dtd = ctf_add_generic (ctfc, flag, name, die);
821
822 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, flag, 0);
823
824 if (size > CTF_MAX_SIZE)
825 {
826 dtd->dtd_data.ctti_size = CTF_LSIZE_SENT;
827 dtd->dtd_data.ctti_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
828 dtd->dtd_data.ctti_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
829 ctfc->ctfc_num_types++;
830 }
831 else
832 {
833 dtd->dtd_data.ctti_size = (uint32_t) size;
834 ctfc->ctfc_num_stypes++;
835 }
836
837 return dtd;
838 }
839
840 /* Given a TREE_TYPE node, return the CTF type object for that type. */
841
842 ctf_dtdef_ref
843 ctf_lookup_tree_type (ctf_container_ref ctfc, const tree type)
844 {
845 dw_die_ref die = lookup_type_die (type);
846 if (die == NULL)
847 return NULL;
848
849 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
850 if (dtd == NULL)
851 return NULL;
852
853 return dtd;
854 }
855
856 /* Check if CTF for TYPE has already been generated. Mainstay for
857 de-duplication. If CTF type already exists, returns TRUE and updates
858 the CTF type object DTD for the caller. */
859
860 bool
861 ctf_type_exists (ctf_container_ref ctfc, dw_die_ref type,
862 ctf_dtdef_ref * dtd)
863 {
864 bool exists = false;
865 ctf_dtdef_ref ctf_type_seen = ctf_dtd_lookup (ctfc, type);
866
867 if (ctf_type_seen)
868 {
869 exists = true;
870 /* CTF type for this type exists. */
871 *dtd = ctf_type_seen;
872 }
873
874 return exists;
875 }
876
877 /* Location information for CTF Types and CTF Variables. CTF section does not
878 emit location information; at this time, location information is needed for
879 BTF CO-RE use-cases. */
880
881 int
882 ctfc_get_dtd_srcloc (ctf_dtdef_ref dtd, ctf_srcloc_ref loc)
883 {
884 loc->ctsloc_file = ctf_get_die_loc_file (dtd->dtd_key);
885 loc->ctsloc_line = ctf_get_die_loc_line (dtd->dtd_key);
886 loc->ctsloc_col = ctf_get_die_loc_col (dtd->dtd_key);
887
888 if (loc->ctsloc_file == NULL)
889 return 1;
890
891 return 0;
892 }
893
894 int
895 ctfc_get_dvd_srcloc (ctf_dvdef_ref dvd, ctf_srcloc_ref loc)
896 {
897 loc->ctsloc_file = ctf_get_die_loc_file (dvd->dvd_key);
898 loc->ctsloc_line = ctf_get_die_loc_line (dvd->dvd_key);
899 loc->ctsloc_col = ctf_get_die_loc_col (dvd->dvd_key);
900
901 if (loc->ctsloc_file == NULL)
902 return 1;
903
904 return 0;
905 }
906
907 /* CTF container setup and teardown routines. */
908
909 /* Initialize the CTF string table.
910 The first entry in the CTF string table (empty string) is added. */
911
912 void
913 init_ctf_strtable (ctf_strtable_t * strtab)
914 {
915 strtab->ctstab_head = NULL;
916 strtab->ctstab_tail = NULL;
917 strtab->ctstab_num = 0;
918 strtab->ctstab_len = 0;
919
920 /* The first entry in the CTF string table is an empty string. E.g., CTF
921 type records with no name (like CTF_K_CONST, CTF_K_VOLATILE etc) point to
922 this string. */
923 uint32_t estr_offset = 0;
924 strtab->ctstab_estr = ctfc_strtable_add_str (strtab, "", &estr_offset);
925 }
926
927 /* Initialize the string tables in the CTF container. */
928
929 static void
930 init_ctf_string_table (ctf_container_ref ctfc)
931 {
932 init_ctf_strtable (&ctfc->ctfc_strtable);
933 ctfc->ctfc_strlen++;
934
935 init_ctf_strtable (&ctfc->ctfc_aux_strtable);
936 ctfc->ctfc_aux_strlen++;
937 }
938
939 /* Allocate a new CTF container with the desired flags. */
940
941 static inline ctf_container_ref
942 new_ctf_container (void)
943 {
944 tu_ctfc = ggc_cleared_alloc<ctf_container_t> ();
945 tu_ctfc->ctfc_types
946 = hash_table<ctfc_dtd_hasher>::create_ggc (100);
947 tu_ctfc->ctfc_vars
948 = hash_table<ctfc_dvd_hasher>::create_ggc (100);
949 tu_ctfc->ctfc_ignore_vars
950 = hash_table<ctfc_dvd_hasher>::create_ggc (10);
951
952 return tu_ctfc;
953 }
954
955 /* Initialize a CTF container per translation unit. */
956
957 static void
958 init_ctf_container (void)
959 {
960 tu_ctfc = new_ctf_container ();
961
962 tu_ctfc->ctfc_magic = CTF_MAGIC;
963 tu_ctfc->ctfc_version = CTF_VERSION;
964 tu_ctfc->ctfc_flags = CTF_F_NEWFUNCINFO;
965 tu_ctfc->ctfc_nextid = CTF_INIT_TYPEID;
966
967 init_ctf_string_table (tu_ctfc);
968 }
969
970 void
971 ctfc_delete_strtab (ctf_strtable_t * strtab)
972 {
973 ctf_string_t * str = NULL;
974 ctf_string_t * next_str = NULL;
975
976 str = strtab->ctstab_head;
977 next_str = str;
978 while (next_str != NULL)
979 {
980 next_str = str->cts_next;
981 ggc_free (str);
982 str = next_str;
983 }
984
985 strtab->ctstab_head = NULL;
986 strtab->ctstab_tail = NULL;
987 strtab->ctstab_estr = NULL;
988 }
989
990 /* Delete the CTF container's resources. */
991
992 void
993 ctfc_delete_container (ctf_container_ref ctfc)
994 {
995 if (ctfc)
996 {
997 ctfc->ctfc_types->empty ();
998 ctfc->ctfc_types = NULL;
999
1000 ctfc->ctfc_vars->empty ();
1001 ctfc->ctfc_types = NULL;
1002
1003 ctfc->ctfc_ignore_vars->empty ();
1004 ctfc->ctfc_ignore_vars = NULL;
1005
1006 ctfc_delete_strtab (&ctfc->ctfc_strtable);
1007 ctfc_delete_strtab (&ctfc->ctfc_aux_strtable);
1008 if (ctfc->ctfc_vars_list)
1009 {
1010 ggc_free (ctfc->ctfc_vars_list);
1011 ctfc->ctfc_vars_list = NULL;
1012 }
1013 if (ctfc->ctfc_types_list)
1014 {
1015 ggc_free (ctfc->ctfc_types_list);
1016 ctfc->ctfc_types_list = NULL;
1017 }
1018 if (ctfc->ctfc_gfuncs_list)
1019 {
1020 ggc_free (ctfc->ctfc_gfuncs_list);
1021 ctfc->ctfc_gfuncs_list = NULL;
1022 }
1023 if (ctfc->ctfc_gobjts_list)
1024 {
1025 ggc_free (ctfc->ctfc_gobjts_list);
1026 ctfc->ctfc_gobjts_list = NULL;
1027 }
1028
1029 ctfc= NULL;
1030 }
1031 }
1032
1033 /* CTF routines interfacing to the compiler. */
1034
1035 void
1036 ctf_init (void)
1037 {
1038 init_ctf_container ();
1039 }