1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2010, 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 Check_Existing_Executable
(File_Name
: String);
203 -- Delete any existing executable to avoid accidentally updating the target
204 -- of a symbolic link, but produce a Fatail_Error if File_Name matches any
205 -- of the source file names. This avoids overwriting of extensionless
206 -- source files by accident on systems where executables do not have
209 procedure Delete
(Name
: String);
210 -- Wrapper to unlink as status is ignored by this application
212 procedure Error_Msg
(Message
: String);
213 -- Output the error or warning Message
215 procedure Exit_With_Error
(Error
: String);
216 -- Output Error and exit program with a fatal condition
218 procedure Process_Args
;
219 -- Go through all the arguments and build option tables
221 procedure Process_Binder_File
(Name
: String);
222 -- Reads the binder file and extracts linker arguments
227 procedure Write_Header
;
228 -- Show user the program name, version and copyright
230 procedure Write_Usage
;
231 -- Show user the program options
237 function Base_Name
(File_Name
: String) return String is
242 Findex1
:= File_Name
'First;
244 -- The file might be specified by a full path name. However,
245 -- we want the path to be stripped away.
247 for J
in reverse File_Name
'Range loop
248 if Is_Directory_Separator
(File_Name
(J
)) then
254 Findex2
:= File_Name
'Last;
255 while Findex2
> Findex1
256 and then File_Name
(Findex2
) /= '.'
258 Findex2
:= Findex2
- 1;
261 if Findex2
= Findex1
then
262 Findex2
:= File_Name
'Last + 1;
265 return File_Name
(Findex1
.. Findex2
- 1);
268 -------------------------------
269 -- Check_Existing_Executable --
270 -------------------------------
272 procedure Check_Existing_Executable
(File_Name
: String) is
273 Ename
: String := File_Name
;
274 Efile
: File_Name_Type
;
275 Sfile
: File_Name_Type
;
278 Canonical_Case_File_Name
(Ename
);
280 Add_Str_To_Name_Buffer
(Ename
);
283 for J
in Units
.Table
'First .. Units
.Last
loop
284 Sfile
:= Units
.Table
(J
).Sfile
;
285 if Sfile
= Efile
then
286 Exit_With_Error
("executable name """ & File_Name
& """ matches "
287 & "source file name """ & Get_Name_String
(Sfile
) & """");
292 end Check_Existing_Executable
;
298 procedure Delete
(Name
: String) is
300 pragma Unreferenced
(Status
);
302 Status
:= unlink
(Name
'Address);
303 -- Is it really right to ignore an error here ???
310 procedure Error_Msg
(Message
: String) is
312 Write_Str
(Base_Name
(Command_Name
));
318 ---------------------
319 -- Exit_With_Error --
320 ---------------------
322 procedure Exit_With_Error
(Error
: String) is
325 Exit_Program
(E_Fatal
);
332 procedure Process_Args
is
334 Skip_Next
: Boolean := False;
335 -- Set to true if the next argument is to be added into the list of
336 -- linker's argument without parsing it.
338 procedure Check_Version_And_Help
is new Check_Version_And_Help_G
(Usage
);
340 -- Start of processing for Process_Args
343 -- First, check for --version and --help
345 Check_Version_And_Help
("GNATLINK", "1995");
347 -- Loop through arguments of gnatlink command
351 exit when Next_Arg
> Argument_Count
;
353 Process_One_Arg
: declare
354 Arg
: constant String := Argument
(Next_Arg
);
357 -- Case of argument which is a switch
359 -- We definitely need section by section comments here ???
363 -- This argument must not be parsed, just add it to the
364 -- list of linker's options.
368 Linker_Options
.Increment_Last
;
369 Linker_Options
.Table
(Linker_Options
.Last
) :=
372 elsif Arg'Length /= 0 and then Arg (1) = '-' then
373 if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then
375 ("invalid switch: """ & Arg & """ (gnat not needed here)");
378 if Arg = "-Xlinker" then
380 -- Next argument should be sent directly to the linker.
381 -- We do not want to parse it here.
385 Linker_Options.Increment_Last;
386 Linker_Options.Table (Linker_Options.Last) :=
390 and then (Arg
'Length < 5 or else Arg
(2 .. 5) /= "gnat")
392 Debug_Flag_Present
:= True;
394 Linker_Options
.Increment_Last
;
395 Linker_Options
.Table
(Linker_Options
.Last
) :=
398 Binder_Options.Increment_Last;
399 Binder_Options.Table (Binder_Options.Last) :=
400 Linker_Options.Table (Linker_Options.Last);
402 elsif Arg'Length >= 3 and then Arg (2) = 'M
' then
404 Switches : String_List_Access;
407 Convert (Map_File, Arg (3 .. Arg'Last), Switches);
409 if Switches /= null then
410 for J in Switches'Range loop
411 Linker_Options.Increment_Last;
412 Linker_Options.Table (Linker_Options.Last) :=
418 elsif Arg'Length = 2 then
421 Ada_Bind_File := True;
422 Begin_Info := "-- BEGIN Object file/option list";
423 End_Info := "-- END Object file/option list ";
426 Linker_Options.Increment_Last;
427 Linker_Options.Table (Linker_Options.Last) :=
430 Binder_Options
.Increment_Last
;
431 Binder_Options
.Table
(Binder_Options
.Last
) :=
432 Linker_Options
.Table
(Linker_Options
.Last
);
434 Next_Arg
:= Next_Arg
+ 1;
436 if Next_Arg
> Argument_Count
then
437 Exit_With_Error
("Missing argument for -b");
440 Get_Machine_Name
: declare
441 Name_Arg
: constant String_Access
:=
442 new String'(Argument (Next_Arg));
445 Linker_Options.Increment_Last;
446 Linker_Options.Table (Linker_Options.Last) :=
449 Binder_Options.Increment_Last;
450 Binder_Options.Table (Binder_Options.Last) :=
453 end Get_Machine_Name;
456 Ada_Bind_File := False;
457 Begin_Info := "/* BEGIN Object file/option list";
458 End_Info := " END Object file/option list */";
461 if Object_List_File_Supported then
462 Object_List_File_Required := True;
465 ("Object list file not supported on this target");
469 Create_Map_File := True;
472 Compile_Bind_File := False;
475 Next_Arg := Next_Arg + 1;
477 if Next_Arg > Argument_Count then
478 Exit_With_Error ("Missing argument for -o");
482 new String'(Executable_Name
483 (Argument
(Next_Arg
),
484 Only_If_No_Suffix
=> True));
487 Opt
.Run_Path_Option
:= False;
491 -- Support "double" verbose mode. Second -v
492 -- gets sent to the linker and binder phases.
495 Very_Verbose_Mode
:= True;
497 Linker_Options
.Increment_Last
;
498 Linker_Options
.Table
(Linker_Options
.Last
) :=
501 Binder_Options.Increment_Last;
502 Binder_Options.Table (Binder_Options.Last) :=
503 Linker_Options.Table (Linker_Options.Last);
506 Verbose_Mode := True;
511 Linker_Options.Increment_Last;
512 Linker_Options.Table (Linker_Options.Last) :=
517 elsif Arg
(2) = 'B' then
518 Linker_Options
.Increment_Last
;
519 Linker_Options
.Table
(Linker_Options
.Last
) :=
522 Binder_Options.Increment_Last;
523 Binder_Options.Table (Binder_Options.Last) :=
524 Linker_Options.Table (Linker_Options.Last);
526 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
527 if Arg'Length = 7 then
528 Exit_With_Error ("Missing argument for --LINK=");
532 System.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
534 if Linker_Path = null then
536 ("Could not locate linker: " & Arg (8 .. Arg'Last));
539 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
541 Program_Args : constant Argument_List_Access :=
542 Argument_String_To_List
543 (Arg (7 .. Arg'Last));
546 if Program_Args.all (1).all /= Gcc.all then
547 Gcc := new String'(Program_Args
.all (1).all);
548 Standard_Gcc
:= False;
551 -- Set appropriate flags for switches passed
553 for J
in 2 .. Program_Args
.all'Last loop
555 Arg
: constant String := Program_Args
.all (J
).all;
556 AF
: constant Integer := Arg
'First;
559 if Arg
'Length /= 0 and then Arg
(AF
) = '-' then
560 if Arg
(AF
+ 1) = 'g'
561 and then (Arg
'Length = 2
562 or else Arg
(AF
+ 2) in '0' .. '3'
563 or else Arg
(AF
+ 2 .. Arg
'Last) = "coff")
565 Debug_Flag_Present
:= True;
569 -- Add directory to source search dirs so that
570 -- Get_Target_Parameters can find system.ads
572 if Arg
(AF
.. AF
+ 1) = "-I"
573 and then Arg
'Length > 2
575 Add_Src_Search_Dir
(Arg
(AF
+ 2 .. Arg
'Last));
578 -- Pass to gcc for compiling binder generated file
579 -- No use passing libraries, it will just generate
582 if not (Arg
(AF
.. AF
+ 1) = "-l"
583 or else Arg
(AF
.. AF
+ 1) = "-L")
585 Binder_Options
.Increment_Last
;
586 Binder_Options
.Table
(Binder_Options
.Last
) :=
590 -- Pass to gcc for linking program
592 Gcc_Linker_Options.Increment_Last;
593 Gcc_Linker_Options.Table
594 (Gcc_Linker_Options.Last) := new String'(Arg
);
599 -- Send all multi-character switches not recognized as
600 -- a special case by gnatlink to the linker/loader stage.
603 Linker_Options
.Increment_Last
;
604 Linker_Options
.Table
(Linker_Options
.Last
) :=
608 -- Here if argument is a file name rather than a switch
611 -- If explicit ali file, capture it
614 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
616 if Ali_File_Name = null then
617 Ali_File_Name := new String'(Arg
);
619 Exit_With_Error
("cannot handle more than one ALI file");
622 -- If target object file, record object file
624 elsif Arg
'Length > Get_Target_Object_Suffix
.all'Length
627 Get_Target_Object_Suffix
.all'Length + 1 .. Arg
'Last)
628 = Get_Target_Object_Suffix
.all
630 Linker_Objects
.Increment_Last
;
631 Linker_Objects
.Table
(Linker_Objects
.Last
) :=
634 -- If host object file, record object file
635 -- e.g. accept foo.o as well as foo.obj on VMS target
637 elsif Arg'Length > Get_Object_Suffix.all'Length
639 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
640 = Get_Object_Suffix.all
642 Linker_Objects.Increment_Last;
643 Linker_Objects.Table (Linker_Objects.Last) :=
646 -- If corresponding ali file exists, capture it
648 elsif Ali_File_Name
= null
649 and then Is_Regular_File
(Arg
& ".ali")
651 Ali_File_Name
:= new String'(Arg & ".ali");
653 -- Otherwise assume this is a linker options entry, but
654 -- see below for interesting adjustment to this assumption.
657 Linker_Options.Increment_Last;
658 Linker_Options.Table (Linker_Options.Last) :=
664 Next_Arg
:= Next_Arg
+ 1;
667 -- If Ada bind file, then compile it with warnings suppressed, because
668 -- otherwise the with of the main program may cause junk warnings.
670 if Ada_Bind_File
then
671 Binder_Options
.Increment_Last
;
672 Binder_Options
.Table
(Binder_Options
.Last
) := new String'("-gnatws");
675 -- If we did not get an ali file at all, and we had at least one
676 -- linker option, then assume that was the intended ali file after
677 -- all, so that we get a nicer message later on.
679 if Ali_File_Name = null
680 and then Linker_Options.Last >= Linker_Options.First
683 new String'(Linker_Options
.Table
(Linker_Options
.First
).all &
688 -------------------------
689 -- Process_Binder_File --
690 -------------------------
692 procedure Process_Binder_File
(Name
: String) is
694 -- Binder file's descriptor
696 Link_Bytes
: Integer := 0;
697 -- Projected number of bytes for the linker command line
700 pragma Import
(C
, Link_Max
, "__gnat_link_max");
701 -- Maximum number of bytes on the command line supported by the OS
702 -- linker. Passed this limit the response file mechanism must be used
705 Next_Line
: String (1 .. 1000);
706 -- Current line value
710 -- Current line slice (the slice does not contain line terminator)
713 -- Current line last character for shared libraries (without version)
715 Objs_Begin
: Integer := 0;
716 -- First object file index in Linker_Objects table
718 Objs_End
: Integer := 0;
719 -- Last object file index in Linker_Objects table
722 pragma Warnings
(Off
, Status
);
723 -- Used for various Interfaces.C_Streams calls
725 Closing_Status
: Boolean;
726 pragma Warnings
(Off
, Closing_Status
);
729 GNAT_Static
: Boolean := False;
730 -- Save state of -static option
732 GNAT_Shared
: Boolean := False;
733 -- Save state of -shared option
735 Xlinker_Was_Previous
: Boolean := False;
736 -- Indicate that "-Xlinker" was the option preceding the current
737 -- option. If True, then the current option is never suppressed.
741 -- These data items are used to store current binder file context.
742 -- The context is composed of the file descriptor position and the
743 -- current line together with the slice indexes (first and last
744 -- position) for this line. The rollback data are used by the
745 -- Store_File_Context and Rollback_File_Context routines below.
746 -- The file context mechanism interact only with the Get_Next_Line
747 -- call. For example:
749 -- Store_File_Context;
751 -- Rollback_File_Context;
754 -- Both Get_Next_Line calls above will read the exact same data from
755 -- the file. In other words, Next_Line, Nfirst and Nlast variables
756 -- will be set with the exact same values.
758 RB_File_Pos
: long
; -- File position
759 RB_Next_Line
: String (1 .. 1000); -- Current line content
760 RB_Nlast
: Integer; -- Slice last index
761 RB_Nfirst
: Integer; -- Slice first index
763 Run_Path_Option_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
764 pragma Import
(C
, Run_Path_Option_Ptr
, "__gnat_run_path_option");
765 -- Pointer to string representing the native linker option which
766 -- specifies the path where the dynamic loader should find shared
767 -- libraries. Equal to null string if this system doesn't support it.
769 Libgcc_Subdir_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
770 pragma Import
(C
, Libgcc_Subdir_Ptr
, "__gnat_default_libgcc_subdir");
771 -- Pointer to string indicating the installation subdirectory where
772 -- a default shared libgcc might be found.
774 Object_Library_Ext_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
776 (C
, Object_Library_Ext_Ptr
, "__gnat_object_library_extension");
777 -- Pointer to string specifying the default extension for
778 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
780 Object_File_Option_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
781 pragma Import
(C
, Object_File_Option_Ptr
, "__gnat_object_file_option");
782 -- Pointer to a string representing the linker option which specifies
783 -- the response file.
785 Using_GNU_Linker
: Boolean;
786 for Using_GNU_Linker
'Size use Character'Size;
787 pragma Import
(C
, Using_GNU_Linker
, "__gnat_using_gnu_linker");
788 -- Predicate indicating whether this target uses the GNU linker. In
789 -- this case we must output a GNU linker compatible response file.
791 Separate_Run_Path_Options
: Boolean;
792 for Separate_Run_Path_Options
'Size use Character'Size;
794 (C
, Separate_Run_Path_Options
, "__gnat_separate_run_path_options");
795 -- Whether separate rpath options should be emitted for each directory
797 Opening
: aliased constant String := """";
798 Closing
: aliased constant String := '"' & ASCII
.LF
;
799 -- Needed to quote object paths in object list files when GNU linker
802 procedure Get_Next_Line
;
803 -- Read the next line from the binder file without the line
806 function Index
(S
, Pattern
: String) return Natural;
807 -- Return the last occurrence of Pattern in S, or 0 if none
809 function Is_Option_Present
(Opt
: String) return Boolean;
810 -- Return true if the option Opt is already present in
811 -- Linker_Options table.
813 procedure Store_File_Context
;
814 -- Store current file context, Fd position and current line data.
815 -- The file context is stored into the rollback data above (RB_*).
816 -- Store_File_Context can be called at any time, only the last call
817 -- will be used (i.e. this routine overwrites the file context).
819 procedure Rollback_File_Context
;
820 -- Restore file context from rollback data. This routine must be called
821 -- after Store_File_Context. The binder file context will be restored
822 -- with the data stored by the last Store_File_Context call.
828 procedure Get_Next_Line
is
832 Fchars
:= fgets
(Next_Line
'Address, Next_Line
'Length, Fd
);
834 if Fchars
= System
.Null_Address
then
835 Exit_With_Error
("Error reading binder output");
838 Nfirst
:= Next_Line
'First;
840 while Nlast
<= Next_Line
'Last
841 and then Next_Line
(Nlast
) /= ASCII
.LF
842 and then Next_Line
(Nlast
) /= ASCII
.CR
854 function Index
(S
, Pattern
: String) return Natural is
855 Len
: constant Natural := Pattern
'Length;
858 for J
in reverse S
'First .. S
'Last - Len
+ 1 loop
859 if Pattern
= S
(J
.. J
+ Len
- 1) then
867 -----------------------
868 -- Is_Option_Present --
869 -----------------------
871 function Is_Option_Present
(Opt
: String) return Boolean is
873 for I
in 1 .. Linker_Options
.Last
loop
875 if Linker_Options
.Table
(I
).all = Opt
then
882 end Is_Option_Present
;
884 ---------------------------
885 -- Rollback_File_Context --
886 ---------------------------
888 procedure Rollback_File_Context
is
890 Next_Line
:= RB_Next_Line
;
893 Status
:= fseek
(Fd
, RB_File_Pos
, Interfaces
.C_Streams
.SEEK_SET
);
896 Exit_With_Error
("Error setting file position");
898 end Rollback_File_Context
;
900 ------------------------
901 -- Store_File_Context --
902 ------------------------
904 procedure Store_File_Context
is
905 use type System
.CRTL
.long
;
907 RB_Next_Line
:= Next_Line
;
910 RB_File_Pos
:= ftell
(Fd
);
912 if RB_File_Pos
= -1 then
913 Exit_With_Error
("Error getting file position");
915 end Store_File_Context
;
917 -- Start of processing for Process_Binder_File
920 Fd
:= fopen
(Name
'Address, Read_Mode
'Address);
922 if Fd
= NULL_Stream
then
923 Exit_With_Error
("Failed to open binder output");
926 -- Skip up to the Begin Info line
930 exit when Next_Line
(Nfirst
.. Nlast
) = Begin_Info
;
936 -- Go to end when end line is reached (this will happen in
937 -- High_Integrity_Mode where no -L switches are generated)
939 exit when Next_Line
(Nfirst
.. Nlast
) = End_Info
;
941 if Ada_Bind_File
then
942 Next_Line
(Nfirst
.. Nlast
- 8) :=
943 Next_Line
(Nfirst
+ 8 .. Nlast
);
947 -- Go to next section when switches are reached
949 exit when Next_Line
(1) = '-';
951 -- Otherwise we have another object file to collect
953 Linker_Objects
.Increment_Last
;
955 -- Mark the positions of first and last object files in case
956 -- they need to be placed with a named file on systems having
957 -- linker line limitations.
959 if Objs_Begin
= 0 then
960 Objs_Begin
:= Linker_Objects
.Last
;
963 Linker_Objects
.Table
(Linker_Objects
.Last
) :=
964 new String'(Next_Line (Nfirst .. Nlast));
966 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
967 -- Nlast - Nfirst + 1, for the size, plus one for the space between
971 Objs_End := Linker_Objects.Last;
973 -- Continue to compute the Link_Bytes, the linker options are part of
974 -- command line length.
978 while Next_Line (Nfirst .. Nlast) /= End_Info loop
979 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
983 Rollback_File_Context;
985 -- On systems that have limitations on handling very long linker lines
986 -- we make use of the system linker option which takes a list of object
987 -- file names from a file instead of the command line itself. What we do
988 -- is to replace the list of object files by the special linker option
989 -- which then reads the object file list from a file instead. The option
990 -- to read from a file instead of the command line is only triggered if
991 -- a conservative threshold is passed.
993 if VM_Target = CLI_Target
994 and then Link_Bytes > Link_Max
996 Lname := new String'("l~" & Base_Name
(Ali_File_Name
.all) & ".il");
998 for J
in Objs_Begin
.. Objs_End
loop
999 Copy_File
(Linker_Objects
.Table
(J
).all, Lname
.all,
1000 Success
=> Closing_Status
,
1004 -- Add the special objects list file option together with the name
1005 -- of the temporary file to the objects file table.
1007 Linker_Objects
.Table
(Objs_Begin
) :=
1008 new String'(Value (Object_File_Option_Ptr) & Lname.all);
1010 -- The slots containing these object file names are then removed
1011 -- from the objects table so they do not appear in the link. They
1012 -- are removed by moving up the linker options and non-Ada object
1013 -- files appearing after the Ada object list in the table.
1019 N := Objs_End - Objs_Begin + 1;
1021 for J in Objs_End + 1 .. Linker_Objects.Last loop
1022 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
1025 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
1028 elsif Object_List_File_Required
1029 or else (Object_List_File_Supported
1030 and then Link_Bytes > Link_Max)
1032 -- Create a temporary file containing the Ada user object files
1033 -- needed by the link. This list is taken from the bind file
1034 -- and is output one object per line for maximal compatibility with
1035 -- linkers supporting this option.
1037 Create_Temp_File (Tname_FD, Tname);
1039 -- ??? File descriptor should be checked to not be Invalid_FD.
1040 -- ??? Status of Write and Close operations should be checked, and
1041 -- failure should occur if a status is wrong.
1043 -- If target is using the GNU linker we must add a special header
1044 -- and footer in the response file.
1046 -- The syntax is : INPUT (object1.o object2.o ... )
1048 -- Because the GNU linker does not like name with characters such
1049 -- as '!', we must put the object paths between double quotes.
1051 if Using_GNU_Linker then
1053 GNU_Header : aliased constant String := "INPUT (";
1056 Status := Write (Tname_FD, GNU_Header'Address,
1061 for J in Objs_Begin .. Objs_End loop
1063 -- Opening quote for GNU linker
1065 if Using_GNU_Linker then
1066 Status := Write (Tname_FD, Opening'Address, 1);
1069 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
1070 Linker_Objects.Table (J).all'Length);
1072 -- Closing quote for GNU linker
1074 if Using_GNU_Linker then
1075 Status := Write (Tname_FD, Closing'Address, 2);
1078 Status := Write (Tname_FD, ASCII.LF'Address, 1);
1081 Response_File_Objects.Increment_Last;
1082 Response_File_Objects.Table (Response_File_Objects.Last) :=
1083 Linker_Objects.Table (J);
1086 -- Handle GNU linker response file footer
1088 if Using_GNU_Linker then
1090 GNU_Footer : aliased constant String := ")";
1093 Status := Write (Tname_FD, GNU_Footer'Address,
1098 Close (Tname_FD, Closing_Status);
1100 -- Add the special objects list file option together with the name
1101 -- of the temporary file (removing the null character) to the objects
1104 Linker_Objects.Table (Objs_Begin) :=
1105 new String'(Value
(Object_File_Option_Ptr
) &
1106 Tname
(Tname
'First .. Tname
'Last - 1));
1108 -- The slots containing these object file names are then removed
1109 -- from the objects table so they do not appear in the link. They
1110 -- are removed by moving up the linker options and non-Ada object
1111 -- files appearing after the Ada object list in the table.
1117 N
:= Objs_End
- Objs_Begin
+ 1;
1119 for J
in Objs_End
+ 1 .. Linker_Objects
.Last
loop
1120 Linker_Objects
.Table
(J
- N
+ 1) := Linker_Objects
.Table
(J
);
1123 Linker_Objects
.Set_Last
(Linker_Objects
.Last
- N
+ 1);
1127 -- Process switches and options
1129 if Next_Line
(Nfirst
.. Nlast
) /= End_Info
then
1130 Xlinker_Was_Previous
:= False;
1133 if Xlinker_Was_Previous
1134 or else Next_Line
(Nfirst
.. Nlast
) = "-Xlinker"
1136 Linker_Options
.Increment_Last
;
1137 Linker_Options
.Table
(Linker_Options
.Last
) :=
1138 new String'(Next_Line (Nfirst .. Nlast));
1140 elsif Next_Line (Nfirst .. Nlast) = "-static" then
1141 GNAT_Static := True;
1143 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1144 GNAT_Shared := True;
1146 -- Add binder options only if not already set on the command
1147 -- line. This rule is a way to control the linker options order.
1149 -- The following test needs comments, why is it VMS specific.
1150 -- The above comment looks out of date ???
1152 elsif not (OpenVMS_On_Target
1154 Is_Option_Present (Next_Line (Nfirst .. Nlast)))
1156 if Nlast > Nfirst + 2 and then
1157 Next_Line (Nfirst .. Nfirst + 1) = "-L"
1159 -- Construct a library search path for use later
1160 -- to locate static gnatlib libraries.
1162 if Libpath.Last > 1 then
1163 Libpath.Increment_Last;
1164 Libpath.Table (Libpath.Last) := Path_Separator;
1167 for I in Nfirst + 2 .. Nlast loop
1168 Libpath.Increment_Last;
1169 Libpath.Table (Libpath.Last) := Next_Line (I);
1172 Linker_Options.Increment_Last;
1174 Linker_Options.Table (Linker_Options.Last) :=
1175 new String'(Next_Line
(Nfirst
.. Nlast
));
1177 elsif Next_Line
(Nfirst
.. Nlast
) = "-ldecgnat"
1178 or else Next_Line
(Nfirst
.. Nlast
) = "-lgnarl"
1179 or else Next_Line
(Nfirst
.. Nlast
) = "-lgnat"
1181 (1 .. Natural'Min (Nlast
, 8 + Library_Version
'Length)) =
1182 Shared_Lib
("gnarl")
1184 (1 .. Natural'Min (Nlast
, 7 + Library_Version
'Length)) =
1187 -- If it is a shared library, remove the library version.
1188 -- We will be looking for the static version of the library
1189 -- as it is in the same directory as the shared version.
1191 if Next_Line
(Nlast
- Library_Version
'Length + 1 .. Nlast
)
1194 -- Set Last to point to last character before the
1197 Last
:= Nlast
- Library_Version
'Length - 1;
1202 -- Given a Gnat standard library, search the library path to
1203 -- find the library location.
1205 -- Shouldn't we abstract a proc here, we are getting awfully
1206 -- heavily nested ???
1209 File_Path
: String_Access
;
1211 Object_Lib_Extension
: constant String :=
1212 Value
(Object_Library_Ext_Ptr
);
1214 File_Name
: constant String := "lib" &
1215 Next_Line
(Nfirst
+ 2 .. Last
) &
1216 Object_Lib_Extension
;
1218 Run_Path_Opt
: constant String :=
1219 Value
(Run_Path_Option_Ptr
);
1221 GCC_Index
: Natural;
1222 Run_Path_Opt_Index
: Natural := 0;
1226 Locate_Regular_File
(File_Name
,
1227 String (Libpath
.Table
(1 .. Libpath
.Last
)));
1229 if File_Path
/= null then
1232 -- If static gnatlib found, explicitly
1233 -- specify to overcome possible linker
1234 -- default usage of shared version.
1236 Linker_Options
.Increment_Last
;
1238 Linker_Options
.Table
(Linker_Options
.Last
) :=
1239 new String'(File_Path.all);
1241 elsif GNAT_Shared then
1242 if Opt.Run_Path_Option then
1244 -- If shared gnatlib desired, add the
1245 -- appropriate system specific switch
1246 -- so that it can be located at runtime.
1248 if Run_Path_Opt'Length /= 0 then
1250 -- Output the system specific linker command
1251 -- that allows the image activator to find
1252 -- the shared library at runtime. Also add
1253 -- path to find libgcc_s.so, if relevant.
1256 Path : String (1 .. File_Path'Length + 15);
1257 Path_Last : constant Natural :=
1261 Path (1 .. File_Path'Length) :=
1264 -- To find the location of the shared version
1265 -- of libgcc, we look for "gcc-lib" in the
1266 -- path of the library. However, this
1267 -- subdirectory is no longer present in
1268 -- recent versions of GCC. So, we look for
1269 -- the last subdirectory "lib" in the path.
1272 Index (Path (1 .. Path_Last), "gcc-lib");
1274 if GCC_Index /= 0 then
1276 -- The shared version of libgcc is
1277 -- located in the parent directory.
1279 GCC_Index := GCC_Index - 1;
1284 (Path (1 .. Path_Last),
1287 if GCC_Index = 0 then
1289 Index (Path (1 .. Path_Last),
1290 Directory_Separator &
1292 Directory_Separator);
1295 -- If we have found a "lib" subdir in
1296 -- the path to libgnat, the possible
1297 -- shared libgcc of interest by default
1298 -- is in libgcc_subdir at the same
1301 if GCC_Index /= 0 then
1303 Subdir : constant String :=
1304 Value (Libgcc_Subdir_Ptr);
1308 GCC_Index + Subdir'Length) :=
1311 GCC_Index + Subdir'Length;
1316 -- Look for an eventual run_path_option in
1317 -- the linker switches.
1319 if Separate_Run_Path_Options then
1320 Linker_Options.Increment_Last;
1321 Linker_Options.Table
1322 (Linker_Options.Last) :=
1326 (1 .. File_Path
'Length
1327 - File_Name
'Length));
1329 if GCC_Index
/= 0 then
1330 Linker_Options
.Increment_Last
;
1331 Linker_Options
.Table
1332 (Linker_Options
.Last
) :=
1335 & Path (1 .. GCC_Index));
1340 1 .. Linker_Options.Last
1342 if Linker_Options.Table (J) /= null
1344 Linker_Options.Table (J)'Length
1345 > Run_Path_Opt'Length
1347 Linker_Options.Table (J)
1348 (1 .. Run_Path_Opt'Length) =
1351 -- We have found an already
1352 -- specified run_path_option: we
1353 -- will add to this switch,
1355 -- run_path_option should be
1358 Run_Path_Opt_Index := J;
1363 -- If there is no run_path_option, we
1366 if Run_Path_Opt_Index = 0 then
1367 Linker_Options.Increment_Last;
1370 if GCC_Index = 0 then
1371 if Run_Path_Opt_Index = 0 then
1372 Linker_Options.Table
1373 (Linker_Options.Last) :=
1377 (1 .. File_Path
'Length
1378 - File_Name
'Length));
1381 Linker_Options
.Table
1382 (Run_Path_Opt_Index
) :=
1384 (Linker_Options.Table
1385 (Run_Path_Opt_Index).all
1388 (1 .. File_Path'Length
1389 - File_Name'Length));
1393 if Run_Path_Opt_Index = 0 then
1394 Linker_Options.Table
1395 (Linker_Options.Last) :=
1399 (1 .. File_Path
'Length
1402 & Path
(1 .. GCC_Index
));
1405 Linker_Options
.Table
1406 (Run_Path_Opt_Index
) :=
1408 (Linker_Options.Table
1409 (Run_Path_Opt_Index).all
1412 (1 .. File_Path'Length
1415 & Path (1 .. GCC_Index));
1423 -- Then we add the appropriate -l switch
1425 Linker_Options.Increment_Last;
1426 Linker_Options.Table (Linker_Options.Last) :=
1427 new String'(Next_Line
(Nfirst
.. Nlast
));
1431 -- If gnatlib library not found, then
1432 -- add it anyway in case some other
1433 -- mechanism may find it.
1435 Linker_Options
.Increment_Last
;
1436 Linker_Options
.Table
(Linker_Options
.Last
) :=
1437 new String'(Next_Line (Nfirst .. Nlast));
1441 Linker_Options.Increment_Last;
1442 Linker_Options.Table (Linker_Options.Last) :=
1443 new String'(Next_Line
(Nfirst
.. Nlast
));
1447 Xlinker_Was_Previous
:= Next_Line
(Nfirst
.. Nlast
) = "-Xlinker";
1450 exit when Next_Line
(Nfirst
.. Nlast
) = End_Info
;
1452 if Ada_Bind_File
then
1453 Next_Line
(Nfirst
.. Nlast
- 8) :=
1454 Next_Line
(Nfirst
+ 8 .. Nlast
);
1460 -- If -shared was specified, invoke gcc with -shared-libgcc
1463 Linker_Options
.Increment_Last
;
1464 Linker_Options
.Table
(Linker_Options
.Last
) := Shared_Libgcc
;
1467 Status
:= fclose
(Fd
);
1468 end Process_Binder_File
;
1476 Write_Str
("Usage: ");
1477 Write_Str
(Base_Name
(Command_Name
));
1478 Write_Str
(" switches mainprog.ali [non-Ada-objects] [linker-options]");
1481 Write_Line
(" mainprog.ali the ALI file of the main program");
1483 Write_Line
(" -f force object file list to be generated");
1484 Write_Line
(" -g Compile binder source file with debug information");
1485 Write_Line
(" -n Do not compile the binder source file");
1486 Write_Line
(" -R Do not use a run_path_option");
1487 Write_Line
(" -v verbose mode");
1488 Write_Line
(" -v -v very verbose mode");
1490 Write_Line
(" -o nam Use 'nam' as the name of the executable");
1491 Write_Line
(" -b target Compile the binder source to run on target");
1492 Write_Line
(" -Bdir Load compiler executables from dir");
1494 if Is_Supported
(Map_File
) then
1495 Write_Line
(" -Mmap Create map file map");
1496 Write_Line
(" -M Create map file mainprog.map");
1499 Write_Line
(" --GCC=comp Use comp as the compiler");
1500 Write_Line
(" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1502 Write_Line
(" [non-Ada-objects] list of non Ada object files");
1503 Write_Line
(" [linker-options] other options for the linker");
1510 procedure Write_Header
is
1512 if Verbose_Mode
then
1514 Display_Version
("GNATLINK", "1995");
1522 procedure Write_Usage
is
1528 -- Start of processing for Gnatlink
1531 -- Add the directory where gnatlink is invoked in front of the path, if
1532 -- gnatlink is invoked with directory information. Only do this if the
1533 -- platform is not VMS, where the notion of path does not really exist.
1535 if not Hostparm
.OpenVMS
then
1537 Command
: constant String := Command_Name
;
1540 for Index
in reverse Command
'Range loop
1541 if Command
(Index
) = Directory_Separator
then
1543 Absolute_Dir
: constant String :=
1545 (Command
(Command
'First .. Index
));
1547 PATH
: constant String :=
1550 Getenv
("PATH").all;
1553 Setenv
("PATH", PATH
);
1564 if Argument_Count
= 0
1565 or else (Verbose_Mode
and then Argument_Count
= 1)
1568 Exit_Program
(E_Fatal
);
1571 -- Initialize packages to be used
1576 -- We always compile with -c
1578 Binder_Options_From_ALI
.Increment_Last
;
1579 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1582 if Ali_File_Name = null then
1583 Exit_With_Error ("no ali file given for link");
1586 if not Is_Regular_File (Ali_File_Name.all) then
1587 Exit_With_Error (Ali_File_Name.all & " not found");
1590 -- Read the ALI file of the main subprogram if the binder generated file
1591 -- needs to be compiled and no --GCC= switch has been specified. Fetch the
1592 -- back end switches from this ALI file and use these switches to compile
1593 -- the binder generated file
1595 if Compile_Bind_File and then Standard_Gcc then
1597 Name_Len := Ali_File_Name'Length;
1598 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1602 F : constant File_Name_Type := Name_Find;
1603 T : Text_Buffer_Ptr;
1607 -- Load the ALI file
1609 T := Read_Library_Info (F, True);
1611 -- Read it. Note that we ignore errors, since we only want very
1612 -- limited information from the ali file, and likely a slightly
1613 -- wrong version will be just fine, though in normal operation
1614 -- we don't expect this to happen!
1621 Ignore_Errors => True);
1623 if A /= No_ALI_Id then
1625 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1626 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1628 -- Do not compile with the front end switches. However, --RTS
1629 -- is to be dealt with specially because it needs to be passed
1630 -- if the binder-generated file is in Ada and may also be used
1631 -- to drive the linker.
1634 Arg : String_Ptr renames Args.Table (Index);
1636 if not Is_Front_End_Switch (Arg.all) then
1637 Binder_Options_From_ALI.Increment_Last;
1638 Binder_Options_From_ALI.Table
1639 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1641 elsif Arg'Length > 5
1642 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1644 if Ada_Bind_File then
1645 Binder_Options_From_ALI.Increment_Last;
1646 Binder_Options_From_ALI.Table
1647 (Binder_Options_From_ALI.Last)
1648 := String_Access (Arg);
1651 -- Set the RTS_*_Path_Name variables, so that
1652 -- the correct directories will be set when
1653 -- Osint.Add_Default_Search_Dirs will be called later.
1655 Opt.RTS_Src_Path_Name :=
1657 (Arg (Arg'First + 6 .. Arg'Last), Include);
1659 Opt.RTS_Lib_Path_Name :=
1661 (Arg (Arg'First + 6 .. Arg'Last), Objects);
1663 -- GNAT doesn't support the GCC multilib mechanism.
1664 -- This means that, when a multilib switch is used
1665 -- to request a particular compilation mode, the
1666 -- corresponding runtime switch (--RTS) must also be
1667 -- specified. The long-term goal is to fully support the
1668 -- multilib mechanism; however, in the meantime, it is
1669 -- convenient to eliminate the redundancy by keying the
1670 -- compilation mode on a single switch, namely --RTS.
1672 -- Pass -mrtp to the linker if --RTS=rtp was passed
1675 and then Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1677 Linker_Options.Increment_Last;
1678 Linker_Options.Table (Linker_Options.Last) :=
1679 new String'("-mrtp");
1688 -- Get target parameters
1690 Osint
.Add_Default_Search_Dirs
;
1691 Targparm
.Get_Target_Parameters
;
1693 if VM_Target
/= No_VM
then
1695 when JVM_Target
=> Gcc
:= new String'("jvm-gnatcompile");
1696 when CLI_Target => Gcc := new String'("dotnet-gnatcompile");
1697 when No_VM
=> raise Program_Error
;
1700 Ada_Bind_File
:= True;
1701 Begin_Info
:= "-- BEGIN Object file/option list";
1702 End_Info
:= "-- END Object file/option list ";
1705 -- If the main program is in Ada it is compiled with the following
1708 -- -gnatA stops reading gnat.adc, since we don't know what
1709 -- pragmas would work, and we do not need it anyway.
1711 -- -gnatWb allows brackets coding for wide characters
1713 -- -gnatiw allows wide characters in identifiers. This is needed
1714 -- because bindgen uses brackets encoding for all upper
1715 -- half and wide characters in identifier names.
1717 if Ada_Bind_File
then
1718 Binder_Options_From_ALI
.Increment_Last
;
1719 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1720 new String'("-gnatA");
1721 Binder_Options_From_ALI.Increment_Last;
1722 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1723 new String'("-gnatWb");
1724 Binder_Options_From_ALI
.Increment_Last
;
1725 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1726 new String'("-gnatiw");
1729 -- Locate all the necessary programs and verify required files are present
1731 Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1733 if Gcc_Path = null then
1734 Exit_With_Error ("Couldn't locate " & Gcc.all);
1737 if Linker_Path = null then
1738 if VM_Target = CLI_Target then
1739 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("ilasm");
1741 if Linker_Path = null then
1742 Exit_With_Error ("Couldn't locate ilasm");
1745 elsif RTX_RTSS_Kernel_Module_On_Target then
1747 -- Use Microsoft linker for RTSS modules
1749 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("link");
1751 if Linker_Path = null then
1752 Exit_With_Error ("Couldn't locate link");
1756 Linker_Path := Gcc_Path;
1762 -- If no output name specified, then use the base name of .ali file name
1764 if Output_File_Name = null then
1766 new String'(Base_Name
(Ali_File_Name
.all)
1767 & Get_Target_Debuggable_Suffix
.all);
1770 if VM_Target
= CLI_Target
then
1771 Linker_Options
.Increment_Last
;
1772 Linker_Options
.Table
(Linker_Options
.Last
) := new String'("/QUIET");
1774 Linker_Options.Increment_Last;
1775 Linker_Options.Table (Linker_Options.Last) := new String'("/DEBUG");
1777 Linker_Options
.Increment_Last
;
1778 Linker_Options
.Table
(Linker_Options
.Last
) :=
1779 new String'("/OUTPUT=" & Output_File_Name.all);
1781 elsif RTX_RTSS_Kernel_Module_On_Target then
1782 Linker_Options.Increment_Last;
1783 Linker_Options.Table (Linker_Options.Last) :=
1784 new String'("/OUT:" & Output_File_Name
.all);
1787 Linker_Options
.Increment_Last
;
1788 Linker_Options
.Table
(Linker_Options
.Last
) := new String'("-o");
1790 Linker_Options.Increment_Last;
1791 Linker_Options.Table (Linker_Options.Last) :=
1792 new String'(Output_File_Name
.all);
1795 Check_Existing_Executable
(Output_File_Name
.all);
1797 -- Warn if main program is called "test", as that may be a built-in command
1798 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1799 -- will not appear. However, do not warn in the case of a cross compiler.
1801 -- Assume this is a cross tool if the executable name is not gnatlink
1803 if Base_Name
(Command_Name
) = "gnatlink"
1804 and then Output_File_Name
.all = "test"
1806 Error_Msg
("warning: executable name """ & Output_File_Name
.all
1807 & """ may conflict with shell command");
1810 -- If -M switch was specified, add the switches to create the map file
1812 if Create_Map_File
then
1814 Map_Name
: constant String := Base_Name
(Ali_File_Name
.all) & ".map";
1815 Switches
: String_List_Access
;
1818 Convert
(Map_File
, Map_Name
, Switches
);
1820 if Switches
/= null then
1821 for J
in Switches
'Range loop
1822 Linker_Options
.Increment_Last
;
1823 Linker_Options
.Table
(Linker_Options
.Last
) := Switches
(J
);
1829 -- Perform consistency checks
1831 -- Transform the .ali file name into the binder output file name
1833 Make_Binder_File_Names
: declare
1834 Fname
: constant String := Base_Name
(Ali_File_Name
.all);
1835 Fname_Len
: Integer := Fname
'Length;
1837 function Get_Maximum_File_Name_Length
return Integer;
1838 pragma Import
(C
, Get_Maximum_File_Name_Length
,
1839 "__gnat_get_maximum_file_name_length");
1841 Maximum_File_Name_Length
: constant Integer :=
1842 Get_Maximum_File_Name_Length
;
1844 Bind_File_Prefix
: Types
.String_Ptr
;
1845 -- Contains prefix used for bind files
1850 if not Ada_Bind_File
then
1851 Bind_File_Prefix
:= new String'("b_");
1852 elsif OpenVMS_On_Target then
1853 Bind_File_Prefix := new String'("b__");
1855 Bind_File_Prefix
:= new String'("b~");
1858 -- If the length of the binder file becomes too long due to
1859 -- the addition of the "b?" prefix, then truncate it.
1861 if Maximum_File_Name_Length > 0 then
1863 Maximum_File_Name_Length - Bind_File_Prefix.all'Length
1865 Fname_Len := Fname_Len - 1;
1870 Fnam : constant String :=
1871 Bind_File_Prefix.all &
1872 Fname (Fname'First .. Fname'First + Fname_Len - 1);
1875 if Ada_Bind_File then
1876 Binder_Spec_Src_File := new String'(Fnam
& ".ads");
1877 Binder_Body_Src_File
:= new String'(Fnam & ".adb");
1878 Binder_Ali_File := new String'(Fnam
& ".ali");
1880 Binder_Body_Src_File
:= new String'(Fnam & ".c");
1883 Binder_Obj_File := new String'(Fnam
& Get_Target_Object_Suffix
.all);
1886 if Fname_Len
/= Fname
'Length then
1887 Binder_Options
.Increment_Last
;
1888 Binder_Options
.Table
(Binder_Options
.Last
) := new String'("-o");
1889 Binder_Options.Increment_Last;
1890 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1892 end Make_Binder_File_Names;
1894 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1896 -- Compile the binder file. This is fast, so we always do it, unless
1897 -- specifically told not to by the -n switch
1899 if Compile_Bind_File then
1902 Args : Argument_List
1903 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1906 for J in 1 .. Binder_Options_From_ALI.Last loop
1907 Args (J) := Binder_Options_From_ALI.Table (J);
1910 for J in 1 .. Binder_Options.Last loop
1911 Args (Binder_Options_From_ALI.Last + J) :=
1912 Binder_Options.Table (J);
1915 -- Use the full path of the binder generated source, so that it is
1916 -- guaranteed that the debugger will find this source, even with
1920 new String'(Normalize_Pathname
(Binder_Body_Src_File
.all));
1922 if Verbose_Mode
then
1923 Write_Str
(Base_Name
(Gcc_Path
.all));
1925 for J
in Args
'Range loop
1927 Write_Str
(Args
(J
).all);
1933 System
.OS_Lib
.Spawn
(Gcc_Path
.all, Args
, Success
);
1936 Exit_Program
(E_Fatal
);
1941 -- Now, actually link the program
1943 -- Skip this step for now on JVM since the Java interpreter will do
1944 -- the actual link at run time. We might consider packing all class files
1945 -- in a .zip file during this step.
1947 if VM_Target
/= JVM_Target
then
1949 Num_Args
: Natural :=
1950 (Linker_Options
.Last
- Linker_Options
.First
+ 1) +
1951 (Gcc_Linker_Options
.Last
- Gcc_Linker_Options
.First
+ 1) +
1952 (Linker_Objects
.Last
- Linker_Objects
.First
+ 1);
1953 Stack_Op
: Boolean := False;
1954 IDENT_Op
: Boolean := False;
1957 if VM_Target
= CLI_Target
then
1959 -- Remove extraneous flags not relevant for CIL. Also remove empty
1960 -- arguments, since ilasm chokes on them.
1962 for J
in reverse Linker_Options
.First
.. Linker_Options
.Last
loop
1963 if Linker_Options
.Table
(J
)'Length = 0
1964 or else Linker_Options
.Table
(J
) (1 .. 2) = "-L"
1965 or else Linker_Options
.Table
(J
) (1 .. 2) = "-l"
1966 or else Linker_Options
.Table
(J
) (1 .. 3) = "-Wl"
1967 or else Linker_Options
.Table
(J
) (1 .. 3) = "-sh"
1968 or else Linker_Options
.Table
(J
) (1 .. 2) = "-g"
1970 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
1971 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
1972 Linker_Options
.Decrement_Last
;
1973 Num_Args
:= Num_Args
- 1;
1977 elsif RTX_RTSS_Kernel_Module_On_Target
then
1979 -- Remove flags not relevant for Microsoft linker and adapt some
1982 for J
in reverse Linker_Options
.First
.. Linker_Options
.Last
loop
1984 -- Remove flags that are not accepted
1985 if Linker_Options
.Table
(J
)'Length = 0
1986 or else Linker_Options
.Table
(J
) (1 .. 2) = "-l"
1987 or else Linker_Options
.Table
(J
) (1 .. 3) = "-Wl"
1988 or else Linker_Options
.Table
(J
) (1 .. 3) = "-sh"
1989 or else Linker_Options
.Table
(J
) (1 .. 8) = "-Xlinker"
1990 or else Linker_Options
.Table
(J
) (1 .. 9) = "-mthreads"
1992 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
1993 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
1994 Linker_Options
.Decrement_Last
;
1995 Num_Args
:= Num_Args
- 1;
1997 -- Replace "-L" by its counterpart "/LIBPATH:" and UNIX "/" by
1999 elsif Linker_Options
.Table
(J
) (1 .. 2) = "-L" then
2001 Libpath_Option
: constant String_Access
:= new String'
2003 Linker_Options.Table (J)
2004 (3 .. Linker_Options.Table (J).all'Last));
2006 for Index in 10 .. Libpath_Option'Last loop
2007 if Libpath_Option (Index) = '/' then
2008 Libpath_Option (Index) := '\
';
2012 Linker_Options.Table (J) := Libpath_Option;
2015 -- Replace "-g" by "/DEBUG"
2016 elsif Linker_Options.Table (J) (1 .. 2) = "-g" then
2017 Linker_Options.Table (J) := new String'("/DEBUG");
2019 -- Replace "-o" by "/OUT:"
2020 elsif Linker_Options
.Table
(J
) (1 .. 2) = "-o" then
2021 Linker_Options
.Table
(J
+ 1) := new String'
2022 ("/OUT:" & Linker_Options.Table (J + 1).all);
2024 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2025 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2026 Linker_Options.Decrement_Last;
2027 Num_Args := Num_Args - 1;
2029 -- Replace "--stack=" by "/STACK:"
2030 elsif Linker_Options.Table (J) (1 .. 8) = "--stack=" then
2031 Linker_Options.Table (J) := new String'
2033 Linker_Options
.Table
(J
)
2034 (9 .. Linker_Options
.Table
(J
).all'Last));
2036 -- Replace "-v" by its counterpart "/VERBOSE"
2037 elsif Linker_Options
.Table
(J
) (1 .. 2) = "-v" then
2038 Linker_Options
.Table
(J
) := new String'("/VERBOSE");
2042 -- Add some required flags to create RTSS modules
2045 Flags_For_Linker : constant array (1 .. 17) of String_Access :=
2046 (new String'("/NODEFAULTLIB"),
2047 new String'("/INCREMENTAL:NO"),
2048 new String'("/NOLOGO"),
2049 new String'("/DRIVER"),
2050 new String'("/ALIGN:0x20"),
2051 new String'("/SUBSYSTEM:NATIVE"),
2052 new String'("/ENTRY:_RtapiProcessEntryCRT@8"),
2053 new String'("/RELEASE"),
2054 new String'("startupCRT.obj"),
2055 new String'("rtxlibcmt.lib"),
2056 new String'("oldnames.lib"),
2057 new String'("rtapi_rtss.lib"),
2058 new String'("Rtx_Rtss.lib"),
2059 new String'("libkernel32.a"),
2060 new String'("libws2_32.a"),
2061 new String'("libmswsock.a"),
2062 new String'("libadvapi32.a"));
2063 -- These flags need to be passed to Microsoft linker. They
2064 -- come from the RTX documentation.
2066 Gcc_Lib_Path
: constant String_Access
:= new String'
2067 ("/LIBPATH:" & Include_Dir_Default_Prefix & "\..\");
2068 -- Place to look for gcc related libraries, such as libgcc
2071 -- Replace UNIX "/" by Windows "\" in the path
2073 for Index in 10 .. Gcc_Lib_Path.all'Last loop
2074 if Gcc_Lib_Path (Index) = '/' then
2075 Gcc_Lib_Path (Index) := '\
';
2079 Linker_Options.Increment_Last;
2080 Linker_Options.Table (Linker_Options.Last) := Gcc_Lib_Path;
2081 Num_Args := Num_Args + 1;
2083 for Index in Flags_For_Linker'Range loop
2084 Linker_Options.Increment_Last;
2085 Linker_Options.Table (Linker_Options.Last) :=
2086 Flags_For_Linker (Index);
2087 Num_Args := Num_Args + 1;
2092 -- Remove duplicate stack size setting from the Linker_Options
2093 -- table. The stack setting option "-Xlinker --stack=R,C" can be
2094 -- found in one line when set by a pragma Linker_Options or in two
2095 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
2096 -- line. We also check for the "-Wl,--stack=R" style option.
2098 -- We must remove the second stack setting option instance
2099 -- because the one on the command line will always be the first
2100 -- one. And any subsequent stack setting option will overwrite the
2101 -- previous one. This is done especially for GNAT/NT where we set
2102 -- the stack size for tasking programs by a pragma in the NT
2103 -- specific tasking package System.Task_Primitives.Operations.
2105 -- Note: This is not a FOR loop that runs from Linker_Options.First
2106 -- to Linker_Options.Last, since operations within the loop can
2107 -- modify the length of the table.
2109 Clean_Link_Option_Set : declare
2110 J : Natural := Linker_Options.First;
2111 Shared_Libgcc_Seen : Boolean := False;
2114 while J <= Linker_Options.Last loop
2116 if Linker_Options.Table (J).all = "-Xlinker"
2117 and then J < Linker_Options.Last
2118 and then Linker_Options.Table (J + 1)'Length > 8
2119 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
2122 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
2123 Linker_Options.Table (J + 2 .. Linker_Options.Last);
2124 Linker_Options.Decrement_Last;
2125 Linker_Options.Decrement_Last;
2126 Num_Args := Num_Args - 2;
2133 -- Remove duplicate -shared-libgcc switch
2135 if Linker_Options.Table (J).all = Shared_Libgcc_String then
2136 if Shared_Libgcc_Seen then
2137 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2138 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2139 Linker_Options.Decrement_Last;
2140 Num_Args := Num_Args - 1;
2143 Shared_Libgcc_Seen := True;
2147 -- Here we just check for a canonical form that matches the
2148 -- pragma Linker_Options set in the NT runtime.
2150 if (Linker_Options.Table (J)'Length > 17
2151 and then Linker_Options.Table (J) (1 .. 17)
2152 = "-Xlinker --stack=")
2154 (Linker_Options.Table (J)'Length > 12
2155 and then Linker_Options.Table (J) (1 .. 12)
2159 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2160 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2161 Linker_Options.Decrement_Last;
2162 Num_Args := Num_Args - 1;
2169 -- Remove duplicate IDENTIFICATION directives (VMS)
2171 if Linker_Options.Table (J)'Length > 29
2172 and then Linker_Options.Table (J) (1 .. 30) =
2173 "--for-linker=--identification="
2176 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2177 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2178 Linker_Options.Decrement_Last;
2179 Num_Args := Num_Args - 1;
2189 if Linker_Path = Gcc_Path and then VM_Target = No_VM then
2191 -- For systems where the default is to link statically with
2192 -- libgcc, if gcc is not called with -shared-libgcc, call it
2193 -- with -static-libgcc, as there are some platforms where one
2194 -- of these two switches is compulsory to link.
2196 if Shared_Libgcc_Default = 'T
'
2197 and then not Shared_Libgcc_Seen
2199 Linker_Options.Increment_Last;
2200 Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
2201 Num_Args := Num_Args + 1;
2204 elsif RTX_RTSS_Kernel_Module_On_Target then
2206 -- Force the use of the static libgcc for RTSS modules
2208 Linker_Options.Increment_Last;
2209 Linker_Options.Table (Linker_Options.Last) :=
2210 new String'("libgcc.a");
2211 Num_Args
:= Num_Args
+ 1;
2214 end Clean_Link_Option_Set
;
2216 -- Prepare arguments for call to linker
2218 Call_Linker
: declare
2220 Args
: Argument_List
(1 .. Num_Args
+ 1);
2221 Index
: Integer := Args
'First;
2224 Args
(Index
) := Binder_Obj_File
;
2226 -- Add the object files and any -largs libraries
2228 for J
in Linker_Objects
.First
.. Linker_Objects
.Last
loop
2230 Args
(Index
) := Linker_Objects
.Table
(J
);
2233 -- Add the linker options from the binder file
2235 for J
in Linker_Options
.First
.. Linker_Options
.Last
loop
2237 Args
(Index
) := Linker_Options
.Table
(J
);
2240 -- Finally add the libraries from the --GCC= switch
2242 for J
in Gcc_Linker_Options
.First
.. Gcc_Linker_Options
.Last
loop
2244 Args
(Index
) := Gcc_Linker_Options
.Table
(J
);
2247 if Verbose_Mode
then
2248 Write_Str
(Linker_Path
.all);
2250 for J
in Args
'Range loop
2252 Write_Str
(Args
(J
).all);
2257 -- If we are on very verbose mode (-v -v) and a response file
2258 -- is used we display its content.
2260 if Very_Verbose_Mode
and then Tname_FD
/= Invalid_FD
then
2262 Write_Str
("Response file (" &
2263 Tname
(Tname
'First .. Tname
'Last - 1) &
2268 Response_File_Objects
.First
..
2269 Response_File_Objects
.Last
2271 Write_Str
(Response_File_Objects
.Table
(J
).all);
2279 System
.OS_Lib
.Spawn
(Linker_Path
.all, Args
, Success
);
2281 -- Delete the temporary file used in conjunction with linking if
2282 -- one was created. See Process_Bind_File for details.
2284 if Tname_FD
/= Invalid_FD
then
2288 if Lname
/= null then
2289 Delete
(Lname
.all & ASCII
.NUL
);
2293 Error_Msg
("error when calling " & Linker_Path
.all);
2294 Exit_Program
(E_Fatal
);
2300 -- Only keep the binder output file and it's associated object
2301 -- file if compiling with the -g option. These files are only
2302 -- useful if debugging.
2304 if not Debug_Flag_Present
then
2305 if Binder_Ali_File
/= null then
2306 Delete
(Binder_Ali_File
.all & ASCII
.NUL
);
2309 if Binder_Spec_Src_File
/= null then
2310 Delete
(Binder_Spec_Src_File
.all & ASCII
.NUL
);
2313 Delete
(Binder_Body_Src_File
.all & ASCII
.NUL
);
2315 if VM_Target
= No_VM
then
2316 Delete
(Binder_Obj_File
.all & ASCII
.NUL
);
2320 Exit_Program
(E_Success
);
2324 Write_Line
(Exception_Information
(X
));
2325 Exit_With_Error
("INTERNAL ERROR. Please report");