2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / gnatlink.adb
blob99898223cf2fb11ad2f4473052b28dbd05338e95
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-2008, 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 function Base_Name (File_Name : String) return String;
193 -- Return just the file name part without the extension (if present)
195 procedure Delete (Name : String);
196 -- Wrapper to unlink as status is ignored by this application
198 procedure Error_Msg (Message : String);
199 -- Output the error or warning Message
201 procedure Exit_With_Error (Error : String);
202 -- Output Error and exit program with a fatal condition
204 procedure Process_Args;
205 -- Go through all the arguments and build option tables
207 procedure Process_Binder_File (Name : String);
208 -- Reads the binder file and extracts linker arguments
210 procedure Usage;
211 -- Display usage
213 procedure Write_Header;
214 -- Show user the program name, version and copyright
216 procedure Write_Usage;
217 -- Show user the program options
219 ---------------
220 -- Base_Name --
221 ---------------
223 function Base_Name (File_Name : String) return String is
224 Findex1 : Natural;
225 Findex2 : Natural;
227 begin
228 Findex1 := File_Name'First;
230 -- The file might be specified by a full path name. However,
231 -- we want the path to be stripped away.
233 for J in reverse File_Name'Range loop
234 if Is_Directory_Separator (File_Name (J)) then
235 Findex1 := J + 1;
236 exit;
237 end if;
238 end loop;
240 Findex2 := File_Name'Last;
241 while Findex2 > Findex1
242 and then File_Name (Findex2) /= '.'
243 loop
244 Findex2 := Findex2 - 1;
245 end loop;
247 if Findex2 = Findex1 then
248 Findex2 := File_Name'Last + 1;
249 end if;
251 return File_Name (Findex1 .. Findex2 - 1);
252 end Base_Name;
254 ------------
255 -- Delete --
256 ------------
258 procedure Delete (Name : String) is
259 Status : int;
260 pragma Unreferenced (Status);
261 begin
262 Status := unlink (Name'Address);
263 -- Is it really right to ignore an error here ???
264 end Delete;
266 ---------------
267 -- Error_Msg --
268 ---------------
270 procedure Error_Msg (Message : String) is
271 begin
272 Write_Str (Base_Name (Command_Name));
273 Write_Str (": ");
274 Write_Str (Message);
275 Write_Eol;
276 end Error_Msg;
278 ---------------------
279 -- Exit_With_Error --
280 ---------------------
282 procedure Exit_With_Error (Error : String) is
283 begin
284 Error_Msg (Error);
285 Exit_Program (E_Fatal);
286 end Exit_With_Error;
288 ------------------
289 -- Process_Args --
290 ------------------
292 procedure Process_Args is
293 Next_Arg : Integer;
294 Skip_Next : Boolean := False;
295 -- Set to true if the next argument is to be added into the list of
296 -- linker's argument without parsing it.
298 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
300 -- Start of processing for Process_Args
302 begin
303 -- First, check for --version and --help
305 Check_Version_And_Help ("GNATLINK", "1995");
307 -- Loop through arguments of gnatlink command
309 Next_Arg := 1;
310 loop
311 exit when Next_Arg > Argument_Count;
313 Process_One_Arg : declare
314 Arg : constant String := Argument (Next_Arg);
316 begin
317 -- Case of argument which is a switch
319 -- We definitely need section by section comments here ???
321 if Skip_Next then
323 -- This argument must not be parsed, just add it to the
324 -- list of linker's options.
326 Skip_Next := False;
328 Linker_Options.Increment_Last;
329 Linker_Options.Table (Linker_Options.Last) :=
330 new String'(Arg);
332 elsif Arg'Length /= 0 and then Arg (1) = '-' then
333 if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then
334 Exit_With_Error
335 ("invalid switch: """ & Arg & """ (gnat not needed here)");
336 end if;
338 if Arg = "-Xlinker" then
340 -- Next argument should be sent directly to the linker.
341 -- We do not want to parse it here.
343 Skip_Next := True;
345 Linker_Options.Increment_Last;
346 Linker_Options.Table (Linker_Options.Last) :=
347 new String'(Arg);
349 elsif Arg (2) = 'g'
350 and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
351 then
352 Debug_Flag_Present := True;
354 Linker_Options.Increment_Last;
355 Linker_Options.Table (Linker_Options.Last) :=
356 new String'(Arg);
358 Binder_Options.Increment_Last;
359 Binder_Options.Table (Binder_Options.Last) :=
360 Linker_Options.Table (Linker_Options.Last);
362 elsif Arg'Length >= 3 and then Arg (2) = 'M' then
363 declare
364 Switches : String_List_Access;
366 begin
367 Convert (Map_File, Arg (3 .. Arg'Last), Switches);
369 if Switches /= null then
370 for J in Switches'Range loop
371 Linker_Options.Increment_Last;
372 Linker_Options.Table (Linker_Options.Last) :=
373 Switches (J);
374 end loop;
375 end if;
376 end;
378 elsif Arg'Length = 2 then
379 case Arg (2) is
380 when 'A' =>
381 Ada_Bind_File := True;
382 Begin_Info := "-- BEGIN Object file/option list";
383 End_Info := "-- END Object file/option list ";
385 when 'b' =>
386 Linker_Options.Increment_Last;
387 Linker_Options.Table (Linker_Options.Last) :=
388 new String'(Arg);
390 Binder_Options.Increment_Last;
391 Binder_Options.Table (Binder_Options.Last) :=
392 Linker_Options.Table (Linker_Options.Last);
394 Next_Arg := Next_Arg + 1;
396 if Next_Arg > Argument_Count then
397 Exit_With_Error ("Missing argument for -b");
398 end if;
400 Get_Machine_Name : declare
401 Name_Arg : constant String_Access :=
402 new String'(Argument (Next_Arg));
404 begin
405 Linker_Options.Increment_Last;
406 Linker_Options.Table (Linker_Options.Last) :=
407 Name_Arg;
409 Binder_Options.Increment_Last;
410 Binder_Options.Table (Binder_Options.Last) :=
411 Name_Arg;
413 end Get_Machine_Name;
415 when 'C' =>
416 Ada_Bind_File := False;
417 Begin_Info := "/* BEGIN Object file/option list";
418 End_Info := " END Object file/option list */";
420 when 'f' =>
421 if Object_List_File_Supported then
422 Object_List_File_Required := True;
423 else
424 Exit_With_Error
425 ("Object list file not supported on this target");
426 end if;
428 when 'M' =>
429 Create_Map_File := True;
431 when 'n' =>
432 Compile_Bind_File := False;
434 when 'o' =>
435 if VM_Target = CLI_Target then
436 Linker_Options.Increment_Last;
437 Linker_Options.Table (Linker_Options.Last) :=
438 new String'("/QUIET");
440 else
441 Linker_Options.Increment_Last;
442 Linker_Options.Table (Linker_Options.Last) :=
443 new String'(Arg);
444 end if;
446 Next_Arg := Next_Arg + 1;
448 if Next_Arg > Argument_Count then
449 Exit_With_Error ("Missing argument for -o");
450 end if;
452 if VM_Target = CLI_Target then
453 Output_File_Name :=
454 new String'("/OUTPUT=" & Argument (Next_Arg));
455 else
456 Output_File_Name :=
457 new String'(Argument (Next_Arg));
458 end if;
460 Linker_Options.Increment_Last;
461 Linker_Options.Table (Linker_Options.Last) :=
462 Output_File_Name;
464 when 'R' =>
465 Opt.Run_Path_Option := False;
467 when 'v' =>
469 -- Support "double" verbose mode. Second -v
470 -- gets sent to the linker and binder phases.
472 if Verbose_Mode then
473 Very_Verbose_Mode := True;
475 Linker_Options.Increment_Last;
476 Linker_Options.Table (Linker_Options.Last) :=
477 new String'(Arg);
479 Binder_Options.Increment_Last;
480 Binder_Options.Table (Binder_Options.Last) :=
481 Linker_Options.Table (Linker_Options.Last);
483 else
484 Verbose_Mode := True;
486 end if;
488 when others =>
489 Linker_Options.Increment_Last;
490 Linker_Options.Table (Linker_Options.Last) :=
491 new String'(Arg);
493 end case;
495 elsif Arg (2) = 'B' then
496 Linker_Options.Increment_Last;
497 Linker_Options.Table (Linker_Options.Last) :=
498 new String'(Arg);
500 Binder_Options.Increment_Last;
501 Binder_Options.Table (Binder_Options.Last) :=
502 Linker_Options.Table (Linker_Options.Last);
504 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
505 if Arg'Length = 7 then
506 Exit_With_Error ("Missing argument for --LINK=");
507 end if;
509 Linker_Path :=
510 System.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
512 if Linker_Path = null then
513 Exit_With_Error
514 ("Could not locate linker: " & Arg (8 .. Arg'Last));
515 end if;
517 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
518 declare
519 Program_Args : constant Argument_List_Access :=
520 Argument_String_To_List
521 (Arg (7 .. Arg'Last));
523 begin
524 if Program_Args.all (1).all /= Gcc.all then
525 Gcc := new String'(Program_Args.all (1).all);
526 Standard_Gcc := False;
527 end if;
529 -- Set appropriate flags for switches passed
531 for J in 2 .. Program_Args.all'Last loop
532 declare
533 Arg : constant String := Program_Args.all (J).all;
534 AF : constant Integer := Arg'First;
536 begin
537 if Arg'Length /= 0 and then Arg (AF) = '-' then
538 if Arg (AF + 1) = 'g'
539 and then (Arg'Length = 2
540 or else Arg (AF + 2) in '0' .. '3'
541 or else Arg (AF + 2 .. Arg'Last) = "coff")
542 then
543 Debug_Flag_Present := True;
544 end if;
545 end if;
547 -- Add directory to source search dirs so that
548 -- Get_Target_Parameters can find system.ads
550 if Arg (AF .. AF + 1) = "-I"
551 and then Arg'Length > 2
552 then
553 Add_Src_Search_Dir (Arg (AF + 2 .. Arg'Last));
554 end if;
556 -- Pass to gcc for compiling binder generated file
557 -- No use passing libraries, it will just generate
558 -- a warning
560 if not (Arg (AF .. AF + 1) = "-l"
561 or else Arg (AF .. AF + 1) = "-L")
562 then
563 Binder_Options.Increment_Last;
564 Binder_Options.Table (Binder_Options.Last) :=
565 new String'(Arg);
566 end if;
568 -- Pass to gcc for linking program
570 Gcc_Linker_Options.Increment_Last;
571 Gcc_Linker_Options.Table
572 (Gcc_Linker_Options.Last) := new String'(Arg);
573 end;
574 end loop;
575 end;
577 -- Send all multi-character switches not recognized as
578 -- a special case by gnatlink to the linker/loader stage.
580 else
581 Linker_Options.Increment_Last;
582 Linker_Options.Table (Linker_Options.Last) :=
583 new String'(Arg);
584 end if;
586 -- Here if argument is a file name rather than a switch
588 else
589 -- If explicit ali file, capture it
591 if Arg'Length > 4
592 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
593 then
594 if Ali_File_Name = null then
595 Ali_File_Name := new String'(Arg);
596 else
597 Exit_With_Error ("cannot handle more than one ALI file");
598 end if;
600 -- If target object file, record object file
602 elsif Arg'Length > Get_Target_Object_Suffix.all'Length
603 and then Arg
604 (Arg'Last -
605 Get_Target_Object_Suffix.all'Length + 1 .. Arg'Last)
606 = Get_Target_Object_Suffix.all
607 then
608 Linker_Objects.Increment_Last;
609 Linker_Objects.Table (Linker_Objects.Last) :=
610 new String'(Arg);
612 -- If host object file, record object file
613 -- e.g. accept foo.o as well as foo.obj on VMS target
615 elsif Arg'Length > Get_Object_Suffix.all'Length
616 and then Arg
617 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
618 = Get_Object_Suffix.all
619 then
620 Linker_Objects.Increment_Last;
621 Linker_Objects.Table (Linker_Objects.Last) :=
622 new String'(Arg);
624 -- If corresponding ali file exists, capture it
626 elsif Ali_File_Name = null
627 and then Is_Regular_File (Arg & ".ali")
628 then
629 Ali_File_Name := new String'(Arg & ".ali");
631 -- Otherwise assume this is a linker options entry, but
632 -- see below for interesting adjustment to this assumption.
634 else
635 Linker_Options.Increment_Last;
636 Linker_Options.Table (Linker_Options.Last) :=
637 new String'(Arg);
638 end if;
639 end if;
640 end Process_One_Arg;
642 Next_Arg := Next_Arg + 1;
643 end loop;
645 -- If Ada bind file, then compile it with warnings suppressed, because
646 -- otherwise the with of the main program may cause junk warnings.
648 if Ada_Bind_File then
649 Binder_Options.Increment_Last;
650 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
651 end if;
653 -- If we did not get an ali file at all, and we had at least one
654 -- linker option, then assume that was the intended ali file after
655 -- all, so that we get a nicer message later on.
657 if Ali_File_Name = null
658 and then Linker_Options.Last >= Linker_Options.First
659 then
660 Ali_File_Name :=
661 new String'(Linker_Options.Table (Linker_Options.First).all &
662 ".ali");
663 end if;
664 end Process_Args;
666 -------------------------
667 -- Process_Binder_File --
668 -------------------------
670 procedure Process_Binder_File (Name : String) is
671 Fd : FILEs;
672 -- Binder file's descriptor
674 Link_Bytes : Integer := 0;
675 -- Projected number of bytes for the linker command line
677 Link_Max : Integer;
678 pragma Import (C, Link_Max, "__gnat_link_max");
679 -- Maximum number of bytes on the command line supported by the OS
680 -- linker. Passed this limit the response file mechanism must be used
681 -- if supported.
683 Next_Line : String (1 .. 1000);
684 -- Current line value
686 Nlast : Integer;
687 Nfirst : Integer;
688 -- Current line slice (the slice does not contain line terminator)
690 Last : Integer;
691 -- Current line last character for shared libraries (without version)
693 Objs_Begin : Integer := 0;
694 -- First object file index in Linker_Objects table
696 Objs_End : Integer := 0;
697 -- Last object file index in Linker_Objects table
699 Status : int;
700 pragma Warnings (Off, Status);
701 -- Used for various Interfaces.C_Streams calls
703 Closing_Status : Boolean;
704 pragma Warnings (Off, Closing_Status);
705 -- For call to Close
707 GNAT_Static : Boolean := False;
708 -- Save state of -static option
710 GNAT_Shared : Boolean := False;
711 -- Save state of -shared option
713 Xlinker_Was_Previous : Boolean := False;
714 -- Indicate that "-Xlinker" was the option preceding the current
715 -- option. If True, then the current option is never suppressed.
717 -- Rollback data
719 -- These data items are used to store current binder file context.
720 -- The context is composed of the file descriptor position and the
721 -- current line together with the slice indexes (first and last
722 -- position) for this line. The rollback data are used by the
723 -- Store_File_Context and Rollback_File_Context routines below.
724 -- The file context mechanism interact only with the Get_Next_Line
725 -- call. For example:
727 -- Store_File_Context;
728 -- Get_Next_Line;
729 -- Rollback_File_Context;
730 -- Get_Next_Line;
732 -- Both Get_Next_Line calls above will read the exact same data from
733 -- the file. In other words, Next_Line, Nfirst and Nlast variables
734 -- will be set with the exact same values.
736 RB_File_Pos : long; -- File position
737 RB_Next_Line : String (1 .. 1000); -- Current line content
738 RB_Nlast : Integer; -- Slice last index
739 RB_Nfirst : Integer; -- Slice first index
741 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
742 pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
743 -- Pointer to string representing the native linker option which
744 -- specifies the path where the dynamic loader should find shared
745 -- libraries. Equal to null string if this system doesn't support it.
747 Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
748 pragma Import
749 (C, Object_Library_Ext_Ptr, "__gnat_object_library_extension");
750 -- Pointer to string specifying the default extension for
751 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
753 Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
754 pragma Import (C, Object_File_Option_Ptr, "__gnat_object_file_option");
755 -- Pointer to a string representing the linker option which specifies
756 -- the response file.
758 Using_GNU_Linker : Boolean;
759 for Using_GNU_Linker'Size use Character'Size;
760 pragma Import (C, Using_GNU_Linker, "__gnat_using_gnu_linker");
761 -- Predicate indicating whether this target uses the GNU linker. In
762 -- this case we must output a GNU linker compatible response file.
764 Opening : aliased constant String := """";
765 Closing : aliased constant String := '"' & ASCII.LF;
766 -- Needed to quote object paths in object list files when GNU linker
767 -- is used.
769 procedure Get_Next_Line;
770 -- Read the next line from the binder file without the line
771 -- terminator.
773 function Index (S, Pattern : String) return Natural;
774 -- Return the last occurrence of Pattern in S, or 0 if none
776 function Is_Option_Present (Opt : String) return Boolean;
777 -- Return true if the option Opt is already present in
778 -- Linker_Options table.
780 procedure Store_File_Context;
781 -- Store current file context, Fd position and current line data.
782 -- The file context is stored into the rollback data above (RB_*).
783 -- Store_File_Context can be called at any time, only the last call
784 -- will be used (i.e. this routine overwrites the file context).
786 procedure Rollback_File_Context;
787 -- Restore file context from rollback data. This routine must be called
788 -- after Store_File_Context. The binder file context will be restored
789 -- with the data stored by the last Store_File_Context call.
791 -------------------
792 -- Get_Next_Line --
793 -------------------
795 procedure Get_Next_Line is
796 Fchars : chars;
798 begin
799 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
801 if Fchars = System.Null_Address then
802 Exit_With_Error ("Error reading binder output");
803 end if;
805 Nfirst := Next_Line'First;
806 Nlast := Nfirst;
807 while Nlast <= Next_Line'Last
808 and then Next_Line (Nlast) /= ASCII.LF
809 and then Next_Line (Nlast) /= ASCII.CR
810 loop
811 Nlast := Nlast + 1;
812 end loop;
814 Nlast := Nlast - 1;
815 end Get_Next_Line;
817 -----------
818 -- Index --
819 -----------
821 function Index (S, Pattern : String) return Natural is
822 Len : constant Natural := Pattern'Length;
824 begin
825 for J in reverse S'First .. S'Last - Len + 1 loop
826 if Pattern = S (J .. J + Len - 1) then
827 return J;
828 end if;
829 end loop;
831 return 0;
832 end Index;
834 -----------------------
835 -- Is_Option_Present --
836 -----------------------
838 function Is_Option_Present (Opt : String) return Boolean is
839 begin
840 for I in 1 .. Linker_Options.Last loop
842 if Linker_Options.Table (I).all = Opt then
843 return True;
844 end if;
846 end loop;
848 return False;
849 end Is_Option_Present;
851 ---------------------------
852 -- Rollback_File_Context --
853 ---------------------------
855 procedure Rollback_File_Context is
856 begin
857 Next_Line := RB_Next_Line;
858 Nfirst := RB_Nfirst;
859 Nlast := RB_Nlast;
860 Status := fseek (Fd, RB_File_Pos, Interfaces.C_Streams.SEEK_SET);
862 if Status = -1 then
863 Exit_With_Error ("Error setting file position");
864 end if;
865 end Rollback_File_Context;
867 ------------------------
868 -- Store_File_Context --
869 ------------------------
871 procedure Store_File_Context is
872 use type System.CRTL.long;
873 begin
874 RB_Next_Line := Next_Line;
875 RB_Nfirst := Nfirst;
876 RB_Nlast := Nlast;
877 RB_File_Pos := ftell (Fd);
879 if RB_File_Pos = -1 then
880 Exit_With_Error ("Error getting file position");
881 end if;
882 end Store_File_Context;
884 -- Start of processing for Process_Binder_File
886 begin
887 Fd := fopen (Name'Address, Read_Mode'Address);
889 if Fd = NULL_Stream then
890 Exit_With_Error ("Failed to open binder output");
891 end if;
893 -- Skip up to the Begin Info line
895 loop
896 Get_Next_Line;
897 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
898 end loop;
900 loop
901 Get_Next_Line;
903 -- Go to end when end line is reached (this will happen in
904 -- High_Integrity_Mode where no -L switches are generated)
906 exit when Next_Line (Nfirst .. Nlast) = End_Info;
908 if Ada_Bind_File then
909 Next_Line (Nfirst .. Nlast - 8) :=
910 Next_Line (Nfirst + 8 .. Nlast);
911 Nlast := Nlast - 8;
912 end if;
914 -- Go to next section when switches are reached
916 exit when Next_Line (1) = '-';
918 -- Otherwise we have another object file to collect
920 Linker_Objects.Increment_Last;
922 -- Mark the positions of first and last object files in case
923 -- they need to be placed with a named file on systems having
924 -- linker line limitations.
926 if Objs_Begin = 0 then
927 Objs_Begin := Linker_Objects.Last;
928 end if;
930 Linker_Objects.Table (Linker_Objects.Last) :=
931 new String'(Next_Line (Nfirst .. Nlast));
933 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
934 -- Nlast - Nfirst + 1, for the size, plus one for the space between
935 -- each arguments.
936 end loop;
938 Objs_End := Linker_Objects.Last;
940 -- Continue to compute the Link_Bytes, the linker options are part of
941 -- command line length.
943 Store_File_Context;
945 while Next_Line (Nfirst .. Nlast) /= End_Info loop
946 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
947 Get_Next_Line;
948 end loop;
950 Rollback_File_Context;
952 -- On systems that have limitations on handling very long linker lines
953 -- we make use of the system linker option which takes a list of object
954 -- file names from a file instead of the command line itself. What we do
955 -- is to replace the list of object files by the special linker option
956 -- which then reads the object file list from a file instead. The option
957 -- to read from a file instead of the command line is only triggered if
958 -- a conservative threshold is passed.
960 if VM_Target = CLI_Target
961 and then Link_Bytes > Link_Max
962 then
963 Lname := new String'("l~" & Base_Name (Ali_File_Name.all) & ".il");
965 for J in Objs_Begin .. Objs_End loop
966 Copy_File (Linker_Objects.Table (J).all, Lname.all,
967 Success => Closing_Status,
968 Mode => Append);
969 end loop;
971 -- Add the special objects list file option together with the name
972 -- of the temporary file to the objects file table.
974 Linker_Objects.Table (Objs_Begin) :=
975 new String'(Value (Object_File_Option_Ptr) & Lname.all);
977 -- The slots containing these object file names are then removed
978 -- from the objects table so they do not appear in the link. They
979 -- are removed by moving up the linker options and non-Ada object
980 -- files appearing after the Ada object list in the table.
982 declare
983 N : Integer;
985 begin
986 N := Objs_End - Objs_Begin + 1;
988 for J in Objs_End + 1 .. Linker_Objects.Last loop
989 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
990 end loop;
992 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
993 end;
995 elsif Object_List_File_Required
996 or else (Object_List_File_Supported
997 and then Link_Bytes > Link_Max)
998 then
999 -- Create a temporary file containing the Ada user object files
1000 -- needed by the link. This list is taken from the bind file
1001 -- and is output one object per line for maximal compatibility with
1002 -- linkers supporting this option.
1004 Create_Temp_File (Tname_FD, Tname);
1006 -- ??? File descriptor should be checked to not be Invalid_FD.
1007 -- ??? Status of Write and Close operations should be checked, and
1008 -- failure should occur if a status is wrong.
1010 -- If target is using the GNU linker we must add a special header
1011 -- and footer in the response file.
1013 -- The syntax is : INPUT (object1.o object2.o ... )
1015 -- Because the GNU linker does not like name with characters such
1016 -- as '!', we must put the object paths between double quotes.
1018 if Using_GNU_Linker then
1019 declare
1020 GNU_Header : aliased constant String := "INPUT (";
1022 begin
1023 Status := Write (Tname_FD, GNU_Header'Address,
1024 GNU_Header'Length);
1025 end;
1026 end if;
1028 for J in Objs_Begin .. Objs_End loop
1030 -- Opening quote for GNU linker
1032 if Using_GNU_Linker then
1033 Status := Write (Tname_FD, Opening'Address, 1);
1034 end if;
1036 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
1037 Linker_Objects.Table (J).all'Length);
1039 -- Closing quote for GNU linker
1041 if Using_GNU_Linker then
1042 Status := Write (Tname_FD, Closing'Address, 2);
1044 else
1045 Status := Write (Tname_FD, ASCII.LF'Address, 1);
1046 end if;
1048 Response_File_Objects.Increment_Last;
1049 Response_File_Objects.Table (Response_File_Objects.Last) :=
1050 Linker_Objects.Table (J);
1051 end loop;
1053 -- Handle GNU linker response file footer
1055 if Using_GNU_Linker then
1056 declare
1057 GNU_Footer : aliased constant String := ")";
1059 begin
1060 Status := Write (Tname_FD, GNU_Footer'Address,
1061 GNU_Footer'Length);
1062 end;
1063 end if;
1065 Close (Tname_FD, Closing_Status);
1067 -- Add the special objects list file option together with the name
1068 -- of the temporary file (removing the null character) to the objects
1069 -- file table.
1071 Linker_Objects.Table (Objs_Begin) :=
1072 new String'(Value (Object_File_Option_Ptr) &
1073 Tname (Tname'First .. Tname'Last - 1));
1075 -- The slots containing these object file names are then removed
1076 -- from the objects table so they do not appear in the link. They
1077 -- are removed by moving up the linker options and non-Ada object
1078 -- files appearing after the Ada object list in the table.
1080 declare
1081 N : Integer;
1083 begin
1084 N := Objs_End - Objs_Begin + 1;
1086 for J in Objs_End + 1 .. Linker_Objects.Last loop
1087 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
1088 end loop;
1090 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
1091 end;
1092 end if;
1094 -- Process switches and options
1096 if Next_Line (Nfirst .. Nlast) /= End_Info then
1097 Xlinker_Was_Previous := False;
1099 loop
1100 if Xlinker_Was_Previous
1101 or else Next_Line (Nfirst .. Nlast) = "-Xlinker"
1102 then
1103 Linker_Options.Increment_Last;
1104 Linker_Options.Table (Linker_Options.Last) :=
1105 new String'(Next_Line (Nfirst .. Nlast));
1107 elsif Next_Line (Nfirst .. Nlast) = "-static" then
1108 GNAT_Static := True;
1110 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1111 GNAT_Shared := True;
1113 -- Add binder options only if not already set on the command
1114 -- line. This rule is a way to control the linker options order.
1116 -- The following test needs comments, why is it VMS specific.
1117 -- The above comment looks out of date ???
1119 elsif not (OpenVMS_On_Target
1120 and then
1121 Is_Option_Present (Next_Line (Nfirst .. Nlast)))
1122 then
1123 if Nlast > Nfirst + 2 and then
1124 Next_Line (Nfirst .. Nfirst + 1) = "-L"
1125 then
1126 -- Construct a library search path for use later
1127 -- to locate static gnatlib libraries.
1129 if Libpath.Last > 1 then
1130 Libpath.Increment_Last;
1131 Libpath.Table (Libpath.Last) := Path_Separator;
1132 end if;
1134 for I in Nfirst + 2 .. Nlast loop
1135 Libpath.Increment_Last;
1136 Libpath.Table (Libpath.Last) := Next_Line (I);
1137 end loop;
1139 Linker_Options.Increment_Last;
1141 Linker_Options.Table (Linker_Options.Last) :=
1142 new String'(Next_Line (Nfirst .. Nlast));
1144 elsif Next_Line (Nfirst .. Nlast) = "-ldecgnat"
1145 or else Next_Line (Nfirst .. Nlast) = "-lgnarl"
1146 or else Next_Line (Nfirst .. Nlast) = "-lgnat"
1147 or else Next_Line
1148 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) =
1149 Shared_Lib ("gnarl")
1150 or else Next_Line
1151 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) =
1152 Shared_Lib ("gnat")
1153 then
1154 -- If it is a shared library, remove the library version.
1155 -- We will be looking for the static version of the library
1156 -- as it is in the same directory as the shared version.
1158 if Next_Line (Nlast - Library_Version'Length + 1 .. Nlast)
1159 = Library_Version
1160 then
1161 -- Set Last to point to last character before the
1162 -- library version.
1164 Last := Nlast - Library_Version'Length - 1;
1165 else
1166 Last := Nlast;
1167 end if;
1169 -- Given a Gnat standard library, search the
1170 -- library path to find the library location
1172 declare
1173 File_Path : String_Access;
1175 Object_Lib_Extension : constant String :=
1176 Value (Object_Library_Ext_Ptr);
1178 File_Name : constant String := "lib" &
1179 Next_Line (Nfirst + 2 .. Last) &
1180 Object_Lib_Extension;
1182 Run_Path_Opt : constant String :=
1183 Value (Run_Path_Option_Ptr);
1185 GCC_Index : Natural;
1186 Run_Path_Opt_Index : Natural := 0;
1188 begin
1189 File_Path :=
1190 Locate_Regular_File (File_Name,
1191 String (Libpath.Table (1 .. Libpath.Last)));
1193 if File_Path /= null then
1194 if GNAT_Static then
1196 -- If static gnatlib found, explicitly
1197 -- specify to overcome possible linker
1198 -- default usage of shared version.
1200 Linker_Options.Increment_Last;
1202 Linker_Options.Table (Linker_Options.Last) :=
1203 new String'(File_Path.all);
1205 elsif GNAT_Shared then
1206 if Opt.Run_Path_Option then
1207 -- If shared gnatlib desired, add the
1208 -- appropriate system specific switch
1209 -- so that it can be located at runtime.
1211 if Run_Path_Opt'Length /= 0 then
1212 -- Output the system specific linker command
1213 -- that allows the image activator to find
1214 -- the shared library at runtime.
1215 -- Also add path to find libgcc_s.so, if
1216 -- relevant.
1218 -- To find the location of the shared version
1219 -- of libgcc, we look for "gcc-lib" in the
1220 -- path of the library. However, this
1221 -- subdirectory is no longer present in
1222 -- in recent version of GCC. So, we look for
1223 -- the last subdirectory "lib" in the path.
1225 GCC_Index :=
1226 Index (File_Path.all, "gcc-lib");
1228 if GCC_Index /= 0 then
1229 -- The shared version of libgcc is
1230 -- located in the parent directory.
1232 GCC_Index := GCC_Index - 1;
1234 else
1235 GCC_Index :=
1236 Index (File_Path.all, "/lib/");
1238 if GCC_Index = 0 then
1239 GCC_Index :=
1240 Index (File_Path.all,
1241 Directory_Separator &
1242 "lib" &
1243 Directory_Separator);
1244 end if;
1246 -- We have found a subdirectory "lib",
1247 -- this is where the shared version of
1248 -- libgcc should be located.
1250 if GCC_Index /= 0 then
1251 GCC_Index := GCC_Index + 3;
1252 end if;
1253 end if;
1255 -- Look for an eventual run_path_option in
1256 -- the linker switches.
1258 for J in reverse 1 .. Linker_Options.Last loop
1259 if Linker_Options.Table (J) /= null
1260 and then
1261 Linker_Options.Table (J)'Length
1262 > Run_Path_Opt'Length
1263 and then
1264 Linker_Options.Table (J)
1265 (1 .. Run_Path_Opt'Length) =
1266 Run_Path_Opt
1267 then
1268 -- We have found a already specified
1269 -- run_path_option: we will add to this
1270 -- switch, because only one
1271 -- run_path_option should be specified.
1273 Run_Path_Opt_Index := J;
1274 exit;
1275 end if;
1276 end loop;
1278 -- If there is no run_path_option, we need
1279 -- to add one.
1281 if Run_Path_Opt_Index = 0 then
1282 Linker_Options.Increment_Last;
1283 end if;
1285 if GCC_Index = 0 then
1286 if Run_Path_Opt_Index = 0 then
1287 Linker_Options.Table
1288 (Linker_Options.Last) :=
1289 new String'
1290 (Run_Path_Opt
1291 & File_Path
1292 (1 .. File_Path'Length
1293 - File_Name'Length));
1295 else
1296 Linker_Options.Table
1297 (Run_Path_Opt_Index) :=
1298 new String'
1299 (Linker_Options.Table
1300 (Run_Path_Opt_Index).all
1301 & Path_Separator
1302 & File_Path
1303 (1 .. File_Path'Length
1304 - File_Name'Length));
1305 end if;
1307 else
1308 if Run_Path_Opt_Index = 0 then
1309 Linker_Options.Table
1310 (Linker_Options.Last) :=
1311 new String'(Run_Path_Opt
1312 & File_Path
1313 (1 .. File_Path'Length
1314 - File_Name'Length)
1315 & Path_Separator
1316 & File_Path (1 .. GCC_Index));
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 & Path_Separator
1329 & File_Path (1 .. GCC_Index));
1330 end if;
1331 end if;
1332 end if;
1333 end if;
1335 -- Then we add the appropriate -l switch
1337 Linker_Options.Increment_Last;
1338 Linker_Options.Table (Linker_Options.Last) :=
1339 new String'(Next_Line (Nfirst .. Nlast));
1340 end if;
1342 else
1343 -- If gnatlib library not found, then
1344 -- add it anyway in case some other
1345 -- mechanism may find it.
1347 Linker_Options.Increment_Last;
1348 Linker_Options.Table (Linker_Options.Last) :=
1349 new String'(Next_Line (Nfirst .. Nlast));
1350 end if;
1351 end;
1352 else
1353 Linker_Options.Increment_Last;
1354 Linker_Options.Table (Linker_Options.Last) :=
1355 new String'(Next_Line (Nfirst .. Nlast));
1356 end if;
1357 end if;
1359 Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker";
1361 Get_Next_Line;
1362 exit when Next_Line (Nfirst .. Nlast) = End_Info;
1364 if Ada_Bind_File then
1365 Next_Line (Nfirst .. Nlast - 8) :=
1366 Next_Line (Nfirst + 8 .. Nlast);
1367 Nlast := Nlast - 8;
1368 end if;
1369 end loop;
1370 end if;
1372 -- If -shared was specified, invoke gcc with -shared-libgcc
1374 if GNAT_Shared then
1375 Linker_Options.Increment_Last;
1376 Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc;
1377 end if;
1379 Status := fclose (Fd);
1380 end Process_Binder_File;
1382 -----------
1383 -- Usage --
1384 -----------
1386 procedure Usage is
1387 begin
1388 Write_Str ("Usage: ");
1389 Write_Str (Base_Name (Command_Name));
1390 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
1391 Write_Eol;
1392 Write_Eol;
1393 Write_Line (" mainprog.ali the ALI file of the main program");
1394 Write_Eol;
1395 Write_Line (" -A Binder generated source file is in Ada (default)");
1396 Write_Line (" -C Binder generated source file is in C");
1397 Write_Line (" -f force object file list to be generated");
1398 Write_Line (" -g Compile binder source file with debug information");
1399 Write_Line (" -n Do not compile the binder source file");
1400 Write_Line (" -R Do not use a run_path_option");
1401 Write_Line (" -v verbose mode");
1402 Write_Line (" -v -v very verbose mode");
1403 Write_Eol;
1404 Write_Line (" -o nam Use 'nam' as the name of the executable");
1405 Write_Line (" -b target Compile the binder source to run on target");
1406 Write_Line (" -Bdir Load compiler executables from dir");
1408 if Is_Supported (Map_File) then
1409 Write_Line (" -Mmap Create map file map");
1410 Write_Line (" -M Create map file mainprog.map");
1411 end if;
1413 Write_Line (" --GCC=comp Use comp as the compiler");
1414 Write_Line (" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
1415 Write_Eol;
1416 Write_Line (" [non-Ada-objects] list of non Ada object files");
1417 Write_Line (" [linker-options] other options for the linker");
1418 end Usage;
1420 ------------------
1421 -- Write_Header --
1422 ------------------
1424 procedure Write_Header is
1425 begin
1426 if Verbose_Mode then
1427 Write_Eol;
1428 Display_Version ("GNATLINK", "1995");
1429 end if;
1430 end Write_Header;
1432 -----------------
1433 -- Write_Usage --
1434 -----------------
1436 procedure Write_Usage is
1437 begin
1438 Write_Header;
1439 Usage;
1440 end Write_Usage;
1442 -- Start of processing for Gnatlink
1444 begin
1445 -- Add the directory where gnatlink is invoked in front of the
1446 -- path, if gnatlink is invoked with directory information.
1447 -- Only do this if the platform is not VMS, where the notion of path
1448 -- does not really exist.
1450 if not Hostparm.OpenVMS then
1451 declare
1452 Command : constant String := Command_Name;
1454 begin
1455 for Index in reverse Command'Range loop
1456 if Command (Index) = Directory_Separator then
1457 declare
1458 Absolute_Dir : constant String :=
1459 Normalize_Pathname
1460 (Command (Command'First .. Index));
1462 PATH : constant String :=
1463 Absolute_Dir &
1464 Path_Separator &
1465 Getenv ("PATH").all;
1467 begin
1468 Setenv ("PATH", PATH);
1469 end;
1471 exit;
1472 end if;
1473 end loop;
1474 end;
1475 end if;
1477 Process_Args;
1479 if Argument_Count = 0
1480 or else
1481 (Verbose_Mode and then Argument_Count = 1)
1482 then
1483 Write_Usage;
1484 Exit_Program (E_Fatal);
1485 end if;
1487 -- Get target parameters
1489 Namet.Initialize;
1490 Csets.Initialize;
1491 Snames.Initialize;
1492 Osint.Add_Default_Search_Dirs;
1493 Targparm.Get_Target_Parameters;
1495 if VM_Target /= No_VM then
1496 case VM_Target is
1497 when JVM_Target => Gcc := new String'("jgnat");
1498 when CLI_Target => Gcc := new String'("dotnet-gnatcompile");
1499 when No_VM => raise Program_Error;
1500 end case;
1502 Ada_Bind_File := True;
1503 Begin_Info := "-- BEGIN Object file/option list";
1504 End_Info := "-- END Object file/option list ";
1505 end if;
1507 -- We always compile with -c
1509 Binder_Options_From_ALI.Increment_Last;
1510 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1511 new String'("-c");
1513 -- If the main program is in Ada it is compiled with the following
1514 -- switches:
1516 -- -gnatA stops reading gnat.adc, since we don't know what
1517 -- pragmas would work, and we do not need it anyway.
1519 -- -gnatWb allows brackets coding for wide characters
1521 -- -gnatiw allows wide characters in identifiers. This is needed
1522 -- because bindgen uses brackets encoding for all upper
1523 -- half and wide characters in identifier names.
1525 if Ada_Bind_File then
1526 Binder_Options_From_ALI.Increment_Last;
1527 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1528 new String'("-gnatA");
1529 Binder_Options_From_ALI.Increment_Last;
1530 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1531 new String'("-gnatWb");
1532 Binder_Options_From_ALI.Increment_Last;
1533 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1534 new String'("-gnatiw");
1535 end if;
1537 -- Locate all the necessary programs and verify required files are present
1539 Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1541 if Gcc_Path = null then
1542 Exit_With_Error ("Couldn't locate " & Gcc.all);
1543 end if;
1545 if Linker_Path = null then
1546 if VM_Target = CLI_Target then
1547 Linker_Path := System.OS_Lib.Locate_Exec_On_Path ("ilasm");
1549 if Linker_Path = null then
1550 Exit_With_Error ("Couldn't locate ilasm");
1551 end if;
1552 else
1553 Linker_Path := Gcc_Path;
1554 end if;
1555 end if;
1557 if Ali_File_Name = null then
1558 Exit_With_Error ("no ali file given for link");
1559 end if;
1561 if not Is_Regular_File (Ali_File_Name.all) then
1562 Exit_With_Error (Ali_File_Name.all & " not found");
1563 end if;
1565 -- Read the ALI file of the main subprogram if the binder generated
1566 -- file needs to be compiled and no --GCC= switch has been specified.
1567 -- Fetch the back end switches from this ALI file and use these switches
1568 -- to compile the binder generated file
1570 if Compile_Bind_File and then Standard_Gcc then
1572 Initialize_ALI;
1573 Name_Len := Ali_File_Name'Length;
1574 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1576 declare
1577 use Types;
1578 F : constant File_Name_Type := Name_Find;
1579 T : Text_Buffer_Ptr;
1580 A : ALI_Id;
1582 begin
1583 -- Load the ALI file
1585 T := Read_Library_Info (F, True);
1587 -- Read it. Note that we ignore errors, since we only want very
1588 -- limited information from the ali file, and likely a slightly
1589 -- wrong version will be just fine, though in normal operation
1590 -- we don't expect this to happen!
1592 A := Scan_ALI
1595 Ignore_ED => False,
1596 Err => False,
1597 Ignore_Errors => True);
1599 if A /= No_ALI_Id then
1601 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1602 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1603 loop
1604 -- Do not compile with the front end switches. However, --RTS
1605 -- is to be dealt with specially because it needs to be passed
1606 -- if the binder-generated file is in Ada and may also be used
1607 -- to drive the linker.
1609 declare
1610 Arg : String_Ptr renames Args.Table (Index);
1611 begin
1612 if not Is_Front_End_Switch (Arg.all) then
1613 Binder_Options_From_ALI.Increment_Last;
1614 Binder_Options_From_ALI.Table
1615 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1617 elsif Arg'Length > 5
1618 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1619 then
1620 if Ada_Bind_File then
1621 Binder_Options_From_ALI.Increment_Last;
1622 Binder_Options_From_ALI.Table
1623 (Binder_Options_From_ALI.Last)
1624 := String_Access (Arg);
1625 end if;
1627 -- GNAT doesn't support the GCC multilib mechanism.
1628 -- This means that, when a multilib switch is used
1629 -- to request a particular compilation mode, the
1630 -- corresponding runtime switch (--RTS) must also be
1631 -- specified. The long-term goal is to fully support the
1632 -- multilib mechanism; however, in the meantime, it is
1633 -- convenient to eliminate the redundancy by keying the
1634 -- compilation mode on a single switch, namely --RTS.
1636 -- Pass -mrtp to the linker if --RTS=rtp was passed
1638 if Linker_Path = Gcc_Path
1639 and then Arg'Length > 8
1640 and then Arg (Arg'First + 6 .. Arg'First + 8) = "rtp"
1641 then
1642 Linker_Options.Increment_Last;
1643 Linker_Options.Table (Linker_Options.Last) :=
1644 new String'("-mrtp");
1646 -- Pass -fsjlj to the linker if --RTS=sjlj was passed
1648 elsif Linker_Path = Gcc_Path
1649 and then Arg'Length > 9
1650 and then Arg (Arg'First + 6 .. Arg'First + 9) = "sjlj"
1651 then
1652 Linker_Options.Increment_Last;
1653 Linker_Options.Table (Linker_Options.Last) :=
1654 new String'("-fsjlj");
1655 end if;
1656 end if;
1657 end;
1658 end loop;
1659 end if;
1660 end;
1661 end if;
1663 Write_Header;
1665 -- If no output name specified, then use the base name of .ali file name
1667 if Output_File_Name = null then
1668 Output_File_Name :=
1669 new String'(Base_Name (Ali_File_Name.all)
1670 & Get_Target_Debuggable_Suffix.all);
1672 if VM_Target = CLI_Target then
1673 Linker_Options.Increment_Last;
1674 Linker_Options.Table (Linker_Options.Last) := new String'("/QUIET");
1676 Linker_Options.Increment_Last;
1677 Linker_Options.Table (Linker_Options.Last) := new String'("/DEBUG");
1679 Linker_Options.Increment_Last;
1680 Linker_Options.Table (Linker_Options.Last) :=
1681 new String'("/OUTPUT=" & Output_File_Name.all);
1683 else
1684 Linker_Options.Increment_Last;
1685 Linker_Options.Table (Linker_Options.Last) := new String'("-o");
1687 Linker_Options.Increment_Last;
1688 Linker_Options.Table (Linker_Options.Last) :=
1689 new String'(Output_File_Name.all);
1690 end if;
1691 end if;
1693 -- Warn if main program is called "test", as that may be a built-in command
1694 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1695 -- will not appear. However, do not warn in the case of a cross compiler.
1697 -- Assume this is a cross tool if the executable name is not gnatlink
1699 if Base_Name (Command_Name) = "gnatlink"
1700 and then Output_File_Name.all = "test"
1701 then
1702 Error_Msg ("warning: executable name """ & Output_File_Name.all
1703 & """ may conflict with shell command");
1704 end if;
1706 -- If -M switch was specified, add the switches to create the map file
1708 if Create_Map_File then
1709 declare
1710 Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map";
1711 Switches : String_List_Access;
1713 begin
1714 Convert (Map_File, Map_Name, Switches);
1716 if Switches /= null then
1717 for J in Switches'Range loop
1718 Linker_Options.Increment_Last;
1719 Linker_Options.Table (Linker_Options.Last) := Switches (J);
1720 end loop;
1721 end if;
1722 end;
1723 end if;
1725 -- Perform consistency checks
1727 -- Transform the .ali file name into the binder output file name
1729 Make_Binder_File_Names : declare
1730 Fname : constant String := Base_Name (Ali_File_Name.all);
1731 Fname_Len : Integer := Fname'Length;
1733 function Get_Maximum_File_Name_Length return Integer;
1734 pragma Import (C, Get_Maximum_File_Name_Length,
1735 "__gnat_get_maximum_file_name_length");
1737 Maximum_File_Name_Length : constant Integer :=
1738 Get_Maximum_File_Name_Length;
1740 Bind_File_Prefix : Types.String_Ptr;
1741 -- Contains prefix used for bind files
1743 begin
1744 -- Set prefix
1746 if not Ada_Bind_File then
1747 Bind_File_Prefix := new String'("b_");
1748 elsif OpenVMS_On_Target then
1749 Bind_File_Prefix := new String'("b__");
1750 else
1751 Bind_File_Prefix := new String'("b~");
1752 end if;
1754 -- If the length of the binder file becomes too long due to
1755 -- the addition of the "b?" prefix, then truncate it.
1757 if Maximum_File_Name_Length > 0 then
1758 while Fname_Len >
1759 Maximum_File_Name_Length - Bind_File_Prefix.all'Length
1760 loop
1761 Fname_Len := Fname_Len - 1;
1762 end loop;
1763 end if;
1765 declare
1766 Fnam : constant String :=
1767 Bind_File_Prefix.all &
1768 Fname (Fname'First .. Fname'First + Fname_Len - 1);
1770 begin
1771 if Ada_Bind_File then
1772 Binder_Spec_Src_File := new String'(Fnam & ".ads");
1773 Binder_Body_Src_File := new String'(Fnam & ".adb");
1774 Binder_Ali_File := new String'(Fnam & ".ali");
1775 else
1776 Binder_Body_Src_File := new String'(Fnam & ".c");
1777 end if;
1779 Binder_Obj_File := new String'(Fnam & Get_Target_Object_Suffix.all);
1780 end;
1782 if Fname_Len /= Fname'Length then
1783 Binder_Options.Increment_Last;
1784 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1785 Binder_Options.Increment_Last;
1786 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1787 end if;
1788 end Make_Binder_File_Names;
1790 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1792 -- Compile the binder file. This is fast, so we always do it, unless
1793 -- specifically told not to by the -n switch
1795 if Compile_Bind_File then
1796 Bind_Step : declare
1797 Success : Boolean;
1798 Args : Argument_List
1799 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1801 begin
1802 for J in 1 .. Binder_Options_From_ALI.Last loop
1803 Args (J) := Binder_Options_From_ALI.Table (J);
1804 end loop;
1806 for J in 1 .. Binder_Options.Last loop
1807 Args (Binder_Options_From_ALI.Last + J) :=
1808 Binder_Options.Table (J);
1809 end loop;
1811 -- Use the full path of the binder generated source, so that it is
1812 -- guaranteed that the debugger will find this source, even with
1813 -- STABS.
1815 Args (Args'Last) :=
1816 new String'(Normalize_Pathname (Binder_Body_Src_File.all));
1818 if Verbose_Mode then
1819 Write_Str (Base_Name (Gcc_Path.all));
1821 for J in Args'Range loop
1822 Write_Str (" ");
1823 Write_Str (Args (J).all);
1824 end loop;
1826 Write_Eol;
1827 end if;
1829 System.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1831 if not Success then
1832 Exit_Program (E_Fatal);
1833 end if;
1834 end Bind_Step;
1835 end if;
1837 -- Now, actually link the program
1839 -- Skip this step for now on JVM since the Java interpreter will do
1840 -- the actual link at run time. We might consider packing all class files
1841 -- in a .zip file during this step.
1843 if VM_Target /= JVM_Target then
1844 Link_Step : declare
1845 Num_Args : Natural :=
1846 (Linker_Options.Last - Linker_Options.First + 1) +
1847 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1848 (Linker_Objects.Last - Linker_Objects.First + 1);
1849 Stack_Op : Boolean := False;
1850 IDENT_Op : Boolean := False;
1852 begin
1853 if VM_Target = CLI_Target then
1855 -- Remove extraneous flags not relevant for CIL. Also remove empty
1856 -- arguments, since ilasm chokes on them.
1858 for J in reverse Linker_Options.First .. Linker_Options.Last loop
1859 if Linker_Options.Table (J)'Length = 0
1860 or else Linker_Options.Table (J) (1 .. 2) = "-L"
1861 or else Linker_Options.Table (J) (1 .. 2) = "-l"
1862 or else Linker_Options.Table (J) (1 .. 3) = "-Wl"
1863 or else Linker_Options.Table (J) (1 .. 3) = "-sh"
1864 or else Linker_Options.Table (J) (1 .. 2) = "-g"
1865 then
1866 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1867 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1868 Linker_Options.Decrement_Last;
1869 Num_Args := Num_Args - 1;
1870 end if;
1871 end loop;
1872 end if;
1874 -- Remove duplicate stack size setting from the Linker_Options
1875 -- table. The stack setting option "-Xlinker --stack=R,C" can be
1876 -- found in one line when set by a pragma Linker_Options or in two
1877 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
1878 -- line. We also check for the "-Wl,--stack=R" style option.
1880 -- We must remove the second stack setting option instance
1881 -- because the one on the command line will always be the first
1882 -- one. And any subsequent stack setting option will overwrite the
1883 -- previous one. This is done especially for GNAT/NT where we set
1884 -- the stack size for tasking programs by a pragma in the NT
1885 -- specific tasking package System.Task_Primitives.Operations.
1887 -- Note: This is not a FOR loop that runs from Linker_Options.First
1888 -- to Linker_Options.Last, since operations within the loop can
1889 -- modify the length of the table.
1891 Clean_Link_Option_Set : declare
1892 J : Natural := Linker_Options.First;
1893 Shared_Libgcc_Seen : Boolean := False;
1895 begin
1896 while J <= Linker_Options.Last loop
1898 if Linker_Options.Table (J).all = "-Xlinker"
1899 and then J < Linker_Options.Last
1900 and then Linker_Options.Table (J + 1)'Length > 8
1901 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
1902 then
1903 if Stack_Op then
1904 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
1905 Linker_Options.Table (J + 2 .. Linker_Options.Last);
1906 Linker_Options.Decrement_Last;
1907 Linker_Options.Decrement_Last;
1908 Num_Args := Num_Args - 2;
1910 else
1911 Stack_Op := True;
1912 end if;
1913 end if;
1915 -- Remove duplicate -shared-libgcc switch
1917 if Linker_Options.Table (J).all = Shared_Libgcc_String then
1918 if Shared_Libgcc_Seen then
1919 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1920 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1921 Linker_Options.Decrement_Last;
1922 Num_Args := Num_Args - 1;
1924 else
1925 Shared_Libgcc_Seen := True;
1926 end if;
1927 end if;
1929 -- Here we just check for a canonical form that matches the
1930 -- pragma Linker_Options set in the NT runtime.
1932 if (Linker_Options.Table (J)'Length > 17
1933 and then Linker_Options.Table (J) (1 .. 17)
1934 = "-Xlinker --stack=")
1935 or else
1936 (Linker_Options.Table (J)'Length > 12
1937 and then Linker_Options.Table (J) (1 .. 12)
1938 = "-Wl,--stack=")
1939 then
1940 if Stack_Op then
1941 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1942 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1943 Linker_Options.Decrement_Last;
1944 Num_Args := Num_Args - 1;
1946 else
1947 Stack_Op := True;
1948 end if;
1949 end if;
1951 -- Remove duplicate IDENTIFICATION directives (VMS)
1953 if Linker_Options.Table (J)'Length > 27
1954 and then Linker_Options.Table (J) (1 .. 28)
1955 = "--for-linker=IDENTIFICATION="
1956 then
1957 if IDENT_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;
1962 else
1963 IDENT_Op := True;
1964 end if;
1965 end if;
1967 J := J + 1;
1968 end loop;
1970 if Linker_Path = Gcc_Path and then VM_Target = No_VM then
1972 -- If gcc is not called with -shared-libgcc, call it with
1973 -- -static-libgcc, as there are some platforms where one of
1974 -- these two switches is compulsory to link.
1976 if not Shared_Libgcc_Seen then
1977 Linker_Options.Increment_Last;
1978 Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
1979 Num_Args := Num_Args + 1;
1980 end if;
1981 end if;
1983 end Clean_Link_Option_Set;
1985 -- Prepare arguments for call to linker
1987 Call_Linker : declare
1988 Success : Boolean;
1989 Args : Argument_List (1 .. Num_Args + 1);
1990 Index : Integer := Args'First;
1992 begin
1993 Args (Index) := Binder_Obj_File;
1995 -- Add the object files and any -largs libraries
1997 for J in Linker_Objects.First .. Linker_Objects.Last loop
1998 Index := Index + 1;
1999 Args (Index) := Linker_Objects.Table (J);
2000 end loop;
2002 -- Add the linker options from the binder file
2004 for J in Linker_Options.First .. Linker_Options.Last loop
2005 Index := Index + 1;
2006 Args (Index) := Linker_Options.Table (J);
2007 end loop;
2009 -- Finally add the libraries from the --GCC= switch
2011 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
2012 Index := Index + 1;
2013 Args (Index) := Gcc_Linker_Options.Table (J);
2014 end loop;
2016 if Verbose_Mode then
2017 Write_Str (Linker_Path.all);
2019 for J in Args'Range loop
2020 Write_Str (" ");
2021 Write_Str (Args (J).all);
2022 end loop;
2024 Write_Eol;
2026 -- If we are on very verbose mode (-v -v) and a response file
2027 -- is used we display its content.
2029 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
2030 Write_Eol;
2031 Write_Str ("Response file (" &
2032 Tname (Tname'First .. Tname'Last - 1) &
2033 ") content : ");
2034 Write_Eol;
2036 for J in
2037 Response_File_Objects.First ..
2038 Response_File_Objects.Last
2039 loop
2040 Write_Str (Response_File_Objects.Table (J).all);
2041 Write_Eol;
2042 end loop;
2044 Write_Eol;
2045 end if;
2046 end if;
2048 System.OS_Lib.Spawn (Linker_Path.all, Args, Success);
2050 -- Delete the temporary file used in conjunction with linking if
2051 -- one was created. See Process_Bind_File for details.
2053 if Tname_FD /= Invalid_FD then
2054 Delete (Tname);
2055 end if;
2057 if Lname /= null then
2058 Delete (Lname.all & ASCII.NUL);
2059 end if;
2061 if not Success then
2062 Error_Msg ("error when calling " & Linker_Path.all);
2063 Exit_Program (E_Fatal);
2064 end if;
2065 end Call_Linker;
2066 end Link_Step;
2067 end if;
2069 -- Only keep the binder output file and it's associated object
2070 -- file if compiling with the -g option. These files are only
2071 -- useful if debugging.
2073 if not Debug_Flag_Present then
2074 if Binder_Ali_File /= null then
2075 Delete (Binder_Ali_File.all & ASCII.NUL);
2076 end if;
2078 if Binder_Spec_Src_File /= null then
2079 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
2080 end if;
2082 Delete (Binder_Body_Src_File.all & ASCII.NUL);
2084 if VM_Target = No_VM then
2085 Delete (Binder_Obj_File.all & ASCII.NUL);
2086 end if;
2087 end if;
2089 Exit_Program (E_Success);
2091 exception
2092 when X : others =>
2093 Write_Line (Exception_Information (X));
2094 Exit_With_Error ("INTERNAL ERROR. Please report");
2095 end Gnatlink;