File: fpimgreadermacho.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (809 lines) | stat: -rw-r--r-- 23,669 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
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
unit FpImgReaderMacho;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, contnrs,
  macho, FpImgReaderMachoFile, FpImgReaderBase, LazLoggerBase,
  DbgIntfBaseTypes,
  lazfglhash,
  fpDbgSymTable, FpDbgUtil;

type

  { TDbgMachoDataSource }

  TDbgMachoDataSource = class(TDbgImageReader)
  private
    fSource     : TDbgFileLoader;
    FSections: TStringList;
    fSubFiles: TStringList;
    fAddressMapList: TDbgAddressMapList;
    fOwnSource  : Boolean;
    fFile       : TMachoFile;
    hasSymTable : Boolean;
    StabsCmd    : symtab_command;
    fileRead    : Boolean;
    function GetSectionInfo(const SectionName: AnsiString; var Size: int64): Boolean;
    function GetSectionData(const SectionName: AnsiString; Offset, {%H-}Size: Int64; var Buf: array of byte): Int64;
    procedure ParseMainAppleDwarfDataMap;
    procedure ParseSubAppleDwarfDataMap(ADebugMap: TObject);
  protected
    procedure ReadFile;
    function GetSubFiles: TStrings; override;
    function GetAddressMapList: TDbgAddressMapList; override;
    function GetSymTableSectionInfo({%H-}StabStr: Boolean; var {%H-}SectionOffset, {%H-}SectionSize: Int64): Boolean;
    function GetSectionIndex(const SectionName: AnsiString): Integer;

    function GetSection(const AName: String): PDbgImageSection; override;
  public
    class function isValid(ASource: TDbgFileLoader): Boolean; override;
    class function UserName: AnsiString; override;
    class procedure LoadSubFiles(ASubFiles: TStrings; ALoaderList: TFPObjectList);
    procedure AddSubFilesToLoaderList(ALoaderList: TObject; PrimaryLoader: TObject); override;
    procedure ParseSymbolTable(AfpSymbolInfo: TfpSymbolList); override;
  public
    constructor Create(ASource: TDbgFileLoader; ADebugMap: TObject; OwnSource: Boolean); override;
    destructor Destroy; override;
  end;

implementation

uses
  FpDbgLoader;

type
  PnlistArray = ^nlist; // ^array[0..infinite] of nlist;
  PnlistArray64 = ^nlist_64; // ^array[0..infinite] of nlist_64;

const
  // Symbol-map section name
  _symbol        = '.symbols';
  _symbolstrings = '.symbolsstrings';

type
  TDebugTableState = (dtsDir, dtsSource, dtsObjectFile, dtsProc, dtsProcLen, dtsProcEnd, dtsEnd);
  TDebugTableEntry = record
    Dir: string;
    SourceFile: string;
    ObjectFile: string;
    ObjFileAge: longint;
    Offset: TDBGPtr;
  end;

  { TAppleDwarfDebugMap }

  TAppleDwarfDebugMap = class(TObject)
  private
    FAddressMap: TDbgAddressMapList;
    FDir: string;
    FGlobalList: TDbgAddressMapHashList;
    FObjectFile: string;
    FObjFileAge: longint;
    FOffset: TDBGPtr;
    FSourceFile: string;
  public
    constructor create;
    destructor destroy; override;
    property Offset: TDBGPtr read FOffset write FOffset;
    property Dir: string read FDir write FDir;
    property ObjectFile: string read FObjectFile write FObjectFile;
    property SourceFile: string read FSourceFile write FSourceFile;
    property ObjFileAge: longint read FObjFileAge write FObjFileAge;
    // This list contains the locations for all symbols in the executable,
    // parsed from the 'stabs'-debuginfo in the executable.
    // This property is only available in the debug-map for the main
    // executable.
    property GlobalList: TDbgAddressMapHashList read FGlobalList;
    // This list maps addresses in an object-file to the addresses
    // in the main executable.
    // This property is only available in the debug-map for a specific
    // object-file.
    property AddressMap: TDbgAddressMapList read FAddressMap;
  end;

function isValidMachoStream(ASource: TDbgFileLoader): Boolean;
var
  header  : mach_header;
begin
  try
    Result := Assigned(ASource);
    if not Result then Exit;
    Result := ASource.Read(0, sizeof(header), @header) = sizeof(header);
    if not Result then Exit;
    Result := (header.magic = MH_CIGAM) or (header.magic = MH_MAGIC) or
              (header.magic = MH_CIGAM_64) or (header.magic = MH_MAGIC_64);
  except
    Result := false;
  end;
end;

function FixMachoName(const macsectionname: String): String;
begin
  if Copy(macsectionName, 1, 2) = '__' then
    Result:= '.'+Copy(macsectionName, 3, length(macsectionName)-2)
  else
    Result := macsectionname;
end;

constructor TAppleDwarfDebugMap.create;
begin
  FGlobalList := TDbgAddressMapHashList.Create;
  FAddressMap := TDbgAddressMapList.Create;
end;

destructor TAppleDwarfDebugMap.destroy;
begin
  FAddressMap.Free;
  FGlobalList.Free;
  inherited destroy;
end;

{ TDbgMachoDataSource }

class function TDbgMachoDataSource.isValid(ASource: TDbgFileLoader): Boolean;
begin
  Result := isValidMachoStream(ASource);
end;

class function TDbgMachoDataSource.UserName: AnsiString;
begin
  Result:='mach-o file';
end;

class procedure TDbgMachoDataSource.LoadSubFiles(ASubFiles: TStrings; ALoaderList: TFPObjectList);
var
  DwarfDebugMap: TAppleDwarfDebugMap;
  Loader: TDbgImageLoader;
  i: Integer;
begin
  if assigned(ASubFiles) then
    begin
    for i := 0 to ASubFiles.Count -1 do
      begin
      if (ASubFiles.Objects[i] is TAppleDwarfDebugMap) and FileExists(ASubFiles[i]) then
        begin
        DwarfDebugMap:=TAppleDwarfDebugMap(ASubFiles.Objects[i]);
        if FileAge(ASubFiles[i]) <> DwarfDebugMap.ObjFileAge then
          debugln(Format('The timestamp of the object-file "%s" does not correspond to the timestamp (%s) stored inside the executable. The debug-info in this file is not loaded.', [DwarfDebugMap.ObjectFile, DateTimeToStr(FileDatetoDateTime(DwarfDebugMap.ObjFileAge))]))
        else
          begin
          Loader := TDbgImageLoader.Create(DwarfDebugMap.ObjectFile, DwarfDebugMap);
          ALoaderList.Add(Loader);
          end;
        end
      else
        DebugLn('File with debug-info "'+DwarfDebugMap.ObjectFile+'" does not exist. This could lead to missing debug-information.');
      end;
    end;
end;

procedure TDbgMachoDataSource.ParseSymbolTable(AfpSymbolInfo: TfpSymbolList);
var
  p: PDbgImageSection;
  ps: PDbgImageSection;
  SymbolArr: PnlistArray;
  SymbolArr64: PnlistArray64;
  SymbolStr: pointer;
  SymbolType: uint8_t;
  StringOffset: uint32_t;
  SymbolValue: TDBGPtr;
  i: integer;
  SymbolCount: integer;
begin
  p := Section[_symbol];
  ps := Section[_symbolstrings];
  if assigned(p) and assigned(ps) then
  begin
    SymbolStr:=PDbgImageSectionEx(ps)^.Sect.RawData;
    if Image64Bit then
      begin
        SymbolArr64:=PDbgImageSectionEx(p)^.Sect.RawData;
        SymbolCount := PDbgImageSectionEx(p)^.Sect.Size div sizeof(nlist_64);
      end
      else
      begin
        SymbolArr:=PDbgImageSectionEx(p)^.Sect.RawData;
        SymbolCount := PDbgImageSectionEx(p)^.Sect.Size div sizeof(nlist);
      end;
    for i := 0 to SymbolCount-1 do
    begin
      if Image64Bit then
      begin
        SymbolType := SymbolArr64[i].n_type;
        StringOffset := SymbolArr64[i].n_un.n_strx;
        SymbolValue := SymbolArr64[i].n_value;
      end
      else
      begin
        SymbolType := SymbolArr[i].n_type;
        StringOffset := SymbolArr[i].n_un.n_strx;
        SymbolValue := SymbolArr[i].n_value;
      end;
      if (SymbolType and $e0)<>0 then
        // This is a stabs-entry. Ignore.
        Continue;
      if (SymbolType and $0e)=$e then
      begin
        // Section-index is ignored for now...
        AfpSymbolInfo.Add(pchar(SymbolStr+StringOffset), SymbolValue);
      end
    end;
  end;

end;

procedure TDbgMachoDataSource.ReadFile;
var
  i : Integer;
begin
  if Assigned(fFile) then fFile.Free;
  fFile:=TMachOFile.Create;
  fFile.LoadFromFile(fSource);
  for i := 0 to fFile.header.ncmds - 1 do begin
    hasSymTable := fFile.commands[i]^.cmd = LC_SYMTAB;
    if hasSymTable then begin
      StabsCmd := psymtab_command(fFile.commands[i])^;
      Break;
    end;
  end;
  SetImage64Bit((fFile.header.cputype and CPU_ARCH_ABI64)=CPU_ARCH_ABI64);
  SetUUID(fFile.UUID);
  fileRead := true;
end;

function TDbgMachoDataSource.GetSubFiles: TStrings;
begin
  if not assigned(fSubFiles) then
    begin
    fSubFiles:=TStringList.Create;
    fSubFiles.OwnsObjects:=true;
    end;
  Result:=fSubFiles;
end;

function TDbgMachoDataSource.GetAddressMapList: TDbgAddressMapList;
begin
  if not assigned(fAddressMapList) then
    begin
    fAddressMapList:=TDbgAddressMapList.Create;
    end;
  Result:=fAddressMapList;
end;

function TDbgMachoDataSource.GetSymTableSectionInfo(StabStr: Boolean;
  var SectionOffset, SectionSize: Int64): Boolean;
begin
  Result := hasSymTable;
  if not Result then Exit;
  if StabStr then begin
    SectionOffset := StabsCmd.stroff;
    SectionSize := StabsCmd.strsize;
  end else begin
    SectionOffset := StabsCmd.symoff;
    if Image64Bit then
      SectionSize := Int64(StabsCmd.nsyms * sizeof(nlist_64))
    else
      SectionSize := Int64(StabsCmd.nsyms * sizeof(nlist));
  end;
end;

function TDbgMachoDataSource.GetSectionIndex(const SectionName: AnsiString): Integer;
var
  i     : Integer;
  Name  : AnsiString;
begin
  //todo: hash-table
  for i := 0 to fFile.Sections.Count - 1 do begin
    with TMachoSection(fFile.sections[i]) do
      if is32
        then Name := FixMachoName(sec32.sectname)
        else Name := FixMachoName(sec64.sectname);
    if Name = SectionName then begin
      Result := i;
      Exit;
    end;
  end;
  Result := -1;
end;

function TDbgMachoDataSource.GetSection(const AName: String): PDbgImageSection;
var
  i: Integer;
  ex: PDbgImageSectionEx;
begin
  Result := nil;
  i := FSections.IndexOf(AName);
  if i < 0 then
    exit;
  ex := PDbgImageSectionEx(FSections.Objects[i]);
  Result := @ex^.Sect;
  if ex^.Loaded then
    exit;
  ex^.Loaded  := True;
  fSource.LoadMemory(ex^.Offs, Result^.Size, Result^.RawData);
end;

procedure TDbgMachoDataSource.AddSubFilesToLoaderList(ALoaderList: TObject;
  PrimaryLoader: TObject);
var
  PLoader: TDbgImageLoader absolute PrimaryLoader;
  LList: TDbgImageLoaderList absolute ALoaderList;
  ALoader: TDbgImageLoader;
  fname, dSYMFilename: String;
  i: SizeInt;
begin
  // JvdS: Mach-O binaries do not contain DWARF-debug info. Instead this info
  // is stored inside the .o files, and the executable contains a map (in stabs-
  // format) of all these .o files. An alternative to parsing this map and reading
  // those .o files a dSYM-bundle could be used, which could be generated
  // with dsymutil.

  // PLoader.FileName in Contents/MacOs
  ALoader:=nil;

  fname := PLoader.FileName;
  i := pos('/Contents/MacOs', fname);
  if i > 0 then delete(fname, i, Length(fname));
  dSYMFilename:=ChangeFileExt(PLoader.FileName, '.dSYM');
  dSYMFilename:=dSYMFilename+'/Contents/Resources/DWARF/'+ExtractFileName(fname); // TDbgProcess.Name

  if ExtractFileExt(dSYMFilename)='.app' then
    dSYMFilename := ChangeFileExt(dSYMFilename,'');

  if FileExists(dSYMFilename) then
    begin
    ALoader := TDbgImageLoader.Create(dSYMFilename);
    if GUIDToString(ALoader.UUID)<>GUIDToString(PLoader.UUID) then
      begin
      Log('The unique UUID''s of the executable and the dSYM bundle with debug-info ('+dSYMFilename+') do not match.');
      FreeAndNil(ALoader);
      end
    else
      begin
      log('Load debug-info from dSYM bundle ('+dSYMFilename+').');
      LList.Add(ALoader);
      end;
    end;

  if not assigned(ALoader) then
    begin
    log('Read debug-info from separate object files.');
    TDbgMachoDataSource.LoadSubFiles(PLoader.SubFiles, LList);
    end;
end;

constructor TDbgMachoDataSource.Create(ASource: TDbgFileLoader; ADebugMap: TObject; OwnSource: Boolean);
const
  SymbolsSectionName : array [Boolean] of AnsiString = (_symbol, _symbolstrings);
var
  p: PDbgImageSectionEx;
  fs: TMachOsection;
  i: Integer;
  Name: String;
  soffset: int64;
  ssize: int64;
begin
  fSource := ASource;
  fOwnSource := OwnSource;

  ReadFile;

  FSections := TStringList.Create;
  FSections.Sorted := True;
  FSections.Duplicates := dupAccept;
  FSections.CaseSensitive := False;

  for i := 0 to fFile.sections.Count - 1 do begin
    fs := TMachoSection(fFile.sections[i]);
    New(p);

    if fs.is32 then begin
      Name := FixMachoName(fs.sec32.sectname);
      P^.Offs := fs.sec32.offset;
      p^.Sect.Size := fs.sec32.size;
    end
    else begin
      Name := FixMachoName(fs.sec64.sectname);
      P^.Offs := fs.sec64.offset;
      p^.Sect.Size := fs.sec64.size;
    end;
DebugLn(Name);

    p^.Sect.VirtualAddress := 0; // Todo?
    p^.Loaded := False;
    FSections.AddObject(Name, TObject(p));
  end;

  if GetSymTableSectionInfo(false, soffset, ssize) then begin
    new(p);
    p^.Offs:=soffset;
    p^.Sect.Size:=ssize;
    p^.Sect.VirtualAddress:=0;
    p^.Loaded:=false;
    FSections.AddObject(SymbolsSectionName[false], TObject(p));
  end;

  if GetSymTableSectionInfo(true, soffset, ssize) then begin
    new(p);
    p^.Offs:=soffset;
    p^.Sect.Size:=ssize;
    p^.Sect.VirtualAddress:=0;
    p^.Loaded:=false;
    FSections.AddObject(SymbolsSectionName[true], TObject(p));
  end;

  if assigned(ADebugMap) then
    ParseSubAppleDwarfDataMap(ADebugMap)
  else
    ParseMainAppleDwarfDataMap;

  inherited Create(ASource, ADebugMap, OwnSource);
end;

destructor TDbgMachoDataSource.Destroy;
begin
  if assigned(fSubFiles) then
    fSubFiles.Free;
  if assigned(fAddressMapList) then
    fAddressMapList.Free;
  if Assigned(fFile) then fFile.Free;
  if fOwnSource then fSource.Free;
  while FSections.Count > 0 do begin
    Freemem(FSections.Objects[0]);
    FSections.Delete(0);
  end;
  FreeAndNil(FSections);
  inherited Destroy;
end;

{function TDbgMachoDataSource.SectionsCount: Integer;
begin
  if not Assigned(fFile) then ReadFile;
  Result := fFile.Sections.Count;
  if isStabs then inc(Result, 2);
end;

function TDbgMachoDataSource.GetSection(Index: Integer; var Name: AnsiString; var Size: Int64): Boolean;
var
  cnt   : Integer;
  sstr  : Boolean;
const
  StabSectionName : array [Boolean] of AnsiString = (_stab, _stabstr);
begin
  if not Assigned(fFile) then ReadFile;
  cnt := fFile.Sections.Count;
  if isStabs then inc(cnt, 2);
  Result := (Index >= 0) and (Index < cnt);
  if not Result then Exit;

  if Index < fFile.Sections.Count then begin
    with TMachoSection(fFile.sections[index]) do
      if is32 then begin
        Name := FixMachoName(sec32.sectname);
        Size := sec32.size;
      end else begin
        Name := FixMachoName(sec64.sectname);
        Size := sec64.size;
      end;
  end else begin
    sstr := Index = cnt - 1;
    Name := StabSectionName[sstr];
    Result := GetStabSectionInfo(sstr, Size);
  end;
end;

function TDbgMachoDataSource.GetSectionData(index: Integer; outStream: TStream): Boolean;
var
  ofs : Int64;
  sz  : Int64;
begin
  //todo: method will be removed
  if not Assigned(outStream) then begin
    Result := false;
    Exit;
  end;
  if not Assigned(fFile) then ReadFile;
  Result := (Index >= 0) and (Index < fFile.Sections.Count);
  if not Result then Exit;

  with TMachOsection(fFile.sections[index]) do begin
    if is32 then begin
      ofs := sec32.offset;
      sz := sec32.size;
    end else begin
      ofs := sec64.offset;
      sz := sec64.size;
    end;
  end;
  if ofs > 0 then begin
    fSource.Position:=ofs;
    outStream.CopyFrom(fSource, sz);
  end;
end;}

function TDbgMachoDataSource.GetSectionInfo(const SectionName: AnsiString; var Size: int64): Boolean;
var
  idx     : integer;
  symtablestr : Boolean;
  dummy   : int64;
begin
  if not fileRead then ReadFile;

  symtablestr := (SectionName = _symbolstrings);
  if symtablestr or (SectionName = _symbol) then
    Result := GetSymTableSectionInfo(symtablestr, dummy, Size)
  else begin
    idx := GetSectionIndex(SectionName);
    Result := idx >= 0;
    if not Result then Exit;

    with TMachOsection(fFile.sections[idx]) do
      if is32
        then Size := sec32.size
        else Size := sec64.size;
  end;
end;

function TDbgMachoDataSource.GetSectionData(const SectionName: AnsiString; Offset, Size: Int64; var Buf: array of byte): Int64;
var
  idx     : Integer;
  sofs    : int64;
  ssize   : int64;
  symstr : Boolean;
  sz      : Int64;
  s       : TMachOsection;
begin
  if not fileRead then ReadFile;

  Result := 0;
  symstr := SectionName = _symbolstrings;
  if symstr or (SectionName = _symbol)  then begin
    if not GetSymTableSectionInfo(symstr, sofs, ssize) then
      Exit;
  end else begin
    idx := GetSectionIndex(SectionName);
    s := TMachOsection(fFile.sections[idx]);
    if s.is32 then begin
      ssize := s.sec32.size;
      sofs := s.sec32.offset;
    end else begin
      sofs := s.sec64.offset;
      ssize := s.sec64.size;
    end;
  end;

  sz := ssize - Offset;
  if sz < 0 then Exit;

  //fSource.Position := sofs + Offset;
  //Result := fSource.Read(Buf[0], sz);
  Result := fSource.Read(sofs + Offset, sz, @Buf[0]);
end;

procedure TDbgMachoDataSource.ParseMainAppleDwarfDataMap;
var
  p: PDbgImageSection;
  ps: PDbgImageSection;
  SymbolArr: PnlistArray;
  SymbolArr64: PnlistArray64;
  SymbolStr: pointer;
  i: integer;
  SymbolCount: integer;
  SymbolType, SymbolSect: uint8_t;
  StringOffset: uint32_t;
  SymbolValue: QWord;
  State: TDebugTableState;
  AddressMap: TDbgAddressMap;
  ProcName: string;
  DwarfDebugMap: TAppleDwarfDebugMap;
  FullDwarfDebugMap: TDbgAddressMapPointerHashList;
  ind: THTCustomNode;
begin
  DwarfDebugMap:=nil;
  FullDwarfDebugMap := TDbgAddressMapPointerHashList.Create;
  p := Section[_symbol];
  ps := Section[_symbolstrings];
  if assigned(p) and assigned(ps) then
  begin
    SymbolStr:=PDbgImageSectionEx(ps)^.Sect.RawData;
    if Image64Bit then
    begin
      SymbolArr64:=PDbgImageSectionEx(p)^.Sect.RawData;
      SymbolCount := PDbgImageSectionEx(p)^.Sect.Size div sizeof(nlist_64);
    end
    else
    begin
      SymbolArr:=PDbgImageSectionEx(p)^.Sect.RawData;
      SymbolCount := PDbgImageSectionEx(p)^.Sect.Size div sizeof(nlist);
    end;
    state := dtsEnd;
    for i := 0 to SymbolCount-1 do
    begin
      if Image64Bit then
      begin
        SymbolType := SymbolArr64[i].n_type;
        StringOffset := SymbolArr64[i].n_un.n_strx;
        SymbolValue := SymbolArr64[i].n_value;
        SymbolSect := SymbolArr64[i].n_sect;
      end
      else
      begin
        SymbolType := SymbolArr[i].n_type;
        StringOffset := SymbolArr[i].n_un.n_strx;
        SymbolValue := SymbolArr[i].n_value;
        SymbolSect := SymbolArr[i].n_sect;
      end;
      case state of
        dtsEnd:
          begin
            if SymbolType = N_SO then
            begin
              if assigned(DwarfDebugMap) then
                DwarfDebugMap.Free;
              DwarfDebugMap := TAppleDwarfDebugMap.Create;
              DwarfDebugMap.Dir := pchar(SymbolStr+StringOffset);
              state := dtsDir;
            end
            else if (SymbolType in [14,15]) then begin
              ind := FullDwarfDebugMap.Find(pchar(SymbolStr+StringOffset));
              if (ind <> nil) and (FullDwarfDebugMap.ItemPointerFromNode(ind)^.NewAddr = 0) then begin
                FullDwarfDebugMap.ItemPointerFromNode(ind)^.NewAddr := SymbolValue;
              end;
            end;
          end;
        dtsDir:
          begin
            if SymbolType = N_SO then
            begin
              DwarfDebugMap.SourceFile:=pchar(SymbolStr+StringOffset);
              inc(state);
            end
            else
              state := dtsEnd;
          end;
        dtsSource:
          begin
            if SymbolType = N_OSO then
            begin
              DwarfDebugMap.ObjectFile:=pchar(SymbolStr+StringOffset);
              DwarfDebugMap.ObjFileAge:=SymbolValue;
              inc(state);
            end;
          end;
        dtsObjectFile:
          begin
            if (SymbolType = N_BNSYM) then
            begin
              inc(state);
            end
            else if (SymbolType = N_STSYM) or (SymbolType = N_GSYM) then
            begin
              AddressMap.NewAddr:=SymbolValue;
              AddressMap.OrgAddr:=0;
              AddressMap.Length:=0;
              DwarfDebugMap.GlobalList.Add(pchar(SymbolStr+StringOffset), AddressMap);
              if (SymbolType = N_GSYM) and (SymbolValue = 0) then begin
                ind := DwarfDebugMap.GlobalList.Find(pchar(SymbolStr+StringOffset));
                FullDwarfDebugMap.Add(pchar(SymbolStr+StringOffset),
                  DwarfDebugMap.GlobalList.ItemPointerFromNode(ind));
              end;
            end
            else if (SymbolType = N_SO) and (SymbolSect=1) then
            begin
              state := dtsEnd;
              SubFiles.AddObject(DwarfDebugMap.ObjectFile, DwarfDebugMap);
              DwarfDebugMap:=nil;
            end;
          end;
        dtsProc:
          begin
            if (SymbolType = N_FUN) and (SymbolSect=1) then
            begin
              AddressMap.NewAddr:=SymbolValue;
              ProcName:=pchar(SymbolStr+StringOffset);
              inc(state);
            end;
          end;
        dtsProcLen:
          begin
            if (SymbolType = N_FUN) and (SymbolSect=0) then
            begin
              AddressMap.Length:=SymbolValue;
              inc(state);
            end;
          end;
        dtsProcEnd:
          begin
            if (SymbolType = N_ENSYM) and (SymbolSect=1) then
            begin
              DwarfDebugMap.GlobalList.Add(ProcName, AddressMap);
              state := dtsObjectFile;
            end;
          end;
      end;
    end;
  end;
  FullDwarfDebugMap.Free;
end;

procedure TDbgMachoDataSource.ParseSubAppleDwarfDataMap(ADebugMap: TObject);
var
  p: PDbgImageSection;
  ps: PDbgImageSection;
  SymbolArr: PnlistArray;
  SymbolArr64: PnlistArray64;
  SymbolStr: pointer;
  SymbolType: uint8_t;
  StringOffset: uint32_t;
  SymbolValue: QWord;
  i: integer;
  SymbolCount: integer;
  MainDwarfDebugMap: TAppleDwarfDebugMap;
  ind: THTCustomNode;
  AddressMap: TDbgAddressMap;
  s: string;
begin
  MainDwarfDebugMap:=TAppleDwarfDebugMap(ADebugMap);
  p := Section[_symbol];
  ps := Section[_symbolstrings];
  if assigned(p) and assigned(ps) then
  begin
    SymbolStr:=PDbgImageSectionEx(ps)^.Sect.RawData;
    if Image64Bit then
    begin
      SymbolArr64:=PDbgImageSectionEx(p)^.Sect.RawData;
      SymbolCount := PDbgImageSectionEx(p)^.Sect.Size div sizeof(nlist_64);
    end
    else
    begin
      SymbolArr:=PDbgImageSectionEx(p)^.Sect.RawData;
      SymbolCount := PDbgImageSectionEx(p)^.Sect.Size div sizeof(nlist);
    end;
    for i := 0 to SymbolCount-1 do
    begin
      if Image64Bit then
      begin
        SymbolType := SymbolArr64[i].n_type;
        StringOffset := SymbolArr64[i].n_un.n_strx;
        SymbolValue := SymbolArr64[i].n_value;
      end
      else
      begin
        SymbolType := SymbolArr[i].n_type;
        StringOffset := SymbolArr[i].n_un.n_strx;
        SymbolValue := SymbolArr[i].n_value;
      end;
      if SymbolType = N_SECT then
      begin
        s := pchar(SymbolStr+StringOffset);
        ind := MainDwarfDebugMap.GlobalList.Find(s);
        if assigned(ind) then
          begin
            AddressMap:=MainDwarfDebugMap.GlobalList.ItemFromNode(ind);
            AddressMap.OrgAddr:=SymbolValue;
            AddressMapList.Add(AddressMap);
          end;
      end;
      if SymbolType = N_SECT+N_EXT then
      begin
        s := pchar(SymbolStr+StringOffset);
        ind := MainDwarfDebugMap.GlobalList.Find(s);
        if assigned(ind) then
          begin
            AddressMap:=MainDwarfDebugMap.GlobalList.ItemFromNode(ind);
            AddressMap.OrgAddr:=SymbolValue;
            AddressMapList.Add(AddressMap);
          end;
      end;
    end;
  end;
end;

initialization
  RegisterImageReaderClass( TDbgMachoDataSource );

end.