1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- Gnatlink usage: please consult the gnat documentation
30 with Gnatvsn
; use Gnatvsn
;
32 with Indepsw
; use Indepsw
;
33 with Namet
; use Namet
;
35 with Osint
; use Osint
;
36 with Output
; use Output
;
38 with Switch
; use Switch
;
39 with System
; use System
;
41 with Targparm
; use Targparm
;
44 with Ada
.Command_Line
; use Ada
.Command_Line
;
45 with Ada
.Exceptions
; use Ada
.Exceptions
;
47 with System
.OS_Lib
; use System
.OS_Lib
;
50 with Interfaces
.C_Streams
; use Interfaces
.C_Streams
;
51 with Interfaces
.C
.Strings
; use Interfaces
.C
.Strings
;
54 pragma Ident
(Gnatvsn
.Gnat_Static_Version_String
);
56 Shared_Libgcc_String
: constant String := "-shared-libgcc";
57 Shared_Libgcc
: constant String_Access
:=
58 new String'(Shared_Libgcc_String);
59 -- Used to invoke gcc when the binder is invoked with -shared
61 Static_Libgcc_String : constant String := "-static-libgcc";
62 Static_Libgcc : constant String_Access :=
63 new String'(Static_Libgcc_String
);
64 -- Used to invoke gcc when shared libs are not used
66 package Gcc_Linker_Options
is new Table
.Table
(
67 Table_Component_Type
=> String_Access
,
68 Table_Index_Type
=> Integer,
71 Table_Increment
=> 100,
72 Table_Name
=> "Gnatlink.Gcc_Linker_Options");
73 -- Comments needed ???
75 package Libpath
is new Table
.Table
(
76 Table_Component_Type
=> Character,
77 Table_Index_Type
=> Integer,
79 Table_Initial
=> 4096,
80 Table_Increment
=> 100,
81 Table_Name
=> "Gnatlink.Libpath");
82 -- Comments needed ???
84 package Linker_Options
is new Table
.Table
(
85 Table_Component_Type
=> String_Access
,
86 Table_Index_Type
=> Integer,
89 Table_Increment
=> 100,
90 Table_Name
=> "Gnatlink.Linker_Options");
91 -- Comments needed ???
93 package Linker_Objects
is new Table
.Table
(
94 Table_Component_Type
=> String_Access
,
95 Table_Index_Type
=> Integer,
98 Table_Increment
=> 100,
99 Table_Name
=> "Gnatlink.Linker_Objects");
100 -- This table collects the objects file to be passed to the linker. In the
101 -- case where the linker command line is too long then programs objects
102 -- are put on the Response_File_Objects table. Note that the binder object
103 -- file and the user's objects remain in this table. This is very
104 -- important because on the GNU linker command line the -L switch is not
105 -- used to look for objects files but -L switch is used to look for
106 -- objects listed in the response file. This is not a problem with the
107 -- applications objects as they are specified with a full name.
109 package Response_File_Objects
is new Table
.Table
(
110 Table_Component_Type
=> String_Access
,
111 Table_Index_Type
=> Integer,
112 Table_Low_Bound
=> 1,
114 Table_Increment
=> 100,
115 Table_Name
=> "Gnatlink.Response_File_Objects");
116 -- This table collects the objects file that are to be put in the response
117 -- file. Only application objects are collected there (see details in
118 -- Linker_Objects table comments)
120 package Binder_Options_From_ALI
is new Table
.Table
(
121 Table_Component_Type
=> String_Access
,
122 Table_Index_Type
=> Integer,
123 Table_Low_Bound
=> 1, -- equals low bound of Argument_List for Spawn
125 Table_Increment
=> 100,
126 Table_Name
=> "Gnatlink.Binder_Options_From_ALI");
127 -- This table collects the switches from the ALI file of the main
130 package Binder_Options
is new Table
.Table
(
131 Table_Component_Type
=> String_Access
,
132 Table_Index_Type
=> Integer,
133 Table_Low_Bound
=> 1, -- equals low bound of Argument_List for Spawn
135 Table_Increment
=> 100,
136 Table_Name
=> "Gnatlink.Binder_Options");
137 -- This table collects the arguments to be passed to compile the binder
140 Gcc
: String_Access
:= Program_Name
("gcc", "gnatlink");
142 Read_Mode
: constant String := "r" & ASCII
.NUL
;
144 Begin_Info
: String := "-- BEGIN Object file/option list";
145 End_Info
: String := "-- END Object file/option list ";
146 -- Note: above lines are modified in C mode, see option processing
148 Gcc_Path
: String_Access
;
149 Linker_Path
: String_Access
;
150 Output_File_Name
: String_Access
;
151 Ali_File_Name
: String_Access
;
152 Binder_Spec_Src_File
: String_Access
;
153 Binder_Body_Src_File
: String_Access
;
154 Binder_Ali_File
: String_Access
;
155 Binder_Obj_File
: String_Access
;
157 Tname
: Temp_File_Name
;
158 Tname_FD
: File_Descriptor
:= Invalid_FD
;
159 -- Temporary file used by linker to pass list of object files on
160 -- certain systems with limitations on size of arguments.
162 Lname
: String_Access
:= null;
163 -- File used by linker for CLI target, used to concatenate all .il files
164 -- when the command line passed to ilasm is too long
166 Debug_Flag_Present
: Boolean := False;
167 Verbose_Mode
: Boolean := False;
168 Very_Verbose_Mode
: Boolean := False;
170 Ada_Bind_File
: Boolean := True;
171 -- Set to True if bind file is generated in Ada
173 Standard_Gcc
: Boolean := True;
175 Compile_Bind_File
: Boolean := True;
176 -- Set to False if bind file is not to be compiled
178 Create_Map_File
: Boolean := False;
179 -- Set to True by switch -M. The map file name is derived from
180 -- the ALI file name (mainprog.ali => mainprog.map).
182 Object_List_File_Supported
: Boolean;
183 for Object_List_File_Supported
'Size use Character'Size;
185 (C
, Object_List_File_Supported
, "__gnat_objlist_file_supported");
186 -- Predicate indicating whether the linker has an option whereby the
187 -- names of object files can be passed to the linker in a file.
189 Object_List_File_Required
: Boolean := False;
190 -- Set to True to force generation of a response file
192 Shared_Libgcc_Default
: Character;
193 for Shared_Libgcc_Default
'Size use Character'Size;
195 (C
, Shared_Libgcc_Default
, "__gnat_shared_libgcc_default");
196 -- Indicates wether libgcc should be statically linked (use 'T') or
197 -- dynamically linked (use 'H') by default.
199 function Base_Name
(File_Name
: String) return String;
200 -- Return just the file name part without the extension (if present)
202 procedure Delete
(Name
: String);
203 -- Wrapper to unlink as status is ignored by this application
205 procedure Error_Msg
(Message
: String);
206 -- Output the error or warning Message
208 procedure Exit_With_Error
(Error
: String);
209 -- Output Error and exit program with a fatal condition
211 procedure Process_Args
;
212 -- Go through all the arguments and build option tables
214 procedure Process_Binder_File
(Name
: String);
215 -- Reads the binder file and extracts linker arguments
220 procedure Write_Header
;
221 -- Show user the program name, version and copyright
223 procedure Write_Usage
;
224 -- Show user the program options
230 function Base_Name
(File_Name
: String) return String is
235 Findex1
:= File_Name
'First;
237 -- The file might be specified by a full path name. However,
238 -- we want the path to be stripped away.
240 for J
in reverse File_Name
'Range loop
241 if Is_Directory_Separator
(File_Name
(J
)) then
247 Findex2
:= File_Name
'Last;
248 while Findex2
> Findex1
249 and then File_Name
(Findex2
) /= '.'
251 Findex2
:= Findex2
- 1;
254 if Findex2
= Findex1
then
255 Findex2
:= File_Name
'Last + 1;
258 return File_Name
(Findex1
.. Findex2
- 1);
265 procedure Delete
(Name
: String) is
267 pragma Unreferenced
(Status
);
269 Status
:= unlink
(Name
'Address);
270 -- Is it really right to ignore an error here ???
277 procedure Error_Msg
(Message
: String) is
279 Write_Str
(Base_Name
(Command_Name
));
285 ---------------------
286 -- Exit_With_Error --
287 ---------------------
289 procedure Exit_With_Error
(Error
: String) is
292 Exit_Program
(E_Fatal
);
299 procedure Process_Args
is
301 Skip_Next
: Boolean := False;
302 -- Set to true if the next argument is to be added into the list of
303 -- linker's argument without parsing it.
305 procedure Check_Version_And_Help
is new Check_Version_And_Help_G
(Usage
);
307 -- Start of processing for Process_Args
310 -- First, check for --version and --help
312 Check_Version_And_Help
("GNATLINK", "1995");
314 -- Loop through arguments of gnatlink command
318 exit when Next_Arg
> Argument_Count
;
320 Process_One_Arg
: declare
321 Arg
: constant String := Argument
(Next_Arg
);
324 -- Case of argument which is a switch
326 -- We definitely need section by section comments here ???
330 -- This argument must not be parsed, just add it to the
331 -- list of linker's options.
335 Linker_Options
.Increment_Last
;
336 Linker_Options
.Table
(Linker_Options
.Last
) :=
339 elsif Arg'Length /= 0 and then Arg (1) = '-' then
340 if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then
342 ("invalid switch: """ & Arg & """ (gnat not needed here)");
345 if Arg = "-Xlinker" then
347 -- Next argument should be sent directly to the linker.
348 -- We do not want to parse it here.
352 Linker_Options.Increment_Last;
353 Linker_Options.Table (Linker_Options.Last) :=
357 and then (Arg
'Length < 5 or else Arg
(2 .. 5) /= "gnat")
359 Debug_Flag_Present
:= True;
361 Linker_Options
.Increment_Last
;
362 Linker_Options
.Table
(Linker_Options
.Last
) :=
365 Binder_Options.Increment_Last;
366 Binder_Options.Table (Binder_Options.Last) :=
367 Linker_Options.Table (Linker_Options.Last);
369 elsif Arg'Length >= 3 and then Arg (2) = 'M
' then
371 Switches : String_List_Access;
374 Convert (Map_File, Arg (3 .. Arg'Last), Switches);
376 if Switches /= null then
377 for J in Switches'Range loop
378 Linker_Options.Increment_Last;
379 Linker_Options.Table (Linker_Options.Last) :=
385 elsif Arg'Length = 2 then
388 Ada_Bind_File := True;
389 Begin_Info := "-- BEGIN Object file/option list";
390 End_Info := "-- END Object file/option list ";
393 Linker_Options.Increment_Last;
394 Linker_Options.Table (Linker_Options.Last) :=
397 Binder_Options
.Increment_Last
;
398 Binder_Options
.Table
(Binder_Options
.Last
) :=
399 Linker_Options
.Table
(Linker_Options
.Last
);
401 Next_Arg
:= Next_Arg
+ 1;
403 if Next_Arg
> Argument_Count
then
404 Exit_With_Error
("Missing argument for -b");
407 Get_Machine_Name
: declare
408 Name_Arg
: constant String_Access
:=
409 new String'(Argument (Next_Arg));
412 Linker_Options.Increment_Last;
413 Linker_Options.Table (Linker_Options.Last) :=
416 Binder_Options.Increment_Last;
417 Binder_Options.Table (Binder_Options.Last) :=
420 end Get_Machine_Name;
423 Ada_Bind_File := False;
424 Begin_Info := "/* BEGIN Object file/option list";
425 End_Info := " END Object file/option list */";
428 if Object_List_File_Supported then
429 Object_List_File_Required := True;
432 ("Object list file not supported on this target");
436 Create_Map_File := True;
439 Compile_Bind_File := False;
442 Next_Arg := Next_Arg + 1;
444 if Next_Arg > Argument_Count then
445 Exit_With_Error ("Missing argument for -o");
449 new String'(Executable_Name
450 (Argument
(Next_Arg
),
451 Only_If_No_Suffix
=> True));
454 Opt
.Run_Path_Option
:= False;
458 -- Support "double" verbose mode. Second -v
459 -- gets sent to the linker and binder phases.
462 Very_Verbose_Mode
:= True;
464 Linker_Options
.Increment_Last
;
465 Linker_Options
.Table
(Linker_Options
.Last
) :=
468 Binder_Options.Increment_Last;
469 Binder_Options.Table (Binder_Options.Last) :=
470 Linker_Options.Table (Linker_Options.Last);
473 Verbose_Mode := True;
478 Linker_Options.Increment_Last;
479 Linker_Options.Table (Linker_Options.Last) :=
484 elsif Arg
(2) = 'B' then
485 Linker_Options
.Increment_Last
;
486 Linker_Options
.Table
(Linker_Options
.Last
) :=
489 Binder_Options.Increment_Last;
490 Binder_Options.Table (Binder_Options.Last) :=
491 Linker_Options.Table (Linker_Options.Last);
493 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
494 if Arg'Length = 7 then
495 Exit_With_Error ("Missing argument for --LINK=");
499 System.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
501 if Linker_Path = null then
503 ("Could not locate linker: " & Arg (8 .. Arg'Last));
506 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
508 Program_Args : constant Argument_List_Access :=
509 Argument_String_To_List
510 (Arg (7 .. Arg'Last));
513 if Program_Args.all (1).all /= Gcc.all then
514 Gcc := new String'(Program_Args
.all (1).all);
515 Standard_Gcc
:= False;
518 -- Set appropriate flags for switches passed
520 for J
in 2 .. Program_Args
.all'Last loop
522 Arg
: constant String := Program_Args
.all (J
).all;
523 AF
: constant Integer := Arg
'First;
526 if Arg
'Length /= 0 and then Arg
(AF
) = '-' then
527 if Arg
(AF
+ 1) = 'g'
528 and then (Arg
'Length = 2
529 or else Arg
(AF
+ 2) in '0' .. '3'
530 or else Arg
(AF
+ 2 .. Arg
'Last) = "coff")
532 Debug_Flag_Present
:= True;
536 -- Add directory to source search dirs so that
537 -- Get_Target_Parameters can find system.ads
539 if Arg
(AF
.. AF
+ 1) = "-I"
540 and then Arg
'Length > 2
542 Add_Src_Search_Dir
(Arg
(AF
+ 2 .. Arg
'Last));
545 -- Pass to gcc for compiling binder generated file
546 -- No use passing libraries, it will just generate
549 if not (Arg
(AF
.. AF
+ 1) = "-l"
550 or else Arg
(AF
.. AF
+ 1) = "-L")
552 Binder_Options
.Increment_Last
;
553 Binder_Options
.Table
(Binder_Options
.Last
) :=
557 -- Pass to gcc for linking program
559 Gcc_Linker_Options.Increment_Last;
560 Gcc_Linker_Options.Table
561 (Gcc_Linker_Options.Last) := new String'(Arg
);
566 -- Send all multi-character switches not recognized as
567 -- a special case by gnatlink to the linker/loader stage.
570 Linker_Options
.Increment_Last
;
571 Linker_Options
.Table
(Linker_Options
.Last
) :=
575 -- Here if argument is a file name rather than a switch
578 -- If explicit ali file, capture it
581 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
583 if Ali_File_Name = null then
584 Ali_File_Name := new String'(Arg
);
586 Exit_With_Error
("cannot handle more than one ALI file");
589 -- If target object file, record object file
591 elsif Arg
'Length > Get_Target_Object_Suffix
.all'Length
594 Get_Target_Object_Suffix
.all'Length + 1 .. Arg
'Last)
595 = Get_Target_Object_Suffix
.all
597 Linker_Objects
.Increment_Last
;
598 Linker_Objects
.Table
(Linker_Objects
.Last
) :=
601 -- If host object file, record object file
602 -- e.g. accept foo.o as well as foo.obj on VMS target
604 elsif Arg'Length > Get_Object_Suffix.all'Length
606 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
607 = Get_Object_Suffix.all
609 Linker_Objects.Increment_Last;
610 Linker_Objects.Table (Linker_Objects.Last) :=
613 -- If corresponding ali file exists, capture it
615 elsif Ali_File_Name
= null
616 and then Is_Regular_File
(Arg
& ".ali")
618 Ali_File_Name
:= new String'(Arg & ".ali");
620 -- Otherwise assume this is a linker options entry, but
621 -- see below for interesting adjustment to this assumption.
624 Linker_Options.Increment_Last;
625 Linker_Options.Table (Linker_Options.Last) :=
631 Next_Arg
:= Next_Arg
+ 1;
634 -- If Ada bind file, then compile it with warnings suppressed, because
635 -- otherwise the with of the main program may cause junk warnings.
637 if Ada_Bind_File
then
638 Binder_Options
.Increment_Last
;
639 Binder_Options
.Table
(Binder_Options
.Last
) := new String'("-gnatws");
642 -- If we did not get an ali file at all, and we had at least one
643 -- linker option, then assume that was the intended ali file after
644 -- all, so that we get a nicer message later on.
646 if Ali_File_Name = null
647 and then Linker_Options.Last >= Linker_Options.First
650 new String'(Linker_Options
.Table
(Linker_Options
.First
).all &
655 -------------------------
656 -- Process_Binder_File --
657 -------------------------
659 procedure Process_Binder_File
(Name
: String) is
661 -- Binder file's descriptor
663 Link_Bytes
: Integer := 0;
664 -- Projected number of bytes for the linker command line
667 pragma Import
(C
, Link_Max
, "__gnat_link_max");
668 -- Maximum number of bytes on the command line supported by the OS
669 -- linker. Passed this limit the response file mechanism must be used
672 Next_Line
: String (1 .. 1000);
673 -- Current line value
677 -- Current line slice (the slice does not contain line terminator)
680 -- Current line last character for shared libraries (without version)
682 Objs_Begin
: Integer := 0;
683 -- First object file index in Linker_Objects table
685 Objs_End
: Integer := 0;
686 -- Last object file index in Linker_Objects table
689 pragma Warnings
(Off
, Status
);
690 -- Used for various Interfaces.C_Streams calls
692 Closing_Status
: Boolean;
693 pragma Warnings
(Off
, Closing_Status
);
696 GNAT_Static
: Boolean := False;
697 -- Save state of -static option
699 GNAT_Shared
: Boolean := False;
700 -- Save state of -shared option
702 Xlinker_Was_Previous
: Boolean := False;
703 -- Indicate that "-Xlinker" was the option preceding the current
704 -- option. If True, then the current option is never suppressed.
708 -- These data items are used to store current binder file context.
709 -- The context is composed of the file descriptor position and the
710 -- current line together with the slice indexes (first and last
711 -- position) for this line. The rollback data are used by the
712 -- Store_File_Context and Rollback_File_Context routines below.
713 -- The file context mechanism interact only with the Get_Next_Line
714 -- call. For example:
716 -- Store_File_Context;
718 -- Rollback_File_Context;
721 -- Both Get_Next_Line calls above will read the exact same data from
722 -- the file. In other words, Next_Line, Nfirst and Nlast variables
723 -- will be set with the exact same values.
725 RB_File_Pos
: long
; -- File position
726 RB_Next_Line
: String (1 .. 1000); -- Current line content
727 RB_Nlast
: Integer; -- Slice last index
728 RB_Nfirst
: Integer; -- Slice first index
730 Run_Path_Option_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
731 pragma Import
(C
, Run_Path_Option_Ptr
, "__gnat_run_path_option");
732 -- Pointer to string representing the native linker option which
733 -- specifies the path where the dynamic loader should find shared
734 -- libraries. Equal to null string if this system doesn't support it.
736 Object_Library_Ext_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
738 (C
, Object_Library_Ext_Ptr
, "__gnat_object_library_extension");
739 -- Pointer to string specifying the default extension for
740 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
742 Object_File_Option_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
743 pragma Import
(C
, Object_File_Option_Ptr
, "__gnat_object_file_option");
744 -- Pointer to a string representing the linker option which specifies
745 -- the response file.
747 Using_GNU_Linker
: Boolean;
748 for Using_GNU_Linker
'Size use Character'Size;
749 pragma Import
(C
, Using_GNU_Linker
, "__gnat_using_gnu_linker");
750 -- Predicate indicating whether this target uses the GNU linker. In
751 -- this case we must output a GNU linker compatible response file.
753 Separate_Run_Path_Options
: Boolean;
754 for Separate_Run_Path_Options
'Size use Character'Size;
756 (C
, Separate_Run_Path_Options
, "__gnat_separate_run_path_options");
757 -- Whether separate rpath options should be emitted for each directory
759 Opening
: aliased constant String := """";
760 Closing
: aliased constant String := '"' & ASCII
.LF
;
761 -- Needed to quote object paths in object list files when GNU linker
764 procedure Get_Next_Line
;
765 -- Read the next line from the binder file without the line
768 function Index
(S
, Pattern
: String) return Natural;
769 -- Return the last occurrence of Pattern in S, or 0 if none
771 function Is_Option_Present
(Opt
: String) return Boolean;
772 -- Return true if the option Opt is already present in
773 -- Linker_Options table.
775 procedure Store_File_Context
;
776 -- Store current file context, Fd position and current line data.
777 -- The file context is stored into the rollback data above (RB_*).
778 -- Store_File_Context can be called at any time, only the last call
779 -- will be used (i.e. this routine overwrites the file context).
781 procedure Rollback_File_Context
;
782 -- Restore file context from rollback data. This routine must be called
783 -- after Store_File_Context. The binder file context will be restored
784 -- with the data stored by the last Store_File_Context call.
790 procedure Get_Next_Line
is
794 Fchars
:= fgets
(Next_Line
'Address, Next_Line
'Length, Fd
);
796 if Fchars
= System
.Null_Address
then
797 Exit_With_Error
("Error reading binder output");
800 Nfirst
:= Next_Line
'First;
802 while Nlast
<= Next_Line
'Last
803 and then Next_Line
(Nlast
) /= ASCII
.LF
804 and then Next_Line
(Nlast
) /= ASCII
.CR
816 function Index
(S
, Pattern
: String) return Natural is
817 Len
: constant Natural := Pattern
'Length;
820 for J
in reverse S
'First .. S
'Last - Len
+ 1 loop
821 if Pattern
= S
(J
.. J
+ Len
- 1) then
829 -----------------------
830 -- Is_Option_Present --
831 -----------------------
833 function Is_Option_Present
(Opt
: String) return Boolean is
835 for I
in 1 .. Linker_Options
.Last
loop
837 if Linker_Options
.Table
(I
).all = Opt
then
844 end Is_Option_Present
;
846 ---------------------------
847 -- Rollback_File_Context --
848 ---------------------------
850 procedure Rollback_File_Context
is
852 Next_Line
:= RB_Next_Line
;
855 Status
:= fseek
(Fd
, RB_File_Pos
, Interfaces
.C_Streams
.SEEK_SET
);
858 Exit_With_Error
("Error setting file position");
860 end Rollback_File_Context
;
862 ------------------------
863 -- Store_File_Context --
864 ------------------------
866 procedure Store_File_Context
is
867 use type System
.CRTL
.long
;
869 RB_Next_Line
:= Next_Line
;
872 RB_File_Pos
:= ftell
(Fd
);
874 if RB_File_Pos
= -1 then
875 Exit_With_Error
("Error getting file position");
877 end Store_File_Context
;
879 -- Start of processing for Process_Binder_File
882 Fd
:= fopen
(Name
'Address, Read_Mode
'Address);
884 if Fd
= NULL_Stream
then
885 Exit_With_Error
("Failed to open binder output");
888 -- Skip up to the Begin Info line
892 exit when Next_Line
(Nfirst
.. Nlast
) = Begin_Info
;
898 -- Go to end when end line is reached (this will happen in
899 -- High_Integrity_Mode where no -L switches are generated)
901 exit when Next_Line
(Nfirst
.. Nlast
) = End_Info
;
903 if Ada_Bind_File
then
904 Next_Line
(Nfirst
.. Nlast
- 8) :=
905 Next_Line
(Nfirst
+ 8 .. Nlast
);
909 -- Go to next section when switches are reached
911 exit when Next_Line
(1) = '-';
913 -- Otherwise we have another object file to collect
915 Linker_Objects
.Increment_Last
;
917 -- Mark the positions of first and last object files in case
918 -- they need to be placed with a named file on systems having
919 -- linker line limitations.
921 if Objs_Begin
= 0 then
922 Objs_Begin
:= Linker_Objects
.Last
;
925 Linker_Objects
.Table
(Linker_Objects
.Last
) :=
926 new String'(Next_Line (Nfirst .. Nlast));
928 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
929 -- Nlast - Nfirst + 1, for the size, plus one for the space between
933 Objs_End := Linker_Objects.Last;
935 -- Continue to compute the Link_Bytes, the linker options are part of
936 -- command line length.
940 while Next_Line (Nfirst .. Nlast) /= End_Info loop
941 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
945 Rollback_File_Context;
947 -- On systems that have limitations on handling very long linker lines
948 -- we make use of the system linker option which takes a list of object
949 -- file names from a file instead of the command line itself. What we do
950 -- is to replace the list of object files by the special linker option
951 -- which then reads the object file list from a file instead. The option
952 -- to read from a file instead of the command line is only triggered if
953 -- a conservative threshold is passed.
955 if VM_Target = CLI_Target
956 and then Link_Bytes > Link_Max
958 Lname := new String'("l~" & Base_Name
(Ali_File_Name
.all) & ".il");
960 for J
in Objs_Begin
.. Objs_End
loop
961 Copy_File
(Linker_Objects
.Table
(J
).all, Lname
.all,
962 Success
=> Closing_Status
,
966 -- Add the special objects list file option together with the name
967 -- of the temporary file to the objects file table.
969 Linker_Objects
.Table
(Objs_Begin
) :=
970 new String'(Value (Object_File_Option_Ptr) & Lname.all);
972 -- The slots containing these object file names are then removed
973 -- from the objects table so they do not appear in the link. They
974 -- are removed by moving up the linker options and non-Ada object
975 -- files appearing after the Ada object list in the table.
981 N := Objs_End - Objs_Begin + 1;
983 for J in Objs_End + 1 .. Linker_Objects.Last loop
984 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
987 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
990 elsif Object_List_File_Required
991 or else (Object_List_File_Supported
992 and then Link_Bytes > Link_Max)
994 -- Create a temporary file containing the Ada user object files
995 -- needed by the link. This list is taken from the bind file
996 -- and is output one object per line for maximal compatibility with
997 -- linkers supporting this option.
999 Create_Temp_File (Tname_FD, Tname);
1001 -- ??? File descriptor should be checked to not be Invalid_FD.
1002 -- ??? Status of Write and Close operations should be checked, and
1003 -- failure should occur if a status is wrong.
1005 -- If target is using the GNU linker we must add a special header
1006 -- and footer in the response file.
1008 -- The syntax is : INPUT (object1.o object2.o ... )
1010 -- Because the GNU linker does not like name with characters such
1011 -- as '!', we must put the object paths between double quotes.
1013 if Using_GNU_Linker then
1015 GNU_Header : aliased constant String := "INPUT (";
1018 Status := Write (Tname_FD, GNU_Header'Address,
1023 for J in Objs_Begin .. Objs_End loop
1025 -- Opening quote for GNU linker
1027 if Using_GNU_Linker then
1028 Status := Write (Tname_FD, Opening'Address, 1);
1031 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
1032 Linker_Objects.Table (J).all'Length);
1034 -- Closing quote for GNU linker
1036 if Using_GNU_Linker then
1037 Status := Write (Tname_FD, Closing'Address, 2);
1040 Status := Write (Tname_FD, ASCII.LF'Address, 1);
1043 Response_File_Objects.Increment_Last;
1044 Response_File_Objects.Table (Response_File_Objects.Last) :=
1045 Linker_Objects.Table (J);
1048 -- Handle GNU linker response file footer
1050 if Using_GNU_Linker then
1052 GNU_Footer : aliased constant String := ")";
1055 Status := Write (Tname_FD, GNU_Footer'Address,
1060 Close (Tname_FD, Closing_Status);
1062 -- Add the special objects list file option together with the name
1063 -- of the temporary file (removing the null character) to the objects
1066 Linker_Objects.Table (Objs_Begin) :=
1067 new String'(Value
(Object_File_Option_Ptr
) &
1068 Tname
(Tname
'First .. Tname
'Last - 1));
1070 -- The slots containing these object file names are then removed
1071 -- from the objects table so they do not appear in the link. They
1072 -- are removed by moving up the linker options and non-Ada object
1073 -- files appearing after the Ada object list in the table.
1079 N
:= Objs_End
- Objs_Begin
+ 1;
1081 for J
in Objs_End
+ 1 .. Linker_Objects
.Last
loop
1082 Linker_Objects
.Table
(J
- N
+ 1) := Linker_Objects
.Table
(J
);
1085 Linker_Objects
.Set_Last
(Linker_Objects
.Last
- N
+ 1);
1089 -- Process switches and options
1091 if Next_Line
(Nfirst
.. Nlast
) /= End_Info
then
1092 Xlinker_Was_Previous
:= False;
1095 if Xlinker_Was_Previous
1096 or else Next_Line
(Nfirst
.. Nlast
) = "-Xlinker"
1098 Linker_Options
.Increment_Last
;
1099 Linker_Options
.Table
(Linker_Options
.Last
) :=
1100 new String'(Next_Line (Nfirst .. Nlast));
1102 elsif Next_Line (Nfirst .. Nlast) = "-static" then
1103 GNAT_Static := True;
1105 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1106 GNAT_Shared := True;
1108 -- Add binder options only if not already set on the command
1109 -- line. This rule is a way to control the linker options order.
1111 -- The following test needs comments, why is it VMS specific.
1112 -- The above comment looks out of date ???
1114 elsif not (OpenVMS_On_Target
1116 Is_Option_Present (Next_Line (Nfirst .. Nlast)))
1118 if Nlast > Nfirst + 2 and then
1119 Next_Line (Nfirst .. Nfirst + 1) = "-L"
1121 -- Construct a library search path for use later
1122 -- to locate static gnatlib libraries.
1124 if Libpath.Last > 1 then
1125 Libpath.Increment_Last;
1126 Libpath.Table (Libpath.Last) := Path_Separator;
1129 for I in Nfirst + 2 .. Nlast loop
1130 Libpath.Increment_Last;
1131 Libpath.Table (Libpath.Last) := Next_Line (I);
1134 Linker_Options.Increment_Last;
1136 Linker_Options.Table (Linker_Options.Last) :=
1137 new String'(Next_Line
(Nfirst
.. Nlast
));
1139 elsif Next_Line
(Nfirst
.. Nlast
) = "-ldecgnat"
1140 or else Next_Line
(Nfirst
.. Nlast
) = "-lgnarl"
1141 or else Next_Line
(Nfirst
.. Nlast
) = "-lgnat"
1143 (1 .. Natural'Min (Nlast
, 8 + Library_Version
'Length)) =
1144 Shared_Lib
("gnarl")
1146 (1 .. Natural'Min (Nlast
, 7 + Library_Version
'Length)) =
1149 -- If it is a shared library, remove the library version.
1150 -- We will be looking for the static version of the library
1151 -- as it is in the same directory as the shared version.
1153 if Next_Line
(Nlast
- Library_Version
'Length + 1 .. Nlast
)
1156 -- Set Last to point to last character before the
1159 Last
:= Nlast
- Library_Version
'Length - 1;
1164 -- Given a Gnat standard library, search the
1165 -- library path to find the library location
1168 File_Path
: String_Access
;
1170 Object_Lib_Extension
: constant String :=
1171 Value
(Object_Library_Ext_Ptr
);
1173 File_Name
: constant String := "lib" &
1174 Next_Line
(Nfirst
+ 2 .. Last
) &
1175 Object_Lib_Extension
;
1177 Run_Path_Opt
: constant String :=
1178 Value
(Run_Path_Option_Ptr
);
1180 GCC_Index
: Natural;
1181 Run_Path_Opt_Index
: Natural := 0;
1185 Locate_Regular_File
(File_Name
,
1186 String (Libpath
.Table
(1 .. Libpath
.Last
)));
1188 if File_Path
/= null then
1191 -- If static gnatlib found, explicitly
1192 -- specify to overcome possible linker
1193 -- default usage of shared version.
1195 Linker_Options
.Increment_Last
;
1197 Linker_Options
.Table
(Linker_Options
.Last
) :=
1198 new String'(File_Path.all);
1200 elsif GNAT_Shared then
1201 if Opt.Run_Path_Option then
1202 -- If shared gnatlib desired, add the
1203 -- appropriate system specific switch
1204 -- so that it can be located at runtime.
1206 if Run_Path_Opt'Length /= 0 then
1207 -- Output the system specific linker command
1208 -- that allows the image activator to find
1209 -- the shared library at runtime.
1210 -- Also add path to find libgcc_s.so, if
1213 -- To find the location of the shared version
1214 -- of libgcc, we look for "gcc-lib" in the
1215 -- path of the library. However, this
1216 -- subdirectory is no longer present in
1217 -- in recent version of GCC. So, we look for
1218 -- the last subdirectory "lib" in the path.
1221 Index (File_Path.all, "gcc-lib");
1223 if GCC_Index /= 0 then
1224 -- The shared version of libgcc is
1225 -- located in the parent directory.
1227 GCC_Index := GCC_Index - 1;
1231 Index (File_Path.all, "/lib/");
1233 if GCC_Index = 0 then
1235 Index (File_Path.all,
1236 Directory_Separator &
1238 Directory_Separator);
1241 -- We have found a subdirectory "lib",
1242 -- this is where the shared version of
1243 -- libgcc should be located.
1245 if GCC_Index /= 0 then
1246 GCC_Index := GCC_Index + 3;
1250 -- Look for an eventual run_path_option in
1251 -- the linker switches.
1253 if Separate_Run_Path_Options then
1254 Linker_Options.Increment_Last;
1255 Linker_Options.Table
1256 (Linker_Options.Last) :=
1260 (1 .. File_Path
'Length
1261 - File_Name
'Length));
1263 if GCC_Index
/= 0 then
1264 Linker_Options
.Increment_Last
;
1265 Linker_Options
.Table
1266 (Linker_Options
.Last
) :=
1269 & File_Path (1 .. GCC_Index));
1273 1 .. Linker_Options.Last
1275 if Linker_Options.Table (J) /= null
1277 Linker_Options.Table (J)'Length
1278 > Run_Path_Opt'Length
1280 Linker_Options.Table (J)
1281 (1 .. Run_Path_Opt'Length) =
1284 -- We have found a already specified
1285 -- run_path_option: we will add to
1286 -- this switch, because only one
1287 -- run_path_option should be
1290 Run_Path_Opt_Index := J;
1295 -- If there is no run_path_option, we need
1298 if Run_Path_Opt_Index = 0 then
1299 Linker_Options.Increment_Last;
1302 if GCC_Index = 0 then
1303 if Run_Path_Opt_Index = 0 then
1304 Linker_Options.Table
1305 (Linker_Options.Last) :=
1309 (1 .. File_Path
'Length
1310 - File_Name
'Length));
1313 Linker_Options
.Table
1314 (Run_Path_Opt_Index
) :=
1316 (Linker_Options.Table
1317 (Run_Path_Opt_Index).all
1320 (1 .. File_Path'Length
1321 - File_Name'Length));
1325 if Run_Path_Opt_Index = 0 then
1326 Linker_Options.Table
1327 (Linker_Options.Last) :=
1328 new String'(Run_Path_Opt
1330 (1 .. File_Path
'Length
1333 & File_Path
(1 .. GCC_Index
));
1336 Linker_Options
.Table
1337 (Run_Path_Opt_Index
) :=
1339 (Linker_Options.Table
1340 (Run_Path_Opt_Index).all
1343 (1 .. File_Path'Length
1346 & File_Path (1 .. GCC_Index));
1353 -- Then we add the appropriate -l switch
1355 Linker_Options.Increment_Last;
1356 Linker_Options.Table (Linker_Options.Last) :=
1357 new String'(Next_Line
(Nfirst
.. Nlast
));
1361 -- If gnatlib library not found, then
1362 -- add it anyway in case some other
1363 -- mechanism may find it.
1365 Linker_Options
.Increment_Last
;
1366 Linker_Options
.Table
(Linker_Options
.Last
) :=
1367 new String'(Next_Line (Nfirst .. Nlast));
1371 Linker_Options.Increment_Last;
1372 Linker_Options.Table (Linker_Options.Last) :=
1373 new String'(Next_Line
(Nfirst
.. Nlast
));
1377 Xlinker_Was_Previous
:= Next_Line
(Nfirst
.. Nlast
) = "-Xlinker";
1380 exit when Next_Line
(Nfirst
.. Nlast
) = End_Info
;
1382 if Ada_Bind_File
then
1383 Next_Line
(Nfirst
.. Nlast
- 8) :=
1384 Next_Line
(Nfirst
+ 8 .. Nlast
);
1390 -- If -shared was specified, invoke gcc with -shared-libgcc
1393 Linker_Options
.Increment_Last
;
1394 Linker_Options
.Table
(Linker_Options
.Last
) := Shared_Libgcc
;
1397 Status
:= fclose
(Fd
);
1398 end Process_Binder_File
;
1406 Write_Str
("Usage: ");
1407 Write_Str
(Base_Name
(Command_Name
));
1408 Write_Str
(" switches mainprog.ali [non-Ada-objects] [linker-options]");
1411 Write_Line
(" mainprog.ali the ALI file of the main program");
1413 Write_Line
(" -A Binder generated source file is in Ada (default)");
1414 Write_Line
(" -C Binder generated source file is in C");
1415 Write_Line
(" -f force object file list to be generated");
1416 Write_Line
(" -g Compile binder source file with debug information");
1417 Write_Line
(" -n Do not compile the binder source file");
1418 Write_Line
(" -R Do not use a run_path_option");
1419 Write_Line
(" -v verbose mode");
1420 Write_Line
(" -v -v very verbose mode");
1422 Write_Line
(" -o nam Use 'nam' as the name of the executable");
1423 Write_Line
(" -b target Compile the binder source to run on target");
1424 Write_Line
(" -Bdir Load compiler executables from dir");
1426 if Is_Supported
(Map_File
) then
1427 Write_Line
(" -Mmap Create map file map");
1428 Write_Line
(" -M Create map file mainprog.map");
1431 Write_Line
(" --GCC=comp Use comp as the compiler");
1432 Write_Line
(" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1434 Write_Line
(" [non-Ada-objects] list of non Ada object files");
1435 Write_Line
(" [linker-options] other options for the linker");
1442 procedure Write_Header
is
1444 if Verbose_Mode
then
1446 Display_Version
("GNATLINK", "1995");
1454 procedure Write_Usage
is
1460 -- Start of processing for Gnatlink
1463 -- Add the directory where gnatlink is invoked in front of the
1464 -- path, if gnatlink is invoked with directory information.
1465 -- Only do this if the platform is not VMS, where the notion of path
1466 -- does not really exist.
1468 if not Hostparm
.OpenVMS
then
1470 Command
: constant String := Command_Name
;
1473 for Index
in reverse Command
'Range loop
1474 if Command
(Index
) = Directory_Separator
then
1476 Absolute_Dir
: constant String :=
1478 (Command
(Command
'First .. Index
));
1480 PATH
: constant String :=
1483 Getenv
("PATH").all;
1486 Setenv
("PATH", PATH
);
1497 if Argument_Count
= 0
1499 (Verbose_Mode
and then Argument_Count
= 1)
1502 Exit_Program
(E_Fatal
);
1505 -- Initialize packages to be used
1511 -- We always compile with -c
1513 Binder_Options_From_ALI
.Increment_Last
;
1514 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1517 if Ali_File_Name = null then
1518 Exit_With_Error ("no ali file given for link");
1521 if not Is_Regular_File (Ali_File_Name.all) then
1522 Exit_With_Error (Ali_File_Name.all & " not found");
1525 -- Read the ALI file of the main subprogram if the binder generated
1526 -- file needs to be compiled and no --GCC= switch has been specified.
1527 -- Fetch the back end switches from this ALI file and use these switches
1528 -- to compile the binder generated file
1530 if Compile_Bind_File and then Standard_Gcc then
1533 Name_Len := Ali_File_Name'Length;
1534 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1538 F : constant File_Name_Type := Name_Find;
1539 T : Text_Buffer_Ptr;
1543 -- Load the ALI file
1545 T := Read_Library_Info (F, True);
1547 -- Read it. Note that we ignore errors, since we only want very
1548 -- limited information from the ali file, and likely a slightly
1549 -- wrong version will be just fine, though in normal operation
1550 -- we don't expect this to happen!
1557 Ignore_Errors => True);
1559 if A /= No_ALI_Id then
1561 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1562 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1564 -- Do not compile with the front end switches. However, --RTS
1565 -- is to be dealt with specially because it needs to be passed
1566 -- if the binder-generated file is in Ada and may also be used
1567 -- to drive the linker.
1570 Arg : String_Ptr renames Args.Table (Index);
1572 if not Is_Front_End_Switch (Arg.all) then
1573 Binder_Options_From_ALI.Increment_Last;
1574 Binder_Options_From_ALI.Table
1575 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1577 elsif Arg'Length > 5
1578 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1580 if Ada_Bind_File then
1581 Binder_Options_From_ALI.Increment_Last;
1582 Binder_Options_From_ALI.Table
1583 (Binder_Options_From_ALI.Last)
1584 := String_Access (Arg);
1587 -- Set the RTS_*_Path_Name variables, so that the
1588 -- correct directories will be set when
1589 -- Osint.Add_Default_Search_Dirs will be called later.
1591 Opt.RTS_Src_Path_Name :=
1593 (Arg (Arg'First + 6 .. Arg'Last), Include);
1595 Opt.RTS_Lib_Path_Name :=
1597 (Arg (Arg'First + 6 .. Arg'Last), Objects);
1599 -- GNAT doesn't support the GCC multilib mechanism.
1600 -- This means that, when a multilib switch is used
1601 -- to request a particular compilation mode, the
1602 -- corresponding runtime switch (--RTS) must also be
1603 -- specified. The long-term goal is to fully support the
1604 -- multilib mechanism; however, in the meantime, it is
1605 -- convenient to eliminate the redundancy by keying the
1606 -- compilation mode on a single switch, namely --RTS.
1608 -- Pass -mrtp to the linker if --RTS=rtp was passed
1611 and then Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1613 Linker_Options.Increment_Last;
1614 Linker_Options.Table (Linker_Options.Last) :=
1615 new String'("-mrtp");
1617 -- Pass -fsjlj to the linker if --RTS=sjlj was passed
1619 elsif Arg
'Length > 9
1620 and then Arg
(Arg
'First + 6 .. Arg
'First + 9) = "sjlj"
1622 Linker_Options
.Increment_Last
;
1623 Linker_Options
.Table
(Linker_Options
.Last
) :=
1624 new String'("-fsjlj");
1633 -- Get target parameters
1635 Osint.Add_Default_Search_Dirs;
1636 Targparm.Get_Target_Parameters;
1638 if VM_Target /= No_VM then
1640 when JVM_Target => Gcc := new String'("jvm-gnatcompile");
1641 when CLI_Target
=> Gcc
:= new String'("dotnet-gnatcompile");
1642 when No_VM => raise Program_Error;
1645 Ada_Bind_File := True;
1646 Begin_Info := "-- BEGIN Object file/option list";
1647 End_Info := "-- END Object file/option list ";
1650 -- If the main program is in Ada it is compiled with the following
1653 -- -gnatA stops reading gnat.adc, since we don't know what
1654 -- pragmas would work, and we do not need it anyway.
1656 -- -gnatWb allows brackets coding for wide characters
1658 -- -gnatiw allows wide characters in identifiers. This is needed
1659 -- because bindgen uses brackets encoding for all upper
1660 -- half and wide characters in identifier names.
1662 if Ada_Bind_File then
1663 Binder_Options_From_ALI.Increment_Last;
1664 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1665 new String'("-gnatA");
1666 Binder_Options_From_ALI
.Increment_Last
;
1667 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
1668 new String'("-gnatWb");
1669 Binder_Options_From_ALI.Increment_Last;
1670 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1671 new String'("-gnatiw");
1674 -- Locate all the necessary programs and verify required files are present
1676 Gcc_Path
:= System
.OS_Lib
.Locate_Exec_On_Path
(Gcc
.all);
1678 if Gcc_Path
= null then
1679 Exit_With_Error
("Couldn't locate " & Gcc
.all);
1682 if Linker_Path
= null then
1683 if VM_Target
= CLI_Target
then
1684 Linker_Path
:= System
.OS_Lib
.Locate_Exec_On_Path
("ilasm");
1686 if Linker_Path
= null then
1687 Exit_With_Error
("Couldn't locate ilasm");
1690 elsif RTX_RTSS_Kernel_Module_On_Target
then
1692 -- Use Microsoft linker for RTSS modules
1694 Linker_Path
:= System
.OS_Lib
.Locate_Exec_On_Path
("link");
1696 if Linker_Path
= null then
1697 Exit_With_Error
("Couldn't locate link");
1701 Linker_Path
:= Gcc_Path
;
1707 -- If no output name specified, then use the base name of .ali file name
1709 if Output_File_Name
= null then
1711 new String'(Base_Name (Ali_File_Name.all)
1712 & Get_Target_Debuggable_Suffix.all);
1715 if VM_Target = CLI_Target then
1716 Linker_Options.Increment_Last;
1717 Linker_Options.Table (Linker_Options.Last) := new String'("/QUIET");
1719 Linker_Options
.Increment_Last
;
1720 Linker_Options
.Table
(Linker_Options
.Last
) := new String'("/DEBUG");
1722 Linker_Options.Increment_Last;
1723 Linker_Options.Table (Linker_Options.Last) :=
1724 new String'("/OUTPUT=" & Output_File_Name
.all);
1726 elsif RTX_RTSS_Kernel_Module_On_Target
then
1727 Linker_Options
.Increment_Last
;
1728 Linker_Options
.Table
(Linker_Options
.Last
) :=
1729 new String'("/OUT:" & Output_File_Name.all);
1732 Linker_Options.Increment_Last;
1733 Linker_Options.Table (Linker_Options.Last) := new String'("-o");
1735 Linker_Options
.Increment_Last
;
1736 Linker_Options
.Table
(Linker_Options
.Last
) :=
1737 new String'(Output_File_Name.all);
1740 -- Delete existing executable, in case it is a symbolic link, to avoid
1741 -- modifying the target of the symbolic link.
1745 pragma Unreferenced (Dummy);
1748 Delete_File (Output_File_Name.all, Dummy);
1751 -- Warn if main program is called "test", as that may be a built-in command
1752 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1753 -- will not appear. However, do not warn in the case of a cross compiler.
1755 -- Assume this is a cross tool if the executable name is not gnatlink
1757 if Base_Name (Command_Name) = "gnatlink"
1758 and then Output_File_Name.all = "test"
1760 Error_Msg ("warning: executable name """ & Output_File_Name.all
1761 & """ may conflict with shell command");
1764 -- If -M switch was specified, add the switches to create the map file
1766 if Create_Map_File then
1768 Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map";
1769 Switches : String_List_Access;
1772 Convert (Map_File, Map_Name, Switches);
1774 if Switches /= null then
1775 for J in Switches'Range loop
1776 Linker_Options.Increment_Last;
1777 Linker_Options.Table (Linker_Options.Last) := Switches (J);
1783 -- Perform consistency checks
1785 -- Transform the .ali file name into the binder output file name
1787 Make_Binder_File_Names : declare
1788 Fname : constant String := Base_Name (Ali_File_Name.all);
1789 Fname_Len : Integer := Fname'Length;
1791 function Get_Maximum_File_Name_Length return Integer;
1792 pragma Import (C, Get_Maximum_File_Name_Length,
1793 "__gnat_get_maximum_file_name_length");
1795 Maximum_File_Name_Length : constant Integer :=
1796 Get_Maximum_File_Name_Length;
1798 Bind_File_Prefix : Types.String_Ptr;
1799 -- Contains prefix used for bind files
1804 if not Ada_Bind_File then
1805 Bind_File_Prefix := new String'("b_");
1806 elsif OpenVMS_On_Target
then
1807 Bind_File_Prefix
:= new String'("b__");
1809 Bind_File_Prefix := new String'("b~");
1812 -- If the length of the binder file becomes too long due to
1813 -- the addition of the "b?" prefix, then truncate it.
1815 if Maximum_File_Name_Length
> 0 then
1817 Maximum_File_Name_Length
- Bind_File_Prefix
.all'Length
1819 Fname_Len
:= Fname_Len
- 1;
1824 Fnam
: constant String :=
1825 Bind_File_Prefix
.all &
1826 Fname
(Fname
'First .. Fname
'First + Fname_Len
- 1);
1829 if Ada_Bind_File
then
1830 Binder_Spec_Src_File
:= new String'(Fnam & ".ads");
1831 Binder_Body_Src_File := new String'(Fnam
& ".adb");
1832 Binder_Ali_File
:= new String'(Fnam & ".ali");
1834 Binder_Body_Src_File := new String'(Fnam
& ".c");
1837 Binder_Obj_File
:= new String'(Fnam & Get_Target_Object_Suffix.all);
1840 if Fname_Len /= Fname'Length then
1841 Binder_Options.Increment_Last;
1842 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1843 Binder_Options
.Increment_Last
;
1844 Binder_Options
.Table
(Binder_Options
.Last
) := Binder_Obj_File
;
1846 end Make_Binder_File_Names
;
1848 Process_Binder_File
(Binder_Body_Src_File
.all & ASCII
.NUL
);
1850 -- Compile the binder file. This is fast, so we always do it, unless
1851 -- specifically told not to by the -n switch
1853 if Compile_Bind_File
then
1856 Args
: Argument_List
1857 (1 .. Binder_Options_From_ALI
.Last
+ Binder_Options
.Last
+ 1);
1860 for J
in 1 .. Binder_Options_From_ALI
.Last
loop
1861 Args
(J
) := Binder_Options_From_ALI
.Table
(J
);
1864 for J
in 1 .. Binder_Options
.Last
loop
1865 Args
(Binder_Options_From_ALI
.Last
+ J
) :=
1866 Binder_Options
.Table
(J
);
1869 -- Use the full path of the binder generated source, so that it is
1870 -- guaranteed that the debugger will find this source, even with
1874 new String'(Normalize_Pathname (Binder_Body_Src_File.all));
1876 if Verbose_Mode then
1877 Write_Str (Base_Name (Gcc_Path.all));
1879 for J in Args'Range loop
1881 Write_Str (Args (J).all);
1887 System.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1890 Exit_Program (E_Fatal);
1895 -- Now, actually link the program
1897 -- Skip this step for now on JVM since the Java interpreter will do
1898 -- the actual link at run time. We might consider packing all class files
1899 -- in a .zip file during this step.
1901 if VM_Target /= JVM_Target then
1903 Num_Args : Natural :=
1904 (Linker_Options.Last - Linker_Options.First + 1) +
1905 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1906 (Linker_Objects.Last - Linker_Objects.First + 1);
1907 Stack_Op : Boolean := False;
1908 IDENT_Op : Boolean := False;
1911 if VM_Target = CLI_Target then
1913 -- Remove extraneous flags not relevant for CIL. Also remove empty
1914 -- arguments, since ilasm chokes on them.
1916 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1917 if Linker_Options.Table (J)'Length = 0
1918 or else Linker_Options.Table (J) (1 .. 2) = "-L"
1919 or else Linker_Options.Table (J) (1 .. 2) = "-l"
1920 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1921 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1922 or else Linker_Options.Table (J) (1 .. 2) = "-g"
1924 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1925 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1926 Linker_Options.Decrement_Last;
1927 Num_Args := Num_Args - 1;
1931 elsif RTX_RTSS_Kernel_Module_On_Target then
1933 -- Remove flags not relevant for Microsoft linker and adapt some
1936 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1938 -- Remove flags that are not accepted
1939 if Linker_Options.Table (J)'Length = 0
1940 or else Linker_Options.Table (J) (1 .. 2) = "-l"
1941 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1942 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1943 or else Linker_Options.Table (J) (1 .. 8) = "-Xlinker"
1944 or else Linker_Options.Table (J) (1 .. 9) = "-mthreads"
1946 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1947 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1948 Linker_Options.Decrement_Last;
1949 Num_Args := Num_Args - 1;
1951 -- Replace "-L" by its counterpart "/LIBPATH:" and UNIX "/" by
1953 elsif Linker_Options.Table (J) (1 .. 2) = "-L" then
1955 Libpath_Option : constant String_Access := new String'
1957 Linker_Options
.Table
(J
)
1958 (3 .. Linker_Options
.Table
(J
).all'Last));
1960 for Index
in 10 .. Libpath_Option
'Last loop
1961 if Libpath_Option
(Index
) = '/' then
1962 Libpath_Option
(Index
) := '\';
1966 Linker_Options
.Table
(J
) := Libpath_Option
;
1969 -- Replace "-g" by "/DEBUG"
1970 elsif Linker_Options
.Table
(J
) (1 .. 2) = "-g" then
1971 Linker_Options
.Table
(J
) := new String'("/DEBUG");
1973 -- Replace "-o" by "/OUT:"
1974 elsif Linker_Options.Table (J) (1 .. 2) = "-o" then
1975 Linker_Options.Table (J + 1) := new String'
1976 ("/OUT:" & Linker_Options
.Table
(J
+ 1).all);
1978 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
1979 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
1980 Linker_Options
.Decrement_Last
;
1981 Num_Args
:= Num_Args
- 1;
1983 -- Replace "--stack=" by "/STACK:"
1984 elsif Linker_Options
.Table
(J
) (1 .. 8) = "--stack=" then
1985 Linker_Options
.Table
(J
) := new String'
1987 Linker_Options.Table (J)
1988 (9 .. Linker_Options.Table (J).all'Last));
1990 -- Replace "-v" by its counterpart "/VERBOSE"
1991 elsif Linker_Options.Table (J) (1 .. 2) = "-v" then
1992 Linker_Options.Table (J) := new String'("/VERBOSE");
1996 -- Add some required flags to create RTSS modules
1999 Flags_For_Linker
: constant array (1 .. 17) of String_Access
:=
2000 (new String'("/NODEFAULTLIB"),
2001 new String'("/INCREMENTAL:NO"),
2002 new String'("/NOLOGO"),
2003 new String'("/DRIVER"),
2004 new String'("/ALIGN:0x20"),
2005 new String'("/SUBSYSTEM:NATIVE"),
2006 new String'("/ENTRY:_RtapiProcessEntryCRT@8"),
2007 new String'("/RELEASE"),
2008 new String'("startupCRT.obj"),
2009 new String'("rtxlibcmt.lib"),
2010 new String'("oldnames.lib"),
2011 new String'("rtapi_rtss.lib"),
2012 new String'("Rtx_Rtss.lib"),
2013 new String'("libkernel32.a"),
2014 new String'("libws2_32.a"),
2015 new String'("libmswsock.a"),
2016 new String'("libadvapi32.a"));
2017 -- These flags need to be passed to Microsoft linker. They
2018 -- come from the RTX documentation.
2020 Gcc_Lib_Path : constant String_Access := new String'
2021 ("/LIBPATH:" & Include_Dir_Default_Prefix
& "\..\");
2022 -- Place to look for gcc related libraries, such as libgcc
2025 -- Replace UNIX "/" by Windows "\" in the path
2027 for Index
in 10 .. Gcc_Lib_Path
.all'Last loop
2028 if Gcc_Lib_Path
(Index
) = '/' then
2029 Gcc_Lib_Path
(Index
) := '\';
2033 Linker_Options
.Increment_Last
;
2034 Linker_Options
.Table
(Linker_Options
.Last
) := Gcc_Lib_Path
;
2035 Num_Args
:= Num_Args
+ 1;
2037 for Index
in Flags_For_Linker
'Range loop
2038 Linker_Options
.Increment_Last
;
2039 Linker_Options
.Table
(Linker_Options
.Last
) :=
2040 Flags_For_Linker
(Index
);
2041 Num_Args
:= Num_Args
+ 1;
2046 -- Remove duplicate stack size setting from the Linker_Options
2047 -- table. The stack setting option "-Xlinker --stack=R,C" can be
2048 -- found in one line when set by a pragma Linker_Options or in two
2049 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
2050 -- line. We also check for the "-Wl,--stack=R" style option.
2052 -- We must remove the second stack setting option instance
2053 -- because the one on the command line will always be the first
2054 -- one. And any subsequent stack setting option will overwrite the
2055 -- previous one. This is done especially for GNAT/NT where we set
2056 -- the stack size for tasking programs by a pragma in the NT
2057 -- specific tasking package System.Task_Primitives.Operations.
2059 -- Note: This is not a FOR loop that runs from Linker_Options.First
2060 -- to Linker_Options.Last, since operations within the loop can
2061 -- modify the length of the table.
2063 Clean_Link_Option_Set
: declare
2064 J
: Natural := Linker_Options
.First
;
2065 Shared_Libgcc_Seen
: Boolean := False;
2068 while J
<= Linker_Options
.Last
loop
2070 if Linker_Options
.Table
(J
).all = "-Xlinker"
2071 and then J
< Linker_Options
.Last
2072 and then Linker_Options
.Table
(J
+ 1)'Length > 8
2073 and then Linker_Options
.Table
(J
+ 1) (1 .. 8) = "--stack="
2076 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 2) :=
2077 Linker_Options
.Table
(J
+ 2 .. Linker_Options
.Last
);
2078 Linker_Options
.Decrement_Last
;
2079 Linker_Options
.Decrement_Last
;
2080 Num_Args
:= Num_Args
- 2;
2087 -- Remove duplicate -shared-libgcc switch
2089 if Linker_Options
.Table
(J
).all = Shared_Libgcc_String
then
2090 if Shared_Libgcc_Seen
then
2091 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
2092 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
2093 Linker_Options
.Decrement_Last
;
2094 Num_Args
:= Num_Args
- 1;
2097 Shared_Libgcc_Seen
:= True;
2101 -- Here we just check for a canonical form that matches the
2102 -- pragma Linker_Options set in the NT runtime.
2104 if (Linker_Options
.Table
(J
)'Length > 17
2105 and then Linker_Options
.Table
(J
) (1 .. 17)
2106 = "-Xlinker --stack=")
2108 (Linker_Options
.Table
(J
)'Length > 12
2109 and then Linker_Options
.Table
(J
) (1 .. 12)
2113 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
2114 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
2115 Linker_Options
.Decrement_Last
;
2116 Num_Args
:= Num_Args
- 1;
2123 -- Remove duplicate IDENTIFICATION directives (VMS)
2125 if Linker_Options
.Table
(J
)'Length > 27
2126 and then Linker_Options
.Table
(J
) (1 .. 28)
2127 = "--for-linker=IDENTIFICATION="
2130 Linker_Options
.Table
(J
.. Linker_Options
.Last
- 1) :=
2131 Linker_Options
.Table
(J
+ 1 .. Linker_Options
.Last
);
2132 Linker_Options
.Decrement_Last
;
2133 Num_Args
:= Num_Args
- 1;
2142 if Linker_Path
= Gcc_Path
and then VM_Target
= No_VM
then
2144 -- For systems where the default is to link statically with
2145 -- libgcc, if gcc is not called with -shared-libgcc, call it
2146 -- with -static-libgcc, as there are some platforms where one
2147 -- of these two switches is compulsory to link.
2149 if Shared_Libgcc_Default
= 'T'
2150 and then not Shared_Libgcc_Seen
2152 Linker_Options
.Increment_Last
;
2153 Linker_Options
.Table
(Linker_Options
.Last
) := Static_Libgcc
;
2154 Num_Args
:= Num_Args
+ 1;
2157 elsif RTX_RTSS_Kernel_Module_On_Target
then
2159 -- Force the use of the static libgcc for RTSS modules
2161 Linker_Options
.Increment_Last
;
2162 Linker_Options
.Table
(Linker_Options
.Last
) :=
2163 new String'("libgcc.a");
2164 Num_Args := Num_Args + 1;
2167 end Clean_Link_Option_Set;
2169 -- Prepare arguments for call to linker
2171 Call_Linker : declare
2173 Args : Argument_List (1 .. Num_Args + 1);
2174 Index : Integer := Args'First;
2177 Args (Index) := Binder_Obj_File;
2179 -- Add the object files and any -largs libraries
2181 for J in Linker_Objects.First .. Linker_Objects.Last loop
2183 Args (Index) := Linker_Objects.Table (J);
2186 -- Add the linker options from the binder file
2188 for J in Linker_Options.First .. Linker_Options.Last loop
2190 Args (Index) := Linker_Options.Table (J);
2193 -- Finally add the libraries from the --GCC= switch
2195 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
2197 Args (Index) := Gcc_Linker_Options.Table (J);
2200 if Verbose_Mode then
2201 Write_Str (Linker_Path.all);
2203 for J in Args'Range loop
2205 Write_Str (Args (J).all);
2210 -- If we are on very verbose mode (-v -v) and a response file
2211 -- is used we display its content.
2213 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
2215 Write_Str ("Response file (" &
2216 Tname (Tname'First .. Tname'Last - 1) &
2221 Response_File_Objects.First ..
2222 Response_File_Objects.Last
2224 Write_Str (Response_File_Objects.Table (J).all);
2232 System.OS_Lib.Spawn (Linker_Path.all, Args, Success);
2234 -- Delete the temporary file used in conjunction with linking if
2235 -- one was created. See Process_Bind_File for details.
2237 if Tname_FD /= Invalid_FD then
2241 if Lname /= null then
2242 Delete (Lname.all & ASCII.NUL);
2246 Error_Msg ("error when calling " & Linker_Path.all);
2247 Exit_Program (E_Fatal);
2253 -- Only keep the binder output file and it's associated object
2254 -- file if compiling with the -g option. These files are only
2255 -- useful if debugging.
2257 if not Debug_Flag_Present then
2258 if Binder_Ali_File /= null then
2259 Delete (Binder_Ali_File.all & ASCII.NUL);
2262 if Binder_Spec_Src_File /= null then
2263 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
2266 Delete (Binder_Body_Src_File.all & ASCII.NUL);
2268 if VM_Target = No_VM then
2269 Delete (Binder_Obj_File.all & ASCII.NUL);
2273 Exit_Program (E_Success);
2277 Write_Line (Exception_Information (X));
2278 Exit_With_Error ("INTERNAL ERROR. Please report");