1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2010, 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 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. 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- This utility application creates symbol files in a format that is
27 -- platform-dependent.
29 -- A symbol file is a text file that lists the symbols to be exported from
30 -- a shared library. The format of a symbol file depends on the platform;
31 -- it may be a simple enumeration of the symbol (one per line) or a more
32 -- elaborate format (on VMS, for example). A symbol file may be used as an
33 -- input to the platform linker when building a shared library.
35 -- This utility is not available on all platforms. It is currently supported
38 -- gnatsym takes as parameters:
39 -- - the name of the symbol file to create
40 -- - (optional) the policy to create the symbol file
41 -- - (optional) the name of the reference symbol file
42 -- - the names of one or more object files where the symbols are found
44 with Gnatvsn; use Gnatvsn;
45 with Osint; use Osint;
46 with Output; use Output;
47 with Symbols; use Symbols;
50 with Ada.Exceptions; use Ada.Exceptions;
51 with Ada.Text_IO; use Ada.Text_IO;
53 with GNAT.Command_Line; use GNAT.Command_Line;
54 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
55 with GNAT.OS_Lib; use GNAT.OS_Lib;
59 Empty_String : aliased String := "";
60 Empty : constant String_Access := Empty_String'Unchecked_Access;
61 -- To initialize variables Reference and Version_String
63 Copyright_Displayed : Boolean := False;
64 -- A flag to prevent multiple display of the Copyright notice
66 Success : Boolean := True;
68 Symbol_Policy : Policy := Autonomous;
70 Verbose : Boolean := False;
71 -- True when -v switch is used
73 Quiet : Boolean := False;
74 -- True when -q switch is used
76 Symbol_File_Name : String_Access := null;
77 -- The name of the symbol file
79 Reference_Symbol_File_Name : String_Access := Empty;
80 -- The name of the reference symbol file
82 Version_String : String_Access := Empty;
83 -- The version of the library (used on VMS)
85 type Object_File_Data is record
90 package Object_Files is new Table.Table
91 (Table_Component_Type => Object_File_Data,
92 Table_Index_Type => Natural,
95 Table_Increment => 100,
96 Table_Name => "Gnatsymb.Object_Files");
97 -- A table to store the object file names
99 Object_File : Natural := 0;
100 -- An index to traverse the Object_Files table
102 procedure Display_Copyright;
103 -- Display Copyright notice
105 procedure Parse_Cmd_Line;
106 -- Parse the command line switches and file names
111 -----------------------
112 -- Display_Copyright --
113 -----------------------
115 procedure Display_Copyright is
117 if not Copyright_Displayed then
119 Write_Str ("GNATSYMB ");
120 Write_Str (Gnat_Version_String);
122 Write_Str ("Copyright 2003-2004 Free Software Foundation, Inc");
124 Copyright_Displayed := True;
126 end Display_Copyright;
132 procedure Parse_Cmd_Line is
135 case GNAT.Command_Line.Getopt ("c C D q r: R s: v V:") is
140 Symbol_Policy := Compliant;
143 Symbol_Policy := Controlled;
146 Symbol_Policy := Direct;
152 Reference_Symbol_File_Name :=
153 new String'(GNAT.Command_Line.Parameter);
156 Symbol_Policy := Restricted;
159 Symbol_File_Name := new String'(GNAT.Command_Line.Parameter);
165 Version_String := new String'(GNAT.Command_Line.Parameter);
168 Fail ("invalid switch: " & Full_Switch);
172 -- Get the object file names and put them in the table in alphabetical
173 -- order of base names.
177 S : constant String_Access :=
178 new String'(GNAT.Command_Line.Get_Argument);
181 exit when S'Length = 0;
183 Object_Files.Increment_Last;
186 Base : constant String := Base_Name (S.all);
187 Last : constant Positive := Object_Files.Last;
193 if Object_Files.Table (J).Name.all > Base then
194 Object_Files.Table (J + 1 .. Last) :=
195 Object_Files.Table (J .. Last - 1);
202 Object_Files.Table (J) := (S, new String'(Base));
207 when Invalid_Switch =>
209 Fail ("invalid switch : " & Full_Switch);
218 Write_Line ("gnatsym [options] object_file {object_file}");
220 Write_Line (" -c Compliant symbol policy");
221 Write_Line (" -C Controlled symbol policy");
222 Write_Line (" -q Quiet mode");
223 Write_Line (" -r<ref> Reference symbol file name");
224 Write_Line (" -R Restricted symbol policy");
225 Write_Line (" -s<sym> Symbol file name");
226 Write_Line (" -v Verbose mode");
227 Write_Line (" -V<ver> Version");
229 Write_Line ("Specifying a symbol file with -s<sym> is compulsory");
233 -- Start of processing of Gnatsym
236 -- Initialize Object_Files table
238 Object_Files.Set_Last (0);
240 -- Parse the command line
248 -- If there is no symbol file or no object files on the command line,
249 -- display the usage and exit with an error status.
251 if Symbol_File_Name = null or else Object_Files.Last = 0 then
255 -- When symbol policy is direct, simply copy the reference symbol file to
258 elsif Symbol_Policy = Direct then
260 File_In : Ada.Text_IO.File_Type;
261 File_Out : Ada.Text_IO.File_Type;
262 Line : String (1 .. 1_000);
267 Open (File_In, In_File, Reference_Symbol_File_Name.all);
273 ("could not open """ &
274 Reference_Symbol_File_Name.all
276 Put_Line (Exception_Message (X));
283 Create (File_Out, Out_File, Symbol_File_Name.all);
289 ("could not create """ & Symbol_File_Name.all & """");
290 Put_Line (Exception_Message (X));
296 while not End_Of_File (File_In) loop
297 Get_Line (File_In, Line, Last);
298 Put_Line (File_Out, Line (1 .. Last));
307 Write_Str ("Initializing symbol file """);
308 Write_Str (Symbol_File_Name.all);
312 -- Initialize symbol file and, if specified, read reference file
315 (Symbol_File => Symbol_File_Name.all,
316 Reference => Reference_Symbol_File_Name.all,
317 Symbol_Policy => Symbol_Policy,
319 Version => Version_String.all,
322 -- Process the object files in order. Stop as soon as there is
327 while Success and then Object_File < Object_Files.Last loop
328 Object_File := Object_File + 1;
331 Write_Str ("Processing object file """);
332 Write_Str (Object_Files.Table (Object_File).Path.all);
337 (Object_Files.Table (Object_File).Path.all,
341 -- Finalize the symbol file
345 Write_Str ("Finalizing """);
346 Write_Str (Symbol_File_Name.all);
350 Finalize (Quiet, Success);
353 -- Fail if there was anything wrong
356 Fail ("unable to build symbol file");