File: fpdbgdwarfcfi.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (621 lines) | stat: -rw-r--r-- 22,578 bytes parent folder | download
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
{
 ---------------------------------------------------------------------------
 FpDbgDwarfCFI.pas - Native Freepascal debugger - Call Frame Information
 ---------------------------------------------------------------------------

 This unit contains classes to process the Dwarf Call Frame Information
 information

 ---------------------------------------------------------------------------

 @created(Wed Jun 16th WET 2022)
 @lastmod($Date$)
 @author(Joost van der Sluis <joost@@cnoc.nl>)

 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code 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     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************
}
unit FpDbgDwarfCFI;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  // LazUtils
  Maps,
  {$ifdef FORCE_LAZLOGGER_DUMMY} LazLoggerDummy {$else} LazLoggerBase {$endif},
  // DebuggerIntf
  DbgIntfBaseTypes,
  // FpDebug
  FpDbgCommon,
  FpDbgUtil,
  FpDbgDwarfConst;

type
  PDwarfCIEEntryHeader32 = ^TDwarfCIEEntryHeader32;
  TDwarfCIEEntryHeader32 = record
    Length: LongWord;
    CIEId: LongWord;
    Version: Byte;
    Augmentation: array[0..1023] of char;
  end;

  PDwarfCIEEntryHeader64 = ^TDwarfCIEEntryHeader64;
  TDwarfCIEEntryHeader64 = record
    Signature: LongWord;
    Length: QWord;
    CIEId: QWord;
    Version: Byte;
    Augmentation: array[0..1023] of char;
  end;

  PDwarfFDEEntryHeader32 = ^TDwarfFDEEntryHeader32;
  TDwarfFDEEntryHeader32 = record
    Length: LongWord;
    CIEPointer: LongWord;
    InitialLocation: LongWord;
    Initialrange: LongWord;
  end;

  PDwarfFDEEntryHeader64 = ^TDwarfFDEEntryHeader64;
  TDwarfFDEEntryHeader64 = record
    Signature: LongWord;
    Length: QWord;
    CIEPointer: QWord;
    InitialLocation: PQWord;
  end;

  TDwarfCallFrameInformationInstructions = array of Byte;

  { TDwarfCIE }

  TDwarfCIE = class
  private
    FAddressSize: Byte;
    FAugmentation: string;
    FCodeAlignmentFactor: QWord;
    FDataAlignmentFactor: Int64;
    FInitialInstructions: TDwarfCallFrameInformationInstructions;
    FReturnAddressRegister: QWord;
    FSegmentSize: Byte;
    FVersion: Byte;
  public
    constructor Create(AVersion: Byte; AnAugmentation: string);
    property Version: Byte read FVersion;
    property Augmentation: string read FAugmentation;
    property AddressSize: Byte read FAddressSize write FAddressSize;
    property SegmentSize: Byte read FSegmentSize write FSegmentSize;
    property CodeAlignmentFactor: QWord read FCodeAlignmentFactor write FCodeAlignmentFactor;
    property DataAlignmentFactor: Int64 read FDataAlignmentFactor write FDataAlignmentFactor;
    property ReturnAddressRegister: QWord read FReturnAddressRegister write FReturnAddressRegister;
    property InitialInstructions: TDwarfCallFrameInformationInstructions read FInitialInstructions write FInitialInstructions;
  end;

  TDwarfCallFrameInformationRegisterRule = (
    cfiUndefined,
    cfiSameValue,
    cfiOffset,
    cfiValOffset,
    cfiRegister,
    cfiExpression,
    cfiValExpression,
    cfiArchitectural
  );
  TDwarfCallFrameInformationCFARule = (
    cfaUndefined,
    cfaRegister,
    cfaExpression
  );

  { TDwarfCallFrameInformationRule }

  TDwarfCallFrameInformationRule = record
    Offset: Int64;
    &Register: QWord;
    // Dwarf-expressions are unsupported
    // Expression: TDwarfLocationExpression;
    case byte of
      0: (RegisterRule: TDwarfCallFrameInformationRegisterRule);
      1: (CFARule: TDwarfCallFrameInformationCFARule);
  end;
  TDwarfCallFrameInformationRow = record
    // The DWARF CFI specification defines the Location and CFARule as the
    // first two columns of the array.
    // By keeping them separate, the code is much cleaner though.
    Location: TDBGPtr;
    CFARule: TDwarfCallFrameInformationRule;
    RegisterArray: array of TDwarfCallFrameInformationRule;
  end;

  { TDwarfFDE }

  TDwarfFDE = class
  private
    FAddressRange: QWord;
    FCIEPointer: QWord;
    FInitialLocation: TDBGPtr;
    FInstructions: TDwarfCallFrameInformationInstructions;
    FSegmentSelector: TDBGPtr;
  public
    constructor Create(ACIEPointer: QWord; AnInitialLocation, ASegmentSelector: TDBGPtr; AnAddressRange: QWord);
    property CIEPointer: QWord read FCIEPointer;
    property InitialLocation: TDBGPtr read FInitialLocation;
    property SegmentSelector: TDBGPtr read FSegmentSelector;
    property AddressRange: QWord read FAddressRange;
    property Instructions: TDwarfCallFrameInformationInstructions read FInstructions write FInstructions;
  end;

  { TDwarfCallFrameInformation }

  TDwarfCallFrameInformation = class
  private
    FFDEMap: TMap;
    FCIEMap: TMap;
    FInitialInstructionsCache: TDwarfCallFrameInformationRow;
    FRowStack: array of TDwarfCallFrameInformationRow;
  protected
    function SetCallFrameInformationRegisterCell(var InformationRow: TDwarfCallFrameInformationRow; &Register: QWord; Rule: TDwarfCallFrameInformationRegisterRule; Offset: Int64; SourceRegister: Byte): Boolean;
    function SetCallFrameInformationCFACell(var InformationRow: TDwarfCallFrameInformationRow; Rule: TDwarfCallFrameInformationCFARule; &Register: Byte; Offset: Int64): Boolean;
    // Process Call Frame Instructions on a given (CFI) row. Return True if succesfull.
    // When False returned the InformationRow can not be trusted.
    function ProcessInstructions(const CIE: TDwarfCIE; var InformationRow: TDwarfCallFrameInformationRow; const Instructions: TDwarfCallFrameInformationInstructions; const InitialAddress, SearchAddress: TDBGPtr): Boolean;
    procedure InitializeABIRules(TargetInfo: TTargetDescriptor; var Row: TDwarfCallFrameInformationRow);
    function CloneRow(const SourceRow: TDwarfCallFrameInformationRow): TDwarfCallFrameInformationRow;
  public
    constructor Create;
    destructor Destroy; override;
    procedure AddCIE(AnOffset: QWord; ACIE: TDwarfCIE);
    procedure AddFDE(AFDE: TDwarfFDE);
    function FindFDEForAddress(AnAddress: TDBGPtr): TDwarfFDE;
    function FindCIEForOffset(AnOffset: QWord): TDwarfCIE;
    function GetRow(TargetInfo: TTargetDescriptor; AnAddress: TDBGPtr; out CIE: TDwarfCIE; out Row: TDwarfCallFrameInformationRow): Boolean;
  end;

implementation

var
  FPDBG_DWARF_CFI_WARNINGS: PLazLoggerLogGroup;

type

  { TCFIMap }

  TCFIMap = class(TMap)
  protected
    procedure ReleaseData(ADataPtr: Pointer); override;
  end;

{ TCFIMap }

procedure TCFIMap.ReleaseData(ADataPtr: Pointer);
begin
  TObject(ADataPtr^).Free;
  inherited ReleaseData(ADataPtr);
end;

{ TDwarfCallFrameInformation }

function TDwarfCallFrameInformation.SetCallFrameInformationRegisterCell(var InformationRow: TDwarfCallFrameInformationRow; &Register: QWord; Rule: TDwarfCallFrameInformationRegisterRule; Offset: Int64; SourceRegister: Byte): Boolean;
var
  Row: Byte;
begin
  Result := True;
  if &Register > High(Byte) then
    begin
    DebugLn(FPDBG_DWARF_CFI_WARNINGS, ['Call frame instruction register-number out of bounds']);
    Exit(False);
    end;
  Row := &Register;

  if Length(InformationRow.RegisterArray) < Row+1 then
    SetLength(InformationRow.RegisterArray, Row+1);
  InformationRow.RegisterArray[Row].RegisterRule := Rule;
  InformationRow.RegisterArray[Row].&Register := SourceRegister;
  InformationRow.RegisterArray[Row].Offset := Offset;
end;

function TDwarfCallFrameInformation.SetCallFrameInformationCFACell(var InformationRow: TDwarfCallFrameInformationRow; Rule: TDwarfCallFrameInformationCFARule; &Register: Byte; Offset: Int64): Boolean;
begin
  Result := True;
  InformationRow.CFARule.CFARule := Rule;
  InformationRow.CFARule.&Register := &Register;
  InformationRow.CFARule.Offset := Offset;
end;

function TDwarfCallFrameInformation.ProcessInstructions(const CIE: TDwarfCIE; var InformationRow: TDwarfCallFrameInformationRow; const Instructions: TDwarfCallFrameInformationInstructions; const InitialAddress, SearchAddress: TDBGPtr): Boolean;
var
  p: PByte;
  pw: PWord absolute p;
  pl: PLongWord absolute p;
  Instruction: Byte;
  CurrentLocation: TDBGPtr;
  uparam1, uparam2: QWord;
  sparam: Int64;
begin
  if Length(Instructions) = 0 then
    begin
    Result := True;
    Exit;
    end;
  Result := False;
  p := @Instructions[0];
  CurrentLocation:=InitialAddress;
  while p < Length(Instructions)+@Instructions[0] do
    begin
    Instruction := p^;
    Inc(p);
    case Instruction of
      DW_CFA_nop:
        begin
        end;
      DW_CFA_set_loc:
        begin
        if not cie.AddressSize in [1, 2, 4, 8] then
          DebugLn(FPDBG_DWARF_CFI_WARNINGS, ['Unsupported address size'])
        else
          CurrentLocation := ReadUnsignedFromExpression(p, CIE.AddressSize);
        end;
      DW_CFA_advance_loc1:
        begin
        Inc(CurrentLocation, p^);
        if CurrentLocation>SearchAddress then
          Exit(True);
        Inc(p);
        end;
      DW_CFA_advance_loc2:
        begin
        Inc(CurrentLocation, pw^);
        if CurrentLocation>SearchAddress then
          Exit(True);
        Inc(p,2);
        end;
      DW_CFA_advance_loc4:
        begin
        Inc(CurrentLocation, pl^);
        if CurrentLocation>SearchAddress then
          Exit(True);
        Inc(p,4);
        end;
      DW_CFA_offset_extended:
        begin
        uparam1 := ULEB128toOrdinal(p); // Register number
        uparam2 := ULEB128toOrdinal(p); // Factored offset
        if not SetCallFrameInformationRegisterCell(InformationRow, uparam1, cfiOffset, uparam2*CIE.DataAlignmentFactor, 0) then
          Exit;
        end;
      DW_CFA_offset_extended_sf:
        begin
        uparam1 := ULEB128toOrdinal(p); // Register number
        sparam := SLEB128toOrdinal(p); // Factored offset
        if not SetCallFrameInformationRegisterCell(InformationRow, uparam1, cfiOffset, sparam*CIE.DataAlignmentFactor, 0) then
          Exit;
        end;
      DW_CFA_restore:
        begin
        uparam1 := ULEB128toOrdinal(p); // Register number
        if not SetCallFrameInformationRegisterCell(InformationRow, uparam1, FInitialInstructionsCache.RegisterArray[uparam1].RegisterRule, FInitialInstructionsCache.RegisterArray[uparam1].Offset, FInitialInstructionsCache.RegisterArray[uparam1].&Register) then
          Exit;
        end;
      DW_CFA_val_offset:
        begin
        uparam1 := ULEB128toOrdinal(p); // Register number
        uparam2 := ULEB128toOrdinal(p); // Factored offset
        if not SetCallFrameInformationRegisterCell(InformationRow, uparam1, cfiValOffset, uparam2*CIE.DataAlignmentFactor, 0) then
          Exit;
        end;
      DW_CFA_val_offset_sf:
        begin
        uparam1 := ULEB128toOrdinal(p); // Register number
        sparam := SLEB128toOrdinal(p); // Factored offset
        if not SetCallFrameInformationRegisterCell(InformationRow, uparam1, cfiValOffset, sparam*CIE.DataAlignmentFactor, 0) then
          Exit;
        end;
      DW_CFA_undefined:
        begin
        uparam1 := ULEB128toOrdinal(p); // Register number
        if not SetCallFrameInformationRegisterCell(InformationRow, uparam1, cfiUndefined, 0, 0) then
          Exit;
        end;
      DW_CFA_same_value:
        begin
        uparam1 := ULEB128toOrdinal(p); // Register number
        if not SetCallFrameInformationRegisterCell(InformationRow, uparam1, cfiSameValue, 0, 0) then
          Exit;
        end;
      DW_CFA_register:
        begin
        uparam1 := ULEB128toOrdinal(p); // Register number
        uparam2 := ULEB128toOrdinal(p); // Register number
        if not SetCallFrameInformationRegisterCell(InformationRow, uparam1, cfiRegister, 0, uparam2) then
          Exit;
        end;
      DW_CFA_remember_state:
        begin
        SetLength(FRowStack, Length(FRowStack) + 1);
        FRowStack[High(FRowStack)] := CloneRow(InformationRow);
        end;
      DW_CFA_restore_state:
        begin
        InformationRow := FRowStack[High(FRowStack)];
        SetLength(FRowStack, Length(FRowStack) - 1);
        end;
      DW_CFA_def_cfa:
        begin
        uparam1 := ULEB128toOrdinal(p); // Register number
        uparam2 := ULEB128toOrdinal(p); // Non-factored offset
        if not SetCallFrameInformationCFACell(InformationRow, cfaRegister, uparam1, uparam2) then
          Exit;
        end;
      DW_CFA_def_cfa_sf:
        begin
        uparam1 := ULEB128toOrdinal(p); // Register number
        sparam := SLEB128toOrdinal(p); // Factored offset
        if not SetCallFrameInformationCFACell(InformationRow, cfaRegister, uparam1, sparam*CIE.DataAlignmentFactor) then
          Exit;
        end;
      DW_CFA_def_cfa_register:
        begin
        uparam1 := ULEB128toOrdinal(p); // Register
        if InformationRow.CFARule.CFARule = cfaRegister then
          begin
          if not SetCallFrameInformationCFACell(InformationRow, cfaRegister, uparam1, InformationRow.CFARule.Offset) then
            Exit;
          end
        else
          DebugLn(FPDBG_DWARF_CFI_WARNINGS, ['Invalid DW_CFA_def_cfa_register rule']);
        end;
      DW_CFA_def_cfa_offset:
        begin
        uparam1 := ULEB128toOrdinal(p); // Non-factored offset
        if InformationRow.CFARule.CFARule = cfaRegister then
          begin
          if not SetCallFrameInformationCFACell(InformationRow, cfaRegister, InformationRow.CFARule.&Register, uparam1) then
            Exit;
          end
        else
          DebugLn(FPDBG_DWARF_CFI_WARNINGS, ['Invalid DW_CFA_def_cfa_offset rule']);
        end;
      DW_CFA_def_cfa_offset_sf:
        begin
        sparam := SLEB128toOrdinal(p); // Factored offset
        if InformationRow.CFARule.CFARule = cfaRegister then
          begin
          if not SetCallFrameInformationCFACell(InformationRow, cfaRegister, InformationRow.CFARule.&Register, sparam*CIE.DataAlignmentFactor) then
            Exit;
          end
        else
          DebugLn(FPDBG_DWARF_CFI_WARNINGS, ['Invalid DW_CFA_def_cfa_offset_sf rule']);
        end;
      else
        begin
        case Instruction and $c0 of
          DW_CFA_advance_loc:
            begin
            Inc(CurrentLocation, (Instruction and $3f)*CIE.CodeAlignmentFactor);
            if CurrentLocation>SearchAddress then
              Exit(True);
            end;
          DW_CFA_offset:
            begin
            uparam1 := Instruction and $3f; // Register number
            uparam2 := ULEB128toOrdinal(p); // Factored offset
            if not SetCallFrameInformationRegisterCell(InformationRow, uparam1, cfiOffset, uparam2*CIE.DataAlignmentFactor, 0) then
              Exit;
            end;
          DW_CFA_restore:
            begin
            uparam1 := Instruction and $3f; // Register number
            if not SetCallFrameInformationRegisterCell(InformationRow, uparam1, FInitialInstructionsCache.RegisterArray[uparam1].RegisterRule, FInitialInstructionsCache.RegisterArray[uparam1].Offset, FInitialInstructionsCache.RegisterArray[uparam1].&Register) then
              Exit;
            end
        else
          DebugLn(FPDBG_DWARF_CFI_WARNINGS, ['Unsupported call frame instruction: ', Instruction]);
          Exit;
        end;
        end;
    end;
    end;
  // This function only proceeded if we reach this point. If the handling is aborted
  // for any reason, the result is unreliable and can not be used.
  Result := True;
end;

procedure TDwarfCallFrameInformation.InitializeABIRules(TargetInfo: TTargetDescriptor; var Row: TDwarfCallFrameInformationRow);
begin
  if TargetInfo.machineType=mtX86_64 then
    begin
    // According to the x86_64 ABI the CFA (call frame address) is defined as
    // "the value of %rsp at the call site in the previous frame"
    // Register 7 is %rsp
    SetCallFrameInformationRegisterCell(Row, 7, cfiValOffset, 0, 0);

    // From the x86_64 ABI:
    // "Registers %rbp, %rbx and %r12 through %r15 “belong” to the calling
    // function and the called function is required to preserve their values."
    // Register 3 is %rbx and 6 is %rbp
    SetCallFrameInformationRegisterCell(Row, 3, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 6, cfiSameValue, 0, 0);

    SetCallFrameInformationRegisterCell(Row, 12, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 13, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 14, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 15, cfiSameValue, 0, 0);
    end
  else if TargetInfo.machineType=mt386 then
    begin
    SetCallFrameInformationRegisterCell(Row, 4, cfiValOffset, 0, 0);

    // The i386 ABI is not as clear as the x86_64 ABI. These are based on
    // some educated guesses:
    SetCallFrameInformationRegisterCell(Row, 3, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 5, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 6, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 7, cfiSameValue, 0, 0);
    end
  else if TargetInfo.machineType=mtAVR8 then
    begin
    // Registers r2..r17, r28, r29 are call saved
    SetCallFrameInformationRegisterCell(Row, 2, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 3, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 4, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 5, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 6, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 7, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 8, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 9, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 10, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 11, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 12, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 13, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 14, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 15, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 16, cfiSameValue, 0, 0);
    SetCallFrameInformationRegisterCell(Row, 17, cfiSameValue, 0, 0);
    end;
end;

function TDwarfCallFrameInformation.CloneRow(const SourceRow: TDwarfCallFrameInformationRow): TDwarfCallFrameInformationRow;
begin
  Result := SourceRow;
  // Create a deep-copy of the array.
  Result.RegisterArray := copy(SourceRow.RegisterArray, 0, Length(SourceRow.RegisterArray));
end;

constructor TDwarfCallFrameInformation.Create;
begin
  FFDEMap := TCFIMap.Create(itu8, SizeOf(Pointer));
  FCIEMap := TCFIMap.Create(itu8, SizeOf(Pointer));
end;

destructor TDwarfCallFrameInformation.Destroy;
begin
  FFDEMap.Free;
  FCIEMap.Free;
  inherited Destroy;
end;

procedure TDwarfCallFrameInformation.AddCIE(AnOffset: QWord; ACIE: TDwarfCIE);
begin
  FCIEMap.Add(AnOffset, ACIE);
end;

procedure TDwarfCallFrameInformation.AddFDE(AFDE: TDwarfFDE);
begin
  FFDEMap.Add(AFDE.InitialLocation, AFDE);
end;

function TDwarfCallFrameInformation.FindFDEForAddress(AnAddress: TDBGPtr): TDwarfFDE;
var
  Iter: TLockedMapIterator;
  FDE: TDwarfFDE;
begin
  Result := nil;

  Iter := TLockedMapIterator.Create(FFDEMap);
  try
    if not Iter.Locate(AnAddress) then
      begin
      if not Iter.BOM then
        Iter.Previous;
      end;

    if not Iter.BOM then
      begin
      // iter is at the closest defined address before AAddress
      FDE := TDwarfFDE(Iter.DataPtr^);

      if AnAddress <= FDE.InitialLocation + FDE.AddressRange then
        begin
        Result := FDE;
        end;
      end;
  finally
    Iter.Free;
  end;
end;

function TDwarfCallFrameInformation.FindCIEForOffset(AnOffset: QWord): TDwarfCIE;
var
  Iter: TLockedMapIterator;
begin
  Result := nil;

  Iter := TLockedMapIterator.Create(FCIEMap);
  try
    if not Iter.Locate(AnOffset) then
      begin
      if not Iter.BOM then
        Iter.Previous;
      end;

    if not Iter.BOM then
      begin
      // iter is at the closest defined address before AAddress
      Result := TDwarfCIE(Iter.DataPtr^);
      end;
  finally
    Iter.Free;
  end;
end;

function TDwarfCallFrameInformation.GetRow(TargetInfo: TTargetDescriptor; AnAddress: TDBGPtr; out CIE: TDwarfCIE; out Row: TDwarfCallFrameInformationRow): Boolean;
var
  FDE: TDwarfFDE;
begin
  Result := False;
  FRowStack := [];
  Row := Default(TDwarfCallFrameInformationRow);
  FDE := FindFDEForAddress(AnAddress);
  if Assigned(FDE) then
    begin
    InitializeABIRules(TargetInfo, Row);
    CIE := FindCIEForOffset(FDE.CIEPointer);
    if ProcessInstructions(CIE, Row, CIE.InitialInstructions, FDE.InitialLocation, AnAddress) then
      begin
      FInitialInstructionsCache := CloneRow(Row);
      Result := ProcessInstructions(CIE, Row, FDE.Instructions, FDE.InitialLocation, AnAddress);
      end;
    end;
end;

{ TDwarfFDE }

constructor TDwarfFDE.Create(ACIEPointer: QWord; AnInitialLocation, ASegmentSelector: TDBGPtr; AnAddressRange: QWord);
begin
  FCIEPointer := ACIEPointer;
  FInitialLocation := AnInitialLocation;
  FSegmentSelector := ASegmentSelector;
  FAddressRange := AnAddressRange;
end;

{ TDwarfCIE }

constructor TDwarfCIE.Create(AVersion: Byte; AnAugmentation: string);
begin
  FVersion := AVersion;
  FAugmentation := AnAugmentation;
end;

initialization
  FPDBG_DWARF_CFI_WARNINGS  := DebugLogger.FindOrRegisterLogGroup('FPDBG_DWARF_CFI_WARNINGS' {$IFDEF FPDBG_DWARF_CFI_WARNINGS} , True {$ENDIF} );
end.