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
23 /* stab debug support */
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;" },
34 { VT_LONG
| VT_INT
, 4, DW_ATE_signed
, "long int:t3=r3;-2147483648;2147483647;" },
36 { VT_LLONG
| VT_LONG
, 8, DW_ATE_signed
, "long int:t3=r3;-9223372036854775808;9223372036854775807;" },
38 { VT_INT
| VT_UNSIGNED
, 4, DW_ATE_unsigned
, "unsigned int:t4=r4;0;037777777777;" },
40 { VT_LONG
| VT_INT
| VT_UNSIGNED
, 4, DW_ATE_unsigned
, "long unsigned int:t5=r5;0;037777777777;" },
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;" },
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;" },
58 { VT_LDOUBLE
, 16, DW_ATE_float
, "long double:t16=r1;16;0;" },
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;" },
71 { VT_BOOL
, 1, DW_ATE_boolean
, "bool:t26=r26;0;255;" },
73 { VT_VOID
, 1, DW_ATE_unsigned_char
, "void:t27=27" },
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" },
82 #define N_DEFAULT_DEBUG (sizeof (default_debug) / sizeof (default_debug[0]))
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
95 #define DWARF_MIN_INSTR_LEN 1
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
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
,
136 DW_AT_high_pc
, DW_FORM_data4
,
138 DW_AT_high_pc
, DW_FORM_data8
,
140 DW_AT_stmt_list
, DW_FORM_sec_offset
,
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
,
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
,
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
,
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
,
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
,
172 DWARF_ABBREV_POINTER
, DW_TAG_pointer_type
, 0,
173 DW_AT_byte_size
, DW_FORM_data1
,
174 DW_AT_type
, DW_FORM_ref4
,
176 DWARF_ABBREV_ARRAY_TYPE
, DW_TAG_array_type
, 1,
177 DW_AT_type
, DW_FORM_ref4
,
178 DW_AT_sibling
, DW_FORM_ref4
,
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
,
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
,
190 DWARF_ABBREV_ENUMERATOR_SIGNED
, DW_TAG_enumerator
, 0,
191 DW_AT_name
, DW_FORM_strp
,
192 DW_AT_const_value
, DW_FORM_sdata
,
194 DWARF_ABBREV_ENUMERATOR_UNSIGNED
, DW_TAG_enumerator
, 0,
195 DW_AT_name
, DW_FORM_strp
,
196 DW_AT_const_value
, DW_FORM_udata
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,
256 DW_AT_high_pc
, DW_FORM_data4
,
258 DW_AT_high_pc
, DW_FORM_data8
,
260 DW_AT_sibling
, DW_FORM_ref4
,
261 DW_AT_frame_base
, DW_FORM_exprloc
,
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
,
270 DW_AT_high_pc
, DW_FORM_data4
,
272 DW_AT_high_pc
, DW_FORM_data8
,
274 DW_AT_sibling
, DW_FORM_ref4
,
275 DW_AT_frame_base
, DW_FORM_exprloc
,
277 DWARF_ABBREV_LEXICAL_BLOCK
, DW_TAG_lexical_block
, 1,
278 DW_AT_low_pc
, DW_FORM_addr
,
280 DW_AT_high_pc
, DW_FORM_data4
,
282 DW_AT_high_pc
, DW_FORM_data8
,
285 DWARF_ABBREV_LEXICAL_EMPTY_BLOCK
, DW_TAG_lexical_block
, 0,
286 DW_AT_low_pc
, DW_FORM_addr
,
288 DW_AT_high_pc
, DW_FORM_data4
,
290 DW_AT_high_pc
, DW_FORM_data8
,
293 DWARF_ABBREV_SUBROUTINE_TYPE
, DW_TAG_subroutine_type
, 1,
294 DW_AT_type
, DW_FORM_ref4
,
295 DW_AT_sibling
, DW_FORM_ref4
,
297 DWARF_ABBREV_SUBROUTINE_EMPTY_TYPE
, DW_TAG_subroutine_type
, 0,
298 DW_AT_type
, DW_FORM_ref4
,
300 DWARF_ABBREV_FORMAL_PARAMETER2
, DW_TAG_formal_parameter
, 0,
301 DW_AT_type
, DW_FORM_ref4
,
306 static const unsigned char dwarf_line_opcodes
[] = {
307 0 ,1 ,1 ,1 ,1 ,0 ,0 ,0 ,1 ,0 ,0 ,1
310 /* ------------------------------------------------------------------------- */
315 int last_line_num
, new_file
;
325 struct _debug_anon_hash
{
332 int n_debug_anon_hash
;
348 struct _debug_info
*child
, *next
, *last
, *parent
;
349 } *debug_info
, *debug_info_root
;
364 struct dwarf_filename_struct
{
370 unsigned char *line_data
;
381 int base_type_used
[N_DEFAULT_DEBUG
];
386 unsigned long offset
;
387 unsigned long last_file_name
;
388 unsigned long last_func_name
;
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
)
418 s1
->dState
= tcc_mallocz(sizeof *s1
->dState
);
419 #ifdef CONFIG_TCC_BACKTRACE
420 /* include stab info with standalone backtrace support */
422 && (s1
->output_type
& (TCC_OUTPUT_EXE
| TCC_OUTPUT_DLL
)))
423 shf
= SHF_ALLOC
| SHF_WRITE
; // SHF_WRITE needed for musl/SELINUX
426 s1
->dwlo
= s1
->nb_sections
;
428 new_section(s1
, ".debug_info", SHT_PROGBITS
, shf
);
429 dwarf_abbrev_section
=
430 new_section(s1
, ".debug_abbrev", SHT_PROGBITS
, shf
);
432 new_section(s1
, ".debug_line", SHT_PROGBITS
, shf
);
433 dwarf_aranges_section
=
434 new_section(s1
, ".debug_aranges", SHT_PROGBITS
, shf
);
435 shf
|= SHF_MERGE
| SHF_STRINGS
;
437 new_section(s1
, ".debug_str", SHT_PROGBITS
, shf
);
438 dwarf_str_section
->sh_entsize
= 1;
439 dwarf_info_section
->sh_addralign
=
440 dwarf_abbrev_section
->sh_addralign
=
441 dwarf_line_section
->sh_addralign
=
442 dwarf_aranges_section
->sh_addralign
=
443 dwarf_str_section
->sh_addralign
= 1;
444 if (s1
->dwarf
>= 5) {
445 dwarf_line_str_section
=
446 new_section(s1
, ".debug_line_str", SHT_PROGBITS
, shf
);
447 dwarf_line_str_section
->sh_entsize
= 1;
448 dwarf_line_str_section
->sh_addralign
= 1;
450 s1
->dwhi
= s1
->nb_sections
;
454 stab_section
= new_section(s1
, ".stab", SHT_PROGBITS
, shf
);
455 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
456 stab_section
->sh_addralign
= sizeof ((Stab_Sym
*)0)->n_value
;
457 stab_section
->link
= new_section(s1
, ".stabstr", SHT_STRTAB
, shf
);
458 /* put first entry */
459 put_stabs(s1
, "", 0, 0, 0, 0);
463 /* put stab debug information */
464 static void put_stabs(TCCState
*s1
, const char *str
, int type
, int other
, int desc
,
471 && (offset
= stab_section
->data_offset
)
472 && (sym
= (Stab_Sym
*)(stab_section
->data
+ offset
) - 1)
473 && sym
->n_type
== type
474 && sym
->n_value
== value
) {
475 /* just update line_number in previous entry */
480 sym
= section_ptr_add(stab_section
, sizeof(Stab_Sym
));
482 sym
->n_strx
= put_elf_str(stab_section
->link
, str
);
487 sym
->n_other
= other
;
489 sym
->n_value
= value
;
492 static void put_stabs_r(TCCState
*s1
, const char *str
, int type
, int other
, int desc
,
493 unsigned long value
, Section
*sec
, int sym_index
)
495 put_elf_reloc(symtab_section
, stab_section
,
496 stab_section
->data_offset
+ 8,
497 sizeof ((Stab_Sym
*)0)->n_value
== PTR_SIZE
? R_DATA_PTR
: R_DATA_32
,
499 put_stabs(s1
, str
, type
, other
, desc
, value
);
502 static void put_stabn(TCCState
*s1
, int type
, int other
, int desc
, int value
)
504 put_stabs(s1
, NULL
, type
, other
, desc
, value
);
507 /* ------------------------------------------------------------------------- */
508 #define dwarf_data1(s,data) \
509 { unsigned char *p = section_ptr_add((s), 1); *p = (data); }
510 #define dwarf_data2(s,data) \
511 write16le(section_ptr_add((s), 2), (data))
512 #define dwarf_data4(s,data) \
513 write32le(section_ptr_add((s), 4), (data))
514 #define dwarf_data8(s,data) \
515 write64le(section_ptr_add((s), 8), (data))
517 static int dwarf_get_section_sym(Section
*s
)
519 TCCState
*s1
= s
->s1
;
520 return put_elf_sym(symtab_section
, 0, 0,
521 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
525 static void dwarf_reloc(Section
*s
, int sym
, int rel
)
527 TCCState
*s1
= s
->s1
;
528 put_elf_reloca(symtab_section
, s
, s
->data_offset
, rel
, sym
, 0);
531 static void dwarf_string(Section
*s
, Section
*dw
, int sym
, const char *str
)
533 TCCState
*s1
= s
->s1
;
537 len
= strlen(str
) + 1;
538 offset
= dw
->data_offset
;
539 ptr
= section_ptr_add(dw
, len
);
540 memmove(ptr
, str
, len
);
541 put_elf_reloca(symtab_section
, s
, s
->data_offset
, R_DATA_32DW
, sym
,
542 PTR_SIZE
== 4 ? 0 : offset
);
543 dwarf_data4(s
, PTR_SIZE
== 4 ? offset
: 0);
546 static void dwarf_strp(Section
*s
, const char *str
)
548 TCCState
*s1
= s
->s1
;
549 dwarf_string(s
, dwarf_str_section
, dwarf_sym
.str
, str
);
552 static void dwarf_line_strp(Section
*s
, const char *str
)
554 TCCState
*s1
= s
->s1
;
555 dwarf_string(s
, dwarf_line_str_section
, dwarf_sym
.line_str
, str
);
558 static void dwarf_line_op(TCCState
*s1
, unsigned char op
)
560 if (dwarf_line
.line_size
>= dwarf_line
.line_max_size
) {
561 dwarf_line
.line_max_size
+= 1024;
562 dwarf_line
.line_data
=
563 (unsigned char *)tcc_realloc(dwarf_line
.line_data
,
564 dwarf_line
.line_max_size
);
566 dwarf_line
.line_data
[dwarf_line
.line_size
++] = op
;
569 static void dwarf_file(TCCState
*s1
)
574 if (!strcmp(file
->filename
, "<command line>")) {
575 dwarf_line
.cur_file
= 1;
578 filename
= strrchr(file
->filename
, '/');
579 if (filename
== NULL
) {
580 for (i
= 1; i
< dwarf_line
.filename_size
; i
++)
581 if (dwarf_line
.filename_table
[i
].dir_entry
== 0 &&
582 strcmp(dwarf_line
.filename_table
[i
].name
,
583 file
->filename
) == 0) {
584 dwarf_line
.cur_file
= i
;
588 filename
= file
->filename
;
591 char *undo
= filename
;
592 char *dir
= file
->filename
;
595 for (i
= 0; i
< dwarf_line
.dir_size
; i
++)
596 if (strcmp(dwarf_line
.dir_table
[i
], dir
) == 0) {
597 for (j
= 1; j
< dwarf_line
.filename_size
; j
++)
598 if (dwarf_line
.filename_table
[j
].dir_entry
== i
&&
599 strcmp(dwarf_line
.filename_table
[j
].name
,
602 dwarf_line
.cur_file
= j
;
607 if (i
== dwarf_line
.dir_size
) {
608 dwarf_line
.dir_size
++;
609 dwarf_line
.dir_table
=
610 (char **) tcc_realloc(dwarf_line
.dir_table
,
611 dwarf_line
.dir_size
*
613 dwarf_line
.dir_table
[i
] = tcc_strdup(dir
);
617 dwarf_line
.filename_table
=
618 (struct dwarf_filename_struct
*)
619 tcc_realloc(dwarf_line
.filename_table
,
620 (dwarf_line
.filename_size
+ 1) *
621 sizeof (struct dwarf_filename_struct
));
622 dwarf_line
.filename_table
[dwarf_line
.filename_size
].dir_entry
= i
;
623 dwarf_line
.filename_table
[dwarf_line
.filename_size
].name
=
624 tcc_strdup(filename
);
625 dwarf_line
.cur_file
= dwarf_line
.filename_size
++;
630 static int dwarf_uleb128_size (unsigned long long value
)
637 } while (value
!= 0);
642 static int dwarf_sleb128_size (long long value
)
645 long long end
= value
>> 63;
646 unsigned char last
= end
& 0x40;
653 } while (value
!= end
|| (byte
& 0x40) != last
);
657 static void dwarf_uleb128 (Section
*s
, unsigned long long value
)
660 unsigned char byte
= value
& 0x7f;
663 dwarf_data1(s
, byte
| (value
? 0x80 : 0));
664 } while (value
!= 0);
667 static void dwarf_sleb128 (Section
*s
, long long value
)
670 long long end
= value
>> 63;
671 unsigned char last
= end
& 0x40;
674 unsigned char byte
= value
& 0x7f;
677 more
= value
!= end
|| (byte
& 0x40) != last
;
678 dwarf_data1(s
, byte
| (0x80 * more
));
682 static void dwarf_uleb128_op (TCCState
*s1
, unsigned long long value
)
685 unsigned char byte
= value
& 0x7f;
688 dwarf_line_op(s1
, byte
| (value
? 0x80 : 0));
689 } while (value
!= 0);
692 static void dwarf_sleb128_op (TCCState
*s1
, long long value
)
695 long long end
= value
>> 63;
696 unsigned char last
= end
& 0x40;
699 unsigned char byte
= value
& 0x7f;
702 more
= value
!= end
|| (byte
& 0x40) != last
;
703 dwarf_line_op(s1
, byte
| (0x80 * more
));
707 /* start of translation unit info */
708 ST_FUNC
void tcc_debug_start(TCCState
*s1
)
714 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
715 symbols can be safely used */
716 filename
= file
->prev
? file
->prev
->filename
: file
->filename
;
717 put_elf_sym(symtab_section
, 0, 0,
718 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
723 new_file
= last_line_num
= 0;
724 debug_next_type
= N_DEFAULT_DEBUG
;
726 debug_anon_hash
= NULL
;
728 n_debug_anon_hash
= 0;
730 getcwd(buf
, sizeof(buf
));
732 normalize_slashes(buf
);
741 start_abbrev
= dwarf_abbrev_section
->data_offset
;
742 ptr
= section_ptr_add(dwarf_abbrev_section
, sizeof(dwarf_abbrev_init
));
743 memcpy(ptr
, dwarf_abbrev_init
, sizeof(dwarf_abbrev_init
));
749 if (ptr
[1] == DW_FORM_line_strp
)
750 ptr
[1] = DW_FORM_strp
;
752 /* These are compatable for DW_TAG_compile_unit
754 if (ptr
[1] == DW_FORM_sec_offset
)
755 ptr
[1] = DW_FORM_data4
;
756 /* This code uses only size < 0x80 so these are
758 if (ptr
[1] == DW_FORM_exprloc
)
759 ptr
[1] = DW_FORM_block1
;
767 dwarf_sym
.info
= dwarf_get_section_sym(dwarf_info_section
);
768 dwarf_sym
.abbrev
= dwarf_get_section_sym(dwarf_abbrev_section
);
769 dwarf_sym
.line
= dwarf_get_section_sym(dwarf_line_section
);
770 dwarf_sym
.str
= dwarf_get_section_sym(dwarf_str_section
);
771 if (tcc_state
->dwarf
>= 5)
772 dwarf_sym
.line_str
= dwarf_get_section_sym(dwarf_line_str_section
);
774 dwarf_line_str_section
= dwarf_str_section
;
775 dwarf_sym
.line_str
= dwarf_sym
.str
;
777 section_sym
= dwarf_get_section_sym(text_section
);
780 dwarf_info
.start
= dwarf_info_section
->data_offset
;
781 dwarf_data4(dwarf_info_section
, 0); // size
782 dwarf_data2(dwarf_info_section
, s1
->dwarf
); // version
783 if (s1
->dwarf
>= 5) {
784 dwarf_data1(dwarf_info_section
, DW_UT_compile
); // unit type
785 dwarf_data1(dwarf_info_section
, PTR_SIZE
);
786 dwarf_reloc(dwarf_info_section
, dwarf_sym
.abbrev
, R_DATA_32DW
);
787 dwarf_data4(dwarf_info_section
, start_abbrev
);
790 dwarf_reloc(dwarf_info_section
, dwarf_sym
.abbrev
, R_DATA_32DW
);
791 dwarf_data4(dwarf_info_section
, start_abbrev
);
792 dwarf_data1(dwarf_info_section
, PTR_SIZE
);
795 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_COMPILE_UNIT
);
796 dwarf_strp(dwarf_info_section
, "tcc " TCC_VERSION
);
797 dwarf_data1(dwarf_info_section
, DW_LANG_C11
);
798 dwarf_line_strp(dwarf_info_section
, filename
);
799 dwarf_line_strp(dwarf_info_section
, buf
);
800 dwarf_reloc(dwarf_info_section
, section_sym
, R_DATA_PTR
);
802 dwarf_data4(dwarf_info_section
, ind
); // low pc
803 dwarf_data4(dwarf_info_section
, 0); // high pc
805 dwarf_data8(dwarf_info_section
, ind
); // low pc
806 dwarf_data8(dwarf_info_section
, 0); // high pc
808 dwarf_reloc(dwarf_info_section
, dwarf_sym
.line
, R_DATA_32DW
);
809 dwarf_data4(dwarf_info_section
, dwarf_line_section
->data_offset
); // stmt_list
812 dwarf_line
.start
= dwarf_line_section
->data_offset
;
813 dwarf_data4(dwarf_line_section
, 0); // length
814 dwarf_data2(dwarf_line_section
, s1
->dwarf
); // version
815 if (s1
->dwarf
>= 5) {
816 dwarf_data1(dwarf_line_section
, PTR_SIZE
); // address size
817 dwarf_data1(dwarf_line_section
, 0); // segment selector
819 dwarf_data4(dwarf_line_section
, 0); // prologue Length
820 dwarf_data1(dwarf_line_section
, DWARF_MIN_INSTR_LEN
);
822 dwarf_data1(dwarf_line_section
, 1); // maximum ops per instruction
823 dwarf_data1(dwarf_line_section
, 1); // Initial value of 'is_stmt'
824 dwarf_data1(dwarf_line_section
, DWARF_LINE_BASE
);
825 dwarf_data1(dwarf_line_section
, DWARF_LINE_RANGE
);
826 dwarf_data1(dwarf_line_section
, DWARF_OPCODE_BASE
);
827 ptr
= section_ptr_add(dwarf_line_section
, sizeof(dwarf_line_opcodes
));
828 memcpy(ptr
, dwarf_line_opcodes
, sizeof(dwarf_line_opcodes
));
829 undo
= strrchr(filename
, '/');
832 dwarf_line
.dir_size
= 1 + (undo
!= NULL
);
833 dwarf_line
.dir_table
= (char **) tcc_malloc(sizeof (char *) *
834 dwarf_line
.dir_size
);
835 dwarf_line
.dir_table
[0] = tcc_strdup(buf
);
837 dwarf_line
.dir_table
[1] = tcc_strdup(filename
);
838 dwarf_line
.filename_size
= 2;
839 dwarf_line
.filename_table
=
840 (struct dwarf_filename_struct
*)
841 tcc_malloc(2*sizeof (struct dwarf_filename_struct
));
842 dwarf_line
.filename_table
[0].dir_entry
= 0;
844 dwarf_line
.filename_table
[0].name
= tcc_strdup(undo
+ 1);
845 dwarf_line
.filename_table
[1].dir_entry
= 1;
846 dwarf_line
.filename_table
[1].name
= tcc_strdup(undo
+ 1);
850 dwarf_line
.filename_table
[0].name
= tcc_strdup(filename
);
851 dwarf_line
.filename_table
[1].dir_entry
= 0;
852 dwarf_line
.filename_table
[1].name
= tcc_strdup(filename
);
854 dwarf_line
.line_size
= dwarf_line
.line_max_size
= 0;
855 dwarf_line
.line_data
= NULL
;
856 dwarf_line
.cur_file
= 1;
857 dwarf_line
.last_file
= 0;
858 dwarf_line
.last_pc
= 0;
859 dwarf_line
.last_line
= 1;
860 dwarf_line_op(s1
, 0); // extended
861 dwarf_uleb128_op(s1
, 1 + PTR_SIZE
); // extended size
862 dwarf_line_op(s1
, DW_LNE_set_address
);
863 for (i
= 0; i
< PTR_SIZE
; i
++)
864 dwarf_line_op(s1
, 0);
865 memset(&dwarf_info
.base_type_used
, 0, sizeof(dwarf_info
.base_type_used
));
869 /* file info: full path + filename */
870 pstrcat(buf
, sizeof(buf
), "/");
871 section_sym
= put_elf_sym(symtab_section
, 0, 0,
872 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
873 text_section
->sh_num
, NULL
);
874 put_stabs_r(s1
, buf
, N_SO
, 0, 0,
875 text_section
->data_offset
, text_section
, section_sym
);
876 put_stabs_r(s1
, filename
, N_SO
, 0, 0,
877 text_section
->data_offset
, text_section
, section_sym
);
878 for (i
= 0; i
< N_DEFAULT_DEBUG
; i
++)
879 put_stabs(s1
, default_debug
[i
].name
, N_LSYM
, 0, 0, 0);
881 /* we're currently 'including' the <command line> */
886 /* put end of translation unit info */
887 ST_FUNC
void tcc_debug_end(TCCState
*s1
)
895 int text_size
= text_section
->data_offset
;
898 for (i
= 0; i
< n_debug_anon_hash
; i
++) {
899 Sym
*t
= debug_anon_hash
[i
].type
;
900 int pos
= dwarf_info_section
->data_offset
;
902 dwarf_data1(dwarf_info_section
,
903 IS_UNION (t
->type
.t
) ? DWARF_ABBREV_UNION_EMPTY_TYPE
904 : DWARF_ABBREV_STRUCTURE_EMPTY_TYPE
);
905 dwarf_strp(dwarf_info_section
,
906 (t
->v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
907 ? "" : get_tok_str(t
->v
& ~SYM_STRUCT
, NULL
));
908 dwarf_uleb128(dwarf_info_section
, 0);
909 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
910 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
911 for (j
= 0; j
< debug_anon_hash
[i
].n_debug_type
; j
++)
912 write32le(dwarf_info_section
->data
+
913 debug_anon_hash
[i
].debug_type
[j
],
914 pos
- dwarf_info
.start
);
915 tcc_free (debug_anon_hash
[i
].debug_type
);
917 tcc_free (debug_anon_hash
);
918 dwarf_data1(dwarf_info_section
, 0);
919 ptr
= dwarf_info_section
->data
+ dwarf_info
.start
;
920 write32le(ptr
, dwarf_info_section
->data_offset
- dwarf_info
.start
- 4);
921 write32le(ptr
+ 25 + (s1
->dwarf
>= 5) + PTR_SIZE
, text_size
);
924 start_aranges
= dwarf_aranges_section
->data_offset
;
925 dwarf_data4(dwarf_aranges_section
, 0); // size
926 dwarf_data2(dwarf_aranges_section
, 2); // version
927 dwarf_reloc(dwarf_aranges_section
, dwarf_sym
.info
, R_DATA_32DW
);
928 dwarf_data4(dwarf_aranges_section
, 0); // dwarf_info
930 dwarf_data1(dwarf_aranges_section
, 4); // address size
932 dwarf_data1(dwarf_aranges_section
, 8); // address size
934 dwarf_data1(dwarf_aranges_section
, 0); // segment selector size
935 dwarf_data4(dwarf_aranges_section
, 0); // padding
936 dwarf_reloc(dwarf_aranges_section
, section_sym
, R_DATA_PTR
);
938 dwarf_data4(dwarf_aranges_section
, 0); // Begin
939 dwarf_data4(dwarf_aranges_section
, text_size
); // End
940 dwarf_data4(dwarf_aranges_section
, 0); // End list
941 dwarf_data4(dwarf_aranges_section
, 0); // End list
943 dwarf_data8(dwarf_aranges_section
, 0); // Begin
944 dwarf_data8(dwarf_aranges_section
, text_size
); // End
945 dwarf_data8(dwarf_aranges_section
, 0); // End list
946 dwarf_data8(dwarf_aranges_section
, 0); // End list
948 ptr
= dwarf_aranges_section
->data
+ start_aranges
;
949 write32le(ptr
, dwarf_aranges_section
->data_offset
- start_aranges
- 4);
952 if (s1
->dwarf
>= 5) {
953 dwarf_data1(dwarf_line_section
, 1); /* col */
954 dwarf_uleb128(dwarf_line_section
, DW_LNCT_path
);
955 dwarf_uleb128(dwarf_line_section
, DW_FORM_line_strp
);
956 dwarf_uleb128(dwarf_line_section
, dwarf_line
.dir_size
);
957 for (i
= 0; i
< dwarf_line
.dir_size
; i
++)
958 dwarf_line_strp(dwarf_line_section
, dwarf_line
.dir_table
[i
]);
959 dwarf_data1(dwarf_line_section
, 2); /* col */
960 dwarf_uleb128(dwarf_line_section
, DW_LNCT_path
);
961 dwarf_uleb128(dwarf_line_section
, DW_FORM_line_strp
);
962 dwarf_uleb128(dwarf_line_section
, DW_LNCT_directory_index
);
963 dwarf_uleb128(dwarf_line_section
, DW_FORM_udata
);
964 dwarf_uleb128(dwarf_line_section
, dwarf_line
.filename_size
);
965 for (i
= 0; i
< dwarf_line
.filename_size
; i
++) {
966 dwarf_line_strp(dwarf_line_section
,
967 dwarf_line
.filename_table
[i
].name
);
968 dwarf_uleb128(dwarf_line_section
,
969 dwarf_line
.filename_table
[i
].dir_entry
);
975 for (i
= 0; i
< dwarf_line
.dir_size
; i
++) {
976 len
= strlen(dwarf_line
.dir_table
[i
]) + 1;
977 ptr
= section_ptr_add(dwarf_line_section
, len
);
978 memmove(ptr
, dwarf_line
.dir_table
[i
], len
);
980 dwarf_data1(dwarf_line_section
, 0); /* end dir */
981 for (i
= 0; i
< dwarf_line
.filename_size
; i
++) {
982 len
= strlen(dwarf_line
.filename_table
[i
].name
) + 1;
983 ptr
= section_ptr_add(dwarf_line_section
, len
);
984 memmove(ptr
, dwarf_line
.filename_table
[i
].name
, len
);
985 dwarf_uleb128(dwarf_line_section
,
986 dwarf_line
.filename_table
[i
].dir_entry
);
987 dwarf_uleb128(dwarf_line_section
, 0); /* time */
988 dwarf_uleb128(dwarf_line_section
, 0); /* size */
990 dwarf_data1(dwarf_line_section
, 0); /* end file */
992 for (i
= 0; i
< dwarf_line
.dir_size
; i
++)
993 tcc_free(dwarf_line
.dir_table
[i
]);
994 tcc_free(dwarf_line
.dir_table
);
995 for (i
= 0; i
< dwarf_line
.filename_size
; i
++)
996 tcc_free(dwarf_line
.filename_table
[i
].name
);
997 tcc_free(dwarf_line
.filename_table
);
999 dwarf_line_op(s1
, 0); // extended
1000 dwarf_uleb128_op(s1
, 1); // extended size
1001 dwarf_line_op(s1
, DW_LNE_end_sequence
);
1002 i
= (s1
->dwarf
>= 5) * 2;
1003 write32le(&dwarf_line_section
->data
[dwarf_line
.start
+ 6 + i
],
1004 dwarf_line_section
->data_offset
- dwarf_line
.start
- (10 + i
));
1005 section_ptr_add(dwarf_line_section
, 3);
1006 dwarf_reloc(dwarf_line_section
, section_sym
, R_DATA_PTR
);
1007 ptr
= section_ptr_add(dwarf_line_section
, dwarf_line
.line_size
- 3);
1008 memmove(ptr
- 3, dwarf_line
.line_data
, dwarf_line
.line_size
);
1009 tcc_free(dwarf_line
.line_data
);
1010 write32le(dwarf_line_section
->data
+ dwarf_line
.start
,
1011 dwarf_line_section
->data_offset
- dwarf_line
.start
- 4);
1015 put_stabs_r(s1
, NULL
, N_SO
, 0, 0,
1016 text_section
->data_offset
, text_section
, section_sym
);
1018 tcc_free(debug_hash
);
1021 static BufferedFile
* put_new_file(TCCState
*s1
)
1023 BufferedFile
*f
= file
;
1024 /* use upper file if from inline ":asm:" */
1025 if (f
->filename
[0] == ':')
1027 if (f
&& new_file
) {
1028 new_file
= last_line_num
= 0;
1032 put_stabs_r(s1
, f
->filename
, N_SOL
, 0, 0, ind
, text_section
, section_sym
);
1037 /* put alternative filename */
1038 ST_FUNC
void tcc_debug_putfile(TCCState
*s1
, const char *filename
)
1040 if (0 == strcmp(file
->filename
, filename
))
1042 pstrcpy(file
->filename
, sizeof(file
->filename
), filename
);
1050 /* begin of #include */
1051 ST_FUNC
void tcc_debug_bincl(TCCState
*s1
)
1058 put_stabs(s1
, file
->filename
, N_BINCL
, 0, 0, 0);
1062 /* end of #include */
1063 ST_FUNC
void tcc_debug_eincl(TCCState
*s1
)
1070 put_stabn(s1
, N_EINCL
, 0, 0, 0);
1074 /* generate line number info */
1075 ST_FUNC
void tcc_debug_line(TCCState
*s1
)
1081 if (cur_text_section
!= text_section
)
1083 f
= put_new_file(s1
);
1086 if (last_line_num
== f
->line_num
)
1088 last_line_num
= f
->line_num
;
1091 int len_pc
= (ind
- dwarf_line
.last_pc
) / DWARF_MIN_INSTR_LEN
;
1092 int len_line
= f
->line_num
- dwarf_line
.last_line
;
1093 int n
= len_pc
* DWARF_LINE_RANGE
+ len_line
+ DWARF_OPCODE_BASE
- DWARF_LINE_BASE
;
1095 if (dwarf_line
.cur_file
!= dwarf_line
.last_file
) {
1096 dwarf_line
.last_file
= dwarf_line
.cur_file
;
1097 dwarf_line_op(s1
, DW_LNS_set_file
);
1098 #ifdef TCC_TARGET_MACHO
1099 dwarf_uleb128_op(s1
, dwarf_line
.cur_file
+ 1);
1101 dwarf_uleb128_op(s1
, dwarf_line
.cur_file
);
1105 len_line
>= DWARF_LINE_BASE
&& len_line
<= (DWARF_OPCODE_BASE
+ DWARF_LINE_BASE
) &&
1106 n
>= DWARF_OPCODE_BASE
&& n
<= 255)
1107 dwarf_line_op(s1
, n
);
1110 n
= len_pc
* DWARF_LINE_RANGE
+ 0 + DWARF_OPCODE_BASE
- DWARF_LINE_BASE
;
1111 if (n
>= DWARF_OPCODE_BASE
&& n
<= 255)
1112 dwarf_line_op(s1
, n
);
1114 dwarf_line_op(s1
, DW_LNS_advance_pc
);
1115 dwarf_uleb128_op(s1
, len_pc
);
1119 n
= 0 * DWARF_LINE_RANGE
+ len_line
+ DWARF_OPCODE_BASE
- DWARF_LINE_BASE
;
1120 if (len_line
>= DWARF_LINE_BASE
&& len_line
<= (DWARF_OPCODE_BASE
+ DWARF_LINE_BASE
) &&
1121 n
>= DWARF_OPCODE_BASE
&& n
<= 255)
1122 dwarf_line_op(s1
, n
);
1124 dwarf_line_op(s1
, DW_LNS_advance_line
);
1125 dwarf_sleb128_op(s1
, len_line
);
1129 dwarf_line
.last_pc
= ind
;
1130 dwarf_line
.last_line
= f
->line_num
;
1134 if (func_ind
!= -1) {
1135 put_stabn(s1
, N_SLINE
, 0, f
->line_num
, ind
- func_ind
);
1137 /* from tcc_assemble */
1138 put_stabs_r(s1
, NULL
, N_SLINE
, 0, f
->line_num
, ind
, text_section
, section_sym
);
1143 static void tcc_debug_stabs (TCCState
*s1
, const char *str
, int type
, unsigned long value
,
1144 Section
*sec
, int sym_index
, int info
)
1146 struct debug_sym
*s
;
1150 (struct debug_sym
*)tcc_realloc (debug_info
->sym
,
1151 sizeof(struct debug_sym
) *
1152 (debug_info
->n_sym
+ 1));
1153 s
= debug_info
->sym
+ debug_info
->n_sym
++;
1156 s
->str
= tcc_strdup(str
);
1158 s
->sym_index
= sym_index
;
1160 s
->file
= dwarf_line
.cur_file
;
1161 s
->line
= file
->line_num
;
1164 put_stabs_r (s1
, str
, type
, 0, 0, value
, sec
, sym_index
);
1166 put_stabs (s1
, str
, type
, 0, 0, value
);
1169 ST_FUNC
void tcc_debug_stabn(TCCState
*s1
, int type
, int value
)
1173 if (type
== N_LBRAC
) {
1174 struct _debug_info
*info
=
1175 (struct _debug_info
*) tcc_mallocz(sizeof (*info
));
1177 info
->start
= value
;
1178 info
->parent
= debug_info
;
1180 if (debug_info
->child
) {
1181 if (debug_info
->child
->last
)
1182 debug_info
->child
->last
->next
= info
;
1184 debug_info
->child
->next
= info
;
1185 debug_info
->child
->last
= info
;
1188 debug_info
->child
= info
;
1191 debug_info_root
= info
;
1195 debug_info
->end
= value
;
1196 debug_info
= debug_info
->parent
;
1200 static int tcc_debug_find(TCCState
*s1
, Sym
*t
, int dwarf
)
1204 if (!debug_info
&& dwarf
&&
1205 (t
->type
.t
& VT_BTYPE
) == VT_STRUCT
&& t
->c
== -1) {
1206 for (i
= 0; i
< n_debug_anon_hash
; i
++)
1207 if (t
== debug_anon_hash
[i
].type
)
1209 debug_anon_hash
= (struct _debug_anon_hash
*)
1210 tcc_realloc (debug_anon_hash
,
1211 (n_debug_anon_hash
+ 1) * sizeof(*debug_anon_hash
));
1212 debug_anon_hash
[n_debug_anon_hash
].n_debug_type
= 0;
1213 debug_anon_hash
[n_debug_anon_hash
].debug_type
= NULL
;
1214 debug_anon_hash
[n_debug_anon_hash
++].type
= t
;
1217 for (i
= 0; i
< n_debug_hash
; i
++)
1218 if (t
== debug_hash
[i
].type
)
1219 return debug_hash
[i
].debug_type
;
1223 static int tcc_get_dwarf_info(TCCState
*s1
, Sym
*s
);
1225 static void tcc_debug_check_anon(TCCState
*s1
, Sym
*t
, int debug_type
)
1229 if (!debug_info
&& (t
->type
.t
& VT_BTYPE
) == VT_STRUCT
&& t
->type
.ref
->c
== -1)
1230 for (i
= 0; i
< n_debug_anon_hash
; i
++)
1231 if (t
->type
.ref
== debug_anon_hash
[i
].type
) {
1232 debug_anon_hash
[i
].debug_type
=
1233 tcc_realloc(debug_anon_hash
[i
].debug_type
,
1234 (debug_anon_hash
[i
].n_debug_type
+ 1) * sizeof(int));
1235 debug_anon_hash
[i
].debug_type
[debug_anon_hash
[i
].n_debug_type
++] =
1240 ST_FUNC
void tcc_debug_fix_anon(TCCState
*s1
, CType
*t
)
1242 int i
, j
, debug_type
;
1244 if (!s1
->do_debug
|| !s1
->dwarf
|| debug_info
)
1246 if ((t
->t
& VT_BTYPE
) == VT_STRUCT
&& t
->ref
->c
!= -1)
1247 for (i
= 0; i
< n_debug_anon_hash
; i
++)
1248 if (t
->ref
== debug_anon_hash
[i
].type
) {
1249 Sym sym
= {0}; sym
.type
= *t
;
1251 /* Trick to not hash this struct */
1252 debug_info
= (struct _debug_info
*) t
;
1253 debug_type
= tcc_get_dwarf_info(s1
, &sym
);
1255 for (j
= 0; j
< debug_anon_hash
[i
].n_debug_type
; j
++)
1256 write32le(dwarf_info_section
->data
+
1257 debug_anon_hash
[i
].debug_type
[j
],
1258 debug_type
- dwarf_info
.start
);
1259 tcc_free(debug_anon_hash
[i
].debug_type
);
1260 n_debug_anon_hash
--;
1261 for (; i
< n_debug_anon_hash
; i
++)
1262 debug_anon_hash
[i
] = debug_anon_hash
[i
+ 1];
1266 static int tcc_debug_add(TCCState
*s1
, Sym
*t
, int dwarf
)
1268 int offset
= dwarf
? dwarf_info_section
->data_offset
: ++debug_next_type
;
1269 debug_hash
= (struct _debug_hash
*)
1270 tcc_realloc (debug_hash
,
1271 (n_debug_hash
+ 1) * sizeof(*debug_hash
));
1272 debug_hash
[n_debug_hash
].debug_type
= offset
;
1273 debug_hash
[n_debug_hash
++].type
= t
;
1277 static void tcc_debug_remove(TCCState
*s1
, Sym
*t
)
1281 for (i
= 0; i
< n_debug_hash
; i
++)
1282 if (t
== debug_hash
[i
].type
) {
1284 for (; i
< n_debug_hash
; i
++)
1285 debug_hash
[i
] = debug_hash
[i
+1];
1289 static void tcc_get_debug_info(TCCState
*s1
, Sym
*s
, CString
*result
)
1293 int debug_type
= -1;
1298 type
= t
->type
.t
& ~(VT_STORAGE
| VT_CONSTANT
| VT_VOLATILE
| VT_VLA
);
1299 if ((type
& VT_BTYPE
) != VT_BYTE
)
1300 type
&= ~VT_DEFSIGN
;
1301 if (type
== VT_PTR
|| type
== (VT_PTR
| VT_ARRAY
))
1302 n
++, t
= t
->type
.ref
;
1306 if ((type
& VT_BTYPE
) == VT_STRUCT
) {
1310 debug_type
= tcc_debug_find(s1
, t
, 0);
1311 if (debug_type
== -1) {
1312 debug_type
= tcc_debug_add(s1
, t
, 0);
1314 cstr_printf (&str
, "%s:T%d=%c%d",
1315 (t
->v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
1316 ? "" : get_tok_str(t
->v
& ~SYM_STRUCT
, NULL
),
1318 IS_UNION (t
->type
.t
) ? 'u' : 's',
1321 int pos
, size
, align
;
1324 cstr_printf (&str
, "%s:",
1325 (t
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
1326 ? "" : get_tok_str(t
->v
& ~SYM_FIELD
, NULL
));
1327 tcc_get_debug_info (s1
, t
, &str
);
1328 if (t
->type
.t
& VT_BITFIELD
) {
1329 pos
= t
->c
* 8 + BIT_POS(t
->type
.t
);
1330 size
= BIT_SIZE(t
->type
.t
);
1334 size
= type_size(&t
->type
, &align
) * 8;
1336 cstr_printf (&str
, ",%d,%d;", pos
, size
);
1338 cstr_printf (&str
, ";");
1339 tcc_debug_stabs(s1
, str
.data
, N_LSYM
, 0, NULL
, 0, 0);
1342 tcc_debug_remove(s1
, e
);
1345 else if (IS_ENUM(type
)) {
1346 Sym
*e
= t
= t
->type
.ref
;
1348 debug_type
= tcc_debug_find(s1
, t
, 0);
1349 if (debug_type
== -1) {
1350 debug_type
= tcc_debug_add(s1
, t
, 0);
1352 cstr_printf (&str
, "%s:T%d=e",
1353 (t
->v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
1354 ? "" : get_tok_str(t
->v
& ~SYM_STRUCT
, NULL
),
1358 cstr_printf (&str
, "%s:",
1359 (t
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
1360 ? "" : get_tok_str(t
->v
& ~SYM_FIELD
, NULL
));
1361 cstr_printf (&str
, e
->type
.t
& VT_UNSIGNED
? "%u," : "%d,",
1364 cstr_printf (&str
, ";");
1365 tcc_debug_stabs(s1
, str
.data
, N_LSYM
, 0, NULL
, 0, 0);
1368 tcc_debug_remove(s1
, e
);
1371 else if ((type
& VT_BTYPE
) != VT_FUNC
) {
1372 type
&= ~VT_STRUCT_MASK
;
1373 for (debug_type
= 1; debug_type
<= N_DEFAULT_DEBUG
; debug_type
++)
1374 if (default_debug
[debug_type
- 1].type
== type
)
1376 if (debug_type
> N_DEFAULT_DEBUG
)
1380 cstr_printf (result
, "%d=", ++debug_next_type
);
1383 type
= t
->type
.t
& ~(VT_STORAGE
| VT_CONSTANT
| VT_VOLATILE
| VT_VLA
);
1384 if ((type
& VT_BTYPE
) != VT_BYTE
)
1385 type
&= ~VT_DEFSIGN
;
1387 cstr_printf (result
, "%d=*", ++debug_next_type
);
1388 else if (type
== (VT_PTR
| VT_ARRAY
))
1389 cstr_printf (result
, "%d=ar1;0;%d;",
1390 ++debug_next_type
, t
->type
.ref
->c
- 1);
1391 else if (type
== VT_FUNC
) {
1392 cstr_printf (result
, "%d=f", ++debug_next_type
);
1393 tcc_get_debug_info (s1
, t
->type
.ref
, result
);
1400 cstr_printf (result
, "%d", debug_type
);
1403 static int tcc_get_dwarf_info(TCCState
*s1
, Sym
*s
)
1406 int debug_type
= -1;
1415 type
= t
->type
.t
& ~(VT_STORAGE
| VT_CONSTANT
| VT_VOLATILE
| VT_VLA
);
1416 if ((type
& VT_BTYPE
) != VT_BYTE
)
1417 type
&= ~VT_DEFSIGN
;
1418 if (type
== VT_PTR
|| type
== (VT_PTR
| VT_ARRAY
))
1423 if ((type
& VT_BTYPE
) == VT_STRUCT
) {
1425 debug_type
= tcc_debug_find(s1
, t
, 1);
1426 if (debug_type
== -1) {
1427 int pos_sib
= 0, i
, *pos_type
;
1429 debug_type
= tcc_debug_add(s1
, t
, 1);
1436 pos_type
= (int *) tcc_malloc(i
* sizeof(int));
1437 dwarf_data1(dwarf_info_section
,
1438 IS_UNION (t
->type
.t
)
1439 ? t
->next
? DWARF_ABBREV_UNION_TYPE
1440 : DWARF_ABBREV_UNION_EMPTY_TYPE
1441 : t
->next
? DWARF_ABBREV_STRUCTURE_TYPE
1442 : DWARF_ABBREV_STRUCTURE_EMPTY_TYPE
);
1443 dwarf_strp(dwarf_info_section
,
1444 (t
->v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
1445 ? "" : get_tok_str(t
->v
& ~SYM_STRUCT
, NULL
));
1446 dwarf_uleb128(dwarf_info_section
, t
->c
);
1447 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1448 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
1450 pos_sib
= dwarf_info_section
->data_offset
;
1451 dwarf_data4(dwarf_info_section
, 0);
1457 dwarf_data1(dwarf_info_section
,
1458 e
->type
.t
& VT_BITFIELD
? DWARF_ABBREV_MEMBER_BF
1459 : DWARF_ABBREV_MEMBER
);
1460 dwarf_strp(dwarf_info_section
,
1461 (e
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
1462 ? "" : get_tok_str(e
->v
& ~SYM_FIELD
, NULL
));
1463 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1464 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
1465 pos_type
[i
++] = dwarf_info_section
->data_offset
;
1466 dwarf_data4(dwarf_info_section
, 0);
1467 if (e
->type
.t
& VT_BITFIELD
) {
1468 int pos
= e
->c
* 8 + BIT_POS(e
->type
.t
);
1469 int size
= BIT_SIZE(e
->type
.t
);
1471 dwarf_uleb128(dwarf_info_section
, size
);
1472 dwarf_uleb128(dwarf_info_section
, pos
);
1475 dwarf_uleb128(dwarf_info_section
, e
->c
);
1478 dwarf_data1(dwarf_info_section
, 0);
1479 write32le(dwarf_info_section
->data
+ pos_sib
,
1480 dwarf_info_section
->data_offset
- dwarf_info
.start
);
1486 type
= tcc_get_dwarf_info(s1
, e
);
1487 tcc_debug_check_anon(s1
, e
, pos_type
[i
]);
1488 write32le(dwarf_info_section
->data
+ pos_type
[i
++],
1489 type
- dwarf_info
.start
);
1493 tcc_debug_remove(s1
, t
);
1496 else if (IS_ENUM(type
)) {
1498 debug_type
= tcc_debug_find(s1
, t
, 1);
1499 if (debug_type
== -1) {
1500 int pos_sib
, pos_type
;
1501 Sym sym
= {0}; sym
.type
.t
= VT_INT
| (type
& VT_UNSIGNED
);
1503 pos_type
= tcc_get_dwarf_info(s1
, &sym
);
1504 debug_type
= tcc_debug_add(s1
, t
, 1);
1505 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_ENUMERATION_TYPE
);
1506 dwarf_strp(dwarf_info_section
,
1507 (t
->v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
1508 ? "" : get_tok_str(t
->v
& ~SYM_STRUCT
, NULL
));
1509 dwarf_data1(dwarf_info_section
,
1510 type
& VT_UNSIGNED
? DW_ATE_unsigned
: DW_ATE_signed
);
1511 dwarf_data1(dwarf_info_section
, 4);
1512 dwarf_data4(dwarf_info_section
, pos_type
- dwarf_info
.start
);
1513 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1514 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
1515 pos_sib
= dwarf_info_section
->data_offset
;
1516 dwarf_data4(dwarf_info_section
, 0);
1520 dwarf_data1(dwarf_info_section
,
1521 type
& VT_UNSIGNED
? DWARF_ABBREV_ENUMERATOR_UNSIGNED
1522 : DWARF_ABBREV_ENUMERATOR_SIGNED
);
1523 dwarf_strp(dwarf_info_section
,
1524 (e
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
1525 ? "" : get_tok_str(e
->v
& ~SYM_FIELD
, NULL
));
1526 if (type
& VT_UNSIGNED
)
1527 dwarf_uleb128(dwarf_info_section
, e
->enum_val
);
1529 dwarf_sleb128(dwarf_info_section
, e
->enum_val
);
1531 dwarf_data1(dwarf_info_section
, 0);
1532 write32le(dwarf_info_section
->data
+ pos_sib
,
1533 dwarf_info_section
->data_offset
- dwarf_info
.start
);
1535 tcc_debug_remove(s1
, t
);
1538 else if ((type
& VT_BTYPE
) != VT_FUNC
) {
1539 type
&= ~VT_STRUCT_MASK
;
1540 for (i
= 1; i
<= N_DEFAULT_DEBUG
; i
++)
1541 if (default_debug
[i
- 1].type
== type
)
1543 if (i
> N_DEFAULT_DEBUG
)
1545 debug_type
= dwarf_info
.base_type_used
[i
- 1];
1546 if (debug_type
== 0) {
1549 debug_type
= dwarf_info_section
->data_offset
;
1550 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_BASE_TYPE
);
1551 dwarf_uleb128(dwarf_info_section
, default_debug
[i
- 1].size
);
1552 dwarf_data1(dwarf_info_section
, default_debug
[i
- 1].encoding
);
1553 strncpy(name
, default_debug
[i
- 1].name
, sizeof(name
) -1);
1554 *strchr(name
, ':') = 0;
1555 dwarf_strp(dwarf_info_section
, name
);
1556 dwarf_info
.base_type_used
[i
- 1] = debug_type
;
1559 retval
= debug_type
;
1563 type
= t
->type
.t
& ~(VT_STORAGE
| VT_CONSTANT
| VT_VOLATILE
| VT_VLA
);
1564 if ((type
& VT_BTYPE
) != VT_BYTE
)
1565 type
&= ~VT_DEFSIGN
;
1566 if (type
== VT_PTR
) {
1567 i
= dwarf_info_section
->data_offset
;
1568 if (retval
== debug_type
)
1570 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_POINTER
);
1571 dwarf_data1(dwarf_info_section
, PTR_SIZE
);
1572 if (last_pos
!= -1) {
1573 tcc_debug_check_anon(s1
, e
, last_pos
);
1574 write32le(dwarf_info_section
->data
+ last_pos
,
1575 i
- dwarf_info
.start
);
1577 last_pos
= dwarf_info_section
->data_offset
;
1579 dwarf_data4(dwarf_info_section
, 0);
1581 else if (type
== (VT_PTR
| VT_ARRAY
)) {
1582 int sib_pos
, sub_type
;
1584 Sym sym
= {0}; sym
.type
.t
= VT_LONG
| VT_INT
| VT_UNSIGNED
;
1586 Sym sym
= {0}; sym
.type
.t
= VT_LLONG
| VT_LONG
| VT_UNSIGNED
;
1589 sub_type
= tcc_get_dwarf_info(s1
, &sym
);
1590 i
= dwarf_info_section
->data_offset
;
1591 if (retval
== debug_type
)
1593 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_ARRAY_TYPE
);
1594 if (last_pos
!= -1) {
1595 tcc_debug_check_anon(s1
, e
, last_pos
);
1596 write32le(dwarf_info_section
->data
+ last_pos
,
1597 i
- dwarf_info
.start
);
1599 last_pos
= dwarf_info_section
->data_offset
;
1601 dwarf_data4(dwarf_info_section
, 0);
1602 sib_pos
= dwarf_info_section
->data_offset
;
1603 dwarf_data4(dwarf_info_section
, 0);
1605 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_SUBRANGE_TYPE
);
1606 dwarf_data4(dwarf_info_section
, sub_type
- dwarf_info
.start
);
1607 dwarf_uleb128(dwarf_info_section
, t
->type
.ref
->c
- 1);
1609 type
= s
->type
.t
& ~(VT_STORAGE
| VT_CONSTANT
| VT_VOLATILE
);
1610 if (type
!= (VT_PTR
| VT_ARRAY
))
1614 dwarf_data1(dwarf_info_section
, 0);
1615 write32le(dwarf_info_section
->data
+ sib_pos
,
1616 dwarf_info_section
->data_offset
- dwarf_info
.start
);
1618 else if (type
== VT_FUNC
) {
1619 int sib_pos
= 0, *pos_type
;
1622 i
= dwarf_info_section
->data_offset
;
1623 debug_type
= tcc_get_dwarf_info(s1
, t
->type
.ref
);
1624 if (retval
== debug_type
)
1626 dwarf_data1(dwarf_info_section
,
1627 t
->type
.ref
->next
? DWARF_ABBREV_SUBROUTINE_TYPE
1628 : DWARF_ABBREV_SUBROUTINE_EMPTY_TYPE
);
1629 if (last_pos
!= -1) {
1630 tcc_debug_check_anon(s1
, e
, last_pos
);
1631 write32le(dwarf_info_section
->data
+ last_pos
,
1632 i
- dwarf_info
.start
);
1634 last_pos
= dwarf_info_section
->data_offset
;
1636 dwarf_data4(dwarf_info_section
, 0);
1637 if (t
->type
.ref
->next
) {
1638 sib_pos
= dwarf_info_section
->data_offset
;
1639 dwarf_data4(dwarf_info_section
, 0);
1647 pos_type
= (int *) tcc_malloc(i
* sizeof(int));
1652 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_FORMAL_PARAMETER2
);
1653 pos_type
[i
++] = dwarf_info_section
->data_offset
;
1654 dwarf_data4(dwarf_info_section
, 0);
1656 if (t
->type
.ref
->next
) {
1657 dwarf_data1(dwarf_info_section
, 0);
1658 write32le(dwarf_info_section
->data
+ sib_pos
,
1659 dwarf_info_section
->data_offset
- dwarf_info
.start
);
1665 type
= tcc_get_dwarf_info(s1
, f
);
1666 tcc_debug_check_anon(s1
, f
, pos_type
[i
]);
1667 write32le(dwarf_info_section
->data
+ pos_type
[i
++],
1668 type
- dwarf_info
.start
);
1673 if (last_pos
!= -1) {
1674 tcc_debug_check_anon(s1
, e
, last_pos
);
1675 write32le(dwarf_info_section
->data
+ last_pos
,
1676 debug_type
- dwarf_info
.start
);
1685 static void tcc_debug_finish (TCCState
*s1
, struct _debug_info
*cur
)
1688 struct _debug_info
*next
= cur
->next
;
1693 for (i
= cur
->n_sym
- 1; i
>= 0; i
--) {
1694 struct debug_sym
*s
= &cur
->sym
[i
];
1696 dwarf_data1(dwarf_info_section
,
1698 ? DWARF_ABBREV_FORMAL_PARAMETER
1700 ? DWARF_ABBREV_VARIABLE_EXTERNAL
1701 : s
->type
== N_STSYM
1702 ? DWARF_ABBREV_VARIABLE_STATIC
1703 : DWARF_ABBREV_VARIABLE_LOCAL
);
1704 dwarf_strp(dwarf_info_section
, s
->str
);
1705 if (s
->type
== N_GSYM
|| s
->type
== N_STSYM
) {
1706 dwarf_uleb128(dwarf_info_section
, s
->file
);
1707 dwarf_uleb128(dwarf_info_section
, s
->line
);
1709 dwarf_data4(dwarf_info_section
, s
->info
- dwarf_info
.start
);
1710 if (s
->type
== N_GSYM
|| s
->type
== N_STSYM
) {
1712 if (s
->type
== N_GSYM
)
1713 dwarf_data1(dwarf_info_section
, 1);
1714 dwarf_data1(dwarf_info_section
, PTR_SIZE
+ 1);
1715 dwarf_data1(dwarf_info_section
, DW_OP_addr
);
1716 if (s
->type
== N_STSYM
)
1717 dwarf_reloc(dwarf_info_section
, section_sym
, R_DATA_PTR
);
1719 dwarf_data4(dwarf_info_section
, s
->value
);
1721 dwarf_data8(dwarf_info_section
, s
->value
);
1726 dwarf_data1(dwarf_info_section
, dwarf_sleb128_size(s
->value
) + 1);
1727 dwarf_data1(dwarf_info_section
, DW_OP_fbreg
);
1728 dwarf_sleb128(dwarf_info_section
, s
->value
);
1732 tcc_free (cur
->sym
);
1733 dwarf_data1(dwarf_info_section
,
1734 cur
->child
? DWARF_ABBREV_LEXICAL_BLOCK
1735 : DWARF_ABBREV_LEXICAL_EMPTY_BLOCK
);
1736 dwarf_reloc(dwarf_info_section
, section_sym
, R_DATA_PTR
);
1738 dwarf_data4(dwarf_info_section
, func_ind
+ cur
->start
);
1739 dwarf_data4(dwarf_info_section
, cur
->end
- cur
->start
);
1741 dwarf_data8(dwarf_info_section
, func_ind
+ cur
->start
);
1742 dwarf_data8(dwarf_info_section
, cur
->end
- cur
->start
);
1744 tcc_debug_finish (s1
, cur
->child
);
1746 dwarf_data1(dwarf_info_section
, 0);
1750 for (i
= 0; i
< cur
->n_sym
; i
++) {
1751 struct debug_sym
*s
= &cur
->sym
[i
];
1754 put_stabs_r(s1
, s
->str
, s
->type
, 0, 0, s
->value
,
1755 s
->sec
, s
->sym_index
);
1757 put_stabs(s1
, s
->str
, s
->type
, 0, 0, s
->value
);
1760 tcc_free (cur
->sym
);
1761 put_stabn(s1
, N_LBRAC
, 0, 0, cur
->start
);
1762 tcc_debug_finish (s1
, cur
->child
);
1763 put_stabn(s1
, N_RBRAC
, 0, 0, cur
->end
);
1770 ST_FUNC
void tcc_add_debug_info(TCCState
*s1
, int param
, Sym
*s
, Sym
*e
)
1775 cstr_new (&debug_str
);
1776 for (; s
!= e
; s
= s
->prev
) {
1777 if (!s
->v
|| (s
->r
& VT_VALMASK
) != VT_LOCAL
)
1780 tcc_debug_stabs(s1
, get_tok_str(s
->v
, NULL
),
1781 param
? N_PSYM
: N_LSYM
, s
->c
, NULL
, 0,
1782 tcc_get_dwarf_info(s1
, s
));
1786 cstr_reset (&debug_str
);
1787 cstr_printf (&debug_str
, "%s:%s", get_tok_str(s
->v
, NULL
),
1789 tcc_get_debug_info(s1
, s
, &debug_str
);
1790 tcc_debug_stabs(s1
, debug_str
.data
, param
? N_PSYM
: N_LSYM
,
1794 cstr_free (&debug_str
);
1797 /* put function symbol */
1798 ST_FUNC
void tcc_debug_funcstart(TCCState
*s1
, Sym
*sym
)
1805 debug_info_root
= NULL
;
1807 tcc_debug_stabn(s1
, N_LBRAC
, ind
- func_ind
);
1808 f
= put_new_file(s1
);
1814 dwarf_info
.func
= sym
;
1815 dwarf_info
.line
= file
->line_num
;
1816 if (s1
->do_backtrace
) {
1819 dwarf_line_op(s1
, 0); // extended
1820 dwarf_uleb128_op(s1
, strlen(funcname
) + 2);
1821 dwarf_line_op(s1
, DW_LNE_hi_user
- 1);
1822 len
= strlen(funcname
) + 1;
1823 for (i
= 0; i
< len
; i
++)
1824 dwarf_line_op(s1
, funcname
[i
]);
1829 cstr_new (&debug_str
);
1830 cstr_printf(&debug_str
, "%s:%c", funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
1831 tcc_get_debug_info(s1
, sym
->type
.ref
, &debug_str
);
1832 put_stabs_r(s1
, debug_str
.data
, N_FUN
, 0, f
->line_num
, 0, cur_text_section
, sym
->c
);
1833 cstr_free (&debug_str
);
1838 ST_FUNC
void tcc_debug_prolog_epilog(TCCState
*s1
, int value
)
1843 dwarf_line_op(s1
, value
== 0 ? DW_LNS_set_prologue_end
1844 : DW_LNS_set_epilogue_begin
);
1848 /* put function size */
1849 ST_FUNC
void tcc_debug_funcend(TCCState
*s1
, int size
)
1851 /* lldb does not like function end and next function start at same pc */
1856 min_instr_len
= dwarf_line
.last_pc
== ind
? 0 : DWARF_MIN_INSTR_LEN
;
1857 ind
-= min_instr_len
;
1859 ind
+= min_instr_len
;
1860 tcc_debug_stabn(s1
, N_RBRAC
, size
);
1863 Sym
*sym
= dwarf_info
.func
;
1864 int n_debug_info
= tcc_get_dwarf_info(s1
, sym
->type
.ref
);
1866 dwarf_data1(dwarf_info_section
,
1867 sym
->type
.t
& VT_STATIC
? DWARF_ABBREV_SUBPROGRAM_STATIC
1868 : DWARF_ABBREV_SUBPROGRAM_EXTERNAL
);
1869 if ((sym
->type
.t
& VT_STATIC
) == 0)
1870 dwarf_data1(dwarf_info_section
, 1);
1871 dwarf_strp(dwarf_info_section
, funcname
);
1872 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1873 dwarf_uleb128(dwarf_info_section
, dwarf_info
.line
);
1874 tcc_debug_check_anon(s1
, sym
->type
.ref
, dwarf_info_section
->data_offset
);
1875 dwarf_data4(dwarf_info_section
, n_debug_info
- dwarf_info
.start
);
1876 dwarf_reloc(dwarf_info_section
, section_sym
, R_DATA_PTR
);
1878 dwarf_data4(dwarf_info_section
, func_ind
); // low_pc
1879 dwarf_data4(dwarf_info_section
, size
); // high_pc
1881 dwarf_data8(dwarf_info_section
, func_ind
); // low_pc
1882 dwarf_data8(dwarf_info_section
, size
); // high_pc
1884 func_sib
= dwarf_info_section
->data_offset
;
1885 dwarf_data4(dwarf_info_section
, 0); // sibling
1886 dwarf_data1(dwarf_info_section
, 1);
1887 #if defined(TCC_TARGET_I386)
1888 dwarf_data1(dwarf_info_section
, DW_OP_reg5
); // ebp
1889 #elif defined(TCC_TARGET_X86_64)
1890 dwarf_data1(dwarf_info_section
, DW_OP_reg6
); // rbp
1891 #elif defined TCC_TARGET_ARM
1892 dwarf_data1(dwarf_info_section
, DW_OP_reg13
); // sp
1893 #elif defined TCC_TARGET_ARM64
1894 dwarf_data1(dwarf_info_section
, DW_OP_reg29
); // reg 29
1895 #elif defined TCC_TARGET_RISCV64
1896 dwarf_data1(dwarf_info_section
, DW_OP_reg8
); // r8(s0)
1898 dwarf_data1(dwarf_info_section
, DW_OP_call_frame_cfa
);
1900 tcc_debug_finish (s1
, debug_info_root
);
1901 dwarf_data1(dwarf_info_section
, 0);
1902 write32le(dwarf_info_section
->data
+ func_sib
,
1903 dwarf_info_section
->data_offset
- dwarf_info
.start
);
1907 tcc_debug_finish (s1
, debug_info_root
);
1912 ST_FUNC
void tcc_debug_extern_sym(TCCState
*s1
, Sym
*sym
, int sh_num
, int sym_bind
, int sym_type
)
1916 if (sym_type
== STT_FUNC
|| sym
->v
>= SYM_FIRST_ANOM
)
1921 debug_type
= tcc_get_dwarf_info(s1
, sym
);
1922 dwarf_data1(dwarf_info_section
,
1923 sym_bind
== STB_GLOBAL
1924 ? DWARF_ABBREV_VARIABLE_EXTERNAL
1925 : DWARF_ABBREV_VARIABLE_STATIC
);
1926 dwarf_strp(dwarf_info_section
, get_tok_str(sym
->v
, NULL
));
1927 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1928 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
1929 tcc_debug_check_anon(s1
, sym
, dwarf_info_section
->data_offset
);
1930 dwarf_data4(dwarf_info_section
, debug_type
- dwarf_info
.start
);
1931 if (sym_bind
== STB_GLOBAL
)
1932 dwarf_data1(dwarf_info_section
, 1);
1933 dwarf_data1(dwarf_info_section
, PTR_SIZE
+ 1);
1934 dwarf_data1(dwarf_info_section
, DW_OP_addr
);
1935 greloca(dwarf_info_section
, sym
, dwarf_info_section
->data_offset
,
1938 dwarf_data4(dwarf_info_section
, 0);
1940 dwarf_data8(dwarf_info_section
, 0);
1945 Section
*s
= sh_num
== SHN_COMMON
? common_section
1946 : s1
->sections
[sh_num
];
1950 cstr_printf (&str
, "%s:%c",
1951 get_tok_str(sym
->v
, NULL
),
1952 sym_bind
== STB_GLOBAL
? 'G' : func_ind
!= -1 ? 'V' : 'S'
1954 tcc_get_debug_info(s1
, sym
, &str
);
1955 if (sym_bind
== STB_GLOBAL
)
1956 tcc_debug_stabs(s1
, str
.data
, N_GSYM
, 0, NULL
, 0, 0);
1958 tcc_debug_stabs(s1
, str
.data
,
1959 (sym
->type
.t
& VT_STATIC
) && data_section
== s
1960 ? N_STSYM
: N_LCSYM
, 0, s
, sym
->c
, 0);
1965 ST_FUNC
void tcc_debug_typedef(TCCState
*s1
, Sym
*sym
)
1972 debug_type
= tcc_get_dwarf_info(s1
, sym
);
1973 if (debug_type
!= -1) {
1974 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_TYPEDEF
);
1975 dwarf_strp(dwarf_info_section
, get_tok_str(sym
->v
& ~SYM_FIELD
, NULL
));
1976 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1977 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
1978 tcc_debug_check_anon(s1
, sym
, dwarf_info_section
->data_offset
);
1979 dwarf_data4(dwarf_info_section
, debug_type
- dwarf_info
.start
);
1986 cstr_printf (&str
, "%s:t",
1987 (sym
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
1988 ? "" : get_tok_str(sym
->v
& ~SYM_FIELD
, NULL
));
1989 tcc_get_debug_info(s1
, sym
, &str
);
1990 tcc_debug_stabs(s1
, str
.data
, N_LSYM
, 0, NULL
, 0, 0);
1995 /* ------------------------------------------------------------------------- */
1996 /* for section layout see lib/tcov.c */
1998 ST_FUNC
void tcc_tcov_block_end(TCCState
*s1
, int line
);
2000 ST_FUNC
void tcc_tcov_block_begin(TCCState
*s1
)
2004 unsigned long last_offset
= tcov_data
.offset
;
2006 tcc_tcov_block_end (tcc_state
, 0);
2007 if (s1
->test_coverage
== 0 || nocode_wanted
)
2010 if (tcov_data
.last_file_name
== 0 ||
2011 strcmp ((const char *)(tcov_section
->data
+ tcov_data
.last_file_name
),
2012 file
->true_filename
) != 0) {
2016 if (tcov_data
.last_func_name
)
2017 section_ptr_add(tcov_section
, 1);
2018 if (tcov_data
.last_file_name
)
2019 section_ptr_add(tcov_section
, 1);
2020 tcov_data
.last_func_name
= 0;
2022 if (file
->true_filename
[0] == '/') {
2023 tcov_data
.last_file_name
= tcov_section
->data_offset
;
2024 cstr_printf (&cstr
, "%s", file
->true_filename
);
2027 getcwd (wd
, sizeof(wd
));
2028 tcov_data
.last_file_name
= tcov_section
->data_offset
+ strlen(wd
) + 1;
2029 cstr_printf (&cstr
, "%s/%s", wd
, file
->true_filename
);
2031 ptr
= section_ptr_add(tcov_section
, cstr
.size
+ 1);
2032 strcpy((char *)ptr
, cstr
.data
);
2034 normalize_slashes((char *)ptr
);
2038 if (tcov_data
.last_func_name
== 0 ||
2039 strcmp ((const char *)(tcov_section
->data
+ tcov_data
.last_func_name
),
2043 if (tcov_data
.last_func_name
)
2044 section_ptr_add(tcov_section
, 1);
2045 tcov_data
.last_func_name
= tcov_section
->data_offset
;
2046 len
= strlen (funcname
);
2047 ptr
= section_ptr_add(tcov_section
, len
+ 1);
2048 strcpy((char *)ptr
, funcname
);
2049 section_ptr_add(tcov_section
, -tcov_section
->data_offset
& 7);
2050 ptr
= section_ptr_add(tcov_section
, 8);
2051 write64le (ptr
, file
->line_num
);
2053 if (ind
== tcov_data
.ind
&& tcov_data
.line
== file
->line_num
)
2054 tcov_data
.offset
= last_offset
;
2057 label
.type
.t
= VT_LLONG
| VT_STATIC
;
2059 ptr
= section_ptr_add(tcov_section
, 16);
2060 tcov_data
.line
= file
->line_num
;
2061 write64le (ptr
, (tcov_data
.line
<< 8) | 0xff);
2062 put_extern_sym(&label
, tcov_section
,
2063 ((unsigned char *)ptr
- tcov_section
->data
) + 8, 0);
2064 sv
.type
= label
.type
;
2065 sv
.r
= VT_SYM
| VT_LVAL
| VT_CONST
;
2069 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 || \
2070 defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 || \
2071 defined TCC_TARGET_RISCV64
2072 gen_increment_tcov (&sv
);
2078 tcov_data
.offset
= (unsigned char *)ptr
- tcov_section
->data
;
2079 tcov_data
.ind
= ind
;
2083 ST_FUNC
void tcc_tcov_block_end(TCCState
*s1
, int line
)
2085 if (s1
->test_coverage
== 0)
2088 line
= tcov_data
.line
;
2089 if (tcov_data
.offset
) {
2090 void *ptr
= tcov_section
->data
+ tcov_data
.offset
;
2091 unsigned long long nline
= line
? line
: file
->line_num
;
2093 write64le (ptr
, (read64le (ptr
) & 0xfffffffffull
) | (nline
<< 36));
2094 tcov_data
.offset
= 0;
2098 ST_FUNC
void tcc_tcov_check_line(TCCState
*s1
, int start
)
2100 if (s1
->test_coverage
== 0)
2102 if (tcov_data
.line
!= file
->line_num
) {
2103 if ((tcov_data
.line
+ 1) != file
->line_num
) {
2104 tcc_tcov_block_end (s1
, -1);
2106 tcc_tcov_block_begin (s1
);
2109 tcov_data
.line
= file
->line_num
;
2113 ST_FUNC
void tcc_tcov_start(TCCState
*s1
)
2115 if (s1
->test_coverage
== 0)
2118 s1
->dState
= tcc_mallocz(sizeof *s1
->dState
);
2119 memset (&tcov_data
, 0, sizeof (tcov_data
));
2120 if (tcov_section
== NULL
) {
2121 tcov_section
= new_section(tcc_state
, ".tcov", SHT_PROGBITS
,
2122 SHF_ALLOC
| SHF_WRITE
);
2123 section_ptr_add(tcov_section
, 4); // pointer to executable name
2127 ST_FUNC
void tcc_tcov_end(TCCState
*s1
)
2129 if (s1
->test_coverage
== 0)
2131 if (tcov_data
.last_func_name
)
2132 section_ptr_add(tcov_section
, 1);
2133 if (tcov_data
.last_file_name
)
2134 section_ptr_add(tcov_section
, 1);
2137 ST_FUNC
void tcc_tcov_reset_ind(TCCState
*s1
)
2142 /* ------------------------------------------------------------------------- */
2143 #undef last_line_num
2146 #undef debug_next_type
2149 #undef debug_anon_hash
2150 #undef n_debug_anon_hash
2152 #undef debug_info_root