* ldgram.y (lang_memory_region_type): Make static.
[deliverable/binutils-gdb.git] / ld / ldfile.c
CommitLineData
a9998805 1/* Linker file opening and searching.
5e2f1575 2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002,
aef6203b 3 2003, 2004, 2005 Free Software Foundation, Inc.
252b5132 4
3fe38064 5 This file is part of GLD, the Gnu Linker.
252b5132 6
3fe38064
NC
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
252b5132 11
3fe38064
NC
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
3fe38064
NC
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
252b5132 21
1c64c4ed 22/* ldfile.c: look after all the file stuff. */
252b5132
RH
23
24#include "bfd.h"
25#include "sysdep.h"
26#include "bfdlink.h"
3882b010 27#include "safe-ctype.h"
252b5132
RH
28#include "ld.h"
29#include "ldmisc.h"
30#include "ldexp.h"
31#include "ldlang.h"
32#include "ldfile.h"
33#include "ldmain.h"
df2a7313 34#include <ldgram.h>
252b5132
RH
35#include "ldlex.h"
36#include "ldemul.h"
d1b2b2dc 37#include "libiberty.h"
3fe38064 38#include "filenames.h"
252b5132 39
3fe38064
NC
40const char * ldfile_input_filename;
41bfd_boolean ldfile_assumed_script = FALSE;
42const char * ldfile_output_machine_name = "";
252b5132
RH
43unsigned long ldfile_output_machine;
44enum bfd_architecture ldfile_output_architecture;
3fe38064 45search_dirs_type * search_head;
252b5132 46
252b5132 47#ifdef VMS
279e75dc 48static char * slash = "";
252b5132
RH
49#else
50#if defined (_WIN32) && ! defined (__CYGWIN32__)
279e75dc 51static char * slash = "\\";
252b5132 52#else
279e75dc 53static char * slash = "/";
252b5132
RH
54#endif
55#endif
252b5132 56
3fe38064
NC
57typedef struct search_arch
58{
4de2d33d 59 char *name;
252b5132
RH
60 struct search_arch *next;
61} search_arch_type;
62
3fe38064 63static search_dirs_type **search_tail_ptr = &search_head;
252b5132
RH
64static search_arch_type *search_arch_head;
65static search_arch_type **search_arch_tail_ptr = &search_arch_head;
4de2d33d 66
3fe38064
NC
67/* Test whether a pathname, after canonicalization, is the same or a
68 sub-directory of the sysroot directory. */
69
70static bfd_boolean
1579bae1 71is_sysrooted_pathname (const char *name, bfd_boolean notsame)
3fe38064
NC
72{
73 char * realname = ld_canon_sysroot ? lrealpath (name) : NULL;
74 int len;
75 bfd_boolean result;
76
77 if (! realname)
78 return FALSE;
5e2f1575 79
3fe38064
NC
80 len = strlen (realname);
81
82 if (((! notsame && len == ld_canon_sysroot_len)
83 || (len >= ld_canon_sysroot_len
84 && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len])
85 && (realname[ld_canon_sysroot_len] = '\0') == '\0'))
86 && FILENAME_CMP (ld_canon_sysroot, realname) == 0)
87 result = TRUE;
88 else
89 result = FALSE;
90
91 if (realname)
92 free (realname);
93
94 return result;
95}
252b5132 96
5ed6aba4
NC
97/* Adds NAME to the library search path.
98 Makes a copy of NAME using xmalloc(). */
99
252b5132 100void
1579bae1 101ldfile_add_library_path (const char *name, bfd_boolean cmdline)
252b5132
RH
102{
103 search_dirs_type *new;
104
361b220e
CD
105 if (!cmdline && config.only_cmd_line_lib_dirs)
106 return;
107
1579bae1 108 new = xmalloc (sizeof (search_dirs_type));
252b5132 109 new->next = NULL;
252b5132
RH
110 new->cmdline = cmdline;
111 *search_tail_ptr = new;
112 search_tail_ptr = &new->next;
9c8ebd6a
DJ
113
114 /* If a directory is marked as honoring sysroot, prepend the sysroot path
115 now. */
5ed6aba4 116 if (name[0] == '=')
e3f2db7f 117 {
5ed6aba4 118 new->name = concat (ld_sysroot, name + 1, NULL);
e3f2db7f
AO
119 new->sysrooted = TRUE;
120 }
121 else
5ed6aba4
NC
122 {
123 new->name = xstrdup (name);
124 new->sysrooted = is_sysrooted_pathname (name, FALSE);
125 }
252b5132
RH
126}
127
128/* Try to open a BFD for a lang_input_statement. */
129
b34976b6 130bfd_boolean
1579bae1
AM
131ldfile_try_open_bfd (const char *attempt,
132 lang_input_statement_type *entry)
252b5132
RH
133{
134 entry->the_bfd = bfd_openr (attempt, entry->target);
135
136 if (trace_file_tries)
137 {
138 if (entry->the_bfd == NULL)
139 info_msg (_("attempt to open %s failed\n"), attempt);
140 else
141 info_msg (_("attempt to open %s succeeded\n"), attempt);
142 }
143
b90d1146 144 if (entry->the_bfd == NULL)
252b5132
RH
145 {
146 if (bfd_get_error () == bfd_error_invalid_target)
147 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
b34976b6 148 return FALSE;
252b5132 149 }
b90d1146
ILT
150
151 /* If we are searching for this file, see if the architecture is
152 compatible with the output file. If it isn't, keep searching.
153 If we can't open the file as an object file, stop the search
6c0c5b1e
AM
154 here. If we are statically linking, ensure that we don't link
155 a dynamic object. */
b90d1146 156
6c0c5b1e 157 if (entry->search_dirs_flag || !entry->dynamic)
b90d1146
ILT
158 {
159 bfd *check;
160
161 if (bfd_check_format (entry->the_bfd, bfd_archive))
162 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
163 else
164 check = entry->the_bfd;
165
a9998805 166 if (check != NULL)
b90d1146 167 {
a9998805 168 if (! bfd_check_format (check, bfd_object))
599917b8
JJ
169 {
170 if (check == entry->the_bfd
6c0c5b1e 171 && entry->search_dirs_flag
599917b8
JJ
172 && bfd_get_error () == bfd_error_file_not_recognized
173 && ! ldemul_unrecognized_file (entry))
174 {
175 int token, skip = 0;
176 char *arg, *arg1, *arg2, *arg3;
177 extern FILE *yyin;
178
179 /* Try to interpret the file as a linker script. */
180 ldfile_open_command_file (attempt);
b34976b6
AM
181
182 ldfile_assumed_script = TRUE;
599917b8
JJ
183 parser_input = input_selected;
184 ldlex_both ();
185 token = INPUT_SCRIPT;
186 while (token != 0)
187 {
188 switch (token)
189 {
190 case OUTPUT_FORMAT:
191 if ((token = yylex ()) != '(')
192 continue;
193 if ((token = yylex ()) != NAME)
194 continue;
195 arg1 = yylval.name;
196 arg2 = NULL;
197 arg3 = NULL;
198 token = yylex ();
199 if (token == ',')
200 {
201 if ((token = yylex ()) != NAME)
202 {
203 free (arg1);
204 continue;
205 }
206 arg2 = yylval.name;
207 if ((token = yylex ()) != ','
208 || (token = yylex ()) != NAME)
209 {
210 free (arg1);
211 free (arg2);
212 continue;
213 }
214 arg3 = yylval.name;
215 token = yylex ();
216 }
217 if (token == ')')
218 {
219 switch (command_line.endian)
220 {
221 default:
222 case ENDIAN_UNSET:
223 arg = arg1; break;
224 case ENDIAN_BIG:
225 arg = arg2 ? arg2 : arg1; break;
226 case ENDIAN_LITTLE:
227 arg = arg3 ? arg3 : arg1; break;
228 }
229 if (strcmp (arg, lang_get_output_target ()) != 0)
230 skip = 1;
231 }
232 free (arg1);
233 if (arg2) free (arg2);
234 if (arg3) free (arg3);
235 break;
236 case NAME:
237 case LNAME:
238 case VERS_IDENTIFIER:
239 case VERS_TAG:
240 free (yylval.name);
241 break;
242 case INT:
243 if (yylval.bigint.str)
244 free (yylval.bigint.str);
245 break;
5e2f1575 246 }
599917b8
JJ
247 token = yylex ();
248 }
4f39e302 249 ldlex_popstate ();
b34976b6 250 ldfile_assumed_script = FALSE;
599917b8
JJ
251 fclose (yyin);
252 yyin = NULL;
253 if (skip)
254 {
255 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
256 attempt, entry->local_sym_name);
257 bfd_close (entry->the_bfd);
258 entry->the_bfd = NULL;
b34976b6 259 return FALSE;
599917b8
JJ
260 }
261 }
b34976b6 262 return TRUE;
599917b8 263 }
f1f0d9ab 264
6c0c5b1e
AM
265 if (!entry->dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
266 {
267 einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
268 attempt);
269 bfd_close (entry->the_bfd);
270 entry->the_bfd = NULL;
271 return FALSE;
272 }
273
274 if (entry->search_dirs_flag
275 && !bfd_arch_get_compatible (check, output_bfd,
276 command_line.accept_unknown_input_arch)
312b768e 277 /* XCOFF archives can have 32 and 64 bit objects. */
f1f0d9ab 278 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
71daf8b4 279 && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
f1f0d9ab 280 && bfd_check_format (entry->the_bfd, bfd_archive)))
a9998805 281 {
1c64c4ed 282 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
a9998805
ILT
283 attempt, entry->local_sym_name);
284 bfd_close (entry->the_bfd);
285 entry->the_bfd = NULL;
b34976b6 286 return FALSE;
a9998805 287 }
b90d1146
ILT
288 }
289 }
290
b34976b6 291 return TRUE;
252b5132
RH
292}
293
294/* Search for and open the file specified by ENTRY. If it is an
295 archive, use ARCH, LIB and SUFFIX to modify the file name. */
296
b34976b6 297bfd_boolean
1579bae1
AM
298ldfile_open_file_search (const char *arch,
299 lang_input_statement_type *entry,
300 const char *lib,
301 const char *suffix)
252b5132
RH
302{
303 search_dirs_type *search;
304
305 /* If this is not an archive, try to open it in the current
306 directory first. */
307 if (! entry->is_archive)
308 {
3fe38064 309 if (entry->sysrooted && IS_ABSOLUTE_PATH (entry->filename))
e3f2db7f
AO
310 {
311 char *name = concat (ld_sysroot, entry->filename,
312 (const char *) NULL);
313 if (ldfile_try_open_bfd (name, entry))
314 {
315 entry->filename = name;
316 return TRUE;
317 }
318 free (name);
319 }
320 else if (ldfile_try_open_bfd (entry->filename, entry))
321 {
3fe38064
NC
322 entry->sysrooted = IS_ABSOLUTE_PATH (entry->filename)
323 && is_sysrooted_pathname (entry->filename, TRUE);
e3f2db7f
AO
324 return TRUE;
325 }
3fe38064
NC
326
327 if (IS_ABSOLUTE_PATH (entry->filename))
328 return FALSE;
252b5132
RH
329 }
330
1579bae1 331 for (search = search_head; search != NULL; search = search->next)
252b5132
RH
332 {
333 char *string;
334
1049f94e 335 if (entry->dynamic && ! link_info.relocatable)
252b5132
RH
336 {
337 if (ldemul_open_dynamic_archive (arch, search, entry))
e3f2db7f
AO
338 {
339 entry->sysrooted = search->sysrooted;
340 return TRUE;
341 }
252b5132
RH
342 }
343
1579bae1
AM
344 string = xmalloc (strlen (search->name)
345 + strlen (slash)
346 + strlen (lib)
347 + strlen (entry->filename)
348 + strlen (arch)
349 + strlen (suffix)
350 + 1);
252b5132
RH
351
352 if (entry->is_archive)
353 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
354 lib, entry->filename, arch, suffix);
252b5132
RH
355 else
356 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
357
358 if (ldfile_try_open_bfd (string, entry))
359 {
b90d1146 360 entry->filename = string;
e3f2db7f 361 entry->sysrooted = search->sysrooted;
b34976b6 362 return TRUE;
252b5132
RH
363 }
364
365 free (string);
366 }
367
b34976b6 368 return FALSE;
252b5132
RH
369}
370
371/* Open the input file specified by ENTRY. */
372
373void
1579bae1 374ldfile_open_file (lang_input_statement_type *entry)
252b5132
RH
375{
376 if (entry->the_bfd != NULL)
377 return;
378
379 if (! entry->search_dirs_flag)
380 {
381 if (ldfile_try_open_bfd (entry->filename, entry))
382 return;
383 if (strcmp (entry->filename, entry->local_sym_name) != 0)
f24ddbdd 384 einfo (_("%F%P: %s (%s): No such file: %E\n"),
252b5132
RH
385 entry->filename, entry->local_sym_name);
386 else
f24ddbdd 387 einfo (_("%F%P: %s: No such file: %E\n"), entry->local_sym_name);
252b5132
RH
388 }
389 else
390 {
391 search_arch_type *arch;
b34976b6 392 bfd_boolean found = FALSE;
252b5132
RH
393
394 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
1579bae1 395 for (arch = search_arch_head; arch != NULL; arch = arch->next)
252b5132 396 {
78f85fd7
L
397 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
398 if (found)
399 break;
252b5132 400#ifdef VMS
78f85fd7
L
401 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
402 if (found)
403 break;
252b5132 404#endif
78f85fd7
L
405 found = ldemul_find_potential_libraries (arch->name, entry);
406 if (found)
407 break;
252b5132 408 }
4de2d33d 409
78f85fd7
L
410 /* If we have found the file, we don't need to search directories
411 again. */
412 if (found)
b34976b6 413 entry->search_dirs_flag = FALSE;
3fe38064
NC
414 else if (entry->sysrooted
415 && ld_sysroot
416 && IS_ABSOLUTE_PATH (entry->local_sym_name))
417 einfo (_("%F%P: cannot find %s inside %s\n"),
418 entry->local_sym_name, ld_sysroot);
78f85fd7
L
419 else
420 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
252b5132
RH
421 }
422}
423
424/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
425
426static FILE *
1579bae1 427try_open (const char *name, const char *exten)
252b5132
RH
428{
429 FILE *result;
430 char buff[1000];
431
432 result = fopen (name, "r");
4de2d33d 433
252b5132
RH
434 if (trace_file_tries)
435 {
436 if (result == NULL)
437 info_msg (_("cannot find script file %s\n"), name);
438 else
439 info_msg (_("opened script file %s\n"), name);
440 }
441
442 if (result != NULL)
443 return result;
444
445 if (*exten)
446 {
447 sprintf (buff, "%s%s", name, exten);
448 result = fopen (buff, "r");
4de2d33d 449
252b5132
RH
450 if (trace_file_tries)
451 {
452 if (result == NULL)
453 info_msg (_("cannot find script file %s\n"), buff);
454 else
455 info_msg (_("opened script file %s\n"), buff);
456 }
457 }
458
459 return result;
460}
461
462/* Try to open NAME; if that fails, look for it in any directories
396a2467 463 specified with -L, without and with EXTEND appended. */
252b5132 464
279e75dc 465static FILE *
1579bae1 466ldfile_find_command_file (const char *name, const char *extend)
252b5132
RH
467{
468 search_dirs_type *search;
469 FILE *result;
470 char buffer[1000];
471
89cdebba 472 /* First try raw name. */
1c64c4ed 473 result = try_open (name, "");
1579bae1 474 if (result == NULL)
1c64c4ed 475 {
89cdebba 476 /* Try now prefixes. */
1579bae1 477 for (search = search_head; search != NULL; search = search->next)
1c64c4ed 478 {
89cdebba 479 sprintf (buffer, "%s%s%s", search->name, slash, name);
4de2d33d 480
1c64c4ed
NC
481 result = try_open (buffer, extend);
482 if (result)
483 break;
484 }
252b5132 485 }
4de2d33d 486
252b5132
RH
487 return result;
488}
489
490void
1579bae1 491ldfile_open_command_file (const char *name)
252b5132
RH
492{
493 FILE *ldlex_input_stack;
1c64c4ed 494 ldlex_input_stack = ldfile_find_command_file (name, "");
252b5132 495
1579bae1 496 if (ldlex_input_stack == NULL)
1c64c4ed
NC
497 {
498 bfd_set_error (bfd_error_system_call);
499 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
500 }
4de2d33d 501
1c64c4ed 502 lex_push_file (ldlex_input_stack, name);
4de2d33d 503
252b5132
RH
504 ldfile_input_filename = name;
505 lineno = 1;
b7a26f91 506
b9a8de1e 507 saved_script_handle = ldlex_input_stack;
252b5132
RH
508}
509
252b5132 510void
1579bae1 511ldfile_add_arch (const char *in_name)
252b5132 512{
d1b2b2dc 513 char *name = xstrdup (in_name);
1579bae1 514 search_arch_type *new = xmalloc (sizeof (search_arch_type));
252b5132
RH
515
516 ldfile_output_machine_name = in_name;
517
518 new->name = name;
1579bae1 519 new->next = NULL;
252b5132
RH
520 while (*name)
521 {
3882b010 522 *name = TOLOWER (*name);
252b5132
RH
523 name++;
524 }
525 *search_arch_tail_ptr = new;
526 search_arch_tail_ptr = &new->next;
527
528}
252b5132 529
89cdebba
KH
530/* Set the output architecture. */
531
252b5132 532void
5e2f1575 533ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
252b5132 534{
1c64c4ed
NC
535 const bfd_arch_info_type *arch = bfd_scan_arch (string);
536
537 if (arch)
538 {
539 ldfile_output_architecture = arch->arch;
540 ldfile_output_machine = arch->mach;
541 ldfile_output_machine_name = arch->printable_name;
542 }
5e2f1575
AM
543 else if (defarch != bfd_arch_unknown)
544 ldfile_output_architecture = defarch;
1c64c4ed 545 else
5e2f1575 546 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
252b5132 547}
This page took 0.309088 seconds and 4 git commands to generate.