| 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 |
-- S p e c -- |
| 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 |
-- This package implements a simple, minimal overhead reader for object files |
| 33 |
-- composed of sections of untyped heterogeneous binary data. |
| 34 |
|
| 35 |
with Interfaces; |
| 36 |
with System.Mmap; |
| 37 |
|
| 38 |
package System.Object_Reader is |
| 39 |
|
| 40 |
-------------- |
| 41 |
-- Limits -- |
| 42 |
-------------- |
| 43 |
|
| 44 |
BUFFER_SIZE : constant := 8 * 1024; |
| 45 |
|
| 46 |
--------------------- |
| 47 |
-- Object sections -- |
| 48 |
---------------------- |
| 49 |
|
| 50 |
type Object_Section is private; |
| 51 |
|
| 52 |
Null_Section : constant Object_Section; |
| 53 |
|
| 54 |
-------------------- |
| 55 |
-- Object symbols -- |
| 56 |
-------------------- |
| 57 |
|
| 58 |
type Object_Symbol is private; |
| 59 |
|
| 60 |
------------------------ |
| 61 |
-- Object format type -- |
| 62 |
------------------------ |
| 63 |
|
| 64 |
type Object_Format is |
| 65 |
(ELF32, |
| 66 |
-- Object format is 32-bit ELF |
| 67 |
|
| 68 |
ELF64, |
| 69 |
-- Object format is 64-bit ELF |
| 70 |
|
| 71 |
PECOFF, |
| 72 |
-- Object format is Microsoft PECOFF |
| 73 |
|
| 74 |
PECOFF_PLUS, |
| 75 |
-- Object format is Microsoft PECOFF+ |
| 76 |
|
| 77 |
XCOFF32); |
| 78 |
-- Object format is AIX 32-bit XCOFF |
| 79 |
|
| 80 |
-- PECOFF | PECOFF_PLUS appears so often as a case choice, would |
| 81 |
-- seem a good idea to have a subtype name covering these two choices ??? |
| 82 |
|
| 83 |
------------------ |
| 84 |
-- Object files -- |
| 85 |
------------------ |
| 86 |
|
| 87 |
type Object_File (Format : Object_Format) is private; |
| 88 |
|
| 89 |
type Object_File_Access is access Object_File; |
| 90 |
|
| 91 |
------------------------------ |
| 92 |
-- Object architecture type -- |
| 93 |
------------------------------ |
| 94 |
|
| 95 |
type Object_Arch is |
| 96 |
(Unknown, |
| 97 |
-- The target architecture has not yet been determined |
| 98 |
|
| 99 |
SPARC, |
| 100 |
-- 32-bit SPARC |
| 101 |
|
| 102 |
SPARC64, |
| 103 |
-- 64-bit SPARC |
| 104 |
|
| 105 |
i386, |
| 106 |
-- Intel IA32 |
| 107 |
|
| 108 |
MIPS, |
| 109 |
-- MIPS Technologies MIPS |
| 110 |
|
| 111 |
x86_64, |
| 112 |
-- x86-64 (64-bit AMD/Intel) |
| 113 |
|
| 114 |
IA64, |
| 115 |
-- Intel IA64 |
| 116 |
|
| 117 |
PPC, |
| 118 |
-- 32-bit PowerPC |
| 119 |
|
| 120 |
PPC64, |
| 121 |
-- 64-bit PowerPC |
| 122 |
|
| 123 |
S390, |
| 124 |
-- IBM S/390 |
| 125 |
|
| 126 |
ARM, |
| 127 |
-- 32-bit ARM |
| 128 |
|
| 129 |
AARCH64, |
| 130 |
-- 64-bit ARM |
| 131 |
|
| 132 |
RISCV); |
| 133 |
-- RISC-V |
| 134 |
|
| 135 |
------------------ |
| 136 |
-- Target types -- |
| 137 |
------------------ |
| 138 |
|
| 139 |
subtype Offset is Interfaces.Integer_64; |
| 140 |
|
| 141 |
subtype uint8 is Interfaces.Unsigned_8; |
| 142 |
subtype uint16 is Interfaces.Unsigned_16; |
| 143 |
subtype uint32 is Interfaces.Unsigned_32; |
| 144 |
subtype uint64 is Interfaces.Unsigned_64; |
| 145 |
|
| 146 |
subtype int8 is Interfaces.Integer_8; |
| 147 |
subtype int16 is Interfaces.Integer_16; |
| 148 |
subtype int32 is Interfaces.Integer_32; |
| 149 |
subtype int64 is Interfaces.Integer_64; |
| 150 |
|
| 151 |
type Buffer is array (0 .. BUFFER_SIZE - 1) of uint8; |
| 152 |
|
| 153 |
type String_Ptr_Len is record |
| 154 |
Ptr : Mmap.Str_Access; |
| 155 |
Len : Natural; |
| 156 |
end record; |
| 157 |
-- A string made from a pointer and a length. Not all strings for name |
| 158 |
-- are C strings: COFF inlined symbol names have a max length of 8. |
| 159 |
|
| 160 |
------------------------------------------- |
| 161 |
-- Operations on buffers of untyped data -- |
| 162 |
------------------------------------------- |
| 163 |
|
| 164 |
function To_String (Buf : Buffer) return String; |
| 165 |
-- Construct string from C style null-terminated string stored in a buffer |
| 166 |
|
| 167 |
function To_String_Ptr_Len |
| 168 |
(Ptr : Mmap.Str_Access; |
| 169 |
Max_Len : Natural := Natural'Last) return String_Ptr_Len; |
| 170 |
-- Convert PTR to a String_Ptr_Len. |
| 171 |
|
| 172 |
function Strlen (Buf : Buffer) return int32; |
| 173 |
-- Return the length of a C style null-terminated string |
| 174 |
|
| 175 |
------------------------- |
| 176 |
-- Opening and closing -- |
| 177 |
------------------------- |
| 178 |
|
| 179 |
function Open |
| 180 |
(File_Name : String; |
| 181 |
In_Exception : Boolean := False) return Object_File_Access; |
| 182 |
-- Open the object file and initialize the reader. In_Exception is true |
| 183 |
-- when the parsing is done as part of an exception handler decorator. In |
| 184 |
-- this mode we do not want to raise an exception. |
| 185 |
|
| 186 |
procedure Close (Obj : in out Object_File); |
| 187 |
-- Close the object file |
| 188 |
|
| 189 |
----------------------- |
| 190 |
-- Sequential access -- |
| 191 |
----------------------- |
| 192 |
|
| 193 |
type Mapped_Stream is private; |
| 194 |
-- Provide an abstraction of a stream on a memory mapped file |
| 195 |
|
| 196 |
function Create_Stream (MF : System.Mmap.Mapped_File; |
| 197 |
File_Offset : System.Mmap.File_Size; |
| 198 |
File_Length : System.Mmap.File_Size) |
| 199 |
return Mapped_Stream; |
| 200 |
-- Create a stream from Mf |
| 201 |
|
| 202 |
procedure Close (S : in out Mapped_Stream); |
| 203 |
-- Close the stream (deallocate memory) |
| 204 |
|
| 205 |
procedure Read_Raw |
| 206 |
(S : in out Mapped_Stream; |
| 207 |
Addr : Address; |
| 208 |
Size : uint32); |
| 209 |
pragma Inline (Read_Raw); |
| 210 |
-- Read a number of fixed sized records |
| 211 |
|
| 212 |
procedure Seek (S : in out Mapped_Stream; Off : Offset); |
| 213 |
-- Seek to an absolute offset in bytes |
| 214 |
|
| 215 |
procedure Tell (Obj : in out Mapped_Stream; Off : out Offset) |
| 216 |
with Inline; |
| 217 |
function Tell (Obj : Mapped_Stream) return Offset |
| 218 |
with Inline; |
| 219 |
-- Fetch the current offset |
| 220 |
|
| 221 |
function Length (Obj : Mapped_Stream) return Offset |
| 222 |
with Inline; |
| 223 |
-- Length of the stream |
| 224 |
|
| 225 |
function Read (S : in out Mapped_Stream) return Mmap.Str_Access; |
| 226 |
-- Provide a pointer in memory at the current offset |
| 227 |
|
| 228 |
function Read (S : in out Mapped_Stream) return String_Ptr_Len; |
| 229 |
-- Provide a pointer in memory at the current offset |
| 230 |
|
| 231 |
function Read (S : in out Mapped_Stream) return uint8; |
| 232 |
function Read (S : in out Mapped_Stream) return uint16; |
| 233 |
function Read (S : in out Mapped_Stream) return uint32; |
| 234 |
function Read (S : in out Mapped_Stream) return uint64; |
| 235 |
function Read (S : in out Mapped_Stream) return int8; |
| 236 |
function Read (S : in out Mapped_Stream) return int16; |
| 237 |
function Read (S : in out Mapped_Stream) return int32; |
| 238 |
function Read (S : in out Mapped_Stream) return int64; |
| 239 |
-- Read a scalar |
| 240 |
|
| 241 |
function Read_Address |
| 242 |
(Obj : Object_File; S : in out Mapped_Stream) return uint64; |
| 243 |
-- Read either a 64 or 32 bit address from the file stream depending on the |
| 244 |
-- address size of the target architecture and promote it to a 64 bit type. |
| 245 |
|
| 246 |
function Read_LEB128 (S : in out Mapped_Stream) return uint32; |
| 247 |
function Read_LEB128 (S : in out Mapped_Stream) return int32; |
| 248 |
-- Read a value encoding in Little-Endian Base 128 format |
| 249 |
|
| 250 |
procedure Read_C_String (S : in out Mapped_Stream; B : out Buffer); |
| 251 |
function Read_C_String (S : in out Mapped_Stream) return Mmap.Str_Access; |
| 252 |
-- Read a C style NULL terminated string |
| 253 |
|
| 254 |
function Offset_To_String |
| 255 |
(S : in out Mapped_Stream; |
| 256 |
Off : Offset) return String; |
| 257 |
-- Construct a string from a C style NULL terminated string located at an |
| 258 |
-- offset into the object file. |
| 259 |
|
| 260 |
------------------------ |
| 261 |
-- Object information -- |
| 262 |
------------------------ |
| 263 |
|
| 264 |
function Arch (Obj : Object_File) return Object_Arch; |
| 265 |
-- Return the object architecture |
| 266 |
|
| 267 |
function Format (Obj : Object_File) return Object_Format; |
| 268 |
-- Return the object file format |
| 269 |
|
| 270 |
function Get_Load_Address (Obj : Object_File) return uint64; |
| 271 |
-- Return the load address defined in Obj. May raise Format_Error if not |
| 272 |
-- implemented |
| 273 |
|
| 274 |
function Num_Sections (Obj : Object_File) return uint32; |
| 275 |
-- Return the number of sections composing the object file |
| 276 |
|
| 277 |
function Get_Section |
| 278 |
(Obj : in out Object_File; |
| 279 |
Shnum : uint32) return Object_Section; |
| 280 |
-- Return the Nth section (numbered from zero) |
| 281 |
|
| 282 |
function Get_Section |
| 283 |
(Obj : in out Object_File; |
| 284 |
Sec_Name : String) return Object_Section; |
| 285 |
-- Return a section by name |
| 286 |
|
| 287 |
function Create_Stream |
| 288 |
(Obj : Object_File; |
| 289 |
Sec : Object_Section) return Mapped_Stream; |
| 290 |
-- Create a stream for section Sec |
| 291 |
|
| 292 |
procedure Get_Xcode_Bounds |
| 293 |
(Obj : in out Object_File; |
| 294 |
Low, High : out uint64); |
| 295 |
-- Return the low and high addresses of the code for the object file. Can |
| 296 |
-- be used to check if an address lies within this object file. This |
| 297 |
-- procedure is not efficient and the result should be saved to avoid |
| 298 |
-- recomputation. |
| 299 |
|
| 300 |
------------------------- |
| 301 |
-- Section information -- |
| 302 |
------------------------- |
| 303 |
|
| 304 |
function Name |
| 305 |
(Obj : in out Object_File; |
| 306 |
Sec : Object_Section) return String; |
| 307 |
-- Return the name of a section as a string |
| 308 |
|
| 309 |
function Size (Sec : Object_Section) return uint64; |
| 310 |
-- Return the size of a section in bytes |
| 311 |
|
| 312 |
function Num (Sec : Object_Section) return uint32; |
| 313 |
-- Return the index of a section from zero |
| 314 |
|
| 315 |
function Off (Sec : Object_Section) return Offset; |
| 316 |
-- Return the byte offset of the section within the object |
| 317 |
|
| 318 |
------------------------------ |
| 319 |
-- Symbol table information -- |
| 320 |
------------------------------ |
| 321 |
|
| 322 |
Null_Symbol : constant Object_Symbol; |
| 323 |
-- An empty symbol table entry. |
| 324 |
|
| 325 |
function First_Symbol (Obj : in out Object_File) return Object_Symbol; |
| 326 |
-- Return the first element in the symbol table or Null_Symbol if the |
| 327 |
-- symbol table is empty. |
| 328 |
|
| 329 |
function Next_Symbol |
| 330 |
(Obj : in out Object_File; |
| 331 |
Prev : Object_Symbol) return Object_Symbol; |
| 332 |
-- Return the element following Prev in the symbol table, or Null_Symbol if |
| 333 |
-- Prev is the last symbol in the table. |
| 334 |
|
| 335 |
function Read_Symbol |
| 336 |
(Obj : in out Object_File; |
| 337 |
Off : Offset) return Object_Symbol; |
| 338 |
-- Read symbol at Off |
| 339 |
|
| 340 |
function Name |
| 341 |
(Obj : in out Object_File; |
| 342 |
Sym : Object_Symbol) return String_Ptr_Len; |
| 343 |
-- Return the name of the symbol |
| 344 |
|
| 345 |
function Decoded_Ada_Name |
| 346 |
(Obj : in out Object_File; |
| 347 |
Sym : String_Ptr_Len) return String; |
| 348 |
-- Return the decoded name of a symbol encoded as per exp_dbug.ads |
| 349 |
|
| 350 |
function Strip_Leading_Char |
| 351 |
(Obj : in out Object_File; |
| 352 |
Sym : String_Ptr_Len) return Positive; |
| 353 |
-- Return the index of the first character to decode the name. This can |
| 354 |
-- strip one character for ABI with a prefix (like x86 for PECOFF). |
| 355 |
|
| 356 |
function Value (Sym : Object_Symbol) return uint64; |
| 357 |
-- Return the name of the symbol |
| 358 |
|
| 359 |
function Size (Sym : Object_Symbol) return uint64; |
| 360 |
-- Return the size of the symbol in bytes |
| 361 |
|
| 362 |
function Spans (Sym : Object_Symbol; Addr : uint64) return Boolean; |
| 363 |
-- Determine whether a particular address corresponds to the range |
| 364 |
-- referenced by this symbol. |
| 365 |
|
| 366 |
function Off (Sym : Object_Symbol) return Offset; |
| 367 |
-- Return the offset of the symbol. |
| 368 |
|
| 369 |
---------------- |
| 370 |
-- Exceptions -- |
| 371 |
---------------- |
| 372 |
|
| 373 |
IO_Error : exception; |
| 374 |
-- Input/Output error reading file |
| 375 |
|
| 376 |
Format_Error : exception; |
| 377 |
-- Encountered a problem parsing the object |
| 378 |
|
| 379 |
private |
| 380 |
type Mapped_Stream is record |
| 381 |
Region : System.Mmap.Mapped_Region; |
| 382 |
Off : Offset; |
| 383 |
Len : Offset; |
| 384 |
end record; |
| 385 |
|
| 386 |
subtype ELF is Object_Format range ELF32 .. ELF64; |
| 387 |
subtype Any_PECOFF is Object_Format range PECOFF .. PECOFF_PLUS; |
| 388 |
|
| 389 |
type Object_File (Format : Object_Format) is record |
| 390 |
MF : System.Mmap.Mapped_File := System.Mmap.Invalid_Mapped_File; |
| 391 |
Arch : Object_Arch := Unknown; |
| 392 |
|
| 393 |
Num_Sections : uint32 := 0; |
| 394 |
-- Number of sections |
| 395 |
|
| 396 |
Symtab_Last : Offset; -- Last offset of symbol table |
| 397 |
|
| 398 |
In_Exception : Boolean := False; |
| 399 |
-- True if the parsing is done as part of an exception handler |
| 400 |
|
| 401 |
Sectab_Stream : Mapped_Stream; |
| 402 |
-- Section table |
| 403 |
|
| 404 |
Symtab_Stream : Mapped_Stream; |
| 405 |
-- Symbol table |
| 406 |
|
| 407 |
Symstr_Stream : Mapped_Stream; |
| 408 |
-- Symbol strings |
| 409 |
|
| 410 |
case Format is |
| 411 |
when ELF => |
| 412 |
Secstr_Stream : Mapped_Stream; |
| 413 |
-- Section strings |
| 414 |
|
| 415 |
when Any_PECOFF => |
| 416 |
ImageBase : uint64; -- ImageBase value from header |
| 417 |
|
| 418 |
-- Cache for latest result of Get_Section_Virtual_Address |
| 419 |
|
| 420 |
GSVA_Sec : uint32 := uint32'Last; |
| 421 |
GSVA_Addr : uint64; |
| 422 |
|
| 423 |
when XCOFF32 => |
| 424 |
null; |
| 425 |
end case; |
| 426 |
end record; |
| 427 |
|
| 428 |
subtype ELF_Object_File is Object_File |
| 429 |
with Predicate => ELF_Object_File.Format in ELF; |
| 430 |
|
| 431 |
subtype PECOFF_Object_File is Object_File |
| 432 |
with Predicate => PECOFF_Object_File.Format in Any_PECOFF; |
| 433 |
|
| 434 |
subtype XCOFF32_Object_File is Object_File |
| 435 |
with Predicate => XCOFF32_Object_File.Format in XCOFF32; |
| 436 |
|
| 437 |
type Object_Section is record |
| 438 |
Num : uint32 := 0; |
| 439 |
-- Section index in the section table |
| 440 |
|
| 441 |
Off : Offset := 0; |
| 442 |
-- First byte of the section in the object file |
| 443 |
|
| 444 |
Addr : uint64 := 0; |
| 445 |
-- Load address of the section. Valid only when Flag_Alloc is true. |
| 446 |
|
| 447 |
Size : uint64 := 0; |
| 448 |
-- Length of the section in bytes |
| 449 |
|
| 450 |
Flag_Xcode : Boolean := False; |
| 451 |
-- True if the section is advertised to contain executable code |
| 452 |
end record; |
| 453 |
|
| 454 |
Null_Section : constant Object_Section := (0, 0, 0, 0, False); |
| 455 |
|
| 456 |
type Object_Symbol is record |
| 457 |
Off : Offset := 0; -- Offset of underlying symbol on disk |
| 458 |
Next : Offset := 0; -- Offset of the following symbol |
| 459 |
Value : uint64 := 0; -- Value associated with this symbol |
| 460 |
Size : uint64 := 0; -- Size of the referenced entity |
| 461 |
end record; |
| 462 |
|
| 463 |
Null_Symbol : constant Object_Symbol := (0, 0, 0, 0); |
| 464 |
end System.Object_Reader; |