1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2011, 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 full name.
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", "gnatlink");
142 Read_Mode : constant String := "r" & ASCII.NUL;
144 Begin_Info : constant String := "-- BEGIN Object file/option list";
145 End_Info : constant String := "-- END Object file/option list ";
147 Gcc_Path : String_Access;
148 Linker_Path : String_Access;
149 Output_File_Name : String_Access;
150 Ali_File_Name : String_Access;
151 Binder_Spec_Src_File : String_Access;
152 Binder_Body_Src_File : String_Access;
153 Binder_Ali_File : String_Access;
154 Binder_Obj_File : String_Access;
156 Tname : Temp_File_Name;
157 Tname_FD : File_Descriptor := Invalid_FD;
158 -- Temporary file used by linker to pass list of object files on
159 -- certain systems with limitations on size of arguments.
161 Debug_Flag_Present : Boolean := False;
162 Verbose_Mode : Boolean := False;
163 Very_Verbose_Mode : Boolean := False;
165 Standard_Gcc : Boolean := True;
167 Compile_Bind_File : Boolean := True;
168 -- Set to False if bind file is not to be compiled
170 Create_Map_File : Boolean := False;
171 -- Set to True by switch -M. The map file name is derived from
172 -- the ALI file name (mainprog.ali => mainprog.map).
174 Object_List_File_Supported : Boolean;
175 for Object_List_File_Supported'Size use Character'Size;
177 (C, Object_List_File_Supported, "__gnat_objlist_file_supported");
178 -- Predicate indicating whether the linker has an option whereby the
179 -- names of object files can be passed to the linker in a file.
181 Object_List_File_Required : Boolean := False;
182 -- Set to True to force generation of a response file
184 Shared_Libgcc_Default : Character;
185 for Shared_Libgcc_Default'Size use Character'Size;
187 (C, Shared_Libgcc_Default, "__gnat_shared_libgcc_default");
188 -- Indicates wether libgcc should be statically linked (use 'T') or
189 -- dynamically linked (use 'H') by default.
191 function Base_Name (File_Name : String) return String;
192 -- Return just the file name part without the extension (if present)
194 procedure Check_Existing_Executable (File_Name : String);
195 -- Delete any existing executable to avoid accidentally updating the target
196 -- of a symbolic link, but produce a Fatail_Error if File_Name matches any
197 -- of the source file names. This avoids overwriting of extensionless
198 -- source files by accident on systems where executables do not have
201 procedure Delete (Name : String);
202 -- Wrapper to unlink as status is ignored by this application
204 procedure Error_Msg (Message : String);
205 -- Output the error or warning Message
207 procedure Exit_With_Error (Error : String);
208 -- Output Error and exit program with a fatal condition
210 procedure Process_Args;
211 -- Go through all the arguments and build option tables
213 procedure Process_Binder_File (Name : String);
214 -- Reads the binder file and extracts linker arguments
219 procedure Write_Header;
220 -- Show user the program name, version and copyright
222 procedure Write_Usage;
223 -- Show user the program options
229 function Base_Name (File_Name : String) return String is
234 Findex1 := File_Name'First;
236 -- The file might be specified by a full path name. However,
237 -- we want the path to be stripped away.
239 for J in reverse File_Name'Range loop
240 if Is_Directory_Separator (File_Name (J)) then
246 Findex2 := File_Name'Last;
247 while Findex2 > Findex1
248 and then File_Name (Findex2) /= '.'
250 Findex2 := Findex2 - 1;
253 if Findex2 = Findex1 then
254 Findex2 := File_Name'Last + 1;
257 return File_Name (Findex1 .. Findex2 - 1);
260 -------------------------------
261 -- Check_Existing_Executable --
262 -------------------------------
264 procedure Check_Existing_Executable (File_Name : String) is
265 Ename : String := File_Name;
266 Efile : File_Name_Type;
267 Sfile : File_Name_Type;
270 Canonical_Case_File_Name (Ename);
272 Add_Str_To_Name_Buffer (Ename);
275 for J in Units.Table'First .. Units.Last loop
276 Sfile := Units.Table (J).Sfile;
277 if Sfile = Efile then
278 Exit_With_Error ("executable name """ & File_Name & """ matches "
279 & "source file name """ & Get_Name_String (Sfile) & """");
284 end Check_Existing_Executable;
290 procedure Delete (Name : String) is
292 pragma Unreferenced (Status);
294 Status := unlink (Name'Address);
295 -- Is it really right to ignore an error here ???
302 procedure Error_Msg (Message : String) is
304 Write_Str (Base_Name (Command_Name));
310 ---------------------
311 -- Exit_With_Error --
312 ---------------------
314 procedure Exit_With_Error (Error : String) is
317 Exit_Program (E_Fatal);
324 procedure Process_Args is
326 Skip_Next : Boolean := False;
327 -- Set to true if the next argument is to be added into the list of
328 -- linker's argument without parsing it.
330 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
332 -- Start of processing for Process_Args
335 -- First, check for --version and --help
337 Check_Version_And_Help ("GNATLINK", "1995");
339 -- Loop through arguments of gnatlink command
343 exit when Next_Arg > Argument_Count;
345 Process_One_Arg : declare
346 Arg : constant String := Argument (Next_Arg);
349 -- Case of argument which is a switch
351 -- We definitely need section by section comments here ???
355 -- This argument must not be parsed, just add it to the
356 -- list of linker's options.
360 Linker_Options.Increment_Last;
361 Linker_Options.Table (Linker_Options.Last) :=
364 elsif Arg'Length /= 0 and then Arg (1) = '-' then
365 if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then
367 ("invalid switch: """ & Arg & """ (gnat not needed here)");
370 if Arg = "-Xlinker" then
372 -- Next argument should be sent directly to the linker.
373 -- We do not want to parse it here.
377 Linker_Options.Increment_Last;
378 Linker_Options.Table (Linker_Options.Last) :=
382 and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
384 Debug_Flag_Present := True;
386 Linker_Options.Increment_Last;
387 Linker_Options.Table (Linker_Options.Last) :=
390 Binder_Options.Increment_Last;
391 Binder_Options.Table (Binder_Options.Last) :=
392 Linker_Options.Table (Linker_Options.Last);
394 elsif Arg'Length >= 3 and then Arg (2) = 'M' then
396 Switches : String_List_Access;
399 Convert (Map_File, Arg (3 .. Arg'Last), Switches);
401 if Switches /= null then
402 for J in Switches'Range loop
403 Linker_Options.Increment_Last;
404 Linker_Options.Table (Linker_Options.Last) :=
410 elsif Arg'Length = 2 then
413 Linker_Options.Increment_Last;
414 Linker_Options.Table (Linker_Options.Last) :=
417 Binder_Options.Increment_Last;
418 Binder_Options.Table (Binder_Options.Last) :=
419 Linker_Options.Table (Linker_Options.Last);
421 Next_Arg := Next_Arg + 1;
423 if Next_Arg > Argument_Count then
424 Exit_With_Error ("Missing argument for -b");
427 Get_Machine_Name : declare
428 Name_Arg : constant String_Access :=
429 new String'(Argument (Next_Arg));
432 Linker_Options.Increment_Last;
433 Linker_Options.Table (Linker_Options.Last) :=
436 Binder_Options.Increment_Last;
437 Binder_Options.Table (Binder_Options.Last) :=
440 end Get_Machine_Name;
443 if Object_List_File_Supported then
444 Object_List_File_Required := True;
447 ("Object list file not supported on this target");
451 Create_Map_File := True;
454 Compile_Bind_File := False;
457 Next_Arg := Next_Arg + 1;
459 if Next_Arg > Argument_Count then
460 Exit_With_Error ("Missing argument for -o");
464 new String'(Executable_Name
465 (Argument (Next_Arg),
466 Only_If_No_Suffix => True));
469 Opt.CodePeer_Mode := True;
472 Opt.Run_Path_Option := False;
476 -- Support "double" verbose mode. Second -v
477 -- gets sent to the linker and binder phases.
480 Very_Verbose_Mode := True;
482 Linker_Options.Increment_Last;
483 Linker_Options.Table (Linker_Options.Last) :=
486 Binder_Options.Increment_Last;
487 Binder_Options.Table (Binder_Options.Last) :=
488 Linker_Options.Table (Linker_Options.Last);
491 Verbose_Mode := True;
496 Linker_Options.Increment_Last;
497 Linker_Options.Table (Linker_Options.Last) :=
502 elsif Arg (2) = 'B' then
503 Linker_Options.Increment_Last;
504 Linker_Options.Table (Linker_Options.Last) :=
507 Binder_Options.Increment_Last;
508 Binder_Options.Table (Binder_Options.Last) :=
509 Linker_Options.Table (Linker_Options.Last);
511 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
512 if Arg'Length = 7 then
513 Exit_With_Error ("Missing argument for --LINK=");
517 System.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
519 if Linker_Path = null then
521 ("Could not locate linker: " & Arg (8 .. Arg'Last));
524 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
526 Program_Args : constant Argument_List_Access :=
527 Argument_String_To_List
528 (Arg (7 .. Arg'Last));
531 if Program_Args.all (1).all /= Gcc.all then
532 Gcc := new String'(Program_Args.all (1).all);
533 Standard_Gcc := False;
536 -- Set appropriate flags for switches passed
538 for J in 2 .. Program_Args.all'Last loop
540 Arg : constant String := Program_Args.all (J).all;
541 AF : constant Integer := Arg'First;
544 if Arg'Length /= 0 and then Arg (AF) = '-' then
545 if Arg (AF + 1) = 'g'
546 and then (Arg'Length = 2
547 or else Arg (AF + 2) in '0' .. '3'
548 or else Arg (AF + 2 .. Arg'Last) = "coff")
550 Debug_Flag_Present := True;
554 -- Add directory to source search dirs so that
555 -- Get_Target_Parameters can find system.ads
557 if Arg (AF .. AF + 1) = "-I"
558 and then Arg'Length > 2
560 Add_Src_Search_Dir (Arg (AF + 2 .. Arg'Last));
563 -- Pass to gcc for compiling binder generated file
564 -- No use passing libraries, it will just generate
567 if not (Arg (AF .. AF + 1) = "-l"
568 or else Arg (AF .. AF + 1) = "-L")
570 Binder_Options.Increment_Last;
571 Binder_Options.Table (Binder_Options.Last) :=
575 -- Pass to gcc for linking program
577 Gcc_Linker_Options.Increment_Last;
578 Gcc_Linker_Options.Table
579 (Gcc_Linker_Options.Last) := new String'(Arg);
584 -- Send all multi-character switches not recognized as
585 -- a special case by gnatlink to the linker/loader stage.
588 Linker_Options.Increment_Last;
589 Linker_Options.Table (Linker_Options.Last) :=
593 -- Here if argument is a file name rather than a switch
596 -- If explicit ali file, capture it
599 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
601 if Ali_File_Name = null then
602 Ali_File_Name := new String'(Arg);
604 Exit_With_Error ("cannot handle more than one ALI file");
607 -- If target object file, record object file
609 elsif Arg'Length > Get_Target_Object_Suffix.all'Length
612 Get_Target_Object_Suffix.all'Length + 1 .. Arg'Last)
613 = Get_Target_Object_Suffix.all
615 Linker_Objects.Increment_Last;
616 Linker_Objects.Table (Linker_Objects.Last) :=
619 -- If host object file, record object file
620 -- e.g. accept foo.o as well as foo.obj on VMS target
622 elsif Arg'Length > Get_Object_Suffix.all'Length
624 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
625 = Get_Object_Suffix.all
627 Linker_Objects.Increment_Last;
628 Linker_Objects.Table (Linker_Objects.Last) :=
631 -- If corresponding ali file exists, capture it
633 elsif Ali_File_Name = null
634 and then Is_Regular_File (Arg & ".ali")
636 Ali_File_Name := new String'(Arg & ".ali");
638 -- Otherwise assume this is a linker options entry, but
639 -- see below for interesting adjustment to this assumption.
642 Linker_Options.Increment_Last;
643 Linker_Options.Table (Linker_Options.Last) :=
649 Next_Arg := Next_Arg + 1;
652 -- Compile the bind file with warnings suppressed, because
653 -- otherwise the with of the main program may cause junk warnings.
655 Binder_Options.Increment_Last;
656 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
658 -- If we did not get an ali file at all, and we had at least one
659 -- linker option, then assume that was the intended ali file after
660 -- all, so that we get a nicer message later on.
662 if Ali_File_Name = null
663 and then Linker_Options.Last >= Linker_Options.First
666 new String'(Linker_Options.Table (Linker_Options.First).all &
671 -------------------------
672 -- Process_Binder_File --
673 -------------------------
675 procedure Process_Binder_File (Name : String) is
677 -- Binder file's descriptor
679 Link_Bytes : Integer := 0;
680 -- Projected number of bytes for the linker command line
683 pragma Import (C, Link_Max, "__gnat_link_max");
684 -- Maximum number of bytes on the command line supported by the OS
685 -- linker. Passed this limit the response file mechanism must be used
688 Next_Line : String (1 .. 1000);
689 -- Current line value
693 -- Current line slice (the slice does not contain line terminator)
696 -- Current line last character for shared libraries (without version)
698 Objs_Begin : Integer := 0;
699 -- First object file index in Linker_Objects table
701 Objs_End : Integer := 0;
702 -- Last object file index in Linker_Objects table
705 pragma Warnings (Off, Status);
706 -- Used for various Interfaces.C_Streams calls
708 Closing_Status : Boolean;
709 pragma Warnings (Off, Closing_Status);
712 GNAT_Static : Boolean := False;
713 -- Save state of -static option
715 GNAT_Shared : Boolean := False;
716 -- Save state of -shared option
718 Xlinker_Was_Previous : Boolean := False;
719 -- Indicate that "-Xlinker" was the option preceding the current
720 -- option. If True, then the current option is never suppressed.
724 -- These data items are used to store current binder file context.
725 -- The context is composed of the file descriptor position and the
726 -- current line together with the slice indexes (first and last
727 -- position) for this line. The rollback data are used by the
728 -- Store_File_Context and Rollback_File_Context routines below.
729 -- The file context mechanism interact only with the Get_Next_Line
730 -- call. For example:
732 -- Store_File_Context;
734 -- Rollback_File_Context;
737 -- Both Get_Next_Line calls above will read the exact same data from
738 -- the file. In other words, Next_Line, Nfirst and Nlast variables
739 -- will be set with the exact same values.
741 RB_File_Pos : long; -- File position
742 RB_Next_Line : String (1 .. 1000); -- Current line content
743 RB_Nlast : Integer; -- Slice last index
744 RB_Nfirst : Integer; -- Slice first index
746 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
747 pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
748 -- Pointer to string representing the native linker option which
749 -- specifies the path where the dynamic loader should find shared
750 -- libraries. Equal to null string if this system doesn't support it.
752 Libgcc_Subdir_Ptr : Interfaces.C.Strings.chars_ptr;
753 pragma Import (C, Libgcc_Subdir_Ptr, "__gnat_default_libgcc_subdir");
754 -- Pointer to string indicating the installation subdirectory where
755 -- a default shared libgcc might be found.
757 Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
759 (C, Object_Library_Ext_Ptr, "__gnat_object_library_extension");
760 -- Pointer to string specifying the default extension for
761 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
763 Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
764 pragma Import (C, Object_File_Option_Ptr, "__gnat_object_file_option");
765 -- Pointer to a string representing the linker option which specifies
766 -- the response file.
768 Using_GNU_Linker : Boolean;
769 for Using_GNU_Linker'Size use Character'Size;
770 pragma Import (C, Using_GNU_Linker, "__gnat_using_gnu_linker");
771 -- Predicate indicating whether this target uses the GNU linker. In
772 -- this case we must output a GNU linker compatible response file.
774 Separate_Run_Path_Options : Boolean;
775 for Separate_Run_Path_Options'Size use Character'Size;
777 (C, Separate_Run_Path_Options, "__gnat_separate_run_path_options");
778 -- Whether separate rpath options should be emitted for each directory
780 Opening : aliased constant String := """";
781 Closing : aliased constant String := '"' & ASCII.LF;
782 -- Needed to quote object paths in object list files when GNU linker
785 procedure Get_Next_Line;
786 -- Read the next line from the binder file without the line
789 function Index (S, Pattern : String) return Natural;
790 -- Return the last occurrence of Pattern in S, or 0 if none
792 function Is_Option_Present (Opt : String) return Boolean;
793 -- Return true if the option Opt is already present in
794 -- Linker_Options table.
796 procedure Store_File_Context;
797 -- Store current file context, Fd position and current line data.
798 -- The file context is stored into the rollback data above (RB_*).
799 -- Store_File_Context can be called at any time, only the last call
800 -- will be used (i.e. this routine overwrites the file context).
802 procedure Rollback_File_Context;
803 -- Restore file context from rollback data. This routine must be called
804 -- after Store_File_Context. The binder file context will be restored
805 -- with the data stored by the last Store_File_Context call.
811 procedure Get_Next_Line is
815 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
817 if Fchars = System.Null_Address then
818 Exit_With_Error ("Error reading binder output");
821 Nfirst := Next_Line'First;
823 while Nlast <= Next_Line'Last
824 and then Next_Line (Nlast) /= ASCII.LF
825 and then Next_Line (Nlast) /= ASCII.CR
837 function Index (S, Pattern : String) return Natural is
838 Len : constant Natural := Pattern'Length;
841 for J in reverse S'First .. S'Last - Len + 1 loop
842 if Pattern = S (J .. J + Len - 1) then
850 -----------------------
851 -- Is_Option_Present --
852 -----------------------
854 function Is_Option_Present (Opt : String) return Boolean is
856 for I in 1 .. Linker_Options.Last loop
858 if Linker_Options.Table (I).all = Opt then
865 end Is_Option_Present;
867 ---------------------------
868 -- Rollback_File_Context --
869 ---------------------------
871 procedure Rollback_File_Context is
873 Next_Line := RB_Next_Line;
876 Status := fseek (Fd, RB_File_Pos, Interfaces.C_Streams.SEEK_SET);
879 Exit_With_Error ("Error setting file position");
881 end Rollback_File_Context;
883 ------------------------
884 -- Store_File_Context --
885 ------------------------
887 procedure Store_File_Context is
888 use type System.CRTL.long;
890 RB_Next_Line := Next_Line;
893 RB_File_Pos := ftell (Fd);
895 if RB_File_Pos = -1 then
896 Exit_With_Error ("Error getting file position");
898 end Store_File_Context;
900 -- Start of processing for Process_Binder_File
903 Fd := fopen (Name'Address, Read_Mode'Address);
905 if Fd = NULL_Stream then
906 Exit_With_Error ("Failed to open binder output");
909 -- Skip up to the Begin Info line
913 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
919 -- Go to end when end line is reached (this will happen in
920 -- High_Integrity_Mode where no -L switches are generated)
922 exit when Next_Line (Nfirst .. Nlast) = End_Info;
924 Next_Line (Nfirst .. Nlast - 8) := Next_Line (Nfirst + 8 .. Nlast);
927 -- Go to next section when switches are reached
929 exit when Next_Line (1) = '-';
931 -- Otherwise we have another object file to collect
933 Linker_Objects.Increment_Last;
935 -- Mark the positions of first and last object files in case
936 -- they need to be placed with a named file on systems having
937 -- linker line limitations.
939 if Objs_Begin = 0 then
940 Objs_Begin := Linker_Objects.Last;
943 Linker_Objects.Table (Linker_Objects.Last) :=
944 new String'(Next_Line (Nfirst .. Nlast));
946 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
947 -- Nlast - Nfirst + 1, for the size, plus one for the space between
951 Objs_End := Linker_Objects.Last;
953 -- Continue to compute the Link_Bytes, the linker options are part of
954 -- command line length.
958 while Next_Line (Nfirst .. Nlast) /= End_Info loop
959 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
963 Rollback_File_Context;
965 -- On systems that have limitations on handling very long linker lines
966 -- we make use of the system linker option which takes a list of object
967 -- file names from a file instead of the command line itself. What we do
968 -- is to replace the list of object files by the special linker option
969 -- which then reads the object file list from a file instead. The option
970 -- to read from a file instead of the command line is only triggered if
971 -- a conservative threshold is passed.
973 if Object_List_File_Required
974 or else (Object_List_File_Supported
975 and then Link_Bytes > Link_Max)
977 -- Create a temporary file containing the Ada user object files
978 -- needed by the link. This list is taken from the bind file
979 -- and is output one object per line for maximal compatibility with
980 -- linkers supporting this option.
982 Create_Temp_File (Tname_FD, Tname);
984 -- ??? File descriptor should be checked to not be Invalid_FD.
985 -- ??? Status of Write and Close operations should be checked, and
986 -- failure should occur if a status is wrong.
988 -- If target is using the GNU linker we must add a special header
989 -- and footer in the response file.
991 -- The syntax is : INPUT (object1.o object2.o ... )
993 -- Because the GNU linker does not like name with characters such
994 -- as '!', we must put the object paths between double quotes.
996 if Using_GNU_Linker then
998 GNU_Header : aliased constant String := "INPUT (";
1001 Status := Write (Tname_FD, GNU_Header'Address,
1006 for J in Objs_Begin .. Objs_End loop
1008 -- Opening quote for GNU linker
1010 if Using_GNU_Linker then
1011 Status := Write (Tname_FD, Opening'Address, 1);
1014 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
1015 Linker_Objects.Table (J).all'Length);
1017 -- Closing quote for GNU linker
1019 if Using_GNU_Linker then
1020 Status := Write (Tname_FD, Closing'Address, 2);
1023 Status := Write (Tname_FD, ASCII.LF'Address, 1);
1026 Response_File_Objects.Increment_Last;
1027 Response_File_Objects.Table (Response_File_Objects.Last) :=
1028 Linker_Objects.Table (J);
1031 -- Handle GNU linker response file footer
1033 if Using_GNU_Linker then
1035 GNU_Footer : aliased constant String := ")";
1038 Status := Write (Tname_FD, GNU_Footer'Address,
1043 Close (Tname_FD, Closing_Status);
1045 -- Add the special objects list file option together with the name
1046 -- of the temporary file (removing the null character) to the objects
1049 Linker_Objects.Table (Objs_Begin) :=
1050 new String'(Value (Object_File_Option_Ptr) &
1051 Tname (Tname'First .. Tname'Last - 1));
1053 -- The slots containing these object file names are then removed
1054 -- from the objects table so they do not appear in the link. They
1055 -- are removed by moving up the linker options and non-Ada object
1056 -- files appearing after the Ada object list in the table.
1062 N := Objs_End - Objs_Begin + 1;
1064 for J in Objs_End + 1 .. Linker_Objects.Last loop
1065 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
1068 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
1072 -- Process switches and options
1074 if Next_Line (Nfirst .. Nlast) /= End_Info then
1075 Xlinker_Was_Previous := False;
1078 if Xlinker_Was_Previous
1079 or else Next_Line (Nfirst .. Nlast) = "-Xlinker"
1081 Linker_Options.Increment_Last;
1082 Linker_Options.Table (Linker_Options.Last) :=
1083 new String'(Next_Line (Nfirst .. Nlast));
1085 elsif Next_Line (Nfirst .. Nlast) = "-static" then
1086 GNAT_Static := True;
1088 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1089 GNAT_Shared := True;
1091 -- Add binder options only if not already set on the command
1092 -- line. This rule is a way to control the linker options order.
1094 -- The following test needs comments, why is it VMS specific.
1095 -- The above comment looks out of date ???
1097 elsif not (OpenVMS_On_Target
1099 Is_Option_Present (Next_Line (Nfirst .. Nlast)))
1101 if Nlast > Nfirst + 2 and then
1102 Next_Line (Nfirst .. Nfirst + 1) = "-L"
1104 -- Construct a library search path for use later
1105 -- to locate static gnatlib libraries.
1107 if Libpath.Last > 1 then
1108 Libpath.Increment_Last;
1109 Libpath.Table (Libpath.Last) := Path_Separator;
1112 for I in Nfirst + 2 .. Nlast loop
1113 Libpath.Increment_Last;
1114 Libpath.Table (Libpath.Last) := Next_Line (I);
1117 Linker_Options.Increment_Last;
1119 Linker_Options.Table (Linker_Options.Last) :=
1120 new String'(Next_Line (Nfirst .. Nlast));
1122 elsif Next_Line (Nfirst .. Nlast) = "-ldecgnat"
1123 or else Next_Line (Nfirst .. Nlast) = "-lgnarl"
1124 or else Next_Line (Nfirst .. Nlast) = "-lgnat"
1126 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) =
1127 Shared_Lib ("gnarl")
1129 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) =
1132 -- If it is a shared library, remove the library version.
1133 -- We will be looking for the static version of the library
1134 -- as it is in the same directory as the shared version.
1136 if Next_Line (Nlast - Library_Version'Length + 1 .. Nlast)
1139 -- Set Last to point to last character before the
1142 Last := Nlast - Library_Version'Length - 1;
1147 -- Given a Gnat standard library, search the library path to
1148 -- find the library location.
1150 -- Shouldn't we abstract a proc here, we are getting awfully
1151 -- heavily nested ???
1154 File_Path : String_Access;
1156 Object_Lib_Extension : constant String :=
1157 Value (Object_Library_Ext_Ptr);
1159 File_Name : constant String := "lib" &
1160 Next_Line (Nfirst + 2 .. Last) &
1161 Object_Lib_Extension;
1163 Run_Path_Opt : constant String :=
1164 Value (Run_Path_Option_Ptr);
1166 GCC_Index : Natural;
1167 Run_Path_Opt_Index : Natural := 0;
1171 Locate_Regular_File (File_Name,
1172 String (Libpath.Table (1 .. Libpath.Last)));
1174 if File_Path /= null then
1177 -- If static gnatlib found, explicitly
1178 -- specify to overcome possible linker
1179 -- default usage of shared version.
1181 Linker_Options.Increment_Last;
1183 Linker_Options.Table (Linker_Options.Last) :=
1184 new String'(File_Path.all);
1186 elsif GNAT_Shared then
1187 if Opt.Run_Path_Option then
1189 -- If shared gnatlib desired, add the
1190 -- appropriate system specific switch
1191 -- so that it can be located at runtime.
1193 if Run_Path_Opt'Length /= 0 then
1195 -- Output the system specific linker command
1196 -- that allows the image activator to find
1197 -- the shared library at runtime. Also add
1198 -- path to find libgcc_s.so, if relevant.
1201 Path : String (1 .. File_Path'Length + 15);
1202 Path_Last : constant Natural :=
1206 Path (1 .. File_Path'Length) :=
1209 -- To find the location of the shared version
1210 -- of libgcc, we look for "gcc-lib" in the
1211 -- path of the library. However, this
1212 -- subdirectory is no longer present in
1213 -- recent versions of GCC. So, we look for
1214 -- the last subdirectory "lib" in the path.
1217 Index (Path (1 .. Path_Last), "gcc-lib");
1219 if GCC_Index /= 0 then
1221 -- The shared version of libgcc is
1222 -- located in the parent directory.
1224 GCC_Index := GCC_Index - 1;
1229 (Path (1 .. Path_Last),
1232 if GCC_Index = 0 then
1234 Index (Path (1 .. Path_Last),
1235 Directory_Separator &
1237 Directory_Separator);
1240 -- If we have found a "lib" subdir in
1241 -- the path to libgnat, the possible
1242 -- shared libgcc of interest by default
1243 -- is in libgcc_subdir at the same
1246 if GCC_Index /= 0 then
1248 Subdir : constant String :=
1249 Value (Libgcc_Subdir_Ptr);
1253 GCC_Index + Subdir'Length) :=
1256 GCC_Index + Subdir'Length;
1261 -- Look for an eventual run_path_option in
1262 -- the linker switches.
1264 if Separate_Run_Path_Options then
1265 Linker_Options.Increment_Last;
1266 Linker_Options.Table
1267 (Linker_Options.Last) :=
1271 (1 .. File_Path'Length
1272 - File_Name'Length));
1274 if GCC_Index /= 0 then
1275 Linker_Options.Increment_Last;
1276 Linker_Options.Table
1277 (Linker_Options.Last) :=
1280 & Path (1 .. GCC_Index));
1285 1 .. Linker_Options.Last
1287 if Linker_Options.Table (J) /= null
1289 Linker_Options.Table (J)'Length
1290 > Run_Path_Opt'Length
1292 Linker_Options.Table (J)
1293 (1 .. Run_Path_Opt'Length) =
1296 -- We have found an already
1297 -- specified run_path_option: we
1298 -- will add to this switch,
1300 -- run_path_option should be
1303 Run_Path_Opt_Index := J;
1308 -- If there is no run_path_option, we
1311 if Run_Path_Opt_Index = 0 then
1312 Linker_Options.Increment_Last;
1315 if GCC_Index = 0 then
1316 if Run_Path_Opt_Index = 0 then
1317 Linker_Options.Table
1318 (Linker_Options.Last) :=
1322 (1 .. File_Path'Length
1323 - File_Name'Length));
1326 Linker_Options.Table
1327 (Run_Path_Opt_Index) :=
1329 (Linker_Options.Table
1330 (Run_Path_Opt_Index).all
1333 (1 .. File_Path'Length
1334 - File_Name'Length));
1338 if Run_Path_Opt_Index = 0 then
1339 Linker_Options.Table
1340 (Linker_Options.Last) :=
1344 (1 .. File_Path'Length
1347 & Path (1 .. GCC_Index));
1350 Linker_Options.Table
1351 (Run_Path_Opt_Index) :=
1353 (Linker_Options.Table
1354 (Run_Path_Opt_Index).all
1357 (1 .. File_Path'Length
1360 & Path (1 .. GCC_Index));
1368 -- Then we add the appropriate -l switch
1370 Linker_Options.Increment_Last;
1371 Linker_Options.Table (Linker_Options.Last) :=
1372 new String'(Next_Line (Nfirst .. Nlast));
1376 -- If gnatlib library not found, then
1377 -- add it anyway in case some other
1378 -- mechanism may find it.
1380 Linker_Options.Increment_Last;
1381 Linker_Options.Table (Linker_Options.Last) :=
1382 new String'(Next_Line (Nfirst .. Nlast));
1386 Linker_Options.Increment_Last;
1387 Linker_Options.Table (Linker_Options.Last) :=
1388 new String'(Next_Line (Nfirst .. Nlast));
1392 Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker";
1395 exit when Next_Line (Nfirst .. Nlast) = End_Info;
1397 Next_Line (Nfirst .. Nlast - 8) := Next_Line (Nfirst + 8 .. Nlast);
1402 -- If -shared was specified, invoke gcc with -shared-libgcc
1405 Linker_Options.Increment_Last;
1406 Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc;
1409 Status := fclose (Fd);
1410 end Process_Binder_File;
1418 Write_Str ("Usage: ");
1419 Write_Str (Base_Name (Command_Name));
1420 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
1423 Write_Line (" mainprog.ali the ALI file of the main program");
1426 Display_Usage_Version_And_Help;
1427 Write_Line (" -f Force object file list to be generated");
1428 Write_Line (" -g Compile binder source file with debug information");
1429 Write_Line (" -n Do not compile the binder source file");
1430 Write_Line (" -P Process files for use by CodePeer");
1431 Write_Line (" -R Do not use a run_path_option");
1432 Write_Line (" -v Verbose mode");
1433 Write_Line (" -v -v Very verbose mode");
1435 Write_Line (" -o nam Use 'nam' as the name of the executable");
1436 Write_Line (" -b target Compile the binder source to run on target");
1437 Write_Line (" -Bdir Load compiler executables from dir");
1439 if Is_Supported (Map_File) then
1440 Write_Line (" -Mmap Create map file map");
1441 Write_Line (" -M Create map file mainprog.map");
1444 Write_Line (" --GCC=comp Use comp as the compiler");
1445 Write_Line (" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1447 Write_Line (" [non-Ada-objects] list of non Ada object files");
1448 Write_Line (" [linker-options] other options for the linker");
1455 procedure Write_Header is
1457 if Verbose_Mode then
1459 Display_Version ("GNATLINK", "1995");
1467 procedure Write_Usage is
1473 -- Start of processing for Gnatlink
1476 -- Add the directory where gnatlink is invoked in front of the path, if
1477 -- gnatlink is invoked with directory information. Only do this if the
1478 -- platform is not VMS, where the notion of path does not really exist.
1480 if not Hostparm.OpenVMS then
1482 Command : constant String := Command_Name;
1485 for Index in reverse Command'Range loop
1486 if Command (Index) = Directory_Separator then
1488 Absolute_Dir : constant String :=
1490 (Command (Command'First .. Index));
1492 PATH : constant String :=
1495 Getenv ("PATH").all;
1498 Setenv ("PATH", PATH);
1509 if Argument_Count = 0
1510 or else (Verbose_Mode and then Argument_Count = 1)
1513 Exit_Program (E_Fatal);
1516 -- Initialize packages to be used
1521 -- We always compile with -c
1523 Binder_Options_From_ALI.Increment_Last;
1524 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1527 if Ali_File_Name = null then
1528 Exit_With_Error ("no ali file given for link");
1531 if not Is_Regular_File (Ali_File_Name.all) then
1532 Exit_With_Error (Ali_File_Name.all & " not found");
1535 -- Read the ALI file of the main subprogram if the binder generated file
1536 -- needs to be compiled and no --GCC= switch has been specified. Fetch the
1537 -- back end switches from this ALI file and use these switches to compile
1538 -- the binder generated file
1540 if Compile_Bind_File and then Standard_Gcc then
1542 Name_Len := Ali_File_Name'Length;
1543 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1547 F : constant File_Name_Type := Name_Find;
1548 T : Text_Buffer_Ptr;
1552 -- Load the ALI file
1554 T := Read_Library_Info (F, True);
1556 -- Read it. Note that we ignore errors, since we only want very
1557 -- limited information from the ali file, and likely a slightly
1558 -- wrong version will be just fine, though in normal operation
1559 -- we don't expect this to happen!
1566 Ignore_Errors => True);
1568 if A /= No_ALI_Id then
1570 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1571 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1573 -- Do not compile with the front end switches. However, --RTS
1574 -- is to be dealt with specially because it needs to be passed
1575 -- if the binder-generated file is in Ada and may also be used
1576 -- to drive the linker.
1579 Arg : String_Ptr renames Args.Table (Index);
1581 if not Is_Front_End_Switch (Arg.all) then
1582 Binder_Options_From_ALI.Increment_Last;
1583 Binder_Options_From_ALI.Table
1584 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1586 elsif Arg'Length > 5
1587 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1589 Binder_Options_From_ALI.Increment_Last;
1590 Binder_Options_From_ALI.Table
1591 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1593 -- Set the RTS_*_Path_Name variables, so that
1594 -- the correct directories will be set when
1595 -- Osint.Add_Default_Search_Dirs will be called later.
1597 Opt.RTS_Src_Path_Name :=
1599 (Arg (Arg'First + 6 .. Arg'Last), Include);
1601 Opt.RTS_Lib_Path_Name :=
1603 (Arg (Arg'First + 6 .. Arg'Last), Objects);
1605 -- GNAT doesn't support the GCC multilib mechanism.
1606 -- This means that, when a multilib switch is used
1607 -- to request a particular compilation mode, the
1608 -- corresponding runtime switch (--RTS) must also be
1609 -- specified. The long-term goal is to fully support the
1610 -- multilib mechanism; however, in the meantime, it is
1611 -- convenient to eliminate the redundancy by keying the
1612 -- compilation mode on a single switch, namely --RTS.
1614 -- Pass -mrtp to the linker if --RTS=rtp was passed
1617 and then Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1619 Linker_Options.Increment_Last;
1620 Linker_Options.Table (Linker_Options.Last) :=
1621 new String'("-mrtp");
1630 -- Get target parameters
1632 Osint.Add_Default_Search_Dirs;
1633 Targparm.Get_Target_Parameters;
1635 if VM_Target /= No_VM then
1637 when JVM_Target => Gcc := new String'("jvm-gnatcompile");
1638 when CLI_Target => Gcc := new String'("dotnet-gnatcompile");
1639 when No_VM => raise Program_Error;
1643 -- Compile the bind file with the following switches:
1645 -- -gnatA stops reading gnat.adc, since we don't know what
1646 -- pragmas would work, and we do not need it anyway.
1648 -- -gnatWb allows brackets coding for wide characters
1650 -- -gnatiw allows wide characters in identifiers. This is needed
1651 -- because bindgen uses brackets encoding for all upper
1652 -- half and wide characters in identifier names.
1654 -- In addition, in CodePeer mode compile with -gnatC
1656 Binder_Options_From_ALI.Increment_Last;
1657 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1658 new String'("-gnatA");
1659 Binder_Options_From_ALI.Increment_Last;
1660 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1661 new String'("-gnatWb");
1662 Binder_Options_From_ALI.Increment_Last;
1663 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1664 new String'("-gnatiw");
1666 if Opt.CodePeer_Mode then
1667 Binder_Options_From_ALI.Increment_Last;
1668 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1669 new String'("-gnatC");
1672 -- Locate all the necessary programs and verify required files are present
1674 Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1676 if Gcc_Path = null then
1677 Exit_With_Error ("Couldn't locate " & Gcc.all);
1680 if Linker_Path = null then
1681 if VM_Target = CLI_Target then
1682 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("dotnet-ld");
1684 if Linker_Path = null then
1685 Exit_With_Error ("Couldn't locate dotnet-ld");
1688 elsif RTX_RTSS_Kernel_Module_On_Target then
1690 -- Use Microsoft linker for RTSS modules
1692 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("link");
1694 if Linker_Path = null then
1695 Exit_With_Error ("Couldn't locate link");
1699 Linker_Path := Gcc_Path;
1705 -- If no output name specified, then use the base name of .ali file name
1707 if Output_File_Name = null then
1709 new String'(Base_Name (Ali_File_Name.all)
1710 & Get_Target_Debuggable_Suffix.all);
1713 if RTX_RTSS_Kernel_Module_On_Target then
1714 Linker_Options.Increment_Last;
1715 Linker_Options.Table (Linker_Options.Last) :=
1716 new String'("/OUT:" & Output_File_Name.all);
1719 Linker_Options.Increment_Last;
1720 Linker_Options.Table (Linker_Options.Last) := new String'("-o");
1722 Linker_Options.Increment_Last;
1723 Linker_Options.Table (Linker_Options.Last) :=
1724 new String'(Output_File_Name.all);
1727 Check_Existing_Executable (Output_File_Name.all);
1729 -- Warn if main program is called "test", as that may be a built-in command
1730 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1731 -- will not appear. However, do not warn in the case of a cross compiler.
1733 -- Assume this is a cross tool if the executable name is not gnatlink
1735 if Base_Name (Command_Name) = "gnatlink"
1736 and then Output_File_Name.all = "test"
1738 Error_Msg ("warning: executable name """ & Output_File_Name.all
1739 & """ may conflict with shell command");
1742 -- If -M switch was specified, add the switches to create the map file
1744 if Create_Map_File then
1746 Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map";
1747 Switches : String_List_Access;
1750 Convert (Map_File, Map_Name, Switches);
1752 if Switches /= null then
1753 for J in Switches'Range loop
1754 Linker_Options.Increment_Last;
1755 Linker_Options.Table (Linker_Options.Last) := Switches (J);
1761 -- Perform consistency checks
1763 -- Transform the .ali file name into the binder output file name
1765 Make_Binder_File_Names : declare
1766 Fname : constant String := Base_Name (Ali_File_Name.all);
1767 Fname_Len : Integer := Fname'Length;
1769 function Get_Maximum_File_Name_Length return Integer;
1770 pragma Import (C, Get_Maximum_File_Name_Length,
1771 "__gnat_get_maximum_file_name_length");
1773 Maximum_File_Name_Length : constant Integer :=
1774 Get_Maximum_File_Name_Length;
1776 Bind_File_Prefix : Types.String_Ptr;
1777 -- Contains prefix used for bind files
1782 if OpenVMS_On_Target then
1783 Bind_File_Prefix := new String'("b__");
1785 Bind_File_Prefix := new String'("b~");
1788 -- If the length of the binder file becomes too long due to
1789 -- the addition of the "b?" prefix, then truncate it.
1791 if Maximum_File_Name_Length > 0 then
1793 Maximum_File_Name_Length - Bind_File_Prefix.all'Length
1795 Fname_Len := Fname_Len - 1;
1800 Fnam : constant String :=
1801 Bind_File_Prefix.all &
1802 Fname (Fname'First .. Fname'First + Fname_Len - 1);
1805 Binder_Spec_Src_File := new String'(Fnam & ".ads");
1806 Binder_Body_Src_File := new String'(Fnam & ".adb");
1807 Binder_Ali_File := new String'(Fnam & ".ali");
1809 Binder_Obj_File := new String'(Fnam & Get_Target_Object_Suffix.all);
1812 if Fname_Len /= Fname'Length then
1813 Binder_Options.Increment_Last;
1814 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1815 Binder_Options.Increment_Last;
1816 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1818 end Make_Binder_File_Names;
1820 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1822 -- Compile the binder file. This is fast, so we always do it, unless
1823 -- specifically told not to by the -n switch
1825 if Compile_Bind_File then
1828 Args : Argument_List
1829 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1832 for J in 1 .. Binder_Options_From_ALI.Last loop
1833 Args (J) := Binder_Options_From_ALI.Table (J);
1836 for J in 1 .. Binder_Options.Last loop
1837 Args (Binder_Options_From_ALI.Last + J) :=
1838 Binder_Options.Table (J);
1841 -- Use the full path of the binder generated source, so that it is
1842 -- guaranteed that the debugger will find this source, even with
1846 new String'(Normalize_Pathname (Binder_Body_Src_File.all));
1848 if Verbose_Mode then
1849 Write_Str (Base_Name (Gcc_Path.all));
1851 for J in Args'Range loop
1853 Write_Str (Args (J).all);
1859 System.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1862 Exit_Program (E_Fatal);
1867 -- In CodePeer mode, there's nothing left to do after the binder file has
1870 if Opt.CodePeer_Mode then
1874 -- Now, actually link the program
1876 -- Skip this step for now on JVM since the Java interpreter will do
1877 -- the actual link at run time. We might consider packing all class files
1878 -- in a .zip file during this step.
1880 if VM_Target /= JVM_Target then
1882 Num_Args : Natural :=
1883 (Linker_Options.Last - Linker_Options.First + 1) +
1884 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1885 (Linker_Objects.Last - Linker_Objects.First + 1);
1886 Stack_Op : Boolean := False;
1887 IDENT_Op : Boolean := False;
1890 if AAMP_On_Target then
1892 -- Remove extraneous flags not relevant for AAMP
1894 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1895 if Linker_Options.Table (J)'Length = 0
1896 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1897 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1898 or else Linker_Options.Table (J) (1 .. 2) = "-O"
1899 or else Linker_Options.Table (J) (1 .. 2) = "-g"
1901 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1902 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1903 Linker_Options.Decrement_Last;
1904 Num_Args := Num_Args - 1;
1908 elsif RTX_RTSS_Kernel_Module_On_Target then
1910 -- Remove flags not relevant for Microsoft linker and adapt some
1913 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1915 -- Remove flags that are not accepted
1917 if Linker_Options.Table (J)'Length = 0
1918 or else Linker_Options.Table (J) (1 .. 2) = "-l"
1919 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1920 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1921 or else Linker_Options.Table (J) (1 .. 2) = "-O"
1922 or else Linker_Options.Table (J) (1 .. 8) = "-Xlinker"
1923 or else Linker_Options.Table (J) (1 .. 9) = "-mthreads"
1925 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1926 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1927 Linker_Options.Decrement_Last;
1928 Num_Args := Num_Args - 1;
1930 -- Replace "-L" by its counterpart "/LIBPATH:" and UNIX "/" by
1932 elsif Linker_Options.Table (J) (1 .. 2) = "-L" then
1934 Libpath_Option : constant String_Access := new String'
1936 Linker_Options.Table (J)
1937 (3 .. Linker_Options.Table (J).all'Last));
1939 for Index in 10 .. Libpath_Option'Last loop
1940 if Libpath_Option (Index) = '/' then
1941 Libpath_Option (Index) := '\';
1945 Linker_Options.Table (J) := Libpath_Option;
1948 -- Replace "-g" by "/DEBUG"
1949 elsif Linker_Options.Table (J) (1 .. 2) = "-g" then
1950 Linker_Options.Table (J) := new String'("/DEBUG");
1952 -- Replace "-o" by "/OUT:"
1953 elsif Linker_Options.Table (J) (1 .. 2) = "-o" then
1954 Linker_Options.Table (J + 1) := new String'
1955 ("/OUT:" & Linker_Options.Table (J + 1).all);
1957 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1958 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1959 Linker_Options.Decrement_Last;
1960 Num_Args := Num_Args - 1;
1962 -- Replace "--stack=" by "/STACK:"
1963 elsif Linker_Options.Table (J) (1 .. 8) = "--stack=" then
1964 Linker_Options.Table (J) := new String'
1966 Linker_Options.Table (J)
1967 (9 .. Linker_Options.Table (J).all'Last));
1969 -- Replace "-v" by its counterpart "/VERBOSE"
1970 elsif Linker_Options.Table (J) (1 .. 2) = "-v" then
1971 Linker_Options.Table (J) := new String'("/VERBOSE");
1975 -- Add some required flags to create RTSS modules
1978 Flags_For_Linker : constant array (1 .. 17) of String_Access :=
1979 (new String'("/NODEFAULTLIB"),
1980 new String'("/INCREMENTAL:NO"),
1981 new String'("/NOLOGO"),
1982 new String'("/DRIVER"),
1983 new String'("/ALIGN:0x20"),
1984 new String'("/SUBSYSTEM:NATIVE"),
1985 new String'("/ENTRY:_RtapiProcessEntryCRT@8"),
1986 new String'("/RELEASE"),
1987 new String'("startupCRT.obj"),
1988 new String'("rtxlibcmt.lib"),
1989 new String'("oldnames.lib"),
1990 new String'("rtapi_rtss.lib"),
1991 new String'("Rtx_Rtss.lib"),
1992 new String'("libkernel32.a"),
1993 new String'("libws2_32.a"),
1994 new String'("libmswsock.a"),
1995 new String'("libadvapi32.a"));
1996 -- These flags need to be passed to Microsoft linker. They
1997 -- come from the RTX documentation.
1999 Gcc_Lib_Path : constant String_Access := new String'
2000 ("/LIBPATH:" & Include_Dir_Default_Prefix & "\..\");
2001 -- Place to look for gcc related libraries, such as libgcc
2004 -- Replace UNIX "/" by Windows "\" in the path
2006 for Index in 10 .. Gcc_Lib_Path.all'Last loop
2007 if Gcc_Lib_Path (Index) = '/' then
2008 Gcc_Lib_Path (Index) := '\';
2012 Linker_Options.Increment_Last;
2013 Linker_Options.Table (Linker_Options.Last) := Gcc_Lib_Path;
2014 Num_Args := Num_Args + 1;
2016 for Index in Flags_For_Linker'Range loop
2017 Linker_Options.Increment_Last;
2018 Linker_Options.Table (Linker_Options.Last) :=
2019 Flags_For_Linker (Index);
2020 Num_Args := Num_Args + 1;
2025 -- Remove duplicate stack size setting from the Linker_Options
2026 -- table. The stack setting option "-Xlinker --stack=R,C" can be
2027 -- found in one line when set by a pragma Linker_Options or in two
2028 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
2029 -- line. We also check for the "-Wl,--stack=R" style option.
2031 -- We must remove the second stack setting option instance
2032 -- because the one on the command line will always be the first
2033 -- one. And any subsequent stack setting option will overwrite the
2034 -- previous one. This is done especially for GNAT/NT where we set
2035 -- the stack size for tasking programs by a pragma in the NT
2036 -- specific tasking package System.Task_Primitives.Operations.
2038 -- Note: This is not a FOR loop that runs from Linker_Options.First
2039 -- to Linker_Options.Last, since operations within the loop can
2040 -- modify the length of the table.
2042 Clean_Link_Option_Set : declare
2043 J : Natural := Linker_Options.First;
2044 Shared_Libgcc_Seen : Boolean := False;
2047 while J <= Linker_Options.Last loop
2049 if Linker_Options.Table (J).all = "-Xlinker"
2050 and then J < Linker_Options.Last
2051 and then Linker_Options.Table (J + 1)'Length > 8
2052 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
2055 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
2056 Linker_Options.Table (J + 2 .. Linker_Options.Last);
2057 Linker_Options.Decrement_Last;
2058 Linker_Options.Decrement_Last;
2059 Num_Args := Num_Args - 2;
2066 -- Remove duplicate -shared-libgcc switch
2068 if Linker_Options.Table (J).all = Shared_Libgcc_String then
2069 if Shared_Libgcc_Seen then
2070 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2071 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2072 Linker_Options.Decrement_Last;
2073 Num_Args := Num_Args - 1;
2076 Shared_Libgcc_Seen := True;
2080 -- Here we just check for a canonical form that matches the
2081 -- pragma Linker_Options set in the NT runtime.
2083 if (Linker_Options.Table (J)'Length > 17
2084 and then Linker_Options.Table (J) (1 .. 17)
2085 = "-Xlinker --stack=")
2087 (Linker_Options.Table (J)'Length > 12
2088 and then Linker_Options.Table (J) (1 .. 12)
2092 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2093 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2094 Linker_Options.Decrement_Last;
2095 Num_Args := Num_Args - 1;
2102 -- Remove duplicate IDENTIFICATION directives (VMS)
2104 if Linker_Options.Table (J)'Length > 29
2105 and then Linker_Options.Table (J) (1 .. 30) =
2106 "--for-linker=--identification="
2109 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2110 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2111 Linker_Options.Decrement_Last;
2112 Num_Args := Num_Args - 1;
2122 if Linker_Path = Gcc_Path and then VM_Target = No_VM then
2124 -- For systems where the default is to link statically with
2125 -- libgcc, if gcc is not called with -shared-libgcc, call it
2126 -- with -static-libgcc, as there are some platforms where one
2127 -- of these two switches is compulsory to link.
2129 if Shared_Libgcc_Default = 'T'
2130 and then not Shared_Libgcc_Seen
2132 Linker_Options.Increment_Last;
2133 Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
2134 Num_Args := Num_Args + 1;
2137 elsif RTX_RTSS_Kernel_Module_On_Target then
2139 -- Force the use of the static libgcc for RTSS modules
2141 Linker_Options.Increment_Last;
2142 Linker_Options.Table (Linker_Options.Last) :=
2143 new String'("libgcc.a");
2144 Num_Args := Num_Args + 1;
2147 end Clean_Link_Option_Set;
2149 -- Prepare arguments for call to linker
2151 Call_Linker : declare
2153 Args : Argument_List (1 .. Num_Args + 1);
2154 Index : Integer := Args'First;
2157 Args (Index) := Binder_Obj_File;
2159 -- Add the object files and any -largs libraries
2161 for J in Linker_Objects.First .. Linker_Objects.Last loop
2163 Args (Index) := Linker_Objects.Table (J);
2166 -- Add the linker options from the binder file
2168 for J in Linker_Options.First .. Linker_Options.Last loop
2170 Args (Index) := Linker_Options.Table (J);
2173 -- Finally add the libraries from the --GCC= switch
2175 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
2177 Args (Index) := Gcc_Linker_Options.Table (J);
2180 if Verbose_Mode then
2181 Write_Str (Linker_Path.all);
2183 for J in Args'Range loop
2185 Write_Str (Args (J).all);
2190 -- If we are on very verbose mode (-v -v) and a response file
2191 -- is used we display its content.
2193 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
2195 Write_Str ("Response file (" &
2196 Tname (Tname'First .. Tname'Last - 1) &
2201 Response_File_Objects.First ..
2202 Response_File_Objects.Last
2204 Write_Str (Response_File_Objects.Table (J).all);
2212 System.OS_Lib.Spawn (Linker_Path.all, Args, Success);
2214 -- Delete the temporary file used in conjunction with linking if
2215 -- one was created. See Process_Bind_File for details.
2217 if Tname_FD /= Invalid_FD then
2222 Error_Msg ("error when calling " & Linker_Path.all);
2223 Exit_Program (E_Fatal);
2229 -- Only keep the binder output file and it's associated object
2230 -- file if compiling with the -g option. These files are only
2231 -- useful if debugging.
2233 if not Debug_Flag_Present then
2234 Delete (Binder_Ali_File.all & ASCII.NUL);
2235 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
2236 Delete (Binder_Body_Src_File.all & ASCII.NUL);
2238 if VM_Target = No_VM then
2239 Delete (Binder_Obj_File.all & ASCII.NUL);
2243 Exit_Program (E_Success);
2247 Write_Line (Exception_Information (X));
2248 Exit_With_Error ("INTERNAL ERROR. Please report");