1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                              G N A T S Y M                               --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 2003-2004 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 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.                                                      --
21--                                                                          --
22-- GNAT was originally developed  by the GNAT team at  New York University. --
23-- Extensive contributions were provided by Ada Core Technologies Inc.      --
24--                                                                          --
25------------------------------------------------------------------------------
26
27--  This utility application creates symbol files in a format that is
28--  platform-dependent.
29
30--  A symbol file is a text file that lists the symbols to be exported from
31--  a shared library. The format of a symbol file depends on the platform;
32--  it may be a simple enumeration of the symbol (one per line) or a more
33--  elaborate format (on VMS, for example). A symbol file may be used as an
34--  input to the platform linker when building a shared library.
35
36--  This utility is not available on all platforms. It is currently supported
37--  only on OpenVMS.
38
39--  gnatsym takes as parameters:
40--    - the name of the symbol file to create
41--    - (optional) the policy to create the symbol file
42--    - (optional) the name of the reference symbol file
43--    - the names of one or more object files where the symbols are found
44
45with GNAT.Command_Line; use GNAT.Command_Line;
46with GNAT.OS_Lib;       use GNAT.OS_Lib;
47
48with Gnatvsn; use Gnatvsn;
49with Osint;   use Osint;
50with Output;  use Output;
51
52with Symbols; use Symbols;
53with Table;
54
55procedure Gnatsym is
56
57   Empty_String : aliased String := "";
58   Empty        : constant String_Access := Empty_String'Unchecked_Access;
59   --  To initialize variables Reference and Version_String
60
61   Copyright_Displayed : Boolean := False;
62   --  A flag to prevent multiple display of the Copyright notice
63
64   Success : Boolean := True;
65
66   Symbol_Policy : Policy := Autonomous;
67
68   Verbose : Boolean := False;
69   --  True when -v switch is used
70
71   Quiet : Boolean := False;
72   --  True when -q switch is used
73
74   Symbol_File_Name : String_Access := null;
75   --  The name of the symbol file
76
77   Reference_Symbol_File_Name : String_Access := Empty;
78   --  The name of the reference symbol file
79
80   Version_String : String_Access := Empty;
81   --  The version of the library. Used on VMS.
82
83   package Object_Files is new Table.Table
84     (Table_Component_Type => String_Access,
85      Table_Index_Type     => Natural,
86      Table_Low_Bound      => 0,
87      Table_Initial        => 10,
88      Table_Increment      => 10,
89      Table_Name           => "Gnatsymb.Object_Files");
90   --  A table to store the object file names
91
92   Object_File : Natural := 0;
93   --  An index to traverse the Object_Files table
94
95   procedure Display_Copyright;
96   --  Display Copyright notice
97
98   procedure Parse_Cmd_Line;
99   --  Parse the command line switches and file names
100
101   procedure Usage;
102   --  Display the usage
103
104   -----------------------
105   -- Display_Copyright --
106   -----------------------
107
108   procedure Display_Copyright is
109   begin
110      if not Copyright_Displayed then
111         Write_Eol;
112         Write_Str ("GNATSYMB ");
113         Write_Str (Gnat_Version_String);
114         Write_Str (" Copyright 2003-2004 Free Software Foundation, Inc");
115         Write_Eol;
116         Copyright_Displayed := True;
117      end if;
118   end Display_Copyright;
119
120   --------------------
121   -- Parse_Cmd_Line --
122   --------------------
123
124   procedure Parse_Cmd_Line is
125   begin
126      loop
127         case GNAT.Command_Line.Getopt ("c C q r: s: v V:") is
128            when ASCII.NUL =>
129               exit;
130
131            when 'c' =>
132               Symbol_Policy := Compliant;
133
134            when 'C' =>
135               Symbol_Policy := Controlled;
136
137            when 'q' =>
138               Quiet := True;
139
140            when 'r' =>
141               Reference_Symbol_File_Name :=
142                 new String'(GNAT.Command_Line.Parameter);
143
144            when 's' =>
145               Symbol_File_Name := new String'(GNAT.Command_Line.Parameter);
146
147            when 'v' =>
148               Verbose := True;
149
150            when 'V' =>
151               Version_String := new String'(GNAT.Command_Line.Parameter);
152
153            when others =>
154               Fail ("invalid switch: ", Full_Switch);
155         end case;
156      end loop;
157
158      --  Get the file names
159
160      loop
161         declare
162            S : constant String_Access :=
163                           new String'(GNAT.Command_Line.Get_Argument);
164
165         begin
166            exit when S'Length = 0;
167
168            Object_Files.Increment_Last;
169            Object_Files.Table (Object_Files.Last) := S;
170         end;
171      end loop;
172   exception
173      when Invalid_Switch =>
174         Usage;
175         Fail ("invalid switch : ", Full_Switch);
176   end Parse_Cmd_Line;
177
178   -----------
179   -- Usage --
180   -----------
181
182   procedure Usage is
183   begin
184      Write_Line ("gnatsym [options] object_file {object_file}");
185      Write_Eol;
186      Write_Line ("   -c       Compliant policy");
187      Write_Line ("   -C       Controlled policy");
188      Write_Line ("   -q       Quiet mode");
189      Write_Line ("   -r<ref>  Reference symbol file name");
190      Write_Line ("   -s<sym>  Symbol file name");
191      Write_Line ("   -v       Verbose mode");
192      Write_Line ("   -V<ver>  Version");
193      Write_Eol;
194      Write_Line ("Specifying a symbol file with -s<sym> is compulsory");
195      Write_Eol;
196   end Usage;
197
198--  Start of processing of Gnatsym
199
200begin
201   --  Initialize Object_Files table
202
203   Object_Files.Set_Last (0);
204
205   --  Parse the command line
206
207   Parse_Cmd_Line;
208
209   if Verbose then
210      Display_Copyright;
211   end if;
212
213   --  If there is no symbol file or no object files on the command line,
214   --  display the usage and exit with an error status.
215
216   if Symbol_File_Name = null or else Object_Files.Last = 0 then
217      Usage;
218      OS_Exit (1);
219
220   else
221      if Verbose then
222         Write_Str ("Initializing symbol file """);
223         Write_Str (Symbol_File_Name.all);
224         Write_Line ("""");
225      end if;
226
227      --  Initialize symbol file and, if specified, read reference file
228
229      Symbols.Initialize
230        (Symbol_File   => Symbol_File_Name.all,
231         Reference     => Reference_Symbol_File_Name.all,
232         Symbol_Policy => Symbol_Policy,
233         Quiet         => Quiet,
234         Version       => Version_String.all,
235         Success       => Success);
236
237      --  Process the object files in order. Stop as soon as there is
238      --  something wrong.
239
240      Object_File := 0;
241
242      while Success and then Object_File < Object_Files.Last loop
243         Object_File := Object_File + 1;
244
245         if Verbose then
246            Write_Str ("Processing object file """);
247            Write_Str (Object_Files.Table (Object_File).all);
248            Write_Line ("""");
249         end if;
250
251         Process (Object_Files.Table (Object_File).all, Success);
252      end loop;
253
254      --  Finalize the object file
255
256      if Success then
257         if Verbose then
258            Write_Str ("Finalizing """);
259            Write_Str (Symbol_File_Name.all);
260            Write_Line ("""");
261         end if;
262
263         Finalize (Quiet, Success);
264      end if;
265
266      --  Fail if there was anything wrong
267
268      if not Success then
269         Fail ("unable to build symbol file");
270      end if;
271   end if;
272end Gnatsym;
273