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-2006, 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;
99 procedure Get_Byte
(B
: out Byte
);
100 procedure Get_Half
(H
: out Integer);
101 procedure Get_Word
(W
: out Integer);
103 -- All the above require comments ???
109 procedure Get_Byte
(B
: out Byte
) is
111 Byte_IO
.Read
(File
, B
);
112 Offset
:= Offset
+ 1;
119 procedure Get_Half
(H
: out Integer) is
122 Get_Byte
(C1
); Get_Byte
(C2
);
124 Integer'(Character'Pos (C2)) * 256 + Integer'(Character'Pos (C1
));
131 procedure Get_Word
(W
: out Integer) is
134 Get_Half
(H1
); Get_Half
(H2
);
135 W
:= H2
* 256 * 256 + H1
;
145 Byte_IO
.Reset
(File
);
148 -- Start of processing for Process
151 -- Open the object file with Byte_IO. Return with Success = False if
155 Open
(File
, In_File
, Object_File
);
159 ("*** Unable to open object file """ & Object_File
& """");
164 -- Assume that the object file has a correct format
168 -- Skip ELF identification
170 while Offset
< 16 loop
200 -- Skip upper half of Shoff
222 Get_Half
(Shentsize
);
226 Section_Headers
:= new Section_Header_Array
(0 .. Shnum
- 1);
228 -- Go to Section Headers
230 while Offset
< Shoff
loop
234 -- Reset Symtab_Index
238 for J
in Section_Headers
'Range loop
240 -- Get the data for each Section Header
245 for K
in 1 .. 16 loop
257 while (Offset
- Shoff
) mod Shentsize
/= 0 loop
261 -- If this is the Symbol Table Section Header, record its index
267 Section_Headers
(J
) := (Shname
, Shtype
, Shoffset
, Shsize
, Shlink
);
270 if Symtab_Index
= 0 then
276 Section_Headers
(Symtab_Index
).Shoffset
+
277 Section_Headers
(Symtab_Index
).Shsize
;
279 String_Table_Index
:= Section_Headers
(Symtab_Index
).Shlink
;
281 new String_Array
(1 .. Section_Headers
(String_Table_Index
).Shsize
);
283 -- Go get the String Table section for the Symbol Table
287 while Offset
< Section_Headers
(String_Table_Index
).Shoffset
loop
293 Get_Byte
(B
); -- zero
295 while Offset
< Section_Headers
(String_Table_Index
).Shsize
loop
300 if B
/= ASCII
.NUL
then
301 Str_Last
:= Str_Last
+ 1;
305 Strings
(Offset
- Str_Last
- 1) :=
306 new String'(Str (1 .. Str_Last));
312 -- Go get the Symbol Table
316 while Offset < Section_Headers (Symtab_Index).Shoffset loop
320 while Offset < End_Symtab loop
329 Sttype := Integer'(Character'Pos (Stinfo
)) mod 16;
330 Stbind
:= Integer'(Character'Pos (Stinfo)) / 16;
332 if (Sttype = 1 or else Sttype = 2)
334 and then Stshndx /= 0
336 -- Check if this is a symbol from a generic body
340 for J in Strings (Stname)'First .. Strings (Stname)'Last - 2 loop
341 if Strings (Stname) (J) = 'G
'
342 and then Strings (Stname) (J + 1) = 'P
'
343 and then Strings (Stname) (J + 2) in '0' .. '9'
352 S_Data : Symbol_Data;
354 S_Data.Name := new String'(Strings
(Stname
).all);
363 -- Put the new symbol in the table
365 Symbol_Table
.Increment_Last
(Complete_Symbols
);
366 Complete_Symbols
.Table
367 (Symbol_Table
.Last
(Complete_Symbols
)) := S_Data
;
373 -- The object file has been processed, close it
377 -- Free the allocated memory
379 Free
(Section_Headers
);
381 for J
in Strings
'Range loop
382 if Strings
(J
) /= null then
390 -- For any exception, output an error message, close the object file
391 -- and return with Success = False.
393 when Ada
.IO_Exceptions
.End_Error
=>
397 Put_Line
("unexpected exception raised while processing """
398 & Object_File
& """");
399 Put_Line
(Exception_Information
(X
));