OSDN Git Service

contrib/
[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-2008, 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    function Base_Name (File_Name : String) return String;
193    --  Return just the file name part without the extension (if present)
194
195    procedure Delete (Name : String);
196    --  Wrapper to unlink as status is ignored by this application
197
198    procedure Error_Msg (Message : String);
199    --  Output the error or warning Message
200
201    procedure Exit_With_Error (Error : String);
202    --  Output Error and exit program with a fatal condition
203
204    procedure Process_Args;
205    --  Go through all the arguments and build option tables
206
207    procedure Process_Binder_File (Name : String);
208    --  Reads the binder file and extracts linker arguments
209
210    procedure Usage;
211    --  Display usage
212
213    procedure Write_Header;
214    --  Show user the program name, version and copyright
215
216    procedure Write_Usage;
217    --  Show user the program options
218
219    ---------------
220    -- Base_Name --
221    ---------------
222
223    function Base_Name (File_Name : String) return String is
224       Findex1 : Natural;
225       Findex2 : Natural;
226
227    begin
228       Findex1 := File_Name'First;
229
230       --  The file might be specified by a full path name. However,
231       --  we want the path to be stripped away.
232
233       for J in reverse File_Name'Range loop
234          if Is_Directory_Separator (File_Name (J)) then
235             Findex1 := J + 1;
236             exit;
237          end if;
238       end loop;
239
240       Findex2 := File_Name'Last;
241       while Findex2 > Findex1
242         and then File_Name (Findex2) /=  '.'
243       loop
244          Findex2 := Findex2 - 1;
245       end loop;
246
247       if Findex2 = Findex1 then
248          Findex2 := File_Name'Last + 1;
249       end if;
250
251       return File_Name (Findex1 .. Findex2 - 1);
252    end Base_Name;
253
254    ------------
255    -- Delete --
256    ------------
257
258    procedure Delete (Name : String) is
259       Status : int;
260       pragma Unreferenced (Status);
261    begin
262       Status := unlink (Name'Address);
263       --  Is it really right to ignore an error here ???
264    end Delete;
265
266    ---------------
267    -- Error_Msg --
268    ---------------
269
270    procedure Error_Msg (Message : String) is
271    begin
272       Write_Str (Base_Name (Command_Name));
273       Write_Str (": ");
274       Write_Str (Message);
275       Write_Eol;
276    end Error_Msg;
277
278    ---------------------
279    -- Exit_With_Error --
280    ---------------------
281
282    procedure Exit_With_Error (Error : String) is
283    begin
284       Error_Msg (Error);
285       Exit_Program (E_Fatal);
286    end Exit_With_Error;
287
288    ------------------
289    -- Process_Args --
290    ------------------
291
292    procedure Process_Args is
293       Next_Arg  : Integer;
294       Skip_Next : Boolean := False;
295       --  Set to true if the next argument is to be added into the list of
296       --  linker's argument without parsing it.
297
298       procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
299
300       --  Start of processing for Process_Args
301
302    begin
303       --  First, check for --version and --help
304
305       Check_Version_And_Help ("GNATLINK", "1995");
306
307       --  Loop through arguments of gnatlink command
308
309       Next_Arg := 1;
310       loop
311          exit when Next_Arg > Argument_Count;
312
313          Process_One_Arg : declare
314             Arg : constant String := Argument (Next_Arg);
315
316          begin
317             --  Case of argument which is a switch
318
319             --  We definitely need section by section comments here ???
320
321             if Skip_Next then
322
323                --  This argument must not be parsed, just add it to the
324                --  list of linker's options.
325
326                Skip_Next := False;
327
328                Linker_Options.Increment_Last;
329                Linker_Options.Table (Linker_Options.Last) :=
330                  new String'(Arg);
331
332             elsif Arg'Length /= 0 and then Arg (1) = '-' then
333                if Arg'Length > 4 and then Arg (2 .. 5) =  "gnat" then
334                   Exit_With_Error
335                     ("invalid switch: """ & Arg & """ (gnat not needed here)");
336                end if;
337
338                if Arg = "-Xlinker" then
339
340                   --  Next argument should be sent directly to the linker.
341                   --  We do not want to parse it here.
342
343                   Skip_Next := True;
344
345                   Linker_Options.Increment_Last;
346                   Linker_Options.Table (Linker_Options.Last) :=
347                     new String'(Arg);
348
349                elsif Arg (2) = 'g'
350                  and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
351                then
352                   Debug_Flag_Present := True;
353
354                   Linker_Options.Increment_Last;
355                   Linker_Options.Table (Linker_Options.Last) :=
356                    new String'(Arg);
357
358                   Binder_Options.Increment_Last;
359                   Binder_Options.Table (Binder_Options.Last) :=
360                     Linker_Options.Table (Linker_Options.Last);
361
362                elsif Arg'Length >= 3 and then Arg (2) = 'M' then
363                   declare
364                      Switches : String_List_Access;
365
366                   begin
367                      Convert (Map_File, Arg (3 .. Arg'Last), Switches);
368
369                      if Switches /= null then
370                         for J in Switches'Range loop
371                            Linker_Options.Increment_Last;
372                            Linker_Options.Table (Linker_Options.Last) :=
373                              Switches (J);
374                         end loop;
375                      end if;
376                   end;
377
378                elsif Arg'Length = 2 then
379                   case Arg (2) is
380                      when 'A' =>
381                         Ada_Bind_File := True;
382                         Begin_Info := "--  BEGIN Object file/option list";
383                         End_Info   := "--  END Object file/option list   ";
384
385                      when 'b' =>
386                         Linker_Options.Increment_Last;
387                         Linker_Options.Table (Linker_Options.Last) :=
388                           new String'(Arg);
389
390                         Binder_Options.Increment_Last;
391                         Binder_Options.Table (Binder_Options.Last) :=
392                           Linker_Options.Table (Linker_Options.Last);
393
394                         Next_Arg := Next_Arg + 1;
395
396                         if Next_Arg > Argument_Count then
397                            Exit_With_Error ("Missing argument for -b");
398                         end if;
399
400                         Get_Machine_Name : declare
401                            Name_Arg : constant String_Access :=
402                                         new String'(Argument (Next_Arg));
403
404                         begin
405                            Linker_Options.Increment_Last;
406                            Linker_Options.Table (Linker_Options.Last) :=
407                              Name_Arg;
408
409                            Binder_Options.Increment_Last;
410                            Binder_Options.Table (Binder_Options.Last) :=
411                              Name_Arg;
412
413                         end Get_Machine_Name;
414
415                      when 'C' =>
416                         Ada_Bind_File := False;
417                         Begin_Info := "/*  BEGIN Object file/option list";
418                         End_Info   := "    END Object file/option list */";
419
420                      when 'f' =>
421                         if Object_List_File_Supported then
422                            Object_List_File_Required := True;
423                         else
424                            Exit_With_Error
425                              ("Object list file not supported on this target");
426                         end if;
427
428                      when 'M' =>
429                         Create_Map_File := True;
430
431                      when 'n' =>
432                         Compile_Bind_File := False;
433
434                      when 'o' =>
435                         if VM_Target = CLI_Target then
436                            Linker_Options.Increment_Last;
437                            Linker_Options.Table (Linker_Options.Last) :=
438                               new String'("/QUIET");
439
440                         else
441                            Linker_Options.Increment_Last;
442                            Linker_Options.Table (Linker_Options.Last) :=
443                              new String'(Arg);
444                         end if;
445
446                         Next_Arg := Next_Arg + 1;
447
448                         if Next_Arg > Argument_Count then
449                            Exit_With_Error ("Missing argument for -o");
450                         end if;
451
452                         if VM_Target = CLI_Target then
453                            Output_File_Name :=
454                              new String'("/OUTPUT=" & Argument (Next_Arg));
455                         else
456                            Output_File_Name :=
457                              new String'(Argument (Next_Arg));
458                         end if;
459
460                         Linker_Options.Increment_Last;
461                         Linker_Options.Table (Linker_Options.Last) :=
462                           Output_File_Name;
463
464                      when 'R' =>
465                         Opt.Run_Path_Option := False;
466
467                      when 'v' =>
468
469                         --  Support "double" verbose mode.  Second -v
470                         --  gets sent to the linker and binder phases.
471
472                         if Verbose_Mode then
473                            Very_Verbose_Mode := True;
474
475                            Linker_Options.Increment_Last;
476                            Linker_Options.Table (Linker_Options.Last) :=
477                             new String'(Arg);
478
479                            Binder_Options.Increment_Last;
480                            Binder_Options.Table (Binder_Options.Last) :=
481                              Linker_Options.Table (Linker_Options.Last);
482
483                         else
484                            Verbose_Mode := True;
485
486                         end if;
487
488                      when others =>
489                         Linker_Options.Increment_Last;
490                         Linker_Options.Table (Linker_Options.Last) :=
491                          new String'(Arg);
492
493                   end case;
494
495                elsif Arg (2) = 'B' then
496                   Linker_Options.Increment_Last;
497                   Linker_Options.Table (Linker_Options.Last) :=
498                     new String'(Arg);
499
500                   Binder_Options.Increment_Last;
501                   Binder_Options.Table (Binder_Options.Last) :=
502                     Linker_Options.Table (Linker_Options.Last);
503
504                elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
505                   if Arg'Length = 7 then
506                      Exit_With_Error ("Missing argument for --LINK=");
507                   end if;
508
509                   Linker_Path :=
510                     System.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
511
512                   if Linker_Path = null then
513                      Exit_With_Error
514                        ("Could not locate linker: " & Arg (8 .. Arg'Last));
515                   end if;
516
517                elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
518                   declare
519                      Program_Args : constant Argument_List_Access :=
520                                       Argument_String_To_List
521                                                  (Arg (7 .. Arg'Last));
522
523                   begin
524                      if Program_Args.all (1).all /= Gcc.all then
525                         Gcc := new String'(Program_Args.all (1).all);
526                         Standard_Gcc := False;
527                      end if;
528
529                      --  Set appropriate flags for switches passed
530
531                      for J in 2 .. Program_Args.all'Last loop
532                         declare
533                            Arg : constant String := Program_Args.all (J).all;
534                            AF  : constant Integer := Arg'First;
535
536                         begin
537                            if Arg'Length /= 0 and then Arg (AF) = '-' then
538                               if Arg (AF + 1) = 'g'
539                                 and then (Arg'Length = 2
540                                   or else Arg (AF + 2) in '0' .. '3'
541                                   or else Arg (AF + 2 .. Arg'Last) = "coff")
542                               then
543                                  Debug_Flag_Present := True;
544                               end if;
545                            end if;
546
547                            --  Add directory to source search dirs so that
548                            --  Get_Target_Parameters can find system.ads
549
550                            if Arg (AF .. AF + 1) = "-I"
551                              and then Arg'Length > 2
552                            then
553                               Add_Src_Search_Dir (Arg (AF + 2 .. Arg'Last));
554                            end if;
555
556                            --  Pass to gcc for compiling binder generated file
557                            --  No use passing libraries, it will just generate
558                            --  a warning
559
560                            if not (Arg (AF .. AF + 1) = "-l"
561                              or else Arg (AF .. AF + 1) = "-L")
562                            then
563                               Binder_Options.Increment_Last;
564                               Binder_Options.Table (Binder_Options.Last) :=
565                                 new String'(Arg);
566                            end if;
567
568                            --  Pass to gcc for linking program
569
570                            Gcc_Linker_Options.Increment_Last;
571                            Gcc_Linker_Options.Table
572                              (Gcc_Linker_Options.Last) := new String'(Arg);
573                         end;
574                      end loop;
575                   end;
576
577                --  Send all multi-character switches not recognized as
578                --  a special case by gnatlink to the linker/loader stage.
579
580                else
581                   Linker_Options.Increment_Last;
582                   Linker_Options.Table (Linker_Options.Last) :=
583                     new String'(Arg);
584                end if;
585
586             --  Here if argument is a file name rather than a switch
587
588             else
589                --  If explicit ali file, capture it
590
591                if Arg'Length > 4
592                  and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
593                then
594                   if Ali_File_Name = null then
595                      Ali_File_Name := new String'(Arg);
596                   else
597                      Exit_With_Error ("cannot handle more than one ALI file");
598                   end if;
599
600                --  If target object file, record object file
601
602                elsif Arg'Length > Get_Target_Object_Suffix.all'Length
603                  and then Arg
604                    (Arg'Last -
605                     Get_Target_Object_Suffix.all'Length + 1 .. Arg'Last)
606                    = Get_Target_Object_Suffix.all
607                then
608                   Linker_Objects.Increment_Last;
609                   Linker_Objects.Table (Linker_Objects.Last) :=
610                     new String'(Arg);
611
612                --  If host object file, record object file
613                --  e.g. accept foo.o as well as foo.obj on VMS target
614
615                elsif Arg'Length > Get_Object_Suffix.all'Length
616                  and then Arg
617                    (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
618                                                 = Get_Object_Suffix.all
619                then
620                   Linker_Objects.Increment_Last;
621                   Linker_Objects.Table (Linker_Objects.Last) :=
622                     new String'(Arg);
623
624                --  If corresponding ali file exists, capture it
625
626                elsif Ali_File_Name = null
627                  and then Is_Regular_File (Arg & ".ali")
628                then
629                   Ali_File_Name := new String'(Arg & ".ali");
630
631                --  Otherwise assume this is a linker options entry, but
632                --  see below for interesting adjustment to this assumption.
633
634                else
635                   Linker_Options.Increment_Last;
636                   Linker_Options.Table (Linker_Options.Last) :=
637                     new String'(Arg);
638                end if;
639             end if;
640          end Process_One_Arg;
641
642          Next_Arg := Next_Arg + 1;
643       end loop;
644
645       --  If Ada bind file, then compile it with warnings suppressed, because
646       --  otherwise the with of the main program may cause junk warnings.
647
648       if Ada_Bind_File then
649          Binder_Options.Increment_Last;
650          Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
651       end if;
652
653       --  If we did not get an ali file at all, and we had at least one
654       --  linker option, then assume that was the intended ali file after
655       --  all, so that we get a nicer message later on.
656
657       if Ali_File_Name = null
658         and then Linker_Options.Last >= Linker_Options.First
659       then
660          Ali_File_Name :=
661            new String'(Linker_Options.Table (Linker_Options.First).all &
662                                                                    ".ali");
663       end if;
664    end Process_Args;
665
666    -------------------------
667    -- Process_Binder_File --
668    -------------------------
669
670    procedure Process_Binder_File (Name : String) is
671       Fd : FILEs;
672       --  Binder file's descriptor
673
674       Link_Bytes : Integer := 0;
675       --  Projected number of bytes for the linker command line
676
677       Link_Max : Integer;
678       pragma Import (C, Link_Max, "__gnat_link_max");
679       --  Maximum number of bytes on the command line supported by the OS
680       --  linker. Passed this limit the response file mechanism must be used
681       --  if supported.
682
683       Next_Line : String (1 .. 1000);
684       --  Current line value
685
686       Nlast  : Integer;
687       Nfirst : Integer;
688       --  Current line slice (the slice does not contain line terminator)
689
690       Last : Integer;
691       --  Current line last character for shared libraries (without version)
692
693       Objs_Begin : Integer := 0;
694       --  First object file index in Linker_Objects table
695
696       Objs_End : Integer := 0;
697       --  Last object file index in Linker_Objects table
698
699       Status : int;
700       pragma Warnings (Off, Status);
701       --  Used for various Interfaces.C_Streams calls
702
703       Closing_Status : Boolean;
704       pragma Warnings (Off, Closing_Status);
705       --  For call to Close
706
707       GNAT_Static : Boolean := False;
708       --  Save state of -static option
709
710       GNAT_Shared : Boolean := False;
711       --  Save state of -shared option
712
713       Xlinker_Was_Previous : Boolean := False;
714       --  Indicate that "-Xlinker" was the option preceding the current
715       --  option. If True, then the current option is never suppressed.
716
717       --  Rollback data
718
719       --  These data items are used to store current binder file context.
720       --  The context is composed of the file descriptor position and the
721       --  current line together with the slice indexes (first and last
722       --  position) for this line. The rollback data are used by the
723       --  Store_File_Context and Rollback_File_Context routines below.
724       --  The file context mechanism interact only with the Get_Next_Line
725       --  call. For example:
726
727       --     Store_File_Context;
728       --     Get_Next_Line;
729       --     Rollback_File_Context;
730       --     Get_Next_Line;
731
732       --  Both Get_Next_Line calls above will read the exact same data from
733       --  the file. In other words, Next_Line, Nfirst and Nlast variables
734       --  will be set with the exact same values.
735
736       RB_File_Pos  : long;                -- File position
737       RB_Next_Line : String (1 .. 1000);  -- Current line content
738       RB_Nlast     : Integer;             -- Slice last index
739       RB_Nfirst    : Integer;             -- Slice first index
740
741       Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
742       pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
743       --  Pointer to string representing the native linker option which
744       --  specifies the path where the dynamic loader should find shared
745       --  libraries. Equal to null string if this system doesn't support it.
746
747       Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
748       pragma Import
749         (C, Object_Library_Ext_Ptr, "__gnat_object_library_extension");
750       --  Pointer to string specifying the default extension for
751       --  object libraries, e.g. Unix uses ".a", VMS uses ".olb".
752
753       Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
754       pragma Import (C, Object_File_Option_Ptr, "__gnat_object_file_option");
755       --  Pointer to a string representing the linker option which specifies
756       --  the response file.
757
758       Using_GNU_Linker : Boolean;
759       for Using_GNU_Linker'Size use Character'Size;
760       pragma Import (C, Using_GNU_Linker, "__gnat_using_gnu_linker");
761       --  Predicate indicating whether this target uses the GNU linker. In
762       --  this case we must output a GNU linker compatible response file.
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
1170                   --  library path to find the library location
1171
1172                   declare
1173                      File_Path : String_Access;
1174
1175                      Object_Lib_Extension : constant String :=
1176                                               Value (Object_Library_Ext_Ptr);
1177
1178                      File_Name : constant String := "lib" &
1179                                    Next_Line (Nfirst + 2 .. Last) &
1180                                    Object_Lib_Extension;
1181
1182                      Run_Path_Opt : constant String :=
1183                        Value (Run_Path_Option_Ptr);
1184
1185                      GCC_Index          : Natural;
1186                      Run_Path_Opt_Index : Natural := 0;
1187
1188                   begin
1189                      File_Path :=
1190                        Locate_Regular_File (File_Name,
1191                          String (Libpath.Table (1 .. Libpath.Last)));
1192
1193                      if File_Path /= null then
1194                         if GNAT_Static then
1195
1196                            --  If static gnatlib found, explicitly
1197                            --  specify to overcome possible linker
1198                            --  default usage of shared version.
1199
1200                            Linker_Options.Increment_Last;
1201
1202                            Linker_Options.Table (Linker_Options.Last) :=
1203                              new String'(File_Path.all);
1204
1205                         elsif GNAT_Shared then
1206                            if Opt.Run_Path_Option then
1207                               --  If shared gnatlib desired, add the
1208                               --  appropriate system specific switch
1209                               --  so that it can be located at runtime.
1210
1211                               if Run_Path_Opt'Length /= 0 then
1212                                  --  Output the system specific linker command
1213                                  --  that allows the image activator to find
1214                                  --  the shared library at runtime.
1215                                  --  Also add path to find libgcc_s.so, if
1216                                  --  relevant.
1217
1218                                  --  To find the location of the shared version
1219                                  --  of libgcc, we look for "gcc-lib" in the
1220                                  --  path of the library. However, this
1221                                  --  subdirectory is no longer present in
1222                                  --  in recent version of GCC. So, we look for
1223                                  --  the last subdirectory "lib" in the path.
1224
1225                                  GCC_Index :=
1226                                    Index (File_Path.all, "gcc-lib");
1227
1228                                  if GCC_Index /= 0 then
1229                                     --  The shared version of libgcc is
1230                                     --  located in the parent directory.
1231
1232                                     GCC_Index := GCC_Index - 1;
1233
1234                                  else
1235                                     GCC_Index :=
1236                                       Index (File_Path.all, "/lib/");
1237
1238                                     if GCC_Index = 0 then
1239                                        GCC_Index :=
1240                                          Index (File_Path.all,
1241                                                 Directory_Separator &
1242                                                 "lib" &
1243                                                 Directory_Separator);
1244                                     end if;
1245
1246                                     --  We have found a subdirectory "lib",
1247                                     --  this is where the shared version of
1248                                     --  libgcc should be located.
1249
1250                                     if GCC_Index /= 0 then
1251                                        GCC_Index := GCC_Index + 3;
1252                                     end if;
1253                                  end if;
1254
1255                                  --  Look for an eventual run_path_option in
1256                                  --  the linker switches.
1257
1258                                  for J in reverse 1 .. Linker_Options.Last loop
1259                                     if Linker_Options.Table (J) /= null
1260                                       and then
1261                                         Linker_Options.Table (J)'Length
1262                                                   > Run_Path_Opt'Length
1263                                       and then
1264                                         Linker_Options.Table (J)
1265                                           (1 .. Run_Path_Opt'Length) =
1266                                                                 Run_Path_Opt
1267                                     then
1268                                        --  We have found a already specified
1269                                        --  run_path_option: we will add to this
1270                                        --  switch, because only one
1271                                        --  run_path_option should be specified.
1272
1273                                        Run_Path_Opt_Index := J;
1274                                        exit;
1275                                     end if;
1276                                  end loop;
1277
1278                                  --  If there is no run_path_option, we need
1279                                  --  to add one.
1280
1281                                  if Run_Path_Opt_Index = 0 then
1282                                     Linker_Options.Increment_Last;
1283                                  end if;
1284
1285                                  if GCC_Index = 0 then
1286                                     if Run_Path_Opt_Index = 0 then
1287                                        Linker_Options.Table
1288                                          (Linker_Options.Last) :=
1289                                            new String'
1290                                               (Run_Path_Opt
1291                                                 & File_Path
1292                                                   (1 .. File_Path'Length
1293                                                          - File_Name'Length));
1294
1295                                     else
1296                                        Linker_Options.Table
1297                                          (Run_Path_Opt_Index) :=
1298                                            new String'
1299                                              (Linker_Options.Table
1300                                                  (Run_Path_Opt_Index).all
1301                                               & Path_Separator
1302                                               & File_Path
1303                                                  (1 .. File_Path'Length
1304                                                        - File_Name'Length));
1305                                     end if;
1306
1307                                  else
1308                                     if Run_Path_Opt_Index = 0 then
1309                                        Linker_Options.Table
1310                                          (Linker_Options.Last) :=
1311                                            new String'(Run_Path_Opt
1312                                              & File_Path
1313                                                  (1 .. File_Path'Length
1314                                                        - File_Name'Length)
1315                                              & Path_Separator
1316                                              & File_Path (1 .. GCC_Index));
1317
1318                                     else
1319                                        Linker_Options.Table
1320                                          (Run_Path_Opt_Index) :=
1321                                            new String'
1322                                             (Linker_Options.Table
1323                                                 (Run_Path_Opt_Index).all
1324                                              & Path_Separator
1325                                              & File_Path
1326                                                  (1 .. File_Path'Length
1327                                                        - File_Name'Length)
1328                                              & Path_Separator
1329                                              & File_Path (1 .. GCC_Index));
1330                                     end if;
1331                                  end if;
1332                               end if;
1333                            end if;
1334
1335                            --  Then we add the appropriate -l switch
1336
1337                            Linker_Options.Increment_Last;
1338                            Linker_Options.Table (Linker_Options.Last) :=
1339                              new String'(Next_Line (Nfirst .. Nlast));
1340                         end if;
1341
1342                      else
1343                         --  If gnatlib library not found, then
1344                         --  add it anyway in case some other
1345                         --  mechanism may find it.
1346
1347                         Linker_Options.Increment_Last;
1348                         Linker_Options.Table (Linker_Options.Last) :=
1349                           new String'(Next_Line (Nfirst .. Nlast));
1350                      end if;
1351                   end;
1352                else
1353                   Linker_Options.Increment_Last;
1354                   Linker_Options.Table (Linker_Options.Last) :=
1355                     new String'(Next_Line (Nfirst .. Nlast));
1356                end if;
1357             end if;
1358
1359             Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker";
1360
1361             Get_Next_Line;
1362             exit when Next_Line (Nfirst .. Nlast) = End_Info;
1363
1364             if Ada_Bind_File then
1365                Next_Line (Nfirst .. Nlast - 8) :=
1366                  Next_Line (Nfirst + 8 .. Nlast);
1367                Nlast := Nlast - 8;
1368             end if;
1369          end loop;
1370       end if;
1371
1372       --  If -shared was specified, invoke gcc with -shared-libgcc
1373
1374       if GNAT_Shared then
1375          Linker_Options.Increment_Last;
1376          Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc;
1377       end if;
1378
1379       Status := fclose (Fd);
1380    end Process_Binder_File;
1381
1382    -----------
1383    -- Usage --
1384    -----------
1385
1386    procedure Usage is
1387    begin
1388       Write_Str ("Usage: ");
1389       Write_Str (Base_Name (Command_Name));
1390       Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
1391       Write_Eol;
1392       Write_Eol;
1393       Write_Line ("  mainprog.ali   the ALI file of the main program");
1394       Write_Eol;
1395       Write_Line ("  -A    Binder generated source file is in Ada (default)");
1396       Write_Line ("  -C    Binder generated source file is in C");
1397       Write_Line ("  -f    force object file list to be generated");
1398       Write_Line ("  -g    Compile binder source file with debug information");
1399       Write_Line ("  -n    Do not compile the binder source file");
1400       Write_Line ("  -R    Do not use a run_path_option");
1401       Write_Line ("  -v    verbose mode");
1402       Write_Line ("  -v -v very verbose mode");
1403       Write_Eol;
1404       Write_Line ("  -o nam     Use 'nam' as the name of the executable");
1405       Write_Line ("  -b target  Compile the binder source to run on target");
1406       Write_Line ("  -Bdir      Load compiler executables from dir");
1407
1408       if Is_Supported (Map_File) then
1409          Write_Line ("  -Mmap      Create map file map");
1410          Write_Line ("  -M         Create map file mainprog.map");
1411       end if;
1412
1413       Write_Line ("  --GCC=comp Use comp as the compiler");
1414       Write_Line ("  --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1415       Write_Eol;
1416       Write_Line ("  [non-Ada-objects]  list of non Ada object files");
1417       Write_Line ("  [linker-options]   other options for the linker");
1418    end Usage;
1419
1420    ------------------
1421    -- Write_Header --
1422    ------------------
1423
1424    procedure Write_Header is
1425    begin
1426       if Verbose_Mode then
1427          Write_Eol;
1428          Display_Version ("GNATLINK", "1995");
1429       end if;
1430    end Write_Header;
1431
1432    -----------------
1433    -- Write_Usage --
1434    -----------------
1435
1436    procedure Write_Usage is
1437    begin
1438       Write_Header;
1439       Usage;
1440    end Write_Usage;
1441
1442 --  Start of processing for Gnatlink
1443
1444 begin
1445    --  Add the directory where gnatlink is invoked in front of the
1446    --  path, if gnatlink is invoked with directory information.
1447    --  Only do this if the platform is not VMS, where the notion of path
1448    --  does not really exist.
1449
1450    if not Hostparm.OpenVMS then
1451       declare
1452          Command : constant String := Command_Name;
1453
1454       begin
1455          for Index in reverse Command'Range loop
1456             if Command (Index) = Directory_Separator then
1457                declare
1458                   Absolute_Dir : constant String :=
1459                                    Normalize_Pathname
1460                                      (Command (Command'First .. Index));
1461
1462                   PATH         : constant String :=
1463                                    Absolute_Dir &
1464                   Path_Separator &
1465                   Getenv ("PATH").all;
1466
1467                begin
1468                   Setenv ("PATH", PATH);
1469                end;
1470
1471                exit;
1472             end if;
1473          end loop;
1474       end;
1475    end if;
1476
1477    Process_Args;
1478
1479    if Argument_Count = 0
1480      or else
1481      (Verbose_Mode and then Argument_Count = 1)
1482    then
1483       Write_Usage;
1484       Exit_Program (E_Fatal);
1485    end if;
1486
1487    --  Initialize packages to be used
1488
1489    Namet.Initialize;
1490    Csets.Initialize;
1491    Snames.Initialize;
1492
1493    --  We always compile with -c
1494
1495    Binder_Options_From_ALI.Increment_Last;
1496    Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1497      new String'("-c");
1498
1499    if Ali_File_Name = null then
1500       Exit_With_Error ("no ali file given for link");
1501    end if;
1502
1503    if not Is_Regular_File (Ali_File_Name.all) then
1504       Exit_With_Error (Ali_File_Name.all & " not found");
1505    end if;
1506
1507    --  Read the ALI file of the main subprogram if the binder generated
1508    --  file needs to be compiled and no --GCC= switch has been specified.
1509    --  Fetch the back end switches from this ALI file and use these switches
1510    --  to compile the binder generated file
1511
1512    if Compile_Bind_File and then Standard_Gcc then
1513
1514       Initialize_ALI;
1515       Name_Len := Ali_File_Name'Length;
1516       Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1517
1518       declare
1519          use Types;
1520          F : constant File_Name_Type := Name_Find;
1521          T : Text_Buffer_Ptr;
1522          A : ALI_Id;
1523
1524       begin
1525          --  Load the ALI file
1526
1527          T := Read_Library_Info (F, True);
1528
1529          --  Read it. Note that we ignore errors, since we only want very
1530          --  limited information from the ali file, and likely a slightly
1531          --  wrong version will be just fine, though in normal operation
1532          --  we don't expect this to happen!
1533
1534          A := Scan_ALI
1535                (F,
1536                 T,
1537                 Ignore_ED     => False,
1538                 Err           => False,
1539                 Ignore_Errors => True);
1540
1541          if A /= No_ALI_Id then
1542             for
1543               Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1544                        Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1545             loop
1546                --  Do not compile with the front end switches. However, --RTS
1547                --  is to be dealt with specially because it needs to be passed
1548                --  if the binder-generated file is in Ada and may also be used
1549                --  to drive the linker.
1550
1551                declare
1552                   Arg : String_Ptr renames Args.Table (Index);
1553                begin
1554                   if not Is_Front_End_Switch (Arg.all) then
1555                      Binder_Options_From_ALI.Increment_Last;
1556                      Binder_Options_From_ALI.Table
1557                        (Binder_Options_From_ALI.Last) := String_Access (Arg);
1558
1559                   elsif Arg'Length > 5
1560                     and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1561                   then
1562                      if Ada_Bind_File then
1563                         Binder_Options_From_ALI.Increment_Last;
1564                         Binder_Options_From_ALI.Table
1565                           (Binder_Options_From_ALI.Last)
1566                             := String_Access (Arg);
1567                      end if;
1568
1569                      --  Set the RTS_*_Path_Name variables, so that the
1570                      --  correct directories will be set when
1571                      --  Osint.Add_Default_Search_Dirs will be called later.
1572
1573                      Opt.RTS_Src_Path_Name :=
1574                        Get_RTS_Search_Dir
1575                          (Arg (Arg'First + 6 .. Arg'Last), Include);
1576
1577                      Opt.RTS_Lib_Path_Name :=
1578                        Get_RTS_Search_Dir
1579                          (Arg (Arg'First + 6 .. Arg'Last), Objects);
1580
1581                      --  GNAT doesn't support the GCC multilib mechanism.
1582                      --  This means that, when a multilib switch is used
1583                      --  to request a particular compilation mode, the
1584                      --  corresponding runtime switch (--RTS) must also be
1585                      --  specified. The long-term goal is to fully support the
1586                      --  multilib mechanism; however, in the meantime, it is
1587                      --  convenient to eliminate the redundancy by keying the
1588                      --  compilation mode on a single switch, namely --RTS.
1589
1590                      --  Pass -mrtp to the linker if --RTS=rtp was passed
1591
1592                      if Arg'Length > 8
1593                        and then Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1594                      then
1595                         Linker_Options.Increment_Last;
1596                         Linker_Options.Table (Linker_Options.Last) :=
1597                           new String'("-mrtp");
1598
1599                      --  Pass -fsjlj to the linker if --RTS=sjlj was passed
1600
1601                      elsif Arg'Length > 9
1602                        and then Arg (Arg'First + 6 .. Arg'First + 9) = "sjlj"
1603                      then
1604                         Linker_Options.Increment_Last;
1605                         Linker_Options.Table (Linker_Options.Last) :=
1606                           new String'("-fsjlj");
1607                      end if;
1608                   end if;
1609                end;
1610             end loop;
1611          end if;
1612       end;
1613    end if;
1614
1615    --  Get target parameters
1616
1617    Osint.Add_Default_Search_Dirs;
1618    Targparm.Get_Target_Parameters;
1619
1620    if VM_Target /= No_VM then
1621       case VM_Target is
1622          when JVM_Target => Gcc := new String'("jgnat");
1623          when CLI_Target => Gcc := new String'("dotnet-gnatcompile");
1624          when No_VM      => raise Program_Error;
1625       end case;
1626
1627       Ada_Bind_File := True;
1628       Begin_Info := "--  BEGIN Object file/option list";
1629       End_Info   := "--  END Object file/option list   ";
1630    end if;
1631
1632    --  If the main program is in Ada it is compiled with the following
1633    --  switches:
1634
1635    --    -gnatA   stops reading gnat.adc, since we don't know what
1636    --             pragmas would work, and we do not need it anyway.
1637
1638    --    -gnatWb  allows brackets coding for wide characters
1639
1640    --    -gnatiw  allows wide characters in identifiers. This is needed
1641    --             because bindgen uses brackets encoding for all upper
1642    --             half and wide characters in identifier names.
1643
1644    if Ada_Bind_File then
1645       Binder_Options_From_ALI.Increment_Last;
1646       Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1647         new String'("-gnatA");
1648       Binder_Options_From_ALI.Increment_Last;
1649       Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1650         new String'("-gnatWb");
1651       Binder_Options_From_ALI.Increment_Last;
1652       Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1653         new String'("-gnatiw");
1654    end if;
1655
1656    --  Locate all the necessary programs and verify required files are present
1657
1658    Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1659
1660    if Gcc_Path = null then
1661       Exit_With_Error ("Couldn't locate " & Gcc.all);
1662    end if;
1663
1664    if Linker_Path = null then
1665       if VM_Target = CLI_Target then
1666          Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("ilasm");
1667
1668          if Linker_Path = null then
1669             Exit_With_Error ("Couldn't locate ilasm");
1670          end if;
1671
1672       elsif RTX_RTSS_Kernel_Module_On_Target then
1673
1674          --  Use Microsoft linker for RTSS modules
1675
1676          Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("link");
1677
1678          if Linker_Path = null then
1679             Exit_With_Error ("Couldn't locate link");
1680          end if;
1681
1682       else
1683          Linker_Path := Gcc_Path;
1684       end if;
1685    end if;
1686
1687    Write_Header;
1688
1689    --  If no output name specified, then use the base name of .ali file name
1690
1691    if Output_File_Name = null then
1692       Output_File_Name :=
1693         new String'(Base_Name (Ali_File_Name.all)
1694                       & Get_Target_Debuggable_Suffix.all);
1695
1696       if VM_Target = CLI_Target then
1697          Linker_Options.Increment_Last;
1698          Linker_Options.Table (Linker_Options.Last) := new String'("/QUIET");
1699
1700          Linker_Options.Increment_Last;
1701          Linker_Options.Table (Linker_Options.Last) := new String'("/DEBUG");
1702
1703          Linker_Options.Increment_Last;
1704          Linker_Options.Table (Linker_Options.Last) :=
1705            new String'("/OUTPUT=" & Output_File_Name.all);
1706
1707       elsif RTX_RTSS_Kernel_Module_On_Target then
1708          Linker_Options.Increment_Last;
1709          Linker_Options.Table (Linker_Options.Last) :=
1710            new String'("/OUT:" & Output_File_Name.all);
1711
1712       else
1713          Linker_Options.Increment_Last;
1714          Linker_Options.Table (Linker_Options.Last) := new String'("-o");
1715
1716          Linker_Options.Increment_Last;
1717          Linker_Options.Table (Linker_Options.Last) :=
1718            new String'(Output_File_Name.all);
1719       end if;
1720    end if;
1721
1722    --  Warn if main program is called "test", as that may be a built-in command
1723    --  on Unix. On non-Unix systems executables have a suffix, so the warning
1724    --  will not appear. However, do not warn in the case of a cross compiler.
1725
1726    --  Assume this is a cross tool if the executable name is not gnatlink
1727
1728    if Base_Name (Command_Name) = "gnatlink"
1729      and then Output_File_Name.all = "test"
1730    then
1731       Error_Msg ("warning: executable name """ & Output_File_Name.all
1732                    & """ may conflict with shell command");
1733    end if;
1734
1735    --  If -M switch was specified, add the switches to create the map file
1736
1737    if Create_Map_File then
1738       declare
1739          Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map";
1740          Switches : String_List_Access;
1741
1742       begin
1743          Convert (Map_File, Map_Name, Switches);
1744
1745          if Switches /= null then
1746             for J in Switches'Range loop
1747                Linker_Options.Increment_Last;
1748                Linker_Options.Table (Linker_Options.Last) := Switches (J);
1749             end loop;
1750          end if;
1751       end;
1752    end if;
1753
1754    --  Perform consistency checks
1755
1756    --  Transform the .ali file name into the binder output file name
1757
1758    Make_Binder_File_Names : declare
1759       Fname     : constant String  := Base_Name (Ali_File_Name.all);
1760       Fname_Len : Integer := Fname'Length;
1761
1762       function Get_Maximum_File_Name_Length return Integer;
1763       pragma Import (C, Get_Maximum_File_Name_Length,
1764                         "__gnat_get_maximum_file_name_length");
1765
1766       Maximum_File_Name_Length : constant Integer :=
1767                                    Get_Maximum_File_Name_Length;
1768
1769       Bind_File_Prefix : Types.String_Ptr;
1770       --  Contains prefix used for bind files
1771
1772    begin
1773       --  Set prefix
1774
1775       if not Ada_Bind_File then
1776          Bind_File_Prefix := new String'("b_");
1777       elsif OpenVMS_On_Target then
1778          Bind_File_Prefix := new String'("b__");
1779       else
1780          Bind_File_Prefix := new String'("b~");
1781       end if;
1782
1783       --  If the length of the binder file becomes too long due to
1784       --  the addition of the "b?" prefix, then truncate it.
1785
1786       if Maximum_File_Name_Length > 0 then
1787          while Fname_Len >
1788                  Maximum_File_Name_Length - Bind_File_Prefix.all'Length
1789          loop
1790             Fname_Len := Fname_Len - 1;
1791          end loop;
1792       end if;
1793
1794       declare
1795          Fnam : constant String :=
1796                   Bind_File_Prefix.all &
1797                     Fname (Fname'First .. Fname'First + Fname_Len - 1);
1798
1799       begin
1800          if Ada_Bind_File then
1801             Binder_Spec_Src_File := new String'(Fnam & ".ads");
1802             Binder_Body_Src_File := new String'(Fnam & ".adb");
1803             Binder_Ali_File      := new String'(Fnam & ".ali");
1804          else
1805             Binder_Body_Src_File := new String'(Fnam & ".c");
1806          end if;
1807
1808          Binder_Obj_File := new String'(Fnam & Get_Target_Object_Suffix.all);
1809       end;
1810
1811       if Fname_Len /= Fname'Length then
1812          Binder_Options.Increment_Last;
1813          Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1814          Binder_Options.Increment_Last;
1815          Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1816       end if;
1817    end Make_Binder_File_Names;
1818
1819    Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1820
1821    --  Compile the binder file. This is fast, so we always do it, unless
1822    --  specifically told not to by the -n switch
1823
1824    if Compile_Bind_File then
1825       Bind_Step : declare
1826          Success : Boolean;
1827          Args    : Argument_List
1828            (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1829
1830       begin
1831          for J in 1 .. Binder_Options_From_ALI.Last loop
1832             Args (J) := Binder_Options_From_ALI.Table (J);
1833          end loop;
1834
1835          for J in 1 .. Binder_Options.Last loop
1836             Args (Binder_Options_From_ALI.Last + J) :=
1837               Binder_Options.Table (J);
1838          end loop;
1839
1840          --  Use the full path of the binder generated source, so that it is
1841          --  guaranteed that the debugger will find this source, even with
1842          --  STABS.
1843
1844          Args (Args'Last) :=
1845            new String'(Normalize_Pathname (Binder_Body_Src_File.all));
1846
1847          if Verbose_Mode then
1848             Write_Str (Base_Name (Gcc_Path.all));
1849
1850             for J in Args'Range loop
1851                Write_Str (" ");
1852                Write_Str (Args (J).all);
1853             end loop;
1854
1855             Write_Eol;
1856          end if;
1857
1858          System.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1859
1860          if not Success then
1861             Exit_Program (E_Fatal);
1862          end if;
1863       end Bind_Step;
1864    end if;
1865
1866    --  Now, actually link the program
1867
1868    --  Skip this step for now on JVM since the Java interpreter will do
1869    --  the actual link at run time. We might consider packing all class files
1870    --  in a .zip file during this step.
1871
1872    if VM_Target /= JVM_Target then
1873       Link_Step : declare
1874          Num_Args : Natural :=
1875                      (Linker_Options.Last - Linker_Options.First + 1) +
1876                      (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1877                      (Linker_Objects.Last - Linker_Objects.First + 1);
1878          Stack_Op : Boolean := False;
1879          IDENT_Op : Boolean := False;
1880
1881       begin
1882          if VM_Target = CLI_Target then
1883
1884             --  Remove extraneous flags not relevant for CIL. Also remove empty
1885             --  arguments, since ilasm chokes on them.
1886
1887             for J in reverse Linker_Options.First .. Linker_Options.Last loop
1888                if Linker_Options.Table (J)'Length = 0
1889                  or else Linker_Options.Table (J) (1 .. 2) = "-L"
1890                  or else Linker_Options.Table (J) (1 .. 2) = "-l"
1891                  or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1892                  or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1893                  or else Linker_Options.Table (J) (1 .. 2) = "-g"
1894                then
1895                   Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1896                     Linker_Options.Table (J + 1 .. Linker_Options.Last);
1897                   Linker_Options.Decrement_Last;
1898                   Num_Args := Num_Args - 1;
1899                end if;
1900             end loop;
1901
1902          elsif RTX_RTSS_Kernel_Module_On_Target then
1903
1904             --  Remove flags not relevant for Microsoft linker and adapt some
1905             --  others.
1906
1907             for J in reverse Linker_Options.First .. Linker_Options.Last loop
1908
1909                --  Remove flags that are not accepted
1910                if Linker_Options.Table (J)'Length = 0
1911                  or else Linker_Options.Table (J) (1 .. 2) = "-l"
1912                  or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1913                  or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1914                  or else Linker_Options.Table (J) (1 .. 8) = "-Xlinker"
1915                  or else Linker_Options.Table (J) (1 .. 9) = "-mthreads"
1916                then
1917                   Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1918                     Linker_Options.Table (J + 1 .. Linker_Options.Last);
1919                   Linker_Options.Decrement_Last;
1920                   Num_Args := Num_Args - 1;
1921
1922                --  Replace "-L" by its counterpart "/LIBPATH:" and UNIX "/" by
1923                --  Windows "\".
1924                elsif Linker_Options.Table (J) (1 .. 2) = "-L" then
1925                   declare
1926                      Libpath_Option : constant String_Access := new String'
1927                        ("/LIBPATH:" &
1928                         Linker_Options.Table (J)
1929                           (3 .. Linker_Options.Table (J).all'Last));
1930                   begin
1931                      for Index in 10 .. Libpath_Option'Last loop
1932                         if Libpath_Option (Index) = '/' then
1933                            Libpath_Option (Index) := '\';
1934                         end if;
1935                      end loop;
1936
1937                      Linker_Options.Table (J) := Libpath_Option;
1938                   end;
1939
1940                --  Replace "-g" by "/DEBUG"
1941                elsif Linker_Options.Table (J) (1 .. 2) = "-g" then
1942                   Linker_Options.Table (J) := new String'("/DEBUG");
1943
1944                --  Replace "-o" by "/OUT:"
1945                elsif Linker_Options.Table (J) (1 .. 2) = "-o" then
1946                   Linker_Options.Table (J + 1) := new String'
1947                     ("/OUT:" & Linker_Options.Table (J + 1).all);
1948
1949                   Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1950                     Linker_Options.Table (J + 1 .. Linker_Options.Last);
1951                   Linker_Options.Decrement_Last;
1952                   Num_Args := Num_Args - 1;
1953
1954                --  Replace "--stack=" by "/STACK:"
1955                elsif Linker_Options.Table (J) (1 .. 8) = "--stack=" then
1956                   Linker_Options.Table (J) := new String'
1957                     ("/STACK:" &
1958                      Linker_Options.Table (J)
1959                        (9 .. Linker_Options.Table (J).all'Last));
1960
1961                --  Replace "-v" by its counterpart "/VERBOSE"
1962                elsif Linker_Options.Table (J) (1 .. 2) = "-v" then
1963                   Linker_Options.Table (J) := new String'("/VERBOSE");
1964                end if;
1965             end loop;
1966
1967             --  Add some required flags to create RTSS modules
1968
1969             declare
1970                Flags_For_Linker : constant array (1 .. 17) of String_Access :=
1971                  (new String'("/NODEFAULTLIB"),
1972                   new String'("/INCREMENTAL:NO"),
1973                   new String'("/NOLOGO"),
1974                   new String'("/DRIVER"),
1975                   new String'("/ALIGN:0x20"),
1976                   new String'("/SUBSYSTEM:NATIVE"),
1977                   new String'("/ENTRY:_RtapiProcessEntryCRT@8"),
1978                   new String'("/RELEASE"),
1979                   new String'("startupCRT.obj"),
1980                   new String'("rtxlibcmt.lib"),
1981                   new String'("oldnames.lib"),
1982                   new String'("rtapi_rtss.lib"),
1983                   new String'("Rtx_Rtss.lib"),
1984                   new String'("libkernel32.a"),
1985                   new String'("libws2_32.a"),
1986                   new String'("libmswsock.a"),
1987                   new String'("libadvapi32.a"));
1988                --  These flags need to be passed to Microsoft linker. They
1989                --  come from the RTX documentation.
1990
1991                Gcc_Lib_Path : constant String_Access := new String'
1992                  ("/LIBPATH:" & Include_Dir_Default_Prefix & "\..\");
1993                --  Place to look for gcc related libraries, such as libgcc
1994
1995             begin
1996                --  Replace UNIX "/" by Windows "\" in the path
1997
1998                for Index in 10 .. Gcc_Lib_Path.all'Last loop
1999                   if Gcc_Lib_Path (Index) = '/' then
2000                      Gcc_Lib_Path (Index) := '\';
2001                   end if;
2002                end loop;
2003
2004                Linker_Options.Increment_Last;
2005                Linker_Options.Table (Linker_Options.Last) := Gcc_Lib_Path;
2006                Num_Args := Num_Args + 1;
2007
2008                for Index in Flags_For_Linker'Range loop
2009                   Linker_Options.Increment_Last;
2010                   Linker_Options.Table (Linker_Options.Last) :=
2011                     Flags_For_Linker (Index);
2012                   Num_Args := Num_Args + 1;
2013                end loop;
2014             end;
2015          end if;
2016
2017          --  Remove duplicate stack size setting from the Linker_Options
2018          --  table. The stack setting option "-Xlinker --stack=R,C" can be
2019          --  found in one line when set by a pragma Linker_Options or in two
2020          --  lines ("-Xlinker" then "--stack=R,C") when set on the command
2021          --  line. We also check for the "-Wl,--stack=R" style option.
2022
2023          --  We must remove the second stack setting option instance
2024          --  because the one on the command line will always be the first
2025          --  one. And any subsequent stack setting option will overwrite the
2026          --  previous one. This is done especially for GNAT/NT where we set
2027          --  the stack size for tasking programs by a pragma in the NT
2028          --  specific tasking package System.Task_Primitives.Operations.
2029
2030          --  Note: This is not a FOR loop that runs from Linker_Options.First
2031          --  to Linker_Options.Last, since operations within the loop can
2032          --  modify the length of the table.
2033
2034          Clean_Link_Option_Set : declare
2035             J : Natural := Linker_Options.First;
2036             Shared_Libgcc_Seen : Boolean := False;
2037
2038          begin
2039             while J <= Linker_Options.Last loop
2040
2041                if Linker_Options.Table (J).all = "-Xlinker"
2042                  and then J < Linker_Options.Last
2043                  and then Linker_Options.Table (J + 1)'Length > 8
2044                  and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
2045                then
2046                   if Stack_Op then
2047                      Linker_Options.Table (J .. Linker_Options.Last - 2) :=
2048                        Linker_Options.Table (J + 2 .. Linker_Options.Last);
2049                      Linker_Options.Decrement_Last;
2050                      Linker_Options.Decrement_Last;
2051                      Num_Args := Num_Args - 2;
2052
2053                   else
2054                      Stack_Op := True;
2055                   end if;
2056                end if;
2057
2058                --  Remove duplicate -shared-libgcc switch
2059
2060                if Linker_Options.Table (J).all = Shared_Libgcc_String then
2061                   if Shared_Libgcc_Seen then
2062                      Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2063                        Linker_Options.Table (J + 1 .. Linker_Options.Last);
2064                      Linker_Options.Decrement_Last;
2065                      Num_Args := Num_Args - 1;
2066
2067                   else
2068                      Shared_Libgcc_Seen := True;
2069                   end if;
2070                end if;
2071
2072                --  Here we just check for a canonical form that matches the
2073                --  pragma Linker_Options set in the NT runtime.
2074
2075                if (Linker_Options.Table (J)'Length > 17
2076                    and then Linker_Options.Table (J) (1 .. 17)
2077                            = "-Xlinker --stack=")
2078                  or else
2079                   (Linker_Options.Table (J)'Length > 12
2080                    and then Linker_Options.Table (J) (1 .. 12)
2081                             = "-Wl,--stack=")
2082                then
2083                   if Stack_Op then
2084                      Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2085                        Linker_Options.Table (J + 1 .. Linker_Options.Last);
2086                      Linker_Options.Decrement_Last;
2087                      Num_Args := Num_Args - 1;
2088
2089                   else
2090                      Stack_Op := True;
2091                   end if;
2092                end if;
2093
2094                --  Remove duplicate IDENTIFICATION directives (VMS)
2095
2096                if Linker_Options.Table (J)'Length > 27
2097                  and then Linker_Options.Table (J) (1 .. 28)
2098                           = "--for-linker=IDENTIFICATION="
2099                then
2100                   if IDENT_Op then
2101                      Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2102                        Linker_Options.Table (J + 1 .. Linker_Options.Last);
2103                      Linker_Options.Decrement_Last;
2104                      Num_Args := Num_Args - 1;
2105                   else
2106                      IDENT_Op := True;
2107                   end if;
2108                end if;
2109
2110                J := J + 1;
2111             end loop;
2112
2113             if Linker_Path = Gcc_Path and then VM_Target = No_VM then
2114
2115                --  If gcc is not called with -shared-libgcc, call it with
2116                --  -static-libgcc, as there are some platforms where one of
2117                --  these two switches is compulsory to link.
2118
2119                if not Shared_Libgcc_Seen then
2120                   Linker_Options.Increment_Last;
2121                   Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
2122                   Num_Args := Num_Args + 1;
2123                end if;
2124
2125             elsif RTX_RTSS_Kernel_Module_On_Target then
2126
2127                --  Force the use of the static libgcc for RTSS modules
2128
2129                Linker_Options.Increment_Last;
2130                Linker_Options.Table (Linker_Options.Last) :=
2131                  new String'("libgcc.a");
2132                Num_Args := Num_Args + 1;
2133             end if;
2134
2135          end Clean_Link_Option_Set;
2136
2137          --  Prepare arguments for call to linker
2138
2139          Call_Linker : declare
2140             Success  : Boolean;
2141             Args     : Argument_List (1 .. Num_Args + 1);
2142             Index    : Integer := Args'First;
2143
2144          begin
2145             Args (Index) := Binder_Obj_File;
2146
2147             --  Add the object files and any -largs libraries
2148
2149             for J in Linker_Objects.First .. Linker_Objects.Last loop
2150                Index := Index + 1;
2151                Args (Index) := Linker_Objects.Table (J);
2152             end loop;
2153
2154             --  Add the linker options from the binder file
2155
2156             for J in Linker_Options.First .. Linker_Options.Last loop
2157                Index := Index + 1;
2158                Args (Index) := Linker_Options.Table (J);
2159             end loop;
2160
2161             --  Finally add the libraries from the --GCC= switch
2162
2163             for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
2164                Index := Index + 1;
2165                Args (Index) := Gcc_Linker_Options.Table (J);
2166             end loop;
2167
2168             if Verbose_Mode then
2169                Write_Str (Linker_Path.all);
2170
2171                for J in Args'Range loop
2172                   Write_Str (" ");
2173                   Write_Str (Args (J).all);
2174                end loop;
2175
2176                Write_Eol;
2177
2178                --  If we are on very verbose mode (-v -v) and a response file
2179                --  is used we display its content.
2180
2181                if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
2182                   Write_Eol;
2183                   Write_Str ("Response file (" &
2184                              Tname (Tname'First .. Tname'Last - 1) &
2185                              ") content : ");
2186                   Write_Eol;
2187
2188                   for J in
2189                     Response_File_Objects.First ..
2190                     Response_File_Objects.Last
2191                   loop
2192                      Write_Str (Response_File_Objects.Table (J).all);
2193                      Write_Eol;
2194                   end loop;
2195
2196                   Write_Eol;
2197                end if;
2198             end if;
2199
2200             System.OS_Lib.Spawn (Linker_Path.all, Args, Success);
2201
2202             --  Delete the temporary file used in conjunction with linking if
2203             --  one was created. See Process_Bind_File for details.
2204
2205             if Tname_FD /= Invalid_FD then
2206                Delete (Tname);
2207             end if;
2208
2209             if Lname /= null then
2210                Delete (Lname.all & ASCII.NUL);
2211             end if;
2212
2213             if not Success then
2214                Error_Msg ("error when calling " & Linker_Path.all);
2215                Exit_Program (E_Fatal);
2216             end if;
2217          end Call_Linker;
2218       end Link_Step;
2219    end if;
2220
2221    --  Only keep the binder output file and it's associated object
2222    --  file if compiling with the -g option.  These files are only
2223    --  useful if debugging.
2224
2225    if not Debug_Flag_Present then
2226       if Binder_Ali_File /= null then
2227          Delete (Binder_Ali_File.all & ASCII.NUL);
2228       end if;
2229
2230       if Binder_Spec_Src_File /= null then
2231          Delete (Binder_Spec_Src_File.all & ASCII.NUL);
2232       end if;
2233
2234       Delete (Binder_Body_Src_File.all & ASCII.NUL);
2235
2236       if VM_Target = No_VM then
2237          Delete (Binder_Obj_File.all & ASCII.NUL);
2238       end if;
2239    end if;
2240
2241    Exit_Program (E_Success);
2242
2243 exception
2244    when X : others =>
2245       Write_Line (Exception_Information (X));
2246       Exit_With_Error ("INTERNAL ERROR. Please report");
2247 end Gnatlink;