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_UNION_TYPE 17
115 #define DWARF_ABBREV_SUBPROGRAM_EXTERNAL 18
116 #define DWARF_ABBREV_SUBPROGRAM_STATIC 19
117 #define DWARF_ABBREV_LEXICAL_BLOCK 20
118 #define DWARF_ABBREV_SUBROUTINE_TYPE 21
119 #define DWARF_ABBREV_FORMAL_PARAMETER2 22
121 /* all entries should have been generated with dwarf_uleb128 except
122 has_children. All values are currently below 128 so this currently
124 static const unsigned char dwarf_abbrev_init
[] = {
125 DWARF_ABBREV_COMPILE_UNIT
, DW_TAG_compile_unit
, 1,
126 DW_AT_producer
, DW_FORM_strp
,
127 DW_AT_language
, DW_FORM_data1
,
128 DW_AT_name
, DW_FORM_line_strp
,
129 DW_AT_comp_dir
, DW_FORM_line_strp
,
130 DW_AT_low_pc
, DW_FORM_addr
,
132 DW_AT_high_pc
, DW_FORM_data4
,
134 DW_AT_high_pc
, DW_FORM_data8
,
136 DW_AT_stmt_list
, DW_FORM_sec_offset
,
138 DWARF_ABBREV_BASE_TYPE
, DW_TAG_base_type
, 0,
139 DW_AT_byte_size
, DW_FORM_udata
,
140 DW_AT_encoding
, DW_FORM_data1
,
141 DW_AT_name
, DW_FORM_strp
,
143 DWARF_ABBREV_VARIABLE_EXTERNAL
, DW_TAG_variable
, 0,
144 DW_AT_name
, DW_FORM_strp
,
145 DW_AT_decl_file
, DW_FORM_udata
,
146 DW_AT_decl_line
, DW_FORM_udata
,
147 DW_AT_type
, DW_FORM_ref4
,
148 DW_AT_external
, DW_FORM_flag
,
149 DW_AT_location
, DW_FORM_exprloc
,
151 DWARF_ABBREV_VARIABLE_STATIC
, DW_TAG_variable
, 0,
152 DW_AT_name
, DW_FORM_strp
,
153 DW_AT_decl_file
, DW_FORM_udata
,
154 DW_AT_decl_line
, DW_FORM_udata
,
155 DW_AT_type
, DW_FORM_ref4
,
156 DW_AT_location
, DW_FORM_exprloc
,
158 DWARF_ABBREV_VARIABLE_LOCAL
, DW_TAG_variable
, 0,
159 DW_AT_name
, DW_FORM_strp
,
160 DW_AT_type
, DW_FORM_ref4
,
161 DW_AT_location
, DW_FORM_exprloc
,
163 DWARF_ABBREV_FORMAL_PARAMETER
, DW_TAG_formal_parameter
, 0,
164 DW_AT_name
, DW_FORM_strp
,
165 DW_AT_type
, DW_FORM_ref4
,
166 DW_AT_location
, DW_FORM_exprloc
,
168 DWARF_ABBREV_POINTER
, DW_TAG_pointer_type
, 0,
169 DW_AT_byte_size
, DW_FORM_data1
,
170 DW_AT_type
, DW_FORM_ref4
,
172 DWARF_ABBREV_ARRAY_TYPE
, DW_TAG_array_type
, 1,
173 DW_AT_type
, DW_FORM_ref4
,
174 DW_AT_sibling
, DW_FORM_ref4
,
176 DWARF_ABBREV_SUBRANGE_TYPE
, DW_TAG_subrange_type
, 0,
177 DW_AT_type
, DW_FORM_ref4
,
178 DW_AT_upper_bound
, DW_FORM_udata
,
180 DWARF_ABBREV_TYPEDEF
, DW_TAG_typedef
, 0,
181 DW_AT_name
, DW_FORM_strp
,
182 DW_AT_decl_file
, DW_FORM_udata
,
183 DW_AT_decl_line
, DW_FORM_udata
,
184 DW_AT_type
, DW_FORM_ref4
,
186 DWARF_ABBREV_ENUMERATOR_SIGNED
, DW_TAG_enumerator
, 0,
187 DW_AT_name
, DW_FORM_strp
,
188 DW_AT_const_value
, DW_FORM_sdata
,
190 DWARF_ABBREV_ENUMERATOR_UNSIGNED
, DW_TAG_enumerator
, 0,
191 DW_AT_name
, DW_FORM_strp
,
192 DW_AT_const_value
, DW_FORM_udata
,
194 DWARF_ABBREV_ENUMERATION_TYPE
, DW_TAG_enumeration_type
, 1,
195 DW_AT_name
, DW_FORM_strp
,
196 DW_AT_encoding
, DW_FORM_data1
,
197 DW_AT_byte_size
, DW_FORM_data1
,
198 DW_AT_type
, DW_FORM_ref4
,
199 DW_AT_decl_file
, DW_FORM_udata
,
200 DW_AT_decl_line
, DW_FORM_udata
,
201 DW_AT_sibling
, DW_FORM_ref4
,
203 DWARF_ABBREV_MEMBER
, DW_TAG_member
, 0,
204 DW_AT_name
, DW_FORM_strp
,
205 DW_AT_decl_file
, DW_FORM_udata
,
206 DW_AT_decl_line
, DW_FORM_udata
,
207 DW_AT_type
, DW_FORM_ref4
,
208 DW_AT_data_member_location
, DW_FORM_udata
,
210 DWARF_ABBREV_MEMBER_BF
, DW_TAG_member
, 0,
211 DW_AT_name
, DW_FORM_strp
,
212 DW_AT_decl_file
, DW_FORM_udata
,
213 DW_AT_decl_line
, DW_FORM_udata
,
214 DW_AT_type
, DW_FORM_ref4
,
215 DW_AT_bit_size
, DW_FORM_udata
,
216 DW_AT_data_bit_offset
, DW_FORM_udata
,
218 DWARF_ABBREV_STRUCTURE_TYPE
, DW_TAG_structure_type
, 1,
219 DW_AT_name
, DW_FORM_strp
,
220 DW_AT_byte_size
, DW_FORM_udata
,
221 DW_AT_decl_file
, DW_FORM_udata
,
222 DW_AT_decl_line
, DW_FORM_udata
,
223 DW_AT_sibling
, DW_FORM_ref4
,
225 DWARF_ABBREV_UNION_TYPE
, DW_TAG_union_type
, 1,
226 DW_AT_name
, DW_FORM_strp
,
227 DW_AT_byte_size
, DW_FORM_udata
,
228 DW_AT_decl_file
, DW_FORM_udata
,
229 DW_AT_decl_line
, DW_FORM_udata
,
230 DW_AT_sibling
, DW_FORM_ref4
,
232 DWARF_ABBREV_SUBPROGRAM_EXTERNAL
, DW_TAG_subprogram
, 1,
233 DW_AT_external
, DW_FORM_flag
,
234 DW_AT_name
, DW_FORM_strp
,
235 DW_AT_decl_file
, DW_FORM_udata
,
236 DW_AT_decl_line
, DW_FORM_udata
,
237 DW_AT_type
, DW_FORM_ref4
,
238 DW_AT_low_pc
, DW_FORM_addr
,
240 DW_AT_high_pc
, DW_FORM_data4
,
242 DW_AT_high_pc
, DW_FORM_data8
,
244 DW_AT_sibling
, DW_FORM_ref4
,
245 DW_AT_frame_base
, DW_FORM_exprloc
,
247 DWARF_ABBREV_SUBPROGRAM_STATIC
, DW_TAG_subprogram
, 1,
248 DW_AT_name
, DW_FORM_strp
,
249 DW_AT_decl_file
, DW_FORM_udata
,
250 DW_AT_decl_line
, DW_FORM_udata
,
251 DW_AT_type
, DW_FORM_ref4
,
252 DW_AT_low_pc
, DW_FORM_addr
,
254 DW_AT_high_pc
, DW_FORM_data4
,
256 DW_AT_high_pc
, DW_FORM_data8
,
258 DW_AT_sibling
, DW_FORM_ref4
,
259 DW_AT_frame_base
, DW_FORM_exprloc
,
261 DWARF_ABBREV_LEXICAL_BLOCK
, DW_TAG_lexical_block
, 1,
262 DW_AT_low_pc
, DW_FORM_addr
,
264 DW_AT_high_pc
, DW_FORM_data4
,
266 DW_AT_high_pc
, DW_FORM_data8
,
269 DWARF_ABBREV_SUBROUTINE_TYPE
, DW_TAG_subroutine_type
, 1,
270 DW_AT_type
, DW_FORM_ref4
,
271 DW_AT_sibling
, DW_FORM_ref4
,
273 DWARF_ABBREV_FORMAL_PARAMETER2
, DW_TAG_formal_parameter
, 0,
274 DW_AT_type
, DW_FORM_ref4
,
279 static const unsigned char dwarf_line_opcodes
[] = {
280 0 ,1 ,1 ,1 ,1 ,0 ,0 ,0 ,1 ,0 ,0 ,1
283 /* ------------------------------------------------------------------------- */
288 int last_line_num
, new_file
;
298 struct _debug_anon_hash
{
305 int n_debug_anon_hash
;
321 struct _debug_info
*child
, *next
, *last
, *parent
;
322 } *debug_info
, *debug_info_root
;
337 struct dwarf_filename_struct
{
343 unsigned char *line_data
;
354 int base_type_used
[N_DEFAULT_DEBUG
];
359 unsigned long offset
;
360 unsigned long last_file_name
;
361 unsigned long last_func_name
;
368 #define last_line_num s1->dState->last_line_num
369 #define new_file s1->dState->new_file
370 #define section_sym s1->dState->section_sym
371 #define debug_next_type s1->dState->debug_next_type
372 #define debug_hash s1->dState->debug_hash
373 #define debug_anon_hash s1->dState->debug_anon_hash
374 #define n_debug_hash s1->dState->n_debug_hash
375 #define n_debug_anon_hash s1->dState->n_debug_anon_hash
376 #define debug_info s1->dState->debug_info
377 #define debug_info_root s1->dState->debug_info_root
378 #define dwarf_sym s1->dState->dwarf_sym
379 #define dwarf_line s1->dState->dwarf_line
380 #define dwarf_info s1->dState->dwarf_info
381 #define tcov_data s1->dState->tcov_data
383 /* ------------------------------------------------------------------------- */
384 static void put_stabs(TCCState
*s1
, const char *str
, int type
, int other
,
385 int desc
, unsigned long value
);
387 ST_FUNC
void tcc_debug_new(TCCState
*s1
)
391 s1
->dState
= tcc_mallocz(sizeof *s1
->dState
);
392 #ifdef CONFIG_TCC_BACKTRACE
393 /* include stab info with standalone backtrace support */
395 && (s1
->output_type
& (TCC_OUTPUT_EXE
| TCC_OUTPUT_DLL
)))
396 shf
= SHF_ALLOC
| SHF_WRITE
; // SHF_WRITE needed for musl/SELINUX
399 s1
->dwlo
= s1
->nb_sections
;
401 new_section(s1
, ".debug_info", SHT_PROGBITS
, shf
);
402 dwarf_abbrev_section
=
403 new_section(s1
, ".debug_abbrev", SHT_PROGBITS
, shf
);
405 new_section(s1
, ".debug_line", SHT_PROGBITS
, shf
);
406 dwarf_aranges_section
=
407 new_section(s1
, ".debug_aranges", SHT_PROGBITS
, shf
);
408 shf
|= SHF_MERGE
| SHF_STRINGS
;
410 new_section(s1
, ".debug_str", SHT_PROGBITS
, shf
);
411 dwarf_str_section
->sh_entsize
= 1;
412 dwarf_info_section
->sh_addralign
=
413 dwarf_abbrev_section
->sh_addralign
=
414 dwarf_line_section
->sh_addralign
=
415 dwarf_aranges_section
->sh_addralign
=
416 dwarf_str_section
->sh_addralign
= 1;
417 if (s1
->dwarf
>= 5) {
418 dwarf_line_str_section
=
419 new_section(s1
, ".debug_line_str", SHT_PROGBITS
, shf
);
420 dwarf_line_str_section
->sh_entsize
= 1;
421 dwarf_line_str_section
->sh_addralign
= 1;
423 s1
->dwhi
= s1
->nb_sections
;
427 stab_section
= new_section(s1
, ".stab", SHT_PROGBITS
, shf
);
428 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
429 stab_section
->sh_addralign
= sizeof ((Stab_Sym
*)0)->n_value
;
430 stab_section
->link
= new_section(s1
, ".stabstr", SHT_STRTAB
, shf
);
431 /* put first entry */
432 put_stabs(s1
, "", 0, 0, 0, 0);
436 /* put stab debug information */
437 static void put_stabs(TCCState
*s1
, const char *str
, int type
, int other
, int desc
,
444 && (offset
= stab_section
->data_offset
)
445 && (sym
= (Stab_Sym
*)(stab_section
->data
+ offset
) - 1)
446 && sym
->n_type
== type
447 && sym
->n_value
== value
) {
448 /* just update line_number in previous entry */
453 sym
= section_ptr_add(stab_section
, sizeof(Stab_Sym
));
455 sym
->n_strx
= put_elf_str(stab_section
->link
, str
);
460 sym
->n_other
= other
;
462 sym
->n_value
= value
;
465 static void put_stabs_r(TCCState
*s1
, const char *str
, int type
, int other
, int desc
,
466 unsigned long value
, Section
*sec
, int sym_index
)
468 put_elf_reloc(symtab_section
, stab_section
,
469 stab_section
->data_offset
+ 8,
470 sizeof ((Stab_Sym
*)0)->n_value
== PTR_SIZE
? R_DATA_PTR
: R_DATA_32
,
472 put_stabs(s1
, str
, type
, other
, desc
, value
);
475 static void put_stabn(TCCState
*s1
, int type
, int other
, int desc
, int value
)
477 put_stabs(s1
, NULL
, type
, other
, desc
, value
);
480 /* ------------------------------------------------------------------------- */
481 #define dwarf_data1(s,data) \
482 { unsigned char *p = section_ptr_add((s), 1); *p = (data); }
483 #define dwarf_data2(s,data) \
484 write16le(section_ptr_add((s), 2), (data))
485 #define dwarf_data4(s,data) \
486 write32le(section_ptr_add((s), 4), (data))
487 #define dwarf_data8(s,data) \
488 write64le(section_ptr_add((s), 8), (data))
490 static int dwarf_get_section_sym(Section
*s
)
492 TCCState
*s1
= s
->s1
;
493 return put_elf_sym(symtab_section
, 0, 0,
494 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
498 static void dwarf_reloc(Section
*s
, int sym
, int rel
)
500 TCCState
*s1
= s
->s1
;
501 put_elf_reloca(symtab_section
, s
, s
->data_offset
, rel
, sym
, 0);
504 static void dwarf_string(Section
*s
, Section
*dw
, int sym
, const char *str
)
506 TCCState
*s1
= s
->s1
;
510 len
= strlen(str
) + 1;
511 offset
= dw
->data_offset
;
512 ptr
= section_ptr_add(dw
, len
);
513 memmove(ptr
, str
, len
);
514 put_elf_reloca(symtab_section
, s
, s
->data_offset
, R_DATA_32DW
, sym
,
515 PTR_SIZE
== 4 ? 0 : offset
);
516 dwarf_data4(s
, PTR_SIZE
== 4 ? offset
: 0);
519 static void dwarf_strp(Section
*s
, const char *str
)
521 TCCState
*s1
= s
->s1
;
522 dwarf_string(s
, dwarf_str_section
, dwarf_sym
.str
, str
);
525 static void dwarf_line_strp(Section
*s
, const char *str
)
527 TCCState
*s1
= s
->s1
;
528 dwarf_string(s
, dwarf_line_str_section
, dwarf_sym
.line_str
, str
);
531 static void dwarf_line_op(TCCState
*s1
, unsigned char op
)
533 if (dwarf_line
.line_size
>= dwarf_line
.line_max_size
) {
534 dwarf_line
.line_max_size
+= 1024;
535 dwarf_line
.line_data
=
536 (unsigned char *)tcc_realloc(dwarf_line
.line_data
,
537 dwarf_line
.line_max_size
);
539 dwarf_line
.line_data
[dwarf_line
.line_size
++] = op
;
542 static void dwarf_file(TCCState
*s1
)
547 filename
= strrchr(file
->filename
, '/');
548 if (filename
== NULL
) {
549 for (i
= 0; i
< dwarf_line
.filename_size
; i
++)
550 if (dwarf_line
.filename_table
[i
].dir_entry
== 0 &&
551 strcmp(dwarf_line
.filename_table
[i
].name
,
552 file
->filename
) == 0) {
553 dwarf_line
.cur_file
= i
;
559 char *undo
= filename
;
562 for (i
= 0; i
< dwarf_line
.dir_size
; i
++)
563 if (strcmp(dwarf_line
.dir_table
[i
], file
->filename
) == 0)
564 for (j
= 1; j
< dwarf_line
.filename_size
; j
++)
565 if (dwarf_line
.filename_table
[j
].dir_entry
== i
&&
566 strcmp(dwarf_line
.filename_table
[j
].name
,
569 dwarf_line
.cur_file
= j
;
578 static int dwarf_uleb128_size (unsigned long long value
)
585 } while (value
!= 0);
590 static int dwarf_sleb128_size (long long value
)
593 long long end
= value
>> 63;
594 unsigned char last
= end
& 0x40;
601 } while (value
!= end
|| (byte
& 0x40) != last
);
605 static void dwarf_uleb128 (Section
*s
, unsigned long long value
)
608 unsigned char byte
= value
& 0x7f;
611 dwarf_data1(s
, byte
| (value
? 0x80 : 0));
612 } while (value
!= 0);
615 static void dwarf_sleb128 (Section
*s
, long long value
)
618 long long end
= value
>> 63;
619 unsigned char last
= end
& 0x40;
622 unsigned char byte
= value
& 0x7f;
625 more
= value
!= end
|| (byte
& 0x40) != last
;
626 dwarf_data1(s
, byte
| (0x80 * more
));
630 static void dwarf_uleb128_op (TCCState
*s1
, unsigned long long value
)
633 unsigned char byte
= value
& 0x7f;
636 dwarf_line_op(s1
, byte
| (value
? 0x80 : 0));
637 } while (value
!= 0);
640 static void dwarf_sleb128_op (TCCState
*s1
, long long value
)
643 long long end
= value
>> 63;
644 unsigned char last
= end
& 0x40;
647 unsigned char byte
= value
& 0x7f;
650 more
= value
!= end
|| (byte
& 0x40) != last
;
651 dwarf_line_op(s1
, byte
| (0x80 * more
));
655 /* start of translation unit info */
656 ST_FUNC
void tcc_debug_start(TCCState
*s1
)
662 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
663 symbols can be safely used */
664 filename
= file
->prev
? file
->prev
->filename
: file
->filename
;
665 put_elf_sym(symtab_section
, 0, 0,
666 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
671 new_file
= last_line_num
= 0;
672 debug_next_type
= N_DEFAULT_DEBUG
;
674 debug_anon_hash
= NULL
;
676 n_debug_anon_hash
= 0;
678 getcwd(buf
, sizeof(buf
));
680 normalize_slashes(buf
);
689 start_abbrev
= dwarf_abbrev_section
->data_offset
;
690 ptr
= section_ptr_add(dwarf_abbrev_section
, sizeof(dwarf_abbrev_init
));
691 memcpy(ptr
, dwarf_abbrev_init
, sizeof(dwarf_abbrev_init
));
697 if (ptr
[1] == DW_FORM_line_strp
)
698 ptr
[1] = DW_FORM_strp
;
700 /* These are compatable for DW_TAG_compile_unit
702 if (ptr
[1] == DW_FORM_sec_offset
)
703 ptr
[1] = DW_FORM_data4
;
704 /* This code uses only size < 0x80 so these are
706 if (ptr
[1] == DW_FORM_exprloc
)
707 ptr
[1] = DW_FORM_block1
;
715 dwarf_sym
.info
= dwarf_get_section_sym(dwarf_info_section
);
716 dwarf_sym
.abbrev
= dwarf_get_section_sym(dwarf_abbrev_section
);
717 dwarf_sym
.line
= dwarf_get_section_sym(dwarf_line_section
);
718 dwarf_sym
.str
= dwarf_get_section_sym(dwarf_str_section
);
719 if (tcc_state
->dwarf
>= 5)
720 dwarf_sym
.line_str
= dwarf_get_section_sym(dwarf_line_str_section
);
722 dwarf_line_str_section
= dwarf_str_section
;
723 dwarf_sym
.line_str
= dwarf_sym
.str
;
725 section_sym
= dwarf_get_section_sym(text_section
);
728 dwarf_info
.start
= dwarf_info_section
->data_offset
;
729 dwarf_data4(dwarf_info_section
, 0); // size
730 dwarf_data2(dwarf_info_section
, s1
->dwarf
); // version
731 if (s1
->dwarf
>= 5) {
732 dwarf_data1(dwarf_info_section
, DW_UT_compile
); // unit type
733 dwarf_data1(dwarf_info_section
, PTR_SIZE
);
734 dwarf_reloc(dwarf_info_section
, dwarf_sym
.abbrev
, R_DATA_32DW
);
735 dwarf_data4(dwarf_info_section
, start_abbrev
);
738 dwarf_reloc(dwarf_info_section
, dwarf_sym
.abbrev
, R_DATA_32DW
);
739 dwarf_data4(dwarf_info_section
, start_abbrev
);
740 dwarf_data1(dwarf_info_section
, PTR_SIZE
);
743 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_COMPILE_UNIT
);
744 dwarf_strp(dwarf_info_section
, "tcc " TCC_VERSION
);
745 dwarf_data1(dwarf_info_section
, DW_LANG_C11
);
746 dwarf_line_strp(dwarf_info_section
, filename
);
747 dwarf_line_strp(dwarf_info_section
, buf
);
748 dwarf_reloc(dwarf_info_section
, section_sym
, R_DATA_PTR
);
750 dwarf_data4(dwarf_info_section
, ind
); // low pc
751 dwarf_data4(dwarf_info_section
, 0); // high pc
753 dwarf_data8(dwarf_info_section
, ind
); // low pc
754 dwarf_data8(dwarf_info_section
, 0); // high pc
756 dwarf_reloc(dwarf_info_section
, dwarf_sym
.line
, R_DATA_32DW
);
757 dwarf_data4(dwarf_info_section
, dwarf_line_section
->data_offset
); // stmt_list
760 dwarf_line
.start
= dwarf_line_section
->data_offset
;
761 dwarf_data4(dwarf_line_section
, 0); // length
762 dwarf_data2(dwarf_line_section
, s1
->dwarf
); // version
763 if (s1
->dwarf
>= 5) {
764 dwarf_data1(dwarf_line_section
, PTR_SIZE
); // address size
765 dwarf_data1(dwarf_line_section
, 0); // segment selector
767 dwarf_data4(dwarf_line_section
, 0); // prologue Length
768 dwarf_data1(dwarf_line_section
, DWARF_MIN_INSTR_LEN
);
770 dwarf_data1(dwarf_line_section
, 1); // maximum ops per instruction
771 dwarf_data1(dwarf_line_section
, 1); // Initial value of 'is_stmt'
772 dwarf_data1(dwarf_line_section
, DWARF_LINE_BASE
);
773 dwarf_data1(dwarf_line_section
, DWARF_LINE_RANGE
);
774 dwarf_data1(dwarf_line_section
, DWARF_OPCODE_BASE
);
775 ptr
= section_ptr_add(dwarf_line_section
, sizeof(dwarf_line_opcodes
));
776 memcpy(ptr
, dwarf_line_opcodes
, sizeof(dwarf_line_opcodes
));
777 undo
= strrchr(filename
, '/');
780 dwarf_line
.dir_size
= 1 + (undo
!= NULL
);
781 dwarf_line
.dir_table
= (char **) tcc_malloc(sizeof (char *) *
782 dwarf_line
.dir_size
);
783 dwarf_line
.dir_table
[0] = tcc_strdup(buf
);
785 dwarf_line
.dir_table
[1] = tcc_strdup(filename
);
786 dwarf_line
.filename_size
= 2;
787 dwarf_line
.filename_table
=
788 (struct dwarf_filename_struct
*)
789 tcc_malloc(2*sizeof (struct dwarf_filename_struct
));
790 dwarf_line
.filename_table
[0].dir_entry
= 0;
792 dwarf_line
.filename_table
[0].name
= tcc_strdup(filename
);
793 dwarf_line
.filename_table
[1].dir_entry
= 1;
794 dwarf_line
.filename_table
[1].name
= tcc_strdup(undo
+ 1);
796 dwarf_line
.filename_table
[0].name
= tcc_strdup(filename
);
799 dwarf_line
.filename_table
[0].name
= tcc_strdup(filename
);
800 dwarf_line
.filename_table
[1].dir_entry
= 0;
801 dwarf_line
.filename_table
[1].name
= tcc_strdup(filename
);
803 dwarf_line
.line_size
= dwarf_line
.line_max_size
= 0;
804 dwarf_line
.line_data
= NULL
;
805 dwarf_line
.cur_file
= 1;
806 dwarf_line
.last_file
= 0;
807 dwarf_line
.last_pc
= 0;
808 dwarf_line
.last_line
= 1;
809 dwarf_line_op(s1
, 0); // extended
810 dwarf_uleb128_op(s1
, 1 + PTR_SIZE
); // extended size
811 dwarf_line_op(s1
, DW_LNE_set_address
);
812 for (i
= 0; i
< PTR_SIZE
; i
++)
813 dwarf_line_op(s1
, 0);
814 memset(&dwarf_info
.base_type_used
, 0, sizeof(dwarf_info
.base_type_used
));
818 /* file info: full path + filename */
819 pstrcat(buf
, sizeof(buf
), "/");
820 section_sym
= put_elf_sym(symtab_section
, 0, 0,
821 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
822 text_section
->sh_num
, NULL
);
823 put_stabs_r(s1
, buf
, N_SO
, 0, 0,
824 text_section
->data_offset
, text_section
, section_sym
);
825 put_stabs_r(s1
, filename
, N_SO
, 0, 0,
826 text_section
->data_offset
, text_section
, section_sym
);
827 for (i
= 0; i
< N_DEFAULT_DEBUG
; i
++)
828 put_stabs(s1
, default_debug
[i
].name
, N_LSYM
, 0, 0, 0);
830 /* we're currently 'including' the <command line> */
835 /* put end of translation unit info */
836 ST_FUNC
void tcc_debug_end(TCCState
*s1
)
844 int text_size
= text_section
->data_offset
;
847 for (i
= 0; i
< n_debug_anon_hash
; i
++) {
848 Sym
*t
= debug_anon_hash
[i
].type
;
849 int pos
= dwarf_info_section
->data_offset
;
851 dwarf_data1(dwarf_info_section
,
852 IS_UNION (t
->type
.t
) ? DWARF_ABBREV_UNION_TYPE
853 : DWARF_ABBREV_STRUCTURE_TYPE
);
854 dwarf_strp(dwarf_info_section
,
855 (t
->v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
856 ? "" : get_tok_str(t
->v
& ~SYM_STRUCT
, NULL
));
857 dwarf_uleb128(dwarf_info_section
, 0);
858 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
859 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
860 j
= dwarf_info_section
->data_offset
+ 5 - dwarf_info
.start
;
861 dwarf_data4(dwarf_info_section
, j
);
862 dwarf_data1(dwarf_info_section
, 0);
863 for (j
= 0; j
< debug_anon_hash
[i
].n_debug_type
; j
++)
864 write32le(dwarf_info_section
->data
+
865 debug_anon_hash
[i
].debug_type
[j
],
866 pos
- dwarf_info
.start
);
867 tcc_free (debug_anon_hash
[i
].debug_type
);
869 tcc_free (debug_anon_hash
);
870 dwarf_data1(dwarf_info_section
, 0);
871 ptr
= dwarf_info_section
->data
+ dwarf_info
.start
;
872 write32le(ptr
, dwarf_info_section
->data_offset
- dwarf_info
.start
- 4);
873 write32le(ptr
+ 25 + (s1
->dwarf
>= 5) + PTR_SIZE
, text_size
);
876 start_aranges
= dwarf_aranges_section
->data_offset
;
877 dwarf_data4(dwarf_aranges_section
, 0); // size
878 dwarf_data2(dwarf_aranges_section
, 2); // version
879 dwarf_reloc(dwarf_aranges_section
, dwarf_sym
.info
, R_DATA_32DW
);
880 dwarf_data4(dwarf_aranges_section
, 0); // dwarf_info
882 dwarf_data1(dwarf_aranges_section
, 4); // address size
884 dwarf_data1(dwarf_aranges_section
, 8); // address size
886 dwarf_data1(dwarf_aranges_section
, 0); // segment selector size
887 dwarf_data4(dwarf_aranges_section
, 0); // padding
888 dwarf_reloc(dwarf_aranges_section
, section_sym
, R_DATA_PTR
);
890 dwarf_data4(dwarf_aranges_section
, 0); // Begin
891 dwarf_data4(dwarf_aranges_section
, text_size
); // End
892 dwarf_data4(dwarf_aranges_section
, 0); // End list
893 dwarf_data4(dwarf_aranges_section
, 0); // End list
895 dwarf_data8(dwarf_aranges_section
, 0); // Begin
896 dwarf_data8(dwarf_aranges_section
, text_size
); // End
897 dwarf_data8(dwarf_aranges_section
, 0); // End list
898 dwarf_data8(dwarf_aranges_section
, 0); // End list
900 ptr
= dwarf_aranges_section
->data
+ start_aranges
;
901 write32le(ptr
, dwarf_aranges_section
->data_offset
- start_aranges
- 4);
904 if (s1
->dwarf
>= 5) {
905 dwarf_data1(dwarf_line_section
, 1); /* col */
906 dwarf_uleb128(dwarf_line_section
, DW_LNCT_path
);
907 dwarf_uleb128(dwarf_line_section
, DW_FORM_line_strp
);
908 dwarf_uleb128(dwarf_line_section
, dwarf_line
.dir_size
);
909 for (i
= 0; i
< dwarf_line
.dir_size
; i
++)
910 dwarf_line_strp(dwarf_line_section
, dwarf_line
.dir_table
[i
]);
911 dwarf_data1(dwarf_line_section
, 2); /* col */
912 dwarf_uleb128(dwarf_line_section
, DW_LNCT_path
);
913 dwarf_uleb128(dwarf_line_section
, DW_FORM_line_strp
);
914 dwarf_uleb128(dwarf_line_section
, DW_LNCT_directory_index
);
915 dwarf_uleb128(dwarf_line_section
, DW_FORM_udata
);
916 dwarf_uleb128(dwarf_line_section
, dwarf_line
.filename_size
);
917 for (i
= 0; i
< dwarf_line
.filename_size
; i
++) {
918 dwarf_line_strp(dwarf_line_section
,
919 dwarf_line
.filename_table
[i
].name
);
920 dwarf_uleb128(dwarf_line_section
,
921 dwarf_line
.filename_table
[i
].dir_entry
);
927 for (i
= 0; i
< dwarf_line
.dir_size
; i
++) {
928 len
= strlen(dwarf_line
.dir_table
[i
]) + 1;
929 ptr
= section_ptr_add(dwarf_line_section
, len
);
930 memmove(ptr
, dwarf_line
.dir_table
[i
], len
);
932 dwarf_data1(dwarf_line_section
, 0); /* end dir */
933 for (i
= 0; i
< dwarf_line
.filename_size
; i
++) {
934 len
= strlen(dwarf_line
.filename_table
[i
].name
) + 1;
935 ptr
= section_ptr_add(dwarf_line_section
, len
);
936 memmove(ptr
, dwarf_line
.filename_table
[i
].name
, len
);
937 dwarf_uleb128(dwarf_line_section
,
938 dwarf_line
.filename_table
[i
].dir_entry
);
939 dwarf_uleb128(dwarf_line_section
, 0); /* time */
940 dwarf_uleb128(dwarf_line_section
, 0); /* size */
942 dwarf_data1(dwarf_line_section
, 0); /* end file */
944 for (i
= 0; i
< dwarf_line
.dir_size
; i
++)
945 tcc_free(dwarf_line
.dir_table
[i
]);
946 tcc_free(dwarf_line
.dir_table
);
947 for (i
= 0; i
< dwarf_line
.filename_size
; i
++)
948 tcc_free(dwarf_line
.filename_table
[i
].name
);
949 tcc_free(dwarf_line
.filename_table
);
951 dwarf_line_op(s1
, 0); // extended
952 dwarf_uleb128_op(s1
, 1); // extended size
953 dwarf_line_op(s1
, DW_LNE_end_sequence
);
954 i
= (s1
->dwarf
>= 5) * 2;
955 write32le(&dwarf_line_section
->data
[dwarf_line
.start
+ 6 + i
],
956 dwarf_line_section
->data_offset
- dwarf_line
.start
- (10 + i
));
957 section_ptr_add(dwarf_line_section
, 3);
958 dwarf_reloc(dwarf_line_section
, section_sym
, R_DATA_PTR
);
959 ptr
= section_ptr_add(dwarf_line_section
, dwarf_line
.line_size
- 3);
960 memmove(ptr
- 3, dwarf_line
.line_data
, dwarf_line
.line_size
);
961 tcc_free(dwarf_line
.line_data
);
962 write32le(dwarf_line_section
->data
+ dwarf_line
.start
,
963 dwarf_line_section
->data_offset
- dwarf_line
.start
- 4);
967 put_stabs_r(s1
, NULL
, N_SO
, 0, 0,
968 text_section
->data_offset
, text_section
, section_sym
);
970 tcc_free(debug_hash
);
973 static BufferedFile
* put_new_file(TCCState
*s1
)
975 BufferedFile
*f
= file
;
976 /* use upper file if from inline ":asm:" */
977 if (f
->filename
[0] == ':')
980 new_file
= last_line_num
= 0;
984 put_stabs_r(s1
, f
->filename
, N_SOL
, 0, 0, ind
, text_section
, section_sym
);
989 /* put alternative filename */
990 ST_FUNC
void tcc_debug_putfile(TCCState
*s1
, const char *filename
)
992 if (0 == strcmp(file
->filename
, filename
))
994 pstrcpy(file
->filename
, sizeof(file
->filename
), filename
);
1002 /* begin of #include */
1003 ST_FUNC
void tcc_debug_bincl(TCCState
*s1
)
1009 char *filename
= strrchr(file
->filename
, '/');
1012 if (filename
== NULL
) {
1013 filename
= file
->filename
;
1017 char *undo
= filename
;
1020 dir
= file
->filename
;
1021 for (i
= 0; i
< dwarf_line
.dir_size
; i
++)
1022 if (strcmp (dwarf_line
.dir_table
[i
], dir
) == 0)
1024 if (i
== dwarf_line
.dir_size
) {
1025 dwarf_line
.dir_size
++;
1026 dwarf_line
.dir_table
=
1027 (char **) tcc_realloc(dwarf_line
.dir_table
,
1028 dwarf_line
.dir_size
*
1030 dwarf_line
.dir_table
[i
] = tcc_strdup(dir
);
1034 if (strcmp(filename
, "<command line>")) {
1035 for (j
= 0; j
< dwarf_line
.filename_size
; j
++)
1036 if (dwarf_line
.filename_table
[j
].dir_entry
== i
&&
1037 strcmp (dwarf_line
.filename_table
[j
].name
, filename
) == 0)
1039 if (j
== dwarf_line
.filename_size
) {
1040 dwarf_line
.filename_table
=
1041 (struct dwarf_filename_struct
*)
1042 tcc_realloc(dwarf_line
.filename_table
,
1043 (dwarf_line
.filename_size
+ 1) *
1044 sizeof (struct dwarf_filename_struct
));
1045 dwarf_line
.filename_table
[dwarf_line
.filename_size
].dir_entry
= i
;
1046 dwarf_line
.filename_table
[dwarf_line
.filename_size
++].name
=
1047 tcc_strdup(filename
);
1054 put_stabs(s1
, file
->filename
, N_BINCL
, 0, 0, 0);
1059 /* end of #include */
1060 ST_FUNC
void tcc_debug_eincl(TCCState
*s1
)
1067 put_stabn(s1
, N_EINCL
, 0, 0, 0);
1071 /* generate line number info */
1072 ST_FUNC
void tcc_debug_line(TCCState
*s1
)
1078 if (cur_text_section
!= text_section
)
1080 f
= put_new_file(s1
);
1083 if (last_line_num
== f
->line_num
)
1085 last_line_num
= f
->line_num
;
1088 int len_pc
= (ind
- dwarf_line
.last_pc
) / DWARF_MIN_INSTR_LEN
;
1089 int len_line
= f
->line_num
- dwarf_line
.last_line
;
1090 int n
= len_pc
* DWARF_LINE_RANGE
+ len_line
+ DWARF_OPCODE_BASE
- DWARF_LINE_BASE
;
1092 if (dwarf_line
.cur_file
!= dwarf_line
.last_file
) {
1093 dwarf_line
.last_file
= dwarf_line
.cur_file
;
1094 dwarf_line_op(s1
, DW_LNS_set_file
);
1095 dwarf_uleb128_op(s1
, dwarf_line
.cur_file
);
1098 len_line
>= DWARF_LINE_BASE
&& len_line
<= (DWARF_OPCODE_BASE
+ DWARF_LINE_BASE
) &&
1099 n
>= DWARF_OPCODE_BASE
&& n
<= 255)
1100 dwarf_line_op(s1
, n
);
1103 n
= len_pc
* DWARF_LINE_RANGE
+ 0 + DWARF_OPCODE_BASE
- DWARF_LINE_BASE
;
1104 if (n
>= DWARF_OPCODE_BASE
&& n
<= 255)
1105 dwarf_line_op(s1
, n
);
1107 dwarf_line_op(s1
, DW_LNS_advance_pc
);
1108 dwarf_uleb128_op(s1
, len_pc
);
1112 n
= 0 * DWARF_LINE_RANGE
+ len_line
+ DWARF_OPCODE_BASE
- DWARF_LINE_BASE
;
1113 if (len_line
>= DWARF_LINE_BASE
&& len_line
<= (DWARF_OPCODE_BASE
+ DWARF_LINE_BASE
) &&
1114 n
>= DWARF_OPCODE_BASE
&& n
<= 255)
1115 dwarf_line_op(s1
, n
);
1117 dwarf_line_op(s1
, DW_LNS_advance_line
);
1118 dwarf_sleb128_op(s1
, len_line
);
1122 dwarf_line
.last_pc
= ind
;
1123 dwarf_line
.last_line
= f
->line_num
;
1127 if (func_ind
!= -1) {
1128 put_stabn(s1
, N_SLINE
, 0, f
->line_num
, ind
- func_ind
);
1130 /* from tcc_assemble */
1131 put_stabs_r(s1
, NULL
, N_SLINE
, 0, f
->line_num
, ind
, text_section
, section_sym
);
1136 static void tcc_debug_stabs (TCCState
*s1
, const char *str
, int type
, unsigned long value
,
1137 Section
*sec
, int sym_index
, int info
)
1139 struct debug_sym
*s
;
1143 (struct debug_sym
*)tcc_realloc (debug_info
->sym
,
1144 sizeof(struct debug_sym
) *
1145 (debug_info
->n_sym
+ 1));
1146 s
= debug_info
->sym
+ debug_info
->n_sym
++;
1149 s
->str
= tcc_strdup(str
);
1151 s
->sym_index
= sym_index
;
1153 s
->file
= dwarf_line
.cur_file
;
1154 s
->line
= file
->line_num
;
1157 put_stabs_r (s1
, str
, type
, 0, 0, value
, sec
, sym_index
);
1159 put_stabs (s1
, str
, type
, 0, 0, value
);
1162 ST_FUNC
void tcc_debug_stabn(TCCState
*s1
, int type
, int value
)
1166 if (type
== N_LBRAC
) {
1167 struct _debug_info
*info
=
1168 (struct _debug_info
*) tcc_mallocz(sizeof (*info
));
1170 info
->start
= value
;
1171 info
->parent
= debug_info
;
1173 if (debug_info
->child
) {
1174 if (debug_info
->child
->last
)
1175 debug_info
->child
->last
->next
= info
;
1177 debug_info
->child
->next
= info
;
1178 debug_info
->child
->last
= info
;
1181 debug_info
->child
= info
;
1184 debug_info_root
= info
;
1188 debug_info
->end
= value
;
1189 debug_info
= debug_info
->parent
;
1193 static int tcc_debug_find(TCCState
*s1
, Sym
*t
, int dwarf
)
1197 if (!debug_info
&& dwarf
&&
1198 (t
->type
.t
& VT_BTYPE
) == VT_STRUCT
&& t
->c
== -1) {
1199 for (i
= 0; i
< n_debug_anon_hash
; i
++)
1200 if (t
== debug_anon_hash
[i
].type
)
1202 debug_anon_hash
= (struct _debug_anon_hash
*)
1203 tcc_realloc (debug_anon_hash
,
1204 (n_debug_anon_hash
+ 1) * sizeof(*debug_anon_hash
));
1205 debug_anon_hash
[n_debug_anon_hash
].n_debug_type
= 0;
1206 debug_anon_hash
[n_debug_anon_hash
].debug_type
= NULL
;
1207 debug_anon_hash
[n_debug_anon_hash
++].type
= t
;
1210 for (i
= 0; i
< n_debug_hash
; i
++)
1211 if (t
== debug_hash
[i
].type
)
1212 return debug_hash
[i
].debug_type
;
1216 static int tcc_get_dwarf_info(TCCState
*s1
, Sym
*s
);
1218 static void tcc_debug_check_anon(TCCState
*s1
, Sym
*t
, int debug_type
)
1222 if (!debug_info
&& (t
->type
.t
& VT_BTYPE
) == VT_STRUCT
&& t
->type
.ref
->c
== -1)
1223 for (i
= 0; i
< n_debug_anon_hash
; i
++)
1224 if (t
->type
.ref
== debug_anon_hash
[i
].type
) {
1225 debug_anon_hash
[i
].debug_type
=
1226 tcc_realloc(debug_anon_hash
[i
].debug_type
,
1227 (debug_anon_hash
[i
].n_debug_type
+ 1) * sizeof(int));
1228 debug_anon_hash
[i
].debug_type
[debug_anon_hash
[i
].n_debug_type
++] =
1233 ST_FUNC
void tcc_debug_fix_anon(TCCState
*s1
, CType
*t
)
1235 int i
, j
, debug_type
;
1237 if (!s1
->dState
|| !s1
->dwarf
|| debug_info
)
1239 if ((t
->t
& VT_BTYPE
) == VT_STRUCT
&& t
->ref
->c
!= -1)
1240 for (i
= 0; i
< n_debug_anon_hash
; i
++)
1241 if (t
->ref
== debug_anon_hash
[i
].type
) {
1242 Sym sym
= {0}; sym
.type
= *t
;
1244 /* Trick to not hash this struct */
1245 debug_info
= (struct _debug_info
*) t
;
1246 debug_type
= tcc_get_dwarf_info(s1
, &sym
);
1248 for (j
= 0; j
< debug_anon_hash
[i
].n_debug_type
; j
++)
1249 write32le(dwarf_info_section
->data
+
1250 debug_anon_hash
[i
].debug_type
[j
],
1251 debug_type
- dwarf_info
.start
);
1252 tcc_free(debug_anon_hash
[i
].debug_type
);
1253 n_debug_anon_hash
--;
1254 for (; i
< n_debug_anon_hash
; i
++)
1255 debug_anon_hash
[i
] = debug_anon_hash
[i
+ 1];
1259 static int tcc_debug_add(TCCState
*s1
, Sym
*t
, int dwarf
)
1261 int offset
= dwarf
? dwarf_info_section
->data_offset
: ++debug_next_type
;
1262 debug_hash
= (struct _debug_hash
*)
1263 tcc_realloc (debug_hash
,
1264 (n_debug_hash
+ 1) * sizeof(*debug_hash
));
1265 debug_hash
[n_debug_hash
].debug_type
= offset
;
1266 debug_hash
[n_debug_hash
++].type
= t
;
1270 static void tcc_debug_remove(TCCState
*s1
, Sym
*t
)
1274 for (i
= 0; i
< n_debug_hash
; i
++)
1275 if (t
== debug_hash
[i
].type
) {
1277 for (; i
< n_debug_hash
; i
++)
1278 debug_hash
[i
] = debug_hash
[i
+1];
1282 static void tcc_get_debug_info(TCCState
*s1
, Sym
*s
, CString
*result
)
1286 int debug_type
= -1;
1291 type
= t
->type
.t
& ~(VT_STORAGE
| VT_CONSTANT
| VT_VOLATILE
| VT_VLA
);
1292 if ((type
& VT_BTYPE
) != VT_BYTE
)
1293 type
&= ~VT_DEFSIGN
;
1294 if (type
== VT_PTR
|| type
== (VT_PTR
| VT_ARRAY
))
1295 n
++, t
= t
->type
.ref
;
1299 if ((type
& VT_BTYPE
) == VT_STRUCT
) {
1303 debug_type
= tcc_debug_find(s1
, t
, 0);
1304 if (debug_type
== -1) {
1305 debug_type
= tcc_debug_add(s1
, t
, 0);
1307 cstr_printf (&str
, "%s:T%d=%c%d",
1308 (t
->v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
1309 ? "" : get_tok_str(t
->v
& ~SYM_STRUCT
, NULL
),
1311 IS_UNION (t
->type
.t
) ? 'u' : 's',
1314 int pos
, size
, align
;
1317 cstr_printf (&str
, "%s:",
1318 (t
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
1319 ? "" : get_tok_str(t
->v
& ~SYM_FIELD
, NULL
));
1320 tcc_get_debug_info (s1
, t
, &str
);
1321 if (t
->type
.t
& VT_BITFIELD
) {
1322 pos
= t
->c
* 8 + BIT_POS(t
->type
.t
);
1323 size
= BIT_SIZE(t
->type
.t
);
1327 size
= type_size(&t
->type
, &align
) * 8;
1329 cstr_printf (&str
, ",%d,%d;", pos
, size
);
1331 cstr_printf (&str
, ";");
1332 tcc_debug_stabs(s1
, str
.data
, N_LSYM
, 0, NULL
, 0, 0);
1335 tcc_debug_remove(s1
, e
);
1338 else if (IS_ENUM(type
)) {
1339 Sym
*e
= t
= t
->type
.ref
;
1341 debug_type
= tcc_debug_find(s1
, t
, 0);
1342 if (debug_type
== -1) {
1343 debug_type
= tcc_debug_add(s1
, t
, 0);
1345 cstr_printf (&str
, "%s:T%d=e",
1346 (t
->v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
1347 ? "" : get_tok_str(t
->v
& ~SYM_STRUCT
, NULL
),
1351 cstr_printf (&str
, "%s:",
1352 (t
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
1353 ? "" : get_tok_str(t
->v
& ~SYM_FIELD
, NULL
));
1354 cstr_printf (&str
, e
->type
.t
& VT_UNSIGNED
? "%u," : "%d,",
1357 cstr_printf (&str
, ";");
1358 tcc_debug_stabs(s1
, str
.data
, N_LSYM
, 0, NULL
, 0, 0);
1361 tcc_debug_remove(s1
, e
);
1364 else if ((type
& VT_BTYPE
) != VT_FUNC
) {
1365 type
&= ~VT_STRUCT_MASK
;
1366 for (debug_type
= 1; debug_type
<= N_DEFAULT_DEBUG
; debug_type
++)
1367 if (default_debug
[debug_type
- 1].type
== type
)
1369 if (debug_type
> N_DEFAULT_DEBUG
)
1373 cstr_printf (result
, "%d=", ++debug_next_type
);
1376 type
= t
->type
.t
& ~(VT_STORAGE
| VT_CONSTANT
| VT_VOLATILE
| VT_VLA
);
1377 if ((type
& VT_BTYPE
) != VT_BYTE
)
1378 type
&= ~VT_DEFSIGN
;
1380 cstr_printf (result
, "%d=*", ++debug_next_type
);
1381 else if (type
== (VT_PTR
| VT_ARRAY
))
1382 cstr_printf (result
, "%d=ar1;0;%d;",
1383 ++debug_next_type
, t
->type
.ref
->c
- 1);
1384 else if (type
== VT_FUNC
) {
1385 cstr_printf (result
, "%d=f", ++debug_next_type
);
1386 tcc_get_debug_info (s1
, t
->type
.ref
, result
);
1393 cstr_printf (result
, "%d", debug_type
);
1396 static int tcc_get_dwarf_info(TCCState
*s1
, Sym
*s
)
1399 int debug_type
= -1;
1408 type
= t
->type
.t
& ~(VT_STORAGE
| VT_CONSTANT
| VT_VOLATILE
| VT_VLA
);
1409 if ((type
& VT_BTYPE
) != VT_BYTE
)
1410 type
&= ~VT_DEFSIGN
;
1411 if (type
== VT_PTR
|| type
== (VT_PTR
| VT_ARRAY
))
1416 if ((type
& VT_BTYPE
) == VT_STRUCT
) {
1418 debug_type
= tcc_debug_find(s1
, t
, 1);
1419 if (debug_type
== -1) {
1420 int pos_sib
, i
, *pos_type
;
1422 debug_type
= tcc_debug_add(s1
, t
, 1);
1429 pos_type
= (int *) tcc_malloc(i
* sizeof(int));
1430 dwarf_data1(dwarf_info_section
,
1431 IS_UNION (t
->type
.t
) ? DWARF_ABBREV_UNION_TYPE
1432 : DWARF_ABBREV_STRUCTURE_TYPE
);
1433 dwarf_strp(dwarf_info_section
,
1434 (t
->v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
1435 ? "" : get_tok_str(t
->v
& ~SYM_STRUCT
, NULL
));
1436 dwarf_uleb128(dwarf_info_section
, t
->c
);
1437 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1438 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
1439 pos_sib
= dwarf_info_section
->data_offset
;
1440 dwarf_data4(dwarf_info_section
, 0);
1445 dwarf_data1(dwarf_info_section
,
1446 e
->type
.t
& VT_BITFIELD
? DWARF_ABBREV_MEMBER_BF
1447 : DWARF_ABBREV_MEMBER
);
1448 dwarf_strp(dwarf_info_section
,
1449 (e
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
1450 ? "" : get_tok_str(e
->v
& ~SYM_FIELD
, NULL
));
1451 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1452 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
1453 pos_type
[i
++] = dwarf_info_section
->data_offset
;
1454 dwarf_data4(dwarf_info_section
, 0);
1455 if (e
->type
.t
& VT_BITFIELD
) {
1456 int pos
= e
->c
* 8 + BIT_POS(e
->type
.t
);
1457 int size
= BIT_SIZE(e
->type
.t
);
1459 dwarf_uleb128(dwarf_info_section
, size
);
1460 dwarf_uleb128(dwarf_info_section
, pos
);
1463 dwarf_uleb128(dwarf_info_section
, e
->c
);
1465 dwarf_data1(dwarf_info_section
, 0);
1466 write32le(dwarf_info_section
->data
+ pos_sib
,
1467 dwarf_info_section
->data_offset
- dwarf_info
.start
);
1472 type
= tcc_get_dwarf_info(s1
, e
);
1473 tcc_debug_check_anon(s1
, e
, pos_type
[i
]);
1474 write32le(dwarf_info_section
->data
+ pos_type
[i
++],
1475 type
- dwarf_info
.start
);
1479 tcc_debug_remove(s1
, t
);
1482 else if (IS_ENUM(type
)) {
1484 debug_type
= tcc_debug_find(s1
, t
, 1);
1485 if (debug_type
== -1) {
1486 int pos_sib
, pos_type
;
1487 Sym sym
= {0}; sym
.type
.t
= VT_INT
| (type
& VT_UNSIGNED
);
1489 pos_type
= tcc_get_dwarf_info(s1
, &sym
);
1490 debug_type
= tcc_debug_add(s1
, t
, 1);
1491 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_ENUMERATION_TYPE
);
1492 dwarf_strp(dwarf_info_section
,
1493 (t
->v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
1494 ? "" : get_tok_str(t
->v
& ~SYM_STRUCT
, NULL
));
1495 dwarf_data1(dwarf_info_section
,
1496 type
& VT_UNSIGNED
? DW_ATE_unsigned
: DW_ATE_signed
);
1497 dwarf_data1(dwarf_info_section
, 4);
1498 dwarf_data4(dwarf_info_section
, pos_type
- dwarf_info
.start
);
1499 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1500 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
1501 pos_sib
= dwarf_info_section
->data_offset
;
1502 dwarf_data4(dwarf_info_section
, 0);
1506 dwarf_data1(dwarf_info_section
,
1507 type
& VT_UNSIGNED
? DWARF_ABBREV_ENUMERATOR_UNSIGNED
1508 : DWARF_ABBREV_ENUMERATOR_SIGNED
);
1509 dwarf_strp(dwarf_info_section
,
1510 (e
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
1511 ? "" : get_tok_str(e
->v
& ~SYM_FIELD
, NULL
));
1512 if (type
& VT_UNSIGNED
)
1513 dwarf_uleb128(dwarf_info_section
, e
->enum_val
);
1515 dwarf_sleb128(dwarf_info_section
, e
->enum_val
);
1517 dwarf_data1(dwarf_info_section
, 0);
1518 write32le(dwarf_info_section
->data
+ pos_sib
,
1519 dwarf_info_section
->data_offset
- dwarf_info
.start
);
1521 tcc_debug_remove(s1
, t
);
1524 else if ((type
& VT_BTYPE
) != VT_FUNC
) {
1525 type
&= ~VT_STRUCT_MASK
;
1526 for (i
= 1; i
<= N_DEFAULT_DEBUG
; i
++)
1527 if (default_debug
[i
- 1].type
== type
)
1529 if (i
> N_DEFAULT_DEBUG
)
1531 debug_type
= dwarf_info
.base_type_used
[i
- 1];
1532 if (debug_type
== 0) {
1535 debug_type
= dwarf_info_section
->data_offset
;
1536 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_BASE_TYPE
);
1537 dwarf_uleb128(dwarf_info_section
, default_debug
[i
- 1].size
);
1538 dwarf_data1(dwarf_info_section
, default_debug
[i
- 1].encoding
);
1539 strncpy(name
, default_debug
[i
- 1].name
, sizeof(name
) -1);
1540 *strchr(name
, ':') = 0;
1541 dwarf_strp(dwarf_info_section
, name
);
1542 dwarf_info
.base_type_used
[i
- 1] = debug_type
;
1545 retval
= debug_type
;
1549 type
= t
->type
.t
& ~(VT_STORAGE
| VT_CONSTANT
| VT_VOLATILE
| VT_VLA
);
1550 if ((type
& VT_BTYPE
) != VT_BYTE
)
1551 type
&= ~VT_DEFSIGN
;
1552 if (type
== VT_PTR
) {
1553 i
= dwarf_info_section
->data_offset
;
1554 if (retval
== debug_type
)
1556 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_POINTER
);
1557 dwarf_data1(dwarf_info_section
, PTR_SIZE
);
1558 if (last_pos
!= -1) {
1559 tcc_debug_check_anon(s1
, e
, last_pos
);
1560 write32le(dwarf_info_section
->data
+ last_pos
,
1561 i
- dwarf_info
.start
);
1563 last_pos
= dwarf_info_section
->data_offset
;
1565 dwarf_data4(dwarf_info_section
, 0);
1567 else if (type
== (VT_PTR
| VT_ARRAY
)) {
1568 int sib_pos
, sub_type
;
1570 Sym sym
= {0}; sym
.type
.t
= VT_LONG
| VT_INT
| VT_UNSIGNED
;
1572 Sym sym
= {0}; sym
.type
.t
= VT_LLONG
| VT_LONG
| VT_UNSIGNED
;
1575 sub_type
= tcc_get_dwarf_info(s1
, &sym
);
1576 i
= dwarf_info_section
->data_offset
;
1577 if (retval
== debug_type
)
1579 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_ARRAY_TYPE
);
1580 if (last_pos
!= -1) {
1581 tcc_debug_check_anon(s1
, e
, last_pos
);
1582 write32le(dwarf_info_section
->data
+ last_pos
,
1583 i
- dwarf_info
.start
);
1585 last_pos
= dwarf_info_section
->data_offset
;
1587 dwarf_data4(dwarf_info_section
, 0);
1588 sib_pos
= dwarf_info_section
->data_offset
;
1589 dwarf_data4(dwarf_info_section
, 0);
1591 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_SUBRANGE_TYPE
);
1592 dwarf_data4(dwarf_info_section
, sub_type
- dwarf_info
.start
);
1593 dwarf_uleb128(dwarf_info_section
, t
->type
.ref
->c
- 1);
1595 type
= s
->type
.t
& ~(VT_STORAGE
| VT_CONSTANT
| VT_VOLATILE
);
1596 if (type
!= (VT_PTR
| VT_ARRAY
))
1600 dwarf_data1(dwarf_info_section
, 0);
1601 write32le(dwarf_info_section
->data
+ sib_pos
,
1602 dwarf_info_section
->data_offset
- dwarf_info
.start
);
1604 else if (type
== VT_FUNC
) {
1605 int sib_pos
, *pos_type
;
1608 i
= dwarf_info_section
->data_offset
;
1609 debug_type
= tcc_get_dwarf_info(s1
, t
->type
.ref
);
1610 if (retval
== debug_type
)
1612 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_SUBROUTINE_TYPE
);
1613 if (last_pos
!= -1) {
1614 tcc_debug_check_anon(s1
, e
, last_pos
);
1615 write32le(dwarf_info_section
->data
+ last_pos
,
1616 i
- dwarf_info
.start
);
1618 last_pos
= dwarf_info_section
->data_offset
;
1620 dwarf_data4(dwarf_info_section
, 0);
1621 sib_pos
= dwarf_info_section
->data_offset
;
1622 dwarf_data4(dwarf_info_section
, 0);
1629 pos_type
= (int *) tcc_malloc(i
* sizeof(int));
1634 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_FORMAL_PARAMETER2
);
1635 pos_type
[i
++] = dwarf_info_section
->data_offset
;
1636 dwarf_data4(dwarf_info_section
, 0);
1638 dwarf_data1(dwarf_info_section
, 0);
1639 write32le(dwarf_info_section
->data
+ sib_pos
,
1640 dwarf_info_section
->data_offset
- dwarf_info
.start
);
1645 type
= tcc_get_dwarf_info(s1
, f
);
1646 tcc_debug_check_anon(s1
, f
, pos_type
[i
]);
1647 write32le(dwarf_info_section
->data
+ pos_type
[i
++],
1648 type
- dwarf_info
.start
);
1653 if (last_pos
!= -1) {
1654 tcc_debug_check_anon(s1
, e
, last_pos
);
1655 write32le(dwarf_info_section
->data
+ last_pos
,
1656 debug_type
- dwarf_info
.start
);
1665 static void tcc_debug_finish (TCCState
*s1
, struct _debug_info
*cur
)
1668 struct _debug_info
*next
= cur
->next
;
1673 for (i
= cur
->n_sym
- 1; i
>= 0; i
--) {
1674 struct debug_sym
*s
= &cur
->sym
[i
];
1676 dwarf_data1(dwarf_info_section
,
1678 ? DWARF_ABBREV_FORMAL_PARAMETER
1680 ? DWARF_ABBREV_VARIABLE_EXTERNAL
1681 : s
->type
== N_STSYM
1682 ? DWARF_ABBREV_VARIABLE_STATIC
1683 : DWARF_ABBREV_VARIABLE_LOCAL
);
1684 dwarf_strp(dwarf_info_section
, s
->str
);
1685 if (s
->type
== N_GSYM
|| s
->type
== N_STSYM
) {
1686 dwarf_uleb128(dwarf_info_section
, s
->file
);
1687 dwarf_uleb128(dwarf_info_section
, s
->line
);
1689 dwarf_data4(dwarf_info_section
, s
->info
- dwarf_info
.start
);
1690 if (s
->type
== N_GSYM
|| s
->type
== N_STSYM
) {
1692 if (s
->type
== N_GSYM
)
1693 dwarf_data1(dwarf_info_section
, 1);
1694 dwarf_data1(dwarf_info_section
, PTR_SIZE
+ 1);
1695 dwarf_data1(dwarf_info_section
, DW_OP_addr
);
1696 if (s
->type
== N_STSYM
)
1697 dwarf_reloc(dwarf_info_section
, section_sym
, R_DATA_PTR
);
1699 dwarf_data4(dwarf_info_section
, s
->value
);
1701 dwarf_data8(dwarf_info_section
, s
->value
);
1706 dwarf_data1(dwarf_info_section
, dwarf_sleb128_size(s
->value
) + 1);
1707 dwarf_data1(dwarf_info_section
, DW_OP_fbreg
);
1708 dwarf_sleb128(dwarf_info_section
, s
->value
);
1712 tcc_free (cur
->sym
);
1713 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_LEXICAL_BLOCK
);
1714 dwarf_reloc(dwarf_info_section
, section_sym
, R_DATA_PTR
);
1716 dwarf_data4(dwarf_info_section
, func_ind
+ cur
->start
);
1717 dwarf_data4(dwarf_info_section
, cur
->end
- cur
->start
);
1719 dwarf_data8(dwarf_info_section
, func_ind
+ cur
->start
);
1720 dwarf_data8(dwarf_info_section
, cur
->end
- cur
->start
);
1722 tcc_debug_finish (s1
, cur
->child
);
1723 dwarf_data1(dwarf_info_section
, 0);
1727 for (i
= 0; i
< cur
->n_sym
; i
++) {
1728 struct debug_sym
*s
= &cur
->sym
[i
];
1731 put_stabs_r(s1
, s
->str
, s
->type
, 0, 0, s
->value
,
1732 s
->sec
, s
->sym_index
);
1734 put_stabs(s1
, s
->str
, s
->type
, 0, 0, s
->value
);
1737 tcc_free (cur
->sym
);
1738 put_stabn(s1
, N_LBRAC
, 0, 0, cur
->start
);
1739 tcc_debug_finish (s1
, cur
->child
);
1740 put_stabn(s1
, N_RBRAC
, 0, 0, cur
->end
);
1747 ST_FUNC
void tcc_add_debug_info(TCCState
*s1
, int param
, Sym
*s
, Sym
*e
)
1752 cstr_new (&debug_str
);
1753 for (; s
!= e
; s
= s
->prev
) {
1754 if (!s
->v
|| (s
->r
& VT_VALMASK
) != VT_LOCAL
)
1757 tcc_debug_stabs(s1
, get_tok_str(s
->v
, NULL
),
1758 param
? N_PSYM
: N_LSYM
, s
->c
, NULL
, 0,
1759 tcc_get_dwarf_info(s1
, s
));
1763 cstr_reset (&debug_str
);
1764 cstr_printf (&debug_str
, "%s:%s", get_tok_str(s
->v
, NULL
),
1766 tcc_get_debug_info(s1
, s
, &debug_str
);
1767 tcc_debug_stabs(s1
, debug_str
.data
, param
? N_PSYM
: N_LSYM
,
1771 cstr_free (&debug_str
);
1774 /* put function symbol */
1775 ST_FUNC
void tcc_debug_funcstart(TCCState
*s1
, Sym
*sym
)
1782 debug_info_root
= NULL
;
1784 tcc_debug_stabn(s1
, N_LBRAC
, ind
- func_ind
);
1785 f
= put_new_file(s1
);
1791 dwarf_line_op(s1
, DW_LNS_copy
);
1792 dwarf_info
.func
= sym
;
1793 dwarf_info
.line
= file
->line_num
;
1794 if (s1
->do_backtrace
) {
1797 dwarf_line_op(s1
, 0); // extended
1798 dwarf_uleb128_op(s1
, strlen(funcname
) + 2);
1799 dwarf_line_op(s1
, DW_LNE_hi_user
- 1);
1800 len
= strlen(funcname
) + 1;
1801 for (i
= 0; i
< len
; i
++)
1802 dwarf_line_op(s1
, funcname
[i
]);
1807 cstr_new (&debug_str
);
1808 cstr_printf(&debug_str
, "%s:%c", funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
1809 tcc_get_debug_info(s1
, sym
->type
.ref
, &debug_str
);
1810 put_stabs_r(s1
, debug_str
.data
, N_FUN
, 0, f
->line_num
, 0, cur_text_section
, sym
->c
);
1811 cstr_free (&debug_str
);
1816 /* put function size */
1817 ST_FUNC
void tcc_debug_funcend(TCCState
*s1
, int size
)
1822 tcc_debug_stabn(s1
, N_RBRAC
, size
);
1825 Sym
*sym
= dwarf_info
.func
;
1826 int n_debug_info
= tcc_get_dwarf_info(s1
, sym
->type
.ref
);
1828 dwarf_data1(dwarf_info_section
,
1829 sym
->type
.t
& VT_STATIC
? DWARF_ABBREV_SUBPROGRAM_STATIC
1830 : DWARF_ABBREV_SUBPROGRAM_EXTERNAL
);
1831 if ((sym
->type
.t
& VT_STATIC
) == 0)
1832 dwarf_data1(dwarf_info_section
, 1);
1833 dwarf_strp(dwarf_info_section
, funcname
);
1834 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1835 dwarf_uleb128(dwarf_info_section
, dwarf_info
.line
);
1836 tcc_debug_check_anon(s1
, sym
->type
.ref
, dwarf_info_section
->data_offset
);
1837 dwarf_data4(dwarf_info_section
, n_debug_info
- dwarf_info
.start
);
1838 dwarf_reloc(dwarf_info_section
, section_sym
, R_DATA_PTR
);
1840 dwarf_data4(dwarf_info_section
, func_ind
); // low_pc
1841 dwarf_data4(dwarf_info_section
, size
); // high_pc
1843 dwarf_data8(dwarf_info_section
, func_ind
); // low_pc
1844 dwarf_data8(dwarf_info_section
, size
); // high_pc
1846 func_sib
= dwarf_info_section
->data_offset
;
1847 dwarf_data4(dwarf_info_section
, 0); // sibling
1848 dwarf_data1(dwarf_info_section
, 1);
1849 #if defined(TCC_TARGET_I386)
1850 dwarf_data1(dwarf_info_section
, DW_OP_reg5
); // ebp
1851 #elif defined(TCC_TARGET_X86_64)
1852 dwarf_data1(dwarf_info_section
, DW_OP_reg6
); // rbp
1853 #elif defined TCC_TARGET_ARM
1854 dwarf_data1(dwarf_info_section
, DW_OP_reg13
); // sp
1855 #elif defined TCC_TARGET_ARM64
1856 dwarf_data1(dwarf_info_section
, DW_OP_reg29
); // reg 29
1857 #elif defined TCC_TARGET_RISCV64
1858 dwarf_data1(dwarf_info_section
, DW_OP_reg8
); // r8(s0)
1860 dwarf_data1(dwarf_info_section
, DW_OP_call_frame_cfa
);
1862 tcc_debug_finish (s1
, debug_info_root
);
1863 dwarf_data1(dwarf_info_section
, 0);
1864 write32le(dwarf_info_section
->data
+ func_sib
,
1865 dwarf_info_section
->data_offset
- dwarf_info
.start
);
1869 tcc_debug_finish (s1
, debug_info_root
);
1874 ST_FUNC
void tcc_debug_extern_sym(TCCState
*s1
, Sym
*sym
, int sh_num
, int sym_bind
, int sym_type
)
1878 if (sym_type
== STT_FUNC
|| sym
->v
>= SYM_FIRST_ANOM
)
1883 debug_type
= tcc_get_dwarf_info(s1
, sym
);
1884 dwarf_data1(dwarf_info_section
,
1885 sym_bind
== STB_GLOBAL
1886 ? DWARF_ABBREV_VARIABLE_EXTERNAL
1887 : DWARF_ABBREV_VARIABLE_STATIC
);
1888 dwarf_strp(dwarf_info_section
, get_tok_str(sym
->v
, NULL
));
1889 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1890 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
1891 tcc_debug_check_anon(s1
, sym
, dwarf_info_section
->data_offset
);
1892 dwarf_data4(dwarf_info_section
, debug_type
- dwarf_info
.start
);
1893 if (sym_bind
== STB_GLOBAL
)
1894 dwarf_data1(dwarf_info_section
, 1);
1895 dwarf_data1(dwarf_info_section
, PTR_SIZE
+ 1);
1896 dwarf_data1(dwarf_info_section
, DW_OP_addr
);
1897 greloca(dwarf_info_section
, sym
, dwarf_info_section
->data_offset
,
1900 dwarf_data4(dwarf_info_section
, 0);
1902 dwarf_data8(dwarf_info_section
, 0);
1907 Section
*s
= s1
->sections
[sh_num
];
1911 cstr_printf (&str
, "%s:%c",
1912 get_tok_str(sym
->v
, NULL
),
1913 sym_bind
== STB_GLOBAL
? 'G' : func_ind
!= -1 ? 'V' : 'S'
1915 tcc_get_debug_info(s1
, sym
, &str
);
1916 if (sym_bind
== STB_GLOBAL
)
1917 tcc_debug_stabs(s1
, str
.data
, N_GSYM
, 0, NULL
, 0, 0);
1919 tcc_debug_stabs(s1
, str
.data
,
1920 (sym
->type
.t
& VT_STATIC
) && data_section
== s
1921 ? N_STSYM
: N_LCSYM
, 0, s
, sym
->c
, 0);
1926 ST_FUNC
void tcc_debug_typedef(TCCState
*s1
, Sym
*sym
)
1933 debug_type
= tcc_get_dwarf_info(s1
, sym
);
1934 if (debug_type
!= -1) {
1935 dwarf_data1(dwarf_info_section
, DWARF_ABBREV_TYPEDEF
);
1936 dwarf_strp(dwarf_info_section
, get_tok_str(sym
->v
& ~SYM_FIELD
, NULL
));
1937 dwarf_uleb128(dwarf_info_section
, dwarf_line
.cur_file
);
1938 dwarf_uleb128(dwarf_info_section
, file
->line_num
);
1939 tcc_debug_check_anon(s1
, sym
, dwarf_info_section
->data_offset
);
1940 dwarf_data4(dwarf_info_section
, debug_type
- dwarf_info
.start
);
1947 cstr_printf (&str
, "%s:t",
1948 (sym
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
1949 ? "" : get_tok_str(sym
->v
& ~SYM_FIELD
, NULL
));
1950 tcc_get_debug_info(s1
, sym
, &str
);
1951 tcc_debug_stabs(s1
, str
.data
, N_LSYM
, 0, NULL
, 0, 0);
1956 /* ------------------------------------------------------------------------- */
1957 /* for section layout see lib/tcov.c */
1959 ST_FUNC
void tcc_tcov_block_end(TCCState
*s1
, int line
);
1961 ST_FUNC
void tcc_tcov_block_begin(TCCState
*s1
)
1965 unsigned long last_offset
= tcov_data
.offset
;
1967 tcc_tcov_block_end (tcc_state
, 0);
1968 if (s1
->test_coverage
== 0 || nocode_wanted
)
1971 if (tcov_data
.last_file_name
== 0 ||
1972 strcmp ((const char *)(tcov_section
->data
+ tcov_data
.last_file_name
),
1973 file
->true_filename
) != 0) {
1977 if (tcov_data
.last_func_name
)
1978 section_ptr_add(tcov_section
, 1);
1979 if (tcov_data
.last_file_name
)
1980 section_ptr_add(tcov_section
, 1);
1981 tcov_data
.last_func_name
= 0;
1983 if (file
->true_filename
[0] == '/') {
1984 tcov_data
.last_file_name
= tcov_section
->data_offset
;
1985 cstr_printf (&cstr
, "%s", file
->true_filename
);
1988 getcwd (wd
, sizeof(wd
));
1989 tcov_data
.last_file_name
= tcov_section
->data_offset
+ strlen(wd
) + 1;
1990 cstr_printf (&cstr
, "%s/%s", wd
, file
->true_filename
);
1992 ptr
= section_ptr_add(tcov_section
, cstr
.size
+ 1);
1993 strcpy((char *)ptr
, cstr
.data
);
1995 normalize_slashes((char *)ptr
);
1999 if (tcov_data
.last_func_name
== 0 ||
2000 strcmp ((const char *)(tcov_section
->data
+ tcov_data
.last_func_name
),
2004 if (tcov_data
.last_func_name
)
2005 section_ptr_add(tcov_section
, 1);
2006 tcov_data
.last_func_name
= tcov_section
->data_offset
;
2007 len
= strlen (funcname
);
2008 ptr
= section_ptr_add(tcov_section
, len
+ 1);
2009 strcpy((char *)ptr
, funcname
);
2010 section_ptr_add(tcov_section
, -tcov_section
->data_offset
& 7);
2011 ptr
= section_ptr_add(tcov_section
, 8);
2012 write64le (ptr
, file
->line_num
);
2014 if (ind
== tcov_data
.ind
&& tcov_data
.line
== file
->line_num
)
2015 tcov_data
.offset
= last_offset
;
2018 label
.type
.t
= VT_LLONG
| VT_STATIC
;
2020 ptr
= section_ptr_add(tcov_section
, 16);
2021 tcov_data
.line
= file
->line_num
;
2022 write64le (ptr
, (tcov_data
.line
<< 8) | 0xff);
2023 put_extern_sym(&label
, tcov_section
,
2024 ((unsigned char *)ptr
- tcov_section
->data
) + 8, 0);
2025 sv
.type
= label
.type
;
2026 sv
.r
= VT_SYM
| VT_LVAL
| VT_CONST
;
2030 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 || \
2031 defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 || \
2032 defined TCC_TARGET_RISCV64
2033 gen_increment_tcov (&sv
);
2039 tcov_data
.offset
= (unsigned char *)ptr
- tcov_section
->data
;
2040 tcov_data
.ind
= ind
;
2044 ST_FUNC
void tcc_tcov_block_end(TCCState
*s1
, int line
)
2046 if (s1
->test_coverage
== 0)
2049 line
= tcov_data
.line
;
2050 if (tcov_data
.offset
) {
2051 void *ptr
= tcov_section
->data
+ tcov_data
.offset
;
2052 unsigned long long nline
= line
? line
: file
->line_num
;
2054 write64le (ptr
, (read64le (ptr
) & 0xfffffffffull
) | (nline
<< 36));
2055 tcov_data
.offset
= 0;
2059 ST_FUNC
void tcc_tcov_check_line(TCCState
*s1
, int start
)
2061 if (s1
->test_coverage
== 0)
2063 if (tcov_data
.line
!= file
->line_num
) {
2064 if ((tcov_data
.line
+ 1) != file
->line_num
) {
2065 tcc_tcov_block_end (s1
, -1);
2067 tcc_tcov_block_begin (s1
);
2070 tcov_data
.line
= file
->line_num
;
2074 ST_FUNC
void tcc_tcov_start(TCCState
*s1
)
2076 if (s1
->test_coverage
== 0)
2079 s1
->dState
= tcc_mallocz(sizeof *s1
->dState
);
2080 memset (&tcov_data
, 0, sizeof (tcov_data
));
2081 if (tcov_section
== NULL
) {
2082 tcov_section
= new_section(tcc_state
, ".tcov", SHT_PROGBITS
,
2083 SHF_ALLOC
| SHF_WRITE
);
2084 section_ptr_add(tcov_section
, 4); // pointer to executable name
2088 ST_FUNC
void tcc_tcov_end(TCCState
*s1
)
2090 if (s1
->test_coverage
== 0)
2092 if (tcov_data
.last_func_name
)
2093 section_ptr_add(tcov_section
, 1);
2094 if (tcov_data
.last_file_name
)
2095 section_ptr_add(tcov_section
, 1);
2098 ST_FUNC
void tcc_tcov_reset_ind(TCCState
*s1
)
2103 /* ------------------------------------------------------------------------- */
2104 #undef last_line_num
2107 #undef debug_next_type
2110 #undef debug_anon_hash
2111 #undef n_debug_anon_hash
2113 #undef debug_info_root