-
Notifications
You must be signed in to change notification settings - Fork 0
/
tccdbg.c
2169 lines (2022 loc) · 70.4 KB
/
tccdbg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* TCC - Tiny C Compiler
*
* Copyright (c) 2001-2004 Fabrice Bellard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tcc.h"
/* stab debug support */
static const struct {
int type;
int size;
int encoding;
const char *name;
} default_debug[] = {
{ VT_INT, 4, DW_ATE_signed, "int:t1=r1;-2147483648;2147483647;" },
{ VT_BYTE, 1, DW_ATE_signed_char, "char:t2=r2;0;127;" },
#if LONG_SIZE == 4
{ VT_LONG | VT_INT, 4, DW_ATE_signed, "long int:t3=r3;-2147483648;2147483647;" },
#else
{ VT_LLONG | VT_LONG, 8, DW_ATE_signed, "long int:t3=r3;-9223372036854775808;9223372036854775807;" },
#endif
{ VT_INT | VT_UNSIGNED, 4, DW_ATE_unsigned, "unsigned int:t4=r4;0;037777777777;" },
#if LONG_SIZE == 4
{ VT_LONG | VT_INT | VT_UNSIGNED, 4, DW_ATE_unsigned, "long unsigned int:t5=r5;0;037777777777;" },
#else
/* use octal instead of -1 so size_t works (-gstabs+ in gcc) */
{ VT_LLONG | VT_LONG | VT_UNSIGNED, 8, DW_ATE_unsigned, "long unsigned int:t5=r5;0;01777777777777777777777;" },
#endif
{ VT_QLONG, 16, DW_ATE_signed, "__int128:t6=r6;0;-1;" },
{ VT_QLONG | VT_UNSIGNED, 16, DW_ATE_unsigned, "__int128 unsigned:t7=r7;0;-1;" },
{ VT_LLONG, 8, DW_ATE_signed, "long long int:t8=r8;-9223372036854775808;9223372036854775807;" },
{ VT_LLONG | VT_UNSIGNED, 8, DW_ATE_unsigned, "long long unsigned int:t9=r9;0;01777777777777777777777;" },
{ VT_SHORT, 2, DW_ATE_signed, "short int:t10=r10;-32768;32767;" },
{ VT_SHORT | VT_UNSIGNED, 2, DW_ATE_unsigned, "short unsigned int:t11=r11;0;65535;" },
{ VT_BYTE | VT_DEFSIGN, 1, DW_ATE_signed_char, "signed char:t12=r12;-128;127;" },
{ VT_BYTE | VT_DEFSIGN | VT_UNSIGNED, 1, DW_ATE_unsigned_char, "unsigned char:t13=r13;0;255;" },
{ VT_FLOAT, 4, DW_ATE_float, "float:t14=r1;4;0;" },
{ VT_DOUBLE, 8, DW_ATE_float, "double:t15=r1;8;0;" },
#ifdef TCC_USING_DOUBLE_FOR_LDOUBLE
{ VT_DOUBLE | VT_LONG, 8, DW_ATE_float, "long double:t16=r1;8;0;" },
#else
{ VT_LDOUBLE, 16, DW_ATE_float, "long double:t16=r1;16;0;" },
#endif
{ -1, -1, -1, "_Float32:t17=r1;4;0;" },
{ -1, -1, -1, "_Float64:t18=r1;8;0;" },
{ -1, -1, -1, "_Float128:t19=r1;16;0;" },
{ -1, -1, -1, "_Float32x:t20=r1;8;0;" },
{ -1, -1, -1, "_Float64x:t21=r1;16;0;" },
{ -1, -1, -1, "_Decimal32:t22=r1;4;0;" },
{ -1, -1, -1, "_Decimal64:t23=r1;8;0;" },
{ -1, -1, -1, "_Decimal128:t24=r1;16;0;" },
/* if default char is unsigned */
{ VT_BYTE | VT_UNSIGNED, 1, DW_ATE_unsigned_char, "unsigned char:t25=r25;0;255;" },
/* boolean type */
{ VT_BOOL, 1, DW_ATE_boolean, "bool:t26=r26;0;255;" },
#if LONG_SIZE == 4
{ VT_VOID, 1, DW_ATE_unsigned_char, "void:t27=27" },
#else
/* bitfields use these */
{ VT_LONG | VT_INT, 8, DW_ATE_signed, "long int:t27=r27;-9223372036854775808;9223372036854775807;" },
{ VT_LONG | VT_INT | VT_UNSIGNED, 8, DW_ATE_unsigned, "long unsigned int:t28=r28;0;01777777777777777777777;" },
{ VT_VOID, 1, DW_ATE_unsigned_char, "void:t29=29" },
#endif
};
#define N_DEFAULT_DEBUG (sizeof (default_debug) / sizeof (default_debug[0]))
/* dwarf debug */
#define DWARF_LINE_BASE -5
#define DWARF_LINE_RANGE 14
#define DWARF_OPCODE_BASE 13
#if defined TCC_TARGET_ARM64
#define DWARF_MIN_INSTR_LEN 4
#elif defined TCC_TARGET_ARM
#define DWARF_MIN_INSTR_LEN 2
#else
#define DWARF_MIN_INSTR_LEN 1
#endif
#define DWARF_ABBREV_COMPILE_UNIT 1
#define DWARF_ABBREV_BASE_TYPE 2
#define DWARF_ABBREV_VARIABLE_EXTERNAL 3
#define DWARF_ABBREV_VARIABLE_STATIC 4
#define DWARF_ABBREV_VARIABLE_LOCAL 5
#define DWARF_ABBREV_FORMAL_PARAMETER 6
#define DWARF_ABBREV_POINTER 7
#define DWARF_ABBREV_ARRAY_TYPE 8
#define DWARF_ABBREV_SUBRANGE_TYPE 9
#define DWARF_ABBREV_TYPEDEF 10
#define DWARF_ABBREV_ENUMERATOR_SIGNED 11
#define DWARF_ABBREV_ENUMERATOR_UNSIGNED 12
#define DWARF_ABBREV_ENUMERATION_TYPE 13
#define DWARF_ABBREV_MEMBER 14
#define DWARF_ABBREV_MEMBER_BF 15
#define DWARF_ABBREV_STRUCTURE_TYPE 16
#define DWARF_ABBREV_STRUCTURE_EMPTY_TYPE 17
#define DWARF_ABBREV_UNION_TYPE 18
#define DWARF_ABBREV_UNION_EMPTY_TYPE 19
#define DWARF_ABBREV_SUBPROGRAM_EXTERNAL 20
#define DWARF_ABBREV_SUBPROGRAM_STATIC 21
#define DWARF_ABBREV_LEXICAL_BLOCK 22
#define DWARF_ABBREV_LEXICAL_EMPTY_BLOCK 23
#define DWARF_ABBREV_SUBROUTINE_TYPE 24
#define DWARF_ABBREV_SUBROUTINE_EMPTY_TYPE 25
#define DWARF_ABBREV_FORMAL_PARAMETER2 26
/* all entries should have been generated with dwarf_uleb128 except
has_children. All values are currently below 128 so this currently
works. */
static const unsigned char dwarf_abbrev_init[] = {
DWARF_ABBREV_COMPILE_UNIT, DW_TAG_compile_unit, 1,
DW_AT_producer, DW_FORM_strp,
DW_AT_language, DW_FORM_data1,
DW_AT_name, DW_FORM_line_strp,
DW_AT_comp_dir, DW_FORM_line_strp,
DW_AT_low_pc, DW_FORM_addr,
#if PTR_SIZE == 4
DW_AT_high_pc, DW_FORM_data4,
#else
DW_AT_high_pc, DW_FORM_data8,
#endif
DW_AT_stmt_list, DW_FORM_sec_offset,
0, 0,
DWARF_ABBREV_BASE_TYPE, DW_TAG_base_type, 0,
DW_AT_byte_size, DW_FORM_udata,
DW_AT_encoding, DW_FORM_data1,
DW_AT_name, DW_FORM_strp,
0, 0,
DWARF_ABBREV_VARIABLE_EXTERNAL, DW_TAG_variable, 0,
DW_AT_name, DW_FORM_strp,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
DW_AT_type, DW_FORM_ref4,
DW_AT_external, DW_FORM_flag,
DW_AT_location, DW_FORM_exprloc,
0, 0,
DWARF_ABBREV_VARIABLE_STATIC, DW_TAG_variable, 0,
DW_AT_name, DW_FORM_strp,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
DW_AT_type, DW_FORM_ref4,
DW_AT_location, DW_FORM_exprloc,
0, 0,
DWARF_ABBREV_VARIABLE_LOCAL, DW_TAG_variable, 0,
DW_AT_name, DW_FORM_strp,
DW_AT_type, DW_FORM_ref4,
DW_AT_location, DW_FORM_exprloc,
0, 0,
DWARF_ABBREV_FORMAL_PARAMETER, DW_TAG_formal_parameter, 0,
DW_AT_name, DW_FORM_strp,
DW_AT_type, DW_FORM_ref4,
DW_AT_location, DW_FORM_exprloc,
0, 0,
DWARF_ABBREV_POINTER, DW_TAG_pointer_type, 0,
DW_AT_byte_size, DW_FORM_data1,
DW_AT_type, DW_FORM_ref4,
0, 0,
DWARF_ABBREV_ARRAY_TYPE, DW_TAG_array_type, 1,
DW_AT_type, DW_FORM_ref4,
DW_AT_sibling, DW_FORM_ref4,
0, 0,
DWARF_ABBREV_SUBRANGE_TYPE, DW_TAG_subrange_type, 0,
DW_AT_type, DW_FORM_ref4,
DW_AT_upper_bound, DW_FORM_udata,
0, 0,
DWARF_ABBREV_TYPEDEF, DW_TAG_typedef, 0,
DW_AT_name, DW_FORM_strp,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
DW_AT_type, DW_FORM_ref4,
0, 0,
DWARF_ABBREV_ENUMERATOR_SIGNED, DW_TAG_enumerator, 0,
DW_AT_name, DW_FORM_strp,
DW_AT_const_value, DW_FORM_sdata,
0, 0,
DWARF_ABBREV_ENUMERATOR_UNSIGNED, DW_TAG_enumerator, 0,
DW_AT_name, DW_FORM_strp,
DW_AT_const_value, DW_FORM_udata,
0, 0,
DWARF_ABBREV_ENUMERATION_TYPE, DW_TAG_enumeration_type, 1,
DW_AT_name, DW_FORM_strp,
DW_AT_encoding, DW_FORM_data1,
DW_AT_byte_size, DW_FORM_data1,
DW_AT_type, DW_FORM_ref4,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
DW_AT_sibling, DW_FORM_ref4,
0, 0,
DWARF_ABBREV_MEMBER, DW_TAG_member, 0,
DW_AT_name, DW_FORM_strp,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
DW_AT_type, DW_FORM_ref4,
DW_AT_data_member_location, DW_FORM_udata,
0, 0,
DWARF_ABBREV_MEMBER_BF, DW_TAG_member, 0,
DW_AT_name, DW_FORM_strp,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
DW_AT_type, DW_FORM_ref4,
DW_AT_bit_size, DW_FORM_udata,
DW_AT_data_bit_offset, DW_FORM_udata,
0, 0,
DWARF_ABBREV_STRUCTURE_TYPE, DW_TAG_structure_type, 1,
DW_AT_name, DW_FORM_strp,
DW_AT_byte_size, DW_FORM_udata,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
DW_AT_sibling, DW_FORM_ref4,
0, 0,
DWARF_ABBREV_STRUCTURE_EMPTY_TYPE, DW_TAG_structure_type, 0,
DW_AT_name, DW_FORM_strp,
DW_AT_byte_size, DW_FORM_udata,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
0, 0,
DWARF_ABBREV_UNION_TYPE, DW_TAG_union_type, 1,
DW_AT_name, DW_FORM_strp,
DW_AT_byte_size, DW_FORM_udata,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
DW_AT_sibling, DW_FORM_ref4,
0, 0,
DWARF_ABBREV_UNION_EMPTY_TYPE, DW_TAG_union_type, 0,
DW_AT_name, DW_FORM_strp,
DW_AT_byte_size, DW_FORM_udata,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
0, 0,
DWARF_ABBREV_SUBPROGRAM_EXTERNAL, DW_TAG_subprogram, 1,
DW_AT_external, DW_FORM_flag,
DW_AT_name, DW_FORM_strp,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
DW_AT_type, DW_FORM_ref4,
DW_AT_low_pc, DW_FORM_addr,
#if PTR_SIZE == 4
DW_AT_high_pc, DW_FORM_data4,
#else
DW_AT_high_pc, DW_FORM_data8,
#endif
DW_AT_sibling, DW_FORM_ref4,
DW_AT_frame_base, DW_FORM_exprloc,
0, 0,
DWARF_ABBREV_SUBPROGRAM_STATIC, DW_TAG_subprogram, 1,
DW_AT_name, DW_FORM_strp,
DW_AT_decl_file, DW_FORM_udata,
DW_AT_decl_line, DW_FORM_udata,
DW_AT_type, DW_FORM_ref4,
DW_AT_low_pc, DW_FORM_addr,
#if PTR_SIZE == 4
DW_AT_high_pc, DW_FORM_data4,
#else
DW_AT_high_pc, DW_FORM_data8,
#endif
DW_AT_sibling, DW_FORM_ref4,
DW_AT_frame_base, DW_FORM_exprloc,
0, 0,
DWARF_ABBREV_LEXICAL_BLOCK, DW_TAG_lexical_block, 1,
DW_AT_low_pc, DW_FORM_addr,
#if PTR_SIZE == 4
DW_AT_high_pc, DW_FORM_data4,
#else
DW_AT_high_pc, DW_FORM_data8,
#endif
0, 0,
DWARF_ABBREV_LEXICAL_EMPTY_BLOCK, DW_TAG_lexical_block, 0,
DW_AT_low_pc, DW_FORM_addr,
#if PTR_SIZE == 4
DW_AT_high_pc, DW_FORM_data4,
#else
DW_AT_high_pc, DW_FORM_data8,
#endif
0, 0,
DWARF_ABBREV_SUBROUTINE_TYPE, DW_TAG_subroutine_type, 1,
DW_AT_type, DW_FORM_ref4,
DW_AT_sibling, DW_FORM_ref4,
0, 0,
DWARF_ABBREV_SUBROUTINE_EMPTY_TYPE, DW_TAG_subroutine_type, 0,
DW_AT_type, DW_FORM_ref4,
0, 0,
DWARF_ABBREV_FORMAL_PARAMETER2, DW_TAG_formal_parameter, 0,
DW_AT_type, DW_FORM_ref4,
0, 0,
0
};
static const unsigned char dwarf_line_opcodes[] = {
0 ,1 ,1 ,1 ,1 ,0 ,0 ,0 ,1 ,0 ,0 ,1
};
/* ------------------------------------------------------------------------- */
/* debug state */
struct _tccdbg {
int last_line_num, new_file;
int section_sym;
int debug_next_type;
struct _debug_hash {
int debug_type;
Sym *type;
} *debug_hash;
struct _debug_anon_hash {
Sym *type;
int n_debug_type;
int *debug_type;
} *debug_anon_hash;
int n_debug_hash;
int n_debug_anon_hash;
struct _debug_info {
int start;
int end;
int n_sym;
struct debug_sym {
int type;
unsigned long value;
char *str;
Section *sec;
int sym_index;
int info;
int file;
int line;
} *sym;
struct _debug_info *child, *next, *last, *parent;
} *debug_info, *debug_info_root;
struct {
int info;
int abbrev;
int line;
int str;
int line_str;
} dwarf_sym;
struct {
int start;
int dir_size;
char **dir_table;
int filename_size;
struct dwarf_filename_struct {
int dir_entry;
char *name;
} *filename_table;
int line_size;
int line_max_size;
unsigned char *line_data;
int cur_file;
int last_file;
int last_pc;
int last_line;
} dwarf_line;
struct {
int start;
Sym *func;
int line;
int base_type_used[N_DEFAULT_DEBUG];
} dwarf_info;
/* test coverage */
struct {
unsigned long offset;
unsigned long last_file_name;
unsigned long last_func_name;
int ind;
int line;
} tcov_data;
};
#define last_line_num s1->dState->last_line_num
#define new_file s1->dState->new_file
#define section_sym s1->dState->section_sym
#define debug_next_type s1->dState->debug_next_type
#define debug_hash s1->dState->debug_hash
#define debug_anon_hash s1->dState->debug_anon_hash
#define n_debug_hash s1->dState->n_debug_hash
#define n_debug_anon_hash s1->dState->n_debug_anon_hash
#define debug_info s1->dState->debug_info
#define debug_info_root s1->dState->debug_info_root
#define dwarf_sym s1->dState->dwarf_sym
#define dwarf_line s1->dState->dwarf_line
#define dwarf_info s1->dState->dwarf_info
#define tcov_data s1->dState->tcov_data
/* ------------------------------------------------------------------------- */
static void put_stabs(TCCState *s1, const char *str, int type, int other,
int desc, unsigned long value);
ST_FUNC void tcc_debug_new(TCCState *s1)
{
int shf = 0;
if (!s1->dState)
s1->dState = tcc_mallocz(sizeof *s1->dState);
#ifdef CONFIG_TCC_BACKTRACE
/* include stab info with standalone backtrace support */
if (s1->do_backtrace
&& (s1->output_type & (TCC_OUTPUT_EXE | TCC_OUTPUT_DLL)))
shf = SHF_ALLOC | SHF_WRITE; // SHF_WRITE needed for musl/SELINUX
#endif
if (s1->dwarf) {
s1->dwlo = s1->nb_sections;
dwarf_info_section =
new_section(s1, ".debug_info", SHT_PROGBITS, shf);
dwarf_abbrev_section =
new_section(s1, ".debug_abbrev", SHT_PROGBITS, shf);
dwarf_line_section =
new_section(s1, ".debug_line", SHT_PROGBITS, shf);
dwarf_aranges_section =
new_section(s1, ".debug_aranges", SHT_PROGBITS, shf);
shf |= SHF_MERGE | SHF_STRINGS;
dwarf_str_section =
new_section(s1, ".debug_str", SHT_PROGBITS, shf);
dwarf_str_section->sh_entsize = 1;
dwarf_info_section->sh_addralign =
dwarf_abbrev_section->sh_addralign =
dwarf_line_section->sh_addralign =
dwarf_aranges_section->sh_addralign =
dwarf_str_section->sh_addralign = 1;
if (s1->dwarf >= 5) {
dwarf_line_str_section =
new_section(s1, ".debug_line_str", SHT_PROGBITS, shf);
dwarf_line_str_section->sh_entsize = 1;
dwarf_line_str_section->sh_addralign = 1;
}
s1->dwhi = s1->nb_sections;
}
else
{
stab_section = new_section(s1, ".stab", SHT_PROGBITS, shf);
stab_section->sh_entsize = sizeof(Stab_Sym);
stab_section->sh_addralign = sizeof ((Stab_Sym*)0)->n_value;
stab_section->link = new_section(s1, ".stabstr", SHT_STRTAB, shf);
/* put first entry */
put_stabs(s1, "", 0, 0, 0, 0);
}
}
/* put stab debug information */
static void put_stabs(TCCState *s1, const char *str, int type, int other, int desc,
unsigned long value)
{
Stab_Sym *sym;
unsigned offset;
if (type == N_SLINE
&& (offset = stab_section->data_offset)
&& (sym = (Stab_Sym*)(stab_section->data + offset) - 1)
&& sym->n_type == type
&& sym->n_value == value) {
/* just update line_number in previous entry */
sym->n_desc = desc;
return;
}
sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
if (str) {
sym->n_strx = put_elf_str(stab_section->link, str);
} else {
sym->n_strx = 0;
}
sym->n_type = type;
sym->n_other = other;
sym->n_desc = desc;
sym->n_value = value;
}
static void put_stabs_r(TCCState *s1, const char *str, int type, int other, int desc,
unsigned long value, Section *sec, int sym_index)
{
put_elf_reloc(symtab_section, stab_section,
stab_section->data_offset + 8,
sizeof ((Stab_Sym*)0)->n_value == PTR_SIZE ? R_DATA_PTR : R_DATA_32,
sym_index);
put_stabs(s1, str, type, other, desc, value);
}
static void put_stabn(TCCState *s1, int type, int other, int desc, int value)
{
put_stabs(s1, NULL, type, other, desc, value);
}
/* ------------------------------------------------------------------------- */
#define dwarf_data1(s,data) \
{ unsigned char *p = section_ptr_add((s), 1); *p = (data); }
#define dwarf_data2(s,data) \
write16le(section_ptr_add((s), 2), (data))
#define dwarf_data4(s,data) \
write32le(section_ptr_add((s), 4), (data))
#define dwarf_data8(s,data) \
write64le(section_ptr_add((s), 8), (data))
static int dwarf_get_section_sym(Section *s)
{
TCCState *s1 = s->s1;
return put_elf_sym(symtab_section, 0, 0,
ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
s->sh_num, NULL);
}
static void dwarf_reloc(Section *s, int sym, int rel)
{
TCCState *s1 = s->s1;
put_elf_reloca(symtab_section, s, s->data_offset, rel, sym, 0);
}
static void dwarf_string(Section *s, Section *dw, int sym, const char *str)
{
TCCState *s1 = s->s1;
int offset, len;
char *ptr;
len = strlen(str) + 1;
offset = dw->data_offset;
ptr = section_ptr_add(dw, len);
memmove(ptr, str, len);
put_elf_reloca(symtab_section, s, s->data_offset, R_DATA_32DW, sym,
PTR_SIZE == 4 ? 0 : offset);
dwarf_data4(s, PTR_SIZE == 4 ? offset : 0);
}
static void dwarf_strp(Section *s, const char *str)
{
TCCState *s1 = s->s1;
dwarf_string(s, dwarf_str_section, dwarf_sym.str, str);
}
static void dwarf_line_strp(Section *s, const char *str)
{
TCCState *s1 = s->s1;
dwarf_string(s, dwarf_line_str_section, dwarf_sym.line_str, str);
}
static void dwarf_line_op(TCCState *s1, unsigned char op)
{
if (dwarf_line.line_size >= dwarf_line.line_max_size) {
dwarf_line.line_max_size += 1024;
dwarf_line.line_data =
(unsigned char *)tcc_realloc(dwarf_line.line_data,
dwarf_line.line_max_size);
}
dwarf_line.line_data[dwarf_line.line_size++] = op;
}
static void dwarf_file(TCCState *s1)
{
int i;
char *filename;
filename = strrchr(file->filename, '/');
if (filename == NULL) {
for (i = 1; i < dwarf_line.filename_size; i++)
if (dwarf_line.filename_table[i].dir_entry == 0 &&
strcmp(dwarf_line.filename_table[i].name,
file->filename) == 0) {
dwarf_line.cur_file = i;
return;
}
}
else {
int j;
char *undo = filename;
*filename++ = '\0';
for (i = 0; i < dwarf_line.dir_size; i++)
if (strcmp(dwarf_line.dir_table[i], file->filename) == 0)
for (j = 1; j < dwarf_line.filename_size; j++)
if (dwarf_line.filename_table[j].dir_entry == i &&
strcmp(dwarf_line.filename_table[j].name,
filename) == 0) {
*undo = '/';
dwarf_line.cur_file = j;
return;
}
*undo = '/';
}
return;
}
#if 0
static int dwarf_uleb128_size (unsigned long long value)
{
int size = 0;
do {
value >>= 7;
size++;
} while (value != 0);
return size;
}
#endif
static int dwarf_sleb128_size (long long value)
{
int size = 0;
long long end = value >> 63;
unsigned char last = end & 0x40;
unsigned char byte;
do {
byte = value & 0x7f;
value >>= 7;
size++;
} while (value != end || (byte & 0x40) != last);
return size;
}
static void dwarf_uleb128 (Section *s, unsigned long long value)
{
do {
unsigned char byte = value & 0x7f;
value >>= 7;
dwarf_data1(s, byte | (value ? 0x80 : 0));
} while (value != 0);
}
static void dwarf_sleb128 (Section *s, long long value)
{
int more;
long long end = value >> 63;
unsigned char last = end & 0x40;
do {
unsigned char byte = value & 0x7f;
value >>= 7;
more = value != end || (byte & 0x40) != last;
dwarf_data1(s, byte | (0x80 * more));
} while (more);
}
static void dwarf_uleb128_op (TCCState *s1, unsigned long long value)
{
do {
unsigned char byte = value & 0x7f;
value >>= 7;
dwarf_line_op(s1, byte | (value ? 0x80 : 0));
} while (value != 0);
}
static void dwarf_sleb128_op (TCCState *s1, long long value)
{
int more;
long long end = value >> 63;
unsigned char last = end & 0x40;
do {
unsigned char byte = value & 0x7f;
value >>= 7;
more = value != end || (byte & 0x40) != last;
dwarf_line_op(s1, byte | (0x80 * more));
} while (more);
}
/* start of translation unit info */
ST_FUNC void tcc_debug_start(TCCState *s1)
{
int i;
char buf[512];
char *filename;
/* an elf symbol of type STT_FILE must be put so that STB_LOCAL
symbols can be safely used */
filename = file->prev ? file->prev->filename : file->filename;
put_elf_sym(symtab_section, 0, 0,
ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
SHN_ABS, filename);
if (s1->do_debug) {
new_file = last_line_num = 0;
debug_next_type = N_DEFAULT_DEBUG;
debug_hash = NULL;
debug_anon_hash = NULL;
n_debug_hash = 0;
n_debug_anon_hash = 0;
getcwd(buf, sizeof(buf));
#ifdef _WIN32
normalize_slashes(buf);
#endif
if (s1->dwarf) {
int start_abbrev;
unsigned char *ptr;
char *undo;
/* dwarf_abbrev */
start_abbrev = dwarf_abbrev_section->data_offset;
ptr = section_ptr_add(dwarf_abbrev_section, sizeof(dwarf_abbrev_init));
memcpy(ptr, dwarf_abbrev_init, sizeof(dwarf_abbrev_init));
if (s1->dwarf < 5) {
while (*ptr) {
ptr += 3;
while (*ptr) {
if (ptr[1] == DW_FORM_line_strp)
ptr[1] = DW_FORM_strp;
if (s1->dwarf < 4) {
/* These are compatable for DW_TAG_compile_unit
DW_AT_stmt_list. */
if (ptr[1] == DW_FORM_sec_offset)
ptr[1] = DW_FORM_data4;
/* This code uses only size < 0x80 so these are
compatible. */
if (ptr[1] == DW_FORM_exprloc)
ptr[1] = DW_FORM_block1;
}
ptr += 2;
}
ptr += 2;
}
}
dwarf_sym.info = dwarf_get_section_sym(dwarf_info_section);
dwarf_sym.abbrev = dwarf_get_section_sym(dwarf_abbrev_section);
dwarf_sym.line = dwarf_get_section_sym(dwarf_line_section);
dwarf_sym.str = dwarf_get_section_sym(dwarf_str_section);
if (tcc_state->dwarf >= 5)
dwarf_sym.line_str = dwarf_get_section_sym(dwarf_line_str_section);
else {
dwarf_line_str_section = dwarf_str_section;
dwarf_sym.line_str = dwarf_sym.str;
}
section_sym = dwarf_get_section_sym(text_section);
/* dwarf_info */
dwarf_info.start = dwarf_info_section->data_offset;
dwarf_data4(dwarf_info_section, 0); // size
dwarf_data2(dwarf_info_section, s1->dwarf); // version
if (s1->dwarf >= 5) {
dwarf_data1(dwarf_info_section, DW_UT_compile); // unit type
dwarf_data1(dwarf_info_section, PTR_SIZE);
dwarf_reloc(dwarf_info_section, dwarf_sym.abbrev, R_DATA_32DW);
dwarf_data4(dwarf_info_section, start_abbrev);
}
else {
dwarf_reloc(dwarf_info_section, dwarf_sym.abbrev, R_DATA_32DW);
dwarf_data4(dwarf_info_section, start_abbrev);
dwarf_data1(dwarf_info_section, PTR_SIZE);
}
dwarf_data1(dwarf_info_section, DWARF_ABBREV_COMPILE_UNIT);
dwarf_strp(dwarf_info_section, "tcc " TCC_VERSION);
dwarf_data1(dwarf_info_section, DW_LANG_C11);
dwarf_line_strp(dwarf_info_section, filename);
dwarf_line_strp(dwarf_info_section, buf);
dwarf_reloc(dwarf_info_section, section_sym, R_DATA_PTR);
#if PTR_SIZE == 4
dwarf_data4(dwarf_info_section, ind); // low pc
dwarf_data4(dwarf_info_section, 0); // high pc
#else
dwarf_data8(dwarf_info_section, ind); // low pc
dwarf_data8(dwarf_info_section, 0); // high pc
#endif
dwarf_reloc(dwarf_info_section, dwarf_sym.line, R_DATA_32DW);
dwarf_data4(dwarf_info_section, dwarf_line_section->data_offset); // stmt_list
/* dwarf_line */
dwarf_line.start = dwarf_line_section->data_offset;
dwarf_data4(dwarf_line_section, 0); // length
dwarf_data2(dwarf_line_section, s1->dwarf); // version
if (s1->dwarf >= 5) {
dwarf_data1(dwarf_line_section, PTR_SIZE); // address size
dwarf_data1(dwarf_line_section, 0); // segment selector
}
dwarf_data4(dwarf_line_section, 0); // prologue Length
dwarf_data1(dwarf_line_section, DWARF_MIN_INSTR_LEN);
if (s1->dwarf >= 4)
dwarf_data1(dwarf_line_section, 1); // maximum ops per instruction
dwarf_data1(dwarf_line_section, 1); // Initial value of 'is_stmt'
dwarf_data1(dwarf_line_section, DWARF_LINE_BASE);
dwarf_data1(dwarf_line_section, DWARF_LINE_RANGE);
dwarf_data1(dwarf_line_section, DWARF_OPCODE_BASE);
ptr = section_ptr_add(dwarf_line_section, sizeof(dwarf_line_opcodes));
memcpy(ptr, dwarf_line_opcodes, sizeof(dwarf_line_opcodes));
undo = strrchr(filename, '/');
if (undo)
*undo = 0;
dwarf_line.dir_size = 1 + (undo != NULL);
dwarf_line.dir_table = (char **) tcc_malloc(sizeof (char *) *
dwarf_line.dir_size);
dwarf_line.dir_table[0] = tcc_strdup(buf);
if (undo)
dwarf_line.dir_table[1] = tcc_strdup(filename);
dwarf_line.filename_size = 2;
dwarf_line.filename_table =
(struct dwarf_filename_struct *)
tcc_malloc(2*sizeof (struct dwarf_filename_struct));
dwarf_line.filename_table[0].dir_entry = 0;
if (undo) {
dwarf_line.filename_table[0].name = tcc_strdup(undo + 1);
dwarf_line.filename_table[1].dir_entry = 1;
dwarf_line.filename_table[1].name = tcc_strdup(undo + 1);
*undo = '/';
}
else {
dwarf_line.filename_table[0].name = tcc_strdup(filename);
dwarf_line.filename_table[1].dir_entry = 0;
dwarf_line.filename_table[1].name = tcc_strdup(filename);
}
dwarf_line.line_size = dwarf_line.line_max_size = 0;
dwarf_line.line_data = NULL;
dwarf_line.cur_file = 1;
dwarf_line.last_file = 0;
dwarf_line.last_pc = 0;
dwarf_line.last_line = 1;
dwarf_line_op(s1, 0); // extended
dwarf_uleb128_op(s1, 1 + PTR_SIZE); // extended size
dwarf_line_op(s1, DW_LNE_set_address);
for (i = 0; i < PTR_SIZE; i++)
dwarf_line_op(s1, 0);
memset(&dwarf_info.base_type_used, 0, sizeof(dwarf_info.base_type_used));
}
else
{
/* file info: full path + filename */
pstrcat(buf, sizeof(buf), "/");
section_sym = put_elf_sym(symtab_section, 0, 0,
ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
text_section->sh_num, NULL);
put_stabs_r(s1, buf, N_SO, 0, 0,
text_section->data_offset, text_section, section_sym);
put_stabs_r(s1, filename, N_SO, 0, 0,
text_section->data_offset, text_section, section_sym);
for (i = 0; i < N_DEFAULT_DEBUG; i++)
put_stabs(s1, default_debug[i].name, N_LSYM, 0, 0, 0);
}
/* we're currently 'including' the <command line> */
tcc_debug_bincl(s1);
}
}
/* put end of translation unit info */
ST_FUNC void tcc_debug_end(TCCState *s1)
{
if (!s1->do_debug)
return;
if (s1->dwarf) {
int i, j;
int start_aranges;
unsigned char *ptr;
int text_size = text_section->data_offset;
/* dwarf_info */
for (i = 0; i < n_debug_anon_hash; i++) {
Sym *t = debug_anon_hash[i].type;
int pos = dwarf_info_section->data_offset;
dwarf_data1(dwarf_info_section,
IS_UNION (t->type.t) ? DWARF_ABBREV_UNION_EMPTY_TYPE
: DWARF_ABBREV_STRUCTURE_EMPTY_TYPE);
dwarf_strp(dwarf_info_section,
(t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
? "" : get_tok_str(t->v & ~SYM_STRUCT, NULL));
dwarf_uleb128(dwarf_info_section, 0);
dwarf_uleb128(dwarf_info_section, dwarf_line.cur_file);
dwarf_uleb128(dwarf_info_section, file->line_num);
for (j = 0; j < debug_anon_hash[i].n_debug_type; j++)
write32le(dwarf_info_section->data +
debug_anon_hash[i].debug_type[j],
pos - dwarf_info.start);
tcc_free (debug_anon_hash[i].debug_type);
}
tcc_free (debug_anon_hash);
dwarf_data1(dwarf_info_section, 0);
ptr = dwarf_info_section->data + dwarf_info.start;
write32le(ptr, dwarf_info_section->data_offset - dwarf_info.start - 4);
write32le(ptr + 25 + (s1->dwarf >= 5) + PTR_SIZE, text_size);
/* dwarf_aranges */
start_aranges = dwarf_aranges_section->data_offset;
dwarf_data4(dwarf_aranges_section, 0); // size
dwarf_data2(dwarf_aranges_section, 2); // version
dwarf_reloc(dwarf_aranges_section, dwarf_sym.info, R_DATA_32DW);
dwarf_data4(dwarf_aranges_section, 0); // dwarf_info
#if PTR_SIZE == 4
dwarf_data1(dwarf_aranges_section, 4); // address size
#else
dwarf_data1(dwarf_aranges_section, 8); // address size
#endif
dwarf_data1(dwarf_aranges_section, 0); // segment selector size
dwarf_data4(dwarf_aranges_section, 0); // padding
dwarf_reloc(dwarf_aranges_section, section_sym, R_DATA_PTR);
#if PTR_SIZE == 4
dwarf_data4(dwarf_aranges_section, 0); // Begin
dwarf_data4(dwarf_aranges_section, text_size); // End
dwarf_data4(dwarf_aranges_section, 0); // End list
dwarf_data4(dwarf_aranges_section, 0); // End list
#else
dwarf_data8(dwarf_aranges_section, 0); // Begin
dwarf_data8(dwarf_aranges_section, text_size); // End
dwarf_data8(dwarf_aranges_section, 0); // End list
dwarf_data8(dwarf_aranges_section, 0); // End list
#endif
ptr = dwarf_aranges_section->data + start_aranges;
write32le(ptr, dwarf_aranges_section->data_offset - start_aranges - 4);
/* dwarf_line */
if (s1->dwarf >= 5) {
dwarf_data1(dwarf_line_section, 1); /* col */
dwarf_uleb128(dwarf_line_section, DW_LNCT_path);
dwarf_uleb128(dwarf_line_section, DW_FORM_line_strp);
dwarf_uleb128(dwarf_line_section, dwarf_line.dir_size);
for (i = 0; i < dwarf_line.dir_size; i++)
dwarf_line_strp(dwarf_line_section, dwarf_line.dir_table[i]);
dwarf_data1(dwarf_line_section, 2); /* col */
dwarf_uleb128(dwarf_line_section, DW_LNCT_path);
dwarf_uleb128(dwarf_line_section, DW_FORM_line_strp);
dwarf_uleb128(dwarf_line_section, DW_LNCT_directory_index);
dwarf_uleb128(dwarf_line_section, DW_FORM_udata);
dwarf_uleb128(dwarf_line_section, dwarf_line.filename_size);
for (i = 0; i < dwarf_line.filename_size; i++) {
dwarf_line_strp(dwarf_line_section,
dwarf_line.filename_table[i].name);
dwarf_uleb128(dwarf_line_section,
dwarf_line.filename_table[i].dir_entry);
}
}
else {
int len;
for (i = 0; i < dwarf_line.dir_size; i++) {
len = strlen(dwarf_line.dir_table[i]) + 1;
ptr = section_ptr_add(dwarf_line_section, len);
memmove(ptr, dwarf_line.dir_table[i], len);
}
dwarf_data1(dwarf_line_section, 0); /* end dir */
for (i = 0; i < dwarf_line.filename_size; i++) {
len = strlen(dwarf_line.filename_table[i].name) + 1;
ptr = section_ptr_add(dwarf_line_section, len);
memmove(ptr, dwarf_line.filename_table[i].name, len);
dwarf_uleb128(dwarf_line_section,
dwarf_line.filename_table[i].dir_entry);
dwarf_uleb128(dwarf_line_section, 0); /* time */
dwarf_uleb128(dwarf_line_section, 0); /* size */
}
dwarf_data1(dwarf_line_section, 0); /* end file */
}
for (i = 0; i < dwarf_line.dir_size; i++)
tcc_free(dwarf_line.dir_table[i]);
tcc_free(dwarf_line.dir_table);
for (i = 0; i < dwarf_line.filename_size; i++)
tcc_free(dwarf_line.filename_table[i].name);
tcc_free(dwarf_line.filename_table);
dwarf_line_op(s1, DW_LNS_negate_stmt);
dwarf_line_op(s1, 0); // extended
dwarf_uleb128_op(s1, 1); // extended size
dwarf_line_op(s1, DW_LNE_end_sequence);
i = (s1->dwarf >= 5) * 2;
write32le(&dwarf_line_section->data[dwarf_line.start + 6 + i],
dwarf_line_section->data_offset - dwarf_line.start - (10 + i));
section_ptr_add(dwarf_line_section, 3);
dwarf_reloc(dwarf_line_section, section_sym, R_DATA_PTR);
ptr = section_ptr_add(dwarf_line_section, dwarf_line.line_size - 3);
memmove(ptr - 3, dwarf_line.line_data, dwarf_line.line_size);
tcc_free(dwarf_line.line_data);
write32le(dwarf_line_section->data + dwarf_line.start,
dwarf_line_section->data_offset - dwarf_line.start - 4);
}
else
{
put_stabs_r(s1, NULL, N_SO, 0, 0,
text_section->data_offset, text_section, section_sym);
}
tcc_free(debug_hash);
}
static BufferedFile* put_new_file(TCCState *s1)
{
BufferedFile *f = file;
/* use upper file if from inline ":asm:" */