Debian Codegraph @

gcc/ada/libgnat/s-objrea.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . O B J E C T _ R E A D E R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2009-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Ada.Unchecked_Conversion;
33
34 with Interfaces.C;
35
36 with System.CRTL;
37
38 package body System.Object_Reader is
39
40 use Interfaces;
41 use Interfaces.C;
42 use System.Mmap;
43
44 SSU : constant := System.Storage_Unit;
45
46 function To_int32 is new Ada.Unchecked_Conversion (uint32, int32);
47
48 function Trim_Trailing_Nuls (Str : String) return String;
49 -- Return a copy of a string with any trailing NUL characters truncated
50
51 procedure Check_Read_Offset (S : Mapped_Stream; Size : uint32);
52 -- Check that the SIZE bytes at the current offset are still in the stream
53
54 -------------------------------------
55 -- ELF object file format handling --
56 -------------------------------------
57
58 generic
59 type uword is mod <>;
60
61 package ELF_Ops is
62
63 -- ELF version codes
64
65 ELFCLASS32 : constant := 1; -- 32 bit ELF
66 ELFCLASS64 : constant := 2; -- 64 bit ELF
67
68 -- ELF machine codes
69
70 EM_NONE : constant := 0; -- No machine
71 EM_SPARC : constant := 2; -- SUN SPARC
72 EM_386 : constant := 3; -- Intel 80386
73 EM_MIPS : constant := 8; -- MIPS RS3000 Big-Endian
74 EM_MIPS_RS3_LE : constant := 10; -- MIPS RS3000 Little-Endian
75 EM_SPARC32PLUS : constant := 18; -- Sun SPARC 32+
76 EM_PPC : constant := 20; -- PowerPC
77 EM_PPC64 : constant := 21; -- PowerPC 64-bit
78 EM_S390 : constant := 22; -- IBM S/390
79 EM_ARM : constant := 40; -- ARM
80 EM_SPARCV9 : constant := 43; -- SPARC v9 64-bit
81 EM_IA_64 : constant := 50; -- Intel Merced
82 EM_X86_64 : constant := 62; -- AMD x86-64 architecture
83 EM_AARCH64 : constant := 183; -- Aarch64
84 EM_RISCV : constant := 243; -- RISC-V
85
86 EN_NIDENT : constant := 16;
87
88 type E_Ident_Type is array (0 .. EN_NIDENT - 1) of uint8;
89
90 type Header is record
91 E_Ident : E_Ident_Type; -- Magic number and other info
92 E_Type : uint16; -- Object file type
93 E_Machine : uint16; -- Architecture
94 E_Version : uint32; -- Object file version
95 E_Entry : uword; -- Entry point virtual address
96 E_Phoff : uword; -- Program header table file offset
97 E_Shoff : uword; -- Section header table file offset
98 E_Flags : uint32; -- Processor-specific flags
99 E_Ehsize : uint16; -- ELF header size in bytes
100 E_Phentsize : uint16; -- Program header table entry size
101 E_Phnum : uint16; -- Program header table entry count
102 E_Shentsize : uint16; -- Section header table entry size
103 E_Shnum : uint16; -- Section header table entry count
104 E_Shstrndx : uint16; -- Section header string table index
105 end record;
106
107 type Section_Header is record
108 Sh_Name : uint32; -- Section name string table index
109 Sh_Type : uint32; -- Section type
110 Sh_Flags : uword; -- Section flags
111 Sh_Addr : uword; -- Section virtual addr at execution
112 Sh_Offset : uword; -- Section file offset
113 Sh_Size : uword; -- Section size in bytes
114 Sh_Link : uint32; -- Link to another section
115 Sh_Info : uint32; -- Additional section information
116 Sh_Addralign : uword; -- Section alignment
117 Sh_Entsize : uword; -- Entry size if section holds table
118 end record;
119
120 SHF_ALLOC : constant := 2;
121 SHF_EXECINSTR : constant := 4;
122
123 type Symtab_Entry32 is record
124 St_Name : uint32; -- Name (string table index)
125 St_Value : uint32; -- Value
126 St_Size : uint32; -- Size in bytes
127 St_Info : uint8; -- Type and binding attributes
128 St_Other : uint8; -- Undefined
129 St_Shndx : uint16; -- Defining section
130 end record;
131
132 type Symtab_Entry64 is record
133 St_Name : uint32; -- Name (string table index)
134 St_Info : uint8; -- Type and binding attributes
135 St_Other : uint8; -- Undefined
136 St_Shndx : uint16; -- Defining section
137 St_Value : uint64; -- Value
138 St_Size : uint64; -- Size in bytes
139 end record;
140
141 function Read_Header (F : in out Mapped_Stream) return Header;
142 -- Read a header from an ELF format object
143
144 function First_Symbol
145 (Obj : in out ELF_Object_File) return Object_Symbol;
146 -- Return the first element in the symbol table, or Null_Symbol if the
147 -- symbol table is empty.
148
149 function Read_Symbol
150 (Obj : in out ELF_Object_File;
151 Off : Offset) return Object_Symbol;
152 -- Read a symbol at offset Off
153
154 function Name
155 (Obj : in out ELF_Object_File;
156 Sym : Object_Symbol) return String_Ptr_Len;
157 -- Return the name of the symbol
158
159 function Name
160 (Obj : in out ELF_Object_File;
161 Sec : Object_Section) return String;
162 -- Return the name of a section
163
164 function Get_Section
165 (Obj : in out ELF_Object_File;
166 Shnum : uint32) return Object_Section;
167 -- Fetch a section by index from zero
168
169 function Initialize
170 (F : Mapped_File;
171 Hdr : Header;
172 In_Exception : Boolean) return ELF_Object_File;
173 -- Initialize an object file
174
175 end ELF_Ops;
176
177 -----------------------------------
178 -- PECOFF object format handling --
179 -----------------------------------
180
181 package PECOFF_Ops is
182
183 -- Constants and data layout are taken from the document "Microsoft
184 -- Portable Executable and Common Object File Format Specification"
185 -- Revision 8.1.
186
187 Signature_Loc_Offset : constant := 16#3C#;
188 -- Offset of pointer to the file signature
189
190 Size_Of_Standard_Header_Fields : constant := 16#18#;
191 -- Length in bytes of the standard header record
192
193 Function_Symbol_Type : constant := 16#20#;
194 -- Type field value indicating a symbol refers to a function
195
196 Not_Function_Symbol_Type : constant := 16#00#;
197 -- Type field value indicating a symbol does not refer to a function
198
199 type Magic_Array is array (0 .. 3) of uint8;
200 -- Array of magic numbers from the header
201
202 -- Magic numbers for PECOFF variants
203
204 VARIANT_PE32 : constant := 16#010B#;
205 VARIANT_PE32_PLUS : constant := 16#020B#;
206
207 -- PECOFF machine codes
208
209 IMAGE_FILE_MACHINE_I386 : constant := 16#014C#;
210 IMAGE_FILE_MACHINE_IA64 : constant := 16#0200#;
211 IMAGE_FILE_MACHINE_AMD64 : constant := 16#8664#;
212
213 -- PECOFF Data layout
214
215 type Header is record
216 Magics : Magic_Array;
217 Machine : uint16;
218 NumberOfSections : uint16;
219 TimeDateStamp : uint32;
220 PointerToSymbolTable : uint32;
221 NumberOfSymbols : uint32;
222 SizeOfOptionalHeader : uint16;
223 Characteristics : uint16;
224 Variant : uint16;
225 end record;
226 pragma Pack (Header);
227
228 type Optional_Header_PE32 is record
229 Magic : uint16;
230 MajorLinkerVersion : uint8;
231 MinorLinkerVersion : uint8;
232 SizeOfCode : uint32;
233 SizeOfInitializedData : uint32;
234 SizeOfUninitializedData : uint32;
235 AddressOfEntryPoint : uint32;
236 BaseOfCode : uint32;
237 BaseOfData : uint32; -- Note: not in PE32+
238 ImageBase : uint32;
239 SectionAlignment : uint32;
240 FileAlignment : uint32;
241 MajorOperatingSystemVersion : uint16;
242 MinorOperationSystemVersion : uint16;
243 MajorImageVersion : uint16;
244 MinorImageVersion : uint16;
245 MajorSubsystemVersion : uint16;
246 MinorSubsystemVersion : uint16;
247 Win32VersionValue : uint32;
248 SizeOfImage : uint32;
249 SizeOfHeaders : uint32;
250 Checksum : uint32;
251 Subsystem : uint16;
252 DllCharacteristics : uint16;
253 SizeOfStackReserve : uint32;
254 SizeOfStackCommit : uint32;
255 SizeOfHeapReserve : uint32;
256 SizeOfHeapCommit : uint32;
257 LoaderFlags : uint32;
258 NumberOfRvaAndSizes : uint32;
259 end record;
260 pragma Pack (Optional_Header_PE32);
261 pragma Assert (Optional_Header_PE32'Size = 96 * SSU);
262
263 type Optional_Header_PE64 is record
264 Magic : uint16;
265 MajorLinkerVersion : uint8;
266 MinorLinkerVersion : uint8;
267 SizeOfCode : uint32;
268 SizeOfInitializedData : uint32;
269 SizeOfUninitializedData : uint32;
270 AddressOfEntryPoint : uint32;
271 BaseOfCode : uint32;
272 ImageBase : uint64;
273 SectionAlignment : uint32;
274 FileAlignment : uint32;
275 MajorOperatingSystemVersion : uint16;
276 MinorOperationSystemVersion : uint16;
277 MajorImageVersion : uint16;
278 MinorImageVersion : uint16;
279 MajorSubsystemVersion : uint16;
280 MinorSubsystemVersion : uint16;
281 Win32VersionValue : uint32;
282 SizeOfImage : uint32;
283 SizeOfHeaders : uint32;
284 Checksum : uint32;
285 Subsystem : uint16;
286 DllCharacteristics : uint16;
287 SizeOfStackReserve : uint64;
288 SizeOfStackCommit : uint64;
289 SizeOfHeapReserve : uint64;
290 SizeOfHeapCommit : uint64;
291 LoaderFlags : uint32;
292 NumberOfRvaAndSizes : uint32;
293 end record;
294 pragma Pack (Optional_Header_PE64);
295 pragma Assert (Optional_Header_PE64'Size = 112 * SSU);
296
297 subtype Name_Str is String (1 .. 8);
298
299 type Section_Header is record
300 Name : Name_Str;
301 VirtualSize : uint32;
302 VirtualAddress : uint32;
303 SizeOfRawData : uint32;
304 PointerToRawData : uint32;
305 PointerToRelocations : uint32;
306 PointerToLinenumbers : uint32;
307 NumberOfRelocations : uint16;
308 NumberOfLinenumbers : uint16;
309 Characteristics : uint32;
310 end record;
311 pragma Pack (Section_Header);
312
313 IMAGE_SCN_CNT_CODE : constant := 16#0020#;
314
315 type Symtab_Entry is record
316 Name : Name_Str;
317 Value : uint32;
318 SectionNumber : int16;
319 TypeField : uint16;
320 StorageClass : uint8;
321 NumberOfAuxSymbols : uint8;
322 end record;
323 pragma Pack (Symtab_Entry);
324
325 type Auxent_Section is record
326 Length : uint32;
327 NumberOfRelocations : uint16;
328 NumberOfLinenumbers : uint16;
329 CheckSum : uint32;
330 Number : uint16;
331 Selection : uint8;
332 Unused1 : uint8;
333 Unused2 : uint8;
334 Unused3 : uint8;
335 end record;
336
337 for Auxent_Section'Size use 18 * 8;
338
339 function Read_Header (F : in out Mapped_Stream) return Header;
340 -- Read the object file header
341
342 function First_Symbol
343 (Obj : in out PECOFF_Object_File) return Object_Symbol;
344 -- Return the first element in the symbol table, or Null_Symbol if the
345 -- symbol table is empty.
346
347 function Read_Symbol
348 (Obj : in out PECOFF_Object_File;
349 Off : Offset) return Object_Symbol;
350 -- Read a symbol at offset Off
351
352 function Name
353 (Obj : in out PECOFF_Object_File;
354 Sym : Object_Symbol) return String_Ptr_Len;
355 -- Return the name of the symbol
356
357 function Name
358 (Obj : in out PECOFF_Object_File;
359 Sec : Object_Section) return String;
360 -- Return the name of a section
361
362 function Get_Section
363 (Obj : in out PECOFF_Object_File;
364 Index : uint32) return Object_Section;
365 -- Fetch a section by index from zero
366
367 function Initialize
368 (F : Mapped_File;
369 Hdr : Header;
370 In_Exception : Boolean) return PECOFF_Object_File;
371 -- Initialize an object file
372
373 end PECOFF_Ops;
374
375 -------------------------------------
376 -- XCOFF-32 object format handling --
377 -------------------------------------
378
379 package XCOFF32_Ops is
380
381 -- XCOFF Data layout
382
383 type Header is record
384 f_magic : uint16;
385 f_nscns : uint16;
386 f_timdat : uint32;
387 f_symptr : uint32;
388 f_nsyms : uint32;
389 f_opthdr : uint16;
390 f_flags : uint16;
391 end record;
392
393 type Auxiliary_Header is record
394 o_mflag : uint16;
395 o_vstamp : uint16;
396 o_tsize : uint32;
397 o_dsize : uint32;
398 o_bsize : uint32;
399 o_entry : uint32;
400 o_text_start : uint32;
401 o_data_start : uint32;
402 o_toc : uint32;
403 o_snentry : uint16;
404 o_sntext : uint16;
405 o_sndata : uint16;
406 o_sntoc : uint16;
407 o_snloader : uint16;
408 o_snbss : uint16;
409 o_algntext : uint16;
410 o_algndata : uint16;
411 o_modtype : uint16;
412 o_cpuflag : uint8;
413 o_cputype : uint8;
414 o_maxstack : uint32;
415 o_maxdata : uint32;
416 o_debugger : uint32;
417 o_flags : uint8;
418 o_sntdata : uint16;
419 o_sntbss : uint16;
420 end record;
421 pragma Unreferenced (Auxiliary_Header);
422 -- Not used, but not removed (just in case)
423
424 subtype Name_Str is String (1 .. 8);
425
426 type Section_Header is record
427 s_name : Name_Str;
428 s_paddr : uint32;
429 s_vaddr : uint32;
430 s_size : uint32;
431 s_scnptr : uint32;
432 s_relptr : uint32;
433 s_lnnoptr : uint32;
434 s_nreloc : uint16;
435 s_nlnno : uint16;
436 s_flags : uint32;
437 end record;
438 pragma Pack (Section_Header);
439
440 STYP_TEXT : constant := 16#0020#;
441
442 type Symbol_Entry is record
443 n_name : Name_Str;
444 n_value : uint32;
445 n_scnum : uint16;
446 n_type : uint16;
447 n_sclass : uint8;
448 n_numaux : uint8;
449 end record;
450 for Symbol_Entry'Size use 18 * 8;
451
452 type Aux_Entry is record
453 x_scnlen : uint32;
454 x_parmhash : uint32;
455 x_snhash : uint16;
456 x_smtyp : uint8;
457 x_smclass : uint8;
458 x_stab : uint32;
459 x_snstab : uint16;
460 end record;
461 for Aux_Entry'Size use 18 * 8;
462 pragma Pack (Aux_Entry);
463
464 C_EXT : constant := 2;
465 C_HIDEXT : constant := 107;
466 C_WEAKEXT : constant := 111;
467
468 XTY_LD : constant := 2;
469 -- Magic constant should be documented, especially since it's changed???
470
471 function Read_Header (F : in out Mapped_Stream) return Header;
472 -- Read the object file header
473
474 function First_Symbol
475 (Obj : in out XCOFF32_Object_File) return Object_Symbol;
476 -- Return the first element in the symbol table, or Null_Symbol if the
477 -- symbol table is empty.
478
479 function Read_Symbol
480 (Obj : in out XCOFF32_Object_File;
481 Off : Offset) return Object_Symbol;
482 -- Read a symbol at offset Off
483
484 function Name
485 (Obj : in out XCOFF32_Object_File;
486 Sym : Object_Symbol) return String_Ptr_Len;
487 -- Return the name of the symbol
488
489 function Name
490 (Obj : in out XCOFF32_Object_File;
491 Sec : Object_Section) return String;
492 -- Return the name of a section
493
494 function Initialize
495 (F : Mapped_File;
496 Hdr : Header;
497 In_Exception : Boolean) return XCOFF32_Object_File;
498 -- Initialize an object file
499
500 function Get_Section
501 (Obj : in out XCOFF32_Object_File;
502 Index : uint32) return Object_Section;
503 -- Fetch a section by index from zero
504
505 end XCOFF32_Ops;
506
507 -------------
508 -- ELF_Ops --
509 -------------
510
511 package body ELF_Ops is
512
513 function Get_String_Table (Obj : in out ELF_Object_File)
514 return Object_Section;
515 -- Fetch the section containing the string table
516
517 function Get_Symbol_Table (Obj : in out ELF_Object_File)
518 return Object_Section;
519 -- Fetch the section containing the symbol table
520
521 function Read_Section_Header
522 (Obj : in out ELF_Object_File;
523 Shnum : uint32) return Section_Header;
524 -- Read the header for an ELF format object section indexed from zero
525
526 ------------------
527 -- First_Symbol --
528 ------------------
529
530 function First_Symbol
531 (Obj : in out ELF_Object_File) return Object_Symbol
532 is
533 begin
534 if Obj.Symtab_Last = 0 then
535 return Null_Symbol;
536 else
537 return Read_Symbol (Obj, 0);
538 end if;
539 end First_Symbol;
540
541 -----------------
542 -- Get_Section --
543 -----------------
544
545 function Get_Section
546 (Obj : in out ELF_Object_File;
547 Shnum : uint32) return Object_Section
548 is
549 SHdr : constant Section_Header := Read_Section_Header (Obj, Shnum);
550
551 begin
552 return (Shnum,
553 Offset (SHdr.Sh_Offset),
554 uint64 (SHdr.Sh_Addr),
555 uint64 (SHdr.Sh_Size),
556 (SHdr.Sh_Flags and SHF_EXECINSTR) /= 0);
557 end Get_Section;
558
559 ------------------------
560 -- Get_String_Table --
561 ------------------------
562
563 function Get_String_Table
564 (Obj : in out ELF_Object_File) return Object_Section
565 is
566 begin
567 -- All cases except MIPS IRIX, string table located in .strtab
568
569 if Obj.Arch /= MIPS then
570 return Get_Section (Obj, ".strtab");
571
572 -- On IRIX only .dynstr is available
573
574 else
575 return Get_Section (Obj, ".dynstr");
576 end if;
577 end Get_String_Table;
578
579 ------------------------
580 -- Get_Symbol_Table --
581 ------------------------
582
583 function Get_Symbol_Table
584 (Obj : in out ELF_Object_File) return Object_Section
585 is
586 begin
587 -- All cases except MIPS IRIX, symbol table located in .symtab
588
589 if Obj.Arch /= MIPS then
590 return Get_Section (Obj, ".symtab");
591
592 -- On IRIX, symbol table located somewhere other than .symtab
593
594 else
595 return Get_Section (Obj, ".dynsym");
596 end if;
597 end Get_Symbol_Table;
598
599 ----------------
600 -- Initialize --
601 ----------------
602
603 function Initialize
604 (F : Mapped_File;
605 Hdr : Header;
606 In_Exception : Boolean) return ELF_Object_File
607 is
608 Res : ELF_Object_File
609 (Format => (case uword'Size is
610 when 64 => ELF64,
611 when 32 => ELF32,
612 when others => raise Program_Error));
613 Sec : Object_Section;
614 begin
615 Res.MF := F;
616 Res.In_Exception := In_Exception;
617 Res.Num_Sections := uint32 (Hdr.E_Shnum);
618
619 case Hdr.E_Machine is
620 when EM_SPARC
621 | EM_SPARC32PLUS
622 =>
623 Res.Arch := SPARC;
624
625 when EM_SPARCV9 =>
626 Res.Arch := SPARC64;
627
628 when EM_MIPS
629 | EM_MIPS_RS3_LE
630 =>
631 Res.Arch := MIPS;
632
633 when EM_PPC =>
634 Res.Arch := PPC;
635
636 when EM_PPC64 =>
637 Res.Arch := PPC64;
638
639 when EM_S390 =>
640 Res.Arch := S390;
641
642 when EM_386 =>
643 Res.Arch := i386;
644
645 when EM_IA_64 =>
646 Res.Arch := IA64;
647
648 when EM_X86_64 =>
649 Res.Arch := x86_64;
650
651 when EM_ARM =>
652 Res.Arch := ARM;
653
654 when EM_AARCH64 =>
655 Res.Arch := AARCH64;
656
657 when EM_RISCV =>
658 Res.Arch := RISCV;
659
660 when others =>
661 raise Format_Error with "unrecognized architecture";
662 end case;
663
664 -- Map section table and section string table
665 Res.Sectab_Stream := Create_Stream
666 (F, File_Size (Hdr.E_Shoff),
667 File_Size (Hdr.E_Shnum) * File_Size (Hdr.E_Shentsize));
668 Sec := Get_Section (Res, uint32 (Hdr.E_Shstrndx));
669 Res.Secstr_Stream := Create_Stream (Res, Sec);
670
671 -- Map symbol and string table
672 Sec := Get_Symbol_Table (Res);
673 Res.Symtab_Stream := Create_Stream (Res, Sec);
674 Res.Symtab_Last := Offset (Sec.Size);
675
676 Sec := Get_String_Table (Res);
677 Res.Symstr_Stream := Create_Stream (Res, Sec);
678
679 return Res;
680 end Initialize;
681
682 -----------------
683 -- Read_Header --
684 -----------------
685
686 function Read_Header (F : in out Mapped_Stream) return Header is
687 Hdr : Header;
688
689 begin
690 Seek (F, 0);
691 Read_Raw (F, Hdr'Address, uint32 (Hdr'Size / SSU));
692 return Hdr;
693 end Read_Header;
694
695 -------------------------
696 -- Read_Section_Header --
697 -------------------------
698
699 function Read_Section_Header
700 (Obj : in out ELF_Object_File;
701 Shnum : uint32) return Section_Header
702 is
703 Shdr : Section_Header;
704
705 begin
706 Seek (Obj.Sectab_Stream, Offset (Shnum * Section_Header'Size / SSU));
707 Read_Raw (Obj.Sectab_Stream, Shdr'Address, Section_Header'Size / SSU);
708 return Shdr;
709 end Read_Section_Header;
710
711 -----------------
712 -- Read_Symbol --
713 -----------------
714
715 function Read_Symbol
716 (Obj : in out ELF_Object_File;
717 Off : Offset) return Object_Symbol
718 is
719 ST_Entry32 : Symtab_Entry32;
720 ST_Entry64 : Symtab_Entry64;
721 Res : Object_Symbol;
722
723 begin
724 Seek (Obj.Symtab_Stream, Off);
725
726 case uword'Size is
727 when 32 =>
728 Read_Raw (Obj.Symtab_Stream, ST_Entry32'Address,
729 uint32 (ST_Entry32'Size / SSU));
730 Res := (Off,
731 Off + ST_Entry32'Size / SSU,
732 uint64 (ST_Entry32.St_Value),
733 uint64 (ST_Entry32.St_Size));
734
735 when 64 =>
736 Read_Raw (Obj.Symtab_Stream, ST_Entry64'Address,
737 uint32 (ST_Entry64'Size / SSU));
738 Res := (Off,
739 Off + ST_Entry64'Size / SSU,
740 ST_Entry64.St_Value,
741 ST_Entry64.St_Size);
742
743 when others =>
744 raise Program_Error;
745 end case;
746
747 return Res;
748 end Read_Symbol;
749
750 ----------
751 -- Name --
752 ----------
753
754 function Name
755 (Obj : in out ELF_Object_File;
756 Sec : Object_Section) return String
757 is
758 SHdr : Section_Header;
759
760 begin
761 SHdr := Read_Section_Header (Obj, Sec.Num);
762 return Offset_To_String (Obj.Secstr_Stream, Offset (SHdr.Sh_Name));
763 end Name;
764
765 function Name
766 (Obj : in out ELF_Object_File;
767 Sym : Object_Symbol) return String_Ptr_Len
768 is
769 ST_Entry32 : Symtab_Entry32;
770 ST_Entry64 : Symtab_Entry64;
771 Name_Off : Offset;
772
773 begin
774 -- Test that this symbol is not null
775
776 if Sym = Null_Symbol then
777 return (null, 0);
778 end if;
779
780 -- Read the symbol table entry
781
782 Seek (Obj.Symtab_Stream, Sym.Off);
783
784 case uword'Size is
785 when 32 =>
786 Read_Raw (Obj.Symtab_Stream, ST_Entry32'Address,
787 uint32 (ST_Entry32'Size / SSU));
788 Name_Off := Offset (ST_Entry32.St_Name);
789
790 when 64 =>
791 Read_Raw (Obj.Symtab_Stream, ST_Entry64'Address,
792 uint32 (ST_Entry64'Size / SSU));
793 Name_Off := Offset (ST_Entry64.St_Name);
794
795 when others =>
796 raise Program_Error;
797 end case;
798
799 -- Fetch the name from the string table
800
801 Seek (Obj.Symstr_Stream, Name_Off);
802 return Read (Obj.Symstr_Stream);
803 end Name;
804
805 end ELF_Ops;
806
807 package ELF32_Ops is new ELF_Ops (uint32);
808 package ELF64_Ops is new ELF_Ops (uint64);
809
810 ----------------
811 -- PECOFF_Ops --
812 ----------------
813
814 package body PECOFF_Ops is
815
816 function Decode_Name
817 (Obj : in out PECOFF_Object_File;
818 Raw_Name : String) return String;
819 -- A section name is an 8 byte field padded on the right with null
820 -- characters, or a '\' followed by an ASCII decimal string indicating
821 -- an offset in to the string table. This routine decodes this
822
823 function Get_Section_Virtual_Address
824 (Obj : in out PECOFF_Object_File;
825 Index : uint32) return uint64;
826 -- Fetch the address at which a section is loaded
827
828 function Read_Section_Header
829 (Obj : in out PECOFF_Object_File;
830 Index : uint32) return Section_Header;
831 -- Read a header from section table
832
833 function String_Table
834 (Obj : in out PECOFF_Object_File;
835 Index : Offset) return String;
836 -- Return an entry from the string table
837
838 -----------------
839 -- Decode_Name --
840 -----------------
841
842 function Decode_Name
843 (Obj : in out PECOFF_Object_File;
844 Raw_Name : String) return String
845 is
846 Name_Or_Ref : constant String := Trim_Trailing_Nuls (Raw_Name);
847 Off : Offset;
848
849 begin
850 -- We should never find a symbol with a zero length name. If we do it
851 -- probably means we are not parsing the symbol table correctly. If
852 -- this happens we raise a fatal error.
853
854 if Name_Or_Ref'Length = 0 then
855 raise Format_Error with
856 "found zero length symbol in symbol table";
857 end if;
858
859 if Name_Or_Ref (1) /= '/' then
860 return Name_Or_Ref;
861 else
862 Off := Offset'Value (Name_Or_Ref (2 .. Name_Or_Ref'Last));
863 return String_Table (Obj, Off);
864 end if;
865 end Decode_Name;
866
867 ------------------
868 -- First_Symbol --
869 ------------------
870
871 function First_Symbol
872 (Obj : in out PECOFF_Object_File) return Object_Symbol
873 is
874 begin
875 -- Return Null_Symbol in the case that the symbol table is empty
876
877 if Obj.Symtab_Last = 0 then
878 return Null_Symbol;
879 end if;
880
881 return Read_Symbol (Obj, 0);
882 end First_Symbol;
883
884 -----------------
885 -- Get_Section --
886 -----------------
887
888 function Get_Section
889 (Obj : in out PECOFF_Object_File;
890 Index : uint32) return Object_Section
891 is
892 Sec : constant Section_Header := Read_Section_Header (Obj, Index);
893
894 begin
895 -- Use VirtualSize instead of SizeOfRawData. The latter is rounded to
896 -- the page size, so it may add garbage to the content. On the other
897 -- side, the former may be larger than the latter in case of 0
898 -- padding.
899
900 return (Index,
901 Offset (Sec.PointerToRawData),
902 uint64 (Sec.VirtualAddress) + Obj.ImageBase,
903 uint64 (Sec.VirtualSize),
904 (Sec.Characteristics and IMAGE_SCN_CNT_CODE) /= 0);
905 end Get_Section;
906
907 ---------------------------------
908 -- Get_Section_Virtual_Address --
909 ---------------------------------
910
911 function Get_Section_Virtual_Address
912 (Obj : in out PECOFF_Object_File;
913 Index : uint32) return uint64
914 is
915 Sec : Section_Header;
916
917 begin
918 -- Try cache
919
920 if Index = Obj.GSVA_Sec then
921 return Obj.GSVA_Addr;
922 end if;
923
924 Obj.GSVA_Sec := Index;
925 Sec := Read_Section_Header (Obj, Index);
926 Obj.GSVA_Addr := Obj.ImageBase + uint64 (Sec.VirtualAddress);
927 return Obj.GSVA_Addr;
928 end Get_Section_Virtual_Address;
929
930 ----------------
931 -- Initialize --
932 ----------------
933
934 function Initialize
935 (F : Mapped_File;
936 Hdr : Header;
937 In_Exception : Boolean) return PECOFF_Object_File
938 is
939 Res : PECOFF_Object_File
940 (Format => (case Hdr.Variant is
941 when PECOFF_Ops.VARIANT_PE32 => PECOFF,
942 when PECOFF_Ops.VARIANT_PE32_PLUS => PECOFF_PLUS,
943 when others => raise Program_Error
944 with "unrecognized PECOFF variant"));
945 Symtab_Size : constant Offset :=
946 Offset (Hdr.NumberOfSymbols) * (Symtab_Entry'Size / SSU);
947 Strtab_Size : uint32;
948 Hdr_Offset : Offset;
949 Opt_Offset : File_Size;
950 Opt_Stream : Mapped_Stream;
951
952 begin
953 Res.MF := F;
954 Res.In_Exception := In_Exception;
955
956 case Hdr.Machine is
957 when PECOFF_Ops.IMAGE_FILE_MACHINE_I386 =>
958 Res.Arch := i386;
959 when PECOFF_Ops.IMAGE_FILE_MACHINE_IA64 =>
960 Res.Arch := IA64;
961 when PECOFF_Ops.IMAGE_FILE_MACHINE_AMD64 =>
962 Res.Arch := x86_64;
963 when others =>
964 raise Format_Error with "unrecognized architecture";
965 end case;
966
967 Res.Num_Sections := uint32 (Hdr.NumberOfSections);
968
969 -- Map symbol table and the first following word (which is the length
970 -- of the string table).
971
972 Res.Symtab_Last := Symtab_Size;
973 Res.Symtab_Stream := Create_Stream
974 (F,
975 File_Size (Hdr.PointerToSymbolTable),
976 File_Size (Symtab_Size + 4));
977
978 -- Map string table. The first 4 bytes are the length of the string
979 -- table and are part of it.
980
981 Seek (Res.Symtab_Stream, Symtab_Size);
982 Strtab_Size := Read (Res.Symtab_Stream);
983 Res.Symstr_Stream := Create_Stream
984 (F,
985 File_Size (Hdr.PointerToSymbolTable) + File_Size (Symtab_Size),
986 File_Size (Strtab_Size));
987
988 -- Map section table
989
990 Opt_Stream := Create_Stream (Res.MF, Signature_Loc_Offset, 4);
991 Hdr_Offset := Offset (uint32'(Read (Opt_Stream)));
992 Close (Opt_Stream);
993 Res.Sectab_Stream := Create_Stream
994 (F,
995 File_Size (Hdr_Offset +
996 Size_Of_Standard_Header_Fields +
997 Offset (Hdr.SizeOfOptionalHeader)),
998 File_Size (Res.Num_Sections)
999 * File_Size (Section_Header'Size / SSU));
1000
1001 -- Read optional header and extract image base
1002
1003 Opt_Offset := File_Size (Hdr_Offset + Size_Of_Standard_Header_Fields);
1004
1005 if Res.Format = PECOFF then
1006 declare
1007 Opt_32 : Optional_Header_PE32;
1008 begin
1009 Opt_Stream := Create_Stream
1010 (Res.MF, Opt_Offset, Opt_32'Size / SSU);
1011 Read_Raw
1012 (Opt_Stream, Opt_32'Address, uint32 (Opt_32'Size / SSU));
1013 Res.ImageBase := uint64 (Opt_32.ImageBase);
1014 Close (Opt_Stream);
1015 end;
1016
1017 else
1018 declare
1019 Opt_64 : Optional_Header_PE64;
1020 begin
1021 Opt_Stream := Create_Stream
1022 (Res.MF, Opt_Offset, Opt_64'Size / SSU);
1023 Read_Raw
1024 (Opt_Stream, Opt_64'Address, uint32 (Opt_64'Size / SSU));
1025 Res.ImageBase := Opt_64.ImageBase;
1026 Close (Opt_Stream);
1027 end;
1028 end if;
1029
1030 return Res;
1031 end Initialize;
1032
1033 -----------------
1034 -- Read_Symbol --
1035 -----------------
1036
1037 function Read_Symbol
1038 (Obj : in out PECOFF_Object_File;
1039 Off : Offset) return Object_Symbol
1040 is
1041 ST_Entry : Symtab_Entry;
1042 ST_Last : Symtab_Entry;
1043 Aux_Entry : Auxent_Section;
1044 Sz : constant Offset := ST_Entry'Size / SSU;
1045 Result : Object_Symbol;
1046 Noff : Offset;
1047 Sym_Off : Offset;
1048
1049 begin
1050 -- Seek to the successor of Prev
1051
1052 Noff := Off;
1053
1054 loop
1055 Sym_Off := Noff;
1056
1057 Seek (Obj.Symtab_Stream, Sym_Off);
1058 Read_Raw (Obj.Symtab_Stream, ST_Entry'Address, uint32 (Sz));
1059
1060 -- Skip AUX entries
1061
1062 Noff := Noff + Offset (1 + ST_Entry.NumberOfAuxSymbols) * Sz;
1063
1064 exit when ST_Entry.TypeField = Function_Symbol_Type
1065 and then ST_Entry.SectionNumber > 0;
1066
1067 if Noff >= Obj.Symtab_Last then
1068 return Null_Symbol;
1069 end if;
1070 end loop;
1071
1072 -- Construct the symbol
1073
1074 Result :=
1075 (Off => Sym_Off,
1076 Next => Noff,
1077 Value => uint64 (ST_Entry.Value),
1078 Size => 0);
1079
1080 -- Set the size as accurately as possible
1081
1082 -- The size of a symbol is not directly available so we try scanning
1083 -- to the next function and assuming the code ends there.
1084
1085 loop
1086 -- Read symbol and AUX entries
1087
1088 Sym_Off := Noff;
1089 Seek (Obj.Symtab_Stream, Sym_Off);
1090 Read_Raw (Obj.Symtab_Stream, ST_Last'Address, uint32 (Sz));
1091
1092 for I in 1 .. ST_Last.NumberOfAuxSymbols loop
1093 Read_Raw (Obj.Symtab_Stream, Aux_Entry'Address, uint32 (Sz));
1094 end loop;
1095
1096 Noff := Noff + Offset (1 + ST_Last.NumberOfAuxSymbols) * Sz;
1097
1098 if ST_Last.TypeField = Function_Symbol_Type then
1099 if ST_Last.SectionNumber = ST_Entry.SectionNumber
1100 and then ST_Last.Value >= ST_Entry.Value
1101 then
1102 -- Symbol is a function past ST_Entry
1103
1104 Result.Size := uint64 (ST_Last.Value - ST_Entry.Value);
1105
1106 else
1107 -- Not correlated function
1108
1109 Result.Next := Sym_Off;
1110 end if;
1111
1112 exit;
1113
1114 elsif ST_Last.SectionNumber = ST_Entry.SectionNumber
1115 and then ST_Last.TypeField = Not_Function_Symbol_Type
1116 and then ST_Last.StorageClass = 3
1117 and then ST_Last.NumberOfAuxSymbols = 1
1118 then
1119 -- Symbol is a section
1120
1121 Result.Size := uint64 (ST_Last.Value + Aux_Entry.Length
1122 - ST_Entry.Value);
1123 Result.Next := Noff;
1124 exit;
1125 end if;
1126
1127 exit when Noff >= Obj.Symtab_Last;
1128 end loop;
1129
1130 -- Relocate the address
1131
1132 Result.Value :=
1133 Result.Value + Get_Section_Virtual_Address
1134 (Obj, uint32 (ST_Entry.SectionNumber - 1));
1135
1136 return Result;
1137 end Read_Symbol;
1138
1139 ------------------
1140 -- Read_Header --
1141 ------------------
1142
1143 function Read_Header (F : in out Mapped_Stream) return Header is
1144 Hdr : Header;
1145 Off : int32;
1146
1147 begin
1148 -- Skip the MSDOS stub, and seek directly to the file offset
1149
1150 Seek (F, Signature_Loc_Offset);
1151 Off := Read (F);
1152
1153 -- Read the COFF file header
1154
1155 Seek (F, Offset (Off));
1156 Read_Raw (F, Hdr'Address, uint32 (Hdr'Size / SSU));
1157 return Hdr;
1158 end Read_Header;
1159
1160 -------------------------
1161 -- Read_Section_Header --
1162 -------------------------
1163
1164 function Read_Section_Header
1165 (Obj : in out PECOFF_Object_File;
1166 Index : uint32) return Section_Header
1167 is
1168 Sec : Section_Header;
1169 begin
1170 Seek (Obj.Sectab_Stream, Offset (Index * Section_Header'Size / SSU));
1171 Read_Raw (Obj.Sectab_Stream, Sec'Address, Section_Header'Size / SSU);
1172 return Sec;
1173 end Read_Section_Header;
1174
1175 ----------
1176 -- Name --
1177 ----------
1178
1179 function Name
1180 (Obj : in out PECOFF_Object_File;
1181 Sec : Object_Section) return String
1182 is
1183 Shdr : constant Section_Header := Read_Section_Header (Obj, Sec.Num);
1184 begin
1185 return Decode_Name (Obj, Shdr.Name);
1186 end Name;
1187
1188 -------------------
1189 -- String_Table --
1190 -------------------
1191
1192 function String_Table
1193 (Obj : in out PECOFF_Object_File;
1194 Index : Offset) return String
1195 is
1196 begin
1197 -- An index of zero is used to represent an empty string, as the
1198 -- first word of the string table is specified to contain the length
1199 -- of the table rather than its contents.
1200
1201 if Index = 0 then
1202 return "";
1203
1204 else
1205 return Offset_To_String (Obj.Symstr_Stream, Index);
1206 end if;
1207 end String_Table;
1208
1209 ----------
1210 -- Name --
1211 ----------
1212
1213 function Name
1214 (Obj : in out PECOFF_Object_File;
1215 Sym : Object_Symbol) return String_Ptr_Len
1216 is
1217 ST_Entry : Symtab_Entry;
1218
1219 begin
1220 Seek (Obj.Symtab_Stream, Sym.Off);
1221 Read_Raw (Obj.Symtab_Stream, ST_Entry'Address, ST_Entry'Size / SSU);
1222
1223 declare
1224 -- Symbol table entries are packed and Table_Entry.Name may not be
1225 -- sufficiently aligned to interpret as a 32 bit word, so it is
1226 -- copied to a temporary
1227
1228 Aligned_Name : Name_Str := ST_Entry.Name;
1229 for Aligned_Name'Alignment use 4;
1230
1231 First_Word : uint32;
1232 pragma Import (Ada, First_Word);
1233 -- Suppress initialization in Normalized_Scalars mode
1234 for First_Word'Address use Aligned_Name (1)'Address;
1235
1236 Second_Word : uint32;
1237 pragma Import (Ada, Second_Word);
1238 -- Suppress initialization in Normalized_Scalars mode
1239 for Second_Word'Address use Aligned_Name (5)'Address;
1240
1241 begin
1242 if First_Word = 0 then
1243 -- Second word is an offset in the symbol table
1244 if Second_Word = 0 then
1245 return (null, 0);
1246 else
1247 Seek (Obj.Symstr_Stream, int64 (Second_Word));
1248 return Read (Obj.Symstr_Stream);
1249 end if;
1250 else
1251 -- Inlined symbol name
1252 Seek (Obj.Symtab_Stream, Sym.Off);
1253 return To_String_Ptr_Len (Read (Obj.Symtab_Stream), 8);
1254 end if;
1255 end;
1256 end Name;
1257
1258 end PECOFF_Ops;
1259
1260 -----------------
1261 -- XCOFF32_Ops --
1262 -----------------
1263
1264 package body XCOFF32_Ops is
1265
1266 function Read_Section_Header
1267 (Obj : in out XCOFF32_Object_File;
1268 Index : uint32) return Section_Header;
1269 -- Read a header from section table
1270
1271 -----------------
1272 -- Read_Symbol --
1273 -----------------
1274
1275 function Read_Symbol
1276 (Obj : in out XCOFF32_Object_File;
1277 Off : Offset) return Object_Symbol
1278 is
1279 Sym : Symbol_Entry;
1280 Sz : constant Offset := Symbol_Entry'Size / SSU;
1281 Aux : Aux_Entry;
1282 Result : Object_Symbol;
1283 Noff : Offset;
1284 Sym_Off : Offset;
1285
1286 procedure Read_LD_Symbol;
1287 -- Read the next LD symbol
1288
1289 --------------------
1290 -- Read_LD_Symbol --
1291 --------------------
1292
1293 procedure Read_LD_Symbol is
1294 begin
1295 loop
1296 Sym_Off := Noff;
1297
1298 Read_Raw (Obj.Symtab_Stream, Sym'Address, uint32 (Sz));
1299
1300 Noff := Noff + Offset (1 + Sym.n_numaux) * Sz;
1301
1302 for J in 1 .. Sym.n_numaux loop
1303 Read_Raw (Obj.Symtab_Stream, Aux'Address, uint32 (Sz));
1304 end loop;
1305
1306 exit when Noff >= Obj.Symtab_Last;
1307
1308 exit when Sym.n_numaux = 1
1309 and then Sym.n_scnum /= 0
1310 and then (Sym.n_sclass = C_EXT
1311 or else Sym.n_sclass = C_HIDEXT
1312 or else Sym.n_sclass = C_WEAKEXT)
1313 and then Aux.x_smtyp = XTY_LD;
1314 end loop;
1315 end Read_LD_Symbol;
1316
1317 -- Start of processing for Read_Symbol
1318
1319 begin
1320 Seek (Obj.Symtab_Stream, Off);
1321 Noff := Off;
1322 Read_LD_Symbol;
1323
1324 if Noff >= Obj.Symtab_Last then
1325 return Null_Symbol;
1326 end if;
1327
1328 -- Construct the symbol
1329
1330 Result := (Off => Sym_Off,
1331 Next => Noff,
1332 Value => uint64 (Sym.n_value),
1333 Size => 0);
1334
1335 -- Look for the next symbol to compute the size
1336
1337 Read_LD_Symbol;
1338
1339 if Noff >= Obj.Symtab_Last then
1340 return Null_Symbol;
1341 end if;
1342
1343 Result.Size := uint64 (Sym.n_value) - Result.Value;
1344 Result.Next := Sym_Off;
1345 return Result;
1346 end Read_Symbol;
1347
1348 ------------------
1349 -- First_Symbol --
1350 ------------------
1351
1352 function First_Symbol
1353 (Obj : in out XCOFF32_Object_File) return Object_Symbol
1354 is
1355 begin
1356 -- Return Null_Symbol in the case that the symbol table is empty
1357
1358 if Obj.Symtab_Last = 0 then
1359 return Null_Symbol;
1360 end if;
1361
1362 return Read_Symbol (Obj, 0);
1363 end First_Symbol;
1364
1365 ----------------
1366 -- Initialize --
1367 ----------------
1368
1369 function Initialize
1370 (F : Mapped_File;
1371 Hdr : Header;
1372 In_Exception : Boolean) return XCOFF32_Object_File
1373 is
1374 Res : XCOFF32_Object_File (Format => XCOFF32);
1375 Strtab_Sz : uint32;
1376
1377 begin
1378 Res.MF := F;
1379 Res.In_Exception := In_Exception;
1380
1381 Res.Arch := PPC;
1382
1383 -- Map sections table
1384 Res.Num_Sections := uint32 (Hdr.f_nscns);
1385 Res.Sectab_Stream := Create_Stream
1386 (F,
1387 File_Size (Header'Size / SSU) + File_Size (Hdr.f_opthdr),
1388 File_Size (Hdr.f_nscns) * (Section_Header'Size / SSU));
1389
1390 -- Map symbols table
1391 Res.Symtab_Last := Offset (Hdr.f_nscns) * (Symbol_Entry'Size / SSU);
1392 Res.Symtab_Stream := Create_Stream
1393 (F,
1394 File_Size (Hdr.f_symptr),
1395 File_Size (Res.Symtab_Last) + 4);
1396
1397 -- Map string table
1398 Seek (Res.Symtab_Stream, Res.Symtab_Last);
1399 Strtab_Sz := Read (Res.Symtab_Stream);
1400 Res.Symstr_Stream := Create_Stream
1401 (F,
1402 File_Size (Res.Symtab_Last) + 4,
1403 File_Size (Strtab_Sz) - 4);
1404
1405 return Res;
1406 end Initialize;
1407
1408 -----------------
1409 -- Get_Section --
1410 -----------------
1411
1412 function Get_Section
1413 (Obj : in out XCOFF32_Object_File;
1414 Index : uint32) return Object_Section
1415 is
1416 Sec : constant Section_Header := Read_Section_Header (Obj, Index);
1417
1418 begin
1419 return (Index, Offset (Sec.s_scnptr),
1420 uint64 (Sec.s_vaddr),
1421 uint64 (Sec.s_size),
1422 (Sec.s_flags and STYP_TEXT) /= 0);
1423 end Get_Section;
1424
1425 -----------------
1426 -- Read_Header --
1427 -----------------
1428
1429 function Read_Header (F : in out Mapped_Stream) return Header is
1430 Hdr : Header;
1431
1432 begin
1433 Seek (F, 0);
1434 Read_Raw (F, Hdr'Address, uint32 (Hdr'Size / SSU));
1435 return Hdr;
1436 end Read_Header;
1437
1438 -------------------------
1439 -- Read_Section_Header --
1440 -------------------------
1441
1442 function Read_Section_Header
1443 (Obj : in out XCOFF32_Object_File;
1444 Index : uint32) return Section_Header
1445 is
1446 Sec : Section_Header;
1447
1448 begin
1449 -- Seek to the end of the object header
1450
1451 Seek (Obj.Sectab_Stream, Offset (Index * Section_Header'Size / SSU));
1452
1453 -- Read the section
1454
1455 Read_Raw (Obj.Sectab_Stream, Sec'Address, Section_Header'Size / SSU);
1456
1457 return Sec;
1458 end Read_Section_Header;
1459
1460 ----------
1461 -- Name --
1462 ----------
1463
1464 function Name
1465 (Obj : in out XCOFF32_Object_File;
1466 Sec : Object_Section) return String
1467 is
1468 Hdr : Section_Header;
1469
1470 begin
1471 Hdr := Read_Section_Header (Obj, Sec.Num);
1472 return Trim_Trailing_Nuls (Hdr.s_name);
1473 end Name;
1474
1475 ----------
1476 -- Name --
1477 ----------
1478
1479 function Name
1480 (Obj : in out XCOFF32_Object_File;
1481 Sym : Object_Symbol) return String_Ptr_Len
1482 is
1483 Symbol : Symbol_Entry;
1484
1485 begin
1486 Seek (Obj.Symtab_Stream, Sym.Off);
1487 Read_Raw (Obj.Symtab_Stream, Symbol'Address, Symbol'Size / SSU);
1488
1489 declare
1490 First_Word : uint32;
1491 pragma Import (Ada, First_Word);
1492 -- Suppress initialization in Normalized_Scalars mode
1493 for First_Word'Address use Symbol.n_name (1)'Address;
1494
1495 Second_Word : uint32;
1496 pragma Import (Ada, Second_Word);
1497 -- Suppress initialization in Normalized_Scalars mode
1498 for Second_Word'Address use Symbol.n_name (5)'Address;
1499
1500 begin
1501 if First_Word = 0 then
1502 if Second_Word = 0 then
1503 return (null, 0);
1504 else
1505 Seek (Obj.Symstr_Stream, int64 (Second_Word));
1506 return Read (Obj.Symstr_Stream);
1507 end if;
1508 else
1509 Seek (Obj.Symtab_Stream, Sym.Off);
1510 return To_String_Ptr_Len (Read (Obj.Symstr_Stream), 8);
1511 end if;
1512 end;
1513 end Name;
1514 end XCOFF32_Ops;
1515
1516 ----------
1517 -- Arch --
1518 ----------
1519
1520 function Arch (Obj : Object_File) return Object_Arch is
1521 begin
1522 return Obj.Arch;
1523 end Arch;
1524
1525 function Create_Stream
1526 (MF : Mapped_File;
1527 File_Offset : File_Size;
1528 File_Length : File_Size)
1529 return Mapped_Stream
1530 is
1531 Region : Mapped_Region;
1532 begin
1533 Read (MF, Region, File_Offset, File_Length, False);
1534 return (Region, 0, Offset (File_Length));
1535 end Create_Stream;
1536
1537 function Create_Stream
1538 (Obj : Object_File;
1539 Sec : Object_Section) return Mapped_Stream
1540 is
1541 begin
1542 return Create_Stream (Obj.MF, File_Size (Sec.Off), File_Size (Sec.Size));
1543 end Create_Stream;
1544
1545 procedure Tell (Obj : in out Mapped_Stream; Off : out Offset) is
1546 begin
1547 Off := Obj.Off;
1548 end Tell;
1549
1550 function Tell (Obj : Mapped_Stream) return Offset is
1551 begin
1552 return Obj.Off;
1553 end Tell;
1554
1555 function Length (Obj : Mapped_Stream) return Offset is
1556 begin
1557 return Obj.Len;
1558 end Length;
1559
1560 -----------
1561 -- Close --
1562 -----------
1563
1564 procedure Close (S : in out Mapped_Stream) is
1565 begin
1566 Free (S.Region);
1567 end Close;
1568
1569 procedure Close (Obj : in out Object_File) is
1570 begin
1571 Close (Obj.Symtab_Stream);
1572 Close (Obj.Symstr_Stream);
1573 Close (Obj.Sectab_Stream);
1574
1575 case Obj.Format is
1576 when ELF =>
1577 Close (Obj.Secstr_Stream);
1578 when Any_PECOFF =>
1579 null;
1580 when XCOFF32 =>
1581 null;
1582 end case;
1583
1584 Close (Obj.MF);
1585 end Close;
1586
1587 ------------------------
1588 -- Strip_Leading_Char --
1589 ------------------------
1590
1591 function Strip_Leading_Char
1592 (Obj : in out Object_File;
1593 Sym : String_Ptr_Len) return Positive
1594 is
1595 begin
1596 if (Obj.Format = PECOFF and then Sym.Ptr (1) = '_')
1597 or else
1598 (Obj.Format = XCOFF32 and then Sym.Ptr (1) = '.')
1599 then
1600 return 2;
1601 else
1602 return 1;
1603 end if;
1604 end Strip_Leading_Char;
1605
1606 ----------------------
1607 -- Decoded_Ada_Name --
1608 ----------------------
1609
1610 function Decoded_Ada_Name
1611 (Obj : in out Object_File;
1612 Sym : String_Ptr_Len) return String
1613 is
1614 procedure gnat_decode
1615 (Coded_Name_Addr : Address;
1616 Ada_Name_Addr : Address;
1617 Verbose : int);
1618 pragma Import (C, gnat_decode, "__gnat_decode");
1619
1620 subtype size_t is Interfaces.C.size_t;
1621
1622 Sym_Name : constant String :=
1623 String (Sym.Ptr (1 .. Sym.Len)) & ASCII.NUL;
1624 Decoded : char_array (0 .. size_t (Sym.Len) * 2 + 60);
1625 Off : Natural;
1626
1627 begin
1628 -- In the PECOFF case most but not all symbol table entries have an
1629 -- extra leading underscore. In this case we trim it.
1630
1631 Off := Strip_Leading_Char (Obj, Sym);
1632
1633 gnat_decode (Sym_Name (Off)'Address, Decoded'Address, 0);
1634
1635 return To_Ada (Decoded);
1636 end Decoded_Ada_Name;
1637
1638 ------------------
1639 -- First_Symbol --
1640 ------------------
1641
1642 function First_Symbol (Obj : in out Object_File) return Object_Symbol is
1643 begin
1644 case Obj.Format is
1645 when ELF32 => return ELF32_Ops.First_Symbol (Obj);
1646 when ELF64 => return ELF64_Ops.First_Symbol (Obj);
1647 when Any_PECOFF => return PECOFF_Ops.First_Symbol (Obj);
1648 when XCOFF32 => return XCOFF32_Ops.First_Symbol (Obj);
1649 end case;
1650 end First_Symbol;
1651
1652 ------------
1653 -- Format --
1654 ------------
1655
1656 function Format (Obj : Object_File) return Object_Format is
1657 begin
1658 return Obj.Format;
1659 end Format;
1660
1661 ----------------------
1662 -- Get_Load_Address --
1663 ----------------------
1664
1665 function Get_Load_Address (Obj : Object_File) return uint64 is
1666 begin
1667 case Obj.Format is
1668 when ELF => return 0;
1669 when Any_PECOFF => return Obj.ImageBase;
1670 when XCOFF32 => raise Format_Error;
1671 end case;
1672 end Get_Load_Address;
1673
1674 -----------------
1675 -- Get_Section --
1676 -----------------
1677
1678 function Get_Section
1679 (Obj : in out Object_File;
1680 Shnum : uint32) return Object_Section
1681 is
1682 begin
1683 case Obj.Format is
1684 when ELF32 => return ELF32_Ops.Get_Section (Obj, Shnum);
1685 when ELF64 => return ELF64_Ops.Get_Section (Obj, Shnum);
1686 when Any_PECOFF => return PECOFF_Ops.Get_Section (Obj, Shnum);
1687 when XCOFF32 => return XCOFF32_Ops.Get_Section (Obj, Shnum);
1688 end case;
1689 end Get_Section;
1690
1691 function Get_Section
1692 (Obj : in out Object_File;
1693 Sec_Name : String) return Object_Section
1694 is
1695 Sec : Object_Section;
1696
1697 begin
1698 for J in 0 .. Obj.Num_Sections - 1 loop
1699 Sec := Get_Section (Obj, J);
1700
1701 if Name (Obj, Sec) = Sec_Name then
1702 return Sec;
1703 end if;
1704 end loop;
1705
1706 if Obj.In_Exception then
1707 return Null_Section;
1708 else
1709 raise Format_Error with "could not find section in object file";
1710 end if;
1711 end Get_Section;
1712
1713 ----------------------
1714 -- Get_Xcode_Bounds --
1715 ----------------------
1716
1717 procedure Get_Xcode_Bounds
1718 (Obj : in out Object_File;
1719 Low, High : out uint64)
1720 is
1721 Sec : Object_Section;
1722
1723 begin
1724 -- First set as an empty range
1725 Low := uint64'Last;
1726 High := uint64'First;
1727
1728 -- Now find the lowest and highest offsets
1729 -- attached to executable code sections
1730 for Idx in 1 .. Num_Sections (Obj) loop
1731 Sec := Get_Section (Obj, Idx - 1);
1732 if Sec.Flag_Xcode then
1733 if Sec.Addr < Low then
1734 Low := Sec.Addr;
1735 end if;
1736 if Sec.Addr + Sec.Size > High then
1737 High := Sec.Addr + Sec.Size;
1738 end if;
1739 end if;
1740 end loop;
1741 end Get_Xcode_Bounds;
1742
1743 ----------
1744 -- Name --
1745 ----------
1746
1747 function Name
1748 (Obj : in out Object_File;
1749 Sec : Object_Section) return String
1750 is
1751 begin
1752 case Obj.Format is
1753 when ELF32 => return ELF32_Ops.Name (Obj, Sec);
1754 when ELF64 => return ELF64_Ops.Name (Obj, Sec);
1755 when Any_PECOFF => return PECOFF_Ops.Name (Obj, Sec);
1756 when XCOFF32 => return XCOFF32_Ops.Name (Obj, Sec);
1757 end case;
1758 end Name;
1759
1760 function Name
1761 (Obj : in out Object_File;
1762 Sym : Object_Symbol) return String_Ptr_Len
1763 is
1764 begin
1765 case Obj.Format is
1766 when ELF32 => return ELF32_Ops.Name (Obj, Sym);
1767 when ELF64 => return ELF64_Ops.Name (Obj, Sym);
1768 when Any_PECOFF => return PECOFF_Ops.Name (Obj, Sym);
1769 when XCOFF32 => return XCOFF32_Ops.Name (Obj, Sym);
1770 end case;
1771 end Name;
1772
1773 -----------------
1774 -- Next_Symbol --
1775 -----------------
1776
1777 function Next_Symbol
1778 (Obj : in out Object_File;
1779 Prev : Object_Symbol) return Object_Symbol
1780 is
1781 begin
1782 -- Test whether we've reached the end of the symbol table
1783
1784 if Prev.Next >= Obj.Symtab_Last then
1785 return Null_Symbol;
1786 end if;
1787
1788 return Read_Symbol (Obj, Prev.Next);
1789 end Next_Symbol;
1790
1791 ---------
1792 -- Num --
1793 ---------
1794
1795 function Num (Sec : Object_Section) return uint32 is
1796 begin
1797 return Sec.Num;
1798 end Num;
1799
1800 ------------------
1801 -- Num_Sections --
1802 ------------------
1803
1804 function Num_Sections (Obj : Object_File) return uint32 is
1805 begin
1806 return Obj.Num_Sections;
1807 end Num_Sections;
1808
1809 ---------
1810 -- Off --
1811 ---------
1812
1813 function Off (Sec : Object_Section) return Offset is
1814 begin
1815 return Sec.Off;
1816 end Off;
1817
1818 function Off (Sym : Object_Symbol) return Offset is
1819 begin
1820 return Sym.Off;
1821 end Off;
1822
1823 ----------------------
1824 -- Offset_To_String --
1825 ----------------------
1826
1827 function Offset_To_String
1828 (S : in out Mapped_Stream;
1829 Off : Offset) return String
1830 is
1831 Buf : Buffer;
1832
1833 begin
1834 Seek (S, Off);
1835 Read_C_String (S, Buf);
1836 return To_String (Buf);
1837 end Offset_To_String;
1838
1839 ----------
1840 -- Open --
1841 ----------
1842
1843 function Open
1844 (File_Name : String;
1845 In_Exception : Boolean := False) return Object_File_Access
1846 is
1847 F : Mapped_File;
1848 Hdr_Stream : Mapped_Stream;
1849
1850 begin
1851 -- Open the file
1852
1853 F := Open_Read_No_Exception (File_Name);
1854
1855 if F = Invalid_Mapped_File then
1856 if In_Exception then
1857 return null;
1858 else
1859 raise IO_Error with "could not open object file";
1860 end if;
1861 end if;
1862
1863 Hdr_Stream := Create_Stream (F, 0, 4096);
1864
1865 declare
1866 Hdr : constant ELF32_Ops.Header := ELF32_Ops.Read_Header (Hdr_Stream);
1867
1868 begin
1869 -- Look for the magic numbers for the ELF case
1870
1871 if Hdr.E_Ident (0) = 16#7F# and then
1872 Hdr.E_Ident (1) = Character'Pos ('E') and then
1873 Hdr.E_Ident (2) = Character'Pos ('L') and then
1874 Hdr.E_Ident (3) = Character'Pos ('F') and then
1875 Hdr.E_Ident (4) = ELF32_Ops.ELFCLASS32
1876 then
1877 Close (Hdr_Stream);
1878 return new Object_File'
1879 (ELF32_Ops.Initialize (F, Hdr, In_Exception));
1880 end if;
1881 end;
1882
1883 declare
1884 Hdr : constant ELF64_Ops.Header :=
1885 ELF64_Ops.Read_Header (Hdr_Stream);
1886
1887 begin
1888 -- Look for the magic numbers for the ELF case
1889
1890 if Hdr.E_Ident (0) = 16#7F# and then
1891 Hdr.E_Ident (1) = Character'Pos ('E') and then
1892 Hdr.E_Ident (2) = Character'Pos ('L') and then
1893 Hdr.E_Ident (3) = Character'Pos ('F') and then
1894 Hdr.E_Ident (4) = ELF32_Ops.ELFCLASS64
1895 then
1896 Close (Hdr_Stream);
1897 return new Object_File'
1898 (ELF64_Ops.Initialize (F, Hdr, In_Exception));
1899 end if;
1900 end;
1901
1902 declare
1903 Hdr : constant PECOFF_Ops.Header :=
1904 PECOFF_Ops.Read_Header (Hdr_Stream);
1905
1906 begin
1907 -- Test the magic numbers
1908
1909 if Hdr.Magics (0) = Character'Pos ('P') and then
1910 Hdr.Magics (1) = Character'Pos ('E') and then
1911 Hdr.Magics (2) = 0 and then
1912 Hdr.Magics (3) = 0
1913 then
1914 Close (Hdr_Stream);
1915 return new Object_File'
1916 (PECOFF_Ops.Initialize (F, Hdr, In_Exception));
1917 end if;
1918
1919 exception
1920 -- If this is not a PECOFF file then we've done a seek and read to a
1921 -- random address, possibly raising IO_Error
1922
1923 when IO_Error =>
1924 null;
1925 end;
1926
1927 declare
1928 Hdr : constant XCOFF32_Ops.Header :=
1929 XCOFF32_Ops.Read_Header (Hdr_Stream);
1930
1931 begin
1932 -- Test the magic numbers
1933
1934 if Hdr.f_magic = 8#0737# then
1935 Close (Hdr_Stream);
1936 return new Object_File'
1937 (XCOFF32_Ops.Initialize (F, Hdr, In_Exception));
1938 end if;
1939 end;
1940
1941 Close (Hdr_Stream);
1942
1943 if In_Exception then
1944 return null;
1945 else
1946 raise Format_Error with "unrecognized object format";
1947 end if;
1948 end Open;
1949
1950 ----------
1951 -- Read --
1952 ----------
1953
1954 function Read (S : in out Mapped_Stream) return Mmap.Str_Access is
1955 function To_Str_Access is
1956 new Ada.Unchecked_Conversion (Address, Str_Access);
1957
1958 begin
1959 return To_Str_Access (Data (S.Region) (Natural (S.Off + 1))'Address);
1960 end Read;
1961
1962 function Read (S : in out Mapped_Stream) return String_Ptr_Len is
1963 begin
1964 return To_String_Ptr_Len (Read (S));
1965 end Read;
1966
1967 procedure Check_Read_Offset (S : Mapped_Stream; Size : uint32) is
1968 begin
1969 if S.Off + Offset (Size) > Offset (Last (S.Region)) then
1970 raise IO_Error with "could not read from object file";
1971 end if;
1972 end Check_Read_Offset;
1973
1974 procedure Read_Raw
1975 (S : in out Mapped_Stream;
1976 Addr : Address;
1977 Size : uint32)
1978 is
1979 function To_Str_Access is
1980 new Ada.Unchecked_Conversion (Address, Str_Access);
1981 Sz : constant Offset := Offset (Size);
1982
1983 begin
1984 -- Check size
1985
1986 pragma Debug (Check_Read_Offset (S, Size));
1987
1988 -- Copy data
1989
1990 To_Str_Access (Addr) (1 .. Positive (Sz)) :=
1991 Data (S.Region) (Positive (S.Off + 1) .. Positive (S.Off + Sz));
1992
1993 -- Update offset
1994
1995 S.Off := S.Off + Sz;
1996 end Read_Raw;
1997
1998 function Read (S : in out Mapped_Stream) return uint8 is
1999 Data : uint8;
2000 begin
2001 Read_Raw (S, Data'Address, Data'Size / SSU);
2002 return Data;
2003 end Read;
2004
2005 function Read (S : in out Mapped_Stream) return uint16 is
2006 Data : uint16;
2007 begin
2008 Read_Raw (S, Data'Address, Data'Size / SSU);
2009 return Data;
2010 end Read;
2011
2012 function Read (S : in out Mapped_Stream) return uint32 is
2013 Data : uint32;
2014 begin
2015 Read_Raw (S, Data'Address, Data'Size / SSU);
2016 return Data;
2017 end Read;
2018
2019 function Read (S : in out Mapped_Stream) return uint64 is
2020 Data : uint64;
2021 begin
2022 Read_Raw (S, Data'Address, Data'Size / SSU);
2023 return Data;
2024 end Read;
2025
2026 function Read (S : in out Mapped_Stream) return int8 is
2027 Data : int8;
2028 begin
2029 Read_Raw (S, Data'Address, Data'Size / SSU);
2030 return Data;
2031 end Read;
2032
2033 function Read (S : in out Mapped_Stream) return int16 is
2034 Data : int16;
2035 begin
2036 Read_Raw (S, Data'Address, Data'Size / SSU);
2037 return Data;
2038 end Read;
2039
2040 function Read (S : in out Mapped_Stream) return int32 is
2041 Data : int32;
2042 begin
2043 Read_Raw (S, Data'Address, Data'Size / SSU);
2044 return Data;
2045 end Read;
2046
2047 function Read (S : in out Mapped_Stream) return int64 is
2048 Data : int64;
2049 begin
2050 Read_Raw (S, Data'Address, Data'Size / SSU);
2051 return Data;
2052 end Read;
2053
2054 ------------------
2055 -- Read_Address --
2056 ------------------
2057
2058 function Read_Address
2059 (Obj : Object_File; S : in out Mapped_Stream) return uint64
2060 is
2061 Address_32 : uint32;
2062 Address_64 : uint64;
2063
2064 begin
2065 case Obj.Arch is
2066 when i386
2067 | MIPS
2068 | PPC
2069 | SPARC
2070 | ARM
2071 =>
2072 Address_32 := Read (S);
2073 return uint64 (Address_32);
2074
2075 when AARCH64
2076 | IA64
2077 | PPC64
2078 | SPARC64
2079 | x86_64
2080 =>
2081 Address_64 := Read (S);
2082 return Address_64;
2083
2084 when RISCV | S390 =>
2085 case Obj.Format is
2086 when ELF32 =>
2087 Address_32 := Read (S);
2088 return uint64 (Address_32);
2089
2090 when ELF64 =>
2091 Address_64 := Read (S);
2092 return Address_64;
2093
2094 when others =>
2095 raise Format_Error with "unrecognized object format";
2096 end case;
2097
2098 when Unknown =>
2099 raise Format_Error with "unrecognized machine architecture";
2100 end case;
2101 end Read_Address;
2102
2103 -------------------
2104 -- Read_C_String --
2105 -------------------
2106
2107 procedure Read_C_String (S : in out Mapped_Stream; B : out Buffer) is
2108 J : Integer := 0;
2109
2110 begin
2111 loop
2112 -- Handle overflow case
2113
2114 if J = B'Last then
2115 B (J) := 0;
2116 exit;
2117 end if;
2118
2119 B (J) := Read (S);
2120 exit when B (J) = 0;
2121 J := J + 1;
2122 end loop;
2123 end Read_C_String;
2124
2125 -------------------
2126 -- Read_C_String --
2127 -------------------
2128
2129 function Read_C_String (S : in out Mapped_Stream) return Str_Access is
2130 Res : constant Str_Access := Read (S);
2131
2132 begin
2133 for J in Res'Range loop
2134 if S.Off + Offset (J - 1) > Offset (Last (S.Region)) then
2135 raise IO_Error with "could not read from object file";
2136 end if;
2137
2138 if Res (J) = ASCII.NUL then
2139 S.Off := S.Off + Offset (J);
2140 return Res;
2141 end if;
2142 end loop;
2143
2144 -- Overflow case
2145 raise Constraint_Error;
2146 end Read_C_String;
2147
2148 -----------------
2149 -- Read_LEB128 --
2150 -----------------
2151
2152 function Read_LEB128 (S : in out Mapped_Stream) return uint32 is
2153 B : uint8;
2154 Shift : Integer := 0;
2155 Res : uint32 := 0;
2156
2157 begin
2158 loop
2159 B := Read (S);
2160 Res := Res or Shift_Left (uint32 (B and 16#7f#), Shift);
2161 exit when (B and 16#80#) = 0;
2162 Shift := Shift + 7;
2163 end loop;
2164
2165 return Res;
2166 end Read_LEB128;
2167
2168 function Read_LEB128 (S : in out Mapped_Stream) return int32 is
2169 B : uint8;
2170 Shift : Integer := 0;
2171 Res : uint32 := 0;
2172
2173 begin
2174 loop
2175 B := Read (S);
2176 Res := Res or Shift_Left (uint32 (B and 16#7f#), Shift);
2177 Shift := Shift + 7;
2178 exit when (B and 16#80#) = 0;
2179 end loop;
2180
2181 if Shift < 32 and then (Res and Shift_Left (1, Shift - 1)) /= 0 then
2182 Res := Res or Shift_Left (-1, Shift);
2183 end if;
2184
2185 return To_int32 (Res);
2186 end Read_LEB128;
2187
2188 -----------------
2189 -- Read_Symbol --
2190 -----------------
2191
2192 function Read_Symbol
2193 (Obj : in out Object_File;
2194 Off : Offset) return Object_Symbol
2195 is
2196 begin
2197 case Obj.Format is
2198 when ELF32 => return ELF32_Ops.Read_Symbol (Obj, Off);
2199 when ELF64 => return ELF64_Ops.Read_Symbol (Obj, Off);
2200 when Any_PECOFF => return PECOFF_Ops.Read_Symbol (Obj, Off);
2201 when XCOFF32 => return XCOFF32_Ops.Read_Symbol (Obj, Off);
2202 end case;
2203 end Read_Symbol;
2204
2205 ----------
2206 -- Seek --
2207 ----------
2208
2209 procedure Seek (S : in out Mapped_Stream; Off : Offset) is
2210 begin
2211 if Off < 0 or else Off > Offset (Last (S.Region)) then
2212 raise IO_Error with "could not seek to offset in object file";
2213 end if;
2214
2215 S.Off := Off;
2216 end Seek;
2217
2218 ----------
2219 -- Size --
2220 ----------
2221
2222 function Size (Sec : Object_Section) return uint64 is
2223 begin
2224 return Sec.Size;
2225 end Size;
2226
2227 function Size (Sym : Object_Symbol) return uint64 is
2228 begin
2229 return Sym.Size;
2230 end Size;
2231
2232 ------------
2233 -- Strlen --
2234 ------------
2235
2236 function Strlen (Buf : Buffer) return int32 is
2237 begin
2238 return int32 (CRTL.strlen (Buf'Address));
2239 end Strlen;
2240
2241 -----------
2242 -- Spans --
2243 -----------
2244
2245 function Spans (Sym : Object_Symbol; Addr : uint64) return Boolean is
2246 begin
2247 return Addr >= Sym.Value and then Addr < Sym.Value + Sym.Size;
2248 end Spans;
2249
2250 ---------------
2251 -- To_String --
2252 ---------------
2253
2254 function To_String (Buf : Buffer) return String is
2255 Result : String (1 .. Integer (CRTL.strlen (Buf'Address)));
2256 for Result'Address use Buf'Address;
2257 pragma Import (Ada, Result);
2258
2259 begin
2260 return Result;
2261 end To_String;
2262
2263 -----------------------
2264 -- To_String_Ptr_Len --
2265 -----------------------
2266
2267 function To_String_Ptr_Len
2268 (Ptr : Mmap.Str_Access;
2269 Max_Len : Natural := Natural'Last) return String_Ptr_Len
2270 is
2271 begin
2272 for I in 1 .. Max_Len loop
2273 if Ptr (I) = ASCII.NUL then
2274 return (Ptr, I - 1);
2275 end if;
2276 end loop;
2277 return (Ptr, Max_Len);
2278 end To_String_Ptr_Len;
2279
2280 ------------------------
2281 -- Trim_Trailing_Nuls --
2282 ------------------------
2283
2284 function Trim_Trailing_Nuls (Str : String) return String is
2285 begin
2286 for J in Str'Range loop
2287 if Str (J) = ASCII.NUL then
2288 return Str (Str'First .. J - 1);
2289 end if;
2290 end loop;
2291
2292 return Str;
2293 end Trim_Trailing_Nuls;
2294
2295 -----------
2296 -- Value --
2297 -----------
2298
2299 function Value (Sym : Object_Symbol) return uint64 is
2300 begin
2301 return Sym.Value;
2302 end Value;
2303
2304 end System.Object_Reader;