This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
/
Bfd.cs
1145 lines (923 loc) · 26.8 KB
/
Bfd.cs
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
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
using Mono.Debugger;
using Mono.Debugger.Languages;
using Mono.Debugger.Architectures;
namespace Mono.Debugger.Backends
{
internal delegate void BfdDisposedHandler (Bfd bfd);
internal class Bfd : SymbolFile, ISymbolContainer
{
IntPtr bfd;
protected Module module;
protected BfdContainer container;
protected TargetMemoryInfo info;
protected Bfd main_bfd;
protected Architecture arch;
TargetAddress first_link_map = TargetAddress.Null;
TargetAddress dynlink_breakpoint = TargetAddress.Null;
TargetAddress rdebug_state_addr = TargetAddress.Null;
TargetAddress entry_point = TargetAddress.Null;
bool is_loaded;
Hashtable symbols;
Hashtable local_symbols;
ArrayList simple_symbols;
BfdSymbolTable simple_symtab;
DwarfReader dwarf;
DwarfFrameReader frame_reader, eh_frame_reader;
bool dwarf_loaded;
bool has_debugging_info;
string filename, target;
bool is_coredump;
bool initialized;
bool has_shlib_info;
TargetAddress base_address, start_address, end_address;
TargetAddress plt_start, plt_end, got_start;
bool is_powerpc;
bool has_got;
[Flags]
internal enum SectionFlags {
Load = 1,
Alloc = 2,
ReadOnly = 4
};
internal class Section
{
public readonly Bfd bfd;
public readonly IntPtr section;
public readonly string name;
public readonly long vma;
public readonly long size;
public readonly SectionFlags flags;
public readonly ObjectCache contents;
internal Section (Bfd bfd, IntPtr section)
{
this.bfd = bfd;
this.section = section;
this.name = bfd_glue_get_section_name (section);
this.vma = bfd_glue_get_section_vma (section);
this.size = bfd_glue_get_section_size (section);
this.flags = bfd_glue_get_section_flags (section);
contents = new ObjectCache (
new ObjectCacheFunc (get_section_contents), section, 5);
}
object get_section_contents (object user_data)
{
byte[] data = bfd.GetSectionContents (section);
if (data == null)
throw new SymbolTableException ("Can't get bfd section {0}", name);
return new TargetReader (data, bfd.info);
}
public TargetReader GetReader (TargetAddress address)
{
TargetReader reader = (TargetReader) contents.Data;
reader.Offset = address.Address - bfd.BaseAddress.Address - vma;
return reader;
}
public override string ToString ()
{
return String.Format ("BfdSection ({0:x}:{1:x}:{2}:{3})", vma, size,
flags, bfd.FileName);
}
}
[DllImport("monodebuggerserver")]
extern static void bfd_init ();
[DllImport("monodebuggerserver")]
extern static IntPtr bfd_glue_openr (string filename, string target);
[DllImport("monodebuggerserver")]
extern static bool bfd_close (IntPtr bfd);
[DllImport("monodebuggerserver")]
extern static string bfd_glue_get_errormsg ();
[DllImport("monodebuggerserver")]
extern static string bfd_glue_get_target_name (IntPtr bfd);
[DllImport("monodebuggerserver")]
extern static IntPtr bfd_get_section_by_name (IntPtr bfd, string name);
[DllImport("monodebuggerserver")]
extern static string bfd_glue_core_file_failing_command (IntPtr bfd);
[DllImport("monodebuggerserver")]
extern static IntPtr disassembler (IntPtr bfd);
[DllImport("monodebuggerserver")]
extern static IntPtr bfd_glue_init_disassembler (IntPtr bfd);
[DllImport("monodebuggerserver")]
extern static bool bfd_glue_check_format_object (IntPtr bfd);
[DllImport("monodebuggerserver")]
extern static bool bfd_glue_check_format_core (IntPtr bfd);
[DllImport("monodebuggerserver")]
extern static int bfd_glue_get_symbols (IntPtr bfd, out IntPtr symtab);
[DllImport("monodebuggerserver")]
extern static int bfd_glue_get_dynamic_symbols (IntPtr bfd, out IntPtr symtab);
[DllImport("monodebuggerserver")]
extern static bool bfd_glue_get_section_contents (IntPtr bfd, IntPtr section, IntPtr data, int size);
[DllImport("monodebuggerserver")]
extern static IntPtr bfd_glue_get_first_section (IntPtr bfd);
[DllImport("monodebuggerserver")]
extern static IntPtr bfd_glue_get_next_section (IntPtr section);
[DllImport("monodebuggerserver")]
extern static long bfd_glue_get_section_vma (IntPtr section);
[DllImport("monodebuggerserver")]
extern static string bfd_glue_get_section_name (IntPtr section);
[DllImport("monodebuggerserver")]
extern static int bfd_glue_get_section_size (IntPtr section);
[DllImport("monodebuggerserver")]
extern static SectionFlags bfd_glue_get_section_flags (IntPtr section);
[DllImport("monodebuggerserver")]
extern static long bfd_glue_elfi386_locate_base (IntPtr bfd, IntPtr data, int size);
[DllImport("libglib-2.0-0.dll")]
extern static void g_free (IntPtr data);
[DllImport("monodebuggerserver")]
extern static string bfd_glue_get_symbol (IntPtr bfd, IntPtr symtab, int index,
out int is_function, out long address);
static Bfd ()
{
bfd_init ();
}
public Bfd (BfdContainer container, TargetMemoryInfo info, string filename,
Bfd main_bfd, TargetAddress base_address, bool is_loaded)
{
this.container = container;
this.info = info;
this.filename = filename;
this.base_address = base_address;
this.main_bfd = main_bfd;
this.is_loaded = is_loaded;
bfd = bfd_glue_openr (filename, null);
if (bfd == IntPtr.Zero)
throw new SymbolTableException ("Can't read symbol file: {0}", filename);
if (bfd_glue_check_format_object (bfd))
is_coredump = false;
else if (bfd_glue_check_format_core (bfd))
is_coredump = true;
else
throw new SymbolTableException ("Not an object file: {0}", filename);
target = bfd_glue_get_target_name (bfd);
if ((target == "elf32-i386") || (target == "elf64-x86-64")) {
if (target == "elf32-i386")
arch = new Architecture_I386 (container.Process, info);
else
arch = new Architecture_X86_64 (container.Process, info);
if (!is_coredump) {
Section text = GetSectionByName (".text", true);
Section bss = GetSectionByName (".bss", true);
if (!base_address.IsNull)
start_address = new TargetAddress (
info.AddressDomain,
base_address.Address + text.vma);
else
start_address = new TargetAddress (
info.AddressDomain, text.vma);
if (!base_address.IsNull)
end_address = new TargetAddress (
info.AddressDomain,
base_address.Address + bss.vma + bss.size);
else
end_address = new TargetAddress (
info.AddressDomain, bss.vma + bss.size);
}
read_bfd_symbols ();
if (DwarfReader.IsSupported (this))
has_debugging_info = true;
Section plt_section = GetSectionByName (".plt", false);
Section got_section = GetSectionByName (".got", false);
if ((plt_section != null) && (got_section != null)) {
plt_start = new TargetAddress (
info.AddressDomain,
base_address.Address + plt_section.vma);
plt_end = plt_start + plt_section.size;
got_start = new TargetAddress (
info.AddressDomain,
base_address.Address + got_section.vma);
has_got = true;
}
} else
throw new SymbolTableException (
"Symbol file {0} has unknown target architecture {1}",
filename, target);
entry_point = this ["main"];
module = container.Process.Session.GetModule (filename);
if (module == null) {
module = container.Process.Session.CreateModule (filename, this);
OnModuleChanged ();
} else {
module.LoadModule (this);
}
container.Process.SymbolTableManager.AddSymbolFile (this);
}
public Bfd OpenCoreFile (string core_file)
{
Bfd core = new Bfd (container, info, core_file, this, TargetAddress.Null, true);
core.is_coredump = true;
return core;
}
void read_bfd_symbols ()
{
IntPtr symtab;
int num_symbols = bfd_glue_get_symbols (bfd, out symtab);
symbols = new Hashtable ();
local_symbols = new Hashtable ();
simple_symbols = new ArrayList ();
for (int i = 0; i < num_symbols; i++) {
string name;
long address;
int is_function;
name = bfd_glue_get_symbol (bfd, symtab, i, out is_function, out address);
if (name == null)
continue;
TargetAddress relocated = new TargetAddress (
info.AddressDomain, base_address.Address + address);
if (is_function != 0)
symbols.Add (name, relocated);
else if ((main_bfd == null) && name.StartsWith ("MONO_DEBUGGER__"))
symbols.Add (name, relocated);
else if (!local_symbols.Contains (name))
local_symbols.Add (name, relocated);
simple_symbols.Add (new Symbol (name, relocated, 0));
}
g_free (symtab);
num_symbols = bfd_glue_get_dynamic_symbols (bfd, out symtab);
for (int i = 0; i < num_symbols; i++) {
string name;
long address;
int is_function;
name = bfd_glue_get_symbol (bfd, symtab, i, out is_function, out address);
if (name == null)
continue;
TargetAddress relocated = new TargetAddress (
info.AddressDomain,
base_address.Address + address);
simple_symbols.Add (new Symbol (name, relocated, 0));
}
g_free (symtab);
simple_symtab = new BfdSymbolTable (this);
}
protected ArrayList GetSimpleSymbols ()
{
return simple_symbols;
}
bool dynlink_handler (Inferior inferior)
{
if (inferior.ReadInteger (rdebug_state_addr) != 0)
return false;
do_update_shlib_info (inferior, inferior);
return false;
}
bool read_dynamic_info (Inferior inferior, TargetMemoryAccess target)
{
if (initialized)
return has_shlib_info;
initialized = true;
Section section = GetSectionByName (".dynamic", false);
if (section == null)
return false;
TargetAddress vma = new TargetAddress (info.AddressDomain, section.vma);
int size = (int) section.size;
byte[] dynamic = target.ReadBuffer (vma, size);
TargetAddress debug_base;
IntPtr data = IntPtr.Zero;
try {
data = Marshal.AllocHGlobal (size);
Marshal.Copy (dynamic, 0, data, size);
long base_ptr = bfd_glue_elfi386_locate_base (bfd, data, size);
if (base_ptr == 0)
return false;
debug_base = new TargetAddress (info.AddressDomain, base_ptr);
} finally {
if (data != IntPtr.Zero)
Marshal.FreeHGlobal (data);
}
int the_size = 2 * info.TargetLongIntegerSize +
3 * info.TargetAddressSize;
TargetBlob blob = target.ReadMemory (debug_base, the_size);
TargetReader reader = new TargetReader (blob.Contents, info);
if (reader.ReadLongInteger () != 1)
return false;
first_link_map = reader.ReadAddress ();
dynlink_breakpoint = reader.ReadAddress ();
rdebug_state_addr = debug_base + reader.Offset;
if (reader.ReadLongInteger () != 0)
return false;
if (inferior != null) {
AddressBreakpoint dynlink_bpt = new DynlinkBreakpoint (
this, dynlink_breakpoint);
dynlink_bpt.Insert (inferior);
}
has_shlib_info = true;
return true;
}
public void UpdateSharedLibraryInfo (Inferior inferior, TargetMemoryAccess target)
{
// This fails if it's a statically linked executable.
try {
if (!read_dynamic_info (inferior, target))
return;
} catch (Exception ex) {
Console.WriteLine ("UPDATE SHLIB INFO #1: {0}", ex);
return;
}
do_update_shlib_info (inferior, target);
}
void do_update_shlib_info (Inferior inferior, TargetMemoryAccess target)
{
bool first = true;
TargetAddress map = first_link_map;
while (!map.IsNull) {
int the_size = 4 * info.TargetAddressSize;
TargetBlob blob = target.ReadMemory (map, the_size);
TargetReader map_reader = new TargetReader (blob.Contents, info);
TargetAddress l_addr = map_reader.ReadAddress ();
TargetAddress l_name = map_reader.ReadAddress ();
map_reader.ReadAddress ();
string name;
try {
name = target.ReadString (l_name);
// glibc 2.3.x uses the empty string for the virtual
// "linux-gate.so.1".
if ((name != null) && (name == ""))
name = null;
} catch {
name = null;
}
map = map_reader.ReadAddress ();
if (first) {
first = false;
continue;
}
if (name == null)
continue;
Bfd bfd = container [name];
if (bfd != null) {
if (!bfd.IsLoaded)
bfd.module_loaded (inferior, l_addr);
continue;
}
bfd = container.AddFile (info, name, l_addr, module.StepInto, true);
bfd.module_loaded (inferior, l_addr);
}
}
public Bfd MainBfd {
get {
return main_bfd;
}
}
public TargetAddress BaseAddress {
get {
return base_address;
}
}
public Architecture Architecture {
get {
return arch;
}
}
public BfdContainer BfdContainer {
get {
return container;
}
}
public TargetAddress GetAddress (long address)
{
if (BaseAddress.IsNull)
return new TargetAddress (
info.AddressDomain, address);
else
return new TargetAddress (
info.AddressDomain, BaseAddress.Address + address);
}
public TargetMemoryInfo TargetMemoryInfo {
get {
return info;
}
}
public override bool IsNative {
get {
return true;
}
}
public string Name {
get {
return filename;
}
}
public override string FullName {
get {
return filename;
}
}
public string FileName {
get {
return filename;
}
}
public string Target {
get {
return target;
}
}
public bool IsCoreDump {
get {
return is_coredump;
}
}
public string CrashProgram {
get {
if (!is_coredump)
throw new InvalidOperationException ();
return bfd_glue_core_file_failing_command (bfd);
}
}
public override Language Language {
get {
return container.NativeLanguage;
}
}
internal DwarfReader DwarfReader {
get { return dwarf; }
}
public override bool SymbolsLoaded {
get { return (dwarf != null); }
}
public override SourceFile[] Sources {
get {
if (dwarf != null)
return dwarf.Sources;
throw new InvalidOperationException ();
}
}
public override MethodSource[] GetMethods (SourceFile file)
{
if (dwarf != null)
return dwarf.GetMethods (file);
throw new InvalidOperationException ();
}
public override MethodSource FindMethod (string name)
{
if (dwarf != null)
return dwarf.FindMethod (name);
return null;
}
public override ISymbolTable SymbolTable {
get {
if (dwarf != null)
return dwarf.SymbolTable;
else
throw new InvalidOperationException ();
}
}
public override Symbol SimpleLookup (TargetAddress address, bool exact_match)
{
if (simple_symtab != null)
return simple_symtab.SimpleLookup (address, exact_match);
return null;
}
public TargetAddress EntryPoint {
get { return entry_point; }
}
void create_frame_reader ()
{
long vma_base = base_address.IsNull ? 0 : base_address.Address;
Section section = GetSectionByName (".debug_frame", false);
if (section != null) {
byte[] contents = GetSectionContents (section.section);
TargetBlob blob = new TargetBlob (contents, info);
frame_reader = new DwarfFrameReader (
this, blob, vma_base + section.vma, false);
}
section = GetSectionByName (".eh_frame", false);
if (section != null) {
byte[] contents = GetSectionContents (section.section);
TargetBlob blob = new TargetBlob (contents, info);
eh_frame_reader = new DwarfFrameReader (
this, blob, vma_base + section.vma, true);
}
}
void load_dwarf ()
{
if (dwarf_loaded)
return;
try {
create_frame_reader ();
} catch (Exception ex) {
Console.WriteLine ("Cannot read DWARF frame info from " +
"symbol file `{0}': {1}", FileName, ex);
}
if (!has_debugging_info)
return;
try {
dwarf = new DwarfReader (this, module);
} catch (Exception ex) {
Console.WriteLine ("Cannot read DWARF debugging info from " +
"symbol file `{0}': {1}", FileName, ex);
has_debugging_info = false;
return;
}
dwarf_loaded = true;
if (dwarf != null) {
has_debugging_info = true;
}
}
void unload_dwarf ()
{
if (!dwarf_loaded || !has_debugging_info)
return;
frame_reader = null;
eh_frame_reader = null;
dwarf_loaded = false;
dwarf = null;
}
internal override void OnModuleChanged ()
{
if (module.LoadSymbols) {
load_dwarf ();
} else {
unload_dwarf ();
}
}
internal BfdDisassembler GetDisassembler (TargetMemoryAccess memory)
{
IntPtr dis = disassembler (bfd);
IntPtr info = bfd_glue_init_disassembler (bfd);
return new BfdDisassembler (
container.Process.SymbolTableManager, memory, dis, info);
}
public TargetAddress this [string name] {
get {
if (symbols == null)
return TargetAddress.Null;
if (symbols.Contains (name))
return (TargetAddress) symbols [name];
return TargetAddress.Null;
}
}
public TargetAddress LookupLocalSymbol (string name)
{
if (local_symbols == null)
return TargetAddress.Null;
if (local_symbols.Contains (name))
return (TargetAddress) local_symbols [name];
return TargetAddress.Null;
}
internal Section FindSection (long address)
{
read_sections ();
foreach (Section section in sections) {
long relocated = base_address.Address + section.vma;
if ((address < relocated) || (address >= relocated + section.size))
continue;
return section;
}
return null;
}
internal Section this [long address] {
get {
Section section = FindSection (address);
if (section != null)
return section;
throw new SymbolTableException (
"No section in file {1} contains address {0:x}.", address,
filename);
}
}
internal Section[] Sections {
get {
read_sections ();
return sections;
}
}
public TargetMemoryArea[] GetMemoryMaps ()
{
if (!is_coredump)
throw new InvalidOperationException ();
ArrayList list = new ArrayList ();
read_sections ();
ArrayList all_sections = new ArrayList ();
all_sections.AddRange (sections);
all_sections.AddRange (main_bfd.Sections);
foreach (Section section in all_sections) {
if ((section.flags & SectionFlags.Alloc) == 0)
continue;
if (section.size == 0)
continue;
TargetAddress start = new TargetAddress (
info.AddressDomain, section.vma);
TargetAddress end = start + section.size;
TargetMemoryFlags flags = 0;
if ((section.flags & SectionFlags.ReadOnly) != 0)
flags |= TargetMemoryFlags.ReadOnly;
if (list.Count > 0) {
TargetMemoryArea last = (TargetMemoryArea) list [list.Count - 1];
if ((last.Flags == flags) &&
((last.End + 1 == start) || (last.End == start))) {
list [list.Count - 1] = new TargetMemoryArea (
last.Start, end, last.Flags, last.Name);
continue;
}
}
string name = section.bfd.FileName;
list.Add (new TargetMemoryArea (start, end, flags, name));
}
TargetMemoryArea[] maps = new TargetMemoryArea [list.Count];
list.CopyTo (maps, 0);
return maps;
}
public TargetReader GetReader (TargetAddress address, bool may_fail)
{
Section section = FindSection (address.Address);
if (section != null)
return section.GetReader (address);
if (may_fail)
return null;
throw new SymbolTableException (
"No section in file {1} contains address {0:x}.", address,
filename);
}
public TargetAddress GetSectionAddress (string name)
{
IntPtr section;
section = bfd_get_section_by_name (bfd, name);
if (section == IntPtr.Zero)
return TargetAddress.Null;
long vma = bfd_glue_get_section_vma (section);
return GetAddress (vma);
}
public byte[] GetSectionContents (string name)
{
IntPtr section;
section = bfd_get_section_by_name (bfd, name);
if (section == IntPtr.Zero)
throw new SymbolTableException ("Can't find bfd section {0}", name);
byte[] contents = GetSectionContents (section);
if (contents == null)
throw new SymbolTableException ("Can't read bfd section {0}", name);
return contents;
}
public TargetReader GetSectionReader (string name)
{
byte[] contents = GetSectionContents (name);
if (contents == null)
throw new SymbolTableException ("Can't read bfd section {0}", name);
return new TargetReader (contents, info);
}
byte[] GetSectionContents (IntPtr section)
{
int size = bfd_glue_get_section_size (section);
IntPtr data = IntPtr.Zero;
try {
data = Marshal.AllocHGlobal (size);
if (!bfd_glue_get_section_contents (bfd, section, data, size)) {
string error = bfd_glue_get_errormsg ();
string name = bfd_glue_get_section_name (section);
throw new SymbolTableException (
"Can't read bfd section {0}: {1}", name, error);
}
byte[] retval = new byte [size];
Marshal.Copy (data, retval, 0, size);
return retval;
} finally {
Marshal.FreeHGlobal (data);
}
}
public bool HasSection (string name)
{
return GetSectionByName (name, false) != null;
}
Section GetSectionByName (string name, bool throw_exc)
{
IntPtr section = bfd_get_section_by_name (bfd, name);
if (section == IntPtr.Zero) {
if (throw_exc)
throw new SymbolTableException (
"Can't get bfd section {0}", name);
else
return null;
}
return new Section (this, section);
}
bool has_sections = false;
Section[] sections = null;
protected void read_sections ()
{
if (has_sections)
return;
ArrayList list = new ArrayList ();
IntPtr asection = bfd_glue_get_first_section (bfd);
while (asection != IntPtr.Zero) {
Section section = new Section (this, asection);
list.Add (section);
asection = bfd_glue_get_next_section (asection);
}
sections = new Section [list.Count];
list.CopyTo (sections, 0);
has_sections = true;
}
public override Module Module {
get {
return module;
}
}
public override bool HasDebuggingInfo {
get {
return has_debugging_info;
}
}
public bool IsLoaded {
get {
return (main_bfd == null) || is_loaded || !base_address.IsNull;
}
}
//
// ISymbolContainer
//
public bool IsContinuous {
get {
return !is_coredump && !end_address.IsNull;
}
}
public TargetAddress StartAddress {
get {
if (!IsContinuous)
throw new InvalidOperationException ();
return start_address;
}
}
public TargetAddress EndAddress {
get {
if (!IsContinuous)
throw new InvalidOperationException ();
return end_address;
}
}
internal bool GetTrampoline (TargetMemoryAccess memory,
TargetAddress call_site,
TargetAddress call_target,
out TargetAddress trampoline,
out bool is_start)
{
if (!has_got || (call_site < plt_start) || (call_site > plt_end)) {
is_start = false;
trampoline = TargetAddress.Null;
return false;
}
TargetAddress method = memory.ReadAddress (call_target);
if (method != call_site + 6) {
is_start = false;
trampoline = method;
return true;
}
is_start = true;
trampoline = memory.ReadAddress (got_start + 3 * info.TargetAddressSize);
return true;
}
internal override StackFrame UnwindStack (StackFrame frame, TargetMemoryAccess memory)
{
if ((frame.TargetAddress < StartAddress) || (frame.TargetAddress > EndAddress))
return null;
StackFrame new_frame;
try {
new_frame = arch.TrySpecialUnwind (frame, memory);
if (new_frame != null)
return new_frame;
} catch {
}
try {
if (frame_reader != null) {
new_frame = frame_reader.UnwindStack (frame, memory, arch);
if (new_frame != null)
return new_frame;
}
if (eh_frame_reader != null) {
new_frame = eh_frame_reader.UnwindStack (frame, memory, arch);
if (new_frame != null)
return new_frame;
}
} catch {
return null;
}
return null;
}
public void ReadTypes ()
{
if (dwarf != null)
dwarf.ReadTypes ();
}
void module_loaded (Inferior inferior, TargetAddress address)