-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlnfodwrf.pas
More file actions
1485 lines (1315 loc) · 42 KB
/
Copy pathlnfodwrf.pas
File metadata and controls
1485 lines (1315 loc) · 42 KB
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
{
This file is part of the Free Pascal run time library.
Copyright (c) 2006 by Thomas Schatzl, member of the FreePascal
Development team
Parts (c) 2000 Peter Vreman (adapted from original dwarfs line
reader)
Dwarf LineInfo Retriever
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program 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.
**********************************************************************}
{
This unit should not be compiled in objfpc mode, since this would make it
dependent on objpas unit.
}
unit lnfodwrf;
interface
{$S-}
type
CodePointer = Pointer;
QWord = UInt64;
PtrUInt = QWord;
CodePtrUInt = PtrUInt;
DWord = Cardinal;
SizeInt = integer;
TBacktraceStrFunc = Function (Addr: CodePointer): ShortString;
function GetLineInfo(addr:codeptruint;var func,source:string;var line:integer) : boolean;
function DwarfBackTraceStr(addr: CodePointer): string;
procedure CloseDwarf;
var
// Allows more efficient operation by reusing previously loaded debug data
// when the target module filename is the same. However, if an invalid memory
// address is supplied then further calls may result in an undefined behaviour.
// In summary: enable for speed, disable for resilience.
AllowReuseOfLineInfoData: Boolean = True;
implementation
uses
System.SysUtils,
exeinfo;
{ Current issues:
- ignores DW_LNS_SET_FILE
}
//{$MACRO ON}
{ $DEFINE DEBUG_DWARF_PARSER}
{$ifdef DEBUG_DWARF_PARSER}
{$define DEBUG_WRITELN := WriteLn}
{$define DEBUG_COMMENT := }
{$else}
{$define DEBUG_WRITELN := //}
{$define DEBUG_COMMENT := //}
{$endif}
{ some type definitions }
type
Bool8 = ByteBool;
{$ifdef CPUI8086}
TOffset = Word;
{$else CPUI8086}
TOffset = PtrUInt;
{$endif CPUI8086}
TSegment = Word;
const
EBUF_SIZE = 100;
//{$WARNING This code is not thread-safe, and needs improvement}
var
{ the input file to read DWARF debug info from, i.e. paramstr(0) }
e : TExeFile;
EBuf: Array [0..EBUF_SIZE-1] of Byte;
EBufCnt, EBufPos: Integer;
{ the offset and size of the DWARF debug_line section in the file }
Dwarf_Debug_Line_Section_Offset,
Dwarf_Debug_Line_Section_Size,
{ the offset and size of the DWARF debug_info section in the file }
Dwarf_Debug_Info_Section_Offset,
Dwarf_Debug_Info_Section_Size,
{ the offset and size of the DWARF debug_aranges section in the file }
Dwarf_Debug_Aranges_Section_Offset,
Dwarf_Debug_Aranges_Section_Size,
{ the offset and size of the DWARF debug_abbrev section in the file }
Dwarf_Debug_Abbrev_Section_Offset,
Dwarf_Debug_Abbrev_Section_Size : integer;
{ DWARF 2 default opcodes}
const
{ Extended opcodes }
DW_LNE_END_SEQUENCE = 1;
DW_LNE_SET_ADDRESS = 2;
DW_LNE_DEFINE_FILE = 3;
{$ifdef CPUI8086}
{ non-standard Open Watcom extension; might conflict with future versions of
the DWARF standard }
DW_LNE_SET_SEGMENT = 4;
{$endif CPUI8086}
{ Standard opcodes }
DW_LNS_COPY = 1;
DW_LNS_ADVANCE_PC = 2;
DW_LNS_ADVANCE_LINE = 3;
DW_LNS_SET_FILE = 4;
DW_LNS_SET_COLUMN = 5;
DW_LNS_NEGATE_STMT = 6;
DW_LNS_SET_BASIC_BLOCK = 7;
DW_LNS_CONST_ADD_PC = 8;
DW_LNS_FIXED_ADVANCE_PC = 9;
DW_LNS_SET_PROLOGUE_END = 10;
DW_LNS_SET_EPILOGUE_BEGIN = 11;
DW_LNS_SET_ISA = 12;
DW_FORM_addr = $1;
DW_FORM_block2 = $3;
DW_FORM_block4 = $4;
DW_FORM_data2 = $5;
DW_FORM_data4 = $6;
DW_FORM_data8 = $7;
DW_FORM_string = $8;
DW_FORM_block = $9;
DW_FORM_block1 = $a;
DW_FORM_data1 = $b;
DW_FORM_flag = $c;
DW_FORM_sdata = $d;
DW_FORM_strp = $e;
DW_FORM_udata = $f;
DW_FORM_ref_addr = $10;
DW_FORM_ref1 = $11;
DW_FORM_ref2 = $12;
DW_FORM_ref4 = $13;
DW_FORM_ref8 = $14;
DW_FORM_ref_udata = $15;
DW_FORM_indirect = $16;
DW_FORM_sec_offset = $17;
DW_FORM_exprloc = $18;
DW_FORM_flag_present = $19;
type
{ state record for the line info state machine }
TMachineState = record
address : QWord;
segment : TSegment;
file_id : DWord;
line : QWord;
column : DWord;
is_stmt : Boolean;
basic_block : Boolean;
end_sequence : Boolean;
prolouge_end : Boolean;
epilouge_begin : Boolean;
isa : DWord;
append_row : Boolean;
end;
{ DWARF line number program header preceding the line number program, 64 bit version }
TLineNumberProgramHeader64 = packed record
magic : DWord;
unit_length : QWord;
version : Word;
length : QWord;
minimum_instruction_length : Byte;
default_is_stmt : Bool8;
line_base : ShortInt;
line_range : Byte;
opcode_base : Byte;
end;
{ DWARF line number program header preceding the line number program, 32 bit version }
TLineNumberProgramHeader32 = packed record
unit_length : DWord;
version : Word;
length : DWord;
minimum_instruction_length : Byte;
default_is_stmt : Bool8;
line_base : ShortInt;
line_range : Byte;
opcode_base : Byte;
end;
TDebugInfoProgramHeader64 = packed record
magic : DWord;
unit_length : QWord;
version : Word;
debug_abbrev_offset : QWord;
address_size : Byte;
end;
TDebugInfoProgramHeader32= packed record
unit_length : DWord;
version : Word;
debug_abbrev_offset : DWord;
address_size : Byte;
end;
TDebugArangesHeader64 = packed record
magic : DWord;
unit_length : QWord;
version : Word;
debug_info_offset : QWord;
address_size : Byte;
segment_size : Byte;
{$ifndef CPUI8086}
padding : DWord;
{$endif CPUI8086}
end;
TDebugArangesHeader32= packed record
unit_length : DWord;
version : Word;
debug_info_offset : DWord;
address_size : Byte;
segment_size : Byte;
{$ifndef CPUI8086}
padding : DWord;
{$endif CPUI8086}
end;
{---------------------------------------------------------------------------
I/O utility functions
---------------------------------------------------------------------------}
type
{$ifdef cpui8086}
TFilePos = integer;
{$else cpui8086}
TFilePos = SizeInt;
{$endif cpui8086}
var
base, limit : TFilePos;
index : TFilePos;
baseaddr : {$ifdef cpui8086}farpointer{$else}pointer{$endif};
filename,
dbgfn : ansistring;
lastfilename: string; { store last processed file }
lastopendwarf: Boolean; { store last result of processing a file }
{$ifdef cpui8086}
function tofar(fp: FarPointer): FarPointer; inline;
begin
tofar:=fp;
end;
function tofar(cp: NearCsPointer): FarPointer; inline;
begin
tofar:=Ptr(CSeg,Word(cp));
end;
function tofar(cp: NearPointer): FarPointer; inline;
begin
tofar:=Ptr(DSeg,Word(cp));
end;
{$else cpui8086}
type
tofar=Pointer;
{$endif cpui8086}
function OpenDwarf(addr : codepointer) : boolean;
var
oldprocessaddress: TExeProcessAddress;
begin
// False by default
OpenDwarf:=false;
// Empty so can test if GetModuleByAddr has worked
filename := '';
// Get filename by address using GetModuleByAddr
GetModuleByAddr(tofar(addr),baseaddr,filename);
{$ifdef DEBUG_LINEINFO}
// WriteLog(stderr,filename,' Baseaddr: ',IntToHex(baseaddr));
{$endif DEBUG_LINEINFO}
// Check if GetModuleByAddr has worked
if filename = '' then
exit;
// If target filename same as previous, then re-use previous result
if AllowReuseOfLineInfoData and (string(filename) = lastfilename) then
begin
{$ifdef DEBUG_LINEINFO}
// WriteLog(stderr,'Reusing debug data');
{$endif DEBUG_LINEINFO}
OpenDwarf:=lastopendwarf;
exit;
end;
// Close previously opened Dwarf
CloseDwarf;
// Reset last open dwarf result
lastopendwarf := false;
// Save newly processed filename
lastfilename := string(filename);
// Open exe file or debug link
if not OpenExeFile(e,filename) then
exit;
if ReadDebugLink(e,dbgfn) then
begin
oldprocessaddress:=e.processaddress;
CloseExeFile(e);
if not OpenExeFile(e,dbgfn) then
exit;
e.processaddress:=oldprocessaddress;
end;
// Find debug data section
//e.processaddress:=ptruint(baseaddr)-e.processaddress;
if FindExeSection(e,'.debug_line',Dwarf_Debug_Line_Section_offset,dwarf_Debug_Line_Section_size) and
FindExeSection(e,'.debug_info',Dwarf_Debug_Info_Section_offset,dwarf_Debug_Info_Section_size) and
FindExeSection(e,'.debug_abbrev',Dwarf_Debug_Abbrev_Section_offset,dwarf_Debug_Abbrev_Section_size) and
FindExeSection(e,'.debug_aranges',Dwarf_Debug_Aranges_Section_offset,dwarf_Debug_Aranges_Section_size) then
begin
lastopendwarf:=true;
OpenDwarf:=true;
// WriteLog('.debug_line starts at offset $' + IntToHex(Dwarf_Debug_Line_Section_offset,8)+' with a size of '+Dwarf_Debug_Line_Section_Size.ToString+' Bytes');
// WriteLog('.debug_info starts at offset $' +IntToHex(Dwarf_Debug_Info_Section_offset,8)+' with a size of '+Dwarf_Debug_Info_Section_Size.ToString+' Bytes');
// WriteLog('.debug_abbrev starts at offset $'+IntToHex(Dwarf_Debug_Abbrev_Section_offset,8)+' with a size of '+Dwarf_Debug_Abbrev_Section_Size.ToString+' Bytes');
// WriteLog('.debug_aranges starts at offset $'+IntToHex(Dwarf_Debug_Aranges_Section_offset,8)+' with a size of '+Dwarf_Debug_Aranges_Section_Size.ToString+' Bytes');
end
else
CloseExeFile(e);
end;
procedure CloseDwarf;
begin
if e.isopen then
CloseExeFile(e);
// Reset last processed filename
lastfilename := '';
end;
function Init(aBase, aLimit : Int64) : Boolean; overload;
begin
base := aBase;
limit := aLimit;
Result := (aBase + limit) <= e.size;
seek(e.f, base);
EBufCnt := 0;
EBufPos := 0;
index := 0;
end;
function Init(aBase : Int64) : Boolean; overload;
begin
Result := Init(aBase, limit - (aBase - base));
end;
function Pos() : TFilePos;
begin
Result := index;
end;
procedure Seek(const newIndex : Int64);
begin
index := newIndex;
system.seek(e.f, base + index);
EBufCnt := 0;
EBufPos := 0;
end;
{ Returns the next Byte from the input stream, or -1 if there has been
an error }
function ReadNext() : integer; inline; overload;
var
bytesread : integer;
begin
//Result := -1;
if EBufPos >= EBufCnt then begin
EBufPos := 0;
EBufCnt := EBUF_SIZE;
if EBufCnt > limit - index then
EBufCnt := limit - index;
blockread(e.f, EBuf, EBufCnt, bytesread);
EBufCnt := bytesread;
end;
if EBufPos < EBufCnt then begin
Result := EBuf[EBufPos];
inc(EBufPos);
inc(index);
end
else
Result := -1;
end;
{ Reads the next size bytes into dest. Returns true if successful,
false otherwise. Note that dest may be partially overwritten after
returning false. }
function ReadNext(var dest; size : SizeInt) : Boolean; overload;
var
bytesread, totalread : SizeInt;
r: Boolean;
d: PByte;
begin
d := @dest;
totalread := 0;
r := True;
while (totalread < size) and r do begin;
if EBufPos >= EBufCnt then begin
EBufPos := 0;
EBufCnt := EBUF_SIZE;
if EBufCnt > limit - index then
EBufCnt := limit - index;
blockread(e.f, EBuf, EBufCnt, bytesread);
EBufCnt := bytesread;
if bytesread <= 0 then
r := False;
end;
if EBufPos < EBufCnt then begin
bytesread := EBufCnt - EBufPos;
if bytesread > size - totalread then bytesread := size - totalread;
System.Move(EBuf[EBufPos], d[totalread], bytesread);
inc(EBufPos, bytesread);
inc(index, bytesread);
inc(totalread, bytesread);
end;
end;
Result := r;
end;
{ Reads an unsigned LEB encoded number from the input stream }
function ReadULEB128() : QWord;
var
shift : Byte;
data : PtrInt;
val : QWord;
begin
shift := 0;
Result := 0;
data := ReadNext();
while (data <> -1) do begin
val := data and $7f;
Result := Result or (val shl shift);
inc(shift, 7);
if ((data and $80) = 0) then
break;
data := ReadNext();
end;
end;
{ Reads a signed LEB encoded number from the input stream }
function ReadLEB128() : Int64;
var
shift : Byte;
data : PtrInt;
val : Int64;
begin
shift := 0;
Result := 0;
data := ReadNext();
while (data <> -1) do begin
val := data and $7f;
Result := Result or (val shl shift);
inc(shift, 7);
if ((data and $80) = 0) then
break;
data := ReadNext();
end;
{ extend sign. Note that we can not use shl/shr since the latter does not
translate to arithmetic shifting for signed types }
Result := (not ((Result and (Int64(1) shl (shift-1)))-1)) or Result;
end;
{$ifdef CPUI8086}
{ Reads an address from the current input stream }
function ReadAddress(addr_size: smallint) : LongWord;
begin
if addr_size = 4 then
ReadNext(Result, 4)
else if addr_size = 2 then begin
Result := 0;
ReadNext(Result, 2);
end
else
Result := 0;
end;
{ Reads a segment from the current input stream }
function ReadSegment() : Word;
begin
ReadNext(Result, sizeof(Result));
end;
{$else CPUI8086}
{ Reads an address from the current input stream }
function ReadAddress(addr_size: smallint) : PtrUInt;
begin
ReadNext(result, (sizeof(result)));
end;
{$endif CPUI8086}
{ Reads a zero-terminated string from the current input stream. If the
string is larger than 255 chars (maximum allowed number of elements in
a ShortString, excess characters will be chopped off. }
function ReadString() : ShortString;
var
temp : PtrInt;
i : PtrUInt;
begin
i := 1;
temp := ReadNext();
while (temp > 0) do begin
Result[i] := AnsiChar(temp);
if (i = 255) then begin
{ skip remaining characters }
repeat
temp := ReadNext();
until (temp <= 0);
break;
end;
inc(i);
temp := ReadNext();
end;
{ unexpected end of file occurred? }
if (temp = -1) then
Result := ''
else
SetLength(Result, i-1);
end;
{ Reads an unsigned Half from the current input stream }
function ReadUHalf() : Word;
begin
ReadNext(Result, sizeof(Result));
end;
{---------------------------------------------------------------------------
Generic Dwarf lineinfo reader
The line info reader is based on the information contained in
DWARF Debugging Information Format Version 3
Chapter 6.2 "Line Number Information"
from the
DWARF Debugging Information Format Workgroup.
For more information on this document see also
http://dwarf.freestandards.org/
---------------------------------------------------------------------------}
{ initializes the line info state to the default values }
procedure InitStateRegisters(var state : TMachineState; const aIs_Stmt : Bool8);
begin
with state do begin
address := 0;
segment := 0;
file_id := 1;
line := 1;
column := 0;
is_stmt := aIs_Stmt;
basic_block := false;
end_sequence := false;
prolouge_end := false;
epilouge_begin := false;
isa := 0;
append_row := false;
end;
end;
{ Skips all line info directory entries }
procedure SkipDirectories();
var s : ShortString;
begin
while (true) do begin
s := ReadString();
if (s = '') then break;
// WriteLog('Skipping directory : '+ string(s));
end;
end;
{ Skips an LEB128 }
procedure SkipLEB128();
{$ifdef DEBUG_DWARF_PARSER}
var temp : QWord;
{$endif}
begin
{$ifdef DEBUG_DWARF_PARSER}temp := {$endif}ReadLEB128();
{$ifdef DEBUG_DWARF_PARSER}
// WriteLog('Skipping LEB128 : '+ temp.ToString);
{$endif}
end;
{ Skips the filename section from the current file stream }
procedure SkipFilenames();
var s : ShortString;
begin
while (true) do begin
s := ReadString();
if (s = '') then break;
// WriteLog('Skipping filename : '+ string(s));
SkipLEB128(); { skip the directory index for the file }
SkipLEB128(); { skip last modification time for file }
SkipLEB128(); { skip length of file }
end;
end;
function CalculateAddressIncrement(opcode : Byte; const header : TLineNumberProgramHeader64) : Int64;
begin
Result := (Int64(opcode) - header.opcode_base) div header.line_range * header.minimum_instruction_length;
end;
function GetFullFilename(const filenameStart, directoryStart : Int64; const file_id : DWord) : ShortString;
var
i : DWord;
filename, directory : ShortString;
dirindex : Int64;
begin
filename := '';
directory := '';
i := 1;
dirindex := 0;
Seek(filenameStart);
while (i <= file_id) do begin
filename := ReadString();
// WriteLog('Found "'+ string(filename)+ '"');
if (filename = '') then break;
dirindex := ReadLEB128(); { read the directory index for the file }
SkipLEB128(); { skip last modification time for file }
SkipLEB128(); { skip length of file }
inc(i);
end;
{ if we could not find the file index, exit }
if (filename = '') then begin
Result := '(Unknown file)';
exit;
end;
Seek(directoryStart);
i := 1;
while (i <= dirindex) do begin
directory := ReadString();
if (directory = '') then break;
inc(i);
end;
if (directory<>'') and (directory[length(directory)]<>'/') then
directory:=directory+'/';
Result := directory + filename;
end;
function ParseCompilationUnit(const addr : TOffset; const segment : TSegment; const file_offset : QWord;
var source : String; var line : integer; var found : Boolean) : QWord;
var
state : TMachineState;
{ we need both headers on the stack, although we only use the 64 bit one internally }
header64 : TLineNumberProgramHeader64;
header32 : TLineNumberProgramHeader32;
adjusted_opcode : Int64;
opcode : PtrInt;
extended_opcode : PtrInt;
extended_opcode_length : PtrInt;
i, addrIncrement, lineIncrement : PtrInt;
{$ifdef DEBUG_DWARF_PARSER}
s : ShortString;
{$endif}
numoptable : array[1..255] of Byte;
{ the offset into the file where the include directories are stored for this compilation unit }
include_directories : QWord;
{ the offset into the file where the file names are stored for this compilation unit }
file_names : Int64;
temp_length : DWord;
unit_length : QWord;
header_length : SizeInt;
first_row : Boolean;
prev_line : QWord;
prev_file : DWord;
begin
prev_line := 0;
prev_file := 0;
first_row := true;
found := false;
ReadNext(temp_length, sizeof(temp_length));
if (temp_length <> $ffffffff) then begin
unit_length := temp_length + sizeof(temp_length)
end else begin
ReadNext(unit_length, sizeof(unit_length));
inc(unit_length, 12);
end;
Result := file_offset + unit_length;
Init(file_offset, unit_length);
// WriteLog('Unit length: '+ unit_length.ToString);
if (temp_length <> $ffffffff) then begin
// WriteLog('32 bit DWARF detected');
ReadNext(header32, sizeof(header32));
header64.magic := $ffffffff;
header64.unit_length := header32.unit_length;
header64.version := header32.version;
header64.length := header32.length;
header64.minimum_instruction_length := header32.minimum_instruction_length;
header64.default_is_stmt := header32.default_is_stmt;
header64.line_base := header32.line_base;
header64.line_range := header32.line_range;
header64.opcode_base := header32.opcode_base;
header_length :=
sizeof(header32.length) + sizeof(header32.version) +
sizeof(header32.unit_length);
end else begin
// WriteLog('64 bit DWARF detected');
ReadNext(header64, sizeof(header64));
header_length :=
sizeof(header64.magic) + sizeof(header64.version) +
sizeof(header64.length) + sizeof(header64.unit_length);
end;
inc(header_length, header64.length);
fillchar(numoptable, sizeof(numoptable), #0);
ReadNext(numoptable, header64.opcode_base-1);
// WriteLog('Reading directories...');
include_directories := Pos();
SkipDirectories();
// WriteLog('Reading filenames...');
file_names := Pos();
SkipFilenames();
Seek(header_length);
with header64 do begin
InitStateRegisters(state, default_is_stmt);
end;
opcode := ReadNext();
while (opcode <> -1) and (not found) do begin
// WriteLog('Next opcode: ');
case (opcode) of
{ extended opcode }
0 : begin
extended_opcode_length := ReadULEB128();
extended_opcode := ReadNext();
case (extended_opcode) of
-1: begin
exit;
end;
DW_LNE_END_SEQUENCE : begin
state.end_sequence := true;
state.append_row := true;
// WriteLog('DW_LNE_END_SEQUENCE');
end;
DW_LNE_SET_ADDRESS : begin
state.address := ReadAddress(extended_opcode_length-1);
// WriteLog('DW_LNE_SET_ADDRESS ('+ IntToHex(state.address, sizeof(state.address)*2)+ ')');
end;
{$ifdef CPUI8086}
DW_LNE_SET_SEGMENT : begin
state.segment := ReadSegment();
// WriteLog('DW_LNE_SET_SEGMENT (', IntToHex(state.segment, sizeof(state.segment)*2), ')');
end;
{$endif CPUI8086}
DW_LNE_DEFINE_FILE : begin
{$ifdef DEBUG_DWARF_PARSER}s := {$endif}ReadString();
SkipLEB128();
SkipLEB128();
SkipLEB128();
{$ifdef DEBUG_DWARF_PARSER}
// WriteLog('DW_LNE_DEFINE_FILE ('+ string(s)+ ')');
{$endif}
end;
else begin
// WriteLog('Unknown extended opcode (opcode '+ extended_opcode.ToString+ ' length '+ extended_opcode_length.ToString+ ')');
for i := 0 to extended_opcode_length-2 do
if ReadNext() = -1 then
exit;
end;
end;
end;
DW_LNS_COPY : begin
state.basic_block := false;
state.prolouge_end := false;
state.epilouge_begin := false;
state.append_row := true;
// WriteLog('DW_LNS_COPY');
end;
DW_LNS_ADVANCE_PC : begin
inc(state.address, ReadULEB128() * header64.minimum_instruction_length);
// WriteLog('DW_LNS_ADVANCE_PC ('+ IntToHex(state.address, sizeof(state.address)*2)+ ')');
end;
DW_LNS_ADVANCE_LINE : begin
// inc(state.line, ReadLEB128()); negative values are allowed
// but those may generate a range check error
var i64 := ReadLEB128();
if i64 >= 0 then
state.line := state.line + i64
else
state.line := state.line - Abs(i64);
// WriteLog('DW_LNS_ADVANCE_LINE ('+ state.line.ToString+ ')');
end;
DW_LNS_SET_FILE : begin
state.file_id := ReadULEB128();
// WriteLog('DW_LNS_SET_FILE ('+ state.file_id.ToString+ ')');
end;
DW_LNS_SET_COLUMN : begin
state.column := ReadULEB128();
// WriteLog('DW_LNS_SET_COLUMN ('+ state.column.ToString+ ')');
end;
DW_LNS_NEGATE_STMT : begin
state.is_stmt := not state.is_stmt;
// WriteLog('DW_LNS_NEGATE_STMT ('+ state.is_stmt.ToString+ ')');
end;
DW_LNS_SET_BASIC_BLOCK : begin
state.basic_block := true;
// WriteLog('DW_LNS_SET_BASIC_BLOCK');
end;
DW_LNS_CONST_ADD_PC : begin
inc(state.address, CalculateAddressIncrement(255, header64));
// WriteLog('DW_LNS_CONST_ADD_PC ('+ IntToHex(state.address, sizeof(state.address)*2)+ ')');
end;
DW_LNS_FIXED_ADVANCE_PC : begin
inc(state.address, ReadUHalf());
// WriteLog('DW_LNS_FIXED_ADVANCE_PC ('+ IntToHex(state.address, sizeof(state.address)*2)+ ')');
end;
DW_LNS_SET_PROLOGUE_END : begin
state.prolouge_end := true;
// WriteLog('DW_LNS_SET_PROLOGUE_END');
end;
DW_LNS_SET_EPILOGUE_BEGIN : begin
state.epilouge_begin := true;
// WriteLog('DW_LNS_SET_EPILOGUE_BEGIN');
end;
DW_LNS_SET_ISA : begin
state.isa := ReadULEB128();
// WriteLog('DW_LNS_SET_ISA ('+ state.isa.ToString + ')');
end;
else begin { special opcode }
if (opcode < header64.opcode_base) then begin
// WriteLog('Unknown standard opcode $'+ IntToHex(opcode, 2)+ '; skipping');
for i := 1 to numoptable[opcode] do
SkipLEB128();
end else begin
adjusted_opcode := opcode - header64.opcode_base;
addrIncrement := CalculateAddressIncrement(opcode, header64);
inc(state.address, addrIncrement);
lineIncrement := header64.line_base + (adjusted_opcode mod header64.line_range);
if lineIncrement >= 0 then
inc(state.line, lineIncrement)
else
dec(state.line, Abs(lineIncrement));
// WriteLog('Special opcode $'+ IntToHex(opcode, 2)+ ' address increment: '+ addrIncrement.ToString+ ' new line: '+ lineIncrement.ToString);
state.basic_block := false;
state.prolouge_end := false;
state.epilouge_begin := false;
state.append_row := true;
end;
end;
end;
if (state.append_row) then begin
// WriteLog('Current state : address = '+ IntToHex(state.address, sizeof(state.address) * 2)+
//{$ifdef CPUI8086}
// DEBUG_COMMENT ' segment = ', IntToHex(state.segment, sizeof(state.segment) * 2)+
//{$endif CPUI8086}
// {DEBUG_COMMENT} ' file_id = '+ state.file_id.ToString+ ' line = '+ state.line.ToString + ' column = ' + state.column.ToString+
// {DEBUG_COMMENT} ' is_stmt = '+ state.is_stmt.ToString+ ' basic_block = '+ state.basic_block.ToString+
// {DEBUG_COMMENT} ' end_sequence = '+ state.end_sequence.ToString+ ' prolouge_end = '+ state.prolouge_end.ToString+
// {DEBUG_COMMENT} ' epilouge_begin = '+ state.epilouge_begin.ToString+ ' isa = '+ state.isa.ToString);
if (first_row) then begin
if (state.segment > segment) or
((state.segment = segment) and
(state.address > addr)) then
break;
first_row := false;
end;
{ when we have found the address we need to return the previous
line because that contains the call instruction
Note that there may not be any call instruction, because this may
be the actual instruction that crashed, and it may be on the first
line of the function }
if (state.segment > segment) or
((state.segment = segment) and
(state.address >= addr)) then
found:=true
else
begin
{ save line information }
prev_file := state.file_id;
prev_line := state.line;
end;
state.append_row := false;
if (state.end_sequence) then begin
InitStateRegisters(state, header64.default_is_stmt);
first_row := true;
end;
end;
opcode := ReadNext();
end;
if (found) then
begin
{ can happen if the crash happens on the first instruction with line info }
if prev_line = 0 then
begin
prev_line := state.line;
prev_file := state.file_id;
end;
line := prev_line;
source := string(GetFullFilename(file_names, include_directories, prev_file));
end;
end;
var
Abbrev_Offsets : array of QWord;
Abbrev_Tags : array of QWord;
Abbrev_Children : array of Byte;
Abbrev_Attrs : array of array of record attr,form : QWord; end;
procedure ReadAbbrevTable;
var
i : PtrInt;
tag,
nr,
attr,
form{,
PrevHigh} : Int64;
begin
// WriteLog('Starting to read abbrev. section at $'+IntToHex(Dwarf_Debug_Abbrev_Section_Offset+Pos,16));
repeat
nr:=ReadULEB128;
if nr=0 then
break;
if nr>high(Abbrev_Offsets) then
begin
SetLength(Abbrev_Offsets,nr+1024);
SetLength(Abbrev_Tags,nr+1024);
SetLength(Abbrev_Attrs,nr+1024);
SetLength(Abbrev_Children,nr+1024);
end;
Abbrev_Offsets[nr]:=Pos;
{ read tag }
tag:=ReadULEB128;
Abbrev_Tags[nr]:=tag;
// WriteLog('Abbrev '+nr.ToString+' at offset '+Pos.ToString +' has tag $' +IntToHex(tag,4));
{ read flag for children }
Abbrev_Children[nr]:=ReadNext;
i:=0;
{ ensure that length(Abbrev_Attrs)=0 if an entry is overwritten (not sure if this will ever happen) and
the new entry has no attributes }
Abbrev_Attrs[nr]:=nil;
repeat
attr:=ReadULEB128;
form:=ReadULEB128;
if attr<>0 then
begin
SetLength(Abbrev_Attrs[nr],i+1);
Abbrev_Attrs[nr][i].attr:=attr;
Abbrev_Attrs[nr][i].form:=form;
end;