Add hppa-openbsd target
[official-gcc.git] / gcc / ada / gnatlink.adb
blob87ba0598f1fb6e1c456b8ea762c6817b8166229b
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T L I N K --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1996-2002 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 -- --
26 ------------------------------------------------------------------------------
28 -- Gnatlink usage: please consult the gnat documentation
30 with Ada.Exceptions; use Ada.Exceptions;
31 with ALI; use ALI;
32 with Gnatvsn; use Gnatvsn;
33 with Hostparm;
34 with Namet; use Namet;
35 with Osint; use Osint;
36 with Output; use Output;
37 with Switch; use Switch;
38 with System; use System;
39 with Table;
40 with Types;
42 with Ada.Command_Line; use Ada.Command_Line;
43 with GNAT.OS_Lib; use GNAT.OS_Lib;
44 with Interfaces.C_Streams; use Interfaces.C_Streams;
46 procedure Gnatlink is
48 pragma Ident (Gnat_Version_String);
50 package Gcc_Linker_Options is new Table.Table (
51 Table_Component_Type => String_Access,
52 Table_Index_Type => Integer,
53 Table_Low_Bound => 1,
54 Table_Initial => 20,
55 Table_Increment => 100,
56 Table_Name => "Gnatlink.Gcc_Linker_Options");
57 -- Comments needed ???
59 package Libpath is new Table.Table (
60 Table_Component_Type => Character,
61 Table_Index_Type => Integer,
62 Table_Low_Bound => 1,
63 Table_Initial => 4096,
64 Table_Increment => 2,
65 Table_Name => "Gnatlink.Libpath");
66 -- Comments needed ???
68 package Linker_Options is new Table.Table (
69 Table_Component_Type => String_Access,
70 Table_Index_Type => Integer,
71 Table_Low_Bound => 1,
72 Table_Initial => 20,
73 Table_Increment => 100,
74 Table_Name => "Gnatlink.Linker_Options");
75 -- Comments needed ???
77 package Linker_Objects is new Table.Table (
78 Table_Component_Type => String_Access,
79 Table_Index_Type => Integer,
80 Table_Low_Bound => 1,
81 Table_Initial => 20,
82 Table_Increment => 100,
83 Table_Name => "Gnatlink.Linker_Objects");
84 -- This table collects the objects file to be passed to the linker. In the
85 -- case where the linker command line is too long then programs objects
86 -- are put on the Response_File_Objects table. Note that the binder object
87 -- file and the user's objects remain in this table. This is very
88 -- important because on the GNU linker command line the -L switch is not
89 -- used to look for objects files but -L switch is used to look for
90 -- objects listed in the response file. This is not a problem with the
91 -- applications objects as they are specified with a fullname.
93 package Response_File_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.Response_File_Objects");
100 -- This table collects the objects file that are to be put in the response
101 -- file. Only application objects are collected there (see details in
102 -- Linker_Objects table comments)
104 package Binder_Options_From_ALI is new Table.Table (
105 Table_Component_Type => String_Access,
106 Table_Index_Type => Integer,
107 Table_Low_Bound => 1, -- equals low bound of Argument_List for Spawn
108 Table_Initial => 20,
109 Table_Increment => 100,
110 Table_Name => "Gnatlink.Binder_Options_From_ALI");
111 -- This table collects the switches from the ALI file of the main
112 -- subprogram.
114 package Binder_Options is new Table.Table (
115 Table_Component_Type => String_Access,
116 Table_Index_Type => Integer,
117 Table_Low_Bound => 1, -- equals low bound of Argument_List for Spawn
118 Table_Initial => 20,
119 Table_Increment => 100,
120 Table_Name => "Gnatlink.Binder_Options");
121 -- This table collects the arguments to be passed to compile the binder
122 -- generated file.
124 subtype chars_ptr is System.Address;
126 Gcc : String_Access := Program_Name ("gcc");
128 Read_Mode : constant String := "r" & ASCII.Nul;
130 Begin_Info : String := "-- BEGIN Object file/option list";
131 End_Info : String := "-- END Object file/option list ";
132 -- Note: above lines are modified in C mode, see option processing
134 Gcc_Path : String_Access;
135 Linker_Path : String_Access;
137 Output_File_Name : String_Access;
138 Ali_File_Name : String_Access;
139 Binder_Spec_Src_File : String_Access;
140 Binder_Body_Src_File : String_Access;
141 Binder_Ali_File : String_Access;
142 Binder_Obj_File : String_Access;
144 Tname : Temp_File_Name;
145 Tname_FD : File_Descriptor := Invalid_FD;
146 -- Temporary file used by linker to pass list of object files on
147 -- certain systems with limitations on size of arguments.
149 Debug_Flag_Present : Boolean := False;
150 Verbose_Mode : Boolean := False;
151 Very_Verbose_Mode : Boolean := False;
153 Ada_Bind_File : Boolean := True;
154 -- Set to True if bind file is generated in Ada
156 Standard_Gcc : Boolean := True;
158 Compile_Bind_File : Boolean := True;
159 -- Set to False if bind file is not to be compiled
161 Object_List_File_Supported : Boolean;
162 pragma Import (C, Object_List_File_Supported, "objlist_file_supported");
163 -- Predicate indicating whether the linker has an option whereby the
164 -- names of object files can be passed to the linker in a file.
166 Object_List_File_Required : Boolean := False;
167 -- Set to True to force generation of a response file
169 function Base_Name (File_Name : in String) return String;
170 -- Return just the file name part without the extension (if present).
172 procedure Delete (Name : in String);
173 -- Wrapper to unlink as status is ignored by this application.
175 procedure Error_Msg (Message : in String);
176 -- Output the error or warning Message
178 procedure Exit_With_Error (Error : in String);
179 -- Output Error and exit program with a fatal condition.
181 procedure Process_Args;
182 -- Go through all the arguments and build option tables.
184 procedure Process_Binder_File (Name : in String);
185 -- Reads the binder file and extracts linker arguments.
187 function Value (chars : chars_ptr) return String;
188 -- Return NUL-terminated string chars as an Ada string.
190 procedure Write_Usage;
191 -- Show user the program options.
193 ---------------
194 -- Base_Name --
195 ---------------
197 function Base_Name (File_Name : in String) return String is
198 Findex1 : Natural;
199 Findex2 : Natural;
201 begin
202 Findex1 := File_Name'First;
204 -- The file might be specified by a full path name. However,
205 -- we want the path to be stripped away.
207 for J in reverse File_Name'Range loop
208 if Is_Directory_Separator (File_Name (J)) then
209 Findex1 := J + 1;
210 exit;
211 end if;
212 end loop;
214 Findex2 := File_Name'Last;
215 while Findex2 > Findex1
216 and then File_Name (Findex2) /= '.'
217 loop
218 Findex2 := Findex2 - 1;
219 end loop;
221 if Findex2 = Findex1 then
222 Findex2 := File_Name'Last + 1;
223 end if;
225 return File_Name (Findex1 .. Findex2 - 1);
226 end Base_Name;
228 ------------
229 -- Delete --
230 ------------
232 procedure Delete (Name : in String) is
233 Status : int;
235 begin
236 Status := unlink (Name'Address);
237 end Delete;
239 ---------------
240 -- Error_Msg --
241 ---------------
243 procedure Error_Msg (Message : in String) is
244 begin
245 Write_Str (Base_Name (Command_Name));
246 Write_Str (": ");
247 Write_Str (Message);
248 Write_Eol;
249 end Error_Msg;
251 ---------------------
252 -- Exit_With_Error --
253 ---------------------
255 procedure Exit_With_Error (Error : in String) is
256 begin
257 Error_Msg (Error);
258 Exit_Program (E_Fatal);
259 end Exit_With_Error;
261 ------------------
262 -- Process_Args --
263 ------------------
265 procedure Process_Args is
266 Next_Arg : Integer;
268 begin
269 -- Loop through arguments of gnatlink command
271 Next_Arg := 1;
272 loop
273 exit when Next_Arg > Argument_Count;
275 Process_One_Arg : declare
276 Arg : String := Argument (Next_Arg);
278 begin
279 -- Case of argument which is a switch
281 -- We definitely need section by section comments here ???
283 if Arg'Length /= 0 and then Arg (1) = '-' then
284 if Arg'Length > 4
285 and then Arg (2 .. 5) = "gnat"
286 then
287 Exit_With_Error
288 ("invalid switch: """ & Arg & """ (gnat not needed here)");
289 end if;
291 if Arg (2) = 'g'
292 and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat")
293 then
294 Debug_Flag_Present := True;
296 Linker_Options.Increment_Last;
297 Linker_Options.Table (Linker_Options.Last) :=
298 new String'(Arg);
300 Binder_Options.Increment_Last;
301 Binder_Options.Table (Binder_Options.Last) :=
302 Linker_Options.Table (Linker_Options.Last);
304 elsif Arg'Length = 2 then
305 case Arg (2) is
306 when 'A' =>
307 Ada_Bind_File := True;
308 Begin_Info := "-- BEGIN Object file/option list";
309 End_Info := "-- END Object file/option list ";
311 when 'b' =>
312 Linker_Options.Increment_Last;
313 Linker_Options.Table (Linker_Options.Last) :=
314 new String'(Arg);
316 Binder_Options.Increment_Last;
317 Binder_Options.Table (Binder_Options.Last) :=
318 Linker_Options.Table (Linker_Options.Last);
320 Next_Arg := Next_Arg + 1;
322 if Next_Arg > Argument_Count then
323 Exit_With_Error ("Missing argument for -b");
324 end if;
326 Get_Machine_Name : declare
327 Name_Arg : String_Access :=
328 new String'(Argument (Next_Arg));
330 begin
331 Linker_Options.Increment_Last;
332 Linker_Options.Table (Linker_Options.Last) :=
333 Name_Arg;
335 Binder_Options.Increment_Last;
336 Binder_Options.Table (Binder_Options.Last) :=
337 Name_Arg;
339 end Get_Machine_Name;
341 when 'C' =>
342 Ada_Bind_File := False;
343 Begin_Info := "/* BEGIN Object file/option list";
344 End_Info := " END Object file/option list */";
346 when 'f' =>
347 if Object_List_File_Supported then
348 Object_List_File_Required := True;
349 else
350 Exit_With_Error
351 ("Object list file not supported on this target");
352 end if;
354 when 'n' =>
355 Compile_Bind_File := False;
357 when 'o' =>
358 Linker_Options.Increment_Last;
359 Linker_Options.Table (Linker_Options.Last) :=
360 new String'(Arg);
362 Next_Arg := Next_Arg + 1;
364 if Next_Arg > Argument_Count then
365 Exit_With_Error ("Missing argument for -o");
366 end if;
368 Output_File_Name := new String'(Argument (Next_Arg));
370 Linker_Options.Increment_Last;
371 Linker_Options.Table (Linker_Options.Last) :=
372 Output_File_Name;
374 when 'v' =>
376 -- Support "double" verbose mode. Second -v
377 -- gets sent to the linker and binder phases.
379 if Verbose_Mode then
380 Very_Verbose_Mode := True;
382 Linker_Options.Increment_Last;
383 Linker_Options.Table (Linker_Options.Last) :=
384 new String'(Arg);
386 Binder_Options.Increment_Last;
387 Binder_Options.Table (Binder_Options.Last) :=
388 Linker_Options.Table (Linker_Options.Last);
390 else
391 Verbose_Mode := True;
393 end if;
395 when others =>
396 Linker_Options.Increment_Last;
397 Linker_Options.Table (Linker_Options.Last) :=
398 new String'(Arg);
400 end case;
402 elsif Arg (2) = 'B' then
403 Linker_Options.Increment_Last;
404 Linker_Options.Table (Linker_Options.Last) :=
405 new String'(Arg);
407 Binder_Options.Increment_Last;
408 Binder_Options.Table (Binder_Options.Last) :=
409 Linker_Options.Table (Linker_Options.Last);
411 elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then
413 if Arg'Length = 7 then
414 Exit_With_Error ("Missing argument for --LINK=");
415 end if;
417 Linker_Path :=
418 GNAT.OS_Lib.Locate_Exec_On_Path (Arg (8 .. Arg'Last));
420 if Linker_Path = null then
421 Exit_With_Error
422 ("Could not locate linker: " & Arg (8 .. Arg'Last));
423 end if;
425 elsif Arg'Length > 6 and then Arg (1 .. 6) = "--GCC=" then
426 declare
427 Program_Args : Argument_List_Access :=
428 Argument_String_To_List
429 (Arg (7 .. Arg'Last));
431 begin
432 Gcc := new String'(Program_Args.all (1).all);
433 Standard_Gcc := False;
435 -- Set appropriate flags for switches passed
437 for J in 2 .. Program_Args.all'Last loop
438 declare
439 Arg : String := Program_Args.all (J).all;
440 AF : Integer := Arg'First;
442 begin
443 if Arg'Length /= 0 and then Arg (AF) = '-' then
444 if Arg (AF + 1) = 'g'
445 and then (Arg'Length = 2
446 or else Arg (AF + 2) in '0' .. '3'
447 or else Arg (AF + 2 .. Arg'Last) = "coff")
448 then
449 Debug_Flag_Present := True;
450 end if;
451 end if;
453 -- Pass to gcc for compiling binder generated file
454 -- No use passing libraries, it will just generate
455 -- a warning
457 if not (Arg (AF .. AF + 1) = "-l"
458 or else Arg (AF .. AF + 1) = "-L")
459 then
460 Binder_Options.Increment_Last;
461 Binder_Options.Table (Binder_Options.Last) :=
462 new String'(Arg);
463 end if;
465 -- Pass to gcc for linking program.
467 Gcc_Linker_Options.Increment_Last;
468 Gcc_Linker_Options.Table
469 (Gcc_Linker_Options.Last) := new String'(Arg);
470 end;
471 end loop;
472 end;
474 -- Send all multi-character switches not recognized as
475 -- a special case by gnatlink to the linker/loader stage.
477 else
478 Linker_Options.Increment_Last;
479 Linker_Options.Table (Linker_Options.Last) :=
480 new String'(Arg);
481 end if;
483 -- Here if argument is a file name rather than a switch
485 else
486 if Arg'Length > 4
487 and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali"
488 then
489 if Ali_File_Name = null then
490 Ali_File_Name := new String'(Arg);
491 else
492 Exit_With_Error ("cannot handle more than one ALI file");
493 end if;
495 elsif Is_Regular_File (Arg & ".ali")
496 and then Ali_File_Name = null
497 then
498 Ali_File_Name := new String'(Arg & ".ali");
500 elsif Arg'Length > Get_Object_Suffix.all'Length
501 and then Arg
502 (Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last)
503 = Get_Object_Suffix.all
504 then
505 Linker_Objects.Increment_Last;
506 Linker_Objects.Table (Linker_Objects.Last) :=
507 new String'(Arg);
509 else
510 Linker_Options.Increment_Last;
511 Linker_Options.Table (Linker_Options.Last) :=
512 new String'(Arg);
513 end if;
515 end if;
517 end Process_One_Arg;
519 Next_Arg := Next_Arg + 1;
520 end loop;
522 -- If Ada bind file, then compile it with warnings suppressed, because
523 -- otherwise the with of the main program may cause junk warnings.
525 if Ada_Bind_File then
526 Binder_Options.Increment_Last;
527 Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws");
528 end if;
529 end Process_Args;
531 -------------------------
532 -- Process_Binder_File --
533 -------------------------
535 procedure Process_Binder_File (Name : in String) is
536 Fd : FILEs;
537 Link_Bytes : Integer := 0;
538 Link_Max : Integer;
539 pragma Import (C, Link_Max, "link_max");
541 Next_Line : String (1 .. 1000);
542 Nlast : Integer;
543 Nfirst : Integer;
544 Objs_Begin : Integer := 0;
545 Objs_End : Integer := 0;
547 Status : int;
548 N : Integer;
550 GNAT_Static : Boolean := False;
551 -- Save state of -static option.
553 GNAT_Shared : Boolean := False;
554 -- Save state of -shared option.
556 Run_Path_Option_Ptr : Address;
557 pragma Import (C, Run_Path_Option_Ptr, "run_path_option");
558 -- Pointer to string representing the native linker option which
559 -- specifies the path where the dynamic loader should find shared
560 -- libraries. Equal to null string if this system doesn't support it.
562 Object_Library_Ext_Ptr : Address;
563 pragma Import (C, Object_Library_Ext_Ptr, "object_library_extension");
564 -- Pointer to string specifying the default extension for
565 -- object libraries, e.g. Unix uses ".a", VMS uses ".olb".
567 Object_File_Option_Ptr : Address;
568 pragma Import (C, Object_File_Option_Ptr, "object_file_option");
569 -- Pointer to a string representing the linker option which specifies
570 -- the response file.
572 Using_GNU_Linker : Boolean;
573 pragma Import (C, Using_GNU_Linker, "using_gnu_linker");
574 -- Predicate indicating whether this target uses the GNU linker. In
575 -- this case we must output a GNU linker compatible response file.
577 procedure Get_Next_Line;
578 -- Read the next line from the binder file without the line
579 -- terminator.
581 function Is_Option_Present (Opt : in String) return Boolean;
582 -- Return true if the option Opt is already present in
583 -- Linker_Options table.
585 procedure Get_Next_Line is
586 Fchars : chars;
588 begin
589 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
591 if Fchars = System.Null_Address then
592 Exit_With_Error ("Error reading binder output");
593 end if;
595 Nfirst := Next_Line'First;
596 Nlast := Nfirst;
597 while Nlast <= Next_Line'Last
598 and then Next_Line (Nlast) /= ASCII.LF
599 and then Next_Line (Nlast) /= ASCII.CR
600 loop
601 Nlast := Nlast + 1;
602 end loop;
604 Nlast := Nlast - 1;
605 end Get_Next_Line;
607 function Is_Option_Present (Opt : in String) return Boolean is
608 begin
609 for I in 1 .. Linker_Options.Last loop
611 if Linker_Options.Table (I).all = Opt then
612 return True;
613 end if;
615 end loop;
617 return False;
618 end Is_Option_Present;
620 -- Start of processing for Process_Binder_File
622 begin
623 Fd := fopen (Name'Address, Read_Mode'Address);
625 if Fd = NULL_Stream then
626 Exit_With_Error ("Failed to open binder output");
627 end if;
629 -- Skip up to the Begin Info line
631 loop
632 Get_Next_Line;
633 exit when Next_Line (Nfirst .. Nlast) = Begin_Info;
634 end loop;
636 loop
637 Get_Next_Line;
639 -- Go to end when end line is reached (this will happen in
640 -- No_Run_Time mode where no -L switches are generated)
642 exit when Next_Line (Nfirst .. Nlast) = End_Info;
644 if Ada_Bind_File then
645 Next_Line (Nfirst .. Nlast - 8) :=
646 Next_Line (Nfirst + 8 .. Nlast);
647 Nlast := Nlast - 8;
648 end if;
650 -- Go to next section when switches are reached
652 exit when Next_Line (1) = '-';
654 -- Otherwise we have another object file to collect
656 Linker_Objects.Increment_Last;
658 -- Mark the positions of first and last object files in case
659 -- they need to be placed with a named file on systems having
660 -- linker line limitations.
662 if Objs_Begin = 0 then
663 Objs_Begin := Linker_Objects.Last;
664 end if;
666 Linker_Objects.Table (Linker_Objects.Last) :=
667 new String'(Next_Line (Nfirst .. Nlast));
669 Link_Bytes := Link_Bytes + Nlast - Nfirst;
670 end loop;
672 Objs_End := Linker_Objects.Last;
674 -- On systems that have limitations on handling very long linker lines
675 -- we make use of the system linker option which takes a list of object
676 -- file names from a file instead of the command line itself. What we do
677 -- is to replace the list of object files by the special linker option
678 -- which then reads the object file list from a file instead. The option
679 -- to read from a file instead of the command line is only triggered if
680 -- a conservative threshold is passed.
682 if Object_List_File_Required
683 or else (Object_List_File_Supported
684 and then Link_Bytes > Link_Max)
685 then
686 -- Create a temporary file containing the Ada user object files
687 -- needed by the link. This list is taken from the bind file
688 -- and is output one object per line for maximal compatibility with
689 -- linkers supporting this option.
691 Create_Temp_File (Tname_FD, Tname);
693 -- If target is using the GNU linker we must add a special header
694 -- and footer in the response file.
695 -- The syntax is : INPUT (object1.o object2.o ... )
697 if Using_GNU_Linker then
698 declare
699 GNU_Header : aliased constant String := "INPUT (";
701 begin
702 Status := Write (Tname_FD, GNU_Header'Address,
703 GNU_Header'Length);
704 end;
705 end if;
707 for J in Objs_Begin .. Objs_End loop
708 Status := Write (Tname_FD, Linker_Objects.Table (J).all'Address,
709 Linker_Objects.Table (J).all'Length);
710 Status := Write (Tname_FD, ASCII.LF'Address, 1);
712 Response_File_Objects.Increment_Last;
713 Response_File_Objects.Table (Response_File_Objects.Last) :=
714 Linker_Objects.Table (J);
715 end loop;
717 -- handle GNU linker response file footer.
719 if Using_GNU_Linker then
720 declare
721 GNU_Footer : aliased constant String := ")";
723 begin
724 Status := Write (Tname_FD, GNU_Footer'Address,
725 GNU_Footer'Length);
726 end;
727 end if;
729 Close (Tname_FD);
731 -- Add the special objects list file option together with the name
732 -- of the temporary file (removing the null character) to the objects
733 -- file table.
735 Linker_Objects.Table (Objs_Begin) :=
736 new String'(Value (Object_File_Option_Ptr) &
737 Tname (Tname'First .. Tname'Last - 1));
739 -- The slots containing these object file names are then removed
740 -- from the objects table so they do not appear in the link. They
741 -- are removed by moving up the linker options and non-Ada object
742 -- files appearing after the Ada object list in the table.
744 N := Objs_End - Objs_Begin + 1;
745 for J in Objs_End + 1 .. Linker_Objects.Last loop
746 Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J);
747 end loop;
749 Linker_Objects.Set_Last (Linker_Objects.Last - N + 1);
750 end if;
752 -- Process switches and options
754 if Next_Line (Nfirst .. Nlast) /= End_Info then
755 loop
756 if Next_Line (Nfirst .. Nlast) = "-static" then
757 GNAT_Static := True;
759 elsif Next_Line (Nfirst .. Nlast) = "-shared" then
760 GNAT_Shared := True;
762 -- Add binder options only if not already set on the command
763 -- line. This rule is a way to control the linker options order.
765 elsif not Is_Option_Present (Next_Line (Nfirst .. Nlast)) then
766 if Nlast > Nfirst + 2 and then
767 Next_Line (Nfirst .. Nfirst + 1) = "-L"
768 then
769 -- Construct a library search path for use later
770 -- to locate static gnatlib libraries.
772 if Libpath.Last > 1 then
773 Libpath.Increment_Last;
774 Libpath.Table (Libpath.Last) := Path_Separator;
775 end if;
777 for I in Nfirst + 2 .. Nlast loop
778 Libpath.Increment_Last;
779 Libpath.Table (Libpath.Last) := Next_Line (I);
780 end loop;
782 Linker_Options.Increment_Last;
784 Linker_Options.Table (Linker_Options.Last) :=
785 new String'(Next_Line (Nfirst .. Nlast));
787 elsif Next_Line (Nfirst .. Nlast) = "-ldecgnat"
788 or else Next_Line (Nfirst .. Nlast) = "-lgnarl"
789 or else Next_Line (Nfirst .. Nlast) = "-lgnat"
790 then
791 -- Given a Gnat standard library, search the
792 -- library path to find the library location
794 declare
795 File_Path : String_Access;
796 Object_Lib_Extension : constant String :=
797 Value (Object_Library_Ext_Ptr);
799 File_Name : String := "lib" &
800 Next_Line (Nfirst + 2 .. Nlast) & Object_Lib_Extension;
802 begin
803 File_Path :=
804 Locate_Regular_File (File_Name,
805 String (Libpath.Table (1 .. Libpath.Last)));
807 if File_Path /= null then
808 if GNAT_Static then
810 -- If static gnatlib found, explicitly
811 -- specify to overcome possible linker
812 -- default usage of shared version.
814 Linker_Options.Increment_Last;
816 Linker_Options.Table (Linker_Options.Last) :=
817 new String'(File_Path.all);
819 elsif GNAT_Shared then
821 -- If shared gnatlib desired, add the
822 -- appropriate system specific switch
823 -- so that it can be located at runtime.
825 declare
826 Run_Path_Opt : constant String :=
827 Value (Run_Path_Option_Ptr);
829 begin
830 if Run_Path_Opt'Length /= 0 then
832 -- Output the system specific linker
833 -- command that allows the image
834 -- activator to find the shared library
835 -- at runtime.
837 Linker_Options.Increment_Last;
839 Linker_Options.Table (Linker_Options.Last)
840 := new String'(Run_Path_Opt
841 & File_Path
842 (1 .. File_Path'Length
843 - File_Name'Length));
844 end if;
846 Linker_Options.Increment_Last;
847 Linker_Options.Table (Linker_Options.Last)
848 := new String'(Next_Line (Nfirst .. Nlast));
849 end;
850 end if;
852 else
853 -- If gnatlib library not found, then
854 -- add it anyway in case some other
855 -- mechanimsm may find it.
857 Linker_Options.Increment_Last;
858 Linker_Options.Table (Linker_Options.Last)
859 := new String'(Next_Line (Nfirst .. Nlast));
860 end if;
861 end;
862 else
863 Linker_Options.Increment_Last;
864 Linker_Options.Table (Linker_Options.Last)
865 := new String'(Next_Line (Nfirst .. Nlast));
866 end if;
867 end if;
869 Get_Next_Line;
870 exit when Next_Line (Nfirst .. Nlast) = End_Info;
872 if Ada_Bind_File then
873 Next_Line (Nfirst .. Nlast - 8)
874 := Next_Line (Nfirst + 8 .. Nlast);
875 Nlast := Nlast - 8;
876 end if;
877 end loop;
878 end if;
880 Status := fclose (Fd);
881 end Process_Binder_File;
883 -----------
884 -- Value --
885 -----------
887 function Value (chars : chars_ptr) return String is
888 function Strlen (chars : chars_ptr) return Natural;
889 pragma Import (C, Strlen);
891 begin
892 if chars = Null_Address then
893 return "";
895 else
896 declare
897 subtype Result_Type is String (1 .. Strlen (chars));
899 Result : Result_Type;
900 for Result'Address use chars;
902 begin
903 return Result;
904 end;
905 end if;
906 end Value;
908 -----------------
909 -- Write_Usage --
910 -----------------
912 procedure Write_Usage is
913 begin
914 Write_Str ("Usage: ");
915 Write_Str (Base_Name (Command_Name));
916 Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]");
917 Write_Eol;
918 Write_Eol;
919 Write_Line (" mainprog.ali the ALI file of the main program");
920 Write_Eol;
921 Write_Line (" -A Binder generated source file is in Ada (default)");
922 Write_Line (" -C Binder generated source file is in C");
923 Write_Line (" -f force object file list to be generated");
924 Write_Line (" -g Compile binder source file with debug information");
925 Write_Line (" -n Do not compile the binder source file");
926 Write_Line (" -v verbose mode");
927 Write_Line (" -v -v very verbose mode");
928 Write_Eol;
929 Write_Line (" -o nam Use 'nam' as the name of the executable");
930 Write_Line (" -b target Compile the binder source to run on target");
931 Write_Line (" -Bdir Load compiler executables from dir");
932 Write_Line (" --GCC=comp Use comp as the compiler");
933 Write_Line (" --LINK=nam Use 'nam' for the linking rather than 'gcc'");
934 Write_Eol;
935 Write_Line (" [non-Ada-objects] list of non Ada object files");
936 Write_Line (" [linker-options] other options for the linker");
937 end Write_Usage;
939 -- Start of processing for Gnatlink
941 begin
942 if Argument_Count = 0 then
943 Write_Usage;
944 Exit_Program (E_Fatal);
945 end if;
947 if Hostparm.Java_VM then
948 Gcc := new String'("jgnat");
949 Ada_Bind_File := True;
950 Begin_Info := "-- BEGIN Object file/option list";
951 End_Info := "-- END Object file/option list ";
952 end if;
954 Process_Args;
956 -- We always compile with -c
958 Binder_Options_From_ALI.Increment_Last;
959 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
960 new String'("-c");
962 -- If the main program is in Ada it is compiled with the following
963 -- switches:
965 -- -gnatA stops reading gnat.adc, since we don't know what
966 -- pagmas would work, and we do not need it anyway.
968 -- -gnatWb allows brackets coding for wide characters
970 -- -gnatiw allows wide characters in identifiers. This is needed
971 -- because bindgen uses brackets encoding for all upper
972 -- half and wide characters in identifier names.
974 if Ada_Bind_File then
975 Binder_Options_From_ALI.Increment_Last;
976 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
977 new String'("-gnatA");
978 Binder_Options_From_ALI.Increment_Last;
979 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
980 new String'("-gnatWb");
981 Binder_Options_From_ALI.Increment_Last;
982 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) :=
983 new String'("-gnatiw");
984 end if;
986 -- Locate all the necessary programs and verify required files are present
988 Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
990 if Gcc_Path = null then
991 Exit_With_Error ("Couldn't locate " & Gcc.all);
992 end if;
994 if Linker_Path = null then
995 Linker_Path := Gcc_Path;
996 end if;
998 if Ali_File_Name = null then
999 Exit_With_Error ("Required 'name'.ali not present.");
1000 end if;
1002 if not Is_Regular_File (Ali_File_Name.all) then
1003 Exit_With_Error (Ali_File_Name.all & " not found.");
1005 -- Read the ALI file of the main subprogram if the binder generated
1006 -- file is in Ada, it need to be compiled and no --GCC= switch has
1007 -- been specified. Fetch the back end switches from this ALI file and use
1008 -- these switches to compile the binder generated file
1010 elsif Ada_Bind_File
1011 and then Compile_Bind_File
1012 and then Standard_Gcc
1013 then
1014 -- Do some initializations
1016 Initialize_ALI;
1017 Namet.Initialize;
1018 Name_Len := Ali_File_Name'Length;
1019 Name_Buffer (1 .. Name_Len) := Ali_File_Name.all;
1021 declare
1022 use Types;
1023 F : constant File_Name_Type := Name_Find;
1024 T : Text_Buffer_Ptr;
1025 A : ALI_Id;
1027 begin
1028 -- Osint.Add_Default_Search_Dirs;
1029 -- Load the ALI file
1031 T := Read_Library_Info (F, True);
1033 -- Read it
1035 A := Scan_ALI (F, T, False, False, False);
1037 if A /= No_ALI_Id then
1039 Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg
1040 .. Units.Table (ALIs.Table (A).First_Unit).Last_Arg
1041 loop
1042 -- Do not compile with the front end switches
1044 if not Is_Front_End_Switch (Args.Table (Index).all) then
1045 Binder_Options_From_ALI.Increment_Last;
1046 Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last)
1047 := String_Access (Args.Table (Index));
1048 end if;
1049 end loop;
1050 end if;
1051 end;
1052 end if;
1054 if Verbose_Mode then
1055 Write_Eol;
1056 Write_Str ("GNATLINK ");
1057 Write_Str (Gnat_Version_String);
1058 Write_Str (" Copyright 1996-2002 Free Software Foundation, Inc.");
1059 Write_Eol;
1060 end if;
1062 -- If there wasn't an output specified, then use the base name of
1063 -- the .ali file name.
1065 if Output_File_Name = null then
1067 Output_File_Name :=
1068 new String'(Base_Name (Ali_File_Name.all)
1069 & Get_Debuggable_Suffix.all);
1071 Linker_Options.Increment_Last;
1072 Linker_Options.Table (Linker_Options.Last) :=
1073 new String'("-o");
1075 Linker_Options.Increment_Last;
1076 Linker_Options.Table (Linker_Options.Last) :=
1077 new String'(Output_File_Name.all);
1079 end if;
1081 -- Warn if main program is called "test", as that may be a built-in command
1082 -- on Unix. On non-Unix systems executables have a suffix, so the warning
1083 -- will not appear. However, do not warn in the case of a cross compiler.
1085 -- Assume that if the executable name is not gnatlink, this is a cross
1086 -- tool.
1088 if Base_Name (Command_Name) = "gnatlink"
1089 and then Output_File_Name.all = "test"
1090 then
1091 Error_Msg ("warning: executable name """ & Output_File_Name.all
1092 & """ may conflict with shell command");
1093 end if;
1095 -- Perform consistency checks
1097 -- Transform the .ali file name into the binder output file name.
1099 Make_Binder_File_Names : declare
1100 Fname : String := Base_Name (Ali_File_Name.all);
1101 Fname_Len : Integer := Fname'Length;
1103 function Get_Maximum_File_Name_Length return Integer;
1104 pragma Import (C, Get_Maximum_File_Name_Length,
1105 "__gnat_get_maximum_file_name_length");
1107 Maximum_File_Name_Length : Integer := Get_Maximum_File_Name_Length;
1109 Second_Char : Character;
1110 -- Second character of name of files
1112 begin
1113 -- Set proper second character of file name
1115 if not Ada_Bind_File then
1116 Second_Char := '_';
1118 elsif Hostparm.OpenVMS then
1119 Second_Char := '$';
1121 else
1122 Second_Char := '~';
1123 end if;
1125 -- If the length of the binder file becomes too long due to
1126 -- the addition of the "b?" prefix, then truncate it.
1128 if Maximum_File_Name_Length > 0 then
1129 while Fname_Len > Maximum_File_Name_Length - 2 loop
1130 Fname_Len := Fname_Len - 1;
1131 end loop;
1132 end if;
1134 if Ada_Bind_File then
1135 Binder_Spec_Src_File :=
1136 new String'('b'
1137 & Second_Char
1138 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1139 & ".ads");
1140 Binder_Body_Src_File :=
1141 new String'('b'
1142 & Second_Char
1143 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1144 & ".adb");
1145 Binder_Ali_File :=
1146 new String'('b'
1147 & Second_Char
1148 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1149 & ".ali");
1151 else
1152 Binder_Body_Src_File :=
1153 new String'('b'
1154 & Second_Char
1155 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1156 & ".c");
1157 end if;
1159 Binder_Obj_File :=
1160 new String'('b'
1161 & Second_Char
1162 & Fname (Fname'First .. Fname'First + Fname_Len - 1)
1163 & Get_Object_Suffix.all);
1165 if Fname_Len /= Fname'Length then
1166 Binder_Options.Increment_Last;
1167 Binder_Options.Table (Binder_Options.Last) := new String'("-o");
1168 Binder_Options.Increment_Last;
1169 Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File;
1170 end if;
1172 end Make_Binder_File_Names;
1174 Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL);
1176 -- Compile the binder file. This is fast, so we always do it, unless
1177 -- specifically told not to by the -n switch
1179 if Compile_Bind_File then
1180 Bind_Step : declare
1181 Success : Boolean;
1182 Args : Argument_List
1183 (1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1);
1185 begin
1186 for J in 1 .. Binder_Options_From_ALI.Last loop
1187 Args (J) := Binder_Options_From_ALI.Table (J);
1188 end loop;
1190 for J in 1 .. Binder_Options.Last loop
1191 Args (Binder_Options_From_ALI.Last + J) :=
1192 Binder_Options.Table (J);
1193 end loop;
1195 Args (Args'Last) := Binder_Body_Src_File;
1197 if Verbose_Mode then
1198 Write_Str (Base_Name (Gcc_Path.all));
1200 for J in Args'Range loop
1201 Write_Str (" ");
1202 Write_Str (Args (J).all);
1203 end loop;
1205 Write_Eol;
1206 end if;
1208 GNAT.OS_Lib.Spawn (Gcc_Path.all, Args, Success);
1210 if not Success then
1211 Exit_Program (E_Fatal);
1212 end if;
1213 end Bind_Step;
1214 end if;
1216 -- Now, actually link the program.
1218 -- Skip this step for now on the JVM since the Java interpreter will do
1219 -- the actual link at run time. We might consider packing all class files
1220 -- in a .zip file during this step.
1222 if not Hostparm.Java_VM then
1223 Link_Step : declare
1224 Num_Args : Natural :=
1225 (Linker_Options.Last - Linker_Options.First + 1) +
1226 (Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) +
1227 (Linker_Objects.Last - Linker_Objects.First + 1);
1228 Stack_Op : Boolean := False;
1229 IDENT_Op : Boolean := False;
1231 begin
1232 -- Remove duplicate stack size setting from the Linker_Options
1233 -- table. The stack setting option "-Xlinker --stack=R,C" can be
1234 -- found in one line when set by a pragma Linker_Options or in two
1235 -- lines ("-Xlinker" then "--stack=R,C") when set on the command
1236 -- line. We also check for the "-Wl,--stack=R" style option.
1238 -- We must remove the second stack setting option instance
1239 -- because the one on the command line will always be the first
1240 -- one. And any subsequent stack setting option will overwrite the
1241 -- previous one. This is done especially for GNAT/NT where we set
1242 -- the stack size for tasking programs by a pragma in the NT
1243 -- specific tasking package System.Task_Primitives.Oparations.
1245 for J in Linker_Options.First .. Linker_Options.Last loop
1246 if Linker_Options.Table (J).all = "-Xlinker"
1247 and then J < Linker_Options.Last
1248 and then Linker_Options.Table (J + 1)'Length > 8
1249 and then Linker_Options.Table (J + 1) (1 .. 8) = "--stack="
1250 then
1251 if Stack_Op then
1252 Linker_Options.Table (J .. Linker_Options.Last - 2) :=
1253 Linker_Options.Table (J + 2 .. Linker_Options.Last);
1254 Linker_Options.Decrement_Last;
1255 Linker_Options.Decrement_Last;
1256 Num_Args := Num_Args - 2;
1258 else
1259 Stack_Op := True;
1260 end if;
1261 end if;
1263 -- Here we just check for a canonical form that matches the
1264 -- pragma Linker_Options set in the NT runtime.
1266 if (Linker_Options.Table (J)'Length > 17
1267 and then Linker_Options.Table (J) (1 .. 17)
1268 = "-Xlinker --stack=")
1269 or else
1270 (Linker_Options.Table (J)'Length > 12
1271 and then Linker_Options.Table (J) (1 .. 12)
1272 = "-Wl,--stack=")
1273 then
1274 if Stack_Op then
1275 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1276 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1277 Linker_Options.Decrement_Last;
1278 Num_Args := Num_Args - 1;
1280 else
1281 Stack_Op := True;
1282 end if;
1283 end if;
1285 -- Remove duplicate IDENTIFICATION directives (VMS)
1287 if Linker_Options.Table (J)'Length > 27
1288 and then Linker_Options.Table (J) (1 .. 27)
1289 = "--for-linker=IDENTIFICATION="
1290 then
1291 if IDENT_Op then
1292 Linker_Options.Table (J .. Linker_Options.Last - 1) :=
1293 Linker_Options.Table (J + 1 .. Linker_Options.Last);
1294 Linker_Options.Decrement_Last;
1295 Num_Args := Num_Args - 1;
1296 else
1297 IDENT_Op := True;
1298 end if;
1299 end if;
1300 end loop;
1302 -- Prepare arguments for call to linker
1304 Call_Linker : declare
1305 Success : Boolean;
1306 Args : Argument_List (1 .. Num_Args + 1);
1307 Index : Integer := Args'First;
1309 begin
1310 Args (Index) := Binder_Obj_File;
1312 -- Add the object files and any -largs libraries
1314 for J in Linker_Objects.First .. Linker_Objects.Last loop
1315 Index := Index + 1;
1316 Args (Index) := Linker_Objects.Table (J);
1317 end loop;
1319 -- Add the linker options from the binder file
1321 for J in Linker_Options.First .. Linker_Options.Last loop
1322 Index := Index + 1;
1323 Args (Index) := Linker_Options.Table (J);
1324 end loop;
1326 -- Finally add the libraries from the --GCC= switch
1328 for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop
1329 Index := Index + 1;
1330 Args (Index) := Gcc_Linker_Options.Table (J);
1331 end loop;
1333 if Verbose_Mode then
1334 Write_Str (Linker_Path.all);
1336 for J in Args'Range loop
1337 Write_Str (" ");
1338 Write_Str (Args (J).all);
1339 end loop;
1341 Write_Eol;
1343 -- If we are on very verbose mode (-v -v) and a response file
1344 -- is used we display its content.
1346 if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then
1347 Write_Eol;
1348 Write_Str ("Response file (" &
1349 Tname (Tname'First .. Tname'Last - 1) &
1350 ") content : ");
1351 Write_Eol;
1353 for J in
1354 Response_File_Objects.First ..
1355 Response_File_Objects.Last
1356 loop
1357 Write_Str (Response_File_Objects.Table (J).all);
1358 Write_Eol;
1359 end loop;
1361 Write_Eol;
1362 end if;
1363 end if;
1365 GNAT.OS_Lib.Spawn (Linker_Path.all, Args, Success);
1367 -- Delete the temporary file used in conjuction with linking if
1368 -- one was created. See Process_Bind_File for details.
1370 if Tname_FD /= Invalid_FD then
1371 Delete (Tname);
1372 end if;
1374 if not Success then
1375 Error_Msg ("cannot call " & Linker_Path.all);
1376 Exit_Program (E_Fatal);
1377 end if;
1378 end Call_Linker;
1379 end Link_Step;
1380 end if;
1382 -- Only keep the binder output file and it's associated object
1383 -- file if compiling with the -g option. These files are only
1384 -- useful if debugging.
1386 if not Debug_Flag_Present then
1387 if Binder_Ali_File /= null then
1388 Delete (Binder_Ali_File.all & ASCII.NUL);
1389 end if;
1391 if Binder_Spec_Src_File /= null then
1392 Delete (Binder_Spec_Src_File.all & ASCII.NUL);
1393 end if;
1395 Delete (Binder_Body_Src_File.all & ASCII.NUL);
1397 if not Hostparm.Java_VM then
1398 Delete (Binder_Obj_File.all & ASCII.NUL);
1399 end if;
1400 end if;
1402 Exit_Program (E_Success);
1404 exception
1405 when X : others =>
1406 Write_Line (Exception_Information (X));
1407 Exit_With_Error ("INTERNAL ERROR. Please report.");
1408 end Gnatlink;