/
dwarf.c
2283 lines (1976 loc) · 72.8 KB
/
dwarf.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
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gpl.txt.
// See the included readme.txt for details.
// Emit Dwarf symbolic debug info
#if !SPP
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#if __DMC__ || linux
#include <malloc.h>
#endif
#if linux || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun&&__SVR4
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#endif
#include "cc.h"
#include "global.h"
#include "code.h"
#include "type.h"
#include "outbuf.h"
#include "filespec.h"
#include "cv4.h"
#include "cgcv.h"
#include "dt.h"
#include "aa.h"
#include "tinfo.h"
#if ELFOBJ
#include "melf.h"
#endif
#if MACHOBJ
#include "mach.h"
#endif
#if ELFOBJ || MACHOBJ
#if MARS
#include "mars.h"
#endif
#include "dwarf.h"
#include "dwarf2.h"
extern int seg_count;
static char __file__[] = __FILE__; // for tassert.h
#include "tassert.h"
#if ELFOBJ
#define MAP_SEG2SYMIDX(seg) (SegData[seg]->SDsymidx)
#else
#define MAP_SEG2SYMIDX(seg) (assert(0))
#endif
#define OFFSET_FAC REGSIZE
int dwarf_getsegment(const char *name, int align)
{
#if ELFOBJ
return elf_getsegment(name, NULL, SHT_PROGDEF, 0, align * 4);
#elif MACHOBJ
return mach_getsegment(name, "__DWARF", align * 2, S_ATTR_DEBUG);
#else
assert(0);
return 0;
#endif
}
void dwarf_addrel(int seg, targ_size_t offset, int targseg, targ_size_t val = 0)
{
#if ELFOBJ
elf_addrel(seg, offset, I64 ? R_X86_64_32 : RI_TYPE_SYM32, MAP_SEG2SYMIDX(targseg), val);
#elif MACHOBJ
mach_addrel(seg, offset, NULL, targseg, val);
#else
assert(0);
#endif
}
void dwarf_addrel64(int seg, targ_size_t offset, int targseg, targ_size_t val)
{
#if ELFOBJ
elf_addrel(seg, offset, R_X86_64_64, MAP_SEG2SYMIDX(targseg), val);
#elif MACHOBJ
mach_addrel(seg, offset, NULL, targseg, val);
#else
assert(0);
#endif
}
void dwarf_addreladdr(int seg, targ_size_t offset, int targseg, targ_size_t val)
{
if (I64)
dwarf_addrel64(seg, offset, targseg, val);
else
dwarf_addrel(seg, offset, targseg, val);
}
void append_addr(Outbuffer *buf, targ_size_t addr)
{
if (I64)
buf->write64(addr);
else
buf->write32(addr);
}
/************************ DWARF DEBUG OUTPUT ********************************/
// Dwarf Symbolic Debugging Information
struct CFA_reg
{
int offset; // offset from CFA
};
// Current CFA state for .debug_frame
struct CFA_state
{
size_t location;
int reg; // CFA register number
int offset; // CFA register offset
CFA_reg regstates[17]; // register states
};
int dwarf_regno(int reg)
{
assert(reg <= R15);
if (I16 || I32)
return reg;
else
{
static const int to_amd64_reg_map[8] =
{ 0 /*AX*/, 2 /*CX*/, 3 /*DX*/, 1 /*BX*/,
7 /*SP*/, 6 /*BP*/, 4 /*SI*/, 5 /*DI*/ };
return reg < 8 ? to_amd64_reg_map[reg] : reg;
}
}
static CFA_state CFA_state_init_32 = // initial CFA state as defined by CIE
{ 0, // location
dwarf_regno(SP), // register
4, // offset
{ { 0 }, // 0: EAX
{ 0 }, // 1: ECX
{ 0 }, // 2: EDX
{ 0 }, // 3: EBX
{ 0 }, // 4: ESP
{ 0 }, // 5: EBP
{ 0 }, // 6: ESI
{ 0 }, // 7: EDI
{ -4 }, // 8: EIP
}
};
static CFA_state CFA_state_init_64 = // initial CFA state as defined by CIE
{ 0, // location
dwarf_regno(SP), // register
8, // offset
{ { 0 }, // 0: RAX
{ 0 }, // 1: RBX
{ 0 }, // 2: RCX
{ 0 }, // 3: RDX
{ 0 }, // 4: RSI
{ 0 }, // 5: RDI
{ 0 }, // 6: RBP
{ 0 }, // 7: RSP
{ 0 }, // 8: R8
{ 0 }, // 9: R9
{ 0 }, // 10: R10
{ 0 }, // 11: R11
{ 0 }, // 12: R12
{ 0 }, // 13: R13
{ 0 }, // 14: R14
{ 0 }, // 15: R15
{ -8 }, // 16: RIP
}
};
static CFA_state CFA_state_current; // current CFA state
static Outbuffer cfa_buf; // CFA instructions
void dwarf_CFA_set_loc(size_t location)
{
assert(location >= CFA_state_current.location);
size_t inc = location - CFA_state_current.location;
if (inc <= 63)
cfa_buf.writeByte(DW_CFA_advance_loc + inc);
else if (inc <= 255)
{ cfa_buf.writeByte(DW_CFA_advance_loc1);
cfa_buf.writeByte(inc);
}
else if (inc <= 0xFFFF)
{ cfa_buf.writeByte(DW_CFA_advance_loc2);
cfa_buf.writeWord(inc);
}
else
{ cfa_buf.writeByte(DW_CFA_advance_loc4);
cfa_buf.write32(inc);
}
CFA_state_current.location = location;
}
void dwarf_CFA_set_reg_offset(int reg, int offset)
{
int dw_reg = dwarf_regno(reg);
if (dw_reg != CFA_state_current.reg)
{
if (offset == CFA_state_current.offset)
{
cfa_buf.writeByte(DW_CFA_def_cfa_register);
cfa_buf.writeuLEB128(dw_reg);
}
else if (offset < 0)
{
cfa_buf.writeByte(DW_CFA_def_cfa_sf);
cfa_buf.writeuLEB128(dw_reg);
cfa_buf.writesLEB128(offset / -OFFSET_FAC);
}
else
{
cfa_buf.writeByte(DW_CFA_def_cfa);
cfa_buf.writeuLEB128(dw_reg);
cfa_buf.writeuLEB128(offset);
}
}
else if (offset < 0)
{
cfa_buf.writeByte(DW_CFA_def_cfa_offset_sf);
cfa_buf.writesLEB128(offset / -OFFSET_FAC);
}
else
{
cfa_buf.writeByte(DW_CFA_def_cfa_offset);
cfa_buf.writeuLEB128(offset);
}
CFA_state_current.reg = dw_reg;
CFA_state_current.offset = offset;
}
void dwarf_CFA_offset(int reg, int offset)
{
int dw_reg = dwarf_regno(reg);
if (CFA_state_current.regstates[dw_reg].offset != offset)
{
if (offset <= 0)
{
cfa_buf.writeByte(DW_CFA_offset + dw_reg);
cfa_buf.writeuLEB128(offset / -OFFSET_FAC);
}
else
{
cfa_buf.writeByte(DW_CFA_offset_extended_sf);
cfa_buf.writeuLEB128(dw_reg);
cfa_buf.writesLEB128(offset / -OFFSET_FAC);
}
}
CFA_state_current.regstates[dw_reg].offset = offset;
}
void dwarf_CFA_args_size(size_t sz)
{
cfa_buf.writeByte(DW_CFA_GNU_args_size);
cfa_buf.writeuLEB128(sz);
}
// .debug_frame
static IDXSEC debug_frame_secidx;
// .debug_str
static IDXSEC debug_str_secidx;
static Outbuffer *debug_str_buf;
// .debug_pubnames
static IDXSEC debug_pubnames_secidx;
static Outbuffer *debug_pubnames_buf;
// .debug_aranges
static IDXSEC debug_aranges_seg;
static IDXSEC debug_aranges_secidx;
static Outbuffer *debug_aranges_buf;
// .debug_ranges
static IDXSEC debug_ranges_seg;
static IDXSEC debug_ranges_secidx;
static Outbuffer *debug_ranges_buf;
// .debug_loc
static IDXSEC debug_loc_seg;
static IDXSEC debug_loc_secidx;
static Outbuffer *debug_loc_buf;
// .debug_abbrev
static IDXSEC abbrevseg;
static Outbuffer *abbrevbuf;
/* DWARF 7.5.3: "Each declaration begins with an unsigned LEB128 number
* representing the abbreviation code itself."
*/
static unsigned abbrevcode = 1;
static AArray *abbrev_table;
static int hasModname; // 1 if has DW_TAG_module
// .debug_info
static IDXSEC infoseg;
static Outbuffer *infobuf;
static AArray *infoFileName_table;
static AArray *type_table;
static AArray *functype_table; // not sure why this cannot be combined with type_table
static Outbuffer *functypebuf;
// typeinfo declarations for hash of char*
struct Abuf
{
const unsigned char *buf;
size_t length;
};
struct TypeInfo_Abuf : TypeInfo
{
const char* toString();
hash_t getHash(void *p);
int equals(void *p1, void *p2);
int compare(void *p1, void *p2);
size_t tsize();
void swap(void *p1, void *p2);
};
TypeInfo_Abuf ti_abuf;
const char* TypeInfo_Abuf::toString()
{
return "Abuf";
}
hash_t TypeInfo_Abuf::getHash(void *p)
{
Abuf a = *(Abuf *)p;
hash_t hash = 0;
for (size_t i = 0; i < a.length; i++)
hash = hash * 11 + a.buf[i];
return hash;
}
int TypeInfo_Abuf::equals(void *p1, void *p2)
{
Abuf a1 = *(Abuf*)p1;
Abuf a2 = *(Abuf*)p2;
return a1.length == a2.length &&
memcmp(a1.buf, a2.buf, a1.length) == 0;
}
int TypeInfo_Abuf::compare(void *p1, void *p2)
{
Abuf a1 = *(Abuf*)p1;
Abuf a2 = *(Abuf*)p2;
if (a1.length == a2.length)
return memcmp(a1.buf, a2.buf, a1.length);
else if (a1.length < a2.length)
return -1;
else
return 1;
}
size_t TypeInfo_Abuf::tsize()
{
return sizeof(Abuf);
}
void TypeInfo_Abuf::swap(void *p1, void *p2)
{
assert(0);
}
#pragma pack(1)
struct DebugInfoHeader
{ unsigned total_length;
unsigned short version;
unsigned abbrev_offset;
unsigned char address_size;
};
#pragma pack()
static DebugInfoHeader debuginfo_init =
{ 0, // total_length
2, // version
0, // abbrev_offset
4 // address_size
};
static DebugInfoHeader debuginfo;
// .debug_line
static IDXSEC lineseg;
static Outbuffer *linebuf;
static size_t linebuf_filetab_end;
#pragma pack(1)
struct DebugLineHeader
{ unsigned total_length;
unsigned short version;
unsigned prologue_length;
unsigned char minimum_instruction_length;
unsigned char default_is_stmt;
signed char line_base;
unsigned char line_range;
unsigned char opcode_base;
unsigned char standard_opcode_lengths[9];
};
#pragma pack()
static DebugLineHeader debugline_init =
{ 0, // total_length
2, // version
0, // prologue_length
1, // minimum_instruction_length
TRUE, // default_is_stmt
-5, // line_base
14, // line_range
10, // opcode_base
{ 0,1,1,1,1,0,0,0,1 }
};
static DebugLineHeader debugline;
unsigned typidx_tab[TYMAX];
#if MACHOBJ
const char* debug_frame = "__debug_frame";
const char* debug_str = "__debug_str";
const char* debug_ranges = "__debug_ranges";
const char* debug_loc = "__debug_loc";
const char* debug_line = "__debug_line";
const char* debug_abbrev = "__debug_abbrev";
const char* debug_info = "__debug_info";
const char* debug_pubnames = "__debug_pubnames";
const char* debug_aranges = "__debug_aranges";
#elif ELFOBJ
const char* debug_frame = ".debug_frame";
const char* debug_str = ".debug_str";
const char* debug_ranges = ".debug_ranges";
const char* debug_loc = ".debug_loc";
const char* debug_line = ".debug_line";
const char* debug_abbrev = ".debug_abbrev";
const char* debug_info = ".debug_info";
const char* debug_pubnames = ".debug_pubnames";
const char* debug_aranges = ".debug_aranges";
#endif
void dwarf_initfile(const char *filename)
{
#pragma pack(1)
struct DebugFrameHeader
{
unsigned length;
unsigned CIE_id;
unsigned char version;
unsigned char augmentation;
unsigned char code_alignment_factor;
unsigned char data_alignment_factor;
unsigned char return_address_register;
unsigned char opcodes[7];
};
#pragma pack()
static DebugFrameHeader debugFrameHeader =
{ 16, // length
0xFFFFFFFF, // CIE_id
1, // version
0, // augmentation
1, // code alignment factor
0x7C, // data alignment factor (-4)
8, // return address register
{
DW_CFA_def_cfa, 4,4, // r4,4 [r7,8]
DW_CFA_offset +8,1, // r8,1 [r16,1]
DW_CFA_nop,
DW_CFA_nop,
}
};
if (I64)
{ debugFrameHeader.data_alignment_factor = 0x78; // (-8)
debugFrameHeader.return_address_register = 16;
debugFrameHeader.opcodes[1] = 7; // RSP
debugFrameHeader.opcodes[2] = 8;
debugFrameHeader.opcodes[3] = DW_CFA_offset + 16; // RIP
}
assert(debugFrameHeader.data_alignment_factor == 0x80 - OFFSET_FAC);
int seg = dwarf_getsegment(debug_frame, 1);
debug_frame_secidx = SegData[seg]->SDshtidx;
Outbuffer *debug_frame_buf = SegData[seg]->SDbuf;
debug_frame_buf->reserve(1000);
debug_frame_buf->writen(&debugFrameHeader,debugFrameHeader.length + 4);
/* ======================================== */
seg = dwarf_getsegment(debug_str, 0);
debug_str_secidx = SegData[seg]->SDshtidx;
debug_str_buf = SegData[seg]->SDbuf;
debug_str_buf->reserve(1000);
/* ======================================== */
debug_ranges_seg = dwarf_getsegment(debug_ranges, 0);
debug_ranges_secidx = SegData[debug_ranges_seg]->SDshtidx;
debug_ranges_buf = SegData[debug_ranges_seg]->SDbuf;
debug_ranges_buf->reserve(1000);
/* ======================================== */
debug_loc_seg = dwarf_getsegment(debug_loc, 0);
debug_loc_secidx = SegData[debug_loc_seg]->SDshtidx;
debug_loc_buf = SegData[debug_loc_seg]->SDbuf;
debug_loc_buf->reserve(1000);
/* ======================================== */
if (infoFileName_table)
{ delete infoFileName_table;
infoFileName_table = NULL;
}
lineseg = dwarf_getsegment(debug_line, 0);
linebuf = SegData[lineseg]->SDbuf;
debugline = debugline_init;
linebuf->write(&debugline, sizeof(debugline));
// include_directories
#if SCPP
list_t pl;
for (pl = pathlist; pl; pl = list_next(pl))
{
linebuf->writeString((char *)list_ptr(pl));
linebuf->writeByte(0);
}
#if linux || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun&&__SVR4
for (pl = pathsyslist; pl; pl = list_next(pl))
{
linebuf->writeString((char *)list_ptr(pl));
linebuf->writeByte(0);
}
#endif
#endif
#if 0 && MARS
for (int i = 0; i < global.params.imppath->dim; i++)
{
linebuf->writeString(global.params.imppath->tdata()[i]);
linebuf->writeByte(0);
}
#endif
linebuf->writeByte(0); // terminated with 0 byte
/* ======================================== */
abbrevseg = dwarf_getsegment(debug_abbrev, 0);
abbrevbuf = SegData[abbrevseg]->SDbuf;
abbrevcode = 1;
// Free only if starting another file. Waste of time otherwise.
if (abbrev_table)
{ delete abbrev_table;
abbrev_table = NULL;
}
static unsigned char abbrevHeader[] =
{
1, // abbreviation code
DW_TAG_compile_unit,
1,
DW_AT_producer, DW_FORM_string,
DW_AT_language, DW_FORM_data1,
DW_AT_name, DW_FORM_string,
DW_AT_comp_dir, DW_FORM_string,
DW_AT_low_pc, DW_FORM_addr,
DW_AT_entry_pc, DW_FORM_addr,
DW_AT_ranges, DW_FORM_data4,
DW_AT_stmt_list, DW_FORM_data4,
0, 0,
};
abbrevbuf->write(abbrevHeader,sizeof(abbrevHeader));
/* ======================================== */
infoseg = dwarf_getsegment(debug_info, 0);
infobuf = SegData[infoseg]->SDbuf;
debuginfo = debuginfo_init;
if (I64)
debuginfo.address_size = 8;
infobuf->write(&debuginfo, sizeof(debuginfo));
#if !MACHOBJ
// Contrary to the DWARF spec, OS X complains if the offset is included
dwarf_addrel(infoseg,6,abbrevseg);
#endif
infobuf->writeuLEB128(1); // abbreviation code
#if MARS
infobuf->write("Digital Mars D ");
infobuf->writeString(global.version); // DW_AT_producer
// DW_AT_language
infobuf->writeByte((config.fulltypes == CVDWARF_D) ? DW_LANG_D : DW_LANG_C89);
#elif SCPP
infobuf->write("Digital Mars C ");
infobuf->writeString(global.version); // DW_AT_producer
infobuf->writeByte(DW_LANG_C89); // DW_AT_language
#else
assert(0);
#endif
infobuf->writeString(filename); // DW_AT_name
#if 0
// This relies on an extension to POSIX.1 not always implemented
char *cwd = getcwd(NULL, 0);
#else
char *cwd;
size_t sz = 80;
while (1)
{
errno = 0;
cwd = (char *)malloc(sz + 1);
if (!cwd)
err_nomem();
char *buf = getcwd(cwd, sz);
if (buf)
{ cwd[sz] = 0; // man page doesn't say if always 0 terminated
break;
}
if (errno == ERANGE)
{
sz += 80;
free(cwd);
continue;
}
cwd[0] = 0;
break;
}
#endif
//infobuf->write32(elf_addstr(debug_str_buf, cwd)); // DW_AT_comp_dir as DW_FORM_strp, doesn't work on some systems
infobuf->writeString(cwd); // DW_AT_comp_dir as DW_FORM_string
free(cwd);
append_addr(infobuf, 0); // DW_AT_low_pc
append_addr(infobuf, 0); // DW_AT_entry_pc
dwarf_addrel(infoseg,infobuf->size(),debug_ranges_seg);
infobuf->write32(0); // DW_AT_ranges
dwarf_addrel(infoseg,infobuf->size(),lineseg);
infobuf->write32(0); // DW_AT_stmt_list
memset(typidx_tab, 0, sizeof(typidx_tab));
/* ======================================== */
seg = dwarf_getsegment(debug_pubnames, 0);
debug_pubnames_secidx = SegData[seg]->SDshtidx;
debug_pubnames_buf = SegData[seg]->SDbuf;
debug_pubnames_buf->reserve(1000);
debug_pubnames_buf->write32(0); // unit_length
debug_pubnames_buf->writeWord(2); // version
dwarf_addrel(seg,debug_pubnames_buf->size(),lineseg);
debug_pubnames_buf->write32(0); // debug_info_offset
debug_pubnames_buf->write32(0); // debug_info_length
/* ======================================== */
debug_aranges_seg = dwarf_getsegment(debug_aranges, 0);
debug_aranges_secidx = SegData[debug_aranges_seg]->SDshtidx;
debug_aranges_buf = SegData[debug_aranges_seg]->SDbuf;
debug_aranges_buf->reserve(1000);
debug_aranges_buf->write32(0); // unit_length
debug_aranges_buf->writeWord(2); // version
dwarf_addrel(debug_aranges_seg,debug_aranges_buf->size(),infoseg);
debug_aranges_buf->write32(0); // debug_info_offset
debug_aranges_buf->writeByte(I64 ? 8 : 4); // address_size
debug_aranges_buf->writeByte(0); // segment_size
debug_aranges_buf->write32(0); // pad to 16
}
/*************************************
* Add a file to the .debug_line header
*/
int dwarf_line_addfile(const char* filename)
{
if (!infoFileName_table) {
infoFileName_table = new AArray(&ti_abuf, sizeof(unsigned));
linebuf_filetab_end = linebuf->size();
}
Abuf abuf;
abuf.buf = (const unsigned char*)filename;
abuf.length = strlen(filename)-1;
unsigned *pidx = (unsigned *)infoFileName_table->get(&abuf);
if (!*pidx) // if no idx assigned yet
{
*pidx = infoFileName_table->length(); // assign newly computed idx
size_t before = linebuf->size();
linebuf->writeString(filename);
linebuf->writeByte(0); // directory table index
linebuf->writeByte(0); // mtime
linebuf->writeByte(0); // length
linebuf_filetab_end += linebuf->size() - before;
}
return *pidx;
}
void dwarf_initmodule(const char *filename, const char *modname)
{
if (modname)
{
static unsigned char abbrevModule[] =
{
DW_TAG_module,
//1, // one children
0, // no children
DW_AT_name, DW_FORM_string, // module name
0, 0,
};
abbrevcode++;
abbrevbuf->writeuLEB128(abbrevcode);
abbrevbuf->write(abbrevModule,sizeof(abbrevModule));
infobuf->writeuLEB128(abbrevcode); // abbreviation code
infobuf->writeString(modname); // DW_AT_name
//hasModname = 1;
}
else
hasModname = 0;
dwarf_line_addfile(filename);
}
void dwarf_termmodule()
{
if (hasModname)
infobuf->writeByte(0); // end of DW_TAG_module's children
}
/*************************************
* Finish writing Dwarf debug info to object file.
*/
void dwarf_termfile()
{
//printf("dwarf_termfile()\n");
/* ======================================== */
// Put out line number info
// file_names
unsigned last_filenumber = 0;
const char* last_filename = NULL;
for (unsigned seg = 1; seg <= seg_count; seg++)
{
for (unsigned i = 0; i < SegData[seg]->SDlinnum_count; i++)
{
linnum_data *ld = &SegData[seg]->SDlinnum_data[i];
const char *filename;
#if MARS
filename = ld->filename;
#else
Sfile *sf = ld->filptr;
if (sf)
filename = sf->SFname;
else
filename = ::filename;
#endif
if (last_filename == filename)
{
ld->filenumber = last_filenumber;
}
else
{
ld->filenumber = dwarf_line_addfile(filename);
last_filenumber = ld->filenumber;
last_filename = filename;
}
}
}
// assert we haven't emitted anything but file table entries
assert(linebuf->size() == linebuf_filetab_end);
linebuf->writeByte(0); // end of file_names
debugline.prologue_length = linebuf->size() - 10;
for (unsigned seg = 1; seg <= seg_count; seg++)
{
seg_data *sd = SegData[seg];
unsigned addressmax = 0;
unsigned linestart = ~0;
if (!sd->SDlinnum_count)
continue;
#if ELFOBJ
if (!sd->SDsym) // gdb ignores line number data without a DW_AT_name
continue;
#endif
//printf("sd = %x, SDlinnum_count = %d\n", sd, sd->SDlinnum_count);
for (int i = 0; i < sd->SDlinnum_count; i++)
{ linnum_data *ld = &sd->SDlinnum_data[i];
// Set address to start of segment with DW_LNE_set_address
linebuf->writeByte(0);
linebuf->writeByte(NPTRSIZE + 1);
linebuf->writeByte(DW_LNE_set_address);
dwarf_addreladdr(lineseg,linebuf->size(),seg,0);
append_addr(linebuf, 0);
// Dwarf2 6.2.2 State machine registers
unsigned address = 0; // instruction address
unsigned file = ld->filenumber;
unsigned line = 1; // line numbers beginning with 1
linebuf->writeByte(DW_LNS_set_file);
linebuf->writeuLEB128(file);
for (int j = 0; j < ld->linoff_count; j++)
{ int lininc = ld->linoff[j][0] - line;
int addinc = ld->linoff[j][1] - address;
//printf("\tld[%d] line = %d offset = x%x lininc = %d addinc = %d\n", j, ld->linoff[j][0], ld->linoff[j][1], lininc, addinc);
//assert(addinc >= 0);
if (addinc < 0)
continue;
if (j && lininc == 0 && !(addinc && j + 1 == ld->linoff_count))
continue;
line += lininc;
if (line < linestart)
linestart = line;
address += addinc;
if (address >= addressmax)
addressmax = address + 1;
if (lininc >= debugline.line_base && lininc < debugline.line_base + debugline.line_range)
{ unsigned opcode = lininc - debugline.line_base +
debugline.line_range * addinc +
debugline.opcode_base;
if (opcode <= 255)
{ linebuf->writeByte(opcode);
continue;
}
}
if (lininc)
{
linebuf->writeByte(DW_LNS_advance_line);
linebuf->writesLEB128((long)lininc);
}
if (addinc)
{
linebuf->writeByte(DW_LNS_advance_pc);
linebuf->writeuLEB128((unsigned long)addinc);
}
if (lininc || addinc)
linebuf->writeByte(DW_LNS_copy);
}
// Write DW_LNS_advance_pc to cover the function prologue
linebuf->writeByte(DW_LNS_advance_pc);
linebuf->writeuLEB128((unsigned long)(sd->SDbuf->size() - address));
// Write DW_LNE_end_sequence
linebuf->writeByte(0);
linebuf->writeByte(1);
linebuf->writeByte(1);
// reset linnum_data
ld->linoff_count = 0;
}
}
debugline.total_length = linebuf->size() - 4;
memcpy(linebuf->buf, &debugline, sizeof(debugline));
/* ================================================= */
abbrevbuf->writeByte(0);
/* ================================================= */
infobuf->writeByte(0); // ending abbreviation code
debuginfo.total_length = infobuf->size() - 4;
memcpy(infobuf->buf, &debuginfo, sizeof(debuginfo));
/* ================================================= */
// Terminate by offset field containing 0
debug_pubnames_buf->write32(0);
// Plug final sizes into header
*(unsigned *)debug_pubnames_buf->buf = debug_pubnames_buf->size() - 4;
*(unsigned *)(debug_pubnames_buf->buf + 10) = infobuf->size();
/* ================================================= */
// Terminate by address/length fields containing 0
append_addr(debug_aranges_buf, 0);
append_addr(debug_aranges_buf, 0);
// Plug final sizes into header
*(unsigned *)debug_aranges_buf->buf = debug_aranges_buf->size() - 4;
/* ================================================= */
// Terminate by beg address/end address fields containing 0
append_addr(debug_ranges_buf, 0);
append_addr(debug_ranges_buf, 0);
/* ================================================= */
// Free only if starting another file. Waste of time otherwise.
if (type_table)
{ delete type_table;
type_table = NULL;
}
if (functype_table)
{ delete functype_table;
functype_table = NULL;
}
if (functypebuf)
functypebuf->setsize(0);
}
/*****************************************
* Start of code gen for function.
*/
void dwarf_func_start(Symbol *sfunc)
{
if (I16 || I32)
CFA_state_current = CFA_state_init_32;
else if (I64)
CFA_state_current = CFA_state_init_64;
else
assert(0);
assert(CFA_state_current.offset == OFFSET_FAC);
cfa_buf.reset();
}
/*****************************************
* End of code gen for function.
*/
void dwarf_func_term(Symbol *sfunc)
{
//printf("dwarf_func_term(sfunc = '%s')\n", sfunc->Sident);
unsigned funcabbrevcode;
/* Put out the start of the debug_frame entry for this function
*/
Outbuffer *debug_frame_buf;
unsigned debug_frame_buf_offset;
if (I64)
{
#pragma pack(1)
struct DebugFrameFDE
{
unsigned length;
unsigned CIE_pointer;
unsigned long long initial_location;
unsigned long long address_range;
};
#pragma pack()
static DebugFrameFDE debugFrameFDE =
{ 20, // length