* gcc.target/powerpc/builtins-1-le.c: Filter out gimple folding disabled
[official-gcc.git] / gcc / ada / gnatlink.adb
blobe28ba34c443c21f1a3609147846201ad00ac1d88
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T L I N K --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1996-2018, Free Software Foundation, Inc. --
10 -- --
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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 -- Gnatlink usage: please consult the gnat documentation
28 with ALI; use ALI;
29 with Csets;
30 with Gnatvsn; use Gnatvsn;
31 with Indepsw; use Indepsw;
32 with Namet; use Namet;
33 with Opt;
34 with Osint; use Osint;
35 with Output; use Output;
36 with Snames;
37 with Switch; use Switch;
38 with System; use System;
39 with Table;
40 with Targparm;
41 with Types;
43 with Ada.Command_Line; use Ada.Command_Line;
44 with Ada.Exceptions; use Ada.Exceptions;
46 with System.OS_Lib; use System.OS_Lib;
47 with System.CRTL;
49 with Interfaces.C_Streams; use Interfaces.C_Streams;
50 with Interfaces.C.Strings; use Interfaces.C.Strings;
52 procedure Gnatlink is
53 pragma Ident (Gnatvsn.Gnat_Static_Version_String);
55 Shared_Libgcc_String : constant String := "-shared-libgcc";
56 Shared_Libgcc : constant String_Access :=
57 new String'(Shared_Libgcc_String);
58 -- Used to invoke gcc when the binder is invoked with -shared
60 Static_Libgcc_String : constant String := "-static-libgcc";
61 Static_Libgcc : constant String_Access :=
62 new String'(Static_Libgcc_String);
63 -- Used to invoke gcc when shared libs are not used
65 package Gcc_Linker_Options is new Table.Table (
66 Table_Component_Type => String_Access,
67 Table_Index_Type => Integer,
68 Table_Low_Bound => 1,
69 Table_Initial => 20,
70 Table_Increment => 100,
71 Table_Name => "Gnatlink.Gcc_Linker_Options");
72 -- Comments needed ???
74 package Libpath is new Table.Table (
75 Table_Component_Type => Character,
76 Table_Index_Type => Integer,
77 Table_Low_Bound => 1,
78 Table_Initial => 4096,
79 Table_Increment => 100,
80 Table_Name => "Gnatlink.Libpath");
81 -- Comments needed ???
83 package Linker_Options is new Table.Table (
84 Table_Component_Type => String_Access,
85 Table_Index_Type => Integer,
86 Table_Low_Bound => 1,
87 Table_Initial => 20,
88 Table_Increment => 100,
89 Table_Name => "Gnatlink.Linker_Options");
90 -- Comments needed ???
92 package Linker_Objects is new Table.Table (
93 Table_Component_Type => String_Access,
94 Table_Index_Type => Integer,
95 Table_Low_Bound => 1,
96 Table_Initial => 20,
97 Table_Increment => 100,
98 Table_Name => "Gnatlink.Linker_Objects");
99 -- This table collects the objects file to be passed to the linker. In the
100 -- case where the linker command line is too long then programs objects
101 -- are put on the Response_File_Objects table. Note that the binder object
102 -- file and the user's objects remain in this table. This is very
103 -- important because on the GNU linker command line the -L switch is not
104 -- used to look for objects files but -L switch is used to look for
105 -- objects listed in the response file. This is not a problem with the
106 -- applications objects as they are specified with a full name.
108 package Response_File_Objects is new Table.Table (
109 Table_Component_Type => String_Access,
110 Table_Index_Type => Integer,
111 Table_Low_Bound => 1,
112 Table_Initial => 20,
113 Table_Increment => 100,
114 Table_Name => "Gnatlink.Response_File_Objects");
115 -- This table collects the objects file that are to be put in the response
116 -- file. Only application objects are collected there (see details in
117 -- Linker_Objects table comments)
119 package Binder_Options_From_ALI is new Table.Table (
120 Table_Component_Type => String_Access,
121 Table_Index_Type => Integer,
122 Table_Low_Bound => 1, -- equals low bound of Argument_List for Spawn
123 Table_Initial => 20,
124 Table_Increment => 100,
125 Table_Name => "Gnatlink.Binder_Options_From_ALI");
126 -- This table collects the switches from the ALI file of the main
127 -- subprogram.
129 package Binder_Options is new Table.Table (
130 Table_Component_Type => String_Access,
131 Table_Index_Type => Integer,
132 Table_Low_Bound => 1, -- equals low bound of Argument_List for Spawn
133 Table_Initial => 20,
134 Table_Increment => 100,
135 Table_Name => "Gnatlink.Binder_Options");
136 -- This table collects the arguments to be passed to compile the binder
137 -- generated file.
139 Gcc : String_Access := Program_Name ("gcc", "gnatlink");
141 Read_Mode : constant String := "r" & ASCII.NUL;
143 Begin_Info : constant String := "-- BEGIN Object file/option list";
144 End_Info : constant String := "-- END Object file/option list ";
146 Gcc_Path : String_Access;
147 Linker_Path : String_Access;
148 Output_File_Name : String_Access;
149 Ali_File_Name : String_Access;
150 Binder_Spec_Src_File : String_Access;
151 Binder_Body_Src_File : String_Access;
152 Binder_Ali_File : String_Access;
153 Binder_Obj_File : String_Access;
155 Base_Command_Name : String_Access;
157 Target_Debuggable_Suffix : String_Access;
159 Tname : Temp_File_Name;
160 Tname_FD : File_Descriptor := Invalid_FD;
161 -- Temporary file used by linker to pass list of object files on
162 -- certain systems with limitations on size of arguments.
164 Debug_Flag_Present : Boolean := False;
165 Verbose_Mode : Boolean := False;
166 Very_Verbose_Mode : Boolean := False;
168 Standard_Gcc : Boolean := True;
170 Compile_Bind_File : Boolean := True;
171 -- Set to False if bind file is not to be compiled
173 Create_Map_File : Boolean := False;
174 -- Set to True by switch -M. The map file name is derived from
175 -- the ALI file name (mainprog.ali => mainprog.map).
177 Object_List_File_Supported : Boolean;
178 for Object_List_File_Supported'Size use Character'Size;
179 pragma Import
180 (C, Object_List_File_Supported, "__gnat_objlist_file_supported");
181 -- Predicate indicating whether the linker has an option whereby the
182 -- names of object files can be passed to the linker in a file.
184 Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
185 pragma Import (C, Object_File_Option_Ptr, "__gnat_object_file_option");
186 -- Pointer to a string representing the linker option which specifies
187 -- the response file.
189 Object_File_Option : constant String := Value (Object_File_Option_Ptr);
190 -- The linker option which specifies the response file as a string
192 Using_GNU_response_file : constant Boolean :=
193 Object_File_Option'Length > 0
194 and then Object_File_Option (Object_File_Option'Last) = '@';
195 -- Whether a GNU response file is used
197 Object_List_File_Required : Boolean := False;
198 -- Set to True to force generation of a response file
200 Shared_Libgcc_Default : Character;
201 for Shared_Libgcc_Default'Size use Character'Size;
202 pragma Import
203 (C, Shared_Libgcc_Default, "__gnat_shared_libgcc_default");
204 -- Indicates wether libgcc should be statically linked (use 'T') or
205 -- dynamically linked (use 'H') by default.
207 function Base_Name (File_Name : String) return String;
208 -- Return just the file name part without the extension (if present)
210 procedure Check_Existing_Executable (File_Name : String);
211 -- Delete any existing executable to avoid accidentally updating the target
212 -- of a symbolic link, but produce a Fatail_Error if File_Name matches any
213 -- of the source file names. This avoids overwriting of extensionless
214 -- source files by accident on systems where executables do not have
215 -- extensions.
217 procedure Delete (Name : String);
218 -- Wrapper to unlink as status is ignored by this application
220 procedure Error_Msg (Message : String);
221 -- Output the error or warning Message
223 procedure Exit_With_Error (Error : String);
224 -- Output Error and exit program with a fatal condition
226 procedure Process_Args;
227 -- Go through all the arguments and build option tables
229 procedure Process_Binder_File (Name : String);
230 -- Reads the binder file and extracts linker arguments
232 procedure Usage;
233 -- Display usage
235 procedure Write_Header;
236 -- Show user the program name, version and copyright
238 procedure Write_Usage;
239 -- Show user the program options
241 ---------------
242 -- Base_Name --
243 ---------------
245 function Base_Name (File_Name : String) return String is
246 Findex1 : Natural;
247 Findex2 : Natural;
249 begin
250 Findex1 := File_Name'First;
252 -- The file might be specified by a full path name. However,
253 -- we want the path to be stripped away.
255 for J in reverse File_Name'Range loop
256 if Is_Directory_Separator (File_Name (J)) then
257 Findex1 := J + 1;
258 exit;
259 end if;
260 end loop;
262 Findex2 := File_Name'Last;
263 while Findex2 > Findex1 and then File_Name (Findex2) /= '.' loop
264 Findex2 := Findex2 - 1;
265 end loop;
267 if Findex2 = Findex1 then
268 Findex2 := File_Name'Last + 1;
269 end if;
271 return File_Name (Findex1 .. Findex2 - 1);
272 end Base_Name;
274 -------------------------------
275 -- Check_Existing_Executable --
276 -------------------------------
278 procedure Check_Existing_Executable (File_Name : String) is
279 Ename : String := File_Name;
280 Efile : File_Name_Type;
281 Sfile : File_Name_Type;
283 begin
284 Canonical_Case_File_Name (Ename);
285 Name_Len := 0;
286 Add_Str_To_Name_Buffer (Ename);
287 Efile := Name_Find;
289 for J in Units.Table'First .. Units.Last loop
290 Sfile := Units.Table (J).Sfile;
291 if Sfile = Efile then
292 Exit_With_Error
293 ("executable name """ & File_Name & """ matches "
294 & "source file name """ & Get_Name_String (Sfile) & """");
295 end if;
296 end loop;
298 Delete (File_Name);
299 end Check_Existing_Executable;
301 ------------
302 -- Delete --
303 ------------
305 procedure Delete (Name : String) is
306 Status : int;
307 pragma Unreferenced (Status);
308 begin
309 Status := unlink (Name'Address);
310 -- Is it really right to ignore an error here ???
311 end Delete;
313 ---------------
314 -- Error_Msg --
315 ---------------
317 procedure Error_Msg (Message : String) is
318 begin
319 Write_Str (Base_Command_Name.all);
320 Write_Str (": ");
321 Write_Str (Message);
322 Write_Eol;
323 end Error_Msg;
325 ---------------------
326 -- Exit_With_Error --
327 ---------------------
329 procedure Exit_With_Error (Error : String) is
330 begin
331 Error_Msg (Error);
332 Exit_Program (E_Fatal);
333 end Exit_With_Error;
335 ------------------
336 -- Process_Args --
337 ------------------
339 procedure Process_Args is
340 Next_Arg : Integer;
342 Skip_Next : Boolean := False;
343 -- Set to true if the next argument is to be added into the list of
344 -- linker's argument without parsing it.
346 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
348 -- Start of processing for Process_Args
350 begin
351 -- First, check for --version and --help
353 Check_Version_And_Help ("GNATLINK", "1996");
355 -- Loop through arguments of gnatlink command
357 Next_Arg := 1;
358 loop
359 exit when Next_Arg > Argument_Count;
361 Process_One_Arg : declare
362 Arg : constant String := Argument (Next_Arg);
364 begin
365 -- Case of argument which is a switch
367 -- We definitely need section by section comments here ???
369 if Skip_Next then
371 -- This argument must not be parsed, just add it to the
372 -- list of linker's options.
374 Skip_Next := False;
376 Linker_Options.Increment_Last;
377 Linker_Options.Table (Linker_Options.Last) :=
378 new String'(Arg);
380 elsif Arg'Length /= 0 and then Arg (1) = '-' then
381 if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then
382 Exit_With_Error
383 ("invalid switch: """ & Arg & """ (gnat not needed here)");
384 end if;
386 if Arg = "-Xlinker" then
388 -- Next argument should be sent directly to the linker.
389 -- We do not want to parse it here.
391 Skip_Next := True;
393 Linker_Options.Increment_Last;
394 Linker_Options.Table (Linker_Options.Last) :=
395 new String'(Arg);
397 elsif Arg (2) = 'g'
398 and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
399 then
400 Debug_Flag_Present := True;
402 Linker_Options.Increment_Last;
403 Linker_Options.Table (Linker_Options.Last) :=
404 new String'(Arg);
406 Binder_Options.Increment_Last;
407 Binder_Options.Table (Binder_Options.Last) :=
408 Linker_Options.Table (Linker_Options.Last);
410 elsif Arg'Length >= 3 and then Arg (2) = 'M' then
411 declare
412 Switches : String_List_Access;
414 begin
415 Convert (Map_File, Arg (3 .. Arg'Last), Switches);
417 if Switches /= null then
418 for J in Switches'Range loop
419 Linker_Options.Increment_Last;
420 Linker_Options.Table (Linker_Options.Last) :=
421 Switches (J);
422 end loop;
423 end if;
424 end;
426 elsif Arg'Length = 2 then
427 case Arg (2) is
428 when 'b' =>
429 Linker_Options.Increment_Last;
430 Linker_Options.Table (Linker_Options.Last) :=
431 new String'(Arg);
433 Binder_Options.Increment_Last;
434 Binder_Options.Table (Binder_Options.Last) :=
435 Linker_Options.Table (Linker_Options.Last);
437 Next_Arg := Next_Arg + 1;
439 if Next_Arg > Argument_Count then
440 Exit_With_Error ("Missing argument for -b");
441 end if;
443 Get_Machine_Name : declare
444 Name_Arg : constant String_Access :=
445 new String'(Argument (Next_Arg));
447 begin
448 Linker_Options.Increment_Last;
449 Linker_Options.Table (Linker_Options.Last) :=
450 Name_Arg;
452 Binder_Options.Increment_Last;
453 Binder_Options.Table (Binder_Options.Last) :=
454 Name_Arg;
456 end Get_Machine_Name;
458 when 'f' =>
459 if Object_List_File_Supported then
460 Object_List_File_Required := True;
461 else
462 Exit_With_Error
463 ("Object list file not supported on this target");
464 end if;
466 when 'M' =>
467 Create_Map_File := True;
469 when 'n' =>
470 Compile_Bind_File := False;
472 when 'o' =>
473 Next_Arg := Next_Arg + 1;
475 if Next_Arg > Argument_Count then
476 Exit_With_Error ("Missing argument for -o");
477 end if;
479 Output_File_Name :=
480 new String'(Executable_Name
481 (Argument (Next_Arg),
482 Only_If_No_Suffix => True));
484 when 'P' =>
485 Opt.CodePeer_Mode := True;
487 when 'R' =>
488 Opt.Run_Path_Option := False;
490 when 'v' =>
492 -- Support "double" verbose mode. Second -v
493 -- gets sent to the linker and binder phases.
495 if Verbose_Mode then
496 Very_Verbose_Mode := True;
498 Linker_Options.Increment_Last;
499 Linker_Options.Table (Linker_Options.Last) :=
500 new String'(Arg);
502 Binder_Options.Increment_Last;
503 Binder_Options.Table (Binder_Options.Last) :=
504 Linker_Options.Table (Linker_Options.Last);
506 else
507 Verbose_Mode := True;
509 end if;
511 when others =>
512 Linker_Options.Increment_Last;
513 Linker_Options.Table (Linker_Options.Last) :=
514 new String'(Arg);
516 end case;
518 elsif Arg (2) = 'B' then
519 Linker_Options.Increment_Last;
520 Linker_Options.Table (Linker_Options.Last) :=
521 new String'(Arg);
523 Binder_Options.Increment_Last;
524 Binder_Options.Table (Binder_Options.Last) :=
525 Linker_Options.Table (Linker_Options.Last);
527 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
528 if Arg'Length = 7 then
529 Exit_With_Error ("Missing argument for --LINK=");
530 end if;
532 declare
533 L_Args : constant Argument_List_Access :=
534 Argument_String_To_List (Arg (8 .. Arg'Last));
535 begin
536 -- The linker program is the first argument
538 Linker_Path :=
539 System.OS_Lib.Locate_Exec_On_Path (L_Args.all (1).all);
541 if Linker_Path = null then
542 Exit_With_Error
543 ("Could not locate linker: " & L_Args.all (1).all);
544 end if;
546 -- The other arguments are passed as-is to the linker
548 for J in 2 .. L_Args.all'Last loop
549 Gcc_Linker_Options.Increment_Last;
550 Gcc_Linker_Options.Table
551 (Gcc_Linker_Options.Last) :=
552 new String'(L_Args.all (J).all);
553 end loop;
554 end;
556 elsif Arg'Length >= 6 and then Arg (1 .. 6) = "--GCC=" then
557 if Arg'Length = 6 then
558 Exit_With_Error ("Missing argument for --GCC=");
559 end if;
561 declare
562 Program_Args : constant Argument_List_Access :=
563 Argument_String_To_List
564 (Arg (7 .. Arg'Last));
566 begin
567 if Program_Args.all (1).all /= Gcc.all then
568 Gcc := new String'(Program_Args.all (1).all);
569 Standard_Gcc := False;
570 end if;
572 -- Set appropriate flags for switches passed
574 for J in 2 .. Program_Args.all'Last loop
575 declare
576 Arg : constant String := Program_Args.all (J).all;
577 AF : constant Integer := Arg'First;
579 begin
580 if Arg'Length /= 0 and then Arg (AF) = '-' then
581 if Arg (AF + 1) = 'g'
582 and then (Arg'Length = 2
583 or else Arg (AF + 2) in '0' .. '3'
584 or else Arg (AF + 2 .. Arg'Last) = "coff")
585 then
586 Debug_Flag_Present := True;
587 end if;
588 end if;
590 -- Add directory to source search dirs so that
591 -- Get_Target_Parameters can find system.ads
593 if Arg (AF .. AF + 1) = "-I"
594 and then Arg'Length > 2
595 then
596 Add_Src_Search_Dir (Arg (AF + 2 .. Arg'Last));
597 end if;
599 -- Pass to gcc for compiling binder generated file
600 -- No use passing libraries, it will just generate
601 -- a warning
603 if not (Arg (AF .. AF + 1) = "-l"
604 or else Arg (AF .. AF + 1) = "-L")
605 then
606 Binder_Options.Increment_Last;
607 Binder_Options.Table (Binder_Options.Last) :=
608 new String'(Arg);
609 end if;
611 -- Pass to gcc for linking program
613 Gcc_Linker_Options.Increment_Last;
614 Gcc_Linker_Options.Table
615 (Gcc_Linker_Options.Last) := new String'(Arg);
616 end;
617 end loop;
618 end;
620 -- Send all multi-character switches not recognized as
621 -- a special case by gnatlink to the linker/loader stage.
623 else
624 Linker_Options.Increment_Last;
625 Linker_Options.Table (Linker_Options.Last) :=
626 new String'(Arg);
627 end if;
629 -- Here if argument is a file name rather than a switch
631 else
632 -- If explicit ali file, capture it
634 if Arg'Length > 4
635 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
636 then
637 if Ali_File_Name = null then
638 Ali_File_Name := new String'(Arg);
639 else
640 Exit_With_Error ("cannot handle more than one ALI file");
641 end if;
643 -- If target object file, record object file
645 elsif Arg'Length > Get_Target_Object_Suffix.all'Length
646 and then Arg
647 (Arg'Last -
648 Get_Target_Object_Suffix.all'Length + 1 .. Arg'Last)
649 = Get_Target_Object_Suffix.all
650 then
651 Linker_Objects.Increment_Last;
652 Linker_Objects.Table (Linker_Objects.Last) :=
653 new String'(Arg);
655 -- If host object file, record object file
657 elsif Arg'Length > Get_Object_Suffix.all'Length
658 and then Arg
659 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
660 = Get_Object_Suffix.all
661 then
662 Linker_Objects.Increment_Last;
663 Linker_Objects.Table (Linker_Objects.Last) :=
664 new String'(Arg);
666 -- If corresponding ali file exists, capture it
668 elsif Ali_File_Name = null
669 and then Is_Regular_File (Arg & ".ali")
670 then
671 Ali_File_Name := new String'(Arg & ".ali");
673 -- Otherwise assume this is a linker options entry, but
674 -- see below for interesting adjustment to this assumption.
676 else
677 Linker_Options.Increment_Last;
678 Linker_Options.Table (Linker_Options.Last) :=
679 new String'(Arg);
680 end if;
681 end if;
682 end Process_One_Arg;
684 Next_Arg := Next_Arg + 1;
685 end loop;
687 -- Compile the bind file with warnings suppressed, because
688 -- otherwise the with of the main program may cause junk warnings.
690 Binder_Options.Increment_Last;
691 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
693 -- If we did not get an ali file at all, and we had at least one
694 -- linker option, then assume that was the intended ali file after
695 -- all, so that we get a nicer message later on.
697 if Ali_File_Name = null
698 and then Linker_Options.Last >= Linker_Options.First
699 then
700 Ali_File_Name :=
701 new String'(Linker_Options.Table (Linker_Options.First).all
702 & ".ali");
703 end if;
704 end Process_Args;
706 -------------------------
707 -- Process_Binder_File --
708 -------------------------
710 procedure Process_Binder_File (Name : String) is
711 Fd : FILEs;
712 -- Binder file's descriptor
714 Link_Bytes : Integer := 0;
715 -- Projected number of bytes for the linker command line
717 Link_Max : Integer;
718 pragma Import (C, Link_Max, "__gnat_link_max");
719 -- Maximum number of bytes on the command line supported by the OS
720 -- linker. Passed this limit the response file mechanism must be used
721 -- if supported.
723 Next_Line : String (1 .. 1000);
724 -- Current line value
726 Nlast : Integer;
727 Nfirst : Integer;
728 -- Current line slice (the slice does not contain line terminator)
730 Last : Integer;
731 -- Current line last character for shared libraries (without version)
733 Objs_Begin : Integer := 0;
734 -- First object file index in Linker_Objects table
736 Objs_End : Integer := 0;
737 -- Last object file index in Linker_Objects table
739 Status : int;
740 pragma Warnings (Off, Status);
741 -- Used for various Interfaces.C_Streams calls
743 Closing_Status : Boolean;
744 pragma Warnings (Off, Closing_Status);
745 -- For call to Close
747 GNAT_Static : Boolean := False;
748 -- Save state of -static option
750 GNAT_Shared : Boolean := False;
751 -- Save state of -shared option
753 Xlinker_Was_Previous : Boolean := False;
754 -- Indicate that "-Xlinker" was the option preceding the current option.
755 -- If True, then the current option is never suppressed.
757 -- Rollback data
759 -- These data items are used to store current binder file context. The
760 -- context is composed of the file descriptor position and the current
761 -- line together with the slice indexes (first and last position) for
762 -- this line. The rollback data are used by the Store_File_Context and
763 -- Rollback_File_Context routines below. The file context mechanism
764 -- interact only with the Get_Next_Line call. For example:
766 -- Store_File_Context;
767 -- Get_Next_Line;
768 -- Rollback_File_Context;
769 -- Get_Next_Line;
771 -- Both Get_Next_Line calls above will read the exact same data from
772 -- the file. In other words, Next_Line, Nfirst and Nlast variables
773 -- will be set with the exact same values.
775 RB_File_Pos : long; -- File position
776 RB_Next_Line : String (1 .. 1000); -- Current line content
777 RB_Nlast : Integer; -- Slice last index
778 RB_Nfirst : Integer; -- Slice first index
780 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
781 pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
782 -- Pointer to string representing the native linker option which
783 -- specifies the path where the dynamic loader should find shared
784 -- libraries. Equal to null string if this system doesn't support it.
786 Libgcc_Subdir_Ptr : Interfaces.C.Strings.chars_ptr;
787 pragma Import (C, Libgcc_Subdir_Ptr, "__gnat_default_libgcc_subdir");
788 -- Pointer to string indicating the installation subdirectory where
789 -- a default shared libgcc might be found.
791 Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
792 pragma Import
793 (C, Object_Library_Ext_Ptr, "__gnat_object_library_extension");
794 -- Pointer to string specifying the default extension for
795 -- object libraries, e.g. Unix uses ".a".
797 Separate_Run_Path_Options : Boolean;
798 for Separate_Run_Path_Options'Size use Character'Size;
799 pragma Import
800 (C, Separate_Run_Path_Options, "__gnat_separate_run_path_options");
801 -- Whether separate rpath options should be emitted for each directory
803 procedure Get_Next_Line;
804 -- Read the next line from the binder file without the line
805 -- terminator.
807 function Index (S, Pattern : String) return Natural;
808 -- Return the last occurrence of Pattern in S, or 0 if none
810 procedure Store_File_Context;
811 -- Store current file context, Fd position and current line data.
812 -- The file context is stored into the rollback data above (RB_*).
813 -- Store_File_Context can be called at any time, only the last call
814 -- will be used (i.e. this routine overwrites the file context).
816 procedure Rollback_File_Context;
817 -- Restore file context from rollback data. This routine must be called
818 -- after Store_File_Context. The binder file context will be restored
819 -- with the data stored by the last Store_File_Context call.
821 procedure Write_RF (S : String);
822 -- Write a string to the response file and check if it was successful.
823 -- Fail the program if it was not successful (disk full).
825 -------------------
826 -- Get_Next_Line --
827 -------------------
829 procedure Get_Next_Line is
830 Fchars : chars;
832 begin
833 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
835 if Fchars = System.Null_Address then
836 Exit_With_Error ("Error reading binder output");
837 end if;
839 Nfirst := Next_Line'First;
840 Nlast := Nfirst;
841 while Nlast <= Next_Line'Last
842 and then Next_Line (Nlast) /= ASCII.LF
843 and then Next_Line (Nlast) /= ASCII.CR
844 loop
845 Nlast := Nlast + 1;
846 end loop;
848 Nlast := Nlast - 1;
849 end Get_Next_Line;
851 -----------
852 -- Index --
853 -----------
855 function Index (S, Pattern : String) return Natural is
856 Len : constant Natural := Pattern'Length;
858 begin
859 for J in reverse S'First .. S'Last - Len + 1 loop
860 if Pattern = S (J .. J + Len - 1) then
861 return J;
862 end if;
863 end loop;
865 return 0;
866 end Index;
868 ---------------------------
869 -- Rollback_File_Context --
870 ---------------------------
872 procedure Rollback_File_Context is
873 begin
874 Next_Line := RB_Next_Line;
875 Nfirst := RB_Nfirst;
876 Nlast := RB_Nlast;
877 Status := fseek (Fd, RB_File_Pos, Interfaces.C_Streams.SEEK_SET);
879 if Status = -1 then
880 Exit_With_Error ("Error setting file position");
881 end if;
882 end Rollback_File_Context;
884 ------------------------
885 -- Store_File_Context --
886 ------------------------
888 procedure Store_File_Context is
889 use type System.CRTL.long;
891 begin
892 RB_Next_Line := Next_Line;
893 RB_Nfirst := Nfirst;
894 RB_Nlast := Nlast;
895 RB_File_Pos := ftell (Fd);
897 if RB_File_Pos = -1 then
898 Exit_With_Error ("Error getting file position");
899 end if;
900 end Store_File_Context;
902 --------------
903 -- Write_RF --
904 --------------
906 procedure Write_RF (S : String) is
907 Success : Boolean := True;
908 Back_Slash : constant Character := '\';
910 begin
911 -- If a GNU response file is used, space and backslash need to be
912 -- escaped because they are interpreted as a string separator and
913 -- an escape character respectively by the underlying mechanism.
914 -- On the other hand, quote and double-quote are not escaped since
915 -- they are interpreted as string delimiters on both sides.
917 if Using_GNU_response_file then
918 for J in S'Range loop
919 if S (J) = ' ' or else S (J) = '\' then
920 if Write (Tname_FD, Back_Slash'Address, 1) /= 1 then
921 Success := False;
922 end if;
923 end if;
925 if Write (Tname_FD, S (J)'Address, 1) /= 1 then
926 Success := False;
927 end if;
928 end loop;
930 else
931 if Write (Tname_FD, S'Address, S'Length) /= S'Length then
932 Success := False;
933 end if;
934 end if;
936 if Write (Tname_FD, ASCII.LF'Address, 1) /= 1 then
937 Success := False;
938 end if;
940 if not Success then
941 Exit_With_Error ("Error generating response file: disk full");
942 end if;
943 end Write_RF;
945 -- Start of processing for Process_Binder_File
947 begin
948 Fd := fopen (Name'Address, Read_Mode'Address);
950 if Fd = NULL_Stream then
951 Exit_With_Error ("Failed to open binder output");
952 end if;
954 -- Skip up to the Begin Info line
956 loop
957 Get_Next_Line;
958 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
959 end loop;
961 loop
962 Get_Next_Line;
964 -- Go to end when end line is reached (this will happen in
965 -- High_Integrity_Mode where no -L switches are generated)
967 exit when Next_Line (Nfirst .. Nlast) = End_Info;
969 Next_Line (Nfirst .. Nlast - 8) := Next_Line (Nfirst + 8 .. Nlast);
970 Nlast := Nlast - 8;
972 -- Go to next section when switches are reached
974 exit when Next_Line (1) = '-';
976 -- Otherwise we have another object file to collect
978 Linker_Objects.Increment_Last;
980 -- Mark the positions of first and last object files in case they
981 -- need to be placed with a named file on systems having linker
982 -- line limitations.
984 if Objs_Begin = 0 then
985 Objs_Begin := Linker_Objects.Last;
986 end if;
988 Linker_Objects.Table (Linker_Objects.Last) :=
989 new String'(Next_Line (Nfirst .. Nlast));
991 -- Nlast - Nfirst + 1, for the size, plus one for the space between
992 -- each arguments.
994 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
995 end loop;
997 Objs_End := Linker_Objects.Last;
999 -- Continue to compute the Link_Bytes, the linker options are part of
1000 -- command line length.
1002 Store_File_Context;
1004 while Next_Line (Nfirst .. Nlast) /= End_Info loop
1005 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
1006 Get_Next_Line;
1007 end loop;
1009 Rollback_File_Context;
1011 -- On systems that have limitations on handling very long linker lines
1012 -- we make use of the system linker option which takes a list of object
1013 -- file names from a file instead of the command line itself. What we do
1014 -- is to replace the list of object files by the special linker option
1015 -- which then reads the object file list from a file instead. The option
1016 -- to read from a file instead of the command line is only triggered if
1017 -- a conservative threshold is passed.
1019 if Object_List_File_Required
1020 or else (Object_List_File_Supported
1021 and then Link_Bytes > Link_Max)
1022 then
1023 -- Create a temporary file containing the Ada user object files
1024 -- needed by the link. This list is taken from the bind file and is
1025 -- output one object per line for maximal compatibility with linkers
1026 -- supporting this option.
1028 Create_Temp_File (Tname_FD, Tname);
1030 -- ??? File descriptor should be checked to not be Invalid_FD.
1031 -- ??? Status of Write and Close operations should be checked, and
1032 -- failure should occur if a status is wrong.
1034 for J in Objs_Begin .. Objs_End loop
1035 Write_RF (Linker_Objects.Table (J).all);
1037 Response_File_Objects.Increment_Last;
1038 Response_File_Objects.Table (Response_File_Objects.Last) :=
1039 Linker_Objects.Table (J);
1040 end loop;
1042 Close (Tname_FD, Closing_Status);
1044 -- Add the special objects list file option together with the name
1045 -- of the temporary file (removing the null character) to the objects
1046 -- file table.
1048 Linker_Objects.Table (Objs_Begin) :=
1049 new String'(Object_File_Option &
1050 Tname (Tname'First .. Tname'Last - 1));
1052 -- The slots containing these object file names are then removed
1053 -- from the objects table so they do not appear in the link. They are
1054 -- removed by moving up the linker options and non-Ada object files
1055 -- appearing after the Ada object list in the table.
1057 declare
1058 N : Integer;
1060 begin
1061 N := Objs_End - Objs_Begin + 1;
1063 for J in Objs_End + 1 .. Linker_Objects.Last loop
1064 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
1065 end loop;
1067 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
1068 end;
1069 end if;
1071 -- Process switches and options
1073 if Next_Line (Nfirst .. Nlast) /= End_Info then
1074 Xlinker_Was_Previous := False;
1076 loop
1077 if Xlinker_Was_Previous
1078 or else Next_Line (Nfirst .. Nlast) = "-Xlinker"
1079 then
1080 Linker_Options.Increment_Last;
1081 Linker_Options.Table (Linker_Options.Last) :=
1082 new String'(Next_Line (Nfirst .. Nlast));
1084 elsif Next_Line (Nfirst .. Nlast) = "-static" then
1085 GNAT_Static := True;
1087 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1088 GNAT_Shared := True;
1090 -- Add binder options only if not already set on the command line.
1091 -- This rule is a way to control the linker options order.
1093 else
1094 if Nlast > Nfirst + 2 and then
1095 Next_Line (Nfirst .. Nfirst + 1) = "-L"
1096 then
1097 -- Construct a library search path for use later to locate
1098 -- static gnatlib libraries.
1100 if Libpath.Last > 1 then
1101 Libpath.Increment_Last;
1102 Libpath.Table (Libpath.Last) := Path_Separator;
1103 end if;
1105 for I in Nfirst + 2 .. Nlast loop
1106 Libpath.Increment_Last;
1107 Libpath.Table (Libpath.Last) := Next_Line (I);
1108 end loop;
1110 Linker_Options.Increment_Last;
1112 Linker_Options.Table (Linker_Options.Last) :=
1113 new String'(Next_Line (Nfirst .. Nlast));
1115 elsif Next_Line (Nfirst .. Nlast) = "-lgnarl"
1116 or else Next_Line (Nfirst .. Nlast) = "-lgnat"
1117 or else
1118 Next_Line
1119 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) =
1120 Shared_Lib ("gnarl")
1121 or else
1122 Next_Line
1123 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) =
1124 Shared_Lib ("gnat")
1125 then
1126 -- If it is a shared library, remove the library version.
1127 -- We will be looking for the static version of the library
1128 -- as it is in the same directory as the shared version.
1130 if Next_Line (Nlast - Library_Version'Length + 1 .. Nlast) =
1131 Library_Version
1132 then
1133 -- Set Last to point to last character before the
1134 -- library version.
1136 Last := Nlast - Library_Version'Length - 1;
1137 else
1138 Last := Nlast;
1139 end if;
1141 -- Given a Gnat standard library, search the library path to
1142 -- find the library location.
1144 -- Shouldn't we abstract a proc here, we are getting awfully
1145 -- heavily nested ???
1147 declare
1148 File_Path : String_Access;
1150 Object_Lib_Extension : constant String :=
1151 Value (Object_Library_Ext_Ptr);
1153 File_Name : constant String := "lib" &
1154 Next_Line (Nfirst + 2 .. Last) & Object_Lib_Extension;
1156 Run_Path_Opt : constant String :=
1157 Value (Run_Path_Option_Ptr);
1159 GCC_Index : Natural;
1160 Run_Path_Opt_Index : Natural := 0;
1162 begin
1163 File_Path :=
1164 Locate_Regular_File (File_Name,
1165 String (Libpath.Table (1 .. Libpath.Last)));
1167 if File_Path /= null then
1168 if GNAT_Static then
1170 -- If static gnatlib found, explicitly specify to
1171 -- overcome possible linker default usage of shared
1172 -- version.
1174 Linker_Options.Increment_Last;
1176 Linker_Options.Table (Linker_Options.Last) :=
1177 new String'(File_Path.all);
1179 elsif GNAT_Shared then
1180 if Opt.Run_Path_Option then
1182 -- If shared gnatlib desired, add appropriate
1183 -- system specific switch so that it can be
1184 -- located at runtime.
1186 if Run_Path_Opt'Length /= 0 then
1188 -- Output the system specific linker command
1189 -- that allows the image activator to find
1190 -- the shared library at runtime. Also add
1191 -- path to find libgcc_s.so, if relevant.
1193 declare
1194 Path : String (1 .. File_Path'Length + 15);
1196 Path_Last : constant Natural :=
1197 File_Path'Length;
1199 begin
1200 Path (1 .. File_Path'Length) :=
1201 File_Path.all;
1203 -- To find the location of the shared version
1204 -- of libgcc, we look for "gcc-lib" in the
1205 -- path of the library. However, this
1206 -- subdirectory is no longer present in
1207 -- recent versions of GCC. So, we look for
1208 -- the last subdirectory "lib" in the path.
1210 GCC_Index :=
1211 Index (Path (1 .. Path_Last), "gcc-lib");
1213 if GCC_Index /= 0 then
1215 -- The shared version of libgcc is
1216 -- located in the parent directory.
1218 GCC_Index := GCC_Index - 1;
1220 else
1221 GCC_Index :=
1222 Index
1223 (Path (1 .. Path_Last),
1224 "/lib/");
1226 if GCC_Index = 0 then
1227 GCC_Index :=
1228 Index (Path (1 .. Path_Last),
1229 Directory_Separator & "lib"
1230 & Directory_Separator);
1231 end if;
1233 -- If we have found a "lib" subdir in
1234 -- the path to libgnat, the possible
1235 -- shared libgcc of interest by default
1236 -- is in libgcc_subdir at the same
1237 -- level.
1239 if GCC_Index /= 0 then
1240 declare
1241 Subdir : constant String :=
1242 Value (Libgcc_Subdir_Ptr);
1243 begin
1244 Path
1245 (GCC_Index + 1 ..
1246 GCC_Index + Subdir'Length) :=
1247 Subdir;
1248 GCC_Index :=
1249 GCC_Index + Subdir'Length;
1250 end;
1251 end if;
1252 end if;
1254 -- Look for an eventual run_path_option in
1255 -- the linker switches.
1257 if Separate_Run_Path_Options then
1258 Linker_Options.Increment_Last;
1259 Linker_Options.Table
1260 (Linker_Options.Last) :=
1261 new String'
1262 (Run_Path_Opt
1263 & File_Path
1264 (1 .. File_Path'Length
1265 - File_Name'Length));
1267 if GCC_Index /= 0 then
1268 Linker_Options.Increment_Last;
1269 Linker_Options.Table
1270 (Linker_Options.Last) :=
1271 new String'
1272 (Run_Path_Opt
1273 & Path (1 .. GCC_Index));
1274 end if;
1276 else
1277 for J in reverse
1278 1 .. Linker_Options.Last
1279 loop
1280 if Linker_Options.Table (J) /= null
1281 and then
1282 Linker_Options.Table (J)'Length
1283 > Run_Path_Opt'Length
1284 and then
1285 Linker_Options.Table (J)
1286 (1 .. Run_Path_Opt'Length) =
1287 Run_Path_Opt
1288 then
1289 -- We have found an already
1290 -- specified run_path_option:
1291 -- we will add to this
1292 -- switch, because only one
1293 -- run_path_option should be
1294 -- specified.
1296 Run_Path_Opt_Index := J;
1297 exit;
1298 end if;
1299 end loop;
1301 -- If there is no run_path_option, we
1302 -- need to add one.
1304 if Run_Path_Opt_Index = 0 then
1305 Linker_Options.Increment_Last;
1306 end if;
1308 if GCC_Index = 0 then
1309 if Run_Path_Opt_Index = 0 then
1310 Linker_Options.Table
1311 (Linker_Options.Last) :=
1312 new String'
1313 (Run_Path_Opt
1314 & File_Path
1315 (1 .. File_Path'Length
1316 - File_Name'Length));
1318 else
1319 Linker_Options.Table
1320 (Run_Path_Opt_Index) :=
1321 new String'
1322 (Linker_Options.Table
1323 (Run_Path_Opt_Index).all
1324 & Path_Separator
1325 & File_Path
1326 (1 .. File_Path'Length
1327 - File_Name'Length));
1328 end if;
1330 else
1331 if Run_Path_Opt_Index = 0 then
1332 Linker_Options.Table
1333 (Linker_Options.Last) :=
1334 new String'
1335 (Run_Path_Opt
1336 & File_Path
1337 (1 .. File_Path'Length
1338 - File_Name'Length)
1339 & Path_Separator
1340 & Path (1 .. GCC_Index));
1342 else
1343 Linker_Options.Table
1344 (Run_Path_Opt_Index) :=
1345 new String'
1346 (Linker_Options.Table
1347 (Run_Path_Opt_Index).all
1348 & Path_Separator
1349 & File_Path
1350 (1 .. File_Path'Length
1351 - File_Name'Length)
1352 & Path_Separator
1353 & Path (1 .. GCC_Index));
1354 end if;
1355 end if;
1356 end if;
1357 end;
1358 end if;
1359 end if;
1361 -- Then we add the appropriate -l switch
1363 Linker_Options.Increment_Last;
1364 Linker_Options.Table (Linker_Options.Last) :=
1365 new String'(Next_Line (Nfirst .. Nlast));
1366 end if;
1368 else
1369 -- If gnatlib library not found, then add it anyway in
1370 -- case some other mechanism may find it.
1372 Linker_Options.Increment_Last;
1373 Linker_Options.Table (Linker_Options.Last) :=
1374 new String'(Next_Line (Nfirst .. Nlast));
1375 end if;
1376 end;
1377 else
1378 Linker_Options.Increment_Last;
1379 Linker_Options.Table (Linker_Options.Last) :=
1380 new String'(Next_Line (Nfirst .. Nlast));
1381 end if;
1382 end if;
1384 Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker";
1386 Get_Next_Line;
1387 exit when Next_Line (Nfirst .. Nlast) = End_Info;
1389 Next_Line (Nfirst .. Nlast - 8) := Next_Line (Nfirst + 8 .. Nlast);
1390 Nlast := Nlast - 8;
1391 end loop;
1392 end if;
1394 -- If -shared was specified, invoke gcc with -shared-libgcc
1396 if GNAT_Shared then
1397 Linker_Options.Increment_Last;
1398 Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc;
1399 end if;
1401 Status := fclose (Fd);
1402 end Process_Binder_File;
1404 -----------
1405 -- Usage --
1406 -----------
1408 procedure Usage is
1409 begin
1410 Write_Str ("Usage: ");
1411 Write_Str (Base_Command_Name.all);
1412 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
1413 Write_Eol;
1414 Write_Eol;
1415 Write_Line (" mainprog.ali the ALI file of the main program");
1416 Write_Eol;
1417 Write_Eol;
1418 Display_Usage_Version_And_Help;
1419 Write_Line (" -f Force object file list to be generated");
1420 Write_Line (" -g Compile binder source file with debug information");
1421 Write_Line (" -n Do not compile the binder source file");
1422 Write_Line (" -P Process files for use by CodePeer");
1423 Write_Line (" -R Do not use a run_path_option");
1424 Write_Line (" -v Verbose mode");
1425 Write_Line (" -v -v Very verbose mode");
1426 Write_Eol;
1427 Write_Line (" -o nam Use 'nam' as the name of the executable");
1428 Write_Line (" -b target Compile the binder source to run on target");
1429 Write_Line (" -Bdir Load compiler executables from dir");
1431 if Is_Supported (Map_File) then
1432 Write_Line (" -Mmap Create map file map");
1433 Write_Line (" -M Create map file mainprog.map");
1434 end if;
1436 Write_Line (" --GCC=comp Use 'comp' as the compiler rather than 'gcc'");
1437 Write_Line (" --LINK=lnk Use 'lnk' as the linker rather than 'gcc'");
1438 Write_Eol;
1439 Write_Line (" [non-Ada-objects] list of non Ada object files");
1440 Write_Line (" [linker-options] other options for the linker");
1441 end Usage;
1443 ------------------
1444 -- Write_Header --
1445 ------------------
1447 procedure Write_Header is
1448 begin
1449 if Verbose_Mode then
1450 Write_Eol;
1451 Display_Version ("GNATLINK", "1995");
1452 end if;
1453 end Write_Header;
1455 -----------------
1456 -- Write_Usage --
1457 -----------------
1459 procedure Write_Usage is
1460 begin
1461 Write_Header;
1462 Usage;
1463 end Write_Usage;
1465 -- Start of processing for Gnatlink
1467 begin
1468 -- Add the directory where gnatlink is invoked in front of the path, if
1469 -- gnatlink is invoked with directory information.
1471 declare
1472 Command : constant String := Command_Name;
1473 begin
1474 for Index in reverse Command'Range loop
1475 if Command (Index) = Directory_Separator then
1476 declare
1477 Absolute_Dir : constant String :=
1478 Normalize_Pathname
1479 (Command (Command'First .. Index));
1481 PATH : constant String :=
1482 Absolute_Dir &
1483 Path_Separator &
1484 Getenv ("PATH").all;
1486 begin
1487 Setenv ("PATH", PATH);
1488 end;
1490 exit;
1491 end if;
1492 end loop;
1493 end;
1495 Base_Command_Name := new String'(Base_Name (Command_Name));
1496 Process_Args;
1498 if Argument_Count = 0
1499 or else (Verbose_Mode and then Argument_Count = 1)
1500 then
1501 Write_Usage;
1502 Exit_Program (E_Fatal);
1503 end if;
1505 -- Initialize packages to be used
1507 Csets.Initialize;
1508 Snames.Initialize;
1510 -- We always compile with -c
1512 Binder_Options_From_ALI.Increment_Last;
1513 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1514 new String'("-c");
1516 if Ali_File_Name = null then
1517 Exit_With_Error ("no ali file given for link");
1518 end if;
1520 if not Is_Regular_File (Ali_File_Name.all) then
1521 Exit_With_Error (Ali_File_Name.all & " not found");
1522 end if;
1524 -- Read the ALI file of the main subprogram if the binder generated file
1525 -- needs to be compiled and no --GCC= switch has been specified. Fetch the
1526 -- back end switches from this ALI file and use these switches to compile
1527 -- the binder generated file
1529 if Compile_Bind_File and then Standard_Gcc then
1530 Initialize_ALI;
1531 Name_Len := Ali_File_Name'Length;
1532 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1534 declare
1535 use Types;
1536 F : constant File_Name_Type := Name_Find;
1537 T : Text_Buffer_Ptr;
1538 A : ALI_Id;
1540 begin
1541 -- Load the ALI file
1543 T := Read_Library_Info (F, True);
1545 -- Read it. Note that we ignore errors, since we only want very
1546 -- limited information from the ali file, and likely a slightly
1547 -- wrong version will be just fine, though in normal operation
1548 -- we don't expect this to happen.
1550 A := Scan_ALI
1553 Ignore_ED => False,
1554 Err => False,
1555 Ignore_Errors => True);
1557 if A /= No_ALI_Id then
1559 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1560 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1561 loop
1562 -- Do not compile with the front end switches. However, --RTS
1563 -- is to be dealt with specially because it needs to be passed
1564 -- to compile the file generated by the binder.
1566 declare
1567 Arg : String_Ptr renames Args.Table (Index);
1568 begin
1569 if not Is_Front_End_Switch (Arg.all) then
1570 Binder_Options_From_ALI.Increment_Last;
1571 Binder_Options_From_ALI.Table
1572 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1574 -- GNAT doesn't support GCC's multilib mechanism when it
1575 -- is configured with --disable-libada. This means that,
1576 -- when a multilib switch is used to request a particular
1577 -- compilation mode, the corresponding --RTS switch must
1578 -- also be specified. It is convenient to eliminate the
1579 -- redundancy by keying the compilation mode on a single
1580 -- switch, namely --RTS, and have the compiler reinstate
1581 -- the multilib switch (see gcc-interface/lang-specs.h).
1582 -- This switch must be passed to the driver at link time.
1584 if Arg'Length = 5
1585 and then Arg (Arg'First + 1 .. Arg'First + 4) = "mrtp"
1586 then
1587 Linker_Options.Increment_Last;
1588 Linker_Options.Table
1589 (Linker_Options.Last) := String_Access (Arg);
1590 end if;
1592 elsif Arg'Length > 5
1593 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1594 then
1595 Binder_Options_From_ALI.Increment_Last;
1596 Binder_Options_From_ALI.Table
1597 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1599 -- Set the RTS_*_Path_Name variables, so that
1600 -- the correct directories will be set when
1601 -- Osint.Add_Default_Search_Dirs will be called later.
1603 Opt.RTS_Src_Path_Name :=
1604 Get_RTS_Search_Dir
1605 (Arg (Arg'First + 6 .. Arg'Last), Include);
1607 Opt.RTS_Lib_Path_Name :=
1608 Get_RTS_Search_Dir
1609 (Arg (Arg'First + 6 .. Arg'Last), Objects);
1610 end if;
1611 end;
1612 end loop;
1613 end if;
1614 end;
1615 end if;
1617 -- Get target parameters
1619 Osint.Add_Default_Search_Dirs;
1620 Targparm.Get_Target_Parameters;
1622 -- Compile the bind file with the following switches:
1624 -- -gnatA stops reading gnat.adc, since we don't know what
1625 -- pragmas would work, and we do not need it anyway.
1627 -- -gnatWb allows brackets coding for wide characters
1629 -- -gnatiw allows wide characters in identifiers. This is needed
1630 -- because bindgen uses brackets encoding for all upper
1631 -- half and wide characters in identifier names.
1633 -- In addition, in CodePeer mode compile with -x adascil -gnatcC
1635 Binder_Options_From_ALI.Increment_Last;
1636 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1637 new String'("-gnatA");
1638 Binder_Options_From_ALI.Increment_Last;
1639 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1640 new String'("-gnatWb");
1641 Binder_Options_From_ALI.Increment_Last;
1642 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1643 new String'("-gnatiw");
1645 if Opt.CodePeer_Mode then
1646 Binder_Options_From_ALI.Increment_Last;
1647 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1648 new String'("-x");
1649 Binder_Options_From_ALI.Increment_Last;
1650 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1651 new String'("adascil");
1652 Binder_Options_From_ALI.Increment_Last;
1653 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1654 new String'("-gnatcC");
1655 end if;
1657 -- Locate all the necessary programs and verify required files are present
1659 Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1661 if Gcc_Path = null then
1662 Exit_With_Error ("Couldn't locate " & Gcc.all);
1663 end if;
1665 if Linker_Path = null then
1666 Linker_Path := Gcc_Path;
1667 end if;
1669 Write_Header;
1671 Target_Debuggable_Suffix := Get_Target_Debuggable_Suffix;
1673 -- If no output name specified, then use the base name of .ali file name
1675 if Output_File_Name = null then
1676 Output_File_Name :=
1677 new String'(Base_Name (Ali_File_Name.all)
1678 & Target_Debuggable_Suffix.all);
1679 end if;
1681 Linker_Options.Increment_Last;
1682 Linker_Options.Table (Linker_Options.Last) := new String'("-o");
1684 Linker_Options.Increment_Last;
1685 Linker_Options.Table (Linker_Options.Last) :=
1686 new String'(Output_File_Name.all);
1688 Check_Existing_Executable (Output_File_Name.all);
1690 -- Warn if main program is called "test", as that may be a built-in command
1691 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1692 -- will not appear. However, do not warn in the case of a cross compiler.
1694 -- Assume this is a cross tool if the executable name is not gnatlink.
1695 -- Note that the executable name is also gnatlink on windows, but in that
1696 -- case the output file name will be test.exe rather than test.
1698 if Base_Command_Name.all = "gnatlink"
1699 and then Output_File_Name.all = "test"
1700 then
1701 Error_Msg ("warning: executable name """ & Output_File_Name.all
1702 & """ may conflict with shell command");
1703 end if;
1705 -- Special warnings for worrisome file names on windows
1707 -- Recent versions of Windows by default cause privilege escalation if an
1708 -- executable file name contains substrings "install", "setup", "update"
1709 -- or "patch". A console application will typically fail to load as a
1710 -- result, so we should warn the user.
1712 Bad_File_Names_On_Windows : declare
1713 FN : String := Output_File_Name.all;
1715 procedure Check_File_Name (S : String);
1716 -- Warn if file name has the substring S
1718 procedure Check_File_Name (S : String) is
1719 begin
1720 for J in 1 .. FN'Length - (S'Length - 1) loop
1721 if FN (J .. J + (S'Length - 1)) = S then
1722 Error_Msg
1723 ("warning: executable file name """ & Output_File_Name.all
1724 & """ contains substring """ & S & '"');
1725 Error_Msg
1726 ("admin privileges may be required to run this file");
1727 end if;
1728 end loop;
1729 end Check_File_Name;
1731 -- Start of processing for Bad_File_Names_On_Windows
1733 begin
1734 for J in FN'Range loop
1735 FN (J) := Csets.Fold_Lower (FN (J));
1736 end loop;
1738 -- For now we detect Windows by its executable suffix of .exe
1740 if Target_Debuggable_Suffix.all = ".exe" then
1741 Check_File_Name ("install");
1742 Check_File_Name ("setup");
1743 Check_File_Name ("update");
1744 Check_File_Name ("patch");
1745 end if;
1746 end Bad_File_Names_On_Windows;
1748 -- If -M switch was specified, add the switches to create the map file
1750 if Create_Map_File then
1751 declare
1752 Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map";
1753 Switches : String_List_Access;
1755 begin
1756 Convert (Map_File, Map_Name, Switches);
1758 if Switches /= null then
1759 for J in Switches'Range loop
1760 Linker_Options.Increment_Last;
1761 Linker_Options.Table (Linker_Options.Last) := Switches (J);
1762 end loop;
1763 end if;
1764 end;
1765 end if;
1767 -- Perform consistency checks
1769 -- Transform the .ali file name into the binder output file name
1771 Make_Binder_File_Names : declare
1772 Fname : constant String := Base_Name (Ali_File_Name.all);
1773 Fname_Len : Integer := Fname'Length;
1775 function Get_Maximum_File_Name_Length return Integer;
1776 pragma Import (C, Get_Maximum_File_Name_Length,
1777 "__gnat_get_maximum_file_name_length");
1779 Maximum_File_Name_Length : constant Integer :=
1780 Get_Maximum_File_Name_Length;
1782 Bind_File_Prefix : Types.String_Ptr;
1783 -- Contains prefix used for bind files
1785 begin
1786 -- Set prefix
1788 Bind_File_Prefix := new String'("b~");
1790 -- If the length of the binder file becomes too long due to
1791 -- the addition of the "b?" prefix, then truncate it.
1793 if Maximum_File_Name_Length > 0 then
1794 while Fname_Len >
1795 Maximum_File_Name_Length - Bind_File_Prefix.all'Length
1796 loop
1797 Fname_Len := Fname_Len - 1;
1798 end loop;
1799 end if;
1801 declare
1802 Fnam : constant String :=
1803 Bind_File_Prefix.all &
1804 Fname (Fname'First .. Fname'First + Fname_Len - 1);
1806 begin
1807 Binder_Spec_Src_File := new String'(Fnam & ".ads");
1808 Binder_Body_Src_File := new String'(Fnam & ".adb");
1809 Binder_Ali_File := new String'(Fnam & ".ali");
1811 Binder_Obj_File := new String'(Fnam & Get_Target_Object_Suffix.all);
1812 end;
1814 if Fname_Len /= Fname'Length then
1815 Binder_Options.Increment_Last;
1816 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1817 Binder_Options.Increment_Last;
1818 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1819 end if;
1820 end Make_Binder_File_Names;
1822 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1824 -- Compile the binder file. This is fast, so we always do it, unless
1825 -- specifically told not to by the -n switch
1827 if Compile_Bind_File then
1828 Bind_Step : declare
1829 Success : Boolean;
1831 Args : Argument_List
1832 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1834 begin
1835 for J in 1 .. Binder_Options_From_ALI.Last loop
1836 Args (J) := Binder_Options_From_ALI.Table (J);
1837 end loop;
1839 for J in 1 .. Binder_Options.Last loop
1840 Args (Binder_Options_From_ALI.Last + J) :=
1841 Binder_Options.Table (J);
1842 end loop;
1844 -- Use the full path of the binder generated source, so that it is
1845 -- guaranteed that the debugger will find this source, even with
1846 -- STABS.
1848 Args (Args'Last) :=
1849 new String'(Normalize_Pathname (Binder_Body_Src_File.all));
1851 if Verbose_Mode then
1852 Write_Str (Base_Name (Gcc_Path.all));
1854 for J in Args'Range loop
1855 Write_Str (" ");
1856 Write_Str (Args (J).all);
1857 end loop;
1859 Write_Eol;
1860 end if;
1862 System.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1864 if not Success then
1865 Exit_Program (E_Fatal);
1866 end if;
1867 end Bind_Step;
1868 end if;
1870 -- In CodePeer mode, there's nothing left to do after the binder file has
1871 -- been compiled.
1873 if Opt.CodePeer_Mode then
1874 if Tname_FD /= Invalid_FD then
1875 Delete (Tname);
1876 end if;
1878 return;
1879 end if;
1881 -- Now, actually link the program
1883 Link_Step : declare
1884 Num_Args : Natural :=
1885 (Linker_Options.Last - Linker_Options.First + 1) +
1886 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1887 (Linker_Objects.Last - Linker_Objects.First + 1);
1888 Stack_Op : Boolean := False;
1890 begin
1891 -- Remove duplicate stack size setting from the Linker_Options table.
1892 -- The stack setting option "-Xlinker --stack=R,C" can be found
1893 -- in one line when set by a pragma Linker_Options or in two lines
1894 -- ("-Xlinker" then "--stack=R,C") when set on the command line. We
1895 -- also check for the "-Wl,--stack=R" style option.
1897 -- We must remove the second stack setting option instance because
1898 -- the one on the command line will always be the first one. And any
1899 -- subsequent stack setting option will overwrite the previous one.
1900 -- This is done especially for GNAT/NT where we set the stack size
1901 -- for tasking programs by a pragma in the NT specific tasking
1902 -- package System.Task_Primitives.Operations.
1904 -- Note: This is not a FOR loop that runs from Linker_Options.First
1905 -- to Linker_Options.Last, since operations within the loop can
1906 -- modify the length of the table.
1908 Clean_Link_Option_Set : declare
1909 J : Natural;
1910 Shared_Libgcc_Seen : Boolean := False;
1912 begin
1913 J := Linker_Options.First;
1914 while J <= Linker_Options.Last loop
1915 if Linker_Options.Table (J).all = "-Xlinker"
1916 and then J < Linker_Options.Last
1917 and then Linker_Options.Table (J + 1)'Length > 8
1918 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
1919 then
1920 if Stack_Op then
1921 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
1922 Linker_Options.Table (J + 2 .. Linker_Options.Last);
1923 Linker_Options.Decrement_Last;
1924 Linker_Options.Decrement_Last;
1925 Num_Args := Num_Args - 2;
1927 else
1928 Stack_Op := True;
1929 end if;
1930 end if;
1932 -- Remove duplicate -shared-libgcc switch
1934 if Linker_Options.Table (J).all = Shared_Libgcc_String then
1935 if Shared_Libgcc_Seen then
1936 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1937 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1938 Linker_Options.Decrement_Last;
1939 Num_Args := Num_Args - 1;
1941 else
1942 Shared_Libgcc_Seen := True;
1943 end if;
1944 end if;
1946 -- Here we just check for a canonical form that matches the
1947 -- pragma Linker_Options set in the NT runtime.
1949 if (Linker_Options.Table (J)'Length > 17
1950 and then Linker_Options.Table (J) (1 .. 17) =
1951 "-Xlinker --stack=")
1952 or else
1953 (Linker_Options.Table (J)'Length > 12
1954 and then Linker_Options.Table (J) (1 .. 12) =
1955 "-Wl,--stack=")
1956 then
1957 if Stack_Op then
1958 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1959 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1960 Linker_Options.Decrement_Last;
1961 Num_Args := Num_Args - 1;
1963 else
1964 Stack_Op := True;
1965 end if;
1966 end if;
1968 J := J + 1;
1969 end loop;
1971 if Linker_Path = Gcc_Path then
1973 -- For systems where the default is to link statically with
1974 -- libgcc, if gcc is not called with -shared-libgcc, call it
1975 -- with -static-libgcc, as there are some platforms where one
1976 -- of these two switches is compulsory to link.
1978 if Shared_Libgcc_Default = 'T'
1979 and then not Shared_Libgcc_Seen
1980 then
1981 Linker_Options.Increment_Last;
1982 Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
1983 Num_Args := Num_Args + 1;
1984 end if;
1985 end if;
1986 end Clean_Link_Option_Set;
1988 -- Prepare arguments for call to linker
1990 Call_Linker : declare
1991 Success : Boolean;
1992 Args : Argument_List (1 .. Num_Args + 1);
1993 Index : Integer := Args'First;
1995 begin
1996 Args (Index) := Binder_Obj_File;
1998 -- Add the object files and any -largs libraries
2000 for J in Linker_Objects.First .. Linker_Objects.Last loop
2001 Index := Index + 1;
2002 Args (Index) := Linker_Objects.Table (J);
2003 end loop;
2005 -- Add the linker options from the binder file
2007 for J in Linker_Options.First .. Linker_Options.Last loop
2008 Index := Index + 1;
2009 Args (Index) := Linker_Options.Table (J);
2010 end loop;
2012 -- Finally add the libraries from the --GCC= switch
2014 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
2015 Index := Index + 1;
2016 Args (Index) := Gcc_Linker_Options.Table (J);
2017 end loop;
2019 if Verbose_Mode then
2020 Write_Str (Linker_Path.all);
2022 for J in Args'Range loop
2023 Write_Str (" ");
2024 Write_Str (Args (J).all);
2025 end loop;
2027 Write_Eol;
2029 -- If we are on very verbose mode (-v -v) and a response file
2030 -- is used we display its content.
2032 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
2033 Write_Eol;
2034 Write_Str ("Response file (" &
2035 Tname (Tname'First .. Tname'Last - 1) &
2036 ") content : ");
2037 Write_Eol;
2039 for J in
2040 Response_File_Objects.First .. Response_File_Objects.Last
2041 loop
2042 Write_Str (Response_File_Objects.Table (J).all);
2043 Write_Eol;
2044 end loop;
2046 Write_Eol;
2047 end if;
2048 end if;
2050 System.OS_Lib.Spawn (Linker_Path.all, Args, Success);
2052 -- Delete the temporary file used in conjunction with linking if one
2053 -- was created. See Process_Bind_File for details.
2055 if Tname_FD /= Invalid_FD then
2056 Delete (Tname);
2057 end if;
2059 if not Success then
2060 Error_Msg ("error when calling " & Linker_Path.all);
2061 Exit_Program (E_Fatal);
2062 end if;
2063 end Call_Linker;
2064 end Link_Step;
2066 -- Only keep the binder output file and it's associated object
2067 -- file if compiling with the -g option. These files are only
2068 -- useful if debugging.
2070 if not Debug_Flag_Present then
2071 Delete (Binder_Ali_File.all & ASCII.NUL);
2072 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
2073 Delete (Binder_Body_Src_File.all & ASCII.NUL);
2074 Delete (Binder_Obj_File.all & ASCII.NUL);
2075 end if;
2077 Exit_Program (E_Success);
2079 exception
2080 when X : others =>
2081 Write_Line (Exception_Information (X));
2082 Exit_With_Error ("INTERNAL ERROR. Please report");
2083 end Gnatlink;