]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ctfc.c
Correct a function pre/postcondition [PR102403].
[thirdparty/gcc.git] / gcc / ctfc.c
CommitLineData
b7e215a8
IB
1/* Generate CTF.
2 Copyright (C) 2019,2021 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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
30ctf_container_ref tu_ctfc;
31
32ctf_container_ref
33ctf_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. */
40bool
41ctfc_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
48unsigned int
49ctfc_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
56unsigned 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
64ctf_strtable_t *
65ctfc_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
72size_t
73ctfc_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
82size_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
90enum ctf_dtu_d_union_enum
91ctf_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
118static void
119ctf_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
44e4ed6a 135ctf_dtdef_ref
b7e215a8
IB
136ctf_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
151static void
152ctf_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
168ctf_dvdef_ref
169ctf_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/* Append member definition to the list. Member list is a singly-linked list
183 with list start pointing to the head. */
184
185static void
186ctf_dmd_list_append (ctf_dmdef_t ** dmd, ctf_dmdef_t * elem)
187{
188 ctf_dmdef_t * tail = (dmd && *dmd) ? *dmd : NULL;
189 if (tail)
190 {
191 while (tail->dmd_next)
192 tail = tail->dmd_next;
193
194 tail->dmd_next = elem;
195 }
196 else
197 *dmd = elem;
198
199 elem->dmd_next = NULL;
200}
201
202/* Append function argument to the list. Member list is a singly-linked list
203 with list start pointing to the head. */
204
205static void
206ctf_farg_list_append (ctf_func_arg_t ** farg, ctf_func_arg_t * elem)
207{
208 ctf_func_arg_t * tail = (farg && *farg) ? *farg : NULL;
209 if (tail)
210 {
211 while (tail->farg_next)
212 tail = tail->farg_next;
213
214 tail->farg_next = elem;
215 }
216 else
217 *farg = elem;
218
219 elem->farg_next = NULL;
220}
221
222/* Append str to the CTF string table. */
223
224static void
225ctfc_strtable_append_str (ctf_strtable_t * str_table, const char * str)
226{
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;
231
232 if (!str_table->ctstab_head)
233 str_table->ctstab_head = ctf_string;
234
235 /* Append to the end of the list. */
236 if (str_table->ctstab_tail)
237 str_table->ctstab_tail->cts_next = ctf_string;
238
239 str_table->ctstab_tail = ctf_string;
240}
241
242/* Wrapper function to add str to the CTF string table. No de-duplication of
243 CTF strings is done by the compiler. */
244
245static const char *
246ctfc_strtable_add_str (ctf_strtable_t * str_table, const char * name,
247 uint32_t * name_offset)
248{
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;
253
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)
257 {
258 ctf_string = CONST_CAST (char *, str_table->ctstab_estr);
259 str_offset = 0;
260 }
261 else
262 {
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));
267
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;
273 }
274
275 *name_offset = str_offset;
276
277 return (const char *) ctf_string;
278
279}
280
281/* Add string to the appropriate string table in the CTF container. */
282
283const char *
284ctf_add_string (ctf_container_ref ctfc, const char * name,
285 uint32_t * name_offset, int aux_str = CTF_STRTAB)
286{
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);
291}
292
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. */
296
297void
298ctf_add_cuname (ctf_container_ref ctfc, const char * filename)
299{
300 char * cuname = NULL;
301
302 /* (filename at this point of compilation cannot be null). */
303
304 if (!IS_DIR_SEPARATOR (filename[0]))
305 {
306 /* Filename is a relative path. */
307 const char * cu_pwd = get_src_pwd ();
308 const int cu_pwd_len = strlen (cu_pwd);
309
310 /* Add a DIR_SEPARATOR char before the filename. */
311 const int len = cu_pwd_len + 2 + strlen (filename);
312
313 cuname = (char *) ggc_alloc_atomic (len);
314 memset (cuname, 0, len);
315
316 strcpy (cuname, cu_pwd);
317 cuname[cu_pwd_len] = DIR_SEPARATOR;
318 cuname[cu_pwd_len+1] = 0;
319 strcat (cuname, filename);
320 }
321 else
322 /* Filename is an absolute path. */
323 cuname = CONST_CAST (char *, ggc_strdup (filename));
324
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;
329
330 /* Mark cuname for garbage collection. */
331 cuname = NULL;
332}
333
334/* Functions to create CTF types.
335
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. */
341
342static ctf_id_t
343ctf_add_generic (ctf_container_ref ctfc, uint32_t flag, const char * name,
344 ctf_dtdef_ref * rp, dw_die_ref die)
345{
346 ctf_dtdef_ref dtd;
347 ctf_id_t type;
348
349 gcc_assert (flag == CTF_ADD_NONROOT || flag == CTF_ADD_ROOT);
350
351 dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
352
353 type = ctfc->ctfc_nextid++;
354 gcc_assert (type < CTF_MAX_TYPE); /* CTF type ID overflow. */
355
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;
360
361 if ((name != NULL) && strcmp (name, ""))
362 ctfc->ctfc_strlen += strlen (name) + 1;
363
364 ctf_dtd_insert (ctfc, dtd);
365
366 *rp = dtd;
367 return type;
368}
369
370static ctf_id_t
371ctf_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)
373{
374 ctf_dtdef_ref dtd;
375 ctf_id_t type;
376
377 type = ctf_add_generic (ctfc, flag, name, &dtd, die);
378
379 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, flag, 0);
380
381 uint32_t roundup_nbytes = (ROUND_UP (ep->cte_bits, BITS_PER_UNIT)
382 / BITS_PER_UNIT);
383
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;
390
391 ctfc->ctfc_num_stypes++;
392
393 return type;
394}
395
396ctf_id_t
397ctf_add_reftype (ctf_container_ref ctfc, uint32_t flag, ctf_id_t ref,
398 uint32_t kind, dw_die_ref die)
399{
400 ctf_dtdef_ref dtd;
401 ctf_id_t type;
402
403 gcc_assert (ref <= CTF_MAX_TYPE);
404
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;
410
411 ctfc->ctfc_num_stypes++;
412
413 return type;
414}
415
416ctf_id_t
417ctf_add_forward (ctf_container_ref ctfc, uint32_t flag, const char * name,
418 uint32_t kind, dw_die_ref die)
419{
420 ctf_dtdef_ref dtd;
421 ctf_id_t type = 0;
422
423 type = ctf_add_generic (ctfc, flag, name, &dtd, die);
424
425 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_FORWARD, flag, 0);
426 dtd->dtd_data.ctti_type = kind;
427
428 ctfc->ctfc_num_stypes++;
429
430 return type;
431}
432
433ctf_id_t
434ctf_add_typedef (ctf_container_ref ctfc, uint32_t flag, const char * name,
435 ctf_id_t ref, dw_die_ref die)
436{
437 ctf_dtdef_ref dtd;
438 ctf_id_t type;
439
440 gcc_assert (ref <= CTF_MAX_TYPE);
441 /* Nameless Typedefs are not expected. */
442 gcc_assert ((name != NULL) && strcmp (name, ""));
443
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;
449
450 gcc_assert (dtd->dtd_type != dtd->dtd_data.ctti_type);
451
452 ctfc->ctfc_num_stypes++;
453
454 return type;
455}
456
457ctf_id_t
458ctf_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)
460{
461 ctf_dtdef_ref dtd;
462 ctf_id_t type;
463 uint32_t roundup_nbytes;
464
465 gcc_assert ((bit_size <= 255) && (bit_offset <= 255));
466
467 gcc_assert (ref <= CTF_MAX_TYPE);
468
469 type = ctf_add_generic (ctfc, flag, NULL, &dtd, die);
470
471 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0);
472
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;
480
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;
486
487 ctfc->ctfc_num_stypes++;
488
489 return type;
490}
491
492ctf_id_t
493ctf_add_float (ctf_container_ref ctfc, uint32_t flag,
494 const char * name, const ctf_encoding_t * ep, dw_die_ref die)
495{
496 return (ctf_add_encoded (ctfc, flag, name, ep, CTF_K_FLOAT, die));
497}
498
499ctf_id_t
500ctf_add_integer (ctf_container_ref ctfc, uint32_t flag,
501 const char * name, const ctf_encoding_t * ep, dw_die_ref die)
502{
503 return (ctf_add_encoded (ctfc, flag, name, ep, CTF_K_INTEGER, die));
504}
505
506ctf_id_t
507ctf_add_unknown (ctf_container_ref ctfc, uint32_t flag,
508 const char * name, const ctf_encoding_t * ep, dw_die_ref die)
509{
510 return (ctf_add_encoded (ctfc, flag, name, ep, CTF_K_UNKNOWN, die));
511}
512
513ctf_id_t
514ctf_add_pointer (ctf_container_ref ctfc, uint32_t flag, ctf_id_t ref,
515 dw_die_ref die)
516{
517 return (ctf_add_reftype (ctfc, flag, ref, CTF_K_POINTER, die));
518}
519
520ctf_id_t
521ctf_add_array (ctf_container_ref ctfc, uint32_t flag, const ctf_arinfo_t * arp,
522 dw_die_ref die)
523{
524 ctf_dtdef_ref dtd;
525 ctf_id_t type;
526
527 gcc_assert (arp);
528
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. */
532
533 type = ctf_add_generic (ctfc, flag, NULL, &dtd, die);
534
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;
538
539 ctfc->ctfc_num_stypes++;
540
541 return type;
542}
543
544ctf_id_t
545ctf_add_enum (ctf_container_ref ctfc, uint32_t flag, const char * name,
546 HOST_WIDE_INT size, dw_die_ref die)
547{
548 ctf_dtdef_ref dtd;
549 ctf_id_t type;
550
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. */
553
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)
559 {
560 enum_fwd_type->dtd_data.ctti_info
561 = CTF_TYPE_INFO (CTF_K_FORWARD, CTF_ADD_NONROOT, 0);
562 }
563
564 type = ctf_add_generic (ctfc, flag, name, &dtd, die);
565
566 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_ENUM, flag, 0);
567
568 /* Size in bytes should always fit, of course.
569 TBD WARN - warn instead? */
570 gcc_assert (size <= CTF_MAX_SIZE);
571
572 dtd->dtd_data.ctti_size = size;
573
574 ctfc->ctfc_num_stypes++;
575
576 return type;
577}
578
579int
580ctf_add_enumerator (ctf_container_ref ctfc, ctf_id_t enid, const char * name,
581 HOST_WIDE_INT value, dw_die_ref die)
582{
583 ctf_dmdef_t * dmd;
584 uint32_t kind, vlen, root;
585
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);
592
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);
596
597 gcc_assert (kind == CTF_K_ENUM && vlen < CTF_MAX_VLEN);
598
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))
603 {
604 /* FIXME - Note this TBD_CTF_REPRESENTATION_LIMIT. */
605 return (1);
606 }
607
608 dmd = ggc_cleared_alloc<ctf_dmdef_t> ();
609
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;
614
615 dmd->dmd_value = value;
616
617 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, root, vlen + 1);
618 ctf_dmd_list_append (&dtd->dtd_u.dtu_members, dmd);
619
620 if ((name != NULL) && strcmp (name, ""))
621 ctfc->ctfc_strlen += strlen (name) + 1;
622
623 return (0);
624}
625
626int
627ctf_add_member_offset (ctf_container_ref ctfc, dw_die_ref sou,
628 const char * name, ctf_id_t type,
629 uint64_t bit_offset)
630{
631 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, sou);
632 ctf_dmdef_t * dmd;
633
634 uint32_t kind, vlen, root;
635
636 /* The type of the member being added must already exist. */
637 gcc_assert (dtd);
638
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);
642
643 gcc_assert (kind == CTF_K_STRUCT || kind == CTF_K_UNION);
644 gcc_assert (vlen < CTF_MAX_VLEN);
645
646 dmd = ggc_cleared_alloc<ctf_dmdef_t> ();
647
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;
652
653 if (kind == CTF_K_STRUCT && vlen != 0)
654 dmd->dmd_offset = bit_offset;
655 else
656 dmd->dmd_offset = 0;
657
658 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, root, vlen + 1);
659 ctf_dmd_list_append (&dtd->dtd_u.dtu_members, dmd);
660
661 if ((name != NULL) && strcmp (name, ""))
662 ctfc->ctfc_strlen += strlen (name) + 1;
663
664 return 0;
665}
666
667int
668ctf_add_variable (ctf_container_ref ctfc, const char * name, ctf_id_t ref,
669 dw_die_ref die, unsigned int external_vis)
670{
671 ctf_dvdef_ref dvd;
672
673 gcc_assert (name);
674
675 if (name != NULL)
676 {
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);
684
685 if (strcmp (name, ""))
686 ctfc->ctfc_strlen += strlen (name) + 1;
687 }
688
689 return 0;
690}
691
692int
693ctf_add_function_arg (ctf_container_ref ctfc, dw_die_ref func,
694 const char * name, ctf_id_t type)
695{
696 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, func);
697 ctf_func_arg_t * farg;
698 uint32_t vlen;
699
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);
705
706 farg = ggc_cleared_alloc<ctf_func_arg_t> ();
707
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;
714
715 ctf_farg_list_append (&dtd->dtd_u.dtu_argv, farg);
716
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;
720
721 return 0;
722}
723
724ctf_id_t
725ctf_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)
728{
729 ctf_dtdef_ref dtd;
730 ctf_id_t type;
731 uint32_t vlen;
732
733 gcc_assert (ctc);
734
735 vlen = ctc->ctc_argc;
736 gcc_assert (vlen <= CTF_MAX_VLEN);
737
738 type = ctf_add_generic (ctfc, flag, name, &dtd, die);
739
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. */
746
747 ctfc->ctfc_num_stypes++;
748
749 return type;
750}
751
752ctf_id_t
753ctf_add_sou (ctf_container_ref ctfc, uint32_t flag, const char * name,
754 uint32_t kind, size_t size, dw_die_ref die)
755{
756 ctf_dtdef_ref dtd;
757 ctf_id_t type = 0;
758
759 gcc_assert ((kind == CTF_K_STRUCT) || (kind == CTF_K_UNION));
760
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. */
763
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)
769 {
770 sou_fwd_type->dtd_data.ctti_info
771 = CTF_TYPE_INFO (CTF_K_FORWARD, CTF_ADD_NONROOT, 0);
772 }
773
774 type = ctf_add_generic (ctfc, flag, name, &dtd, die);
775
776 dtd->dtd_data.ctti_info = CTF_TYPE_INFO (kind, flag, 0);
777
778 if (size > CTF_MAX_SIZE)
779 {
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++;
784 }
785 else
786 {
787 dtd->dtd_data.ctti_size = (uint32_t) size;
788 ctfc->ctfc_num_stypes++;
789 }
790
791 return type;
792}
793
5b723401
DF
794/* Given a TREE_TYPE node, return the CTF type ID for that type. */
795
796ctf_id_t
797ctf_lookup_tree_type (ctf_container_ref ctfc, const tree type)
798{
799 dw_die_ref die = lookup_type_die (type);
800 if (die == NULL)
801 return CTF_NULL_TYPEID;
802
803 ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
804 if (dtd == NULL)
805 return CTF_NULL_TYPEID;
806
807 return dtd->dtd_type;
808}
809
b7e215a8
IB
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. */
813
814bool
815ctf_type_exists (ctf_container_ref ctfc, dw_die_ref type,
816 ctf_id_t * type_id)
817{
818 bool exists = false;
819 ctf_dtdef_ref ctf_type_seen = ctf_dtd_lookup (ctfc, type);
820
821 if (ctf_type_seen)
822 {
823 exists = true;
824 /* CTF type for this type exists. */
825 *type_id = ctf_type_seen->dtd_type;
826 }
827
828 return exists;
829}
830
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. */
834
835int
836ctfc_get_dtd_srcloc (ctf_dtdef_ref dtd, ctf_srcloc_ref loc)
837{
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);
841
842 if (loc->ctsloc_file == NULL)
843 return 1;
844
845 return 0;
846}
847
848int
849ctfc_get_dvd_srcloc (ctf_dvdef_ref dvd, ctf_srcloc_ref loc)
850{
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);
854
855 if (loc->ctsloc_file == NULL)
856 return 1;
857
858 return 0;
859}
860
861/* CTF container setup and teardown routines. */
862
863/* Initialize the CTF string table.
864 The first entry in the CTF string table (empty string) is added. */
865
866static void
867init_ctf_strtable (ctf_strtable_t * strtab)
868{
869 strtab->ctstab_head = NULL;
870 strtab->ctstab_tail = NULL;
871 strtab->ctstab_num = 0;
872 strtab->ctstab_len = 0;
873
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);
879}
880
881/* Initialize the string tables in the CTF container. */
882
883static void
884init_ctf_string_table (ctf_container_ref ctfc)
885{
886 init_ctf_strtable (&ctfc->ctfc_strtable);
887 ctfc->ctfc_strlen++;
888
889 init_ctf_strtable (&ctfc->ctfc_aux_strtable);
890 ctfc->ctfc_aux_strlen++;
891}
892
893/* Allocate a new CTF container with the desired flags. */
894
895static inline ctf_container_ref
896new_ctf_container (void)
897{
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);
903
904 return tu_ctfc;
905}
906
907/* Initialize a CTF container per translation unit. */
908
909static void
910init_ctf_container (void)
911{
912 tu_ctfc = new_ctf_container ();
913
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;
918
919 init_ctf_string_table (tu_ctfc);
920}
921
922void
923ctfc_delete_strtab (ctf_strtable_t * strtab)
924{
925 ctf_string_t * str = NULL;
926 ctf_string_t * next_str = NULL;
927
928 str = strtab->ctstab_head;
929 next_str = str;
930 while (next_str != NULL)
931 {
932 next_str = str->cts_next;
933 ggc_free (str);
934 str = next_str;
935 }
936
937 strtab->ctstab_head = NULL;
938 strtab->ctstab_tail = NULL;
939 strtab->ctstab_estr = NULL;
940}
941
942/* Delete the CTF container's resources. */
943
944void
945ctfc_delete_container (ctf_container_ref ctfc)
946{
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)
951 {
952 ctfc_delete_strtab (&ctfc->ctfc_strtable);
953 ctfc_delete_strtab (&ctfc->ctfc_aux_strtable);
954 if (ctfc->ctfc_vars_list)
955 {
956 ggc_free (ctfc->ctfc_vars_list);
957 ctfc->ctfc_vars_list = NULL;
958 }
959 if (ctfc->ctfc_types_list)
960 {
961 ggc_free (ctfc->ctfc_types_list);
962 ctfc->ctfc_types_list = NULL;
963 }
964 if (ctfc->ctfc_gfuncs_list)
965 {
966 ggc_free (ctfc->ctfc_gfuncs_list);
967 ctfc->ctfc_gfuncs_list = NULL;
968 }
969 if (ctfc->ctfc_gobjts_list)
970 {
971 ggc_free (ctfc->ctfc_gobjts_list);
972 ctfc->ctfc_gobjts_list = NULL;
973 }
974
975 ctfc= NULL;
976 }
977}
978
979/* CTF routines interfacing to the compiler. */
980
981void
982ctf_init (void)
983{
984 init_ctf_container ();
985}