1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1996-2002 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 ------------------------------------------------------------------------------
28 -- Gnatlink usage: please consult the gnat documentation
30 with Ada
.Exceptions
; use Ada
.Exceptions
;
32 with Gnatvsn
; use Gnatvsn
;
34 with Namet
; use Namet
;
35 with Osint
; use Osint
;
36 with Output
; use Output
;
37 with Switch
; use Switch
;
38 with System
; use System
;
42 with Ada
.Command_Line
; use Ada
.Command_Line
;
43 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
44 with Interfaces
.C_Streams
; use Interfaces
.C_Streams
;
48 pragma Ident
(Gnat_Version_String
);
50 package Gcc_Linker_Options
is new Table
.Table
(
51 Table_Component_Type
=> String_Access
,
52 Table_Index_Type
=> Integer,
55 Table_Increment
=> 100,
56 Table_Name
=> "Gnatlink.Gcc_Linker_Options");
57 -- Comments needed ???
59 package Libpath
is new Table
.Table
(
60 Table_Component_Type
=> Character,
61 Table_Index_Type
=> Integer,
63 Table_Initial
=> 4096,
65 Table_Name
=> "Gnatlink.Libpath");
66 -- Comments needed ???
68 package Linker_Options
is new Table
.Table
(
69 Table_Component_Type
=> String_Access
,
70 Table_Index_Type
=> Integer,
73 Table_Increment
=> 100,
74 Table_Name
=> "Gnatlink.Linker_Options");
75 -- Comments needed ???
77 package Linker_Objects
is new Table
.Table
(
78 Table_Component_Type
=> String_Access
,
79 Table_Index_Type
=> Integer,
82 Table_Increment
=> 100,
83 Table_Name
=> "Gnatlink.Linker_Objects");
84 -- This table collects the objects file to be passed to the linker. In the
85 -- case where the linker command line is too long then programs objects
86 -- are put on the Response_File_Objects table. Note that the binder object
87 -- file and the user's objects remain in this table. This is very
88 -- important because on the GNU linker command line the -L switch is not
89 -- used to look for objects files but -L switch is used to look for
90 -- objects listed in the response file. This is not a problem with the
91 -- applications objects as they are specified with a fullname.
93 package Response_File_Objects
is new Table
.Table
(
94 Table_Component_Type
=> String_Access
,
95 Table_Index_Type
=> Integer,
98 Table_Increment
=> 100,
99 Table_Name
=> "Gnatlink.Response_File_Objects");
100 -- This table collects the objects file that are to be put in the response
101 -- file. Only application objects are collected there (see details in
102 -- Linker_Objects table comments)
104 package Binder_Options_From_ALI
is new Table
.Table
(
105 Table_Component_Type
=> String_Access
,
106 Table_Index_Type
=> Integer,
107 Table_Low_Bound
=> 1, -- equals low bound of Argument_List for Spawn
109 Table_Increment
=> 100,
110 Table_Name
=> "Gnatlink.Binder_Options_From_ALI");
111 -- This table collects the switches from the ALI file of the main
114 package Binder_Options
is new Table
.Table
(
115 Table_Component_Type
=> String_Access
,
116 Table_Index_Type
=> Integer,
117 Table_Low_Bound
=> 1, -- equals low bound of Argument_List for Spawn
119 Table_Increment
=> 100,
120 Table_Name
=> "Gnatlink.Binder_Options");
121 -- This table collects the arguments to be passed to compile the binder
124 subtype chars_ptr
is System
.Address
;
126 Gcc
: String_Access
:= Program_Name
("gcc");
128 Read_Mode
: constant String := "r" & ASCII
.Nul
;
130 Begin_Info
: String := "-- BEGIN Object file/option list";
131 End_Info
: String := "-- END Object file/option list ";
132 -- Note: above lines are modified in C mode, see option processing
134 Gcc_Path
: String_Access
;
135 Linker_Path
: String_Access
;
137 Output_File_Name
: String_Access
;
138 Ali_File_Name
: String_Access
;
139 Binder_Spec_Src_File
: String_Access
;
140 Binder_Body_Src_File
: String_Access
;
141 Binder_Ali_File
: String_Access
;
142 Binder_Obj_File
: String_Access
;
144 Tname
: Temp_File_Name
;
145 Tname_FD
: File_Descriptor
:= Invalid_FD
;
146 -- Temporary file used by linker to pass list of object files on
147 -- certain systems with limitations on size of arguments.
149 Debug_Flag_Present
: Boolean := False;
150 Verbose_Mode
: Boolean := False;
151 Very_Verbose_Mode
: Boolean := False;
153 Ada_Bind_File
: Boolean := True;
154 -- Set to True if bind file is generated in Ada
156 Standard_Gcc
: Boolean := True;
158 Compile_Bind_File
: Boolean := True;
159 -- Set to False if bind file is not to be compiled
161 Object_List_File_Supported
: Boolean;
162 pragma Import
(C
, Object_List_File_Supported
, "objlist_file_supported");
163 -- Predicate indicating whether the linker has an option whereby the
164 -- names of object files can be passed to the linker in a file.
166 Object_List_File_Required
: Boolean := False;
167 -- Set to True to force generation of a response file
169 function Base_Name
(File_Name
: in String) return String;
170 -- Return just the file name part without the extension (if present).
172 procedure Delete
(Name
: in String);
173 -- Wrapper to unlink as status is ignored by this application.
175 procedure Error_Msg
(Message
: in String);
176 -- Output the error or warning Message
178 procedure Exit_With_Error
(Error
: in String);
179 -- Output Error and exit program with a fatal condition.
181 procedure Process_Args
;
182 -- Go through all the arguments and build option tables.
184 procedure Process_Binder_File
(Name
: in String);
185 -- Reads the binder file and extracts linker arguments.
187 function Value
(chars
: chars_ptr
) return String;
188 -- Return NUL-terminated string chars as an Ada string.
190 procedure Write_Usage
;
191 -- Show user the program options.
197 function Base_Name
(File_Name
: in String) return String is
202 Findex1
:= File_Name
'First;
204 -- The file might be specified by a full path name. However,
205 -- we want the path to be stripped away.
207 for J
in reverse File_Name
'Range loop
208 if Is_Directory_Separator
(File_Name
(J
)) then
214 Findex2
:= File_Name
'Last;
215 while Findex2
> Findex1
216 and then File_Name
(Findex2
) /= '.'
218 Findex2
:= Findex2
- 1;
221 if Findex2
= Findex1
then
222 Findex2
:= File_Name
'Last + 1;
225 return File_Name
(Findex1
.. Findex2
- 1);
232 procedure Delete
(Name
: in String) is
236 Status
:= unlink
(Name
'Address);
243 procedure Error_Msg
(Message
: in String) is
245 Write_Str
(Base_Name
(Command_Name
));
251 ---------------------
252 -- Exit_With_Error --
253 ---------------------
255 procedure Exit_With_Error
(Error
: in String) is
258 Exit_Program
(E_Fatal
);
265 procedure Process_Args
is
269 -- Loop through arguments of gnatlink command
273 exit when Next_Arg
> Argument_Count
;
275 Process_One_Arg
: declare
276 Arg
: String := Argument
(Next_Arg
);
279 -- Case of argument which is a switch
281 -- We definitely need section by section comments here ???
283 if Arg
'Length /= 0 and then Arg
(1) = '-' then
285 and then Arg
(2 .. 5) = "gnat"
288 ("invalid switch: """ & Arg
& """ (gnat not needed here)");
292 and then (Arg
'Length < 5 or else Arg
(2 .. 5) /= "gnat")
294 Debug_Flag_Present
:= True;
296 Linker_Options
.Increment_Last
;
297 Linker_Options
.Table
(Linker_Options
.Last
) :=
300 Binder_Options.Increment_Last;
301 Binder_Options.Table (Binder_Options.Last) :=
302 Linker_Options.Table (Linker_Options.Last);
304 elsif Arg'Length = 2 then
307 Ada_Bind_File := True;
308 Begin_Info := "-- BEGIN Object file/option list";
309 End_Info := "-- END Object file/option list ";
312 Linker_Options.Increment_Last;
313 Linker_Options.Table (Linker_Options.Last) :=
316 Binder_Options
.Increment_Last
;
317 Binder_Options
.Table
(Binder_Options
.Last
) :=
318 Linker_Options
.Table
(Linker_Options
.Last
);
320 Next_Arg
:= Next_Arg
+ 1;
322 if Next_Arg
> Argument_Count
then
323 Exit_With_Error
("Missing argument for -b");
326 Get_Machine_Name
: declare
327 Name_Arg
: String_Access
:=
328 new String'(Argument (Next_Arg));
331 Linker_Options.Increment_Last;
332 Linker_Options.Table (Linker_Options.Last) :=
335 Binder_Options.Increment_Last;
336 Binder_Options.Table (Binder_Options.Last) :=
339 end Get_Machine_Name;
342 Ada_Bind_File := False;
343 Begin_Info := "/* BEGIN Object file/option list";
344 End_Info := " END Object file/option list */";
347 if Object_List_File_Supported then
348 Object_List_File_Required := True;
351 ("Object list file not supported on this target");
355 Compile_Bind_File := False;
358 Linker_Options.Increment_Last;
359 Linker_Options.Table (Linker_Options.Last) :=
362 Next_Arg
:= Next_Arg
+ 1;
364 if Next_Arg
> Argument_Count
then
365 Exit_With_Error
("Missing argument for -o");
368 Output_File_Name
:= new String'(Argument (Next_Arg));
370 Linker_Options.Increment_Last;
371 Linker_Options.Table (Linker_Options.Last) :=
376 -- Support "double" verbose mode. Second -v
377 -- gets sent to the linker and binder phases.
380 Very_Verbose_Mode := True;
382 Linker_Options.Increment_Last;
383 Linker_Options.Table (Linker_Options.Last) :=
386 Binder_Options
.Increment_Last
;
387 Binder_Options
.Table
(Binder_Options
.Last
) :=
388 Linker_Options
.Table
(Linker_Options
.Last
);
391 Verbose_Mode
:= True;
396 Linker_Options
.Increment_Last
;
397 Linker_Options
.Table
(Linker_Options
.Last
) :=
402 elsif Arg (2) = 'B
' then
403 Linker_Options.Increment_Last;
404 Linker_Options.Table (Linker_Options.Last) :=
407 Binder_Options
.Increment_Last
;
408 Binder_Options
.Table
(Binder_Options
.Last
) :=
409 Linker_Options
.Table
(Linker_Options
.Last
);
411 elsif Arg
'Length >= 7 and then Arg
(1 .. 7) = "--LINK=" then
413 if Arg
'Length = 7 then
414 Exit_With_Error
("Missing argument for --LINK=");
418 GNAT
.OS_Lib
.Locate_Exec_On_Path
(Arg
(8 .. Arg
'Last));
420 if Linker_Path
= null then
422 ("Could not locate linker: " & Arg
(8 .. Arg
'Last));
425 elsif Arg
'Length > 6 and then Arg
(1 .. 6) = "--GCC=" then
427 Program_Args
: Argument_List_Access
:=
428 Argument_String_To_List
429 (Arg
(7 .. Arg
'Last));
432 Gcc
:= new String'(Program_Args.all (1).all);
433 Standard_Gcc := False;
435 -- Set appropriate flags for switches passed
437 for J in 2 .. Program_Args.all'Last loop
439 Arg : String := Program_Args.all (J).all;
440 AF : Integer := Arg'First;
443 if Arg'Length /= 0 and then Arg (AF) = '-' then
444 if Arg (AF + 1) = 'g
'
445 and then (Arg'Length = 2
446 or else Arg (AF + 2) in '0' .. '3'
447 or else Arg (AF + 2 .. Arg'Last) = "coff")
449 Debug_Flag_Present := True;
453 -- Pass to gcc for compiling binder generated file
454 -- No use passing libraries, it will just generate
457 if not (Arg (AF .. AF + 1) = "-l"
458 or else Arg (AF .. AF + 1) = "-L")
460 Binder_Options.Increment_Last;
461 Binder_Options.Table (Binder_Options.Last) :=
465 -- Pass to gcc for linking program.
467 Gcc_Linker_Options
.Increment_Last
;
468 Gcc_Linker_Options
.Table
469 (Gcc_Linker_Options
.Last
) := new String'(Arg);
474 -- Send all multi-character switches not recognized as
475 -- a special case by gnatlink to the linker/loader stage.
478 Linker_Options.Increment_Last;
479 Linker_Options.Table (Linker_Options.Last) :=
483 -- Here if argument is a file name rather than a switch
487 and then Arg
(Arg
'Last - 3 .. Arg
'Last) = ".ali"
489 if Ali_File_Name
= null then
490 Ali_File_Name
:= new String'(Arg);
492 Exit_With_Error ("cannot handle more than one ALI file");
495 elsif Is_Regular_File (Arg & ".ali")
496 and then Ali_File_Name = null
498 Ali_File_Name := new String'(Arg
& ".ali");
500 elsif Arg
'Length > Get_Object_Suffix
.all'Length
502 (Arg
'Last - Get_Object_Suffix
.all'Length + 1 .. Arg
'Last)
503 = Get_Object_Suffix
.all
505 Linker_Objects
.Increment_Last
;
506 Linker_Objects
.Table
(Linker_Objects
.Last
) :=
510 Linker_Options.Increment_Last;
511 Linker_Options.Table (Linker_Options.Last) :=
519 Next_Arg
:= Next_Arg
+ 1;
522 -- If Ada bind file, then compile it with warnings suppressed, because
523 -- otherwise the with of the main program may cause junk warnings.
525 if Ada_Bind_File
then
526 Binder_Options
.Increment_Last
;
527 Binder_Options
.Table
(Binder_Options
.Last
) := new String'("-gnatws");
531 -------------------------
532 -- Process_Binder_File --
533 -------------------------
535 procedure Process_Binder_File (Name : in String) is
537 Link_Bytes : Integer := 0;
539 pragma Import (C, Link_Max, "link_max");
541 Next_Line : String (1 .. 1000);
544 Objs_Begin : Integer := 0;
545 Objs_End : Integer := 0;
550 GNAT_Static : Boolean := False;
551 -- Save state of -static option.
553 GNAT_Shared : Boolean := False;
554 -- Save state of -shared option.
556 Run_Path_Option_Ptr : Address;
557 pragma Import (C, Run_Path_Option_Ptr, "run_path_option");
558 -- Pointer to string representing the native linker option which
559 -- specifies the path where the dynamic loader should find shared
560 -- libraries. Equal to null string if this system doesn't support it.
562 Object_Library_Ext_Ptr : Address;
563 pragma Import (C, Object_Library_Ext_Ptr, "object_library_extension");
564 -- Pointer to string specifying the default extension for
565 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
567 Object_File_Option_Ptr : Address;
568 pragma Import (C, Object_File_Option_Ptr, "object_file_option");
569 -- Pointer to a string representing the linker option which specifies
570 -- the response file.
572 Using_GNU_Linker : Boolean;
573 pragma Import (C, Using_GNU_Linker, "using_gnu_linker");
574 -- Predicate indicating whether this target uses the GNU linker. In
575 -- this case we must output a GNU linker compatible response file.
577 procedure Get_Next_Line;
578 -- Read the next line from the binder file without the line
581 function Is_Option_Present (Opt : in String) return Boolean;
582 -- Return true if the option Opt is already present in
583 -- Linker_Options table.
585 procedure Get_Next_Line is
589 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
591 if Fchars = System.Null_Address then
592 Exit_With_Error ("Error reading binder output");
595 Nfirst := Next_Line'First;
597 while Nlast <= Next_Line'Last
598 and then Next_Line (Nlast) /= ASCII.LF
599 and then Next_Line (Nlast) /= ASCII.CR
607 function Is_Option_Present (Opt : in String) return Boolean is
609 for I in 1 .. Linker_Options.Last loop
611 if Linker_Options.Table (I).all = Opt then
618 end Is_Option_Present;
620 -- Start of processing for Process_Binder_File
623 Fd := fopen (Name'Address, Read_Mode'Address);
625 if Fd = NULL_Stream then
626 Exit_With_Error ("Failed to open binder output");
629 -- Skip up to the Begin Info line
633 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
639 -- Go to end when end line is reached (this will happen in
640 -- No_Run_Time mode where no -L switches are generated)
642 exit when Next_Line (Nfirst .. Nlast) = End_Info;
644 if Ada_Bind_File then
645 Next_Line (Nfirst .. Nlast - 8) :=
646 Next_Line (Nfirst + 8 .. Nlast);
650 -- Go to next section when switches are reached
652 exit when Next_Line (1) = '-';
654 -- Otherwise we have another object file to collect
656 Linker_Objects.Increment_Last;
658 -- Mark the positions of first and last object files in case
659 -- they need to be placed with a named file on systems having
660 -- linker line limitations.
662 if Objs_Begin = 0 then
663 Objs_Begin := Linker_Objects.Last;
666 Linker_Objects.Table (Linker_Objects.Last) :=
667 new String'(Next_Line
(Nfirst
.. Nlast
));
669 Link_Bytes
:= Link_Bytes
+ Nlast
- Nfirst
;
672 Objs_End
:= Linker_Objects
.Last
;
674 -- On systems that have limitations on handling very long linker lines
675 -- we make use of the system linker option which takes a list of object
676 -- file names from a file instead of the command line itself. What we do
677 -- is to replace the list of object files by the special linker option
678 -- which then reads the object file list from a file instead. The option
679 -- to read from a file instead of the command line is only triggered if
680 -- a conservative threshold is passed.
682 if Object_List_File_Required
683 or else (Object_List_File_Supported
684 and then Link_Bytes
> Link_Max
)
686 -- Create a temporary file containing the Ada user object files
687 -- needed by the link. This list is taken from the bind file
688 -- and is output one object per line for maximal compatibility with
689 -- linkers supporting this option.
691 Create_Temp_File
(Tname_FD
, Tname
);
693 -- If target is using the GNU linker we must add a special header
694 -- and footer in the response file.
695 -- The syntax is : INPUT (object1.o object2.o ... )
697 if Using_GNU_Linker
then
699 GNU_Header
: aliased constant String := "INPUT (";
702 Status
:= Write
(Tname_FD
, GNU_Header
'Address,
707 for J
in Objs_Begin
.. Objs_End
loop
708 Status
:= Write
(Tname_FD
, Linker_Objects
.Table
(J
).all'Address,
709 Linker_Objects
.Table
(J
).all'Length);
710 Status
:= Write
(Tname_FD
, ASCII
.LF
'Address, 1);
712 Response_File_Objects
.Increment_Last
;
713 Response_File_Objects
.Table
(Response_File_Objects
.Last
) :=
714 Linker_Objects
.Table
(J
);
717 -- handle GNU linker response file footer.
719 if Using_GNU_Linker
then
721 GNU_Footer
: aliased constant String := ")";
724 Status
:= Write
(Tname_FD
, GNU_Footer
'Address,
731 -- Add the special objects list file option together with the name
732 -- of the temporary file (removing the null character) to the objects
735 Linker_Objects
.Table
(Objs_Begin
) :=
736 new String'(Value (Object_File_Option_Ptr) &
737 Tname (Tname'First .. Tname'Last - 1));
739 -- The slots containing these object file names are then removed
740 -- from the objects table so they do not appear in the link. They
741 -- are removed by moving up the linker options and non-Ada object
742 -- files appearing after the Ada object list in the table.
744 N := Objs_End - Objs_Begin + 1;
745 for J in Objs_End + 1 .. Linker_Objects.Last loop
746 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
749 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
752 -- Process switches and options
754 if Next_Line (Nfirst .. Nlast) /= End_Info then
756 if Next_Line (Nfirst .. Nlast) = "-static" then
759 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
762 -- Add binder options only if not already set on the command
763 -- line. This rule is a way to control the linker options order.
765 elsif not Is_Option_Present (Next_Line (Nfirst .. Nlast)) then
766 if Nlast > Nfirst + 2 and then
767 Next_Line (Nfirst .. Nfirst + 1) = "-L"
769 -- Construct a library search path for use later
770 -- to locate static gnatlib libraries.
772 if Libpath.Last > 1 then
773 Libpath.Increment_Last;
774 Libpath.Table (Libpath.Last) := Path_Separator;
777 for I in Nfirst + 2 .. Nlast loop
778 Libpath.Increment_Last;
779 Libpath.Table (Libpath.Last) := Next_Line (I);
782 Linker_Options.Increment_Last;
784 Linker_Options.Table (Linker_Options.Last) :=
785 new String'(Next_Line
(Nfirst
.. Nlast
));
787 elsif Next_Line
(Nfirst
.. Nlast
) = "-ldecgnat"
788 or else Next_Line
(Nfirst
.. Nlast
) = "-lgnarl"
789 or else Next_Line
(Nfirst
.. Nlast
) = "-lgnat"
791 -- Given a Gnat standard library, search the
792 -- library path to find the library location
795 File_Path
: String_Access
;
796 Object_Lib_Extension
: constant String :=
797 Value
(Object_Library_Ext_Ptr
);
799 File_Name
: String := "lib" &
800 Next_Line
(Nfirst
+ 2 .. Nlast
) & Object_Lib_Extension
;
804 Locate_Regular_File
(File_Name
,
805 String (Libpath
.Table
(1 .. Libpath
.Last
)));
807 if File_Path
/= null then
810 -- If static gnatlib found, explicitly
811 -- specify to overcome possible linker
812 -- default usage of shared version.
814 Linker_Options
.Increment_Last
;
816 Linker_Options
.Table
(Linker_Options
.Last
) :=
817 new String'(File_Path.all);
819 elsif GNAT_Shared then
821 -- If shared gnatlib desired, add the
822 -- appropriate system specific switch
823 -- so that it can be located at runtime.
826 Run_Path_Opt : constant String :=
827 Value (Run_Path_Option_Ptr);
830 if Run_Path_Opt'Length /= 0 then
832 -- Output the system specific linker
833 -- command that allows the image
834 -- activator to find the shared library
837 Linker_Options.Increment_Last;
839 Linker_Options.Table (Linker_Options.Last)
840 := new String'(Run_Path_Opt
842 (1 .. File_Path
'Length
843 - File_Name
'Length));
846 Linker_Options
.Increment_Last
;
847 Linker_Options
.Table
(Linker_Options
.Last
)
848 := new String'(Next_Line (Nfirst .. Nlast));
853 -- If gnatlib library not found, then
854 -- add it anyway in case some other
855 -- mechanimsm may find it.
857 Linker_Options.Increment_Last;
858 Linker_Options.Table (Linker_Options.Last)
859 := new String'(Next_Line
(Nfirst
.. Nlast
));
863 Linker_Options
.Increment_Last
;
864 Linker_Options
.Table
(Linker_Options
.Last
)
865 := new String'(Next_Line (Nfirst .. Nlast));
870 exit when Next_Line (Nfirst .. Nlast) = End_Info;
872 if Ada_Bind_File then
873 Next_Line (Nfirst .. Nlast - 8)
874 := Next_Line (Nfirst + 8 .. Nlast);
880 Status := fclose (Fd);
881 end Process_Binder_File;
887 function Value (chars : chars_ptr) return String is
888 function Strlen (chars : chars_ptr) return Natural;
889 pragma Import (C, Strlen);
892 if chars = Null_Address then
897 subtype Result_Type is String (1 .. Strlen (chars));
899 Result : Result_Type;
900 for Result'Address use chars;
912 procedure Write_Usage is
914 Write_Str ("Usage: ");
915 Write_Str (Base_Name (Command_Name));
916 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
919 Write_Line (" mainprog.ali the ALI file of the main program");
921 Write_Line (" -A Binder generated source file is in Ada (default)");
922 Write_Line (" -C Binder generated source file is in C");
923 Write_Line (" -f force object file list to be generated");
924 Write_Line (" -g Compile binder source file with debug information");
925 Write_Line (" -n Do not compile the binder source file");
926 Write_Line (" -v verbose mode");
927 Write_Line (" -v -v very verbose mode");
929 Write_Line (" -o nam Use 'nam
' as the name of the executable");
930 Write_Line (" -b target Compile the binder source to run on target");
931 Write_Line (" -Bdir Load compiler executables from dir");
932 Write_Line (" --GCC=comp Use comp as the compiler");
933 Write_Line (" --LINK=nam Use 'nam
' for the linking rather than 'gcc
'");
935 Write_Line (" [non-Ada-objects] list of non Ada object files");
936 Write_Line (" [linker-options] other options for the linker");
939 -- Start of processing for Gnatlink
942 if Argument_Count = 0 then
944 Exit_Program (E_Fatal);
947 if Hostparm.Java_VM then
948 Gcc := new String'("jgnat");
949 Ada_Bind_File
:= True;
950 Begin_Info
:= "-- BEGIN Object file/option list";
951 End_Info
:= "-- END Object file/option list ";
956 -- We always compile with -c
958 Binder_Options_From_ALI
.Increment_Last
;
959 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
962 -- If the main program is in Ada it is compiled with the following
965 -- -gnatA stops reading gnat.adc, since we don't know what
966 -- pagmas would work, and we do not need it anyway.
968 -- -gnatWb allows brackets coding for wide characters
970 -- -gnatiw allows wide characters in identifiers. This is needed
971 -- because bindgen uses brackets encoding for all upper
972 -- half and wide characters in identifier names.
974 if Ada_Bind_File then
975 Binder_Options_From_ALI.Increment_Last;
976 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
977 new String'("-gnatA");
978 Binder_Options_From_ALI
.Increment_Last
;
979 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
) :=
980 new String'("-gnatWb");
981 Binder_Options_From_ALI.Increment_Last;
982 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
983 new String'("-gnatiw");
986 -- Locate all the necessary programs and verify required files are present
988 Gcc_Path
:= GNAT
.OS_Lib
.Locate_Exec_On_Path
(Gcc
.all);
990 if Gcc_Path
= null then
991 Exit_With_Error
("Couldn't locate " & Gcc
.all);
994 if Linker_Path
= null then
995 Linker_Path
:= Gcc_Path
;
998 if Ali_File_Name
= null then
999 Exit_With_Error
("Required 'name'.ali not present.");
1002 if not Is_Regular_File
(Ali_File_Name
.all) then
1003 Exit_With_Error
(Ali_File_Name
.all & " not found.");
1005 -- Read the ALI file of the main subprogram if the binder generated
1006 -- file is in Ada, it need to be compiled and no --GCC= switch has
1007 -- been specified. Fetch the back end switches from this ALI file and use
1008 -- these switches to compile the binder generated file
1011 and then Compile_Bind_File
1012 and then Standard_Gcc
1014 -- Do some initializations
1018 Name_Len
:= Ali_File_Name
'Length;
1019 Name_Buffer
(1 .. Name_Len
) := Ali_File_Name
.all;
1023 F
: constant File_Name_Type
:= Name_Find
;
1024 T
: Text_Buffer_Ptr
;
1028 -- Osint.Add_Default_Search_Dirs;
1029 -- Load the ALI file
1031 T
:= Read_Library_Info
(F
, True);
1035 A
:= Scan_ALI
(F
, T
, False, False, False);
1037 if A
/= No_ALI_Id
then
1039 Index
in Units
.Table
(ALIs
.Table
(A
).First_Unit
).First_Arg
1040 .. Units
.Table
(ALIs
.Table
(A
).First_Unit
).Last_Arg
1042 -- Do not compile with the front end switches
1044 if not Is_Front_End_Switch
(Args
.Table
(Index
).all) then
1045 Binder_Options_From_ALI
.Increment_Last
;
1046 Binder_Options_From_ALI
.Table
(Binder_Options_From_ALI
.Last
)
1047 := String_Access
(Args
.Table
(Index
));
1054 if Verbose_Mode
then
1056 Write_Str
("GNATLINK ");
1057 Write_Str
(Gnat_Version_String
);
1058 Write_Str
(" Copyright 1996-2002 Free Software Foundation, Inc.");
1062 -- If there wasn't an output specified, then use the base name of
1063 -- the .ali file name.
1065 if Output_File_Name
= null then
1068 new String'(Base_Name (Ali_File_Name.all)
1069 & Get_Debuggable_Suffix.all);
1071 Linker_Options.Increment_Last;
1072 Linker_Options.Table (Linker_Options.Last) :=
1075 Linker_Options
.Increment_Last
;
1076 Linker_Options
.Table
(Linker_Options
.Last
) :=
1077 new String'(Output_File_Name.all);
1081 -- Warn if main program is called "test", as that may be a built-in command
1082 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1083 -- will not appear. However, do not warn in the case of a cross compiler.
1085 -- Assume that if the executable name is not gnatlink, this is a cross
1088 if Base_Name (Command_Name) = "gnatlink"
1089 and then Output_File_Name.all = "test"
1091 Error_Msg ("warning: executable name """ & Output_File_Name.all
1092 & """ may conflict with shell command");
1095 -- Perform consistency checks
1097 -- Transform the .ali file name into the binder output file name.
1099 Make_Binder_File_Names : declare
1100 Fname : String := Base_Name (Ali_File_Name.all);
1101 Fname_Len : Integer := Fname'Length;
1103 function Get_Maximum_File_Name_Length return Integer;
1104 pragma Import (C, Get_Maximum_File_Name_Length,
1105 "__gnat_get_maximum_file_name_length");
1107 Maximum_File_Name_Length : Integer := Get_Maximum_File_Name_Length;
1109 Second_Char : Character;
1110 -- Second character of name of files
1113 -- Set proper second character of file name
1115 if not Ada_Bind_File then
1118 elsif Hostparm.OpenVMS then
1125 -- If the length of the binder file becomes too long due to
1126 -- the addition of the "b?" prefix, then truncate it.
1128 if Maximum_File_Name_Length > 0 then
1129 while Fname_Len > Maximum_File_Name_Length - 2 loop
1130 Fname_Len := Fname_Len - 1;
1134 if Ada_Bind_File then
1135 Binder_Spec_Src_File :=
1138 & Fname
(Fname
'First .. Fname
'First + Fname_Len
- 1)
1140 Binder_Body_Src_File
:=
1143 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1148 & Fname
(Fname
'First .. Fname
'First + Fname_Len
- 1)
1152 Binder_Body_Src_File
:=
1155 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1162 & Fname
(Fname
'First .. Fname
'First + Fname_Len
- 1)
1163 & Get_Object_Suffix
.all);
1165 if Fname_Len
/= Fname
'Length then
1166 Binder_Options
.Increment_Last
;
1167 Binder_Options
.Table
(Binder_Options
.Last
) := new String'("-o");
1168 Binder_Options.Increment_Last;
1169 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1172 end Make_Binder_File_Names;
1174 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1176 -- Compile the binder file. This is fast, so we always do it, unless
1177 -- specifically told not to by the -n switch
1179 if Compile_Bind_File then
1182 Args : Argument_List
1183 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1186 for J in 1 .. Binder_Options_From_ALI.Last loop
1187 Args (J) := Binder_Options_From_ALI.Table (J);
1190 for J in 1 .. Binder_Options.Last loop
1191 Args (Binder_Options_From_ALI.Last + J) :=
1192 Binder_Options.Table (J);
1195 Args (Args'Last) := Binder_Body_Src_File;
1197 if Verbose_Mode then
1198 Write_Str (Base_Name (Gcc_Path.all));
1200 for J in Args'Range loop
1202 Write_Str (Args (J).all);
1208 GNAT.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1211 Exit_Program (E_Fatal);
1216 -- Now, actually link the program.
1218 -- Skip this step for now on the JVM since the Java interpreter will do
1219 -- the actual link at run time. We might consider packing all class files
1220 -- in a .zip file during this step.
1222 if not Hostparm.Java_VM then
1224 Num_Args : Natural :=
1225 (Linker_Options.Last - Linker_Options.First + 1) +
1226 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1227 (Linker_Objects.Last - Linker_Objects.First + 1);
1228 Stack_Op : Boolean := False;
1229 IDENT_Op : Boolean := False;
1232 -- Remove duplicate stack size setting from the Linker_Options
1233 -- table. The stack setting option "-Xlinker --stack=R,C" can be
1234 -- found in one line when set by a pragma Linker_Options or in two
1235 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
1236 -- line. We also check for the "-Wl,--stack=R" style option.
1238 -- We must remove the second stack setting option instance
1239 -- because the one on the command line will always be the first
1240 -- one. And any subsequent stack setting option will overwrite the
1241 -- previous one. This is done especially for GNAT/NT where we set
1242 -- the stack size for tasking programs by a pragma in the NT
1243 -- specific tasking package System.Task_Primitives.Oparations.
1245 for J in Linker_Options.First .. Linker_Options.Last loop
1246 if Linker_Options.Table (J).all = "-Xlinker"
1247 and then J < Linker_Options.Last
1248 and then Linker_Options.Table (J + 1)'Length > 8
1249 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
1252 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
1253 Linker_Options.Table (J + 2 .. Linker_Options.Last);
1254 Linker_Options.Decrement_Last;
1255 Linker_Options.Decrement_Last;
1256 Num_Args := Num_Args - 2;
1263 -- Here we just check for a canonical form that matches the
1264 -- pragma Linker_Options set in the NT runtime.
1266 if (Linker_Options.Table (J)'Length > 17
1267 and then Linker_Options.Table (J) (1 .. 17)
1268 = "-Xlinker --stack=")
1270 (Linker_Options.Table (J)'Length > 12
1271 and then Linker_Options.Table (J) (1 .. 12)
1275 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1276 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1277 Linker_Options.Decrement_Last;
1278 Num_Args := Num_Args - 1;
1285 -- Remove duplicate IDENTIFICATION directives (VMS)
1287 if Linker_Options.Table (J)'Length > 27
1288 and then Linker_Options.Table (J) (1 .. 27)
1289 = "--for-linker=IDENTIFICATION="
1292 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1293 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1294 Linker_Options.Decrement_Last;
1295 Num_Args := Num_Args - 1;
1302 -- Prepare arguments for call to linker
1304 Call_Linker : declare
1306 Args : Argument_List (1 .. Num_Args + 1);
1307 Index : Integer := Args'First;
1310 Args (Index) := Binder_Obj_File;
1312 -- Add the object files and any -largs libraries
1314 for J in Linker_Objects.First .. Linker_Objects.Last loop
1316 Args (Index) := Linker_Objects.Table (J);
1319 -- Add the linker options from the binder file
1321 for J in Linker_Options.First .. Linker_Options.Last loop
1323 Args (Index) := Linker_Options.Table (J);
1326 -- Finally add the libraries from the --GCC= switch
1328 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
1330 Args (Index) := Gcc_Linker_Options.Table (J);
1333 if Verbose_Mode then
1334 Write_Str (Linker_Path.all);
1336 for J in Args'Range loop
1338 Write_Str (Args (J).all);
1343 -- If we are on very verbose mode (-v -v) and a response file
1344 -- is used we display its content.
1346 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
1348 Write_Str ("Response file (" &
1349 Tname (Tname'First .. Tname'Last - 1) &
1354 Response_File_Objects.First ..
1355 Response_File_Objects.Last
1357 Write_Str (Response_File_Objects.Table (J).all);
1365 GNAT.OS_Lib.Spawn (Linker_Path.all, Args, Success);
1367 -- Delete the temporary file used in conjuction with linking if
1368 -- one was created. See Process_Bind_File for details.
1370 if Tname_FD /= Invalid_FD then
1375 Error_Msg ("cannot call " & Linker_Path.all);
1376 Exit_Program (E_Fatal);
1382 -- Only keep the binder output file and it's associated object
1383 -- file if compiling with the -g option. These files are only
1384 -- useful if debugging.
1386 if not Debug_Flag_Present then
1387 if Binder_Ali_File /= null then
1388 Delete (Binder_Ali_File.all & ASCII.NUL);
1391 if Binder_Spec_Src_File /= null then
1392 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
1395 Delete (Binder_Body_Src_File.all & ASCII.NUL);
1397 if not Hostparm.Java_VM then
1398 Delete (Binder_Obj_File.all & ASCII.NUL);
1402 Exit_Program (E_Success);
1406 Write_Line (Exception_Information (X));
1407 Exit_With_Error ("INTERNAL ERROR. Please report.");