1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 version of this package
29 with Ada.Exceptions; use Ada.Exceptions;
30 with Ada.Sequential_IO;
31 with Ada.Text_IO; use Ada.Text_IO;
33 package body Symbols is
35 Case_Sensitive : constant String := "case_sensitive=";
36 Symbol_Vector : constant String := "SYMBOL_VECTOR=(";
37 Equal_Data : constant String := "=DATA)";
38 Equal_Procedure : constant String := "=PROCEDURE)";
40 Symbol_File_Name : String_Access := null;
41 -- Name of the symbol file
43 subtype Byte is Character;
44 -- Object files are stream of bytes, but some of these bytes, those for
45 -- the names of the symbols, are ASCII characters.
47 package Byte_IO is new Ada.Sequential_IO (Byte);
50 type Number is mod 2**16;
51 -- 16 bits unsigned number for number of characters
53 GSD : constant Number := 10;
54 -- Code for the Global Symbol Definition section
56 C_SYM : constant Number := 1;
57 -- Code for a Symbol subsection
59 V_DEF_Mask : constant Number := 2**1;
60 V_NORM_Mask : constant Number := 2**6;
62 File : Byte_IO.File_Type;
63 -- Each object file is read as a stream of bytes (characters)
67 Number_Of_Characters : Natural := 0;
68 -- The number of characters of each section
71 Length : Natural := 0;
75 Nchars : Natural := 0;
78 Symbol : String (1 .. 255);
81 function Equal (Left, Right : Symbol_Data) return Boolean;
82 -- Test for equality of symbols
84 procedure Get (N : out Number);
85 -- Read two bytes from the object file LSB first as unsigned 16 bit number
87 procedure Get (N : out Natural);
88 -- Read two bytes from the object file, LSByte first, as a Natural
94 function Equal (Left, Right : Symbol_Data) return Boolean is
96 return Left.Name /= null and then
97 Right.Name /= null and then
98 Left.Name.all = Right.Name.all and then
99 Left.Kind = Right.Kind and then
100 Left.Present = Right.Present;
107 procedure Get (N : out Number) is
112 LSByte := Byte'Pos (C);
114 N := LSByte + (256 * Byte'Pos (C));
117 procedure Get (N : out Natural) is
121 N := Natural (Result);
129 (Symbol_File : String;
132 Success : out Boolean)
134 File : Ada.Text_IO.File_Type;
135 Line : String (1 .. 1_000);
139 -- Record the symbol file name
141 Symbol_File_Name := new String'(Symbol_File);
143 -- Empty the symbol tables
145 Symbol_Table.Set_Last (Original_Symbols, 0);
146 Symbol_Table.Set_Last (Complete_Symbols, 0);
148 -- Assume that everything will be fine
152 -- If Force is not set, attempt to read the symbol file
156 Open (File, In_File, Symbol_File);
159 when Ada.Text_IO.Name_Error =>
164 Put_Line ("could not open """ & Symbol_File & """");
165 Put_Line (Exception_Message (X));
172 while not End_Of_File (File) loop
173 Get_Line (File, Line, Last);
178 elsif Last > Case_Sensitive'Length
179 and then Line (1 .. Case_Sensitive'Length) = Case_Sensitive
183 elsif Last > Symbol_Vector'Length
184 and then Line (1 .. Symbol_Vector'Length) = Symbol_Vector
186 if Last > Symbol_Vector'Length + Equal_Data'Length and then
187 Line (Last - Equal_Data'Length + 1 .. Last) = Equal_Data
189 Symbol_Table.Increment_Last (Original_Symbols);
190 Original_Symbols.Table
191 (Symbol_Table.Last (Original_Symbols)) :=
193 new String'(Line (Symbol_Vector'Length + 1 ..
194 Last - Equal_Data'Length)),
198 elsif Last > Symbol_Vector'Length + Equal_Procedure'Length
200 Line (Last - Equal_Procedure'Length + 1 .. Last) =
203 Symbol_Table.Increment_Last (Original_Symbols);
204 Original_Symbols.Table
205 (Symbol_Table.Last (Original_Symbols)) :=
207 new String'(Line (Symbol_Vector'Length + 1 ..
208 Last - Equal_Procedure'Length)),
214 Put_Line ("symbol file """ & Symbol_File &
215 """ is incorrectly formatted:");
216 Put_Line ("""" & Line (1 .. Last) & """");
226 Put_Line ("unexpected line in symbol file """ &
228 Put_Line ("""" & Line (1 .. Last) & """");
246 (Object_File : String;
247 Success : out Boolean)
250 -- Open the object file. Return with Success = False if this fails.
253 Open (File, In_File, Object_File);
257 ("*** Unable to open object file """ & Object_File & """");
262 -- Assume that the object file has a correct format
266 -- Get the different sections one by one from the object file
268 while not End_Of_File (File) loop
271 Get (Number_Of_Characters);
272 Number_Of_Characters := Number_Of_Characters - 4;
274 -- If this is not a Global Symbol Definition section, skip to the
279 for J in 1 .. Number_Of_Characters loop
285 -- Skip over the next 4 bytes
289 Number_Of_Characters := Number_Of_Characters - 4;
291 -- Get each subsection in turn
298 Number_Of_Characters := Number_Of_Characters - 8;
299 Nchars := Nchars - 8;
301 -- If this is a symbol and the V_DEF flag is set, get the
304 if Code = C_SYM and then ((Flags and V_DEF_Mask) /= 0) then
305 -- First, reach the symbol length
307 for J in 1 .. 25 loop
309 Nchars := Nchars - 1;
310 Number_Of_Characters := Number_Of_Characters - 1;
313 Length := Byte'Pos (B);
316 -- Get the symbol characters
318 for J in 1 .. Nchars loop
320 Number_Of_Characters := Number_Of_Characters - 1;
324 Length := Length - 1;
328 -- Create the new Symbol
331 S_Data : Symbol_Data;
333 S_Data.Name := new String'(Symbol (1 .. LSymb));
335 -- The symbol kind (Data or Procedure) depends on the
338 if (Flags and V_NORM_Mask) = 0 then
345 -- Put the new symbol in the table
347 Symbol_Table.Increment_Last (Complete_Symbols);
348 Complete_Symbols.Table
349 (Symbol_Table.Last (Complete_Symbols)) := S_Data;
353 -- As it is not a symbol subsection, skip to the next
356 for J in 1 .. Nchars loop
358 Number_Of_Characters := Number_Of_Characters - 1;
362 -- Exit the GSD section when number of characters reaches 0
364 exit when Number_Of_Characters = 0;
369 -- The object file has been processed, close it
374 -- For any exception, output an error message, close the object file
375 -- and return with Success = False.
378 Put_Line ("unexpected exception raised while processing """
379 & Object_File & """");
380 Put_Line (Exception_Information (X));
391 Success : out Boolean)
393 File : Ada.Text_IO.File_Type;
396 S_Data : Symbol_Data;
400 -- Most probable index in the Complete_Symbols of the current symbol
401 -- in Original_Symbol.
406 -- Nothing to be done if Initialize has never been called
408 if Symbol_File_Name = null then
413 -- First find if the symbols in the symbol file are also in the
416 -- Expect the first symbol in the symbol file to also be the first
417 -- in Complete_Symbols.
421 for Index_1 in 1 .. Symbol_Table.Last (Original_Symbols) loop
422 S_Data := Original_Symbols.Table (Index_1);
426 for Index_2 in Cur .. Symbol_Table.Last (Complete_Symbols) loop
427 if Equal (S_Data, Complete_Symbols.Table (Index_2)) then
429 Complete_Symbols.Table (Index_2).Present := False;
431 exit First_Object_Loop;
433 end loop First_Object_Loop;
435 -- If the symbol could not be found between Cur and Last, try
440 for Index_2 in 1 .. Cur - 1 loop
441 if Equal (S_Data, Complete_Symbols.Table (Index_2)) then
443 Complete_Symbols.Table (Index_2).Present := False;
445 exit Second_Object_Loop;
447 end loop Second_Object_Loop;
450 -- If the symbol is not found, mark it as such in the table
454 Put_Line ("symbol """ & S_Data.Name.all &
455 """ is no longer present in the object files");
458 Original_Symbols.Table (Index_1).Present := False;
459 Free (Original_Symbols.Table (Index_1).Name);
463 -- Append additional symbols, if any, to the Original_Symbols table
465 for Index in 1 .. Symbol_Table.Last (Complete_Symbols) loop
466 S_Data := Complete_Symbols.Table (Index);
468 if S_Data.Present then
469 Symbol_Table.Increment_Last (Original_Symbols);
470 Original_Symbols.Table (Symbol_Table.Last (Original_Symbols)) :=
472 Complete_Symbols.Table (Index).Present := False;
476 -- Create the symbol file
478 Create (File, Ada.Text_IO.Out_File, Symbol_File_Name.all);
480 Put (File, Case_Sensitive);
481 Put_Line (File, "yes");
483 -- Put a line in the symbol file for each symbol in the symbol table
485 for Index in 1 .. Symbol_Table.Last (Original_Symbols) loop
486 if Original_Symbols.Table (Index).Present then
487 Put (File, Symbol_Vector);
488 Put (File, Original_Symbols.Table (Index).Name.all);
490 if Original_Symbols.Table (Index).Kind = Data then
491 Put_Line (File, Equal_Data);
494 Put_Line (File, Equal_Procedure);
497 Free (Original_Symbols.Table (Index).Name);
501 Put (File, Case_Sensitive);
502 Put_Line (File, "NO");
510 Symbol_Table.Set_Last (Original_Symbols, 0);
511 Symbol_Table.Set_Last (Complete_Symbols, 0);
513 -- Clear the symbol file name
515 Free (Symbol_File_Name);
522 Put_Line ("unexpected exception raised while finalizing """
523 & Symbol_File_Name.all & """");
524 Put_Line (Exception_Information (X));