1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2004 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 -- Gnatlink usage: please consult the gnat documentation
30 with Gnatvsn; use Gnatvsn;
32 with Namet; use Namet;
34 with Osint; use Osint;
35 with Output; use Output;
36 with Switch; use Switch;
37 with System; use System;
41 with Ada.Command_Line; use Ada.Command_Line;
42 with Ada.Exceptions; use Ada.Exceptions;
43 with GNAT.OS_Lib; use GNAT.OS_Lib;
44 with Interfaces.C_Streams; use Interfaces.C_Streams;
45 with Interfaces.C.Strings; use Interfaces.C.Strings;
49 pragma Ident (Gnatvsn.Gnat_Static_Version_String);
51 package Gcc_Linker_Options is new Table.Table (
52 Table_Component_Type => String_Access,
53 Table_Index_Type => Integer,
56 Table_Increment => 100,
57 Table_Name => "Gnatlink.Gcc_Linker_Options");
58 -- Comments needed ???
60 package Libpath is new Table.Table (
61 Table_Component_Type => Character,
62 Table_Index_Type => Integer,
64 Table_Initial => 4096,
66 Table_Name => "Gnatlink.Libpath");
67 -- Comments needed ???
69 package Linker_Options is new Table.Table (
70 Table_Component_Type => String_Access,
71 Table_Index_Type => Integer,
74 Table_Increment => 100,
75 Table_Name => "Gnatlink.Linker_Options");
76 -- Comments needed ???
78 package Linker_Objects is new Table.Table (
79 Table_Component_Type => String_Access,
80 Table_Index_Type => Integer,
83 Table_Increment => 100,
84 Table_Name => "Gnatlink.Linker_Objects");
85 -- This table collects the objects file to be passed to the linker. In the
86 -- case where the linker command line is too long then programs objects
87 -- are put on the Response_File_Objects table. Note that the binder object
88 -- file and the user's objects remain in this table. This is very
89 -- important because on the GNU linker command line the -L switch is not
90 -- used to look for objects files but -L switch is used to look for
91 -- objects listed in the response file. This is not a problem with the
92 -- applications objects as they are specified with a fullname.
94 package Response_File_Objects is new Table.Table (
95 Table_Component_Type => String_Access,
96 Table_Index_Type => Integer,
99 Table_Increment => 100,
100 Table_Name => "Gnatlink.Response_File_Objects");
101 -- This table collects the objects file that are to be put in the response
102 -- file. Only application objects are collected there (see details in
103 -- Linker_Objects table comments)
105 package Binder_Options_From_ALI is new Table.Table (
106 Table_Component_Type => String_Access,
107 Table_Index_Type => Integer,
108 Table_Low_Bound => 1, -- equals low bound of Argument_List for Spawn
110 Table_Increment => 100,
111 Table_Name => "Gnatlink.Binder_Options_From_ALI");
112 -- This table collects the switches from the ALI file of the main
115 package Binder_Options is new Table.Table (
116 Table_Component_Type => String_Access,
117 Table_Index_Type => Integer,
118 Table_Low_Bound => 1, -- equals low bound of Argument_List for Spawn
120 Table_Increment => 100,
121 Table_Name => "Gnatlink.Binder_Options");
122 -- This table collects the arguments to be passed to compile the binder
125 Gcc : String_Access := Program_Name ("gcc");
127 Read_Mode : constant String := "r" & ASCII.Nul;
129 Begin_Info : String := "-- BEGIN Object file/option list";
130 End_Info : String := "-- END Object file/option list ";
131 -- Note: above lines are modified in C mode, see option processing
133 Gcc_Path : String_Access;
134 Linker_Path : String_Access;
136 Output_File_Name : String_Access;
137 Ali_File_Name : String_Access;
138 Binder_Spec_Src_File : String_Access;
139 Binder_Body_Src_File : String_Access;
140 Binder_Ali_File : String_Access;
141 Binder_Obj_File : String_Access;
143 Tname : Temp_File_Name;
144 Tname_FD : File_Descriptor := Invalid_FD;
145 -- Temporary file used by linker to pass list of object files on
146 -- certain systems with limitations on size of arguments.
148 Debug_Flag_Present : Boolean := False;
149 Verbose_Mode : Boolean := False;
150 Very_Verbose_Mode : Boolean := False;
152 Ada_Bind_File : Boolean := True;
153 -- Set to True if bind file is generated in Ada
155 Standard_Gcc : Boolean := True;
157 Compile_Bind_File : Boolean := True;
158 -- Set to False if bind file is not to be compiled
160 Object_List_File_Supported : Boolean;
162 (C, Object_List_File_Supported, "__gnat_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 procedure Write_Header;
188 -- Show user the program name, version and copyright.
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
234 pragma Unreferenced (Status);
236 Status := unlink (Name'Address);
237 -- Is it really right to ignore an error here ???
244 procedure Error_Msg (Message : in String) is
246 Write_Str (Base_Name (Command_Name));
252 ---------------------
253 -- Exit_With_Error --
254 ---------------------
256 procedure Exit_With_Error (Error : in String) is
259 Exit_Program (E_Fatal);
266 procedure Process_Args is
268 Skip_Next : Boolean := False;
269 -- Set to true if the next argument is to be added into the list of
270 -- linker's argument without parsing it.
273 -- Loop through arguments of gnatlink command
277 exit when Next_Arg > Argument_Count;
279 Process_One_Arg : declare
280 Arg : constant String := Argument (Next_Arg);
283 -- Case of argument which is a switch
285 -- We definitely need section by section comments here ???
289 -- This argument must not be parsed, just add it to the
290 -- list of linker's options.
294 Linker_Options.Increment_Last;
295 Linker_Options.Table (Linker_Options.Last) :=
298 elsif Arg'Length /= 0 and then Arg (1) = '-' then
300 and then Arg (2 .. 5) = "gnat"
303 ("invalid switch: """ & Arg & """ (gnat not needed here)");
306 if Arg = "-Xlinker" then
308 -- Next argument should be sent directly to the linker.
309 -- We do not want to parse it here.
313 Linker_Options.Increment_Last;
314 Linker_Options.Table (Linker_Options.Last) :=
318 and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
320 Debug_Flag_Present := True;
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 elsif Arg'Length = 2 then
333 Ada_Bind_File := True;
334 Begin_Info := "-- BEGIN Object file/option list";
335 End_Info := "-- END Object file/option list ";
338 Linker_Options.Increment_Last;
339 Linker_Options.Table (Linker_Options.Last) :=
342 Binder_Options.Increment_Last;
343 Binder_Options.Table (Binder_Options.Last) :=
344 Linker_Options.Table (Linker_Options.Last);
346 Next_Arg := Next_Arg + 1;
348 if Next_Arg > Argument_Count then
349 Exit_With_Error ("Missing argument for -b");
352 Get_Machine_Name : declare
353 Name_Arg : constant String_Access :=
354 new String'(Argument (Next_Arg));
357 Linker_Options.Increment_Last;
358 Linker_Options.Table (Linker_Options.Last) :=
361 Binder_Options.Increment_Last;
362 Binder_Options.Table (Binder_Options.Last) :=
365 end Get_Machine_Name;
368 Ada_Bind_File := False;
369 Begin_Info := "/* BEGIN Object file/option list";
370 End_Info := " END Object file/option list */";
373 if Object_List_File_Supported then
374 Object_List_File_Required := True;
377 ("Object list file not supported on this target");
381 Compile_Bind_File := False;
384 Linker_Options.Increment_Last;
385 Linker_Options.Table (Linker_Options.Last) :=
388 Next_Arg := Next_Arg + 1;
390 if Next_Arg > Argument_Count then
391 Exit_With_Error ("Missing argument for -o");
394 Output_File_Name := new String'(Argument (Next_Arg));
396 Linker_Options.Increment_Last;
397 Linker_Options.Table (Linker_Options.Last) :=
401 Opt.Run_Path_Option := False;
405 -- Support "double" verbose mode. Second -v
406 -- gets sent to the linker and binder phases.
409 Very_Verbose_Mode := True;
411 Linker_Options.Increment_Last;
412 Linker_Options.Table (Linker_Options.Last) :=
415 Binder_Options.Increment_Last;
416 Binder_Options.Table (Binder_Options.Last) :=
417 Linker_Options.Table (Linker_Options.Last);
420 Verbose_Mode := True;
425 Linker_Options.Increment_Last;
426 Linker_Options.Table (Linker_Options.Last) :=
431 elsif Arg (2) = 'B' then
432 Linker_Options.Increment_Last;
433 Linker_Options.Table (Linker_Options.Last) :=
436 Binder_Options.Increment_Last;
437 Binder_Options.Table (Binder_Options.Last) :=
438 Linker_Options.Table (Linker_Options.Last);
440 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
442 if Arg'Length = 7 then
443 Exit_With_Error ("Missing argument for --LINK=");
447 GNAT.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
449 if Linker_Path = null then
451 ("Could not locate linker: " & Arg (8 .. Arg'Last));
454 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
456 Program_Args : constant Argument_List_Access :=
457 Argument_String_To_List
458 (Arg (7 .. Arg'Last));
461 Gcc := new String'(Program_Args.all (1).all);
462 Standard_Gcc := False;
464 -- Set appropriate flags for switches passed
466 for J in 2 .. Program_Args.all'Last loop
468 Arg : constant String := Program_Args.all (J).all;
469 AF : constant Integer := Arg'First;
472 if Arg'Length /= 0 and then Arg (AF) = '-' then
473 if Arg (AF + 1) = 'g'
474 and then (Arg'Length = 2
475 or else Arg (AF + 2) in '0' .. '3'
476 or else Arg (AF + 2 .. Arg'Last) = "coff")
478 Debug_Flag_Present := True;
482 -- Pass to gcc for compiling binder generated file
483 -- No use passing libraries, it will just generate
486 if not (Arg (AF .. AF + 1) = "-l"
487 or else Arg (AF .. AF + 1) = "-L")
489 Binder_Options.Increment_Last;
490 Binder_Options.Table (Binder_Options.Last) :=
494 -- Pass to gcc for linking program.
496 Gcc_Linker_Options.Increment_Last;
497 Gcc_Linker_Options.Table
498 (Gcc_Linker_Options.Last) := new String'(Arg);
503 -- Send all multi-character switches not recognized as
504 -- a special case by gnatlink to the linker/loader stage.
507 Linker_Options.Increment_Last;
508 Linker_Options.Table (Linker_Options.Last) :=
512 -- Here if argument is a file name rather than a switch
515 -- If explicit ali file, capture it
518 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
520 if Ali_File_Name = null then
521 Ali_File_Name := new String'(Arg);
523 Exit_With_Error ("cannot handle more than one ALI file");
526 -- If object file, record object file
528 elsif Arg'Length > Get_Object_Suffix.all'Length
530 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
531 = Get_Object_Suffix.all
533 Linker_Objects.Increment_Last;
534 Linker_Objects.Table (Linker_Objects.Last) :=
537 -- If corresponding ali file exists, capture it
539 elsif Ali_File_Name = null
540 and then Is_Regular_File (Arg & ".ali")
542 Ali_File_Name := new String'(Arg & ".ali");
544 -- Otherwise assume this is a linker options entry, but
545 -- see below for interesting adjustment to this assumption.
548 Linker_Options.Increment_Last;
549 Linker_Options.Table (Linker_Options.Last) :=
555 Next_Arg := Next_Arg + 1;
558 -- If Ada bind file, then compile it with warnings suppressed, because
559 -- otherwise the with of the main program may cause junk warnings.
561 if Ada_Bind_File then
562 Binder_Options.Increment_Last;
563 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
566 -- If we did not get an ali file at all, and we had at least one
567 -- linker option, then assume that was the intended ali file after
568 -- all, so that we get a nicer message later on.
570 if Ali_File_Name = null
571 and then Linker_Options.Last >= Linker_Options.First
574 new String'(Linker_Options.Table (Linker_Options.First).all &
579 -------------------------
580 -- Process_Binder_File --
581 -------------------------
583 procedure Process_Binder_File (Name : in String) is
585 -- Binder file's descriptor
587 Link_Bytes : Integer := 0;
588 -- Projected number of bytes for the linker command line
591 pragma Import (C, Link_Max, "__gnat_link_max");
592 -- Maximum number of bytes on the command line supported by the OS
593 -- linker. Passed this limit the response file mechanism must be used
596 Next_Line : String (1 .. 1000);
597 -- Current line value
601 -- Current line slice (the slice does not contain line terminator)
604 -- Current line last character for shared libraries (without version)
606 Objs_Begin : Integer := 0;
607 -- First object file index in Linker_Objects table
609 Objs_End : Integer := 0;
610 -- Last object file index in Linker_Objects table
613 -- Used for various Interfaces.C_Streams calls
615 Closing_Status : Boolean;
618 GNAT_Static : Boolean := False;
619 -- Save state of -static option.
621 GNAT_Shared : Boolean := False;
622 -- Save state of -shared option.
624 Xlinker_Was_Previous : Boolean := False;
625 -- Indicate that "-Xlinker" was the option preceding the current
626 -- option. If True, then the current option is never suppressed.
630 -- These data items are used to store current binder file context.
631 -- The context is composed of the file descriptor position and the
632 -- current line together with the slice indexes (first and last
633 -- position) for this line. The rollback data are used by the
634 -- Store_File_Context and Rollback_File_Context routines below.
635 -- The file context mechanism interact only with the Get_Next_Line
636 -- call. For example:
638 -- Store_File_Context;
640 -- Rollback_File_Context;
643 -- Both Get_Next_Line calls above will read the exact same data from
644 -- the file. In other words, Next_Line, Nfirst and Nlast variables
645 -- will be set with the exact same values.
647 RB_File_Pos : long; -- File position
648 RB_Next_Line : String (1 .. 1000); -- Current line content
649 RB_Nlast : Integer; -- Slice last index
650 RB_Nfirst : Integer; -- Slice first index
652 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
653 pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
654 -- Pointer to string representing the native linker option which
655 -- specifies the path where the dynamic loader should find shared
656 -- libraries. Equal to null string if this system doesn't support it.
658 Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
660 (C, Object_Library_Ext_Ptr, "__gnat_object_library_extension");
661 -- Pointer to string specifying the default extension for
662 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
664 Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
665 pragma Import (C, Object_File_Option_Ptr, "__gnat_object_file_option");
666 -- Pointer to a string representing the linker option which specifies
667 -- the response file.
669 Using_GNU_Linker : Boolean;
670 pragma Import (C, Using_GNU_Linker, "__gnat_using_gnu_linker");
671 -- Predicate indicating whether this target uses the GNU linker. In
672 -- this case we must output a GNU linker compatible response file.
674 Opening : aliased constant String := """";
675 Closing : aliased constant String := '"' & ASCII.LF;
676 -- Needed to quote object paths in object list files when GNU linker
679 procedure Get_Next_Line;
680 -- Read the next line from the binder file without the line
683 function Index (S, Pattern : String) return Natural;
684 -- Return the last occurrence of Pattern in S, or 0 if none.
686 function Is_Option_Present (Opt : in String) return Boolean;
687 -- Return true if the option Opt is already present in
688 -- Linker_Options table.
690 procedure Store_File_Context;
691 -- Store current file context, Fd position and current line data.
692 -- The file context is stored into the rollback data above (RB_*).
693 -- Store_File_Context can be called at any time, only the last call
694 -- will be used (i.e. this routine overwrites the file context).
696 procedure Rollback_File_Context;
697 -- Restore file context from rollback data. This routine must be called
698 -- after Store_File_Context. The binder file context will be restored
699 -- with the data stored by the last Store_File_Context call.
705 procedure Get_Next_Line is
709 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
711 if Fchars = System.Null_Address then
712 Exit_With_Error ("Error reading binder output");
715 Nfirst := Next_Line'First;
717 while Nlast <= Next_Line'Last
718 and then Next_Line (Nlast) /= ASCII.LF
719 and then Next_Line (Nlast) /= ASCII.CR
731 function Index (S, Pattern : String) return Natural is
732 Len : constant Natural := Pattern'Length;
735 for J in reverse S'First .. S'Last - Len + 1 loop
736 if Pattern = S (J .. J + Len - 1) then
744 -----------------------
745 -- Is_Option_Present --
746 -----------------------
748 function Is_Option_Present (Opt : in String) return Boolean is
750 for I in 1 .. Linker_Options.Last loop
752 if Linker_Options.Table (I).all = Opt then
759 end Is_Option_Present;
761 ---------------------------
762 -- Rollback_File_Context --
763 ---------------------------
765 procedure Rollback_File_Context is
767 Next_Line := RB_Next_Line;
770 Status := fseek (Fd, RB_File_Pos, Interfaces.C_Streams.SEEK_SET);
773 Exit_With_Error ("Error setting file position");
775 end Rollback_File_Context;
777 ------------------------
778 -- Store_File_Context --
779 ------------------------
781 procedure Store_File_Context is
782 use type System.CRTL.long;
784 RB_Next_Line := Next_Line;
787 RB_File_Pos := ftell (Fd);
789 if RB_File_Pos = -1 then
790 Exit_With_Error ("Error getting file position");
792 end Store_File_Context;
794 -- Start of processing for Process_Binder_File
797 Fd := fopen (Name'Address, Read_Mode'Address);
799 if Fd = NULL_Stream then
800 Exit_With_Error ("Failed to open binder output");
803 -- Skip up to the Begin Info line
807 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
813 -- Go to end when end line is reached (this will happen in
814 -- High_Integrity_Mode where no -L switches are generated)
816 exit when Next_Line (Nfirst .. Nlast) = End_Info;
818 if Ada_Bind_File then
819 Next_Line (Nfirst .. Nlast - 8) :=
820 Next_Line (Nfirst + 8 .. Nlast);
824 -- Go to next section when switches are reached
826 exit when Next_Line (1) = '-';
828 -- Otherwise we have another object file to collect
830 Linker_Objects.Increment_Last;
832 -- Mark the positions of first and last object files in case
833 -- they need to be placed with a named file on systems having
834 -- linker line limitations.
836 if Objs_Begin = 0 then
837 Objs_Begin := Linker_Objects.Last;
840 Linker_Objects.Table (Linker_Objects.Last) :=
841 new String'(Next_Line (Nfirst .. Nlast));
843 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
844 -- Nlast - Nfirst + 1, for the size, plus one for the space between
848 Objs_End := Linker_Objects.Last;
850 -- Let's continue to compute the Link_Bytes, the linker options are
851 -- part of command line length.
855 while Next_Line (Nfirst .. Nlast) /= End_Info loop
856 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
861 Rollback_File_Context;
863 -- On systems that have limitations on handling very long linker lines
864 -- we make use of the system linker option which takes a list of object
865 -- file names from a file instead of the command line itself. What we do
866 -- is to replace the list of object files by the special linker option
867 -- which then reads the object file list from a file instead. The option
868 -- to read from a file instead of the command line is only triggered if
869 -- a conservative threshold is passed.
871 if Object_List_File_Required
872 or else (Object_List_File_Supported
873 and then Link_Bytes > Link_Max)
875 -- Create a temporary file containing the Ada user object files
876 -- needed by the link. This list is taken from the bind file
877 -- and is output one object per line for maximal compatibility with
878 -- linkers supporting this option.
880 Create_Temp_File (Tname_FD, Tname);
882 -- ??? File descriptor should be checked to not be Invalid_FD.
883 -- ??? Status of Write and Close operations should be checked, and
884 -- failure should occur if a status is wrong.
886 -- If target is using the GNU linker we must add a special header
887 -- and footer in the response file.
888 -- The syntax is : INPUT (object1.o object2.o ... )
889 -- Because the GNU linker does not like name with characters such
890 -- as '!', we must put the object paths between double quotes.
892 if Using_GNU_Linker then
894 GNU_Header : aliased constant String := "INPUT (";
897 Status := Write (Tname_FD, GNU_Header'Address,
902 for J in Objs_Begin .. Objs_End loop
904 -- Opening quote for GNU linker
906 if Using_GNU_Linker then
907 Status := Write (Tname_FD, Opening'Address, 1);
910 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
911 Linker_Objects.Table (J).all'Length);
913 -- Closing quote for GNU linker
915 if Using_GNU_Linker then
916 Status := Write (Tname_FD, Closing'Address, 2);
919 Status := Write (Tname_FD, ASCII.LF'Address, 1);
922 Response_File_Objects.Increment_Last;
923 Response_File_Objects.Table (Response_File_Objects.Last) :=
924 Linker_Objects.Table (J);
927 -- Handle GNU linker response file footer
929 if Using_GNU_Linker then
931 GNU_Footer : aliased constant String := ")";
934 Status := Write (Tname_FD, GNU_Footer'Address,
939 Close (Tname_FD, Closing_Status);
941 -- Add the special objects list file option together with the name
942 -- of the temporary file (removing the null character) to the objects
945 Linker_Objects.Table (Objs_Begin) :=
946 new String'(Value (Object_File_Option_Ptr) &
947 Tname (Tname'First .. Tname'Last - 1));
949 -- The slots containing these object file names are then removed
950 -- from the objects table so they do not appear in the link. They
951 -- are removed by moving up the linker options and non-Ada object
952 -- files appearing after the Ada object list in the table.
957 N := Objs_End - Objs_Begin + 1;
959 for J in Objs_End + 1 .. Linker_Objects.Last loop
960 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
963 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
967 -- Process switches and options
969 if Next_Line (Nfirst .. Nlast) /= End_Info then
970 Xlinker_Was_Previous := False;
973 if Xlinker_Was_Previous
974 or else Next_Line (Nfirst .. Nlast) = "-Xlinker"
976 Linker_Options.Increment_Last;
977 Linker_Options.Table (Linker_Options.Last) :=
978 new String'(Next_Line (Nfirst .. Nlast));
980 elsif Next_Line (Nfirst .. Nlast) = "-static" then
983 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
986 -- Add binder options only if not already set on the command
987 -- line. This rule is a way to control the linker options order.
989 -- The following test needs comments, why is it VMS specific.
990 -- The above comment looks out of date ???
992 elsif not (Hostparm.OpenVMS
994 Is_Option_Present (Next_Line (Nfirst .. Nlast)))
996 if Nlast > Nfirst + 2 and then
997 Next_Line (Nfirst .. Nfirst + 1) = "-L"
999 -- Construct a library search path for use later
1000 -- to locate static gnatlib libraries.
1002 if Libpath.Last > 1 then
1003 Libpath.Increment_Last;
1004 Libpath.Table (Libpath.Last) := Path_Separator;
1007 for I in Nfirst + 2 .. Nlast loop
1008 Libpath.Increment_Last;
1009 Libpath.Table (Libpath.Last) := Next_Line (I);
1012 Linker_Options.Increment_Last;
1014 Linker_Options.Table (Linker_Options.Last) :=
1015 new String'(Next_Line (Nfirst .. Nlast));
1017 elsif Next_Line (Nfirst .. Nlast) = "-ldecgnat"
1018 or else Next_Line (Nfirst .. Nlast) = "-lgnarl"
1019 or else Next_Line (Nfirst .. Nlast) = "-lgnat"
1021 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) =
1022 Shared_Lib ("gnarl")
1024 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) =
1027 -- If it is a shared library, remove the library version.
1028 -- We will be looking for the static version of the library
1029 -- as it is in the same directory as the shared version.
1031 if Next_Line (Nlast - Library_Version'Length + 1 .. Nlast)
1034 -- Set Last to point to last character before the
1037 Last := Nlast - Library_Version'Length - 1;
1042 -- Given a Gnat standard library, search the
1043 -- library path to find the library location
1046 File_Path : String_Access;
1048 Object_Lib_Extension : constant String :=
1049 Value (Object_Library_Ext_Ptr);
1051 File_Name : constant String := "lib" &
1052 Next_Line (Nfirst + 2 .. Last) &
1053 Object_Lib_Extension;
1055 Run_Path_Opt : constant String :=
1056 Value (Run_Path_Option_Ptr);
1058 GCC_Index : Natural;
1059 Run_Path_Opt_Index : Natural := 0;
1063 Locate_Regular_File (File_Name,
1064 String (Libpath.Table (1 .. Libpath.Last)));
1066 if File_Path /= null then
1069 -- If static gnatlib found, explicitly
1070 -- specify to overcome possible linker
1071 -- default usage of shared version.
1073 Linker_Options.Increment_Last;
1075 Linker_Options.Table (Linker_Options.Last) :=
1076 new String'(File_Path.all);
1078 elsif GNAT_Shared then
1079 if Opt.Run_Path_Option then
1080 -- If shared gnatlib desired, add the
1081 -- appropriate system specific switch
1082 -- so that it can be located at runtime.
1084 if Run_Path_Opt'Length /= 0 then
1085 -- Output the system specific linker command
1086 -- that allows the image activator to find
1087 -- the shared library at runtime.
1088 -- Also add path to find libgcc_s.so, if
1091 -- To find the location of the shared version
1092 -- of libgcc, we look for "gcc-lib" in the
1093 -- path of the library. However, this
1094 -- subdirectory is no longer present in
1095 -- in recent version of GCC. So, we look for
1096 -- the last subdirectory "lib" in the path.
1099 Index (File_Path.all, "gcc-lib");
1101 if GCC_Index /= 0 then
1102 -- The shared version of libgcc is
1103 -- located in the parent directory.
1105 GCC_Index := GCC_Index - 1;
1109 Index (File_Path.all, "/lib/");
1111 if GCC_Index = 0 then
1113 Index (File_Path.all,
1114 Directory_Separator &
1116 Directory_Separator);
1119 -- We have found a subdirectory "lib",
1120 -- this is where the shared version of
1121 -- libgcc should be located.
1123 if GCC_Index /= 0 then
1124 GCC_Index := GCC_Index + 3;
1128 -- Look for an eventual run_path_option in
1129 -- the linker switches.
1131 for J in reverse 1 .. Linker_Options.Last loop
1132 if Linker_Options.Table (J) /= null
1134 Linker_Options.Table (J)'Length
1135 > Run_Path_Opt'Length
1137 Linker_Options.Table (J)
1138 (1 .. Run_Path_Opt'Length) =
1141 -- We have found a already specified
1142 -- run_path_option: we will add to this
1143 -- switch, because only one
1144 -- run_path_option should be specified.
1146 Run_Path_Opt_Index := J;
1151 -- If there is no run_path_option, we need
1154 if Run_Path_Opt_Index = 0 then
1155 Linker_Options.Increment_Last;
1158 if GCC_Index = 0 then
1159 if Run_Path_Opt_Index = 0 then
1160 Linker_Options.Table
1161 (Linker_Options.Last) :=
1165 (1 .. File_Path'Length
1166 - File_Name'Length));
1169 Linker_Options.Table
1170 (Run_Path_Opt_Index) :=
1172 (Linker_Options.Table
1173 (Run_Path_Opt_Index).all
1176 (1 .. File_Path'Length
1177 - File_Name'Length));
1181 if Run_Path_Opt_Index = 0 then
1182 Linker_Options.Table
1183 (Linker_Options.Last) :=
1184 new String'(Run_Path_Opt
1186 (1 .. File_Path'Length
1189 & File_Path (1 .. GCC_Index));
1192 Linker_Options.Table
1193 (Run_Path_Opt_Index) :=
1195 (Linker_Options.Table
1196 (Run_Path_Opt_Index).all
1199 (1 .. File_Path'Length
1202 & File_Path (1 .. GCC_Index));
1208 -- Then we add the appropriate -l switch
1210 Linker_Options.Increment_Last;
1211 Linker_Options.Table (Linker_Options.Last) :=
1212 new String'(Next_Line (Nfirst .. Nlast));
1216 -- If gnatlib library not found, then
1217 -- add it anyway in case some other
1218 -- mechanimsm may find it.
1220 Linker_Options.Increment_Last;
1221 Linker_Options.Table (Linker_Options.Last) :=
1222 new String'(Next_Line (Nfirst .. Nlast));
1226 Linker_Options.Increment_Last;
1227 Linker_Options.Table (Linker_Options.Last) :=
1228 new String'(Next_Line (Nfirst .. Nlast));
1232 Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker";
1235 exit when Next_Line (Nfirst .. Nlast) = End_Info;
1237 if Ada_Bind_File then
1238 Next_Line (Nfirst .. Nlast - 8) :=
1239 Next_Line (Nfirst + 8 .. Nlast);
1245 Status := fclose (Fd);
1246 end Process_Binder_File;
1252 procedure Write_Header is
1254 if Verbose_Mode then
1256 Write_Str ("GNATLINK ");
1257 Write_Str (Gnat_Version_String);
1258 Write_Str (" Copyright 1995-2004 Free Software Foundation, Inc");
1267 procedure Write_Usage is
1271 Write_Str ("Usage: ");
1272 Write_Str (Base_Name (Command_Name));
1273 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
1276 Write_Line (" mainprog.ali the ALI file of the main program");
1278 Write_Line (" -A Binder generated source file is in Ada (default)");
1279 Write_Line (" -C Binder generated source file is in C");
1280 Write_Line (" -f force object file list to be generated");
1281 Write_Line (" -g Compile binder source file with debug information");
1282 Write_Line (" -n Do not compile the binder source file");
1283 Write_Line (" -R Do not use a run_path_option");
1284 Write_Line (" -v verbose mode");
1285 Write_Line (" -v -v very verbose mode");
1287 Write_Line (" -o nam Use 'nam' as the name of the executable");
1288 Write_Line (" -b target Compile the binder source to run on target");
1289 Write_Line (" -Bdir Load compiler executables from dir");
1290 Write_Line (" --GCC=comp Use comp as the compiler");
1291 Write_Line (" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1293 Write_Line (" [non-Ada-objects] list of non Ada object files");
1294 Write_Line (" [linker-options] other options for the linker");
1297 -- Start of processing for Gnatlink
1300 -- Add the directory where gnatlink is invoked in front of the
1301 -- path, if gnatlink is invoked with directory information.
1302 -- Only do this if the platform is not VMS, where the notion of path
1303 -- does not really exist.
1305 if not Hostparm.OpenVMS then
1307 Command : constant String := Command_Name;
1310 for Index in reverse Command'Range loop
1311 if Command (Index) = Directory_Separator then
1313 Absolute_Dir : constant String :=
1315 (Command (Command'First .. Index));
1317 PATH : constant String :=
1320 Getenv ("PATH").all;
1323 Setenv ("PATH", PATH);
1334 if Argument_Count = 0
1336 (Verbose_Mode and then Argument_Count = 1)
1339 Exit_Program (E_Fatal);
1342 if Hostparm.Java_VM then
1343 Gcc := new String'("jgnat");
1344 Ada_Bind_File := True;
1345 Begin_Info := "-- BEGIN Object file/option list";
1346 End_Info := "-- END Object file/option list ";
1349 -- We always compile with -c
1351 Binder_Options_From_ALI.Increment_Last;
1352 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1355 -- If the main program is in Ada it is compiled with the following
1358 -- -gnatA stops reading gnat.adc, since we don't know what
1359 -- pagmas would work, and we do not need it anyway.
1361 -- -gnatWb allows brackets coding for wide characters
1363 -- -gnatiw allows wide characters in identifiers. This is needed
1364 -- because bindgen uses brackets encoding for all upper
1365 -- half and wide characters in identifier names.
1367 if Ada_Bind_File then
1368 Binder_Options_From_ALI.Increment_Last;
1369 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1370 new String'("-gnatA");
1371 Binder_Options_From_ALI.Increment_Last;
1372 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1373 new String'("-gnatWb");
1374 Binder_Options_From_ALI.Increment_Last;
1375 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1376 new String'("-gnatiw");
1379 -- Locate all the necessary programs and verify required files are present
1381 Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1383 if Gcc_Path = null then
1384 Exit_With_Error ("Couldn't locate " & Gcc.all);
1387 if Linker_Path = null then
1388 Linker_Path := Gcc_Path;
1391 if Ali_File_Name = null then
1392 Exit_With_Error ("no ali file given for link");
1395 if not Is_Regular_File (Ali_File_Name.all) then
1396 Exit_With_Error (Ali_File_Name.all & " not found");
1398 -- Read the ALI file of the main subprogram if the binder generated
1399 -- file needs to be compiled and no --GCC= switch has been specified.
1400 -- Fetch the back end switches from this ALI file and use these switches
1401 -- to compile the binder generated file
1403 elsif Compile_Bind_File and then Standard_Gcc then
1404 -- Do some initializations
1408 Name_Len := Ali_File_Name'Length;
1409 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1413 F : constant File_Name_Type := Name_Find;
1414 T : Text_Buffer_Ptr;
1418 -- Osint.Add_Default_Search_Dirs;
1419 -- Load the ALI file
1421 T := Read_Library_Info (F, True);
1423 -- Read it. Note that we ignore errors, since we only want very
1424 -- limited information from the ali file, and likely a slightly
1425 -- wrong version will be just fine, though in normal operation
1426 -- we don't expect this to happen!
1433 Ignore_Errors => True);
1435 if A /= No_ALI_Id then
1437 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1438 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1440 -- Do not compile with the front end switches except for --RTS
1441 -- if the binder generated file is in Ada.
1444 Arg : String_Ptr renames Args.Table (Index);
1446 if not Is_Front_End_Switch (Arg.all)
1449 and then Arg'Length > 5
1450 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS=")
1452 Binder_Options_From_ALI.Increment_Last;
1453 Binder_Options_From_ALI.Table
1454 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1464 -- If no output name specified, then use the base name of .ali file name
1466 if Output_File_Name = null then
1469 new String'(Base_Name (Ali_File_Name.all)
1470 & Get_Debuggable_Suffix.all);
1472 Linker_Options.Increment_Last;
1473 Linker_Options.Table (Linker_Options.Last) :=
1476 Linker_Options.Increment_Last;
1477 Linker_Options.Table (Linker_Options.Last) :=
1478 new String'(Output_File_Name.all);
1482 -- Warn if main program is called "test", as that may be a built-in command
1483 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1484 -- will not appear. However, do not warn in the case of a cross compiler.
1486 -- Assume this is a cross tool if the executable name is not gnatlink
1488 if Base_Name (Command_Name) = "gnatlink"
1489 and then Output_File_Name.all = "test"
1491 Error_Msg ("warning: executable name """ & Output_File_Name.all
1492 & """ may conflict with shell command");
1495 -- Perform consistency checks
1497 -- Transform the .ali file name into the binder output file name
1499 Make_Binder_File_Names : declare
1500 Fname : constant String := Base_Name (Ali_File_Name.all);
1501 Fname_Len : Integer := Fname'Length;
1503 function Get_Maximum_File_Name_Length return Integer;
1504 pragma Import (C, Get_Maximum_File_Name_Length,
1505 "__gnat_get_maximum_file_name_length");
1507 Maximum_File_Name_Length : constant Integer :=
1508 Get_Maximum_File_Name_Length;
1510 Second_Char : Character;
1511 -- Second character of name of files
1514 -- Set proper second character of file name
1516 if not Ada_Bind_File then
1519 elsif Hostparm.OpenVMS then
1526 -- If the length of the binder file becomes too long due to
1527 -- the addition of the "b?" prefix, then truncate it.
1529 if Maximum_File_Name_Length > 0 then
1530 while Fname_Len > Maximum_File_Name_Length - 2 loop
1531 Fname_Len := Fname_Len - 1;
1535 if Ada_Bind_File then
1536 Binder_Spec_Src_File :=
1539 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1541 Binder_Body_Src_File :=
1544 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1549 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1553 Binder_Body_Src_File :=
1556 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1563 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1564 & Get_Object_Suffix.all);
1566 if Fname_Len /= Fname'Length then
1567 Binder_Options.Increment_Last;
1568 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1569 Binder_Options.Increment_Last;
1570 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1573 end Make_Binder_File_Names;
1575 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1577 -- Compile the binder file. This is fast, so we always do it, unless
1578 -- specifically told not to by the -n switch
1580 if Compile_Bind_File then
1583 Args : Argument_List
1584 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1587 for J in 1 .. Binder_Options_From_ALI.Last loop
1588 Args (J) := Binder_Options_From_ALI.Table (J);
1591 for J in 1 .. Binder_Options.Last loop
1592 Args (Binder_Options_From_ALI.Last + J) :=
1593 Binder_Options.Table (J);
1596 Args (Args'Last) := Binder_Body_Src_File;
1598 if Verbose_Mode then
1599 Write_Str (Base_Name (Gcc_Path.all));
1601 for J in Args'Range loop
1603 Write_Str (Args (J).all);
1609 GNAT.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1612 Exit_Program (E_Fatal);
1617 -- Now, actually link the program.
1619 -- Skip this step for now on the JVM since the Java interpreter will do
1620 -- the actual link at run time. We might consider packing all class files
1621 -- in a .zip file during this step.
1623 if not Hostparm.Java_VM then
1625 Num_Args : Natural :=
1626 (Linker_Options.Last - Linker_Options.First + 1) +
1627 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1628 (Linker_Objects.Last - Linker_Objects.First + 1);
1629 Stack_Op : Boolean := False;
1630 IDENT_Op : Boolean := False;
1633 -- Remove duplicate stack size setting from the Linker_Options
1634 -- table. The stack setting option "-Xlinker --stack=R,C" can be
1635 -- found in one line when set by a pragma Linker_Options or in two
1636 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
1637 -- line. We also check for the "-Wl,--stack=R" style option.
1639 -- We must remove the second stack setting option instance
1640 -- because the one on the command line will always be the first
1641 -- one. And any subsequent stack setting option will overwrite the
1642 -- previous one. This is done especially for GNAT/NT where we set
1643 -- the stack size for tasking programs by a pragma in the NT
1644 -- specific tasking package System.Task_Primitives.Oparations.
1646 -- Note: This is not a FOR loop that runs from Linker_Options.First
1647 -- to Linker_Options.Last, since operations within the loop can
1648 -- modify the length of the table.
1650 Clean_Link_Option_Set : declare
1651 J : Natural := Linker_Options.First;
1654 while J <= Linker_Options.Last loop
1656 if Linker_Options.Table (J).all = "-Xlinker"
1657 and then J < Linker_Options.Last
1658 and then Linker_Options.Table (J + 1)'Length > 8
1659 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
1662 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
1663 Linker_Options.Table (J + 2 .. Linker_Options.Last);
1664 Linker_Options.Decrement_Last;
1665 Linker_Options.Decrement_Last;
1666 Num_Args := Num_Args - 2;
1673 -- Here we just check for a canonical form that matches the
1674 -- pragma Linker_Options set in the NT runtime.
1676 if (Linker_Options.Table (J)'Length > 17
1677 and then Linker_Options.Table (J) (1 .. 17)
1678 = "-Xlinker --stack=")
1680 (Linker_Options.Table (J)'Length > 12
1681 and then Linker_Options.Table (J) (1 .. 12)
1685 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1686 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1687 Linker_Options.Decrement_Last;
1688 Num_Args := Num_Args - 1;
1695 -- Remove duplicate IDENTIFICATION directives (VMS)
1697 if Linker_Options.Table (J)'Length > 27
1698 and then Linker_Options.Table (J) (1 .. 28)
1699 = "--for-linker=IDENTIFICATION="
1702 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1703 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1704 Linker_Options.Decrement_Last;
1705 Num_Args := Num_Args - 1;
1713 end Clean_Link_Option_Set;
1715 -- Prepare arguments for call to linker
1717 Call_Linker : declare
1719 Args : Argument_List (1 .. Num_Args + 1);
1720 Index : Integer := Args'First;
1723 Args (Index) := Binder_Obj_File;
1725 -- Add the object files and any -largs libraries
1727 for J in Linker_Objects.First .. Linker_Objects.Last loop
1729 Args (Index) := Linker_Objects.Table (J);
1732 -- Add the linker options from the binder file
1734 for J in Linker_Options.First .. Linker_Options.Last loop
1736 Args (Index) := Linker_Options.Table (J);
1739 -- Finally add the libraries from the --GCC= switch
1741 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
1743 Args (Index) := Gcc_Linker_Options.Table (J);
1746 if Verbose_Mode then
1747 Write_Str (Linker_Path.all);
1749 for J in Args'Range loop
1751 Write_Str (Args (J).all);
1756 -- If we are on very verbose mode (-v -v) and a response file
1757 -- is used we display its content.
1759 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
1761 Write_Str ("Response file (" &
1762 Tname (Tname'First .. Tname'Last - 1) &
1767 Response_File_Objects.First ..
1768 Response_File_Objects.Last
1770 Write_Str (Response_File_Objects.Table (J).all);
1778 GNAT.OS_Lib.Spawn (Linker_Path.all, Args, Success);
1780 -- Delete the temporary file used in conjuction with linking if
1781 -- one was created. See Process_Bind_File for details.
1783 if Tname_FD /= Invalid_FD then
1788 Error_Msg ("cannot call " & Linker_Path.all);
1789 Exit_Program (E_Fatal);
1795 -- Only keep the binder output file and it's associated object
1796 -- file if compiling with the -g option. These files are only
1797 -- useful if debugging.
1799 if not Debug_Flag_Present then
1800 if Binder_Ali_File /= null then
1801 Delete (Binder_Ali_File.all & ASCII.NUL);
1804 if Binder_Spec_Src_File /= null then
1805 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
1808 Delete (Binder_Body_Src_File.all & ASCII.NUL);
1810 if not Hostparm.Java_VM then
1811 Delete (Binder_Obj_File.all & ASCII.NUL);
1815 Exit_Program (E_Success);
1819 Write_Line (Exception_Information (X));
1820 Exit_With_Error ("INTERNAL ERROR. Please report");