OSDN Git Service

2010-06-22 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnatlink.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             G N A T L I N K                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1996-2009, 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 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.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 --  Gnatlink usage: please consult the gnat documentation
27
28 with ALI;      use ALI;
29 with Csets;
30 with Gnatvsn;  use Gnatvsn;
31 with Hostparm;
32 with Indepsw;  use Indepsw;
33 with Namet;    use Namet;
34 with Opt;
35 with Osint;    use Osint;
36 with Output;   use Output;
37 with Snames;
38 with Switch;   use Switch;
39 with System;   use System;
40 with Table;
41 with Targparm; use Targparm;
42 with Types;
43
44 with Ada.Command_Line;     use Ada.Command_Line;
45 with Ada.Exceptions;       use Ada.Exceptions;
46
47 with System.OS_Lib;        use System.OS_Lib;
48 with System.CRTL;
49
50 with Interfaces.C_Streams; use Interfaces.C_Streams;
51 with Interfaces.C.Strings; use Interfaces.C.Strings;
52
53 procedure Gnatlink is
54    pragma Ident (Gnatvsn.Gnat_Static_Version_String);
55
56    Shared_Libgcc_String : constant String := "-shared-libgcc";
57    Shared_Libgcc        : constant String_Access :=
58                             new String'(Shared_Libgcc_String);
59    --  Used to invoke gcc when the binder is invoked with -shared
60
61    Static_Libgcc_String : constant String := "-static-libgcc";
62    Static_Libgcc        : constant String_Access :=
63                             new String'(Static_Libgcc_String);
64    --  Used to invoke gcc when shared libs are not used
65
66    package Gcc_Linker_Options is new Table.Table (
67      Table_Component_Type => String_Access,
68      Table_Index_Type     => Integer,
69      Table_Low_Bound      => 1,
70      Table_Initial        => 20,
71      Table_Increment      => 100,
72      Table_Name           => "Gnatlink.Gcc_Linker_Options");
73    --  Comments needed ???
74
75    package Libpath is new Table.Table (
76      Table_Component_Type => Character,
77      Table_Index_Type     => Integer,
78      Table_Low_Bound      => 1,
79      Table_Initial        => 4096,
80      Table_Increment      => 100,
81      Table_Name           => "Gnatlink.Libpath");
82    --  Comments needed ???
83
84    package Linker_Options is new Table.Table (
85      Table_Component_Type => String_Access,
86      Table_Index_Type     => Integer,
87      Table_Low_Bound      => 1,
88      Table_Initial        => 20,
89      Table_Increment      => 100,
90      Table_Name           => "Gnatlink.Linker_Options");
91    --  Comments needed ???
92
93    package Linker_Objects is new Table.Table (
94      Table_Component_Type => String_Access,
95      Table_Index_Type     => Integer,
96      Table_Low_Bound      => 1,
97      Table_Initial        => 20,
98      Table_Increment      => 100,
99      Table_Name           => "Gnatlink.Linker_Objects");
100    --  This table collects the objects file to be passed to the linker. In the
101    --  case where the linker command line is too long then programs objects
102    --  are put on the Response_File_Objects table. Note that the binder object
103    --  file and the user's objects remain in this table. This is very
104    --  important because on the GNU linker command line the -L switch is not
105    --  used to look for objects files but -L switch is used to look for
106    --  objects listed in the response file. This is not a problem with the
107    --  applications objects as they are specified with a full name.
108
109    package Response_File_Objects is new Table.Table (
110      Table_Component_Type => String_Access,
111      Table_Index_Type     => Integer,
112      Table_Low_Bound      => 1,
113      Table_Initial        => 20,
114      Table_Increment      => 100,
115      Table_Name           => "Gnatlink.Response_File_Objects");
116    --  This table collects the objects file that are to be put in the response
117    --  file. Only application objects are collected there (see details in
118    --  Linker_Objects table comments)
119
120    package Binder_Options_From_ALI is new Table.Table (
121      Table_Component_Type => String_Access,
122      Table_Index_Type     => Integer,
123      Table_Low_Bound      => 1, -- equals low bound of Argument_List for Spawn
124      Table_Initial        => 20,
125      Table_Increment      => 100,
126      Table_Name           => "Gnatlink.Binder_Options_From_ALI");
127    --  This table collects the switches from the ALI file of the main
128    --  subprogram.
129
130    package Binder_Options is new Table.Table (
131      Table_Component_Type => String_Access,
132      Table_Index_Type     => Integer,
133      Table_Low_Bound      => 1, -- equals low bound of Argument_List for Spawn
134      Table_Initial        => 20,
135      Table_Increment      => 100,
136      Table_Name           => "Gnatlink.Binder_Options");
137    --  This table collects the arguments to be passed to compile the binder
138    --  generated file.
139
140    Gcc : String_Access := Program_Name ("gcc", "gnatlink");
141
142    Read_Mode : constant String := "r" & ASCII.NUL;
143
144    Begin_Info : String := "--  BEGIN Object file/option list";
145    End_Info   : String := "--  END Object file/option list   ";
146    --  Note: above lines are modified in C mode, see option processing
147
148    Gcc_Path             : String_Access;
149    Linker_Path          : String_Access;
150    Output_File_Name     : String_Access;
151    Ali_File_Name        : String_Access;
152    Binder_Spec_Src_File : String_Access;
153    Binder_Body_Src_File : String_Access;
154    Binder_Ali_File      : String_Access;
155    Binder_Obj_File      : String_Access;
156
157    Tname    : Temp_File_Name;
158    Tname_FD : File_Descriptor := Invalid_FD;
159    --  Temporary file used by linker to pass list of object files on
160    --  certain systems with limitations on size of arguments.
161
162    Lname : String_Access := null;
163    --  File used by linker for CLI target, used to concatenate all .il files
164    --  when the command line passed to ilasm is too long
165
166    Debug_Flag_Present : Boolean := False;
167    Verbose_Mode       : Boolean := False;
168    Very_Verbose_Mode  : Boolean := False;
169
170    Ada_Bind_File : Boolean := True;
171    --  Set to True if bind file is generated in Ada
172
173    Standard_Gcc : Boolean := True;
174
175    Compile_Bind_File : Boolean := True;
176    --  Set to False if bind file is not to be compiled
177
178    Create_Map_File : Boolean := False;
179    --  Set to True by switch -M. The map file name is derived from
180    --  the ALI file name (mainprog.ali => mainprog.map).
181
182    Object_List_File_Supported : Boolean;
183    for Object_List_File_Supported'Size use Character'Size;
184    pragma Import
185      (C, Object_List_File_Supported, "__gnat_objlist_file_supported");
186    --  Predicate indicating whether the linker has an option whereby the
187    --  names of object files can be passed to the linker in a file.
188
189    Object_List_File_Required : Boolean := False;
190    --  Set to True to force generation of a response file
191
192    Shared_Libgcc_Default : Character;
193    for Shared_Libgcc_Default'Size use Character'Size;
194    pragma Import
195      (C, Shared_Libgcc_Default, "__gnat_shared_libgcc_default");
196    --  Indicates wether libgcc should be statically linked (use 'T') or
197    --  dynamically linked (use 'H') by default.
198
199    function Base_Name (File_Name : String) return String;
200    --  Return just the file name part without the extension (if present)
201
202    procedure Delete (Name : String);
203    --  Wrapper to unlink as status is ignored by this application
204
205    procedure Error_Msg (Message : String);
206    --  Output the error or warning Message
207
208    procedure Exit_With_Error (Error : String);
209    --  Output Error and exit program with a fatal condition
210
211    procedure Process_Args;
212    --  Go through all the arguments and build option tables
213
214    procedure Process_Binder_File (Name : String);
215    --  Reads the binder file and extracts linker arguments
216
217    procedure Usage;
218    --  Display usage
219
220    procedure Write_Header;
221    --  Show user the program name, version and copyright
222
223    procedure Write_Usage;
224    --  Show user the program options
225
226    ---------------
227    -- Base_Name --
228    ---------------
229
230    function Base_Name (File_Name : String) return String is
231       Findex1 : Natural;
232       Findex2 : Natural;
233
234    begin
235       Findex1 := File_Name'First;
236
237       --  The file might be specified by a full path name. However,
238       --  we want the path to be stripped away.
239
240       for J in reverse File_Name'Range loop
241          if Is_Directory_Separator (File_Name (J)) then
242             Findex1 := J + 1;
243             exit;
244          end if;
245       end loop;
246
247       Findex2 := File_Name'Last;
248       while Findex2 > Findex1
249         and then File_Name (Findex2) /=  '.'
250       loop
251          Findex2 := Findex2 - 1;
252       end loop;
253
254       if Findex2 = Findex1 then
255          Findex2 := File_Name'Last + 1;
256       end if;
257
258       return File_Name (Findex1 .. Findex2 - 1);
259    end Base_Name;
260
261    ------------
262    -- Delete --
263    ------------
264
265    procedure Delete (Name : String) is
266       Status : int;
267       pragma Unreferenced (Status);
268    begin
269       Status := unlink (Name'Address);
270       --  Is it really right to ignore an error here ???
271    end Delete;
272
273    ---------------
274    -- Error_Msg --
275    ---------------
276
277    procedure Error_Msg (Message : String) is
278    begin
279       Write_Str (Base_Name (Command_Name));
280       Write_Str (": ");
281       Write_Str (Message);
282       Write_Eol;
283    end Error_Msg;
284
285    ---------------------
286    -- Exit_With_Error --
287    ---------------------
288
289    procedure Exit_With_Error (Error : String) is
290    begin
291       Error_Msg (Error);
292       Exit_Program (E_Fatal);
293    end Exit_With_Error;
294
295    ------------------
296    -- Process_Args --
297    ------------------
298
299    procedure Process_Args is
300       Next_Arg  : Integer;
301       Skip_Next : Boolean := False;
302       --  Set to true if the next argument is to be added into the list of
303       --  linker's argument without parsing it.
304
305       procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
306
307       --  Start of processing for Process_Args
308
309    begin
310       --  First, check for --version and --help
311
312       Check_Version_And_Help ("GNATLINK", "1995");
313
314       --  Loop through arguments of gnatlink command
315
316       Next_Arg := 1;
317       loop
318          exit when Next_Arg > Argument_Count;
319
320          Process_One_Arg : declare
321             Arg : constant String := Argument (Next_Arg);
322
323          begin
324             --  Case of argument which is a switch
325
326             --  We definitely need section by section comments here ???
327
328             if Skip_Next then
329
330                --  This argument must not be parsed, just add it to the
331                --  list of linker's options.
332
333                Skip_Next := False;
334
335                Linker_Options.Increment_Last;
336                Linker_Options.Table (Linker_Options.Last) :=
337                  new String'(Arg);
338
339             elsif Arg'Length /= 0 and then Arg (1) = '-' then
340                if Arg'Length > 4 and then Arg (2 .. 5) =  "gnat" then
341                   Exit_With_Error
342                     ("invalid switch: """ & Arg & """ (gnat not needed here)");
343                end if;
344
345                if Arg = "-Xlinker" then
346
347                   --  Next argument should be sent directly to the linker.
348                   --  We do not want to parse it here.
349
350                   Skip_Next := True;
351
352                   Linker_Options.Increment_Last;
353                   Linker_Options.Table (Linker_Options.Last) :=
354                     new String'(Arg);
355
356                elsif Arg (2) = 'g'
357                  and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
358                then
359                   Debug_Flag_Present := True;
360
361                   Linker_Options.Increment_Last;
362                   Linker_Options.Table (Linker_Options.Last) :=
363                    new String'(Arg);
364
365                   Binder_Options.Increment_Last;
366                   Binder_Options.Table (Binder_Options.Last) :=
367                     Linker_Options.Table (Linker_Options.Last);
368
369                elsif Arg'Length >= 3 and then Arg (2) = 'M' then
370                   declare
371                      Switches : String_List_Access;
372
373                   begin
374                      Convert (Map_File, Arg (3 .. Arg'Last), Switches);
375
376                      if Switches /= null then
377                         for J in Switches'Range loop
378                            Linker_Options.Increment_Last;
379                            Linker_Options.Table (Linker_Options.Last) :=
380                              Switches (J);
381                         end loop;
382                      end if;
383                   end;
384
385                elsif Arg'Length = 2 then
386                   case Arg (2) is
387                      when 'A' =>
388                         Ada_Bind_File := True;
389                         Begin_Info := "--  BEGIN Object file/option list";
390                         End_Info   := "--  END Object file/option list   ";
391
392                      when 'b' =>
393                         Linker_Options.Increment_Last;
394                         Linker_Options.Table (Linker_Options.Last) :=
395                           new String'(Arg);
396
397                         Binder_Options.Increment_Last;
398                         Binder_Options.Table (Binder_Options.Last) :=
399                           Linker_Options.Table (Linker_Options.Last);
400
401                         Next_Arg := Next_Arg + 1;
402
403                         if Next_Arg > Argument_Count then
404                            Exit_With_Error ("Missing argument for -b");
405                         end if;
406
407                         Get_Machine_Name : declare
408                            Name_Arg : constant String_Access :=
409                                         new String'(Argument (Next_Arg));
410
411                         begin
412                            Linker_Options.Increment_Last;
413                            Linker_Options.Table (Linker_Options.Last) :=
414                              Name_Arg;
415
416                            Binder_Options.Increment_Last;
417                            Binder_Options.Table (Binder_Options.Last) :=
418                              Name_Arg;
419
420                         end Get_Machine_Name;
421
422                      when 'C' =>
423                         Ada_Bind_File := False;
424                         Begin_Info := "/*  BEGIN Object file/option list";
425                         End_Info   := "    END Object file/option list */";
426
427                      when 'f' =>
428                         if Object_List_File_Supported then
429                            Object_List_File_Required := True;
430                         else
431                            Exit_With_Error
432                              ("Object list file not supported on this target");
433                         end if;
434
435                      when 'M' =>
436                         Create_Map_File := True;
437
438                      when 'n' =>
439                         Compile_Bind_File := False;
440
441                      when 'o' =>
442                         Next_Arg := Next_Arg + 1;
443
444                         if Next_Arg > Argument_Count then
445                            Exit_With_Error ("Missing argument for -o");
446                         end if;
447
448                         Output_File_Name :=
449                           new String'(Executable_Name
450                                         (Argument (Next_Arg),
451                                          Only_If_No_Suffix => True));
452
453                      when 'R' =>
454                         Opt.Run_Path_Option := False;
455
456                      when 'v' =>
457
458                         --  Support "double" verbose mode.  Second -v
459                         --  gets sent to the linker and binder phases.
460
461                         if Verbose_Mode then
462                            Very_Verbose_Mode := True;
463
464                            Linker_Options.Increment_Last;
465                            Linker_Options.Table (Linker_Options.Last) :=
466                             new String'(Arg);
467
468                            Binder_Options.Increment_Last;
469                            Binder_Options.Table (Binder_Options.Last) :=
470                              Linker_Options.Table (Linker_Options.Last);
471
472                         else
473                            Verbose_Mode := True;
474
475                         end if;
476
477                      when others =>
478                         Linker_Options.Increment_Last;
479                         Linker_Options.Table (Linker_Options.Last) :=
480                          new String'(Arg);
481
482                   end case;
483
484                elsif Arg (2) = 'B' then
485                   Linker_Options.Increment_Last;
486                   Linker_Options.Table (Linker_Options.Last) :=
487                     new String'(Arg);
488
489                   Binder_Options.Increment_Last;
490                   Binder_Options.Table (Binder_Options.Last) :=
491                     Linker_Options.Table (Linker_Options.Last);
492
493                elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
494                   if Arg'Length = 7 then
495                      Exit_With_Error ("Missing argument for --LINK=");
496                   end if;
497
498                   Linker_Path :=
499                     System.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
500
501                   if Linker_Path = null then
502                      Exit_With_Error
503                        ("Could not locate linker: " & Arg (8 .. Arg'Last));
504                   end if;
505
506                elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
507                   declare
508                      Program_Args : constant Argument_List_Access :=
509                                       Argument_String_To_List
510                                                  (Arg (7 .. Arg'Last));
511
512                   begin
513                      if Program_Args.all (1).all /= Gcc.all then
514                         Gcc := new String'(Program_Args.all (1).all);
515                         Standard_Gcc := False;
516                      end if;
517
518                      --  Set appropriate flags for switches passed
519
520                      for J in 2 .. Program_Args.all'Last loop
521                         declare
522                            Arg : constant String := Program_Args.all (J).all;
523                            AF  : constant Integer := Arg'First;
524
525                         begin
526                            if Arg'Length /= 0 and then Arg (AF) = '-' then
527                               if Arg (AF + 1) = 'g'
528                                 and then (Arg'Length = 2
529                                   or else Arg (AF + 2) in '0' .. '3'
530                                   or else Arg (AF + 2 .. Arg'Last) = "coff")
531                               then
532                                  Debug_Flag_Present := True;
533                               end if;
534                            end if;
535
536                            --  Add directory to source search dirs so that
537                            --  Get_Target_Parameters can find system.ads
538
539                            if Arg (AF .. AF + 1) = "-I"
540                              and then Arg'Length > 2
541                            then
542                               Add_Src_Search_Dir (Arg (AF + 2 .. Arg'Last));
543                            end if;
544
545                            --  Pass to gcc for compiling binder generated file
546                            --  No use passing libraries, it will just generate
547                            --  a warning
548
549                            if not (Arg (AF .. AF + 1) = "-l"
550                              or else Arg (AF .. AF + 1) = "-L")
551                            then
552                               Binder_Options.Increment_Last;
553                               Binder_Options.Table (Binder_Options.Last) :=
554                                 new String'(Arg);
555                            end if;
556
557                            --  Pass to gcc for linking program
558
559                            Gcc_Linker_Options.Increment_Last;
560                            Gcc_Linker_Options.Table
561                              (Gcc_Linker_Options.Last) := new String'(Arg);
562                         end;
563                      end loop;
564                   end;
565
566                --  Send all multi-character switches not recognized as
567                --  a special case by gnatlink to the linker/loader stage.
568
569                else
570                   Linker_Options.Increment_Last;
571                   Linker_Options.Table (Linker_Options.Last) :=
572                     new String'(Arg);
573                end if;
574
575             --  Here if argument is a file name rather than a switch
576
577             else
578                --  If explicit ali file, capture it
579
580                if Arg'Length > 4
581                  and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
582                then
583                   if Ali_File_Name = null then
584                      Ali_File_Name := new String'(Arg);
585                   else
586                      Exit_With_Error ("cannot handle more than one ALI file");
587                   end if;
588
589                --  If target object file, record object file
590
591                elsif Arg'Length > Get_Target_Object_Suffix.all'Length
592                  and then Arg
593                    (Arg'Last -
594                     Get_Target_Object_Suffix.all'Length + 1 .. Arg'Last)
595                    = Get_Target_Object_Suffix.all
596                then
597                   Linker_Objects.Increment_Last;
598                   Linker_Objects.Table (Linker_Objects.Last) :=
599                     new String'(Arg);
600
601                --  If host object file, record object file
602                --  e.g. accept foo.o as well as foo.obj on VMS target
603
604                elsif Arg'Length > Get_Object_Suffix.all'Length
605                  and then Arg
606                    (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
607                                                 = Get_Object_Suffix.all
608                then
609                   Linker_Objects.Increment_Last;
610                   Linker_Objects.Table (Linker_Objects.Last) :=
611                     new String'(Arg);
612
613                --  If corresponding ali file exists, capture it
614
615                elsif Ali_File_Name = null
616                  and then Is_Regular_File (Arg & ".ali")
617                then
618                   Ali_File_Name := new String'(Arg & ".ali");
619
620                --  Otherwise assume this is a linker options entry, but
621                --  see below for interesting adjustment to this assumption.
622
623                else
624                   Linker_Options.Increment_Last;
625                   Linker_Options.Table (Linker_Options.Last) :=
626                     new String'(Arg);
627                end if;
628             end if;
629          end Process_One_Arg;
630
631          Next_Arg := Next_Arg + 1;
632       end loop;
633
634       --  If Ada bind file, then compile it with warnings suppressed, because
635       --  otherwise the with of the main program may cause junk warnings.
636
637       if Ada_Bind_File then
638          Binder_Options.Increment_Last;
639          Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
640       end if;
641
642       --  If we did not get an ali file at all, and we had at least one
643       --  linker option, then assume that was the intended ali file after
644       --  all, so that we get a nicer message later on.
645
646       if Ali_File_Name = null
647         and then Linker_Options.Last >= Linker_Options.First
648       then
649          Ali_File_Name :=
650            new String'(Linker_Options.Table (Linker_Options.First).all &
651                                                                    ".ali");
652       end if;
653    end Process_Args;
654
655    -------------------------
656    -- Process_Binder_File --
657    -------------------------
658
659    procedure Process_Binder_File (Name : String) is
660       Fd : FILEs;
661       --  Binder file's descriptor
662
663       Link_Bytes : Integer := 0;
664       --  Projected number of bytes for the linker command line
665
666       Link_Max : Integer;
667       pragma Import (C, Link_Max, "__gnat_link_max");
668       --  Maximum number of bytes on the command line supported by the OS
669       --  linker. Passed this limit the response file mechanism must be used
670       --  if supported.
671
672       Next_Line : String (1 .. 1000);
673       --  Current line value
674
675       Nlast  : Integer;
676       Nfirst : Integer;
677       --  Current line slice (the slice does not contain line terminator)
678
679       Last : Integer;
680       --  Current line last character for shared libraries (without version)
681
682       Objs_Begin : Integer := 0;
683       --  First object file index in Linker_Objects table
684
685       Objs_End : Integer := 0;
686       --  Last object file index in Linker_Objects table
687
688       Status : int;
689       pragma Warnings (Off, Status);
690       --  Used for various Interfaces.C_Streams calls
691
692       Closing_Status : Boolean;
693       pragma Warnings (Off, Closing_Status);
694       --  For call to Close
695
696       GNAT_Static : Boolean := False;
697       --  Save state of -static option
698
699       GNAT_Shared : Boolean := False;
700       --  Save state of -shared option
701
702       Xlinker_Was_Previous : Boolean := False;
703       --  Indicate that "-Xlinker" was the option preceding the current
704       --  option. If True, then the current option is never suppressed.
705
706       --  Rollback data
707
708       --  These data items are used to store current binder file context.
709       --  The context is composed of the file descriptor position and the
710       --  current line together with the slice indexes (first and last
711       --  position) for this line. The rollback data are used by the
712       --  Store_File_Context and Rollback_File_Context routines below.
713       --  The file context mechanism interact only with the Get_Next_Line
714       --  call. For example:
715
716       --     Store_File_Context;
717       --     Get_Next_Line;
718       --     Rollback_File_Context;
719       --     Get_Next_Line;
720
721       --  Both Get_Next_Line calls above will read the exact same data from
722       --  the file. In other words, Next_Line, Nfirst and Nlast variables
723       --  will be set with the exact same values.
724
725       RB_File_Pos  : long;                -- File position
726       RB_Next_Line : String (1 .. 1000);  -- Current line content
727       RB_Nlast     : Integer;             -- Slice last index
728       RB_Nfirst    : Integer;             -- Slice first index
729
730       Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
731       pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
732       --  Pointer to string representing the native linker option which
733       --  specifies the path where the dynamic loader should find shared
734       --  libraries. Equal to null string if this system doesn't support it.
735
736       Libgcc_Subdir_Ptr : Interfaces.C.Strings.chars_ptr;
737       pragma Import (C, Libgcc_Subdir_Ptr, "__gnat_default_libgcc_subdir");
738       --  Pointer to string indicating the installation subdirectory where
739       --  a default shared libgcc might be found.
740
741       Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
742       pragma Import
743         (C, Object_Library_Ext_Ptr, "__gnat_object_library_extension");
744       --  Pointer to string specifying the default extension for
745       --  object libraries, e.g. Unix uses ".a", VMS uses ".olb".
746
747       Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
748       pragma Import (C, Object_File_Option_Ptr, "__gnat_object_file_option");
749       --  Pointer to a string representing the linker option which specifies
750       --  the response file.
751
752       Using_GNU_Linker : Boolean;
753       for Using_GNU_Linker'Size use Character'Size;
754       pragma Import (C, Using_GNU_Linker, "__gnat_using_gnu_linker");
755       --  Predicate indicating whether this target uses the GNU linker. In
756       --  this case we must output a GNU linker compatible response file.
757
758       Separate_Run_Path_Options : Boolean;
759       for Separate_Run_Path_Options'Size use Character'Size;
760       pragma Import
761         (C, Separate_Run_Path_Options, "__gnat_separate_run_path_options");
762       --  Whether separate rpath options should be emitted for each directory
763
764       Opening : aliased constant String := """";
765       Closing : aliased constant String := '"' & ASCII.LF;
766       --  Needed to quote object paths in object list files when GNU linker
767       --  is used.
768
769       procedure Get_Next_Line;
770       --  Read the next line from the binder file without the line
771       --  terminator.
772
773       function Index (S, Pattern : String) return Natural;
774       --  Return the last occurrence of Pattern in S, or 0 if none
775
776       function Is_Option_Present (Opt : String) return Boolean;
777       --  Return true if the option Opt is already present in
778       --  Linker_Options table.
779
780       procedure Store_File_Context;
781       --  Store current file context, Fd position and current line data.
782       --  The file context is stored into the rollback data above (RB_*).
783       --  Store_File_Context can be called at any time, only the last call
784       --  will be used (i.e. this routine overwrites the file context).
785
786       procedure Rollback_File_Context;
787       --  Restore file context from rollback data. This routine must be called
788       --  after Store_File_Context. The binder file context will be restored
789       --  with the data stored by the last Store_File_Context call.
790
791       -------------------
792       -- Get_Next_Line --
793       -------------------
794
795       procedure Get_Next_Line is
796          Fchars : chars;
797
798       begin
799          Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
800
801          if Fchars = System.Null_Address then
802             Exit_With_Error ("Error reading binder output");
803          end if;
804
805          Nfirst := Next_Line'First;
806          Nlast := Nfirst;
807          while Nlast <= Next_Line'Last
808            and then Next_Line (Nlast) /= ASCII.LF
809            and then Next_Line (Nlast) /= ASCII.CR
810          loop
811             Nlast := Nlast + 1;
812          end loop;
813
814          Nlast := Nlast - 1;
815       end Get_Next_Line;
816
817       -----------
818       -- Index --
819       -----------
820
821       function Index (S, Pattern : String) return Natural is
822          Len : constant Natural := Pattern'Length;
823
824       begin
825          for J in reverse S'First .. S'Last - Len + 1 loop
826             if Pattern = S (J .. J + Len - 1) then
827                return J;
828             end if;
829          end loop;
830
831          return 0;
832       end Index;
833
834       -----------------------
835       -- Is_Option_Present --
836       -----------------------
837
838       function Is_Option_Present (Opt : String) return Boolean is
839       begin
840          for I in 1 .. Linker_Options.Last loop
841
842             if Linker_Options.Table (I).all = Opt then
843                return True;
844             end if;
845
846          end loop;
847
848          return False;
849       end Is_Option_Present;
850
851       ---------------------------
852       -- Rollback_File_Context --
853       ---------------------------
854
855       procedure Rollback_File_Context is
856       begin
857          Next_Line := RB_Next_Line;
858          Nfirst    := RB_Nfirst;
859          Nlast     := RB_Nlast;
860          Status    := fseek (Fd, RB_File_Pos, Interfaces.C_Streams.SEEK_SET);
861
862          if Status = -1 then
863             Exit_With_Error ("Error setting file position");
864          end if;
865       end Rollback_File_Context;
866
867       ------------------------
868       -- Store_File_Context --
869       ------------------------
870
871       procedure Store_File_Context is
872          use type System.CRTL.long;
873       begin
874          RB_Next_Line := Next_Line;
875          RB_Nfirst    := Nfirst;
876          RB_Nlast     := Nlast;
877          RB_File_Pos  := ftell (Fd);
878
879          if RB_File_Pos = -1 then
880             Exit_With_Error ("Error getting file position");
881          end if;
882       end Store_File_Context;
883
884    --  Start of processing for Process_Binder_File
885
886    begin
887       Fd := fopen (Name'Address, Read_Mode'Address);
888
889       if Fd = NULL_Stream then
890          Exit_With_Error ("Failed to open binder output");
891       end if;
892
893       --  Skip up to the Begin Info line
894
895       loop
896          Get_Next_Line;
897          exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
898       end loop;
899
900       loop
901          Get_Next_Line;
902
903          --  Go to end when end line is reached (this will happen in
904          --  High_Integrity_Mode where no -L switches are generated)
905
906          exit when Next_Line (Nfirst .. Nlast) = End_Info;
907
908          if Ada_Bind_File then
909             Next_Line (Nfirst .. Nlast - 8) :=
910               Next_Line (Nfirst + 8 .. Nlast);
911             Nlast := Nlast - 8;
912          end if;
913
914          --  Go to next section when switches are reached
915
916          exit when Next_Line (1) = '-';
917
918          --  Otherwise we have another object file to collect
919
920          Linker_Objects.Increment_Last;
921
922          --  Mark the positions of first and last object files in case
923          --  they need to be placed with a named file on systems having
924          --  linker line limitations.
925
926          if Objs_Begin = 0 then
927             Objs_Begin := Linker_Objects.Last;
928          end if;
929
930          Linker_Objects.Table (Linker_Objects.Last) :=
931            new String'(Next_Line (Nfirst .. Nlast));
932
933          Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
934          --  Nlast - Nfirst + 1, for the size, plus one for the space between
935          --  each arguments.
936       end loop;
937
938       Objs_End := Linker_Objects.Last;
939
940       --  Continue to compute the Link_Bytes, the linker options are part of
941       --  command line length.
942
943       Store_File_Context;
944
945       while Next_Line (Nfirst .. Nlast) /= End_Info loop
946          Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
947          Get_Next_Line;
948       end loop;
949
950       Rollback_File_Context;
951
952       --  On systems that have limitations on handling very long linker lines
953       --  we make use of the system linker option which takes a list of object
954       --  file names from a file instead of the command line itself. What we do
955       --  is to replace the list of object files by the special linker option
956       --  which then reads the object file list from a file instead. The option
957       --  to read from a file instead of the command line is only triggered if
958       --  a conservative threshold is passed.
959
960       if VM_Target = CLI_Target
961         and then Link_Bytes > Link_Max
962       then
963          Lname := new String'("l~" & Base_Name (Ali_File_Name.all) & ".il");
964
965          for J in Objs_Begin .. Objs_End loop
966             Copy_File (Linker_Objects.Table (J).all, Lname.all,
967                        Success => Closing_Status,
968                        Mode    => Append);
969          end loop;
970
971          --  Add the special objects list file option together with the name
972          --  of the temporary file to the objects file table.
973
974          Linker_Objects.Table (Objs_Begin) :=
975            new String'(Value (Object_File_Option_Ptr) & Lname.all);
976
977          --  The slots containing these object file names are then removed
978          --  from the objects table so they do not appear in the link. They
979          --  are removed by moving up the linker options and non-Ada object
980          --  files appearing after the Ada object list in the table.
981
982          declare
983             N : Integer;
984
985          begin
986             N := Objs_End - Objs_Begin + 1;
987
988             for J in Objs_End + 1 .. Linker_Objects.Last loop
989                Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
990             end loop;
991
992             Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
993          end;
994
995       elsif Object_List_File_Required
996         or else (Object_List_File_Supported
997                    and then Link_Bytes > Link_Max)
998       then
999          --  Create a temporary file containing the Ada user object files
1000          --  needed by the link. This list is taken from the bind file
1001          --  and is output one object per line for maximal compatibility with
1002          --  linkers supporting this option.
1003
1004          Create_Temp_File (Tname_FD, Tname);
1005
1006          --  ??? File descriptor should be checked to not be Invalid_FD.
1007          --  ??? Status of Write and Close operations should be checked, and
1008          --  failure should occur if a status is wrong.
1009
1010          --  If target is using the GNU linker we must add a special header
1011          --  and footer in the response file.
1012
1013          --  The syntax is : INPUT (object1.o object2.o ... )
1014
1015          --  Because the GNU linker does not like name with characters such
1016          --  as '!', we must put the object paths between double quotes.
1017
1018          if Using_GNU_Linker then
1019             declare
1020                GNU_Header : aliased constant String := "INPUT (";
1021
1022             begin
1023                Status := Write (Tname_FD, GNU_Header'Address,
1024                  GNU_Header'Length);
1025             end;
1026          end if;
1027
1028          for J in Objs_Begin .. Objs_End loop
1029
1030             --  Opening quote for GNU linker
1031
1032             if Using_GNU_Linker then
1033                Status := Write (Tname_FD, Opening'Address, 1);
1034             end if;
1035
1036             Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
1037                              Linker_Objects.Table (J).all'Length);
1038
1039             --  Closing quote for GNU linker
1040
1041             if Using_GNU_Linker then
1042                Status := Write (Tname_FD, Closing'Address, 2);
1043
1044             else
1045                Status := Write (Tname_FD, ASCII.LF'Address, 1);
1046             end if;
1047
1048             Response_File_Objects.Increment_Last;
1049             Response_File_Objects.Table (Response_File_Objects.Last) :=
1050               Linker_Objects.Table (J);
1051          end loop;
1052
1053          --  Handle GNU linker response file footer
1054
1055          if Using_GNU_Linker then
1056             declare
1057                GNU_Footer : aliased constant String := ")";
1058
1059             begin
1060                Status := Write (Tname_FD, GNU_Footer'Address,
1061                  GNU_Footer'Length);
1062             end;
1063          end if;
1064
1065          Close (Tname_FD, Closing_Status);
1066
1067          --  Add the special objects list file option together with the name
1068          --  of the temporary file (removing the null character) to the objects
1069          --  file table.
1070
1071          Linker_Objects.Table (Objs_Begin) :=
1072            new String'(Value (Object_File_Option_Ptr) &
1073                        Tname (Tname'First .. Tname'Last - 1));
1074
1075          --  The slots containing these object file names are then removed
1076          --  from the objects table so they do not appear in the link. They
1077          --  are removed by moving up the linker options and non-Ada object
1078          --  files appearing after the Ada object list in the table.
1079
1080          declare
1081             N : Integer;
1082
1083          begin
1084             N := Objs_End - Objs_Begin + 1;
1085
1086             for J in Objs_End + 1 .. Linker_Objects.Last loop
1087                Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
1088             end loop;
1089
1090             Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
1091          end;
1092       end if;
1093
1094       --  Process switches and options
1095
1096       if Next_Line (Nfirst .. Nlast) /= End_Info then
1097          Xlinker_Was_Previous := False;
1098
1099          loop
1100             if Xlinker_Was_Previous
1101               or else Next_Line (Nfirst .. Nlast) = "-Xlinker"
1102             then
1103                Linker_Options.Increment_Last;
1104                Linker_Options.Table (Linker_Options.Last) :=
1105                  new String'(Next_Line (Nfirst .. Nlast));
1106
1107             elsif Next_Line (Nfirst .. Nlast) = "-static" then
1108                GNAT_Static := True;
1109
1110             elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1111                GNAT_Shared := True;
1112
1113             --  Add binder options only if not already set on the command
1114             --  line. This rule is a way to control the linker options order.
1115
1116             --  The following test needs comments, why is it VMS specific.
1117             --  The above comment looks out of date ???
1118
1119             elsif not (OpenVMS_On_Target
1120                          and then
1121                        Is_Option_Present (Next_Line (Nfirst .. Nlast)))
1122             then
1123                if Nlast > Nfirst + 2 and then
1124                  Next_Line (Nfirst .. Nfirst + 1) = "-L"
1125                then
1126                   --  Construct a library search path for use later
1127                   --  to locate static gnatlib libraries.
1128
1129                   if Libpath.Last > 1 then
1130                      Libpath.Increment_Last;
1131                      Libpath.Table (Libpath.Last) := Path_Separator;
1132                   end if;
1133
1134                   for I in Nfirst + 2 .. Nlast loop
1135                      Libpath.Increment_Last;
1136                      Libpath.Table (Libpath.Last) := Next_Line (I);
1137                   end loop;
1138
1139                   Linker_Options.Increment_Last;
1140
1141                   Linker_Options.Table (Linker_Options.Last) :=
1142                     new String'(Next_Line (Nfirst .. Nlast));
1143
1144                elsif Next_Line (Nfirst .. Nlast) = "-ldecgnat"
1145                  or else Next_Line (Nfirst .. Nlast) = "-lgnarl"
1146                  or else Next_Line (Nfirst .. Nlast) = "-lgnat"
1147                  or else Next_Line
1148                      (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) =
1149                        Shared_Lib ("gnarl")
1150                  or else Next_Line
1151                      (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) =
1152                        Shared_Lib ("gnat")
1153                then
1154                   --  If it is a shared library, remove the library version.
1155                   --  We will be looking for the static version of the library
1156                   --  as it is in the same directory as the shared version.
1157
1158                   if Next_Line (Nlast - Library_Version'Length + 1 .. Nlast)
1159                        = Library_Version
1160                   then
1161                      --  Set Last to point to last character before the
1162                      --  library version.
1163
1164                      Last := Nlast - Library_Version'Length - 1;
1165                   else
1166                      Last := Nlast;
1167                   end if;
1168
1169                   --  Given a Gnat standard library, search the library path to
1170                   --  find the library location.
1171
1172                   --  Shouldn't we abstract a proc here, we are getting awfully
1173                   --  heavily nested ???
1174
1175                   declare
1176                      File_Path : String_Access;
1177
1178                      Object_Lib_Extension : constant String :=
1179                                               Value (Object_Library_Ext_Ptr);
1180
1181                      File_Name : constant String := "lib" &
1182                                    Next_Line (Nfirst + 2 .. Last) &
1183                                    Object_Lib_Extension;
1184
1185                      Run_Path_Opt : constant String :=
1186                        Value (Run_Path_Option_Ptr);
1187
1188                      GCC_Index          : Natural;
1189                      Run_Path_Opt_Index : Natural := 0;
1190
1191                   begin
1192                      File_Path :=
1193                        Locate_Regular_File (File_Name,
1194                          String (Libpath.Table (1 .. Libpath.Last)));
1195
1196                      if File_Path /= null then
1197                         if GNAT_Static then
1198
1199                            --  If static gnatlib found, explicitly
1200                            --  specify to overcome possible linker
1201                            --  default usage of shared version.
1202
1203                            Linker_Options.Increment_Last;
1204
1205                            Linker_Options.Table (Linker_Options.Last) :=
1206                              new String'(File_Path.all);
1207
1208                         elsif GNAT_Shared then
1209                            if Opt.Run_Path_Option then
1210
1211                               --  If shared gnatlib desired, add the
1212                               --  appropriate system specific switch
1213                               --  so that it can be located at runtime.
1214
1215                               if Run_Path_Opt'Length /= 0 then
1216
1217                                  --  Output the system specific linker command
1218                                  --  that allows the image activator to find
1219                                  --  the shared library at runtime. Also add
1220                                  --  path to find libgcc_s.so, if relevant.
1221
1222                                  declare
1223                                     Path : String (1 .. File_Path'Length + 15);
1224                                     Path_Last : constant Natural :=
1225                                                   File_Path'Length;
1226
1227                                  begin
1228                                     Path (1 .. File_Path'Length) :=
1229                                       File_Path.all;
1230
1231                                  --  To find the location of the shared version
1232                                  --  of libgcc, we look for "gcc-lib" in the
1233                                  --  path of the library. However, this
1234                                  --  subdirectory is no longer present in
1235                                  --  recent versions of GCC. So, we look for
1236                                  --  the last subdirectory "lib" in the path.
1237
1238                                     GCC_Index :=
1239                                       Index (Path (1 .. Path_Last), "gcc-lib");
1240
1241                                     if GCC_Index /= 0 then
1242
1243                                        --  The shared version of libgcc is
1244                                        --  located in the parent directory.
1245
1246                                        GCC_Index := GCC_Index - 1;
1247
1248                                     else
1249                                        GCC_Index :=
1250                                          Index
1251                                            (Path (1 .. Path_Last),
1252                                             "/lib/");
1253
1254                                        if GCC_Index = 0 then
1255                                           GCC_Index :=
1256                                             Index (Path (1 .. Path_Last),
1257                                                    Directory_Separator &
1258                                                    "lib" &
1259                                                    Directory_Separator);
1260                                        end if;
1261
1262                                        --  If we have found a "lib" subdir in
1263                                        --  the path to libgnat, the possible
1264                                        --  shared libgcc of interest by default
1265                                        --  is in libgcc_subdir at the same
1266                                        --  level.
1267
1268                                        if GCC_Index /= 0 then
1269                                           declare
1270                                              Subdir : constant String :=
1271                                                Value (Libgcc_Subdir_Ptr);
1272                                           begin
1273                                              Path
1274                                                (GCC_Index + 1 ..
1275                                                 GCC_Index + Subdir'Length) :=
1276                                                Subdir;
1277                                              GCC_Index :=
1278                                                GCC_Index + Subdir'Length;
1279                                           end;
1280                                        end if;
1281                                     end if;
1282
1283                                  --  Look for an eventual run_path_option in
1284                                  --  the linker switches.
1285
1286                                     if Separate_Run_Path_Options then
1287                                        Linker_Options.Increment_Last;
1288                                        Linker_Options.Table
1289                                          (Linker_Options.Last) :=
1290                                            new String'
1291                                              (Run_Path_Opt
1292                                               & File_Path
1293                                                 (1 .. File_Path'Length
1294                                                  - File_Name'Length));
1295
1296                                        if GCC_Index /= 0 then
1297                                           Linker_Options.Increment_Last;
1298                                           Linker_Options.Table
1299                                             (Linker_Options.Last) :=
1300                                             new String'
1301                                               (Run_Path_Opt
1302                                                & Path (1 .. GCC_Index));
1303                                        end if;
1304
1305                                     else
1306                                        for J in reverse
1307                                          1 .. Linker_Options.Last
1308                                        loop
1309                                           if Linker_Options.Table (J) /= null
1310                                             and then
1311                                               Linker_Options.Table (J)'Length
1312                                                         > Run_Path_Opt'Length
1313                                             and then
1314                                               Linker_Options.Table (J)
1315                                                 (1 .. Run_Path_Opt'Length) =
1316                                                                  Run_Path_Opt
1317                                           then
1318                                              --  We have found an already
1319                                              --  specified run_path_option: we
1320                                              --  will add to this switch,
1321                                              --  because only one
1322                                              --  run_path_option should be
1323                                              --  specified.
1324
1325                                              Run_Path_Opt_Index := J;
1326                                              exit;
1327                                           end if;
1328                                        end loop;
1329
1330                                        --  If there is no run_path_option, we
1331                                        --  need to add one.
1332
1333                                        if Run_Path_Opt_Index = 0 then
1334                                           Linker_Options.Increment_Last;
1335                                        end if;
1336
1337                                        if GCC_Index = 0 then
1338                                           if Run_Path_Opt_Index = 0 then
1339                                              Linker_Options.Table
1340                                                (Linker_Options.Last) :=
1341                                                  new String'
1342                                                    (Run_Path_Opt
1343                                                     & File_Path
1344                                                       (1 .. File_Path'Length
1345                                                        - File_Name'Length));
1346
1347                                           else
1348                                              Linker_Options.Table
1349                                                (Run_Path_Opt_Index) :=
1350                                                  new String'
1351                                                    (Linker_Options.Table
1352                                                      (Run_Path_Opt_Index).all
1353                                                     & Path_Separator
1354                                                     & File_Path
1355                                                       (1 .. File_Path'Length
1356                                                        - File_Name'Length));
1357                                           end if;
1358
1359                                        else
1360                                           if Run_Path_Opt_Index = 0 then
1361                                              Linker_Options.Table
1362                                                (Linker_Options.Last) :=
1363                                                  new String'
1364                                                    (Run_Path_Opt
1365                                                     & File_Path
1366                                                       (1 .. File_Path'Length
1367                                                        - File_Name'Length)
1368                                                     & Path_Separator
1369                                                     & Path (1 .. GCC_Index));
1370
1371                                           else
1372                                              Linker_Options.Table
1373                                                (Run_Path_Opt_Index) :=
1374                                                  new String'
1375                                                    (Linker_Options.Table
1376                                                      (Run_Path_Opt_Index).all
1377                                                     & Path_Separator
1378                                                     & File_Path
1379                                                       (1 .. File_Path'Length
1380                                                        - File_Name'Length)
1381                                                     & Path_Separator
1382                                                     & Path (1 .. GCC_Index));
1383                                           end if;
1384                                        end if;
1385                                     end if;
1386                                  end;
1387                               end if;
1388                            end if;
1389
1390                            --  Then we add the appropriate -l switch
1391
1392                            Linker_Options.Increment_Last;
1393                            Linker_Options.Table (Linker_Options.Last) :=
1394                              new String'(Next_Line (Nfirst .. Nlast));
1395                         end if;
1396
1397                      else
1398                         --  If gnatlib library not found, then
1399                         --  add it anyway in case some other
1400                         --  mechanism may find it.
1401
1402                         Linker_Options.Increment_Last;
1403                         Linker_Options.Table (Linker_Options.Last) :=
1404                           new String'(Next_Line (Nfirst .. Nlast));
1405                      end if;
1406                   end;
1407                else
1408                   Linker_Options.Increment_Last;
1409                   Linker_Options.Table (Linker_Options.Last) :=
1410                     new String'(Next_Line (Nfirst .. Nlast));
1411                end if;
1412             end if;
1413
1414             Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker";
1415
1416             Get_Next_Line;
1417             exit when Next_Line (Nfirst .. Nlast) = End_Info;
1418
1419             if Ada_Bind_File then
1420                Next_Line (Nfirst .. Nlast - 8) :=
1421                  Next_Line (Nfirst + 8 .. Nlast);
1422                Nlast := Nlast - 8;
1423             end if;
1424          end loop;
1425       end if;
1426
1427       --  If -shared was specified, invoke gcc with -shared-libgcc
1428
1429       if GNAT_Shared then
1430          Linker_Options.Increment_Last;
1431          Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc;
1432       end if;
1433
1434       Status := fclose (Fd);
1435    end Process_Binder_File;
1436
1437    -----------
1438    -- Usage --
1439    -----------
1440
1441    procedure Usage is
1442    begin
1443       Write_Str ("Usage: ");
1444       Write_Str (Base_Name (Command_Name));
1445       Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
1446       Write_Eol;
1447       Write_Eol;
1448       Write_Line ("  mainprog.ali   the ALI file of the main program");
1449       Write_Eol;
1450       Write_Line ("  -f    force object file list to be generated");
1451       Write_Line ("  -g    Compile binder source file with debug information");
1452       Write_Line ("  -n    Do not compile the binder source file");
1453       Write_Line ("  -R    Do not use a run_path_option");
1454       Write_Line ("  -v    verbose mode");
1455       Write_Line ("  -v -v very verbose mode");
1456       Write_Eol;
1457       Write_Line ("  -o nam     Use 'nam' as the name of the executable");
1458       Write_Line ("  -b target  Compile the binder source to run on target");
1459       Write_Line ("  -Bdir      Load compiler executables from dir");
1460
1461       if Is_Supported (Map_File) then
1462          Write_Line ("  -Mmap      Create map file map");
1463          Write_Line ("  -M         Create map file mainprog.map");
1464       end if;
1465
1466       Write_Line ("  --GCC=comp Use comp as the compiler");
1467       Write_Line ("  --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1468       Write_Eol;
1469       Write_Line ("  [non-Ada-objects]  list of non Ada object files");
1470       Write_Line ("  [linker-options]   other options for the linker");
1471    end Usage;
1472
1473    ------------------
1474    -- Write_Header --
1475    ------------------
1476
1477    procedure Write_Header is
1478    begin
1479       if Verbose_Mode then
1480          Write_Eol;
1481          Display_Version ("GNATLINK", "1995");
1482       end if;
1483    end Write_Header;
1484
1485    -----------------
1486    -- Write_Usage --
1487    -----------------
1488
1489    procedure Write_Usage is
1490    begin
1491       Write_Header;
1492       Usage;
1493    end Write_Usage;
1494
1495 --  Start of processing for Gnatlink
1496
1497 begin
1498    --  Add the directory where gnatlink is invoked in front of the path, if
1499    --  gnatlink is invoked with directory information. Only do this if the
1500    --  platform is not VMS, where the notion of path does not really exist.
1501
1502    if not Hostparm.OpenVMS then
1503       declare
1504          Command : constant String := Command_Name;
1505
1506       begin
1507          for Index in reverse Command'Range loop
1508             if Command (Index) = Directory_Separator then
1509                declare
1510                   Absolute_Dir : constant String :=
1511                                    Normalize_Pathname
1512                                      (Command (Command'First .. Index));
1513
1514                   PATH : constant String :=
1515                            Absolute_Dir &
1516                            Path_Separator &
1517                            Getenv ("PATH").all;
1518
1519                begin
1520                   Setenv ("PATH", PATH);
1521                end;
1522
1523                exit;
1524             end if;
1525          end loop;
1526       end;
1527    end if;
1528
1529    Process_Args;
1530
1531    if Argument_Count = 0
1532      or else (Verbose_Mode and then Argument_Count = 1)
1533    then
1534       Write_Usage;
1535       Exit_Program (E_Fatal);
1536    end if;
1537
1538    --  Initialize packages to be used
1539
1540    Namet.Initialize;
1541    Csets.Initialize;
1542    Snames.Initialize;
1543
1544    --  We always compile with -c
1545
1546    Binder_Options_From_ALI.Increment_Last;
1547    Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1548      new String'("-c");
1549
1550    if Ali_File_Name = null then
1551       Exit_With_Error ("no ali file given for link");
1552    end if;
1553
1554    if not Is_Regular_File (Ali_File_Name.all) then
1555       Exit_With_Error (Ali_File_Name.all & " not found");
1556    end if;
1557
1558    --  Read the ALI file of the main subprogram if the binder generated file
1559    --  needs to be compiled and no --GCC= switch has been specified. Fetch the
1560    --  back end switches from this ALI file and use these switches to compile
1561    --  the binder generated file
1562
1563    if Compile_Bind_File and then Standard_Gcc then
1564
1565       Initialize_ALI;
1566       Name_Len := Ali_File_Name'Length;
1567       Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1568
1569       declare
1570          use Types;
1571          F : constant File_Name_Type := Name_Find;
1572          T : Text_Buffer_Ptr;
1573          A : ALI_Id;
1574
1575       begin
1576          --  Load the ALI file
1577
1578          T := Read_Library_Info (F, True);
1579
1580          --  Read it. Note that we ignore errors, since we only want very
1581          --  limited information from the ali file, and likely a slightly
1582          --  wrong version will be just fine, though in normal operation
1583          --  we don't expect this to happen!
1584
1585          A := Scan_ALI
1586                (F,
1587                 T,
1588                 Ignore_ED     => False,
1589                 Err           => False,
1590                 Ignore_Errors => True);
1591
1592          if A /= No_ALI_Id then
1593             for
1594               Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1595                        Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1596             loop
1597                --  Do not compile with the front end switches. However, --RTS
1598                --  is to be dealt with specially because it needs to be passed
1599                --  if the binder-generated file is in Ada and may also be used
1600                --  to drive the linker.
1601
1602                declare
1603                   Arg : String_Ptr renames Args.Table (Index);
1604                begin
1605                   if not Is_Front_End_Switch (Arg.all) then
1606                      Binder_Options_From_ALI.Increment_Last;
1607                      Binder_Options_From_ALI.Table
1608                        (Binder_Options_From_ALI.Last) := String_Access (Arg);
1609
1610                   elsif Arg'Length > 5
1611                     and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1612                   then
1613                      if Ada_Bind_File then
1614                         Binder_Options_From_ALI.Increment_Last;
1615                         Binder_Options_From_ALI.Table
1616                           (Binder_Options_From_ALI.Last)
1617                             := String_Access (Arg);
1618                      end if;
1619
1620                      --  Set the RTS_*_Path_Name variables, so that
1621                      --  the correct directories will be set when
1622                      --  Osint.Add_Default_Search_Dirs will be called later.
1623
1624                      Opt.RTS_Src_Path_Name :=
1625                        Get_RTS_Search_Dir
1626                          (Arg (Arg'First + 6 .. Arg'Last), Include);
1627
1628                      Opt.RTS_Lib_Path_Name :=
1629                        Get_RTS_Search_Dir
1630                          (Arg (Arg'First + 6 .. Arg'Last), Objects);
1631
1632                      --  GNAT doesn't support the GCC multilib mechanism.
1633                      --  This means that, when a multilib switch is used
1634                      --  to request a particular compilation mode, the
1635                      --  corresponding runtime switch (--RTS) must also be
1636                      --  specified. The long-term goal is to fully support the
1637                      --  multilib mechanism; however, in the meantime, it is
1638                      --  convenient to eliminate the redundancy by keying the
1639                      --  compilation mode on a single switch, namely --RTS.
1640
1641                      --  Pass -mrtp to the linker if --RTS=rtp was passed
1642
1643                      if Arg'Length > 8
1644                        and then Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1645                      then
1646                         Linker_Options.Increment_Last;
1647                         Linker_Options.Table (Linker_Options.Last) :=
1648                           new String'("-mrtp");
1649                      end if;
1650                   end if;
1651                end;
1652             end loop;
1653          end if;
1654       end;
1655    end if;
1656
1657    --  Get target parameters
1658
1659    Osint.Add_Default_Search_Dirs;
1660    Targparm.Get_Target_Parameters;
1661
1662    if VM_Target /= No_VM then
1663       case VM_Target is
1664          when JVM_Target => Gcc := new String'("jvm-gnatcompile");
1665          when CLI_Target => Gcc := new String'("dotnet-gnatcompile");
1666          when No_VM      => raise Program_Error;
1667       end case;
1668
1669       Ada_Bind_File := True;
1670       Begin_Info := "--  BEGIN Object file/option list";
1671       End_Info   := "--  END Object file/option list   ";
1672    end if;
1673
1674    --  If the main program is in Ada it is compiled with the following
1675    --  switches:
1676
1677    --    -gnatA   stops reading gnat.adc, since we don't know what
1678    --             pragmas would work, and we do not need it anyway.
1679
1680    --    -gnatWb  allows brackets coding for wide characters
1681
1682    --    -gnatiw  allows wide characters in identifiers. This is needed
1683    --             because bindgen uses brackets encoding for all upper
1684    --             half and wide characters in identifier names.
1685
1686    if Ada_Bind_File then
1687       Binder_Options_From_ALI.Increment_Last;
1688       Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1689         new String'("-gnatA");
1690       Binder_Options_From_ALI.Increment_Last;
1691       Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1692         new String'("-gnatWb");
1693       Binder_Options_From_ALI.Increment_Last;
1694       Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1695         new String'("-gnatiw");
1696    end if;
1697
1698    --  Locate all the necessary programs and verify required files are present
1699
1700    Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1701
1702    if Gcc_Path = null then
1703       Exit_With_Error ("Couldn't locate " & Gcc.all);
1704    end if;
1705
1706    if Linker_Path = null then
1707       if VM_Target = CLI_Target then
1708          Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("ilasm");
1709
1710          if Linker_Path = null then
1711             Exit_With_Error ("Couldn't locate ilasm");
1712          end if;
1713
1714       elsif RTX_RTSS_Kernel_Module_On_Target then
1715
1716          --  Use Microsoft linker for RTSS modules
1717
1718          Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("link");
1719
1720          if Linker_Path = null then
1721             Exit_With_Error ("Couldn't locate link");
1722          end if;
1723
1724       else
1725          Linker_Path := Gcc_Path;
1726       end if;
1727    end if;
1728
1729    Write_Header;
1730
1731    --  If no output name specified, then use the base name of .ali file name
1732
1733    if Output_File_Name = null then
1734       Output_File_Name :=
1735         new String'(Base_Name (Ali_File_Name.all)
1736                       & Get_Target_Debuggable_Suffix.all);
1737    end if;
1738
1739    if VM_Target = CLI_Target then
1740       Linker_Options.Increment_Last;
1741       Linker_Options.Table (Linker_Options.Last) := new String'("/QUIET");
1742
1743       Linker_Options.Increment_Last;
1744       Linker_Options.Table (Linker_Options.Last) := new String'("/DEBUG");
1745
1746       Linker_Options.Increment_Last;
1747       Linker_Options.Table (Linker_Options.Last) :=
1748         new String'("/OUTPUT=" & Output_File_Name.all);
1749
1750    elsif RTX_RTSS_Kernel_Module_On_Target then
1751       Linker_Options.Increment_Last;
1752       Linker_Options.Table (Linker_Options.Last) :=
1753         new String'("/OUT:" & Output_File_Name.all);
1754
1755    else
1756       Linker_Options.Increment_Last;
1757       Linker_Options.Table (Linker_Options.Last) := new String'("-o");
1758
1759       Linker_Options.Increment_Last;
1760       Linker_Options.Table (Linker_Options.Last) :=
1761         new String'(Output_File_Name.all);
1762    end if;
1763
1764    --  Delete existing executable, in case it is a symbolic link, to avoid
1765    --  modifying the target of the symbolic link.
1766
1767    declare
1768       Dummy : Boolean;
1769       pragma Unreferenced (Dummy);
1770
1771    begin
1772       Delete_File (Output_File_Name.all, Dummy);
1773    end;
1774
1775    --  Warn if main program is called "test", as that may be a built-in command
1776    --  on Unix. On non-Unix systems executables have a suffix, so the warning
1777    --  will not appear. However, do not warn in the case of a cross compiler.
1778
1779    --  Assume this is a cross tool if the executable name is not gnatlink
1780
1781    if Base_Name (Command_Name) = "gnatlink"
1782      and then Output_File_Name.all = "test"
1783    then
1784       Error_Msg ("warning: executable name """ & Output_File_Name.all
1785                    & """ may conflict with shell command");
1786    end if;
1787
1788    --  If -M switch was specified, add the switches to create the map file
1789
1790    if Create_Map_File then
1791       declare
1792          Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map";
1793          Switches : String_List_Access;
1794
1795       begin
1796          Convert (Map_File, Map_Name, Switches);
1797
1798          if Switches /= null then
1799             for J in Switches'Range loop
1800                Linker_Options.Increment_Last;
1801                Linker_Options.Table (Linker_Options.Last) := Switches (J);
1802             end loop;
1803          end if;
1804       end;
1805    end if;
1806
1807    --  Perform consistency checks
1808
1809    --  Transform the .ali file name into the binder output file name
1810
1811    Make_Binder_File_Names : declare
1812       Fname     : constant String  := Base_Name (Ali_File_Name.all);
1813       Fname_Len : Integer := Fname'Length;
1814
1815       function Get_Maximum_File_Name_Length return Integer;
1816       pragma Import (C, Get_Maximum_File_Name_Length,
1817                         "__gnat_get_maximum_file_name_length");
1818
1819       Maximum_File_Name_Length : constant Integer :=
1820                                    Get_Maximum_File_Name_Length;
1821
1822       Bind_File_Prefix : Types.String_Ptr;
1823       --  Contains prefix used for bind files
1824
1825    begin
1826       --  Set prefix
1827
1828       if not Ada_Bind_File then
1829          Bind_File_Prefix := new String'("b_");
1830       elsif OpenVMS_On_Target then
1831          Bind_File_Prefix := new String'("b__");
1832       else
1833          Bind_File_Prefix := new String'("b~");
1834       end if;
1835
1836       --  If the length of the binder file becomes too long due to
1837       --  the addition of the "b?" prefix, then truncate it.
1838
1839       if Maximum_File_Name_Length > 0 then
1840          while Fname_Len >
1841                  Maximum_File_Name_Length - Bind_File_Prefix.all'Length
1842          loop
1843             Fname_Len := Fname_Len - 1;
1844          end loop;
1845       end if;
1846
1847       declare
1848          Fnam : constant String :=
1849                   Bind_File_Prefix.all &
1850                     Fname (Fname'First .. Fname'First + Fname_Len - 1);
1851
1852       begin
1853          if Ada_Bind_File then
1854             Binder_Spec_Src_File := new String'(Fnam & ".ads");
1855             Binder_Body_Src_File := new String'(Fnam & ".adb");
1856             Binder_Ali_File      := new String'(Fnam & ".ali");
1857          else
1858             Binder_Body_Src_File := new String'(Fnam & ".c");
1859          end if;
1860
1861          Binder_Obj_File := new String'(Fnam & Get_Target_Object_Suffix.all);
1862       end;
1863
1864       if Fname_Len /= Fname'Length then
1865          Binder_Options.Increment_Last;
1866          Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1867          Binder_Options.Increment_Last;
1868          Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1869       end if;
1870    end Make_Binder_File_Names;
1871
1872    Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1873
1874    --  Compile the binder file. This is fast, so we always do it, unless
1875    --  specifically told not to by the -n switch
1876
1877    if Compile_Bind_File then
1878       Bind_Step : declare
1879          Success : Boolean;
1880          Args    : Argument_List
1881            (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1882
1883       begin
1884          for J in 1 .. Binder_Options_From_ALI.Last loop
1885             Args (J) := Binder_Options_From_ALI.Table (J);
1886          end loop;
1887
1888          for J in 1 .. Binder_Options.Last loop
1889             Args (Binder_Options_From_ALI.Last + J) :=
1890               Binder_Options.Table (J);
1891          end loop;
1892
1893          --  Use the full path of the binder generated source, so that it is
1894          --  guaranteed that the debugger will find this source, even with
1895          --  STABS.
1896
1897          Args (Args'Last) :=
1898            new String'(Normalize_Pathname (Binder_Body_Src_File.all));
1899
1900          if Verbose_Mode then
1901             Write_Str (Base_Name (Gcc_Path.all));
1902
1903             for J in Args'Range loop
1904                Write_Str (" ");
1905                Write_Str (Args (J).all);
1906             end loop;
1907
1908             Write_Eol;
1909          end if;
1910
1911          System.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1912
1913          if not Success then
1914             Exit_Program (E_Fatal);
1915          end if;
1916       end Bind_Step;
1917    end if;
1918
1919    --  Now, actually link the program
1920
1921    --  Skip this step for now on JVM since the Java interpreter will do
1922    --  the actual link at run time. We might consider packing all class files
1923    --  in a .zip file during this step.
1924
1925    if VM_Target /= JVM_Target then
1926       Link_Step : declare
1927          Num_Args : Natural :=
1928                      (Linker_Options.Last - Linker_Options.First + 1) +
1929                      (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1930                      (Linker_Objects.Last - Linker_Objects.First + 1);
1931          Stack_Op : Boolean := False;
1932          IDENT_Op : Boolean := False;
1933
1934       begin
1935          if VM_Target = CLI_Target then
1936
1937             --  Remove extraneous flags not relevant for CIL. Also remove empty
1938             --  arguments, since ilasm chokes on them.
1939
1940             for J in reverse Linker_Options.First .. Linker_Options.Last loop
1941                if Linker_Options.Table (J)'Length = 0
1942                  or else Linker_Options.Table (J) (1 .. 2) = "-L"
1943                  or else Linker_Options.Table (J) (1 .. 2) = "-l"
1944                  or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1945                  or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1946                  or else Linker_Options.Table (J) (1 .. 2) = "-g"
1947                then
1948                   Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1949                     Linker_Options.Table (J + 1 .. Linker_Options.Last);
1950                   Linker_Options.Decrement_Last;
1951                   Num_Args := Num_Args - 1;
1952                end if;
1953             end loop;
1954
1955          elsif RTX_RTSS_Kernel_Module_On_Target then
1956
1957             --  Remove flags not relevant for Microsoft linker and adapt some
1958             --  others.
1959
1960             for J in reverse Linker_Options.First .. Linker_Options.Last loop
1961
1962                --  Remove flags that are not accepted
1963                if Linker_Options.Table (J)'Length = 0
1964                  or else Linker_Options.Table (J) (1 .. 2) = "-l"
1965                  or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1966                  or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1967                  or else Linker_Options.Table (J) (1 .. 8) = "-Xlinker"
1968                  or else Linker_Options.Table (J) (1 .. 9) = "-mthreads"
1969                then
1970                   Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1971                     Linker_Options.Table (J + 1 .. Linker_Options.Last);
1972                   Linker_Options.Decrement_Last;
1973                   Num_Args := Num_Args - 1;
1974
1975                --  Replace "-L" by its counterpart "/LIBPATH:" and UNIX "/" by
1976                --  Windows "\".
1977                elsif Linker_Options.Table (J) (1 .. 2) = "-L" then
1978                   declare
1979                      Libpath_Option : constant String_Access := new String'
1980                        ("/LIBPATH:" &
1981                         Linker_Options.Table (J)
1982                           (3 .. Linker_Options.Table (J).all'Last));
1983                   begin
1984                      for Index in 10 .. Libpath_Option'Last loop
1985                         if Libpath_Option (Index) = '/' then
1986                            Libpath_Option (Index) := '\';
1987                         end if;
1988                      end loop;
1989
1990                      Linker_Options.Table (J) := Libpath_Option;
1991                   end;
1992
1993                --  Replace "-g" by "/DEBUG"
1994                elsif Linker_Options.Table (J) (1 .. 2) = "-g" then
1995                   Linker_Options.Table (J) := new String'("/DEBUG");
1996
1997                --  Replace "-o" by "/OUT:"
1998                elsif Linker_Options.Table (J) (1 .. 2) = "-o" then
1999                   Linker_Options.Table (J + 1) := new String'
2000                     ("/OUT:" & Linker_Options.Table (J + 1).all);
2001
2002                   Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2003                     Linker_Options.Table (J + 1 .. Linker_Options.Last);
2004                   Linker_Options.Decrement_Last;
2005                   Num_Args := Num_Args - 1;
2006
2007                --  Replace "--stack=" by "/STACK:"
2008                elsif Linker_Options.Table (J) (1 .. 8) = "--stack=" then
2009                   Linker_Options.Table (J) := new String'
2010                     ("/STACK:" &
2011                      Linker_Options.Table (J)
2012                        (9 .. Linker_Options.Table (J).all'Last));
2013
2014                --  Replace "-v" by its counterpart "/VERBOSE"
2015                elsif Linker_Options.Table (J) (1 .. 2) = "-v" then
2016                   Linker_Options.Table (J) := new String'("/VERBOSE");
2017                end if;
2018             end loop;
2019
2020             --  Add some required flags to create RTSS modules
2021
2022             declare
2023                Flags_For_Linker : constant array (1 .. 17) of String_Access :=
2024                  (new String'("/NODEFAULTLIB"),
2025                   new String'("/INCREMENTAL:NO"),
2026                   new String'("/NOLOGO"),
2027                   new String'("/DRIVER"),
2028                   new String'("/ALIGN:0x20"),
2029                   new String'("/SUBSYSTEM:NATIVE"),
2030                   new String'("/ENTRY:_RtapiProcessEntryCRT@8"),
2031                   new String'("/RELEASE"),
2032                   new String'("startupCRT.obj"),
2033                   new String'("rtxlibcmt.lib"),
2034                   new String'("oldnames.lib"),
2035                   new String'("rtapi_rtss.lib"),
2036                   new String'("Rtx_Rtss.lib"),
2037                   new String'("libkernel32.a"),
2038                   new String'("libws2_32.a"),
2039                   new String'("libmswsock.a"),
2040                   new String'("libadvapi32.a"));
2041                --  These flags need to be passed to Microsoft linker. They
2042                --  come from the RTX documentation.
2043
2044                Gcc_Lib_Path : constant String_Access := new String'
2045                  ("/LIBPATH:" & Include_Dir_Default_Prefix & "\..\");
2046                --  Place to look for gcc related libraries, such as libgcc
2047
2048             begin
2049                --  Replace UNIX "/" by Windows "\" in the path
2050
2051                for Index in 10 .. Gcc_Lib_Path.all'Last loop
2052                   if Gcc_Lib_Path (Index) = '/' then
2053                      Gcc_Lib_Path (Index) := '\';
2054                   end if;
2055                end loop;
2056
2057                Linker_Options.Increment_Last;
2058                Linker_Options.Table (Linker_Options.Last) := Gcc_Lib_Path;
2059                Num_Args := Num_Args + 1;
2060
2061                for Index in Flags_For_Linker'Range loop
2062                   Linker_Options.Increment_Last;
2063                   Linker_Options.Table (Linker_Options.Last) :=
2064                     Flags_For_Linker (Index);
2065                   Num_Args := Num_Args + 1;
2066                end loop;
2067             end;
2068          end if;
2069
2070          --  Remove duplicate stack size setting from the Linker_Options
2071          --  table. The stack setting option "-Xlinker --stack=R,C" can be
2072          --  found in one line when set by a pragma Linker_Options or in two
2073          --  lines ("-Xlinker" then "--stack=R,C") when set on the command
2074          --  line. We also check for the "-Wl,--stack=R" style option.
2075
2076          --  We must remove the second stack setting option instance
2077          --  because the one on the command line will always be the first
2078          --  one. And any subsequent stack setting option will overwrite the
2079          --  previous one. This is done especially for GNAT/NT where we set
2080          --  the stack size for tasking programs by a pragma in the NT
2081          --  specific tasking package System.Task_Primitives.Operations.
2082
2083          --  Note: This is not a FOR loop that runs from Linker_Options.First
2084          --  to Linker_Options.Last, since operations within the loop can
2085          --  modify the length of the table.
2086
2087          Clean_Link_Option_Set : declare
2088             J : Natural := Linker_Options.First;
2089             Shared_Libgcc_Seen : Boolean := False;
2090
2091          begin
2092             while J <= Linker_Options.Last loop
2093
2094                if Linker_Options.Table (J).all = "-Xlinker"
2095                  and then J < Linker_Options.Last
2096                  and then Linker_Options.Table (J + 1)'Length > 8
2097                  and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
2098                then
2099                   if Stack_Op then
2100                      Linker_Options.Table (J .. Linker_Options.Last - 2) :=
2101                        Linker_Options.Table (J + 2 .. Linker_Options.Last);
2102                      Linker_Options.Decrement_Last;
2103                      Linker_Options.Decrement_Last;
2104                      Num_Args := Num_Args - 2;
2105
2106                   else
2107                      Stack_Op := True;
2108                   end if;
2109                end if;
2110
2111                --  Remove duplicate -shared-libgcc switch
2112
2113                if Linker_Options.Table (J).all = Shared_Libgcc_String then
2114                   if Shared_Libgcc_Seen then
2115                      Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2116                        Linker_Options.Table (J + 1 .. Linker_Options.Last);
2117                      Linker_Options.Decrement_Last;
2118                      Num_Args := Num_Args - 1;
2119
2120                   else
2121                      Shared_Libgcc_Seen := True;
2122                   end if;
2123                end if;
2124
2125                --  Here we just check for a canonical form that matches the
2126                --  pragma Linker_Options set in the NT runtime.
2127
2128                if (Linker_Options.Table (J)'Length > 17
2129                    and then Linker_Options.Table (J) (1 .. 17)
2130                            = "-Xlinker --stack=")
2131                  or else
2132                   (Linker_Options.Table (J)'Length > 12
2133                    and then Linker_Options.Table (J) (1 .. 12)
2134                             = "-Wl,--stack=")
2135                then
2136                   if Stack_Op then
2137                      Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2138                        Linker_Options.Table (J + 1 .. Linker_Options.Last);
2139                      Linker_Options.Decrement_Last;
2140                      Num_Args := Num_Args - 1;
2141
2142                   else
2143                      Stack_Op := True;
2144                   end if;
2145                end if;
2146
2147                --  Remove duplicate IDENTIFICATION directives (VMS)
2148
2149                if Linker_Options.Table (J)'Length > 27
2150                  and then Linker_Options.Table (J) (1 .. 28)
2151                           = "--for-linker=IDENTIFICATION="
2152                then
2153                   if IDENT_Op then
2154                      Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2155                        Linker_Options.Table (J + 1 .. Linker_Options.Last);
2156                      Linker_Options.Decrement_Last;
2157                      Num_Args := Num_Args - 1;
2158                   else
2159                      IDENT_Op := True;
2160                   end if;
2161                end if;
2162
2163                J := J + 1;
2164             end loop;
2165
2166             if Linker_Path = Gcc_Path and then VM_Target = No_VM then
2167
2168                --  For systems where the default is to link statically with
2169                --  libgcc, if gcc is not called with -shared-libgcc, call it
2170                --  with -static-libgcc, as there are some platforms where one
2171                --  of these two switches is compulsory to link.
2172
2173                if Shared_Libgcc_Default = 'T'
2174                  and then not Shared_Libgcc_Seen
2175                then
2176                   Linker_Options.Increment_Last;
2177                   Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
2178                   Num_Args := Num_Args + 1;
2179                end if;
2180
2181             elsif RTX_RTSS_Kernel_Module_On_Target then
2182
2183                --  Force the use of the static libgcc for RTSS modules
2184
2185                Linker_Options.Increment_Last;
2186                Linker_Options.Table (Linker_Options.Last) :=
2187                  new String'("libgcc.a");
2188                Num_Args := Num_Args + 1;
2189             end if;
2190
2191          end Clean_Link_Option_Set;
2192
2193          --  Prepare arguments for call to linker
2194
2195          Call_Linker : declare
2196             Success  : Boolean;
2197             Args     : Argument_List (1 .. Num_Args + 1);
2198             Index    : Integer := Args'First;
2199
2200          begin
2201             Args (Index) := Binder_Obj_File;
2202
2203             --  Add the object files and any -largs libraries
2204
2205             for J in Linker_Objects.First .. Linker_Objects.Last loop
2206                Index := Index + 1;
2207                Args (Index) := Linker_Objects.Table (J);
2208             end loop;
2209
2210             --  Add the linker options from the binder file
2211
2212             for J in Linker_Options.First .. Linker_Options.Last loop
2213                Index := Index + 1;
2214                Args (Index) := Linker_Options.Table (J);
2215             end loop;
2216
2217             --  Finally add the libraries from the --GCC= switch
2218
2219             for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
2220                Index := Index + 1;
2221                Args (Index) := Gcc_Linker_Options.Table (J);
2222             end loop;
2223
2224             if Verbose_Mode then
2225                Write_Str (Linker_Path.all);
2226
2227                for J in Args'Range loop
2228                   Write_Str (" ");
2229                   Write_Str (Args (J).all);
2230                end loop;
2231
2232                Write_Eol;
2233
2234                --  If we are on very verbose mode (-v -v) and a response file
2235                --  is used we display its content.
2236
2237                if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
2238                   Write_Eol;
2239                   Write_Str ("Response file (" &
2240                              Tname (Tname'First .. Tname'Last - 1) &
2241                              ") content : ");
2242                   Write_Eol;
2243
2244                   for J in
2245                     Response_File_Objects.First ..
2246                     Response_File_Objects.Last
2247                   loop
2248                      Write_Str (Response_File_Objects.Table (J).all);
2249                      Write_Eol;
2250                   end loop;
2251
2252                   Write_Eol;
2253                end if;
2254             end if;
2255
2256             System.OS_Lib.Spawn (Linker_Path.all, Args, Success);
2257
2258             --  Delete the temporary file used in conjunction with linking if
2259             --  one was created. See Process_Bind_File for details.
2260
2261             if Tname_FD /= Invalid_FD then
2262                Delete (Tname);
2263             end if;
2264
2265             if Lname /= null then
2266                Delete (Lname.all & ASCII.NUL);
2267             end if;
2268
2269             if not Success then
2270                Error_Msg ("error when calling " & Linker_Path.all);
2271                Exit_Program (E_Fatal);
2272             end if;
2273          end Call_Linker;
2274       end Link_Step;
2275    end if;
2276
2277    --  Only keep the binder output file and it's associated object
2278    --  file if compiling with the -g option.  These files are only
2279    --  useful if debugging.
2280
2281    if not Debug_Flag_Present then
2282       if Binder_Ali_File /= null then
2283          Delete (Binder_Ali_File.all & ASCII.NUL);
2284       end if;
2285
2286       if Binder_Spec_Src_File /= null then
2287          Delete (Binder_Spec_Src_File.all & ASCII.NUL);
2288       end if;
2289
2290       Delete (Binder_Body_Src_File.all & ASCII.NUL);
2291
2292       if VM_Target = No_VM then
2293          Delete (Binder_Obj_File.all & ASCII.NUL);
2294       end if;
2295    end if;
2296
2297    Exit_Program (E_Success);
2298
2299 exception
2300    when X : others =>
2301       Write_Line (Exception_Information (X));
2302       Exit_With_Error ("INTERNAL ERROR. Please report");
2303 end Gnatlink;