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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080 | with Ada.Strings.Fixed;
with Ada.Text_IO;
with Ada.Integer_Text_IO;
package body WL.Files.ELF is
Trace_Reader : constant Boolean := False;
String_Table_Name : constant String := ".shstrtab";
procedure Scan_Form (Text : String;
Process : not null access
procedure (Name, Value : String));
function Find_Section
(File : File_Type;
Name : String)
return Elf_Word_16;
function Save_Name
(File : in out File_Type;
Name : String)
return Elf_Word_32;
procedure Align
(File : in out File_Type;
Alignment : System.Storage_Elements.Storage_Count);
-- procedure Read
-- (File : in out File_Type;
-- Value : out System.Storage_Elements.Storage_Element);
procedure Read_Record
(File : in out File_Type;
Offset : Elf_Word_32;
Addr : System.Address;
Length : System.Storage_Elements.Storage_Count);
procedure Read_Vector
(File : in out File_Type;
Offset : Elf_Word_32;
Count : Natural;
Vector : in out Storage_Element_Vectors.Vector);
procedure Write
(File : in out File_Type;
Value : System.Storage_Elements.Storage_Element);
procedure Write
(File : in out File_Type;
Addr : System.Address;
Length : System.Storage_Elements.Storage_Count);
procedure Write
(File : in out File_Type;
Vector : Storage_Element_Vectors.Vector);
procedure Write_Name
(Vector : in out Storage_Element_Vectors.Vector;
Nm : String);
procedure Put_Hex
(Value : Elf_Word_32;
Width : Positive);
--------------------------
-- Add_Symbol_Reference --
--------------------------
procedure Add_Symbol_Reference
(File : in out File_Type;
Name : String;
Section_Name : String;
Section_Offset : Address_32;
Info : Octet)
is
Rel_Name : constant String := ".rel" & Section_Name;
begin
if not File.Section_Map.Contains (Rel_Name) then
File.Section_List.Append
(Section_Record'
(Header => Section_Header'
(Sh_Name => Save_Name (File, Rel_Name),
Sh_Type => Section_Header_Type'Pos (Sht_Rel),
Sh_Flags => 2 ** 6,
Sh_Addr => 0,
Sh_Offset => 0,
Sh_Size => 0,
Sh_Link => 0,
Sh_Info => 1,
Sh_Addralign => 4,
Sh_Entsize => 8),
Index => Elf_Word_16 (File.Section_List.Length),
Data => <>));
File.Section_Map.Insert (Rel_Name, File.Section_List.Last);
end if;
if not File.Symbol_Map.Contains (Name) then
New_Symbol (File, Name, 0, 0, Global, Object, Default, Section_Name);
end if;
declare
use System.Storage_Elements;
Rel : Relocation_Entry :=
Relocation_Entry'
(Offset => Section_Offset,
Info => (File.Symbol_Map (Name) - 1) * 256
+ Elf_Word_32 (Info));
Data : Storage_Array (1 .. Rel'Size / 8);
pragma Import (Ada, Data);
for Data'Address use Rel'Address;
Section : Section_Record renames
File.Section_List (File.Section_Map (Rel_Name));
begin
for Element of Data loop
Section.Data.Append (Element);
end loop;
end;
end Add_Symbol_Reference;
-----------
-- Align --
-----------
procedure Align
(File : in out File_Type;
Alignment : System.Storage_Elements.Storage_Count)
is
use System.Storage_Elements;
Extra : constant Storage_Count :=
Storage_Count (File.Data.Length) mod Alignment;
begin
if Extra > 0 then
declare
Pad_Size : constant Storage_Count := Alignment - Extra;
Padding : constant Storage_Array (1 .. Pad_Size) :=
(others => 0);
begin
Write (File, Padding'Address, Pad_Size);
end;
end if;
end Align;
-----------
-- Close --
-----------
procedure Close (File : in out File_Type) is
use type System.Storage_Elements.Storage_Count;
Start_Offset : constant := 16#0040#;
begin
if File.Reading then
return;
end if;
for Section of File.Section_List loop
if Section.Header.Sh_Type = Section_Header_Type'Pos (Sht_Rel) then
Section.Header.Sh_Link := Elf_Word_32 (File.Section_List.Length);
end if;
end loop;
File.Symbol_Table.Header.Sh_Name :=
Elf_Word_32 (File.String_Table.Data.Length);
Write_Name (File.String_Table.Data, ".symtab");
File.Symbol_Table.Header.Sh_Link :=
Elf_Word_32 (File.Section_List.Length) + 1;
-- File.Symbol_Table.Header.Sh_Info := 1;
File.Section_List.Append (File.Symbol_Table);
File.Section_Map.Insert
(".symtab", File.Section_List.Last);
File.String_Table.Header.Sh_Name :=
Elf_Word_32 (File.String_Table.Data.Length);
Write_Name (File.String_Table.Data, String_Table_Name);
File.Section_List.Append (File.String_Table);
File.Section_Map.Insert
(String_Table_Name, File.Section_List.Last);
File.Header.E_Shstrndx :=
Elf_Shstrndx (File.Section_List.Length) - 1;
for Section of File.Section_List loop
Align (File, Start_Offset);
Section.Header.Sh_Offset :=
Elf_Word_32 (File.Data.Length) + Start_Offset;
Section.Header.Sh_Size := Elf_Word_32 (Section.Data.Length);
Write (File, Section.Data);
end loop;
File.Header.E_Shentsize := Elf_Shentsize (Section_Header'Size / 8);
File.Header.E_Shoff := Elf_Shoff (File.Data.Length) + Start_Offset;
File.Header.E_Shnum := Elf_Shnum (File.Section_List.Length);
for Section of File.Section_List loop
Write (File, Section.Header'Address, Section.Header'Size / 8);
end loop;
File.Saving := True;
Write (File, File.Header'Address, File.Header'Size / 8);
for I in 1 .. Start_Offset - File.Header'Size / 8 loop
Write (File, 0);
end loop;
Write (File, File.Data);
Storage_IO.Close (File.Storage_File);
end Close;
------------
-- Create --
------------
procedure Create
(File : in out File_Type;
Mode : File_Mode := Out_File;
Path : String;
Form : String := "")
is
pragma Unreferenced (Mode);
procedure Set_Form (Name, Value : String);
--------------
-- Set_Form --
--------------
procedure Set_Form (Name, Value : String) is
begin
if Name = "endien" then
if Value = "big" then
File.Header.E_Magic (EI_DATA) := 2;
end if;
elsif Name = "word-size" then
if Value = "64" then
File.Header.E_Magic (EI_CLASS) := 2;
end if;
end if;
end Set_Form;
begin
Scan_Form (Form, Set_Form'Access);
Storage_IO.Create (File.Storage_File, Storage_IO.Out_File, Path);
File.Header.E_Type := 16#01#;
File.Header.E_Machine := 16#F3#;
File.Header.E_Ehsize := File.Header'Size / 8;
File.Symbol_Table :=
Section_Record'
(Header => Section_Header'
(Sh_Name => 0,
Sh_Type => Section_Header_Type'Pos (Sht_Symtab),
Sh_Flags => 0,
Sh_Addr => 0,
Sh_Offset => 0,
Sh_Size => 16,
Sh_Link => 0,
Sh_Info => 1,
Sh_Addralign => 0,
Sh_Entsize => 16),
Index => 0,
Data => <>);
for I in 1 .. File.Symbol_Table.Header.Sh_Size loop
File.Symbol_Table.Data.Append (0);
end loop;
File.String_Table :=
Section_Record'
(Header => Section_Header'
(Sh_Name => 0,
Sh_Type => Section_Header_Type'Pos (Sht_Strtab),
Sh_Flags => 0,
Sh_Addr => 0,
Sh_Offset => 0,
Sh_Size => 0,
Sh_Link => 0,
Sh_Info => 0,
Sh_Addralign => 0,
Sh_Entsize => 0),
Index => 0,
Data => <>);
for I in 1 .. 4 loop
File.String_Table.Data.Append (0);
end loop;
File.Section_List.Append
(Section_Record'
(Header => Section_Header'
(Sh_Name => 0,
Sh_Type => 0,
Sh_Flags => 0,
Sh_Addr => 0,
Sh_Offset => 0,
Sh_Size => 0,
Sh_Link => 0,
Sh_Info => 0,
Sh_Addralign => 0,
Sh_Entsize => 0),
Index => 0,
Data => <>));
end Create;
------------------
-- Find_Section --
------------------
function Find_Section
(File : File_Type;
Name : String)
return Elf_Word_16
is
begin
return File.Section_List (File.Section_Map (Name)).Index;
end Find_Section;
--------------
-- Get_Name --
--------------
function Get_Name
(File : File_Type;
Symbol : Symbol_Table_Entry)
return String
is
begin
return Get_String (File, Symbol.St_Name);
end Get_Name;
-----------------
-- Get_Section --
-----------------
function Get_Section
(File : File_Type;
Section_Header : Section_Header_Type)
return Elf_Word_32
is
Index : Elf_Word_32 := 0;
begin
for Section of File.Section_List loop
if Section.Header.Sh_Type
= Section_Header_Type'Pos (Section_Header)
then
return Index;
end if;
end loop;
return 0;
end Get_Section;
-----------------------
-- Get_Section_Entry --
-----------------------
function Get_Section_Entry
(File : File_Type;
Section_Header : Section_Header_Type)
return Section_Entry
is
begin
for Position in File.Section_List.Iterate loop
if Section_Lists.Element (Position).Header.Sh_Type
= Section_Header_Type'Pos (Section_Header)
then
return (Position => Position);
end if;
end loop;
return (Position => Section_Lists.No_Element);
end Get_Section_Entry;
-----------------------
-- Get_Section_Entry --
-----------------------
function Get_Section_Entry
(File : File_Type;
Index : Elf_Word_32)
return Section_Entry
is
begin
for Position in File.Section_List.Iterate loop
if Elf_Word_32 (Section_Lists.Element (Position).Index) = Index then
return (Position => Position);
end if;
end loop;
return (Position => Section_Lists.No_Element);
end Get_Section_Entry;
----------------
-- Get_String --
----------------
function Get_String
(File : File_Type;
Offset : Elf_Word_32)
return String
is
use type System.Storage_Elements.Storage_Element;
Str_Sec : constant Section_Entry :=
Get_Section_Entry (File, Sht_Strtab);
Rec : Section_Record renames
File.Section_List (Str_Sec.Position);
First : constant Natural := Natural (Offset);
Last : Natural := First;
begin
while Last <= Rec.Data.Last_Index
and then Rec.Data (Last) /= 0
loop
Last := Last + 1;
end loop;
return S : String (1 .. Last - First) do
for I in S'Range loop
S (I) := Character'Val (Rec.Data (First + I - 1));
end loop;
end return;
end Get_String;
------------------------
-- Iterate_Relocation --
------------------------
procedure Iterate_Relocation
(File : File_Type;
Process : not null access
procedure (Section : Elf_Word_16;
Offset : Address_32;
Info : Octet;
Symbol : Symbol_Table_Entry))
is
Size : constant Positive :=
Natural (Relocation_Entry'Size) / 8;
begin
for Rec of File.Section_List loop
if Section_Header_Type'Val (Rec.Header.Sh_Type) = Sht_Rel then
declare
Symbols : constant Elf_Word_16 :=
Elf_Word_16 (Rec.Header.Sh_Link);
Section : constant Elf_Word_16 :=
Elf_Word_16 (Rec.Header.Sh_Info);
Offset : Natural := 0;
begin
while Offset <= Rec.Data.Last_Index - Size + 1 loop
declare
use System.Storage_Elements;
Data : Storage_Array (1 .. Storage_Offset (Size));
Ent : Relocation_Entry;
for Ent'Address use Data'Address;
begin
for Element of Data loop
Element := Rec.Data.Element (Offset);
Offset := Offset + 1;
end loop;
declare
Sym_Idx : constant Elf_Word_32 := Ent.Info / 256;
Sym_Ent : constant Symbol_Table_Entry :=
Get_Symbol_Entry
(File, Elf_Word_32 (Symbols),
Ent.Info / 256);
begin
Process (Section, Ent.Offset,
Octet (Ent.Info mod 8),
Sym_Ent);
end;
end;
end loop;
end;
end if;
end loop;
end Iterate_Relocation;
----------------------
-- Iterate_Sections --
----------------------
procedure Iterate_Sections
(File : File_Type;
Header_Type : Section_Header_Type;
Process : not null access
procedure (Section : Section_Entry))
is
begin
for Position in File.Section_List.Iterate loop
if File.Section_List (Position).Header.Sh_Type
= Section_Header_Type'Pos (Header_Type)
then
Process ((Position => Position));
end if;
end loop;
end Iterate_Sections;
---------------------
-- Iterate_Symbols --
---------------------
procedure Iterate_Symbols
(File : File_Type;
Process : not null access
procedure (Name : String;
Value : Address_32;
Size : Elf_Word_32;
Binding : Symbol_Table_Binding;
Typ : Symbol_Table_Type;
Visibility : Symbol_Table_Visibility;
Section : Elf_Word_16))
is
Rec : Section_Record renames
File.Section_List (Get_Section_Entry (File, Sht_Symtab).Position);
Size : constant Positive := Natural (Symbol_Table_Entry'Size) / 8;
Offset : Natural := Size;
begin
while Offset <= Rec.Data.Last_Index - Size + 1 loop
declare
use System.Storage_Elements;
Data : Storage_Array (1 .. Storage_Offset (Size));
Ent : Symbol_Table_Entry;
for Ent'Address use Data'Address;
begin
for Element of Data loop
Element := Rec.Data.Element (Offset);
Offset := Offset + 1;
end loop;
Process (Get_String (File, Ent.St_Name),
Ent.St_Value,
Ent.St_Size,
Symbol_Table_Binding'Val (Ent.St_Info / 16),
Symbol_Table_Type'Val (Ent.St_Info mod 16),
Default,
Ent.St_Shndx);
end;
end loop;
end Iterate_Symbols;
------------------------
-- New_Program_Header --
------------------------
procedure New_Program_Header
(File : in out File_Type;
Header_Type : Program_Header_Type;
Read : Boolean;
Write : Boolean;
Execute : Boolean;
Virtual_Address : Address_32)
is
begin
null;
end New_Program_Header;
------------------------
-- New_Section_Header --
------------------------
procedure New_Section_Header
(File : in out File_Type;
Name : String;
Header_Type : Section_Header_Type;
Write : Boolean := False;
Alloc : Boolean := False;
Execinstr : Boolean := False;
Merge : Boolean := False;
Strings : Boolean := False;
Info_Link : Boolean := False;
Link_Order : Boolean := False)
is
function Flag (B : Boolean;
Shift : Natural)
return Elf_Word_32
is (if B then 2 ** Shift else 0);
Section_Flags : constant Elf_Word_32 :=
Flag (Write, 0)
+ Flag (Alloc, 1)
+ Flag (Execinstr, 2)
+ Flag (Merge, 4)
+ Flag (Strings, 5)
+ Flag (Info_Link, 6)
+ Flag (Link_Order, 7);
Rec : constant Section_Record :=
(Header => Section_Header'
(Sh_Name => Save_Name (File, Name),
Sh_Type => Section_Header_Type'Pos (Header_Type),
Sh_Flags => Section_Flags,
Sh_Addr => 0,
Sh_Offset => 0,
Sh_Size => 0,
Sh_Link => 0,
Sh_Info => 0,
Sh_Addralign => 0,
Sh_Entsize => 0),
Index => Elf_Word_16 (File.Section_List.Length),
Data => Storage_Element_Vectors.Empty_Vector);
begin
File.Section_List.Append (Rec);
File.Section_Map.Insert (Name, File.Section_List.Last);
File.Current := File.Section_List.Last;
end New_Section_Header;
----------------
-- New_Symbol --
----------------
procedure New_Symbol
(File : in out File_Type;
Name : String;
Value : Address_32;
Size : Natural;
Binding : Symbol_Table_Binding;
Typ : Symbol_Table_Type;
Visibility : Symbol_Table_Visibility;
Section_Name : String)
is
use System.Storage_Elements;
Shndx : constant Elf_Word_16 :=
(if Section_Name = "" then 0
else Find_Section (File, Section_Name));
Sym : constant Symbol_Table_Entry :=
Symbol_Table_Entry'
(St_Name => Save_Name (File, Name),
St_Value => Value,
St_Size => Elf_Word_32 (Size),
St_Info => Symbol_Table_Binding'Pos (Binding) * 2 ** 4
+ Symbol_Table_Type'Pos (Typ) mod 16,
St_Other => Symbol_Table_Visibility'Pos (Visibility),
St_Shndx => Shndx);
Data : Storage_Array (1 .. Sym'Size / 8);
pragma Import (Ada, Data);
for Data'Address use Sym'Address;
begin
for Element of Data loop
File.Symbol_Table.Data.Append (Element);
end loop;
File.Symbol_Map.Insert
(Name, Elf_Word_32 (File.Symbol_Table.Data.Length)
/ Elf_Word_32 (Data'Length));
if Binding = Local then
declare
Sh_Info : Elf_Word_32 renames
File.Symbol_Table.Header.Sh_Info;
begin
Sh_Info := Sh_Info + 1;
end;
end if;
end New_Symbol;
----------
-- Open --
----------
procedure Open
(File : in out File_Type;
Mode : File_Mode;
Path : String)
is
use type System.Storage_Elements.Storage_Offset;
begin
File.Reading := True;
Storage_IO.Open (File.Storage_File, Storage_IO.In_File, Path);
File.Data.Clear;
while not Storage_IO.End_Of_File (File.Storage_File) loop
declare
X : System.Storage_Elements.Storage_Element;
begin
Storage_IO.Read (File.Storage_File, X);
File.Data.Append (X);
end;
end loop;
Storage_IO.Close (File.Storage_File);
Read_Record (File, 0, File.Header'Address, File.Header'Size / 8);
if Trace_Reader then
Ada.Text_IO.Put (" Magic: ");
for X of File.Header.E_Magic loop
declare
S : constant String := "0123456789abcdef";
I : constant Positive := Natural (X / 16) + 1;
J : constant Positive := Natural (X mod 16) + 1;
begin
Ada.Text_IO.Put (' ');
Ada.Text_IO.Put (S (I));
Ada.Text_IO.Put (S (J));
end;
end loop;
Ada.Text_IO.New_Line;
end if;
if File.Header.E_Magic /= Elf_Magic_Sequence then
Ada.Text_IO.Put
("magic: [");
for Ch of File.Header.E_Magic loop
if Ch in 32 .. 126 then
Ada.Text_IO.Put (Character'Val (Ch));
else
Ada.Text_IO.Put
("#"
& Character'Val (48 + Ch / 100)
& Character'Val (48 + Ch / 10 mod 10)
& Character'Val (48 + Ch mod 10));
end if;
end loop;
Ada.Text_IO.Put_Line ("]");
raise Bad_Magic;
end if;
if Trace_Reader then
Ada.Text_IO.Put_Line
(" Class: ");
Ada.Text_IO.Put_Line
(" Start of section headers: "
& File.Header.E_Shoff'Image
& " (bytes into the file)");
Ada.Text_IO.Put_Line
(" Size of section headers: "
& File.Header.E_Shentsize'Image
& " (bytes)");
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line
("Section Headers:");
Ada.Text_IO.Put_Line
(" [Nr] Name Type Addr Off Size"
& " ES Flg Lk Inf Al");
end if;
declare
Sh_Base : Elf_Word_32 := Elf_Word_32 (File.Header.E_Shoff);
Sh_Idx : Elf_Shnum := 0;
begin
while Sh_Idx < File.Header.E_Shnum loop
declare
Rec : Section_Record;
begin
Read_Record (File, Sh_Base,
Rec.Header'Address, Rec.Header'Size / 8);
Rec.Index := Elf_Word_16 (Sh_Idx);
if Rec.Header.Sh_Size > 0 then
Read_Vector
(File, Rec.Header.Sh_Offset,
Natural (Rec.Header.Sh_Size), Rec.Data);
end if;
File.Section_List.Append (Rec);
Sh_Idx := Sh_Idx + 1;
Sh_Base := Sh_Base + Elf_Word_32 (File.Header.E_Shentsize);
end;
end loop;
if Trace_Reader then
for Section of File.Section_List loop
declare
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Put_String
(Text : String;
Width : Positive);
----------------
-- Put_String --
----------------
procedure Put_String
(Text : String;
Width : Positive)
is
S : String (1 .. Width) := (others => ' ');
Last : constant Natural :=
Natural'Min (Text'Length, Width);
begin
S (1 .. Last) :=
Text (Text'First .. Text'First + Last - 1);
Put (S);
end;
begin
Put (" [");
Put (Natural (Section.Index), 2);
Put ("] ");
Put_String (Get_String (File, Section.Header.Sh_Name), 18);
declare
Sh_Type : Section_Header_Type;
begin
Sh_Type :=
Section_Header_Type'Val (Section.Header.Sh_Type);
declare
Img : constant String :=
Section_Header_Type'Image (Sh_Type);
begin
Put_String (Img (5 .. Img'Last), 16);
end;
exception
when Constraint_Error =>
Put_String ("UNKNOWN", 16);
end;
Put_Hex (Section.Header.Sh_Addr, 8);
Put (" ");
Put_Hex (Section.Header.Sh_Offset, 6);
Put (" ");
Put_Hex (Section.Header.Sh_Size, 6);
end;
Ada.Text_IO.New_Line;
end loop;
end if;
end;
end Open;
---------
-- Put --
---------
procedure Put (File : in out File_Type; Value : Octet) is
begin
File.Section_List (File.Section_List.Last)
.Data.Append (System.Storage_Elements.Storage_Element (Value));
end Put;
-------------
-- Put_Hex --
-------------
procedure Put_Hex
(Value : Elf_Word_32;
Width : Positive)
is
S : constant String := "0123456789abcdef";
It : Elf_Word_32 := Value;
R : String (1 .. Width);
begin
for Ch of reverse R loop
Ch := S (Natural (It mod 16) + 1);
It := It / 16;
end loop;
Ada.Text_IO.Put (R);
end Put_Hex;
----------
-- Read --
----------
procedure Read
(File : in out File_Type;
Offset : Elf_Word_32;
Data : out System.Storage_Elements.Storage_Array)
is
Index : Natural := Natural (Offset);
Dump : constant Boolean := False;
begin
for Element of Data loop
declare
Dx : constant Natural := Index - Natural (Offset);
begin
if Dump then
if Dx mod 16 = 0 then
Put_Hex (Elf_Word_32 (Dx), 4);
Ada.Text_IO.Put (":");
end if;
end if;
if Index <= File.Data.Last_Index then
Element := File.Data (Index);
else
Element := 0;
end if;
if Dump then
Ada.Text_IO.Put (" ");
Put_Hex (Elf_Word_32 (Element), 2);
end if;
end;
Index := Index + 1;
if Dump then
if (Index - Natural (Offset)) mod 16 = 0 then
Ada.Text_IO.New_Line;
end if;
end if;
end loop;
if Dump then
if (Index - Natural (Offset)) mod 16 /= 0 then
Ada.Text_IO.New_Line;
end if;
end if;
end Read;
-----------------
-- Read_Record --
-----------------
procedure Read_Record
(File : in out File_Type;
Offset : Elf_Word_32;
Addr : System.Address;
Length : System.Storage_Elements.Storage_Count)
is
Data : System.Storage_Elements.Storage_Array (1 .. Length);
for Data'Address use Addr;
begin
Read (File, Offset, Data);
end Read_Record;
-----------------
-- Read_Vector --
-----------------
procedure Read_Vector
(File : in out File_Type;
Offset : Elf_Word_32;
Count : Natural;
Vector : in out Storage_Element_Vectors.Vector)
is
Data : System.Storage_Elements.Storage_Array
(1 .. System.Storage_Elements.Storage_Offset (Count));
begin
Read (File, Offset, Data);
Vector.Clear;
for Element of Data loop
Vector.Append (Element);
end loop;
end Read_Vector;
---------------
-- Save_Name --
---------------
function Save_Name
(File : in out File_Type;
Name : String)
return Elf_Word_32
is
begin
return Addr : constant Elf_Word_32 :=
Elf_Word_32 (File.String_Table.Data.Length)
do
Write_Name (File.String_Table.Data, Name);
end return;
end Save_Name;
---------------
-- Scan_Form --
---------------
procedure Scan_Form (Text : String;
Process : not null access
procedure (Name, Value : String))
is
Source : constant String := Text & ";";
Start : Positive := Source'First;
Index : Natural := Ada.Strings.Fixed.Index (Source, ";");
begin
while Index > 0 loop
declare
Name_And_Value : constant String := Source (Start .. Index - 1);
Colon_Index : constant Natural :=
Ada.Strings.Fixed.Index (Name_And_Value, ":");
begin
if Colon_Index > 0 then
declare
Name : constant String :=
Name_And_Value
(Name_And_Value'First .. Colon_Index - 1);
Value : constant String :=
Name_And_Value
(Colon_Index + 1 .. Name_And_Value'Last);
begin
Process (Name, Value);
end;
end if;
end;
Start := Index + 1;
Index := Ada.Strings.Fixed.Index (Source, ";", Start);
end loop;
end Scan_Form;
----------------------
-- Get_Symbol_Entry --
----------------------
function Get_Symbol_Entry
(File : File_Type;
Section : Elf_Word_32;
Index : Elf_Word_32)
return Symbol_Table_Entry
is
use type System.Storage_Elements.Storage_Offset;
Rec : Section_Record renames
File.Section_List
(Get_Section_Entry (File, Section).Position);
Base : constant Natural :=
Natural (Index * Symbol_Table_Entry'Size / 8);
Data : System.Storage_Elements.Storage_Array
(1 .. Symbol_Table_Entry'Size / 8);
Result : Symbol_Table_Entry;
for Result'Address use Data'Address;
begin
for I in Data'Range loop
Data (I) := Rec.Data (Base + Natural (I) - 1);
end loop;
return Result;
end Get_Symbol_Entry;
-----------
-- Write --
-----------
procedure Write
(File : in out File_Type;
Value : System.Storage_Elements.Storage_Element)
is
begin
if File.Saving then
Storage_IO.Write (File.Storage_File, Value);
else
File.Data.Append (Value);
end if;
end Write;
-----------
-- Write --
-----------
procedure Write
(File : in out File_Type;
Addr : System.Address;
Length : System.Storage_Elements.Storage_Count)
is
Elements : System.Storage_Elements.Storage_Array (1 .. Length);
pragma Import (Ada, Elements);
for Elements'Address use Addr;
begin
for Element of Elements loop
Write (File, Element);
end loop;
end Write;
-----------
-- Write --
-----------
procedure Write
(File : in out File_Type;
Vector : Storage_Element_Vectors.Vector)
is
begin
for Element of Vector loop
Write (File, Element);
end loop;
end Write;
----------------
-- Write_Name --
----------------
procedure Write_Name
(Vector : in out Storage_Element_Vectors.Vector;
Nm : String)
is
begin
for Ch of Nm loop
Vector.Append (Character'Pos (Ch));
end loop;
Vector.Append (0);
while Natural (Vector.Length) mod 4 /= 0 loop
Vector.Append (0);
end loop;
end Write_Name;
end WL.Files.ELF;
|