1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2007, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- Gnatlink usage: please consult the gnat documentation
30 with Gnatvsn; use Gnatvsn;
32 with Indepsw; use Indepsw;
33 with Namet; use Namet;
35 with Osint; use Osint;
36 with Output; use Output;
38 with Switch; use Switch;
39 with System; use System;
41 with Targparm; use Targparm;
44 with Ada.Command_Line; use Ada.Command_Line;
45 with Ada.Exceptions; use Ada.Exceptions;
47 with System.OS_Lib; use System.OS_Lib;
50 with Interfaces.C_Streams; use Interfaces.C_Streams;
51 with Interfaces.C.Strings; use Interfaces.C.Strings;
54 pragma Ident (Gnatvsn.Gnat_Static_Version_String);
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
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
66 package Gcc_Linker_Options is new Table.Table (
67 Table_Component_Type => String_Access,
68 Table_Index_Type => Integer,
71 Table_Increment => 100,
72 Table_Name => "Gnatlink.Gcc_Linker_Options");
73 -- Comments needed ???
75 package Libpath is new Table.Table (
76 Table_Component_Type => Character,
77 Table_Index_Type => Integer,
79 Table_Initial => 4096,
80 Table_Increment => 100,
81 Table_Name => "Gnatlink.Libpath");
82 -- Comments needed ???
84 package Linker_Options is new Table.Table (
85 Table_Component_Type => String_Access,
86 Table_Index_Type => Integer,
89 Table_Increment => 100,
90 Table_Name => "Gnatlink.Linker_Options");
91 -- Comments needed ???
93 package Linker_Objects is new Table.Table (
94 Table_Component_Type => String_Access,
95 Table_Index_Type => Integer,
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 fullname.
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,
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)
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
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
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
135 Table_Increment => 100,
136 Table_Name => "Gnatlink.Binder_Options");
137 -- This table collects the arguments to be passed to compile the binder
140 Gcc : String_Access := Program_Name ("gcc");
142 Read_Mode : constant String := "r" & ASCII.Nul;
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
148 Gcc_Path : String_Access;
149 Linker_Path : String_Access;
151 Output_File_Name : String_Access;
152 Ali_File_Name : String_Access;
153 Binder_Spec_Src_File : String_Access;
154 Binder_Body_Src_File : String_Access;
155 Binder_Ali_File : String_Access;
156 Binder_Obj_File : String_Access;
158 Tname : Temp_File_Name;
159 Tname_FD : File_Descriptor := Invalid_FD;
160 -- Temporary file used by linker to pass list of object files on
161 -- certain systems with limitations on size of arguments.
163 Debug_Flag_Present : Boolean := False;
164 Verbose_Mode : Boolean := False;
165 Very_Verbose_Mode : Boolean := False;
167 Ada_Bind_File : Boolean := True;
168 -- Set to True if bind file is generated in Ada
170 Standard_Gcc : Boolean := True;
172 Compile_Bind_File : Boolean := True;
173 -- Set to False if bind file is not to be compiled
175 Create_Map_File : Boolean := False;
176 -- Set to True by switch -M. The map file name is derived from
177 -- the ALI file name (mainprog.ali => mainprog.map).
179 Object_List_File_Supported : Boolean;
180 for Object_List_File_Supported'Size use Character'Size;
182 (C, Object_List_File_Supported, "__gnat_objlist_file_supported");
183 -- Predicate indicating whether the linker has an option whereby the
184 -- names of object files can be passed to the linker in a file.
186 Object_List_File_Required : Boolean := False;
187 -- Set to True to force generation of a response file
189 function Base_Name (File_Name : String) return String;
190 -- Return just the file name part without the extension (if present)
192 procedure Delete (Name : String);
193 -- Wrapper to unlink as status is ignored by this application
195 procedure Error_Msg (Message : String);
196 -- Output the error or warning Message
198 procedure Exit_With_Error (Error : String);
199 -- Output Error and exit program with a fatal condition
201 procedure Process_Args;
202 -- Go through all the arguments and build option tables
204 procedure Process_Binder_File (Name : String);
205 -- Reads the binder file and extracts linker arguments
210 procedure Write_Header;
211 -- Show user the program name, version and copyright
213 procedure Write_Usage;
214 -- Show user the program options
220 function Base_Name (File_Name : String) return String is
225 Findex1 := File_Name'First;
227 -- The file might be specified by a full path name. However,
228 -- we want the path to be stripped away.
230 for J in reverse File_Name'Range loop
231 if Is_Directory_Separator (File_Name (J)) then
237 Findex2 := File_Name'Last;
238 while Findex2 > Findex1
239 and then File_Name (Findex2) /= '.'
241 Findex2 := Findex2 - 1;
244 if Findex2 = Findex1 then
245 Findex2 := File_Name'Last + 1;
248 return File_Name (Findex1 .. Findex2 - 1);
255 procedure Delete (Name : String) is
257 pragma Unreferenced (Status);
259 Status := unlink (Name'Address);
260 -- Is it really right to ignore an error here ???
267 procedure Error_Msg (Message : String) is
269 Write_Str (Base_Name (Command_Name));
275 ---------------------
276 -- Exit_With_Error --
277 ---------------------
279 procedure Exit_With_Error (Error : String) is
282 Exit_Program (E_Fatal);
289 procedure Process_Args is
291 Skip_Next : Boolean := False;
292 -- Set to true if the next argument is to be added into the list of
293 -- linker's argument without parsing it.
296 -- First, check for --version and --help
298 Check_Version_And_Help ("GNATLINK", "1995", Usage'Unrestricted_Access);
300 -- Loop through arguments of gnatlink command
304 exit when Next_Arg > Argument_Count;
306 Process_One_Arg : declare
307 Arg : constant String := Argument (Next_Arg);
310 -- Case of argument which is a switch
312 -- We definitely need section by section comments here ???
316 -- This argument must not be parsed, just add it to the
317 -- list of linker's options.
321 Linker_Options.Increment_Last;
322 Linker_Options.Table (Linker_Options.Last) :=
325 elsif Arg'Length /= 0 and then Arg (1) = '-' then
326 if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then
328 ("invalid switch: """ & Arg & """ (gnat not needed here)");
331 if Arg = "-Xlinker" then
333 -- Next argument should be sent directly to the linker.
334 -- We do not want to parse it here.
338 Linker_Options.Increment_Last;
339 Linker_Options.Table (Linker_Options.Last) :=
343 and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
345 Debug_Flag_Present := True;
347 Linker_Options.Increment_Last;
348 Linker_Options.Table (Linker_Options.Last) :=
351 Binder_Options.Increment_Last;
352 Binder_Options.Table (Binder_Options.Last) :=
353 Linker_Options.Table (Linker_Options.Last);
355 elsif Arg'Length >= 3 and then Arg (2) = 'M' then
357 Switches : String_List_Access;
360 Convert (Map_File, Arg (3 .. Arg'Last), Switches);
362 if Switches /= null then
363 for J in Switches'Range loop
364 Linker_Options.Increment_Last;
365 Linker_Options.Table (Linker_Options.Last) :=
371 elsif Arg'Length = 2 then
374 Ada_Bind_File := True;
375 Begin_Info := "-- BEGIN Object file/option list";
376 End_Info := "-- END Object file/option list ";
379 Linker_Options.Increment_Last;
380 Linker_Options.Table (Linker_Options.Last) :=
383 Binder_Options.Increment_Last;
384 Binder_Options.Table (Binder_Options.Last) :=
385 Linker_Options.Table (Linker_Options.Last);
387 Next_Arg := Next_Arg + 1;
389 if Next_Arg > Argument_Count then
390 Exit_With_Error ("Missing argument for -b");
393 Get_Machine_Name : declare
394 Name_Arg : constant String_Access :=
395 new String'(Argument (Next_Arg));
398 Linker_Options.Increment_Last;
399 Linker_Options.Table (Linker_Options.Last) :=
402 Binder_Options.Increment_Last;
403 Binder_Options.Table (Binder_Options.Last) :=
406 end Get_Machine_Name;
409 Ada_Bind_File := False;
410 Begin_Info := "/* BEGIN Object file/option list";
411 End_Info := " END Object file/option list */";
414 if Object_List_File_Supported then
415 Object_List_File_Required := True;
418 ("Object list file not supported on this target");
422 Create_Map_File := True;
425 Compile_Bind_File := False;
428 if VM_Target = CLI_Target then
429 Linker_Options.Increment_Last;
430 Linker_Options.Table (Linker_Options.Last) :=
431 new String'("/QUIET");
434 Linker_Options.Increment_Last;
435 Linker_Options.Table (Linker_Options.Last) :=
439 Next_Arg := Next_Arg + 1;
441 if Next_Arg > Argument_Count then
442 Exit_With_Error ("Missing argument for -o");
445 if VM_Target = CLI_Target then
447 new String'("/OUTPUT=" & Argument (Next_Arg));
450 new String'(Argument (Next_Arg));
453 Linker_Options.Increment_Last;
454 Linker_Options.Table (Linker_Options.Last) :=
458 Opt.Run_Path_Option := False;
462 -- Support "double" verbose mode. Second -v
463 -- gets sent to the linker and binder phases.
466 Very_Verbose_Mode := True;
468 Linker_Options.Increment_Last;
469 Linker_Options.Table (Linker_Options.Last) :=
472 Binder_Options.Increment_Last;
473 Binder_Options.Table (Binder_Options.Last) :=
474 Linker_Options.Table (Linker_Options.Last);
477 Verbose_Mode := True;
482 Linker_Options.Increment_Last;
483 Linker_Options.Table (Linker_Options.Last) :=
488 elsif Arg (2) = 'B' then
489 Linker_Options.Increment_Last;
490 Linker_Options.Table (Linker_Options.Last) :=
493 Binder_Options.Increment_Last;
494 Binder_Options.Table (Binder_Options.Last) :=
495 Linker_Options.Table (Linker_Options.Last);
497 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
498 if Arg'Length = 7 then
499 Exit_With_Error ("Missing argument for --LINK=");
503 System.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
505 if Linker_Path = null then
507 ("Could not locate linker: " & Arg (8 .. Arg'Last));
510 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
512 Program_Args : constant Argument_List_Access :=
513 Argument_String_To_List
514 (Arg (7 .. Arg'Last));
517 Gcc := new String'(Program_Args.all (1).all);
518 Standard_Gcc := False;
520 -- Set appropriate flags for switches passed
522 for J in 2 .. Program_Args.all'Last loop
524 Arg : constant String := Program_Args.all (J).all;
525 AF : constant Integer := Arg'First;
528 if Arg'Length /= 0 and then Arg (AF) = '-' then
529 if Arg (AF + 1) = 'g'
530 and then (Arg'Length = 2
531 or else Arg (AF + 2) in '0' .. '3'
532 or else Arg (AF + 2 .. Arg'Last) = "coff")
534 Debug_Flag_Present := True;
538 -- Add directory to source search dirs so that
539 -- Get_Target_Parameters can find system.ads
541 if Arg (AF .. AF + 1) = "-I"
542 and then Arg'Length > 2
544 Add_Src_Search_Dir (Arg (AF + 2 .. Arg'Last));
547 -- Pass to gcc for compiling binder generated file
548 -- No use passing libraries, it will just generate
551 if not (Arg (AF .. AF + 1) = "-l"
552 or else Arg (AF .. AF + 1) = "-L")
554 Binder_Options.Increment_Last;
555 Binder_Options.Table (Binder_Options.Last) :=
559 -- Pass to gcc for linking program
561 Gcc_Linker_Options.Increment_Last;
562 Gcc_Linker_Options.Table
563 (Gcc_Linker_Options.Last) := new String'(Arg);
568 -- Send all multi-character switches not recognized as
569 -- a special case by gnatlink to the linker/loader stage.
572 Linker_Options.Increment_Last;
573 Linker_Options.Table (Linker_Options.Last) :=
577 -- Here if argument is a file name rather than a switch
580 -- If explicit ali file, capture it
583 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
585 if Ali_File_Name = null then
586 Ali_File_Name := new String'(Arg);
588 Exit_With_Error ("cannot handle more than one ALI file");
591 -- If target object file, record object file
593 elsif Arg'Length > Get_Target_Object_Suffix.all'Length
596 Get_Target_Object_Suffix.all'Length + 1 .. Arg'Last)
597 = Get_Target_Object_Suffix.all
599 Linker_Objects.Increment_Last;
600 Linker_Objects.Table (Linker_Objects.Last) :=
603 -- If host object file, record object file
604 -- e.g. accept foo.o as well as foo.obj on VMS target
606 elsif Arg'Length > Get_Object_Suffix.all'Length
608 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
609 = Get_Object_Suffix.all
611 Linker_Objects.Increment_Last;
612 Linker_Objects.Table (Linker_Objects.Last) :=
615 -- If corresponding ali file exists, capture it
617 elsif Ali_File_Name = null
618 and then Is_Regular_File (Arg & ".ali")
620 Ali_File_Name := new String'(Arg & ".ali");
622 -- Otherwise assume this is a linker options entry, but
623 -- see below for interesting adjustment to this assumption.
626 Linker_Options.Increment_Last;
627 Linker_Options.Table (Linker_Options.Last) :=
633 Next_Arg := Next_Arg + 1;
636 -- If Ada bind file, then compile it with warnings suppressed, because
637 -- otherwise the with of the main program may cause junk warnings.
639 if Ada_Bind_File then
640 Binder_Options.Increment_Last;
641 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
644 -- If we did not get an ali file at all, and we had at least one
645 -- linker option, then assume that was the intended ali file after
646 -- all, so that we get a nicer message later on.
648 if Ali_File_Name = null
649 and then Linker_Options.Last >= Linker_Options.First
652 new String'(Linker_Options.Table (Linker_Options.First).all &
657 -------------------------
658 -- Process_Binder_File --
659 -------------------------
661 procedure Process_Binder_File (Name : String) is
663 -- Binder file's descriptor
665 Link_Bytes : Integer := 0;
666 -- Projected number of bytes for the linker command line
669 pragma Import (C, Link_Max, "__gnat_link_max");
670 -- Maximum number of bytes on the command line supported by the OS
671 -- linker. Passed this limit the response file mechanism must be used
674 Next_Line : String (1 .. 1000);
675 -- Current line value
679 -- Current line slice (the slice does not contain line terminator)
682 -- Current line last character for shared libraries (without version)
684 Objs_Begin : Integer := 0;
685 -- First object file index in Linker_Objects table
687 Objs_End : Integer := 0;
688 -- Last object file index in Linker_Objects table
691 pragma Warnings (Off, Status);
692 -- Used for various Interfaces.C_Streams calls
694 Closing_Status : Boolean;
697 GNAT_Static : Boolean := False;
698 -- Save state of -static option
700 GNAT_Shared : Boolean := False;
701 -- Save state of -shared option
703 Xlinker_Was_Previous : Boolean := False;
704 -- Indicate that "-Xlinker" was the option preceding the current
705 -- option. If True, then the current option is never suppressed.
709 -- These data items are used to store current binder file context.
710 -- The context is composed of the file descriptor position and the
711 -- current line together with the slice indexes (first and last
712 -- position) for this line. The rollback data are used by the
713 -- Store_File_Context and Rollback_File_Context routines below.
714 -- The file context mechanism interact only with the Get_Next_Line
715 -- call. For example:
717 -- Store_File_Context;
719 -- Rollback_File_Context;
722 -- Both Get_Next_Line calls above will read the exact same data from
723 -- the file. In other words, Next_Line, Nfirst and Nlast variables
724 -- will be set with the exact same values.
726 RB_File_Pos : long; -- File position
727 RB_Next_Line : String (1 .. 1000); -- Current line content
728 RB_Nlast : Integer; -- Slice last index
729 RB_Nfirst : Integer; -- Slice first index
731 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
732 pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
733 -- Pointer to string representing the native linker option which
734 -- specifies the path where the dynamic loader should find shared
735 -- libraries. Equal to null string if this system doesn't support it.
737 Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
739 (C, Object_Library_Ext_Ptr, "__gnat_object_library_extension");
740 -- Pointer to string specifying the default extension for
741 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
743 Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
744 pragma Import (C, Object_File_Option_Ptr, "__gnat_object_file_option");
745 -- Pointer to a string representing the linker option which specifies
746 -- the response file.
748 Using_GNU_Linker : Boolean;
749 for Using_GNU_Linker'Size use Character'Size;
750 pragma Import (C, Using_GNU_Linker, "__gnat_using_gnu_linker");
751 -- Predicate indicating whether this target uses the GNU linker. In
752 -- this case we must output a GNU linker compatible response file.
754 Opening : aliased constant String := """";
755 Closing : aliased constant String := '"' & ASCII.LF;
756 -- Needed to quote object paths in object list files when GNU linker
759 procedure Get_Next_Line;
760 -- Read the next line from the binder file without the line
763 function Index (S, Pattern : String) return Natural;
764 -- Return the last occurrence of Pattern in S, or 0 if none
766 function Is_Option_Present (Opt : String) return Boolean;
767 -- Return true if the option Opt is already present in
768 -- Linker_Options table.
770 procedure Store_File_Context;
771 -- Store current file context, Fd position and current line data.
772 -- The file context is stored into the rollback data above (RB_*).
773 -- Store_File_Context can be called at any time, only the last call
774 -- will be used (i.e. this routine overwrites the file context).
776 procedure Rollback_File_Context;
777 -- Restore file context from rollback data. This routine must be called
778 -- after Store_File_Context. The binder file context will be restored
779 -- with the data stored by the last Store_File_Context call.
785 procedure Get_Next_Line is
789 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
791 if Fchars = System.Null_Address then
792 Exit_With_Error ("Error reading binder output");
795 Nfirst := Next_Line'First;
797 while Nlast <= Next_Line'Last
798 and then Next_Line (Nlast) /= ASCII.LF
799 and then Next_Line (Nlast) /= ASCII.CR
811 function Index (S, Pattern : String) return Natural is
812 Len : constant Natural := Pattern'Length;
815 for J in reverse S'First .. S'Last - Len + 1 loop
816 if Pattern = S (J .. J + Len - 1) then
824 -----------------------
825 -- Is_Option_Present --
826 -----------------------
828 function Is_Option_Present (Opt : String) return Boolean is
830 for I in 1 .. Linker_Options.Last loop
832 if Linker_Options.Table (I).all = Opt then
839 end Is_Option_Present;
841 ---------------------------
842 -- Rollback_File_Context --
843 ---------------------------
845 procedure Rollback_File_Context is
847 Next_Line := RB_Next_Line;
850 Status := fseek (Fd, RB_File_Pos, Interfaces.C_Streams.SEEK_SET);
853 Exit_With_Error ("Error setting file position");
855 end Rollback_File_Context;
857 ------------------------
858 -- Store_File_Context --
859 ------------------------
861 procedure Store_File_Context is
862 use type System.CRTL.long;
864 RB_Next_Line := Next_Line;
867 RB_File_Pos := ftell (Fd);
869 if RB_File_Pos = -1 then
870 Exit_With_Error ("Error getting file position");
872 end Store_File_Context;
874 -- Start of processing for Process_Binder_File
877 Fd := fopen (Name'Address, Read_Mode'Address);
879 if Fd = NULL_Stream then
880 Exit_With_Error ("Failed to open binder output");
883 -- Skip up to the Begin Info line
887 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
893 -- Go to end when end line is reached (this will happen in
894 -- High_Integrity_Mode where no -L switches are generated)
896 exit when Next_Line (Nfirst .. Nlast) = End_Info;
898 if Ada_Bind_File then
899 Next_Line (Nfirst .. Nlast - 8) :=
900 Next_Line (Nfirst + 8 .. Nlast);
904 -- Go to next section when switches are reached
906 exit when Next_Line (1) = '-';
908 -- Otherwise we have another object file to collect
910 Linker_Objects.Increment_Last;
912 -- Mark the positions of first and last object files in case
913 -- they need to be placed with a named file on systems having
914 -- linker line limitations.
916 if Objs_Begin = 0 then
917 Objs_Begin := Linker_Objects.Last;
920 Linker_Objects.Table (Linker_Objects.Last) :=
921 new String'(Next_Line (Nfirst .. Nlast));
923 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
924 -- Nlast - Nfirst + 1, for the size, plus one for the space between
928 Objs_End := Linker_Objects.Last;
930 -- Let's continue to compute the Link_Bytes, the linker options are
931 -- part of command line length.
935 while Next_Line (Nfirst .. Nlast) /= End_Info loop
936 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
941 Rollback_File_Context;
943 -- On systems that have limitations on handling very long linker lines
944 -- we make use of the system linker option which takes a list of object
945 -- file names from a file instead of the command line itself. What we do
946 -- is to replace the list of object files by the special linker option
947 -- which then reads the object file list from a file instead. The option
948 -- to read from a file instead of the command line is only triggered if
949 -- a conservative threshold is passed.
951 if Object_List_File_Required
952 or else (Object_List_File_Supported
953 and then Link_Bytes > Link_Max)
955 -- Create a temporary file containing the Ada user object files
956 -- needed by the link. This list is taken from the bind file
957 -- and is output one object per line for maximal compatibility with
958 -- linkers supporting this option.
960 Create_Temp_File (Tname_FD, Tname);
962 -- ??? File descriptor should be checked to not be Invalid_FD.
963 -- ??? Status of Write and Close operations should be checked, and
964 -- failure should occur if a status is wrong.
966 -- If target is using the GNU linker we must add a special header
967 -- and footer in the response file.
969 -- The syntax is : INPUT (object1.o object2.o ... )
971 -- Because the GNU linker does not like name with characters such
972 -- as '!', we must put the object paths between double quotes.
974 if Using_GNU_Linker then
976 GNU_Header : aliased constant String := "INPUT (";
979 Status := Write (Tname_FD, GNU_Header'Address,
984 for J in Objs_Begin .. Objs_End loop
986 -- Opening quote for GNU linker
988 if Using_GNU_Linker then
989 Status := Write (Tname_FD, Opening'Address, 1);
992 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
993 Linker_Objects.Table (J).all'Length);
995 -- Closing quote for GNU linker
997 if Using_GNU_Linker then
998 Status := Write (Tname_FD, Closing'Address, 2);
1001 Status := Write (Tname_FD, ASCII.LF'Address, 1);
1004 Response_File_Objects.Increment_Last;
1005 Response_File_Objects.Table (Response_File_Objects.Last) :=
1006 Linker_Objects.Table (J);
1009 -- Handle GNU linker response file footer
1011 if Using_GNU_Linker then
1013 GNU_Footer : aliased constant String := ")";
1016 Status := Write (Tname_FD, GNU_Footer'Address,
1021 Close (Tname_FD, Closing_Status);
1023 -- Add the special objects list file option together with the name
1024 -- of the temporary file (removing the null character) to the objects
1027 Linker_Objects.Table (Objs_Begin) :=
1028 new String'(Value (Object_File_Option_Ptr) &
1029 Tname (Tname'First .. Tname'Last - 1));
1031 -- The slots containing these object file names are then removed
1032 -- from the objects table so they do not appear in the link. They
1033 -- are removed by moving up the linker options and non-Ada object
1034 -- files appearing after the Ada object list in the table.
1040 N := Objs_End - Objs_Begin + 1;
1042 for J in Objs_End + 1 .. Linker_Objects.Last loop
1043 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
1046 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
1050 -- Process switches and options
1052 if Next_Line (Nfirst .. Nlast) /= End_Info then
1053 Xlinker_Was_Previous := False;
1056 if Xlinker_Was_Previous
1057 or else Next_Line (Nfirst .. Nlast) = "-Xlinker"
1059 Linker_Options.Increment_Last;
1060 Linker_Options.Table (Linker_Options.Last) :=
1061 new String'(Next_Line (Nfirst .. Nlast));
1063 elsif Next_Line (Nfirst .. Nlast) = "-static" then
1064 GNAT_Static := True;
1066 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1067 GNAT_Shared := True;
1069 -- Add binder options only if not already set on the command
1070 -- line. This rule is a way to control the linker options order.
1072 -- The following test needs comments, why is it VMS specific.
1073 -- The above comment looks out of date ???
1075 elsif not (OpenVMS_On_Target
1077 Is_Option_Present (Next_Line (Nfirst .. Nlast)))
1079 if Nlast > Nfirst + 2 and then
1080 Next_Line (Nfirst .. Nfirst + 1) = "-L"
1082 -- Construct a library search path for use later
1083 -- to locate static gnatlib libraries.
1085 if Libpath.Last > 1 then
1086 Libpath.Increment_Last;
1087 Libpath.Table (Libpath.Last) := Path_Separator;
1090 for I in Nfirst + 2 .. Nlast loop
1091 Libpath.Increment_Last;
1092 Libpath.Table (Libpath.Last) := Next_Line (I);
1095 Linker_Options.Increment_Last;
1097 Linker_Options.Table (Linker_Options.Last) :=
1098 new String'(Next_Line (Nfirst .. Nlast));
1100 elsif Next_Line (Nfirst .. Nlast) = "-ldecgnat"
1101 or else Next_Line (Nfirst .. Nlast) = "-lgnarl"
1102 or else Next_Line (Nfirst .. Nlast) = "-lgnat"
1104 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) =
1105 Shared_Lib ("gnarl")
1107 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) =
1110 -- If it is a shared library, remove the library version.
1111 -- We will be looking for the static version of the library
1112 -- as it is in the same directory as the shared version.
1114 if Next_Line (Nlast - Library_Version'Length + 1 .. Nlast)
1117 -- Set Last to point to last character before the
1120 Last := Nlast - Library_Version'Length - 1;
1125 -- Given a Gnat standard library, search the
1126 -- library path to find the library location
1129 File_Path : String_Access;
1131 Object_Lib_Extension : constant String :=
1132 Value (Object_Library_Ext_Ptr);
1134 File_Name : constant String := "lib" &
1135 Next_Line (Nfirst + 2 .. Last) &
1136 Object_Lib_Extension;
1138 Run_Path_Opt : constant String :=
1139 Value (Run_Path_Option_Ptr);
1141 GCC_Index : Natural;
1142 Run_Path_Opt_Index : Natural := 0;
1146 Locate_Regular_File (File_Name,
1147 String (Libpath.Table (1 .. Libpath.Last)));
1149 if File_Path /= null then
1152 -- If static gnatlib found, explicitly
1153 -- specify to overcome possible linker
1154 -- default usage of shared version.
1156 Linker_Options.Increment_Last;
1158 Linker_Options.Table (Linker_Options.Last) :=
1159 new String'(File_Path.all);
1161 elsif GNAT_Shared then
1162 if Opt.Run_Path_Option then
1163 -- If shared gnatlib desired, add the
1164 -- appropriate system specific switch
1165 -- so that it can be located at runtime.
1167 if Run_Path_Opt'Length /= 0 then
1168 -- Output the system specific linker command
1169 -- that allows the image activator to find
1170 -- the shared library at runtime.
1171 -- Also add path to find libgcc_s.so, if
1174 -- To find the location of the shared version
1175 -- of libgcc, we look for "gcc-lib" in the
1176 -- path of the library. However, this
1177 -- subdirectory is no longer present in
1178 -- in recent version of GCC. So, we look for
1179 -- the last subdirectory "lib" in the path.
1182 Index (File_Path.all, "gcc-lib");
1184 if GCC_Index /= 0 then
1185 -- The shared version of libgcc is
1186 -- located in the parent directory.
1188 GCC_Index := GCC_Index - 1;
1192 Index (File_Path.all, "/lib/");
1194 if GCC_Index = 0 then
1196 Index (File_Path.all,
1197 Directory_Separator &
1199 Directory_Separator);
1202 -- We have found a subdirectory "lib",
1203 -- this is where the shared version of
1204 -- libgcc should be located.
1206 if GCC_Index /= 0 then
1207 GCC_Index := GCC_Index + 3;
1211 -- Look for an eventual run_path_option in
1212 -- the linker switches.
1214 for J in reverse 1 .. Linker_Options.Last loop
1215 if Linker_Options.Table (J) /= null
1217 Linker_Options.Table (J)'Length
1218 > Run_Path_Opt'Length
1220 Linker_Options.Table (J)
1221 (1 .. Run_Path_Opt'Length) =
1224 -- We have found a already specified
1225 -- run_path_option: we will add to this
1226 -- switch, because only one
1227 -- run_path_option should be specified.
1229 Run_Path_Opt_Index := J;
1234 -- If there is no run_path_option, we need
1237 if Run_Path_Opt_Index = 0 then
1238 Linker_Options.Increment_Last;
1241 if GCC_Index = 0 then
1242 if Run_Path_Opt_Index = 0 then
1243 Linker_Options.Table
1244 (Linker_Options.Last) :=
1248 (1 .. File_Path'Length
1249 - File_Name'Length));
1252 Linker_Options.Table
1253 (Run_Path_Opt_Index) :=
1255 (Linker_Options.Table
1256 (Run_Path_Opt_Index).all
1259 (1 .. File_Path'Length
1260 - File_Name'Length));
1264 if Run_Path_Opt_Index = 0 then
1265 Linker_Options.Table
1266 (Linker_Options.Last) :=
1267 new String'(Run_Path_Opt
1269 (1 .. File_Path'Length
1272 & File_Path (1 .. GCC_Index));
1275 Linker_Options.Table
1276 (Run_Path_Opt_Index) :=
1278 (Linker_Options.Table
1279 (Run_Path_Opt_Index).all
1282 (1 .. File_Path'Length
1285 & File_Path (1 .. GCC_Index));
1291 -- Then we add the appropriate -l switch
1293 Linker_Options.Increment_Last;
1294 Linker_Options.Table (Linker_Options.Last) :=
1295 new String'(Next_Line (Nfirst .. Nlast));
1299 -- If gnatlib library not found, then
1300 -- add it anyway in case some other
1301 -- mechanimsm may find it.
1303 Linker_Options.Increment_Last;
1304 Linker_Options.Table (Linker_Options.Last) :=
1305 new String'(Next_Line (Nfirst .. Nlast));
1309 Linker_Options.Increment_Last;
1310 Linker_Options.Table (Linker_Options.Last) :=
1311 new String'(Next_Line (Nfirst .. Nlast));
1315 Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker";
1318 exit when Next_Line (Nfirst .. Nlast) = End_Info;
1320 if Ada_Bind_File then
1321 Next_Line (Nfirst .. Nlast - 8) :=
1322 Next_Line (Nfirst + 8 .. Nlast);
1328 -- If -shared was specified, invoke gcc with -shared-libgcc
1331 Linker_Options.Increment_Last;
1332 Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc;
1335 Status := fclose (Fd);
1336 end Process_Binder_File;
1344 Write_Str ("Usage: ");
1345 Write_Str (Base_Name (Command_Name));
1346 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
1349 Write_Line (" mainprog.ali the ALI file of the main program");
1351 Write_Line (" -A Binder generated source file is in Ada (default)");
1352 Write_Line (" -C Binder generated source file is in C");
1353 Write_Line (" -f force object file list to be generated");
1354 Write_Line (" -g Compile binder source file with debug information");
1355 Write_Line (" -n Do not compile the binder source file");
1356 Write_Line (" -R Do not use a run_path_option");
1357 Write_Line (" -v verbose mode");
1358 Write_Line (" -v -v very verbose mode");
1360 Write_Line (" -o nam Use 'nam' as the name of the executable");
1361 Write_Line (" -b target Compile the binder source to run on target");
1362 Write_Line (" -Bdir Load compiler executables from dir");
1364 if Is_Supported (Map_File) then
1365 Write_Line (" -Mmap Create map file map");
1366 Write_Line (" -M Create map file mainprog.map");
1369 Write_Line (" --GCC=comp Use comp as the compiler");
1370 Write_Line (" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1372 Write_Line (" [non-Ada-objects] list of non Ada object files");
1373 Write_Line (" [linker-options] other options for the linker");
1380 procedure Write_Header is
1382 if Verbose_Mode then
1384 Display_Version ("GNATLINK", "1995");
1392 procedure Write_Usage is
1398 -- Start of processing for Gnatlink
1401 -- Add the directory where gnatlink is invoked in front of the
1402 -- path, if gnatlink is invoked with directory information.
1403 -- Only do this if the platform is not VMS, where the notion of path
1404 -- does not really exist.
1406 if not Hostparm.OpenVMS then
1408 Command : constant String := Command_Name;
1411 for Index in reverse Command'Range loop
1412 if Command (Index) = Directory_Separator then
1414 Absolute_Dir : constant String :=
1416 (Command (Command'First .. Index));
1418 PATH : constant String :=
1421 Getenv ("PATH").all;
1424 Setenv ("PATH", PATH);
1435 if Argument_Count = 0
1437 (Verbose_Mode and then Argument_Count = 1)
1440 Exit_Program (E_Fatal);
1443 -- Get target parameters
1448 Osint.Add_Default_Search_Dirs;
1449 Targparm.Get_Target_Parameters;
1451 if VM_Target /= No_VM then
1453 when JVM_Target => Gcc := new String'("jgnat");
1454 when CLI_Target => Gcc := new String'("dotnet-gnatcompile");
1455 when No_VM => raise Program_Error;
1458 Ada_Bind_File := True;
1459 Begin_Info := "-- BEGIN Object file/option list";
1460 End_Info := "-- END Object file/option list ";
1463 -- We always compile with -c
1465 Binder_Options_From_ALI.Increment_Last;
1466 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1469 -- If the main program is in Ada it is compiled with the following
1472 -- -gnatA stops reading gnat.adc, since we don't know what
1473 -- pagmas would work, and we do not need it anyway.
1475 -- -gnatWb allows brackets coding for wide characters
1477 -- -gnatiw allows wide characters in identifiers. This is needed
1478 -- because bindgen uses brackets encoding for all upper
1479 -- half and wide characters in identifier names.
1481 if Ada_Bind_File then
1482 Binder_Options_From_ALI.Increment_Last;
1483 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1484 new String'("-gnatA");
1485 Binder_Options_From_ALI.Increment_Last;
1486 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1487 new String'("-gnatWb");
1488 Binder_Options_From_ALI.Increment_Last;
1489 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1490 new String'("-gnatiw");
1493 -- Locate all the necessary programs and verify required files are present
1495 Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1497 if Gcc_Path = null then
1498 Exit_With_Error ("Couldn't locate " & Gcc.all);
1501 if Linker_Path = null then
1502 if VM_Target = CLI_Target then
1503 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("ilasm");
1505 if Linker_Path = null then
1506 Exit_With_Error ("Couldn't locate ilasm");
1509 Linker_Path := Gcc_Path;
1513 if Ali_File_Name = null then
1514 Exit_With_Error ("no ali file given for link");
1517 if not Is_Regular_File (Ali_File_Name.all) then
1518 Exit_With_Error (Ali_File_Name.all & " not found");
1521 -- Read the ALI file of the main subprogram if the binder generated
1522 -- file needs to be compiled and no --GCC= switch has been specified.
1523 -- Fetch the back end switches from this ALI file and use these switches
1524 -- to compile the binder generated file
1526 if Compile_Bind_File and then Standard_Gcc then
1529 Name_Len := Ali_File_Name'Length;
1530 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1534 F : constant File_Name_Type := Name_Find;
1535 T : Text_Buffer_Ptr;
1539 -- Load the ALI file
1541 T := Read_Library_Info (F, True);
1543 -- Read it. Note that we ignore errors, since we only want very
1544 -- limited information from the ali file, and likely a slightly
1545 -- wrong version will be just fine, though in normal operation
1546 -- we don't expect this to happen!
1553 Ignore_Errors => True);
1555 if A /= No_ALI_Id then
1557 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1558 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1560 -- Do not compile with the front end switches. However, --RTS
1561 -- is to be dealt with specially because it needs to be passed
1562 -- if the binder-generated file is in Ada and may also be used
1563 -- to drive the linker.
1566 Arg : String_Ptr renames Args.Table (Index);
1568 if not Is_Front_End_Switch (Arg.all) then
1569 Binder_Options_From_ALI.Increment_Last;
1570 Binder_Options_From_ALI.Table
1571 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1573 elsif Arg'Length > 5
1574 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1576 if Ada_Bind_File then
1577 Binder_Options_From_ALI.Increment_Last;
1578 Binder_Options_From_ALI.Table
1579 (Binder_Options_From_ALI.Last)
1580 := String_Access (Arg);
1583 -- GNAT doesn't support the GCC multilib mechanism.
1584 -- This means that, when a multilib switch is used
1585 -- to request a particular compilation mode, the
1586 -- corresponding runtime switch (--RTS) must also be
1587 -- specified. The long-term goal is to fully support the
1588 -- multilib mechanism; however, in the meantime, it is
1589 -- convenient to eliminate the redundancy by keying the
1590 -- compilation mode on a single switch, namely --RTS.
1592 -- Pass -mrtp to the linker if --RTS=rtp was passed.
1594 if Linker_Path = Gcc_Path
1595 and then Arg'Length > 8
1596 and then Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1598 Linker_Options.Increment_Last;
1599 Linker_Options.Table (Linker_Options.Last) :=
1600 new String'("-mrtp");
1602 -- Pass -fsjlj to the linker if --RTS=sjlj was passed.
1604 elsif Linker_Path = Gcc_Path
1605 and then Arg'Length > 9
1606 and then Arg (Arg'First + 6 .. Arg'First + 9) = "sjlj"
1608 Linker_Options.Increment_Last;
1609 Linker_Options.Table (Linker_Options.Last) :=
1610 new String'("-fsjlj");
1621 -- If no output name specified, then use the base name of .ali file name
1623 if Output_File_Name = null then
1625 new String'(Base_Name (Ali_File_Name.all)
1626 & Get_Target_Debuggable_Suffix.all);
1628 if VM_Target = CLI_Target then
1629 Linker_Options.Increment_Last;
1630 Linker_Options.Table (Linker_Options.Last) := new String'("/QUIET");
1632 Linker_Options.Increment_Last;
1633 Linker_Options.Table (Linker_Options.Last) := new String'("/DEBUG");
1635 Linker_Options.Increment_Last;
1636 Linker_Options.Table (Linker_Options.Last) :=
1637 new String'("/OUTPUT=" & Output_File_Name.all);
1640 Linker_Options.Increment_Last;
1641 Linker_Options.Table (Linker_Options.Last) := new String'("-o");
1643 Linker_Options.Increment_Last;
1644 Linker_Options.Table (Linker_Options.Last) :=
1645 new String'(Output_File_Name.all);
1649 -- Warn if main program is called "test", as that may be a built-in command
1650 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1651 -- will not appear. However, do not warn in the case of a cross compiler.
1653 -- Assume this is a cross tool if the executable name is not gnatlink
1655 if Base_Name (Command_Name) = "gnatlink"
1656 and then Output_File_Name.all = "test"
1658 Error_Msg ("warning: executable name """ & Output_File_Name.all
1659 & """ may conflict with shell command");
1662 -- If -M switch was specified, add the switches to create the map file
1664 if Create_Map_File then
1666 Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map";
1667 Switches : String_List_Access;
1670 Convert (Map_File, Map_Name, Switches);
1672 if Switches /= null then
1673 for J in Switches'Range loop
1674 Linker_Options.Increment_Last;
1675 Linker_Options.Table (Linker_Options.Last) := Switches (J);
1681 -- Perform consistency checks
1683 -- Transform the .ali file name into the binder output file name
1685 Make_Binder_File_Names : declare
1686 Fname : constant String := Base_Name (Ali_File_Name.all);
1687 Fname_Len : Integer := Fname'Length;
1689 function Get_Maximum_File_Name_Length return Integer;
1690 pragma Import (C, Get_Maximum_File_Name_Length,
1691 "__gnat_get_maximum_file_name_length");
1693 Maximum_File_Name_Length : constant Integer :=
1694 Get_Maximum_File_Name_Length;
1696 Bind_File_Prefix : Types.String_Ptr;
1697 -- Contains prefix used for bind files
1702 if not Ada_Bind_File then
1703 Bind_File_Prefix := new String'("b_");
1704 elsif OpenVMS_On_Target then
1705 Bind_File_Prefix := new String'("b__");
1707 Bind_File_Prefix := new String'("b~");
1710 -- If the length of the binder file becomes too long due to
1711 -- the addition of the "b?" prefix, then truncate it.
1713 if Maximum_File_Name_Length > 0 then
1715 Maximum_File_Name_Length - Bind_File_Prefix.all'Length
1717 Fname_Len := Fname_Len - 1;
1722 Fnam : constant String :=
1723 Bind_File_Prefix.all &
1724 Fname (Fname'First .. Fname'First + Fname_Len - 1);
1727 if Ada_Bind_File then
1728 Binder_Spec_Src_File := new String'(Fnam & ".ads");
1729 Binder_Body_Src_File := new String'(Fnam & ".adb");
1730 Binder_Ali_File := new String'(Fnam & ".ali");
1732 Binder_Body_Src_File := new String'(Fnam & ".c");
1735 Binder_Obj_File := new String'(Fnam & Get_Target_Object_Suffix.all);
1738 if Fname_Len /= Fname'Length then
1739 Binder_Options.Increment_Last;
1740 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1741 Binder_Options.Increment_Last;
1742 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1744 end Make_Binder_File_Names;
1746 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1748 -- Compile the binder file. This is fast, so we always do it, unless
1749 -- specifically told not to by the -n switch
1751 if Compile_Bind_File then
1754 Args : Argument_List
1755 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1758 for J in 1 .. Binder_Options_From_ALI.Last loop
1759 Args (J) := Binder_Options_From_ALI.Table (J);
1762 for J in 1 .. Binder_Options.Last loop
1763 Args (Binder_Options_From_ALI.Last + J) :=
1764 Binder_Options.Table (J);
1767 Args (Args'Last) := Binder_Body_Src_File;
1769 if Verbose_Mode then
1770 Write_Str (Base_Name (Gcc_Path.all));
1772 for J in Args'Range loop
1774 Write_Str (Args (J).all);
1780 System.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1783 Exit_Program (E_Fatal);
1788 -- Now, actually link the program
1790 -- Skip this step for now on JVM since the Java interpreter will do
1791 -- the actual link at run time. We might consider packing all class files
1792 -- in a .zip file during this step.
1794 if VM_Target /= JVM_Target then
1796 Num_Args : Natural :=
1797 (Linker_Options.Last - Linker_Options.First + 1) +
1798 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1799 (Linker_Objects.Last - Linker_Objects.First + 1);
1800 Stack_Op : Boolean := False;
1801 IDENT_Op : Boolean := False;
1804 if VM_Target = CLI_Target then
1806 -- Remove extraneous flags not relevant for CIL. Also remove empty
1807 -- arguments, since ilasm chokes on them.
1809 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1810 if Linker_Options.Table (J)'Length = 0
1811 or else Linker_Options.Table (J) (1 .. 2) = "-L"
1812 or else Linker_Options.Table (J) (1 .. 2) = "-l"
1813 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1814 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1815 or else Linker_Options.Table (J) (1 .. 2) = "-g"
1817 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1818 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1819 Linker_Options.Decrement_Last;
1820 Num_Args := Num_Args - 1;
1825 -- Remove duplicate stack size setting from the Linker_Options
1826 -- table. The stack setting option "-Xlinker --stack=R,C" can be
1827 -- found in one line when set by a pragma Linker_Options or in two
1828 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
1829 -- line. We also check for the "-Wl,--stack=R" style option.
1831 -- We must remove the second stack setting option instance
1832 -- because the one on the command line will always be the first
1833 -- one. And any subsequent stack setting option will overwrite the
1834 -- previous one. This is done especially for GNAT/NT where we set
1835 -- the stack size for tasking programs by a pragma in the NT
1836 -- specific tasking package System.Task_Primitives.Oparations.
1838 -- Note: This is not a FOR loop that runs from Linker_Options.First
1839 -- to Linker_Options.Last, since operations within the loop can
1840 -- modify the length of the table.
1842 Clean_Link_Option_Set : declare
1843 J : Natural := Linker_Options.First;
1844 Shared_Libgcc_Seen : Boolean := False;
1847 while J <= Linker_Options.Last loop
1849 if Linker_Options.Table (J).all = "-Xlinker"
1850 and then J < Linker_Options.Last
1851 and then Linker_Options.Table (J + 1)'Length > 8
1852 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
1855 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
1856 Linker_Options.Table (J + 2 .. Linker_Options.Last);
1857 Linker_Options.Decrement_Last;
1858 Linker_Options.Decrement_Last;
1859 Num_Args := Num_Args - 2;
1866 -- Remove duplicate -shared-libgcc switch
1868 if Linker_Options.Table (J).all = Shared_Libgcc_String then
1869 if Shared_Libgcc_Seen then
1870 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1871 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1872 Linker_Options.Decrement_Last;
1873 Num_Args := Num_Args - 1;
1876 Shared_Libgcc_Seen := True;
1880 -- Here we just check for a canonical form that matches the
1881 -- pragma Linker_Options set in the NT runtime.
1883 if (Linker_Options.Table (J)'Length > 17
1884 and then Linker_Options.Table (J) (1 .. 17)
1885 = "-Xlinker --stack=")
1887 (Linker_Options.Table (J)'Length > 12
1888 and then Linker_Options.Table (J) (1 .. 12)
1892 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1893 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1894 Linker_Options.Decrement_Last;
1895 Num_Args := Num_Args - 1;
1902 -- Remove duplicate IDENTIFICATION directives (VMS)
1904 if Linker_Options.Table (J)'Length > 27
1905 and then Linker_Options.Table (J) (1 .. 28)
1906 = "--for-linker=IDENTIFICATION="
1909 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1910 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1911 Linker_Options.Decrement_Last;
1912 Num_Args := Num_Args - 1;
1921 if Linker_Path = Gcc_Path and then VM_Target = No_VM then
1923 -- If gcc is not called with -shared-libgcc, call it with
1924 -- -static-libgcc, as there are some platforms where one of
1925 -- these two switches is compulsory to link.
1927 if not Shared_Libgcc_Seen then
1928 Linker_Options.Increment_Last;
1929 Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
1930 Num_Args := Num_Args + 1;
1934 end Clean_Link_Option_Set;
1936 -- Prepare arguments for call to linker
1938 Call_Linker : declare
1940 Args : Argument_List (1 .. Num_Args + 1);
1941 Index : Integer := Args'First;
1944 Args (Index) := Binder_Obj_File;
1946 -- Add the object files and any -largs libraries
1948 for J in Linker_Objects.First .. Linker_Objects.Last loop
1950 Args (Index) := Linker_Objects.Table (J);
1953 -- Add the linker options from the binder file
1955 for J in Linker_Options.First .. Linker_Options.Last loop
1957 Args (Index) := Linker_Options.Table (J);
1960 -- Finally add the libraries from the --GCC= switch
1962 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
1964 Args (Index) := Gcc_Linker_Options.Table (J);
1967 if Verbose_Mode then
1968 Write_Str (Linker_Path.all);
1970 for J in Args'Range loop
1972 Write_Str (Args (J).all);
1977 -- If we are on very verbose mode (-v -v) and a response file
1978 -- is used we display its content.
1980 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
1982 Write_Str ("Response file (" &
1983 Tname (Tname'First .. Tname'Last - 1) &
1988 Response_File_Objects.First ..
1989 Response_File_Objects.Last
1991 Write_Str (Response_File_Objects.Table (J).all);
1999 System.OS_Lib.Spawn (Linker_Path.all, Args, Success);
2001 -- Delete the temporary file used in conjuction with linking if
2002 -- one was created. See Process_Bind_File for details.
2004 if Tname_FD /= Invalid_FD then
2009 Error_Msg ("error when calling " & Linker_Path.all);
2010 Exit_Program (E_Fatal);
2016 -- Only keep the binder output file and it's associated object
2017 -- file if compiling with the -g option. These files are only
2018 -- useful if debugging.
2020 if not Debug_Flag_Present then
2021 if Binder_Ali_File /= null then
2022 Delete (Binder_Ali_File.all & ASCII.NUL);
2025 if Binder_Spec_Src_File /= null then
2026 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
2029 Delete (Binder_Body_Src_File.all & ASCII.NUL);
2031 if VM_Target = No_VM then
2032 Delete (Binder_Obj_File.all & ASCII.NUL);
2036 Exit_Program (E_Success);
2040 Write_Line (Exception_Information (X));
2041 Exit_With_Error ("INTERNAL ERROR. Please report");