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 Next_Arg := Next_Arg + 1;
444 if Next_Arg > Argument_Count then
445 Exit_With_Error ("Missing argument for -o");
449 new String'(Executable_Name
450 (Argument
(Next_Arg
),
451 Only_If_No_Suffix
=> True));
454 Opt
.Run_Path_Option
:= False;
458 -- Support "double" verbose mode. Second -v
459 -- gets sent to the linker and binder phases.
462 Very_Verbose_Mode
:= True;
464 Linker_Options
.Increment_Last
;
465 Linker_Options
.Table
(Linker_Options
.Last
) :=
468 Binder_Options.Increment_Last;
469 Binder_Options.Table (Binder_Options.Last) :=
470 Linker_Options.Table (Linker_Options.Last);
473 Verbose_Mode := True;
478 Linker_Options.Increment_Last;
479 Linker_Options.Table (Linker_Options.Last) :=
484 elsif Arg
(2) = 'B' then
485 Linker_Options
.Increment_Last
;
486 Linker_Options
.Table
(Linker_Options
.Last
) :=
489 Binder_Options.Increment_Last;
490 Binder_Options.Table (Binder_Options.Last) :=
491 Linker_Options.Table (Linker_Options.Last);
493 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
494 if Arg'Length = 7 then
495 Exit_With_Error ("Missing argument for --LINK=");
499 System.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
501 if Linker_Path = null then
503 ("Could not locate linker: " & Arg (8 .. Arg'Last));
506 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
508 Program_Args : constant Argument_List_Access :=
509 Argument_String_To_List
510 (Arg (7 .. Arg'Last));
513 if Program_Args.all (1).all /= Gcc.all then
514 Gcc := new String'(Program_Args
.all (1).all);
515 Standard_Gcc
:= False;
518 -- Set appropriate flags for switches passed
520 for J
in 2 .. Program_Args
.all'Last loop
522 Arg
: constant String := Program_Args
.all (J
).all;
523 AF
: constant Integer := Arg
'First;
526 if Arg
'Length /= 0 and then Arg
(AF
) = '-' then
527 if Arg
(AF
+ 1) = 'g'
528 and then (Arg
'Length = 2
529 or else Arg
(AF
+ 2) in '0' .. '3'
530 or else Arg
(AF
+ 2 .. Arg
'Last) = "coff")
532 Debug_Flag_Present
:= True;
536 -- Add directory to source search dirs so that
537 -- Get_Target_Parameters can find system.ads
539 if Arg
(AF
.. AF
+ 1) = "-I"
540 and then Arg
'Length > 2
542 Add_Src_Search_Dir
(Arg
(AF
+ 2 .. Arg
'Last));
545 -- Pass to gcc for compiling binder generated file
546 -- No use passing libraries, it will just generate
549 if not (Arg
(AF
.. AF
+ 1) = "-l"
550 or else Arg
(AF
.. AF
+ 1) = "-L")
552 Binder_Options
.Increment_Last
;
553 Binder_Options
.Table
(Binder_Options
.Last
) :=
557 -- Pass to gcc for linking program
559 Gcc_Linker_Options.Increment_Last;
560 Gcc_Linker_Options.Table
561 (Gcc_Linker_Options.Last) := new String'(Arg
);
566 -- Send all multi-character switches not recognized as
567 -- a special case by gnatlink to the linker/loader stage.
570 Linker_Options
.Increment_Last
;
571 Linker_Options
.Table
(Linker_Options
.Last
) :=
575 -- Here if argument is a file name rather than a switch
578 -- If explicit ali file, capture it
581 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
583 if Ali_File_Name = null then
584 Ali_File_Name := new String'(Arg
);
586 Exit_With_Error
("cannot handle more than one ALI file");
589 -- If target object file, record object file
591 elsif Arg
'Length > Get_Target_Object_Suffix
.all'Length
594 Get_Target_Object_Suffix
.all'Length + 1 .. Arg
'Last)
595 = Get_Target_Object_Suffix
.all
597 Linker_Objects
.Increment_Last
;
598 Linker_Objects
.Table
(Linker_Objects
.Last
) :=
601 -- If host object file, record object file
602 -- e.g. accept foo.o as well as foo.obj on VMS target
604 elsif Arg'Length > Get_Object_Suffix.all'Length
606 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
607 = Get_Object_Suffix.all
609 Linker_Objects.Increment_Last;
610 Linker_Objects.Table (Linker_Objects.Last) :=
613 -- If corresponding ali file exists, capture it
615 elsif Ali_File_Name
= null
616 and then Is_Regular_File
(Arg
& ".ali")
618 Ali_File_Name
:= new String'(Arg & ".ali");
620 -- Otherwise assume this is a linker options entry, but
621 -- see below for interesting adjustment to this assumption.
624 Linker_Options.Increment_Last;
625 Linker_Options.Table (Linker_Options.Last) :=
631 Next_Arg
:= Next_Arg
+ 1;
634 -- If Ada bind file, then compile it with warnings suppressed, because
635 -- otherwise the with of the main program may cause junk warnings.
637 if Ada_Bind_File
then
638 Binder_Options
.Increment_Last
;
639 Binder_Options
.Table
(Binder_Options
.Last
) := new String'("-gnatws");
642 -- If we did not get an ali file at all, and we had at least one
643 -- linker option, then assume that was the intended ali file after
644 -- all, so that we get a nicer message later on.
646 if Ali_File_Name = null
647 and then Linker_Options.Last >= Linker_Options.First
650 new String'(Linker_Options
.Table
(Linker_Options
.First
).all &
655 -------------------------
656 -- Process_Binder_File --
657 -------------------------
659 procedure Process_Binder_File
(Name
: String) is
661 -- Binder file's descriptor
663 Link_Bytes
: Integer := 0;
664 -- Projected number of bytes for the linker command line
667 pragma Import
(C
, Link_Max
, "__gnat_link_max");
668 -- Maximum number of bytes on the command line supported by the OS
669 -- linker. Passed this limit the response file mechanism must be used
672 Next_Line
: String (1 .. 1000);
673 -- Current line value
677 -- Current line slice (the slice does not contain line terminator)
680 -- Current line last character for shared libraries (without version)
682 Objs_Begin
: Integer := 0;
683 -- First object file index in Linker_Objects table
685 Objs_End
: Integer := 0;
686 -- Last object file index in Linker_Objects table
689 pragma Warnings
(Off
, Status
);
690 -- Used for various Interfaces.C_Streams calls
692 Closing_Status
: Boolean;
693 pragma Warnings
(Off
, Closing_Status
);
696 GNAT_Static
: Boolean := False;
697 -- Save state of -static option
699 GNAT_Shared
: Boolean := False;
700 -- Save state of -shared option
702 Xlinker_Was_Previous
: Boolean := False;
703 -- Indicate that "-Xlinker" was the option preceding the current
704 -- option. If True, then the current option is never suppressed.
708 -- These data items are used to store current binder file context.
709 -- The context is composed of the file descriptor position and the
710 -- current line together with the slice indexes (first and last
711 -- position) for this line. The rollback data are used by the
712 -- Store_File_Context and Rollback_File_Context routines below.
713 -- The file context mechanism interact only with the Get_Next_Line
714 -- call. For example:
716 -- Store_File_Context;
718 -- Rollback_File_Context;
721 -- Both Get_Next_Line calls above will read the exact same data from
722 -- the file. In other words, Next_Line, Nfirst and Nlast variables
723 -- will be set with the exact same values.
725 RB_File_Pos
: long
; -- File position
726 RB_Next_Line
: String (1 .. 1000); -- Current line content
727 RB_Nlast
: Integer; -- Slice last index
728 RB_Nfirst
: Integer; -- Slice first index
730 Run_Path_Option_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
731 pragma Import
(C
, Run_Path_Option_Ptr
, "__gnat_run_path_option");
732 -- Pointer to string representing the native linker option which
733 -- specifies the path where the dynamic loader should find shared
734 -- libraries. Equal to null string if this system doesn't support it.
736 Libgcc_Subdir_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
737 pragma Import
(C
, Libgcc_Subdir_Ptr
, "__gnat_default_libgcc_subdir");
738 -- Pointer to string indicating the installation subdirectory where
739 -- a default shared libgcc might be found.
741 Object_Library_Ext_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
743 (C
, Object_Library_Ext_Ptr
, "__gnat_object_library_extension");
744 -- Pointer to string specifying the default extension for
745 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
747 Object_File_Option_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
748 pragma Import
(C
, Object_File_Option_Ptr
, "__gnat_object_file_option");
749 -- Pointer to a string representing the linker option which specifies
750 -- the response file.
752 Using_GNU_Linker
: Boolean;
753 for Using_GNU_Linker
'Size use Character'Size;
754 pragma Import
(C
, Using_GNU_Linker
, "__gnat_using_gnu_linker");
755 -- Predicate indicating whether this target uses the GNU linker. In
756 -- this case we must output a GNU linker compatible response file.
758 Separate_Run_Path_Options
: Boolean;
759 for Separate_Run_Path_Options
'Size use Character'Size;
761 (C
, Separate_Run_Path_Options
, "__gnat_separate_run_path_options");
762 -- Whether separate rpath options should be emitted for each directory
764 Opening
: aliased constant String := """";
765 Closing
: aliased constant String := '"' & ASCII
.LF
;
766 -- Needed to quote object paths in object list files when GNU linker
769 procedure Get_Next_Line
;
770 -- Read the next line from the binder file without the line
773 function Index
(S
, Pattern
: String) return Natural;
774 -- Return the last occurrence of Pattern in S, or 0 if none
776 function Is_Option_Present
(Opt
: String) return Boolean;
777 -- Return true if the option Opt is already present in
778 -- Linker_Options table.
780 procedure Store_File_Context
;
781 -- Store current file context, Fd position and current line data.
782 -- The file context is stored into the rollback data above (RB_*).
783 -- Store_File_Context can be called at any time, only the last call
784 -- will be used (i.e. this routine overwrites the file context).
786 procedure Rollback_File_Context
;
787 -- Restore file context from rollback data. This routine must be called
788 -- after Store_File_Context. The binder file context will be restored
789 -- with the data stored by the last Store_File_Context call.
795 procedure Get_Next_Line
is
799 Fchars
:= fgets
(Next_Line
'Address, Next_Line
'Length, Fd
);
801 if Fchars
= System
.Null_Address
then
802 Exit_With_Error
("Error reading binder output");
805 Nfirst
:= Next_Line
'First;
807 while Nlast
<= Next_Line
'Last
808 and then Next_Line
(Nlast
) /= ASCII
.LF
809 and then Next_Line
(Nlast
) /= ASCII
.CR
821 function Index
(S
, Pattern
: String) return Natural is
822 Len
: constant Natural := Pattern
'Length;
825 for J
in reverse S
'First .. S
'Last - Len
+ 1 loop
826 if Pattern
= S
(J
.. J
+ Len
- 1) then
834 -----------------------
835 -- Is_Option_Present --
836 -----------------------
838 function Is_Option_Present
(Opt
: String) return Boolean is
840 for I
in 1 .. Linker_Options
.Last
loop
842 if Linker_Options
.Table
(I
).all = Opt
then
849 end Is_Option_Present
;
851 ---------------------------
852 -- Rollback_File_Context --
853 ---------------------------
855 procedure Rollback_File_Context
is
857 Next_Line
:= RB_Next_Line
;
860 Status
:= fseek
(Fd
, RB_File_Pos
, Interfaces
.C_Streams
.SEEK_SET
);
863 Exit_With_Error
("Error setting file position");
865 end Rollback_File_Context
;
867 ------------------------
868 -- Store_File_Context --
869 ------------------------
871 procedure Store_File_Context
is
872 use type System
.CRTL
.long
;
874 RB_Next_Line
:= Next_Line
;
877 RB_File_Pos
:= ftell
(Fd
);
879 if RB_File_Pos
= -1 then
880 Exit_With_Error
("Error getting file position");
882 end Store_File_Context
;
884 -- Start of processing for Process_Binder_File
887 Fd
:= fopen
(Name
'Address, Read_Mode
'Address);
889 if Fd
= NULL_Stream
then
890 Exit_With_Error
("Failed to open binder output");
893 -- Skip up to the Begin Info line
897 exit when Next_Line
(Nfirst
.. Nlast
) = Begin_Info
;
903 -- Go to end when end line is reached (this will happen in
904 -- High_Integrity_Mode where no -L switches are generated)
906 exit when Next_Line
(Nfirst
.. Nlast
) = End_Info
;
908 if Ada_Bind_File
then
909 Next_Line
(Nfirst
.. Nlast
- 8) :=
910 Next_Line
(Nfirst
+ 8 .. Nlast
);
914 -- Go to next section when switches are reached
916 exit when Next_Line
(1) = '-';
918 -- Otherwise we have another object file to collect
920 Linker_Objects
.Increment_Last
;
922 -- Mark the positions of first and last object files in case
923 -- they need to be placed with a named file on systems having
924 -- linker line limitations.
926 if Objs_Begin
= 0 then
927 Objs_Begin
:= Linker_Objects
.Last
;
930 Linker_Objects
.Table
(Linker_Objects
.Last
) :=
931 new String'(Next_Line (Nfirst .. Nlast));
933 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
934 -- Nlast - Nfirst + 1, for the size, plus one for the space between
938 Objs_End := Linker_Objects.Last;
940 -- Continue to compute the Link_Bytes, the linker options are part of
941 -- command line length.
945 while Next_Line (Nfirst .. Nlast) /= End_Info loop
946 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
950 Rollback_File_Context;
952 -- On systems that have limitations on handling very long linker lines
953 -- we make use of the system linker option which takes a list of object
954 -- file names from a file instead of the command line itself. What we do
955 -- is to replace the list of object files by the special linker option
956 -- which then reads the object file list from a file instead. The option
957 -- to read from a file instead of the command line is only triggered if
958 -- a conservative threshold is passed.
960 if VM_Target = CLI_Target
961 and then Link_Bytes > Link_Max
963 Lname := new String'("l~" & Base_Name
(Ali_File_Name
.all) & ".il");
965 for J
in Objs_Begin
.. Objs_End
loop
966 Copy_File
(Linker_Objects
.Table
(J
).all, Lname
.all,
967 Success
=> Closing_Status
,
971 -- Add the special objects list file option together with the name
972 -- of the temporary file to the objects file table.
974 Linker_Objects
.Table
(Objs_Begin
) :=
975 new String'(Value (Object_File_Option_Ptr) & Lname.all);
977 -- The slots containing these object file names are then removed
978 -- from the objects table so they do not appear in the link. They
979 -- are removed by moving up the linker options and non-Ada object
980 -- files appearing after the Ada object list in the table.
986 N := Objs_End - Objs_Begin + 1;
988 for J in Objs_End + 1 .. Linker_Objects.Last loop
989 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
992 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
995 elsif Object_List_File_Required
996 or else (Object_List_File_Supported
997 and then Link_Bytes > Link_Max)
999 -- Create a temporary file containing the Ada user object files
1000 -- needed by the link. This list is taken from the bind file
1001 -- and is output one object per line for maximal compatibility with
1002 -- linkers supporting this option.
1004 Create_Temp_File (Tname_FD, Tname);
1006 -- ??? File descriptor should be checked to not be Invalid_FD.
1007 -- ??? Status of Write and Close operations should be checked, and
1008 -- failure should occur if a status is wrong.
1010 -- If target is using the GNU linker we must add a special header
1011 -- and footer in the response file.
1013 -- The syntax is : INPUT (object1.o object2.o ... )
1015 -- Because the GNU linker does not like name with characters such
1016 -- as '!', we must put the object paths between double quotes.
1018 if Using_GNU_Linker then
1020 GNU_Header : aliased constant String := "INPUT (";
1023 Status := Write (Tname_FD, GNU_Header'Address,
1028 for J in Objs_Begin .. Objs_End loop
1030 -- Opening quote for GNU linker
1032 if Using_GNU_Linker then
1033 Status := Write (Tname_FD, Opening'Address, 1);
1036 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
1037 Linker_Objects.Table (J).all'Length);
1039 -- Closing quote for GNU linker
1041 if Using_GNU_Linker then
1042 Status := Write (Tname_FD, Closing'Address, 2);
1045 Status := Write (Tname_FD, ASCII.LF'Address, 1);
1048 Response_File_Objects.Increment_Last;
1049 Response_File_Objects.Table (Response_File_Objects.Last) :=
1050 Linker_Objects.Table (J);
1053 -- Handle GNU linker response file footer
1055 if Using_GNU_Linker then
1057 GNU_Footer : aliased constant String := ")";
1060 Status := Write (Tname_FD, GNU_Footer'Address,
1065 Close (Tname_FD, Closing_Status);
1067 -- Add the special objects list file option together with the name
1068 -- of the temporary file (removing the null character) to the objects
1071 Linker_Objects.Table (Objs_Begin) :=
1072 new String'(Value
(Object_File_Option_Ptr
) &
1073 Tname
(Tname
'First .. Tname
'Last - 1));
1075 -- The slots containing these object file names are then removed
1076 -- from the objects table so they do not appear in the link. They
1077 -- are removed by moving up the linker options and non-Ada object
1078 -- files appearing after the Ada object list in the table.
1084 N
:= Objs_End
- Objs_Begin
+ 1;
1086 for J
in Objs_End
+ 1 .. Linker_Objects
.Last
loop
1087 Linker_Objects
.Table
(J
- N
+ 1) := Linker_Objects
.Table
(J
);
1090 Linker_Objects
.Set_Last
(Linker_Objects
.Last
- N
+ 1);
1094 -- Process switches and options
1096 if Next_Line
(Nfirst
.. Nlast
) /= End_Info
then
1097 Xlinker_Was_Previous
:= False;
1100 if Xlinker_Was_Previous
1101 or else Next_Line
(Nfirst
.. Nlast
) = "-Xlinker"
1103 Linker_Options
.Increment_Last
;
1104 Linker_Options
.Table
(Linker_Options
.Last
) :=
1105 new String'(Next_Line (Nfirst .. Nlast));
1107 elsif Next_Line (Nfirst .. Nlast) = "-static" then
1108 GNAT_Static := True;
1110 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1111 GNAT_Shared := True;
1113 -- Add binder options only if not already set on the command
1114 -- line. This rule is a way to control the linker options order.
1116 -- The following test needs comments, why is it VMS specific.
1117 -- The above comment looks out of date ???
1119 elsif not (OpenVMS_On_Target
1121 Is_Option_Present (Next_Line (Nfirst .. Nlast)))
1123 if Nlast > Nfirst + 2 and then
1124 Next_Line (Nfirst .. Nfirst + 1) = "-L"
1126 -- Construct a library search path for use later
1127 -- to locate static gnatlib libraries.
1129 if Libpath.Last > 1 then
1130 Libpath.Increment_Last;
1131 Libpath.Table (Libpath.Last) := Path_Separator;
1134 for I in Nfirst + 2 .. Nlast loop
1135 Libpath.Increment_Last;
1136 Libpath.Table (Libpath.Last) := Next_Line (I);
1139 Linker_Options.Increment_Last;
1141 Linker_Options.Table (Linker_Options.Last) :=
1142 new String'(Next_Line
(Nfirst
.. Nlast
));
1144 elsif Next_Line
(Nfirst
.. Nlast
) = "-ldecgnat"
1145 or else Next_Line
(Nfirst
.. Nlast
) = "-lgnarl"
1146 or else Next_Line
(Nfirst
.. Nlast
) = "-lgnat"
1148 (1 .. Natural'Min (Nlast
, 8 + Library_Version
'Length)) =
1149 Shared_Lib
("gnarl")
1151 (1 .. Natural'Min (Nlast
, 7 + Library_Version
'Length)) =
1154 -- If it is a shared library, remove the library version.
1155 -- We will be looking for the static version of the library
1156 -- as it is in the same directory as the shared version.
1158 if Next_Line
(Nlast
- Library_Version
'Length + 1 .. Nlast
)
1161 -- Set Last to point to last character before the
1164 Last
:= Nlast
- Library_Version
'Length - 1;
1169 -- Given a Gnat standard library, search the library path to
1170 -- find the library location.
1172 -- Shouldn't we abstract a proc here, we are getting awfully
1173 -- heavily nested ???
1176 File_Path
: String_Access
;
1178 Object_Lib_Extension
: constant String :=
1179 Value
(Object_Library_Ext_Ptr
);
1181 File_Name
: constant String := "lib" &
1182 Next_Line
(Nfirst
+ 2 .. Last
) &
1183 Object_Lib_Extension
;
1185 Run_Path_Opt
: constant String :=
1186 Value
(Run_Path_Option_Ptr
);
1188 GCC_Index
: Natural;
1189 Run_Path_Opt_Index
: Natural := 0;
1193 Locate_Regular_File
(File_Name
,
1194 String (Libpath
.Table
(1 .. Libpath
.Last
)));
1196 if File_Path
/= null then
1199 -- If static gnatlib found, explicitly
1200 -- specify to overcome possible linker
1201 -- default usage of shared version.
1203 Linker_Options
.Increment_Last
;
1205 Linker_Options
.Table
(Linker_Options
.Last
) :=
1206 new String'(File_Path.all);
1208 elsif GNAT_Shared then
1209 if Opt.Run_Path_Option then
1211 -- If shared gnatlib desired, add the
1212 -- appropriate system specific switch
1213 -- so that it can be located at runtime.
1215 if Run_Path_Opt'Length /= 0 then
1217 -- Output the system specific linker command
1218 -- that allows the image activator to find
1219 -- the shared library at runtime. Also add
1220 -- path to find libgcc_s.so, if relevant.
1223 Path : String (1 .. File_Path'Length + 15);
1224 Path_Last : constant Natural :=
1228 Path (1 .. File_Path'Length) :=
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 -- recent versions of GCC. So, we look for
1236 -- the last subdirectory "lib" in the path.
1239 Index (Path (1 .. Path_Last), "gcc-lib");
1241 if GCC_Index /= 0 then
1243 -- The shared version of libgcc is
1244 -- located in the parent directory.
1246 GCC_Index := GCC_Index - 1;
1251 (Path (1 .. Path_Last),
1254 if GCC_Index = 0 then
1256 Index (Path (1 .. Path_Last),
1257 Directory_Separator &
1259 Directory_Separator);
1262 -- If we have found a "lib" subdir in
1263 -- the path to libgnat, the possible
1264 -- shared libgcc of interest by default
1265 -- is in libgcc_subdir at the same
1268 if GCC_Index /= 0 then
1270 Subdir : constant String :=
1271 Value (Libgcc_Subdir_Ptr);
1275 GCC_Index + Subdir'Length) :=
1278 GCC_Index + Subdir'Length;
1283 -- Look for an eventual run_path_option in
1284 -- the linker switches.
1286 if Separate_Run_Path_Options then
1287 Linker_Options.Increment_Last;
1288 Linker_Options.Table
1289 (Linker_Options.Last) :=
1293 (1 .. File_Path
'Length
1294 - File_Name
'Length));
1296 if GCC_Index
/= 0 then
1297 Linker_Options
.Increment_Last
;
1298 Linker_Options
.Table
1299 (Linker_Options
.Last
) :=
1302 & Path (1 .. GCC_Index));
1307 1 .. Linker_Options.Last
1309 if Linker_Options.Table (J) /= null
1311 Linker_Options.Table (J)'Length
1312 > Run_Path_Opt'Length
1314 Linker_Options.Table (J)
1315 (1 .. Run_Path_Opt'Length) =
1318 -- We have found an already
1319 -- specified run_path_option: we
1320 -- will add to this switch,
1322 -- run_path_option should be
1325 Run_Path_Opt_Index := J;
1330 -- If there is no run_path_option, we
1333 if Run_Path_Opt_Index = 0 then
1334 Linker_Options.Increment_Last;
1337 if GCC_Index = 0 then
1338 if Run_Path_Opt_Index = 0 then
1339 Linker_Options.Table
1340 (Linker_Options.Last) :=
1344 (1 .. File_Path
'Length
1345 - File_Name
'Length));
1348 Linker_Options
.Table
1349 (Run_Path_Opt_Index
) :=
1351 (Linker_Options.Table
1352 (Run_Path_Opt_Index).all
1355 (1 .. File_Path'Length
1356 - File_Name'Length));
1360 if Run_Path_Opt_Index = 0 then
1361 Linker_Options.Table
1362 (Linker_Options.Last) :=
1366 (1 .. File_Path
'Length
1369 & Path
(1 .. GCC_Index
));
1372 Linker_Options
.Table
1373 (Run_Path_Opt_Index
) :=
1375 (Linker_Options.Table
1376 (Run_Path_Opt_Index).all
1379 (1 .. File_Path'Length
1382 & Path (1 .. GCC_Index));
1390 -- Then we add the appropriate -l switch
1392 Linker_Options.Increment_Last;
1393 Linker_Options.Table (Linker_Options.Last) :=
1394 new String'(Next_Line
(Nfirst
.. Nlast
));
1398 -- If gnatlib library not found, then
1399 -- add it anyway in case some other
1400 -- mechanism may find it.
1402 Linker_Options
.Increment_Last
;
1403 Linker_Options
.Table
(Linker_Options
.Last
) :=
1404 new String'(Next_Line (Nfirst .. Nlast));
1408 Linker_Options.Increment_Last;
1409 Linker_Options.Table (Linker_Options.Last) :=
1410 new String'(Next_Line
(Nfirst
.. Nlast
));
1414 Xlinker_Was_Previous
:= Next_Line
(Nfirst
.. Nlast
) = "-Xlinker";
1417 exit when Next_Line
(Nfirst
.. Nlast
) = End_Info
;
1419 if Ada_Bind_File
then
1420 Next_Line
(Nfirst
.. Nlast
- 8) :=
1421 Next_Line
(Nfirst
+ 8 .. Nlast
);
1427 -- If -shared was specified, invoke gcc with -shared-libgcc
1430 Linker_Options
.Increment_Last
;
1431 Linker_Options
.Table
(Linker_Options
.Last
) := Shared_Libgcc
;
1434 Status
:= fclose
(Fd
);
1435 end Process_Binder_File
;
1443 Write_Str
("Usage: ");
1444 Write_Str
(Base_Name
(Command_Name
));
1445 Write_Str
(" switches mainprog.ali [non-Ada-objects] [linker-options]");
1448 Write_Line
(" mainprog.ali the ALI file of the main program");
1450 Write_Line
(" -f force object file list to be generated");
1451 Write_Line
(" -g Compile binder source file with debug information");
1452 Write_Line
(" -n Do not compile the binder source file");
1453 Write_Line
(" -R Do not use a run_path_option");
1454 Write_Line
(" -v verbose mode");
1455 Write_Line
(" -v -v very verbose mode");
1457 Write_Line
(" -o nam Use 'nam' as the name of the executable");
1458 Write_Line
(" -b target Compile the binder source to run on target");
1459 Write_Line
(" -Bdir Load compiler executables from dir");
1461 if Is_Supported
(Map_File
) then
1462 Write_Line
(" -Mmap Create map file map");
1463 Write_Line
(" -M Create map file mainprog.map");
1466 Write_Line
(" --GCC=comp Use comp as the compiler");
1467 Write_Line
(" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1469 Write_Line
(" [non-Ada-objects] list of non Ada object files");
1470 Write_Line
(" [linker-options] other options for the linker");
1477 procedure Write_Header
is
1479 if Verbose_Mode
then
1481 Display_Version
("GNATLINK", "1995");
1489 procedure Write_Usage
is
1495 -- Start of processing for Gnatlink
1498 -- Add the directory where gnatlink is invoked in front of the path, if
1499 -- gnatlink is invoked with directory information. Only do this if the
1500 -- platform is not VMS, where the notion of path does not really exist.
1502 if not Hostparm
.OpenVMS
then
1504 Command
: constant String := Command_Name
;
1507 for Index
in reverse Command
'Range loop
1508 if Command
(Index
) = Directory_Separator
then
1510 Absolute_Dir
: constant String :=
1512 (Command
(Command
'First .. Index
));
1514 PATH
: constant String :=
1517 Getenv
("PATH").all;
1520 Setenv
("PATH", PATH
);
1531 if Argument_Count
= 0
1532 or else (Verbose_Mode
and then Argument_Count
= 1)
1535 Exit_Program
(E_Fatal
);
1538 -- Initialize packages to be used
1544 -- We always compile with -c
1546 Binder_Options_From_ALI
.Increment_Last
;
1547 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1550 if Ali_File_Name = null then
1551 Exit_With_Error ("no ali file given for link");
1554 if not Is_Regular_File (Ali_File_Name.all) then
1555 Exit_With_Error (Ali_File_Name.all & " not found");
1558 -- Read the ALI file of the main subprogram if the binder generated file
1559 -- needs to be compiled and no --GCC= switch has been specified. Fetch the
1560 -- back end switches from this ALI file and use these switches to compile
1561 -- the binder generated file
1563 if Compile_Bind_File and then Standard_Gcc then
1566 Name_Len := Ali_File_Name'Length;
1567 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1571 F : constant File_Name_Type := Name_Find;
1572 T : Text_Buffer_Ptr;
1576 -- Load the ALI file
1578 T := Read_Library_Info (F, True);
1580 -- Read it. Note that we ignore errors, since we only want very
1581 -- limited information from the ali file, and likely a slightly
1582 -- wrong version will be just fine, though in normal operation
1583 -- we don't expect this to happen!
1590 Ignore_Errors => True);
1592 if A /= No_ALI_Id then
1594 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1595 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1597 -- Do not compile with the front end switches. However, --RTS
1598 -- is to be dealt with specially because it needs to be passed
1599 -- if the binder-generated file is in Ada and may also be used
1600 -- to drive the linker.
1603 Arg : String_Ptr renames Args.Table (Index);
1605 if not Is_Front_End_Switch (Arg.all) then
1606 Binder_Options_From_ALI.Increment_Last;
1607 Binder_Options_From_ALI.Table
1608 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1610 elsif Arg'Length > 5
1611 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1613 if Ada_Bind_File then
1614 Binder_Options_From_ALI.Increment_Last;
1615 Binder_Options_From_ALI.Table
1616 (Binder_Options_From_ALI.Last)
1617 := String_Access (Arg);
1620 -- Set the RTS_*_Path_Name variables, so that
1621 -- the correct directories will be set when
1622 -- Osint.Add_Default_Search_Dirs will be called later.
1624 Opt.RTS_Src_Path_Name :=
1626 (Arg (Arg'First + 6 .. Arg'Last), Include);
1628 Opt.RTS_Lib_Path_Name :=
1630 (Arg (Arg'First + 6 .. Arg'Last), Objects);
1632 -- GNAT doesn't support the GCC multilib mechanism.
1633 -- This means that, when a multilib switch is used
1634 -- to request a particular compilation mode, the
1635 -- corresponding runtime switch (--RTS) must also be
1636 -- specified. The long-term goal is to fully support the
1637 -- multilib mechanism; however, in the meantime, it is
1638 -- convenient to eliminate the redundancy by keying the
1639 -- compilation mode on a single switch, namely --RTS.
1641 -- Pass -mrtp to the linker if --RTS=rtp was passed
1644 and then Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1646 Linker_Options.Increment_Last;
1647 Linker_Options.Table (Linker_Options.Last) :=
1648 new String'("-mrtp");
1657 -- Get target parameters
1659 Osint
.Add_Default_Search_Dirs
;
1660 Targparm
.Get_Target_Parameters
;
1662 if VM_Target
/= No_VM
then
1664 when JVM_Target
=> Gcc
:= new String'("jvm-gnatcompile");
1665 when CLI_Target => Gcc := new String'("dotnet-gnatcompile");
1666 when No_VM
=> raise Program_Error
;
1669 Ada_Bind_File
:= True;
1670 Begin_Info
:= "-- BEGIN Object file/option list";
1671 End_Info
:= "-- END Object file/option list ";
1674 -- If the main program is in Ada it is compiled with the following
1677 -- -gnatA stops reading gnat.adc, since we don't know what
1678 -- pragmas would work, and we do not need it anyway.
1680 -- -gnatWb allows brackets coding for wide characters
1682 -- -gnatiw allows wide characters in identifiers. This is needed
1683 -- because bindgen uses brackets encoding for all upper
1684 -- half and wide characters in identifier names.
1686 if Ada_Bind_File
then
1687 Binder_Options_From_ALI
.Increment_Last
;
1688 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1689 new String'("-gnatA");
1690 Binder_Options_From_ALI.Increment_Last;
1691 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1692 new String'("-gnatWb");
1693 Binder_Options_From_ALI
.Increment_Last
;
1694 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1695 new String'("-gnatiw");
1698 -- Locate all the necessary programs and verify required files are present
1700 Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1702 if Gcc_Path = null then
1703 Exit_With_Error ("Couldn't locate " & Gcc.all);
1706 if Linker_Path = null then
1707 if VM_Target = CLI_Target then
1708 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("ilasm");
1710 if Linker_Path = null then
1711 Exit_With_Error ("Couldn't locate ilasm");
1714 elsif RTX_RTSS_Kernel_Module_On_Target then
1716 -- Use Microsoft linker for RTSS modules
1718 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("link");
1720 if Linker_Path = null then
1721 Exit_With_Error ("Couldn't locate link");
1725 Linker_Path := Gcc_Path;
1731 -- If no output name specified, then use the base name of .ali file name
1733 if Output_File_Name = null then
1735 new String'(Base_Name
(Ali_File_Name
.all)
1736 & Get_Target_Debuggable_Suffix
.all);
1739 if VM_Target
= CLI_Target
then
1740 Linker_Options
.Increment_Last
;
1741 Linker_Options
.Table
(Linker_Options
.Last
) := new String'("/QUIET");
1743 Linker_Options.Increment_Last;
1744 Linker_Options.Table (Linker_Options.Last) := new String'("/DEBUG");
1746 Linker_Options
.Increment_Last
;
1747 Linker_Options
.Table
(Linker_Options
.Last
) :=
1748 new String'("/OUTPUT=" & Output_File_Name.all);
1750 elsif RTX_RTSS_Kernel_Module_On_Target then
1751 Linker_Options.Increment_Last;
1752 Linker_Options.Table (Linker_Options.Last) :=
1753 new String'("/OUT:" & Output_File_Name
.all);
1756 Linker_Options
.Increment_Last
;
1757 Linker_Options
.Table
(Linker_Options
.Last
) := new String'("-o");
1759 Linker_Options.Increment_Last;
1760 Linker_Options.Table (Linker_Options.Last) :=
1761 new String'(Output_File_Name
.all);
1764 -- Delete existing executable, in case it is a symbolic link, to avoid
1765 -- modifying the target of the symbolic link.
1769 pragma Unreferenced
(Dummy
);
1772 Delete_File
(Output_File_Name
.all, Dummy
);
1775 -- Warn if main program is called "test", as that may be a built-in command
1776 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1777 -- will not appear. However, do not warn in the case of a cross compiler.
1779 -- Assume this is a cross tool if the executable name is not gnatlink
1781 if Base_Name
(Command_Name
) = "gnatlink"
1782 and then Output_File_Name
.all = "test"
1784 Error_Msg
("warning: executable name """ & Output_File_Name
.all
1785 & """ may conflict with shell command");
1788 -- If -M switch was specified, add the switches to create the map file
1790 if Create_Map_File
then
1792 Map_Name
: constant String := Base_Name
(Ali_File_Name
.all) & ".map";
1793 Switches
: String_List_Access
;
1796 Convert
(Map_File
, Map_Name
, Switches
);
1798 if Switches
/= null then
1799 for J
in Switches
'Range loop
1800 Linker_Options
.Increment_Last
;
1801 Linker_Options
.Table
(Linker_Options
.Last
) := Switches
(J
);
1807 -- Perform consistency checks
1809 -- Transform the .ali file name into the binder output file name
1811 Make_Binder_File_Names
: declare
1812 Fname
: constant String := Base_Name
(Ali_File_Name
.all);
1813 Fname_Len
: Integer := Fname
'Length;
1815 function Get_Maximum_File_Name_Length
return Integer;
1816 pragma Import
(C
, Get_Maximum_File_Name_Length
,
1817 "__gnat_get_maximum_file_name_length");
1819 Maximum_File_Name_Length
: constant Integer :=
1820 Get_Maximum_File_Name_Length
;
1822 Bind_File_Prefix
: Types
.String_Ptr
;
1823 -- Contains prefix used for bind files
1828 if not Ada_Bind_File
then
1829 Bind_File_Prefix
:= new String'("b_");
1830 elsif OpenVMS_On_Target then
1831 Bind_File_Prefix := new String'("b__");
1833 Bind_File_Prefix
:= new String'("b~");
1836 -- If the length of the binder file becomes too long due to
1837 -- the addition of the "b?" prefix, then truncate it.
1839 if Maximum_File_Name_Length > 0 then
1841 Maximum_File_Name_Length - Bind_File_Prefix.all'Length
1843 Fname_Len := Fname_Len - 1;
1848 Fnam : constant String :=
1849 Bind_File_Prefix.all &
1850 Fname (Fname'First .. Fname'First + Fname_Len - 1);
1853 if Ada_Bind_File then
1854 Binder_Spec_Src_File := new String'(Fnam
& ".ads");
1855 Binder_Body_Src_File
:= new String'(Fnam & ".adb");
1856 Binder_Ali_File := new String'(Fnam
& ".ali");
1858 Binder_Body_Src_File
:= new String'(Fnam & ".c");
1861 Binder_Obj_File := new String'(Fnam
& Get_Target_Object_Suffix
.all);
1864 if Fname_Len
/= Fname
'Length then
1865 Binder_Options
.Increment_Last
;
1866 Binder_Options
.Table
(Binder_Options
.Last
) := new String'("-o");
1867 Binder_Options.Increment_Last;
1868 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1870 end Make_Binder_File_Names;
1872 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1874 -- Compile the binder file. This is fast, so we always do it, unless
1875 -- specifically told not to by the -n switch
1877 if Compile_Bind_File then
1880 Args : Argument_List
1881 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1884 for J in 1 .. Binder_Options_From_ALI.Last loop
1885 Args (J) := Binder_Options_From_ALI.Table (J);
1888 for J in 1 .. Binder_Options.Last loop
1889 Args (Binder_Options_From_ALI.Last + J) :=
1890 Binder_Options.Table (J);
1893 -- Use the full path of the binder generated source, so that it is
1894 -- guaranteed that the debugger will find this source, even with
1898 new String'(Normalize_Pathname
(Binder_Body_Src_File
.all));
1900 if Verbose_Mode
then
1901 Write_Str
(Base_Name
(Gcc_Path
.all));
1903 for J
in Args
'Range loop
1905 Write_Str
(Args
(J
).all);
1911 System
.OS_Lib
.Spawn
(Gcc_Path
.all, Args
, Success
);
1914 Exit_Program
(E_Fatal
);
1919 -- Now, actually link the program
1921 -- Skip this step for now on JVM since the Java interpreter will do
1922 -- the actual link at run time. We might consider packing all class files
1923 -- in a .zip file during this step.
1925 if VM_Target
/= JVM_Target
then
1927 Num_Args
: Natural :=
1928 (Linker_Options
.Last
- Linker_Options
.First
+ 1) +
1929 (Gcc_Linker_Options
.Last
- Gcc_Linker_Options
.First
+ 1) +
1930 (Linker_Objects
.Last
- Linker_Objects
.First
+ 1);
1931 Stack_Op
: Boolean := False;
1932 IDENT_Op
: Boolean := False;
1935 if VM_Target
= CLI_Target
then
1937 -- Remove extraneous flags not relevant for CIL. Also remove empty
1938 -- arguments, since ilasm chokes on them.
1940 for J
in reverse Linker_Options
.First
.. Linker_Options
.Last
loop
1941 if Linker_Options
.Table
(J
)'Length = 0
1942 or else Linker_Options
.Table
(J
) (1 .. 2) = "-L"
1943 or else Linker_Options
.Table
(J
) (1 .. 2) = "-l"
1944 or else Linker_Options
.Table
(J
) (1 .. 3) = "-Wl"
1945 or else Linker_Options
.Table
(J
) (1 .. 3) = "-sh"
1946 or else Linker_Options
.Table
(J
) (1 .. 2) = "-g"
1948 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
1949 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
1950 Linker_Options
.Decrement_Last
;
1951 Num_Args
:= Num_Args
- 1;
1955 elsif RTX_RTSS_Kernel_Module_On_Target
then
1957 -- Remove flags not relevant for Microsoft linker and adapt some
1960 for J
in reverse Linker_Options
.First
.. Linker_Options
.Last
loop
1962 -- Remove flags that are not accepted
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 .. 3) = "-Wl"
1966 or else Linker_Options
.Table
(J
) (1 .. 3) = "-sh"
1967 or else Linker_Options
.Table
(J
) (1 .. 8) = "-Xlinker"
1968 or else Linker_Options
.Table
(J
) (1 .. 9) = "-mthreads"
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;
1975 -- Replace "-L" by its counterpart "/LIBPATH:" and UNIX "/" by
1977 elsif Linker_Options
.Table
(J
) (1 .. 2) = "-L" then
1979 Libpath_Option
: constant String_Access
:= new String'
1981 Linker_Options.Table (J)
1982 (3 .. Linker_Options.Table (J).all'Last));
1984 for Index in 10 .. Libpath_Option'Last loop
1985 if Libpath_Option (Index) = '/' then
1986 Libpath_Option (Index) := '\
';
1990 Linker_Options.Table (J) := Libpath_Option;
1993 -- Replace "-g" by "/DEBUG"
1994 elsif Linker_Options.Table (J) (1 .. 2) = "-g" then
1995 Linker_Options.Table (J) := new String'("/DEBUG");
1997 -- Replace "-o" by "/OUT:"
1998 elsif Linker_Options
.Table
(J
) (1 .. 2) = "-o" then
1999 Linker_Options
.Table
(J
+ 1) := new String'
2000 ("/OUT:" & Linker_Options.Table (J + 1).all);
2002 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2003 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2004 Linker_Options.Decrement_Last;
2005 Num_Args := Num_Args - 1;
2007 -- Replace "--stack=" by "/STACK:"
2008 elsif Linker_Options.Table (J) (1 .. 8) = "--stack=" then
2009 Linker_Options.Table (J) := new String'
2011 Linker_Options
.Table
(J
)
2012 (9 .. Linker_Options
.Table
(J
).all'Last));
2014 -- Replace "-v" by its counterpart "/VERBOSE"
2015 elsif Linker_Options
.Table
(J
) (1 .. 2) = "-v" then
2016 Linker_Options
.Table
(J
) := new String'("/VERBOSE");
2020 -- Add some required flags to create RTSS modules
2023 Flags_For_Linker : constant array (1 .. 17) of String_Access :=
2024 (new String'("/NODEFAULTLIB"),
2025 new String'("/INCREMENTAL:NO"),
2026 new String'("/NOLOGO"),
2027 new String'("/DRIVER"),
2028 new String'("/ALIGN:0x20"),
2029 new String'("/SUBSYSTEM:NATIVE"),
2030 new String'("/ENTRY:_RtapiProcessEntryCRT@8"),
2031 new String'("/RELEASE"),
2032 new String'("startupCRT.obj"),
2033 new String'("rtxlibcmt.lib"),
2034 new String'("oldnames.lib"),
2035 new String'("rtapi_rtss.lib"),
2036 new String'("Rtx_Rtss.lib"),
2037 new String'("libkernel32.a"),
2038 new String'("libws2_32.a"),
2039 new String'("libmswsock.a"),
2040 new String'("libadvapi32.a"));
2041 -- These flags need to be passed to Microsoft linker. They
2042 -- come from the RTX documentation.
2044 Gcc_Lib_Path
: constant String_Access
:= new String'
2045 ("/LIBPATH:" & Include_Dir_Default_Prefix & "\..\");
2046 -- Place to look for gcc related libraries, such as libgcc
2049 -- Replace UNIX "/" by Windows "\" in the path
2051 for Index in 10 .. Gcc_Lib_Path.all'Last loop
2052 if Gcc_Lib_Path (Index) = '/' then
2053 Gcc_Lib_Path (Index) := '\
';
2057 Linker_Options.Increment_Last;
2058 Linker_Options.Table (Linker_Options.Last) := Gcc_Lib_Path;
2059 Num_Args := Num_Args + 1;
2061 for Index in Flags_For_Linker'Range loop
2062 Linker_Options.Increment_Last;
2063 Linker_Options.Table (Linker_Options.Last) :=
2064 Flags_For_Linker (Index);
2065 Num_Args := Num_Args + 1;
2070 -- Remove duplicate stack size setting from the Linker_Options
2071 -- table. The stack setting option "-Xlinker --stack=R,C" can be
2072 -- found in one line when set by a pragma Linker_Options or in two
2073 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
2074 -- line. We also check for the "-Wl,--stack=R" style option.
2076 -- We must remove the second stack setting option instance
2077 -- because the one on the command line will always be the first
2078 -- one. And any subsequent stack setting option will overwrite the
2079 -- previous one. This is done especially for GNAT/NT where we set
2080 -- the stack size for tasking programs by a pragma in the NT
2081 -- specific tasking package System.Task_Primitives.Operations.
2083 -- Note: This is not a FOR loop that runs from Linker_Options.First
2084 -- to Linker_Options.Last, since operations within the loop can
2085 -- modify the length of the table.
2087 Clean_Link_Option_Set : declare
2088 J : Natural := Linker_Options.First;
2089 Shared_Libgcc_Seen : Boolean := False;
2092 while J <= Linker_Options.Last loop
2094 if Linker_Options.Table (J).all = "-Xlinker"
2095 and then J < Linker_Options.Last
2096 and then Linker_Options.Table (J + 1)'Length > 8
2097 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
2100 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
2101 Linker_Options.Table (J + 2 .. Linker_Options.Last);
2102 Linker_Options.Decrement_Last;
2103 Linker_Options.Decrement_Last;
2104 Num_Args := Num_Args - 2;
2111 -- Remove duplicate -shared-libgcc switch
2113 if Linker_Options.Table (J).all = Shared_Libgcc_String then
2114 if Shared_Libgcc_Seen then
2115 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2116 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2117 Linker_Options.Decrement_Last;
2118 Num_Args := Num_Args - 1;
2121 Shared_Libgcc_Seen := True;
2125 -- Here we just check for a canonical form that matches the
2126 -- pragma Linker_Options set in the NT runtime.
2128 if (Linker_Options.Table (J)'Length > 17
2129 and then Linker_Options.Table (J) (1 .. 17)
2130 = "-Xlinker --stack=")
2132 (Linker_Options.Table (J)'Length > 12
2133 and then Linker_Options.Table (J) (1 .. 12)
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;
2147 -- Remove duplicate IDENTIFICATION directives (VMS)
2149 if Linker_Options.Table (J)'Length > 27
2150 and then Linker_Options.Table (J) (1 .. 28)
2151 = "--for-linker=IDENTIFICATION="
2154 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2155 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2156 Linker_Options.Decrement_Last;
2157 Num_Args := Num_Args - 1;
2166 if Linker_Path = Gcc_Path and then VM_Target = No_VM then
2168 -- For systems where the default is to link statically with
2169 -- libgcc, if gcc is not called with -shared-libgcc, call it
2170 -- with -static-libgcc, as there are some platforms where one
2171 -- of these two switches is compulsory to link.
2173 if Shared_Libgcc_Default = 'T
'
2174 and then not Shared_Libgcc_Seen
2176 Linker_Options.Increment_Last;
2177 Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
2178 Num_Args := Num_Args + 1;
2181 elsif RTX_RTSS_Kernel_Module_On_Target then
2183 -- Force the use of the static libgcc for RTSS modules
2185 Linker_Options.Increment_Last;
2186 Linker_Options.Table (Linker_Options.Last) :=
2187 new String'("libgcc.a");
2188 Num_Args
:= Num_Args
+ 1;
2191 end Clean_Link_Option_Set
;
2193 -- Prepare arguments for call to linker
2195 Call_Linker
: declare
2197 Args
: Argument_List
(1 .. Num_Args
+ 1);
2198 Index
: Integer := Args
'First;
2201 Args
(Index
) := Binder_Obj_File
;
2203 -- Add the object files and any -largs libraries
2205 for J
in Linker_Objects
.First
.. Linker_Objects
.Last
loop
2207 Args
(Index
) := Linker_Objects
.Table
(J
);
2210 -- Add the linker options from the binder file
2212 for J
in Linker_Options
.First
.. Linker_Options
.Last
loop
2214 Args
(Index
) := Linker_Options
.Table
(J
);
2217 -- Finally add the libraries from the --GCC= switch
2219 for J
in Gcc_Linker_Options
.First
.. Gcc_Linker_Options
.Last
loop
2221 Args
(Index
) := Gcc_Linker_Options
.Table
(J
);
2224 if Verbose_Mode
then
2225 Write_Str
(Linker_Path
.all);
2227 for J
in Args
'Range loop
2229 Write_Str
(Args
(J
).all);
2234 -- If we are on very verbose mode (-v -v) and a response file
2235 -- is used we display its content.
2237 if Very_Verbose_Mode
and then Tname_FD
/= Invalid_FD
then
2239 Write_Str
("Response file (" &
2240 Tname
(Tname
'First .. Tname
'Last - 1) &
2245 Response_File_Objects
.First
..
2246 Response_File_Objects
.Last
2248 Write_Str
(Response_File_Objects
.Table
(J
).all);
2256 System
.OS_Lib
.Spawn
(Linker_Path
.all, Args
, Success
);
2258 -- Delete the temporary file used in conjunction with linking if
2259 -- one was created. See Process_Bind_File for details.
2261 if Tname_FD
/= Invalid_FD
then
2265 if Lname
/= null then
2266 Delete
(Lname
.all & ASCII
.NUL
);
2270 Error_Msg
("error when calling " & Linker_Path
.all);
2271 Exit_Program
(E_Fatal
);
2277 -- Only keep the binder output file and it's associated object
2278 -- file if compiling with the -g option. These files are only
2279 -- useful if debugging.
2281 if not Debug_Flag_Present
then
2282 if Binder_Ali_File
/= null then
2283 Delete
(Binder_Ali_File
.all & ASCII
.NUL
);
2286 if Binder_Spec_Src_File
/= null then
2287 Delete
(Binder_Spec_Src_File
.all & ASCII
.NUL
);
2290 Delete
(Binder_Body_Src_File
.all & ASCII
.NUL
);
2292 if VM_Target
= No_VM
then
2293 Delete
(Binder_Obj_File
.all & ASCII
.NUL
);
2297 Exit_Program
(E_Success
);
2301 Write_Line
(Exception_Information
(X
));
2302 Exit_With_Error
("INTERNAL ERROR. Please report");