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;
161 pragma Import (C, Object_List_File_Supported, "objlist_file_supported");
162 -- Predicate indicating whether the linker has an option whereby the
163 -- names of object files can be passed to the linker in a file.
165 Object_List_File_Required : Boolean := False;
166 -- Set to True to force generation of a response file
168 function Base_Name (File_Name : in String) return String;
169 -- Return just the file name part without the extension (if present).
171 procedure Delete (Name : in String);
172 -- Wrapper to unlink as status is ignored by this application.
174 procedure Error_Msg (Message : in String);
175 -- Output the error or warning Message
177 procedure Exit_With_Error (Error : in String);
178 -- Output Error and exit program with a fatal condition.
180 procedure Process_Args;
181 -- Go through all the arguments and build option tables.
183 procedure Process_Binder_File (Name : in String);
184 -- Reads the binder file and extracts linker arguments.
186 procedure Write_Header;
187 -- Show user the program name, version and copyright.
189 procedure Write_Usage;
190 -- Show user the program options.
196 function Base_Name (File_Name : in String) return String is
201 Findex1 := File_Name'First;
203 -- The file might be specified by a full path name. However,
204 -- we want the path to be stripped away.
206 for J in reverse File_Name'Range loop
207 if Is_Directory_Separator (File_Name (J)) then
213 Findex2 := File_Name'Last;
214 while Findex2 > Findex1
215 and then File_Name (Findex2) /= '.'
217 Findex2 := Findex2 - 1;
220 if Findex2 = Findex1 then
221 Findex2 := File_Name'Last + 1;
224 return File_Name (Findex1 .. Findex2 - 1);
231 procedure Delete (Name : in String) is
233 pragma Unreferenced (Status);
235 Status := unlink (Name'Address);
236 -- Is it really right to ignore an error here ???
243 procedure Error_Msg (Message : in String) is
245 Write_Str (Base_Name (Command_Name));
251 ---------------------
252 -- Exit_With_Error --
253 ---------------------
255 procedure Exit_With_Error (Error : in String) is
258 Exit_Program (E_Fatal);
265 procedure Process_Args is
267 Skip_Next : Boolean := False;
268 -- Set to true if the next argument is to be added into the list of
269 -- linker's argument without parsing it.
272 -- Loop through arguments of gnatlink command
276 exit when Next_Arg > Argument_Count;
278 Process_One_Arg : declare
279 Arg : constant String := Argument (Next_Arg);
282 -- Case of argument which is a switch
284 -- We definitely need section by section comments here ???
288 -- This argument must not be parsed, just add it to the
289 -- list of linker's options.
293 Linker_Options.Increment_Last;
294 Linker_Options.Table (Linker_Options.Last) :=
297 elsif Arg'Length /= 0 and then Arg (1) = '-' then
299 and then Arg (2 .. 5) = "gnat"
302 ("invalid switch: """ & Arg & """ (gnat not needed here)");
305 if Arg = "-Xlinker" then
307 -- Next argument should be sent directly to the linker.
308 -- We do not want to parse it here.
312 Linker_Options.Increment_Last;
313 Linker_Options.Table (Linker_Options.Last) :=
317 and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
319 Debug_Flag_Present := True;
321 Linker_Options.Increment_Last;
322 Linker_Options.Table (Linker_Options.Last) :=
325 Binder_Options.Increment_Last;
326 Binder_Options.Table (Binder_Options.Last) :=
327 Linker_Options.Table (Linker_Options.Last);
329 elsif Arg'Length = 2 then
332 Ada_Bind_File := True;
333 Begin_Info := "-- BEGIN Object file/option list";
334 End_Info := "-- END Object file/option list ";
337 Linker_Options.Increment_Last;
338 Linker_Options.Table (Linker_Options.Last) :=
341 Binder_Options.Increment_Last;
342 Binder_Options.Table (Binder_Options.Last) :=
343 Linker_Options.Table (Linker_Options.Last);
345 Next_Arg := Next_Arg + 1;
347 if Next_Arg > Argument_Count then
348 Exit_With_Error ("Missing argument for -b");
351 Get_Machine_Name : declare
352 Name_Arg : constant String_Access :=
353 new String'(Argument (Next_Arg));
356 Linker_Options.Increment_Last;
357 Linker_Options.Table (Linker_Options.Last) :=
360 Binder_Options.Increment_Last;
361 Binder_Options.Table (Binder_Options.Last) :=
364 end Get_Machine_Name;
367 Ada_Bind_File := False;
368 Begin_Info := "/* BEGIN Object file/option list";
369 End_Info := " END Object file/option list */";
372 if Object_List_File_Supported then
373 Object_List_File_Required := True;
376 ("Object list file not supported on this target");
380 Compile_Bind_File := False;
383 Linker_Options.Increment_Last;
384 Linker_Options.Table (Linker_Options.Last) :=
387 Next_Arg := Next_Arg + 1;
389 if Next_Arg > Argument_Count then
390 Exit_With_Error ("Missing argument for -o");
393 Output_File_Name := new String'(Argument (Next_Arg));
395 Linker_Options.Increment_Last;
396 Linker_Options.Table (Linker_Options.Last) :=
400 Opt.Run_Path_Option := False;
404 -- Support "double" verbose mode. Second -v
405 -- gets sent to the linker and binder phases.
408 Very_Verbose_Mode := True;
410 Linker_Options.Increment_Last;
411 Linker_Options.Table (Linker_Options.Last) :=
414 Binder_Options.Increment_Last;
415 Binder_Options.Table (Binder_Options.Last) :=
416 Linker_Options.Table (Linker_Options.Last);
419 Verbose_Mode := True;
424 Linker_Options.Increment_Last;
425 Linker_Options.Table (Linker_Options.Last) :=
430 elsif Arg (2) = 'B' then
431 Linker_Options.Increment_Last;
432 Linker_Options.Table (Linker_Options.Last) :=
435 Binder_Options.Increment_Last;
436 Binder_Options.Table (Binder_Options.Last) :=
437 Linker_Options.Table (Linker_Options.Last);
439 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
441 if Arg'Length = 7 then
442 Exit_With_Error ("Missing argument for --LINK=");
446 GNAT.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
448 if Linker_Path = null then
450 ("Could not locate linker: " & Arg (8 .. Arg'Last));
453 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
455 Program_Args : constant Argument_List_Access :=
456 Argument_String_To_List
457 (Arg (7 .. Arg'Last));
460 Gcc := new String'(Program_Args.all (1).all);
461 Standard_Gcc := False;
463 -- Set appropriate flags for switches passed
465 for J in 2 .. Program_Args.all'Last loop
467 Arg : constant String := Program_Args.all (J).all;
468 AF : constant Integer := Arg'First;
471 if Arg'Length /= 0 and then Arg (AF) = '-' then
472 if Arg (AF + 1) = 'g'
473 and then (Arg'Length = 2
474 or else Arg (AF + 2) in '0' .. '3'
475 or else Arg (AF + 2 .. Arg'Last) = "coff")
477 Debug_Flag_Present := True;
481 -- Pass to gcc for compiling binder generated file
482 -- No use passing libraries, it will just generate
485 if not (Arg (AF .. AF + 1) = "-l"
486 or else Arg (AF .. AF + 1) = "-L")
488 Binder_Options.Increment_Last;
489 Binder_Options.Table (Binder_Options.Last) :=
493 -- Pass to gcc for linking program.
495 Gcc_Linker_Options.Increment_Last;
496 Gcc_Linker_Options.Table
497 (Gcc_Linker_Options.Last) := new String'(Arg);
502 -- Send all multi-character switches not recognized as
503 -- a special case by gnatlink to the linker/loader stage.
506 Linker_Options.Increment_Last;
507 Linker_Options.Table (Linker_Options.Last) :=
511 -- Here if argument is a file name rather than a switch
514 -- If explicit ali file, capture it
517 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
519 if Ali_File_Name = null then
520 Ali_File_Name := new String'(Arg);
522 Exit_With_Error ("cannot handle more than one ALI file");
525 -- If object file, record object file
527 elsif Arg'Length > Get_Object_Suffix.all'Length
529 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
530 = Get_Object_Suffix.all
532 Linker_Objects.Increment_Last;
533 Linker_Objects.Table (Linker_Objects.Last) :=
536 -- If corresponding ali file exists, capture it
538 elsif Ali_File_Name = null
539 and then Is_Regular_File (Arg & ".ali")
541 Ali_File_Name := new String'(Arg & ".ali");
543 -- Otherwise assume this is a linker options entry, but
544 -- see below for interesting adjustment to this assumption.
547 Linker_Options.Increment_Last;
548 Linker_Options.Table (Linker_Options.Last) :=
554 Next_Arg := Next_Arg + 1;
557 -- If Ada bind file, then compile it with warnings suppressed, because
558 -- otherwise the with of the main program may cause junk warnings.
560 if Ada_Bind_File then
561 Binder_Options.Increment_Last;
562 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
565 -- If we did not get an ali file at all, and we had at least one
566 -- linker option, then assume that was the intended ali file after
567 -- all, so that we get a nicer message later on.
569 if Ali_File_Name = null
570 and then Linker_Options.Last >= Linker_Options.First
573 new String'(Linker_Options.Table (Linker_Options.First).all &
578 -------------------------
579 -- Process_Binder_File --
580 -------------------------
582 procedure Process_Binder_File (Name : in String) is
584 -- Binder file's descriptor
586 Link_Bytes : Integer := 0;
587 -- Projected number of bytes for the linker command line
590 pragma Import (C, Link_Max, "link_max");
591 -- Maximum number of bytes on the command line supported by the OS
592 -- linker. Passed this limit the response file mechanism must be used
595 Next_Line : String (1 .. 1000);
596 -- Current line value
600 -- Current line slice (the slice does not contain line terminator)
603 -- Current line last character for shared libraries (without version)
605 Objs_Begin : Integer := 0;
606 -- First object file index in Linker_Objects table
608 Objs_End : Integer := 0;
609 -- Last object file index in Linker_Objects table
612 -- Used for various Interfaces.C_Streams calls
614 Closing_Status : Boolean;
617 GNAT_Static : Boolean := False;
618 -- Save state of -static option.
620 GNAT_Shared : Boolean := False;
621 -- Save state of -shared option.
623 Xlinker_Was_Previous : Boolean := False;
624 -- Indicate that "-Xlinker" was the option preceding the current
625 -- option. If True, then the current option is never suppressed.
629 -- These data items are used to store current binder file context.
630 -- The context is composed of the file descriptor position and the
631 -- current line together with the slice indexes (first and last
632 -- position) for this line. The rollback data are used by the
633 -- Store_File_Context and Rollback_File_Context routines below.
634 -- The file context mechanism interact only with the Get_Next_Line
635 -- call. For example:
637 -- Store_File_Context;
639 -- Rollback_File_Context;
642 -- Both Get_Next_Line calls above will read the exact same data from
643 -- the file. In other words, Next_Line, Nfirst and Nlast variables
644 -- will be set with the exact same values.
646 RB_File_Pos : long; -- File position
647 RB_Next_Line : String (1 .. 1000); -- Current line content
648 RB_Nlast : Integer; -- Slice last index
649 RB_Nfirst : Integer; -- Slice first index
651 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
652 pragma Import (C, Run_Path_Option_Ptr, "run_path_option");
653 -- Pointer to string representing the native linker option which
654 -- specifies the path where the dynamic loader should find shared
655 -- libraries. Equal to null string if this system doesn't support it.
657 Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
658 pragma Import (C, Object_Library_Ext_Ptr, "object_library_extension");
659 -- Pointer to string specifying the default extension for
660 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
662 Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
663 pragma Import (C, Object_File_Option_Ptr, "object_file_option");
664 -- Pointer to a string representing the linker option which specifies
665 -- the response file.
667 Using_GNU_Linker : Boolean;
668 pragma Import (C, Using_GNU_Linker, "using_gnu_linker");
669 -- Predicate indicating whether this target uses the GNU linker. In
670 -- this case we must output a GNU linker compatible response file.
672 Opening : aliased constant String := """";
673 Closing : aliased constant String := '"' & ASCII.LF;
674 -- Needed to quote object paths in object list files when GNU linker
677 procedure Get_Next_Line;
678 -- Read the next line from the binder file without the line
681 function Index (S, Pattern : String) return Natural;
682 -- Return the last occurrence of Pattern in S, or 0 if none.
684 function Is_Option_Present (Opt : in String) return Boolean;
685 -- Return true if the option Opt is already present in
686 -- Linker_Options table.
688 procedure Store_File_Context;
689 -- Store current file context, Fd position and current line data.
690 -- The file context is stored into the rollback data above (RB_*).
691 -- Store_File_Context can be called at any time, only the last call
692 -- will be used (i.e. this routine overwrites the file context).
694 procedure Rollback_File_Context;
695 -- Restore file context from rollback data. This routine must be called
696 -- after Store_File_Context. The binder file context will be restored
697 -- with the data stored by the last Store_File_Context call.
703 procedure Get_Next_Line is
707 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
709 if Fchars = System.Null_Address then
710 Exit_With_Error ("Error reading binder output");
713 Nfirst := Next_Line'First;
715 while Nlast <= Next_Line'Last
716 and then Next_Line (Nlast) /= ASCII.LF
717 and then Next_Line (Nlast) /= ASCII.CR
729 function Index (S, Pattern : String) return Natural is
730 Len : constant Natural := Pattern'Length;
733 for J in reverse S'First .. S'Last - Len + 1 loop
734 if Pattern = S (J .. J + Len - 1) then
742 -----------------------
743 -- Is_Option_Present --
744 -----------------------
746 function Is_Option_Present (Opt : in String) return Boolean is
748 for I in 1 .. Linker_Options.Last loop
750 if Linker_Options.Table (I).all = Opt then
757 end Is_Option_Present;
759 ---------------------------
760 -- Rollback_File_Context --
761 ---------------------------
763 procedure Rollback_File_Context is
765 Next_Line := RB_Next_Line;
768 Status := fseek (Fd, RB_File_Pos, Interfaces.C_Streams.SEEK_SET);
771 Exit_With_Error ("Error setting file position");
773 end Rollback_File_Context;
775 ------------------------
776 -- Store_File_Context --
777 ------------------------
779 procedure Store_File_Context is
780 use type System.CRTL.long;
782 RB_Next_Line := Next_Line;
785 RB_File_Pos := ftell (Fd);
787 if RB_File_Pos = -1 then
788 Exit_With_Error ("Error getting file position");
790 end Store_File_Context;
792 -- Start of processing for Process_Binder_File
795 Fd := fopen (Name'Address, Read_Mode'Address);
797 if Fd = NULL_Stream then
798 Exit_With_Error ("Failed to open binder output");
801 -- Skip up to the Begin Info line
805 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
811 -- Go to end when end line is reached (this will happen in
812 -- High_Integrity_Mode where no -L switches are generated)
814 exit when Next_Line (Nfirst .. Nlast) = End_Info;
816 if Ada_Bind_File then
817 Next_Line (Nfirst .. Nlast - 8) :=
818 Next_Line (Nfirst + 8 .. Nlast);
822 -- Go to next section when switches are reached
824 exit when Next_Line (1) = '-';
826 -- Otherwise we have another object file to collect
828 Linker_Objects.Increment_Last;
830 -- Mark the positions of first and last object files in case
831 -- they need to be placed with a named file on systems having
832 -- linker line limitations.
834 if Objs_Begin = 0 then
835 Objs_Begin := Linker_Objects.Last;
838 Linker_Objects.Table (Linker_Objects.Last) :=
839 new String'(Next_Line (Nfirst .. Nlast));
841 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
842 -- Nlast - Nfirst + 1, for the size, plus one for the space between
846 Objs_End := Linker_Objects.Last;
848 -- Let's continue to compute the Link_Bytes, the linker options are
849 -- part of command line length.
853 while Next_Line (Nfirst .. Nlast) /= End_Info loop
854 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
859 Rollback_File_Context;
861 -- On systems that have limitations on handling very long linker lines
862 -- we make use of the system linker option which takes a list of object
863 -- file names from a file instead of the command line itself. What we do
864 -- is to replace the list of object files by the special linker option
865 -- which then reads the object file list from a file instead. The option
866 -- to read from a file instead of the command line is only triggered if
867 -- a conservative threshold is passed.
869 if Object_List_File_Required
870 or else (Object_List_File_Supported
871 and then Link_Bytes > Link_Max)
873 -- Create a temporary file containing the Ada user object files
874 -- needed by the link. This list is taken from the bind file
875 -- and is output one object per line for maximal compatibility with
876 -- linkers supporting this option.
878 Create_Temp_File (Tname_FD, Tname);
880 -- ??? File descriptor should be checked to not be Invalid_FD.
881 -- ??? Status of Write and Close operations should be checked, and
882 -- failure should occur if a status is wrong.
884 -- If target is using the GNU linker we must add a special header
885 -- and footer in the response file.
886 -- The syntax is : INPUT (object1.o object2.o ... )
887 -- Because the GNU linker does not like name with characters such
888 -- as '!', we must put the object paths between double quotes.
890 if Using_GNU_Linker then
892 GNU_Header : aliased constant String := "INPUT (";
895 Status := Write (Tname_FD, GNU_Header'Address,
900 for J in Objs_Begin .. Objs_End loop
902 -- Opening quote for GNU linker
904 if Using_GNU_Linker then
905 Status := Write (Tname_FD, Opening'Address, 1);
908 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
909 Linker_Objects.Table (J).all'Length);
911 -- Closing quote for GNU linker
913 if Using_GNU_Linker then
914 Status := Write (Tname_FD, Closing'Address, 2);
917 Status := Write (Tname_FD, ASCII.LF'Address, 1);
920 Response_File_Objects.Increment_Last;
921 Response_File_Objects.Table (Response_File_Objects.Last) :=
922 Linker_Objects.Table (J);
925 -- Handle GNU linker response file footer
927 if Using_GNU_Linker then
929 GNU_Footer : aliased constant String := ")";
932 Status := Write (Tname_FD, GNU_Footer'Address,
937 Close (Tname_FD, Closing_Status);
939 -- Add the special objects list file option together with the name
940 -- of the temporary file (removing the null character) to the objects
943 Linker_Objects.Table (Objs_Begin) :=
944 new String'(Value (Object_File_Option_Ptr) &
945 Tname (Tname'First .. Tname'Last - 1));
947 -- The slots containing these object file names are then removed
948 -- from the objects table so they do not appear in the link. They
949 -- are removed by moving up the linker options and non-Ada object
950 -- files appearing after the Ada object list in the table.
955 N := Objs_End - Objs_Begin + 1;
957 for J in Objs_End + 1 .. Linker_Objects.Last loop
958 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
961 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
965 -- Process switches and options
967 if Next_Line (Nfirst .. Nlast) /= End_Info then
968 Xlinker_Was_Previous := False;
971 if Xlinker_Was_Previous
972 or else Next_Line (Nfirst .. Nlast) = "-Xlinker"
974 Linker_Options.Increment_Last;
975 Linker_Options.Table (Linker_Options.Last) :=
976 new String'(Next_Line (Nfirst .. Nlast));
978 elsif Next_Line (Nfirst .. Nlast) = "-static" then
981 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
984 -- Add binder options only if not already set on the command
985 -- line. This rule is a way to control the linker options order.
987 -- The following test needs comments, why is it VMS specific.
988 -- The above comment looks out of date ???
990 elsif not (Hostparm.OpenVMS
992 Is_Option_Present (Next_Line (Nfirst .. Nlast)))
994 if Nlast > Nfirst + 2 and then
995 Next_Line (Nfirst .. Nfirst + 1) = "-L"
997 -- Construct a library search path for use later
998 -- to locate static gnatlib libraries.
1000 if Libpath.Last > 1 then
1001 Libpath.Increment_Last;
1002 Libpath.Table (Libpath.Last) := Path_Separator;
1005 for I in Nfirst + 2 .. Nlast loop
1006 Libpath.Increment_Last;
1007 Libpath.Table (Libpath.Last) := Next_Line (I);
1010 Linker_Options.Increment_Last;
1012 Linker_Options.Table (Linker_Options.Last) :=
1013 new String'(Next_Line (Nfirst .. Nlast));
1015 elsif Next_Line (Nfirst .. Nlast) = "-ldecgnat"
1016 or else Next_Line (Nfirst .. Nlast) = "-lgnarl"
1017 or else Next_Line (Nfirst .. Nlast) = "-lgnat"
1019 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) =
1020 Shared_Lib ("gnarl")
1022 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) =
1025 -- If it is a shared library, remove the library version.
1026 -- We will be looking for the static version of the library
1027 -- as it is in the same directory as the shared version.
1029 if Next_Line (Nlast - Library_Version'Length + 1 .. Nlast)
1032 -- Set Last to point to last character before the
1035 Last := Nlast - Library_Version'Length - 1;
1040 -- Given a Gnat standard library, search the
1041 -- library path to find the library location
1044 File_Path : String_Access;
1046 Object_Lib_Extension : constant String :=
1047 Value (Object_Library_Ext_Ptr);
1049 File_Name : constant String := "lib" &
1050 Next_Line (Nfirst + 2 .. Last) &
1051 Object_Lib_Extension;
1053 Run_Path_Opt : constant String :=
1054 Value (Run_Path_Option_Ptr);
1056 GCC_Index : Natural;
1057 Run_Path_Opt_Index : Natural := 0;
1061 Locate_Regular_File (File_Name,
1062 String (Libpath.Table (1 .. Libpath.Last)));
1064 if File_Path /= null then
1067 -- If static gnatlib found, explicitly
1068 -- specify to overcome possible linker
1069 -- default usage of shared version.
1071 Linker_Options.Increment_Last;
1073 Linker_Options.Table (Linker_Options.Last) :=
1074 new String'(File_Path.all);
1076 elsif GNAT_Shared then
1077 if Opt.Run_Path_Option then
1078 -- If shared gnatlib desired, add the
1079 -- appropriate system specific switch
1080 -- so that it can be located at runtime.
1082 if Run_Path_Opt'Length /= 0 then
1083 -- Output the system specific linker command
1084 -- that allows the image activator to find
1085 -- the shared library at runtime.
1086 -- Also add path to find libgcc_s.so, if
1089 -- To find the location of the shared version
1090 -- of libgcc, we look for "gcc-lib" in the
1091 -- path of the library. However, this
1092 -- subdirectory is no longer present in
1093 -- in recent version of GCC. So, we look for
1094 -- the last subdirectory "lib" in the path.
1097 Index (File_Path.all, "gcc-lib");
1099 if GCC_Index /= 0 then
1100 -- The shared version of libgcc is
1101 -- located in the parent directory.
1103 GCC_Index := GCC_Index - 1;
1107 Index (File_Path.all, "/lib/");
1109 if GCC_Index = 0 then
1111 Index (File_Path.all,
1112 Directory_Separator &
1114 Directory_Separator);
1117 -- We have found a subdirectory "lib",
1118 -- this is where the shared version of
1119 -- libgcc should be located.
1121 if GCC_Index /= 0 then
1122 GCC_Index := GCC_Index + 3;
1126 -- Look for an eventual run_path_option in
1127 -- the linker switches.
1129 for J in reverse 1 .. Linker_Options.Last loop
1130 if Linker_Options.Table (J) /= null
1132 Linker_Options.Table (J)'Length
1133 > Run_Path_Opt'Length
1135 Linker_Options.Table (J)
1136 (1 .. Run_Path_Opt'Length) =
1139 -- We have found a already specified
1140 -- run_path_option: we will add to this
1141 -- switch, because only one
1142 -- run_path_option should be specified.
1144 Run_Path_Opt_Index := J;
1149 -- If there is no run_path_option, we need
1152 if Run_Path_Opt_Index = 0 then
1153 Linker_Options.Increment_Last;
1156 if GCC_Index = 0 then
1157 if Run_Path_Opt_Index = 0 then
1158 Linker_Options.Table
1159 (Linker_Options.Last) :=
1163 (1 .. File_Path'Length
1164 - File_Name'Length));
1167 Linker_Options.Table
1168 (Run_Path_Opt_Index) :=
1170 (Linker_Options.Table
1171 (Run_Path_Opt_Index).all
1174 (1 .. File_Path'Length
1175 - File_Name'Length));
1179 if Run_Path_Opt_Index = 0 then
1180 Linker_Options.Table
1181 (Linker_Options.Last) :=
1182 new String'(Run_Path_Opt
1184 (1 .. File_Path'Length
1187 & File_Path (1 .. GCC_Index));
1190 Linker_Options.Table
1191 (Run_Path_Opt_Index) :=
1193 (Linker_Options.Table
1194 (Run_Path_Opt_Index).all
1197 (1 .. File_Path'Length
1200 & File_Path (1 .. GCC_Index));
1206 -- Then we add the appropriate -l switch
1208 Linker_Options.Increment_Last;
1209 Linker_Options.Table (Linker_Options.Last) :=
1210 new String'(Next_Line (Nfirst .. Nlast));
1214 -- If gnatlib library not found, then
1215 -- add it anyway in case some other
1216 -- mechanimsm may find it.
1218 Linker_Options.Increment_Last;
1219 Linker_Options.Table (Linker_Options.Last) :=
1220 new String'(Next_Line (Nfirst .. Nlast));
1224 Linker_Options.Increment_Last;
1225 Linker_Options.Table (Linker_Options.Last) :=
1226 new String'(Next_Line (Nfirst .. Nlast));
1230 Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker";
1233 exit when Next_Line (Nfirst .. Nlast) = End_Info;
1235 if Ada_Bind_File then
1236 Next_Line (Nfirst .. Nlast - 8) :=
1237 Next_Line (Nfirst + 8 .. Nlast);
1243 Status := fclose (Fd);
1244 end Process_Binder_File;
1250 procedure Write_Header is
1252 if Verbose_Mode then
1254 Write_Str ("GNATLINK ");
1255 Write_Str (Gnat_Version_String);
1256 Write_Str (" Copyright 1995-2004 Free Software Foundation, Inc");
1265 procedure Write_Usage is
1269 Write_Str ("Usage: ");
1270 Write_Str (Base_Name (Command_Name));
1271 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
1274 Write_Line (" mainprog.ali the ALI file of the main program");
1276 Write_Line (" -A Binder generated source file is in Ada (default)");
1277 Write_Line (" -C Binder generated source file is in C");
1278 Write_Line (" -f force object file list to be generated");
1279 Write_Line (" -g Compile binder source file with debug information");
1280 Write_Line (" -n Do not compile the binder source file");
1281 Write_Line (" -R Do not use a run_path_option");
1282 Write_Line (" -v verbose mode");
1283 Write_Line (" -v -v very verbose mode");
1285 Write_Line (" -o nam Use 'nam' as the name of the executable");
1286 Write_Line (" -b target Compile the binder source to run on target");
1287 Write_Line (" -Bdir Load compiler executables from dir");
1288 Write_Line (" --GCC=comp Use comp as the compiler");
1289 Write_Line (" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1291 Write_Line (" [non-Ada-objects] list of non Ada object files");
1292 Write_Line (" [linker-options] other options for the linker");
1295 -- Start of processing for Gnatlink
1300 if Argument_Count = 0
1302 (Verbose_Mode and then Argument_Count = 1)
1305 Exit_Program (E_Fatal);
1308 if Hostparm.Java_VM then
1309 Gcc := new String'("jgnat");
1310 Ada_Bind_File := True;
1311 Begin_Info := "-- BEGIN Object file/option list";
1312 End_Info := "-- END Object file/option list ";
1315 -- We always compile with -c
1317 Binder_Options_From_ALI.Increment_Last;
1318 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1321 -- If the main program is in Ada it is compiled with the following
1324 -- -gnatA stops reading gnat.adc, since we don't know what
1325 -- pagmas would work, and we do not need it anyway.
1327 -- -gnatWb allows brackets coding for wide characters
1329 -- -gnatiw allows wide characters in identifiers. This is needed
1330 -- because bindgen uses brackets encoding for all upper
1331 -- half and wide characters in identifier names.
1333 if Ada_Bind_File then
1334 Binder_Options_From_ALI.Increment_Last;
1335 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1336 new String'("-gnatA");
1337 Binder_Options_From_ALI.Increment_Last;
1338 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1339 new String'("-gnatWb");
1340 Binder_Options_From_ALI.Increment_Last;
1341 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1342 new String'("-gnatiw");
1345 -- Locate all the necessary programs and verify required files are present
1347 Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1349 if Gcc_Path = null then
1350 Exit_With_Error ("Couldn't locate " & Gcc.all);
1353 if Linker_Path = null then
1354 Linker_Path := Gcc_Path;
1357 if Ali_File_Name = null then
1358 Exit_With_Error ("no ali file given for link");
1361 if not Is_Regular_File (Ali_File_Name.all) then
1362 Exit_With_Error (Ali_File_Name.all & " not found");
1364 -- Read the ALI file of the main subprogram if the binder generated
1365 -- file needs to be compiled and no --GCC= switch has been specified.
1366 -- Fetch the back end switches from this ALI file and use these switches
1367 -- to compile the binder generated file
1369 elsif Compile_Bind_File and then Standard_Gcc then
1370 -- Do some initializations
1374 Name_Len := Ali_File_Name'Length;
1375 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1379 F : constant File_Name_Type := Name_Find;
1380 T : Text_Buffer_Ptr;
1384 -- Osint.Add_Default_Search_Dirs;
1385 -- Load the ALI file
1387 T := Read_Library_Info (F, True);
1389 -- Read it. Note that we ignore errors, since we only want very
1390 -- limited information from the ali file, and likely a slightly
1391 -- wrong version will be just fine, though in normal operation
1392 -- we don't expect this to happen!
1399 Ignore_Errors => True);
1401 if A /= No_ALI_Id then
1403 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1404 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1406 -- Do not compile with the front end switches except for --RTS
1409 Arg : String_Ptr renames Args.Table (Index);
1411 if not Is_Front_End_Switch (Arg.all)
1412 or else Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1414 Binder_Options_From_ALI.Increment_Last;
1415 Binder_Options_From_ALI.Table
1416 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1426 -- If no output name specified, then use the base name of .ali file name
1428 if Output_File_Name = null then
1431 new String'(Base_Name (Ali_File_Name.all)
1432 & Get_Debuggable_Suffix.all);
1434 Linker_Options.Increment_Last;
1435 Linker_Options.Table (Linker_Options.Last) :=
1438 Linker_Options.Increment_Last;
1439 Linker_Options.Table (Linker_Options.Last) :=
1440 new String'(Output_File_Name.all);
1444 -- Warn if main program is called "test", as that may be a built-in command
1445 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1446 -- will not appear. However, do not warn in the case of a cross compiler.
1448 -- Assume this is a cross tool if the executable name is not gnatlink
1450 if Base_Name (Command_Name) = "gnatlink"
1451 and then Output_File_Name.all = "test"
1453 Error_Msg ("warning: executable name """ & Output_File_Name.all
1454 & """ may conflict with shell command");
1457 -- Perform consistency checks
1459 -- Transform the .ali file name into the binder output file name
1461 Make_Binder_File_Names : declare
1462 Fname : constant String := Base_Name (Ali_File_Name.all);
1463 Fname_Len : Integer := Fname'Length;
1465 function Get_Maximum_File_Name_Length return Integer;
1466 pragma Import (C, Get_Maximum_File_Name_Length,
1467 "__gnat_get_maximum_file_name_length");
1469 Maximum_File_Name_Length : constant Integer :=
1470 Get_Maximum_File_Name_Length;
1472 Second_Char : Character;
1473 -- Second character of name of files
1476 -- Set proper second character of file name
1478 if not Ada_Bind_File then
1481 elsif Hostparm.OpenVMS then
1488 -- If the length of the binder file becomes too long due to
1489 -- the addition of the "b?" prefix, then truncate it.
1491 if Maximum_File_Name_Length > 0 then
1492 while Fname_Len > Maximum_File_Name_Length - 2 loop
1493 Fname_Len := Fname_Len - 1;
1497 if Ada_Bind_File then
1498 Binder_Spec_Src_File :=
1501 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1503 Binder_Body_Src_File :=
1506 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1511 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1515 Binder_Body_Src_File :=
1518 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1525 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1526 & Get_Object_Suffix.all);
1528 if Fname_Len /= Fname'Length then
1529 Binder_Options.Increment_Last;
1530 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1531 Binder_Options.Increment_Last;
1532 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1535 end Make_Binder_File_Names;
1537 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1539 -- Compile the binder file. This is fast, so we always do it, unless
1540 -- specifically told not to by the -n switch
1542 if Compile_Bind_File then
1545 Args : Argument_List
1546 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1549 for J in 1 .. Binder_Options_From_ALI.Last loop
1550 Args (J) := Binder_Options_From_ALI.Table (J);
1553 for J in 1 .. Binder_Options.Last loop
1554 Args (Binder_Options_From_ALI.Last + J) :=
1555 Binder_Options.Table (J);
1558 Args (Args'Last) := Binder_Body_Src_File;
1560 if Verbose_Mode then
1561 Write_Str (Base_Name (Gcc_Path.all));
1563 for J in Args'Range loop
1565 Write_Str (Args (J).all);
1571 GNAT.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1574 Exit_Program (E_Fatal);
1579 -- Now, actually link the program.
1581 -- Skip this step for now on the JVM since the Java interpreter will do
1582 -- the actual link at run time. We might consider packing all class files
1583 -- in a .zip file during this step.
1585 if not Hostparm.Java_VM then
1587 Num_Args : Natural :=
1588 (Linker_Options.Last - Linker_Options.First + 1) +
1589 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1590 (Linker_Objects.Last - Linker_Objects.First + 1);
1591 Stack_Op : Boolean := False;
1592 IDENT_Op : Boolean := False;
1595 -- Remove duplicate stack size setting from the Linker_Options
1596 -- table. The stack setting option "-Xlinker --stack=R,C" can be
1597 -- found in one line when set by a pragma Linker_Options or in two
1598 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
1599 -- line. We also check for the "-Wl,--stack=R" style option.
1601 -- We must remove the second stack setting option instance
1602 -- because the one on the command line will always be the first
1603 -- one. And any subsequent stack setting option will overwrite the
1604 -- previous one. This is done especially for GNAT/NT where we set
1605 -- the stack size for tasking programs by a pragma in the NT
1606 -- specific tasking package System.Task_Primitives.Oparations.
1608 -- Note: This is not a FOR loop that runs from Linker_Options.First
1609 -- to Linker_Options.Last, since operations within the loop can
1610 -- modify the length of the table.
1612 Clean_Link_Option_Set : declare
1613 J : Natural := Linker_Options.First;
1616 while J <= Linker_Options.Last loop
1618 if Linker_Options.Table (J).all = "-Xlinker"
1619 and then J < Linker_Options.Last
1620 and then Linker_Options.Table (J + 1)'Length > 8
1621 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
1624 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
1625 Linker_Options.Table (J + 2 .. Linker_Options.Last);
1626 Linker_Options.Decrement_Last;
1627 Linker_Options.Decrement_Last;
1628 Num_Args := Num_Args - 2;
1635 -- Here we just check for a canonical form that matches the
1636 -- pragma Linker_Options set in the NT runtime.
1638 if (Linker_Options.Table (J)'Length > 17
1639 and then Linker_Options.Table (J) (1 .. 17)
1640 = "-Xlinker --stack=")
1642 (Linker_Options.Table (J)'Length > 12
1643 and then Linker_Options.Table (J) (1 .. 12)
1647 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1648 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1649 Linker_Options.Decrement_Last;
1650 Num_Args := Num_Args - 1;
1657 -- Remove duplicate IDENTIFICATION directives (VMS)
1659 if Linker_Options.Table (J)'Length > 27
1660 and then Linker_Options.Table (J) (1 .. 28)
1661 = "--for-linker=IDENTIFICATION="
1664 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1665 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1666 Linker_Options.Decrement_Last;
1667 Num_Args := Num_Args - 1;
1675 end Clean_Link_Option_Set;
1677 -- Prepare arguments for call to linker
1679 Call_Linker : declare
1681 Args : Argument_List (1 .. Num_Args + 1);
1682 Index : Integer := Args'First;
1685 Args (Index) := Binder_Obj_File;
1687 -- Add the object files and any -largs libraries
1689 for J in Linker_Objects.First .. Linker_Objects.Last loop
1691 Args (Index) := Linker_Objects.Table (J);
1694 -- Add the linker options from the binder file
1696 for J in Linker_Options.First .. Linker_Options.Last loop
1698 Args (Index) := Linker_Options.Table (J);
1701 -- Finally add the libraries from the --GCC= switch
1703 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
1705 Args (Index) := Gcc_Linker_Options.Table (J);
1708 if Verbose_Mode then
1709 Write_Str (Linker_Path.all);
1711 for J in Args'Range loop
1713 Write_Str (Args (J).all);
1718 -- If we are on very verbose mode (-v -v) and a response file
1719 -- is used we display its content.
1721 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
1723 Write_Str ("Response file (" &
1724 Tname (Tname'First .. Tname'Last - 1) &
1729 Response_File_Objects.First ..
1730 Response_File_Objects.Last
1732 Write_Str (Response_File_Objects.Table (J).all);
1740 GNAT.OS_Lib.Spawn (Linker_Path.all, Args, Success);
1742 -- Delete the temporary file used in conjuction with linking if
1743 -- one was created. See Process_Bind_File for details.
1745 if Tname_FD /= Invalid_FD then
1750 Error_Msg ("cannot call " & Linker_Path.all);
1751 Exit_Program (E_Fatal);
1757 -- Only keep the binder output file and it's associated object
1758 -- file if compiling with the -g option. These files are only
1759 -- useful if debugging.
1761 if not Debug_Flag_Present then
1762 if Binder_Ali_File /= null then
1763 Delete (Binder_Ali_File.all & ASCII.NUL);
1766 if Binder_Spec_Src_File /= null then
1767 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
1770 Delete (Binder_Body_Src_File.all & ASCII.NUL);
1772 if not Hostparm.Java_VM then
1773 Delete (Binder_Obj_File.all & ASCII.NUL);
1777 Exit_Program (E_Success);
1781 Write_Line (Exception_Information (X));
1782 Exit_With_Error ("INTERNAL ERROR. Please report");