-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
dwarfdbginf.d
2905 lines (2515 loc) · 96.8 KB
/
dwarfdbginf.d
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
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/dwarfdbginf.d, backend/dwarfdbginf.d)
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/backend/dwarfdbginf.d
*/
// Emit Dwarf symbolic debug info
/*
Some generic information for debug info on macOS:
The linker on macOS will remove any debug info, i.e. every section with the
`S_ATTR_DEBUG` flag, this includes everything in the `__DWARF` section. By using
the `S_REGULAR` flag the linker will not remove this section. This allows to get
the filenames and line numbers for backtraces from the executable.
Normally the linker removes all the debug info but adds a reference to the
object files. The debugger can then read the object files to get filename and
line number information. It's also possible to use an additional tool that
generates a separate `.dSYM` file. This file can then later be deployed with the
application if debug info is needed when the application is deployed.
*/
module dmd.backend.dwarfdbginf;
version (SCPP)
version = COMPILE;
version (MARS)
version = COMPILE;
version (COMPILE)
{
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import core.stdc.errno;
version(Windows)
{
extern (C) char* getcwd(char* buffer, size_t maxlen);
extern (C) int* _errno(); // not the multi-threaded version
}
else
{
import core.sys.posix.unistd : getcwd;
}
import dmd.backend.cc;
import dmd.backend.cdef;
import dmd.backend.code;
import dmd.backend.code_x86;
import dmd.backend.mem;
import dmd.backend.dlist;
import dmd.backend.el;
import dmd.backend.global;
import dmd.backend.obj;
import dmd.backend.oper;
import dmd.backend.outbuf;
import dmd.backend.ty;
import dmd.backend.type;
static if (ELFOBJ || MACHOBJ)
{
import dmd.backend.aarray;
static if (ELFOBJ)
import dmd.backend.melf;
static if (MACHOBJ)
import dmd.backend.mach;
import dmd.backend.dwarf;
import dmd.backend.dwarf2;
extern (C++):
nothrow:
int REGSIZE();
__gshared
{
extern int seg_count;
static if (MACHOBJ)
{
int except_table_seg = 0; // __gcc_except_tab segment
int except_table_num = 0; // sequence number for GCC_except_table%d symbols
int eh_frame_seg = 0; // __eh_frame segment
Symbol *eh_frame_sym = null; // past end of __eh_frame
}
uint CIE_offset_unwind; // CIE offset for unwind data
uint CIE_offset_no_unwind; // CIE offset for no unwind data
static if (ELFOBJ)
{
IDXSYM elf_addsym(IDXSTR nam, targ_size_t val, uint sz,
uint typ, uint bind, IDXSEC sec,
ubyte visibility = STV_DEFAULT);
void addSegmentToComdat(segidx_t seg, segidx_t comdatseg);
}
Symbol* getRtlsymPersonality();
private Outbuffer *reset_symbuf; // Keep pointers to reset symbols
}
/***********************************
* Determine if generating a eh_frame with full
* unwinding information.
* This decision is done on a per-function basis.
* Returns:
* true if unwinding needs to be done
*/
bool doUnwindEhFrame()
{
if (funcsym_p.Sfunc.Fflags3 & Feh_none)
{
return (config.exe & (EX_FREEBSD | EX_FREEBSD64 | EX_DRAGONFLYBSD64)) != 0;
}
/* FreeBSD fails when having some frames as having unwinding info and some not.
* (It hangs in unittests for std.datetime.)
* g++ on FreeBSD does not generate mixed frames, while g++ on OSX and Linux does.
*/
assert(!(usednteh & ~(EHtry | EHcleanup)));
return (usednteh & (EHtry | EHcleanup)) ||
(config.exe & (EX_FREEBSD | EX_FREEBSD64 | EX_DRAGONFLYBSD64)) && config.useExceptions;
}
static if (ELFOBJ)
SYMIDX MAP_SEG2SYMIDX(int seg) { return SegData[seg].SDsymidx; }
else
SYMIDX MAP_SEG2SYMIDX(int seg) { assert(0); }
int OFFSET_FAC() { return REGSIZE(); }
int dwarf_getsegment(const(char)* name, int align_, int flags)
{
static if (ELFOBJ)
return Obj.getsegment(name, null, flags, 0, align_ * 4);
else static if (MACHOBJ)
return Obj.getsegment(name, "__DWARF", align_ * 2, flags);
else
assert(0);
}
static if (ELFOBJ)
{
int dwarf_getsegment_alloc(const(char)* name, const(char)* suffix, int align_)
{
return Obj.getsegment(name, suffix, SHT_PROGBITS, SHF_ALLOC, align_ * 4);
}
}
int dwarf_except_table_alloc(Symbol *s)
{
//printf("dwarf_except_table_alloc('%s')\n", s.Sident.ptr);
static if (ELFOBJ)
{
/* If `s` is in a COMDAT, then this table needs to go into
* a unique section, which then gets added to the COMDAT group
* associated with `s`.
*/
seg_data *pseg = SegData[s.Sseg];
if (pseg.SDassocseg)
{
const(char)* suffix = s.Sident.ptr; // cpp_mangle(s);
segidx_t tableseg = Obj.getsegment(".gcc_except_table.", suffix, SHT_PROGBITS, SHF_ALLOC|SHF_GROUP, 1);
addSegmentToComdat(tableseg, s.Sseg);
return tableseg;
}
else
return dwarf_getsegment_alloc(".gcc_except_table", null, 1);
}
else static if (MACHOBJ)
{
int seg = Obj.getsegment("__gcc_except_tab", "__TEXT", 2, S_REGULAR);
except_table_seg = seg;
return seg;
}
else
assert(0);
}
int dwarf_eh_frame_alloc()
{
static if (ELFOBJ)
return dwarf_getsegment_alloc(".eh_frame", null, I64 ? 2 : 1);
else static if (MACHOBJ)
{
int seg = Obj.getsegment("__eh_frame", "__TEXT", I64 ? 3 : 2,
S_COALESCED | S_ATTR_NO_TOC | S_ATTR_STRIP_STATIC_SYMS | S_ATTR_LIVE_SUPPORT);
/* Generate symbol for it to use for fixups
*/
if (!eh_frame_sym)
{
type *t = tspvoid;
t.Tcount++;
type_setmangle(&t, mTYman_sys); // no leading '_' for mangled name
eh_frame_sym = symbol_name("EH_frame0", SCstatic, t);
Obj.pubdef(seg, eh_frame_sym, 0);
symbol_keep(eh_frame_sym);
eh_frame_seg = seg;
}
return seg;
}
else
assert(0);
}
// machobj.c
enum RELaddr = 0; // straight address
enum RELrel = 1; // relative to location to be fixed up
void dwarf_addrel(int seg, targ_size_t offset, int targseg, targ_size_t val = 0)
{
static if (ELFOBJ)
Obj.addrel(seg, offset, I64 ? R_X86_64_32 : R_386_32, MAP_SEG2SYMIDX(targseg), val);
else static if (MACHOBJ)
Obj.addrel(seg, offset, null, targseg, RELaddr, cast(uint)val);
else
assert(0);
}
void dwarf_addrel64(int seg, targ_size_t offset, int targseg, targ_size_t val)
{
static if (ELFOBJ)
Obj.addrel(seg, offset, R_X86_64_64, MAP_SEG2SYMIDX(targseg), val);
else static if (MACHOBJ)
Obj.addrel(seg, offset, null, targseg, RELaddr, cast(uint)val);
else
assert(0);
}
void dwarf_appreladdr(int seg, Outbuffer *buf, int targseg, targ_size_t val)
{
if (I64)
{
dwarf_addrel64(seg, buf.size(), targseg, val);
buf.write64(0);
}
else
{
dwarf_addrel(seg, buf.size(), targseg, 0);
buf.write32(cast(uint)val);
}
}
void dwarf_apprel32(int seg, Outbuffer *buf, int targseg, targ_size_t val)
{
dwarf_addrel(seg, buf.size(), targseg, I64 ? val : 0);
buf.write32(I64 ? 0 : cast(uint)val);
}
void append_addr(Outbuffer *buf, targ_size_t addr)
{
if (I64)
buf.write64(addr);
else
buf.write32(cast(uint)addr);
}
/************************ DWARF DEBUG OUTPUT ********************************/
// Dwarf Symbolic Debugging Information
// CFA = value of the stack pointer at the call site in the previous frame
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[17] regstates; // register states
}
/***********************
* Convert CPU register number to Dwarf register number.
* Params:
* reg = CPU register
* Returns:
* dwarf register
*/
int dwarf_regno(int reg)
{
assert(reg < NUMGENREGS);
if (I32)
{
static if (MACHOBJ)
{
if (reg == BP || reg == SP)
reg ^= BP ^ SP; // swap EBP and ESP register values for OSX (!)
}
return reg;
}
else
{
assert(I64);
/* See https://software.intel.com/sites/default/files/article/402129/mpx-linux64-abi.pdf
* Figure 3.3.8 pg. 62
* R8..15 : 8..15
* XMM0..15 : 17..32
* ST0..7 : 33..40
* MM0..7 : 41..48
* XMM16..31 : 67..82
*/
static immutable int[8] to_amd64_reg_map =
// AX CX DX BX SP BP SI DI
[ 0, 2, 1, 3, 7, 6, 4, 5 ];
return reg < 8 ? to_amd64_reg_map[reg] : reg;
}
}
private __gshared
{
CFA_state CFA_state_init_32 = // initial CFA state as defined by CIE
{ 0, // location
-1, // 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
]
};
CFA_state CFA_state_init_64 = // initial CFA state as defined by CIE
{ 0, // location
-1, // 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
]
};
CFA_state CFA_state_current; // current CFA state
Outbuffer cfa_buf; // CFA instructions
}
/***********************************
* Set the location, i.e. the offset from the start
* of the function. It must always be greater than
* the current location.
* Params:
* location = offset from the start of the function
*/
void dwarf_CFA_set_loc(uint location)
{
assert(location >= CFA_state_current.location);
uint inc = cast(uint)(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;
}
/*******************************************
* Set the frame register, and its offset.
* Params:
* reg = machine register
* offset = offset from frame register
*/
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;
}
/***********************************************
* Set reg to be at offset from frame register.
* Params:
* reg = machine register
* offset = offset from frame register
*/
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;
}
/**************************************
* Set total size of arguments pushed on the stack.
* Params:
* sz = total size
*/
void dwarf_CFA_args_size(size_t sz)
{
cfa_buf.writeByte(DW_CFA_GNU_args_size);
cfa_buf.writeuLEB128(cast(uint)sz);
}
struct Section
{
segidx_t seg = 0;
IDXSEC secidx = 0;
Outbuffer *buf = null;
const(char)* name;
static if (MACHOBJ)
immutable flags = S_ATTR_DEBUG;
else
immutable flags = SHT_PROGBITS;
/* Allocate and initialize Section
*/
nothrow void initialize()
{
const segidx_t segi = dwarf_getsegment(name, 0, flags);
seg = segi;
secidx = SegData[segi].SDshtidx;
buf = SegData[segi].SDbuf;
buf.reserve(1000);
}
}
private __gshared
{
static if (MACHOBJ)
{
Section debug_pubnames = { name: "__debug_pubnames" };
Section debug_aranges = { name: "__debug_aranges" };
Section debug_ranges = { name: "__debug_ranges" };
Section debug_loc = { name: "__debug_loc" };
Section debug_abbrev = { name: "__debug_abbrev" };
Section debug_info = { name: "__debug_info" };
Section debug_str = { name: "__debug_str" };
// We use S_REGULAR to make sure the linker doesn't remove this section. Needed
// for filenames and line numbers in backtraces.
Section debug_line = { name: "__debug_line", flags: S_REGULAR };
}
else static if (ELFOBJ)
{
Section debug_pubnames = { name: ".debug_pubnames" };
Section debug_aranges = { name: ".debug_aranges" };
Section debug_ranges = { name: ".debug_ranges" };
Section debug_loc = { name: ".debug_loc" };
Section debug_abbrev = { name: ".debug_abbrev" };
Section debug_info = { name: ".debug_info" };
Section debug_str = { name: ".debug_str" };
Section debug_line = { name: ".debug_line" };
}
static if (MACHOBJ)
const char* debug_frame_name = "__debug_frame";
else static if (ELFOBJ)
const char* debug_frame_name = ".debug_frame";
/* DWARF 7.5.3: "Each declaration begins with an unsigned LEB128 number
* representing the abbreviation code itself."
*/
uint abbrevcode = 1;
AApair *abbrev_table;
int hasModname; // 1 if has DW_TAG_module
// .debug_info
AAchars *infoFileName_table;
AApair *type_table;
AApair *functype_table; // not sure why this cannot be combined with type_table
Outbuffer *functypebuf;
struct DebugInfoHeader
{
align (1):
uint total_length;
ushort version_;
uint abbrev_offset;
ubyte address_size;
}
// Workaround https://issues.dlang.org/show_bug.cgi?id=16563
// Struct alignment is ignored due to 2.072 regression.
static assert((DebugInfoHeader.alignof == 1 && DebugInfoHeader.sizeof == 11) ||
(DebugInfoHeader.alignof == 4 && DebugInfoHeader.sizeof == 12));
DebugInfoHeader debuginfo_init =
{ 0, // total_length
3, // version_
0, // abbrev_offset
4 // address_size
};
DebugInfoHeader debuginfo;
// .debug_line
size_t linebuf_filetab_end;
struct DebugLineHeader
{
align (1):
uint total_length;
ushort version_;
uint prologue_length;
ubyte minimum_instruction_length;
ubyte default_is_stmt;
byte line_base;
ubyte line_range;
ubyte opcode_base;
ubyte[9] standard_opcode_lengths;
}
static assert(DebugLineHeader.sizeof == 24);
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 ]
};
DebugLineHeader debugline;
public uint[TYMAX] typidx_tab;
}
/*****************************************
* Append .debug_frame header to buf.
* Params:
* buf = write raw data here
*/
void writeDebugFrameHeader(Outbuffer *buf)
{
static struct DebugFrameHeader
{
align (1):
uint length;
uint CIE_id;
ubyte version_;
ubyte augmentation;
ubyte code_alignment_factor;
ubyte data_alignment_factor;
ubyte return_address_register;
ubyte[11] opcodes;
}
static assert(DebugFrameHeader.sizeof == 24);
__gshared 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,
DW_CFA_nop, DW_CFA_nop, // 64 padding
DW_CFA_nop, DW_CFA_nop, // 64 padding
]
};
if (I64)
{ debugFrameHeader.length = 20;
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);
buf.writen(&debugFrameHeader,debugFrameHeader.length + 4);
}
/*****************************************
* Append .eh_frame header to buf.
* Almost identical to .debug_frame
* Params:
* dfseg = SegData[] index for .eh_frame
* buf = write raw data here
* personality = "__dmd_personality_v0"
* ehunwind = will have EH unwind table
* Returns:
* offset of start of this header
* See_Also:
* https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/ehframechpt.html
*/
private uint writeEhFrameHeader(IDXSEC dfseg, Outbuffer *buf, Symbol *personality, bool ehunwind)
{
/* Augmentation string:
* z = first character, means Augmentation Data field is present
* eh = EH Data field is present
* P = Augmentation Data contains 2 args:
* 1. encoding of 2nd arg
* 2. address of personality routine
* L = Augmentation Data contains 1 arg:
* 1. the encoding used for Augmentation Data in FDE
* Augmentation Data in FDE:
* 1. address of LSDA (gcc_except_table)
* R = Augmentation Data contains 1 arg:
* 1. encoding of addresses in FDE
* Non-EH code: "zR"
* EH code: "zPLR"
*/
const uint startsize = cast(uint)buf.size();
// Length of CIE, not including padding
const uint cielen = 4 + 4 + 1 +
(ehunwind ? 5 : 3) +
1 + 1 + 1 +
(ehunwind ? 8 : 2) +
5;
const uint pad = -cielen & (I64 ? 7 : 3); // pad to addressing unit size boundary
const uint length = cielen + pad - 4;
buf.reserve(length + 4);
buf.write32(length); // length of CIE, not including length and extended length fields
buf.write32(0); // CIE ID
buf.writeByten(1); // version_
if (ehunwind)
buf.write("zPLR".ptr, 5); // Augmentation String
else
buf.writen("zR".ptr, 3);
// not present: EH Data: 4 bytes for I32, 8 bytes for I64
buf.writeByten(1); // code alignment factor
buf.writeByten(cast(ubyte)(0x80 - OFFSET_FAC)); // data alignment factor (I64 ? -8 : -4)
buf.writeByten(I64 ? 16 : 8); // return address register
if (ehunwind)
{
static if (ELFOBJ)
{
const ubyte personality_pointer_encoding = config.flags3 & CFG3pic
? DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4
: DW_EH_PE_absptr | DW_EH_PE_udata4;
const ubyte LSDA_pointer_encoding = config.flags3 & CFG3pic
? DW_EH_PE_pcrel | DW_EH_PE_sdata4
: DW_EH_PE_absptr | DW_EH_PE_udata4;
const ubyte address_pointer_encoding =
DW_EH_PE_pcrel | DW_EH_PE_sdata4;
}
else static if (MACHOBJ)
{
const ubyte personality_pointer_encoding =
DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
const ubyte LSDA_pointer_encoding =
DW_EH_PE_pcrel | DW_EH_PE_ptr;
const ubyte address_pointer_encoding =
DW_EH_PE_pcrel | DW_EH_PE_ptr;
}
buf.writeByten(7); // Augmentation Length
buf.writeByten(personality_pointer_encoding); // P: personality routine address encoding
/* MACHOBJ 64: pcrel 1 length 2 extern 1 RELOC_GOT
* 32: [4] address x0013 pcrel 0 length 2 value xfc type 4 RELOC_LOCAL_SECTDIFF
* [5] address x0000 pcrel 0 length 2 value xc7 type 1 RELOC_PAIR
*/
dwarf_reftoident(dfseg, buf.size(), personality, 0);
buf.writeByten(LSDA_pointer_encoding); // L: address encoding for LSDA in FDE
buf.writeByten(address_pointer_encoding); // R: encoding of addresses in FDE
}
else
{
buf.writeByten(1); // Augmentation Length
static if (ELFOBJ)
buf.writeByten(DW_EH_PE_pcrel | DW_EH_PE_sdata4); // R: encoding of addresses in FDE
static if (MACHOBJ)
buf.writeByten(DW_EH_PE_pcrel | DW_EH_PE_ptr); // R: encoding of addresses in FDE
}
// Set CFA beginning state at function entry point
if (I64)
{
buf.writeByten(DW_CFA_def_cfa); // DEF_CFA r7,8 RSP is at offset 8
buf.writeByten(7); // r7 is RSP
buf.writeByten(8);
buf.writeByten(DW_CFA_offset + 16); // OFFSET r16,1 RIP is at -8*1[RSP]
buf.writeByten(1);
}
else
{
buf.writeByten(DW_CFA_def_cfa); // DEF_CFA ESP,4
buf.writeByten(cast(ubyte)dwarf_regno(SP));
buf.writeByten(4);
buf.writeByten(DW_CFA_offset + 8); // OFFSET r8,1
buf.writeByten(1);
}
for (uint i = 0; i < pad; ++i)
buf.writeByten(DW_CFA_nop);
assert(startsize + length + 4 == buf.size());
return startsize;
}
/*********************************************
* Generate function's Frame Description Entry into .debug_frame
* Params:
* dfseg = SegData[] index for .debug_frame
* sfunc = the function
*/
void writeDebugFrameFDE(IDXSEC dfseg, Symbol *sfunc)
{
if (I64)
{
static struct DebugFrameFDE64
{
align (1):
uint length;
uint CIE_pointer;
ulong initial_location;
ulong address_range;
}
static assert(DebugFrameFDE64.sizeof == 24);
__gshared DebugFrameFDE64 debugFrameFDE64 =
{ 20, // length
0, // CIE_pointer
0, // initial_location
0, // address_range
};
// Pad to 8 byte boundary
for (uint n = (-cfa_buf.size() & 7); n; n--)
cfa_buf.writeByte(DW_CFA_nop);
debugFrameFDE64.length = 20 + cast(uint)cfa_buf.size();
debugFrameFDE64.address_range = sfunc.Ssize;
// Do we need this?
//debugFrameFDE64.initial_location = sfunc.Soffset;
Outbuffer *debug_frame_buf = SegData[dfseg].SDbuf;
uint debug_frame_buf_offset = cast(uint)(debug_frame_buf.p - debug_frame_buf.buf);
debug_frame_buf.reserve(1000);
debug_frame_buf.writen(&debugFrameFDE64,debugFrameFDE64.sizeof);
debug_frame_buf.write(&cfa_buf);
static if (ELFOBJ)
// Absolute address for debug_frame, relative offset for eh_frame
dwarf_addrel(dfseg,debug_frame_buf_offset + 4,dfseg,0);
dwarf_addrel64(dfseg,debug_frame_buf_offset + 8,sfunc.Sseg,0);
}
else
{
static struct DebugFrameFDE32
{
align (1):
uint length;
uint CIE_pointer;
uint initial_location;
uint address_range;
}
static assert(DebugFrameFDE32.sizeof == 16);
__gshared DebugFrameFDE32 debugFrameFDE32 =
{ 12, // length
0, // CIE_pointer
0, // initial_location
0, // address_range
};
// Pad to 4 byte boundary
for (uint n = (-cfa_buf.size() & 3); n; n--)
cfa_buf.writeByte(DW_CFA_nop);
debugFrameFDE32.length = 12 + cast(uint)cfa_buf.size();
debugFrameFDE32.address_range = cast(uint)sfunc.Ssize;
// Do we need this?
//debugFrameFDE32.initial_location = sfunc.Soffset;
Outbuffer *debug_frame_buf = SegData[dfseg].SDbuf;
uint debug_frame_buf_offset = cast(uint)(debug_frame_buf.p - debug_frame_buf.buf);
debug_frame_buf.reserve(1000);
debug_frame_buf.writen(&debugFrameFDE32,debugFrameFDE32.sizeof);
debug_frame_buf.write(&cfa_buf);
static if (ELFOBJ)
// Absolute address for debug_frame, relative offset for eh_frame
dwarf_addrel(dfseg,debug_frame_buf_offset + 4,dfseg,0);
dwarf_addrel(dfseg,debug_frame_buf_offset + 8,sfunc.Sseg,0);
}
}
/*********************************************
* Append function's FDE (Frame Description Entry) to .eh_frame
* Params:
* dfseg = SegData[] index for .eh_frame
* sfunc = the function
* ehunwind = will have EH unwind table
* CIE_offset = offset of enclosing CIE
*/
void writeEhFrameFDE(IDXSEC dfseg, Symbol *sfunc, bool ehunwind, uint CIE_offset)
{
Outbuffer *buf = SegData[dfseg].SDbuf;
const uint startsize = cast(uint)buf.size();
static if (MACHOBJ)
{
/* Create symbol named "funcname.eh" for the start of the FDE
*/
Symbol *fdesym;
{
const size_t len = strlen(sfunc.Sident.ptr);
char *name = cast(char *)malloc(len + 3 + 1);
if (!name)
err_nomem();
memcpy(name, sfunc.Sident.ptr, len);
memcpy(name + len, ".eh".ptr, 3 + 1);
fdesym = symbol_name(name, SCglobal, tspvoid);
Obj.pubdef(dfseg, fdesym, startsize);
symbol_keep(fdesym);
}
}
if (sfunc.ty() & mTYnaked)
{
/* Do not have info on naked functions. Assume they are set up as:
* push RBP
* mov RSP,RSP
*/
int off = 2 * REGSIZE;
dwarf_CFA_set_loc(1);
dwarf_CFA_set_reg_offset(SP, off);
dwarf_CFA_offset(BP, -off);
dwarf_CFA_set_loc(I64 ? 4 : 3);
dwarf_CFA_set_reg_offset(BP, off);
}
// Length of FDE, not including padding
static if (ELFOBJ)
const uint fdelen = 4 + 4
+ 4 + 4
+ (ehunwind ? 5 : 1) + cast(uint)cfa_buf.size();
else static if (MACHOBJ)
const uint fdelen = 4 + 4
+ (I64 ? 8 + 8 : 4 + 4) // PC_Begin + PC_Range
+ (ehunwind ? (I64 ? 9 : 5) : 1) + cast(uint)cfa_buf.size();
const uint pad = -fdelen & (I64 ? 7 : 3); // pad to addressing unit size boundary
const uint length = fdelen + pad - 4;
buf.reserve(length + 4);
buf.write32(length); // Length (no Extended Length)
buf.write32((startsize + 4) - CIE_offset); // CIE Pointer
static if (ELFOBJ)
{
int fixup = I64 ? R_X86_64_PC32 : R_386_PC32;
buf.write32(cast(uint)(I64 ? 0 : sfunc.Soffset)); // address of function
Obj.addrel(dfseg, startsize + 8, fixup, MAP_SEG2SYMIDX(sfunc.Sseg), sfunc.Soffset);
//Obj.reftoident(dfseg, startsize + 8, sfunc, 0, CFpc32 | CFoff); // PC_begin
buf.write32(cast(uint)sfunc.Ssize); // PC Range
}
else static if (MACHOBJ)
{
dwarf_eh_frame_fixup(dfseg, buf.size(), sfunc, 0, fdesym);
if (I64)
buf.write64(sfunc.Ssize); // PC Range
else
buf.write32(cast(uint)sfunc.Ssize); // PC Range
}
else
assert(0);
if (ehunwind)
{
int etseg = dwarf_except_table_alloc(sfunc);
static if (ELFOBJ)
{
buf.writeByten(4); // Augmentation Data Length
buf.write32(I64 ? 0 : sfunc.Sfunc.LSDAoffset); // address of LSDA (".gcc_except_table")
if (config.flags3 & CFG3pic)
{
Obj.addrel(dfseg, buf.size() - 4, fixup, MAP_SEG2SYMIDX(etseg), sfunc.Sfunc.LSDAoffset);
}
else
dwarf_addrel(dfseg, buf.size() - 4, etseg, sfunc.Sfunc.LSDAoffset); // and the fixup
}
else static if (MACHOBJ)
{
buf.writeByten(I64 ? 8 : 4); // Augmentation Data Length
dwarf_eh_frame_fixup(dfseg, buf.size(), sfunc.Sfunc.LSDAsym, 0, fdesym);
}
}
else
buf.writeByten(0); // Augmentation Data Length
buf.write(&cfa_buf);
for (uint i = 0; i < pad; ++i)
buf.writeByten(DW_CFA_nop);
assert(startsize + length + 4 == buf.size());
}
void dwarf_initfile(const(char)* filename)
{
if (config.ehmethod == EHmethod.EH_DWARF)
{
static if (MACHOBJ)
{
except_table_seg = 0;
except_table_num = 0;
eh_frame_seg = 0;