fixup! riscv: Implement large addend for global address
[tinycc.git] / tccdbg.c
bloba2c6b56fa397ad5493e932369f7d8b5a4d7e7f30
1 /*
2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 /* stab debug support */
25 static const struct {
26 int type;
27 int size;
28 int encoding;
29 const char *name;
30 } default_debug[] = {
31 { VT_INT, 4, DW_ATE_signed, "int:t1=r1;-2147483648;2147483647;" },
32 { VT_BYTE, 1, DW_ATE_signed_char, "char:t2=r2;0;127;" },
33 #if LONG_SIZE == 4
34 { VT_LONG | VT_INT, 4, DW_ATE_signed, "long int:t3=r3;-2147483648;2147483647;" },
35 #else
36 { VT_LLONG | VT_LONG, 8, DW_ATE_signed, "long int:t3=r3;-9223372036854775808;9223372036854775807;" },
37 #endif
38 { VT_INT | VT_UNSIGNED, 4, DW_ATE_unsigned, "unsigned int:t4=r4;0;037777777777;" },
39 #if LONG_SIZE == 4
40 { VT_LONG | VT_INT | VT_UNSIGNED, 4, DW_ATE_unsigned, "long unsigned int:t5=r5;0;037777777777;" },
41 #else
42 /* use octal instead of -1 so size_t works (-gstabs+ in gcc) */
43 { VT_LLONG | VT_LONG | VT_UNSIGNED, 8, DW_ATE_unsigned, "long unsigned int:t5=r5;0;01777777777777777777777;" },
44 #endif
45 { VT_QLONG, 16, DW_ATE_signed, "__int128:t6=r6;0;-1;" },
46 { VT_QLONG | VT_UNSIGNED, 16, DW_ATE_unsigned, "__int128 unsigned:t7=r7;0;-1;" },
47 { VT_LLONG, 8, DW_ATE_signed, "long long int:t8=r8;-9223372036854775808;9223372036854775807;" },
48 { VT_LLONG | VT_UNSIGNED, 8, DW_ATE_unsigned, "long long unsigned int:t9=r9;0;01777777777777777777777;" },
49 { VT_SHORT, 2, DW_ATE_signed, "short int:t10=r10;-32768;32767;" },
50 { VT_SHORT | VT_UNSIGNED, 2, DW_ATE_unsigned, "short unsigned int:t11=r11;0;65535;" },
51 { VT_BYTE | VT_DEFSIGN, 1, DW_ATE_signed_char, "signed char:t12=r12;-128;127;" },
52 { VT_BYTE | VT_DEFSIGN | VT_UNSIGNED, 1, DW_ATE_unsigned_char, "unsigned char:t13=r13;0;255;" },
53 { VT_FLOAT, 4, DW_ATE_float, "float:t14=r1;4;0;" },
54 { VT_DOUBLE, 8, DW_ATE_float, "double:t15=r1;8;0;" },
55 #ifdef TCC_USING_DOUBLE_FOR_LDOUBLE
56 { VT_DOUBLE | VT_LONG, 8, DW_ATE_float, "long double:t16=r1;8;0;" },
57 #else
58 { VT_LDOUBLE, 16, DW_ATE_float, "long double:t16=r1;16;0;" },
59 #endif
60 { -1, -1, -1, "_Float32:t17=r1;4;0;" },
61 { -1, -1, -1, "_Float64:t18=r1;8;0;" },
62 { -1, -1, -1, "_Float128:t19=r1;16;0;" },
63 { -1, -1, -1, "_Float32x:t20=r1;8;0;" },
64 { -1, -1, -1, "_Float64x:t21=r1;16;0;" },
65 { -1, -1, -1, "_Decimal32:t22=r1;4;0;" },
66 { -1, -1, -1, "_Decimal64:t23=r1;8;0;" },
67 { -1, -1, -1, "_Decimal128:t24=r1;16;0;" },
68 /* if default char is unsigned */
69 { VT_BYTE | VT_UNSIGNED, 1, DW_ATE_unsigned_char, "unsigned char:t25=r25;0;255;" },
70 /* boolean type */
71 { VT_BOOL, 1, DW_ATE_boolean, "bool:t26=r26;0;255;" },
72 #if LONG_SIZE == 4
73 { VT_VOID, 1, DW_ATE_unsigned_char, "void:t27=27" },
74 #else
75 /* bitfields use these */
76 { VT_LONG | VT_INT, 8, DW_ATE_signed, "long int:t27=r27;-9223372036854775808;9223372036854775807;" },
77 { VT_LONG | VT_INT | VT_UNSIGNED, 8, DW_ATE_unsigned, "long unsigned int:t28=r28;0;01777777777777777777777;" },
78 { VT_VOID, 1, DW_ATE_unsigned_char, "void:t29=29" },
79 #endif
82 #define N_DEFAULT_DEBUG (sizeof (default_debug) / sizeof (default_debug[0]))
84 /* dwarf debug */
86 #define DWARF_LINE_BASE -5
87 #define DWARF_LINE_RANGE 14
88 #define DWARF_OPCODE_BASE 13
90 #if defined TCC_TARGET_ARM64
91 #define DWARF_MIN_INSTR_LEN 4
92 #elif defined TCC_TARGET_ARM
93 #define DWARF_MIN_INSTR_LEN 2
94 #else
95 #define DWARF_MIN_INSTR_LEN 1
96 #endif
98 #define DWARF_ABBREV_COMPILE_UNIT 1
99 #define DWARF_ABBREV_BASE_TYPE 2
100 #define DWARF_ABBREV_VARIABLE_EXTERNAL 3
101 #define DWARF_ABBREV_VARIABLE_STATIC 4
102 #define DWARF_ABBREV_VARIABLE_LOCAL 5
103 #define DWARF_ABBREV_FORMAL_PARAMETER 6
104 #define DWARF_ABBREV_POINTER 7
105 #define DWARF_ABBREV_ARRAY_TYPE 8
106 #define DWARF_ABBREV_SUBRANGE_TYPE 9
107 #define DWARF_ABBREV_TYPEDEF 10
108 #define DWARF_ABBREV_ENUMERATOR_SIGNED 11
109 #define DWARF_ABBREV_ENUMERATOR_UNSIGNED 12
110 #define DWARF_ABBREV_ENUMERATION_TYPE 13
111 #define DWARF_ABBREV_MEMBER 14
112 #define DWARF_ABBREV_MEMBER_BF 15
113 #define DWARF_ABBREV_STRUCTURE_TYPE 16
114 #define DWARF_ABBREV_STRUCTURE_EMPTY_TYPE 17
115 #define DWARF_ABBREV_UNION_TYPE 18
116 #define DWARF_ABBREV_UNION_EMPTY_TYPE 19
117 #define DWARF_ABBREV_SUBPROGRAM_EXTERNAL 20
118 #define DWARF_ABBREV_SUBPROGRAM_STATIC 21
119 #define DWARF_ABBREV_LEXICAL_BLOCK 22
120 #define DWARF_ABBREV_LEXICAL_EMPTY_BLOCK 23
121 #define DWARF_ABBREV_SUBROUTINE_TYPE 24
122 #define DWARF_ABBREV_SUBROUTINE_EMPTY_TYPE 25
123 #define DWARF_ABBREV_FORMAL_PARAMETER2 26
125 /* all entries should have been generated with dwarf_uleb128 except
126 has_children. All values are currently below 128 so this currently
127 works. */
128 static const unsigned char dwarf_abbrev_init[] = {
129 DWARF_ABBREV_COMPILE_UNIT, DW_TAG_compile_unit, 1,
130 DW_AT_producer, DW_FORM_strp,
131 DW_AT_language, DW_FORM_data1,
132 DW_AT_name, DW_FORM_line_strp,
133 DW_AT_comp_dir, DW_FORM_line_strp,
134 DW_AT_low_pc, DW_FORM_addr,
135 #if PTR_SIZE == 4
136 DW_AT_high_pc, DW_FORM_data4,
137 #else
138 DW_AT_high_pc, DW_FORM_data8,
139 #endif
140 DW_AT_stmt_list, DW_FORM_sec_offset,
141 0, 0,
142 DWARF_ABBREV_BASE_TYPE, DW_TAG_base_type, 0,
143 DW_AT_byte_size, DW_FORM_udata,
144 DW_AT_encoding, DW_FORM_data1,
145 DW_AT_name, DW_FORM_strp,
146 0, 0,
147 DWARF_ABBREV_VARIABLE_EXTERNAL, DW_TAG_variable, 0,
148 DW_AT_name, DW_FORM_strp,
149 DW_AT_decl_file, DW_FORM_udata,
150 DW_AT_decl_line, DW_FORM_udata,
151 DW_AT_type, DW_FORM_ref4,
152 DW_AT_external, DW_FORM_flag,
153 DW_AT_location, DW_FORM_exprloc,
154 0, 0,
155 DWARF_ABBREV_VARIABLE_STATIC, DW_TAG_variable, 0,
156 DW_AT_name, DW_FORM_strp,
157 DW_AT_decl_file, DW_FORM_udata,
158 DW_AT_decl_line, DW_FORM_udata,
159 DW_AT_type, DW_FORM_ref4,
160 DW_AT_location, DW_FORM_exprloc,
161 0, 0,
162 DWARF_ABBREV_VARIABLE_LOCAL, DW_TAG_variable, 0,
163 DW_AT_name, DW_FORM_strp,
164 DW_AT_type, DW_FORM_ref4,
165 DW_AT_location, DW_FORM_exprloc,
166 0, 0,
167 DWARF_ABBREV_FORMAL_PARAMETER, DW_TAG_formal_parameter, 0,
168 DW_AT_name, DW_FORM_strp,
169 DW_AT_type, DW_FORM_ref4,
170 DW_AT_location, DW_FORM_exprloc,
171 0, 0,
172 DWARF_ABBREV_POINTER, DW_TAG_pointer_type, 0,
173 DW_AT_byte_size, DW_FORM_data1,
174 DW_AT_type, DW_FORM_ref4,
175 0, 0,
176 DWARF_ABBREV_ARRAY_TYPE, DW_TAG_array_type, 1,
177 DW_AT_type, DW_FORM_ref4,
178 DW_AT_sibling, DW_FORM_ref4,
179 0, 0,
180 DWARF_ABBREV_SUBRANGE_TYPE, DW_TAG_subrange_type, 0,
181 DW_AT_type, DW_FORM_ref4,
182 DW_AT_upper_bound, DW_FORM_udata,
183 0, 0,
184 DWARF_ABBREV_TYPEDEF, DW_TAG_typedef, 0,
185 DW_AT_name, DW_FORM_strp,
186 DW_AT_decl_file, DW_FORM_udata,
187 DW_AT_decl_line, DW_FORM_udata,
188 DW_AT_type, DW_FORM_ref4,
189 0, 0,
190 DWARF_ABBREV_ENUMERATOR_SIGNED, DW_TAG_enumerator, 0,
191 DW_AT_name, DW_FORM_strp,
192 DW_AT_const_value, DW_FORM_sdata,
193 0, 0,
194 DWARF_ABBREV_ENUMERATOR_UNSIGNED, DW_TAG_enumerator, 0,
195 DW_AT_name, DW_FORM_strp,
196 DW_AT_const_value, DW_FORM_udata,
197 0, 0,
198 DWARF_ABBREV_ENUMERATION_TYPE, DW_TAG_enumeration_type, 1,
199 DW_AT_name, DW_FORM_strp,
200 DW_AT_encoding, DW_FORM_data1,
201 DW_AT_byte_size, DW_FORM_data1,
202 DW_AT_type, DW_FORM_ref4,
203 DW_AT_decl_file, DW_FORM_udata,
204 DW_AT_decl_line, DW_FORM_udata,
205 DW_AT_sibling, DW_FORM_ref4,
206 0, 0,
207 DWARF_ABBREV_MEMBER, DW_TAG_member, 0,
208 DW_AT_name, DW_FORM_strp,
209 DW_AT_decl_file, DW_FORM_udata,
210 DW_AT_decl_line, DW_FORM_udata,
211 DW_AT_type, DW_FORM_ref4,
212 DW_AT_data_member_location, DW_FORM_udata,
213 0, 0,
214 DWARF_ABBREV_MEMBER_BF, DW_TAG_member, 0,
215 DW_AT_name, DW_FORM_strp,
216 DW_AT_decl_file, DW_FORM_udata,
217 DW_AT_decl_line, DW_FORM_udata,
218 DW_AT_type, DW_FORM_ref4,
219 DW_AT_bit_size, DW_FORM_udata,
220 DW_AT_data_bit_offset, DW_FORM_udata,
221 0, 0,
222 DWARF_ABBREV_STRUCTURE_TYPE, DW_TAG_structure_type, 1,
223 DW_AT_name, DW_FORM_strp,
224 DW_AT_byte_size, DW_FORM_udata,
225 DW_AT_decl_file, DW_FORM_udata,
226 DW_AT_decl_line, DW_FORM_udata,
227 DW_AT_sibling, DW_FORM_ref4,
228 0, 0,
229 DWARF_ABBREV_STRUCTURE_EMPTY_TYPE, DW_TAG_structure_type, 0,
230 DW_AT_name, DW_FORM_strp,
231 DW_AT_byte_size, DW_FORM_udata,
232 DW_AT_decl_file, DW_FORM_udata,
233 DW_AT_decl_line, DW_FORM_udata,
234 0, 0,
235 DWARF_ABBREV_UNION_TYPE, DW_TAG_union_type, 1,
236 DW_AT_name, DW_FORM_strp,
237 DW_AT_byte_size, DW_FORM_udata,
238 DW_AT_decl_file, DW_FORM_udata,
239 DW_AT_decl_line, DW_FORM_udata,
240 DW_AT_sibling, DW_FORM_ref4,
241 0, 0,
242 DWARF_ABBREV_UNION_EMPTY_TYPE, DW_TAG_union_type, 0,
243 DW_AT_name, DW_FORM_strp,
244 DW_AT_byte_size, DW_FORM_udata,
245 DW_AT_decl_file, DW_FORM_udata,
246 DW_AT_decl_line, DW_FORM_udata,
247 0, 0,
248 DWARF_ABBREV_SUBPROGRAM_EXTERNAL, DW_TAG_subprogram, 1,
249 DW_AT_external, DW_FORM_flag,
250 DW_AT_name, DW_FORM_strp,
251 DW_AT_decl_file, DW_FORM_udata,
252 DW_AT_decl_line, DW_FORM_udata,
253 DW_AT_type, DW_FORM_ref4,
254 DW_AT_low_pc, DW_FORM_addr,
255 #if PTR_SIZE == 4
256 DW_AT_high_pc, DW_FORM_data4,
257 #else
258 DW_AT_high_pc, DW_FORM_data8,
259 #endif
260 DW_AT_sibling, DW_FORM_ref4,
261 DW_AT_frame_base, DW_FORM_exprloc,
262 0, 0,
263 DWARF_ABBREV_SUBPROGRAM_STATIC, DW_TAG_subprogram, 1,
264 DW_AT_name, DW_FORM_strp,
265 DW_AT_decl_file, DW_FORM_udata,
266 DW_AT_decl_line, DW_FORM_udata,
267 DW_AT_type, DW_FORM_ref4,
268 DW_AT_low_pc, DW_FORM_addr,
269 #if PTR_SIZE == 4
270 DW_AT_high_pc, DW_FORM_data4,
271 #else
272 DW_AT_high_pc, DW_FORM_data8,
273 #endif
274 DW_AT_sibling, DW_FORM_ref4,
275 DW_AT_frame_base, DW_FORM_exprloc,
276 0, 0,
277 DWARF_ABBREV_LEXICAL_BLOCK, DW_TAG_lexical_block, 1,
278 DW_AT_low_pc, DW_FORM_addr,
279 #if PTR_SIZE == 4
280 DW_AT_high_pc, DW_FORM_data4,
281 #else
282 DW_AT_high_pc, DW_FORM_data8,
283 #endif
284 0, 0,
285 DWARF_ABBREV_LEXICAL_EMPTY_BLOCK, DW_TAG_lexical_block, 0,
286 DW_AT_low_pc, DW_FORM_addr,
287 #if PTR_SIZE == 4
288 DW_AT_high_pc, DW_FORM_data4,
289 #else
290 DW_AT_high_pc, DW_FORM_data8,
291 #endif
292 0, 0,
293 DWARF_ABBREV_SUBROUTINE_TYPE, DW_TAG_subroutine_type, 1,
294 DW_AT_type, DW_FORM_ref4,
295 DW_AT_sibling, DW_FORM_ref4,
296 0, 0,
297 DWARF_ABBREV_SUBROUTINE_EMPTY_TYPE, DW_TAG_subroutine_type, 0,
298 DW_AT_type, DW_FORM_ref4,
299 0, 0,
300 DWARF_ABBREV_FORMAL_PARAMETER2, DW_TAG_formal_parameter, 0,
301 DW_AT_type, DW_FORM_ref4,
302 0, 0,
306 static const unsigned char dwarf_line_opcodes[] = {
307 0 ,1 ,1 ,1 ,1 ,0 ,0 ,0 ,1 ,0 ,0 ,1
310 /* ------------------------------------------------------------------------- */
311 /* debug state */
313 struct _tccdbg {
315 int last_line_num, new_file;
316 int section_sym;
318 int debug_next_type;
320 struct _debug_hash {
321 int debug_type;
322 Sym *type;
323 } *debug_hash;
325 struct _debug_anon_hash {
326 Sym *type;
327 int n_debug_type;
328 int *debug_type;
329 } *debug_anon_hash;
331 int n_debug_hash;
332 int n_debug_anon_hash;
334 struct _debug_info {
335 int start;
336 int end;
337 int n_sym;
338 struct debug_sym {
339 int type;
340 unsigned long value;
341 char *str;
342 Section *sec;
343 int sym_index;
344 int info;
345 int file;
346 int line;
347 } *sym;
348 struct _debug_info *child, *next, *last, *parent;
349 } *debug_info, *debug_info_root;
351 struct {
352 int info;
353 int abbrev;
354 int line;
355 int str;
356 int line_str;
357 } dwarf_sym;
359 struct {
360 int start;
361 int dir_size;
362 char **dir_table;
363 int filename_size;
364 struct dwarf_filename_struct {
365 int dir_entry;
366 char *name;
367 } *filename_table;
368 int line_size;
369 int line_max_size;
370 unsigned char *line_data;
371 int cur_file;
372 int last_file;
373 int last_pc;
374 int last_line;
375 } dwarf_line;
377 struct {
378 int start;
379 Sym *func;
380 int line;
381 int base_type_used[N_DEFAULT_DEBUG];
382 } dwarf_info;
384 /* test coverage */
385 struct {
386 unsigned long offset;
387 unsigned long last_file_name;
388 unsigned long last_func_name;
389 int ind;
390 int line;
391 } tcov_data;
395 #define last_line_num s1->dState->last_line_num
396 #define new_file s1->dState->new_file
397 #define section_sym s1->dState->section_sym
398 #define debug_next_type s1->dState->debug_next_type
399 #define debug_hash s1->dState->debug_hash
400 #define debug_anon_hash s1->dState->debug_anon_hash
401 #define n_debug_hash s1->dState->n_debug_hash
402 #define n_debug_anon_hash s1->dState->n_debug_anon_hash
403 #define debug_info s1->dState->debug_info
404 #define debug_info_root s1->dState->debug_info_root
405 #define dwarf_sym s1->dState->dwarf_sym
406 #define dwarf_line s1->dState->dwarf_line
407 #define dwarf_info s1->dState->dwarf_info
408 #define tcov_data s1->dState->tcov_data
410 /* ------------------------------------------------------------------------- */
411 static void put_stabs(TCCState *s1, const char *str, int type, int other,
412 int desc, unsigned long value);
414 ST_FUNC void tcc_debug_new(TCCState *s1)
416 int shf = 0;
417 if (!s1->dState)
418 s1->dState = tcc_mallocz(sizeof *s1->dState);
420 #ifdef CONFIG_TCC_BACKTRACE
421 /* include stab info with standalone backtrace support */
422 if (s1->do_debug && s1->output_type == TCC_OUTPUT_MEMORY)
423 s1->do_backtrace = 1;
424 if (s1->do_backtrace)
425 shf = SHF_ALLOC | SHF_WRITE; // SHF_WRITE needed for musl/SELINUX
426 #endif
428 if (s1->dwarf) {
429 s1->dwlo = s1->nb_sections;
430 dwarf_info_section =
431 new_section(s1, ".debug_info", SHT_PROGBITS, shf);
432 dwarf_abbrev_section =
433 new_section(s1, ".debug_abbrev", SHT_PROGBITS, shf);
434 dwarf_line_section =
435 new_section(s1, ".debug_line", SHT_PROGBITS, shf);
436 dwarf_aranges_section =
437 new_section(s1, ".debug_aranges", SHT_PROGBITS, shf);
438 shf |= SHF_MERGE | SHF_STRINGS;
439 dwarf_str_section =
440 new_section(s1, ".debug_str", SHT_PROGBITS, shf);
441 dwarf_str_section->sh_entsize = 1;
442 dwarf_info_section->sh_addralign =
443 dwarf_abbrev_section->sh_addralign =
444 dwarf_line_section->sh_addralign =
445 dwarf_aranges_section->sh_addralign =
446 dwarf_str_section->sh_addralign = 1;
447 if (s1->dwarf >= 5) {
448 dwarf_line_str_section =
449 new_section(s1, ".debug_line_str", SHT_PROGBITS, shf);
450 dwarf_line_str_section->sh_entsize = 1;
451 dwarf_line_str_section->sh_addralign = 1;
453 s1->dwhi = s1->nb_sections;
455 else
457 stab_section = new_section(s1, ".stab", SHT_PROGBITS, shf);
458 stab_section->sh_entsize = sizeof(Stab_Sym);
459 stab_section->sh_addralign = sizeof ((Stab_Sym*)0)->n_value;
460 stab_section->link = new_section(s1, ".stabstr", SHT_STRTAB, shf);
461 /* put first entry */
462 put_stabs(s1, "", 0, 0, 0, 0);
466 /* put stab debug information */
467 static void put_stabs(TCCState *s1, const char *str, int type, int other, int desc,
468 unsigned long value)
470 Stab_Sym *sym;
472 unsigned offset;
473 if (type == N_SLINE
474 && (offset = stab_section->data_offset)
475 && (sym = (Stab_Sym*)(stab_section->data + offset) - 1)
476 && sym->n_type == type
477 && sym->n_value == value) {
478 /* just update line_number in previous entry */
479 sym->n_desc = desc;
480 return;
483 sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
484 if (str) {
485 sym->n_strx = put_elf_str(stab_section->link, str);
486 } else {
487 sym->n_strx = 0;
489 sym->n_type = type;
490 sym->n_other = other;
491 sym->n_desc = desc;
492 sym->n_value = value;
495 static void put_stabs_r(TCCState *s1, const char *str, int type, int other, int desc,
496 unsigned long value, Section *sec, int sym_index)
498 put_elf_reloc(symtab_section, stab_section,
499 stab_section->data_offset + 8,
500 sizeof ((Stab_Sym*)0)->n_value == PTR_SIZE ? R_DATA_PTR : R_DATA_32,
501 sym_index);
502 put_stabs(s1, str, type, other, desc, value);
505 static void put_stabn(TCCState *s1, int type, int other, int desc, int value)
507 put_stabs(s1, NULL, type, other, desc, value);
510 /* ------------------------------------------------------------------------- */
511 #define dwarf_data1(s,data) \
512 { unsigned char *p = section_ptr_add((s), 1); *p = (data); }
513 #define dwarf_data2(s,data) \
514 write16le(section_ptr_add((s), 2), (data))
515 #define dwarf_data4(s,data) \
516 write32le(section_ptr_add((s), 4), (data))
517 #define dwarf_data8(s,data) \
518 write64le(section_ptr_add((s), 8), (data))
520 static int dwarf_get_section_sym(Section *s)
522 TCCState *s1 = s->s1;
523 return put_elf_sym(symtab_section, 0, 0,
524 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
525 s->sh_num, NULL);
528 static void dwarf_reloc(Section *s, int sym, int rel)
530 TCCState *s1 = s->s1;
531 put_elf_reloca(symtab_section, s, s->data_offset, rel, sym, 0);
534 static void dwarf_string(Section *s, Section *dw, int sym, const char *str)
536 TCCState *s1 = s->s1;
537 int offset, len;
538 char *ptr;
540 len = strlen(str) + 1;
541 offset = dw->data_offset;
542 ptr = section_ptr_add(dw, len);
543 memmove(ptr, str, len);
544 put_elf_reloca(symtab_section, s, s->data_offset, R_DATA_32DW, sym,
545 PTR_SIZE == 4 ? 0 : offset);
546 dwarf_data4(s, PTR_SIZE == 4 ? offset : 0);
549 static void dwarf_strp(Section *s, const char *str)
551 TCCState *s1 = s->s1;
552 dwarf_string(s, dwarf_str_section, dwarf_sym.str, str);
555 static void dwarf_line_strp(Section *s, const char *str)
557 TCCState *s1 = s->s1;
558 dwarf_string(s, dwarf_line_str_section, dwarf_sym.line_str, str);
561 static void dwarf_line_op(TCCState *s1, unsigned char op)
563 if (dwarf_line.line_size >= dwarf_line.line_max_size) {
564 dwarf_line.line_max_size += 1024;
565 dwarf_line.line_data =
566 (unsigned char *)tcc_realloc(dwarf_line.line_data,
567 dwarf_line.line_max_size);
569 dwarf_line.line_data[dwarf_line.line_size++] = op;
572 static void dwarf_file(TCCState *s1)
574 int i, j;
575 char *filename;
576 int index_offset = s1->dwarf < 5;
578 if (!strcmp(file->filename, "<command line>")) {
579 dwarf_line.cur_file = 1;
580 return;
582 filename = strrchr(file->filename, '/');
583 if (filename == NULL) {
584 for (i = 1; i < dwarf_line.filename_size; i++)
585 if (dwarf_line.filename_table[i].dir_entry == 0 &&
586 strcmp(dwarf_line.filename_table[i].name,
587 file->filename) == 0) {
588 dwarf_line.cur_file = i + index_offset;
589 return;
591 i = -index_offset;
592 filename = file->filename;
594 else {
595 char *undo = filename;
596 char *dir = file->filename;
598 *filename++ = '\0';
599 for (i = 0; i < dwarf_line.dir_size; i++)
600 if (strcmp(dwarf_line.dir_table[i], dir) == 0) {
601 for (j = 1; j < dwarf_line.filename_size; j++)
602 if (dwarf_line.filename_table[j].dir_entry - index_offset
603 == i &&
604 strcmp(dwarf_line.filename_table[j].name,
605 filename) == 0) {
606 *undo = '/';
607 dwarf_line.cur_file = j + index_offset;
608 return;
610 break;
612 if (i == dwarf_line.dir_size) {
613 dwarf_line.dir_size++;
614 dwarf_line.dir_table =
615 (char **) tcc_realloc(dwarf_line.dir_table,
616 dwarf_line.dir_size *
617 sizeof (char *));
618 dwarf_line.dir_table[i] = tcc_strdup(dir);
620 *undo = '/';
622 dwarf_line.filename_table =
623 (struct dwarf_filename_struct *)
624 tcc_realloc(dwarf_line.filename_table,
625 (dwarf_line.filename_size + 1) *
626 sizeof (struct dwarf_filename_struct));
627 dwarf_line.filename_table[dwarf_line.filename_size].dir_entry =
628 i + index_offset;
629 dwarf_line.filename_table[dwarf_line.filename_size].name =
630 tcc_strdup(filename);
631 dwarf_line.cur_file = dwarf_line.filename_size++ + index_offset;
632 return;
635 #if 0
636 static int dwarf_uleb128_size (unsigned long long value)
638 int size = 0;
640 do {
641 value >>= 7;
642 size++;
643 } while (value != 0);
644 return size;
646 #endif
648 static int dwarf_sleb128_size (long long value)
650 int size = 0;
651 long long end = value >> 63;
652 unsigned char last = end & 0x40;
653 unsigned char byte;
655 do {
656 byte = value & 0x7f;
657 value >>= 7;
658 size++;
659 } while (value != end || (byte & 0x40) != last);
660 return size;
663 static void dwarf_uleb128 (Section *s, unsigned long long value)
665 do {
666 unsigned char byte = value & 0x7f;
668 value >>= 7;
669 dwarf_data1(s, byte | (value ? 0x80 : 0));
670 } while (value != 0);
673 static void dwarf_sleb128 (Section *s, long long value)
675 int more;
676 long long end = value >> 63;
677 unsigned char last = end & 0x40;
679 do {
680 unsigned char byte = value & 0x7f;
682 value >>= 7;
683 more = value != end || (byte & 0x40) != last;
684 dwarf_data1(s, byte | (0x80 * more));
685 } while (more);
688 static void dwarf_uleb128_op (TCCState *s1, unsigned long long value)
690 do {
691 unsigned char byte = value & 0x7f;
693 value >>= 7;
694 dwarf_line_op(s1, byte | (value ? 0x80 : 0));
695 } while (value != 0);
698 static void dwarf_sleb128_op (TCCState *s1, long long value)
700 int more;
701 long long end = value >> 63;
702 unsigned char last = end & 0x40;
704 do {
705 unsigned char byte = value & 0x7f;
707 value >>= 7;
708 more = value != end || (byte & 0x40) != last;
709 dwarf_line_op(s1, byte | (0x80 * more));
710 } while (more);
713 /* start of translation unit info */
714 ST_FUNC void tcc_debug_start(TCCState *s1)
716 int i;
717 char buf[512];
718 char *filename;
720 /* we might currently #include the <command-line> */
721 filename = file->prev ? file->prev->filename : file->filename;
723 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
724 symbols can be safely used */
725 put_elf_sym(symtab_section, 0, 0,
726 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
727 SHN_ABS, filename);
729 if (s1->do_debug) {
731 new_file = last_line_num = 0;
732 debug_next_type = N_DEFAULT_DEBUG;
733 debug_hash = NULL;
734 debug_anon_hash = NULL;
735 n_debug_hash = 0;
736 n_debug_anon_hash = 0;
738 getcwd(buf, sizeof(buf));
739 #ifdef _WIN32
740 normalize_slashes(buf);
741 #endif
743 if (s1->dwarf) {
744 int start_abbrev;
745 unsigned char *ptr;
746 char *undo;
748 /* dwarf_abbrev */
749 start_abbrev = dwarf_abbrev_section->data_offset;
750 ptr = section_ptr_add(dwarf_abbrev_section, sizeof(dwarf_abbrev_init));
751 memcpy(ptr, dwarf_abbrev_init, sizeof(dwarf_abbrev_init));
753 if (s1->dwarf < 5) {
754 while (*ptr) {
755 ptr += 3;
756 while (*ptr) {
757 if (ptr[1] == DW_FORM_line_strp)
758 ptr[1] = DW_FORM_strp;
759 if (s1->dwarf < 4) {
760 /* These are compatable for DW_TAG_compile_unit
761 DW_AT_stmt_list. */
762 if (ptr[1] == DW_FORM_sec_offset)
763 ptr[1] = DW_FORM_data4;
764 /* This code uses only size < 0x80 so these are
765 compatible. */
766 if (ptr[1] == DW_FORM_exprloc)
767 ptr[1] = DW_FORM_block1;
769 ptr += 2;
771 ptr += 2;
775 dwarf_sym.info = dwarf_get_section_sym(dwarf_info_section);
776 dwarf_sym.abbrev = dwarf_get_section_sym(dwarf_abbrev_section);
777 dwarf_sym.line = dwarf_get_section_sym(dwarf_line_section);
778 dwarf_sym.str = dwarf_get_section_sym(dwarf_str_section);
779 if (tcc_state->dwarf >= 5)
780 dwarf_sym.line_str = dwarf_get_section_sym(dwarf_line_str_section);
781 else {
782 dwarf_line_str_section = dwarf_str_section;
783 dwarf_sym.line_str = dwarf_sym.str;
785 section_sym = dwarf_get_section_sym(text_section);
787 /* dwarf_info */
788 dwarf_info.start = dwarf_info_section->data_offset;
789 dwarf_data4(dwarf_info_section, 0); // size
790 dwarf_data2(dwarf_info_section, s1->dwarf); // version
791 if (s1->dwarf >= 5) {
792 dwarf_data1(dwarf_info_section, DW_UT_compile); // unit type
793 dwarf_data1(dwarf_info_section, PTR_SIZE);
794 dwarf_reloc(dwarf_info_section, dwarf_sym.abbrev, R_DATA_32DW);
795 dwarf_data4(dwarf_info_section, start_abbrev);
797 else {
798 dwarf_reloc(dwarf_info_section, dwarf_sym.abbrev, R_DATA_32DW);
799 dwarf_data4(dwarf_info_section, start_abbrev);
800 dwarf_data1(dwarf_info_section, PTR_SIZE);
803 dwarf_data1(dwarf_info_section, DWARF_ABBREV_COMPILE_UNIT);
804 dwarf_strp(dwarf_info_section, "tcc " TCC_VERSION);
805 dwarf_data1(dwarf_info_section, DW_LANG_C11);
806 dwarf_line_strp(dwarf_info_section, filename);
807 dwarf_line_strp(dwarf_info_section, buf);
808 dwarf_reloc(dwarf_info_section, section_sym, R_DATA_PTR);
809 #if PTR_SIZE == 4
810 dwarf_data4(dwarf_info_section, ind); // low pc
811 dwarf_data4(dwarf_info_section, 0); // high pc
812 #else
813 dwarf_data8(dwarf_info_section, ind); // low pc
814 dwarf_data8(dwarf_info_section, 0); // high pc
815 #endif
816 dwarf_reloc(dwarf_info_section, dwarf_sym.line, R_DATA_32DW);
817 dwarf_data4(dwarf_info_section, dwarf_line_section->data_offset); // stmt_list
819 /* dwarf_line */
820 dwarf_line.start = dwarf_line_section->data_offset;
821 dwarf_data4(dwarf_line_section, 0); // length
822 dwarf_data2(dwarf_line_section, s1->dwarf); // version
823 if (s1->dwarf >= 5) {
824 dwarf_data1(dwarf_line_section, PTR_SIZE); // address size
825 dwarf_data1(dwarf_line_section, 0); // segment selector
827 dwarf_data4(dwarf_line_section, 0); // prologue Length
828 dwarf_data1(dwarf_line_section, DWARF_MIN_INSTR_LEN);
829 if (s1->dwarf >= 4)
830 dwarf_data1(dwarf_line_section, 1); // maximum ops per instruction
831 dwarf_data1(dwarf_line_section, 1); // Initial value of 'is_stmt'
832 dwarf_data1(dwarf_line_section, DWARF_LINE_BASE);
833 dwarf_data1(dwarf_line_section, DWARF_LINE_RANGE);
834 dwarf_data1(dwarf_line_section, DWARF_OPCODE_BASE);
835 ptr = section_ptr_add(dwarf_line_section, sizeof(dwarf_line_opcodes));
836 memcpy(ptr, dwarf_line_opcodes, sizeof(dwarf_line_opcodes));
837 undo = strrchr(filename, '/');
838 if (undo)
839 *undo = 0;
840 dwarf_line.dir_size = 1 + (undo != NULL);
841 dwarf_line.dir_table = (char **) tcc_malloc(sizeof (char *) *
842 dwarf_line.dir_size);
843 dwarf_line.dir_table[0] = tcc_strdup(buf);
844 if (undo)
845 dwarf_line.dir_table[1] = tcc_strdup(filename);
846 dwarf_line.filename_size = 2;
847 dwarf_line.filename_table =
848 (struct dwarf_filename_struct *)
849 tcc_malloc(2*sizeof (struct dwarf_filename_struct));
850 dwarf_line.filename_table[0].dir_entry = 0;
851 if (undo) {
852 dwarf_line.filename_table[0].name = tcc_strdup(undo + 1);
853 dwarf_line.filename_table[1].dir_entry = 1;
854 dwarf_line.filename_table[1].name = tcc_strdup(undo + 1);
855 *undo = '/';
857 else {
858 dwarf_line.filename_table[0].name = tcc_strdup(filename);
859 dwarf_line.filename_table[1].dir_entry = 0;
860 dwarf_line.filename_table[1].name = tcc_strdup(filename);
862 dwarf_line.line_size = dwarf_line.line_max_size = 0;
863 dwarf_line.line_data = NULL;
864 dwarf_line.cur_file = 1;
865 dwarf_line.last_file = 0;
866 dwarf_line.last_pc = 0;
867 dwarf_line.last_line = 1;
868 dwarf_line_op(s1, 0); // extended
869 dwarf_uleb128_op(s1, 1 + PTR_SIZE); // extended size
870 dwarf_line_op(s1, DW_LNE_set_address);
871 for (i = 0; i < PTR_SIZE; i++)
872 dwarf_line_op(s1, 0);
873 memset(&dwarf_info.base_type_used, 0, sizeof(dwarf_info.base_type_used));
875 else
877 /* file info: full path + filename */
878 pstrcat(buf, sizeof(buf), "/");
879 section_sym = put_elf_sym(symtab_section, 0, 0,
880 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
881 text_section->sh_num, NULL);
882 put_stabs_r(s1, buf, N_SO, 0, 0,
883 text_section->data_offset, text_section, section_sym);
884 put_stabs_r(s1, filename, N_SO, 0, 0,
885 text_section->data_offset, text_section, section_sym);
886 for (i = 0; i < N_DEFAULT_DEBUG; i++)
887 put_stabs(s1, default_debug[i].name, N_LSYM, 0, 0, 0);
889 /* we're currently 'including' the <command line> */
890 tcc_debug_bincl(s1);
894 /* put end of translation unit info */
895 ST_FUNC void tcc_debug_end(TCCState *s1)
897 if (!s1->do_debug || debug_next_type == 0)
898 return;
900 if (debug_info_root)
901 tcc_debug_funcend(s1, 0); /* free stuff in case of errors */
903 if (s1->dwarf) {
904 int i, j;
905 int start_aranges;
906 unsigned char *ptr;
907 int text_size = text_section->data_offset;
909 /* dwarf_info */
910 for (i = 0; i < n_debug_anon_hash; i++) {
911 Sym *t = debug_anon_hash[i].type;
912 int pos = dwarf_info_section->data_offset;
914 dwarf_data1(dwarf_info_section,
915 IS_UNION (t->type.t) ? DWARF_ABBREV_UNION_EMPTY_TYPE
916 : DWARF_ABBREV_STRUCTURE_EMPTY_TYPE);
917 dwarf_strp(dwarf_info_section,
918 (t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
919 ? "" : get_tok_str(t->v, NULL));
920 dwarf_uleb128(dwarf_info_section, 0);
921 dwarf_uleb128(dwarf_info_section, dwarf_line.cur_file);
922 dwarf_uleb128(dwarf_info_section, file->line_num);
923 for (j = 0; j < debug_anon_hash[i].n_debug_type; j++)
924 write32le(dwarf_info_section->data +
925 debug_anon_hash[i].debug_type[j],
926 pos - dwarf_info.start);
927 tcc_free (debug_anon_hash[i].debug_type);
929 tcc_free (debug_anon_hash);
930 dwarf_data1(dwarf_info_section, 0);
931 ptr = dwarf_info_section->data + dwarf_info.start;
932 write32le(ptr, dwarf_info_section->data_offset - dwarf_info.start - 4);
933 write32le(ptr + 25 + (s1->dwarf >= 5) + PTR_SIZE, text_size);
935 /* dwarf_aranges */
936 start_aranges = dwarf_aranges_section->data_offset;
937 dwarf_data4(dwarf_aranges_section, 0); // size
938 dwarf_data2(dwarf_aranges_section, 2); // version
939 dwarf_reloc(dwarf_aranges_section, dwarf_sym.info, R_DATA_32DW);
940 dwarf_data4(dwarf_aranges_section, 0); // dwarf_info
941 #if PTR_SIZE == 4
942 dwarf_data1(dwarf_aranges_section, 4); // address size
943 #else
944 dwarf_data1(dwarf_aranges_section, 8); // address size
945 #endif
946 dwarf_data1(dwarf_aranges_section, 0); // segment selector size
947 dwarf_data4(dwarf_aranges_section, 0); // padding
948 dwarf_reloc(dwarf_aranges_section, section_sym, R_DATA_PTR);
949 #if PTR_SIZE == 4
950 dwarf_data4(dwarf_aranges_section, 0); // Begin
951 dwarf_data4(dwarf_aranges_section, text_size); // End
952 dwarf_data4(dwarf_aranges_section, 0); // End list
953 dwarf_data4(dwarf_aranges_section, 0); // End list
954 #else
955 dwarf_data8(dwarf_aranges_section, 0); // Begin
956 dwarf_data8(dwarf_aranges_section, text_size); // End
957 dwarf_data8(dwarf_aranges_section, 0); // End list
958 dwarf_data8(dwarf_aranges_section, 0); // End list
959 #endif
960 ptr = dwarf_aranges_section->data + start_aranges;
961 write32le(ptr, dwarf_aranges_section->data_offset - start_aranges - 4);
963 /* dwarf_line */
964 if (s1->dwarf >= 5) {
965 dwarf_data1(dwarf_line_section, 1); /* col */
966 dwarf_uleb128(dwarf_line_section, DW_LNCT_path);
967 dwarf_uleb128(dwarf_line_section, DW_FORM_line_strp);
968 dwarf_uleb128(dwarf_line_section, dwarf_line.dir_size);
969 for (i = 0; i < dwarf_line.dir_size; i++)
970 dwarf_line_strp(dwarf_line_section, dwarf_line.dir_table[i]);
971 dwarf_data1(dwarf_line_section, 2); /* col */
972 dwarf_uleb128(dwarf_line_section, DW_LNCT_path);
973 dwarf_uleb128(dwarf_line_section, DW_FORM_line_strp);
974 dwarf_uleb128(dwarf_line_section, DW_LNCT_directory_index);
975 dwarf_uleb128(dwarf_line_section, DW_FORM_udata);
976 dwarf_uleb128(dwarf_line_section, dwarf_line.filename_size);
977 for (i = 0; i < dwarf_line.filename_size; i++) {
978 dwarf_line_strp(dwarf_line_section,
979 dwarf_line.filename_table[i].name);
980 dwarf_uleb128(dwarf_line_section,
981 dwarf_line.filename_table[i].dir_entry);
984 else {
985 int len;
987 for (i = 0; i < dwarf_line.dir_size; i++) {
988 len = strlen(dwarf_line.dir_table[i]) + 1;
989 ptr = section_ptr_add(dwarf_line_section, len);
990 memmove(ptr, dwarf_line.dir_table[i], len);
992 dwarf_data1(dwarf_line_section, 0); /* end dir */
993 for (i = 0; i < dwarf_line.filename_size; i++) {
994 len = strlen(dwarf_line.filename_table[i].name) + 1;
995 ptr = section_ptr_add(dwarf_line_section, len);
996 memmove(ptr, dwarf_line.filename_table[i].name, len);
997 dwarf_uleb128(dwarf_line_section,
998 dwarf_line.filename_table[i].dir_entry);
999 dwarf_uleb128(dwarf_line_section, 0); /* time */
1000 dwarf_uleb128(dwarf_line_section, 0); /* size */
1002 dwarf_data1(dwarf_line_section, 0); /* end file */
1004 for (i = 0; i < dwarf_line.dir_size; i++)
1005 tcc_free(dwarf_line.dir_table[i]);
1006 tcc_free(dwarf_line.dir_table);
1007 for (i = 0; i < dwarf_line.filename_size; i++)
1008 tcc_free(dwarf_line.filename_table[i].name);
1009 tcc_free(dwarf_line.filename_table);
1011 dwarf_line_op(s1, 0); // extended
1012 dwarf_uleb128_op(s1, 1); // extended size
1013 dwarf_line_op(s1, DW_LNE_end_sequence);
1014 i = (s1->dwarf >= 5) * 2;
1015 write32le(&dwarf_line_section->data[dwarf_line.start + 6 + i],
1016 dwarf_line_section->data_offset - dwarf_line.start - (10 + i));
1017 section_ptr_add(dwarf_line_section, 3);
1018 dwarf_reloc(dwarf_line_section, section_sym, R_DATA_PTR);
1019 ptr = section_ptr_add(dwarf_line_section, dwarf_line.line_size - 3);
1020 memmove(ptr - 3, dwarf_line.line_data, dwarf_line.line_size);
1021 tcc_free(dwarf_line.line_data);
1022 write32le(dwarf_line_section->data + dwarf_line.start,
1023 dwarf_line_section->data_offset - dwarf_line.start - 4);
1025 else
1027 put_stabs_r(s1, NULL, N_SO, 0, 0,
1028 text_section->data_offset, text_section, section_sym);
1030 tcc_free(debug_hash);
1031 debug_next_type = 0;
1034 static BufferedFile* put_new_file(TCCState *s1)
1036 BufferedFile *f = file;
1037 /* use upper file if from inline ":asm:" */
1038 if (f->filename[0] == ':')
1039 f = f->prev;
1040 if (f && new_file) {
1041 new_file = last_line_num = 0;
1042 if (s1->dwarf)
1043 dwarf_file(s1);
1044 else
1045 put_stabs_r(s1, f->filename, N_SOL, 0, 0, ind, text_section, section_sym);
1047 return f;
1050 /* put alternative filename */
1051 ST_FUNC void tcc_debug_putfile(TCCState *s1, const char *filename)
1053 if (0 == strcmp(file->filename, filename))
1054 return;
1055 pstrcpy(file->filename, sizeof(file->filename), filename);
1056 if (!s1->do_debug)
1057 return;
1058 if (s1->dwarf)
1059 dwarf_file(s1);
1060 new_file = 1;
1063 /* begin of #include */
1064 ST_FUNC void tcc_debug_bincl(TCCState *s1)
1066 if (!s1->do_debug)
1067 return;
1068 if (s1->dwarf)
1069 dwarf_file(s1);
1070 else
1071 put_stabs(s1, file->filename, N_BINCL, 0, 0, 0);
1072 new_file = 1;
1075 /* end of #include */
1076 ST_FUNC void tcc_debug_eincl(TCCState *s1)
1078 if (!s1->do_debug)
1079 return;
1080 if (s1->dwarf)
1081 dwarf_file(s1);
1082 else
1083 put_stabn(s1, N_EINCL, 0, 0, 0);
1084 new_file = 1;
1087 /* generate line number info */
1088 ST_FUNC void tcc_debug_line(TCCState *s1)
1090 BufferedFile *f;
1092 if (!s1->do_debug)
1093 return;
1094 if (cur_text_section != text_section || nocode_wanted)
1095 return;
1096 f = put_new_file(s1);
1097 if (!f)
1098 return;
1099 if (last_line_num == f->line_num)
1100 return;
1101 last_line_num = f->line_num;
1103 if (s1->dwarf) {
1104 int len_pc = (ind - dwarf_line.last_pc) / DWARF_MIN_INSTR_LEN;
1105 int len_line = f->line_num - dwarf_line.last_line;
1106 int n = len_pc * DWARF_LINE_RANGE + len_line + DWARF_OPCODE_BASE - DWARF_LINE_BASE;
1108 if (dwarf_line.cur_file != dwarf_line.last_file) {
1109 dwarf_line.last_file = dwarf_line.cur_file;
1110 dwarf_line_op(s1, DW_LNS_set_file);
1111 dwarf_uleb128_op(s1, dwarf_line.cur_file);
1113 if (len_pc &&
1114 len_line >= DWARF_LINE_BASE && len_line <= (DWARF_OPCODE_BASE + DWARF_LINE_BASE) &&
1115 n >= DWARF_OPCODE_BASE && n <= 255)
1116 dwarf_line_op(s1, n);
1117 else {
1118 if (len_pc) {
1119 n = len_pc * DWARF_LINE_RANGE + 0 + DWARF_OPCODE_BASE - DWARF_LINE_BASE;
1120 if (n >= DWARF_OPCODE_BASE && n <= 255)
1121 dwarf_line_op(s1, n);
1122 else {
1123 dwarf_line_op(s1, DW_LNS_advance_pc);
1124 dwarf_uleb128_op(s1, len_pc);
1127 if (len_line) {
1128 n = 0 * DWARF_LINE_RANGE + len_line + DWARF_OPCODE_BASE - DWARF_LINE_BASE;
1129 if (len_line >= DWARF_LINE_BASE && len_line <= (DWARF_OPCODE_BASE + DWARF_LINE_BASE) &&
1130 n >= DWARF_OPCODE_BASE && n <= 255)
1131 dwarf_line_op(s1, n);
1132 else {
1133 dwarf_line_op(s1, DW_LNS_advance_line);
1134 dwarf_sleb128_op(s1, len_line);
1138 dwarf_line.last_pc = ind;
1139 dwarf_line.last_line = f->line_num;
1141 else
1143 if (func_ind != -1) {
1144 put_stabn(s1, N_SLINE, 0, f->line_num, ind - func_ind);
1145 } else {
1146 /* from tcc_assemble */
1147 put_stabs_r(s1, NULL, N_SLINE, 0, f->line_num, ind, text_section, section_sym);
1152 static void tcc_debug_stabs (TCCState *s1, const char *str, int type, unsigned long value,
1153 Section *sec, int sym_index, int info)
1155 struct debug_sym *s;
1157 if (debug_info) {
1158 debug_info->sym =
1159 (struct debug_sym *)tcc_realloc (debug_info->sym,
1160 sizeof(struct debug_sym) *
1161 (debug_info->n_sym + 1));
1162 s = debug_info->sym + debug_info->n_sym++;
1163 s->type = type;
1164 s->value = value;
1165 s->str = tcc_strdup(str);
1166 s->sec = sec;
1167 s->sym_index = sym_index;
1168 s->info = info;
1169 s->file = dwarf_line.cur_file;
1170 s->line = file->line_num;
1172 else if (sec)
1173 put_stabs_r (s1, str, type, 0, 0, value, sec, sym_index);
1174 else
1175 put_stabs (s1, str, type, 0, 0, value);
1178 ST_FUNC void tcc_debug_stabn(TCCState *s1, int type, int value)
1180 if (!s1->do_debug)
1181 return;
1182 if (type == N_LBRAC) {
1183 struct _debug_info *info =
1184 (struct _debug_info *) tcc_mallocz(sizeof (*info));
1186 info->start = value;
1187 info->parent = debug_info;
1188 if (debug_info) {
1189 if (debug_info->child) {
1190 if (debug_info->child->last)
1191 debug_info->child->last->next = info;
1192 else
1193 debug_info->child->next = info;
1194 debug_info->child->last = info;
1196 else
1197 debug_info->child = info;
1199 else
1200 debug_info_root = info;
1201 debug_info = info;
1203 else {
1204 debug_info->end = value;
1205 debug_info = debug_info->parent;
1209 static int tcc_debug_find(TCCState *s1, Sym *t, int dwarf)
1211 int i;
1213 if (!debug_info && dwarf &&
1214 (t->type.t & VT_BTYPE) == VT_STRUCT && t->c == -1) {
1215 for (i = 0; i < n_debug_anon_hash; i++)
1216 if (t == debug_anon_hash[i].type)
1217 return 0;
1218 debug_anon_hash = (struct _debug_anon_hash *)
1219 tcc_realloc (debug_anon_hash,
1220 (n_debug_anon_hash + 1) * sizeof(*debug_anon_hash));
1221 debug_anon_hash[n_debug_anon_hash].n_debug_type = 0;
1222 debug_anon_hash[n_debug_anon_hash].debug_type = NULL;
1223 debug_anon_hash[n_debug_anon_hash++].type = t;
1224 return 0;
1226 for (i = 0; i < n_debug_hash; i++)
1227 if (t == debug_hash[i].type)
1228 return debug_hash[i].debug_type;
1229 return -1;
1232 static int tcc_get_dwarf_info(TCCState *s1, Sym *s);
1234 static void tcc_debug_check_anon(TCCState *s1, Sym *t, int debug_type)
1236 int i;
1238 if (!debug_info && (t->type.t & VT_BTYPE) == VT_STRUCT && t->type.ref->c == -1)
1239 for (i = 0; i < n_debug_anon_hash; i++)
1240 if (t->type.ref == debug_anon_hash[i].type) {
1241 debug_anon_hash[i].debug_type =
1242 tcc_realloc(debug_anon_hash[i].debug_type,
1243 (debug_anon_hash[i].n_debug_type + 1) * sizeof(int));
1244 debug_anon_hash[i].debug_type[debug_anon_hash[i].n_debug_type++] =
1245 debug_type;
1249 ST_FUNC void tcc_debug_fix_anon(TCCState *s1, CType *t)
1251 int i, j, debug_type;
1253 if (!(s1->do_debug & 2) || !s1->dwarf || debug_info)
1254 return;
1256 if ((t->t & VT_BTYPE) == VT_STRUCT && t->ref->c != -1)
1257 for (i = 0; i < n_debug_anon_hash; i++)
1258 if (t->ref == debug_anon_hash[i].type) {
1259 Sym sym = {0}; sym .type = *t ;
1261 /* Trick to not hash this struct */
1262 debug_info = (struct _debug_info *) t;
1263 debug_type = tcc_get_dwarf_info(s1, &sym);
1264 debug_info = NULL;
1265 for (j = 0; j < debug_anon_hash[i].n_debug_type; j++)
1266 write32le(dwarf_info_section->data +
1267 debug_anon_hash[i].debug_type[j],
1268 debug_type - dwarf_info.start);
1269 tcc_free(debug_anon_hash[i].debug_type);
1270 n_debug_anon_hash--;
1271 for (; i < n_debug_anon_hash; i++)
1272 debug_anon_hash[i] = debug_anon_hash[i + 1];
1276 static int tcc_debug_add(TCCState *s1, Sym *t, int dwarf)
1278 int offset = dwarf ? dwarf_info_section->data_offset : ++debug_next_type;
1279 debug_hash = (struct _debug_hash *)
1280 tcc_realloc (debug_hash,
1281 (n_debug_hash + 1) * sizeof(*debug_hash));
1282 debug_hash[n_debug_hash].debug_type = offset;
1283 debug_hash[n_debug_hash++].type = t;
1284 return offset;
1287 static void tcc_debug_remove(TCCState *s1, Sym *t)
1289 int i;
1291 for (i = 0; i < n_debug_hash; i++)
1292 if (t == debug_hash[i].type) {
1293 n_debug_hash--;
1294 for (; i < n_debug_hash; i++)
1295 debug_hash[i] = debug_hash[i+1];
1299 #define STRUCT_NODEBUG(s) \
1300 (s->a.nodebug || \
1301 ((s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM && \
1302 ((s->type.t & VT_BTYPE) == VT_BYTE || \
1303 (s->type.t & VT_BTYPE) == VT_BOOL || \
1304 (s->type.t & VT_BTYPE) == VT_SHORT || \
1305 (s->type.t & VT_BTYPE) == VT_INT || \
1306 (s->type.t & VT_BTYPE) == VT_LLONG)))
1308 static void tcc_get_debug_info(TCCState *s1, Sym *s, CString *result)
1310 int type;
1311 int n = 0;
1312 int debug_type = -1;
1313 Sym *t = s;
1314 CString str;
1316 for (;;) {
1317 type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA);
1318 if ((type & VT_BTYPE) != VT_BYTE)
1319 type &= ~VT_DEFSIGN;
1320 if (type == VT_PTR || type == (VT_PTR | VT_ARRAY))
1321 n++, t = t->type.ref;
1322 else
1323 break;
1325 if ((type & VT_BTYPE) == VT_STRUCT) {
1326 Sym *e = t;
1328 t = t->type.ref;
1329 debug_type = tcc_debug_find(s1, t, 0);
1330 if (debug_type == -1) {
1331 debug_type = tcc_debug_add(s1, t, 0);
1332 cstr_new (&str);
1333 cstr_printf (&str, "%s:T%d=%c%d",
1334 (t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
1335 ? "" : get_tok_str(t->v, NULL),
1336 debug_type,
1337 IS_UNION (t->type.t) ? 'u' : 's',
1338 t->c);
1339 while (t->next) {
1340 int pos, size, align;
1342 t = t->next;
1343 if (STRUCT_NODEBUG(t))
1344 continue;
1345 cstr_printf (&str, "%s:",
1346 get_tok_str(t->v, NULL));
1347 tcc_get_debug_info (s1, t, &str);
1348 if (t->type.t & VT_BITFIELD) {
1349 pos = t->c * 8 + BIT_POS(t->type.t);
1350 size = BIT_SIZE(t->type.t);
1352 else {
1353 pos = t->c * 8;
1354 size = type_size(&t->type, &align) * 8;
1356 cstr_printf (&str, ",%d,%d;", pos, size);
1358 cstr_printf (&str, ";");
1359 tcc_debug_stabs(s1, str.data, N_LSYM, 0, NULL, 0, 0);
1360 cstr_free (&str);
1361 if (debug_info)
1362 tcc_debug_remove(s1, e);
1365 else if (IS_ENUM(type)) {
1366 Sym *e = t = t->type.ref;
1368 debug_type = tcc_debug_find(s1, t, 0);
1369 if (debug_type == -1) {
1370 debug_type = tcc_debug_add(s1, t, 0);
1371 cstr_new (&str);
1372 cstr_printf (&str, "%s:T%d=e",
1373 (t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
1374 ? "" : get_tok_str(t->v, NULL),
1375 debug_type);
1376 while (t->next) {
1377 t = t->next;
1378 cstr_printf (&str, "%s:",
1379 (t->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
1380 ? "" : get_tok_str(t->v, NULL));
1381 cstr_printf (&str, e->type.t & VT_UNSIGNED ? "%u," : "%d,",
1382 (int)t->enum_val);
1384 cstr_printf (&str, ";");
1385 tcc_debug_stabs(s1, str.data, N_LSYM, 0, NULL, 0, 0);
1386 cstr_free (&str);
1387 if (debug_info)
1388 tcc_debug_remove(s1, e);
1391 else if ((type & VT_BTYPE) != VT_FUNC) {
1392 type &= ~VT_STRUCT_MASK;
1393 for (debug_type = 1; debug_type <= N_DEFAULT_DEBUG; debug_type++)
1394 if (default_debug[debug_type - 1].type == type)
1395 break;
1396 if (debug_type > N_DEFAULT_DEBUG)
1397 return;
1399 if (n > 0)
1400 cstr_printf (result, "%d=", ++debug_next_type);
1401 t = s;
1402 for (;;) {
1403 type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA);
1404 if ((type & VT_BTYPE) != VT_BYTE)
1405 type &= ~VT_DEFSIGN;
1406 if (type == VT_PTR)
1407 cstr_printf (result, "%d=*", ++debug_next_type);
1408 else if (type == (VT_PTR | VT_ARRAY))
1409 cstr_printf (result, "%d=ar1;0;%d;",
1410 ++debug_next_type, t->type.ref->c - 1);
1411 else if (type == VT_FUNC) {
1412 cstr_printf (result, "%d=f", ++debug_next_type);
1413 tcc_get_debug_info (s1, t->type.ref, result);
1414 return;
1416 else
1417 break;
1418 t = t->type.ref;
1420 cstr_printf (result, "%d", debug_type);
1423 static int tcc_get_dwarf_info(TCCState *s1, Sym *s)
1425 int type;
1426 int debug_type = -1;
1427 Sym *e, *t = s;
1428 int i;
1429 int last_pos = -1;
1430 int retval;
1432 if (new_file)
1433 put_new_file(s1);
1434 for (;;) {
1435 type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA);
1436 if ((type & VT_BTYPE) != VT_BYTE)
1437 type &= ~VT_DEFSIGN;
1438 if (type == VT_PTR || type == (VT_PTR | VT_ARRAY))
1439 t = t->type.ref;
1440 else
1441 break;
1443 if ((type & VT_BTYPE) == VT_STRUCT) {
1444 t = t->type.ref;
1445 debug_type = tcc_debug_find(s1, t, 1);
1446 if (debug_type == -1) {
1447 int pos_sib = 0, i, *pos_type;
1449 debug_type = tcc_debug_add(s1, t, 1);
1450 e = t;
1451 i = 0;
1452 while (e->next) {
1453 e = e->next;
1454 if (STRUCT_NODEBUG(e))
1455 continue;
1456 i++;
1458 pos_type = (int *) tcc_malloc(i * sizeof(int));
1459 dwarf_data1(dwarf_info_section,
1460 IS_UNION (t->type.t)
1461 ? t->next ? DWARF_ABBREV_UNION_TYPE
1462 : DWARF_ABBREV_UNION_EMPTY_TYPE
1463 : t->next ? DWARF_ABBREV_STRUCTURE_TYPE
1464 : DWARF_ABBREV_STRUCTURE_EMPTY_TYPE);
1465 dwarf_strp(dwarf_info_section,
1466 (t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
1467 ? "" : get_tok_str(t->v, NULL));
1468 dwarf_uleb128(dwarf_info_section, t->c);
1469 dwarf_uleb128(dwarf_info_section, dwarf_line.cur_file);
1470 dwarf_uleb128(dwarf_info_section, file->line_num);
1471 if (t->next) {
1472 pos_sib = dwarf_info_section->data_offset;
1473 dwarf_data4(dwarf_info_section, 0);
1475 e = t;
1476 i = 0;
1477 while (e->next) {
1478 e = e->next;
1479 if (STRUCT_NODEBUG(e))
1480 continue;
1481 dwarf_data1(dwarf_info_section,
1482 e->type.t & VT_BITFIELD ? DWARF_ABBREV_MEMBER_BF
1483 : DWARF_ABBREV_MEMBER);
1484 dwarf_strp(dwarf_info_section,
1485 get_tok_str(e->v, NULL));
1486 dwarf_uleb128(dwarf_info_section, dwarf_line.cur_file);
1487 dwarf_uleb128(dwarf_info_section, file->line_num);
1488 pos_type[i++] = dwarf_info_section->data_offset;
1489 dwarf_data4(dwarf_info_section, 0);
1490 if (e->type.t & VT_BITFIELD) {
1491 int pos = e->c * 8 + BIT_POS(e->type.t);
1492 int size = BIT_SIZE(e->type.t);
1494 dwarf_uleb128(dwarf_info_section, size);
1495 dwarf_uleb128(dwarf_info_section, pos);
1497 else
1498 dwarf_uleb128(dwarf_info_section, e->c);
1500 if (t->next) {
1501 dwarf_data1(dwarf_info_section, 0);
1502 write32le(dwarf_info_section->data + pos_sib,
1503 dwarf_info_section->data_offset - dwarf_info.start);
1505 e = t;
1506 i = 0;
1507 while (e->next) {
1508 e = e->next;
1509 if (STRUCT_NODEBUG(e))
1510 continue;
1511 type = tcc_get_dwarf_info(s1, e);
1512 tcc_debug_check_anon(s1, e, pos_type[i]);
1513 write32le(dwarf_info_section->data + pos_type[i++],
1514 type - dwarf_info.start);
1516 tcc_free(pos_type);
1517 if (debug_info)
1518 tcc_debug_remove(s1, t);
1521 else if (IS_ENUM(type)) {
1522 t = t->type.ref;
1523 debug_type = tcc_debug_find(s1, t, 1);
1524 if (debug_type == -1) {
1525 int pos_sib, pos_type;
1526 Sym sym = {0}; sym.type.t = VT_INT | (type & VT_UNSIGNED);
1528 pos_type = tcc_get_dwarf_info(s1, &sym);
1529 debug_type = tcc_debug_add(s1, t, 1);
1530 dwarf_data1(dwarf_info_section, DWARF_ABBREV_ENUMERATION_TYPE);
1531 dwarf_strp(dwarf_info_section,
1532 (t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
1533 ? "" : get_tok_str(t->v, NULL));
1534 dwarf_data1(dwarf_info_section,
1535 type & VT_UNSIGNED ? DW_ATE_unsigned : DW_ATE_signed );
1536 dwarf_data1(dwarf_info_section, 4);
1537 dwarf_data4(dwarf_info_section, pos_type - dwarf_info.start);
1538 dwarf_uleb128(dwarf_info_section, dwarf_line.cur_file);
1539 dwarf_uleb128(dwarf_info_section, file->line_num);
1540 pos_sib = dwarf_info_section->data_offset;
1541 dwarf_data4(dwarf_info_section, 0);
1542 e = t;
1543 while (e->next) {
1544 e = e->next;
1545 dwarf_data1(dwarf_info_section,
1546 type & VT_UNSIGNED ? DWARF_ABBREV_ENUMERATOR_UNSIGNED
1547 : DWARF_ABBREV_ENUMERATOR_SIGNED);
1548 dwarf_strp(dwarf_info_section,
1549 (e->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
1550 ? "" : get_tok_str(e->v, NULL));
1551 if (type & VT_UNSIGNED)
1552 dwarf_uleb128(dwarf_info_section, e->enum_val);
1553 else
1554 dwarf_sleb128(dwarf_info_section, e->enum_val);
1556 dwarf_data1(dwarf_info_section, 0);
1557 write32le(dwarf_info_section->data + pos_sib,
1558 dwarf_info_section->data_offset - dwarf_info.start);
1559 if (debug_info)
1560 tcc_debug_remove(s1, t);
1563 else if ((type & VT_BTYPE) != VT_FUNC) {
1564 type &= ~VT_STRUCT_MASK;
1565 for (i = 1; i <= N_DEFAULT_DEBUG; i++)
1566 if (default_debug[i - 1].type == type)
1567 break;
1568 if (i > N_DEFAULT_DEBUG)
1569 return 0;
1570 debug_type = dwarf_info.base_type_used[i - 1];
1571 if (debug_type == 0) {
1572 char name[100];
1574 debug_type = dwarf_info_section->data_offset;
1575 dwarf_data1(dwarf_info_section, DWARF_ABBREV_BASE_TYPE);
1576 dwarf_uleb128(dwarf_info_section, default_debug[i - 1].size);
1577 dwarf_data1(dwarf_info_section, default_debug[i - 1].encoding);
1578 strncpy(name, default_debug[i - 1].name, sizeof(name) -1);
1579 *strchr(name, ':') = 0;
1580 dwarf_strp(dwarf_info_section, name);
1581 dwarf_info.base_type_used[i - 1] = debug_type;
1584 retval = debug_type;
1585 e = NULL;
1586 t = s;
1587 for (;;) {
1588 type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA);
1589 if ((type & VT_BTYPE) != VT_BYTE)
1590 type &= ~VT_DEFSIGN;
1591 if (type == VT_PTR) {
1592 i = dwarf_info_section->data_offset;
1593 if (retval == debug_type)
1594 retval = i;
1595 dwarf_data1(dwarf_info_section, DWARF_ABBREV_POINTER);
1596 dwarf_data1(dwarf_info_section, PTR_SIZE);
1597 if (last_pos != -1) {
1598 tcc_debug_check_anon(s1, e, last_pos);
1599 write32le(dwarf_info_section->data + last_pos,
1600 i - dwarf_info.start);
1602 last_pos = dwarf_info_section->data_offset;
1603 e = t->type.ref;
1604 dwarf_data4(dwarf_info_section, 0);
1606 else if (type == (VT_PTR | VT_ARRAY)) {
1607 int sib_pos, sub_type;
1608 #if LONG_SIZE == 4
1609 Sym sym = {0}; sym.type.t = VT_LONG | VT_INT | VT_UNSIGNED;
1610 #else
1611 Sym sym = {0}; sym.type.t = VT_LLONG | VT_LONG | VT_UNSIGNED;
1612 #endif
1614 sub_type = tcc_get_dwarf_info(s1, &sym);
1615 i = dwarf_info_section->data_offset;
1616 if (retval == debug_type)
1617 retval = i;
1618 dwarf_data1(dwarf_info_section, DWARF_ABBREV_ARRAY_TYPE);
1619 if (last_pos != -1) {
1620 tcc_debug_check_anon(s1, e, last_pos);
1621 write32le(dwarf_info_section->data + last_pos,
1622 i - dwarf_info.start);
1624 last_pos = dwarf_info_section->data_offset;
1625 e = t->type.ref;
1626 dwarf_data4(dwarf_info_section, 0);
1627 sib_pos = dwarf_info_section->data_offset;
1628 dwarf_data4(dwarf_info_section, 0);
1629 for (;;) {
1630 dwarf_data1(dwarf_info_section, DWARF_ABBREV_SUBRANGE_TYPE);
1631 dwarf_data4(dwarf_info_section, sub_type - dwarf_info.start);
1632 dwarf_uleb128(dwarf_info_section, t->type.ref->c - 1);
1633 s = t->type.ref;
1634 type = s->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE);
1635 if (type != (VT_PTR | VT_ARRAY))
1636 break;
1637 t = s;
1639 dwarf_data1(dwarf_info_section, 0);
1640 write32le(dwarf_info_section->data + sib_pos,
1641 dwarf_info_section->data_offset - dwarf_info.start);
1643 else if (type == VT_FUNC) {
1644 int sib_pos = 0, *pos_type;
1645 Sym *f;
1647 i = dwarf_info_section->data_offset;
1648 debug_type = tcc_get_dwarf_info(s1, t->type.ref);
1649 if (retval == debug_type)
1650 retval = i;
1651 dwarf_data1(dwarf_info_section,
1652 t->type.ref->next ? DWARF_ABBREV_SUBROUTINE_TYPE
1653 : DWARF_ABBREV_SUBROUTINE_EMPTY_TYPE);
1654 if (last_pos != -1) {
1655 tcc_debug_check_anon(s1, e, last_pos);
1656 write32le(dwarf_info_section->data + last_pos,
1657 i - dwarf_info.start);
1659 last_pos = dwarf_info_section->data_offset;
1660 e = t->type.ref;
1661 dwarf_data4(dwarf_info_section, 0);
1662 if (t->type.ref->next) {
1663 sib_pos = dwarf_info_section->data_offset;
1664 dwarf_data4(dwarf_info_section, 0);
1666 f = t->type.ref;
1667 i = 0;
1668 while (f->next) {
1669 f = f->next;
1670 i++;
1672 pos_type = (int *) tcc_malloc(i * sizeof(int));
1673 f = t->type.ref;
1674 i = 0;
1675 while (f->next) {
1676 f = f->next;
1677 dwarf_data1(dwarf_info_section, DWARF_ABBREV_FORMAL_PARAMETER2);
1678 pos_type[i++] = dwarf_info_section->data_offset;
1679 dwarf_data4(dwarf_info_section, 0);
1681 if (t->type.ref->next) {
1682 dwarf_data1(dwarf_info_section, 0);
1683 write32le(dwarf_info_section->data + sib_pos,
1684 dwarf_info_section->data_offset - dwarf_info.start);
1686 f = t->type.ref;
1687 i = 0;
1688 while (f->next) {
1689 f = f->next;
1690 type = tcc_get_dwarf_info(s1, f);
1691 tcc_debug_check_anon(s1, f, pos_type[i]);
1692 write32le(dwarf_info_section->data + pos_type[i++],
1693 type - dwarf_info.start);
1695 tcc_free(pos_type);
1697 else {
1698 if (last_pos != -1) {
1699 tcc_debug_check_anon(s1, e, last_pos);
1700 write32le(dwarf_info_section->data + last_pos,
1701 debug_type - dwarf_info.start);
1703 break;
1705 t = t->type.ref;
1707 return retval;
1710 static void tcc_debug_finish (TCCState *s1, struct _debug_info *cur)
1712 while (cur) {
1713 struct _debug_info *next = cur->next;
1714 int i;
1716 if (s1->dwarf) {
1718 for (i = cur->n_sym - 1; i >= 0; i--) {
1719 struct debug_sym *s = &cur->sym[i];
1721 dwarf_data1(dwarf_info_section,
1722 s->type == N_PSYM
1723 ? DWARF_ABBREV_FORMAL_PARAMETER
1724 : s->type == N_GSYM
1725 ? DWARF_ABBREV_VARIABLE_EXTERNAL
1726 : s->type == N_STSYM
1727 ? DWARF_ABBREV_VARIABLE_STATIC
1728 : DWARF_ABBREV_VARIABLE_LOCAL);
1729 dwarf_strp(dwarf_info_section, s->str);
1730 if (s->type == N_GSYM || s->type == N_STSYM) {
1731 dwarf_uleb128(dwarf_info_section, s->file);
1732 dwarf_uleb128(dwarf_info_section, s->line);
1734 dwarf_data4(dwarf_info_section, s->info - dwarf_info.start);
1735 if (s->type == N_GSYM || s->type == N_STSYM) {
1736 /* global/static */
1737 if (s->type == N_GSYM)
1738 dwarf_data1(dwarf_info_section, 1);
1739 dwarf_data1(dwarf_info_section, PTR_SIZE + 1);
1740 dwarf_data1(dwarf_info_section, DW_OP_addr);
1741 if (s->type == N_STSYM)
1742 dwarf_reloc(dwarf_info_section, section_sym, R_DATA_PTR);
1743 #if PTR_SIZE == 4
1744 dwarf_data4(dwarf_info_section, s->value);
1745 #else
1746 dwarf_data8(dwarf_info_section, s->value);
1747 #endif
1749 else {
1750 /* param/local */
1751 dwarf_data1(dwarf_info_section, dwarf_sleb128_size(s->value) + 1);
1752 dwarf_data1(dwarf_info_section, DW_OP_fbreg);
1753 dwarf_sleb128(dwarf_info_section, s->value);
1755 tcc_free (s->str);
1757 tcc_free (cur->sym);
1758 dwarf_data1(dwarf_info_section,
1759 cur->child ? DWARF_ABBREV_LEXICAL_BLOCK
1760 : DWARF_ABBREV_LEXICAL_EMPTY_BLOCK);
1761 dwarf_reloc(dwarf_info_section, section_sym, R_DATA_PTR);
1762 #if PTR_SIZE == 4
1763 dwarf_data4(dwarf_info_section, func_ind + cur->start);
1764 dwarf_data4(dwarf_info_section, cur->end - cur->start);
1765 #else
1766 dwarf_data8(dwarf_info_section, func_ind + cur->start);
1767 dwarf_data8(dwarf_info_section, cur->end - cur->start);
1768 #endif
1769 tcc_debug_finish (s1, cur->child);
1770 if (cur->child)
1771 dwarf_data1(dwarf_info_section, 0);
1773 else
1775 for (i = 0; i < cur->n_sym; i++) {
1776 struct debug_sym *s = &cur->sym[i];
1778 if (s->sec)
1779 put_stabs_r(s1, s->str, s->type, 0, 0, s->value,
1780 s->sec, s->sym_index);
1781 else
1782 put_stabs(s1, s->str, s->type, 0, 0, s->value);
1783 tcc_free (s->str);
1785 tcc_free (cur->sym);
1786 put_stabn(s1, N_LBRAC, 0, 0, cur->start);
1787 tcc_debug_finish (s1, cur->child);
1788 put_stabn(s1, N_RBRAC, 0, 0, cur->end);
1790 tcc_free (cur);
1791 cur = next;
1795 ST_FUNC void tcc_add_debug_info(TCCState *s1, int param, Sym *s, Sym *e)
1797 CString debug_str;
1799 if (!(s1->do_debug & 2))
1800 return;
1802 cstr_new (&debug_str);
1803 for (; s != e; s = s->prev) {
1804 if (!s->v || (s->r & VT_VALMASK) != VT_LOCAL)
1805 continue;
1806 if (s1->dwarf) {
1807 tcc_debug_stabs(s1, get_tok_str(s->v, NULL),
1808 param ? N_PSYM : N_LSYM, s->c, NULL, 0,
1809 tcc_get_dwarf_info(s1, s));
1811 else
1813 cstr_reset (&debug_str);
1814 cstr_printf (&debug_str, "%s:%s", get_tok_str(s->v, NULL),
1815 param ? "p" : "");
1816 tcc_get_debug_info(s1, s, &debug_str);
1817 tcc_debug_stabs(s1, debug_str.data, param ? N_PSYM : N_LSYM,
1818 s->c, NULL, 0, 0);
1821 cstr_free (&debug_str);
1824 /* put function symbol */
1825 ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym)
1827 CString debug_str;
1828 BufferedFile *f;
1830 if (!s1->do_debug)
1831 return;
1832 debug_info_root = NULL;
1833 debug_info = NULL;
1834 tcc_debug_stabn(s1, N_LBRAC, ind - func_ind);
1835 f = put_new_file(s1);
1836 if (!f)
1837 return;
1839 if (s1->dwarf) {
1840 tcc_debug_line(s1);
1841 dwarf_info.func = sym;
1842 dwarf_info.line = file->line_num;
1843 if (s1->do_backtrace) {
1844 int i, len;
1846 dwarf_line_op(s1, 0); // extended
1847 dwarf_uleb128_op(s1, strlen(funcname) + 2);
1848 dwarf_line_op(s1, DW_LNE_hi_user - 1);
1849 len = strlen(funcname) + 1;
1850 for (i = 0; i < len; i++)
1851 dwarf_line_op(s1, funcname[i]);
1854 else
1856 cstr_new (&debug_str);
1857 cstr_printf(&debug_str, "%s:%c", funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
1858 tcc_get_debug_info(s1, sym->type.ref, &debug_str);
1859 put_stabs_r(s1, debug_str.data, N_FUN, 0, f->line_num, 0, cur_text_section, sym->c);
1860 cstr_free (&debug_str);
1861 tcc_debug_line(s1);
1865 ST_FUNC void tcc_debug_prolog_epilog(TCCState *s1, int value)
1867 if (!s1->do_debug)
1868 return;
1869 if (s1->dwarf) {
1870 dwarf_line_op(s1, value == 0 ? DW_LNS_set_prologue_end
1871 : DW_LNS_set_epilogue_begin);
1875 /* put function size */
1876 ST_FUNC void tcc_debug_funcend(TCCState *s1, int size)
1878 /* lldb does not like function end and next function start at same pc */
1879 int min_instr_len;
1881 if (!s1->do_debug)
1882 return;
1883 min_instr_len = dwarf_line.last_pc == ind ? 0 : DWARF_MIN_INSTR_LEN;
1884 ind -= min_instr_len;
1885 tcc_debug_line(s1);
1886 ind += min_instr_len;
1887 tcc_debug_stabn(s1, N_RBRAC, size);
1888 if (s1->dwarf) {
1889 int func_sib = 0;
1890 Sym *sym = dwarf_info.func;
1891 int n_debug_info = tcc_get_dwarf_info(s1, sym->type.ref);
1893 dwarf_data1(dwarf_info_section,
1894 sym->type.t & VT_STATIC ? DWARF_ABBREV_SUBPROGRAM_STATIC
1895 : DWARF_ABBREV_SUBPROGRAM_EXTERNAL);
1896 if ((sym->type.t & VT_STATIC) == 0)
1897 dwarf_data1(dwarf_info_section, 1);
1898 dwarf_strp(dwarf_info_section, funcname);
1899 dwarf_uleb128(dwarf_info_section, dwarf_line.cur_file);
1900 dwarf_uleb128(dwarf_info_section, dwarf_info.line);
1901 tcc_debug_check_anon(s1, sym->type.ref, dwarf_info_section->data_offset);
1902 dwarf_data4(dwarf_info_section, n_debug_info - dwarf_info.start);
1903 dwarf_reloc(dwarf_info_section, section_sym, R_DATA_PTR);
1904 #if PTR_SIZE == 4
1905 dwarf_data4(dwarf_info_section, func_ind); // low_pc
1906 dwarf_data4(dwarf_info_section, size); // high_pc
1907 #else
1908 dwarf_data8(dwarf_info_section, func_ind); // low_pc
1909 dwarf_data8(dwarf_info_section, size); // high_pc
1910 #endif
1911 func_sib = dwarf_info_section->data_offset;
1912 dwarf_data4(dwarf_info_section, 0); // sibling
1913 dwarf_data1(dwarf_info_section, 1);
1914 #if defined(TCC_TARGET_I386)
1915 dwarf_data1(dwarf_info_section, DW_OP_reg5); // ebp
1916 #elif defined(TCC_TARGET_X86_64)
1917 dwarf_data1(dwarf_info_section, DW_OP_reg6); // rbp
1918 #elif defined TCC_TARGET_ARM
1919 dwarf_data1(dwarf_info_section, DW_OP_reg13); // sp
1920 #elif defined TCC_TARGET_ARM64
1921 dwarf_data1(dwarf_info_section, DW_OP_reg29); // reg 29
1922 #elif defined TCC_TARGET_RISCV64
1923 dwarf_data1(dwarf_info_section, DW_OP_reg8); // r8(s0)
1924 #else
1925 dwarf_data1(dwarf_info_section, DW_OP_call_frame_cfa);
1926 #endif
1927 tcc_debug_finish (s1, debug_info_root);
1928 dwarf_data1(dwarf_info_section, 0);
1929 write32le(dwarf_info_section->data + func_sib,
1930 dwarf_info_section->data_offset - dwarf_info.start);
1932 else
1934 tcc_debug_finish (s1, debug_info_root);
1936 debug_info_root = 0;
1940 ST_FUNC void tcc_debug_extern_sym(TCCState *s1, Sym *sym, int sh_num, int sym_bind, int sym_type)
1942 if (!(s1->do_debug & 2))
1943 return;
1945 if (sym_type == STT_FUNC || sym->v >= SYM_FIRST_ANOM)
1946 return;
1947 if (s1->dwarf) {
1948 int debug_type;
1950 debug_type = tcc_get_dwarf_info(s1, sym);
1951 dwarf_data1(dwarf_info_section,
1952 sym_bind == STB_GLOBAL
1953 ? DWARF_ABBREV_VARIABLE_EXTERNAL
1954 : DWARF_ABBREV_VARIABLE_STATIC);
1955 dwarf_strp(dwarf_info_section, get_tok_str(sym->v, NULL));
1956 dwarf_uleb128(dwarf_info_section, dwarf_line.cur_file);
1957 dwarf_uleb128(dwarf_info_section, file->line_num);
1958 tcc_debug_check_anon(s1, sym, dwarf_info_section->data_offset);
1959 dwarf_data4(dwarf_info_section, debug_type - dwarf_info.start);
1960 if (sym_bind == STB_GLOBAL)
1961 dwarf_data1(dwarf_info_section, 1);
1962 dwarf_data1(dwarf_info_section, PTR_SIZE + 1);
1963 dwarf_data1(dwarf_info_section, DW_OP_addr);
1964 greloca(dwarf_info_section, sym, dwarf_info_section->data_offset,
1965 R_DATA_PTR, 0);
1966 #if PTR_SIZE == 4
1967 dwarf_data4(dwarf_info_section, 0);
1968 #else
1969 dwarf_data8(dwarf_info_section, 0);
1970 #endif
1972 else
1974 Section *s = sh_num == SHN_COMMON ? common_section
1975 : s1->sections[sh_num];
1976 CString str;
1978 cstr_new (&str);
1979 cstr_printf (&str, "%s:%c",
1980 get_tok_str(sym->v, NULL),
1981 sym_bind == STB_GLOBAL ? 'G' : func_ind != -1 ? 'V' : 'S'
1983 tcc_get_debug_info(s1, sym, &str);
1984 if (sym_bind == STB_GLOBAL)
1985 tcc_debug_stabs(s1, str.data, N_GSYM, 0, NULL, 0, 0);
1986 else
1987 tcc_debug_stabs(s1, str.data,
1988 (sym->type.t & VT_STATIC) && data_section == s
1989 ? N_STSYM : N_LCSYM, 0, s, sym->c, 0);
1990 cstr_free (&str);
1994 ST_FUNC void tcc_debug_typedef(TCCState *s1, Sym *sym)
1996 if (!(s1->do_debug & 2))
1997 return;
1999 if (s1->dwarf) {
2000 int debug_type;
2002 debug_type = tcc_get_dwarf_info(s1, sym);
2003 if (debug_type != -1) {
2004 dwarf_data1(dwarf_info_section, DWARF_ABBREV_TYPEDEF);
2005 dwarf_strp(dwarf_info_section, get_tok_str(sym->v, NULL));
2006 dwarf_uleb128(dwarf_info_section, dwarf_line.cur_file);
2007 dwarf_uleb128(dwarf_info_section, file->line_num);
2008 tcc_debug_check_anon(s1, sym, dwarf_info_section->data_offset);
2009 dwarf_data4(dwarf_info_section, debug_type - dwarf_info.start);
2012 else
2014 CString str;
2015 cstr_new (&str);
2016 cstr_printf (&str, "%s:t",
2017 (sym->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
2018 ? "" : get_tok_str(sym->v, NULL));
2019 tcc_get_debug_info(s1, sym, &str);
2020 tcc_debug_stabs(s1, str.data, N_LSYM, 0, NULL, 0, 0);
2021 cstr_free (&str);
2025 /* ------------------------------------------------------------------------- */
2026 /* for section layout see lib/tcov.c */
2028 ST_FUNC void tcc_tcov_block_end(TCCState *s1, int line);
2030 ST_FUNC void tcc_tcov_block_begin(TCCState *s1)
2032 SValue sv;
2033 void *ptr;
2034 unsigned long last_offset = tcov_data.offset;
2036 tcc_tcov_block_end (tcc_state, 0);
2037 if (s1->test_coverage == 0 || nocode_wanted)
2038 return;
2040 if (tcov_data.last_file_name == 0 ||
2041 strcmp ((const char *)(tcov_section->data + tcov_data.last_file_name),
2042 file->true_filename) != 0) {
2043 char wd[1024];
2044 CString cstr;
2046 if (tcov_data.last_func_name)
2047 section_ptr_add(tcov_section, 1);
2048 if (tcov_data.last_file_name)
2049 section_ptr_add(tcov_section, 1);
2050 tcov_data.last_func_name = 0;
2051 cstr_new (&cstr);
2052 if (file->true_filename[0] == '/') {
2053 tcov_data.last_file_name = tcov_section->data_offset;
2054 cstr_printf (&cstr, "%s", file->true_filename);
2056 else {
2057 getcwd (wd, sizeof(wd));
2058 tcov_data.last_file_name = tcov_section->data_offset + strlen(wd) + 1;
2059 cstr_printf (&cstr, "%s/%s", wd, file->true_filename);
2061 ptr = section_ptr_add(tcov_section, cstr.size + 1);
2062 strcpy((char *)ptr, cstr.data);
2063 #ifdef _WIN32
2064 normalize_slashes((char *)ptr);
2065 #endif
2066 cstr_free (&cstr);
2068 if (tcov_data.last_func_name == 0 ||
2069 strcmp ((const char *)(tcov_section->data + tcov_data.last_func_name),
2070 funcname) != 0) {
2071 size_t len;
2073 if (tcov_data.last_func_name)
2074 section_ptr_add(tcov_section, 1);
2075 tcov_data.last_func_name = tcov_section->data_offset;
2076 len = strlen (funcname);
2077 ptr = section_ptr_add(tcov_section, len + 1);
2078 strcpy((char *)ptr, funcname);
2079 section_ptr_add(tcov_section, -tcov_section->data_offset & 7);
2080 ptr = section_ptr_add(tcov_section, 8);
2081 write64le (ptr, file->line_num);
2083 if (ind == tcov_data.ind && tcov_data.line == file->line_num)
2084 tcov_data.offset = last_offset;
2085 else {
2086 Sym label = {0};
2087 label.type.t = VT_LLONG | VT_STATIC;
2089 ptr = section_ptr_add(tcov_section, 16);
2090 tcov_data.line = file->line_num;
2091 write64le (ptr, (tcov_data.line << 8) | 0xff);
2092 put_extern_sym(&label, tcov_section,
2093 ((unsigned char *)ptr - tcov_section->data) + 8, 0);
2094 sv.type = label.type;
2095 sv.r = VT_SYM | VT_LVAL | VT_CONST;
2096 sv.r2 = VT_CONST;
2097 sv.c.i = 0;
2098 sv.sym = &label;
2099 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 || \
2100 defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 || \
2101 defined TCC_TARGET_RISCV64
2102 gen_increment_tcov (&sv);
2103 #else
2104 vpushv(&sv);
2105 inc(0, TOK_INC);
2106 vpop();
2107 #endif
2108 tcov_data.offset = (unsigned char *)ptr - tcov_section->data;
2109 tcov_data.ind = ind;
2113 ST_FUNC void tcc_tcov_block_end(TCCState *s1, int line)
2115 if (s1->test_coverage == 0)
2116 return;
2117 if (line == -1)
2118 line = tcov_data.line;
2119 if (tcov_data.offset) {
2120 void *ptr = tcov_section->data + tcov_data.offset;
2121 unsigned long long nline = line ? line : file->line_num;
2123 write64le (ptr, (read64le (ptr) & 0xfffffffffull) | (nline << 36));
2124 tcov_data.offset = 0;
2128 ST_FUNC void tcc_tcov_check_line(TCCState *s1, int start)
2130 if (s1->test_coverage == 0)
2131 return;
2132 if (tcov_data.line != file->line_num) {
2133 if ((tcov_data.line + 1) != file->line_num) {
2134 tcc_tcov_block_end (s1, -1);
2135 if (start)
2136 tcc_tcov_block_begin (s1);
2138 else
2139 tcov_data.line = file->line_num;
2143 ST_FUNC void tcc_tcov_start(TCCState *s1)
2145 if (s1->test_coverage == 0)
2146 return;
2147 if (!s1->dState)
2148 s1->dState = tcc_mallocz(sizeof *s1->dState);
2149 memset (&tcov_data, 0, sizeof (tcov_data));
2150 if (tcov_section == NULL) {
2151 tcov_section = new_section(tcc_state, ".tcov", SHT_PROGBITS,
2152 SHF_ALLOC | SHF_WRITE);
2153 section_ptr_add(tcov_section, 4); // pointer to executable name
2157 ST_FUNC void tcc_tcov_end(TCCState *s1)
2159 if (s1->test_coverage == 0)
2160 return;
2161 if (tcov_data.last_func_name)
2162 section_ptr_add(tcov_section, 1);
2163 if (tcov_data.last_file_name)
2164 section_ptr_add(tcov_section, 1);
2167 ST_FUNC void tcc_tcov_reset_ind(TCCState *s1)
2169 tcov_data.ind = 0;
2172 /* ------------------------------------------------------------------------- */
2173 #undef last_line_num
2174 #undef new_file
2175 #undef section_sym
2176 #undef debug_next_type
2177 #undef debug_hash
2178 #undef n_debug_hash
2179 #undef debug_anon_hash
2180 #undef n_debug_anon_hash
2181 #undef debug_info
2182 #undef debug_info_root
2183 #undef dwarf_sym
2184 #undef dwarf_line
2185 #undef dwarf_info
2186 #undef tcov_data