Default to dwarf version 4 on hppa64-hpux
[official-gcc.git] / gcc / ctfc.c
blob73c118e3d49432bae4379d14ae631202bd02bedd
1 /* Generate CTF.
2 Copyright (C) 2019,2021 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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"
28 /* A CTF container object - one per translation unit. */
30 ctf_container_ref tu_ctfc;
32 ctf_container_ref
33 ctf_get_tu_ctfc (void)
35 return tu_ctfc;
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)
43 return ((ctfc)->ctfc_nextid == CTF_INIT_TYPEID);
46 /* Get the total number of CTF types in the container. */
48 unsigned int
49 ctfc_get_num_ctf_types (ctf_container_ref ctfc)
51 return ctfc->ctfc_types->elements ();
54 /* Get the total number of CTF variables in the container. */
56 unsigned int ctfc_get_num_ctf_vars (ctf_container_ref ctfc)
58 return ctfc->ctfc_vars->elements ();
61 /* Get reference to the CTF string table or the CTF auxilliary
62 string table. */
64 ctf_strtable_t *
65 ctfc_get_strtab (ctf_container_ref ctfc, int aux)
67 return aux ? &(ctfc)->ctfc_aux_strtable : &(ctfc->ctfc_strtable);
70 /* Get the length of the specified string table of the CTF container. */
72 size_t
73 ctfc_get_strtab_len (ctf_container_ref ctfc, int aux)
75 ctf_strtable_t * strtab = ctfc_get_strtab (ctfc, aux);
76 return strtab->ctstab_len;
79 /* Get the number of bytes to represent the variable length portion of all CTF
80 types in the CTF container. */
82 size_t ctfc_get_num_vlen_bytes (ctf_container_ref ctfc)
84 return ctfc->ctfc_num_vlen_bytes;
87 /* Return which member of the union is used in CTFTYPE. Used for garbage
88 collection. */
90 enum ctf_dtu_d_union_enum
91 ctf_dtu_d_union_selector (ctf_dtdef_ref ctftype)
93 uint32_t kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
94 switch (kind)
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;
116 /* Insert CTF type into the CTF container. */
118 static void
119 ctf_dtd_insert (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
121 bool existed = false;
122 ctf_dtdef_ref entry = dtd;
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);
133 /* Lookup CTF type given a DWARF die for the type. */
135 ctf_dtdef_ref
136 ctf_dtd_lookup (const ctf_container_ref ctfc, const dw_die_ref type)
138 ctf_dtdef_t entry;
139 entry.dtd_key = type;
141 ctf_dtdef_ref * slot = ctfc->ctfc_types->find_slot (&entry, NO_INSERT);
143 if (slot)
144 return (ctf_dtdef_ref)*slot;
146 return NULL;
149 /* Insert CTF variable into the CTF container. */
151 static void
152 ctf_dvd_insert (ctf_container_ref ctfc, ctf_dvdef_ref dvd)
154 bool existed = false;
155 ctf_dvdef_ref entry = dvd;
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);
166 /* Lookup CTF variable given a DWARF die for the decl. */
168 ctf_dvdef_ref
169 ctf_dvd_lookup (const ctf_container_ref ctfc, dw_die_ref die)
171 ctf_dvdef_t entry;
172 entry.dvd_key = die;
174 ctf_dvdef_ref * slot = ctfc->ctfc_vars->find_slot (&entry, NO_INSERT);
176 if (slot)
177 return (ctf_dvdef_ref)*slot;
179 return NULL;
182 /* Append member definition to the list. Member list is a singly-linked list
183 with list start pointing to the head. */
185 static void
186 ctf_dmd_list_append (ctf_dmdef_t ** dmd, ctf_dmdef_t * elem)
188 ctf_dmdef_t * tail = (dmd && *dmd) ? *dmd : NULL;
189 if (tail)
191 while (tail->dmd_next)
192 tail = tail->dmd_next;
194 tail->dmd_next = elem;
196 else
197 *dmd = elem;
199 elem->dmd_next = NULL;
202 /* Append function argument to the list. Member list is a singly-linked list
203 with list start pointing to the head. */
205 static void
206 ctf_farg_list_append (ctf_func_arg_t ** farg, ctf_func_arg_t * elem)
208 ctf_func_arg_t * tail = (farg && *farg) ? *farg : NULL;
209 if (tail)
211 while (tail->farg_next)
212 tail = tail->farg_next;
214 tail->farg_next = elem;
216 else
217 *farg = elem;
219 elem->farg_next = NULL;
222 /* Append str to the CTF string table. */
224 static void
225 ctfc_strtable_append_str (ctf_strtable_t * str_table, const char * str)
227 ctf_string_t * ctf_string = ggc_cleared_alloc<ctf_string_t> ();
228 /* Keep a reference to the input STR. */
229 ctf_string->cts_str = str;
230 ctf_string->cts_next = NULL;
232 if (!str_table->ctstab_head)
233 str_table->ctstab_head = ctf_string;
235 /* Append to the end of the list. */
236 if (str_table->ctstab_tail)
237 str_table->ctstab_tail->cts_next = ctf_string;
239 str_table->ctstab_tail = ctf_string;
242 /* Wrapper function to add str to the CTF string table. No de-duplication of
243 CTF strings is done by the compiler. */
245 static const char *
246 ctfc_strtable_add_str (ctf_strtable_t * str_table, const char * name,
247 uint32_t * name_offset)
249 size_t len;
250 char * ctf_string;
251 /* Return value is the offset to the string in the string table. */
252 uint32_t str_offset = str_table->ctstab_len;
254 /* Add empty string only once at the beginning of the string table. Also, do
255 not add null strings, return the offset to the empty string for them. */
256 if ((!name || (name != NULL && !strcmp (name, ""))) && str_offset)
258 ctf_string = CONST_CAST (char *, str_table->ctstab_estr);
259 str_offset = 0;
261 else
263 gcc_assert (name);
264 /* Add null-terminated strings to the string table. */
265 len = strlen (name) + 1;
266 ctf_string = CONST_CAST (char *, ggc_strdup (name));
268 ctfc_strtable_append_str (str_table, ctf_string);
269 /* Add string to the string table. Keep number of strings updated. */
270 str_table->ctstab_num++;
271 /* Keep the number of bytes contained in the string table updated. */
272 str_table->ctstab_len += len;
275 *name_offset = str_offset;
277 return (const char *) ctf_string;
281 /* Add string to the appropriate string table in the CTF container. */
283 const char *
284 ctf_add_string (ctf_container_ref ctfc, const char * name,
285 uint32_t * name_offset, int aux_str = CTF_STRTAB)
287 /* Get the CTF string table or the CTF auxilliary string table,
288 as applicable. */
289 ctf_strtable_t *str_table = ctfc_get_strtab (ctfc, aux_str);
290 return ctfc_strtable_add_str (str_table, name, name_offset);
293 /* Add the compilation unit (CU) name string to the the CTF string table. The
294 CU name has a prepended pwd string if it is a relative path. Also set the
295 CU name offset in the CTF container. */
297 void
298 ctf_add_cuname (ctf_container_ref ctfc, const char * filename)
300 char * cuname = NULL;
302 /* (filename at this point of compilation cannot be null). */
304 if (!IS_DIR_SEPARATOR (filename[0]))
306 /* Filename is a relative path. */
307 const char * cu_pwd = get_src_pwd ();
308 const int cu_pwd_len = strlen (cu_pwd);
310 /* Add a DIR_SEPARATOR char before the filename. */
311 const int len = cu_pwd_len + 2 + strlen (filename);
313 cuname = (char *) ggc_alloc_atomic (len);
314 memset (cuname, 0, len);
316 strcpy (cuname, cu_pwd);
317 cuname[cu_pwd_len] = DIR_SEPARATOR;
318 cuname[cu_pwd_len+1] = 0;
319 strcat (cuname, filename);
321 else
322 /* Filename is an absolute path. */
323 cuname = CONST_CAST (char *, ggc_strdup (filename));
325 ctf_add_string (ctfc, cuname, &(ctfc->ctfc_cuname_offset));
326 /* Add 1 as CTF strings in the CTF string table are null-terminated
327 strings. */
328 ctfc->ctfc_strlen += strlen (cuname) + 1;
330 /* Mark cuname for garbage collection. */
331 cuname = NULL;
334 /* Functions to create CTF types.
336 These functions perform the task of adding CTF types to the CTF container.
337 No de-duplication is done by them; the onus is on the calling function to do
338 so. The caller must first do a lookup via ctf_dtd_lookup or
339 ctf_dvd_lookup, as applicable, to ascertain that the CTF type or the CTF
340 variable respectively does not already exist, and then add it. */
342 static ctf_id_t
343 ctf_add_generic (ctf_container_ref ctfc, uint32_t flag, const char * name,
344 ctf_dtdef_ref * rp, dw_die_ref die)
346 ctf_dtdef_ref dtd;
347 ctf_id_t type;
349 gcc_assert (flag == CTF_ADD_NONROOT || flag == CTF_ADD_ROOT);
351 dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
353 type = ctfc->ctfc_nextid++;
354 gcc_assert (type < CTF_MAX_TYPE); /* CTF type ID overflow. */
356 /* Buffer the strings in the CTF string table. */
357 dtd->dtd_name = ctf_add_string (ctfc, name, &(dtd->dtd_data.ctti_name));
358 dtd->dtd_type = type;
359 dtd->dtd_key = die;
361 if ((name != NULL) && strcmp (name, ""))
362 ctfc->ctfc_strlen += strlen (name) + 1;
364 ctf_dtd_insert (ctfc, dtd);
366 *rp = dtd;
367 return type;
370 static ctf_id_t
371 ctf_add_encoded (ctf_container_ref ctfc, uint32_t flag, const char * name,
372 const ctf_encoding_t * ep, uint32_t kind, dw_die_ref die)
374 ctf_dtdef_ref dtd;
375 ctf_id_t type;
377 type = ctf_add_generic (ctfc, flag, name, &dtd, die);
379 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, flag, 0);
381 uint32_t roundup_nbytes = (ROUND_UP (ep->cte_bits, BITS_PER_UNIT)
382 / BITS_PER_UNIT);
384 /* FIXME, stay close to what libctf does. But by getting next power of two,
385 aren't we conveying less precise information. E.g. floating point mode
386 XF has a size of 12 bytes. */
387 dtd->dtd_data.ctti_size = roundup_nbytes ? (1 << ceil_log2 (roundup_nbytes))
388 : roundup_nbytes;
389 dtd->dtd_u.dtu_enc = *ep;
391 ctfc->ctfc_num_stypes++;
393 return type;
396 ctf_id_t
397 ctf_add_reftype (ctf_container_ref ctfc, uint32_t flag, ctf_id_t ref,
398 uint32_t kind, dw_die_ref die)
400 ctf_dtdef_ref dtd;
401 ctf_id_t type;
403 gcc_assert (ref <= CTF_MAX_TYPE);
405 type = ctf_add_generic (ctfc, flag, NULL, &dtd, die);
406 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, flag, 0);
407 /* Caller of this API must guarantee that a CTF type with id = ref already
408 exists. This will also be validated for us at link-time. */
409 dtd->dtd_data.ctti_type = (uint32_t) ref;
411 ctfc->ctfc_num_stypes++;
413 return type;
416 ctf_id_t
417 ctf_add_forward (ctf_container_ref ctfc, uint32_t flag, const char * name,
418 uint32_t kind, dw_die_ref die)
420 ctf_dtdef_ref dtd;
421 ctf_id_t type = 0;
423 type = ctf_add_generic (ctfc, flag, name, &dtd, die);
425 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_FORWARD, flag, 0);
426 dtd->dtd_data.ctti_type = kind;
428 ctfc->ctfc_num_stypes++;
430 return type;
433 ctf_id_t
434 ctf_add_typedef (ctf_container_ref ctfc, uint32_t flag, const char * name,
435 ctf_id_t ref, dw_die_ref die)
437 ctf_dtdef_ref dtd;
438 ctf_id_t type;
440 gcc_assert (ref <= CTF_MAX_TYPE);
441 /* Nameless Typedefs are not expected. */
442 gcc_assert ((name != NULL) && strcmp (name, ""));
444 type = ctf_add_generic (ctfc, flag, name, &dtd, die);
445 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_TYPEDEF, flag, 0);
446 /* Caller of this API must guarantee that a CTF type with id = ref already
447 exists. This will also be validated for us at link-time. */
448 dtd->dtd_data.ctti_type = (uint32_t) ref;
450 gcc_assert (dtd->dtd_type != dtd->dtd_data.ctti_type);
452 ctfc->ctfc_num_stypes++;
454 return type;
457 ctf_id_t
458 ctf_add_slice (ctf_container_ref ctfc, uint32_t flag, ctf_id_t ref,
459 uint32_t bit_offset, uint32_t bit_size, dw_die_ref die)
461 ctf_dtdef_ref dtd;
462 ctf_id_t type;
463 uint32_t roundup_nbytes;
465 gcc_assert ((bit_size <= 255) && (bit_offset <= 255));
467 gcc_assert (ref <= CTF_MAX_TYPE);
469 type = ctf_add_generic (ctfc, flag, NULL, &dtd, die);
471 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0);
473 roundup_nbytes = (ROUND_UP (bit_size, BITS_PER_UNIT) / BITS_PER_UNIT);
474 /* FIXME, stay close to what libctf does. But by getting next power of two,
475 aren't we conveying less precise information, especially for bitfields.
476 For example, cte_bits = 33, roundup_nbytes = 5, ctti_size = 8 in the
477 implementation below. */
478 dtd->dtd_data.ctti_size = roundup_nbytes ? (1 << ceil_log2 (roundup_nbytes))
479 : 0;
481 /* Caller of this API must guarantee that a CTF type with id = ref already
482 exists. This will also be validated for us at link-time. */
483 dtd->dtd_u.dtu_slice.cts_type = (uint32_t) ref;
484 dtd->dtd_u.dtu_slice.cts_bits = bit_size;
485 dtd->dtd_u.dtu_slice.cts_offset = bit_offset;
487 ctfc->ctfc_num_stypes++;
489 return type;
492 ctf_id_t
493 ctf_add_float (ctf_container_ref ctfc, uint32_t flag,
494 const char * name, const ctf_encoding_t * ep, dw_die_ref die)
496 return (ctf_add_encoded (ctfc, flag, name, ep, CTF_K_FLOAT, die));
499 ctf_id_t
500 ctf_add_integer (ctf_container_ref ctfc, uint32_t flag,
501 const char * name, const ctf_encoding_t * ep, dw_die_ref die)
503 return (ctf_add_encoded (ctfc, flag, name, ep, CTF_K_INTEGER, die));
506 ctf_id_t
507 ctf_add_unknown (ctf_container_ref ctfc, uint32_t flag,
508 const char * name, const ctf_encoding_t * ep, dw_die_ref die)
510 return (ctf_add_encoded (ctfc, flag, name, ep, CTF_K_UNKNOWN, die));
513 ctf_id_t
514 ctf_add_pointer (ctf_container_ref ctfc, uint32_t flag, ctf_id_t ref,
515 dw_die_ref die)
517 return (ctf_add_reftype (ctfc, flag, ref, CTF_K_POINTER, die));
520 ctf_id_t
521 ctf_add_array (ctf_container_ref ctfc, uint32_t flag, const ctf_arinfo_t * arp,
522 dw_die_ref die)
524 ctf_dtdef_ref dtd;
525 ctf_id_t type;
527 gcc_assert (arp);
529 /* Caller of this API must make sure CTF type for arp->ctr_contents and
530 arp->ctr_index are already added. This will also be validated for us at
531 link-time. */
533 type = ctf_add_generic (ctfc, flag, NULL, &dtd, die);
535 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_ARRAY, flag, 0);
536 dtd->dtd_data.ctti_size = 0;
537 dtd->dtd_u.dtu_arr = *arp;
539 ctfc->ctfc_num_stypes++;
541 return type;
544 ctf_id_t
545 ctf_add_enum (ctf_container_ref ctfc, uint32_t flag, const char * name,
546 HOST_WIDE_INT size, dw_die_ref die)
548 ctf_dtdef_ref dtd;
549 ctf_id_t type;
551 /* In the compiler, no need to handle the case of promoting forwards to
552 enums. This comment is simply to note a divergence from libctf. */
554 /* The compiler does, however, update any previously existing forward types
555 to non-root. CTF does not allow existence of two root types with the same
556 name. */
557 ctf_dtdef_ref enum_fwd_type = ctf_dtd_lookup (ctfc, die);
558 if (enum_fwd_type)
560 enum_fwd_type->dtd_data.ctti_info
561 = CTF_TYPE_INFO (CTF_K_FORWARD, CTF_ADD_NONROOT, 0);
564 type = ctf_add_generic (ctfc, flag, name, &dtd, die);
566 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_ENUM, flag, 0);
568 /* Size in bytes should always fit, of course.
569 TBD WARN - warn instead? */
570 gcc_assert (size <= CTF_MAX_SIZE);
572 dtd->dtd_data.ctti_size = size;
574 ctfc->ctfc_num_stypes++;
576 return type;
580 ctf_add_enumerator (ctf_container_ref ctfc, ctf_id_t enid, const char * name,
581 HOST_WIDE_INT value, dw_die_ref die)
583 ctf_dmdef_t * dmd;
584 uint32_t kind, vlen, root;
586 /* Callers of this API must make sure that CTF_K_ENUM with enid has been
587 addded. This will also be validated for us at link-time. */
588 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
589 gcc_assert (dtd);
590 gcc_assert (dtd->dtd_type == enid);
591 gcc_assert (name);
593 kind = CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info);
594 root = CTF_V2_INFO_ISROOT (dtd->dtd_data.ctti_info);
595 vlen = CTF_V2_INFO_VLEN (dtd->dtd_data.ctti_info);
597 gcc_assert (kind == CTF_K_ENUM && vlen < CTF_MAX_VLEN);
599 /* Enum value is of type HOST_WIDE_INT in the compiler, dmd_value is int32_t
600 on the other hand. Check bounds and skip adding this enum value if out of
601 bounds. */
602 if ((value > INT_MAX) || (value < INT_MIN))
604 /* FIXME - Note this TBD_CTF_REPRESENTATION_LIMIT. */
605 return (1);
608 dmd = ggc_cleared_alloc<ctf_dmdef_t> ();
610 /* Buffer the strings in the CTF string table. */
611 dmd->dmd_name = ctf_add_string (ctfc, name, &(dmd->dmd_name_offset));
612 dmd->dmd_type = CTF_NULL_TYPEID;
613 dmd->dmd_offset = 0;
615 dmd->dmd_value = value;
617 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, root, vlen + 1);
618 ctf_dmd_list_append (&dtd->dtd_u.dtu_members, dmd);
620 if ((name != NULL) && strcmp (name, ""))
621 ctfc->ctfc_strlen += strlen (name) + 1;
623 return (0);
627 ctf_add_member_offset (ctf_container_ref ctfc, dw_die_ref sou,
628 const char * name, ctf_id_t type,
629 uint64_t bit_offset)
631 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, sou);
632 ctf_dmdef_t * dmd;
634 uint32_t kind, vlen, root;
636 /* The type of the member being added must already exist. */
637 gcc_assert (dtd);
639 kind = CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info);
640 root = CTF_V2_INFO_ISROOT (dtd->dtd_data.ctti_info);
641 vlen = CTF_V2_INFO_VLEN (dtd->dtd_data.ctti_info);
643 gcc_assert (kind == CTF_K_STRUCT || kind == CTF_K_UNION);
644 gcc_assert (vlen < CTF_MAX_VLEN);
646 dmd = ggc_cleared_alloc<ctf_dmdef_t> ();
648 /* Buffer the strings in the CTF string table. */
649 dmd->dmd_name = ctf_add_string (ctfc, name, &(dmd->dmd_name_offset));
650 dmd->dmd_type = type;
651 dmd->dmd_value = -1;
653 if (kind == CTF_K_STRUCT && vlen != 0)
654 dmd->dmd_offset = bit_offset;
655 else
656 dmd->dmd_offset = 0;
658 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, root, vlen + 1);
659 ctf_dmd_list_append (&dtd->dtd_u.dtu_members, dmd);
661 if ((name != NULL) && strcmp (name, ""))
662 ctfc->ctfc_strlen += strlen (name) + 1;
664 return 0;
668 ctf_add_variable (ctf_container_ref ctfc, const char * name, ctf_id_t ref,
669 dw_die_ref die, unsigned int external_vis)
671 ctf_dvdef_ref dvd;
673 gcc_assert (name);
675 if (name != NULL)
677 dvd = ggc_cleared_alloc<ctf_dvdef_t> ();
678 dvd->dvd_key = die;
679 /* Buffer the strings in the CTF string table. */
680 dvd->dvd_name = ctf_add_string (ctfc, name, &(dvd->dvd_name_offset));
681 dvd->dvd_visibility = external_vis;
682 dvd->dvd_type = ref;
683 ctf_dvd_insert (ctfc, dvd);
685 if (strcmp (name, ""))
686 ctfc->ctfc_strlen += strlen (name) + 1;
689 return 0;
693 ctf_add_function_arg (ctf_container_ref ctfc, dw_die_ref func,
694 const char * name, ctf_id_t type)
696 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, func);
697 ctf_func_arg_t * farg;
698 uint32_t vlen;
700 /* The function to which argument is being added must already exist. */
701 gcc_assert (dtd);
702 /* The number of args must have been non-zero. */
703 vlen = CTF_V2_INFO_VLEN (dtd->dtd_data.ctti_info);
704 gcc_assert (vlen);
706 farg = ggc_cleared_alloc<ctf_func_arg_t> ();
708 /* Buffer the strings in the auxilliary string table. CTF V3 format does not
709 require function argument names. Use auxilliary string table to keep
710 these strings to avoid unnecessary bloat in CTF section in CTF V3. */
711 farg->farg_name = ctf_add_string (ctfc, name, &(farg->farg_name_offset),
712 CTF_AUX_STRTAB);
713 farg->farg_type = type;
715 ctf_farg_list_append (&dtd->dtd_u.dtu_argv, farg);
717 /* For aux_str, keep ctfc_aux_strlen updated for debugging. */
718 if ((name != NULL) && strcmp (name, ""))
719 ctfc->ctfc_aux_strlen += strlen (name) + 1;
721 return 0;
724 ctf_id_t
725 ctf_add_function (ctf_container_ref ctfc, uint32_t flag, const char * name,
726 const ctf_funcinfo_t * ctc, dw_die_ref die,
727 bool from_global_func)
729 ctf_dtdef_ref dtd;
730 ctf_id_t type;
731 uint32_t vlen;
733 gcc_assert (ctc);
735 vlen = ctc->ctc_argc;
736 gcc_assert (vlen <= CTF_MAX_VLEN);
738 type = ctf_add_generic (ctfc, flag, name, &dtd, die);
740 dtd->from_global_func = from_global_func;
741 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_FUNCTION, flag, vlen);
742 /* Caller must make sure CTF types for ctc->ctc_return are already added. */
743 dtd->dtd_data.ctti_type = (uint32_t) ctc->ctc_return;
744 /* Caller must make sure CTF types for function arguments are already added
745 via ctf_add_function_arg () API. */
747 ctfc->ctfc_num_stypes++;
749 return type;
752 ctf_id_t
753 ctf_add_sou (ctf_container_ref ctfc, uint32_t flag, const char * name,
754 uint32_t kind, size_t size, dw_die_ref die)
756 ctf_dtdef_ref dtd;
757 ctf_id_t type = 0;
759 gcc_assert ((kind == CTF_K_STRUCT) || (kind == CTF_K_UNION));
761 /* In the compiler, no need to handle the case of promoting forwards to
762 structs. This comment is simply to note a divergence from libctf. */
764 /* The compiler does, however, update any previously existing forward types
765 to non-root. CTF does not allow existence of two root types with the same
766 name. */
767 ctf_dtdef_ref sou_fwd_type = ctf_dtd_lookup (ctfc, die);
768 if (sou_fwd_type)
770 sou_fwd_type->dtd_data.ctti_info
771 = CTF_TYPE_INFO (CTF_K_FORWARD, CTF_ADD_NONROOT, 0);
774 type = ctf_add_generic (ctfc, flag, name, &dtd, die);
776 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, flag, 0);
778 if (size > CTF_MAX_SIZE)
780 dtd->dtd_data.ctti_size = CTF_LSIZE_SENT;
781 dtd->dtd_data.ctti_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
782 dtd->dtd_data.ctti_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
783 ctfc->ctfc_num_types++;
785 else
787 dtd->dtd_data.ctti_size = (uint32_t) size;
788 ctfc->ctfc_num_stypes++;
791 return type;
794 /* Given a TREE_TYPE node, return the CTF type ID for that type. */
796 ctf_id_t
797 ctf_lookup_tree_type (ctf_container_ref ctfc, const tree type)
799 dw_die_ref die = lookup_type_die (type);
800 if (die == NULL)
801 return CTF_NULL_TYPEID;
803 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
804 if (dtd == NULL)
805 return CTF_NULL_TYPEID;
807 return dtd->dtd_type;
810 /* Check if CTF for TYPE has already been generated. Mainstay for
811 de-duplication. If CTF type already exists, returns TRUE and updates
812 the TYPE_ID for the caller. */
814 bool
815 ctf_type_exists (ctf_container_ref ctfc, dw_die_ref type,
816 ctf_id_t * type_id)
818 bool exists = false;
819 ctf_dtdef_ref ctf_type_seen = ctf_dtd_lookup (ctfc, type);
821 if (ctf_type_seen)
823 exists = true;
824 /* CTF type for this type exists. */
825 *type_id = ctf_type_seen->dtd_type;
828 return exists;
831 /* Location information for CTF Types and CTF Variables. CTF section does not
832 emit location information; at this time, location information is needed for
833 BTF CO-RE use-cases. */
836 ctfc_get_dtd_srcloc (ctf_dtdef_ref dtd, ctf_srcloc_ref loc)
838 loc->ctsloc_file = ctf_get_die_loc_file (dtd->dtd_key);
839 loc->ctsloc_line = ctf_get_die_loc_line (dtd->dtd_key);
840 loc->ctsloc_col = ctf_get_die_loc_col (dtd->dtd_key);
842 if (loc->ctsloc_file == NULL)
843 return 1;
845 return 0;
849 ctfc_get_dvd_srcloc (ctf_dvdef_ref dvd, ctf_srcloc_ref loc)
851 loc->ctsloc_file = ctf_get_die_loc_file (dvd->dvd_key);
852 loc->ctsloc_line = ctf_get_die_loc_line (dvd->dvd_key);
853 loc->ctsloc_col = ctf_get_die_loc_col (dvd->dvd_key);
855 if (loc->ctsloc_file == NULL)
856 return 1;
858 return 0;
861 /* CTF container setup and teardown routines. */
863 /* Initialize the CTF string table.
864 The first entry in the CTF string table (empty string) is added. */
866 static void
867 init_ctf_strtable (ctf_strtable_t * strtab)
869 strtab->ctstab_head = NULL;
870 strtab->ctstab_tail = NULL;
871 strtab->ctstab_num = 0;
872 strtab->ctstab_len = 0;
874 /* The first entry in the CTF string table is an empty string. E.g., CTF
875 type records with no name (like CTF_K_CONST, CTF_K_VOLATILE etc) point to
876 this string. */
877 uint32_t estr_offset = 0;
878 strtab->ctstab_estr = ctfc_strtable_add_str (strtab, "", &estr_offset);
881 /* Initialize the string tables in the CTF container. */
883 static void
884 init_ctf_string_table (ctf_container_ref ctfc)
886 init_ctf_strtable (&ctfc->ctfc_strtable);
887 ctfc->ctfc_strlen++;
889 init_ctf_strtable (&ctfc->ctfc_aux_strtable);
890 ctfc->ctfc_aux_strlen++;
893 /* Allocate a new CTF container with the desired flags. */
895 static inline ctf_container_ref
896 new_ctf_container (void)
898 tu_ctfc = ggc_cleared_alloc<ctf_container_t> ();
899 tu_ctfc->ctfc_types
900 = hash_table<ctfc_dtd_hasher>::create_ggc (100);
901 tu_ctfc->ctfc_vars
902 = hash_table<ctfc_dvd_hasher>::create_ggc (100);
904 return tu_ctfc;
907 /* Initialize a CTF container per translation unit. */
909 static void
910 init_ctf_container (void)
912 tu_ctfc = new_ctf_container ();
914 tu_ctfc->ctfc_magic = CTF_MAGIC;
915 tu_ctfc->ctfc_version = CTF_VERSION;
916 tu_ctfc->ctfc_flags = CTF_F_NEWFUNCINFO;
917 tu_ctfc->ctfc_nextid = CTF_INIT_TYPEID;
919 init_ctf_string_table (tu_ctfc);
922 void
923 ctfc_delete_strtab (ctf_strtable_t * strtab)
925 ctf_string_t * str = NULL;
926 ctf_string_t * next_str = NULL;
928 str = strtab->ctstab_head;
929 next_str = str;
930 while (next_str != NULL)
932 next_str = str->cts_next;
933 ggc_free (str);
934 str = next_str;
937 strtab->ctstab_head = NULL;
938 strtab->ctstab_tail = NULL;
939 strtab->ctstab_estr = NULL;
942 /* Delete the CTF container's resources. */
944 void
945 ctfc_delete_container (ctf_container_ref ctfc)
947 /* FIXME - CTF container can be cleaned up now.
948 Will the ggc machinery take care of cleaning up the container structure
949 including the hash_map members etc. ? */
950 if (ctfc)
952 ctfc_delete_strtab (&ctfc->ctfc_strtable);
953 ctfc_delete_strtab (&ctfc->ctfc_aux_strtable);
954 if (ctfc->ctfc_vars_list)
956 ggc_free (ctfc->ctfc_vars_list);
957 ctfc->ctfc_vars_list = NULL;
959 if (ctfc->ctfc_types_list)
961 ggc_free (ctfc->ctfc_types_list);
962 ctfc->ctfc_types_list = NULL;
964 if (ctfc->ctfc_gfuncs_list)
966 ggc_free (ctfc->ctfc_gfuncs_list);
967 ctfc->ctfc_gfuncs_list = NULL;
969 if (ctfc->ctfc_gobjts_list)
971 ggc_free (ctfc->ctfc_gobjts_list);
972 ctfc->ctfc_gobjts_list = NULL;
975 ctfc= NULL;
979 /* CTF routines interfacing to the compiler. */
981 void
982 ctf_init (void)
984 init_ctf_container ();