-
Notifications
You must be signed in to change notification settings - Fork 389
/
tccdbg.c
2482 lines (2312 loc) · 83.3 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
#define FDE_ENCODING (DW_EH_PE_udata4 | DW_EH_PE_signed | DW_EH_PE_pcrel)
/* ------------------------------------------------------------------------- */
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_debug && s1->output_type == TCC_OUTPUT_MEMORY)
s1->do_backtrace = 1;
if (s1->do_backtrace)
shf = SHF_ALLOC; /* have debug data available at runtime */
#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) \
(*(uint8_t*)section_ptr_add((s), 1) = (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, j;
char *filename;
int index_offset = s1->dwarf < 5;
if (!strcmp(file->filename, "<command line>")) {
dwarf_line.cur_file = 1;
return;
}
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 + index_offset;
return;
}
i = -index_offset;
filename = file->filename;
}
else {
char *undo = filename;
char *dir = file->filename;
*filename++ = '\0';
for (i = 0; i < dwarf_line.dir_size; i++)
if (strcmp(dwarf_line.dir_table[i], dir) == 0) {
for (j = 1; j < dwarf_line.filename_size; j++)
if (dwarf_line.filename_table[j].dir_entry - index_offset
== i &&
strcmp(dwarf_line.filename_table[j].name,
filename) == 0) {
*undo = '/';
dwarf_line.cur_file = j + index_offset;
return;
}
break;
}
if (i == dwarf_line.dir_size) {
dwarf_line.dir_size++;
dwarf_line.dir_table =
(char **) tcc_realloc(dwarf_line.dir_table,
dwarf_line.dir_size *
sizeof (char *));
dwarf_line.dir_table[i] = tcc_strdup(dir);
}
*undo = '/';
}
dwarf_line.filename_table =
(struct dwarf_filename_struct *)
tcc_realloc(dwarf_line.filename_table,
(dwarf_line.filename_size + 1) *
sizeof (struct dwarf_filename_struct));
dwarf_line.filename_table[dwarf_line.filename_size].dir_entry =
i + index_offset;
dwarf_line.filename_table[dwarf_line.filename_size].name =
tcc_strdup(filename);
dwarf_line.cur_file = dwarf_line.filename_size++ + index_offset;
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);
}
#if TCC_EH_FRAME
ST_FUNC void tcc_eh_frame_start(TCCState *s1)
{
if (!s1->unwind_tables)
return;
eh_frame_section = new_section(s1, ".eh_frame", SHT_PROGBITS, SHF_ALLOC);
s1->eh_start = eh_frame_section->data_offset;
dwarf_data4(eh_frame_section, 0); // length
dwarf_data4(eh_frame_section, 0); // CIE ID
dwarf_data1(eh_frame_section, 1); // Version
dwarf_data1(eh_frame_section, 'z'); // Augmentation String
dwarf_data1(eh_frame_section, 'R');
dwarf_data1(eh_frame_section, 0);
#if defined TCC_TARGET_I386
dwarf_uleb128(eh_frame_section, 1); // code_alignment_factor
dwarf_sleb128(eh_frame_section, -4); // data_alignment_factor
dwarf_uleb128(eh_frame_section, 8); // return address column
dwarf_uleb128(eh_frame_section, 1); // Augmentation len
dwarf_data1(eh_frame_section, FDE_ENCODING);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa);
dwarf_uleb128(eh_frame_section, 4); // r4 (esp)
dwarf_uleb128(eh_frame_section, 4); // ofs 4
dwarf_data1(eh_frame_section, DW_CFA_offset + 8); // r8 (eip)
dwarf_uleb128(eh_frame_section, 1); // cfa-4
#elif defined TCC_TARGET_X86_64
dwarf_uleb128(eh_frame_section, 1); // code_alignment_factor
dwarf_sleb128(eh_frame_section, -8); // data_alignment_factor
dwarf_uleb128(eh_frame_section, 16); // return address column
dwarf_uleb128(eh_frame_section, 1); // Augmentation len
dwarf_data1(eh_frame_section, FDE_ENCODING);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa);
dwarf_uleb128(eh_frame_section, 7); // r7 (rsp)
dwarf_uleb128(eh_frame_section, 8); // ofs 8
dwarf_data1(eh_frame_section, DW_CFA_offset + 16); // r16 (rip)
dwarf_uleb128(eh_frame_section, 1); // cfa-8
#elif defined TCC_TARGET_ARM
/* TODO: arm must be compiled with: -funwind-tables */
/* arm also uses .ARM.extab and .ARM.exidx sections */
dwarf_uleb128(eh_frame_section, 2); // code_alignment_factor
dwarf_sleb128(eh_frame_section, -4); // data_alignment_factor
dwarf_uleb128(eh_frame_section, 14); // return address column
dwarf_uleb128(eh_frame_section, 1); // Augmentation len
dwarf_data1(eh_frame_section, FDE_ENCODING);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa);
dwarf_uleb128(eh_frame_section, 13); // r13 (sp)
dwarf_uleb128(eh_frame_section, 0); // ofs 0
#elif defined TCC_TARGET_ARM64
dwarf_uleb128(eh_frame_section, 4); // code_alignment_factor
dwarf_sleb128(eh_frame_section, -8); // data_alignment_factor
dwarf_uleb128(eh_frame_section, 30); // return address column
dwarf_uleb128(eh_frame_section, 1); // Augmentation len
dwarf_data1(eh_frame_section, FDE_ENCODING);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa);
dwarf_uleb128(eh_frame_section, 31); // x31 (sp)
dwarf_uleb128(eh_frame_section, 0); // ofs 0
#elif defined TCC_TARGET_RISCV64
eh_frame_section->data[s1->eh_start + 8] = 3; // version = 3
dwarf_uleb128(eh_frame_section, 1); // code_alignment_factor
dwarf_sleb128(eh_frame_section, -4); // data_alignment_factor
dwarf_uleb128(eh_frame_section, 1); // return address column
dwarf_uleb128(eh_frame_section, 1); // Augmentation len
dwarf_data1(eh_frame_section, FDE_ENCODING);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa);
dwarf_uleb128(eh_frame_section, 2); // r2 (sp)
dwarf_uleb128(eh_frame_section, 0); // ofs 0
#endif
while ((eh_frame_section->data_offset - s1->eh_start) & 3)
dwarf_data1(eh_frame_section, DW_CFA_nop);
write32le(eh_frame_section->data + s1->eh_start, // length
eh_frame_section->data_offset - s1->eh_start - 4);
}
static void tcc_debug_frame_end(TCCState *s1, int size)
{
int eh_section_sym;
unsigned long fde_start;
if (!eh_frame_section)
return;
eh_section_sym = dwarf_get_section_sym(text_section);
fde_start = eh_frame_section->data_offset;
dwarf_data4(eh_frame_section, 0); // length
dwarf_data4(eh_frame_section,
fde_start - s1->eh_start + 4); // CIE Pointer
#if defined TCC_TARGET_I386
dwarf_reloc(eh_frame_section, eh_section_sym, R_386_PC32);
#elif defined TCC_TARGET_X86_64
dwarf_reloc(eh_frame_section, eh_section_sym, R_X86_64_PC32);
#elif defined TCC_TARGET_ARM
dwarf_reloc(eh_frame_section, eh_section_sym, R_ARM_REL32);
#elif defined TCC_TARGET_ARM64
dwarf_reloc(eh_frame_section, eh_section_sym, R_AARCH64_PREL32);
#elif defined TCC_TARGET_RISCV64
dwarf_reloc(eh_frame_section, eh_section_sym, R_RISCV_32_PCREL);
#endif
dwarf_data4(eh_frame_section, func_ind); // PC Begin
dwarf_data4(eh_frame_section, size); // PC Range
dwarf_data1(eh_frame_section, 0); // Augmentation Length
#if defined TCC_TARGET_I386
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 1);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset);
dwarf_uleb128(eh_frame_section, 8);
dwarf_data1(eh_frame_section, DW_CFA_offset + 5); // r5 (ebp)
dwarf_uleb128(eh_frame_section, 2); // cfa-8
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 2);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa_register);
dwarf_uleb128(eh_frame_section, 5); // r5 (ebp)
dwarf_data1(eh_frame_section, DW_CFA_advance_loc4);
dwarf_data4(eh_frame_section, size - 5);
dwarf_data1(eh_frame_section, DW_CFA_restore + 5); // r5 (ebp)
dwarf_data1(eh_frame_section, DW_CFA_def_cfa);
dwarf_uleb128(eh_frame_section, 4); // r4 (esp)
dwarf_uleb128(eh_frame_section, 4); // ofs 4
#elif defined TCC_TARGET_X86_64
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 1);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset);
dwarf_uleb128(eh_frame_section, 16);
dwarf_data1(eh_frame_section, DW_CFA_offset + 6); // r6 (rbp)
dwarf_uleb128(eh_frame_section, 2); // cfa-16
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 3);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa_register);
dwarf_uleb128(eh_frame_section, 6); // r6 (rbp)
dwarf_data1(eh_frame_section, DW_CFA_advance_loc4);
dwarf_data4(eh_frame_section, size - 5);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa);
dwarf_uleb128(eh_frame_section, 7); // r7 (rsp)
dwarf_uleb128(eh_frame_section, 8); // ofs 8
#elif defined TCC_TARGET_ARM
/* TODO */
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 2);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset);
dwarf_uleb128(eh_frame_section, 8);
dwarf_data1(eh_frame_section, DW_CFA_offset + 14); // r14 (lr)
dwarf_uleb128(eh_frame_section, 1);
dwarf_data1(eh_frame_section, DW_CFA_offset + 11); // r11 (fp)
dwarf_uleb128(eh_frame_section, 2);
dwarf_data1(eh_frame_section, DW_CFA_advance_loc4);
dwarf_data4(eh_frame_section, size / 2 - 5);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa_register);
dwarf_uleb128(eh_frame_section, 11); // r11 (fp)
#elif defined TCC_TARGET_ARM64
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 1);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset);
dwarf_uleb128(eh_frame_section, 224);
dwarf_data1(eh_frame_section, DW_CFA_offset + 29); // x29 (fp)
dwarf_uleb128(eh_frame_section, 28); // cfa-224
dwarf_data1(eh_frame_section, DW_CFA_offset + 30); // x30 (lr)
dwarf_uleb128(eh_frame_section, 27); // cfa-216
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 3);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset);
dwarf_uleb128(eh_frame_section, 224 + ((-loc + 15) & ~15));
dwarf_data1(eh_frame_section, DW_CFA_advance_loc4);
dwarf_data4(eh_frame_section, size / 4 - 5);
dwarf_data1(eh_frame_section, DW_CFA_restore + 30); // x30 (lr)
dwarf_data1(eh_frame_section, DW_CFA_restore + 29); // x29 (fp)
dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset);
dwarf_uleb128(eh_frame_section, 0);
#elif defined TCC_TARGET_RISCV64
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 4);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset);
dwarf_uleb128(eh_frame_section, 16); // ofs 16
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 8);
dwarf_data1(eh_frame_section, DW_CFA_offset + 1); // r1 (ra, lr)
dwarf_uleb128(eh_frame_section, 2); // cfa-8
dwarf_data1(eh_frame_section, DW_CFA_offset + 8); // r8 (s0, fp)
dwarf_uleb128(eh_frame_section, 4); // cfa-16
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 8);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa);
dwarf_uleb128(eh_frame_section, 8); // r8 (s0, fp)
dwarf_uleb128(eh_frame_section, 0); // ofs 0
dwarf_data1(eh_frame_section, DW_CFA_advance_loc4);
while (size >= 4 &&
read32le(cur_text_section->data + func_ind + size - 4) != 0x00008067)
size -= 4;
dwarf_data4(eh_frame_section, size - 36);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa);
dwarf_uleb128(eh_frame_section, 2); // r2 (r2, sp)
dwarf_uleb128(eh_frame_section, 16); // ofs 16
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 4);
dwarf_data1(eh_frame_section, DW_CFA_restore + 1); // r1 (lr)
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 4);
dwarf_data1(eh_frame_section, DW_CFA_restore + 8); // r8 (s0, fp)
dwarf_data1(eh_frame_section, DW_CFA_advance_loc + 4);
dwarf_data1(eh_frame_section, DW_CFA_def_cfa_offset);
dwarf_uleb128(eh_frame_section, 0); // ofs 0
#endif
while ((eh_frame_section->data_offset - fde_start) & 3)
dwarf_data1(eh_frame_section, DW_CFA_nop);
write32le(eh_frame_section->data + fde_start, // length
eh_frame_section->data_offset - fde_start - 4);
}
ST_FUNC void tcc_eh_frame_end(TCCState *s1)
{
if (!eh_frame_section)
return;
dwarf_data4(eh_frame_section, 0);
}
struct eh_search_table {
uint32_t pc_offset;
uint32_t fde_offset;
};
static int sort_eh_table(const void *a, const void *b)
{
uint32_t pc1 = ((const struct eh_search_table *)a)->pc_offset;
uint32_t pc2 = ((const struct eh_search_table *)b)->pc_offset;
return pc1 < pc2 ? -1 : pc1 > pc2 ? 1 : 0;
}
ST_FUNC void tcc_eh_frame_hdr(TCCState *s1, int final)
{
int count = 0, offset;
unsigned long count_offset, tab_offset;
unsigned char *ln, *end;
unsigned int last_cie_offset = 0xffffffff;
if (!eh_frame_section || !eh_frame_section->data_offset)
return;
if (final && !eh_frame_hdr_section)
return;
if (final == 0)
eh_frame_hdr_section =
new_section(s1, ".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC);
eh_frame_hdr_section->data_offset = 0;
dwarf_data1(eh_frame_hdr_section, 1); // Version
// Pointer Encoding Format
dwarf_data1(eh_frame_hdr_section, DW_EH_PE_sdata4 | DW_EH_PE_pcrel);
// Count Encoding Format
dwarf_data1(eh_frame_hdr_section, DW_EH_PE_udata4 | DW_EH_PE_absptr);
// Table Encoding Format
dwarf_data1(eh_frame_hdr_section, DW_EH_PE_sdata4 | DW_EH_PE_datarel);
offset = eh_frame_section->sh_addr -
eh_frame_hdr_section->sh_addr -
eh_frame_hdr_section->data_offset;
dwarf_data4(eh_frame_hdr_section, offset); // eh_frame_ptr
// Count
count_offset = eh_frame_hdr_section->data_offset;
dwarf_data4(eh_frame_hdr_section, 0);
tab_offset = eh_frame_hdr_section->data_offset;
ln = eh_frame_section->data;
end = eh_frame_section->data + eh_frame_section->data_offset;
while (ln < end) {
unsigned char *fde = ln, *rd = ln;
unsigned int cie_offset, version, length = dwarf_read_4(rd, end);
unsigned int pc_offset, fde_offset;
if (length == 0)
goto next;
cie_offset = dwarf_read_4(rd, end);
if (cie_offset == 0)
goto next;
if (cie_offset != last_cie_offset) {
unsigned char *cie = rd - cie_offset + 4;
if (cie < eh_frame_section->data)
goto next;
version = dwarf_read_1(cie, end);
if ((version == 1 || version == 3) &&
dwarf_read_1(cie, end) == 'z' && // Augmentation String
dwarf_read_1(cie, end) == 'R' &&
dwarf_read_1(cie, end) == 0) {
dwarf_read_uleb128(&cie, end); // code_alignment_factor
dwarf_read_sleb128(&cie, end); // data_alignment_factor
dwarf_read_1(cie, end); // return address column
if (dwarf_read_uleb128(&cie, end) == 1 &&
dwarf_read_1(cie, end) == FDE_ENCODING) {
last_cie_offset = cie_offset;
}
else
goto next;
}
else
goto next;
}
count++;
fde_offset = eh_frame_section->sh_addr +
(fde - eh_frame_section->data) -
eh_frame_hdr_section->sh_addr;
pc_offset = dwarf_read_4(rd, end) + fde_offset + 8;
dwarf_data4(eh_frame_hdr_section, pc_offset);
dwarf_data4(eh_frame_hdr_section, fde_offset);
next: