| 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-2025, 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 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
-- Gnatlink usage: please consult the gnat documentation |
| 27 |
|
| 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; |
| 42 |
|
| 43 |
with Ada.Command_Line; use Ada.Command_Line; |
| 44 |
with Ada.Exceptions; use Ada.Exceptions; |
| 45 |
with Ada.Strings.Fixed; |
| 46 |
|
| 47 |
with System.OS_Lib; use System.OS_Lib; |
| 48 |
with System.CRTL; |
| 49 |
|
| 50 |
with Interfaces.C_Streams; use Interfaces.C_Streams; |
| 51 |
with Interfaces.C.Strings; use Interfaces.C.Strings; |
| 52 |
|
| 53 |
procedure Gnatlink is |
| 54 |
pragma Ident (Gnatvsn.Gnat_Static_Version_String); |
| 55 |
|
| 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 |
| 60 |
|
| 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 |
| 65 |
|
| 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 |
| 74 |
|
| 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 |
| 83 |
|
| 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 |
| 92 |
|
| 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. |
| 108 |
|
| 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) |
| 119 |
|
| 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. |
| 129 |
|
| 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. |
| 139 |
|
| 140 |
Gcc : String_Access := Program_Name ("gcc", "gnatlink"); |
| 141 |
|
| 142 |
Read_Mode : constant String := "r" & ASCII.NUL; |
| 143 |
|
| 144 |
Begin_Info : constant String := "-- BEGIN Object file/option list"; |
| 145 |
End_Info : constant String := "-- END Object file/option list "; |
| 146 |
|
| 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; |
| 155 |
|
| 156 |
Base_Command_Name : String_Access; |
| 157 |
|
| 158 |
Target_Debuggable_Suffix : String_Access; |
| 159 |
|
| 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. |
| 164 |
|
| 165 |
Debug_Flag_Present : Boolean := False; |
| 166 |
Verbose_Mode : Boolean := False; |
| 167 |
Very_Verbose_Mode : Boolean := False; |
| 168 |
|
| 169 |
Standard_Gcc : Boolean := True; |
| 170 |
|
| 171 |
Compile_Bind_File : Boolean := True; |
| 172 |
-- Set to False if bind file is not to be compiled |
| 173 |
|
| 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). |
| 177 |
|
| 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. |
| 184 |
|
| 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. |
| 189 |
|
| 190 |
Object_File_Option : constant String := Value (Object_File_Option_Ptr); |
| 191 |
-- The linker option which specifies the response file as a string |
| 192 |
|
| 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 |
| 197 |
|
| 198 |
Object_List_File_Required : Boolean := False; |
| 199 |
-- Set to True to force generation of a response file |
| 200 |
|
| 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. |
| 207 |
|
| 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. |
| 213 |
|
| 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. |
| 219 |
|
| 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. |
| 224 |
|
| 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". |
| 230 |
|
| 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 |
| 236 |
|
| 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"); |
| 240 |
|
| 241 |
function Base_Name (File_Name : String) return String; |
| 242 |
-- Return just the file name part without the extension (if present) |
| 243 |
|
| 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. |
| 250 |
|
| 251 |
procedure Delete (Name : String); |
| 252 |
-- Wrapper to unlink as status is ignored by this application |
| 253 |
|
| 254 |
procedure Error_Msg (Message : String); |
| 255 |
-- Output the error or warning Message |
| 256 |
|
| 257 |
procedure Exit_With_Error (Error : String); |
| 258 |
-- Output Error and exit program with a fatal condition |
| 259 |
|
| 260 |
procedure Process_Args; |
| 261 |
-- Go through all the arguments and build option tables |
| 262 |
|
| 263 |
procedure Process_Binder_File (Name : String); |
| 264 |
-- Reads the binder file and extracts linker arguments |
| 265 |
|
| 266 |
function Index (S, Pattern : String) return Natural; |
| 267 |
-- Return the last occurrence of Pattern in S, or 0 if none |
| 268 |
|
| 269 |
function Is_Prefix (S, Prefix : String) return Boolean; |
| 270 |
-- Return whether Prefix is a strict prefix of S |
| 271 |
|
| 272 |
procedure Search_Library_Path |
| 273 |
(Next_Line : String; |
| 274 |
Nfirst : Integer; |
| 275 |
Nlast : Integer; |
| 276 |
Last : Integer; |
| 277 |
GNAT_Static : Boolean; |
| 278 |
GNAT_Shared : Boolean); |
| 279 |
-- Given a Gnat standard library, search the library path to find the |
| 280 |
-- library location. Parameters are documented in Process_Binder_File. |
| 281 |
|
| 282 |
procedure Usage; |
| 283 |
-- Display usage |
| 284 |
|
| 285 |
procedure Write_Header; |
| 286 |
-- Show user the program name, version and copyright |
| 287 |
|
| 288 |
procedure Write_Usage; |
| 289 |
-- Show user the program options |
| 290 |
|
| 291 |
--------------- |
| 292 |
-- Base_Name -- |
| 293 |
--------------- |
| 294 |
|
| 295 |
function Base_Name (File_Name : String) return String is |
| 296 |
Findex1 : Natural; |
| 297 |
Findex2 : Natural; |
| 298 |
|
| 299 |
begin |
| 300 |
Findex1 := File_Name'First; |
| 301 |
|
| 302 |
-- The file might be specified by a full path name. However, |
| 303 |
-- we want the path to be stripped away. |
| 304 |
|
| 305 |
for J in reverse File_Name'Range loop |
| 306 |
if Is_Directory_Separator (File_Name (J)) then |
| 307 |
Findex1 := J + 1; |
| 308 |
exit; |
| 309 |
end if; |
| 310 |
end loop; |
| 311 |
|
| 312 |
Findex2 := File_Name'Last; |
| 313 |
while Findex2 > Findex1 and then File_Name (Findex2) /= '.' loop |
| 314 |
Findex2 := Findex2 - 1; |
| 315 |
end loop; |
| 316 |
|
| 317 |
if Findex2 = Findex1 then |
| 318 |
Findex2 := File_Name'Last + 1; |
| 319 |
end if; |
| 320 |
|
| 321 |
return File_Name (Findex1 .. Findex2 - 1); |
| 322 |
end Base_Name; |
| 323 |
|
| 324 |
------------------------------- |
| 325 |
-- Check_Existing_Executable -- |
| 326 |
------------------------------- |
| 327 |
|
| 328 |
procedure Check_Existing_Executable (File_Name : String) is |
| 329 |
Ename : String := File_Name; |
| 330 |
Efile : File_Name_Type; |
| 331 |
Sfile : File_Name_Type; |
| 332 |
|
| 333 |
begin |
| 334 |
Canonical_Case_File_Name (Ename); |
| 335 |
Name_Len := 0; |
| 336 |
Add_Str_To_Name_Buffer (Ename); |
| 337 |
Efile := Name_Find; |
| 338 |
|
| 339 |
for J in Units.Table'First .. Units.Last loop |
| 340 |
Sfile := Units.Table (J).Sfile; |
| 341 |
if Sfile = Efile then |
| 342 |
Exit_With_Error |
| 343 |
("executable name """ & File_Name & """ matches " |
| 344 |
& "source file name """ & Get_Name_String (Sfile) & """"); |
| 345 |
end if; |
| 346 |
end loop; |
| 347 |
|
| 348 |
Delete (File_Name); |
| 349 |
end Check_Existing_Executable; |
| 350 |
|
| 351 |
------------ |
| 352 |
-- Delete -- |
| 353 |
------------ |
| 354 |
|
| 355 |
procedure Delete (Name : String) is |
| 356 |
Status : int; |
| 357 |
pragma Unreferenced (Status); |
| 358 |
begin |
| 359 |
Status := unlink (Name'Address); |
| 360 |
end Delete; |
| 361 |
|
| 362 |
--------------- |
| 363 |
-- Error_Msg -- |
| 364 |
--------------- |
| 365 |
|
| 366 |
procedure Error_Msg (Message : String) is |
| 367 |
begin |
| 368 |
Write_Str (Base_Command_Name.all); |
| 369 |
Write_Str (": "); |
| 370 |
Write_Str (Message); |
| 371 |
Write_Eol; |
| 372 |
end Error_Msg; |
| 373 |
|
| 374 |
--------------------- |
| 375 |
-- Exit_With_Error -- |
| 376 |
--------------------- |
| 377 |
|
| 378 |
procedure Exit_With_Error (Error : String) is |
| 379 |
begin |
| 380 |
Error_Msg (Error); |
| 381 |
Exit_Program (E_Fatal); |
| 382 |
end Exit_With_Error; |
| 383 |
|
| 384 |
----------- |
| 385 |
-- Index -- |
| 386 |
----------- |
| 387 |
|
| 388 |
function Index (S, Pattern : String) return Natural is |
| 389 |
Len : constant Natural := Pattern'Length; |
| 390 |
|
| 391 |
begin |
| 392 |
for J in reverse S'First .. S'Last - Len + 1 loop |
| 393 |
if Pattern = S (J .. J + Len - 1) then |
| 394 |
return J; |
| 395 |
end if; |
| 396 |
end loop; |
| 397 |
|
| 398 |
return 0; |
| 399 |
end Index; |
| 400 |
|
| 401 |
--------------- |
| 402 |
-- Is_Prefix -- |
| 403 |
--------------- |
| 404 |
|
| 405 |
function Is_Prefix (S, Prefix : String) return Boolean is |
| 406 |
begin |
| 407 |
return Prefix'Length < S'Length |
| 408 |
and then S (S'First .. S'First + Prefix'Length - 1) = Prefix; |
| 409 |
end Is_Prefix; |
| 410 |
|
| 411 |
------------------ |
| 412 |
-- Process_Args -- |
| 413 |
------------------ |
| 414 |
|
| 415 |
procedure Process_Args is |
| 416 |
Next_Arg : Integer; |
| 417 |
|
| 418 |
Skip_Next : Boolean := False; |
| 419 |
-- Set to true if the next argument is to be added into the list of |
| 420 |
-- linker's argument without parsing it. |
| 421 |
|
| 422 |
procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage); |
| 423 |
|
| 424 |
-- Start of processing for Process_Args |
| 425 |
|
| 426 |
begin |
| 427 |
-- First, check for --version and --help |
| 428 |
|
| 429 |
Check_Version_And_Help ("GNATLINK", "1996"); |
| 430 |
|
| 431 |
-- Loop through arguments of gnatlink command |
| 432 |
|
| 433 |
Next_Arg := 1; |
| 434 |
loop |
| 435 |
exit when Next_Arg > Argument_Count; |
| 436 |
|
| 437 |
Process_One_Arg : declare |
| 438 |
Arg : constant String := Argument (Next_Arg); |
| 439 |
|
| 440 |
begin |
| 441 |
-- This argument must not be parsed, just add it to the list of |
| 442 |
-- linker's options. |
| 443 |
|
| 444 |
if Skip_Next then |
| 445 |
|
| 446 |
Skip_Next := False; |
| 447 |
|
| 448 |
Linker_Options.Increment_Last; |
| 449 |
Linker_Options.Table (Linker_Options.Last) := |
| 450 |
new String'(Arg); |
| 451 |
|
| 452 |
-- Case of argument which is a switch |
| 453 |
|
| 454 |
elsif Arg'Length /= 0 and then Arg (1) = '-' then |
| 455 |
if Arg'Length > 4 and then Arg (2 .. 5) = "gnat" then |
| 456 |
Exit_With_Error |
| 457 |
("invalid switch: """ & Arg & """ (gnat not needed here)"); |
| 458 |
end if; |
| 459 |
|
| 460 |
if Arg = "-Xlinker" then |
| 461 |
|
| 462 |
-- Next argument should be sent directly to the linker. |
| 463 |
-- We do not want to parse it here. |
| 464 |
|
| 465 |
Skip_Next := True; |
| 466 |
|
| 467 |
Linker_Options.Increment_Last; |
| 468 |
Linker_Options.Table (Linker_Options.Last) := |
| 469 |
new String'(Arg); |
| 470 |
|
| 471 |
elsif Arg (2) = 'g' |
| 472 |
and then (Arg'Length < 5 or else Arg (2 .. 5) /= "gnat") |
| 473 |
then |
| 474 |
Debug_Flag_Present := True; |
| 475 |
|
| 476 |
Linker_Options.Increment_Last; |
| 477 |
Linker_Options.Table (Linker_Options.Last) := |
| 478 |
new String'(Arg); |
| 479 |
|
| 480 |
Binder_Options.Increment_Last; |
| 481 |
Binder_Options.Table (Binder_Options.Last) := |
| 482 |
Linker_Options.Table (Linker_Options.Last); |
| 483 |
|
| 484 |
elsif Arg'Length >= 3 and then Arg (2) = 'M' then |
| 485 |
declare |
| 486 |
Switches : String_List_Access; |
| 487 |
|
| 488 |
begin |
| 489 |
Convert (Map_File, Arg (3 .. Arg'Last), Switches); |
| 490 |
|
| 491 |
if Switches /= null then |
| 492 |
for J in Switches'Range loop |
| 493 |
Linker_Options.Increment_Last; |
| 494 |
Linker_Options.Table (Linker_Options.Last) := |
| 495 |
Switches (J); |
| 496 |
end loop; |
| 497 |
end if; |
| 498 |
end; |
| 499 |
|
| 500 |
elsif Arg'Length = 2 then |
| 501 |
case Arg (2) is |
| 502 |
when 'f' => |
| 503 |
if Object_List_File_Supported then |
| 504 |
Object_List_File_Required := True; |
| 505 |
else |
| 506 |
Exit_With_Error |
| 507 |
("Object list file not supported on this target"); |
| 508 |
end if; |
| 509 |
|
| 510 |
when 'M' => |
| 511 |
Create_Map_File := True; |
| 512 |
|
| 513 |
when 'n' => |
| 514 |
Compile_Bind_File := False; |
| 515 |
|
| 516 |
when 'o' => |
| 517 |
Next_Arg := Next_Arg + 1; |
| 518 |
|
| 519 |
if Next_Arg > Argument_Count then |
| 520 |
Exit_With_Error ("Missing argument for -o"); |
| 521 |
end if; |
| 522 |
|
| 523 |
Output_File_Name := |
| 524 |
new String'(Executable_Name |
| 525 |
(Argument (Next_Arg), |
| 526 |
Only_If_No_Suffix => True)); |
| 527 |
|
| 528 |
when 'P' => |
| 529 |
Opt.CodePeer_Mode := True; |
| 530 |
|
| 531 |
when 'R' => |
| 532 |
Opt.Run_Path_Option := False; |
| 533 |
|
| 534 |
when 'v' => |
| 535 |
|
| 536 |
-- Support "double" verbose mode. Second -v |
| 537 |
-- gets sent to the linker and binder phases. |
| 538 |
|
| 539 |
if Verbose_Mode then |
| 540 |
Very_Verbose_Mode := True; |
| 541 |
|
| 542 |
Linker_Options.Increment_Last; |
| 543 |
Linker_Options.Table (Linker_Options.Last) := |
| 544 |
new String'(Arg); |
| 545 |
|
| 546 |
Binder_Options.Increment_Last; |
| 547 |
Binder_Options.Table (Binder_Options.Last) := |
| 548 |
Linker_Options.Table (Linker_Options.Last); |
| 549 |
|
| 550 |
else |
| 551 |
Verbose_Mode := True; |
| 552 |
|
| 553 |
end if; |
| 554 |
|
| 555 |
when others => |
| 556 |
Linker_Options.Increment_Last; |
| 557 |
Linker_Options.Table (Linker_Options.Last) := |
| 558 |
new String'(Arg); |
| 559 |
|
| 560 |
end case; |
| 561 |
|
| 562 |
elsif Arg (2) = 'B' then |
| 563 |
Linker_Options.Increment_Last; |
| 564 |
Linker_Options.Table (Linker_Options.Last) := |
| 565 |
new String'(Arg); |
| 566 |
|
| 567 |
Binder_Options.Increment_Last; |
| 568 |
Binder_Options.Table (Binder_Options.Last) := |
| 569 |
Linker_Options.Table (Linker_Options.Last); |
| 570 |
|
| 571 |
elsif Arg'Length >= 7 and then Arg (1 .. 7) = "--LINK=" then |
| 572 |
if Arg'Length = 7 then |
| 573 |
Exit_With_Error ("Missing argument for --LINK="); |
| 574 |
end if; |
| 575 |
|
| 576 |
declare |
| 577 |
L_Args : constant Argument_List_Access := |
| 578 |
Argument_String_To_List (Arg (8 .. Arg'Last)); |
| 579 |
begin |
| 580 |
-- The linker program is the first argument |
| 581 |
|
| 582 |
Linker_Path := |
| 583 |
System.OS_Lib.Locate_Exec_On_Path (L_Args.all (1).all); |
| 584 |
|
| 585 |
if Linker_Path = null then |
| 586 |
Exit_With_Error |
| 587 |
("Could not locate linker: " & L_Args.all (1).all); |
| 588 |
end if; |
| 589 |
|
| 590 |
-- The other arguments are passed as-is to the linker and |
| 591 |
-- override those coming from --GCC= if any. |
| 592 |
|
| 593 |
if L_Args.all'Last >= 2 then |
| 594 |
Gcc_Linker_Options.Set_Last (0); |
| 595 |
end if; |
| 596 |
|
| 597 |
for J in 2 .. L_Args.all'Last loop |
| 598 |
Gcc_Linker_Options.Increment_Last; |
| 599 |
Gcc_Linker_Options.Table |
| 600 |
(Gcc_Linker_Options.Last) := |
| 601 |
new String'(L_Args.all (J).all); |
| 602 |
end loop; |
| 603 |
end; |
| 604 |
|
| 605 |
elsif Arg'Length >= 6 and then Arg (1 .. 6) = "--GCC=" then |
| 606 |
if Arg'Length = 6 then |
| 607 |
Exit_With_Error ("Missing argument for --GCC="); |
| 608 |
end if; |
| 609 |
|
| 610 |
declare |
| 611 |
Program_Args : constant Argument_List_Access := |
| 612 |
Argument_String_To_List |
| 613 |
(Arg (7 .. Arg'Last)); |
| 614 |
|
| 615 |
begin |
| 616 |
if Program_Args.all (1).all /= Gcc.all then |
| 617 |
Gcc := new String'(Program_Args.all (1).all); |
| 618 |
Standard_Gcc := False; |
| 619 |
end if; |
| 620 |
|
| 621 |
-- Set appropriate flags for switches passed |
| 622 |
|
| 623 |
for J in 2 .. Program_Args.all'Last loop |
| 624 |
declare |
| 625 |
Arg : constant String := Program_Args.all (J).all; |
| 626 |
AF : constant Integer := Arg'First; |
| 627 |
|
| 628 |
begin |
| 629 |
if Arg'Length /= 0 and then Arg (AF) = '-' then |
| 630 |
if Arg (AF + 1) = 'g' |
| 631 |
and then (Arg'Length = 2 |
| 632 |
or else Arg (AF + 2) in '0' .. '3' |
| 633 |
or else Arg (AF + 2 .. Arg'Last) = "coff") |
| 634 |
then |
| 635 |
Debug_Flag_Present := True; |
| 636 |
end if; |
| 637 |
end if; |
| 638 |
|
| 639 |
-- Add directory to source search dirs so that |
| 640 |
-- Get_Target_Parameters can find system.ads |
| 641 |
|
| 642 |
if Arg (AF .. AF + 1) = "-I" |
| 643 |
and then Arg'Length > 2 |
| 644 |
then |
| 645 |
Add_Src_Search_Dir (Arg (AF + 2 .. Arg'Last)); |
| 646 |
end if; |
| 647 |
|
| 648 |
-- Pass to gcc for compiling binder generated file |
| 649 |
-- No use passing libraries, it will just generate |
| 650 |
-- a warning |
| 651 |
|
| 652 |
if not (Arg (AF .. AF + 1) = "-l" |
| 653 |
or else Arg (AF .. AF + 1) = "-L") |
| 654 |
then |
| 655 |
Binder_Options.Increment_Last; |
| 656 |
Binder_Options.Table (Binder_Options.Last) := |
| 657 |
new String'(Arg); |
| 658 |
end if; |
| 659 |
|
| 660 |
-- Pass to gcc for linking program |
| 661 |
|
| 662 |
Gcc_Linker_Options.Increment_Last; |
| 663 |
Gcc_Linker_Options.Table |
| 664 |
(Gcc_Linker_Options.Last) := new String'(Arg); |
| 665 |
end; |
| 666 |
end loop; |
| 667 |
end; |
| 668 |
|
| 669 |
-- Send all multi-character switches not recognized as |
| 670 |
-- a special case by gnatlink to the linker/loader stage. |
| 671 |
|
| 672 |
else |
| 673 |
Linker_Options.Increment_Last; |
| 674 |
Linker_Options.Table (Linker_Options.Last) := |
| 675 |
new String'(Arg); |
| 676 |
end if; |
| 677 |
|
| 678 |
-- Here if argument is a file name rather than a switch |
| 679 |
|
| 680 |
else |
| 681 |
-- If explicit ali file, capture it |
| 682 |
|
| 683 |
if Arg'Length > 4 |
| 684 |
and then Arg (Arg'Last - 3 .. Arg'Last) = ".ali" |
| 685 |
then |
| 686 |
if Ali_File_Name = null then |
| 687 |
Ali_File_Name := new String'(Arg); |
| 688 |
else |
| 689 |
Exit_With_Error ("cannot handle more than one ALI file"); |
| 690 |
end if; |
| 691 |
|
| 692 |
-- If target object file, record object file |
| 693 |
|
| 694 |
elsif Arg'Length > Get_Target_Object_Suffix.all'Length |
| 695 |
and then Arg |
| 696 |
(Arg'Last - |
| 697 |
Get_Target_Object_Suffix.all'Length + 1 .. Arg'Last) |
| 698 |
= Get_Target_Object_Suffix.all |
| 699 |
then |
| 700 |
Linker_Objects.Increment_Last; |
| 701 |
Linker_Objects.Table (Linker_Objects.Last) := |
| 702 |
new String'(Arg); |
| 703 |
|
| 704 |
-- If host object file, record object file |
| 705 |
|
| 706 |
elsif Arg'Length > Get_Object_Suffix.all'Length |
| 707 |
and then Arg |
| 708 |
(Arg'Last - Get_Object_Suffix.all'Length + 1 .. Arg'Last) |
| 709 |
= Get_Object_Suffix.all |
| 710 |
then |
| 711 |
Linker_Objects.Increment_Last; |
| 712 |
Linker_Objects.Table (Linker_Objects.Last) := |
| 713 |
new String'(Arg); |
| 714 |
|
| 715 |
-- If corresponding ali file exists, capture it |
| 716 |
|
| 717 |
elsif Ali_File_Name = null |
| 718 |
and then Is_Regular_File (Arg & ".ali") |
| 719 |
then |
| 720 |
Ali_File_Name := new String'(Arg & ".ali"); |
| 721 |
|
| 722 |
-- Otherwise assume this is a linker options entry, but |
| 723 |
-- see below for interesting adjustment to this assumption. |
| 724 |
|
| 725 |
else |
| 726 |
Linker_Options.Increment_Last; |
| 727 |
Linker_Options.Table (Linker_Options.Last) := |
| 728 |
new String'(Arg); |
| 729 |
end if; |
| 730 |
end if; |
| 731 |
end Process_One_Arg; |
| 732 |
|
| 733 |
Next_Arg := Next_Arg + 1; |
| 734 |
end loop; |
| 735 |
|
| 736 |
-- Compile the bind file with warnings suppressed, because |
| 737 |
-- otherwise the with of the main program may cause junk warnings. |
| 738 |
|
| 739 |
Binder_Options.Increment_Last; |
| 740 |
Binder_Options.Table (Binder_Options.Last) := new String'("-gnatws"); |
| 741 |
|
| 742 |
-- If we did not get an ali file at all, and we had at least one |
| 743 |
-- linker option, then assume that was the intended ali file after |
| 744 |
-- all, so that we get a nicer message later on. |
| 745 |
|
| 746 |
if Ali_File_Name = null |
| 747 |
and then Linker_Options.Last >= Linker_Options.First |
| 748 |
then |
| 749 |
Ali_File_Name := |
| 750 |
new String'(Linker_Options.Table (Linker_Options.First).all |
| 751 |
& ".ali"); |
| 752 |
end if; |
| 753 |
end Process_Args; |
| 754 |
|
| 755 |
------------------------- |
| 756 |
-- Process_Binder_File -- |
| 757 |
------------------------- |
| 758 |
|
| 759 |
procedure Process_Binder_File (Name : String) is |
| 760 |
Fd : FILEs; |
| 761 |
-- Binder file's descriptor |
| 762 |
|
| 763 |
Link_Bytes : Integer := 0; |
| 764 |
-- Projected number of bytes for the linker command line |
| 765 |
|
| 766 |
Next_Line : String (1 .. 1000); |
| 767 |
-- Current line value |
| 768 |
|
| 769 |
Nlast : Integer; |
| 770 |
Nfirst : Integer; |
| 771 |
-- Current line slice (the slice does not contain line terminator) |
| 772 |
|
| 773 |
Last : Integer; |
| 774 |
-- Current line last character for shared libraries (without version) |
| 775 |
|
| 776 |
Objs_Begin : Integer := 0; |
| 777 |
-- First object file index in Linker_Objects table |
| 778 |
|
| 779 |
Objs_End : Integer := 0; |
| 780 |
-- Last object file index in Linker_Objects table |
| 781 |
|
| 782 |
Status : int; |
| 783 |
pragma Warnings (Off, Status); |
| 784 |
-- Used for various Interfaces.C_Streams calls |
| 785 |
|
| 786 |
Closing_Status : Boolean; |
| 787 |
pragma Warnings (Off, Closing_Status); |
| 788 |
-- For call to Close |
| 789 |
|
| 790 |
GNAT_Static : Boolean := False; |
| 791 |
-- Save state of -static option |
| 792 |
|
| 793 |
GNAT_Shared : Boolean := False; |
| 794 |
-- Save state of -shared option |
| 795 |
|
| 796 |
Xlinker_Was_Previous : Boolean := False; |
| 797 |
-- Indicate that "-Xlinker" was the option preceding the current option. |
| 798 |
-- If True, then the current option is never suppressed. |
| 799 |
|
| 800 |
-- Rollback data |
| 801 |
|
| 802 |
-- These data items are used to store current binder file context. The |
| 803 |
-- context is composed of the file descriptor position and the current |
| 804 |
-- line together with the slice indexes (first and last position) for |
| 805 |
-- this line. The rollback data are used by the Store_File_Context and |
| 806 |
-- Rollback_File_Context routines below. The file context mechanism |
| 807 |
-- interact only with the Get_Next_Line call. For example: |
| 808 |
|
| 809 |
-- Store_File_Context; |
| 810 |
-- Get_Next_Line; |
| 811 |
-- Rollback_File_Context; |
| 812 |
-- Get_Next_Line; |
| 813 |
|
| 814 |
-- Both Get_Next_Line calls above will read the exact same data from |
| 815 |
-- the file. In other words, Next_Line, Nfirst and Nlast variables |
| 816 |
-- will be set with the exact same values. |
| 817 |
|
| 818 |
RB_File_Pos : long; -- File position |
| 819 |
RB_Next_Line : String (1 .. 1000); -- Current line content |
| 820 |
RB_Nlast : Integer; -- Slice last index |
| 821 |
RB_Nfirst : Integer; -- Slice first index |
| 822 |
|
| 823 |
procedure Get_Next_Line; |
| 824 |
-- Read the next line from the binder file without the line |
| 825 |
-- terminator. |
| 826 |
|
| 827 |
procedure Store_File_Context; |
| 828 |
-- Store current file context, Fd position and current line data. |
| 829 |
-- The file context is stored into the rollback data above (RB_*). |
| 830 |
-- Store_File_Context can be called at any time, only the last call |
| 831 |
-- will be used (i.e. this routine overwrites the file context). |
| 832 |
|
| 833 |
procedure Rollback_File_Context; |
| 834 |
-- Restore file context from rollback data. This routine must be called |
| 835 |
-- after Store_File_Context. The binder file context will be restored |
| 836 |
-- with the data stored by the last Store_File_Context call. |
| 837 |
|
| 838 |
procedure Write_RF (S : String); |
| 839 |
-- Write a string to the response file and check if it was successful. |
| 840 |
-- Fail the program if it was not successful (disk full). |
| 841 |
|
| 842 |
------------------- |
| 843 |
-- Get_Next_Line -- |
| 844 |
------------------- |
| 845 |
|
| 846 |
procedure Get_Next_Line is |
| 847 |
Fchars : chars; |
| 848 |
|
| 849 |
begin |
| 850 |
Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd); |
| 851 |
|
| 852 |
if Fchars = System.Null_Address then |
| 853 |
Exit_With_Error ("Error reading binder output"); |
| 854 |
end if; |
| 855 |
|
| 856 |
Nfirst := Next_Line'First; |
| 857 |
Nlast := Nfirst; |
| 858 |
while Nlast <= Next_Line'Last |
| 859 |
and then Next_Line (Nlast) /= ASCII.LF |
| 860 |
and then Next_Line (Nlast) /= ASCII.CR |
| 861 |
loop |
| 862 |
Nlast := Nlast + 1; |
| 863 |
end loop; |
| 864 |
|
| 865 |
Nlast := Nlast - 1; |
| 866 |
end Get_Next_Line; |
| 867 |
|
| 868 |
--------------------------- |
| 869 |
-- Rollback_File_Context -- |
| 870 |
--------------------------- |
| 871 |
|
| 872 |
procedure Rollback_File_Context is |
| 873 |
begin |
| 874 |
Next_Line := RB_Next_Line; |
| 875 |
Nfirst := RB_Nfirst; |
| 876 |
Nlast := RB_Nlast; |
| 877 |
Status := fseek (Fd, RB_File_Pos, Interfaces.C_Streams.SEEK_SET); |
| 878 |
|
| 879 |
if Status = -1 then |
| 880 |
Exit_With_Error ("Error setting file position"); |
| 881 |
end if; |
| 882 |
end Rollback_File_Context; |
| 883 |
|
| 884 |
------------------------ |
| 885 |
-- Store_File_Context -- |
| 886 |
------------------------ |
| 887 |
|
| 888 |
procedure Store_File_Context is |
| 889 |
use type System.CRTL.long; |
| 890 |
|
| 891 |
begin |
| 892 |
RB_Next_Line := Next_Line; |
| 893 |
RB_Nfirst := Nfirst; |
| 894 |
RB_Nlast := Nlast; |
| 895 |
RB_File_Pos := ftell (Fd); |
| 896 |
|
| 897 |
if RB_File_Pos = -1 then |
| 898 |
Exit_With_Error ("Error getting file position"); |
| 899 |
end if; |
| 900 |
end Store_File_Context; |
| 901 |
|
| 902 |
-------------- |
| 903 |
-- Write_RF -- |
| 904 |
-------------- |
| 905 |
|
| 906 |
procedure Write_RF (S : String) is |
| 907 |
Success : Boolean := True; |
| 908 |
Back_Slash : constant Character := '\'; |
| 909 |
|
| 910 |
begin |
| 911 |
-- If a GNU response file is used, space and backslash need to be |
| 912 |
-- escaped because they are interpreted as a string separator and |
| 913 |
-- an escape character respectively by the underlying mechanism. |
| 914 |
-- On the other hand, quote and double-quote are not escaped since |
| 915 |
-- they are interpreted as string delimiters on both sides. |
| 916 |
|
| 917 |
if Using_GNU_response_file then |
| 918 |
for J in S'Range loop |
| 919 |
if S (J) = ' ' or else S (J) = '\' then |
| 920 |
if Write (Tname_FD, Back_Slash'Address, 1) /= 1 then |
| 921 |
Success := False; |
| 922 |
end if; |
| 923 |
end if; |
| 924 |
|
| 925 |
if Write (Tname_FD, S (J)'Address, 1) /= 1 then |
| 926 |
Success := False; |
| 927 |
end if; |
| 928 |
end loop; |
| 929 |
|
| 930 |
else |
| 931 |
if Write (Tname_FD, S'Address, S'Length) /= S'Length then |
| 932 |
Success := False; |
| 933 |
end if; |
| 934 |
end if; |
| 935 |
|
| 936 |
if Write (Tname_FD, ASCII.LF'Address, 1) /= 1 then |
| 937 |
Success := False; |
| 938 |
end if; |
| 939 |
|
| 940 |
if not Success then |
| 941 |
Exit_With_Error ("Error generating response file: disk full"); |
| 942 |
end if; |
| 943 |
end Write_RF; |
| 944 |
|
| 945 |
-- Start of processing for Process_Binder_File |
| 946 |
|
| 947 |
begin |
| 948 |
Fd := fopen (Name'Address, Read_Mode'Address); |
| 949 |
|
| 950 |
if Fd = NULL_Stream then |
| 951 |
Exit_With_Error ("Failed to open binder output"); |
| 952 |
end if; |
| 953 |
|
| 954 |
-- Skip up to the Begin Info line |
| 955 |
|
| 956 |
loop |
| 957 |
Get_Next_Line; |
| 958 |
exit when Next_Line (Nfirst .. Nlast) = Begin_Info; |
| 959 |
end loop; |
| 960 |
|
| 961 |
loop |
| 962 |
Get_Next_Line; |
| 963 |
|
| 964 |
-- Go to end when end line is reached (this will happen in |
| 965 |
-- High_Integrity_Mode where no -L switches are generated) |
| 966 |
|
| 967 |
exit when Next_Line (Nfirst .. Nlast) = End_Info; |
| 968 |
|
| 969 |
Next_Line (Nfirst .. Nlast - 8) := Next_Line (Nfirst + 8 .. Nlast); |
| 970 |
Nlast := Nlast - 8; |
| 971 |
|
| 972 |
-- Go to next section when switches are reached |
| 973 |
|
| 974 |
exit when Next_Line (1) = '-'; |
| 975 |
|
| 976 |
-- Otherwise we have another object file to collect |
| 977 |
|
| 978 |
Linker_Objects.Increment_Last; |
| 979 |
|
| 980 |
-- Mark the positions of first and last object files in case they |
| 981 |
-- need to be placed with a named file on systems having linker |
| 982 |
-- line limitations. |
| 983 |
|
| 984 |
if Objs_Begin = 0 then |
| 985 |
Objs_Begin := Linker_Objects.Last; |
| 986 |
end if; |
| 987 |
|
| 988 |
Linker_Objects.Table (Linker_Objects.Last) := |
| 989 |
new String'(Next_Line (Nfirst .. Nlast)); |
| 990 |
|
| 991 |
-- Nlast - Nfirst + 1, for the size, plus one for the space between |
| 992 |
-- each arguments. |
| 993 |
|
| 994 |
Link_Bytes := Link_Bytes + Nlast - Nfirst + 2; |
| 995 |
end loop; |
| 996 |
|
| 997 |
Objs_End := Linker_Objects.Last; |
| 998 |
|
| 999 |
-- Continue to compute the Link_Bytes, the linker options are part of |
| 1000 |
-- command line length. |
| 1001 |
|
| 1002 |
Store_File_Context; |
| 1003 |
|
| 1004 |
while Next_Line (Nfirst .. Nlast) /= End_Info loop |
| 1005 |
Link_Bytes := Link_Bytes + Nlast - Nfirst + 2; |
| 1006 |
Get_Next_Line; |
| 1007 |
end loop; |
| 1008 |
|
| 1009 |
Rollback_File_Context; |
| 1010 |
|
| 1011 |
-- On systems that have limitations on handling very long linker lines |
| 1012 |
-- we make use of the system linker option which takes a list of object |
| 1013 |
-- file names from a file instead of the command line itself. What we do |
| 1014 |
-- is to replace the list of object files by the special linker option |
| 1015 |
-- which then reads the object file list from a file instead. The option |
| 1016 |
-- to read from a file instead of the command line is only triggered if |
| 1017 |
-- a conservative threshold is passed. |
| 1018 |
|
| 1019 |
if Object_List_File_Required |
| 1020 |
or else (Object_List_File_Supported |
| 1021 |
and then Link_Bytes > Link_Max) |
| 1022 |
then |
| 1023 |
-- Create a temporary file containing the Ada user object files |
| 1024 |
-- needed by the link. This list is taken from the bind file and is |
| 1025 |
-- output one object per line for maximal compatibility with linkers |
| 1026 |
-- supporting this option. |
| 1027 |
|
| 1028 |
Create_Temp_File (Tname_FD, Tname); |
| 1029 |
|
| 1030 |
-- ??? File descriptor should be checked to not be Invalid_FD. |
| 1031 |
-- Status of Write and Close operations should be checked, and |
| 1032 |
-- failure should occur if a status is wrong. |
| 1033 |
|
| 1034 |
for J in Objs_Begin .. Objs_End loop |
| 1035 |
Write_RF (Linker_Objects.Table (J).all); |
| 1036 |
|
| 1037 |
Response_File_Objects.Increment_Last; |
| 1038 |
Response_File_Objects.Table (Response_File_Objects.Last) := |
| 1039 |
Linker_Objects.Table (J); |
| 1040 |
end loop; |
| 1041 |
|
| 1042 |
Close (Tname_FD, Closing_Status); |
| 1043 |
|
| 1044 |
-- Add the special objects list file option together with the name |
| 1045 |
-- of the temporary file (removing the null character) to the objects |
| 1046 |
-- file table. |
| 1047 |
|
| 1048 |
Linker_Objects.Table (Objs_Begin) := |
| 1049 |
new String'(Object_File_Option & |
| 1050 |
Tname (Tname'First .. Tname'Last - 1)); |
| 1051 |
|
| 1052 |
-- The slots containing these object file names are then removed |
| 1053 |
-- from the objects table so they do not appear in the link. They are |
| 1054 |
-- removed by moving up the linker options and non-Ada object files |
| 1055 |
-- appearing after the Ada object list in the table. |
| 1056 |
|
| 1057 |
declare |
| 1058 |
N : Integer; |
| 1059 |
|
| 1060 |
begin |
| 1061 |
N := Objs_End - Objs_Begin + 1; |
| 1062 |
|
| 1063 |
for J in Objs_End + 1 .. Linker_Objects.Last loop |
| 1064 |
Linker_Objects.Table (J - N + 1) := Linker_Objects.Table (J); |
| 1065 |
end loop; |
| 1066 |
|
| 1067 |
Linker_Objects.Set_Last (Linker_Objects.Last - N + 1); |
| 1068 |
end; |
| 1069 |
end if; |
| 1070 |
|
| 1071 |
-- Process switches and options |
| 1072 |
|
| 1073 |
if Next_Line (Nfirst .. Nlast) /= End_Info then |
| 1074 |
Xlinker_Was_Previous := False; |
| 1075 |
|
| 1076 |
loop |
| 1077 |
if Xlinker_Was_Previous |
| 1078 |
or else Next_Line (Nfirst .. Nlast) = "-Xlinker" |
| 1079 |
then |
| 1080 |
Linker_Options.Increment_Last; |
| 1081 |
Linker_Options.Table (Linker_Options.Last) := |
| 1082 |
new String'(Next_Line (Nfirst .. Nlast)); |
| 1083 |
|
| 1084 |
elsif Next_Line (Nfirst .. Nlast) = "-static" then |
| 1085 |
GNAT_Static := True; |
| 1086 |
|
| 1087 |
elsif Next_Line (Nfirst .. Nlast) = "-shared" then |
| 1088 |
GNAT_Shared := True; |
| 1089 |
|
| 1090 |
-- Add binder options only if not already set on the command line. |
| 1091 |
-- This rule is a way to control the linker options order. |
| 1092 |
|
| 1093 |
else |
| 1094 |
if Nlast > Nfirst + 2 and then |
| 1095 |
Next_Line (Nfirst .. Nfirst + 1) = "-L" |
| 1096 |
then |
| 1097 |
-- Construct a library search path for use later to locate |
| 1098 |
-- static gnatlib libraries. |
| 1099 |
|
| 1100 |
if Libpath.Last > 1 then |
| 1101 |
Libpath.Increment_Last; |
| 1102 |
Libpath.Table (Libpath.Last) := Path_Separator; |
| 1103 |
end if; |
| 1104 |
|
| 1105 |
for I in Nfirst + 2 .. Nlast loop |
| 1106 |
Libpath.Increment_Last; |
| 1107 |
Libpath.Table (Libpath.Last) := Next_Line (I); |
| 1108 |
end loop; |
| 1109 |
|
| 1110 |
Linker_Options.Increment_Last; |
| 1111 |
|
| 1112 |
Linker_Options.Table (Linker_Options.Last) := |
| 1113 |
new String'(Next_Line (Nfirst .. Nlast)); |
| 1114 |
|
| 1115 |
elsif Next_Line (Nfirst .. Nlast) = "-lgnarl" |
| 1116 |
or else Next_Line (Nfirst .. Nlast) = "-lgnat" |
| 1117 |
or else |
| 1118 |
Next_Line |
| 1119 |
(1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) = |
| 1120 |
Shared_Lib ("gnarl") |
| 1121 |
or else |
| 1122 |
Next_Line |
| 1123 |
(1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) = |
| 1124 |
Shared_Lib ("gnat") |
| 1125 |
then |
| 1126 |
-- If it is a shared library, remove the library version. |
| 1127 |
-- We will be looking for the static version of the library |
| 1128 |
-- as it is in the same directory as the shared version. |
| 1129 |
|
| 1130 |
if Nlast >= Library_Version'Length |
| 1131 |
and then |
| 1132 |
Next_Line (Nlast - Library_Version'Length + 1 .. Nlast) = |
| 1133 |
Library_Version |
| 1134 |
then |
| 1135 |
-- Set Last to point to last character before the |
| 1136 |
-- library version. |
| 1137 |
|
| 1138 |
Last := Nlast - Library_Version'Length - 1; |
| 1139 |
else |
| 1140 |
Last := Nlast; |
| 1141 |
end if; |
| 1142 |
|
| 1143 |
Search_Library_Path |
| 1144 |
(Next_Line => Next_Line, |
| 1145 |
Nfirst => Nfirst, |
| 1146 |
Nlast => Nlast, |
| 1147 |
Last => Last, |
| 1148 |
GNAT_Static => GNAT_Static, |
| 1149 |
GNAT_Shared => GNAT_Shared); |
| 1150 |
|
| 1151 |
else |
| 1152 |
Linker_Options.Increment_Last; |
| 1153 |
Linker_Options.Table (Linker_Options.Last) := |
| 1154 |
new String'(Next_Line (Nfirst .. Nlast)); |
| 1155 |
end if; |
| 1156 |
end if; |
| 1157 |
|
| 1158 |
Xlinker_Was_Previous := Next_Line (Nfirst .. Nlast) = "-Xlinker"; |
| 1159 |
|
| 1160 |
Get_Next_Line; |
| 1161 |
exit when Next_Line (Nfirst .. Nlast) = End_Info; |
| 1162 |
|
| 1163 |
Next_Line (Nfirst .. Nlast - 8) := Next_Line (Nfirst + 8 .. Nlast); |
| 1164 |
Nlast := Nlast - 8; |
| 1165 |
end loop; |
| 1166 |
end if; |
| 1167 |
|
| 1168 |
-- If -shared was specified, invoke gcc with -shared-libgcc |
| 1169 |
|
| 1170 |
if GNAT_Shared then |
| 1171 |
Linker_Options.Increment_Last; |
| 1172 |
Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc; |
| 1173 |
end if; |
| 1174 |
|
| 1175 |
Status := fclose (Fd); |
| 1176 |
end Process_Binder_File; |
| 1177 |
|
| 1178 |
------------------------- |
| 1179 |
-- Search_Library_Path -- |
| 1180 |
------------------------- |
| 1181 |
|
| 1182 |
procedure Search_Library_Path |
| 1183 |
(Next_Line : String; |
| 1184 |
Nfirst : Integer; |
| 1185 |
Nlast : Integer; |
| 1186 |
Last : Integer; |
| 1187 |
GNAT_Static : Boolean; |
| 1188 |
GNAT_Shared : Boolean) |
| 1189 |
is |
| 1190 |
File_Path : String_Access; |
| 1191 |
|
| 1192 |
Object_Lib_Extension : constant String := |
| 1193 |
Value (Object_Library_Ext_Ptr); |
| 1194 |
|
| 1195 |
File_Name : constant String := "lib" & |
| 1196 |
Next_Line (Nfirst + 2 .. Last) & Object_Lib_Extension; |
| 1197 |
|
| 1198 |
Run_Path_Opt : constant String := |
| 1199 |
Value (Run_Path_Option_Ptr); |
| 1200 |
|
| 1201 |
GCC_Index : Natural; |
| 1202 |
Run_Path_Opt_Index : Natural := 0; |
| 1203 |
|
| 1204 |
begin |
| 1205 |
File_Path := |
| 1206 |
Locate_Regular_File (File_Name, |
| 1207 |
String (Libpath.Table (1 .. Libpath.Last))); |
| 1208 |
|
| 1209 |
if File_Path /= null then |
| 1210 |
if GNAT_Static then |
| 1211 |
|
| 1212 |
-- If static gnatlib found, explicitly specify to overcome |
| 1213 |
-- possible linker default usage of shared version. |
| 1214 |
|
| 1215 |
Linker_Options.Increment_Last; |
| 1216 |
|
| 1217 |
Linker_Options.Table (Linker_Options.Last) := |
| 1218 |
new String'(File_Path.all); |
| 1219 |
|
| 1220 |
elsif GNAT_Shared then |
| 1221 |
if Opt.Run_Path_Option then |
| 1222 |
|
| 1223 |
-- If shared gnatlib desired, add appropriate system specific |
| 1224 |
-- switch so that it can be located at runtime. |
| 1225 |
|
| 1226 |
if Run_Path_Opt'Length /= 0 then |
| 1227 |
|
| 1228 |
-- Output the system specific linker command that allows the |
| 1229 |
-- image activator to find the shared library at |
| 1230 |
-- runtime. Also add path to find libgcc_s.so, if relevant. |
| 1231 |
|
| 1232 |
declare |
| 1233 |
Path : String (1 .. File_Path'Length + 15); |
| 1234 |
|
| 1235 |
Path_Last : constant Natural := File_Path'Length; |
| 1236 |
|
| 1237 |
begin |
| 1238 |
Path (1 .. File_Path'Length) := File_Path.all; |
| 1239 |
|
| 1240 |
-- To find the location of the shared version of libgcc, |
| 1241 |
-- we look for "gcc-lib" in the path of the |
| 1242 |
-- library. However, this subdirectory is no longer |
| 1243 |
-- present in recent versions of GCC. So, we look for the |
| 1244 |
-- last subdirectory "lib" in the path. |
| 1245 |
|
| 1246 |
GCC_Index := Index (Path (1 .. Path_Last), "gcc-lib"); |
| 1247 |
|
| 1248 |
if GCC_Index /= 0 then |
| 1249 |
|
| 1250 |
-- The shared version of libgcc is located in the |
| 1251 |
-- parent directory. |
| 1252 |
|
| 1253 |
GCC_Index := GCC_Index - 1; |
| 1254 |
|
| 1255 |
else |
| 1256 |
GCC_Index := Index (Path (1 .. Path_Last), "/lib/"); |
| 1257 |
|
| 1258 |
if GCC_Index = 0 then |
| 1259 |
GCC_Index := |
| 1260 |
Index (Path (1 .. Path_Last), |
| 1261 |
Directory_Separator & "lib" |
| 1262 |
& Directory_Separator); |
| 1263 |
end if; |
| 1264 |
|
| 1265 |
-- If we have found a "lib" subdir in the path to |
| 1266 |
-- libgnat, the possible shared libgcc of interest by |
| 1267 |
-- default is in libgcc_subdir at the same level. |
| 1268 |
|
| 1269 |
if GCC_Index /= 0 then |
| 1270 |
declare |
| 1271 |
Subdir : constant String := |
| 1272 |
Value (Libgcc_Subdir_Ptr); |
| 1273 |
|
| 1274 |
begin |
| 1275 |
Path |
| 1276 |
(GCC_Index + 1 |
| 1277 |
.. |
| 1278 |
GCC_Index + Subdir'Length) := Subdir; |
| 1279 |
GCC_Index := GCC_Index + Subdir'Length; |
| 1280 |
end; |
| 1281 |
end if; |
| 1282 |
end if; |
| 1283 |
|
| 1284 |
-- Look for an eventual run_path_option in |
| 1285 |
-- the linker switches. |
| 1286 |
|
| 1287 |
if Separate_Run_Path_Options then |
| 1288 |
Linker_Options.Increment_Last; |
| 1289 |
Linker_Options.Table |
| 1290 |
(Linker_Options.Last) := |
| 1291 |
new String' |
| 1292 |
(Run_Path_Opt |
| 1293 |
& File_Path |
| 1294 |
(1 .. File_Path'Length |
| 1295 |
- File_Name'Length)); |
| 1296 |
|
| 1297 |
if GCC_Index /= 0 then |
| 1298 |
Linker_Options.Increment_Last; |
| 1299 |
Linker_Options.Table (Linker_Options.Last) := |
| 1300 |
new String' |
| 1301 |
(Run_Path_Opt |
| 1302 |
& Path (1 .. GCC_Index)); |
| 1303 |
end if; |
| 1304 |
|
| 1305 |
else |
| 1306 |
for J in reverse 1 .. Linker_Options.Last loop |
| 1307 |
if Linker_Options.Table (J) /= null |
| 1308 |
and then Is_Prefix |
| 1309 |
(Linker_Options.Table (J).all, Run_Path_Opt) |
| 1310 |
then |
| 1311 |
-- We have found an already specified |
| 1312 |
-- run_path_option: we will add to this switch, |
| 1313 |
-- because only one run_path_option should be |
| 1314 |
-- specified. |
| 1315 |
|
| 1316 |
Run_Path_Opt_Index := J; |
| 1317 |
exit; |
| 1318 |
end if; |
| 1319 |
end loop; |
| 1320 |
|
| 1321 |
-- If there is no run_path_option, we need to add one. |
| 1322 |
|
| 1323 |
if Run_Path_Opt_Index = 0 then |
| 1324 |
Linker_Options.Increment_Last; |
| 1325 |
end if; |
| 1326 |
|
| 1327 |
if GCC_Index = 0 then |
| 1328 |
if Run_Path_Opt_Index = 0 then |
| 1329 |
Linker_Options.Table |
| 1330 |
(Linker_Options.Last) := |
| 1331 |
new String' |
| 1332 |
(Run_Path_Opt |
| 1333 |
& File_Path |
| 1334 |
(1 .. File_Path'Length |
| 1335 |
- File_Name'Length)); |
| 1336 |
|
| 1337 |
else |
| 1338 |
Linker_Options.Table |
| 1339 |
(Run_Path_Opt_Index) := |
| 1340 |
new String' |
| 1341 |
(Linker_Options.Table |
| 1342 |
(Run_Path_Opt_Index).all |
| 1343 |
& Path_Separator |
| 1344 |
& File_Path |
| 1345 |
(1 .. File_Path'Length |
| 1346 |
- File_Name'Length)); |
| 1347 |
end if; |
| 1348 |
|
| 1349 |
else |
| 1350 |
if Run_Path_Opt_Index = 0 then |
| 1351 |
Linker_Options.Table |
| 1352 |
(Linker_Options.Last) := |
| 1353 |
new String' |
| 1354 |
(Run_Path_Opt |
| 1355 |
& File_Path |
| 1356 |
(1 .. File_Path'Length |
| 1357 |
- File_Name'Length) |
| 1358 |
& Path_Separator |
| 1359 |
& Path (1 .. GCC_Index)); |
| 1360 |
|
| 1361 |
else |
| 1362 |
Linker_Options.Table |
| 1363 |
(Run_Path_Opt_Index) := |
| 1364 |
new String' |
| 1365 |
(Linker_Options.Table |
| 1366 |
(Run_Path_Opt_Index).all |
| 1367 |
& Path_Separator |
| 1368 |
& File_Path |
| 1369 |
(1 .. File_Path'Length |
| 1370 |
- File_Name'Length) |
| 1371 |
& Path_Separator |
| 1372 |
& Path (1 .. GCC_Index)); |
| 1373 |
end if; |
| 1374 |
end if; |
| 1375 |
end if; |
| 1376 |
end; |
| 1377 |
end if; |
| 1378 |
end if; |
| 1379 |
|
| 1380 |
-- Then we add the appropriate -l switch |
| 1381 |
|
| 1382 |
Linker_Options.Increment_Last; |
| 1383 |
Linker_Options.Table (Linker_Options.Last) := |
| 1384 |
new String'(Next_Line (Nfirst .. Nlast)); |
| 1385 |
end if; |
| 1386 |
|
| 1387 |
else |
| 1388 |
-- If gnatlib library not found, then add it anyway in |
| 1389 |
-- case some other mechanism may find it. |
| 1390 |
|
| 1391 |
Linker_Options.Increment_Last; |
| 1392 |
Linker_Options.Table (Linker_Options.Last) := |
| 1393 |
new String'(Next_Line (Nfirst .. Nlast)); |
| 1394 |
end if; |
| 1395 |
end Search_Library_Path; |
| 1396 |
|
| 1397 |
----------- |
| 1398 |
-- Usage -- |
| 1399 |
----------- |
| 1400 |
|
| 1401 |
procedure Usage is |
| 1402 |
begin |
| 1403 |
Write_Str ("Usage: "); |
| 1404 |
Write_Str (Base_Command_Name.all); |
| 1405 |
Write_Str (" switches mainprog.ali [non-Ada-objects] [linker-options]"); |
| 1406 |
Write_Eol; |
| 1407 |
Write_Eol; |
| 1408 |
Write_Line (" mainprog.ali the ALI file of the main program"); |
| 1409 |
Write_Eol; |
| 1410 |
Write_Eol; |
| 1411 |
Display_Usage_Version_And_Help; |
| 1412 |
Write_Line (" -f Force object file list to be generated"); |
| 1413 |
Write_Line (" -g Compile binder source file with debug information"); |
| 1414 |
Write_Line (" -n Do not compile the binder source file"); |
| 1415 |
Write_Line (" -P Process files for use by CodePeer"); |
| 1416 |
Write_Line (" -R Do not use a run_path_option"); |
| 1417 |
Write_Line (" -v Verbose mode"); |
| 1418 |
Write_Line (" -v -v Very verbose mode"); |
| 1419 |
Write_Eol; |
| 1420 |
Write_Line (" -o nam Use 'nam' as the name of the executable"); |
| 1421 |
Write_Line (" -Bdir Load compiler executables from dir"); |
| 1422 |
|
| 1423 |
if Is_Supported (Map_File) then |
| 1424 |
Write_Line (" -Mmap Create map file map"); |
| 1425 |
Write_Line (" -M Create map file mainprog.map"); |
| 1426 |
end if; |
| 1427 |
|
| 1428 |
Write_Line (" --GCC=comp Use 'comp' as the compiler rather than 'gcc'"); |
| 1429 |
Write_Line (" --LINK=lnk Use 'lnk' as the linker rather than 'gcc'"); |
| 1430 |
Write_Eol; |
| 1431 |
Write_Line (" [non-Ada-objects] list of non Ada object files"); |
| 1432 |
Write_Line (" [linker-options] other options for the linker"); |
| 1433 |
end Usage; |
| 1434 |
|
| 1435 |
------------------ |
| 1436 |
-- Write_Header -- |
| 1437 |
------------------ |
| 1438 |
|
| 1439 |
procedure Write_Header is |
| 1440 |
begin |
| 1441 |
if Verbose_Mode then |
| 1442 |
Write_Eol; |
| 1443 |
Display_Version ("GNATLINK", "1995"); |
| 1444 |
end if; |
| 1445 |
end Write_Header; |
| 1446 |
|
| 1447 |
----------------- |
| 1448 |
-- Write_Usage -- |
| 1449 |
----------------- |
| 1450 |
|
| 1451 |
procedure Write_Usage is |
| 1452 |
begin |
| 1453 |
Write_Header; |
| 1454 |
Usage; |
| 1455 |
end Write_Usage; |
| 1456 |
|
| 1457 |
-- Start of processing for Gnatlink |
| 1458 |
|
| 1459 |
begin |
| 1460 |
-- Add the directory where gnatlink is invoked in front of the path, if |
| 1461 |
-- gnatlink is invoked with directory information. |
| 1462 |
|
| 1463 |
declare |
| 1464 |
Command : constant String := Command_Name; |
| 1465 |
begin |
| 1466 |
for Index in reverse Command'Range loop |
| 1467 |
if Command (Index) = Directory_Separator then |
| 1468 |
declare |
| 1469 |
Absolute_Dir : constant String := |
| 1470 |
Normalize_Pathname |
| 1471 |
(Command (Command'First .. Index)); |
| 1472 |
|
| 1473 |
PATH : constant String := |
| 1474 |
Absolute_Dir & |
| 1475 |
Path_Separator & |
| 1476 |
Getenv ("PATH").all; |
| 1477 |
|
| 1478 |
begin |
| 1479 |
Setenv ("PATH", PATH); |
| 1480 |
end; |
| 1481 |
|
| 1482 |
exit; |
| 1483 |
end if; |
| 1484 |
end loop; |
| 1485 |
end; |
| 1486 |
|
| 1487 |
Base_Command_Name := new String'(Base_Name (Command_Name)); |
| 1488 |
Process_Args; |
| 1489 |
|
| 1490 |
if Argument_Count = 0 |
| 1491 |
or else (Verbose_Mode and then Argument_Count = 1) |
| 1492 |
then |
| 1493 |
Write_Usage; |
| 1494 |
Exit_Program (E_Fatal); |
| 1495 |
end if; |
| 1496 |
|
| 1497 |
-- Initialize packages to be used |
| 1498 |
|
| 1499 |
Csets.Initialize; |
| 1500 |
Snames.Initialize; |
| 1501 |
|
| 1502 |
-- We always compile with -c |
| 1503 |
|
| 1504 |
Binder_Options_From_ALI.Increment_Last; |
| 1505 |
Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) := |
| 1506 |
new String'("-c"); |
| 1507 |
|
| 1508 |
if Ali_File_Name = null then |
| 1509 |
Exit_With_Error ("no ali file given for link"); |
| 1510 |
end if; |
| 1511 |
|
| 1512 |
if not Is_Regular_File (Ali_File_Name.all) then |
| 1513 |
Exit_With_Error (Ali_File_Name.all & " not found"); |
| 1514 |
end if; |
| 1515 |
|
| 1516 |
-- Read the ALI file of the main subprogram if the binder generated file |
| 1517 |
-- needs to be compiled and no --GCC= switch has been specified. Fetch the |
| 1518 |
-- back end switches from this ALI file and use these switches to compile |
| 1519 |
-- the binder generated file |
| 1520 |
|
| 1521 |
if Compile_Bind_File and then Standard_Gcc then |
| 1522 |
Initialize_ALI; |
| 1523 |
Name_Len := Ali_File_Name'Length; |
| 1524 |
Name_Buffer (1 .. Name_Len) := Ali_File_Name.all; |
| 1525 |
|
| 1526 |
declare |
| 1527 |
use Types; |
| 1528 |
F : constant File_Name_Type := Name_Find; |
| 1529 |
T : Text_Buffer_Ptr; |
| 1530 |
A : ALI_Id; |
| 1531 |
|
| 1532 |
begin |
| 1533 |
-- Load the ALI file |
| 1534 |
|
| 1535 |
T := Read_Library_Info (F, True); |
| 1536 |
|
| 1537 |
-- Read it. Note that we ignore errors, since we only want very |
| 1538 |
-- limited information from the ali file, and likely a slightly |
| 1539 |
-- wrong version will be just fine, though in normal operation |
| 1540 |
-- we don't expect this to happen. |
| 1541 |
|
| 1542 |
A := Scan_ALI |
| 1543 |
(F, |
| 1544 |
T, |
| 1545 |
Err => False, |
| 1546 |
Ignore_Errors => True); |
| 1547 |
|
| 1548 |
if A /= No_ALI_Id then |
| 1549 |
for |
| 1550 |
Index in Units.Table (ALIs.Table (A).First_Unit).First_Arg .. |
| 1551 |
Units.Table (ALIs.Table (A).First_Unit).Last_Arg |
| 1552 |
loop |
| 1553 |
-- Do not compile with the front end switches. However, --RTS |
| 1554 |
-- is to be dealt with specially because it needs to be passed |
| 1555 |
-- to compile the file generated by the binder. |
| 1556 |
|
| 1557 |
declare |
| 1558 |
Arg : String_Ptr renames Args.Table (Index); |
| 1559 |
begin |
| 1560 |
if not Is_Front_End_Switch (Arg.all) then |
| 1561 |
Binder_Options_From_ALI.Increment_Last; |
| 1562 |
Binder_Options_From_ALI.Table |
| 1563 |
(Binder_Options_From_ALI.Last) := String_Access (Arg); |
| 1564 |
|
| 1565 |
-- GNAT doesn't support GCC's multilib mechanism when it |
| 1566 |
-- is configured with --disable-libada. This means that, |
| 1567 |
-- when a multilib switch is used to request a particular |
| 1568 |
-- compilation mode, the corresponding --RTS switch must |
| 1569 |
-- also be specified. It is convenient to eliminate the |
| 1570 |
-- redundancy by keying the compilation mode on a single |
| 1571 |
-- switch, namely --RTS, and have the compiler reinstate |
| 1572 |
-- the multilib switch (see gcc-interface/lang-specs.h). |
| 1573 |
-- This switch must be passed to the driver at link time. |
| 1574 |
|
| 1575 |
if Arg'Length = 5 |
| 1576 |
and then Arg (Arg'First + 1 .. Arg'First + 4) = "mrtp" |
| 1577 |
then |
| 1578 |
Linker_Options.Increment_Last; |
| 1579 |
Linker_Options.Table |
| 1580 |
(Linker_Options.Last) := String_Access (Arg); |
| 1581 |
end if; |
| 1582 |
|
| 1583 |
elsif Arg'Length > 5 |
| 1584 |
and then Arg (Arg'First + 2 .. Arg'First + 5) = "RTS=" |
| 1585 |
then |
| 1586 |
Binder_Options_From_ALI.Increment_Last; |
| 1587 |
Binder_Options_From_ALI.Table |
| 1588 |
(Binder_Options_From_ALI.Last) := String_Access (Arg); |
| 1589 |
|
| 1590 |
-- Set the RTS_*_Path_Name variables, so that |
| 1591 |
-- the correct directories will be set when |
| 1592 |
-- Osint.Add_Default_Search_Dirs will be called later. |
| 1593 |
|
| 1594 |
Opt.RTS_Src_Path_Name := |
| 1595 |
Get_RTS_Search_Dir |
| 1596 |
(Arg (Arg'First + 6 .. Arg'Last), Include); |
| 1597 |
|
| 1598 |
Opt.RTS_Lib_Path_Name := |
| 1599 |
Get_RTS_Search_Dir |
| 1600 |
(Arg (Arg'First + 6 .. Arg'Last), Objects); |
| 1601 |
end if; |
| 1602 |
end; |
| 1603 |
end loop; |
| 1604 |
end if; |
| 1605 |
end; |
| 1606 |
end if; |
| 1607 |
|
| 1608 |
-- Get target parameters |
| 1609 |
|
| 1610 |
Osint.Add_Default_Search_Dirs; |
| 1611 |
Targparm.Get_Target_Parameters; |
| 1612 |
|
| 1613 |
-- Compile the bind file with the following switches: |
| 1614 |
|
| 1615 |
-- -gnatA stops reading gnat.adc, since we don't know what |
| 1616 |
-- pragmas would work, and we do not need it anyway. |
| 1617 |
|
| 1618 |
-- -gnatWb allows brackets coding for wide characters |
| 1619 |
|
| 1620 |
-- -gnatiw allows wide characters in identifiers. This is needed |
| 1621 |
-- because bindgen uses brackets encoding for all upper |
| 1622 |
-- half and wide characters in identifier names. |
| 1623 |
|
| 1624 |
-- In addition, in CodePeer mode compile with -x adascil -gnatcC |
| 1625 |
|
| 1626 |
Binder_Options_From_ALI.Increment_Last; |
| 1627 |
Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) := |
| 1628 |
new String'("-gnatA"); |
| 1629 |
Binder_Options_From_ALI.Increment_Last; |
| 1630 |
Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) := |
| 1631 |
new String'("-gnatWb"); |
| 1632 |
Binder_Options_From_ALI.Increment_Last; |
| 1633 |
Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) := |
| 1634 |
new String'("-gnatiw"); |
| 1635 |
|
| 1636 |
if Opt.CodePeer_Mode then |
| 1637 |
Binder_Options_From_ALI.Increment_Last; |
| 1638 |
Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) := |
| 1639 |
new String'("-x"); |
| 1640 |
Binder_Options_From_ALI.Increment_Last; |
| 1641 |
Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) := |
| 1642 |
new String'("adascil"); |
| 1643 |
Binder_Options_From_ALI.Increment_Last; |
| 1644 |
Binder_Options_From_ALI.Table (Binder_Options_From_ALI.Last) := |
| 1645 |
new String'("-gnatcC"); |
| 1646 |
end if; |
| 1647 |
|
| 1648 |
-- Locate all the necessary programs and verify required files are present |
| 1649 |
|
| 1650 |
Gcc_Path := System.OS_Lib.Locate_Exec_On_Path (Gcc.all); |
| 1651 |
|
| 1652 |
if Gcc_Path = null then |
| 1653 |
Exit_With_Error ("Couldn't locate " & Gcc.all); |
| 1654 |
end if; |
| 1655 |
|
| 1656 |
if Linker_Path = null then |
| 1657 |
Linker_Path := Gcc_Path; |
| 1658 |
end if; |
| 1659 |
|
| 1660 |
Write_Header; |
| 1661 |
|
| 1662 |
Target_Debuggable_Suffix := Get_Target_Debuggable_Suffix; |
| 1663 |
|
| 1664 |
-- If no output name specified, then use the base name of .ali file name |
| 1665 |
|
| 1666 |
if Output_File_Name = null then |
| 1667 |
Output_File_Name := |
| 1668 |
new String'(Base_Name (Ali_File_Name.all) |
| 1669 |
& Target_Debuggable_Suffix.all); |
| 1670 |
end if; |
| 1671 |
|
| 1672 |
Linker_Options.Increment_Last; |
| 1673 |
Linker_Options.Table (Linker_Options.Last) := new String'("-o"); |
| 1674 |
|
| 1675 |
Linker_Options.Increment_Last; |
| 1676 |
Linker_Options.Table (Linker_Options.Last) := |
| 1677 |
new String'(Output_File_Name.all); |
| 1678 |
|
| 1679 |
Check_Existing_Executable (Output_File_Name.all); |
| 1680 |
|
| 1681 |
-- Warn if main program is called "test", as that may be a built-in command |
| 1682 |
-- on Unix. On non-Unix systems executables have a suffix, so the warning |
| 1683 |
-- will not appear. However, do not warn in the case of a cross compiler. |
| 1684 |
|
| 1685 |
-- Assume this is a cross tool if the executable name is not gnatlink. |
| 1686 |
-- Note that the executable name is also gnatlink on windows, but in that |
| 1687 |
-- case the output file name will be test.exe rather than test. |
| 1688 |
|
| 1689 |
if Base_Command_Name.all = "gnatlink" |
| 1690 |
and then Output_File_Name.all = "test" |
| 1691 |
then |
| 1692 |
Error_Msg ("warning: executable name """ & Output_File_Name.all |
| 1693 |
& """ may conflict with shell command"); |
| 1694 |
end if; |
| 1695 |
|
| 1696 |
-- Special warnings for worrisome file names on windows |
| 1697 |
|
| 1698 |
-- Recent versions of Windows by default cause privilege escalation if an |
| 1699 |
-- executable file name contains substrings "install", "setup", "update" |
| 1700 |
-- or "patch". A console application will typically fail to load as a |
| 1701 |
-- result, so we should warn the user. |
| 1702 |
|
| 1703 |
Bad_File_Names_On_Windows : declare |
| 1704 |
FN : String := Output_File_Name.all; |
| 1705 |
|
| 1706 |
procedure Check_File_Name (S : String); |
| 1707 |
-- Warn if file name has the substring S |
| 1708 |
|
| 1709 |
procedure Check_File_Name (S : String) is |
| 1710 |
begin |
| 1711 |
if Ada.Strings.Fixed.Index (FN, S) /= 0 then |
| 1712 |
Error_Msg |
| 1713 |
("warning: executable file name """ & Output_File_Name.all |
| 1714 |
& """ contains substring """ & S & '"'); |
| 1715 |
Error_Msg |
| 1716 |
("admin privileges may be required to run this file"); |
| 1717 |
end if; |
| 1718 |
end Check_File_Name; |
| 1719 |
|
| 1720 |
-- Start of processing for Bad_File_Names_On_Windows |
| 1721 |
|
| 1722 |
begin |
| 1723 |
for J in FN'Range loop |
| 1724 |
FN (J) := Csets.Fold_Lower (FN (J)); |
| 1725 |
end loop; |
| 1726 |
|
| 1727 |
-- For now we detect Windows by its executable suffix of .exe |
| 1728 |
|
| 1729 |
if Target_Debuggable_Suffix.all = ".exe" then |
| 1730 |
Check_File_Name ("install"); |
| 1731 |
Check_File_Name ("setup"); |
| 1732 |
Check_File_Name ("update"); |
| 1733 |
Check_File_Name ("patch"); |
| 1734 |
end if; |
| 1735 |
end Bad_File_Names_On_Windows; |
| 1736 |
|
| 1737 |
-- If -M switch was specified, add the switches to create the map file |
| 1738 |
|
| 1739 |
if Create_Map_File then |
| 1740 |
declare |
| 1741 |
Map_Name : constant String := Base_Name (Ali_File_Name.all) & ".map"; |
| 1742 |
Switches : String_List_Access; |
| 1743 |
|
| 1744 |
begin |
| 1745 |
Convert (Map_File, Map_Name, Switches); |
| 1746 |
|
| 1747 |
if Switches /= null then |
| 1748 |
for J in Switches'Range loop |
| 1749 |
Linker_Options.Increment_Last; |
| 1750 |
Linker_Options.Table (Linker_Options.Last) := Switches (J); |
| 1751 |
end loop; |
| 1752 |
end if; |
| 1753 |
end; |
| 1754 |
end if; |
| 1755 |
|
| 1756 |
-- Perform consistency checks |
| 1757 |
|
| 1758 |
-- Transform the .ali file name into the binder output file name |
| 1759 |
|
| 1760 |
Make_Binder_File_Names : declare |
| 1761 |
Fname : constant String := Base_Name (Ali_File_Name.all); |
| 1762 |
Fname_Len : Integer := Fname'Length; |
| 1763 |
|
| 1764 |
Maximum_File_Name_Length : constant Integer := |
| 1765 |
Get_Maximum_File_Name_Length; |
| 1766 |
|
| 1767 |
Bind_File_Prefix : Types.String_Ptr; |
| 1768 |
-- Contains prefix used for bind files |
| 1769 |
|
| 1770 |
begin |
| 1771 |
-- Set prefix |
| 1772 |
|
| 1773 |
Bind_File_Prefix := new String'("b~"); |
| 1774 |
|
| 1775 |
-- If the length of the binder file becomes too long due to |
| 1776 |
-- the addition of the "b?" prefix, then truncate it. |
| 1777 |
|
| 1778 |
if Maximum_File_Name_Length > 0 then |
| 1779 |
while Fname_Len > |
| 1780 |
Maximum_File_Name_Length - Bind_File_Prefix.all'Length |
| 1781 |
loop |
| 1782 |
Fname_Len := Fname_Len - 1; |
| 1783 |
end loop; |
| 1784 |
end if; |
| 1785 |
|
| 1786 |
declare |
| 1787 |
Fnam : constant String := |
| 1788 |
Bind_File_Prefix.all & |
| 1789 |
Fname (Fname'First .. Fname'First + Fname_Len - 1); |
| 1790 |
|
| 1791 |
begin |
| 1792 |
Binder_Spec_Src_File := new String'(Fnam & ".ads"); |
| 1793 |
Binder_Body_Src_File := new String'(Fnam & ".adb"); |
| 1794 |
Binder_Ali_File := new String'(Fnam & ".ali"); |
| 1795 |
|
| 1796 |
Binder_Obj_File := new String'(Fnam & Get_Target_Object_Suffix.all); |
| 1797 |
end; |
| 1798 |
|
| 1799 |
if Fname_Len /= Fname'Length then |
| 1800 |
Binder_Options.Increment_Last; |
| 1801 |
Binder_Options.Table (Binder_Options.Last) := new String'("-o"); |
| 1802 |
Binder_Options.Increment_Last; |
| 1803 |
Binder_Options.Table (Binder_Options.Last) := Binder_Obj_File; |
| 1804 |
end if; |
| 1805 |
end Make_Binder_File_Names; |
| 1806 |
|
| 1807 |
Process_Binder_File (Binder_Body_Src_File.all & ASCII.NUL); |
| 1808 |
|
| 1809 |
-- Compile the binder file. This is fast, so we always do it, unless |
| 1810 |
-- specifically told not to by the -n switch |
| 1811 |
|
| 1812 |
if Compile_Bind_File then |
| 1813 |
Bind_Step : declare |
| 1814 |
Success : Boolean; |
| 1815 |
|
| 1816 |
Args : Argument_List |
| 1817 |
(1 .. Binder_Options_From_ALI.Last + Binder_Options.Last + 1); |
| 1818 |
|
| 1819 |
begin |
| 1820 |
for J in 1 .. Binder_Options_From_ALI.Last loop |
| 1821 |
Args (J) := Binder_Options_From_ALI.Table (J); |
| 1822 |
end loop; |
| 1823 |
|
| 1824 |
for J in 1 .. Binder_Options.Last loop |
| 1825 |
Args (Binder_Options_From_ALI.Last + J) := |
| 1826 |
Binder_Options.Table (J); |
| 1827 |
end loop; |
| 1828 |
|
| 1829 |
-- Use the full path of the binder generated source, so that it is |
| 1830 |
-- guaranteed that the debugger will find this source, even with |
| 1831 |
-- STABS. |
| 1832 |
|
| 1833 |
Args (Args'Last) := |
| 1834 |
new String'(Normalize_Pathname (Binder_Body_Src_File.all)); |
| 1835 |
|
| 1836 |
if Verbose_Mode then |
| 1837 |
Write_Str (Base_Name (Gcc_Path.all)); |
| 1838 |
|
| 1839 |
for J in Args'Range loop |
| 1840 |
Write_Str (" "); |
| 1841 |
Write_Str (Args (J).all); |
| 1842 |
end loop; |
| 1843 |
|
| 1844 |
Write_Eol; |
| 1845 |
end if; |
| 1846 |
|
| 1847 |
System.OS_Lib.Spawn (Gcc_Path.all, Args, Success); |
| 1848 |
|
| 1849 |
if not Success then |
| 1850 |
Exit_Program (E_Fatal); |
| 1851 |
end if; |
| 1852 |
end Bind_Step; |
| 1853 |
end if; |
| 1854 |
|
| 1855 |
-- In CodePeer mode, there's nothing left to do after the binder file has |
| 1856 |
-- been compiled. |
| 1857 |
|
| 1858 |
if Opt.CodePeer_Mode then |
| 1859 |
if Tname_FD /= Invalid_FD then |
| 1860 |
Delete (Tname); |
| 1861 |
end if; |
| 1862 |
|
| 1863 |
return; |
| 1864 |
end if; |
| 1865 |
|
| 1866 |
-- Now, actually link the program |
| 1867 |
|
| 1868 |
Link_Step : declare |
| 1869 |
Num_Args : Natural := |
| 1870 |
(Linker_Options.Last - Linker_Options.First + 1) + |
| 1871 |
(Gcc_Linker_Options.Last - Gcc_Linker_Options.First + 1) + |
| 1872 |
(Linker_Objects.Last - Linker_Objects.First + 1); |
| 1873 |
Stack_Op : Boolean := False; |
| 1874 |
|
| 1875 |
begin |
| 1876 |
-- Remove duplicate stack size setting from the Linker_Options table. |
| 1877 |
-- The stack setting option "-Xlinker --stack=R,C" can be found |
| 1878 |
-- in one line when set by a pragma Linker_Options or in two lines |
| 1879 |
-- ("-Xlinker" then "--stack=R,C") when set on the command line. We |
| 1880 |
-- also check for the "-Wl,--stack=R" style option. |
| 1881 |
|
| 1882 |
-- We must remove the second stack setting option instance because |
| 1883 |
-- the one on the command line will always be the first one. And any |
| 1884 |
-- subsequent stack setting option will overwrite the previous one. |
| 1885 |
-- This is done especially for GNAT/NT where we set the stack size |
| 1886 |
-- for tasking programs by a pragma in the NT specific tasking |
| 1887 |
-- package System.Task_Primitives.Operations. |
| 1888 |
|
| 1889 |
-- Note: This is not a FOR loop that runs from Linker_Options.First |
| 1890 |
-- to Linker_Options.Last, since operations within the loop can |
| 1891 |
-- modify the length of the table. |
| 1892 |
|
| 1893 |
Clean_Link_Option_Set : declare |
| 1894 |
J : Natural; |
| 1895 |
Shared_Libgcc_Seen : Boolean := False; |
| 1896 |
Static_Libgcc_Seen : Boolean := False; |
| 1897 |
|
| 1898 |
begin |
| 1899 |
J := Linker_Options.First; |
| 1900 |
while J <= Linker_Options.Last loop |
| 1901 |
if Linker_Options.Table (J).all = "-Xlinker" |
| 1902 |
and then J < Linker_Options.Last |
| 1903 |
and then Is_Prefix (Linker_Options.Table (J + 1).all, "--stack=") |
| 1904 |
then |
| 1905 |
if Stack_Op then |
| 1906 |
Linker_Options.Table (J .. Linker_Options.Last - 2) := |
| 1907 |
Linker_Options.Table (J + 2 .. Linker_Options.Last); |
| 1908 |
Linker_Options.Decrement_Last; |
| 1909 |
Linker_Options.Decrement_Last; |
| 1910 |
Num_Args := Num_Args - 2; |
| 1911 |
|
| 1912 |
else |
| 1913 |
Stack_Op := True; |
| 1914 |
end if; |
| 1915 |
end if; |
| 1916 |
|
| 1917 |
-- Remove duplicate -shared-libgcc switches |
| 1918 |
|
| 1919 |
if Linker_Options.Table (J).all = Shared_Libgcc_String then |
| 1920 |
if Shared_Libgcc_Seen then |
| 1921 |
Linker_Options.Table (J .. Linker_Options.Last - 1) := |
| 1922 |
Linker_Options.Table (J + 1 .. Linker_Options.Last); |
| 1923 |
Linker_Options.Decrement_Last; |
| 1924 |
Num_Args := Num_Args - 1; |
| 1925 |
|
| 1926 |
else |
| 1927 |
Shared_Libgcc_Seen := True; |
| 1928 |
end if; |
| 1929 |
end if; |
| 1930 |
|
| 1931 |
-- Remove duplicate -static-libgcc switches |
| 1932 |
|
| 1933 |
if Linker_Options.Table (J).all = Static_Libgcc_String then |
| 1934 |
if Static_Libgcc_Seen then |
| 1935 |
Linker_Options.Table (J .. Linker_Options.Last - 1) := |
| 1936 |
Linker_Options.Table (J + 1 .. Linker_Options.Last); |
| 1937 |
Linker_Options.Decrement_Last; |
| 1938 |
Num_Args := Num_Args - 1; |
| 1939 |
|
| 1940 |
else |
| 1941 |
Static_Libgcc_Seen := True; |
| 1942 |
end if; |
| 1943 |
end if; |
| 1944 |
|
| 1945 |
-- Here we just check for a canonical form that matches the |
| 1946 |
-- pragma Linker_Options set in the NT runtime. |
| 1947 |
|
| 1948 |
if Is_Prefix (Linker_Options.Table (J).all, "-Xlinker --stack=") |
| 1949 |
or else Is_Prefix (Linker_Options.Table (J).all, "-Wl,--stack=") |
| 1950 |
then |
| 1951 |
if Stack_Op then |
| 1952 |
Linker_Options.Table (J .. Linker_Options.Last - 1) := |
| 1953 |
Linker_Options.Table (J + 1 .. Linker_Options.Last); |
| 1954 |
Linker_Options.Decrement_Last; |
| 1955 |
Num_Args := Num_Args - 1; |
| 1956 |
|
| 1957 |
else |
| 1958 |
Stack_Op := True; |
| 1959 |
end if; |
| 1960 |
end if; |
| 1961 |
|
| 1962 |
J := J + 1; |
| 1963 |
end loop; |
| 1964 |
|
| 1965 |
if Linker_Path = Gcc_Path then |
| 1966 |
|
| 1967 |
-- For systems where the default is to link statically with |
| 1968 |
-- libgcc, if gcc is not called with -shared-libgcc, call it |
| 1969 |
-- with -static-libgcc, as there are some platforms where one |
| 1970 |
-- of these two switches is compulsory to link. |
| 1971 |
-- Don't push extra switches if we already saw one. |
| 1972 |
|
| 1973 |
if Shared_Libgcc_Default = 'T' |
| 1974 |
and then not Shared_Libgcc_Seen |
| 1975 |
and then not Static_Libgcc_Seen |
| 1976 |
then |
| 1977 |
Linker_Options.Increment_Last; |
| 1978 |
Linker_Options.Table (Linker_Options.Last) := Static_Libgcc; |
| 1979 |
Num_Args := Num_Args + 1; |
| 1980 |
end if; |
| 1981 |
|
| 1982 |
-- Likewise, the reverse. |
| 1983 |
|
| 1984 |
if Shared_Libgcc_Default = 'H' |
| 1985 |
and then not Static_Libgcc_Seen |
| 1986 |
and then not Shared_Libgcc_Seen |
| 1987 |
then |
| 1988 |
Linker_Options.Increment_Last; |
| 1989 |
Linker_Options.Table (Linker_Options.Last) := Shared_Libgcc; |
| 1990 |
Num_Args := Num_Args + 1; |
| 1991 |
end if; |
| 1992 |
end if; |
| 1993 |
end Clean_Link_Option_Set; |
| 1994 |
|
| 1995 |
-- Prepare arguments for call to linker |
| 1996 |
|
| 1997 |
Call_Linker : declare |
| 1998 |
Success : Boolean; |
| 1999 |
Args : Argument_List (1 .. Num_Args + 1); |
| 2000 |
Index : Integer := Args'First; |
| 2001 |
|
| 2002 |
begin |
| 2003 |
Args (Index) := Binder_Obj_File; |
| 2004 |
|
| 2005 |
-- Add the object files and any -largs libraries |
| 2006 |
|
| 2007 |
for J in Linker_Objects.First .. Linker_Objects.Last loop |
| 2008 |
Index := Index + 1; |
| 2009 |
Args (Index) := Linker_Objects.Table (J); |
| 2010 |
end loop; |
| 2011 |
|
| 2012 |
-- Add the linker options from the binder file |
| 2013 |
|
| 2014 |
for J in Linker_Options.First .. Linker_Options.Last loop |
| 2015 |
Index := Index + 1; |
| 2016 |
Args (Index) := Linker_Options.Table (J); |
| 2017 |
end loop; |
| 2018 |
|
| 2019 |
-- Finally add the libraries from the --GCC= switch |
| 2020 |
|
| 2021 |
for J in Gcc_Linker_Options.First .. Gcc_Linker_Options.Last loop |
| 2022 |
Index := Index + 1; |
| 2023 |
Args (Index) := Gcc_Linker_Options.Table (J); |
| 2024 |
end loop; |
| 2025 |
|
| 2026 |
if Verbose_Mode then |
| 2027 |
Write_Str (Linker_Path.all); |
| 2028 |
|
| 2029 |
for J in Args'Range loop |
| 2030 |
Write_Str (" "); |
| 2031 |
Write_Str (Args (J).all); |
| 2032 |
end loop; |
| 2033 |
|
| 2034 |
Write_Eol; |
| 2035 |
|
| 2036 |
-- If we are on very verbose mode (-v -v) and a response file |
| 2037 |
-- is used we display its content. |
| 2038 |
|
| 2039 |
if Very_Verbose_Mode and then Tname_FD /= Invalid_FD then |
| 2040 |
Write_Eol; |
| 2041 |
Write_Str ("Response file (" & |
| 2042 |
Tname (Tname'First .. Tname'Last - 1) & |
| 2043 |
") content : "); |
| 2044 |
Write_Eol; |
| 2045 |
|
| 2046 |
for J in |
| 2047 |
Response_File_Objects.First .. Response_File_Objects.Last |
| 2048 |
loop |
| 2049 |
Write_Str (Response_File_Objects.Table (J).all); |
| 2050 |
Write_Eol; |
| 2051 |
end loop; |
| 2052 |
|
| 2053 |
Write_Eol; |
| 2054 |
end if; |
| 2055 |
end if; |
| 2056 |
|
| 2057 |
System.OS_Lib.Spawn (Linker_Path.all, Args, Success); |
| 2058 |
|
| 2059 |
-- Delete the temporary file used in conjunction with linking if one |
| 2060 |
-- was created. See Process_Bind_File for details. |
| 2061 |
|
| 2062 |
if Tname_FD /= Invalid_FD then |
| 2063 |
Delete (Tname); |
| 2064 |
end if; |
| 2065 |
|
| 2066 |
if not Success then |
| 2067 |
Error_Msg ("error when calling " & Linker_Path.all); |
| 2068 |
Exit_Program (E_Fatal); |
| 2069 |
end if; |
| 2070 |
end Call_Linker; |
| 2071 |
end Link_Step; |
| 2072 |
|
| 2073 |
-- Only keep the binder output file and it's associated object |
| 2074 |
-- file if compiling with the -g option. These files are only |
| 2075 |
-- useful if debugging. |
| 2076 |
|
| 2077 |
if not Debug_Flag_Present then |
| 2078 |
Delete (Binder_Ali_File.all & ASCII.NUL); |
| 2079 |
Delete (Binder_Spec_Src_File.all & ASCII.NUL); |
| 2080 |
Delete (Binder_Body_Src_File.all & ASCII.NUL); |
| 2081 |
Delete (Binder_Obj_File.all & ASCII.NUL); |
| 2082 |
end if; |
| 2083 |
|
| 2084 |
Exit_Program (E_Success); |
| 2085 |
|
| 2086 |
exception |
| 2087 |
when X : others => |
| 2088 |
Write_Line (Exception_Information (X)); |
| 2089 |
Exit_With_Error ("INTERNAL ERROR. Please report"); |
| 2090 |
end Gnatlink; |