1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y M B O L S . P R O C E S S I N G --
9 -- Copyright (C) 2004-2005 Free Software Foundation, Inc. --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 -- This is the VMS/IA64 version of this package
29 with Ada
.IO_Exceptions
;
31 with Ada
.Unchecked_Deallocation
;
34 package body Processing
is
36 type String_Array
is array (Positive range <>) of String_Access
;
37 type Strings_Ptr
is access String_Array
;
40 new Ada
.Unchecked_Deallocation
(String_Array
, Strings_Ptr
);
42 type Section_Header
is record
50 type Section_Header_Array
is array (Natural range <>) of Section_Header
;
51 type Section_Header_Ptr
is access Section_Header_Array
;
54 new Ada
.Unchecked_Deallocation
(Section_Header_Array
, Section_Header_Ptr
);
61 (Object_File
: String;
62 Success
: out Boolean)
68 Str
: String (1 .. 1000) := (others => ' ');
71 Strings
: Strings_Ptr
;
83 Symtab_Index
: Natural := 0;
84 String_Table_Index
: Natural := 0;
94 Section_Headers
: Section_Header_Ptr
;
96 Offset
: Natural := 0;
98 procedure Get_Byte
(B
: out Byte
);
99 procedure Get_Half
(H
: out Integer);
100 procedure Get_Word
(W
: out Integer);
103 procedure Get_Byte
(B
: out Byte
) is
105 Byte_IO
.Read
(File
, B
);
106 Offset
:= Offset
+ 1;
109 procedure Get_Half
(H
: out Integer) is
112 Get_Byte
(C1
); Get_Byte
(C2
);
114 Integer'(Character'Pos (C2)) * 256 + Integer'(Character'Pos (C1
));
117 procedure Get_Word
(W
: out Integer) is
120 Get_Half
(H1
); Get_Half
(H2
);
121 W
:= H2
* 256 * 256 + H1
;
127 Byte_IO
.Reset
(File
);
131 -- Open the object file with Byte_IO. Return with Success = False if
135 Open
(File
, In_File
, Object_File
);
139 ("*** Unable to open object file """ & Object_File
& """");
144 -- Assume that the object file has a correct format
148 -- Skip ELF identification
150 while Offset
< 16 loop
180 -- Skip upper half of Shoff
202 Get_Half
(Shentsize
);
206 Section_Headers
:= new Section_Header_Array
(0 .. Shnum
- 1);
208 -- Go to Section Headers
210 while Offset
< Shoff
loop
214 -- Reset Symtab_Index
218 for J
in Section_Headers
'Range loop
219 -- Get the data for each Section Header
224 for K
in 1 .. 16 loop
236 while (Offset
- Shoff
) mod Shentsize
/= 0 loop
240 -- If this is the Symbol Table Section Header, record its index
246 Section_Headers
(J
) := (Shname
, Shtype
, Shoffset
, Shsize
, Shlink
);
249 if Symtab_Index
= 0 then
255 Section_Headers
(Symtab_Index
).Shoffset
+
256 Section_Headers
(Symtab_Index
).Shsize
;
258 String_Table_Index
:= Section_Headers
(Symtab_Index
).Shlink
;
260 new String_Array
(1 .. Section_Headers
(String_Table_Index
).Shsize
);
262 -- Go get the String Table section for the Symbol Table
266 while Offset
< Section_Headers
(String_Table_Index
).Shoffset
loop
272 Get_Byte
(B
); -- zero
274 while Offset
< Section_Headers
(String_Table_Index
).Shsize
loop
279 if B
/= ASCII
.NUL
then
280 Str_Last
:= Str_Last
+ 1;
284 Strings
(Offset
- Str_Last
- 1) :=
285 new String'(Str (1 .. Str_Last));
291 -- Go get the Symbol Table
295 while Offset < Section_Headers (Symtab_Index).Shoffset loop
299 while Offset < End_Symtab loop
308 Sttype := Integer'(Character'Pos (Stinfo
)) mod 16;
309 Stbind
:= Integer'(Character'Pos (Stinfo)) / 16;
311 if (Sttype = 1 or else Sttype = 2)
313 and then Stshndx /= 0
316 S_Data : Symbol_Data;
318 S_Data.Name := new String'(Strings
(Stname
).all);
327 -- Put the new symbol in the table
329 Symbol_Table
.Increment_Last
(Complete_Symbols
);
330 Complete_Symbols
.Table
331 (Symbol_Table
.Last
(Complete_Symbols
)) := S_Data
;
336 -- The object file has been processed, close it
340 -- Free the allocated memory
342 Free
(Section_Headers
);
344 for J
in Strings
'Range loop
345 if Strings
(J
) /= null then
353 -- For any exception, output an error message, close the object file
354 -- and return with Success = False.
356 when Ada
.IO_Exceptions
.End_Error
=>
360 Put_Line
("unexpected exception raised while processing """
361 & Object_File
& """");
362 Put_Line
(Exception_Information
(X
));