* mips-tdep.c (mips16_scan_prologue): Remove redundant code.
[deliverable/binutils-gdb.git] / ld / ldfile.c
CommitLineData
a9998805 1/* Linker file opening and searching.
5e2f1575
AM
2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004 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
3fe38064 48char * slash = "";
252b5132
RH
49#else
50#if defined (_WIN32) && ! defined (__CYGWIN32__)
3fe38064 51char * slash = "\\";
252b5132 52#else
3fe38064 53char * 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
154 here. */
155
156 if (entry->search_dirs_flag)
157 {
158 bfd *check;
159
160 if (bfd_check_format (entry->the_bfd, bfd_archive))
161 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
162 else
163 check = entry->the_bfd;
164
a9998805 165 if (check != NULL)
b90d1146 166 {
a9998805 167 if (! bfd_check_format (check, bfd_object))
599917b8
JJ
168 {
169 if (check == entry->the_bfd
170 && bfd_get_error () == bfd_error_file_not_recognized
171 && ! ldemul_unrecognized_file (entry))
172 {
173 int token, skip = 0;
174 char *arg, *arg1, *arg2, *arg3;
175 extern FILE *yyin;
176
177 /* Try to interpret the file as a linker script. */
178 ldfile_open_command_file (attempt);
b34976b6
AM
179
180 ldfile_assumed_script = TRUE;
599917b8
JJ
181 parser_input = input_selected;
182 ldlex_both ();
183 token = INPUT_SCRIPT;
184 while (token != 0)
185 {
186 switch (token)
187 {
188 case OUTPUT_FORMAT:
189 if ((token = yylex ()) != '(')
190 continue;
191 if ((token = yylex ()) != NAME)
192 continue;
193 arg1 = yylval.name;
194 arg2 = NULL;
195 arg3 = NULL;
196 token = yylex ();
197 if (token == ',')
198 {
199 if ((token = yylex ()) != NAME)
200 {
201 free (arg1);
202 continue;
203 }
204 arg2 = yylval.name;
205 if ((token = yylex ()) != ','
206 || (token = yylex ()) != NAME)
207 {
208 free (arg1);
209 free (arg2);
210 continue;
211 }
212 arg3 = yylval.name;
213 token = yylex ();
214 }
215 if (token == ')')
216 {
217 switch (command_line.endian)
218 {
219 default:
220 case ENDIAN_UNSET:
221 arg = arg1; break;
222 case ENDIAN_BIG:
223 arg = arg2 ? arg2 : arg1; break;
224 case ENDIAN_LITTLE:
225 arg = arg3 ? arg3 : arg1; break;
226 }
227 if (strcmp (arg, lang_get_output_target ()) != 0)
228 skip = 1;
229 }
230 free (arg1);
231 if (arg2) free (arg2);
232 if (arg3) free (arg3);
233 break;
234 case NAME:
235 case LNAME:
236 case VERS_IDENTIFIER:
237 case VERS_TAG:
238 free (yylval.name);
239 break;
240 case INT:
241 if (yylval.bigint.str)
242 free (yylval.bigint.str);
243 break;
5e2f1575 244 }
599917b8
JJ
245 token = yylex ();
246 }
4f39e302 247 ldlex_popstate ();
b34976b6 248 ldfile_assumed_script = FALSE;
599917b8
JJ
249 fclose (yyin);
250 yyin = NULL;
251 if (skip)
252 {
253 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
254 attempt, entry->local_sym_name);
255 bfd_close (entry->the_bfd);
256 entry->the_bfd = NULL;
b34976b6 257 return FALSE;
599917b8
JJ
258 }
259 }
b34976b6 260 return TRUE;
599917b8 261 }
f1f0d9ab 262
312b768e
NC
263 if ((bfd_arch_get_compatible (check, output_bfd,
264 command_line.accept_unknown_input_arch) == NULL)
265 /* XCOFF archives can have 32 and 64 bit objects. */
f1f0d9ab 266 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
71daf8b4 267 && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
f1f0d9ab 268 && bfd_check_format (entry->the_bfd, bfd_archive)))
a9998805 269 {
1c64c4ed 270 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
a9998805
ILT
271 attempt, entry->local_sym_name);
272 bfd_close (entry->the_bfd);
273 entry->the_bfd = NULL;
b34976b6 274 return FALSE;
a9998805 275 }
b90d1146
ILT
276 }
277 }
278
b34976b6 279 return TRUE;
252b5132
RH
280}
281
282/* Search for and open the file specified by ENTRY. If it is an
283 archive, use ARCH, LIB and SUFFIX to modify the file name. */
284
b34976b6 285bfd_boolean
1579bae1
AM
286ldfile_open_file_search (const char *arch,
287 lang_input_statement_type *entry,
288 const char *lib,
289 const char *suffix)
252b5132
RH
290{
291 search_dirs_type *search;
292
293 /* If this is not an archive, try to open it in the current
294 directory first. */
295 if (! entry->is_archive)
296 {
3fe38064 297 if (entry->sysrooted && IS_ABSOLUTE_PATH (entry->filename))
e3f2db7f
AO
298 {
299 char *name = concat (ld_sysroot, entry->filename,
300 (const char *) NULL);
301 if (ldfile_try_open_bfd (name, entry))
302 {
303 entry->filename = name;
304 return TRUE;
305 }
306 free (name);
307 }
308 else if (ldfile_try_open_bfd (entry->filename, entry))
309 {
3fe38064
NC
310 entry->sysrooted = IS_ABSOLUTE_PATH (entry->filename)
311 && is_sysrooted_pathname (entry->filename, TRUE);
e3f2db7f
AO
312 return TRUE;
313 }
3fe38064
NC
314
315 if (IS_ABSOLUTE_PATH (entry->filename))
316 return FALSE;
252b5132
RH
317 }
318
1579bae1 319 for (search = search_head; search != NULL; search = search->next)
252b5132
RH
320 {
321 char *string;
322
1049f94e 323 if (entry->dynamic && ! link_info.relocatable)
252b5132
RH
324 {
325 if (ldemul_open_dynamic_archive (arch, search, entry))
e3f2db7f
AO
326 {
327 entry->sysrooted = search->sysrooted;
328 return TRUE;
329 }
252b5132
RH
330 }
331
1579bae1
AM
332 string = xmalloc (strlen (search->name)
333 + strlen (slash)
334 + strlen (lib)
335 + strlen (entry->filename)
336 + strlen (arch)
337 + strlen (suffix)
338 + 1);
252b5132
RH
339
340 if (entry->is_archive)
341 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
342 lib, entry->filename, arch, suffix);
252b5132
RH
343 else
344 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
345
346 if (ldfile_try_open_bfd (string, entry))
347 {
b90d1146 348 entry->filename = string;
e3f2db7f 349 entry->sysrooted = search->sysrooted;
b34976b6 350 return TRUE;
252b5132
RH
351 }
352
353 free (string);
354 }
355
b34976b6 356 return FALSE;
252b5132
RH
357}
358
359/* Open the input file specified by ENTRY. */
360
361void
1579bae1 362ldfile_open_file (lang_input_statement_type *entry)
252b5132
RH
363{
364 if (entry->the_bfd != NULL)
365 return;
366
367 if (! entry->search_dirs_flag)
368 {
369 if (ldfile_try_open_bfd (entry->filename, entry))
370 return;
371 if (strcmp (entry->filename, entry->local_sym_name) != 0)
f24ddbdd 372 einfo (_("%F%P: %s (%s): No such file: %E\n"),
252b5132
RH
373 entry->filename, entry->local_sym_name);
374 else
f24ddbdd 375 einfo (_("%F%P: %s: No such file: %E\n"), entry->local_sym_name);
252b5132
RH
376 }
377 else
378 {
379 search_arch_type *arch;
b34976b6 380 bfd_boolean found = FALSE;
252b5132
RH
381
382 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
1579bae1 383 for (arch = search_arch_head; arch != NULL; arch = arch->next)
252b5132 384 {
78f85fd7
L
385 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
386 if (found)
387 break;
252b5132 388#ifdef VMS
78f85fd7
L
389 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
390 if (found)
391 break;
252b5132 392#endif
78f85fd7
L
393 found = ldemul_find_potential_libraries (arch->name, entry);
394 if (found)
395 break;
252b5132 396 }
4de2d33d 397
78f85fd7
L
398 /* If we have found the file, we don't need to search directories
399 again. */
400 if (found)
b34976b6 401 entry->search_dirs_flag = FALSE;
3fe38064
NC
402 else if (entry->sysrooted
403 && ld_sysroot
404 && IS_ABSOLUTE_PATH (entry->local_sym_name))
405 einfo (_("%F%P: cannot find %s inside %s\n"),
406 entry->local_sym_name, ld_sysroot);
78f85fd7
L
407 else
408 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
252b5132
RH
409 }
410}
411
412/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
413
414static FILE *
1579bae1 415try_open (const char *name, const char *exten)
252b5132
RH
416{
417 FILE *result;
418 char buff[1000];
419
420 result = fopen (name, "r");
4de2d33d 421
252b5132
RH
422 if (trace_file_tries)
423 {
424 if (result == NULL)
425 info_msg (_("cannot find script file %s\n"), name);
426 else
427 info_msg (_("opened script file %s\n"), name);
428 }
429
430 if (result != NULL)
431 return result;
432
433 if (*exten)
434 {
435 sprintf (buff, "%s%s", name, exten);
436 result = fopen (buff, "r");
4de2d33d 437
252b5132
RH
438 if (trace_file_tries)
439 {
440 if (result == NULL)
441 info_msg (_("cannot find script file %s\n"), buff);
442 else
443 info_msg (_("opened script file %s\n"), buff);
444 }
445 }
446
447 return result;
448}
449
450/* Try to open NAME; if that fails, look for it in any directories
396a2467 451 specified with -L, without and with EXTEND appended. */
252b5132
RH
452
453FILE *
1579bae1 454ldfile_find_command_file (const char *name, const char *extend)
252b5132
RH
455{
456 search_dirs_type *search;
457 FILE *result;
458 char buffer[1000];
459
89cdebba 460 /* First try raw name. */
1c64c4ed 461 result = try_open (name, "");
1579bae1 462 if (result == NULL)
1c64c4ed 463 {
89cdebba 464 /* Try now prefixes. */
1579bae1 465 for (search = search_head; search != NULL; search = search->next)
1c64c4ed 466 {
89cdebba 467 sprintf (buffer, "%s%s%s", search->name, slash, name);
4de2d33d 468
1c64c4ed
NC
469 result = try_open (buffer, extend);
470 if (result)
471 break;
472 }
252b5132 473 }
4de2d33d 474
252b5132
RH
475 return result;
476}
477
478void
1579bae1 479ldfile_open_command_file (const char *name)
252b5132
RH
480{
481 FILE *ldlex_input_stack;
1c64c4ed 482 ldlex_input_stack = ldfile_find_command_file (name, "");
252b5132 483
1579bae1 484 if (ldlex_input_stack == NULL)
1c64c4ed
NC
485 {
486 bfd_set_error (bfd_error_system_call);
487 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
488 }
4de2d33d 489
1c64c4ed 490 lex_push_file (ldlex_input_stack, name);
4de2d33d 491
252b5132
RH
492 ldfile_input_filename = name;
493 lineno = 1;
b7a26f91 494
b9a8de1e 495 saved_script_handle = ldlex_input_stack;
252b5132
RH
496}
497
252b5132 498#ifdef GNU960
1c64c4ed 499static char *
1579bae1 500gnu960_map_archname (char *name)
252b5132
RH
501{
502 struct tabentry { char *cmd_switch; char *arch; };
1c64c4ed
NC
503 static struct tabentry arch_tab[] =
504 {
252b5132
RH
505 "", "",
506 "KA", "ka",
507 "KB", "kb",
508 "KC", "mc", /* Synonym for MC */
509 "MC", "mc",
510 "CA", "ca",
511 "SA", "ka", /* Functionally equivalent to KA */
512 "SB", "kb", /* Functionally equivalent to KB */
513 NULL, ""
514 };
515 struct tabentry *tp;
252b5132 516
1c64c4ed
NC
517 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
518 {
519 if (! strcmp (name,tp->cmd_switch))
520 break;
252b5132 521 }
252b5132 522
1c64c4ed 523 if (tp->cmd_switch == NULL)
89cdebba 524 einfo (_("%P%F: unknown architecture: %s\n"), name);
4de2d33d 525
252b5132
RH
526 return tp->arch;
527}
528
252b5132 529void
1579bae1 530ldfile_add_arch (char *name)
252b5132 531{
1579bae1 532 search_arch_type *new = xmalloc (sizeof (search_arch_type));
252b5132 533
1c64c4ed
NC
534 if (*name != '\0')
535 {
536 if (ldfile_output_machine_name[0] != '\0')
537 {
538 einfo (_("%P%F: target architecture respecified\n"));
539 return;
540 }
4de2d33d 541
1c64c4ed 542 ldfile_output_machine_name = name;
252b5132 543 }
252b5132 544
1579bae1 545 new->next = NULL;
1c64c4ed 546 new->name = gnu960_map_archname (name);
252b5132
RH
547 *search_arch_tail_ptr = new;
548 search_arch_tail_ptr = &new->next;
252b5132
RH
549}
550
89cdebba 551#else /* not GNU960 */
252b5132 552
252b5132 553void
1579bae1 554ldfile_add_arch (const char *in_name)
252b5132 555{
d1b2b2dc 556 char *name = xstrdup (in_name);
1579bae1 557 search_arch_type *new = xmalloc (sizeof (search_arch_type));
252b5132
RH
558
559 ldfile_output_machine_name = in_name;
560
561 new->name = name;
1579bae1 562 new->next = NULL;
252b5132
RH
563 while (*name)
564 {
3882b010 565 *name = TOLOWER (*name);
252b5132
RH
566 name++;
567 }
568 *search_arch_tail_ptr = new;
569 search_arch_tail_ptr = &new->next;
570
571}
572#endif
573
89cdebba
KH
574/* Set the output architecture. */
575
252b5132 576void
5e2f1575 577ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
252b5132 578{
1c64c4ed
NC
579 const bfd_arch_info_type *arch = bfd_scan_arch (string);
580
581 if (arch)
582 {
583 ldfile_output_architecture = arch->arch;
584 ldfile_output_machine = arch->mach;
585 ldfile_output_machine_name = arch->printable_name;
586 }
5e2f1575
AM
587 else if (defarch != bfd_arch_unknown)
588 ldfile_output_architecture = defarch;
1c64c4ed 589 else
5e2f1575 590 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
252b5132 591}
This page took 0.257708 seconds and 4 git commands to generate.