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