1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1996-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 -- Gnatlink usage: please consult the gnat documentation
31 with Gnatvsn; use Gnatvsn;
33 with Osint; use Osint;
34 with Output; use Output;
35 with System; use System;
38 with Ada.Command_Line; use Ada.Command_Line;
39 with GNAT.OS_Lib; use GNAT.OS_Lib;
40 with Interfaces.C_Streams; use Interfaces.C_Streams;
44 pragma Ident (Gnat_Version_String);
46 package Gcc_Linker_Options is new Table.Table (
47 Table_Component_Type => String_Access,
48 Table_Index_Type => Integer,
51 Table_Increment => 100,
52 Table_Name => "Gnatlink.Gcc_Linker_Options");
53 -- Comments needed ???
55 package Libpath is new Table.Table (
56 Table_Component_Type => Character,
57 Table_Index_Type => Integer,
59 Table_Initial => 4096,
61 Table_Name => "Gnatlink.Libpath");
62 -- Comments needed ???
64 package Linker_Options is new Table.Table (
65 Table_Component_Type => String_Access,
66 Table_Index_Type => Integer,
69 Table_Increment => 100,
70 Table_Name => "Gnatlink.Linker_Options");
71 -- Comments needed ???
73 package Linker_Objects is new Table.Table (
74 Table_Component_Type => String_Access,
75 Table_Index_Type => Integer,
78 Table_Increment => 100,
79 Table_Name => "Gnatlink.Linker_Objects");
80 -- This table collects the objects file to be passed to the linker. In the
81 -- case where the linker command line is too long then programs objects
82 -- are put on the Response_File_Objects table. Note that the binder object
83 -- file and the user's objects remain in this table. This is very
84 -- important because on the GNU linker command line the -L switch is not
85 -- used to look for objects files but -L switch is used to look for
86 -- objects listed in the response file. This is not a problem with the
87 -- applications objects as they are specified with a fullname.
89 package Response_File_Objects is new Table.Table (
90 Table_Component_Type => String_Access,
91 Table_Index_Type => Integer,
94 Table_Increment => 100,
95 Table_Name => "Gnatlink.Response_File_Objects");
96 -- This table collects the objects file that are to be put in the response
97 -- file. Only application objects are collected there (see details in
98 -- Linker_Objects table comments)
100 package Binder_Options is new Table.Table (
101 Table_Component_Type => String_Access,
102 Table_Index_Type => Integer,
103 Table_Low_Bound => 1, -- equals low bound of Argument_List for Spawn
105 Table_Increment => 100,
106 Table_Name => "Gnatlink.Binder_Options");
107 -- This table collects the arguments to be passed to compile the binder
110 subtype chars_ptr is System.Address;
112 Gcc : String_Access := Program_Name ("gcc");
114 Read_Mode : constant String := "r" & ASCII.Nul;
116 Begin_Info : String := "-- BEGIN Object file/option list";
117 End_Info : String := "-- END Object file/option list ";
118 -- Note: above lines are modified in C mode, see option processing
120 Gcc_Path : String_Access;
121 Linker_Path : String_Access;
123 Output_File_Name : String_Access;
124 Ali_File_Name : String_Access;
125 Binder_Spec_Src_File : String_Access;
126 Binder_Body_Src_File : String_Access;
127 Binder_Ali_File : String_Access;
128 Binder_Obj_File : String_Access;
130 Tname : Temp_File_Name;
131 Tname_FD : File_Descriptor := Invalid_FD;
132 -- Temporary file used by linker to pass list of object files on
133 -- certain systems with limitations on size of arguments.
135 Debug_Flag_Present : Boolean := False;
136 Verbose_Mode : Boolean := False;
137 Very_Verbose_Mode : Boolean := False;
139 Ada_Bind_File : Boolean := True;
140 -- Set to True if bind file is generated in Ada
142 Compile_Bind_File : Boolean := True;
143 -- Set to False if bind file is not to be compiled
145 Object_List_File_Supported : Boolean;
146 pragma Import (C, Object_List_File_Supported, "objlist_file_supported");
147 -- Predicate indicating whether the linker has an option whereby the
148 -- names of object files can be passed to the linker in a file.
150 Object_List_File_Required : Boolean := False;
151 -- Set to True to force generation of a response file
153 function Base_Name (File_Name : in String) return String;
154 -- Return just the file name part without the extension (if present).
156 procedure Delete (Name : in String);
157 -- Wrapper to unlink as status is ignored by this application.
159 procedure Error_Msg (Message : in String);
160 -- Output the error or warning Message
162 procedure Exit_With_Error (Error : in String);
163 -- Output Error and exit program with a fatal condition.
165 procedure Process_Args;
166 -- Go through all the arguments and build option tables.
168 procedure Process_Binder_File (Name : in String);
169 -- Reads the binder file and extracts linker arguments.
171 function Value (chars : chars_ptr) return String;
172 -- Return NUL-terminated string chars as an Ada string.
174 procedure Write_Usage;
175 -- Show user the program options.
181 function Base_Name (File_Name : in String) return String is
186 Findex1 := File_Name'First;
188 -- The file might be specified by a full path name. However,
189 -- we want the path to be stripped away.
191 for J in reverse File_Name'Range loop
192 if Is_Directory_Separator (File_Name (J)) then
198 Findex2 := File_Name'Last;
199 while Findex2 > Findex1
200 and then File_Name (Findex2) /= '.'
202 Findex2 := Findex2 - 1;
205 if Findex2 = Findex1 then
206 Findex2 := File_Name'Last + 1;
209 return File_Name (Findex1 .. Findex2 - 1);
216 procedure Delete (Name : in String) is
220 Status := unlink (Name'Address);
227 procedure Error_Msg (Message : in String) is
229 Write_Str (Base_Name (Command_Name));
235 ---------------------
236 -- Exit_With_Error --
237 ---------------------
239 procedure Exit_With_Error (Error : in String) is
242 Exit_Program (E_Fatal);
249 procedure Process_Args is
253 Binder_Options.Increment_Last;
254 Binder_Options.Table (Binder_Options.Last) := new String'("-c");
256 -- If the main program is in Ada it is compiled with the following
259 -- -gnatA stops reading gnat.adc, since we don't know what
260 -- pagmas would work, and we do not need it anyway.
262 -- -gnatWb allows brackets coding for wide characters
264 -- -gnatiw allows wide characters in identifiers. This is needed
265 -- because bindgen uses brackets encoding for all upper
266 -- half and wide characters in identifier names.
268 if Ada_Bind_File then
269 Binder_Options.Increment_Last;
270 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatA");
271 Binder_Options.Increment_Last;
272 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatWb");
273 Binder_Options.Increment_Last;
274 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatiw");
277 -- Loop through arguments of gnatlink command
281 exit when Next_Arg > Argument_Count;
283 Process_One_Arg : declare
284 Arg : String := Argument (Next_Arg);
287 -- Case of argument which is a switch
289 -- We definitely need section by section comments here ???
292 and then (Arg (1) = Switch_Character or else Arg (1) = '-')
295 and then Arg (2 .. 5) = "gnat"
298 ("invalid switch: """ & Arg & """ (gnat not needed here)");
302 and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
304 Debug_Flag_Present := True;
306 Linker_Options.Increment_Last;
307 Linker_Options.Table (Linker_Options.Last) :=
310 Binder_Options.Increment_Last;
311 Binder_Options.Table (Binder_Options.Last) :=
312 Linker_Options.Table (Linker_Options.Last);
314 elsif Arg'Length = 2 then
317 Ada_Bind_File := True;
318 Begin_Info := "-- BEGIN Object file/option list";
319 End_Info := "-- END Object file/option list ";
322 Linker_Options.Increment_Last;
323 Linker_Options.Table (Linker_Options.Last) :=
326 Binder_Options.Increment_Last;
327 Binder_Options.Table (Binder_Options.Last) :=
328 Linker_Options.Table (Linker_Options.Last);
330 Next_Arg := Next_Arg + 1;
332 if Next_Arg > Argument_Count then
333 Exit_With_Error ("Missing argument for -b");
336 Get_Machine_Name : declare
337 Name_Arg : String_Access :=
338 new String'(Argument (Next_Arg));
341 Linker_Options.Increment_Last;
342 Linker_Options.Table (Linker_Options.Last) :=
345 Binder_Options.Increment_Last;
346 Binder_Options.Table (Binder_Options.Last) :=
349 end Get_Machine_Name;
352 Ada_Bind_File := False;
353 Begin_Info := "/* BEGIN Object file/option list";
354 End_Info := " END Object file/option list */";
357 if Object_List_File_Supported then
358 Object_List_File_Required := True;
361 ("Object list file not supported on this target");
365 Compile_Bind_File := False;
368 Linker_Options.Increment_Last;
369 Linker_Options.Table (Linker_Options.Last) :=
372 Next_Arg := Next_Arg + 1;
374 if Next_Arg > Argument_Count then
375 Exit_With_Error ("Missing argument for -o");
378 Output_File_Name := new String'(Argument (Next_Arg));
380 Linker_Options.Increment_Last;
381 Linker_Options.Table (Linker_Options.Last) :=
386 -- Support "double" verbose mode. Second -v
387 -- gets sent to the linker and binder phases.
390 Very_Verbose_Mode := True;
392 Linker_Options.Increment_Last;
393 Linker_Options.Table (Linker_Options.Last) :=
396 Binder_Options.Increment_Last;
397 Binder_Options.Table (Binder_Options.Last) :=
398 Linker_Options.Table (Linker_Options.Last);
401 Verbose_Mode := True;
406 Linker_Options.Increment_Last;
407 Linker_Options.Table (Linker_Options.Last) :=
412 elsif Arg (2) = 'B' then
413 Linker_Options.Increment_Last;
414 Linker_Options.Table (Linker_Options.Last) :=
417 Binder_Options.Increment_Last;
418 Binder_Options.Table (Binder_Options.Last) :=
419 Linker_Options.Table (Linker_Options.Last);
421 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
423 if Arg'Length = 7 then
424 Exit_With_Error ("Missing argument for --LINK=");
428 GNAT.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
430 if Linker_Path = null then
432 ("Could not locate linker: " & Arg (8 .. Arg'Last));
435 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
437 Program_Args : Argument_List_Access :=
438 Argument_String_To_List
439 (Arg (7 .. Arg'Last));
442 Gcc := new String'(Program_Args.all (1).all);
444 -- Set appropriate flags for switches passed
446 for J in 2 .. Program_Args.all'Last loop
448 Arg : String := Program_Args.all (J).all;
449 AF : Integer := Arg'First;
453 and then (Arg (AF) = Switch_Character
454 or else Arg (AF) = '-')
456 if Arg (AF + 1) = 'g'
457 and then (Arg'Length = 2
458 or else Arg (AF + 2) in '0' .. '3'
459 or else Arg (AF + 2 .. Arg'Last) = "coff")
461 Debug_Flag_Present := True;
465 -- Pass to gcc for compiling binder generated file
466 -- No use passing libraries, it will just generate
469 if not (Arg (AF .. AF + 1) = "-l"
470 or else Arg (AF .. AF + 1) = "-L")
472 Binder_Options.Increment_Last;
473 Binder_Options.Table (Binder_Options.Last) :=
477 -- Pass to gcc for linking program.
479 Gcc_Linker_Options.Increment_Last;
480 Gcc_Linker_Options.Table
481 (Gcc_Linker_Options.Last) := new String'(Arg);
486 -- Send all multi-character switches not recognized as
487 -- a special case by gnatlink to the linker/loader stage.
490 Linker_Options.Increment_Last;
491 Linker_Options.Table (Linker_Options.Last) :=
495 -- Here if argument is a file name rather than a switch
499 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
501 if Ali_File_Name = null then
502 Ali_File_Name := new String'(Arg);
504 Exit_With_Error ("cannot handle more than one ALI file");
507 elsif Is_Regular_File (Arg & ".ali")
508 and then Ali_File_Name = null
510 Ali_File_Name := new String'(Arg & ".ali");
512 elsif Arg'Length > Get_Object_Suffix.all'Length
514 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
515 = Get_Object_Suffix.all
517 Linker_Objects.Increment_Last;
518 Linker_Objects.Table (Linker_Objects.Last) :=
522 Linker_Options.Increment_Last;
523 Linker_Options.Table (Linker_Options.Last) :=
531 Next_Arg := Next_Arg + 1;
534 -- If Ada bind file, then compile it with warnings suppressed, because
535 -- otherwise the with of the main program may cause junk warnings.
537 if Ada_Bind_File then
538 Binder_Options.Increment_Last;
539 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
543 -------------------------
544 -- Process_Binder_File --
545 -------------------------
547 procedure Process_Binder_File (Name : in String) is
549 Link_Bytes : Integer := 0;
551 pragma Import (C, Link_Max, "link_max");
553 Next_Line : String (1 .. 1000);
556 Objs_Begin : Integer := 0;
557 Objs_End : Integer := 0;
562 GNAT_Static : Boolean := False;
563 -- Save state of -static option.
565 GNAT_Shared : Boolean := False;
566 -- Save state of -shared option.
568 Run_Path_Option_Ptr : Address;
569 pragma Import (C, Run_Path_Option_Ptr, "run_path_option");
570 -- Pointer to string representing the native linker option which
571 -- specifies the path where the dynamic loader should find shared
572 -- libraries. Equal to null string if this system doesn't support it.
574 Object_Library_Ext_Ptr : Address;
575 pragma Import (C, Object_Library_Ext_Ptr, "object_library_extension");
576 -- Pointer to string specifying the default extension for
577 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
579 Object_File_Option_Ptr : Address;
580 pragma Import (C, Object_File_Option_Ptr, "object_file_option");
581 -- Pointer to a string representing the linker option which specifies
582 -- the response file.
584 Using_GNU_Linker : Boolean;
585 pragma Import (C, Using_GNU_Linker, "using_gnu_linker");
586 -- Predicate indicating whether this target uses the GNU linker. In
587 -- this case we must output a GNU linker compatible response file.
589 procedure Get_Next_Line;
590 -- Read the next line from the binder file without the line
593 function Is_Option_Present (Opt : in String) return Boolean;
594 -- Return true if the option Opt is already present in
595 -- Linker_Options table.
597 procedure Get_Next_Line is
601 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
603 if Fchars = System.Null_Address then
604 Exit_With_Error ("Error reading binder output");
607 Nfirst := Next_Line'First;
609 while Nlast <= Next_Line'Last
610 and then Next_Line (Nlast) /= ASCII.LF
611 and then Next_Line (Nlast) /= ASCII.CR
619 function Is_Option_Present (Opt : in String) return Boolean is
621 for I in 1 .. Linker_Options.Last loop
623 if Linker_Options.Table (I).all = Opt then
630 end Is_Option_Present;
632 -- Start of processing for Process_Binder_File
635 Fd := fopen (Name'Address, Read_Mode'Address);
637 if Fd = NULL_Stream then
638 Exit_With_Error ("Failed to open binder output");
641 -- Skip up to the Begin Info line
645 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
651 -- Go to end when end line is reached (this will happen in
652 -- No_Run_Time mode where no -L switches are generated)
654 exit when Next_Line (Nfirst .. Nlast) = End_Info;
656 if Ada_Bind_File then
657 Next_Line (Nfirst .. Nlast - 8) :=
658 Next_Line (Nfirst + 8 .. Nlast);
662 -- Go to next section when switches are reached
664 exit when Next_Line (1) = '-';
666 -- Otherwise we have another object file to collect
668 Linker_Objects.Increment_Last;
670 -- Mark the positions of first and last object files in case
671 -- they need to be placed with a named file on systems having
672 -- linker line limitations.
674 if Objs_Begin = 0 then
675 Objs_Begin := Linker_Objects.Last;
678 Linker_Objects.Table (Linker_Objects.Last) :=
679 new String'(Next_Line (Nfirst .. Nlast));
681 Link_Bytes := Link_Bytes + Nlast - Nfirst;
684 Objs_End := Linker_Objects.Last;
686 -- On systems that have limitations on handling very long linker lines
687 -- we make use of the system linker option which takes a list of object
688 -- file names from a file instead of the command line itself. What we do
689 -- is to replace the list of object files by the special linker option
690 -- which then reads the object file list from a file instead. The option
691 -- to read from a file instead of the command line is only triggered if
692 -- a conservative threshold is passed.
694 if Object_List_File_Required
695 or else (Object_List_File_Supported
696 and then Link_Bytes > Link_Max)
698 -- Create a temporary file containing the Ada user object files
699 -- needed by the link. This list is taken from the bind file
700 -- and is output one object per line for maximal compatibility with
701 -- linkers supporting this option.
703 Create_Temp_File (Tname_FD, Tname);
705 -- If target is using the GNU linker we must add a special header
706 -- and footer in the response file.
707 -- The syntax is : INPUT (object1.o object2.o ... )
709 if Using_GNU_Linker then
711 GNU_Header : aliased constant String := "INPUT (";
714 Status := Write (Tname_FD, GNU_Header'Address,
719 for J in Objs_Begin .. Objs_End loop
720 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
721 Linker_Objects.Table (J).all'Length);
722 Status := Write (Tname_FD, ASCII.LF'Address, 1);
724 Response_File_Objects.Increment_Last;
725 Response_File_Objects.Table (Response_File_Objects.Last) :=
726 Linker_Objects.Table (J);
729 -- handle GNU linker response file footer.
731 if Using_GNU_Linker then
733 GNU_Footer : aliased constant String := ")";
736 Status := Write (Tname_FD, GNU_Footer'Address,
743 -- Add the special objects list file option together with the name
744 -- of the temporary file (removing the null character) to the objects
747 Linker_Objects.Table (Objs_Begin) :=
748 new String'(Value (Object_File_Option_Ptr) &
749 Tname (Tname'First .. Tname'Last - 1));
751 -- The slots containing these object file names are then removed
752 -- from the objects table so they do not appear in the link. They
753 -- are removed by moving up the linker options and non-Ada object
754 -- files appearing after the Ada object list in the table.
756 N := Objs_End - Objs_Begin + 1;
757 for J in Objs_End + 1 .. Linker_Objects.Last loop
758 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
761 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
764 -- Process switches and options
766 if Next_Line (Nfirst .. Nlast) /= End_Info then
768 -- Add binder options only if not already set on the command
769 -- line. This rule is a way to control the linker options order.
771 if not Is_Option_Present
772 (Next_Line (Nfirst .. Nlast))
774 if Next_Line (Nfirst .. Nlast) = "-static" then
777 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
781 if Nlast > Nfirst + 2 and then
782 Next_Line (Nfirst .. Nfirst + 1) = "-L"
784 -- Construct a library search path for use later
785 -- to locate static gnatlib libraries.
787 if Libpath.Last > 1 then
788 Libpath.Increment_Last;
789 Libpath.Table (Libpath.Last) := Path_Separator;
792 for I in Nfirst + 2 .. Nlast loop
793 Libpath.Increment_Last;
794 Libpath.Table (Libpath.Last) := Next_Line (I);
797 Linker_Options.Increment_Last;
799 Linker_Options.Table (Linker_Options.Last) :=
800 new String'(Next_Line (Nfirst .. Nlast));
802 elsif Next_Line (Nfirst .. Nlast) = "-ldecgnat"
803 or else Next_Line (Nfirst .. Nlast) = "-lgnarl"
804 or else Next_Line (Nfirst .. Nlast) = "-lgnat"
806 -- Given a Gnat standard library, search the
807 -- library path to find the library location
809 File_Path : String_Access;
811 Object_Lib_Extension : constant String :=
813 (Object_Library_Ext_Ptr);
815 File_Name : String :=
817 Next_Line (Nfirst + 2 .. Nlast) &
818 Object_Lib_Extension;
824 String (Libpath.Table (1 .. Libpath.Last)));
826 if File_Path /= null then
829 -- If static gnatlib found, explicitly
830 -- specify to overcome possible linker
831 -- default usage of shared version.
833 Linker_Options.Increment_Last;
835 Linker_Options.Table (Linker_Options.Last) :=
836 new String'(File_Path.all);
838 elsif GNAT_Shared then
840 -- If shared gnatlib desired, add the
841 -- appropriate system specific switch
842 -- so that it can be located at runtime.
845 Run_Path_Opt : constant String :=
847 (Run_Path_Option_Ptr);
850 if Run_Path_Opt'Length /= 0 then
852 -- Output the system specific linker
853 -- command that allows the image
854 -- activator to find the shared library
857 Linker_Options.Increment_Last;
860 (Linker_Options.Last) :=
861 new String'(Run_Path_Opt
863 (1 .. File_Path'Length
864 - File_Name'Length));
867 Linker_Options.Increment_Last;
870 (Linker_Options.Last) :=
871 new String'(Next_Line
878 -- If gnatlib library not found, then
879 -- add it anyway in case some other
880 -- mechanimsm may find it.
882 Linker_Options.Increment_Last;
884 Linker_Options.Table (Linker_Options.Last) :=
885 new String'(Next_Line (Nfirst .. Nlast));
889 Linker_Options.Increment_Last;
890 Linker_Options.Table (Linker_Options.Last) :=
891 new String'(Next_Line (Nfirst .. Nlast));
897 exit when Next_Line (Nfirst .. Nlast) = End_Info;
899 if Ada_Bind_File then
900 Next_Line (Nfirst .. Nlast - 8) :=
901 Next_Line (Nfirst + 8 .. Nlast);
907 Status := fclose (Fd);
908 end Process_Binder_File;
914 function Value (chars : chars_ptr) return String is
915 function Strlen (chars : chars_ptr) return Natural;
916 pragma Import (C, Strlen);
919 if chars = Null_Address then
924 subtype Result_Type is String (1 .. Strlen (chars));
926 Result : Result_Type;
927 for Result'Address use chars;
939 procedure Write_Usage is
941 Write_Str ("Usage: ");
942 Write_Str (Base_Name (Command_Name));
943 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
946 Write_Line (" mainprog.ali the ALI file of the main program");
948 Write_Line (" -A Binder generated source file is in Ada (default)");
949 Write_Line (" -C Binder generated source file is in C");
950 Write_Line (" -f force object file list to be generated");
951 Write_Line (" -g Compile binder source file with debug information");
952 Write_Line (" -n Do not compile the binder source file");
953 Write_Line (" -v verbose mode");
954 Write_Line (" -v -v very verbose mode");
956 Write_Line (" -o nam Use 'nam' as the name of the executable");
957 Write_Line (" -b target Compile the binder source to run on target");
958 Write_Line (" -Bdir Load compiler executables from dir");
959 Write_Line (" --GCC=comp Use comp as the compiler");
960 Write_Line (" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
962 Write_Line (" [non-Ada-objects] list of non Ada object files");
963 Write_Line (" [linker-options] other options for the linker");
966 -- Start of processing for Gnatlink
970 if Argument_Count = 0 then
972 Exit_Program (E_Fatal);
975 if Hostparm.Java_VM then
976 Gcc := new String'("jgnat");
977 Ada_Bind_File := True;
978 Begin_Info := "-- BEGIN Object file/option list";
979 End_Info := "-- END Object file/option list ";
984 -- Locate all the necessary programs and verify required files are present
986 Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
988 if Gcc_Path = null then
989 Exit_With_Error ("Couldn't locate " & Gcc.all);
992 if Linker_Path = null then
993 Linker_Path := Gcc_Path;
996 if Ali_File_Name = null then
997 Exit_With_Error ("Required 'name'.ali not present.");
1000 if not Is_Regular_File (Ali_File_Name.all) then
1001 Exit_With_Error (Ali_File_Name.all & " not found.");
1004 if Verbose_Mode then
1006 Write_Str ("GNATLINK ");
1007 Write_Str (Gnat_Version_String);
1008 Write_Str (" Copyright 1996-2001 Free Software Foundation, Inc.");
1012 -- If there wasn't an output specified, then use the base name of
1013 -- the .ali file name.
1015 if Output_File_Name = null then
1018 new String'(Base_Name (Ali_File_Name.all)
1019 & Get_Debuggable_Suffix.all);
1021 Linker_Options.Increment_Last;
1022 Linker_Options.Table (Linker_Options.Last) :=
1025 Linker_Options.Increment_Last;
1026 Linker_Options.Table (Linker_Options.Last) :=
1027 new String'(Output_File_Name.all);
1031 -- Warn if main program is called "test", as that may be a built-in command
1032 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1033 -- will not appear. However, do not warn in the case of a cross compiler.
1035 -- Assume that if the executable name is not gnatlink, this is a cross
1038 if Base_Name (Command_Name) = "gnatlink"
1039 and then Output_File_Name.all = "test"
1041 Error_Msg ("warning: executable name """ & Output_File_Name.all
1042 & """ may conflict with shell command");
1045 -- Perform consistency checks
1047 -- Transform the .ali file name into the binder output file name.
1049 Make_Binder_File_Names : declare
1050 Fname : String := Base_Name (Ali_File_Name.all);
1051 Fname_Len : Integer := Fname'Length;
1053 function Get_Maximum_File_Name_Length return Integer;
1054 pragma Import (C, Get_Maximum_File_Name_Length,
1055 "__gnat_get_maximum_file_name_length");
1057 Maximum_File_Name_Length : Integer := Get_Maximum_File_Name_Length;
1059 Second_Char : Character;
1060 -- Second character of name of files
1063 -- Set proper second character of file name
1065 if not Ada_Bind_File then
1068 elsif Hostparm.OpenVMS then
1075 -- If the length of the binder file becomes too long due to
1076 -- the addition of the "b?" prefix, then truncate it.
1078 if Maximum_File_Name_Length > 0 then
1079 while Fname_Len > Maximum_File_Name_Length - 2 loop
1080 Fname_Len := Fname_Len - 1;
1084 if Ada_Bind_File then
1085 Binder_Spec_Src_File :=
1088 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1090 Binder_Body_Src_File :=
1093 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1098 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1102 Binder_Body_Src_File :=
1105 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1112 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1113 & Get_Object_Suffix.all);
1115 if Fname_Len /= Fname'Length then
1116 Binder_Options.Increment_Last;
1117 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1118 Binder_Options.Increment_Last;
1119 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1122 end Make_Binder_File_Names;
1124 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1126 -- Compile the binder file. This is fast, so we always do it, unless
1127 -- specifically told not to by the -n switch
1129 if Compile_Bind_File then
1132 Args : Argument_List (1 .. Binder_Options.Last + 1);
1135 for J in Binder_Options.First .. Binder_Options.Last loop
1136 Args (J) := Binder_Options.Table (J);
1139 Args (Args'Last) := Binder_Body_Src_File;
1141 if Verbose_Mode then
1142 Write_Str (Base_Name (Gcc_Path.all));
1144 for J in Args'Range loop
1146 Write_Str (Args (J).all);
1152 GNAT.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1155 Exit_Program (E_Fatal);
1160 -- Now, actually link the program.
1162 -- Skip this step for now on the JVM since the Java interpreter will do
1163 -- the actual link at run time. We might consider packing all class files
1164 -- in a .zip file during this step.
1166 if not Hostparm.Java_VM then
1168 Num_Args : Natural :=
1169 (Linker_Options.Last - Linker_Options.First + 1) +
1170 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1171 (Linker_Objects.Last - Linker_Objects.First + 1);
1172 Stack_Op : Boolean := False;
1173 IDENT_Op : Boolean := False;
1176 -- Remove duplicate stack size setting from the Linker_Options
1177 -- table. The stack setting option "-Xlinker --stack=R,C" can be
1178 -- found in one line when set by a pragma Linker_Options or in two
1179 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
1180 -- line. We also check for the "-Wl,--stack=R" style option.
1182 -- We must remove the second stack setting option instance
1183 -- because the one on the command line will always be the first
1184 -- one. And any subsequent stack setting option will overwrite the
1185 -- previous one. This is done especially for GNAT/NT where we set
1186 -- the stack size for tasking programs by a pragma in the NT
1187 -- specific tasking package System.Task_Primitives.Oparations.
1189 for J in Linker_Options.First .. Linker_Options.Last loop
1190 if Linker_Options.Table (J).all = "-Xlinker"
1191 and then J < Linker_Options.Last
1192 and then Linker_Options.Table (J + 1)'Length > 8
1193 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
1196 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
1197 Linker_Options.Table (J + 2 .. Linker_Options.Last);
1198 Linker_Options.Decrement_Last;
1199 Linker_Options.Decrement_Last;
1200 Num_Args := Num_Args - 2;
1207 -- Here we just check for a canonical form that matches the
1208 -- pragma Linker_Options set in the NT runtime.
1210 if (Linker_Options.Table (J)'Length > 17
1211 and then Linker_Options.Table (J) (1 .. 17)
1212 = "-Xlinker --stack=")
1214 (Linker_Options.Table (J)'Length > 12
1215 and then Linker_Options.Table (J) (1 .. 12)
1219 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1220 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1221 Linker_Options.Decrement_Last;
1222 Num_Args := Num_Args - 1;
1229 -- Remove duplicate IDENTIFICATION directives (VMS)
1231 if Linker_Options.Table (J)'Length > 27
1232 and then Linker_Options.Table (J) (1 .. 27)
1233 = "--for-linker=IDENTIFICATION="
1236 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1237 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1238 Linker_Options.Decrement_Last;
1239 Num_Args := Num_Args - 1;
1246 -- Prepare arguments for call to linker
1248 Call_Linker : declare
1250 Args : Argument_List (1 .. Num_Args + 1);
1251 Index : Integer := Args'First;
1254 Args (Index) := Binder_Obj_File;
1256 -- Add the object files and any -largs libraries
1258 for J in Linker_Objects.First .. Linker_Objects.Last loop
1260 Args (Index) := Linker_Objects.Table (J);
1263 -- Add the linker options from the binder file
1265 for J in Linker_Options.First .. Linker_Options.Last loop
1267 Args (Index) := Linker_Options.Table (J);
1270 -- Finally add the libraries from the --GCC= switch
1272 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
1274 Args (Index) := Gcc_Linker_Options.Table (J);
1277 if Verbose_Mode then
1278 Write_Str (Linker_Path.all);
1280 for J in Args'Range loop
1282 Write_Str (Args (J).all);
1287 -- If we are on very verbose mode (-v -v) and a response file
1288 -- is used we display its content.
1290 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
1292 Write_Str ("Response file (" &
1293 Tname (Tname'First .. Tname'Last - 1) &
1298 Response_File_Objects.First ..
1299 Response_File_Objects.Last
1301 Write_Str (Response_File_Objects.Table (J).all);
1309 GNAT.OS_Lib.Spawn (Linker_Path.all, Args, Success);
1311 -- Delete the temporary file used in conjuction with linking if
1312 -- one was created. See Process_Bind_File for details.
1314 if Tname_FD /= Invalid_FD then
1319 Error_Msg ("cannot call " & Linker_Path.all);
1320 Exit_Program (E_Fatal);
1326 -- Only keep the binder output file and it's associated object
1327 -- file if compiling with the -g option. These files are only
1328 -- useful if debugging.
1330 if not Debug_Flag_Present then
1331 if Binder_Ali_File /= null then
1332 Delete (Binder_Ali_File.all & ASCII.NUL);
1335 if Binder_Spec_Src_File /= null then
1336 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
1339 Delete (Binder_Body_Src_File.all & ASCII.NUL);
1341 if not Hostparm.Java_VM then
1342 Delete (Binder_Obj_File.all & ASCII.NUL);
1346 Exit_Program (E_Success);
1350 Exit_With_Error ("INTERNAL ERROR. Please report.");