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
1302 if Argument_Count = 0
1304 (Verbose_Mode and then Argument_Count = 1)
1307 Exit_Program (E_Fatal);
1310 if Hostparm.Java_VM then
1311 Gcc := new String'("jgnat");
1312 Ada_Bind_File := True;
1313 Begin_Info := "-- BEGIN Object file/option list";
1314 End_Info := "-- END Object file/option list ";
1317 -- We always compile with -c
1319 Binder_Options_From_ALI.Increment_Last;
1320 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1323 -- If the main program is in Ada it is compiled with the following
1326 -- -gnatA stops reading gnat.adc, since we don't know what
1327 -- pagmas would work, and we do not need it anyway.
1329 -- -gnatWb allows brackets coding for wide characters
1331 -- -gnatiw allows wide characters in identifiers. This is needed
1332 -- because bindgen uses brackets encoding for all upper
1333 -- half and wide characters in identifier names.
1335 if Ada_Bind_File then
1336 Binder_Options_From_ALI.Increment_Last;
1337 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1338 new String'("-gnatA");
1339 Binder_Options_From_ALI.Increment_Last;
1340 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1341 new String'("-gnatWb");
1342 Binder_Options_From_ALI.Increment_Last;
1343 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1344 new String'("-gnatiw");
1347 -- Locate all the necessary programs and verify required files are present
1349 Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1351 if Gcc_Path = null then
1352 Exit_With_Error ("Couldn't locate " & Gcc.all);
1355 if Linker_Path = null then
1356 Linker_Path := Gcc_Path;
1359 if Ali_File_Name = null then
1360 Exit_With_Error ("no ali file given for link");
1363 if not Is_Regular_File (Ali_File_Name.all) then
1364 Exit_With_Error (Ali_File_Name.all & " not found");
1366 -- Read the ALI file of the main subprogram if the binder generated
1367 -- file needs to be compiled and no --GCC= switch has been specified.
1368 -- Fetch the back end switches from this ALI file and use these switches
1369 -- to compile the binder generated file
1371 elsif Compile_Bind_File and then Standard_Gcc then
1372 -- Do some initializations
1376 Name_Len := Ali_File_Name'Length;
1377 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1381 F : constant File_Name_Type := Name_Find;
1382 T : Text_Buffer_Ptr;
1386 -- Osint.Add_Default_Search_Dirs;
1387 -- Load the ALI file
1389 T := Read_Library_Info (F, True);
1391 -- Read it. Note that we ignore errors, since we only want very
1392 -- limited information from the ali file, and likely a slightly
1393 -- wrong version will be just fine, though in normal operation
1394 -- we don't expect this to happen!
1401 Ignore_Errors => True);
1403 if A /= No_ALI_Id then
1405 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1406 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1408 -- Do not compile with the front end switches except for --RTS
1411 Arg : String_Ptr renames Args.Table (Index);
1413 if not Is_Front_End_Switch (Arg.all)
1414 or else Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1416 Binder_Options_From_ALI.Increment_Last;
1417 Binder_Options_From_ALI.Table
1418 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1428 -- If no output name specified, then use the base name of .ali file name
1430 if Output_File_Name = null then
1433 new String'(Base_Name (Ali_File_Name.all)
1434 & Get_Debuggable_Suffix.all);
1436 Linker_Options.Increment_Last;
1437 Linker_Options.Table (Linker_Options.Last) :=
1440 Linker_Options.Increment_Last;
1441 Linker_Options.Table (Linker_Options.Last) :=
1442 new String'(Output_File_Name.all);
1446 -- Warn if main program is called "test", as that may be a built-in command
1447 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1448 -- will not appear. However, do not warn in the case of a cross compiler.
1450 -- Assume this is a cross tool if the executable name is not gnatlink
1452 if Base_Name (Command_Name) = "gnatlink"
1453 and then Output_File_Name.all = "test"
1455 Error_Msg ("warning: executable name """ & Output_File_Name.all
1456 & """ may conflict with shell command");
1459 -- Perform consistency checks
1461 -- Transform the .ali file name into the binder output file name
1463 Make_Binder_File_Names : declare
1464 Fname : constant String := Base_Name (Ali_File_Name.all);
1465 Fname_Len : Integer := Fname'Length;
1467 function Get_Maximum_File_Name_Length return Integer;
1468 pragma Import (C, Get_Maximum_File_Name_Length,
1469 "__gnat_get_maximum_file_name_length");
1471 Maximum_File_Name_Length : constant Integer :=
1472 Get_Maximum_File_Name_Length;
1474 Second_Char : Character;
1475 -- Second character of name of files
1478 -- Set proper second character of file name
1480 if not Ada_Bind_File then
1483 elsif Hostparm.OpenVMS then
1490 -- If the length of the binder file becomes too long due to
1491 -- the addition of the "b?" prefix, then truncate it.
1493 if Maximum_File_Name_Length > 0 then
1494 while Fname_Len > Maximum_File_Name_Length - 2 loop
1495 Fname_Len := Fname_Len - 1;
1499 if Ada_Bind_File then
1500 Binder_Spec_Src_File :=
1503 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1505 Binder_Body_Src_File :=
1508 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1513 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1517 Binder_Body_Src_File :=
1520 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1527 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1528 & Get_Object_Suffix.all);
1530 if Fname_Len /= Fname'Length then
1531 Binder_Options.Increment_Last;
1532 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1533 Binder_Options.Increment_Last;
1534 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1537 end Make_Binder_File_Names;
1539 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1541 -- Compile the binder file. This is fast, so we always do it, unless
1542 -- specifically told not to by the -n switch
1544 if Compile_Bind_File then
1547 Args : Argument_List
1548 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1551 for J in 1 .. Binder_Options_From_ALI.Last loop
1552 Args (J) := Binder_Options_From_ALI.Table (J);
1555 for J in 1 .. Binder_Options.Last loop
1556 Args (Binder_Options_From_ALI.Last + J) :=
1557 Binder_Options.Table (J);
1560 Args (Args'Last) := Binder_Body_Src_File;
1562 if Verbose_Mode then
1563 Write_Str (Base_Name (Gcc_Path.all));
1565 for J in Args'Range loop
1567 Write_Str (Args (J).all);
1573 GNAT.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1576 Exit_Program (E_Fatal);
1581 -- Now, actually link the program.
1583 -- Skip this step for now on the JVM since the Java interpreter will do
1584 -- the actual link at run time. We might consider packing all class files
1585 -- in a .zip file during this step.
1587 if not Hostparm.Java_VM then
1589 Num_Args : Natural :=
1590 (Linker_Options.Last - Linker_Options.First + 1) +
1591 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1592 (Linker_Objects.Last - Linker_Objects.First + 1);
1593 Stack_Op : Boolean := False;
1594 IDENT_Op : Boolean := False;
1597 -- Remove duplicate stack size setting from the Linker_Options
1598 -- table. The stack setting option "-Xlinker --stack=R,C" can be
1599 -- found in one line when set by a pragma Linker_Options or in two
1600 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
1601 -- line. We also check for the "-Wl,--stack=R" style option.
1603 -- We must remove the second stack setting option instance
1604 -- because the one on the command line will always be the first
1605 -- one. And any subsequent stack setting option will overwrite the
1606 -- previous one. This is done especially for GNAT/NT where we set
1607 -- the stack size for tasking programs by a pragma in the NT
1608 -- specific tasking package System.Task_Primitives.Oparations.
1610 -- Note: This is not a FOR loop that runs from Linker_Options.First
1611 -- to Linker_Options.Last, since operations within the loop can
1612 -- modify the length of the table.
1614 Clean_Link_Option_Set : declare
1615 J : Natural := Linker_Options.First;
1618 while J <= Linker_Options.Last loop
1620 if Linker_Options.Table (J).all = "-Xlinker"
1621 and then J < Linker_Options.Last
1622 and then Linker_Options.Table (J + 1)'Length > 8
1623 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
1626 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
1627 Linker_Options.Table (J + 2 .. Linker_Options.Last);
1628 Linker_Options.Decrement_Last;
1629 Linker_Options.Decrement_Last;
1630 Num_Args := Num_Args - 2;
1637 -- Here we just check for a canonical form that matches the
1638 -- pragma Linker_Options set in the NT runtime.
1640 if (Linker_Options.Table (J)'Length > 17
1641 and then Linker_Options.Table (J) (1 .. 17)
1642 = "-Xlinker --stack=")
1644 (Linker_Options.Table (J)'Length > 12
1645 and then Linker_Options.Table (J) (1 .. 12)
1649 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1650 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1651 Linker_Options.Decrement_Last;
1652 Num_Args := Num_Args - 1;
1659 -- Remove duplicate IDENTIFICATION directives (VMS)
1661 if Linker_Options.Table (J)'Length > 27
1662 and then Linker_Options.Table (J) (1 .. 28)
1663 = "--for-linker=IDENTIFICATION="
1666 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1667 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1668 Linker_Options.Decrement_Last;
1669 Num_Args := Num_Args - 1;
1677 end Clean_Link_Option_Set;
1679 -- Prepare arguments for call to linker
1681 Call_Linker : declare
1683 Args : Argument_List (1 .. Num_Args + 1);
1684 Index : Integer := Args'First;
1687 Args (Index) := Binder_Obj_File;
1689 -- Add the object files and any -largs libraries
1691 for J in Linker_Objects.First .. Linker_Objects.Last loop
1693 Args (Index) := Linker_Objects.Table (J);
1696 -- Add the linker options from the binder file
1698 for J in Linker_Options.First .. Linker_Options.Last loop
1700 Args (Index) := Linker_Options.Table (J);
1703 -- Finally add the libraries from the --GCC= switch
1705 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
1707 Args (Index) := Gcc_Linker_Options.Table (J);
1710 if Verbose_Mode then
1711 Write_Str (Linker_Path.all);
1713 for J in Args'Range loop
1715 Write_Str (Args (J).all);
1720 -- If we are on very verbose mode (-v -v) and a response file
1721 -- is used we display its content.
1723 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
1725 Write_Str ("Response file (" &
1726 Tname (Tname'First .. Tname'Last - 1) &
1731 Response_File_Objects.First ..
1732 Response_File_Objects.Last
1734 Write_Str (Response_File_Objects.Table (J).all);
1742 GNAT.OS_Lib.Spawn (Linker_Path.all, Args, Success);
1744 -- Delete the temporary file used in conjuction with linking if
1745 -- one was created. See Process_Bind_File for details.
1747 if Tname_FD /= Invalid_FD then
1752 Error_Msg ("cannot call " & Linker_Path.all);
1753 Exit_Program (E_Fatal);
1759 -- Only keep the binder output file and it's associated object
1760 -- file if compiling with the -g option. These files are only
1761 -- useful if debugging.
1763 if not Debug_Flag_Present then
1764 if Binder_Ali_File /= null then
1765 Delete (Binder_Ali_File.all & ASCII.NUL);
1768 if Binder_Spec_Src_File /= null then
1769 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
1772 Delete (Binder_Body_Src_File.all & ASCII.NUL);
1774 if not Hostparm.Java_VM then
1775 Delete (Binder_Obj_File.all & ASCII.NUL);
1779 Exit_Program (E_Success);
1783 Write_Line (Exception_Information (X));
1784 Exit_With_Error ("INTERNAL ERROR. Please report");