ctfout.cc revision 1.1 1 1.1 mrg /* Output CTF format from GCC.
2 1.1 mrg Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 1.1 mrg
4 1.1 mrg This file is part of GCC.
5 1.1 mrg
6 1.1 mrg GCC is free software; you can redistribute it and/or modify it under
7 1.1 mrg the terms of the GNU General Public License as published by the Free
8 1.1 mrg Software Foundation; either version 3, or (at your option) any later
9 1.1 mrg version.
10 1.1 mrg
11 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 1.1 mrg for more details.
15 1.1 mrg
16 1.1 mrg You should have received a copy of the GNU General Public License
17 1.1 mrg along with GCC; see the file COPYING3. If not see
18 1.1 mrg <http://www.gnu.org/licenses/>. */
19 1.1 mrg
20 1.1 mrg #include "config.h"
21 1.1 mrg #include "system.h"
22 1.1 mrg #include "coretypes.h"
23 1.1 mrg #include "target.h"
24 1.1 mrg #include "memmodel.h"
25 1.1 mrg #include "tm_p.h"
26 1.1 mrg #include "output.h"
27 1.1 mrg #include "dwarf2asm.h"
28 1.1 mrg #include "debug.h"
29 1.1 mrg #include "ctfc.h"
30 1.1 mrg #include "diagnostic-core.h"
31 1.1 mrg
32 1.1 mrg static int ctf_label_num;
33 1.1 mrg
34 1.1 mrg /* Pointers to various CTF sections. */
35 1.1 mrg
36 1.1 mrg static GTY (()) section * ctf_info_section;
37 1.1 mrg
38 1.1 mrg /* Section names used to hold CTF debugging information. */
39 1.1 mrg
40 1.1 mrg /* CTF debug info section. */
41 1.1 mrg
42 1.1 mrg #ifndef CTF_INFO_SECTION_NAME
43 1.1 mrg #define CTF_INFO_SECTION_NAME ".ctf"
44 1.1 mrg #endif
45 1.1 mrg
46 1.1 mrg /* Section flags for the CTF debug info section. */
47 1.1 mrg
48 1.1 mrg #define CTF_INFO_SECTION_FLAGS (SECTION_DEBUG)
49 1.1 mrg
50 1.1 mrg /* Maximum size (in bytes) of an artificially generated CTF label. */
51 1.1 mrg
52 1.1 mrg #define MAX_CTF_LABEL_BYTES 40
53 1.1 mrg
54 1.1 mrg static char ctf_info_section_label[MAX_CTF_LABEL_BYTES];
55 1.1 mrg
56 1.1 mrg #ifndef CTF_INFO_SECTION_LABEL
57 1.1 mrg #define CTF_INFO_SECTION_LABEL "Lctf"
58 1.1 mrg #endif
59 1.1 mrg
60 1.1 mrg /* CTF preprocess callback arguments. */
61 1.1 mrg
62 1.1 mrg typedef struct ctf_dtd_preprocess_arg
63 1.1 mrg {
64 1.1 mrg uint64_t dtd_global_func_idx;
65 1.1 mrg ctf_container_ref dtd_arg_ctfc;
66 1.1 mrg } ctf_dtd_preprocess_arg_t;
67 1.1 mrg
68 1.1 mrg typedef struct ctf_dvd_preprocess_arg
69 1.1 mrg {
70 1.1 mrg uint64_t dvd_global_obj_idx;
71 1.1 mrg ctf_container_ref dvd_arg_ctfc;
72 1.1 mrg } ctf_dvd_preprocess_arg_t;
73 1.1 mrg
74 1.1 mrg /* Compare two CTF variable definition entries. Currently used for sorting
75 1.1 mrg by name. */
76 1.1 mrg
77 1.1 mrg static int
78 1.1 mrg ctf_varent_compare (const void * entry1, const void * entry2)
79 1.1 mrg {
80 1.1 mrg int result;
81 1.1 mrg const ctf_dvdef_t * e1 = *(const ctf_dvdef_t * const*) entry1;
82 1.1 mrg const ctf_dvdef_t * e2 = *(const ctf_dvdef_t * const*) entry2;
83 1.1 mrg
84 1.1 mrg result = strcmp (e1->dvd_name, e2->dvd_name);
85 1.1 mrg
86 1.1 mrg return result;
87 1.1 mrg }
88 1.1 mrg
89 1.1 mrg /* A CTF type record may be followed by variable-length of bytes to encode the
90 1.1 mrg CTF type completely. This routine calculates the number of bytes, in the
91 1.1 mrg final binary CTF format, which are used to encode information about the type
92 1.1 mrg completely.
93 1.1 mrg
94 1.1 mrg This function must always be in sync with the CTF header. */
95 1.1 mrg
96 1.1 mrg static uint64_t
97 1.1 mrg ctf_calc_num_vbytes (ctf_dtdef_ref ctftype)
98 1.1 mrg {
99 1.1 mrg uint32_t size;
100 1.1 mrg uint64_t vlen_bytes = 0;
101 1.1 mrg
102 1.1 mrg uint32_t kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
103 1.1 mrg uint32_t vlen = CTF_V2_INFO_VLEN (ctftype->dtd_data.ctti_info);
104 1.1 mrg
105 1.1 mrg ctf_dmdef_t * dmd;
106 1.1 mrg ctf_func_arg_t * farg;
107 1.1 mrg uint32_t size_per_member = 0;
108 1.1 mrg unsigned int num_members = 0;
109 1.1 mrg unsigned int num_fargs = 0;
110 1.1 mrg
111 1.1 mrg switch (kind)
112 1.1 mrg {
113 1.1 mrg case CTF_K_FORWARD:
114 1.1 mrg case CTF_K_UNKNOWN:
115 1.1 mrg case CTF_K_POINTER:
116 1.1 mrg case CTF_K_TYPEDEF:
117 1.1 mrg case CTF_K_VOLATILE:
118 1.1 mrg case CTF_K_CONST:
119 1.1 mrg case CTF_K_RESTRICT:
120 1.1 mrg /* These types have no vlen data. */
121 1.1 mrg break;
122 1.1 mrg
123 1.1 mrg case CTF_K_INTEGER:
124 1.1 mrg case CTF_K_FLOAT:
125 1.1 mrg /* 4 bytes to represent encoding CTF_INT_DATA, CTF_FP_DATA. */
126 1.1 mrg vlen_bytes += sizeof (uint32_t);
127 1.1 mrg break;
128 1.1 mrg case CTF_K_FUNCTION:
129 1.1 mrg /* Sanity check - number of function args must be the same as
130 1.1 mrg vlen. */
131 1.1 mrg for (farg = ctftype->dtd_u.dtu_argv;
132 1.1 mrg farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
133 1.1 mrg num_fargs++;
134 1.1 mrg gcc_assert (vlen == num_fargs);
135 1.1 mrg
136 1.1 mrg /* FIXME - CTF_PADDING_FOR_ALIGNMENT. */
137 1.1 mrg vlen_bytes += (vlen + (vlen & 1)) * sizeof (uint32_t);
138 1.1 mrg break;
139 1.1 mrg case CTF_K_ARRAY:
140 1.1 mrg /* This has a single ctf_array_t. */
141 1.1 mrg vlen_bytes += sizeof (ctf_array_t);
142 1.1 mrg break;
143 1.1 mrg case CTF_K_SLICE:
144 1.1 mrg vlen_bytes += sizeof (ctf_slice_t);
145 1.1 mrg break;
146 1.1 mrg case CTF_K_STRUCT:
147 1.1 mrg case CTF_K_UNION:
148 1.1 mrg /* Count the number and type of members. */
149 1.1 mrg size = ctftype->dtd_data.ctti_size;
150 1.1 mrg size_per_member = size >= CTF_LSTRUCT_THRESH
151 1.1 mrg ? sizeof (ctf_lmember_t) : sizeof (ctf_member_t);
152 1.1 mrg
153 1.1 mrg /* Sanity check - number of members of struct must be the same as
154 1.1 mrg vlen. */
155 1.1 mrg for (dmd = ctftype->dtd_u.dtu_members;
156 1.1 mrg dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
157 1.1 mrg num_members++;
158 1.1 mrg gcc_assert (vlen == num_members);
159 1.1 mrg
160 1.1 mrg vlen_bytes += (num_members * size_per_member);
161 1.1 mrg break;
162 1.1 mrg case CTF_K_ENUM:
163 1.1 mrg vlen_bytes += vlen * sizeof (ctf_enum_t);
164 1.1 mrg break;
165 1.1 mrg default :
166 1.1 mrg break;
167 1.1 mrg }
168 1.1 mrg return vlen_bytes;
169 1.1 mrg }
170 1.1 mrg
171 1.1 mrg /* Add a CTF variable to the end of the list. */
172 1.1 mrg
173 1.1 mrg static void
174 1.1 mrg ctf_list_add_ctf_vars (ctf_container_ref ctfc, ctf_dvdef_ref var)
175 1.1 mrg {
176 1.1 mrg ctfc->ctfc_vars_list[ctfc->ctfc_vars_list_count++] = var;
177 1.1 mrg }
178 1.1 mrg
179 1.1 mrg /* Initialize the various sections and labels for CTF output. */
180 1.1 mrg
181 1.1 mrg void
182 1.1 mrg init_ctf_sections (void)
183 1.1 mrg {
184 1.1 mrg /* Note : Even in case of LTO, the compiler continues to generate a single
185 1.1 mrg CTF section for each compilation unit "early". Unlike other debug
186 1.1 mrg sections, CTF sections are non-LTO sections, and do not take the
187 1.1 mrg .gnu.debuglto_ prefix. The linker will de-duplicate the types in the CTF
188 1.1 mrg sections, in case of LTO or otherwise. */
189 1.1 mrg ctf_info_section = get_section (CTF_INFO_SECTION_NAME, CTF_INFO_SECTION_FLAGS,
190 1.1 mrg NULL);
191 1.1 mrg
192 1.1 mrg ASM_GENERATE_INTERNAL_LABEL (ctf_info_section_label,
193 1.1 mrg CTF_INFO_SECTION_LABEL, ctf_label_num++);
194 1.1 mrg }
195 1.1 mrg
196 1.1 mrg /* Routines for CTF pre-processing. */
197 1.1 mrg
198 1.1 mrg static void
199 1.1 mrg ctf_preprocess_var (ctf_container_ref ctfc, ctf_dvdef_ref var)
200 1.1 mrg {
201 1.1 mrg /* Add it to the list of types. This array of types will be sorted before
202 1.1 mrg assembling into output. */
203 1.1 mrg ctf_list_add_ctf_vars (ctfc, var);
204 1.1 mrg }
205 1.1 mrg
206 1.1 mrg /* CTF preprocess callback routine for CTF variables. */
207 1.1 mrg
208 1.1 mrg int
209 1.1 mrg ctf_dvd_preprocess_cb (ctf_dvdef_ref * slot, void * arg)
210 1.1 mrg {
211 1.1 mrg ctf_dvd_preprocess_arg_t * dvd_arg = (ctf_dvd_preprocess_arg_t *)arg;
212 1.1 mrg ctf_dvdef_ref var = (ctf_dvdef_ref) *slot;
213 1.1 mrg ctf_container_ref arg_ctfc = dvd_arg->dvd_arg_ctfc;
214 1.1 mrg
215 1.1 mrg /* If the CTF variable corresponds to an extern variable declaration with
216 1.1 mrg a defining declaration later on, skip it. Only CTF variable
217 1.1 mrg corresponding to the defining declaration for the extern variable is
218 1.1 mrg desirable. */
219 1.1 mrg if (ctf_dvd_ignore_lookup (arg_ctfc, var->dvd_key))
220 1.1 mrg return 1;
221 1.1 mrg
222 1.1 mrg ctf_preprocess_var (arg_ctfc, var);
223 1.1 mrg
224 1.1 mrg /* Keep track of global objts. */
225 1.1 mrg arg_ctfc->ctfc_gobjts_list[dvd_arg->dvd_global_obj_idx] = var;
226 1.1 mrg dvd_arg->dvd_global_obj_idx++;
227 1.1 mrg
228 1.1 mrg return 1;
229 1.1 mrg }
230 1.1 mrg
231 1.1 mrg /* CTF preprocess callback routine for CTF types. */
232 1.1 mrg
233 1.1 mrg int
234 1.1 mrg ctf_dtd_preprocess_cb (ctf_dtdef_ref * slot, void * arg)
235 1.1 mrg {
236 1.1 mrg uint32_t kind;
237 1.1 mrg
238 1.1 mrg ctf_dtdef_ref ctftype = (ctf_dtdef_ref) *slot;
239 1.1 mrg ctf_dtd_preprocess_arg_t * dtd_arg = (ctf_dtd_preprocess_arg_t *)arg;
240 1.1 mrg ctf_container_ref arg_ctfc = dtd_arg->dtd_arg_ctfc;
241 1.1 mrg
242 1.1 mrg size_t index = ctftype->dtd_type;
243 1.1 mrg gcc_assert (index <= arg_ctfc->ctfc_types->elements ());
244 1.1 mrg
245 1.1 mrg /* CTF types need to be output in the order of their type IDs. In other
246 1.1 mrg words, if type A is used to define type B, type ID of type A must
247 1.1 mrg appear before type ID of type B. */
248 1.1 mrg arg_ctfc->ctfc_types_list[index] = ctftype;
249 1.1 mrg
250 1.1 mrg /* Keep track of the CTF type if it's a function type and the type
251 1.1 mrg was generated from a function object. */
252 1.1 mrg kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
253 1.1 mrg if (kind == CTF_K_FUNCTION && ctftype->from_global_func)
254 1.1 mrg {
255 1.1 mrg arg_ctfc->ctfc_gfuncs_list[dtd_arg->dtd_global_func_idx] = ctftype;
256 1.1 mrg dtd_arg->dtd_global_func_idx++;
257 1.1 mrg }
258 1.1 mrg
259 1.1 mrg /* Calculate the vlen bytes. */
260 1.1 mrg arg_ctfc->ctfc_num_vlen_bytes += ctf_calc_num_vbytes (ctftype);
261 1.1 mrg
262 1.1 mrg return 1;
263 1.1 mrg }
264 1.1 mrg
265 1.1 mrg /* CTF preprocessing.
266 1.1 mrg After the CTF types for the compilation unit have been generated fully, the
267 1.1 mrg compiler writes out the asm for the CTF types.
268 1.1 mrg
269 1.1 mrg CTF writeout in the compiler requires two passes over the CTF types. In the
270 1.1 mrg first pass, the CTF preprocess pass:
271 1.1 mrg 1. CTF types are sorted in the order of their type IDs.
272 1.1 mrg 2. The variable number of bytes after each CTF type record are calculated.
273 1.1 mrg This is used to calculate the offsets in the ctf_header_t.
274 1.1 mrg 3. If the CTF type is of CTF_K_FUNCTION, the number of bytes in the
275 1.1 mrg funcinfo sub-section are calculated. This is used to calculate the
276 1.1 mrg offsets in the ctf_header_t.
277 1.1 mrg 4. Keep the list of CTF variables in ASCIIbetical order of their names.
278 1.1 mrg
279 1.1 mrg In the second pass, the CTF writeout pass, asm tags are written out using
280 1.1 mrg the compiler's afore-generated internal pre-processed CTF types. */
281 1.1 mrg
282 1.1 mrg static void
283 1.1 mrg ctf_preprocess (ctf_container_ref ctfc)
284 1.1 mrg {
285 1.1 mrg size_t num_ctf_types = ctfc->ctfc_types->elements ();
286 1.1 mrg size_t num_ctf_vars = ctfc_get_num_ctf_vars (ctfc);
287 1.1 mrg
288 1.1 mrg /* Initialize an array to keep track of the CTF variables at global
289 1.1 mrg scope. At this time, size it conservatively. */
290 1.1 mrg size_t num_global_objts = num_ctf_vars;
291 1.1 mrg if (num_global_objts)
292 1.1 mrg {
293 1.1 mrg ctfc->ctfc_gobjts_list = ggc_vec_alloc<ctf_dvdef_t*>(num_global_objts);
294 1.1 mrg }
295 1.1 mrg
296 1.1 mrg if (num_ctf_vars)
297 1.1 mrg {
298 1.1 mrg ctf_dvd_preprocess_arg_t dvd_arg;
299 1.1 mrg dvd_arg.dvd_global_obj_idx = 0;
300 1.1 mrg dvd_arg.dvd_arg_ctfc = ctfc;
301 1.1 mrg
302 1.1 mrg /* Allocate CTF var list. */
303 1.1 mrg ctfc->ctfc_vars_list = ggc_vec_alloc<ctf_dvdef_ref>(num_ctf_vars);
304 1.1 mrg /* Variables appear in the sort ASCIIbetical order of their names. This
305 1.1 mrg permits binary searching in the CTF reader. Add the variables to a
306 1.1 mrg list for sorting. */
307 1.1 mrg ctfc->ctfc_vars->traverse<void *, ctf_dvd_preprocess_cb> (&dvd_arg);
308 1.1 mrg /* Sort the list. */
309 1.1 mrg qsort (ctfc->ctfc_vars_list, ctfc->ctfc_vars_list_count,
310 1.1 mrg sizeof (ctf_dvdef_ref), ctf_varent_compare);
311 1.1 mrg /* Update the actual number of the generated CTF variables at global
312 1.1 mrg scope. */
313 1.1 mrg ctfc->ctfc_num_global_objts = dvd_arg.dvd_global_obj_idx;
314 1.1 mrg }
315 1.1 mrg
316 1.1 mrg /* Initialize an array to keep track of the CTF functions types for global
317 1.1 mrg functions in the CTF data section. */
318 1.1 mrg size_t num_global_funcs = ctfc->ctfc_num_global_funcs;
319 1.1 mrg if (num_global_funcs)
320 1.1 mrg {
321 1.1 mrg ctfc->ctfc_gfuncs_list = ggc_vec_alloc<ctf_dtdef_t*>(num_global_funcs);
322 1.1 mrg gcc_assert (num_ctf_types);
323 1.1 mrg }
324 1.1 mrg
325 1.1 mrg if (num_ctf_types)
326 1.1 mrg {
327 1.1 mrg ctf_dtd_preprocess_arg_t dtd_arg;
328 1.1 mrg dtd_arg.dtd_global_func_idx = 0;
329 1.1 mrg dtd_arg.dtd_arg_ctfc = ctfc;
330 1.1 mrg /* Allocate the CTF types list. Add 1 because type ID 0 is never a valid
331 1.1 mrg CTF type ID. No CTF type record should appear at that offset, this
332 1.1 mrg eases debugging and readability. */
333 1.1 mrg ctfc->ctfc_types_list = ggc_vec_alloc<ctf_dtdef_ref>(num_ctf_types + 1);
334 1.1 mrg /* Pre-process CTF types. */
335 1.1 mrg ctfc->ctfc_types->traverse<void *, ctf_dtd_preprocess_cb> (&dtd_arg);
336 1.1 mrg
337 1.1 mrg gcc_assert (dtd_arg.dtd_global_func_idx == num_global_funcs);
338 1.1 mrg }
339 1.1 mrg }
340 1.1 mrg
341 1.1 mrg /* CTF asm helper routines. */
342 1.1 mrg
343 1.1 mrg /* Asm'out the CTF preamble. */
344 1.1 mrg
345 1.1 mrg static void
346 1.1 mrg ctf_asm_preamble (ctf_container_ref ctfc)
347 1.1 mrg {
348 1.1 mrg dw2_asm_output_data (2, ctfc->ctfc_magic,
349 1.1 mrg "CTF preamble magic number");
350 1.1 mrg dw2_asm_output_data (1, ctfc->ctfc_version, "CTF preamble version");
351 1.1 mrg dw2_asm_output_data (1, ctfc->ctfc_flags, "CTF preamble flags");
352 1.1 mrg }
353 1.1 mrg
354 1.1 mrg /* Asm'out a CTF type which is represented by ctf_stype_t. */
355 1.1 mrg
356 1.1 mrg static void
357 1.1 mrg ctf_asm_stype (ctf_dtdef_ref type)
358 1.1 mrg {
359 1.1 mrg dw2_asm_output_data (4, type->dtd_data.ctti_name, "ctt_name");
360 1.1 mrg dw2_asm_output_data (4, type->dtd_data.ctti_info, "ctt_info");
361 1.1 mrg /* union. */
362 1.1 mrg dw2_asm_output_data (4, type->dtd_data.ctti_size, "ctt_size or ctt_type");
363 1.1 mrg }
364 1.1 mrg
365 1.1 mrg /* Asm'out a CTF type which is represented by ctf_type_t. */
366 1.1 mrg
367 1.1 mrg static void
368 1.1 mrg ctf_asm_type (ctf_dtdef_ref type)
369 1.1 mrg {
370 1.1 mrg dw2_asm_output_data (4, type->dtd_data.ctti_name, "ctt_name");
371 1.1 mrg dw2_asm_output_data (4, type->dtd_data.ctti_info, "ctt_info");
372 1.1 mrg /* union. */
373 1.1 mrg dw2_asm_output_data (4, type->dtd_data.ctti_size, "ctt_size");
374 1.1 mrg dw2_asm_output_data (4, type->dtd_data.ctti_lsizehi, "ctt_lsizehi");
375 1.1 mrg dw2_asm_output_data (4, type->dtd_data.ctti_lsizelo, "ctt_lsizelo");
376 1.1 mrg }
377 1.1 mrg
378 1.1 mrg /* Asm'out a CTF type of kind CTF_K_SLICE. */
379 1.1 mrg
380 1.1 mrg static void
381 1.1 mrg ctf_asm_slice (ctf_dtdef_ref type)
382 1.1 mrg {
383 1.1 mrg dw2_asm_output_data (4, type->dtd_u.dtu_slice.cts_type, "cts_type");
384 1.1 mrg dw2_asm_output_data (2, type->dtd_u.dtu_slice.cts_offset, "cts_offset");
385 1.1 mrg dw2_asm_output_data (2, type->dtd_u.dtu_slice.cts_bits, "cts_bits");
386 1.1 mrg }
387 1.1 mrg
388 1.1 mrg /* Asm'out a CTF type of kind CTF_K_ARRAY. */
389 1.1 mrg
390 1.1 mrg static void
391 1.1 mrg ctf_asm_array (ctf_dtdef_ref dtd)
392 1.1 mrg {
393 1.1 mrg dw2_asm_output_data (4, dtd->dtd_u.dtu_arr.ctr_contents, "cta_contents");
394 1.1 mrg dw2_asm_output_data (4, dtd->dtd_u.dtu_arr.ctr_index, "cta_index");
395 1.1 mrg dw2_asm_output_data (4, dtd->dtd_u.dtu_arr.ctr_nelems, "cta_nelems");
396 1.1 mrg }
397 1.1 mrg
398 1.1 mrg /* Asm'out a CTF variable. */
399 1.1 mrg
400 1.1 mrg static void
401 1.1 mrg ctf_asm_varent (ctf_dvdef_ref var)
402 1.1 mrg {
403 1.1 mrg /* Output the reference to the name in the string table. */
404 1.1 mrg dw2_asm_output_data (4, var->dvd_name_offset, "ctv_name");
405 1.1 mrg /* Output the type index. */
406 1.1 mrg dw2_asm_output_data (4, var->dvd_type, "ctv_typeidx");
407 1.1 mrg }
408 1.1 mrg
409 1.1 mrg /* Asm'out a member of CTF struct or union, represented by ctf_lmember_t. */
410 1.1 mrg
411 1.1 mrg static void
412 1.1 mrg ctf_asm_sou_lmember (ctf_dmdef_t * dmd)
413 1.1 mrg {
414 1.1 mrg dw2_asm_output_data (4, dmd->dmd_name_offset, "ctlm_name");
415 1.1 mrg dw2_asm_output_data (4, CTF_OFFSET_TO_LMEMHI (dmd->dmd_offset),
416 1.1 mrg "ctlm_offsethi");
417 1.1 mrg dw2_asm_output_data (4, dmd->dmd_type, "ctlm_type");
418 1.1 mrg dw2_asm_output_data (4, CTF_OFFSET_TO_LMEMLO (dmd->dmd_offset),
419 1.1 mrg "ctlm_offsetlo");
420 1.1 mrg }
421 1.1 mrg
422 1.1 mrg /* Asm'out a member of a CTF sruct or union, represented by ctf_member_t. */
423 1.1 mrg
424 1.1 mrg static void
425 1.1 mrg ctf_asm_sou_member (ctf_dmdef_t * dmd)
426 1.1 mrg {
427 1.1 mrg dw2_asm_output_data (4, dmd->dmd_name_offset, "ctm_name");
428 1.1 mrg dw2_asm_output_data (4, dmd->dmd_offset, "ctm_offset");
429 1.1 mrg dw2_asm_output_data (4, dmd->dmd_type, "ctm_type");
430 1.1 mrg }
431 1.1 mrg
432 1.1 mrg /* Asm'out an enumerator constant. */
433 1.1 mrg
434 1.1 mrg static void
435 1.1 mrg ctf_asm_enum_const (ctf_dmdef_t * dmd)
436 1.1 mrg {
437 1.1 mrg dw2_asm_output_data (4, dmd->dmd_name_offset, "cte_name");
438 1.1 mrg dw2_asm_output_data (4, dmd->dmd_value, "cte_value");
439 1.1 mrg }
440 1.1 mrg
441 1.1 mrg /* Asm'out a function argument. */
442 1.1 mrg
443 1.1 mrg static void
444 1.1 mrg ctf_asm_func_arg (ctf_func_arg_t * farg)
445 1.1 mrg {
446 1.1 mrg dw2_asm_output_data (4, farg->farg_type, "dtu_argv");
447 1.1 mrg }
448 1.1 mrg
449 1.1 mrg /* CTF writeout to asm file. */
450 1.1 mrg
451 1.1 mrg static void
452 1.1 mrg output_ctf_header (ctf_container_ref ctfc)
453 1.1 mrg {
454 1.1 mrg switch_to_section (ctf_info_section);
455 1.1 mrg ASM_OUTPUT_LABEL (asm_out_file, ctf_info_section_label);
456 1.1 mrg
457 1.1 mrg ctf_asm_preamble (ctfc);
458 1.1 mrg
459 1.1 mrg /* For a single compilation unit, the parent container's name and label are
460 1.1 mrg NULL. */
461 1.1 mrg dw2_asm_output_data (4, 0, "cth_parlabel");
462 1.1 mrg dw2_asm_output_data (4, 0, "cth_parname");
463 1.1 mrg dw2_asm_output_data (4, ctfc->ctfc_cuname_offset, "cth_cuname");
464 1.1 mrg
465 1.1 mrg int typeslen = 0;
466 1.1 mrg /* Initialize the offsets. The offsets are from after the CTF header. */
467 1.1 mrg uint32_t lbloff = 0;
468 1.1 mrg uint32_t objtoff = 0;
469 1.1 mrg uint32_t funcoff = 0;
470 1.1 mrg uint32_t objtidxoff = 0;
471 1.1 mrg uint32_t funcidxoff = 0;
472 1.1 mrg uint32_t varoff = 0;
473 1.1 mrg uint32_t typeoff = 0;
474 1.1 mrg uint32_t stroff = 0;
475 1.1 mrg
476 1.1 mrg if (!ctfc_is_empty_container (ctfc))
477 1.1 mrg {
478 1.1 mrg gcc_assert (ctfc_get_num_ctf_types (ctfc)
479 1.1 mrg == (ctfc->ctfc_num_types + ctfc->ctfc_num_stypes));
480 1.1 mrg
481 1.1 mrg funcoff = objtoff + ctfc->ctfc_num_global_objts * sizeof (uint32_t);
482 1.1 mrg /* Object index appears after function info. */
483 1.1 mrg objtidxoff = funcoff + ctfc->ctfc_num_global_funcs * sizeof (uint32_t);
484 1.1 mrg /* Funxtion index goes next. */
485 1.1 mrg funcidxoff = objtidxoff + ctfc->ctfc_num_global_objts * sizeof (uint32_t);
486 1.1 mrg /* Vars appear after function index. */
487 1.1 mrg varoff = funcidxoff + ctfc->ctfc_num_global_funcs * sizeof (uint32_t);
488 1.1 mrg /* CTF types appear after vars. */
489 1.1 mrg typeoff = varoff + (ctfc->ctfc_vars_list_count) * sizeof (ctf_varent_t);
490 1.1 mrg /* The total number of bytes for CTF types is the sum of the number of
491 1.1 mrg times struct ctf_type_t, struct ctf_stype_t are written, plus the
492 1.1 mrg amount of variable length data after each one of these. */
493 1.1 mrg typeslen = ctfc->ctfc_num_types * sizeof (ctf_type_t)
494 1.1 mrg + ctfc->ctfc_num_stypes * (sizeof (ctf_stype_t))
495 1.1 mrg + ctfc_get_num_vlen_bytes (ctfc);
496 1.1 mrg
497 1.1 mrg /* Strings appear after types. */
498 1.1 mrg stroff = typeoff + typeslen;
499 1.1 mrg }
500 1.1 mrg
501 1.1 mrg /* Offset of label section. */
502 1.1 mrg dw2_asm_output_data (4, lbloff, "cth_lbloff");
503 1.1 mrg /* Offset of object section. */
504 1.1 mrg dw2_asm_output_data (4, objtoff, "cth_objtoff");
505 1.1 mrg /* Offset of function section. */
506 1.1 mrg dw2_asm_output_data (4, funcoff, "cth_funcoff");
507 1.1 mrg /* Offset of object index section. */
508 1.1 mrg dw2_asm_output_data (4, objtidxoff, "cth_objtidxoff");
509 1.1 mrg /* Offset of function index section. */
510 1.1 mrg dw2_asm_output_data (4, funcidxoff, "cth_funcidxoff");
511 1.1 mrg
512 1.1 mrg /* Offset of variable section. */
513 1.1 mrg dw2_asm_output_data (4, varoff, "cth_varoff");
514 1.1 mrg /* Offset of type section. */
515 1.1 mrg dw2_asm_output_data (4, typeoff, "cth_typeoff");
516 1.1 mrg /* Offset of string section. */
517 1.1 mrg dw2_asm_output_data (4, stroff, "cth_stroff");
518 1.1 mrg /* Length of string section in bytes. */
519 1.1 mrg dw2_asm_output_data (4, ctfc->ctfc_strlen, "cth_strlen");
520 1.1 mrg }
521 1.1 mrg
522 1.1 mrg /* Output the CTF object info section. */
523 1.1 mrg
524 1.1 mrg static void
525 1.1 mrg output_ctf_obj_info (ctf_container_ref ctfc)
526 1.1 mrg {
527 1.1 mrg uint64_t i;
528 1.1 mrg ctf_dvdef_ref var;
529 1.1 mrg
530 1.1 mrg if (!ctfc->ctfc_num_global_objts) return;
531 1.1 mrg
532 1.1 mrg /* Compiler spits out the objts (at global scope) in the CTF obj info section.
533 1.1 mrg In no specific order. In an object file, the CTF object index section is
534 1.1 mrg used to associate the objts to their corresponding names. */
535 1.1 mrg for (i = 0; i < ctfc->ctfc_num_global_objts; i++)
536 1.1 mrg {
537 1.1 mrg var = ctfc->ctfc_gobjts_list[i];
538 1.1 mrg
539 1.1 mrg /* CTF type ID corresponding to the type of the variable. */
540 1.1 mrg dw2_asm_output_data (4, var->dvd_type, "objtinfo_var_type");
541 1.1 mrg }
542 1.1 mrg
543 1.1 mrg }
544 1.1 mrg
545 1.1 mrg /* Output the CTF function info section. */
546 1.1 mrg
547 1.1 mrg static void
548 1.1 mrg output_ctf_func_info (ctf_container_ref ctfc)
549 1.1 mrg {
550 1.1 mrg uint64_t i;
551 1.1 mrg ctf_dtdef_ref ctftype;
552 1.1 mrg
553 1.1 mrg if (!ctfc->ctfc_num_global_funcs) return;
554 1.1 mrg
555 1.1 mrg /* The CTF funcinfo section is simply an array of CTF_K_FUNCTION type IDs in
556 1.1 mrg the type section. In an object file, the CTF function index section is
557 1.1 mrg used to associate functions to their corresponding names. */
558 1.1 mrg for (i = 0; i < ctfc->ctfc_num_global_funcs; i++)
559 1.1 mrg {
560 1.1 mrg ctftype = ctfc->ctfc_gfuncs_list[i];
561 1.1 mrg dw2_asm_output_data (4, ctftype->dtd_type, "funcinfo_func_type");
562 1.1 mrg }
563 1.1 mrg }
564 1.1 mrg
565 1.1 mrg /* Output the CTF object index section. */
566 1.1 mrg
567 1.1 mrg static void
568 1.1 mrg output_ctf_objtidx (ctf_container_ref ctfc)
569 1.1 mrg {
570 1.1 mrg uint64_t i;
571 1.1 mrg ctf_dvdef_ref var;
572 1.1 mrg
573 1.1 mrg if (!ctfc->ctfc_num_global_objts) return;
574 1.1 mrg
575 1.1 mrg for (i = 0; i < ctfc->ctfc_num_global_objts; i++)
576 1.1 mrg {
577 1.1 mrg var = ctfc->ctfc_gobjts_list[i];
578 1.1 mrg /* Offset to the name in CTF string table. */
579 1.1 mrg dw2_asm_output_data (4, var->dvd_name_offset, "objtinfo_name");
580 1.1 mrg }
581 1.1 mrg }
582 1.1 mrg
583 1.1 mrg /* Output the CTF function index section. */
584 1.1 mrg
585 1.1 mrg static void
586 1.1 mrg output_ctf_funcidx (ctf_container_ref ctfc)
587 1.1 mrg {
588 1.1 mrg uint64_t i;
589 1.1 mrg ctf_dtdef_ref ctftype;
590 1.1 mrg
591 1.1 mrg if (!ctfc->ctfc_num_global_funcs) return;
592 1.1 mrg
593 1.1 mrg for (i = 0; i < ctfc->ctfc_num_global_funcs; i++)
594 1.1 mrg {
595 1.1 mrg ctftype = ctfc->ctfc_gfuncs_list[i];
596 1.1 mrg /* Offset to the name in CTF string table. */
597 1.1 mrg dw2_asm_output_data (4, ctftype->dtd_data.ctti_name, "funcinfo_name");
598 1.1 mrg }
599 1.1 mrg }
600 1.1 mrg
601 1.1 mrg /* Output the CTF variables. Variables appear in the sorted ASCIIbetical
602 1.1 mrg order of their names. This permits binary searching in the CTF reader. */
603 1.1 mrg
604 1.1 mrg static void
605 1.1 mrg output_ctf_vars (ctf_container_ref ctfc)
606 1.1 mrg {
607 1.1 mrg size_t i;
608 1.1 mrg unsigned int num_ctf_vars = ctfc->ctfc_vars_list_count;
609 1.1 mrg if (num_ctf_vars)
610 1.1 mrg {
611 1.1 mrg /* Iterate over the list of sorted vars and output the asm. */
612 1.1 mrg for (i = 0; i < num_ctf_vars; i++)
613 1.1 mrg {
614 1.1 mrg ctf_asm_varent (ctfc->ctfc_vars_list[i]);
615 1.1 mrg /* The type of variable must be a valid one. */
616 1.1 mrg gcc_assert (ctfc->ctfc_vars_list[i]->dvd_type != CTF_NULL_TYPEID);
617 1.1 mrg }
618 1.1 mrg }
619 1.1 mrg }
620 1.1 mrg
621 1.1 mrg /* Output the CTF string records. */
622 1.1 mrg
623 1.1 mrg static void
624 1.1 mrg output_ctf_strs (ctf_container_ref ctfc)
625 1.1 mrg {
626 1.1 mrg ctf_string_t * ctf_string = ctfc->ctfc_strtable.ctstab_head;
627 1.1 mrg
628 1.1 mrg while (ctf_string)
629 1.1 mrg {
630 1.1 mrg dw2_asm_output_nstring (ctf_string->cts_str, -1, "ctf_string");
631 1.1 mrg ctf_string = ctf_string->cts_next;
632 1.1 mrg }
633 1.1 mrg }
634 1.1 mrg
635 1.1 mrg /* Output the members of the CTF struct or union. */
636 1.1 mrg
637 1.1 mrg static void
638 1.1 mrg output_asm_ctf_sou_fields (ctf_container_ref ARG_UNUSED (ctfc),
639 1.1 mrg ctf_dtdef_ref dtd)
640 1.1 mrg {
641 1.1 mrg ctf_dmdef_t * dmd;
642 1.1 mrg
643 1.1 mrg /* Function pointer to dump struct/union members. */
644 1.1 mrg void (*ctf_asm_sou_field_func) (ctf_dmdef_t *);
645 1.1 mrg
646 1.1 mrg uint32_t size = dtd->dtd_data.ctti_size;
647 1.1 mrg
648 1.1 mrg /* The variable length data struct/union CTF types is an array of
649 1.1 mrg ctf_member or ctf_lmember, depending on size of the member. */
650 1.1 mrg if (size >= CTF_LSTRUCT_THRESH)
651 1.1 mrg ctf_asm_sou_field_func = ctf_asm_sou_lmember;
652 1.1 mrg else
653 1.1 mrg ctf_asm_sou_field_func = ctf_asm_sou_member;
654 1.1 mrg
655 1.1 mrg for (dmd = dtd->dtd_u.dtu_members;
656 1.1 mrg dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
657 1.1 mrg {
658 1.1 mrg ctf_asm_sou_field_func (dmd);
659 1.1 mrg /* Sanity Check - Unrepresented types appear as explicit types. */
660 1.1 mrg gcc_assert (dmd->dmd_type != CTF_NULL_TYPEID);
661 1.1 mrg }
662 1.1 mrg }
663 1.1 mrg
664 1.1 mrg /* Output the list of enumerator constants of the CTF enum type. */
665 1.1 mrg
666 1.1 mrg static void
667 1.1 mrg output_asm_ctf_enum_list (ctf_container_ref ARG_UNUSED (ctfc),
668 1.1 mrg ctf_dtdef_ref dtd)
669 1.1 mrg {
670 1.1 mrg ctf_dmdef_t * dmd;
671 1.1 mrg
672 1.1 mrg for (dmd = dtd->dtd_u.dtu_members;
673 1.1 mrg dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
674 1.1 mrg ctf_asm_enum_const (dmd);
675 1.1 mrg }
676 1.1 mrg
677 1.1 mrg /* Output the list of function arguments of the CTF function type. */
678 1.1 mrg
679 1.1 mrg static void
680 1.1 mrg output_asm_func_args_list (ctf_container_ref ARG_UNUSED (ctfc),
681 1.1 mrg ctf_dtdef_ref dtd)
682 1.1 mrg {
683 1.1 mrg ctf_func_arg_t * farg;
684 1.1 mrg
685 1.1 mrg for (farg = dtd->dtd_u.dtu_argv;
686 1.1 mrg farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
687 1.1 mrg ctf_asm_func_arg (farg);
688 1.1 mrg }
689 1.1 mrg
690 1.1 mrg /* Output the variable length portion of the CTF type record. */
691 1.1 mrg
692 1.1 mrg static void
693 1.1 mrg output_asm_ctf_vlen_bytes (ctf_container_ref ctfc, ctf_dtdef_ref ctftype)
694 1.1 mrg {
695 1.1 mrg uint32_t encoding;
696 1.1 mrg uint32_t kind = CTF_V2_INFO_KIND (ctftype->dtd_data.ctti_info);
697 1.1 mrg uint32_t vlen = CTF_V2_INFO_VLEN (ctftype->dtd_data.ctti_info);
698 1.1 mrg
699 1.1 mrg switch (kind)
700 1.1 mrg {
701 1.1 mrg case CTF_K_INTEGER:
702 1.1 mrg case CTF_K_FLOAT:
703 1.1 mrg if (kind == CTF_K_INTEGER)
704 1.1 mrg {
705 1.1 mrg encoding = CTF_INT_DATA (ctftype->dtd_u.dtu_enc.cte_format,
706 1.1 mrg ctftype->dtd_u.dtu_enc.cte_offset,
707 1.1 mrg ctftype->dtd_u.dtu_enc.cte_bits);
708 1.1 mrg }
709 1.1 mrg else
710 1.1 mrg {
711 1.1 mrg encoding = CTF_FP_DATA (ctftype->dtd_u.dtu_enc.cte_format,
712 1.1 mrg ctftype->dtd_u.dtu_enc.cte_offset,
713 1.1 mrg ctftype->dtd_u.dtu_enc.cte_bits);
714 1.1 mrg }
715 1.1 mrg dw2_asm_output_data (4, encoding, "ctf_encoding_data");
716 1.1 mrg break;
717 1.1 mrg case CTF_K_FUNCTION:
718 1.1 mrg {
719 1.1 mrg output_asm_func_args_list (ctfc, ctftype);
720 1.1 mrg /* FIXME - CTF_PADDING_FOR_ALIGNMENT.
721 1.1 mrg libctf expects this padding for alignment reasons. Expected to
722 1.1 mrg be redundant in CTF_VERSION_4. */
723 1.1 mrg if (vlen & 1)
724 1.1 mrg dw2_asm_output_data (4, 0, "dtu_argv_padding");
725 1.1 mrg
726 1.1 mrg break;
727 1.1 mrg }
728 1.1 mrg case CTF_K_ARRAY:
729 1.1 mrg ctf_asm_array (ctftype);
730 1.1 mrg break;
731 1.1 mrg case CTF_K_SLICE:
732 1.1 mrg {
733 1.1 mrg ctf_asm_slice (ctftype);
734 1.1 mrg /* Type of the slice must be a valid CTF type. */
735 1.1 mrg gcc_assert (ctftype->dtd_u.dtu_slice.cts_type != CTF_NULL_TYPEID);
736 1.1 mrg break;
737 1.1 mrg }
738 1.1 mrg case CTF_K_STRUCT:
739 1.1 mrg case CTF_K_UNION:
740 1.1 mrg output_asm_ctf_sou_fields (ctfc, ctftype);
741 1.1 mrg break;
742 1.1 mrg case CTF_K_ENUM:
743 1.1 mrg output_asm_ctf_enum_list (ctfc, ctftype);
744 1.1 mrg break;
745 1.1 mrg
746 1.1 mrg default:
747 1.1 mrg /* CTF types of kind CTF_K_VOLATILE, CTF_K_CONST, CTF_K_RESTRICT,
748 1.1 mrg etc have no vlen data to write. */
749 1.1 mrg break;
750 1.1 mrg }
751 1.1 mrg }
752 1.1 mrg
753 1.1 mrg /* Output a CTF Type. */
754 1.1 mrg
755 1.1 mrg static void
756 1.1 mrg output_asm_ctf_type (ctf_container_ref ctfc, ctf_dtdef_ref type)
757 1.1 mrg {
758 1.1 mrg if (type->dtd_data.ctti_size <= CTF_MAX_SIZE)
759 1.1 mrg ctf_asm_stype (type);
760 1.1 mrg else
761 1.1 mrg ctf_asm_type (type);
762 1.1 mrg /* Now comes the variable-length portion for defining types completely.
763 1.1 mrg E.g., encoding follows CTF_INT_DATA, CTF_FP_DATA types,
764 1.1 mrg struct ctf_array_t follows CTF_K_ARRAY types, or a bunch of
765 1.1 mrg struct ctf_member / ctf_lmember ctf_enum sit in there for CTF_K_STRUCT or
766 1.1 mrg CTF_K_UNION. */
767 1.1 mrg output_asm_ctf_vlen_bytes (ctfc, type);
768 1.1 mrg
769 1.1 mrg uint32_t kind = CTF_V2_INFO_KIND (type->dtd_data.ctti_info);
770 1.1 mrg /* The underlying type must be a valid CTF type. */
771 1.1 mrg if (kind == CTF_K_POINTER || kind == CTF_K_TYPEDEF
772 1.1 mrg || kind == CTF_K_VOLATILE || kind == CTF_K_CONST
773 1.1 mrg || kind == CTF_K_RESTRICT)
774 1.1 mrg gcc_assert (type->dtd_data.ctti_type != CTF_NULL_TYPEID);
775 1.1 mrg }
776 1.1 mrg
777 1.1 mrg /* Output all CTF type records. */
778 1.1 mrg
779 1.1 mrg static void
780 1.1 mrg output_ctf_types (ctf_container_ref ctfc)
781 1.1 mrg {
782 1.1 mrg size_t i;
783 1.1 mrg size_t num_ctf_types = ctfc->ctfc_types->elements ();
784 1.1 mrg if (num_ctf_types)
785 1.1 mrg {
786 1.1 mrg /* Type ID = 0 is used as sentinel value; not a valid type. */
787 1.1 mrg for (i = 1; i <= num_ctf_types; i++)
788 1.1 mrg output_asm_ctf_type (ctfc, ctfc->ctfc_types_list[i]);
789 1.1 mrg }
790 1.1 mrg }
791 1.1 mrg
792 1.1 mrg /* CTF routines interfacing to the compiler. */
793 1.1 mrg
794 1.1 mrg /* Prepare and output the CTF section. */
795 1.1 mrg
796 1.1 mrg void
797 1.1 mrg ctf_output (const char * filename)
798 1.1 mrg {
799 1.1 mrg if (ctf_debug_info_level == CTFINFO_LEVEL_NONE)
800 1.1 mrg return;
801 1.1 mrg
802 1.1 mrg /* Get the CTF container for the current translation unit. */
803 1.1 mrg ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
804 1.1 mrg
805 1.1 mrg init_ctf_sections ();
806 1.1 mrg
807 1.1 mrg ctf_add_cuname (tu_ctfc, filename);
808 1.1 mrg
809 1.1 mrg /* Pre-process CTF before generating assembly. */
810 1.1 mrg ctf_preprocess (tu_ctfc);
811 1.1 mrg output_ctf_header (tu_ctfc);
812 1.1 mrg output_ctf_obj_info (tu_ctfc);
813 1.1 mrg output_ctf_func_info (tu_ctfc);
814 1.1 mrg output_ctf_objtidx (tu_ctfc);
815 1.1 mrg output_ctf_funcidx (tu_ctfc);
816 1.1 mrg output_ctf_vars (tu_ctfc);
817 1.1 mrg output_ctf_types (tu_ctfc);
818 1.1 mrg output_ctf_strs (tu_ctfc);
819 1.1 mrg
820 1.1 mrg /* The total number of string bytes must be equal to those processed out to
821 1.1 mrg the str subsection. */
822 1.1 mrg gcc_assert (tu_ctfc->ctfc_strlen
823 1.1 mrg == ctfc_get_strtab_len (tu_ctfc, CTF_STRTAB));
824 1.1 mrg
825 1.1 mrg }
826 1.1 mrg
827 1.1 mrg /* Reset all state for CTF generation so that we can rerun the compiler within
828 1.1 mrg the same process. */
829 1.1 mrg
830 1.1 mrg void
831 1.1 mrg ctf_finalize (void)
832 1.1 mrg {
833 1.1 mrg ctf_info_section = NULL;
834 1.1 mrg
835 1.1 mrg ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
836 1.1 mrg ctfc_delete_container (tu_ctfc);
837 1.1 mrg tu_ctfc = NULL;
838 1.1 mrg }
839 1.1 mrg
840 1.1 mrg #include "gt-ctfout.h"
841