[PATCH 06/11] Handle enums for CodeView
[official-gcc.git] / gcc / ada / gnatlink.adb
blobdb0fd144a13e420ad7e0818673431528e2b9374e
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-2024, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 -- Gnatlink usage: please consult the gnat documentation
28 with ALI; use ALI;
29 with Csets;
30 with Gnatvsn; use Gnatvsn;
31 with Indepsw; use Indepsw;
32 with Namet; use Namet;
33 with Opt;
34 with Osint; use Osint;
35 with Output; use Output;
36 with Snames;
37 with Switch; use Switch;
38 with System; use System;
39 with Table;
40 with Targparm;
41 with Types;
43 with Ada.Command_Line; use Ada.Command_Line;
44 with Ada.Exceptions; use Ada.Exceptions;
45 with Ada.Strings.Fixed;
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 -- Options to be passed to the gcc linker
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 -- Library search path
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 -- Options to be passed to gnatlink
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 : constant String := "-- BEGIN Object file/option list";
145 End_Info : constant String := "-- END Object file/option list ";
147 Gcc_Path : String_Access;
148 Linker_Path : String_Access;
149 Output_File_Name : String_Access;
150 Ali_File_Name : String_Access;
151 Binder_Spec_Src_File : String_Access;
152 Binder_Body_Src_File : String_Access;
153 Binder_Ali_File : String_Access;
154 Binder_Obj_File : String_Access;
156 Base_Command_Name : String_Access;
158 Target_Debuggable_Suffix : String_Access;
160 Tname : Temp_File_Name;
161 Tname_FD : File_Descriptor := Invalid_FD;
162 -- Temporary file used by linker to pass list of object files on
163 -- certain systems with limitations on size of arguments.
165 Debug_Flag_Present : Boolean := False;
166 Verbose_Mode : Boolean := False;
167 Very_Verbose_Mode : Boolean := False;
169 Standard_Gcc : Boolean := True;
171 Compile_Bind_File : Boolean := True;
172 -- Set to False if bind file is not to be compiled
174 Create_Map_File : Boolean := False;
175 -- Set to True by switch -M. The map file name is derived from
176 -- the ALI file name (mainprog.ali => mainprog.map).
178 Object_List_File_Supported : Boolean;
179 for Object_List_File_Supported'Size use Character'Size;
180 pragma Import
181 (C, Object_List_File_Supported, "__gnat_objlist_file_supported");
182 -- Predicate indicating whether the linker has an option whereby the
183 -- names of object files can be passed to the linker in a file.
185 Object_File_Option_Ptr : Interfaces.C.Strings.chars_ptr;
186 pragma Import (C, Object_File_Option_Ptr, "__gnat_object_file_option");
187 -- Pointer to a string representing the linker option which specifies
188 -- the response file.
190 Object_File_Option : constant String := Value (Object_File_Option_Ptr);
191 -- The linker option which specifies the response file as a string
193 Using_GNU_response_file : constant Boolean :=
194 Object_File_Option'Length > 0
195 and then Object_File_Option (Object_File_Option'Last) = '@';
196 -- Whether a GNU response file is used
198 Object_List_File_Required : Boolean := False;
199 -- Set to True to force generation of a response file
201 Shared_Libgcc_Default : Character;
202 for Shared_Libgcc_Default'Size use Character'Size;
203 pragma Import
204 (C, Shared_Libgcc_Default, "__gnat_shared_libgcc_default");
205 -- Indicates wether libgcc should be statically linked (use 'T') or
206 -- dynamically linked (use 'H') by default.
208 Link_Max : Integer;
209 pragma Import (C, Link_Max, "__gnat_link_max");
210 -- Maximum number of bytes on the command line supported by the OS
211 -- linker. Passed this limit the response file mechanism must be used
212 -- if supported.
214 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
215 pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
216 -- Pointer to string representing the native linker option which
217 -- specifies the path where the dynamic loader should find shared
218 -- libraries. Equal to null string if this system doesn't support it.
220 Libgcc_Subdir_Ptr : Interfaces.C.Strings.chars_ptr;
221 pragma Import (C, Libgcc_Subdir_Ptr, "__gnat_default_libgcc_subdir");
222 -- Pointer to string indicating the installation subdirectory where
223 -- a default shared libgcc might be found.
225 Object_Library_Ext_Ptr : Interfaces.C.Strings.chars_ptr;
226 pragma Import
227 (C, Object_Library_Ext_Ptr, "__gnat_object_library_extension");
228 -- Pointer to string specifying the default extension for
229 -- object libraries, e.g. Unix uses ".a".
231 Separate_Run_Path_Options : Boolean;
232 for Separate_Run_Path_Options'Size use Character'Size;
233 pragma Import
234 (C, Separate_Run_Path_Options, "__gnat_separate_run_path_options");
235 -- Whether separate rpath options should be emitted for each directory
237 function Get_Maximum_File_Name_Length return Integer;
238 pragma Import (C, Get_Maximum_File_Name_Length,
239 "__gnat_get_maximum_file_name_length");
241 function Base_Name (File_Name : String) return String;
242 -- Return just the file name part without the extension (if present)
244 procedure Check_Existing_Executable (File_Name : String);
245 -- Delete any existing executable to avoid accidentally updating the target
246 -- of a symbolic link, but produce a Fatal_Error if File_Name matches any
247 -- of the source file names. This avoids overwriting of extensionless
248 -- source files by accident on systems where executables do not have
249 -- extensions.
251 procedure Delete (Name : String);
252 -- Wrapper to unlink as status is ignored by this application
254 procedure Error_Msg (Message : String);
255 -- Output the error or warning Message
257 procedure Exit_With_Error (Error : String);
258 -- Output Error and exit program with a fatal condition
260 procedure Process_Args;
261 -- Go through all the arguments and build option tables
263 procedure Process_Binder_File (Name : String);
264 -- Reads the binder file and extracts linker arguments
266 function Index (S, Pattern : String) return Natural;
267 -- Return the last occurrence of Pattern in S, or 0 if none
269 procedure Search_Library_Path
270 (Next_Line : String;
271 Nfirst : Integer;
272 Nlast : Integer;
273 Last : Integer;
274 GNAT_Static : Boolean;
275 GNAT_Shared : Boolean);
276 -- Given a Gnat standard library, search the library path to find the
277 -- library location. Parameters are documented in Process_Binder_File.
279 procedure Usage;
280 -- Display usage
282 procedure Write_Header;
283 -- Show user the program name, version and copyright
285 procedure Write_Usage;
286 -- Show user the program options
288 ---------------
289 -- Base_Name --
290 ---------------
292 function Base_Name (File_Name : String) return String is
293 Findex1 : Natural;
294 Findex2 : Natural;
296 begin
297 Findex1 := File_Name'First;
299 -- The file might be specified by a full path name. However,
300 -- we want the path to be stripped away.
302 for J in reverse File_Name'Range loop
303 if Is_Directory_Separator (File_Name (J)) then
304 Findex1 := J + 1;
305 exit;
306 end if;
307 end loop;
309 Findex2 := File_Name'Last;
310 while Findex2 > Findex1 and then File_Name (Findex2) /= '.' loop
311 Findex2 := Findex2 - 1;
312 end loop;
314 if Findex2 = Findex1 then
315 Findex2 := File_Name'Last + 1;
316 end if;
318 return File_Name (Findex1 .. Findex2 - 1);
319 end Base_Name;
321 -------------------------------
322 -- Check_Existing_Executable --
323 -------------------------------
325 procedure Check_Existing_Executable (File_Name : String) is
326 Ename : String := File_Name;
327 Efile : File_Name_Type;
328 Sfile : File_Name_Type;
330 begin
331 Canonical_Case_File_Name (Ename);
332 Name_Len := 0;
333 Add_Str_To_Name_Buffer (Ename);
334 Efile := Name_Find;
336 for J in Units.Table'First .. Units.Last loop
337 Sfile := Units.Table (J).Sfile;
338 if Sfile = Efile then
339 Exit_With_Error
340 ("executable name """ & File_Name & """ matches "
341 & "source file name """ & Get_Name_String (Sfile) & """");
342 end if;
343 end loop;
345 Delete (File_Name);
346 end Check_Existing_Executable;
348 ------------
349 -- Delete --
350 ------------
352 procedure Delete (Name : String) is
353 Status : int;
354 pragma Unreferenced (Status);
355 begin
356 Status := unlink (Name'Address);
357 end Delete;
359 ---------------
360 -- Error_Msg --
361 ---------------
363 procedure Error_Msg (Message : String) is
364 begin
365 Write_Str (Base_Command_Name.all);
366 Write_Str (": ");
367 Write_Str (Message);
368 Write_Eol;
369 end Error_Msg;
371 ---------------------
372 -- Exit_With_Error --
373 ---------------------
375 procedure Exit_With_Error (Error : String) is
376 begin
377 Error_Msg (Error);
378 Exit_Program (E_Fatal);
379 end Exit_With_Error;
381 -----------
382 -- Index --
383 -----------
385 function Index (S, Pattern : String) return Natural is
386 Len : constant Natural := Pattern'Length;
388 begin
389 for J in reverse S'First .. S'Last - Len + 1 loop
390 if Pattern = S (J .. J + Len - 1) then
391 return J;
392 end if;
393 end loop;
395 return 0;
396 end Index;
398 ------------------
399 -- Process_Args --
400 ------------------
402 procedure Process_Args is
403 Next_Arg : Integer;
405 Skip_Next : Boolean := False;
406 -- Set to true if the next argument is to be added into the list of
407 -- linker's argument without parsing it.
409 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
411 -- Start of processing for Process_Args
413 begin
414 -- First, check for --version and --help
416 Check_Version_And_Help ("GNATLINK", "1996");
418 -- Loop through arguments of gnatlink command
420 Next_Arg := 1;
421 loop
422 exit when Next_Arg > Argument_Count;
424 Process_One_Arg : declare
425 Arg : constant String := Argument (Next_Arg);
427 begin
428 -- This argument must not be parsed, just add it to the list of
429 -- linker's options.
431 if Skip_Next then
433 Skip_Next := False;
435 Linker_Options.Increment_Last;
436 Linker_Options.Table (Linker_Options.Last) :=
437 new String'(Arg);
439 -- Case of argument which is a switch
441 elsif Arg'Length /= 0 and then Arg (1) = '-' then
442 if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then
443 Exit_With_Error
444 ("invalid switch: """ & Arg & """ (gnat not needed here)");
445 end if;
447 if Arg = "-Xlinker" then
449 -- Next argument should be sent directly to the linker.
450 -- We do not want to parse it here.
452 Skip_Next := True;
454 Linker_Options.Increment_Last;
455 Linker_Options.Table (Linker_Options.Last) :=
456 new String'(Arg);
458 elsif Arg (2) = 'g'
459 and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
460 then
461 Debug_Flag_Present := True;
463 Linker_Options.Increment_Last;
464 Linker_Options.Table (Linker_Options.Last) :=
465 new String'(Arg);
467 Binder_Options.Increment_Last;
468 Binder_Options.Table (Binder_Options.Last) :=
469 Linker_Options.Table (Linker_Options.Last);
471 elsif Arg'Length >= 3 and then Arg (2) = 'M' then
472 declare
473 Switches : String_List_Access;
475 begin
476 Convert (Map_File, Arg (3 .. Arg'Last), Switches);
478 if Switches /= null then
479 for J in Switches'Range loop
480 Linker_Options.Increment_Last;
481 Linker_Options.Table (Linker_Options.Last) :=
482 Switches (J);
483 end loop;
484 end if;
485 end;
487 elsif Arg'Length = 2 then
488 case Arg (2) is
489 when 'f' =>
490 if Object_List_File_Supported then
491 Object_List_File_Required := True;
492 else
493 Exit_With_Error
494 ("Object list file not supported on this target");
495 end if;
497 when 'M' =>
498 Create_Map_File := True;
500 when 'n' =>
501 Compile_Bind_File := False;
503 when 'o' =>
504 Next_Arg := Next_Arg + 1;
506 if Next_Arg > Argument_Count then
507 Exit_With_Error ("Missing argument for -o");
508 end if;
510 Output_File_Name :=
511 new String'(Executable_Name
512 (Argument (Next_Arg),
513 Only_If_No_Suffix => True));
515 when 'P' =>
516 Opt.CodePeer_Mode := True;
518 when 'R' =>
519 Opt.Run_Path_Option := False;
521 when 'v' =>
523 -- Support "double" verbose mode. Second -v
524 -- gets sent to the linker and binder phases.
526 if Verbose_Mode then
527 Very_Verbose_Mode := True;
529 Linker_Options.Increment_Last;
530 Linker_Options.Table (Linker_Options.Last) :=
531 new String'(Arg);
533 Binder_Options.Increment_Last;
534 Binder_Options.Table (Binder_Options.Last) :=
535 Linker_Options.Table (Linker_Options.Last);
537 else
538 Verbose_Mode := True;
540 end if;
542 when others =>
543 Linker_Options.Increment_Last;
544 Linker_Options.Table (Linker_Options.Last) :=
545 new String'(Arg);
547 end case;
549 elsif Arg (2) = 'B' then
550 Linker_Options.Increment_Last;
551 Linker_Options.Table (Linker_Options.Last) :=
552 new String'(Arg);
554 Binder_Options.Increment_Last;
555 Binder_Options.Table (Binder_Options.Last) :=
556 Linker_Options.Table (Linker_Options.Last);
558 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
559 if Arg'Length = 7 then
560 Exit_With_Error ("Missing argument for --LINK=");
561 end if;
563 declare
564 L_Args : constant Argument_List_Access :=
565 Argument_String_To_List (Arg (8 .. Arg'Last));
566 begin
567 -- The linker program is the first argument
569 Linker_Path :=
570 System.OS_Lib.Locate_Exec_On_Path (L_Args.all (1).all);
572 if Linker_Path = null then
573 Exit_With_Error
574 ("Could not locate linker: " & L_Args.all (1).all);
575 end if;
577 -- The other arguments are passed as-is to the linker and
578 -- override those coming from --GCC= if any.
580 if L_Args.all'Last >= 2 then
581 Gcc_Linker_Options.Set_Last (0);
582 end if;
584 for J in 2 .. L_Args.all'Last loop
585 Gcc_Linker_Options.Increment_Last;
586 Gcc_Linker_Options.Table
587 (Gcc_Linker_Options.Last) :=
588 new String'(L_Args.all (J).all);
589 end loop;
590 end;
592 elsif Arg'Length >= 6 and then Arg (1 .. 6) = "--GCC=" then
593 if Arg'Length = 6 then
594 Exit_With_Error ("Missing argument for --GCC=");
595 end if;
597 declare
598 Program_Args : constant Argument_List_Access :=
599 Argument_String_To_List
600 (Arg (7 .. Arg'Last));
602 begin
603 if Program_Args.all (1).all /= Gcc.all then
604 Gcc := new String'(Program_Args.all (1).all);
605 Standard_Gcc := False;
606 end if;
608 -- Set appropriate flags for switches passed
610 for J in 2 .. Program_Args.all'Last loop
611 declare
612 Arg : constant String := Program_Args.all (J).all;
613 AF : constant Integer := Arg'First;
615 begin
616 if Arg'Length /= 0 and then Arg (AF) = '-' then
617 if Arg (AF + 1) = 'g'
618 and then (Arg'Length = 2
619 or else Arg (AF + 2) in '0' .. '3'
620 or else Arg (AF + 2 .. Arg'Last) = "coff")
621 then
622 Debug_Flag_Present := True;
623 end if;
624 end if;
626 -- Add directory to source search dirs so that
627 -- Get_Target_Parameters can find system.ads
629 if Arg (AF .. AF + 1) = "-I"
630 and then Arg'Length > 2
631 then
632 Add_Src_Search_Dir (Arg (AF + 2 .. Arg'Last));
633 end if;
635 -- Pass to gcc for compiling binder generated file
636 -- No use passing libraries, it will just generate
637 -- a warning
639 if not (Arg (AF .. AF + 1) = "-l"
640 or else Arg (AF .. AF + 1) = "-L")
641 then
642 Binder_Options.Increment_Last;
643 Binder_Options.Table (Binder_Options.Last) :=
644 new String'(Arg);
645 end if;
647 -- Pass to gcc for linking program
649 Gcc_Linker_Options.Increment_Last;
650 Gcc_Linker_Options.Table
651 (Gcc_Linker_Options.Last) := new String'(Arg);
652 end;
653 end loop;
654 end;
656 -- Send all multi-character switches not recognized as
657 -- a special case by gnatlink to the linker/loader stage.
659 else
660 Linker_Options.Increment_Last;
661 Linker_Options.Table (Linker_Options.Last) :=
662 new String'(Arg);
663 end if;
665 -- Here if argument is a file name rather than a switch
667 else
668 -- If explicit ali file, capture it
670 if Arg'Length > 4
671 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
672 then
673 if Ali_File_Name = null then
674 Ali_File_Name := new String'(Arg);
675 else
676 Exit_With_Error ("cannot handle more than one ALI file");
677 end if;
679 -- If target object file, record object file
681 elsif Arg'Length > Get_Target_Object_Suffix.all'Length
682 and then Arg
683 (Arg'Last -
684 Get_Target_Object_Suffix.all'Length + 1 .. Arg'Last)
685 = Get_Target_Object_Suffix.all
686 then
687 Linker_Objects.Increment_Last;
688 Linker_Objects.Table (Linker_Objects.Last) :=
689 new String'(Arg);
691 -- If host object file, record object file
693 elsif Arg'Length > Get_Object_Suffix.all'Length
694 and then Arg
695 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
696 = Get_Object_Suffix.all
697 then
698 Linker_Objects.Increment_Last;
699 Linker_Objects.Table (Linker_Objects.Last) :=
700 new String'(Arg);
702 -- If corresponding ali file exists, capture it
704 elsif Ali_File_Name = null
705 and then Is_Regular_File (Arg & ".ali")
706 then
707 Ali_File_Name := new String'(Arg & ".ali");
709 -- Otherwise assume this is a linker options entry, but
710 -- see below for interesting adjustment to this assumption.
712 else
713 Linker_Options.Increment_Last;
714 Linker_Options.Table (Linker_Options.Last) :=
715 new String'(Arg);
716 end if;
717 end if;
718 end Process_One_Arg;
720 Next_Arg := Next_Arg + 1;
721 end loop;
723 -- Compile the bind file with warnings suppressed, because
724 -- otherwise the with of the main program may cause junk warnings.
726 Binder_Options.Increment_Last;
727 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
729 -- If we did not get an ali file at all, and we had at least one
730 -- linker option, then assume that was the intended ali file after
731 -- all, so that we get a nicer message later on.
733 if Ali_File_Name = null
734 and then Linker_Options.Last >= Linker_Options.First
735 then
736 Ali_File_Name :=
737 new String'(Linker_Options.Table (Linker_Options.First).all
738 & ".ali");
739 end if;
740 end Process_Args;
742 -------------------------
743 -- Process_Binder_File --
744 -------------------------
746 procedure Process_Binder_File (Name : String) is
747 Fd : FILEs;
748 -- Binder file's descriptor
750 Link_Bytes : Integer := 0;
751 -- Projected number of bytes for the linker command line
753 Next_Line : String (1 .. 1000);
754 -- Current line value
756 Nlast : Integer;
757 Nfirst : Integer;
758 -- Current line slice (the slice does not contain line terminator)
760 Last : Integer;
761 -- Current line last character for shared libraries (without version)
763 Objs_Begin : Integer := 0;
764 -- First object file index in Linker_Objects table
766 Objs_End : Integer := 0;
767 -- Last object file index in Linker_Objects table
769 Status : int;
770 pragma Warnings (Off, Status);
771 -- Used for various Interfaces.C_Streams calls
773 Closing_Status : Boolean;
774 pragma Warnings (Off, Closing_Status);
775 -- For call to Close
777 GNAT_Static : Boolean := False;
778 -- Save state of -static option
780 GNAT_Shared : Boolean := False;
781 -- Save state of -shared option
783 Xlinker_Was_Previous : Boolean := False;
784 -- Indicate that "-Xlinker" was the option preceding the current option.
785 -- If True, then the current option is never suppressed.
787 -- Rollback data
789 -- These data items are used to store current binder file context. The
790 -- context is composed of the file descriptor position and the current
791 -- line together with the slice indexes (first and last position) for
792 -- this line. The rollback data are used by the Store_File_Context and
793 -- Rollback_File_Context routines below. The file context mechanism
794 -- interact only with the Get_Next_Line call. For example:
796 -- Store_File_Context;
797 -- Get_Next_Line;
798 -- Rollback_File_Context;
799 -- Get_Next_Line;
801 -- Both Get_Next_Line calls above will read the exact same data from
802 -- the file. In other words, Next_Line, Nfirst and Nlast variables
803 -- will be set with the exact same values.
805 RB_File_Pos : long; -- File position
806 RB_Next_Line : String (1 .. 1000); -- Current line content
807 RB_Nlast : Integer; -- Slice last index
808 RB_Nfirst : Integer; -- Slice first index
810 procedure Get_Next_Line;
811 -- Read the next line from the binder file without the line
812 -- terminator.
814 procedure Store_File_Context;
815 -- Store current file context, Fd position and current line data.
816 -- The file context is stored into the rollback data above (RB_*).
817 -- Store_File_Context can be called at any time, only the last call
818 -- will be used (i.e. this routine overwrites the file context).
820 procedure Rollback_File_Context;
821 -- Restore file context from rollback data. This routine must be called
822 -- after Store_File_Context. The binder file context will be restored
823 -- with the data stored by the last Store_File_Context call.
825 procedure Write_RF (S : String);
826 -- Write a string to the response file and check if it was successful.
827 -- Fail the program if it was not successful (disk full).
829 -------------------
830 -- Get_Next_Line --
831 -------------------
833 procedure Get_Next_Line is
834 Fchars : chars;
836 begin
837 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
839 if Fchars = System.Null_Address then
840 Exit_With_Error ("Error reading binder output");
841 end if;
843 Nfirst := Next_Line'First;
844 Nlast := Nfirst;
845 while Nlast <= Next_Line'Last
846 and then Next_Line (Nlast) /= ASCII.LF
847 and then Next_Line (Nlast) /= ASCII.CR
848 loop
849 Nlast := Nlast + 1;
850 end loop;
852 Nlast := Nlast - 1;
853 end Get_Next_Line;
855 ---------------------------
856 -- Rollback_File_Context --
857 ---------------------------
859 procedure Rollback_File_Context is
860 begin
861 Next_Line := RB_Next_Line;
862 Nfirst := RB_Nfirst;
863 Nlast := RB_Nlast;
864 Status := fseek (Fd, RB_File_Pos, Interfaces.C_Streams.SEEK_SET);
866 if Status = -1 then
867 Exit_With_Error ("Error setting file position");
868 end if;
869 end Rollback_File_Context;
871 ------------------------
872 -- Store_File_Context --
873 ------------------------
875 procedure Store_File_Context is
876 use type System.CRTL.long;
878 begin
879 RB_Next_Line := Next_Line;
880 RB_Nfirst := Nfirst;
881 RB_Nlast := Nlast;
882 RB_File_Pos := ftell (Fd);
884 if RB_File_Pos = -1 then
885 Exit_With_Error ("Error getting file position");
886 end if;
887 end Store_File_Context;
889 --------------
890 -- Write_RF --
891 --------------
893 procedure Write_RF (S : String) is
894 Success : Boolean := True;
895 Back_Slash : constant Character := '\';
897 begin
898 -- If a GNU response file is used, space and backslash need to be
899 -- escaped because they are interpreted as a string separator and
900 -- an escape character respectively by the underlying mechanism.
901 -- On the other hand, quote and double-quote are not escaped since
902 -- they are interpreted as string delimiters on both sides.
904 if Using_GNU_response_file then
905 for J in S'Range loop
906 if S (J) = ' ' or else S (J) = '\' then
907 if Write (Tname_FD, Back_Slash'Address, 1) /= 1 then
908 Success := False;
909 end if;
910 end if;
912 if Write (Tname_FD, S (J)'Address, 1) /= 1 then
913 Success := False;
914 end if;
915 end loop;
917 else
918 if Write (Tname_FD, S'Address, S'Length) /= S'Length then
919 Success := False;
920 end if;
921 end if;
923 if Write (Tname_FD, ASCII.LF'Address, 1) /= 1 then
924 Success := False;
925 end if;
927 if not Success then
928 Exit_With_Error ("Error generating response file: disk full");
929 end if;
930 end Write_RF;
932 -- Start of processing for Process_Binder_File
934 begin
935 Fd := fopen (Name'Address, Read_Mode'Address);
937 if Fd = NULL_Stream then
938 Exit_With_Error ("Failed to open binder output");
939 end if;
941 -- Skip up to the Begin Info line
943 loop
944 Get_Next_Line;
945 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
946 end loop;
948 loop
949 Get_Next_Line;
951 -- Go to end when end line is reached (this will happen in
952 -- High_Integrity_Mode where no -L switches are generated)
954 exit when Next_Line (Nfirst .. Nlast) = End_Info;
956 Next_Line (Nfirst .. Nlast - 8) := Next_Line (Nfirst + 8 .. Nlast);
957 Nlast := Nlast - 8;
959 -- Go to next section when switches are reached
961 exit when Next_Line (1) = '-';
963 -- Otherwise we have another object file to collect
965 Linker_Objects.Increment_Last;
967 -- Mark the positions of first and last object files in case they
968 -- need to be placed with a named file on systems having linker
969 -- line limitations.
971 if Objs_Begin = 0 then
972 Objs_Begin := Linker_Objects.Last;
973 end if;
975 Linker_Objects.Table (Linker_Objects.Last) :=
976 new String'(Next_Line (Nfirst .. Nlast));
978 -- Nlast - Nfirst + 1, for the size, plus one for the space between
979 -- each arguments.
981 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
982 end loop;
984 Objs_End := Linker_Objects.Last;
986 -- Continue to compute the Link_Bytes, the linker options are part of
987 -- command line length.
989 Store_File_Context;
991 while Next_Line (Nfirst .. Nlast) /= End_Info loop
992 Link_Bytes := Link_Bytes + Nlast - Nfirst + 2;
993 Get_Next_Line;
994 end loop;
996 Rollback_File_Context;
998 -- On systems that have limitations on handling very long linker lines
999 -- we make use of the system linker option which takes a list of object
1000 -- file names from a file instead of the command line itself. What we do
1001 -- is to replace the list of object files by the special linker option
1002 -- which then reads the object file list from a file instead. The option
1003 -- to read from a file instead of the command line is only triggered if
1004 -- a conservative threshold is passed.
1006 if Object_List_File_Required
1007 or else (Object_List_File_Supported
1008 and then Link_Bytes > Link_Max)
1009 then
1010 -- Create a temporary file containing the Ada user object files
1011 -- needed by the link. This list is taken from the bind file and is
1012 -- output one object per line for maximal compatibility with linkers
1013 -- supporting this option.
1015 Create_Temp_File (Tname_FD, Tname);
1017 -- ??? File descriptor should be checked to not be Invalid_FD.
1018 -- Status of Write and Close operations should be checked, and
1019 -- failure should occur if a status is wrong.
1021 for J in Objs_Begin .. Objs_End loop
1022 Write_RF (Linker_Objects.Table (J).all);
1024 Response_File_Objects.Increment_Last;
1025 Response_File_Objects.Table (Response_File_Objects.Last) :=
1026 Linker_Objects.Table (J);
1027 end loop;
1029 Close (Tname_FD, Closing_Status);
1031 -- Add the special objects list file option together with the name
1032 -- of the temporary file (removing the null character) to the objects
1033 -- file table.
1035 Linker_Objects.Table (Objs_Begin) :=
1036 new String'(Object_File_Option &
1037 Tname (Tname'First .. Tname'Last - 1));
1039 -- The slots containing these object file names are then removed
1040 -- from the objects table so they do not appear in the link. They are
1041 -- removed by moving up the linker options and non-Ada object files
1042 -- appearing after the Ada object list in the table.
1044 declare
1045 N : Integer;
1047 begin
1048 N := Objs_End - Objs_Begin + 1;
1050 for J in Objs_End + 1 .. Linker_Objects.Last loop
1051 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
1052 end loop;
1054 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
1055 end;
1056 end if;
1058 -- Process switches and options
1060 if Next_Line (Nfirst .. Nlast) /= End_Info then
1061 Xlinker_Was_Previous := False;
1063 loop
1064 if Xlinker_Was_Previous
1065 or else Next_Line (Nfirst .. Nlast) = "-Xlinker"
1066 then
1067 Linker_Options.Increment_Last;
1068 Linker_Options.Table (Linker_Options.Last) :=
1069 new String'(Next_Line (Nfirst .. Nlast));
1071 elsif Next_Line (Nfirst .. Nlast) = "-static" then
1072 GNAT_Static := True;
1074 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
1075 GNAT_Shared := True;
1077 -- Add binder options only if not already set on the command line.
1078 -- This rule is a way to control the linker options order.
1080 else
1081 if Nlast > Nfirst + 2 and then
1082 Next_Line (Nfirst .. Nfirst + 1) = "-L"
1083 then
1084 -- Construct a library search path for use later to locate
1085 -- static gnatlib libraries.
1087 if Libpath.Last > 1 then
1088 Libpath.Increment_Last;
1089 Libpath.Table (Libpath.Last) := Path_Separator;
1090 end if;
1092 for I in Nfirst + 2 .. Nlast loop
1093 Libpath.Increment_Last;
1094 Libpath.Table (Libpath.Last) := Next_Line (I);
1095 end loop;
1097 Linker_Options.Increment_Last;
1099 Linker_Options.Table (Linker_Options.Last) :=
1100 new String'(Next_Line (Nfirst .. Nlast));
1102 elsif Next_Line (Nfirst .. Nlast) = "-lgnarl"
1103 or else Next_Line (Nfirst .. Nlast) = "-lgnat"
1104 or else
1105 Next_Line
1106 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) =
1107 Shared_Lib ("gnarl")
1108 or else
1109 Next_Line
1110 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) =
1111 Shared_Lib ("gnat")
1112 then
1113 -- If it is a shared library, remove the library version.
1114 -- We will be looking for the static version of the library
1115 -- as it is in the same directory as the shared version.
1117 if Nlast >= Library_Version'Length
1118 and then
1119 Next_Line (Nlast - Library_Version'Length + 1 .. Nlast) =
1120 Library_Version
1121 then
1122 -- Set Last to point to last character before the
1123 -- library version.
1125 Last := Nlast - Library_Version'Length - 1;
1126 else
1127 Last := Nlast;
1128 end if;
1130 Search_Library_Path
1131 (Next_Line => Next_Line,
1132 Nfirst => Nfirst,
1133 Nlast => Nlast,
1134 Last => Last,
1135 GNAT_Static => GNAT_Static,
1136 GNAT_Shared => GNAT_Shared);
1138 else
1139 Linker_Options.Increment_Last;
1140 Linker_Options.Table (Linker_Options.Last) :=
1141 new String'(Next_Line (Nfirst .. Nlast));
1142 end if;
1143 end if;
1145 Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker";
1147 Get_Next_Line;
1148 exit when Next_Line (Nfirst .. Nlast) = End_Info;
1150 Next_Line (Nfirst .. Nlast - 8) := Next_Line (Nfirst + 8 .. Nlast);
1151 Nlast := Nlast - 8;
1152 end loop;
1153 end if;
1155 -- If -shared was specified, invoke gcc with -shared-libgcc
1157 if GNAT_Shared then
1158 Linker_Options.Increment_Last;
1159 Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc;
1160 end if;
1162 Status := fclose (Fd);
1163 end Process_Binder_File;
1165 -------------------------
1166 -- Search_Library_Path --
1167 -------------------------
1169 procedure Search_Library_Path
1170 (Next_Line : String;
1171 Nfirst : Integer;
1172 Nlast : Integer;
1173 Last : Integer;
1174 GNAT_Static : Boolean;
1175 GNAT_Shared : Boolean)
1177 File_Path : String_Access;
1179 Object_Lib_Extension : constant String :=
1180 Value (Object_Library_Ext_Ptr);
1182 File_Name : constant String := "lib" &
1183 Next_Line (Nfirst + 2 .. Last) & Object_Lib_Extension;
1185 Run_Path_Opt : constant String :=
1186 Value (Run_Path_Option_Ptr);
1188 GCC_Index : Natural;
1189 Run_Path_Opt_Index : Natural := 0;
1191 begin
1192 File_Path :=
1193 Locate_Regular_File (File_Name,
1194 String (Libpath.Table (1 .. Libpath.Last)));
1196 if File_Path /= null then
1197 if GNAT_Static then
1199 -- If static gnatlib found, explicitly specify to overcome
1200 -- possible linker default usage of shared version.
1202 Linker_Options.Increment_Last;
1204 Linker_Options.Table (Linker_Options.Last) :=
1205 new String'(File_Path.all);
1207 elsif GNAT_Shared then
1208 if Opt.Run_Path_Option then
1210 -- If shared gnatlib desired, add appropriate system specific
1211 -- switch so that it can be located at runtime.
1213 if Run_Path_Opt'Length /= 0 then
1215 -- Output the system specific linker command that allows the
1216 -- image activator to find the shared library at
1217 -- runtime. Also add path to find libgcc_s.so, if relevant.
1219 declare
1220 Path : String (1 .. File_Path'Length + 15);
1222 Path_Last : constant Natural := File_Path'Length;
1224 begin
1225 Path (1 .. File_Path'Length) := File_Path.all;
1227 -- To find the location of the shared version of libgcc, we
1228 -- look for "gcc-lib" in the path of the library. However,
1229 -- this subdirectory is no longer present in recent versions
1230 -- of GCC. So, we look for the last subdirectory "lib" in
1231 -- the path.
1233 GCC_Index := Index (Path (1 .. Path_Last), "gcc-lib");
1235 if GCC_Index /= 0 then
1237 -- The shared version of libgcc is located in the
1238 -- parent directory.
1240 GCC_Index := GCC_Index - 1;
1242 else
1243 GCC_Index := Index (Path (1 .. Path_Last), "/lib/");
1245 if GCC_Index = 0 then
1246 GCC_Index :=
1247 Index (Path (1 .. Path_Last),
1248 Directory_Separator & "lib"
1249 & Directory_Separator);
1250 end if;
1252 -- If we have found a "lib" subdir in the path to
1253 -- libgnat, the possible shared libgcc of interest by
1254 -- default is in libgcc_subdir at the same level.
1256 if GCC_Index /= 0 then
1257 declare
1258 Subdir : constant String :=
1259 Value (Libgcc_Subdir_Ptr);
1261 begin
1262 Path (GCC_Index + 1 .. GCC_Index + Subdir'Length)
1263 := Subdir;
1264 GCC_Index := GCC_Index + Subdir'Length;
1265 end;
1266 end if;
1267 end if;
1269 -- Look for an eventual run_path_option in
1270 -- the linker switches.
1272 if Separate_Run_Path_Options then
1273 Linker_Options.Increment_Last;
1274 Linker_Options.Table
1275 (Linker_Options.Last) :=
1276 new String'
1277 (Run_Path_Opt
1278 & File_Path
1279 (1 .. File_Path'Length
1280 - File_Name'Length));
1282 if GCC_Index /= 0 then
1283 Linker_Options.Increment_Last;
1284 Linker_Options.Table (Linker_Options.Last) :=
1285 new String'
1286 (Run_Path_Opt
1287 & Path (1 .. GCC_Index));
1288 end if;
1290 else
1291 for J in reverse 1 .. Linker_Options.Last loop
1292 if Linker_Options.Table (J) /= null
1293 and then
1294 Linker_Options.Table (J)'Length
1295 > Run_Path_Opt'Length
1296 and then
1297 Linker_Options.Table (J)
1298 (1 .. Run_Path_Opt'Length) =
1299 Run_Path_Opt
1300 then
1301 -- We have found an already specified
1302 -- run_path_option: we will add to this switch,
1303 -- because only one run_path_option should be
1304 -- specified.
1306 Run_Path_Opt_Index := J;
1307 exit;
1308 end if;
1309 end loop;
1311 -- If there is no run_path_option, we need to add one.
1313 if Run_Path_Opt_Index = 0 then
1314 Linker_Options.Increment_Last;
1315 end if;
1317 if GCC_Index = 0 then
1318 if Run_Path_Opt_Index = 0 then
1319 Linker_Options.Table
1320 (Linker_Options.Last) :=
1321 new String'
1322 (Run_Path_Opt
1323 & File_Path
1324 (1 .. File_Path'Length
1325 - File_Name'Length));
1327 else
1328 Linker_Options.Table
1329 (Run_Path_Opt_Index) :=
1330 new String'
1331 (Linker_Options.Table
1332 (Run_Path_Opt_Index).all
1333 & Path_Separator
1334 & File_Path
1335 (1 .. File_Path'Length
1336 - File_Name'Length));
1337 end if;
1339 else
1340 if Run_Path_Opt_Index = 0 then
1341 Linker_Options.Table
1342 (Linker_Options.Last) :=
1343 new String'
1344 (Run_Path_Opt
1345 & File_Path
1346 (1 .. File_Path'Length
1347 - File_Name'Length)
1348 & Path_Separator
1349 & Path (1 .. GCC_Index));
1351 else
1352 Linker_Options.Table
1353 (Run_Path_Opt_Index) :=
1354 new String'
1355 (Linker_Options.Table
1356 (Run_Path_Opt_Index).all
1357 & Path_Separator
1358 & File_Path
1359 (1 .. File_Path'Length
1360 - File_Name'Length)
1361 & Path_Separator
1362 & Path (1 .. GCC_Index));
1363 end if;
1364 end if;
1365 end if;
1366 end;
1367 end if;
1368 end if;
1370 -- Then we add the appropriate -l switch
1372 Linker_Options.Increment_Last;
1373 Linker_Options.Table (Linker_Options.Last) :=
1374 new String'(Next_Line (Nfirst .. Nlast));
1375 end if;
1377 else
1378 -- If gnatlib library not found, then add it anyway in
1379 -- case some other mechanism may find it.
1381 Linker_Options.Increment_Last;
1382 Linker_Options.Table (Linker_Options.Last) :=
1383 new String'(Next_Line (Nfirst .. Nlast));
1384 end if;
1385 end Search_Library_Path;
1387 -----------
1388 -- Usage --
1389 -----------
1391 procedure Usage is
1392 begin
1393 Write_Str ("Usage: ");
1394 Write_Str (Base_Command_Name.all);
1395 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
1396 Write_Eol;
1397 Write_Eol;
1398 Write_Line (" mainprog.ali the ALI file of the main program");
1399 Write_Eol;
1400 Write_Eol;
1401 Display_Usage_Version_And_Help;
1402 Write_Line (" -f Force object file list to be generated");
1403 Write_Line (" -g Compile binder source file with debug information");
1404 Write_Line (" -n Do not compile the binder source file");
1405 Write_Line (" -P Process files for use by CodePeer");
1406 Write_Line (" -R Do not use a run_path_option");
1407 Write_Line (" -v Verbose mode");
1408 Write_Line (" -v -v Very verbose mode");
1409 Write_Eol;
1410 Write_Line (" -o nam Use 'nam' as the name of the executable");
1411 Write_Line (" -Bdir Load compiler executables from dir");
1413 if Is_Supported (Map_File) then
1414 Write_Line (" -Mmap Create map file map");
1415 Write_Line (" -M Create map file mainprog.map");
1416 end if;
1418 Write_Line (" --GCC=comp Use 'comp' as the compiler rather than 'gcc'");
1419 Write_Line (" --LINK=lnk Use 'lnk' as the linker rather than 'gcc'");
1420 Write_Eol;
1421 Write_Line (" [non-Ada-objects] list of non Ada object files");
1422 Write_Line (" [linker-options] other options for the linker");
1423 end Usage;
1425 ------------------
1426 -- Write_Header --
1427 ------------------
1429 procedure Write_Header is
1430 begin
1431 if Verbose_Mode then
1432 Write_Eol;
1433 Display_Version ("GNATLINK", "1995");
1434 end if;
1435 end Write_Header;
1437 -----------------
1438 -- Write_Usage --
1439 -----------------
1441 procedure Write_Usage is
1442 begin
1443 Write_Header;
1444 Usage;
1445 end Write_Usage;
1447 -- Start of processing for Gnatlink
1449 begin
1450 -- Add the directory where gnatlink is invoked in front of the path, if
1451 -- gnatlink is invoked with directory information.
1453 declare
1454 Command : constant String := Command_Name;
1455 begin
1456 for Index in reverse Command'Range loop
1457 if Command (Index) = Directory_Separator then
1458 declare
1459 Absolute_Dir : constant String :=
1460 Normalize_Pathname
1461 (Command (Command'First .. Index));
1463 PATH : constant String :=
1464 Absolute_Dir &
1465 Path_Separator &
1466 Getenv ("PATH").all;
1468 begin
1469 Setenv ("PATH", PATH);
1470 end;
1472 exit;
1473 end if;
1474 end loop;
1475 end;
1477 Base_Command_Name := new String'(Base_Name (Command_Name));
1478 Process_Args;
1480 if Argument_Count = 0
1481 or else (Verbose_Mode and then Argument_Count = 1)
1482 then
1483 Write_Usage;
1484 Exit_Program (E_Fatal);
1485 end if;
1487 -- Initialize packages to be used
1489 Csets.Initialize;
1490 Snames.Initialize;
1492 -- We always compile with -c
1494 Binder_Options_From_ALI.Increment_Last;
1495 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1496 new String'("-c");
1498 if Ali_File_Name = null then
1499 Exit_With_Error ("no ali file given for link");
1500 end if;
1502 if not Is_Regular_File (Ali_File_Name.all) then
1503 Exit_With_Error (Ali_File_Name.all & " not found");
1504 end if;
1506 -- Read the ALI file of the main subprogram if the binder generated file
1507 -- needs to be compiled and no --GCC= switch has been specified. Fetch the
1508 -- back end switches from this ALI file and use these switches to compile
1509 -- the binder generated file
1511 if Compile_Bind_File and then Standard_Gcc then
1512 Initialize_ALI;
1513 Name_Len := Ali_File_Name'Length;
1514 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1516 declare
1517 use Types;
1518 F : constant File_Name_Type := Name_Find;
1519 T : Text_Buffer_Ptr;
1520 A : ALI_Id;
1522 begin
1523 -- Load the ALI file
1525 T := Read_Library_Info (F, True);
1527 -- Read it. Note that we ignore errors, since we only want very
1528 -- limited information from the ali file, and likely a slightly
1529 -- wrong version will be just fine, though in normal operation
1530 -- we don't expect this to happen.
1532 A := Scan_ALI
1535 Err => False,
1536 Ignore_Errors => True);
1538 if A /= No_ALI_Id then
1540 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg ..
1541 Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1542 loop
1543 -- Do not compile with the front end switches. However, --RTS
1544 -- is to be dealt with specially because it needs to be passed
1545 -- to compile the file generated by the binder.
1547 declare
1548 Arg : String_Ptr renames Args.Table (Index);
1549 begin
1550 if not Is_Front_End_Switch (Arg.all) then
1551 Binder_Options_From_ALI.Increment_Last;
1552 Binder_Options_From_ALI.Table
1553 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1555 -- GNAT doesn't support GCC's multilib mechanism when it
1556 -- is configured with --disable-libada. This means that,
1557 -- when a multilib switch is used to request a particular
1558 -- compilation mode, the corresponding --RTS switch must
1559 -- also be specified. It is convenient to eliminate the
1560 -- redundancy by keying the compilation mode on a single
1561 -- switch, namely --RTS, and have the compiler reinstate
1562 -- the multilib switch (see gcc-interface/lang-specs.h).
1563 -- This switch must be passed to the driver at link time.
1565 if Arg'Length = 5
1566 and then Arg (Arg'First + 1 .. Arg'First + 4) = "mrtp"
1567 then
1568 Linker_Options.Increment_Last;
1569 Linker_Options.Table
1570 (Linker_Options.Last) := String_Access (Arg);
1571 end if;
1573 elsif Arg'Length > 5
1574 and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1575 then
1576 Binder_Options_From_ALI.Increment_Last;
1577 Binder_Options_From_ALI.Table
1578 (Binder_Options_From_ALI.Last) := String_Access (Arg);
1580 -- Set the RTS_*_Path_Name variables, so that
1581 -- the correct directories will be set when
1582 -- Osint.Add_Default_Search_Dirs will be called later.
1584 Opt.RTS_Src_Path_Name :=
1585 Get_RTS_Search_Dir
1586 (Arg (Arg'First + 6 .. Arg'Last), Include);
1588 Opt.RTS_Lib_Path_Name :=
1589 Get_RTS_Search_Dir
1590 (Arg (Arg'First + 6 .. Arg'Last), Objects);
1591 end if;
1592 end;
1593 end loop;
1594 end if;
1595 end;
1596 end if;
1598 -- Get target parameters
1600 Osint.Add_Default_Search_Dirs;
1601 Targparm.Get_Target_Parameters;
1603 -- Compile the bind file with the following switches:
1605 -- -gnatA stops reading gnat.adc, since we don't know what
1606 -- pragmas would work, and we do not need it anyway.
1608 -- -gnatWb allows brackets coding for wide characters
1610 -- -gnatiw allows wide characters in identifiers. This is needed
1611 -- because bindgen uses brackets encoding for all upper
1612 -- half and wide characters in identifier names.
1614 -- In addition, in CodePeer mode compile with -x adascil -gnatcC
1616 Binder_Options_From_ALI.Increment_Last;
1617 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1618 new String'("-gnatA");
1619 Binder_Options_From_ALI.Increment_Last;
1620 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1621 new String'("-gnatWb");
1622 Binder_Options_From_ALI.Increment_Last;
1623 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1624 new String'("-gnatiw");
1626 if Opt.CodePeer_Mode then
1627 Binder_Options_From_ALI.Increment_Last;
1628 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1629 new String'("-x");
1630 Binder_Options_From_ALI.Increment_Last;
1631 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1632 new String'("adascil");
1633 Binder_Options_From_ALI.Increment_Last;
1634 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
1635 new String'("-gnatcC");
1636 end if;
1638 -- Locate all the necessary programs and verify required files are present
1640 Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all);
1642 if Gcc_Path = null then
1643 Exit_With_Error ("Couldn't locate " & Gcc.all);
1644 end if;
1646 if Linker_Path = null then
1647 Linker_Path := Gcc_Path;
1648 end if;
1650 Write_Header;
1652 Target_Debuggable_Suffix := Get_Target_Debuggable_Suffix;
1654 -- If no output name specified, then use the base name of .ali file name
1656 if Output_File_Name = null then
1657 Output_File_Name :=
1658 new String'(Base_Name (Ali_File_Name.all)
1659 & Target_Debuggable_Suffix.all);
1660 end if;
1662 Linker_Options.Increment_Last;
1663 Linker_Options.Table (Linker_Options.Last) := new String'("-o");
1665 Linker_Options.Increment_Last;
1666 Linker_Options.Table (Linker_Options.Last) :=
1667 new String'(Output_File_Name.all);
1669 Check_Existing_Executable (Output_File_Name.all);
1671 -- Warn if main program is called "test", as that may be a built-in command
1672 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1673 -- will not appear. However, do not warn in the case of a cross compiler.
1675 -- Assume this is a cross tool if the executable name is not gnatlink.
1676 -- Note that the executable name is also gnatlink on windows, but in that
1677 -- case the output file name will be test.exe rather than test.
1679 if Base_Command_Name.all = "gnatlink"
1680 and then Output_File_Name.all = "test"
1681 then
1682 Error_Msg ("warning: executable name """ & Output_File_Name.all
1683 & """ may conflict with shell command");
1684 end if;
1686 -- Special warnings for worrisome file names on windows
1688 -- Recent versions of Windows by default cause privilege escalation if an
1689 -- executable file name contains substrings "install", "setup", "update"
1690 -- or "patch". A console application will typically fail to load as a
1691 -- result, so we should warn the user.
1693 Bad_File_Names_On_Windows : declare
1694 FN : String := Output_File_Name.all;
1696 procedure Check_File_Name (S : String);
1697 -- Warn if file name has the substring S
1699 procedure Check_File_Name (S : String) is
1700 begin
1701 if Ada.Strings.Fixed.Index (FN, S) /= 0 then
1702 Error_Msg
1703 ("warning: executable file name """ & Output_File_Name.all
1704 & """ contains substring """ & S & '"');
1705 Error_Msg
1706 ("admin privileges may be required to run this file");
1707 end if;
1708 end Check_File_Name;
1710 -- Start of processing for Bad_File_Names_On_Windows
1712 begin
1713 for J in FN'Range loop
1714 FN (J) := Csets.Fold_Lower (FN (J));
1715 end loop;
1717 -- For now we detect Windows by its executable suffix of .exe
1719 if Target_Debuggable_Suffix.all = ".exe" then
1720 Check_File_Name ("install");
1721 Check_File_Name ("setup");
1722 Check_File_Name ("update");
1723 Check_File_Name ("patch");
1724 end if;
1725 end Bad_File_Names_On_Windows;
1727 -- If -M switch was specified, add the switches to create the map file
1729 if Create_Map_File then
1730 declare
1731 Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map";
1732 Switches : String_List_Access;
1734 begin
1735 Convert (Map_File, Map_Name, Switches);
1737 if Switches /= null then
1738 for J in Switches'Range loop
1739 Linker_Options.Increment_Last;
1740 Linker_Options.Table (Linker_Options.Last) := Switches (J);
1741 end loop;
1742 end if;
1743 end;
1744 end if;
1746 -- Perform consistency checks
1748 -- Transform the .ali file name into the binder output file name
1750 Make_Binder_File_Names : declare
1751 Fname : constant String := Base_Name (Ali_File_Name.all);
1752 Fname_Len : Integer := Fname'Length;
1754 Maximum_File_Name_Length : constant Integer :=
1755 Get_Maximum_File_Name_Length;
1757 Bind_File_Prefix : Types.String_Ptr;
1758 -- Contains prefix used for bind files
1760 begin
1761 -- Set prefix
1763 Bind_File_Prefix := new String'("b~");
1765 -- If the length of the binder file becomes too long due to
1766 -- the addition of the "b?" prefix, then truncate it.
1768 if Maximum_File_Name_Length > 0 then
1769 while Fname_Len >
1770 Maximum_File_Name_Length - Bind_File_Prefix.all'Length
1771 loop
1772 Fname_Len := Fname_Len - 1;
1773 end loop;
1774 end if;
1776 declare
1777 Fnam : constant String :=
1778 Bind_File_Prefix.all &
1779 Fname (Fname'First .. Fname'First + Fname_Len - 1);
1781 begin
1782 Binder_Spec_Src_File := new String'(Fnam & ".ads");
1783 Binder_Body_Src_File := new String'(Fnam & ".adb");
1784 Binder_Ali_File := new String'(Fnam & ".ali");
1786 Binder_Obj_File := new String'(Fnam & Get_Target_Object_Suffix.all);
1787 end;
1789 if Fname_Len /= Fname'Length then
1790 Binder_Options.Increment_Last;
1791 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1792 Binder_Options.Increment_Last;
1793 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1794 end if;
1795 end Make_Binder_File_Names;
1797 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1799 -- Compile the binder file. This is fast, so we always do it, unless
1800 -- specifically told not to by the -n switch
1802 if Compile_Bind_File then
1803 Bind_Step : declare
1804 Success : Boolean;
1806 Args : Argument_List
1807 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1809 begin
1810 for J in 1 .. Binder_Options_From_ALI.Last loop
1811 Args (J) := Binder_Options_From_ALI.Table (J);
1812 end loop;
1814 for J in 1 .. Binder_Options.Last loop
1815 Args (Binder_Options_From_ALI.Last + J) :=
1816 Binder_Options.Table (J);
1817 end loop;
1819 -- Use the full path of the binder generated source, so that it is
1820 -- guaranteed that the debugger will find this source, even with
1821 -- STABS.
1823 Args (Args'Last) :=
1824 new String'(Normalize_Pathname (Binder_Body_Src_File.all));
1826 if Verbose_Mode then
1827 Write_Str (Base_Name (Gcc_Path.all));
1829 for J in Args'Range loop
1830 Write_Str (" ");
1831 Write_Str (Args (J).all);
1832 end loop;
1834 Write_Eol;
1835 end if;
1837 System.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1839 if not Success then
1840 Exit_Program (E_Fatal);
1841 end if;
1842 end Bind_Step;
1843 end if;
1845 -- In CodePeer mode, there's nothing left to do after the binder file has
1846 -- been compiled.
1848 if Opt.CodePeer_Mode then
1849 if Tname_FD /= Invalid_FD then
1850 Delete (Tname);
1851 end if;
1853 return;
1854 end if;
1856 -- Now, actually link the program
1858 Link_Step : declare
1859 Num_Args : Natural :=
1860 (Linker_Options.Last - Linker_Options.First + 1) +
1861 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1862 (Linker_Objects.Last - Linker_Objects.First + 1);
1863 Stack_Op : Boolean := False;
1865 begin
1866 -- Remove duplicate stack size setting from the Linker_Options table.
1867 -- The stack setting option "-Xlinker --stack=R,C" can be found
1868 -- in one line when set by a pragma Linker_Options or in two lines
1869 -- ("-Xlinker" then "--stack=R,C") when set on the command line. We
1870 -- also check for the "-Wl,--stack=R" style option.
1872 -- We must remove the second stack setting option instance because
1873 -- the one on the command line will always be the first one. And any
1874 -- subsequent stack setting option will overwrite the previous one.
1875 -- This is done especially for GNAT/NT where we set the stack size
1876 -- for tasking programs by a pragma in the NT specific tasking
1877 -- package System.Task_Primitives.Operations.
1879 -- Note: This is not a FOR loop that runs from Linker_Options.First
1880 -- to Linker_Options.Last, since operations within the loop can
1881 -- modify the length of the table.
1883 Clean_Link_Option_Set : declare
1884 J : Natural;
1885 Shared_Libgcc_Seen : Boolean := False;
1886 Static_Libgcc_Seen : Boolean := False;
1888 function Is_Prefix
1889 (Complete_String : String; Prefix : String) return Boolean;
1890 -- Returns whether Prefix is a prefix of Complete_String
1892 ---------------
1893 -- Is_Prefix --
1894 ---------------
1896 function Is_Prefix
1897 (Complete_String : String; Prefix : String) return Boolean
1899 S : String renames Complete_String;
1900 P : String renames Prefix;
1901 begin
1902 return P'Length <= S'Length
1903 and then S (S'First .. S'First + P'Length - 1) = P;
1904 end Is_Prefix;
1906 begin
1907 J := Linker_Options.First;
1908 while J <= Linker_Options.Last loop
1909 if Linker_Options.Table (J).all = "-Xlinker"
1910 and then J < Linker_Options.Last
1911 and then Linker_Options.Table (J + 1)'Length > 8
1912 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
1913 then
1914 if Stack_Op then
1915 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
1916 Linker_Options.Table (J + 2 .. Linker_Options.Last);
1917 Linker_Options.Decrement_Last;
1918 Linker_Options.Decrement_Last;
1919 Num_Args := Num_Args - 2;
1921 else
1922 Stack_Op := True;
1923 end if;
1924 end if;
1926 -- Remove duplicate -shared-libgcc switches
1928 if Linker_Options.Table (J).all = Shared_Libgcc_String then
1929 if Shared_Libgcc_Seen then
1930 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1931 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1932 Linker_Options.Decrement_Last;
1933 Num_Args := Num_Args - 1;
1935 else
1936 Shared_Libgcc_Seen := True;
1937 end if;
1938 end if;
1940 -- Remove duplicate -static-libgcc switches
1942 if Linker_Options.Table (J).all = Static_Libgcc_String then
1943 if Static_Libgcc_Seen then
1944 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1945 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1946 Linker_Options.Decrement_Last;
1947 Num_Args := Num_Args - 1;
1949 else
1950 Static_Libgcc_Seen := True;
1951 end if;
1952 end if;
1954 -- Here we just check for a canonical form that matches the
1955 -- pragma Linker_Options set in the NT runtime.
1957 if Is_Prefix
1958 (Complete_String => Linker_Options.Table (J).all,
1959 Prefix => "-Xlinker --stack=")
1960 or else Is_Prefix
1961 (Complete_String => Linker_Options.Table (J).all,
1962 Prefix => "-Wl,--stack=")
1963 then
1964 if Stack_Op then
1965 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1966 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1967 Linker_Options.Decrement_Last;
1968 Num_Args := Num_Args - 1;
1970 else
1971 Stack_Op := True;
1972 end if;
1973 end if;
1975 J := J + 1;
1976 end loop;
1978 if Linker_Path = Gcc_Path then
1980 -- For systems where the default is to link statically with
1981 -- libgcc, if gcc is not called with -shared-libgcc, call it
1982 -- with -static-libgcc, as there are some platforms where one
1983 -- of these two switches is compulsory to link.
1984 -- Don't push extra switches if we already saw one.
1986 if Shared_Libgcc_Default = 'T'
1987 and then not Shared_Libgcc_Seen
1988 and then not Static_Libgcc_Seen
1989 then
1990 Linker_Options.Increment_Last;
1991 Linker_Options.Table (Linker_Options.Last) := Static_Libgcc;
1992 Num_Args := Num_Args + 1;
1993 end if;
1995 -- Likewise, the reverse.
1997 if Shared_Libgcc_Default = 'H'
1998 and then not Static_Libgcc_Seen
1999 and then not Shared_Libgcc_Seen
2000 then
2001 Linker_Options.Increment_Last;
2002 Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc;
2003 Num_Args := Num_Args + 1;
2004 end if;
2005 end if;
2006 end Clean_Link_Option_Set;
2008 -- Prepare arguments for call to linker
2010 Call_Linker : declare
2011 Success : Boolean;
2012 Args : Argument_List (1 .. Num_Args + 1);
2013 Index : Integer := Args'First;
2015 begin
2016 Args (Index) := Binder_Obj_File;
2018 -- Add the object files and any -largs libraries
2020 for J in Linker_Objects.First .. Linker_Objects.Last loop
2021 Index := Index + 1;
2022 Args (Index) := Linker_Objects.Table (J);
2023 end loop;
2025 -- Add the linker options from the binder file
2027 for J in Linker_Options.First .. Linker_Options.Last loop
2028 Index := Index + 1;
2029 Args (Index) := Linker_Options.Table (J);
2030 end loop;
2032 -- Finally add the libraries from the --GCC= switch
2034 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
2035 Index := Index + 1;
2036 Args (Index) := Gcc_Linker_Options.Table (J);
2037 end loop;
2039 if Verbose_Mode then
2040 Write_Str (Linker_Path.all);
2042 for J in Args'Range loop
2043 Write_Str (" ");
2044 Write_Str (Args (J).all);
2045 end loop;
2047 Write_Eol;
2049 -- If we are on very verbose mode (-v -v) and a response file
2050 -- is used we display its content.
2052 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
2053 Write_Eol;
2054 Write_Str ("Response file (" &
2055 Tname (Tname'First .. Tname'Last - 1) &
2056 ") content : ");
2057 Write_Eol;
2059 for J in
2060 Response_File_Objects.First .. Response_File_Objects.Last
2061 loop
2062 Write_Str (Response_File_Objects.Table (J).all);
2063 Write_Eol;
2064 end loop;
2066 Write_Eol;
2067 end if;
2068 end if;
2070 System.OS_Lib.Spawn (Linker_Path.all, Args, Success);
2072 -- Delete the temporary file used in conjunction with linking if one
2073 -- was created. See Process_Bind_File for details.
2075 if Tname_FD /= Invalid_FD then
2076 Delete (Tname);
2077 end if;
2079 if not Success then
2080 Error_Msg ("error when calling " & Linker_Path.all);
2081 Exit_Program (E_Fatal);
2082 end if;
2083 end Call_Linker;
2084 end Link_Step;
2086 -- Only keep the binder output file and it's associated object
2087 -- file if compiling with the -g option. These files are only
2088 -- useful if debugging.
2090 if not Debug_Flag_Present then
2091 Delete (Binder_Ali_File.all & ASCII.NUL);
2092 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
2093 Delete (Binder_Body_Src_File.all & ASCII.NUL);
2094 Delete (Binder_Obj_File.all & ASCII.NUL);
2095 end if;
2097 Exit_Program (E_Success);
2099 exception
2100 when X : others =>
2101 Write_Line (Exception_Information (X));
2102 Exit_With_Error ("INTERNAL ERROR. Please report");
2103 end Gnatlink;