1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2012, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- Gnatlink usage: please consult the gnat documentation
30 with Gnatvsn
; use Gnatvsn
;
32 with Indepsw
; use Indepsw
;
33 with Namet
; use Namet
;
35 with Osint
; use Osint
;
36 with Output
; use Output
;
38 with Switch
; use Switch
;
39 with System
; use System
;
41 with Targparm
; use Targparm
;
44 with Ada
.Command_Line
; use Ada
.Command_Line
;
45 with Ada
.Exceptions
; use Ada
.Exceptions
;
47 with System
.OS_Lib
; use System
.OS_Lib
;
50 with Interfaces
.C_Streams
; use Interfaces
.C_Streams
;
51 with Interfaces
.C
.Strings
; use Interfaces
.C
.Strings
;
54 pragma Ident
(Gnatvsn
.Gnat_Static_Version_String
);
56 Shared_Libgcc_String
: constant String := "-shared-libgcc";
57 Shared_Libgcc
: constant String_Access
:=
58 new String'(Shared_Libgcc_String);
59 -- Used to invoke gcc when the binder is invoked with -shared
61 Static_Libgcc_String : constant String := "-static-libgcc";
62 Static_Libgcc : constant String_Access :=
63 new String'(Static_Libgcc_String
);
64 -- Used to invoke gcc when shared libs are not used
66 package Gcc_Linker_Options
is new Table
.Table
(
67 Table_Component_Type
=> String_Access
,
68 Table_Index_Type
=> Integer,
71 Table_Increment
=> 100,
72 Table_Name
=> "Gnatlink.Gcc_Linker_Options");
73 -- Comments needed ???
75 package Libpath
is new Table
.Table
(
76 Table_Component_Type
=> Character,
77 Table_Index_Type
=> Integer,
79 Table_Initial
=> 4096,
80 Table_Increment
=> 100,
81 Table_Name
=> "Gnatlink.Libpath");
82 -- Comments needed ???
84 package Linker_Options
is new Table
.Table
(
85 Table_Component_Type
=> String_Access
,
86 Table_Index_Type
=> Integer,
89 Table_Increment
=> 100,
90 Table_Name
=> "Gnatlink.Linker_Options");
91 -- Comments needed ???
93 package Linker_Objects
is new Table
.Table
(
94 Table_Component_Type
=> String_Access
,
95 Table_Index_Type
=> Integer,
98 Table_Increment
=> 100,
99 Table_Name
=> "Gnatlink.Linker_Objects");
100 -- This table collects the objects file to be passed to the linker. In the
101 -- case where the linker command line is too long then programs objects
102 -- are put on the Response_File_Objects table. Note that the binder object
103 -- file and the user's objects remain in this table. This is very
104 -- important because on the GNU linker command line the -L switch is not
105 -- used to look for objects files but -L switch is used to look for
106 -- objects listed in the response file. This is not a problem with the
107 -- applications objects as they are specified with a full name.
109 package Response_File_Objects
is new Table
.Table
(
110 Table_Component_Type
=> String_Access
,
111 Table_Index_Type
=> Integer,
112 Table_Low_Bound
=> 1,
114 Table_Increment
=> 100,
115 Table_Name
=> "Gnatlink.Response_File_Objects");
116 -- This table collects the objects file that are to be put in the response
117 -- file. Only application objects are collected there (see details in
118 -- Linker_Objects table comments)
120 package Binder_Options_From_ALI
is new Table
.Table
(
121 Table_Component_Type
=> String_Access
,
122 Table_Index_Type
=> Integer,
123 Table_Low_Bound
=> 1, -- equals low bound of Argument_List for Spawn
125 Table_Increment
=> 100,
126 Table_Name
=> "Gnatlink.Binder_Options_From_ALI");
127 -- This table collects the switches from the ALI file of the main
130 package Binder_Options
is new Table
.Table
(
131 Table_Component_Type
=> String_Access
,
132 Table_Index_Type
=> Integer,
133 Table_Low_Bound
=> 1, -- equals low bound of Argument_List for Spawn
135 Table_Increment
=> 100,
136 Table_Name
=> "Gnatlink.Binder_Options");
137 -- This table collects the arguments to be passed to compile the binder
140 Gcc
: String_Access
:= Program_Name
("gcc", "gnatlink");
142 Read_Mode
: constant String := "r" & ASCII
.NUL
;
144 Begin_Info
: constant String := "-- BEGIN Object file/option list";
145 End_Info
: constant String := "-- END Object file/option list ";
147 Gcc_Path
: String_Access
;
148 Linker_Path
: String_Access
;
149 Output_File_Name
: String_Access
;
150 Ali_File_Name
: String_Access
;
151 Binder_Spec_Src_File
: String_Access
;
152 Binder_Body_Src_File
: String_Access
;
153 Binder_Ali_File
: String_Access
;
154 Binder_Obj_File
: String_Access
;
156 Tname
: Temp_File_Name
;
157 Tname_FD
: File_Descriptor
:= Invalid_FD
;
158 -- Temporary file used by linker to pass list of object files on
159 -- certain systems with limitations on size of arguments.
161 Debug_Flag_Present
: Boolean := False;
162 Verbose_Mode
: Boolean := False;
163 Very_Verbose_Mode
: Boolean := False;
165 Standard_Gcc
: Boolean := True;
167 Compile_Bind_File
: Boolean := True;
168 -- Set to False if bind file is not to be compiled
170 Create_Map_File
: Boolean := False;
171 -- Set to True by switch -M. The map file name is derived from
172 -- the ALI file name (mainprog.ali => mainprog.map).
174 Object_List_File_Supported
: Boolean;
175 for Object_List_File_Supported
'Size use Character'Size;
177 (C
, Object_List_File_Supported
, "__gnat_objlist_file_supported");
178 -- Predicate indicating whether the linker has an option whereby the
179 -- names of object files can be passed to the linker in a file.
181 Object_File_Option_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
182 pragma Import
(C
, Object_File_Option_Ptr
, "__gnat_object_file_option");
183 -- Pointer to a string representing the linker option which specifies
184 -- the response file.
186 Object_File_Option
: constant String := Value
(Object_File_Option_Ptr
);
187 -- The linker option which specifies the response file as a string
189 Using_GNU_response_file
: constant Boolean :=
190 Object_File_Option
'Length > 0
191 and then Object_File_Option
(Object_File_Option
'Last) = '@';
192 -- Whether a GNU response file is used
194 Object_List_File_Required
: Boolean := False;
195 -- Set to True to force generation of a response file
197 Shared_Libgcc_Default
: Character;
198 for Shared_Libgcc_Default
'Size use Character'Size;
200 (C
, Shared_Libgcc_Default
, "__gnat_shared_libgcc_default");
201 -- Indicates wether libgcc should be statically linked (use 'T') or
202 -- dynamically linked (use 'H') by default.
204 function Base_Name
(File_Name
: String) return String;
205 -- Return just the file name part without the extension (if present)
207 procedure Check_Existing_Executable
(File_Name
: String);
208 -- Delete any existing executable to avoid accidentally updating the target
209 -- of a symbolic link, but produce a Fatail_Error if File_Name matches any
210 -- of the source file names. This avoids overwriting of extensionless
211 -- source files by accident on systems where executables do not have
214 procedure Delete
(Name
: String);
215 -- Wrapper to unlink as status is ignored by this application
217 procedure Error_Msg
(Message
: String);
218 -- Output the error or warning Message
220 procedure Exit_With_Error
(Error
: String);
221 -- Output Error and exit program with a fatal condition
223 procedure Process_Args
;
224 -- Go through all the arguments and build option tables
226 procedure Process_Binder_File
(Name
: String);
227 -- Reads the binder file and extracts linker arguments
232 procedure Write_Header
;
233 -- Show user the program name, version and copyright
235 procedure Write_Usage
;
236 -- Show user the program options
242 function Base_Name
(File_Name
: String) return String is
247 Findex1
:= File_Name
'First;
249 -- The file might be specified by a full path name. However,
250 -- we want the path to be stripped away.
252 for J
in reverse File_Name
'Range loop
253 if Is_Directory_Separator
(File_Name
(J
)) then
259 Findex2
:= File_Name
'Last;
260 while Findex2
> Findex1
261 and then File_Name
(Findex2
) /= '.'
263 Findex2
:= Findex2
- 1;
266 if Findex2
= Findex1
then
267 Findex2
:= File_Name
'Last + 1;
270 return File_Name
(Findex1
.. Findex2
- 1);
273 -------------------------------
274 -- Check_Existing_Executable --
275 -------------------------------
277 procedure Check_Existing_Executable
(File_Name
: String) is
278 Ename
: String := File_Name
;
279 Efile
: File_Name_Type
;
280 Sfile
: File_Name_Type
;
283 Canonical_Case_File_Name
(Ename
);
285 Add_Str_To_Name_Buffer
(Ename
);
288 for J
in Units
.Table
'First .. Units
.Last
loop
289 Sfile
:= Units
.Table
(J
).Sfile
;
290 if Sfile
= Efile
then
291 Exit_With_Error
("executable name """ & File_Name
& """ matches "
292 & "source file name """ & Get_Name_String
(Sfile
) & """");
297 end Check_Existing_Executable
;
303 procedure Delete
(Name
: String) is
305 pragma Unreferenced
(Status
);
307 Status
:= unlink
(Name
'Address);
308 -- Is it really right to ignore an error here ???
315 procedure Error_Msg
(Message
: String) is
317 Write_Str
(Base_Name
(Command_Name
));
323 ---------------------
324 -- Exit_With_Error --
325 ---------------------
327 procedure Exit_With_Error
(Error
: String) is
330 Exit_Program
(E_Fatal
);
337 procedure Process_Args
is
339 Skip_Next
: Boolean := False;
340 -- Set to true if the next argument is to be added into the list of
341 -- linker's argument without parsing it.
343 procedure Check_Version_And_Help
is new Check_Version_And_Help_G
(Usage
);
345 -- Start of processing for Process_Args
348 -- First, check for --version and --help
350 Check_Version_And_Help
("GNATLINK", "1995");
352 -- Loop through arguments of gnatlink command
356 exit when Next_Arg
> Argument_Count
;
358 Process_One_Arg
: declare
359 Arg
: constant String := Argument
(Next_Arg
);
362 -- Case of argument which is a switch
364 -- We definitely need section by section comments here ???
368 -- This argument must not be parsed, just add it to the
369 -- list of linker's options.
373 Linker_Options
.Increment_Last
;
374 Linker_Options
.Table
(Linker_Options
.Last
) :=
377 elsif Arg'Length /= 0 and then Arg (1) = '-' then
378 if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then
380 ("invalid switch: """ & Arg & """ (gnat not needed here)");
383 if Arg = "-Xlinker" then
385 -- Next argument should be sent directly to the linker.
386 -- We do not want to parse it here.
390 Linker_Options.Increment_Last;
391 Linker_Options.Table (Linker_Options.Last) :=
395 and then (Arg
'Length < 5 or else Arg
(2 .. 5) /= "gnat")
397 Debug_Flag_Present
:= True;
399 Linker_Options
.Increment_Last
;
400 Linker_Options
.Table
(Linker_Options
.Last
) :=
403 Binder_Options.Increment_Last;
404 Binder_Options.Table (Binder_Options.Last) :=
405 Linker_Options.Table (Linker_Options.Last);
407 elsif Arg'Length >= 3 and then Arg (2) = 'M
' then
409 Switches : String_List_Access;
412 Convert (Map_File, Arg (3 .. Arg'Last), Switches);
414 if Switches /= null then
415 for J in Switches'Range loop
416 Linker_Options.Increment_Last;
417 Linker_Options.Table (Linker_Options.Last) :=
423 elsif Arg'Length = 2 then
426 Linker_Options.Increment_Last;
427 Linker_Options.Table (Linker_Options.Last) :=
430 Binder_Options
.Increment_Last
;
431 Binder_Options
.Table
(Binder_Options
.Last
) :=
432 Linker_Options
.Table
(Linker_Options
.Last
);
434 Next_Arg
:= Next_Arg
+ 1;
436 if Next_Arg
> Argument_Count
then
437 Exit_With_Error
("Missing argument for -b");
440 Get_Machine_Name
: declare
441 Name_Arg
: constant String_Access
:=
442 new String'(Argument (Next_Arg));
445 Linker_Options.Increment_Last;
446 Linker_Options.Table (Linker_Options.Last) :=
449 Binder_Options.Increment_Last;
450 Binder_Options.Table (Binder_Options.Last) :=
453 end Get_Machine_Name;
456 if Object_List_File_Supported then
457 Object_List_File_Required := True;
460 ("Object list file not supported on this target");
464 Create_Map_File := True;
467 Compile_Bind_File := False;
470 Next_Arg := Next_Arg + 1;
472 if Next_Arg > Argument_Count then
473 Exit_With_Error ("Missing argument for -o");
477 new String'(Executable_Name
478 (Argument
(Next_Arg
),
479 Only_If_No_Suffix
=> True));
482 Opt
.CodePeer_Mode
:= True;
485 Opt
.Run_Path_Option
:= False;
489 -- Support "double" verbose mode. Second -v
490 -- gets sent to the linker and binder phases.
493 Very_Verbose_Mode
:= True;
495 Linker_Options
.Increment_Last
;
496 Linker_Options
.Table
(Linker_Options
.Last
) :=
499 Binder_Options.Increment_Last;
500 Binder_Options.Table (Binder_Options.Last) :=
501 Linker_Options.Table (Linker_Options.Last);
504 Verbose_Mode := True;
509 Linker_Options.Increment_Last;
510 Linker_Options.Table (Linker_Options.Last) :=
515 elsif Arg
(2) = 'B' then
516 Linker_Options
.Increment_Last
;
517 Linker_Options
.Table
(Linker_Options
.Last
) :=
520 Binder_Options.Increment_Last;
521 Binder_Options.Table (Binder_Options.Last) :=
522 Linker_Options.Table (Linker_Options.Last);
524 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
525 if Arg'Length = 7 then
526 Exit_With_Error ("Missing argument for --LINK=");
530 System.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
532 if Linker_Path = null then
534 ("Could not locate linker: " & Arg (8 .. Arg'Last));
537 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
539 Program_Args : constant Argument_List_Access :=
540 Argument_String_To_List
541 (Arg (7 .. Arg'Last));
544 if Program_Args.all (1).all /= Gcc.all then
545 Gcc := new String'(Program_Args
.all (1).all);
546 Standard_Gcc
:= False;
549 -- Set appropriate flags for switches passed
551 for J
in 2 .. Program_Args
.all'Last loop
553 Arg
: constant String := Program_Args
.all (J
).all;
554 AF
: constant Integer := Arg
'First;
557 if Arg
'Length /= 0 and then Arg
(AF
) = '-' then
558 if Arg
(AF
+ 1) = 'g'
559 and then (Arg
'Length = 2
560 or else Arg
(AF
+ 2) in '0' .. '3'
561 or else Arg
(AF
+ 2 .. Arg
'Last) = "coff")
563 Debug_Flag_Present
:= True;
567 -- Add directory to source search dirs so that
568 -- Get_Target_Parameters can find system.ads
570 if Arg
(AF
.. AF
+ 1) = "-I"
571 and then Arg
'Length > 2
573 Add_Src_Search_Dir
(Arg
(AF
+ 2 .. Arg
'Last));
576 -- Pass to gcc for compiling binder generated file
577 -- No use passing libraries, it will just generate
580 if not (Arg
(AF
.. AF
+ 1) = "-l"
581 or else Arg
(AF
.. AF
+ 1) = "-L")
583 Binder_Options
.Increment_Last
;
584 Binder_Options
.Table
(Binder_Options
.Last
) :=
588 -- Pass to gcc for linking program
590 Gcc_Linker_Options.Increment_Last;
591 Gcc_Linker_Options.Table
592 (Gcc_Linker_Options.Last) := new String'(Arg
);
597 -- Send all multi-character switches not recognized as
598 -- a special case by gnatlink to the linker/loader stage.
601 Linker_Options
.Increment_Last
;
602 Linker_Options
.Table
(Linker_Options
.Last
) :=
606 -- Here if argument is a file name rather than a switch
609 -- If explicit ali file, capture it
612 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
614 if Ali_File_Name = null then
615 Ali_File_Name := new String'(Arg
);
617 Exit_With_Error
("cannot handle more than one ALI file");
620 -- If target object file, record object file
622 elsif Arg
'Length > Get_Target_Object_Suffix
.all'Length
625 Get_Target_Object_Suffix
.all'Length + 1 .. Arg
'Last)
626 = Get_Target_Object_Suffix
.all
628 Linker_Objects
.Increment_Last
;
629 Linker_Objects
.Table
(Linker_Objects
.Last
) :=
632 -- If host object file, record object file
633 -- e.g. accept foo.o as well as foo.obj on VMS target
635 elsif Arg'Length > Get_Object_Suffix.all'Length
637 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
638 = Get_Object_Suffix.all
640 Linker_Objects.Increment_Last;
641 Linker_Objects.Table (Linker_Objects.Last) :=
644 -- If corresponding ali file exists, capture it
646 elsif Ali_File_Name
= null
647 and then Is_Regular_File
(Arg
& ".ali")
649 Ali_File_Name
:= new String'(Arg & ".ali");
651 -- Otherwise assume this is a linker options entry, but
652 -- see below for interesting adjustment to this assumption.
655 Linker_Options.Increment_Last;
656 Linker_Options.Table (Linker_Options.Last) :=
662 Next_Arg
:= Next_Arg
+ 1;
665 -- Compile the bind file with warnings suppressed, because
666 -- otherwise the with of the main program may cause junk warnings.
668 Binder_Options
.Increment_Last
;
669 Binder_Options
.Table
(Binder_Options
.Last
) := new String'("-gnatws");
671 -- If we did not get an ali file at all, and we had at least one
672 -- linker option, then assume that was the intended ali file after
673 -- all, so that we get a nicer message later on.
675 if Ali_File_Name = null
676 and then Linker_Options.Last >= Linker_Options.First
679 new String'(Linker_Options
.Table
(Linker_Options
.First
).all &
684 -------------------------
685 -- Process_Binder_File --
686 -------------------------
688 procedure Process_Binder_File
(Name
: String) is
690 -- Binder file's descriptor
692 Link_Bytes
: Integer := 0;
693 -- Projected number of bytes for the linker command line
696 pragma Import
(C
, Link_Max
, "__gnat_link_max");
697 -- Maximum number of bytes on the command line supported by the OS
698 -- linker. Passed this limit the response file mechanism must be used
701 Next_Line
: String (1 .. 1000);
702 -- Current line value
706 -- Current line slice (the slice does not contain line terminator)
709 -- Current line last character for shared libraries (without version)
711 Objs_Begin
: Integer := 0;
712 -- First object file index in Linker_Objects table
714 Objs_End
: Integer := 0;
715 -- Last object file index in Linker_Objects table
718 pragma Warnings
(Off
, Status
);
719 -- Used for various Interfaces.C_Streams calls
721 Closing_Status
: Boolean;
722 pragma Warnings
(Off
, Closing_Status
);
725 GNAT_Static
: Boolean := False;
726 -- Save state of -static option
728 GNAT_Shared
: Boolean := False;
729 -- Save state of -shared option
731 Xlinker_Was_Previous
: Boolean := False;
732 -- Indicate that "-Xlinker" was the option preceding the current
733 -- option. If True, then the current option is never suppressed.
737 -- These data items are used to store current binder file context.
738 -- The context is composed of the file descriptor position and the
739 -- current line together with the slice indexes (first and last
740 -- position) for this line. The rollback data are used by the
741 -- Store_File_Context and Rollback_File_Context routines below.
742 -- The file context mechanism interact only with the Get_Next_Line
743 -- call. For example:
745 -- Store_File_Context;
747 -- Rollback_File_Context;
750 -- Both Get_Next_Line calls above will read the exact same data from
751 -- the file. In other words, Next_Line, Nfirst and Nlast variables
752 -- will be set with the exact same values.
754 RB_File_Pos
: long
; -- File position
755 RB_Next_Line
: String (1 .. 1000); -- Current line content
756 RB_Nlast
: Integer; -- Slice last index
757 RB_Nfirst
: Integer; -- Slice first index
759 Run_Path_Option_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
760 pragma Import
(C
, Run_Path_Option_Ptr
, "__gnat_run_path_option");
761 -- Pointer to string representing the native linker option which
762 -- specifies the path where the dynamic loader should find shared
763 -- libraries. Equal to null string if this system doesn't support it.
765 Libgcc_Subdir_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
766 pragma Import
(C
, Libgcc_Subdir_Ptr
, "__gnat_default_libgcc_subdir");
767 -- Pointer to string indicating the installation subdirectory where
768 -- a default shared libgcc might be found.
770 Object_Library_Ext_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
772 (C
, Object_Library_Ext_Ptr
, "__gnat_object_library_extension");
773 -- Pointer to string specifying the default extension for
774 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
776 Separate_Run_Path_Options
: Boolean;
777 for Separate_Run_Path_Options
'Size use Character'Size;
779 (C
, Separate_Run_Path_Options
, "__gnat_separate_run_path_options");
780 -- Whether separate rpath options should be emitted for each directory
782 procedure Get_Next_Line
;
783 -- Read the next line from the binder file without the line
786 function Index
(S
, Pattern
: String) return Natural;
787 -- Return the last occurrence of Pattern in S, or 0 if none
789 function Is_Option_Present
(Opt
: String) return Boolean;
790 -- Return true if the option Opt is already present in
791 -- Linker_Options table.
793 procedure Store_File_Context
;
794 -- Store current file context, Fd position and current line data.
795 -- The file context is stored into the rollback data above (RB_*).
796 -- Store_File_Context can be called at any time, only the last call
797 -- will be used (i.e. this routine overwrites the file context).
799 procedure Rollback_File_Context
;
800 -- Restore file context from rollback data. This routine must be called
801 -- after Store_File_Context. The binder file context will be restored
802 -- with the data stored by the last Store_File_Context call.
804 procedure Write_RF
(S
: String);
805 -- Write a string to the response file and check if it was successful.
806 -- Fail the program if it was not successful (disk full).
812 procedure Get_Next_Line
is
816 Fchars
:= fgets
(Next_Line
'Address, Next_Line
'Length, Fd
);
818 if Fchars
= System
.Null_Address
then
819 Exit_With_Error
("Error reading binder output");
822 Nfirst
:= Next_Line
'First;
824 while Nlast
<= Next_Line
'Last
825 and then Next_Line
(Nlast
) /= ASCII
.LF
826 and then Next_Line
(Nlast
) /= ASCII
.CR
838 function Index
(S
, Pattern
: String) return Natural is
839 Len
: constant Natural := Pattern
'Length;
842 for J
in reverse S
'First .. S
'Last - Len
+ 1 loop
843 if Pattern
= S
(J
.. J
+ Len
- 1) then
851 -----------------------
852 -- Is_Option_Present --
853 -----------------------
855 function Is_Option_Present
(Opt
: String) return Boolean is
857 for I
in 1 .. Linker_Options
.Last
loop
859 if Linker_Options
.Table
(I
).all = Opt
then
866 end Is_Option_Present
;
868 ---------------------------
869 -- Rollback_File_Context --
870 ---------------------------
872 procedure Rollback_File_Context
is
874 Next_Line
:= RB_Next_Line
;
877 Status
:= fseek
(Fd
, RB_File_Pos
, Interfaces
.C_Streams
.SEEK_SET
);
880 Exit_With_Error
("Error setting file position");
882 end Rollback_File_Context
;
884 ------------------------
885 -- Store_File_Context --
886 ------------------------
888 procedure Store_File_Context
is
889 use type System
.CRTL
.long
;
891 RB_Next_Line
:= Next_Line
;
894 RB_File_Pos
:= ftell
(Fd
);
896 if RB_File_Pos
= -1 then
897 Exit_With_Error
("Error getting file position");
899 end Store_File_Context
;
905 procedure Write_RF
(S
: String) is
906 Success
: Boolean := True;
909 -- If a GNU response file is used, space and backslash need to be
910 -- escaped because they are interpreted as a string separator and
911 -- an escape character respectively by the underlying mechanism.
912 -- On the other hand, quote and double-quote are not escaped since
913 -- they are interpreted as string delimiters on both sides.
915 if Using_GNU_response_file
then
916 for J
in S
'Range loop
917 if S
(J
) = ' ' or else S
(J
) = '\' then
918 if Write
(Tname_FD
, ASCII
.BACK_SLASH
'Address, 1) /= 1 then
923 if Write
(Tname_FD
, S
(J
)'Address, 1) /= 1 then
929 if Write
(Tname_FD
, S
'Address, S
'Length) /= S
'Length then
934 if Write
(Tname_FD
, ASCII
.LF
'Address, 1) /= 1 then
939 Exit_With_Error
("Error generating response file: disk full");
943 -- Start of processing for Process_Binder_File
946 Fd
:= fopen
(Name
'Address, Read_Mode
'Address);
948 if Fd
= NULL_Stream
then
949 Exit_With_Error
("Failed to open binder output");
952 -- Skip up to the Begin Info line
956 exit when Next_Line
(Nfirst
.. Nlast
) = Begin_Info
;
962 -- Go to end when end line is reached (this will happen in
963 -- High_Integrity_Mode where no -L switches are generated)
965 exit when Next_Line
(Nfirst
.. Nlast
) = End_Info
;
967 Next_Line
(Nfirst
.. Nlast
- 8) := Next_Line
(Nfirst
+ 8 .. Nlast
);
970 -- Go to next section when switches are reached
972 exit when Next_Line
(1) = '-';
974 -- Otherwise we have another object file to collect
976 Linker_Objects
.Increment_Last
;
978 -- Mark the positions of first and last object files in case they
979 -- need to be placed with a named file on systems having linker
982 if Objs_Begin
= 0 then
983 Objs_Begin
:= Linker_Objects
.Last
;
986 Linker_Objects
.Table
(Linker_Objects
.Last
) :=
987 new String'(Next_Line (Nfirst .. Nlast));
989 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
990 -- Nlast - Nfirst + 1, for the size, plus one for the space between
994 Objs_End := Linker_Objects.Last;
996 -- Continue to compute the Link_Bytes, the linker options are part of
997 -- command line length.
1001 while Next_Line (Nfirst .. Nlast) /= End_Info loop
1002 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
1006 Rollback_File_Context;
1008 -- On systems that have limitations on handling very long linker lines
1009 -- we make use of the system linker option which takes a list of object
1010 -- file names from a file instead of the command line itself. What we do
1011 -- is to replace the list of object files by the special linker option
1012 -- which then reads the object file list from a file instead. The option
1013 -- to read from a file instead of the command line is only triggered if
1014 -- a conservative threshold is passed.
1016 if Object_List_File_Required
1017 or else (Object_List_File_Supported
1018 and then Link_Bytes > Link_Max)
1020 -- Create a temporary file containing the Ada user object files
1021 -- needed by the link. This list is taken from the bind file and is
1022 -- output one object per line for maximal compatibility with linkers
1023 -- supporting this option.
1025 Create_Temp_File (Tname_FD, Tname);
1027 -- ??? File descriptor should be checked to not be Invalid_FD.
1028 -- ??? Status of Write and Close operations should be checked, and
1029 -- failure should occur if a status is wrong.
1031 for J in Objs_Begin .. Objs_End loop
1032 Write_RF (Linker_Objects.Table (J).all);
1034 Response_File_Objects.Increment_Last;
1035 Response_File_Objects.Table (Response_File_Objects.Last) :=
1036 Linker_Objects.Table (J);
1039 Close (Tname_FD, Closing_Status);
1041 -- Add the special objects list file option together with the name
1042 -- of the temporary file (removing the null character) to the objects
1045 Linker_Objects.Table (Objs_Begin) :=
1046 new String'(Object_File_Option
&
1047 Tname
(Tname
'First .. Tname
'Last - 1));
1049 -- The slots containing these object file names are then removed
1050 -- from the objects table so they do not appear in the link. They are
1051 -- removed by moving up the linker options and non-Ada object files
1052 -- appearing after the Ada object list in the table.
1058 N
:= Objs_End
- Objs_Begin
+ 1;
1060 for J
in Objs_End
+ 1 .. Linker_Objects
.Last
loop
1061 Linker_Objects
.Table
(J
- N
+ 1) := Linker_Objects
.Table
(J
);
1064 Linker_Objects
.Set_Last
(Linker_Objects
.Last
- N
+ 1);
1068 -- Process switches and options
1070 if Next_Line
(Nfirst
.. Nlast
) /= End_Info
then
1071 Xlinker_Was_Previous
:= False;
1074 if Xlinker_Was_Previous
1075 or else Next_Line
(Nfirst
.. Nlast
) = "-Xlinker"
1077 Linker_Options
.Increment_Last
;
1078 Linker_Options
.Table
(Linker_Options
.Last
) :=
1079 new String'(Next_Line (Nfirst .. Nlast));
1081 elsif Next_Line (Nfirst .. Nlast) = "-static" then
1082 GNAT_Static := True;
1084 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1085 GNAT_Shared := True;
1087 -- Add binder options only if not already set on the command line.
1088 -- This rule is a way to control the linker options order.
1090 -- The following test needs comments, why is it VMS specific.
1091 -- The above comment looks out of date ???
1093 elsif not (OpenVMS_On_Target
1095 Is_Option_Present (Next_Line (Nfirst .. Nlast)))
1097 if Nlast > Nfirst + 2 and then
1098 Next_Line (Nfirst .. Nfirst + 1) = "-L"
1100 -- Construct a library search path for use later to locate
1101 -- static gnatlib libraries.
1103 if Libpath.Last > 1 then
1104 Libpath.Increment_Last;
1105 Libpath.Table (Libpath.Last) := Path_Separator;
1108 for I in Nfirst + 2 .. Nlast loop
1109 Libpath.Increment_Last;
1110 Libpath.Table (Libpath.Last) := Next_Line (I);
1113 Linker_Options.Increment_Last;
1115 Linker_Options.Table (Linker_Options.Last) :=
1116 new String'(Next_Line
(Nfirst
.. Nlast
));
1118 elsif Next_Line
(Nfirst
.. Nlast
) = "-ldecgnat"
1119 or else Next_Line
(Nfirst
.. Nlast
) = "-lgnarl"
1120 or else Next_Line
(Nfirst
.. Nlast
) = "-lgnat"
1122 (1 .. Natural'Min (Nlast
, 8 + Library_Version
'Length)) =
1123 Shared_Lib
("gnarl")
1125 (1 .. Natural'Min (Nlast
, 7 + Library_Version
'Length)) =
1128 -- If it is a shared library, remove the library version.
1129 -- We will be looking for the static version of the library
1130 -- as it is in the same directory as the shared version.
1132 if Next_Line
(Nlast
- Library_Version
'Length + 1 .. Nlast
)
1135 -- Set Last to point to last character before the
1138 Last
:= Nlast
- Library_Version
'Length - 1;
1143 -- Given a Gnat standard library, search the library path to
1144 -- find the library location.
1146 -- Shouldn't we abstract a proc here, we are getting awfully
1147 -- heavily nested ???
1150 File_Path
: String_Access
;
1152 Object_Lib_Extension
: constant String :=
1153 Value
(Object_Library_Ext_Ptr
);
1155 File_Name
: constant String := "lib" &
1156 Next_Line
(Nfirst
+ 2 .. Last
) &
1157 Object_Lib_Extension
;
1159 Run_Path_Opt
: constant String :=
1160 Value
(Run_Path_Option_Ptr
);
1162 GCC_Index
: Natural;
1163 Run_Path_Opt_Index
: Natural := 0;
1167 Locate_Regular_File
(File_Name
,
1168 String (Libpath
.Table
(1 .. Libpath
.Last
)));
1170 if File_Path
/= null then
1173 -- If static gnatlib found, explicitly
1174 -- specify to overcome possible linker
1175 -- default usage of shared version.
1177 Linker_Options
.Increment_Last
;
1179 Linker_Options
.Table
(Linker_Options
.Last
) :=
1180 new String'(File_Path.all);
1182 elsif GNAT_Shared then
1183 if Opt.Run_Path_Option then
1185 -- If shared gnatlib desired, add the
1186 -- appropriate system specific switch
1187 -- so that it can be located at runtime.
1189 if Run_Path_Opt'Length /= 0 then
1191 -- Output the system specific linker command
1192 -- that allows the image activator to find
1193 -- the shared library at runtime. Also add
1194 -- path to find libgcc_s.so, if relevant.
1197 Path : String (1 .. File_Path'Length + 15);
1198 Path_Last : constant Natural :=
1202 Path (1 .. File_Path'Length) :=
1205 -- To find the location of the shared version
1206 -- of libgcc, we look for "gcc-lib" in the
1207 -- path of the library. However, this
1208 -- subdirectory is no longer present in
1209 -- recent versions of GCC. So, we look for
1210 -- the last subdirectory "lib" in the path.
1213 Index (Path (1 .. Path_Last), "gcc-lib");
1215 if GCC_Index /= 0 then
1217 -- The shared version of libgcc is
1218 -- located in the parent directory.
1220 GCC_Index := GCC_Index - 1;
1225 (Path (1 .. Path_Last),
1228 if GCC_Index = 0 then
1230 Index (Path (1 .. Path_Last),
1231 Directory_Separator &
1233 Directory_Separator);
1236 -- If we have found a "lib" subdir in
1237 -- the path to libgnat, the possible
1238 -- shared libgcc of interest by default
1239 -- is in libgcc_subdir at the same
1242 if GCC_Index /= 0 then
1244 Subdir : constant String :=
1245 Value (Libgcc_Subdir_Ptr);
1249 GCC_Index + Subdir'Length) :=
1252 GCC_Index + Subdir'Length;
1257 -- Look for an eventual run_path_option in
1258 -- the linker switches.
1260 if Separate_Run_Path_Options then
1261 Linker_Options.Increment_Last;
1262 Linker_Options.Table
1263 (Linker_Options.Last) :=
1267 (1 .. File_Path
'Length
1268 - File_Name
'Length));
1270 if GCC_Index
/= 0 then
1271 Linker_Options
.Increment_Last
;
1272 Linker_Options
.Table
1273 (Linker_Options
.Last
) :=
1276 & Path (1 .. GCC_Index));
1281 1 .. Linker_Options.Last
1283 if Linker_Options.Table (J) /= null
1285 Linker_Options.Table (J)'Length
1286 > Run_Path_Opt'Length
1288 Linker_Options.Table (J)
1289 (1 .. Run_Path_Opt'Length) =
1292 -- We have found an already
1293 -- specified run_path_option: we
1294 -- will add to this switch,
1296 -- run_path_option should be
1299 Run_Path_Opt_Index := J;
1304 -- If there is no run_path_option, we
1307 if Run_Path_Opt_Index = 0 then
1308 Linker_Options.Increment_Last;
1311 if GCC_Index = 0 then
1312 if Run_Path_Opt_Index = 0 then
1313 Linker_Options.Table
1314 (Linker_Options.Last) :=
1318 (1 .. File_Path
'Length
1319 - File_Name
'Length));
1322 Linker_Options
.Table
1323 (Run_Path_Opt_Index
) :=
1325 (Linker_Options.Table
1326 (Run_Path_Opt_Index).all
1329 (1 .. File_Path'Length
1330 - File_Name'Length));
1334 if Run_Path_Opt_Index = 0 then
1335 Linker_Options.Table
1336 (Linker_Options.Last) :=
1340 (1 .. File_Path
'Length
1343 & Path
(1 .. GCC_Index
));
1346 Linker_Options
.Table
1347 (Run_Path_Opt_Index
) :=
1349 (Linker_Options.Table
1350 (Run_Path_Opt_Index).all
1353 (1 .. File_Path'Length
1356 & Path (1 .. GCC_Index));
1364 -- Then we add the appropriate -l switch
1366 Linker_Options.Increment_Last;
1367 Linker_Options.Table (Linker_Options.Last) :=
1368 new String'(Next_Line
(Nfirst
.. Nlast
));
1372 -- If gnatlib library not found, then
1373 -- add it anyway in case some other
1374 -- mechanism may find it.
1376 Linker_Options
.Increment_Last
;
1377 Linker_Options
.Table
(Linker_Options
.Last
) :=
1378 new String'(Next_Line (Nfirst .. Nlast));
1382 Linker_Options.Increment_Last;
1383 Linker_Options.Table (Linker_Options.Last) :=
1384 new String'(Next_Line
(Nfirst
.. Nlast
));
1388 Xlinker_Was_Previous
:= Next_Line
(Nfirst
.. Nlast
) = "-Xlinker";
1391 exit when Next_Line
(Nfirst
.. Nlast
) = End_Info
;
1393 Next_Line
(Nfirst
.. Nlast
- 8) := Next_Line
(Nfirst
+ 8 .. Nlast
);
1398 -- If -shared was specified, invoke gcc with -shared-libgcc
1401 Linker_Options
.Increment_Last
;
1402 Linker_Options
.Table
(Linker_Options
.Last
) := Shared_Libgcc
;
1405 Status
:= fclose
(Fd
);
1406 end Process_Binder_File
;
1414 Write_Str
("Usage: ");
1415 Write_Str
(Base_Name
(Command_Name
));
1416 Write_Str
(" switches mainprog.ali [non-Ada-objects] [linker-options]");
1419 Write_Line
(" mainprog.ali the ALI file of the main program");
1422 Display_Usage_Version_And_Help
;
1423 Write_Line
(" -f Force object file list to be generated");
1424 Write_Line
(" -g Compile binder source file with debug information");
1425 Write_Line
(" -n Do not compile the binder source file");
1426 Write_Line
(" -P Process files for use by CodePeer");
1427 Write_Line
(" -R Do not use a run_path_option");
1428 Write_Line
(" -v Verbose mode");
1429 Write_Line
(" -v -v Very verbose mode");
1431 Write_Line
(" -o nam Use 'nam' as the name of the executable");
1432 Write_Line
(" -b target Compile the binder source to run on target");
1433 Write_Line
(" -Bdir Load compiler executables from dir");
1435 if Is_Supported
(Map_File
) then
1436 Write_Line
(" -Mmap Create map file map");
1437 Write_Line
(" -M Create map file mainprog.map");
1440 Write_Line
(" --GCC=comp Use comp as the compiler");
1441 Write_Line
(" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1443 Write_Line
(" [non-Ada-objects] list of non Ada object files");
1444 Write_Line
(" [linker-options] other options for the linker");
1451 procedure Write_Header
is
1453 if Verbose_Mode
then
1455 Display_Version
("GNATLINK", "1995");
1463 procedure Write_Usage
is
1469 -- Start of processing for Gnatlink
1472 -- Add the directory where gnatlink is invoked in front of the path, if
1473 -- gnatlink is invoked with directory information. Only do this if the
1474 -- platform is not VMS, where the notion of path does not really exist.
1476 if not Hostparm
.OpenVMS
then
1478 Command
: constant String := Command_Name
;
1481 for Index
in reverse Command
'Range loop
1482 if Command
(Index
) = Directory_Separator
then
1484 Absolute_Dir
: constant String :=
1486 (Command
(Command
'First .. Index
));
1488 PATH
: constant String :=
1491 Getenv
("PATH").all;
1494 Setenv
("PATH", PATH
);
1505 if Argument_Count
= 0
1506 or else (Verbose_Mode
and then Argument_Count
= 1)
1509 Exit_Program
(E_Fatal
);
1512 -- Initialize packages to be used
1517 -- We always compile with -c
1519 Binder_Options_From_ALI
.Increment_Last
;
1520 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1523 if Ali_File_Name = null then
1524 Exit_With_Error ("no ali file given for link");
1527 if not Is_Regular_File (Ali_File_Name.all) then
1528 Exit_With_Error (Ali_File_Name.all & " not found");
1531 -- Read the ALI file of the main subprogram if the binder generated file
1532 -- needs to be compiled and no --GCC= switch has been specified. Fetch the
1533 -- back end switches from this ALI file and use these switches to compile
1534 -- the binder generated file
1536 if Compile_Bind_File and then Standard_Gcc then
1538 Name_Len := Ali_File_Name'Length;
1539 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1543 F : constant File_Name_Type := Name_Find;
1544 T : Text_Buffer_Ptr;
1548 -- Load the ALI file
1550 T := Read_Library_Info (F, True);
1552 -- Read it. Note that we ignore errors, since we only want very
1553 -- limited information from the ali file, and likely a slightly
1554 -- wrong version will be just fine, though in normal operation
1555 -- we don't expect this to happen!
1562 Ignore_Errors => True);
1564 if A /= No_ALI_Id then
1566 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1567 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1569 -- Do not compile with the front end switches. However, --RTS
1570 -- is to be dealt with specially because it needs to be passed
1571 -- if the binder-generated file is in Ada and may also be used
1572 -- to drive the linker.
1575 Arg : String_Ptr renames Args.Table (Index);
1577 if not Is_Front_End_Switch (Arg.all) then
1578 Binder_Options_From_ALI.Increment_Last;
1579 Binder_Options_From_ALI.Table
1580 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1582 elsif Arg'Length > 5
1583 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1585 Binder_Options_From_ALI.Increment_Last;
1586 Binder_Options_From_ALI.Table
1587 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1589 -- Set the RTS_*_Path_Name variables, so that
1590 -- the correct directories will be set when
1591 -- Osint.Add_Default_Search_Dirs will be called later.
1593 Opt.RTS_Src_Path_Name :=
1595 (Arg (Arg'First + 6 .. Arg'Last), Include);
1597 Opt.RTS_Lib_Path_Name :=
1599 (Arg (Arg'First + 6 .. Arg'Last), Objects);
1601 -- GNAT doesn't support the GCC multilib mechanism.
1602 -- This means that, when a multilib switch is used
1603 -- to request a particular compilation mode, the
1604 -- corresponding runtime switch (--RTS) must also be
1605 -- specified. The long-term goal is to fully support the
1606 -- multilib mechanism; however, in the meantime, it is
1607 -- convenient to eliminate the redundancy by keying the
1608 -- compilation mode on a single switch, namely --RTS.
1610 -- Pass -mrtp to the linker if --RTS=rtp was passed
1614 (Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1615 or else Arg (Arg'Last - 2 .. Arg'Last) = "rtp")
1617 Linker_Options.Increment_Last;
1618 Linker_Options.Table (Linker_Options.Last) :=
1619 new String'("-mrtp");
1628 -- Get target parameters
1630 Osint
.Add_Default_Search_Dirs
;
1631 Targparm
.Get_Target_Parameters
;
1633 if VM_Target
/= No_VM
then
1635 when JVM_Target
=> Gcc
:= new String'("jvm-gnatcompile");
1636 when CLI_Target => Gcc := new String'("dotnet-gnatcompile");
1637 when No_VM
=> raise Program_Error
;
1641 -- Compile the bind file with the following switches:
1643 -- -gnatA stops reading gnat.adc, since we don't know what
1644 -- pragmas would work, and we do not need it anyway.
1646 -- -gnatWb allows brackets coding for wide characters
1648 -- -gnatiw allows wide characters in identifiers. This is needed
1649 -- because bindgen uses brackets encoding for all upper
1650 -- half and wide characters in identifier names.
1652 -- In addition, in CodePeer mode compile with -gnatcC
1654 Binder_Options_From_ALI
.Increment_Last
;
1655 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1656 new String'("-gnatA");
1657 Binder_Options_From_ALI.Increment_Last;
1658 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1659 new String'("-gnatWb");
1660 Binder_Options_From_ALI
.Increment_Last
;
1661 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1662 new String'("-gnatiw");
1664 if Opt.CodePeer_Mode then
1665 Binder_Options_From_ALI.Increment_Last;
1666 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1667 new String'("-gnatcC");
1670 -- Locate all the necessary programs and verify required files are present
1672 Gcc_Path
:= System
.OS_Lib
.Locate_Exec_On_Path
(Gcc
.all);
1674 if Gcc_Path
= null then
1675 Exit_With_Error
("Couldn't locate " & Gcc
.all);
1678 if Linker_Path
= null then
1679 if VM_Target
= CLI_Target
then
1680 Linker_Path
:= System
.OS_Lib
.Locate_Exec_On_Path
("dotnet-ld");
1682 if Linker_Path
= null then
1683 Exit_With_Error
("Couldn't locate dotnet-ld");
1686 elsif RTX_RTSS_Kernel_Module_On_Target
then
1688 -- Use Microsoft linker for RTSS modules
1690 Linker_Path
:= System
.OS_Lib
.Locate_Exec_On_Path
("link");
1692 if Linker_Path
= null then
1693 Exit_With_Error
("Couldn't locate link");
1697 Linker_Path
:= Gcc_Path
;
1703 -- If no output name specified, then use the base name of .ali file name
1705 if Output_File_Name
= null then
1707 new String'(Base_Name (Ali_File_Name.all)
1708 & Get_Target_Debuggable_Suffix.all);
1711 if RTX_RTSS_Kernel_Module_On_Target then
1712 Linker_Options.Increment_Last;
1713 Linker_Options.Table (Linker_Options.Last) :=
1714 new String'("/OUT:" & Output_File_Name
.all);
1717 Linker_Options
.Increment_Last
;
1718 Linker_Options
.Table
(Linker_Options
.Last
) := new String'("-o");
1720 Linker_Options.Increment_Last;
1721 Linker_Options.Table (Linker_Options.Last) :=
1722 new String'(Output_File_Name
.all);
1725 Check_Existing_Executable
(Output_File_Name
.all);
1727 -- Warn if main program is called "test", as that may be a built-in command
1728 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1729 -- will not appear. However, do not warn in the case of a cross compiler.
1731 -- Assume this is a cross tool if the executable name is not gnatlink
1733 if Base_Name
(Command_Name
) = "gnatlink"
1734 and then Output_File_Name
.all = "test"
1736 Error_Msg
("warning: executable name """ & Output_File_Name
.all
1737 & """ may conflict with shell command");
1740 -- If -M switch was specified, add the switches to create the map file
1742 if Create_Map_File
then
1744 Map_Name
: constant String := Base_Name
(Ali_File_Name
.all) & ".map";
1745 Switches
: String_List_Access
;
1748 Convert
(Map_File
, Map_Name
, Switches
);
1750 if Switches
/= null then
1751 for J
in Switches
'Range loop
1752 Linker_Options
.Increment_Last
;
1753 Linker_Options
.Table
(Linker_Options
.Last
) := Switches
(J
);
1759 -- Perform consistency checks
1761 -- Transform the .ali file name into the binder output file name
1763 Make_Binder_File_Names
: declare
1764 Fname
: constant String := Base_Name
(Ali_File_Name
.all);
1765 Fname_Len
: Integer := Fname
'Length;
1767 function Get_Maximum_File_Name_Length
return Integer;
1768 pragma Import
(C
, Get_Maximum_File_Name_Length
,
1769 "__gnat_get_maximum_file_name_length");
1771 Maximum_File_Name_Length
: constant Integer :=
1772 Get_Maximum_File_Name_Length
;
1774 Bind_File_Prefix
: Types
.String_Ptr
;
1775 -- Contains prefix used for bind files
1780 if OpenVMS_On_Target
then
1781 Bind_File_Prefix
:= new String'("b__");
1783 Bind_File_Prefix := new String'("b~");
1786 -- If the length of the binder file becomes too long due to
1787 -- the addition of the "b?" prefix, then truncate it.
1789 if Maximum_File_Name_Length
> 0 then
1791 Maximum_File_Name_Length
- Bind_File_Prefix
.all'Length
1793 Fname_Len
:= Fname_Len
- 1;
1798 Fnam
: constant String :=
1799 Bind_File_Prefix
.all &
1800 Fname
(Fname
'First .. Fname
'First + Fname_Len
- 1);
1803 Binder_Spec_Src_File
:= new String'(Fnam & ".ads");
1804 Binder_Body_Src_File := new String'(Fnam
& ".adb");
1805 Binder_Ali_File
:= new String'(Fnam & ".ali");
1807 Binder_Obj_File := new String'(Fnam
& Get_Target_Object_Suffix
.all);
1810 if Fname_Len
/= Fname
'Length then
1811 Binder_Options
.Increment_Last
;
1812 Binder_Options
.Table
(Binder_Options
.Last
) := new String'("-o");
1813 Binder_Options.Increment_Last;
1814 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1816 end Make_Binder_File_Names;
1818 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1820 -- Compile the binder file. This is fast, so we always do it, unless
1821 -- specifically told not to by the -n switch
1823 if Compile_Bind_File then
1826 Args : Argument_List
1827 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1830 for J in 1 .. Binder_Options_From_ALI.Last loop
1831 Args (J) := Binder_Options_From_ALI.Table (J);
1834 for J in 1 .. Binder_Options.Last loop
1835 Args (Binder_Options_From_ALI.Last + J) :=
1836 Binder_Options.Table (J);
1839 -- Use the full path of the binder generated source, so that it is
1840 -- guaranteed that the debugger will find this source, even with
1844 new String'(Normalize_Pathname
(Binder_Body_Src_File
.all));
1846 if Verbose_Mode
then
1847 Write_Str
(Base_Name
(Gcc_Path
.all));
1849 for J
in Args
'Range loop
1851 Write_Str
(Args
(J
).all);
1857 System
.OS_Lib
.Spawn
(Gcc_Path
.all, Args
, Success
);
1860 Exit_Program
(E_Fatal
);
1865 -- In CodePeer mode, there's nothing left to do after the binder file has
1868 if Opt
.CodePeer_Mode
then
1872 -- Now, actually link the program
1874 -- Skip this step for now on JVM since the Java interpreter will do
1875 -- the actual link at run time. We might consider packing all class files
1876 -- in a .zip file during this step.
1878 if VM_Target
/= JVM_Target
then
1880 Num_Args
: Natural :=
1881 (Linker_Options
.Last
- Linker_Options
.First
+ 1) +
1882 (Gcc_Linker_Options
.Last
- Gcc_Linker_Options
.First
+ 1) +
1883 (Linker_Objects
.Last
- Linker_Objects
.First
+ 1);
1884 Stack_Op
: Boolean := False;
1885 IDENT_Op
: Boolean := False;
1888 if AAMP_On_Target
then
1890 -- Remove extraneous flags not relevant for AAMP
1892 for J
in reverse Linker_Options
.First
.. Linker_Options
.Last
loop
1893 if Linker_Options
.Table
(J
)'Length = 0
1894 or else Linker_Options
.Table
(J
) (1 .. 3) = "-Wl"
1895 or else Linker_Options
.Table
(J
) (1 .. 3) = "-sh"
1896 or else Linker_Options
.Table
(J
) (1 .. 2) = "-O"
1897 or else Linker_Options
.Table
(J
) (1 .. 2) = "-g"
1899 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
1900 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
1901 Linker_Options
.Decrement_Last
;
1902 Num_Args
:= Num_Args
- 1;
1906 elsif RTX_RTSS_Kernel_Module_On_Target
then
1908 -- Remove flags not relevant for Microsoft linker and adapt some
1911 for J
in reverse Linker_Options
.First
.. Linker_Options
.Last
loop
1913 -- Remove flags that are not accepted
1915 if Linker_Options
.Table
(J
)'Length = 0
1916 or else Linker_Options
.Table
(J
) (1 .. 2) = "-l"
1917 or else Linker_Options
.Table
(J
) (1 .. 3) = "-Wl"
1918 or else Linker_Options
.Table
(J
) (1 .. 3) = "-sh"
1919 or else Linker_Options
.Table
(J
) (1 .. 2) = "-O"
1920 or else Linker_Options
.Table
(J
) (1 .. 8) = "-Xlinker"
1921 or else Linker_Options
.Table
(J
) (1 .. 9) = "-mthreads"
1923 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
1924 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
1925 Linker_Options
.Decrement_Last
;
1926 Num_Args
:= Num_Args
- 1;
1928 -- Replace "-L" by its counterpart "/LIBPATH:" and UNIX "/" by
1930 elsif Linker_Options
.Table
(J
) (1 .. 2) = "-L" then
1932 Libpath_Option
: constant String_Access
:= new String'
1934 Linker_Options.Table (J)
1935 (3 .. Linker_Options.Table (J).all'Last));
1937 for Index in 10 .. Libpath_Option'Last loop
1938 if Libpath_Option (Index) = '/' then
1939 Libpath_Option (Index) := '\
';
1943 Linker_Options.Table (J) := Libpath_Option;
1946 -- Replace "-g" by "/DEBUG"
1947 elsif Linker_Options.Table (J) (1 .. 2) = "-g" then
1948 Linker_Options.Table (J) := new String'("/DEBUG");
1950 -- Replace "-o" by "/OUT:"
1951 elsif Linker_Options
.Table
(J
) (1 .. 2) = "-o" then
1952 Linker_Options
.Table
(J
+ 1) := new String'
1953 ("/OUT:" & Linker_Options.Table (J + 1).all);
1955 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1956 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1957 Linker_Options.Decrement_Last;
1958 Num_Args := Num_Args - 1;
1960 -- Replace "--stack=" by "/STACK:"
1961 elsif Linker_Options.Table (J) (1 .. 8) = "--stack=" then
1962 Linker_Options.Table (J) := new String'
1964 Linker_Options
.Table
(J
)
1965 (9 .. Linker_Options
.Table
(J
).all'Last));
1967 -- Replace "-v" by its counterpart "/VERBOSE"
1968 elsif Linker_Options
.Table
(J
) (1 .. 2) = "-v" then
1969 Linker_Options
.Table
(J
) := new String'("/VERBOSE");
1973 -- Add some required flags to create RTSS modules
1976 Flags_For_Linker : constant array (1 .. 17) of String_Access :=
1977 (new String'("/NODEFAULTLIB"),
1978 new String'("/INCREMENTAL:NO"),
1979 new String'("/NOLOGO"),
1980 new String'("/DRIVER"),
1981 new String'("/ALIGN:0x20"),
1982 new String'("/SUBSYSTEM:NATIVE"),
1983 new String'("/ENTRY:_RtapiProcessEntryCRT@8"),
1984 new String'("/RELEASE"),
1985 new String'("startupCRT.obj"),
1986 new String'("rtxlibcmt.lib"),
1987 new String'("oldnames.lib"),
1988 new String'("rtapi_rtss.lib"),
1989 new String'("Rtx_Rtss.lib"),
1990 new String'("libkernel32.a"),
1991 new String'("libws2_32.a"),
1992 new String'("libmswsock.a"),
1993 new String'("libadvapi32.a"));
1994 -- These flags need to be passed to Microsoft linker. They
1995 -- come from the RTX documentation.
1997 Gcc_Lib_Path
: constant String_Access
:= new String'
1998 ("/LIBPATH:" & Include_Dir_Default_Prefix & "\..\");
1999 -- Place to look for gcc related libraries, such as libgcc
2002 -- Replace UNIX "/" by Windows "\" in the path
2004 for Index in 10 .. Gcc_Lib_Path.all'Last loop
2005 if Gcc_Lib_Path (Index) = '/' then
2006 Gcc_Lib_Path (Index) := '\
';
2010 Linker_Options.Increment_Last;
2011 Linker_Options.Table (Linker_Options.Last) := Gcc_Lib_Path;
2012 Num_Args := Num_Args + 1;
2014 for Index in Flags_For_Linker'Range loop
2015 Linker_Options.Increment_Last;
2016 Linker_Options.Table (Linker_Options.Last) :=
2017 Flags_For_Linker (Index);
2018 Num_Args := Num_Args + 1;
2023 -- Remove duplicate stack size setting from the Linker_Options
2024 -- table. The stack setting option "-Xlinker --stack=R,C" can be
2025 -- found in one line when set by a pragma Linker_Options or in two
2026 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
2027 -- line. We also check for the "-Wl,--stack=R" style option.
2029 -- We must remove the second stack setting option instance
2030 -- because the one on the command line will always be the first
2031 -- one. And any subsequent stack setting option will overwrite the
2032 -- previous one. This is done especially for GNAT/NT where we set
2033 -- the stack size for tasking programs by a pragma in the NT
2034 -- specific tasking package System.Task_Primitives.Operations.
2036 -- Note: This is not a FOR loop that runs from Linker_Options.First
2037 -- to Linker_Options.Last, since operations within the loop can
2038 -- modify the length of the table.
2040 Clean_Link_Option_Set : declare
2041 J : Natural := Linker_Options.First;
2042 Shared_Libgcc_Seen : Boolean := False;
2045 while J <= Linker_Options.Last loop
2047 if Linker_Options.Table (J).all = "-Xlinker"
2048 and then J < Linker_Options.Last
2049 and then Linker_Options.Table (J + 1)'Length > 8
2050 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
2053 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
2054 Linker_Options.Table (J + 2 .. Linker_Options.Last);
2055 Linker_Options.Decrement_Last;
2056 Linker_Options.Decrement_Last;
2057 Num_Args := Num_Args - 2;
2064 -- Remove duplicate -shared-libgcc switch
2066 if Linker_Options.Table (J).all = Shared_Libgcc_String then
2067 if Shared_Libgcc_Seen then
2068 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2069 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2070 Linker_Options.Decrement_Last;
2071 Num_Args := Num_Args - 1;
2074 Shared_Libgcc_Seen := True;
2078 -- Here we just check for a canonical form that matches the
2079 -- pragma Linker_Options set in the NT runtime.
2081 if (Linker_Options.Table (J)'Length > 17
2082 and then Linker_Options.Table (J) (1 .. 17)
2083 = "-Xlinker --stack=")
2085 (Linker_Options.Table (J)'Length > 12
2086 and then Linker_Options.Table (J) (1 .. 12)
2090 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2091 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2092 Linker_Options.Decrement_Last;
2093 Num_Args := Num_Args - 1;
2100 -- Remove duplicate IDENTIFICATION directives (VMS)
2102 if Linker_Options.Table (J)'Length > 29
2103 and then Linker_Options.Table (J) (1 .. 30) =
2104 "--for-linker=--identification="
2107 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2108 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2109 Linker_Options.Decrement_Last;
2110 Num_Args := Num_Args - 1;
2120 if Linker_Path = Gcc_Path and then VM_Target = No_VM then
2122 -- For systems where the default is to link statically with
2123 -- libgcc, if gcc is not called with -shared-libgcc, call it
2124 -- with -static-libgcc, as there are some platforms where one
2125 -- of these two switches is compulsory to link.
2127 if Shared_Libgcc_Default = 'T
'
2128 and then not Shared_Libgcc_Seen
2130 Linker_Options.Increment_Last;
2131 Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
2132 Num_Args := Num_Args + 1;
2135 elsif RTX_RTSS_Kernel_Module_On_Target then
2137 -- Force the use of the static libgcc for RTSS modules
2139 Linker_Options.Increment_Last;
2140 Linker_Options.Table (Linker_Options.Last) :=
2141 new String'("libgcc.a");
2142 Num_Args
:= Num_Args
+ 1;
2145 end Clean_Link_Option_Set
;
2147 -- Prepare arguments for call to linker
2149 Call_Linker
: declare
2151 Args
: Argument_List
(1 .. Num_Args
+ 1);
2152 Index
: Integer := Args
'First;
2155 Args
(Index
) := Binder_Obj_File
;
2157 -- Add the object files and any -largs libraries
2159 for J
in Linker_Objects
.First
.. Linker_Objects
.Last
loop
2161 Args
(Index
) := Linker_Objects
.Table
(J
);
2164 -- Add the linker options from the binder file
2166 for J
in Linker_Options
.First
.. Linker_Options
.Last
loop
2168 Args
(Index
) := Linker_Options
.Table
(J
);
2171 -- Finally add the libraries from the --GCC= switch
2173 for J
in Gcc_Linker_Options
.First
.. Gcc_Linker_Options
.Last
loop
2175 Args
(Index
) := Gcc_Linker_Options
.Table
(J
);
2178 if Verbose_Mode
then
2179 Write_Str
(Linker_Path
.all);
2181 for J
in Args
'Range loop
2183 Write_Str
(Args
(J
).all);
2188 -- If we are on very verbose mode (-v -v) and a response file
2189 -- is used we display its content.
2191 if Very_Verbose_Mode
and then Tname_FD
/= Invalid_FD
then
2193 Write_Str
("Response file (" &
2194 Tname
(Tname
'First .. Tname
'Last - 1) &
2199 Response_File_Objects
.First
..
2200 Response_File_Objects
.Last
2202 Write_Str
(Response_File_Objects
.Table
(J
).all);
2210 System
.OS_Lib
.Spawn
(Linker_Path
.all, Args
, Success
);
2214 -- Delete the temporary file used in conjunction with linking
2215 -- if one was created. See Process_Bind_File for details.
2217 if Tname_FD
/= Invalid_FD
then
2222 Error_Msg
("error when calling " & Linker_Path
.all);
2223 Exit_Program
(E_Fatal
);
2229 -- Only keep the binder output file and it's associated object
2230 -- file if compiling with the -g option. These files are only
2231 -- useful if debugging.
2233 if not Debug_Flag_Present
then
2234 Delete
(Binder_Ali_File
.all & ASCII
.NUL
);
2235 Delete
(Binder_Spec_Src_File
.all & ASCII
.NUL
);
2236 Delete
(Binder_Body_Src_File
.all & ASCII
.NUL
);
2238 if VM_Target
= No_VM
then
2239 Delete
(Binder_Obj_File
.all & ASCII
.NUL
);
2243 Exit_Program
(E_Success
);
2247 Write_Line
(Exception_Information
(X
));
2248 Exit_With_Error
("INTERNAL ERROR. Please report");