1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2009, 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 : 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;
150 Output_File_Name : String_Access;
151 Ali_File_Name : String_Access;
152 Binder_Spec_Src_File : String_Access;
153 Binder_Body_Src_File : String_Access;
154 Binder_Ali_File : String_Access;
155 Binder_Obj_File : String_Access;
157 Tname : Temp_File_Name;
158 Tname_FD : File_Descriptor := Invalid_FD;
159 -- Temporary file used by linker to pass list of object files on
160 -- certain systems with limitations on size of arguments.
162 Lname : String_Access := null;
163 -- File used by linker for CLI target, used to concatenate all .il files
164 -- when the command line passed to ilasm is too long
166 Debug_Flag_Present : Boolean := False;
167 Verbose_Mode : Boolean := False;
168 Very_Verbose_Mode : Boolean := False;
170 Ada_Bind_File : Boolean := True;
171 -- Set to True if bind file is generated in Ada
173 Standard_Gcc : Boolean := True;
175 Compile_Bind_File : Boolean := True;
176 -- Set to False if bind file is not to be compiled
178 Create_Map_File : Boolean := False;
179 -- Set to True by switch -M. The map file name is derived from
180 -- the ALI file name (mainprog.ali => mainprog.map).
182 Object_List_File_Supported : Boolean;
183 for Object_List_File_Supported'Size use Character'Size;
185 (C, Object_List_File_Supported, "__gnat_objlist_file_supported");
186 -- Predicate indicating whether the linker has an option whereby the
187 -- names of object files can be passed to the linker in a file.
189 Object_List_File_Required : Boolean := False;
190 -- Set to True to force generation of a response file
192 Shared_Libgcc_Default : Character;
193 for Shared_Libgcc_Default'Size use Character'Size;
195 (C, Shared_Libgcc_Default, "__gnat_shared_libgcc_default");
196 -- Indicates wether libgcc should be statically linked (use 'T') or
197 -- dynamically linked (use 'H') by default.
199 function Base_Name (File_Name : String) return String;
200 -- Return just the file name part without the extension (if present)
202 procedure Delete (Name : String);
203 -- Wrapper to unlink as status is ignored by this application
205 procedure Error_Msg (Message : String);
206 -- Output the error or warning Message
208 procedure Exit_With_Error (Error : String);
209 -- Output Error and exit program with a fatal condition
211 procedure Process_Args;
212 -- Go through all the arguments and build option tables
214 procedure Process_Binder_File (Name : String);
215 -- Reads the binder file and extracts linker arguments
220 procedure Write_Header;
221 -- Show user the program name, version and copyright
223 procedure Write_Usage;
224 -- Show user the program options
230 function Base_Name (File_Name : String) return String is
235 Findex1 := File_Name'First;
237 -- The file might be specified by a full path name. However,
238 -- we want the path to be stripped away.
240 for J in reverse File_Name'Range loop
241 if Is_Directory_Separator (File_Name (J)) then
247 Findex2 := File_Name'Last;
248 while Findex2 > Findex1
249 and then File_Name (Findex2) /= '.'
251 Findex2 := Findex2 - 1;
254 if Findex2 = Findex1 then
255 Findex2 := File_Name'Last + 1;
258 return File_Name (Findex1 .. Findex2 - 1);
265 procedure Delete (Name : String) is
267 pragma Unreferenced (Status);
269 Status := unlink (Name'Address);
270 -- Is it really right to ignore an error here ???
277 procedure Error_Msg (Message : String) is
279 Write_Str (Base_Name (Command_Name));
285 ---------------------
286 -- Exit_With_Error --
287 ---------------------
289 procedure Exit_With_Error (Error : String) is
292 Exit_Program (E_Fatal);
299 procedure Process_Args is
301 Skip_Next : Boolean := False;
302 -- Set to true if the next argument is to be added into the list of
303 -- linker's argument without parsing it.
305 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
307 -- Start of processing for Process_Args
310 -- First, check for --version and --help
312 Check_Version_And_Help ("GNATLINK", "1995");
314 -- Loop through arguments of gnatlink command
318 exit when Next_Arg > Argument_Count;
320 Process_One_Arg : declare
321 Arg : constant String := Argument (Next_Arg);
324 -- Case of argument which is a switch
326 -- We definitely need section by section comments here ???
330 -- This argument must not be parsed, just add it to the
331 -- list of linker's options.
335 Linker_Options.Increment_Last;
336 Linker_Options.Table (Linker_Options.Last) :=
339 elsif Arg'Length /= 0 and then Arg (1) = '-' then
340 if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then
342 ("invalid switch: """ & Arg & """ (gnat not needed here)");
345 if Arg = "-Xlinker" then
347 -- Next argument should be sent directly to the linker.
348 -- We do not want to parse it here.
352 Linker_Options.Increment_Last;
353 Linker_Options.Table (Linker_Options.Last) :=
357 and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
359 Debug_Flag_Present := True;
361 Linker_Options.Increment_Last;
362 Linker_Options.Table (Linker_Options.Last) :=
365 Binder_Options.Increment_Last;
366 Binder_Options.Table (Binder_Options.Last) :=
367 Linker_Options.Table (Linker_Options.Last);
369 elsif Arg'Length >= 3 and then Arg (2) = 'M' then
371 Switches : String_List_Access;
374 Convert (Map_File, Arg (3 .. Arg'Last), Switches);
376 if Switches /= null then
377 for J in Switches'Range loop
378 Linker_Options.Increment_Last;
379 Linker_Options.Table (Linker_Options.Last) :=
385 elsif Arg'Length = 2 then
388 Ada_Bind_File := True;
389 Begin_Info := "-- BEGIN Object file/option list";
390 End_Info := "-- END Object file/option list ";
393 Linker_Options.Increment_Last;
394 Linker_Options.Table (Linker_Options.Last) :=
397 Binder_Options.Increment_Last;
398 Binder_Options.Table (Binder_Options.Last) :=
399 Linker_Options.Table (Linker_Options.Last);
401 Next_Arg := Next_Arg + 1;
403 if Next_Arg > Argument_Count then
404 Exit_With_Error ("Missing argument for -b");
407 Get_Machine_Name : declare
408 Name_Arg : constant String_Access :=
409 new String'(Argument (Next_Arg));
412 Linker_Options.Increment_Last;
413 Linker_Options.Table (Linker_Options.Last) :=
416 Binder_Options.Increment_Last;
417 Binder_Options.Table (Binder_Options.Last) :=
420 end Get_Machine_Name;
423 Ada_Bind_File := False;
424 Begin_Info := "/* BEGIN Object file/option list";
425 End_Info := " END Object file/option list */";
428 if Object_List_File_Supported then
429 Object_List_File_Required := True;
432 ("Object list file not supported on this target");
436 Create_Map_File := True;
439 Compile_Bind_File := False;
442 if VM_Target = CLI_Target then
443 Linker_Options.Increment_Last;
444 Linker_Options.Table (Linker_Options.Last) :=
445 new String'("/QUIET");
448 Linker_Options.Increment_Last;
449 Linker_Options.Table (Linker_Options.Last) :=
453 Next_Arg := Next_Arg + 1;
455 if Next_Arg > Argument_Count then
456 Exit_With_Error ("Missing argument for -o");
459 if VM_Target = CLI_Target then
461 new String'("/OUTPUT=" & Argument (Next_Arg));
464 new String'(Argument (Next_Arg));
467 Linker_Options.Increment_Last;
468 Linker_Options.Table (Linker_Options.Last) :=
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 -- If Ada bind file, then compile it with warnings suppressed, because
653 -- otherwise the with of the main program may cause junk warnings.
655 if Ada_Bind_File then
656 Binder_Options.Increment_Last;
657 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
660 -- If we did not get an ali file at all, and we had at least one
661 -- linker option, then assume that was the intended ali file after
662 -- all, so that we get a nicer message later on.
664 if Ali_File_Name = null
665 and then Linker_Options.Last >= Linker_Options.First
668 new String'(Linker_Options.Table (Linker_Options.First).all &
673 -------------------------
674 -- Process_Binder_File --
675 -------------------------
677 procedure Process_Binder_File (Name : String) is
679 -- Binder file's descriptor
681 Link_Bytes : Integer := 0;
682 -- Projected number of bytes for the linker command line
685 pragma Import (C, Link_Max, "__gnat_link_max");
686 -- Maximum number of bytes on the command line supported by the OS
687 -- linker. Passed this limit the response file mechanism must be used
690 Next_Line : String (1 .. 1000);
691 -- Current line value
695 -- Current line slice (the slice does not contain line terminator)
698 -- Current line last character for shared libraries (without version)
700 Objs_Begin : Integer := 0;
701 -- First object file index in Linker_Objects table
703 Objs_End : Integer := 0;
704 -- Last object file index in Linker_Objects table
707 pragma Warnings (Off, Status);
708 -- Used for various Interfaces.C_Streams calls
710 Closing_Status : Boolean;
711 pragma Warnings (Off, Closing_Status);
714 GNAT_Static : Boolean := False;
715 -- Save state of -static option
717 GNAT_Shared : Boolean := False;
718 -- Save state of -shared option
720 Xlinker_Was_Previous : Boolean := False;
721 -- Indicate that "-Xlinker" was the option preceding the current
722 -- option. If True, then the current option is never suppressed.
726 -- These data items are used to store current binder file context.
727 -- The context is composed of the file descriptor position and the
728 -- current line together with the slice indexes (first and last
729 -- position) for this line. The rollback data are used by the
730 -- Store_File_Context and Rollback_File_Context routines below.
731 -- The file context mechanism interact only with the Get_Next_Line
732 -- call. For example:
734 -- Store_File_Context;
736 -- Rollback_File_Context;
739 -- Both Get_Next_Line calls above will read the exact same data from
740 -- the file. In other words, Next_Line, Nfirst and Nlast variables
741 -- will be set with the exact same values.
743 RB_File_Pos : long; -- File position
744 RB_Next_Line : String (1 .. 1000); -- Current line content
745 RB_Nlast : Integer; -- Slice last index
746 RB_Nfirst : Integer; -- Slice first index
748 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
749 pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
750 -- Pointer to string representing the native linker option which
751 -- specifies the path where the dynamic loader should find shared
752 -- libraries. Equal to null string if this system doesn't support it.
754 Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
756 (C, Object_Library_Ext_Ptr, "__gnat_object_library_extension");
757 -- Pointer to string specifying the default extension for
758 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
760 Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
761 pragma Import (C, Object_File_Option_Ptr, "__gnat_object_file_option");
762 -- Pointer to a string representing the linker option which specifies
763 -- the response file.
765 Using_GNU_Linker : Boolean;
766 for Using_GNU_Linker'Size use Character'Size;
767 pragma Import (C, Using_GNU_Linker, "__gnat_using_gnu_linker");
768 -- Predicate indicating whether this target uses the GNU linker. In
769 -- this case we must output a GNU linker compatible response file.
771 Separate_Run_Path_Options : Boolean;
772 for Separate_Run_Path_Options'Size use Character'Size;
774 (C, Separate_Run_Path_Options, "__gnat_separate_run_path_options");
775 -- Whether separate rpath options should be emitted for each directory
777 Opening : aliased constant String := """";
778 Closing : aliased constant String := '"' & ASCII.LF;
779 -- Needed to quote object paths in object list files when GNU linker
782 procedure Get_Next_Line;
783 -- Read the next line from the binder file without the line
786 function Index (S, Pattern : String) return Natural;
787 -- Return the last occurrence of Pattern in S, or 0 if none
789 function Is_Option_Present (Opt : String) return Boolean;
790 -- Return true if the option Opt is already present in
791 -- Linker_Options table.
793 procedure Store_File_Context;
794 -- Store current file context, Fd position and current line data.
795 -- The file context is stored into the rollback data above (RB_*).
796 -- Store_File_Context can be called at any time, only the last call
797 -- will be used (i.e. this routine overwrites the file context).
799 procedure Rollback_File_Context;
800 -- Restore file context from rollback data. This routine must be called
801 -- after Store_File_Context. The binder file context will be restored
802 -- with the data stored by the last Store_File_Context call.
808 procedure Get_Next_Line is
812 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
814 if Fchars = System.Null_Address then
815 Exit_With_Error ("Error reading binder output");
818 Nfirst := Next_Line'First;
820 while Nlast <= Next_Line'Last
821 and then Next_Line (Nlast) /= ASCII.LF
822 and then Next_Line (Nlast) /= ASCII.CR
834 function Index (S, Pattern : String) return Natural is
835 Len : constant Natural := Pattern'Length;
838 for J in reverse S'First .. S'Last - Len + 1 loop
839 if Pattern = S (J .. J + Len - 1) then
847 -----------------------
848 -- Is_Option_Present --
849 -----------------------
851 function Is_Option_Present (Opt : String) return Boolean is
853 for I in 1 .. Linker_Options.Last loop
855 if Linker_Options.Table (I).all = Opt then
862 end Is_Option_Present;
864 ---------------------------
865 -- Rollback_File_Context --
866 ---------------------------
868 procedure Rollback_File_Context is
870 Next_Line := RB_Next_Line;
873 Status := fseek (Fd, RB_File_Pos, Interfaces.C_Streams.SEEK_SET);
876 Exit_With_Error ("Error setting file position");
878 end Rollback_File_Context;
880 ------------------------
881 -- Store_File_Context --
882 ------------------------
884 procedure Store_File_Context is
885 use type System.CRTL.long;
887 RB_Next_Line := Next_Line;
890 RB_File_Pos := ftell (Fd);
892 if RB_File_Pos = -1 then
893 Exit_With_Error ("Error getting file position");
895 end Store_File_Context;
897 -- Start of processing for Process_Binder_File
900 Fd := fopen (Name'Address, Read_Mode'Address);
902 if Fd = NULL_Stream then
903 Exit_With_Error ("Failed to open binder output");
906 -- Skip up to the Begin Info line
910 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
916 -- Go to end when end line is reached (this will happen in
917 -- High_Integrity_Mode where no -L switches are generated)
919 exit when Next_Line (Nfirst .. Nlast) = End_Info;
921 if Ada_Bind_File then
922 Next_Line (Nfirst .. Nlast - 8) :=
923 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 VM_Target = CLI_Target
974 and then Link_Bytes > Link_Max
976 Lname := new String'("l~" & Base_Name (Ali_File_Name.all) & ".il");
978 for J in Objs_Begin .. Objs_End loop
979 Copy_File (Linker_Objects.Table (J).all, Lname.all,
980 Success => Closing_Status,
984 -- Add the special objects list file option together with the name
985 -- of the temporary file to the objects file table.
987 Linker_Objects.Table (Objs_Begin) :=
988 new String'(Value (Object_File_Option_Ptr) & Lname.all);
990 -- The slots containing these object file names are then removed
991 -- from the objects table so they do not appear in the link. They
992 -- are removed by moving up the linker options and non-Ada object
993 -- files appearing after the Ada object list in the table.
999 N := Objs_End - Objs_Begin + 1;
1001 for J in Objs_End + 1 .. Linker_Objects.Last loop
1002 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
1005 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
1008 elsif Object_List_File_Required
1009 or else (Object_List_File_Supported
1010 and then Link_Bytes > Link_Max)
1012 -- Create a temporary file containing the Ada user object files
1013 -- needed by the link. This list is taken from the bind file
1014 -- and is output one object per line for maximal compatibility with
1015 -- linkers supporting this option.
1017 Create_Temp_File (Tname_FD, Tname);
1019 -- ??? File descriptor should be checked to not be Invalid_FD.
1020 -- ??? Status of Write and Close operations should be checked, and
1021 -- failure should occur if a status is wrong.
1023 -- If target is using the GNU linker we must add a special header
1024 -- and footer in the response file.
1026 -- The syntax is : INPUT (object1.o object2.o ... )
1028 -- Because the GNU linker does not like name with characters such
1029 -- as '!', we must put the object paths between double quotes.
1031 if Using_GNU_Linker then
1033 GNU_Header : aliased constant String := "INPUT (";
1036 Status := Write (Tname_FD, GNU_Header'Address,
1041 for J in Objs_Begin .. Objs_End loop
1043 -- Opening quote for GNU linker
1045 if Using_GNU_Linker then
1046 Status := Write (Tname_FD, Opening'Address, 1);
1049 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
1050 Linker_Objects.Table (J).all'Length);
1052 -- Closing quote for GNU linker
1054 if Using_GNU_Linker then
1055 Status := Write (Tname_FD, Closing'Address, 2);
1058 Status := Write (Tname_FD, ASCII.LF'Address, 1);
1061 Response_File_Objects.Increment_Last;
1062 Response_File_Objects.Table (Response_File_Objects.Last) :=
1063 Linker_Objects.Table (J);
1066 -- Handle GNU linker response file footer
1068 if Using_GNU_Linker then
1070 GNU_Footer : aliased constant String := ")";
1073 Status := Write (Tname_FD, GNU_Footer'Address,
1078 Close (Tname_FD, Closing_Status);
1080 -- Add the special objects list file option together with the name
1081 -- of the temporary file (removing the null character) to the objects
1084 Linker_Objects.Table (Objs_Begin) :=
1085 new String'(Value (Object_File_Option_Ptr) &
1086 Tname (Tname'First .. Tname'Last - 1));
1088 -- The slots containing these object file names are then removed
1089 -- from the objects table so they do not appear in the link. They
1090 -- are removed by moving up the linker options and non-Ada object
1091 -- files appearing after the Ada object list in the table.
1097 N := Objs_End - Objs_Begin + 1;
1099 for J in Objs_End + 1 .. Linker_Objects.Last loop
1100 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
1103 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
1107 -- Process switches and options
1109 if Next_Line (Nfirst .. Nlast) /= End_Info then
1110 Xlinker_Was_Previous := False;
1113 if Xlinker_Was_Previous
1114 or else Next_Line (Nfirst .. Nlast) = "-Xlinker"
1116 Linker_Options.Increment_Last;
1117 Linker_Options.Table (Linker_Options.Last) :=
1118 new String'(Next_Line (Nfirst .. Nlast));
1120 elsif Next_Line (Nfirst .. Nlast) = "-static" then
1121 GNAT_Static := True;
1123 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1124 GNAT_Shared := True;
1126 -- Add binder options only if not already set on the command
1127 -- line. This rule is a way to control the linker options order.
1129 -- The following test needs comments, why is it VMS specific.
1130 -- The above comment looks out of date ???
1132 elsif not (OpenVMS_On_Target
1134 Is_Option_Present (Next_Line (Nfirst .. Nlast)))
1136 if Nlast > Nfirst + 2 and then
1137 Next_Line (Nfirst .. Nfirst + 1) = "-L"
1139 -- Construct a library search path for use later
1140 -- to locate static gnatlib libraries.
1142 if Libpath.Last > 1 then
1143 Libpath.Increment_Last;
1144 Libpath.Table (Libpath.Last) := Path_Separator;
1147 for I in Nfirst + 2 .. Nlast loop
1148 Libpath.Increment_Last;
1149 Libpath.Table (Libpath.Last) := Next_Line (I);
1152 Linker_Options.Increment_Last;
1154 Linker_Options.Table (Linker_Options.Last) :=
1155 new String'(Next_Line (Nfirst .. Nlast));
1157 elsif Next_Line (Nfirst .. Nlast) = "-ldecgnat"
1158 or else Next_Line (Nfirst .. Nlast) = "-lgnarl"
1159 or else Next_Line (Nfirst .. Nlast) = "-lgnat"
1161 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) =
1162 Shared_Lib ("gnarl")
1164 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) =
1167 -- If it is a shared library, remove the library version.
1168 -- We will be looking for the static version of the library
1169 -- as it is in the same directory as the shared version.
1171 if Next_Line (Nlast - Library_Version'Length + 1 .. Nlast)
1174 -- Set Last to point to last character before the
1177 Last := Nlast - Library_Version'Length - 1;
1182 -- Given a Gnat standard library, search the
1183 -- library path to find the library location
1186 File_Path : String_Access;
1188 Object_Lib_Extension : constant String :=
1189 Value (Object_Library_Ext_Ptr);
1191 File_Name : constant String := "lib" &
1192 Next_Line (Nfirst + 2 .. Last) &
1193 Object_Lib_Extension;
1195 Run_Path_Opt : constant String :=
1196 Value (Run_Path_Option_Ptr);
1198 GCC_Index : Natural;
1199 Run_Path_Opt_Index : Natural := 0;
1203 Locate_Regular_File (File_Name,
1204 String (Libpath.Table (1 .. Libpath.Last)));
1206 if File_Path /= null then
1209 -- If static gnatlib found, explicitly
1210 -- specify to overcome possible linker
1211 -- default usage of shared version.
1213 Linker_Options.Increment_Last;
1215 Linker_Options.Table (Linker_Options.Last) :=
1216 new String'(File_Path.all);
1218 elsif GNAT_Shared then
1219 if Opt.Run_Path_Option then
1220 -- If shared gnatlib desired, add the
1221 -- appropriate system specific switch
1222 -- so that it can be located at runtime.
1224 if Run_Path_Opt'Length /= 0 then
1225 -- Output the system specific linker command
1226 -- that allows the image activator to find
1227 -- the shared library at runtime.
1228 -- Also add path to find libgcc_s.so, if
1231 -- To find the location of the shared version
1232 -- of libgcc, we look for "gcc-lib" in the
1233 -- path of the library. However, this
1234 -- subdirectory is no longer present in
1235 -- in recent version of GCC. So, we look for
1236 -- the last subdirectory "lib" in the path.
1239 Index (File_Path.all, "gcc-lib");
1241 if GCC_Index /= 0 then
1242 -- The shared version of libgcc is
1243 -- located in the parent directory.
1245 GCC_Index := GCC_Index - 1;
1249 Index (File_Path.all, "/lib/");
1251 if GCC_Index = 0 then
1253 Index (File_Path.all,
1254 Directory_Separator &
1256 Directory_Separator);
1259 -- We have found a subdirectory "lib",
1260 -- this is where the shared version of
1261 -- libgcc should be located.
1263 if GCC_Index /= 0 then
1264 GCC_Index := GCC_Index + 3;
1268 -- Look for an eventual run_path_option in
1269 -- the linker switches.
1271 if Separate_Run_Path_Options then
1272 Linker_Options.Increment_Last;
1273 Linker_Options.Table
1274 (Linker_Options.Last) :=
1278 (1 .. File_Path'Length
1279 - File_Name'Length));
1281 if GCC_Index /= 0 then
1282 Linker_Options.Increment_Last;
1283 Linker_Options.Table
1284 (Linker_Options.Last) :=
1287 & File_Path (1 .. GCC_Index));
1291 1 .. Linker_Options.Last
1293 if Linker_Options.Table (J) /= null
1295 Linker_Options.Table (J)'Length
1296 > Run_Path_Opt'Length
1298 Linker_Options.Table (J)
1299 (1 .. Run_Path_Opt'Length) =
1302 -- We have found a already specified
1303 -- run_path_option: we will add to
1304 -- this switch, because only one
1305 -- run_path_option should be
1308 Run_Path_Opt_Index := J;
1313 -- If there is no run_path_option, we need
1316 if Run_Path_Opt_Index = 0 then
1317 Linker_Options.Increment_Last;
1320 if GCC_Index = 0 then
1321 if Run_Path_Opt_Index = 0 then
1322 Linker_Options.Table
1323 (Linker_Options.Last) :=
1327 (1 .. File_Path'Length
1328 - File_Name'Length));
1331 Linker_Options.Table
1332 (Run_Path_Opt_Index) :=
1334 (Linker_Options.Table
1335 (Run_Path_Opt_Index).all
1338 (1 .. File_Path'Length
1339 - File_Name'Length));
1343 if Run_Path_Opt_Index = 0 then
1344 Linker_Options.Table
1345 (Linker_Options.Last) :=
1346 new String'(Run_Path_Opt
1348 (1 .. File_Path'Length
1351 & File_Path (1 .. GCC_Index));
1354 Linker_Options.Table
1355 (Run_Path_Opt_Index) :=
1357 (Linker_Options.Table
1358 (Run_Path_Opt_Index).all
1361 (1 .. File_Path'Length
1364 & File_Path (1 .. GCC_Index));
1371 -- Then we add the appropriate -l switch
1373 Linker_Options.Increment_Last;
1374 Linker_Options.Table (Linker_Options.Last) :=
1375 new String'(Next_Line (Nfirst .. Nlast));
1379 -- If gnatlib library not found, then
1380 -- add it anyway in case some other
1381 -- mechanism may find it.
1383 Linker_Options.Increment_Last;
1384 Linker_Options.Table (Linker_Options.Last) :=
1385 new String'(Next_Line (Nfirst .. Nlast));
1389 Linker_Options.Increment_Last;
1390 Linker_Options.Table (Linker_Options.Last) :=
1391 new String'(Next_Line (Nfirst .. Nlast));
1395 Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker";
1398 exit when Next_Line (Nfirst .. Nlast) = End_Info;
1400 if Ada_Bind_File then
1401 Next_Line (Nfirst .. Nlast - 8) :=
1402 Next_Line (Nfirst + 8 .. Nlast);
1408 -- If -shared was specified, invoke gcc with -shared-libgcc
1411 Linker_Options.Increment_Last;
1412 Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc;
1415 Status := fclose (Fd);
1416 end Process_Binder_File;
1424 Write_Str ("Usage: ");
1425 Write_Str (Base_Name (Command_Name));
1426 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
1429 Write_Line (" mainprog.ali the ALI file of the main program");
1431 Write_Line (" -A Binder generated source file is in Ada (default)");
1432 Write_Line (" -C Binder generated source file is in C");
1433 Write_Line (" -f force object file list to be generated");
1434 Write_Line (" -g Compile binder source file with debug information");
1435 Write_Line (" -n Do not compile the binder source file");
1436 Write_Line (" -R Do not use a run_path_option");
1437 Write_Line (" -v verbose mode");
1438 Write_Line (" -v -v very verbose mode");
1440 Write_Line (" -o nam Use 'nam' as the name of the executable");
1441 Write_Line (" -b target Compile the binder source to run on target");
1442 Write_Line (" -Bdir Load compiler executables from dir");
1444 if Is_Supported (Map_File) then
1445 Write_Line (" -Mmap Create map file map");
1446 Write_Line (" -M Create map file mainprog.map");
1449 Write_Line (" --GCC=comp Use comp as the compiler");
1450 Write_Line (" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1452 Write_Line (" [non-Ada-objects] list of non Ada object files");
1453 Write_Line (" [linker-options] other options for the linker");
1460 procedure Write_Header is
1462 if Verbose_Mode then
1464 Display_Version ("GNATLINK", "1995");
1472 procedure Write_Usage is
1478 -- Start of processing for Gnatlink
1481 -- Add the directory where gnatlink is invoked in front of the
1482 -- path, if gnatlink is invoked with directory information.
1483 -- Only do this if the platform is not VMS, where the notion of path
1484 -- does not really exist.
1486 if not Hostparm.OpenVMS then
1488 Command : constant String := Command_Name;
1491 for Index in reverse Command'Range loop
1492 if Command (Index) = Directory_Separator then
1494 Absolute_Dir : constant String :=
1496 (Command (Command'First .. Index));
1498 PATH : constant String :=
1501 Getenv ("PATH").all;
1504 Setenv ("PATH", PATH);
1515 if Argument_Count = 0
1517 (Verbose_Mode and then Argument_Count = 1)
1520 Exit_Program (E_Fatal);
1523 -- Initialize packages to be used
1529 -- We always compile with -c
1531 Binder_Options_From_ALI.Increment_Last;
1532 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1535 if Ali_File_Name = null then
1536 Exit_With_Error ("no ali file given for link");
1539 if not Is_Regular_File (Ali_File_Name.all) then
1540 Exit_With_Error (Ali_File_Name.all & " not found");
1543 -- Read the ALI file of the main subprogram if the binder generated
1544 -- file needs to be compiled and no --GCC= switch has been specified.
1545 -- Fetch the back end switches from this ALI file and use these switches
1546 -- to compile the binder generated file
1548 if Compile_Bind_File and then Standard_Gcc then
1551 Name_Len := Ali_File_Name'Length;
1552 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1556 F : constant File_Name_Type := Name_Find;
1557 T : Text_Buffer_Ptr;
1561 -- Load the ALI file
1563 T := Read_Library_Info (F, True);
1565 -- Read it. Note that we ignore errors, since we only want very
1566 -- limited information from the ali file, and likely a slightly
1567 -- wrong version will be just fine, though in normal operation
1568 -- we don't expect this to happen!
1575 Ignore_Errors => True);
1577 if A /= No_ALI_Id then
1579 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1580 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1582 -- Do not compile with the front end switches. However, --RTS
1583 -- is to be dealt with specially because it needs to be passed
1584 -- if the binder-generated file is in Ada and may also be used
1585 -- to drive the linker.
1588 Arg : String_Ptr renames Args.Table (Index);
1590 if not Is_Front_End_Switch (Arg.all) then
1591 Binder_Options_From_ALI.Increment_Last;
1592 Binder_Options_From_ALI.Table
1593 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1595 elsif Arg'Length > 5
1596 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1598 if Ada_Bind_File then
1599 Binder_Options_From_ALI.Increment_Last;
1600 Binder_Options_From_ALI.Table
1601 (Binder_Options_From_ALI.Last)
1602 := String_Access (Arg);
1605 -- Set the RTS_*_Path_Name variables, so that the
1606 -- correct directories will be set when
1607 -- Osint.Add_Default_Search_Dirs will be called later.
1609 Opt.RTS_Src_Path_Name :=
1611 (Arg (Arg'First + 6 .. Arg'Last), Include);
1613 Opt.RTS_Lib_Path_Name :=
1615 (Arg (Arg'First + 6 .. Arg'Last), Objects);
1617 -- GNAT doesn't support the GCC multilib mechanism.
1618 -- This means that, when a multilib switch is used
1619 -- to request a particular compilation mode, the
1620 -- corresponding runtime switch (--RTS) must also be
1621 -- specified. The long-term goal is to fully support the
1622 -- multilib mechanism; however, in the meantime, it is
1623 -- convenient to eliminate the redundancy by keying the
1624 -- compilation mode on a single switch, namely --RTS.
1626 -- Pass -mrtp to the linker if --RTS=rtp was passed
1629 and then Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1631 Linker_Options.Increment_Last;
1632 Linker_Options.Table (Linker_Options.Last) :=
1633 new String'("-mrtp");
1635 -- Pass -fsjlj to the linker if --RTS=sjlj was passed
1637 elsif Arg'Length > 9
1638 and then Arg (Arg'First + 6 .. Arg'First + 9) = "sjlj"
1640 Linker_Options.Increment_Last;
1641 Linker_Options.Table (Linker_Options.Last) :=
1642 new String'("-fsjlj");
1651 -- Get target parameters
1653 Osint.Add_Default_Search_Dirs;
1654 Targparm.Get_Target_Parameters;
1656 if VM_Target /= No_VM then
1658 when JVM_Target => Gcc := new String'("jvm-gnatcompile");
1659 when CLI_Target => Gcc := new String'("dotnet-gnatcompile");
1660 when No_VM => raise Program_Error;
1663 Ada_Bind_File := True;
1664 Begin_Info := "-- BEGIN Object file/option list";
1665 End_Info := "-- END Object file/option list ";
1668 -- If the main program is in Ada it is compiled with the following
1671 -- -gnatA stops reading gnat.adc, since we don't know what
1672 -- pragmas would work, and we do not need it anyway.
1674 -- -gnatWb allows brackets coding for wide characters
1676 -- -gnatiw allows wide characters in identifiers. This is needed
1677 -- because bindgen uses brackets encoding for all upper
1678 -- half and wide characters in identifier names.
1680 if Ada_Bind_File then
1681 Binder_Options_From_ALI.Increment_Last;
1682 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1683 new String'("-gnatA");
1684 Binder_Options_From_ALI.Increment_Last;
1685 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1686 new String'("-gnatWb");
1687 Binder_Options_From_ALI.Increment_Last;
1688 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1689 new String'("-gnatiw");
1692 -- Locate all the necessary programs and verify required files are present
1694 Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1696 if Gcc_Path = null then
1697 Exit_With_Error ("Couldn't locate " & Gcc.all);
1700 if Linker_Path = null then
1701 if VM_Target = CLI_Target then
1702 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("ilasm");
1704 if Linker_Path = null then
1705 Exit_With_Error ("Couldn't locate ilasm");
1708 elsif RTX_RTSS_Kernel_Module_On_Target then
1710 -- Use Microsoft linker for RTSS modules
1712 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("link");
1714 if Linker_Path = null then
1715 Exit_With_Error ("Couldn't locate link");
1719 Linker_Path := Gcc_Path;
1725 -- If no output name specified, then use the base name of .ali file name
1727 if Output_File_Name = null then
1729 new String'(Base_Name (Ali_File_Name.all)
1730 & Get_Target_Debuggable_Suffix.all);
1732 if VM_Target = CLI_Target then
1733 Linker_Options.Increment_Last;
1734 Linker_Options.Table (Linker_Options.Last) := new String'("/QUIET");
1736 Linker_Options.Increment_Last;
1737 Linker_Options.Table (Linker_Options.Last) := new String'("/DEBUG");
1739 Linker_Options.Increment_Last;
1740 Linker_Options.Table (Linker_Options.Last) :=
1741 new String'("/OUTPUT=" & Output_File_Name.all);
1743 elsif RTX_RTSS_Kernel_Module_On_Target then
1744 Linker_Options.Increment_Last;
1745 Linker_Options.Table (Linker_Options.Last) :=
1746 new String'("/OUT:" & Output_File_Name.all);
1749 Linker_Options.Increment_Last;
1750 Linker_Options.Table (Linker_Options.Last) := new String'("-o");
1752 Linker_Options.Increment_Last;
1753 Linker_Options.Table (Linker_Options.Last) :=
1754 new String'(Output_File_Name.all);
1758 -- Warn if main program is called "test", as that may be a built-in command
1759 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1760 -- will not appear. However, do not warn in the case of a cross compiler.
1762 -- Assume this is a cross tool if the executable name is not gnatlink
1764 if Base_Name (Command_Name) = "gnatlink"
1765 and then Output_File_Name.all = "test"
1767 Error_Msg ("warning: executable name """ & Output_File_Name.all
1768 & """ may conflict with shell command");
1771 -- If -M switch was specified, add the switches to create the map file
1773 if Create_Map_File then
1775 Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map";
1776 Switches : String_List_Access;
1779 Convert (Map_File, Map_Name, Switches);
1781 if Switches /= null then
1782 for J in Switches'Range loop
1783 Linker_Options.Increment_Last;
1784 Linker_Options.Table (Linker_Options.Last) := Switches (J);
1790 -- Perform consistency checks
1792 -- Transform the .ali file name into the binder output file name
1794 Make_Binder_File_Names : declare
1795 Fname : constant String := Base_Name (Ali_File_Name.all);
1796 Fname_Len : Integer := Fname'Length;
1798 function Get_Maximum_File_Name_Length return Integer;
1799 pragma Import (C, Get_Maximum_File_Name_Length,
1800 "__gnat_get_maximum_file_name_length");
1802 Maximum_File_Name_Length : constant Integer :=
1803 Get_Maximum_File_Name_Length;
1805 Bind_File_Prefix : Types.String_Ptr;
1806 -- Contains prefix used for bind files
1811 if not Ada_Bind_File then
1812 Bind_File_Prefix := new String'("b_");
1813 elsif OpenVMS_On_Target then
1814 Bind_File_Prefix := new String'("b__");
1816 Bind_File_Prefix := new String'("b~");
1819 -- If the length of the binder file becomes too long due to
1820 -- the addition of the "b?" prefix, then truncate it.
1822 if Maximum_File_Name_Length > 0 then
1824 Maximum_File_Name_Length - Bind_File_Prefix.all'Length
1826 Fname_Len := Fname_Len - 1;
1831 Fnam : constant String :=
1832 Bind_File_Prefix.all &
1833 Fname (Fname'First .. Fname'First + Fname_Len - 1);
1836 if Ada_Bind_File then
1837 Binder_Spec_Src_File := new String'(Fnam & ".ads");
1838 Binder_Body_Src_File := new String'(Fnam & ".adb");
1839 Binder_Ali_File := new String'(Fnam & ".ali");
1841 Binder_Body_Src_File := new String'(Fnam & ".c");
1844 Binder_Obj_File := new String'(Fnam & Get_Target_Object_Suffix.all);
1847 if Fname_Len /= Fname'Length then
1848 Binder_Options.Increment_Last;
1849 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1850 Binder_Options.Increment_Last;
1851 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1853 end Make_Binder_File_Names;
1855 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1857 -- Compile the binder file. This is fast, so we always do it, unless
1858 -- specifically told not to by the -n switch
1860 if Compile_Bind_File then
1863 Args : Argument_List
1864 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1867 for J in 1 .. Binder_Options_From_ALI.Last loop
1868 Args (J) := Binder_Options_From_ALI.Table (J);
1871 for J in 1 .. Binder_Options.Last loop
1872 Args (Binder_Options_From_ALI.Last + J) :=
1873 Binder_Options.Table (J);
1876 -- Use the full path of the binder generated source, so that it is
1877 -- guaranteed that the debugger will find this source, even with
1881 new String'(Normalize_Pathname (Binder_Body_Src_File.all));
1883 if Verbose_Mode then
1884 Write_Str (Base_Name (Gcc_Path.all));
1886 for J in Args'Range loop
1888 Write_Str (Args (J).all);
1894 System.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1897 Exit_Program (E_Fatal);
1902 -- Now, actually link the program
1904 -- Skip this step for now on JVM since the Java interpreter will do
1905 -- the actual link at run time. We might consider packing all class files
1906 -- in a .zip file during this step.
1908 if VM_Target /= JVM_Target then
1910 Num_Args : Natural :=
1911 (Linker_Options.Last - Linker_Options.First + 1) +
1912 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1913 (Linker_Objects.Last - Linker_Objects.First + 1);
1914 Stack_Op : Boolean := False;
1915 IDENT_Op : Boolean := False;
1918 if VM_Target = CLI_Target then
1920 -- Remove extraneous flags not relevant for CIL. Also remove empty
1921 -- arguments, since ilasm chokes on them.
1923 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1924 if Linker_Options.Table (J)'Length = 0
1925 or else Linker_Options.Table (J) (1 .. 2) = "-L"
1926 or else Linker_Options.Table (J) (1 .. 2) = "-l"
1927 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1928 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1929 or else Linker_Options.Table (J) (1 .. 2) = "-g"
1931 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1932 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1933 Linker_Options.Decrement_Last;
1934 Num_Args := Num_Args - 1;
1938 elsif RTX_RTSS_Kernel_Module_On_Target then
1940 -- Remove flags not relevant for Microsoft linker and adapt some
1943 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1945 -- Remove flags that are not accepted
1946 if Linker_Options.Table (J)'Length = 0
1947 or else Linker_Options.Table (J) (1 .. 2) = "-l"
1948 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1949 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1950 or else Linker_Options.Table (J) (1 .. 8) = "-Xlinker"
1951 or else Linker_Options.Table (J) (1 .. 9) = "-mthreads"
1953 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1954 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1955 Linker_Options.Decrement_Last;
1956 Num_Args := Num_Args - 1;
1958 -- Replace "-L" by its counterpart "/LIBPATH:" and UNIX "/" by
1960 elsif Linker_Options.Table (J) (1 .. 2) = "-L" then
1962 Libpath_Option : constant String_Access := new String'
1964 Linker_Options.Table (J)
1965 (3 .. Linker_Options.Table (J).all'Last));
1967 for Index in 10 .. Libpath_Option'Last loop
1968 if Libpath_Option (Index) = '/' then
1969 Libpath_Option (Index) := '\';
1973 Linker_Options.Table (J) := Libpath_Option;
1976 -- Replace "-g" by "/DEBUG"
1977 elsif Linker_Options.Table (J) (1 .. 2) = "-g" then
1978 Linker_Options.Table (J) := new String'("/DEBUG");
1980 -- Replace "-o" by "/OUT:"
1981 elsif Linker_Options.Table (J) (1 .. 2) = "-o" then
1982 Linker_Options.Table (J + 1) := new String'
1983 ("/OUT:" & Linker_Options.Table (J + 1).all);
1985 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1986 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1987 Linker_Options.Decrement_Last;
1988 Num_Args := Num_Args - 1;
1990 -- Replace "--stack=" by "/STACK:"
1991 elsif Linker_Options.Table (J) (1 .. 8) = "--stack=" then
1992 Linker_Options.Table (J) := new String'
1994 Linker_Options.Table (J)
1995 (9 .. Linker_Options.Table (J).all'Last));
1997 -- Replace "-v" by its counterpart "/VERBOSE"
1998 elsif Linker_Options.Table (J) (1 .. 2) = "-v" then
1999 Linker_Options.Table (J) := new String'("/VERBOSE");
2003 -- Add some required flags to create RTSS modules
2006 Flags_For_Linker : constant array (1 .. 17) of String_Access :=
2007 (new String'("/NODEFAULTLIB"),
2008 new String'("/INCREMENTAL:NO"),
2009 new String'("/NOLOGO"),
2010 new String'("/DRIVER"),
2011 new String'("/ALIGN:0x20"),
2012 new String'("/SUBSYSTEM:NATIVE"),
2013 new String'("/ENTRY:_RtapiProcessEntryCRT@8"),
2014 new String'("/RELEASE"),
2015 new String'("startupCRT.obj"),
2016 new String'("rtxlibcmt.lib"),
2017 new String'("oldnames.lib"),
2018 new String'("rtapi_rtss.lib"),
2019 new String'("Rtx_Rtss.lib"),
2020 new String'("libkernel32.a"),
2021 new String'("libws2_32.a"),
2022 new String'("libmswsock.a"),
2023 new String'("libadvapi32.a"));
2024 -- These flags need to be passed to Microsoft linker. They
2025 -- come from the RTX documentation.
2027 Gcc_Lib_Path : constant String_Access := new String'
2028 ("/LIBPATH:" & Include_Dir_Default_Prefix & "\..\");
2029 -- Place to look for gcc related libraries, such as libgcc
2032 -- Replace UNIX "/" by Windows "\" in the path
2034 for Index in 10 .. Gcc_Lib_Path.all'Last loop
2035 if Gcc_Lib_Path (Index) = '/' then
2036 Gcc_Lib_Path (Index) := '\';
2040 Linker_Options.Increment_Last;
2041 Linker_Options.Table (Linker_Options.Last) := Gcc_Lib_Path;
2042 Num_Args := Num_Args + 1;
2044 for Index in Flags_For_Linker'Range loop
2045 Linker_Options.Increment_Last;
2046 Linker_Options.Table (Linker_Options.Last) :=
2047 Flags_For_Linker (Index);
2048 Num_Args := Num_Args + 1;
2053 -- Remove duplicate stack size setting from the Linker_Options
2054 -- table. The stack setting option "-Xlinker --stack=R,C" can be
2055 -- found in one line when set by a pragma Linker_Options or in two
2056 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
2057 -- line. We also check for the "-Wl,--stack=R" style option.
2059 -- We must remove the second stack setting option instance
2060 -- because the one on the command line will always be the first
2061 -- one. And any subsequent stack setting option will overwrite the
2062 -- previous one. This is done especially for GNAT/NT where we set
2063 -- the stack size for tasking programs by a pragma in the NT
2064 -- specific tasking package System.Task_Primitives.Operations.
2066 -- Note: This is not a FOR loop that runs from Linker_Options.First
2067 -- to Linker_Options.Last, since operations within the loop can
2068 -- modify the length of the table.
2070 Clean_Link_Option_Set : declare
2071 J : Natural := Linker_Options.First;
2072 Shared_Libgcc_Seen : Boolean := False;
2075 while J <= Linker_Options.Last loop
2077 if Linker_Options.Table (J).all = "-Xlinker"
2078 and then J < Linker_Options.Last
2079 and then Linker_Options.Table (J + 1)'Length > 8
2080 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
2083 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
2084 Linker_Options.Table (J + 2 .. Linker_Options.Last);
2085 Linker_Options.Decrement_Last;
2086 Linker_Options.Decrement_Last;
2087 Num_Args := Num_Args - 2;
2094 -- Remove duplicate -shared-libgcc switch
2096 if Linker_Options.Table (J).all = Shared_Libgcc_String then
2097 if Shared_Libgcc_Seen then
2098 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2099 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2100 Linker_Options.Decrement_Last;
2101 Num_Args := Num_Args - 1;
2104 Shared_Libgcc_Seen := True;
2108 -- Here we just check for a canonical form that matches the
2109 -- pragma Linker_Options set in the NT runtime.
2111 if (Linker_Options.Table (J)'Length > 17
2112 and then Linker_Options.Table (J) (1 .. 17)
2113 = "-Xlinker --stack=")
2115 (Linker_Options.Table (J)'Length > 12
2116 and then Linker_Options.Table (J) (1 .. 12)
2120 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2121 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2122 Linker_Options.Decrement_Last;
2123 Num_Args := Num_Args - 1;
2130 -- Remove duplicate IDENTIFICATION directives (VMS)
2132 if Linker_Options.Table (J)'Length > 27
2133 and then Linker_Options.Table (J) (1 .. 28)
2134 = "--for-linker=IDENTIFICATION="
2137 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2138 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2139 Linker_Options.Decrement_Last;
2140 Num_Args := Num_Args - 1;
2149 if Linker_Path = Gcc_Path and then VM_Target = No_VM then
2151 -- For systems where the default is to link statically with
2152 -- libgcc, if gcc is not called with -shared-libgcc, call it
2153 -- with -static-libgcc, as there are some platforms where one
2154 -- of these two switches is compulsory to link.
2156 if Shared_Libgcc_Default = 'T'
2157 and then not Shared_Libgcc_Seen
2159 Linker_Options.Increment_Last;
2160 Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
2161 Num_Args := Num_Args + 1;
2164 elsif RTX_RTSS_Kernel_Module_On_Target then
2166 -- Force the use of the static libgcc for RTSS modules
2168 Linker_Options.Increment_Last;
2169 Linker_Options.Table (Linker_Options.Last) :=
2170 new String'("libgcc.a");
2171 Num_Args := Num_Args + 1;
2174 end Clean_Link_Option_Set;
2176 -- Prepare arguments for call to linker
2178 Call_Linker : declare
2180 Args : Argument_List (1 .. Num_Args + 1);
2181 Index : Integer := Args'First;
2184 Args (Index) := Binder_Obj_File;
2186 -- Add the object files and any -largs libraries
2188 for J in Linker_Objects.First .. Linker_Objects.Last loop
2190 Args (Index) := Linker_Objects.Table (J);
2193 -- Add the linker options from the binder file
2195 for J in Linker_Options.First .. Linker_Options.Last loop
2197 Args (Index) := Linker_Options.Table (J);
2200 -- Finally add the libraries from the --GCC= switch
2202 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
2204 Args (Index) := Gcc_Linker_Options.Table (J);
2207 if Verbose_Mode then
2208 Write_Str (Linker_Path.all);
2210 for J in Args'Range loop
2212 Write_Str (Args (J).all);
2217 -- If we are on very verbose mode (-v -v) and a response file
2218 -- is used we display its content.
2220 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
2222 Write_Str ("Response file (" &
2223 Tname (Tname'First .. Tname'Last - 1) &
2228 Response_File_Objects.First ..
2229 Response_File_Objects.Last
2231 Write_Str (Response_File_Objects.Table (J).all);
2239 System.OS_Lib.Spawn (Linker_Path.all, Args, Success);
2241 -- Delete the temporary file used in conjunction with linking if
2242 -- one was created. See Process_Bind_File for details.
2244 if Tname_FD /= Invalid_FD then
2248 if Lname /= null then
2249 Delete (Lname.all & ASCII.NUL);
2253 Error_Msg ("error when calling " & Linker_Path.all);
2254 Exit_Program (E_Fatal);
2260 -- Only keep the binder output file and it's associated object
2261 -- file if compiling with the -g option. These files are only
2262 -- useful if debugging.
2264 if not Debug_Flag_Present then
2265 if Binder_Ali_File /= null then
2266 Delete (Binder_Ali_File.all & ASCII.NUL);
2269 if Binder_Spec_Src_File /= null then
2270 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
2273 Delete (Binder_Body_Src_File.all & ASCII.NUL);
2275 if VM_Target = No_VM then
2276 Delete (Binder_Obj_File.all & ASCII.NUL);
2280 Exit_Program (E_Success);
2284 Write_Line (Exception_Information (X));
2285 Exit_With_Error ("INTERNAL ERROR. Please report");