1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2008, 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 function Base_Name
(File_Name
: String) return String;
193 -- Return just the file name part without the extension (if present)
195 procedure Delete
(Name
: String);
196 -- Wrapper to unlink as status is ignored by this application
198 procedure Error_Msg
(Message
: String);
199 -- Output the error or warning Message
201 procedure Exit_With_Error
(Error
: String);
202 -- Output Error and exit program with a fatal condition
204 procedure Process_Args
;
205 -- Go through all the arguments and build option tables
207 procedure Process_Binder_File
(Name
: String);
208 -- Reads the binder file and extracts linker arguments
213 procedure Write_Header
;
214 -- Show user the program name, version and copyright
216 procedure Write_Usage
;
217 -- Show user the program options
223 function Base_Name
(File_Name
: String) return String is
228 Findex1
:= File_Name
'First;
230 -- The file might be specified by a full path name. However,
231 -- we want the path to be stripped away.
233 for J
in reverse File_Name
'Range loop
234 if Is_Directory_Separator
(File_Name
(J
)) then
240 Findex2
:= File_Name
'Last;
241 while Findex2
> Findex1
242 and then File_Name
(Findex2
) /= '.'
244 Findex2
:= Findex2
- 1;
247 if Findex2
= Findex1
then
248 Findex2
:= File_Name
'Last + 1;
251 return File_Name
(Findex1
.. Findex2
- 1);
258 procedure Delete
(Name
: String) is
260 pragma Unreferenced
(Status
);
262 Status
:= unlink
(Name
'Address);
263 -- Is it really right to ignore an error here ???
270 procedure Error_Msg
(Message
: String) is
272 Write_Str
(Base_Name
(Command_Name
));
278 ---------------------
279 -- Exit_With_Error --
280 ---------------------
282 procedure Exit_With_Error
(Error
: String) is
285 Exit_Program
(E_Fatal
);
292 procedure Process_Args
is
294 Skip_Next
: Boolean := False;
295 -- Set to true if the next argument is to be added into the list of
296 -- linker's argument without parsing it.
298 procedure Check_Version_And_Help
is new Check_Version_And_Help_G
(Usage
);
300 -- Start of processing for Process_Args
303 -- First, check for --version and --help
305 Check_Version_And_Help
("GNATLINK", "1995");
307 -- Loop through arguments of gnatlink command
311 exit when Next_Arg
> Argument_Count
;
313 Process_One_Arg
: declare
314 Arg
: constant String := Argument
(Next_Arg
);
317 -- Case of argument which is a switch
319 -- We definitely need section by section comments here ???
323 -- This argument must not be parsed, just add it to the
324 -- list of linker's options.
328 Linker_Options
.Increment_Last
;
329 Linker_Options
.Table
(Linker_Options
.Last
) :=
332 elsif Arg'Length /= 0 and then Arg (1) = '-' then
333 if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then
335 ("invalid switch: """ & Arg & """ (gnat not needed here)");
338 if Arg = "-Xlinker" then
340 -- Next argument should be sent directly to the linker.
341 -- We do not want to parse it here.
345 Linker_Options.Increment_Last;
346 Linker_Options.Table (Linker_Options.Last) :=
350 and then (Arg
'Length < 5 or else Arg
(2 .. 5) /= "gnat")
352 Debug_Flag_Present
:= True;
354 Linker_Options
.Increment_Last
;
355 Linker_Options
.Table
(Linker_Options
.Last
) :=
358 Binder_Options.Increment_Last;
359 Binder_Options.Table (Binder_Options.Last) :=
360 Linker_Options.Table (Linker_Options.Last);
362 elsif Arg'Length >= 3 and then Arg (2) = 'M
' then
364 Switches : String_List_Access;
367 Convert (Map_File, Arg (3 .. Arg'Last), Switches);
369 if Switches /= null then
370 for J in Switches'Range loop
371 Linker_Options.Increment_Last;
372 Linker_Options.Table (Linker_Options.Last) :=
378 elsif Arg'Length = 2 then
381 Ada_Bind_File := True;
382 Begin_Info := "-- BEGIN Object file/option list";
383 End_Info := "-- END Object file/option list ";
386 Linker_Options.Increment_Last;
387 Linker_Options.Table (Linker_Options.Last) :=
390 Binder_Options
.Increment_Last
;
391 Binder_Options
.Table
(Binder_Options
.Last
) :=
392 Linker_Options
.Table
(Linker_Options
.Last
);
394 Next_Arg
:= Next_Arg
+ 1;
396 if Next_Arg
> Argument_Count
then
397 Exit_With_Error
("Missing argument for -b");
400 Get_Machine_Name
: declare
401 Name_Arg
: constant String_Access
:=
402 new String'(Argument (Next_Arg));
405 Linker_Options.Increment_Last;
406 Linker_Options.Table (Linker_Options.Last) :=
409 Binder_Options.Increment_Last;
410 Binder_Options.Table (Binder_Options.Last) :=
413 end Get_Machine_Name;
416 Ada_Bind_File := False;
417 Begin_Info := "/* BEGIN Object file/option list";
418 End_Info := " END Object file/option list */";
421 if Object_List_File_Supported then
422 Object_List_File_Required := True;
425 ("Object list file not supported on this target");
429 Create_Map_File := True;
432 Compile_Bind_File := False;
435 if VM_Target = CLI_Target then
436 Linker_Options.Increment_Last;
437 Linker_Options.Table (Linker_Options.Last) :=
438 new String'("/QUIET");
441 Linker_Options
.Increment_Last
;
442 Linker_Options
.Table
(Linker_Options
.Last
) :=
446 Next_Arg := Next_Arg + 1;
448 if Next_Arg > Argument_Count then
449 Exit_With_Error ("Missing argument for -o");
452 if VM_Target = CLI_Target then
454 new String'("/OUTPUT=" & Argument
(Next_Arg
));
457 new String'(Argument (Next_Arg));
460 Linker_Options.Increment_Last;
461 Linker_Options.Table (Linker_Options.Last) :=
465 Opt.Run_Path_Option := False;
469 -- Support "double" verbose mode. Second -v
470 -- gets sent to the linker and binder phases.
473 Very_Verbose_Mode := True;
475 Linker_Options.Increment_Last;
476 Linker_Options.Table (Linker_Options.Last) :=
479 Binder_Options
.Increment_Last
;
480 Binder_Options
.Table
(Binder_Options
.Last
) :=
481 Linker_Options
.Table
(Linker_Options
.Last
);
484 Verbose_Mode
:= True;
489 Linker_Options
.Increment_Last
;
490 Linker_Options
.Table
(Linker_Options
.Last
) :=
495 elsif Arg (2) = 'B
' then
496 Linker_Options.Increment_Last;
497 Linker_Options.Table (Linker_Options.Last) :=
500 Binder_Options
.Increment_Last
;
501 Binder_Options
.Table
(Binder_Options
.Last
) :=
502 Linker_Options
.Table
(Linker_Options
.Last
);
504 elsif Arg
'Length >= 7 and then Arg
(1 .. 7) = "--LINK=" then
505 if Arg
'Length = 7 then
506 Exit_With_Error
("Missing argument for --LINK=");
510 System
.OS_Lib
.Locate_Exec_On_Path
(Arg
(8 .. Arg
'Last));
512 if Linker_Path
= null then
514 ("Could not locate linker: " & Arg
(8 .. Arg
'Last));
517 elsif Arg
'Length > 6 and then Arg
(1 .. 6) = "--GCC=" then
519 Program_Args
: constant Argument_List_Access
:=
520 Argument_String_To_List
521 (Arg
(7 .. Arg
'Last));
524 if Program_Args
.all (1).all /= Gcc
.all then
525 Gcc
:= new String'(Program_Args.all (1).all);
526 Standard_Gcc := False;
529 -- Set appropriate flags for switches passed
531 for J in 2 .. Program_Args.all'Last loop
533 Arg : constant String := Program_Args.all (J).all;
534 AF : constant Integer := Arg'First;
537 if Arg'Length /= 0 and then Arg (AF) = '-' then
538 if Arg (AF + 1) = 'g
'
539 and then (Arg'Length = 2
540 or else Arg (AF + 2) in '0' .. '3'
541 or else Arg (AF + 2 .. Arg'Last) = "coff")
543 Debug_Flag_Present := True;
547 -- Add directory to source search dirs so that
548 -- Get_Target_Parameters can find system.ads
550 if Arg (AF .. AF + 1) = "-I"
551 and then Arg'Length > 2
553 Add_Src_Search_Dir (Arg (AF + 2 .. Arg'Last));
556 -- Pass to gcc for compiling binder generated file
557 -- No use passing libraries, it will just generate
560 if not (Arg (AF .. AF + 1) = "-l"
561 or else Arg (AF .. AF + 1) = "-L")
563 Binder_Options.Increment_Last;
564 Binder_Options.Table (Binder_Options.Last) :=
568 -- Pass to gcc for linking program
570 Gcc_Linker_Options
.Increment_Last
;
571 Gcc_Linker_Options
.Table
572 (Gcc_Linker_Options
.Last
) := new String'(Arg);
577 -- Send all multi-character switches not recognized as
578 -- a special case by gnatlink to the linker/loader stage.
581 Linker_Options.Increment_Last;
582 Linker_Options.Table (Linker_Options.Last) :=
586 -- Here if argument is a file name rather than a switch
589 -- If explicit ali file, capture it
592 and then Arg
(Arg
'Last - 3 .. Arg
'Last) = ".ali"
594 if Ali_File_Name
= null then
595 Ali_File_Name
:= new String'(Arg);
597 Exit_With_Error ("cannot handle more than one ALI file");
600 -- If target object file, record object file
602 elsif Arg'Length > Get_Target_Object_Suffix.all'Length
605 Get_Target_Object_Suffix.all'Length + 1 .. Arg'Last)
606 = Get_Target_Object_Suffix.all
608 Linker_Objects.Increment_Last;
609 Linker_Objects.Table (Linker_Objects.Last) :=
612 -- If host object file, record object file
613 -- e.g. accept foo.o as well as foo.obj on VMS target
615 elsif Arg
'Length > Get_Object_Suffix
.all'Length
617 (Arg
'Last - Get_Object_Suffix
.all'Length + 1 .. Arg
'Last)
618 = Get_Object_Suffix
.all
620 Linker_Objects
.Increment_Last
;
621 Linker_Objects
.Table
(Linker_Objects
.Last
) :=
624 -- If corresponding ali file exists, capture it
626 elsif Ali_File_Name = null
627 and then Is_Regular_File (Arg & ".ali")
629 Ali_File_Name := new String'(Arg
& ".ali");
631 -- Otherwise assume this is a linker options entry, but
632 -- see below for interesting adjustment to this assumption.
635 Linker_Options
.Increment_Last
;
636 Linker_Options
.Table
(Linker_Options
.Last
) :=
642 Next_Arg := Next_Arg + 1;
645 -- If Ada bind file, then compile it with warnings suppressed, because
646 -- otherwise the with of the main program may cause junk warnings.
648 if Ada_Bind_File then
649 Binder_Options.Increment_Last;
650 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
653 -- If we did not get an ali file at all, and we had at least one
654 -- linker option, then assume that was the intended ali file after
655 -- all, so that we get a nicer message later on.
657 if Ali_File_Name
= null
658 and then Linker_Options
.Last
>= Linker_Options
.First
661 new String'(Linker_Options.Table (Linker_Options.First).all &
666 -------------------------
667 -- Process_Binder_File --
668 -------------------------
670 procedure Process_Binder_File (Name : String) is
672 -- Binder file's descriptor
674 Link_Bytes : Integer := 0;
675 -- Projected number of bytes for the linker command line
678 pragma Import (C, Link_Max, "__gnat_link_max");
679 -- Maximum number of bytes on the command line supported by the OS
680 -- linker. Passed this limit the response file mechanism must be used
683 Next_Line : String (1 .. 1000);
684 -- Current line value
688 -- Current line slice (the slice does not contain line terminator)
691 -- Current line last character for shared libraries (without version)
693 Objs_Begin : Integer := 0;
694 -- First object file index in Linker_Objects table
696 Objs_End : Integer := 0;
697 -- Last object file index in Linker_Objects table
700 pragma Warnings (Off, Status);
701 -- Used for various Interfaces.C_Streams calls
703 Closing_Status : Boolean;
704 pragma Warnings (Off, Closing_Status);
707 GNAT_Static : Boolean := False;
708 -- Save state of -static option
710 GNAT_Shared : Boolean := False;
711 -- Save state of -shared option
713 Xlinker_Was_Previous : Boolean := False;
714 -- Indicate that "-Xlinker" was the option preceding the current
715 -- option. If True, then the current option is never suppressed.
719 -- These data items are used to store current binder file context.
720 -- The context is composed of the file descriptor position and the
721 -- current line together with the slice indexes (first and last
722 -- position) for this line. The rollback data are used by the
723 -- Store_File_Context and Rollback_File_Context routines below.
724 -- The file context mechanism interact only with the Get_Next_Line
725 -- call. For example:
727 -- Store_File_Context;
729 -- Rollback_File_Context;
732 -- Both Get_Next_Line calls above will read the exact same data from
733 -- the file. In other words, Next_Line, Nfirst and Nlast variables
734 -- will be set with the exact same values.
736 RB_File_Pos : long; -- File position
737 RB_Next_Line : String (1 .. 1000); -- Current line content
738 RB_Nlast : Integer; -- Slice last index
739 RB_Nfirst : Integer; -- Slice first index
741 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
742 pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
743 -- Pointer to string representing the native linker option which
744 -- specifies the path where the dynamic loader should find shared
745 -- libraries. Equal to null string if this system doesn't support it.
747 Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
749 (C, Object_Library_Ext_Ptr, "__gnat_object_library_extension");
750 -- Pointer to string specifying the default extension for
751 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
753 Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
754 pragma Import (C, Object_File_Option_Ptr, "__gnat_object_file_option");
755 -- Pointer to a string representing the linker option which specifies
756 -- the response file.
758 Using_GNU_Linker : Boolean;
759 for Using_GNU_Linker'Size use Character'Size;
760 pragma Import (C, Using_GNU_Linker, "__gnat_using_gnu_linker");
761 -- Predicate indicating whether this target uses the GNU linker. In
762 -- this case we must output a GNU linker compatible response file.
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
1170 -- library path to find the library location
1173 File_Path : String_Access;
1175 Object_Lib_Extension : constant String :=
1176 Value (Object_Library_Ext_Ptr);
1178 File_Name : constant String := "lib
" &
1179 Next_Line (Nfirst + 2 .. Last) &
1180 Object_Lib_Extension;
1182 Run_Path_Opt : constant String :=
1183 Value (Run_Path_Option_Ptr);
1185 GCC_Index : Natural;
1186 Run_Path_Opt_Index : Natural := 0;
1190 Locate_Regular_File (File_Name,
1191 String (Libpath.Table (1 .. Libpath.Last)));
1193 if File_Path /= null then
1196 -- If static gnatlib found, explicitly
1197 -- specify to overcome possible linker
1198 -- default usage of shared version.
1200 Linker_Options.Increment_Last;
1202 Linker_Options.Table (Linker_Options.Last) :=
1203 new String'(File_Path.all);
1205 elsif GNAT_Shared then
1206 if Opt.Run_Path_Option then
1207 -- If shared gnatlib desired, add the
1208 -- appropriate system specific switch
1209 -- so that it can be located at runtime.
1211 if Run_Path_Opt'Length /= 0 then
1212 -- Output the system specific linker command
1213 -- that allows the image activator to find
1214 -- the shared library at runtime.
1215 -- Also add path to find libgcc_s.so, if
1218 -- To find the location of the shared version
1219 -- of libgcc, we look for "gcc
-lib
" in the
1220 -- path of the library. However, this
1221 -- subdirectory is no longer present in
1222 -- in recent version of GCC. So, we look for
1223 -- the last subdirectory "lib
" in the path.
1226 Index (File_Path.all, "gcc
-lib
");
1228 if GCC_Index /= 0 then
1229 -- The shared version of libgcc is
1230 -- located in the parent directory.
1232 GCC_Index := GCC_Index - 1;
1236 Index (File_Path.all, "/lib
/");
1238 if GCC_Index = 0 then
1240 Index (File_Path.all,
1241 Directory_Separator &
1243 Directory_Separator);
1246 -- We have found a subdirectory "lib
",
1247 -- this is where the shared version of
1248 -- libgcc should be located.
1250 if GCC_Index /= 0 then
1251 GCC_Index := GCC_Index + 3;
1255 -- Look for an eventual run_path_option in
1256 -- the linker switches.
1258 for J in reverse 1 .. Linker_Options.Last loop
1259 if Linker_Options.Table (J) /= null
1261 Linker_Options.Table (J)'Length
1262 > Run_Path_Opt'Length
1264 Linker_Options.Table (J)
1265 (1 .. Run_Path_Opt'Length) =
1268 -- We have found a already specified
1269 -- run_path_option: we will add to this
1270 -- switch, because only one
1271 -- run_path_option should be specified.
1273 Run_Path_Opt_Index := J;
1278 -- If there is no run_path_option, we need
1281 if Run_Path_Opt_Index = 0 then
1282 Linker_Options.Increment_Last;
1285 if GCC_Index = 0 then
1286 if Run_Path_Opt_Index = 0 then
1287 Linker_Options.Table
1288 (Linker_Options.Last) :=
1292 (1 .. File_Path'Length
1293 - File_Name'Length));
1296 Linker_Options.Table
1297 (Run_Path_Opt_Index) :=
1299 (Linker_Options.Table
1300 (Run_Path_Opt_Index).all
1303 (1 .. File_Path'Length
1304 - File_Name'Length));
1308 if Run_Path_Opt_Index = 0 then
1309 Linker_Options.Table
1310 (Linker_Options.Last) :=
1311 new String'(Run_Path_Opt
1313 (1 .. File_Path'Length
1316 & File_Path (1 .. GCC_Index));
1319 Linker_Options.Table
1320 (Run_Path_Opt_Index) :=
1322 (Linker_Options.Table
1323 (Run_Path_Opt_Index).all
1326 (1 .. File_Path'Length
1329 & File_Path (1 .. GCC_Index));
1335 -- Then we add the appropriate -l switch
1337 Linker_Options.Increment_Last;
1338 Linker_Options.Table (Linker_Options.Last) :=
1339 new String'(Next_Line (Nfirst .. Nlast));
1343 -- If gnatlib library not found, then
1344 -- add it anyway in case some other
1345 -- mechanism may find it.
1347 Linker_Options.Increment_Last;
1348 Linker_Options.Table (Linker_Options.Last) :=
1349 new String'(Next_Line (Nfirst .. Nlast));
1353 Linker_Options.Increment_Last;
1354 Linker_Options.Table (Linker_Options.Last) :=
1355 new String'(Next_Line (Nfirst .. Nlast));
1359 Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker
";
1362 exit when Next_Line (Nfirst .. Nlast) = End_Info;
1364 if Ada_Bind_File then
1365 Next_Line (Nfirst .. Nlast - 8) :=
1366 Next_Line (Nfirst + 8 .. Nlast);
1372 -- If -shared was specified, invoke gcc with -shared-libgcc
1375 Linker_Options.Increment_Last;
1376 Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc;
1379 Status := fclose (Fd);
1380 end Process_Binder_File;
1388 Write_Str ("Usage
: ");
1389 Write_Str (Base_Name (Command_Name));
1390 Write_Str (" switches mainprog
.ali
[non
-Ada
-objects
] [linker
-options
]");
1393 Write_Line (" mainprog
.ali the ALI file
of the main program
");
1395 Write_Line (" -A Binder generated source file
is in Ada
(default
)");
1396 Write_Line (" -C Binder generated source file
is in C
");
1397 Write_Line (" -f force object file list to be generated
");
1398 Write_Line (" -g Compile binder source file
with debug information
");
1399 Write_Line (" -n
Do not compile the binder source file
");
1400 Write_Line (" -R
Do not use a run_path_option
");
1401 Write_Line (" -v verbose mode
");
1402 Write_Line (" -v
-v very verbose mode
");
1404 Write_Line (" -o nam
Use 'nam' as the name
of the executable
");
1405 Write_Line (" -b target Compile the binder source to run on target
");
1406 Write_Line (" -Bdir Load compiler executables from dir
");
1408 if Is_Supported (Map_File) then
1409 Write_Line (" -Mmap Create map file map
");
1410 Write_Line (" -M Create map file mainprog
.map
");
1413 Write_Line (" --GCC=comp Use comp as the compiler");
1414 Write_Line
(" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1416 Write_Line
(" [non-Ada-objects] list of non Ada object files");
1417 Write_Line
(" [linker-options] other options for the linker");
1424 procedure Write_Header
is
1426 if Verbose_Mode
then
1428 Display_Version
("GNATLINK", "1995");
1436 procedure Write_Usage
is
1442 -- Start of processing for Gnatlink
1445 -- Add the directory where gnatlink is invoked in front of the
1446 -- path, if gnatlink is invoked with directory information.
1447 -- Only do this if the platform is not VMS, where the notion of path
1448 -- does not really exist.
1450 if not Hostparm
.OpenVMS
then
1452 Command
: constant String := Command_Name
;
1455 for Index
in reverse Command
'Range loop
1456 if Command
(Index
) = Directory_Separator
then
1458 Absolute_Dir
: constant String :=
1460 (Command
(Command
'First .. Index
));
1462 PATH
: constant String :=
1465 Getenv
("PATH").all;
1468 Setenv
("PATH", PATH
);
1479 if Argument_Count
= 0
1481 (Verbose_Mode
and then Argument_Count
= 1)
1484 Exit_Program
(E_Fatal
);
1487 -- Initialize packages to be used
1493 -- We always compile with -c
1495 Binder_Options_From_ALI
.Increment_Last
;
1496 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1499 if Ali_File_Name = null then
1500 Exit_With_Error ("no ali file given for link");
1503 if not Is_Regular_File (Ali_File_Name.all) then
1504 Exit_With_Error (Ali_File_Name.all & " not found");
1507 -- Read the ALI file of the main subprogram if the binder generated
1508 -- file needs to be compiled and no --GCC= switch has been specified.
1509 -- Fetch the back end switches from this ALI file and use these switches
1510 -- to compile the binder generated file
1512 if Compile_Bind_File and then Standard_Gcc then
1515 Name_Len := Ali_File_Name'Length;
1516 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1520 F : constant File_Name_Type := Name_Find;
1521 T : Text_Buffer_Ptr;
1525 -- Load the ALI file
1527 T := Read_Library_Info (F, True);
1529 -- Read it. Note that we ignore errors, since we only want very
1530 -- limited information from the ali file, and likely a slightly
1531 -- wrong version will be just fine, though in normal operation
1532 -- we don't expect this to happen!
1539 Ignore_Errors => True);
1541 if A /= No_ALI_Id then
1543 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1544 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1546 -- Do not compile with the front end switches. However, --RTS
1547 -- is to be dealt with specially because it needs to be passed
1548 -- if the binder-generated file is in Ada and may also be used
1549 -- to drive the linker.
1552 Arg : String_Ptr renames Args.Table (Index);
1554 if not Is_Front_End_Switch (Arg.all) then
1555 Binder_Options_From_ALI.Increment_Last;
1556 Binder_Options_From_ALI.Table
1557 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1559 elsif Arg'Length > 5
1560 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1562 if Ada_Bind_File then
1563 Binder_Options_From_ALI.Increment_Last;
1564 Binder_Options_From_ALI.Table
1565 (Binder_Options_From_ALI.Last)
1566 := String_Access (Arg);
1569 -- Set the RTS_*_Path_Name variables, so that the
1570 -- correct directories will be set when
1571 -- Osint.Add_Default_Search_Dirs will be called later.
1573 Opt.RTS_Src_Path_Name :=
1575 (Arg (Arg'First + 6 .. Arg'Last), Include);
1577 Opt.RTS_Lib_Path_Name :=
1579 (Arg (Arg'First + 6 .. Arg'Last), Objects);
1581 -- GNAT doesn't support the GCC multilib mechanism.
1582 -- This means that, when a multilib switch is used
1583 -- to request a particular compilation mode, the
1584 -- corresponding runtime switch (--RTS) must also be
1585 -- specified. The long-term goal is to fully support the
1586 -- multilib mechanism; however, in the meantime, it is
1587 -- convenient to eliminate the redundancy by keying the
1588 -- compilation mode on a single switch, namely --RTS.
1590 -- Pass -mrtp to the linker if --RTS=rtp was passed
1593 and then Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1595 Linker_Options.Increment_Last;
1596 Linker_Options.Table (Linker_Options.Last) :=
1597 new String'("-mrtp");
1599 -- Pass -fsjlj to the linker if --RTS=sjlj was passed
1601 elsif Arg
'Length > 9
1602 and then Arg
(Arg
'First + 6 .. Arg
'First + 9) = "sjlj"
1604 Linker_Options
.Increment_Last
;
1605 Linker_Options
.Table
(Linker_Options
.Last
) :=
1606 new String'("-fsjlj");
1615 -- Get target parameters
1617 Osint.Add_Default_Search_Dirs;
1618 Targparm.Get_Target_Parameters;
1620 if VM_Target /= No_VM then
1622 when JVM_Target => Gcc := new String'("jgnat");
1623 when CLI_Target
=> Gcc
:= new String'("dotnet-gnatcompile");
1624 when No_VM => raise Program_Error;
1627 Ada_Bind_File := True;
1628 Begin_Info := "-- BEGIN Object file/option list";
1629 End_Info := "-- END Object file/option list ";
1632 -- If the main program is in Ada it is compiled with the following
1635 -- -gnatA stops reading gnat.adc, since we don't know what
1636 -- pragmas would work, and we do not need it anyway.
1638 -- -gnatWb allows brackets coding for wide characters
1640 -- -gnatiw allows wide characters in identifiers. This is needed
1641 -- because bindgen uses brackets encoding for all upper
1642 -- half and wide characters in identifier names.
1644 if Ada_Bind_File then
1645 Binder_Options_From_ALI.Increment_Last;
1646 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1647 new String'("-gnatA");
1648 Binder_Options_From_ALI
.Increment_Last
;
1649 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1650 new String'("-gnatWb");
1651 Binder_Options_From_ALI.Increment_Last;
1652 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1653 new String'("-gnatiw");
1656 -- Locate all the necessary programs and verify required files are present
1658 Gcc_Path
:= System
.OS_Lib
.Locate_Exec_On_Path
(Gcc
.all);
1660 if Gcc_Path
= null then
1661 Exit_With_Error
("Couldn't locate " & Gcc
.all);
1664 if Linker_Path
= null then
1665 if VM_Target
= CLI_Target
then
1666 Linker_Path
:= System
.OS_Lib
.Locate_Exec_On_Path
("ilasm");
1668 if Linker_Path
= null then
1669 Exit_With_Error
("Couldn't locate ilasm");
1672 elsif RTX_RTSS_Kernel_Module_On_Target
then
1674 -- Use Microsoft linker for RTSS modules
1676 Linker_Path
:= System
.OS_Lib
.Locate_Exec_On_Path
("link");
1678 if Linker_Path
= null then
1679 Exit_With_Error
("Couldn't locate link");
1683 Linker_Path
:= Gcc_Path
;
1689 -- If no output name specified, then use the base name of .ali file name
1691 if Output_File_Name
= null then
1693 new String'(Base_Name (Ali_File_Name.all)
1694 & Get_Target_Debuggable_Suffix.all);
1696 if VM_Target = CLI_Target then
1697 Linker_Options.Increment_Last;
1698 Linker_Options.Table (Linker_Options.Last) := new String'("/QUIET");
1700 Linker_Options
.Increment_Last
;
1701 Linker_Options
.Table
(Linker_Options
.Last
) := new String'("/DEBUG");
1703 Linker_Options.Increment_Last;
1704 Linker_Options.Table (Linker_Options.Last) :=
1705 new String'("/OUTPUT=" & Output_File_Name
.all);
1707 elsif RTX_RTSS_Kernel_Module_On_Target
then
1708 Linker_Options
.Increment_Last
;
1709 Linker_Options
.Table
(Linker_Options
.Last
) :=
1710 new String'("/OUT:" & Output_File_Name.all);
1713 Linker_Options.Increment_Last;
1714 Linker_Options.Table (Linker_Options.Last) := new String'("-o");
1716 Linker_Options
.Increment_Last
;
1717 Linker_Options
.Table
(Linker_Options
.Last
) :=
1718 new String'(Output_File_Name.all);
1722 -- Warn if main program is called "test", as that may be a built-in command
1723 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1724 -- will not appear. However, do not warn in the case of a cross compiler.
1726 -- Assume this is a cross tool if the executable name is not gnatlink
1728 if Base_Name (Command_Name) = "gnatlink"
1729 and then Output_File_Name.all = "test"
1731 Error_Msg ("warning: executable name """ & Output_File_Name.all
1732 & """ may conflict with shell command");
1735 -- If -M switch was specified, add the switches to create the map file
1737 if Create_Map_File then
1739 Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map";
1740 Switches : String_List_Access;
1743 Convert (Map_File, Map_Name, Switches);
1745 if Switches /= null then
1746 for J in Switches'Range loop
1747 Linker_Options.Increment_Last;
1748 Linker_Options.Table (Linker_Options.Last) := Switches (J);
1754 -- Perform consistency checks
1756 -- Transform the .ali file name into the binder output file name
1758 Make_Binder_File_Names : declare
1759 Fname : constant String := Base_Name (Ali_File_Name.all);
1760 Fname_Len : Integer := Fname'Length;
1762 function Get_Maximum_File_Name_Length return Integer;
1763 pragma Import (C, Get_Maximum_File_Name_Length,
1764 "__gnat_get_maximum_file_name_length");
1766 Maximum_File_Name_Length : constant Integer :=
1767 Get_Maximum_File_Name_Length;
1769 Bind_File_Prefix : Types.String_Ptr;
1770 -- Contains prefix used for bind files
1775 if not Ada_Bind_File then
1776 Bind_File_Prefix := new String'("b_");
1777 elsif OpenVMS_On_Target
then
1778 Bind_File_Prefix
:= new String'("b__");
1780 Bind_File_Prefix := new String'("b~");
1783 -- If the length of the binder file becomes too long due to
1784 -- the addition of the "b?" prefix, then truncate it.
1786 if Maximum_File_Name_Length
> 0 then
1788 Maximum_File_Name_Length
- Bind_File_Prefix
.all'Length
1790 Fname_Len
:= Fname_Len
- 1;
1795 Fnam
: constant String :=
1796 Bind_File_Prefix
.all &
1797 Fname
(Fname
'First .. Fname
'First + Fname_Len
- 1);
1800 if Ada_Bind_File
then
1801 Binder_Spec_Src_File
:= new String'(Fnam & ".ads");
1802 Binder_Body_Src_File := new String'(Fnam
& ".adb");
1803 Binder_Ali_File
:= new String'(Fnam & ".ali");
1805 Binder_Body_Src_File := new String'(Fnam
& ".c");
1808 Binder_Obj_File
:= new String'(Fnam & Get_Target_Object_Suffix.all);
1811 if Fname_Len /= Fname'Length then
1812 Binder_Options.Increment_Last;
1813 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1814 Binder_Options
.Increment_Last
;
1815 Binder_Options
.Table
(Binder_Options
.Last
) := Binder_Obj_File
;
1817 end Make_Binder_File_Names
;
1819 Process_Binder_File
(Binder_Body_Src_File
.all & ASCII
.NUL
);
1821 -- Compile the binder file. This is fast, so we always do it, unless
1822 -- specifically told not to by the -n switch
1824 if Compile_Bind_File
then
1827 Args
: Argument_List
1828 (1 .. Binder_Options_From_ALI
.Last
+ Binder_Options
.Last
+ 1);
1831 for J
in 1 .. Binder_Options_From_ALI
.Last
loop
1832 Args
(J
) := Binder_Options_From_ALI
.Table
(J
);
1835 for J
in 1 .. Binder_Options
.Last
loop
1836 Args
(Binder_Options_From_ALI
.Last
+ J
) :=
1837 Binder_Options
.Table
(J
);
1840 -- Use the full path of the binder generated source, so that it is
1841 -- guaranteed that the debugger will find this source, even with
1845 new String'(Normalize_Pathname (Binder_Body_Src_File.all));
1847 if Verbose_Mode then
1848 Write_Str (Base_Name (Gcc_Path.all));
1850 for J in Args'Range loop
1852 Write_Str (Args (J).all);
1858 System.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1861 Exit_Program (E_Fatal);
1866 -- Now, actually link the program
1868 -- Skip this step for now on JVM since the Java interpreter will do
1869 -- the actual link at run time. We might consider packing all class files
1870 -- in a .zip file during this step.
1872 if VM_Target /= JVM_Target then
1874 Num_Args : Natural :=
1875 (Linker_Options.Last - Linker_Options.First + 1) +
1876 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1877 (Linker_Objects.Last - Linker_Objects.First + 1);
1878 Stack_Op : Boolean := False;
1879 IDENT_Op : Boolean := False;
1882 if VM_Target = CLI_Target then
1884 -- Remove extraneous flags not relevant for CIL. Also remove empty
1885 -- arguments, since ilasm chokes on them.
1887 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1888 if Linker_Options.Table (J)'Length = 0
1889 or else Linker_Options.Table (J) (1 .. 2) = "-L"
1890 or else Linker_Options.Table (J) (1 .. 2) = "-l"
1891 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1892 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1893 or else Linker_Options.Table (J) (1 .. 2) = "-g"
1895 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1896 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1897 Linker_Options.Decrement_Last;
1898 Num_Args := Num_Args - 1;
1902 elsif RTX_RTSS_Kernel_Module_On_Target then
1904 -- Remove flags not relevant for Microsoft linker and adapt some
1907 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1909 -- Remove flags that are not accepted
1910 if Linker_Options.Table (J)'Length = 0
1911 or else Linker_Options.Table (J) (1 .. 2) = "-l"
1912 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1913 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1914 or else Linker_Options.Table (J) (1 .. 8) = "-Xlinker"
1915 or else Linker_Options.Table (J) (1 .. 9) = "-mthreads"
1917 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1918 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1919 Linker_Options.Decrement_Last;
1920 Num_Args := Num_Args - 1;
1922 -- Replace "-L" by its counterpart "/LIBPATH:" and UNIX "/" by
1924 elsif Linker_Options.Table (J) (1 .. 2) = "-L" then
1926 Libpath_Option : constant String_Access := new String'
1928 Linker_Options
.Table
(J
)
1929 (3 .. Linker_Options
.Table
(J
).all'Last));
1931 for Index
in 10 .. Libpath_Option
'Last loop
1932 if Libpath_Option
(Index
) = '/' then
1933 Libpath_Option
(Index
) := '\';
1937 Linker_Options
.Table
(J
) := Libpath_Option
;
1940 -- Replace "-g" by "/DEBUG"
1941 elsif Linker_Options
.Table
(J
) (1 .. 2) = "-g" then
1942 Linker_Options
.Table
(J
) := new String'("/DEBUG");
1944 -- Replace "-o" by "/OUT:"
1945 elsif Linker_Options.Table (J) (1 .. 2) = "-o" then
1946 Linker_Options.Table (J + 1) := new String'
1947 ("/OUT:" & Linker_Options
.Table
(J
+ 1).all);
1949 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
1950 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
1951 Linker_Options
.Decrement_Last
;
1952 Num_Args
:= Num_Args
- 1;
1954 -- Replace "--stack=" by "/STACK:"
1955 elsif Linker_Options
.Table
(J
) (1 .. 8) = "--stack=" then
1956 Linker_Options
.Table
(J
) := new String'
1958 Linker_Options.Table (J)
1959 (9 .. Linker_Options.Table (J).all'Last));
1961 -- Replace "-v" by its counterpart "/VERBOSE"
1962 elsif Linker_Options.Table (J) (1 .. 2) = "-v" then
1963 Linker_Options.Table (J) := new String'("/VERBOSE");
1967 -- Add some required flags to create RTSS modules
1970 Flags_For_Linker
: constant array (1 .. 17) of String_Access
:=
1971 (new String'("/NODEFAULTLIB"),
1972 new String'("/INCREMENTAL:NO"),
1973 new String'("/NOLOGO"),
1974 new String'("/DRIVER"),
1975 new String'("/ALIGN:0x20"),
1976 new String'("/SUBSYSTEM:NATIVE"),
1977 new String'("/ENTRY:_RtapiProcessEntryCRT@8"),
1978 new String'("/RELEASE"),
1979 new String'("startupCRT.obj"),
1980 new String'("rtxlibcmt.lib"),
1981 new String'("oldnames.lib"),
1982 new String'("rtapi_rtss.lib"),
1983 new String'("Rtx_Rtss.lib"),
1984 new String'("libkernel32.a"),
1985 new String'("libws2_32.a"),
1986 new String'("libmswsock.a"),
1987 new String'("libadvapi32.a"));
1988 -- These flags need to be passed to Microsoft linker. They
1989 -- come from the RTX documentation.
1991 Gcc_Lib_Path : constant String_Access := new String'
1992 ("/LIBPATH:" & Include_Dir_Default_Prefix
& "\..\");
1993 -- Place to look for gcc related libraries, such as libgcc
1996 -- Replace UNIX "/" by Windows "\" in the path
1998 for Index
in 10 .. Gcc_Lib_Path
.all'Last loop
1999 if Gcc_Lib_Path
(Index
) = '/' then
2000 Gcc_Lib_Path
(Index
) := '\';
2004 Linker_Options
.Increment_Last
;
2005 Linker_Options
.Table
(Linker_Options
.Last
) := Gcc_Lib_Path
;
2006 Num_Args
:= Num_Args
+ 1;
2008 for Index
in Flags_For_Linker
'Range loop
2009 Linker_Options
.Increment_Last
;
2010 Linker_Options
.Table
(Linker_Options
.Last
) :=
2011 Flags_For_Linker
(Index
);
2012 Num_Args
:= Num_Args
+ 1;
2017 -- Remove duplicate stack size setting from the Linker_Options
2018 -- table. The stack setting option "-Xlinker --stack=R,C" can be
2019 -- found in one line when set by a pragma Linker_Options or in two
2020 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
2021 -- line. We also check for the "-Wl,--stack=R" style option.
2023 -- We must remove the second stack setting option instance
2024 -- because the one on the command line will always be the first
2025 -- one. And any subsequent stack setting option will overwrite the
2026 -- previous one. This is done especially for GNAT/NT where we set
2027 -- the stack size for tasking programs by a pragma in the NT
2028 -- specific tasking package System.Task_Primitives.Operations.
2030 -- Note: This is not a FOR loop that runs from Linker_Options.First
2031 -- to Linker_Options.Last, since operations within the loop can
2032 -- modify the length of the table.
2034 Clean_Link_Option_Set
: declare
2035 J
: Natural := Linker_Options
.First
;
2036 Shared_Libgcc_Seen
: Boolean := False;
2039 while J
<= Linker_Options
.Last
loop
2041 if Linker_Options
.Table
(J
).all = "-Xlinker"
2042 and then J
< Linker_Options
.Last
2043 and then Linker_Options
.Table
(J
+ 1)'Length > 8
2044 and then Linker_Options
.Table
(J
+ 1) (1 .. 8) = "--stack="
2047 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 2) :=
2048 Linker_Options
.Table
(J
+ 2 .. Linker_Options
.Last
);
2049 Linker_Options
.Decrement_Last
;
2050 Linker_Options
.Decrement_Last
;
2051 Num_Args
:= Num_Args
- 2;
2058 -- Remove duplicate -shared-libgcc switch
2060 if Linker_Options
.Table
(J
).all = Shared_Libgcc_String
then
2061 if Shared_Libgcc_Seen
then
2062 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
2063 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
2064 Linker_Options
.Decrement_Last
;
2065 Num_Args
:= Num_Args
- 1;
2068 Shared_Libgcc_Seen
:= True;
2072 -- Here we just check for a canonical form that matches the
2073 -- pragma Linker_Options set in the NT runtime.
2075 if (Linker_Options
.Table
(J
)'Length > 17
2076 and then Linker_Options
.Table
(J
) (1 .. 17)
2077 = "-Xlinker --stack=")
2079 (Linker_Options
.Table
(J
)'Length > 12
2080 and then Linker_Options
.Table
(J
) (1 .. 12)
2084 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
2085 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
2086 Linker_Options
.Decrement_Last
;
2087 Num_Args
:= Num_Args
- 1;
2094 -- Remove duplicate IDENTIFICATION directives (VMS)
2096 if Linker_Options
.Table
(J
)'Length > 27
2097 and then Linker_Options
.Table
(J
) (1 .. 28)
2098 = "--for-linker=IDENTIFICATION="
2101 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
2102 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
2103 Linker_Options
.Decrement_Last
;
2104 Num_Args
:= Num_Args
- 1;
2113 if Linker_Path
= Gcc_Path
and then VM_Target
= No_VM
then
2115 -- If gcc is not called with -shared-libgcc, call it with
2116 -- -static-libgcc, as there are some platforms where one of
2117 -- these two switches is compulsory to link.
2119 if not Shared_Libgcc_Seen
then
2120 Linker_Options
.Increment_Last
;
2121 Linker_Options
.Table
(Linker_Options
.Last
) := Static_Libgcc
;
2122 Num_Args
:= Num_Args
+ 1;
2125 elsif RTX_RTSS_Kernel_Module_On_Target
then
2127 -- Force the use of the static libgcc for RTSS modules
2129 Linker_Options
.Increment_Last
;
2130 Linker_Options
.Table
(Linker_Options
.Last
) :=
2131 new String'("libgcc.a");
2132 Num_Args := Num_Args + 1;
2135 end Clean_Link_Option_Set;
2137 -- Prepare arguments for call to linker
2139 Call_Linker : declare
2141 Args : Argument_List (1 .. Num_Args + 1);
2142 Index : Integer := Args'First;
2145 Args (Index) := Binder_Obj_File;
2147 -- Add the object files and any -largs libraries
2149 for J in Linker_Objects.First .. Linker_Objects.Last loop
2151 Args (Index) := Linker_Objects.Table (J);
2154 -- Add the linker options from the binder file
2156 for J in Linker_Options.First .. Linker_Options.Last loop
2158 Args (Index) := Linker_Options.Table (J);
2161 -- Finally add the libraries from the --GCC= switch
2163 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
2165 Args (Index) := Gcc_Linker_Options.Table (J);
2168 if Verbose_Mode then
2169 Write_Str (Linker_Path.all);
2171 for J in Args'Range loop
2173 Write_Str (Args (J).all);
2178 -- If we are on very verbose mode (-v -v) and a response file
2179 -- is used we display its content.
2181 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
2183 Write_Str ("Response file (" &
2184 Tname (Tname'First .. Tname'Last - 1) &
2189 Response_File_Objects.First ..
2190 Response_File_Objects.Last
2192 Write_Str (Response_File_Objects.Table (J).all);
2200 System.OS_Lib.Spawn (Linker_Path.all, Args, Success);
2202 -- Delete the temporary file used in conjunction with linking if
2203 -- one was created. See Process_Bind_File for details.
2205 if Tname_FD /= Invalid_FD then
2209 if Lname /= null then
2210 Delete (Lname.all & ASCII.NUL);
2214 Error_Msg ("error when calling " & Linker_Path.all);
2215 Exit_Program (E_Fatal);
2221 -- Only keep the binder output file and it's associated object
2222 -- file if compiling with the -g option. These files are only
2223 -- useful if debugging.
2225 if not Debug_Flag_Present then
2226 if Binder_Ali_File /= null then
2227 Delete (Binder_Ali_File.all & ASCII.NUL);
2230 if Binder_Spec_Src_File /= null then
2231 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
2234 Delete (Binder_Body_Src_File.all & ASCII.NUL);
2236 if VM_Target = No_VM then
2237 Delete (Binder_Obj_File.all & ASCII.NUL);
2241 Exit_Program (E_Success);
2245 Write_Line (Exception_Information (X));
2246 Exit_With_Error ("INTERNAL ERROR. Please report");