Merge from mainline (163495:164578).
[official-gcc/graphite-test-results.git] / gcc / ada / gnatlink.adb
blob47397c5c92c59e780331fec3a441fab7d57e3470
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-2010, 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 Hostparm;
32 with Indepsw; use Indepsw;
33 with Namet; use Namet;
34 with Opt;
35 with Osint; use Osint;
36 with Output; use Output;
37 with Snames;
38 with Switch; use Switch;
39 with System; use System;
40 with Table;
41 with Targparm; use Targparm;
42 with Types;
44 with Ada.Command_Line; use Ada.Command_Line;
45 with Ada.Exceptions; use Ada.Exceptions;
47 with System.OS_Lib; use System.OS_Lib;
48 with System.CRTL;
50 with Interfaces.C_Streams; use Interfaces.C_Streams;
51 with Interfaces.C.Strings; use Interfaces.C.Strings;
53 procedure Gnatlink is
54 pragma Ident (Gnatvsn.Gnat_Static_Version_String);
56 Shared_Libgcc_String : constant String := "-shared-libgcc";
57 Shared_Libgcc : constant String_Access :=
58 new String'(Shared_Libgcc_String);
59 -- Used to invoke gcc when the binder is invoked with -shared
61 Static_Libgcc_String : constant String := "-static-libgcc";
62 Static_Libgcc : constant String_Access :=
63 new String'(Static_Libgcc_String);
64 -- Used to invoke gcc when shared libs are not used
66 package Gcc_Linker_Options is new Table.Table (
67 Table_Component_Type => String_Access,
68 Table_Index_Type => Integer,
69 Table_Low_Bound => 1,
70 Table_Initial => 20,
71 Table_Increment => 100,
72 Table_Name => "Gnatlink.Gcc_Linker_Options");
73 -- Comments needed ???
75 package Libpath is new Table.Table (
76 Table_Component_Type => Character,
77 Table_Index_Type => Integer,
78 Table_Low_Bound => 1,
79 Table_Initial => 4096,
80 Table_Increment => 100,
81 Table_Name => "Gnatlink.Libpath");
82 -- Comments needed ???
84 package Linker_Options is new Table.Table (
85 Table_Component_Type => String_Access,
86 Table_Index_Type => Integer,
87 Table_Low_Bound => 1,
88 Table_Initial => 20,
89 Table_Increment => 100,
90 Table_Name => "Gnatlink.Linker_Options");
91 -- Comments needed ???
93 package Linker_Objects is new Table.Table (
94 Table_Component_Type => String_Access,
95 Table_Index_Type => Integer,
96 Table_Low_Bound => 1,
97 Table_Initial => 20,
98 Table_Increment => 100,
99 Table_Name => "Gnatlink.Linker_Objects");
100 -- This table collects the objects file to be passed to the linker. In the
101 -- case where the linker command line is too long then programs objects
102 -- are put on the Response_File_Objects table. Note that the binder object
103 -- file and the user's objects remain in this table. This is very
104 -- important because on the GNU linker command line the -L switch is not
105 -- used to look for objects files but -L switch is used to look for
106 -- objects listed in the response file. This is not a problem with the
107 -- applications objects as they are specified with a full name.
109 package Response_File_Objects is new Table.Table (
110 Table_Component_Type => String_Access,
111 Table_Index_Type => Integer,
112 Table_Low_Bound => 1,
113 Table_Initial => 20,
114 Table_Increment => 100,
115 Table_Name => "Gnatlink.Response_File_Objects");
116 -- This table collects the objects file that are to be put in the response
117 -- file. Only application objects are collected there (see details in
118 -- Linker_Objects table comments)
120 package Binder_Options_From_ALI is new Table.Table (
121 Table_Component_Type => String_Access,
122 Table_Index_Type => Integer,
123 Table_Low_Bound => 1, -- equals low bound of Argument_List for Spawn
124 Table_Initial => 20,
125 Table_Increment => 100,
126 Table_Name => "Gnatlink.Binder_Options_From_ALI");
127 -- This table collects the switches from the ALI file of the main
128 -- subprogram.
130 package Binder_Options is new Table.Table (
131 Table_Component_Type => String_Access,
132 Table_Index_Type => Integer,
133 Table_Low_Bound => 1, -- equals low bound of Argument_List for Spawn
134 Table_Initial => 20,
135 Table_Increment => 100,
136 Table_Name => "Gnatlink.Binder_Options");
137 -- This table collects the arguments to be passed to compile the binder
138 -- generated file.
140 Gcc : String_Access := Program_Name ("gcc", "gnatlink");
142 Read_Mode : constant String := "r" & ASCII.NUL;
144 Begin_Info : String := "-- BEGIN Object file/option list";
145 End_Info : String := "-- END Object file/option list ";
146 -- Note: above lines are modified in C mode, see option processing
148 Gcc_Path : String_Access;
149 Linker_Path : String_Access;
150 Output_File_Name : String_Access;
151 Ali_File_Name : String_Access;
152 Binder_Spec_Src_File : String_Access;
153 Binder_Body_Src_File : String_Access;
154 Binder_Ali_File : String_Access;
155 Binder_Obj_File : String_Access;
157 Tname : Temp_File_Name;
158 Tname_FD : File_Descriptor := Invalid_FD;
159 -- Temporary file used by linker to pass list of object files on
160 -- certain systems with limitations on size of arguments.
162 Lname : String_Access := null;
163 -- File used by linker for CLI target, used to concatenate all .il files
164 -- when the command line passed to ilasm is too long
166 Debug_Flag_Present : Boolean := False;
167 Verbose_Mode : Boolean := False;
168 Very_Verbose_Mode : Boolean := False;
170 Ada_Bind_File : Boolean := True;
171 -- Set to True if bind file is generated in Ada
173 Standard_Gcc : Boolean := True;
175 Compile_Bind_File : Boolean := True;
176 -- Set to False if bind file is not to be compiled
178 Create_Map_File : Boolean := False;
179 -- Set to True by switch -M. The map file name is derived from
180 -- the ALI file name (mainprog.ali => mainprog.map).
182 Object_List_File_Supported : Boolean;
183 for Object_List_File_Supported'Size use Character'Size;
184 pragma Import
185 (C, Object_List_File_Supported, "__gnat_objlist_file_supported");
186 -- Predicate indicating whether the linker has an option whereby the
187 -- names of object files can be passed to the linker in a file.
189 Object_List_File_Required : Boolean := False;
190 -- Set to True to force generation of a response file
192 Shared_Libgcc_Default : Character;
193 for Shared_Libgcc_Default'Size use Character'Size;
194 pragma Import
195 (C, Shared_Libgcc_Default, "__gnat_shared_libgcc_default");
196 -- Indicates wether libgcc should be statically linked (use 'T') or
197 -- dynamically linked (use 'H') by default.
199 function Base_Name (File_Name : String) return String;
200 -- Return just the file name part without the extension (if present)
202 procedure Check_Existing_Executable (File_Name : String);
203 -- Delete any existing executable to avoid accidentally updating the target
204 -- of a symbolic link, but produce a Fatail_Error if File_Name matches any
205 -- of the source file names. This avoids overwriting of extensionless
206 -- source files by accident on systems where executables do not have
207 -- extensions.
209 procedure Delete (Name : String);
210 -- Wrapper to unlink as status is ignored by this application
212 procedure Error_Msg (Message : String);
213 -- Output the error or warning Message
215 procedure Exit_With_Error (Error : String);
216 -- Output Error and exit program with a fatal condition
218 procedure Process_Args;
219 -- Go through all the arguments and build option tables
221 procedure Process_Binder_File (Name : String);
222 -- Reads the binder file and extracts linker arguments
224 procedure Usage;
225 -- Display usage
227 procedure Write_Header;
228 -- Show user the program name, version and copyright
230 procedure Write_Usage;
231 -- Show user the program options
233 ---------------
234 -- Base_Name --
235 ---------------
237 function Base_Name (File_Name : String) return String is
238 Findex1 : Natural;
239 Findex2 : Natural;
241 begin
242 Findex1 := File_Name'First;
244 -- The file might be specified by a full path name. However,
245 -- we want the path to be stripped away.
247 for J in reverse File_Name'Range loop
248 if Is_Directory_Separator (File_Name (J)) then
249 Findex1 := J + 1;
250 exit;
251 end if;
252 end loop;
254 Findex2 := File_Name'Last;
255 while Findex2 > Findex1
256 and then File_Name (Findex2) /= '.'
257 loop
258 Findex2 := Findex2 - 1;
259 end loop;
261 if Findex2 = Findex1 then
262 Findex2 := File_Name'Last + 1;
263 end if;
265 return File_Name (Findex1 .. Findex2 - 1);
266 end Base_Name;
268 -------------------------------
269 -- Check_Existing_Executable --
270 -------------------------------
272 procedure Check_Existing_Executable (File_Name : String) is
273 Ename : String := File_Name;
274 Efile : File_Name_Type;
275 Sfile : File_Name_Type;
277 begin
278 Canonical_Case_File_Name (Ename);
279 Name_Len := 0;
280 Add_Str_To_Name_Buffer (Ename);
281 Efile := Name_Find;
283 for J in Units.Table'First .. Units.Last loop
284 Sfile := Units.Table (J).Sfile;
285 if Sfile = Efile then
286 Exit_With_Error ("executable name """ & File_Name & """ matches "
287 & "source file name """ & Get_Name_String (Sfile) & """");
288 end if;
289 end loop;
291 Delete (File_Name);
292 end Check_Existing_Executable;
294 ------------
295 -- Delete --
296 ------------
298 procedure Delete (Name : String) is
299 Status : int;
300 pragma Unreferenced (Status);
301 begin
302 Status := unlink (Name'Address);
303 -- Is it really right to ignore an error here ???
304 end Delete;
306 ---------------
307 -- Error_Msg --
308 ---------------
310 procedure Error_Msg (Message : String) is
311 begin
312 Write_Str (Base_Name (Command_Name));
313 Write_Str (": ");
314 Write_Str (Message);
315 Write_Eol;
316 end Error_Msg;
318 ---------------------
319 -- Exit_With_Error --
320 ---------------------
322 procedure Exit_With_Error (Error : String) is
323 begin
324 Error_Msg (Error);
325 Exit_Program (E_Fatal);
326 end Exit_With_Error;
328 ------------------
329 -- Process_Args --
330 ------------------
332 procedure Process_Args is
333 Next_Arg : Integer;
334 Skip_Next : Boolean := False;
335 -- Set to true if the next argument is to be added into the list of
336 -- linker's argument without parsing it.
338 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
340 -- Start of processing for Process_Args
342 begin
343 -- First, check for --version and --help
345 Check_Version_And_Help ("GNATLINK", "1995");
347 -- Loop through arguments of gnatlink command
349 Next_Arg := 1;
350 loop
351 exit when Next_Arg > Argument_Count;
353 Process_One_Arg : declare
354 Arg : constant String := Argument (Next_Arg);
356 begin
357 -- Case of argument which is a switch
359 -- We definitely need section by section comments here ???
361 if Skip_Next then
363 -- This argument must not be parsed, just add it to the
364 -- list of linker's options.
366 Skip_Next := False;
368 Linker_Options.Increment_Last;
369 Linker_Options.Table (Linker_Options.Last) :=
370 new String'(Arg);
372 elsif Arg'Length /= 0 and then Arg (1) = '-' then
373 if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then
374 Exit_With_Error
375 ("invalid switch: """ & Arg & """ (gnat not needed here)");
376 end if;
378 if Arg = "-Xlinker" then
380 -- Next argument should be sent directly to the linker.
381 -- We do not want to parse it here.
383 Skip_Next := True;
385 Linker_Options.Increment_Last;
386 Linker_Options.Table (Linker_Options.Last) :=
387 new String'(Arg);
389 elsif Arg (2) = 'g'
390 and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
391 then
392 Debug_Flag_Present := True;
394 Linker_Options.Increment_Last;
395 Linker_Options.Table (Linker_Options.Last) :=
396 new String'(Arg);
398 Binder_Options.Increment_Last;
399 Binder_Options.Table (Binder_Options.Last) :=
400 Linker_Options.Table (Linker_Options.Last);
402 elsif Arg'Length >= 3 and then Arg (2) = 'M' then
403 declare
404 Switches : String_List_Access;
406 begin
407 Convert (Map_File, Arg (3 .. Arg'Last), Switches);
409 if Switches /= null then
410 for J in Switches'Range loop
411 Linker_Options.Increment_Last;
412 Linker_Options.Table (Linker_Options.Last) :=
413 Switches (J);
414 end loop;
415 end if;
416 end;
418 elsif Arg'Length = 2 then
419 case Arg (2) is
420 when 'A' =>
421 Ada_Bind_File := True;
422 Begin_Info := "-- BEGIN Object file/option list";
423 End_Info := "-- END Object file/option list ";
425 when 'b' =>
426 Linker_Options.Increment_Last;
427 Linker_Options.Table (Linker_Options.Last) :=
428 new String'(Arg);
430 Binder_Options.Increment_Last;
431 Binder_Options.Table (Binder_Options.Last) :=
432 Linker_Options.Table (Linker_Options.Last);
434 Next_Arg := Next_Arg + 1;
436 if Next_Arg > Argument_Count then
437 Exit_With_Error ("Missing argument for -b");
438 end if;
440 Get_Machine_Name : declare
441 Name_Arg : constant String_Access :=
442 new String'(Argument (Next_Arg));
444 begin
445 Linker_Options.Increment_Last;
446 Linker_Options.Table (Linker_Options.Last) :=
447 Name_Arg;
449 Binder_Options.Increment_Last;
450 Binder_Options.Table (Binder_Options.Last) :=
451 Name_Arg;
453 end Get_Machine_Name;
455 when 'C' =>
456 Ada_Bind_File := False;
457 Begin_Info := "/* BEGIN Object file/option list";
458 End_Info := " END Object file/option list */";
460 when 'f' =>
461 if Object_List_File_Supported then
462 Object_List_File_Required := True;
463 else
464 Exit_With_Error
465 ("Object list file not supported on this target");
466 end if;
468 when 'M' =>
469 Create_Map_File := True;
471 when 'n' =>
472 Compile_Bind_File := False;
474 when 'o' =>
475 Next_Arg := Next_Arg + 1;
477 if Next_Arg > Argument_Count then
478 Exit_With_Error ("Missing argument for -o");
479 end if;
481 Output_File_Name :=
482 new String'(Executable_Name
483 (Argument (Next_Arg),
484 Only_If_No_Suffix => True));
486 when 'R' =>
487 Opt.Run_Path_Option := False;
489 when 'v' =>
491 -- Support "double" verbose mode. Second -v
492 -- gets sent to the linker and binder phases.
494 if Verbose_Mode then
495 Very_Verbose_Mode := True;
497 Linker_Options.Increment_Last;
498 Linker_Options.Table (Linker_Options.Last) :=
499 new String'(Arg);
501 Binder_Options.Increment_Last;
502 Binder_Options.Table (Binder_Options.Last) :=
503 Linker_Options.Table (Linker_Options.Last);
505 else
506 Verbose_Mode := True;
508 end if;
510 when others =>
511 Linker_Options.Increment_Last;
512 Linker_Options.Table (Linker_Options.Last) :=
513 new String'(Arg);
515 end case;
517 elsif Arg (2) = 'B' then
518 Linker_Options.Increment_Last;
519 Linker_Options.Table (Linker_Options.Last) :=
520 new String'(Arg);
522 Binder_Options.Increment_Last;
523 Binder_Options.Table (Binder_Options.Last) :=
524 Linker_Options.Table (Linker_Options.Last);
526 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
527 if Arg'Length = 7 then
528 Exit_With_Error ("Missing argument for --LINK=");
529 end if;
531 Linker_Path :=
532 System.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
534 if Linker_Path = null then
535 Exit_With_Error
536 ("Could not locate linker: " & Arg (8 .. Arg'Last));
537 end if;
539 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
540 declare
541 Program_Args : constant Argument_List_Access :=
542 Argument_String_To_List
543 (Arg (7 .. Arg'Last));
545 begin
546 if Program_Args.all (1).all /= Gcc.all then
547 Gcc := new String'(Program_Args.all (1).all);
548 Standard_Gcc := False;
549 end if;
551 -- Set appropriate flags for switches passed
553 for J in 2 .. Program_Args.all'Last loop
554 declare
555 Arg : constant String := Program_Args.all (J).all;
556 AF : constant Integer := Arg'First;
558 begin
559 if Arg'Length /= 0 and then Arg (AF) = '-' then
560 if Arg (AF + 1) = 'g'
561 and then (Arg'Length = 2
562 or else Arg (AF + 2) in '0' .. '3'
563 or else Arg (AF + 2 .. Arg'Last) = "coff")
564 then
565 Debug_Flag_Present := True;
566 end if;
567 end if;
569 -- Add directory to source search dirs so that
570 -- Get_Target_Parameters can find system.ads
572 if Arg (AF .. AF + 1) = "-I"
573 and then Arg'Length > 2
574 then
575 Add_Src_Search_Dir (Arg (AF + 2 .. Arg'Last));
576 end if;
578 -- Pass to gcc for compiling binder generated file
579 -- No use passing libraries, it will just generate
580 -- a warning
582 if not (Arg (AF .. AF + 1) = "-l"
583 or else Arg (AF .. AF + 1) = "-L")
584 then
585 Binder_Options.Increment_Last;
586 Binder_Options.Table (Binder_Options.Last) :=
587 new String'(Arg);
588 end if;
590 -- Pass to gcc for linking program
592 Gcc_Linker_Options.Increment_Last;
593 Gcc_Linker_Options.Table
594 (Gcc_Linker_Options.Last) := new String'(Arg);
595 end;
596 end loop;
597 end;
599 -- Send all multi-character switches not recognized as
600 -- a special case by gnatlink to the linker/loader stage.
602 else
603 Linker_Options.Increment_Last;
604 Linker_Options.Table (Linker_Options.Last) :=
605 new String'(Arg);
606 end if;
608 -- Here if argument is a file name rather than a switch
610 else
611 -- If explicit ali file, capture it
613 if Arg'Length > 4
614 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
615 then
616 if Ali_File_Name = null then
617 Ali_File_Name := new String'(Arg);
618 else
619 Exit_With_Error ("cannot handle more than one ALI file");
620 end if;
622 -- If target object file, record object file
624 elsif Arg'Length > Get_Target_Object_Suffix.all'Length
625 and then Arg
626 (Arg'Last -
627 Get_Target_Object_Suffix.all'Length + 1 .. Arg'Last)
628 = Get_Target_Object_Suffix.all
629 then
630 Linker_Objects.Increment_Last;
631 Linker_Objects.Table (Linker_Objects.Last) :=
632 new String'(Arg);
634 -- If host object file, record object file
635 -- e.g. accept foo.o as well as foo.obj on VMS target
637 elsif Arg'Length > Get_Object_Suffix.all'Length
638 and then Arg
639 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
640 = Get_Object_Suffix.all
641 then
642 Linker_Objects.Increment_Last;
643 Linker_Objects.Table (Linker_Objects.Last) :=
644 new String'(Arg);
646 -- If corresponding ali file exists, capture it
648 elsif Ali_File_Name = null
649 and then Is_Regular_File (Arg & ".ali")
650 then
651 Ali_File_Name := new String'(Arg & ".ali");
653 -- Otherwise assume this is a linker options entry, but
654 -- see below for interesting adjustment to this assumption.
656 else
657 Linker_Options.Increment_Last;
658 Linker_Options.Table (Linker_Options.Last) :=
659 new String'(Arg);
660 end if;
661 end if;
662 end Process_One_Arg;
664 Next_Arg := Next_Arg + 1;
665 end loop;
667 -- If Ada bind file, then compile it with warnings suppressed, because
668 -- otherwise the with of the main program may cause junk warnings.
670 if Ada_Bind_File then
671 Binder_Options.Increment_Last;
672 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
673 end if;
675 -- If we did not get an ali file at all, and we had at least one
676 -- linker option, then assume that was the intended ali file after
677 -- all, so that we get a nicer message later on.
679 if Ali_File_Name = null
680 and then Linker_Options.Last >= Linker_Options.First
681 then
682 Ali_File_Name :=
683 new String'(Linker_Options.Table (Linker_Options.First).all &
684 ".ali");
685 end if;
686 end Process_Args;
688 -------------------------
689 -- Process_Binder_File --
690 -------------------------
692 procedure Process_Binder_File (Name : String) is
693 Fd : FILEs;
694 -- Binder file's descriptor
696 Link_Bytes : Integer := 0;
697 -- Projected number of bytes for the linker command line
699 Link_Max : Integer;
700 pragma Import (C, Link_Max, "__gnat_link_max");
701 -- Maximum number of bytes on the command line supported by the OS
702 -- linker. Passed this limit the response file mechanism must be used
703 -- if supported.
705 Next_Line : String (1 .. 1000);
706 -- Current line value
708 Nlast : Integer;
709 Nfirst : Integer;
710 -- Current line slice (the slice does not contain line terminator)
712 Last : Integer;
713 -- Current line last character for shared libraries (without version)
715 Objs_Begin : Integer := 0;
716 -- First object file index in Linker_Objects table
718 Objs_End : Integer := 0;
719 -- Last object file index in Linker_Objects table
721 Status : int;
722 pragma Warnings (Off, Status);
723 -- Used for various Interfaces.C_Streams calls
725 Closing_Status : Boolean;
726 pragma Warnings (Off, Closing_Status);
727 -- For call to Close
729 GNAT_Static : Boolean := False;
730 -- Save state of -static option
732 GNAT_Shared : Boolean := False;
733 -- Save state of -shared option
735 Xlinker_Was_Previous : Boolean := False;
736 -- Indicate that "-Xlinker" was the option preceding the current
737 -- option. If True, then the current option is never suppressed.
739 -- Rollback data
741 -- These data items are used to store current binder file context.
742 -- The context is composed of the file descriptor position and the
743 -- current line together with the slice indexes (first and last
744 -- position) for this line. The rollback data are used by the
745 -- Store_File_Context and Rollback_File_Context routines below.
746 -- The file context mechanism interact only with the Get_Next_Line
747 -- call. For example:
749 -- Store_File_Context;
750 -- Get_Next_Line;
751 -- Rollback_File_Context;
752 -- Get_Next_Line;
754 -- Both Get_Next_Line calls above will read the exact same data from
755 -- the file. In other words, Next_Line, Nfirst and Nlast variables
756 -- will be set with the exact same values.
758 RB_File_Pos : long; -- File position
759 RB_Next_Line : String (1 .. 1000); -- Current line content
760 RB_Nlast : Integer; -- Slice last index
761 RB_Nfirst : Integer; -- Slice first index
763 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
764 pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
765 -- Pointer to string representing the native linker option which
766 -- specifies the path where the dynamic loader should find shared
767 -- libraries. Equal to null string if this system doesn't support it.
769 Libgcc_Subdir_Ptr : Interfaces.C.Strings.chars_ptr;
770 pragma Import (C, Libgcc_Subdir_Ptr, "__gnat_default_libgcc_subdir");
771 -- Pointer to string indicating the installation subdirectory where
772 -- a default shared libgcc might be found.
774 Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
775 pragma Import
776 (C, Object_Library_Ext_Ptr, "__gnat_object_library_extension");
777 -- Pointer to string specifying the default extension for
778 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
780 Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
781 pragma Import (C, Object_File_Option_Ptr, "__gnat_object_file_option");
782 -- Pointer to a string representing the linker option which specifies
783 -- the response file.
785 Using_GNU_Linker : Boolean;
786 for Using_GNU_Linker'Size use Character'Size;
787 pragma Import (C, Using_GNU_Linker, "__gnat_using_gnu_linker");
788 -- Predicate indicating whether this target uses the GNU linker. In
789 -- this case we must output a GNU linker compatible response file.
791 Separate_Run_Path_Options : Boolean;
792 for Separate_Run_Path_Options'Size use Character'Size;
793 pragma Import
794 (C, Separate_Run_Path_Options, "__gnat_separate_run_path_options");
795 -- Whether separate rpath options should be emitted for each directory
797 Opening : aliased constant String := """";
798 Closing : aliased constant String := '"' & ASCII.LF;
799 -- Needed to quote object paths in object list files when GNU linker
800 -- is used.
802 procedure Get_Next_Line;
803 -- Read the next line from the binder file without the line
804 -- terminator.
806 function Index (S, Pattern : String) return Natural;
807 -- Return the last occurrence of Pattern in S, or 0 if none
809 function Is_Option_Present (Opt : String) return Boolean;
810 -- Return true if the option Opt is already present in
811 -- Linker_Options table.
813 procedure Store_File_Context;
814 -- Store current file context, Fd position and current line data.
815 -- The file context is stored into the rollback data above (RB_*).
816 -- Store_File_Context can be called at any time, only the last call
817 -- will be used (i.e. this routine overwrites the file context).
819 procedure Rollback_File_Context;
820 -- Restore file context from rollback data. This routine must be called
821 -- after Store_File_Context. The binder file context will be restored
822 -- with the data stored by the last Store_File_Context call.
824 -------------------
825 -- Get_Next_Line --
826 -------------------
828 procedure Get_Next_Line is
829 Fchars : chars;
831 begin
832 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
834 if Fchars = System.Null_Address then
835 Exit_With_Error ("Error reading binder output");
836 end if;
838 Nfirst := Next_Line'First;
839 Nlast := Nfirst;
840 while Nlast <= Next_Line'Last
841 and then Next_Line (Nlast) /= ASCII.LF
842 and then Next_Line (Nlast) /= ASCII.CR
843 loop
844 Nlast := Nlast + 1;
845 end loop;
847 Nlast := Nlast - 1;
848 end Get_Next_Line;
850 -----------
851 -- Index --
852 -----------
854 function Index (S, Pattern : String) return Natural is
855 Len : constant Natural := Pattern'Length;
857 begin
858 for J in reverse S'First .. S'Last - Len + 1 loop
859 if Pattern = S (J .. J + Len - 1) then
860 return J;
861 end if;
862 end loop;
864 return 0;
865 end Index;
867 -----------------------
868 -- Is_Option_Present --
869 -----------------------
871 function Is_Option_Present (Opt : String) return Boolean is
872 begin
873 for I in 1 .. Linker_Options.Last loop
875 if Linker_Options.Table (I).all = Opt then
876 return True;
877 end if;
879 end loop;
881 return False;
882 end Is_Option_Present;
884 ---------------------------
885 -- Rollback_File_Context --
886 ---------------------------
888 procedure Rollback_File_Context is
889 begin
890 Next_Line := RB_Next_Line;
891 Nfirst := RB_Nfirst;
892 Nlast := RB_Nlast;
893 Status := fseek (Fd, RB_File_Pos, Interfaces.C_Streams.SEEK_SET);
895 if Status = -1 then
896 Exit_With_Error ("Error setting file position");
897 end if;
898 end Rollback_File_Context;
900 ------------------------
901 -- Store_File_Context --
902 ------------------------
904 procedure Store_File_Context is
905 use type System.CRTL.long;
906 begin
907 RB_Next_Line := Next_Line;
908 RB_Nfirst := Nfirst;
909 RB_Nlast := Nlast;
910 RB_File_Pos := ftell (Fd);
912 if RB_File_Pos = -1 then
913 Exit_With_Error ("Error getting file position");
914 end if;
915 end Store_File_Context;
917 -- Start of processing for Process_Binder_File
919 begin
920 Fd := fopen (Name'Address, Read_Mode'Address);
922 if Fd = NULL_Stream then
923 Exit_With_Error ("Failed to open binder output");
924 end if;
926 -- Skip up to the Begin Info line
928 loop
929 Get_Next_Line;
930 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
931 end loop;
933 loop
934 Get_Next_Line;
936 -- Go to end when end line is reached (this will happen in
937 -- High_Integrity_Mode where no -L switches are generated)
939 exit when Next_Line (Nfirst .. Nlast) = End_Info;
941 if Ada_Bind_File then
942 Next_Line (Nfirst .. Nlast - 8) :=
943 Next_Line (Nfirst + 8 .. Nlast);
944 Nlast := Nlast - 8;
945 end if;
947 -- Go to next section when switches are reached
949 exit when Next_Line (1) = '-';
951 -- Otherwise we have another object file to collect
953 Linker_Objects.Increment_Last;
955 -- Mark the positions of first and last object files in case
956 -- they need to be placed with a named file on systems having
957 -- linker line limitations.
959 if Objs_Begin = 0 then
960 Objs_Begin := Linker_Objects.Last;
961 end if;
963 Linker_Objects.Table (Linker_Objects.Last) :=
964 new String'(Next_Line (Nfirst .. Nlast));
966 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
967 -- Nlast - Nfirst + 1, for the size, plus one for the space between
968 -- each arguments.
969 end loop;
971 Objs_End := Linker_Objects.Last;
973 -- Continue to compute the Link_Bytes, the linker options are part of
974 -- command line length.
976 Store_File_Context;
978 while Next_Line (Nfirst .. Nlast) /= End_Info loop
979 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
980 Get_Next_Line;
981 end loop;
983 Rollback_File_Context;
985 -- On systems that have limitations on handling very long linker lines
986 -- we make use of the system linker option which takes a list of object
987 -- file names from a file instead of the command line itself. What we do
988 -- is to replace the list of object files by the special linker option
989 -- which then reads the object file list from a file instead. The option
990 -- to read from a file instead of the command line is only triggered if
991 -- a conservative threshold is passed.
993 if VM_Target = CLI_Target
994 and then Link_Bytes > Link_Max
995 then
996 Lname := new String'("l~" & Base_Name (Ali_File_Name.all) & ".il");
998 for J in Objs_Begin .. Objs_End loop
999 Copy_File (Linker_Objects.Table (J).all, Lname.all,
1000 Success => Closing_Status,
1001 Mode => Append);
1002 end loop;
1004 -- Add the special objects list file option together with the name
1005 -- of the temporary file to the objects file table.
1007 Linker_Objects.Table (Objs_Begin) :=
1008 new String'(Value (Object_File_Option_Ptr) & Lname.all);
1010 -- The slots containing these object file names are then removed
1011 -- from the objects table so they do not appear in the link. They
1012 -- are removed by moving up the linker options and non-Ada object
1013 -- files appearing after the Ada object list in the table.
1015 declare
1016 N : Integer;
1018 begin
1019 N := Objs_End - Objs_Begin + 1;
1021 for J in Objs_End + 1 .. Linker_Objects.Last loop
1022 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
1023 end loop;
1025 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
1026 end;
1028 elsif Object_List_File_Required
1029 or else (Object_List_File_Supported
1030 and then Link_Bytes > Link_Max)
1031 then
1032 -- Create a temporary file containing the Ada user object files
1033 -- needed by the link. This list is taken from the bind file
1034 -- and is output one object per line for maximal compatibility with
1035 -- linkers supporting this option.
1037 Create_Temp_File (Tname_FD, Tname);
1039 -- ??? File descriptor should be checked to not be Invalid_FD.
1040 -- ??? Status of Write and Close operations should be checked, and
1041 -- failure should occur if a status is wrong.
1043 -- If target is using the GNU linker we must add a special header
1044 -- and footer in the response file.
1046 -- The syntax is : INPUT (object1.o object2.o ... )
1048 -- Because the GNU linker does not like name with characters such
1049 -- as '!', we must put the object paths between double quotes.
1051 if Using_GNU_Linker then
1052 declare
1053 GNU_Header : aliased constant String := "INPUT (";
1055 begin
1056 Status := Write (Tname_FD, GNU_Header'Address,
1057 GNU_Header'Length);
1058 end;
1059 end if;
1061 for J in Objs_Begin .. Objs_End loop
1063 -- Opening quote for GNU linker
1065 if Using_GNU_Linker then
1066 Status := Write (Tname_FD, Opening'Address, 1);
1067 end if;
1069 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
1070 Linker_Objects.Table (J).all'Length);
1072 -- Closing quote for GNU linker
1074 if Using_GNU_Linker then
1075 Status := Write (Tname_FD, Closing'Address, 2);
1077 else
1078 Status := Write (Tname_FD, ASCII.LF'Address, 1);
1079 end if;
1081 Response_File_Objects.Increment_Last;
1082 Response_File_Objects.Table (Response_File_Objects.Last) :=
1083 Linker_Objects.Table (J);
1084 end loop;
1086 -- Handle GNU linker response file footer
1088 if Using_GNU_Linker then
1089 declare
1090 GNU_Footer : aliased constant String := ")";
1092 begin
1093 Status := Write (Tname_FD, GNU_Footer'Address,
1094 GNU_Footer'Length);
1095 end;
1096 end if;
1098 Close (Tname_FD, Closing_Status);
1100 -- Add the special objects list file option together with the name
1101 -- of the temporary file (removing the null character) to the objects
1102 -- file table.
1104 Linker_Objects.Table (Objs_Begin) :=
1105 new String'(Value (Object_File_Option_Ptr) &
1106 Tname (Tname'First .. Tname'Last - 1));
1108 -- The slots containing these object file names are then removed
1109 -- from the objects table so they do not appear in the link. They
1110 -- are removed by moving up the linker options and non-Ada object
1111 -- files appearing after the Ada object list in the table.
1113 declare
1114 N : Integer;
1116 begin
1117 N := Objs_End - Objs_Begin + 1;
1119 for J in Objs_End + 1 .. Linker_Objects.Last loop
1120 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
1121 end loop;
1123 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
1124 end;
1125 end if;
1127 -- Process switches and options
1129 if Next_Line (Nfirst .. Nlast) /= End_Info then
1130 Xlinker_Was_Previous := False;
1132 loop
1133 if Xlinker_Was_Previous
1134 or else Next_Line (Nfirst .. Nlast) = "-Xlinker"
1135 then
1136 Linker_Options.Increment_Last;
1137 Linker_Options.Table (Linker_Options.Last) :=
1138 new String'(Next_Line (Nfirst .. Nlast));
1140 elsif Next_Line (Nfirst .. Nlast) = "-static" then
1141 GNAT_Static := True;
1143 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1144 GNAT_Shared := True;
1146 -- Add binder options only if not already set on the command
1147 -- line. This rule is a way to control the linker options order.
1149 -- The following test needs comments, why is it VMS specific.
1150 -- The above comment looks out of date ???
1152 elsif not (OpenVMS_On_Target
1153 and then
1154 Is_Option_Present (Next_Line (Nfirst .. Nlast)))
1155 then
1156 if Nlast > Nfirst + 2 and then
1157 Next_Line (Nfirst .. Nfirst + 1) = "-L"
1158 then
1159 -- Construct a library search path for use later
1160 -- to locate static gnatlib libraries.
1162 if Libpath.Last > 1 then
1163 Libpath.Increment_Last;
1164 Libpath.Table (Libpath.Last) := Path_Separator;
1165 end if;
1167 for I in Nfirst + 2 .. Nlast loop
1168 Libpath.Increment_Last;
1169 Libpath.Table (Libpath.Last) := Next_Line (I);
1170 end loop;
1172 Linker_Options.Increment_Last;
1174 Linker_Options.Table (Linker_Options.Last) :=
1175 new String'(Next_Line (Nfirst .. Nlast));
1177 elsif Next_Line (Nfirst .. Nlast) = "-ldecgnat"
1178 or else Next_Line (Nfirst .. Nlast) = "-lgnarl"
1179 or else Next_Line (Nfirst .. Nlast) = "-lgnat"
1180 or else Next_Line
1181 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) =
1182 Shared_Lib ("gnarl")
1183 or else Next_Line
1184 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) =
1185 Shared_Lib ("gnat")
1186 then
1187 -- If it is a shared library, remove the library version.
1188 -- We will be looking for the static version of the library
1189 -- as it is in the same directory as the shared version.
1191 if Next_Line (Nlast - Library_Version'Length + 1 .. Nlast)
1192 = Library_Version
1193 then
1194 -- Set Last to point to last character before the
1195 -- library version.
1197 Last := Nlast - Library_Version'Length - 1;
1198 else
1199 Last := Nlast;
1200 end if;
1202 -- Given a Gnat standard library, search the library path to
1203 -- find the library location.
1205 -- Shouldn't we abstract a proc here, we are getting awfully
1206 -- heavily nested ???
1208 declare
1209 File_Path : String_Access;
1211 Object_Lib_Extension : constant String :=
1212 Value (Object_Library_Ext_Ptr);
1214 File_Name : constant String := "lib" &
1215 Next_Line (Nfirst + 2 .. Last) &
1216 Object_Lib_Extension;
1218 Run_Path_Opt : constant String :=
1219 Value (Run_Path_Option_Ptr);
1221 GCC_Index : Natural;
1222 Run_Path_Opt_Index : Natural := 0;
1224 begin
1225 File_Path :=
1226 Locate_Regular_File (File_Name,
1227 String (Libpath.Table (1 .. Libpath.Last)));
1229 if File_Path /= null then
1230 if GNAT_Static then
1232 -- If static gnatlib found, explicitly
1233 -- specify to overcome possible linker
1234 -- default usage of shared version.
1236 Linker_Options.Increment_Last;
1238 Linker_Options.Table (Linker_Options.Last) :=
1239 new String'(File_Path.all);
1241 elsif GNAT_Shared then
1242 if Opt.Run_Path_Option then
1244 -- If shared gnatlib desired, add the
1245 -- appropriate system specific switch
1246 -- so that it can be located at runtime.
1248 if Run_Path_Opt'Length /= 0 then
1250 -- Output the system specific linker command
1251 -- that allows the image activator to find
1252 -- the shared library at runtime. Also add
1253 -- path to find libgcc_s.so, if relevant.
1255 declare
1256 Path : String (1 .. File_Path'Length + 15);
1257 Path_Last : constant Natural :=
1258 File_Path'Length;
1260 begin
1261 Path (1 .. File_Path'Length) :=
1262 File_Path.all;
1264 -- To find the location of the shared version
1265 -- of libgcc, we look for "gcc-lib" in the
1266 -- path of the library. However, this
1267 -- subdirectory is no longer present in
1268 -- recent versions of GCC. So, we look for
1269 -- the last subdirectory "lib" in the path.
1271 GCC_Index :=
1272 Index (Path (1 .. Path_Last), "gcc-lib");
1274 if GCC_Index /= 0 then
1276 -- The shared version of libgcc is
1277 -- located in the parent directory.
1279 GCC_Index := GCC_Index - 1;
1281 else
1282 GCC_Index :=
1283 Index
1284 (Path (1 .. Path_Last),
1285 "/lib/");
1287 if GCC_Index = 0 then
1288 GCC_Index :=
1289 Index (Path (1 .. Path_Last),
1290 Directory_Separator &
1291 "lib" &
1292 Directory_Separator);
1293 end if;
1295 -- If we have found a "lib" subdir in
1296 -- the path to libgnat, the possible
1297 -- shared libgcc of interest by default
1298 -- is in libgcc_subdir at the same
1299 -- level.
1301 if GCC_Index /= 0 then
1302 declare
1303 Subdir : constant String :=
1304 Value (Libgcc_Subdir_Ptr);
1305 begin
1306 Path
1307 (GCC_Index + 1 ..
1308 GCC_Index + Subdir'Length) :=
1309 Subdir;
1310 GCC_Index :=
1311 GCC_Index + Subdir'Length;
1312 end;
1313 end if;
1314 end if;
1316 -- Look for an eventual run_path_option in
1317 -- the linker switches.
1319 if Separate_Run_Path_Options then
1320 Linker_Options.Increment_Last;
1321 Linker_Options.Table
1322 (Linker_Options.Last) :=
1323 new String'
1324 (Run_Path_Opt
1325 & File_Path
1326 (1 .. File_Path'Length
1327 - File_Name'Length));
1329 if GCC_Index /= 0 then
1330 Linker_Options.Increment_Last;
1331 Linker_Options.Table
1332 (Linker_Options.Last) :=
1333 new String'
1334 (Run_Path_Opt
1335 & Path (1 .. GCC_Index));
1336 end if;
1338 else
1339 for J in reverse
1340 1 .. Linker_Options.Last
1341 loop
1342 if Linker_Options.Table (J) /= null
1343 and then
1344 Linker_Options.Table (J)'Length
1345 > Run_Path_Opt'Length
1346 and then
1347 Linker_Options.Table (J)
1348 (1 .. Run_Path_Opt'Length) =
1349 Run_Path_Opt
1350 then
1351 -- We have found an already
1352 -- specified run_path_option: we
1353 -- will add to this switch,
1354 -- because only one
1355 -- run_path_option should be
1356 -- specified.
1358 Run_Path_Opt_Index := J;
1359 exit;
1360 end if;
1361 end loop;
1363 -- If there is no run_path_option, we
1364 -- need to add one.
1366 if Run_Path_Opt_Index = 0 then
1367 Linker_Options.Increment_Last;
1368 end if;
1370 if GCC_Index = 0 then
1371 if Run_Path_Opt_Index = 0 then
1372 Linker_Options.Table
1373 (Linker_Options.Last) :=
1374 new String'
1375 (Run_Path_Opt
1376 & File_Path
1377 (1 .. File_Path'Length
1378 - File_Name'Length));
1380 else
1381 Linker_Options.Table
1382 (Run_Path_Opt_Index) :=
1383 new String'
1384 (Linker_Options.Table
1385 (Run_Path_Opt_Index).all
1386 & Path_Separator
1387 & File_Path
1388 (1 .. File_Path'Length
1389 - File_Name'Length));
1390 end if;
1392 else
1393 if Run_Path_Opt_Index = 0 then
1394 Linker_Options.Table
1395 (Linker_Options.Last) :=
1396 new String'
1397 (Run_Path_Opt
1398 & File_Path
1399 (1 .. File_Path'Length
1400 - File_Name'Length)
1401 & Path_Separator
1402 & Path (1 .. GCC_Index));
1404 else
1405 Linker_Options.Table
1406 (Run_Path_Opt_Index) :=
1407 new String'
1408 (Linker_Options.Table
1409 (Run_Path_Opt_Index).all
1410 & Path_Separator
1411 & File_Path
1412 (1 .. File_Path'Length
1413 - File_Name'Length)
1414 & Path_Separator
1415 & Path (1 .. GCC_Index));
1416 end if;
1417 end if;
1418 end if;
1419 end;
1420 end if;
1421 end if;
1423 -- Then we add the appropriate -l switch
1425 Linker_Options.Increment_Last;
1426 Linker_Options.Table (Linker_Options.Last) :=
1427 new String'(Next_Line (Nfirst .. Nlast));
1428 end if;
1430 else
1431 -- If gnatlib library not found, then
1432 -- add it anyway in case some other
1433 -- mechanism may find it.
1435 Linker_Options.Increment_Last;
1436 Linker_Options.Table (Linker_Options.Last) :=
1437 new String'(Next_Line (Nfirst .. Nlast));
1438 end if;
1439 end;
1440 else
1441 Linker_Options.Increment_Last;
1442 Linker_Options.Table (Linker_Options.Last) :=
1443 new String'(Next_Line (Nfirst .. Nlast));
1444 end if;
1445 end if;
1447 Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker";
1449 Get_Next_Line;
1450 exit when Next_Line (Nfirst .. Nlast) = End_Info;
1452 if Ada_Bind_File then
1453 Next_Line (Nfirst .. Nlast - 8) :=
1454 Next_Line (Nfirst + 8 .. Nlast);
1455 Nlast := Nlast - 8;
1456 end if;
1457 end loop;
1458 end if;
1460 -- If -shared was specified, invoke gcc with -shared-libgcc
1462 if GNAT_Shared then
1463 Linker_Options.Increment_Last;
1464 Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc;
1465 end if;
1467 Status := fclose (Fd);
1468 end Process_Binder_File;
1470 -----------
1471 -- Usage --
1472 -----------
1474 procedure Usage is
1475 begin
1476 Write_Str ("Usage: ");
1477 Write_Str (Base_Name (Command_Name));
1478 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
1479 Write_Eol;
1480 Write_Eol;
1481 Write_Line (" mainprog.ali the ALI file of the main program");
1482 Write_Eol;
1483 Write_Line (" -f force object file list to be generated");
1484 Write_Line (" -g Compile binder source file with debug information");
1485 Write_Line (" -n Do not compile the binder source file");
1486 Write_Line (" -R Do not use a run_path_option");
1487 Write_Line (" -v verbose mode");
1488 Write_Line (" -v -v very verbose mode");
1489 Write_Eol;
1490 Write_Line (" -o nam Use 'nam' as the name of the executable");
1491 Write_Line (" -b target Compile the binder source to run on target");
1492 Write_Line (" -Bdir Load compiler executables from dir");
1494 if Is_Supported (Map_File) then
1495 Write_Line (" -Mmap Create map file map");
1496 Write_Line (" -M Create map file mainprog.map");
1497 end if;
1499 Write_Line (" --GCC=comp Use comp as the compiler");
1500 Write_Line (" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1501 Write_Eol;
1502 Write_Line (" [non-Ada-objects] list of non Ada object files");
1503 Write_Line (" [linker-options] other options for the linker");
1504 end Usage;
1506 ------------------
1507 -- Write_Header --
1508 ------------------
1510 procedure Write_Header is
1511 begin
1512 if Verbose_Mode then
1513 Write_Eol;
1514 Display_Version ("GNATLINK", "1995");
1515 end if;
1516 end Write_Header;
1518 -----------------
1519 -- Write_Usage --
1520 -----------------
1522 procedure Write_Usage is
1523 begin
1524 Write_Header;
1525 Usage;
1526 end Write_Usage;
1528 -- Start of processing for Gnatlink
1530 begin
1531 -- Add the directory where gnatlink is invoked in front of the path, if
1532 -- gnatlink is invoked with directory information. Only do this if the
1533 -- platform is not VMS, where the notion of path does not really exist.
1535 if not Hostparm.OpenVMS then
1536 declare
1537 Command : constant String := Command_Name;
1539 begin
1540 for Index in reverse Command'Range loop
1541 if Command (Index) = Directory_Separator then
1542 declare
1543 Absolute_Dir : constant String :=
1544 Normalize_Pathname
1545 (Command (Command'First .. Index));
1547 PATH : constant String :=
1548 Absolute_Dir &
1549 Path_Separator &
1550 Getenv ("PATH").all;
1552 begin
1553 Setenv ("PATH", PATH);
1554 end;
1556 exit;
1557 end if;
1558 end loop;
1559 end;
1560 end if;
1562 Process_Args;
1564 if Argument_Count = 0
1565 or else (Verbose_Mode and then Argument_Count = 1)
1566 then
1567 Write_Usage;
1568 Exit_Program (E_Fatal);
1569 end if;
1571 -- Initialize packages to be used
1573 Csets.Initialize;
1574 Snames.Initialize;
1576 -- We always compile with -c
1578 Binder_Options_From_ALI.Increment_Last;
1579 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1580 new String'("-c");
1582 if Ali_File_Name = null then
1583 Exit_With_Error ("no ali file given for link");
1584 end if;
1586 if not Is_Regular_File (Ali_File_Name.all) then
1587 Exit_With_Error (Ali_File_Name.all & " not found");
1588 end if;
1590 -- Read the ALI file of the main subprogram if the binder generated file
1591 -- needs to be compiled and no --GCC= switch has been specified. Fetch the
1592 -- back end switches from this ALI file and use these switches to compile
1593 -- the binder generated file
1595 if Compile_Bind_File and then Standard_Gcc then
1596 Initialize_ALI;
1597 Name_Len := Ali_File_Name'Length;
1598 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1600 declare
1601 use Types;
1602 F : constant File_Name_Type := Name_Find;
1603 T : Text_Buffer_Ptr;
1604 A : ALI_Id;
1606 begin
1607 -- Load the ALI file
1609 T := Read_Library_Info (F, True);
1611 -- Read it. Note that we ignore errors, since we only want very
1612 -- limited information from the ali file, and likely a slightly
1613 -- wrong version will be just fine, though in normal operation
1614 -- we don't expect this to happen!
1616 A := Scan_ALI
1619 Ignore_ED => False,
1620 Err => False,
1621 Ignore_Errors => True);
1623 if A /= No_ALI_Id then
1625 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1626 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1627 loop
1628 -- Do not compile with the front end switches. However, --RTS
1629 -- is to be dealt with specially because it needs to be passed
1630 -- if the binder-generated file is in Ada and may also be used
1631 -- to drive the linker.
1633 declare
1634 Arg : String_Ptr renames Args.Table (Index);
1635 begin
1636 if not Is_Front_End_Switch (Arg.all) then
1637 Binder_Options_From_ALI.Increment_Last;
1638 Binder_Options_From_ALI.Table
1639 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1641 elsif Arg'Length > 5
1642 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1643 then
1644 if Ada_Bind_File then
1645 Binder_Options_From_ALI.Increment_Last;
1646 Binder_Options_From_ALI.Table
1647 (Binder_Options_From_ALI.Last)
1648 := String_Access (Arg);
1649 end if;
1651 -- Set the RTS_*_Path_Name variables, so that
1652 -- the correct directories will be set when
1653 -- Osint.Add_Default_Search_Dirs will be called later.
1655 Opt.RTS_Src_Path_Name :=
1656 Get_RTS_Search_Dir
1657 (Arg (Arg'First + 6 .. Arg'Last), Include);
1659 Opt.RTS_Lib_Path_Name :=
1660 Get_RTS_Search_Dir
1661 (Arg (Arg'First + 6 .. Arg'Last), Objects);
1663 -- GNAT doesn't support the GCC multilib mechanism.
1664 -- This means that, when a multilib switch is used
1665 -- to request a particular compilation mode, the
1666 -- corresponding runtime switch (--RTS) must also be
1667 -- specified. The long-term goal is to fully support the
1668 -- multilib mechanism; however, in the meantime, it is
1669 -- convenient to eliminate the redundancy by keying the
1670 -- compilation mode on a single switch, namely --RTS.
1672 -- Pass -mrtp to the linker if --RTS=rtp was passed
1674 if Arg'Length > 8
1675 and then Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1676 then
1677 Linker_Options.Increment_Last;
1678 Linker_Options.Table (Linker_Options.Last) :=
1679 new String'("-mrtp");
1680 end if;
1681 end if;
1682 end;
1683 end loop;
1684 end if;
1685 end;
1686 end if;
1688 -- Get target parameters
1690 Osint.Add_Default_Search_Dirs;
1691 Targparm.Get_Target_Parameters;
1693 if VM_Target /= No_VM then
1694 case VM_Target is
1695 when JVM_Target => Gcc := new String'("jvm-gnatcompile");
1696 when CLI_Target => Gcc := new String'("dotnet-gnatcompile");
1697 when No_VM => raise Program_Error;
1698 end case;
1700 Ada_Bind_File := True;
1701 Begin_Info := "-- BEGIN Object file/option list";
1702 End_Info := "-- END Object file/option list ";
1703 end if;
1705 -- If the main program is in Ada it is compiled with the following
1706 -- switches:
1708 -- -gnatA stops reading gnat.adc, since we don't know what
1709 -- pragmas would work, and we do not need it anyway.
1711 -- -gnatWb allows brackets coding for wide characters
1713 -- -gnatiw allows wide characters in identifiers. This is needed
1714 -- because bindgen uses brackets encoding for all upper
1715 -- half and wide characters in identifier names.
1717 if Ada_Bind_File then
1718 Binder_Options_From_ALI.Increment_Last;
1719 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1720 new String'("-gnatA");
1721 Binder_Options_From_ALI.Increment_Last;
1722 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1723 new String'("-gnatWb");
1724 Binder_Options_From_ALI.Increment_Last;
1725 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1726 new String'("-gnatiw");
1727 end if;
1729 -- Locate all the necessary programs and verify required files are present
1731 Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1733 if Gcc_Path = null then
1734 Exit_With_Error ("Couldn't locate " & Gcc.all);
1735 end if;
1737 if Linker_Path = null then
1738 if VM_Target = CLI_Target then
1739 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("ilasm");
1741 if Linker_Path = null then
1742 Exit_With_Error ("Couldn't locate ilasm");
1743 end if;
1745 elsif RTX_RTSS_Kernel_Module_On_Target then
1747 -- Use Microsoft linker for RTSS modules
1749 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("link");
1751 if Linker_Path = null then
1752 Exit_With_Error ("Couldn't locate link");
1753 end if;
1755 else
1756 Linker_Path := Gcc_Path;
1757 end if;
1758 end if;
1760 Write_Header;
1762 -- If no output name specified, then use the base name of .ali file name
1764 if Output_File_Name = null then
1765 Output_File_Name :=
1766 new String'(Base_Name (Ali_File_Name.all)
1767 & Get_Target_Debuggable_Suffix.all);
1768 end if;
1770 if VM_Target = CLI_Target then
1771 Linker_Options.Increment_Last;
1772 Linker_Options.Table (Linker_Options.Last) := new String'("/QUIET");
1774 Linker_Options.Increment_Last;
1775 Linker_Options.Table (Linker_Options.Last) := new String'("/DEBUG");
1777 Linker_Options.Increment_Last;
1778 Linker_Options.Table (Linker_Options.Last) :=
1779 new String'("/OUTPUT=" & Output_File_Name.all);
1781 elsif RTX_RTSS_Kernel_Module_On_Target then
1782 Linker_Options.Increment_Last;
1783 Linker_Options.Table (Linker_Options.Last) :=
1784 new String'("/OUT:" & Output_File_Name.all);
1786 else
1787 Linker_Options.Increment_Last;
1788 Linker_Options.Table (Linker_Options.Last) := new String'("-o");
1790 Linker_Options.Increment_Last;
1791 Linker_Options.Table (Linker_Options.Last) :=
1792 new String'(Output_File_Name.all);
1793 end if;
1795 Check_Existing_Executable (Output_File_Name.all);
1797 -- Warn if main program is called "test", as that may be a built-in command
1798 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1799 -- will not appear. However, do not warn in the case of a cross compiler.
1801 -- Assume this is a cross tool if the executable name is not gnatlink
1803 if Base_Name (Command_Name) = "gnatlink"
1804 and then Output_File_Name.all = "test"
1805 then
1806 Error_Msg ("warning: executable name """ & Output_File_Name.all
1807 & """ may conflict with shell command");
1808 end if;
1810 -- If -M switch was specified, add the switches to create the map file
1812 if Create_Map_File then
1813 declare
1814 Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map";
1815 Switches : String_List_Access;
1817 begin
1818 Convert (Map_File, Map_Name, Switches);
1820 if Switches /= null then
1821 for J in Switches'Range loop
1822 Linker_Options.Increment_Last;
1823 Linker_Options.Table (Linker_Options.Last) := Switches (J);
1824 end loop;
1825 end if;
1826 end;
1827 end if;
1829 -- Perform consistency checks
1831 -- Transform the .ali file name into the binder output file name
1833 Make_Binder_File_Names : declare
1834 Fname : constant String := Base_Name (Ali_File_Name.all);
1835 Fname_Len : Integer := Fname'Length;
1837 function Get_Maximum_File_Name_Length return Integer;
1838 pragma Import (C, Get_Maximum_File_Name_Length,
1839 "__gnat_get_maximum_file_name_length");
1841 Maximum_File_Name_Length : constant Integer :=
1842 Get_Maximum_File_Name_Length;
1844 Bind_File_Prefix : Types.String_Ptr;
1845 -- Contains prefix used for bind files
1847 begin
1848 -- Set prefix
1850 if not Ada_Bind_File then
1851 Bind_File_Prefix := new String'("b_");
1852 elsif OpenVMS_On_Target then
1853 Bind_File_Prefix := new String'("b__");
1854 else
1855 Bind_File_Prefix := new String'("b~");
1856 end if;
1858 -- If the length of the binder file becomes too long due to
1859 -- the addition of the "b?" prefix, then truncate it.
1861 if Maximum_File_Name_Length > 0 then
1862 while Fname_Len >
1863 Maximum_File_Name_Length - Bind_File_Prefix.all'Length
1864 loop
1865 Fname_Len := Fname_Len - 1;
1866 end loop;
1867 end if;
1869 declare
1870 Fnam : constant String :=
1871 Bind_File_Prefix.all &
1872 Fname (Fname'First .. Fname'First + Fname_Len - 1);
1874 begin
1875 if Ada_Bind_File then
1876 Binder_Spec_Src_File := new String'(Fnam & ".ads");
1877 Binder_Body_Src_File := new String'(Fnam & ".adb");
1878 Binder_Ali_File := new String'(Fnam & ".ali");
1879 else
1880 Binder_Body_Src_File := new String'(Fnam & ".c");
1881 end if;
1883 Binder_Obj_File := new String'(Fnam & Get_Target_Object_Suffix.all);
1884 end;
1886 if Fname_Len /= Fname'Length then
1887 Binder_Options.Increment_Last;
1888 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1889 Binder_Options.Increment_Last;
1890 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1891 end if;
1892 end Make_Binder_File_Names;
1894 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1896 -- Compile the binder file. This is fast, so we always do it, unless
1897 -- specifically told not to by the -n switch
1899 if Compile_Bind_File then
1900 Bind_Step : declare
1901 Success : Boolean;
1902 Args : Argument_List
1903 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1905 begin
1906 for J in 1 .. Binder_Options_From_ALI.Last loop
1907 Args (J) := Binder_Options_From_ALI.Table (J);
1908 end loop;
1910 for J in 1 .. Binder_Options.Last loop
1911 Args (Binder_Options_From_ALI.Last + J) :=
1912 Binder_Options.Table (J);
1913 end loop;
1915 -- Use the full path of the binder generated source, so that it is
1916 -- guaranteed that the debugger will find this source, even with
1917 -- STABS.
1919 Args (Args'Last) :=
1920 new String'(Normalize_Pathname (Binder_Body_Src_File.all));
1922 if Verbose_Mode then
1923 Write_Str (Base_Name (Gcc_Path.all));
1925 for J in Args'Range loop
1926 Write_Str (" ");
1927 Write_Str (Args (J).all);
1928 end loop;
1930 Write_Eol;
1931 end if;
1933 System.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1935 if not Success then
1936 Exit_Program (E_Fatal);
1937 end if;
1938 end Bind_Step;
1939 end if;
1941 -- Now, actually link the program
1943 -- Skip this step for now on JVM since the Java interpreter will do
1944 -- the actual link at run time. We might consider packing all class files
1945 -- in a .zip file during this step.
1947 if VM_Target /= JVM_Target then
1948 Link_Step : declare
1949 Num_Args : Natural :=
1950 (Linker_Options.Last - Linker_Options.First + 1) +
1951 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1952 (Linker_Objects.Last - Linker_Objects.First + 1);
1953 Stack_Op : Boolean := False;
1954 IDENT_Op : Boolean := False;
1956 begin
1957 if VM_Target = CLI_Target then
1959 -- Remove extraneous flags not relevant for CIL. Also remove empty
1960 -- arguments, since ilasm chokes on them.
1962 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1963 if Linker_Options.Table (J)'Length = 0
1964 or else Linker_Options.Table (J) (1 .. 2) = "-L"
1965 or else Linker_Options.Table (J) (1 .. 2) = "-l"
1966 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1967 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1968 or else Linker_Options.Table (J) (1 .. 2) = "-g"
1969 then
1970 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1971 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1972 Linker_Options.Decrement_Last;
1973 Num_Args := Num_Args - 1;
1974 end if;
1975 end loop;
1977 elsif RTX_RTSS_Kernel_Module_On_Target then
1979 -- Remove flags not relevant for Microsoft linker and adapt some
1980 -- others.
1982 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1984 -- Remove flags that are not accepted
1985 if Linker_Options.Table (J)'Length = 0
1986 or else Linker_Options.Table (J) (1 .. 2) = "-l"
1987 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1988 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1989 or else Linker_Options.Table (J) (1 .. 8) = "-Xlinker"
1990 or else Linker_Options.Table (J) (1 .. 9) = "-mthreads"
1991 then
1992 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1993 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1994 Linker_Options.Decrement_Last;
1995 Num_Args := Num_Args - 1;
1997 -- Replace "-L" by its counterpart "/LIBPATH:" and UNIX "/" by
1998 -- Windows "\".
1999 elsif Linker_Options.Table (J) (1 .. 2) = "-L" then
2000 declare
2001 Libpath_Option : constant String_Access := new String'
2002 ("/LIBPATH:" &
2003 Linker_Options.Table (J)
2004 (3 .. Linker_Options.Table (J).all'Last));
2005 begin
2006 for Index in 10 .. Libpath_Option'Last loop
2007 if Libpath_Option (Index) = '/' then
2008 Libpath_Option (Index) := '\';
2009 end if;
2010 end loop;
2012 Linker_Options.Table (J) := Libpath_Option;
2013 end;
2015 -- Replace "-g" by "/DEBUG"
2016 elsif Linker_Options.Table (J) (1 .. 2) = "-g" then
2017 Linker_Options.Table (J) := new String'("/DEBUG");
2019 -- Replace "-o" by "/OUT:"
2020 elsif Linker_Options.Table (J) (1 .. 2) = "-o" then
2021 Linker_Options.Table (J + 1) := new String'
2022 ("/OUT:" & Linker_Options.Table (J + 1).all);
2024 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2025 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2026 Linker_Options.Decrement_Last;
2027 Num_Args := Num_Args - 1;
2029 -- Replace "--stack=" by "/STACK:"
2030 elsif Linker_Options.Table (J) (1 .. 8) = "--stack=" then
2031 Linker_Options.Table (J) := new String'
2032 ("/STACK:" &
2033 Linker_Options.Table (J)
2034 (9 .. Linker_Options.Table (J).all'Last));
2036 -- Replace "-v" by its counterpart "/VERBOSE"
2037 elsif Linker_Options.Table (J) (1 .. 2) = "-v" then
2038 Linker_Options.Table (J) := new String'("/VERBOSE");
2039 end if;
2040 end loop;
2042 -- Add some required flags to create RTSS modules
2044 declare
2045 Flags_For_Linker : constant array (1 .. 17) of String_Access :=
2046 (new String'("/NODEFAULTLIB"),
2047 new String'("/INCREMENTAL:NO"),
2048 new String'("/NOLOGO"),
2049 new String'("/DRIVER"),
2050 new String'("/ALIGN:0x20"),
2051 new String'("/SUBSYSTEM:NATIVE"),
2052 new String'("/ENTRY:_RtapiProcessEntryCRT@8"),
2053 new String'("/RELEASE"),
2054 new String'("startupCRT.obj"),
2055 new String'("rtxlibcmt.lib"),
2056 new String'("oldnames.lib"),
2057 new String'("rtapi_rtss.lib"),
2058 new String'("Rtx_Rtss.lib"),
2059 new String'("libkernel32.a"),
2060 new String'("libws2_32.a"),
2061 new String'("libmswsock.a"),
2062 new String'("libadvapi32.a"));
2063 -- These flags need to be passed to Microsoft linker. They
2064 -- come from the RTX documentation.
2066 Gcc_Lib_Path : constant String_Access := new String'
2067 ("/LIBPATH:" & Include_Dir_Default_Prefix & "\..\");
2068 -- Place to look for gcc related libraries, such as libgcc
2070 begin
2071 -- Replace UNIX "/" by Windows "\" in the path
2073 for Index in 10 .. Gcc_Lib_Path.all'Last loop
2074 if Gcc_Lib_Path (Index) = '/' then
2075 Gcc_Lib_Path (Index) := '\';
2076 end if;
2077 end loop;
2079 Linker_Options.Increment_Last;
2080 Linker_Options.Table (Linker_Options.Last) := Gcc_Lib_Path;
2081 Num_Args := Num_Args + 1;
2083 for Index in Flags_For_Linker'Range loop
2084 Linker_Options.Increment_Last;
2085 Linker_Options.Table (Linker_Options.Last) :=
2086 Flags_For_Linker (Index);
2087 Num_Args := Num_Args + 1;
2088 end loop;
2089 end;
2090 end if;
2092 -- Remove duplicate stack size setting from the Linker_Options
2093 -- table. The stack setting option "-Xlinker --stack=R,C" can be
2094 -- found in one line when set by a pragma Linker_Options or in two
2095 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
2096 -- line. We also check for the "-Wl,--stack=R" style option.
2098 -- We must remove the second stack setting option instance
2099 -- because the one on the command line will always be the first
2100 -- one. And any subsequent stack setting option will overwrite the
2101 -- previous one. This is done especially for GNAT/NT where we set
2102 -- the stack size for tasking programs by a pragma in the NT
2103 -- specific tasking package System.Task_Primitives.Operations.
2105 -- Note: This is not a FOR loop that runs from Linker_Options.First
2106 -- to Linker_Options.Last, since operations within the loop can
2107 -- modify the length of the table.
2109 Clean_Link_Option_Set : declare
2110 J : Natural := Linker_Options.First;
2111 Shared_Libgcc_Seen : Boolean := False;
2113 begin
2114 while J <= Linker_Options.Last loop
2116 if Linker_Options.Table (J).all = "-Xlinker"
2117 and then J < Linker_Options.Last
2118 and then Linker_Options.Table (J + 1)'Length > 8
2119 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
2120 then
2121 if Stack_Op then
2122 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
2123 Linker_Options.Table (J + 2 .. Linker_Options.Last);
2124 Linker_Options.Decrement_Last;
2125 Linker_Options.Decrement_Last;
2126 Num_Args := Num_Args - 2;
2128 else
2129 Stack_Op := True;
2130 end if;
2131 end if;
2133 -- Remove duplicate -shared-libgcc switch
2135 if Linker_Options.Table (J).all = Shared_Libgcc_String then
2136 if Shared_Libgcc_Seen then
2137 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2138 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2139 Linker_Options.Decrement_Last;
2140 Num_Args := Num_Args - 1;
2142 else
2143 Shared_Libgcc_Seen := True;
2144 end if;
2145 end if;
2147 -- Here we just check for a canonical form that matches the
2148 -- pragma Linker_Options set in the NT runtime.
2150 if (Linker_Options.Table (J)'Length > 17
2151 and then Linker_Options.Table (J) (1 .. 17)
2152 = "-Xlinker --stack=")
2153 or else
2154 (Linker_Options.Table (J)'Length > 12
2155 and then Linker_Options.Table (J) (1 .. 12)
2156 = "-Wl,--stack=")
2157 then
2158 if Stack_Op then
2159 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2160 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2161 Linker_Options.Decrement_Last;
2162 Num_Args := Num_Args - 1;
2164 else
2165 Stack_Op := True;
2166 end if;
2167 end if;
2169 -- Remove duplicate IDENTIFICATION directives (VMS)
2171 if Linker_Options.Table (J)'Length > 29
2172 and then Linker_Options.Table (J) (1 .. 30) =
2173 "--for-linker=--identification="
2174 then
2175 if IDENT_Op then
2176 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
2177 Linker_Options.Table (J + 1 .. Linker_Options.Last);
2178 Linker_Options.Decrement_Last;
2179 Num_Args := Num_Args - 1;
2181 else
2182 IDENT_Op := True;
2183 end if;
2184 end if;
2186 J := J + 1;
2187 end loop;
2189 if Linker_Path = Gcc_Path and then VM_Target = No_VM then
2191 -- For systems where the default is to link statically with
2192 -- libgcc, if gcc is not called with -shared-libgcc, call it
2193 -- with -static-libgcc, as there are some platforms where one
2194 -- of these two switches is compulsory to link.
2196 if Shared_Libgcc_Default = 'T'
2197 and then not Shared_Libgcc_Seen
2198 then
2199 Linker_Options.Increment_Last;
2200 Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
2201 Num_Args := Num_Args + 1;
2202 end if;
2204 elsif RTX_RTSS_Kernel_Module_On_Target then
2206 -- Force the use of the static libgcc for RTSS modules
2208 Linker_Options.Increment_Last;
2209 Linker_Options.Table (Linker_Options.Last) :=
2210 new String'("libgcc.a");
2211 Num_Args := Num_Args + 1;
2212 end if;
2214 end Clean_Link_Option_Set;
2216 -- Prepare arguments for call to linker
2218 Call_Linker : declare
2219 Success : Boolean;
2220 Args : Argument_List (1 .. Num_Args + 1);
2221 Index : Integer := Args'First;
2223 begin
2224 Args (Index) := Binder_Obj_File;
2226 -- Add the object files and any -largs libraries
2228 for J in Linker_Objects.First .. Linker_Objects.Last loop
2229 Index := Index + 1;
2230 Args (Index) := Linker_Objects.Table (J);
2231 end loop;
2233 -- Add the linker options from the binder file
2235 for J in Linker_Options.First .. Linker_Options.Last loop
2236 Index := Index + 1;
2237 Args (Index) := Linker_Options.Table (J);
2238 end loop;
2240 -- Finally add the libraries from the --GCC= switch
2242 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
2243 Index := Index + 1;
2244 Args (Index) := Gcc_Linker_Options.Table (J);
2245 end loop;
2247 if Verbose_Mode then
2248 Write_Str (Linker_Path.all);
2250 for J in Args'Range loop
2251 Write_Str (" ");
2252 Write_Str (Args (J).all);
2253 end loop;
2255 Write_Eol;
2257 -- If we are on very verbose mode (-v -v) and a response file
2258 -- is used we display its content.
2260 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
2261 Write_Eol;
2262 Write_Str ("Response file (" &
2263 Tname (Tname'First .. Tname'Last - 1) &
2264 ") content : ");
2265 Write_Eol;
2267 for J in
2268 Response_File_Objects.First ..
2269 Response_File_Objects.Last
2270 loop
2271 Write_Str (Response_File_Objects.Table (J).all);
2272 Write_Eol;
2273 end loop;
2275 Write_Eol;
2276 end if;
2277 end if;
2279 System.OS_Lib.Spawn (Linker_Path.all, Args, Success);
2281 -- Delete the temporary file used in conjunction with linking if
2282 -- one was created. See Process_Bind_File for details.
2284 if Tname_FD /= Invalid_FD then
2285 Delete (Tname);
2286 end if;
2288 if Lname /= null then
2289 Delete (Lname.all & ASCII.NUL);
2290 end if;
2292 if not Success then
2293 Error_Msg ("error when calling " & Linker_Path.all);
2294 Exit_Program (E_Fatal);
2295 end if;
2296 end Call_Linker;
2297 end Link_Step;
2298 end if;
2300 -- Only keep the binder output file and it's associated object
2301 -- file if compiling with the -g option. These files are only
2302 -- useful if debugging.
2304 if not Debug_Flag_Present then
2305 if Binder_Ali_File /= null then
2306 Delete (Binder_Ali_File.all & ASCII.NUL);
2307 end if;
2309 if Binder_Spec_Src_File /= null then
2310 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
2311 end if;
2313 Delete (Binder_Body_Src_File.all & ASCII.NUL);
2315 if VM_Target = No_VM then
2316 Delete (Binder_Obj_File.all & ASCII.NUL);
2317 end if;
2318 end if;
2320 Exit_Program (E_Success);
2322 exception
2323 when X : others =>
2324 Write_Line (Exception_Information (X));
2325 Exit_With_Error ("INTERNAL ERROR. Please report");
2326 end Gnatlink;