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