* config/tc-sh.c (assemble_ppi): Initialize reg_x / reg_y / reg_n
[deliverable/binutils-gdb.git] / ld / ldfile.c
CommitLineData
a9998805 1/* Linker file opening and searching.
b7a26f91 2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002
a9998805 3 Free Software Foundation, Inc.
252b5132
RH
4
5This file is part of GLD, the Gnu Linker.
6
7GLD is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GLD is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GLD; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
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"
34#include "ldgram.h"
35#include "ldlex.h"
36#include "ldemul.h"
d1b2b2dc 37#include "libiberty.h"
252b5132 38
252b5132
RH
39const char *ldfile_input_filename;
40boolean ldfile_assumed_script = false;
41const char *ldfile_output_machine_name = "";
42unsigned long ldfile_output_machine;
43enum bfd_architecture ldfile_output_architecture;
44search_dirs_type *search_head;
45
46#ifndef MPW
47#ifdef VMS
48char *slash = "";
49#else
50#if defined (_WIN32) && ! defined (__CYGWIN32__)
51char *slash = "\\";
52#else
53char *slash = "/";
54#endif
55#endif
56#else /* MPW */
1c64c4ed 57/* The MPW path char is a colon. */
252b5132
RH
58char *slash = ":";
59#endif /* MPW */
60
61/* LOCAL */
62
63static search_dirs_type **search_tail_ptr = &search_head;
64
89cdebba 65typedef struct search_arch {
4de2d33d 66 char *name;
252b5132
RH
67 struct search_arch *next;
68} search_arch_type;
69
70static search_arch_type *search_arch_head;
71static search_arch_type **search_arch_tail_ptr = &search_arch_head;
4de2d33d 72
252b5132
RH
73static FILE *try_open PARAMS ((const char *name, const char *exten));
74
75void
76ldfile_add_library_path (name, cmdline)
77 const char *name;
78 boolean cmdline;
79{
80 search_dirs_type *new;
81
361b220e
CD
82 if (!cmdline && config.only_cmd_line_lib_dirs)
83 return;
84
252b5132
RH
85 new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
86 new->next = NULL;
87 new->name = name;
88 new->cmdline = cmdline;
89 *search_tail_ptr = new;
90 search_tail_ptr = &new->next;
91}
92
93/* Try to open a BFD for a lang_input_statement. */
94
95boolean
96ldfile_try_open_bfd (attempt, entry)
97 const char *attempt;
98 lang_input_statement_type *entry;
99{
100 entry->the_bfd = bfd_openr (attempt, entry->target);
101
102 if (trace_file_tries)
103 {
104 if (entry->the_bfd == NULL)
105 info_msg (_("attempt to open %s failed\n"), attempt);
106 else
107 info_msg (_("attempt to open %s succeeded\n"), attempt);
108 }
109
b90d1146 110 if (entry->the_bfd == NULL)
252b5132
RH
111 {
112 if (bfd_get_error () == bfd_error_invalid_target)
113 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
114 return false;
115 }
b90d1146
ILT
116
117 /* If we are searching for this file, see if the architecture is
118 compatible with the output file. If it isn't, keep searching.
119 If we can't open the file as an object file, stop the search
120 here. */
121
122 if (entry->search_dirs_flag)
123 {
124 bfd *check;
125
126 if (bfd_check_format (entry->the_bfd, bfd_archive))
127 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
128 else
129 check = entry->the_bfd;
130
a9998805 131 if (check != NULL)
b90d1146 132 {
a9998805
ILT
133 if (! bfd_check_format (check, bfd_object))
134 return true;
f1f0d9ab
TR
135
136 if ((bfd_arch_get_compatible (check, output_bfd) == NULL)
137 /* XCOFF archives can have 32 and 64 bit objects */
138 && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
71daf8b4 139 && bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour
f1f0d9ab 140 && bfd_check_format (entry->the_bfd, bfd_archive)))
a9998805 141 {
1c64c4ed 142 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
a9998805
ILT
143 attempt, entry->local_sym_name);
144 bfd_close (entry->the_bfd);
145 entry->the_bfd = NULL;
146 return false;
147 }
b90d1146
ILT
148 }
149 }
150
151 return true;
252b5132
RH
152}
153
154/* Search for and open the file specified by ENTRY. If it is an
155 archive, use ARCH, LIB and SUFFIX to modify the file name. */
156
344a211f 157boolean
252b5132
RH
158ldfile_open_file_search (arch, entry, lib, suffix)
159 const char *arch;
160 lang_input_statement_type *entry;
161 const char *lib;
162 const char *suffix;
163{
164 search_dirs_type *search;
165
166 /* If this is not an archive, try to open it in the current
167 directory first. */
168 if (! entry->is_archive)
169 {
170 if (ldfile_try_open_bfd (entry->filename, entry))
171 return true;
172 }
173
174 for (search = search_head;
89cdebba 175 search != (search_dirs_type *) NULL;
4de2d33d 176 search = search->next)
252b5132
RH
177 {
178 char *string;
179
180 if (entry->dynamic && ! link_info.relocateable)
181 {
182 if (ldemul_open_dynamic_archive (arch, search, entry))
183 return true;
184 }
185
186 string = (char *) xmalloc (strlen (search->name)
187 + strlen (slash)
188 + strlen (lib)
189 + strlen (entry->filename)
190 + strlen (arch)
191 + strlen (suffix)
192 + 1);
193
194 if (entry->is_archive)
195 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
196 lib, entry->filename, arch, suffix);
197 else if (entry->filename[0] == '/' || entry->filename[0] == '.'
198#if defined (__MSDOS__) || defined (_WIN32)
4de2d33d 199 || entry->filename[0] == '\\'
3882b010 200 || (ISALPHA (entry->filename[0])
252b5132
RH
201 && entry->filename[1] == ':')
202#endif
203 )
204 strcpy (string, entry->filename);
205 else
206 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
207
208 if (ldfile_try_open_bfd (string, entry))
209 {
b90d1146
ILT
210 entry->filename = string;
211 return true;
252b5132
RH
212 }
213
214 free (string);
215 }
216
217 return false;
218}
219
220/* Open the input file specified by ENTRY. */
221
222void
223ldfile_open_file (entry)
224 lang_input_statement_type *entry;
225{
226 if (entry->the_bfd != NULL)
227 return;
228
229 if (! entry->search_dirs_flag)
230 {
231 if (ldfile_try_open_bfd (entry->filename, entry))
232 return;
233 if (strcmp (entry->filename, entry->local_sym_name) != 0)
234 einfo (_("%F%P: cannot open %s for %s: %E\n"),
235 entry->filename, entry->local_sym_name);
236 else
1c64c4ed 237 einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
252b5132
RH
238 }
239 else
240 {
241 search_arch_type *arch;
78f85fd7 242 boolean found = false;
252b5132
RH
243
244 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
245 for (arch = search_arch_head;
246 arch != (search_arch_type *) NULL;
247 arch = arch->next)
248 {
78f85fd7
L
249 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
250 if (found)
251 break;
252b5132 252#ifdef VMS
78f85fd7
L
253 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
254 if (found)
255 break;
252b5132 256#endif
78f85fd7
L
257 found = ldemul_find_potential_libraries (arch->name, entry);
258 if (found)
259 break;
252b5132 260 }
4de2d33d 261
78f85fd7
L
262 /* If we have found the file, we don't need to search directories
263 again. */
264 if (found)
265 entry->search_dirs_flag = false;
266 else
267 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
252b5132
RH
268 }
269}
270
271/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
272
273static FILE *
274try_open (name, exten)
275 const char *name;
276 const char *exten;
277{
278 FILE *result;
279 char buff[1000];
280
281 result = fopen (name, "r");
4de2d33d 282
252b5132
RH
283 if (trace_file_tries)
284 {
285 if (result == NULL)
286 info_msg (_("cannot find script file %s\n"), name);
287 else
288 info_msg (_("opened script file %s\n"), name);
289 }
290
291 if (result != NULL)
292 return result;
293
294 if (*exten)
295 {
296 sprintf (buff, "%s%s", name, exten);
297 result = fopen (buff, "r");
4de2d33d 298
252b5132
RH
299 if (trace_file_tries)
300 {
301 if (result == NULL)
302 info_msg (_("cannot find script file %s\n"), buff);
303 else
304 info_msg (_("opened script file %s\n"), buff);
305 }
306 }
307
308 return result;
309}
310
311/* Try to open NAME; if that fails, look for it in any directories
312 specified with -L, without and with EXTEND apppended. */
313
314FILE *
315ldfile_find_command_file (name, extend)
316 const char *name;
317 const char *extend;
318{
319 search_dirs_type *search;
320 FILE *result;
321 char buffer[1000];
322
89cdebba 323 /* First try raw name. */
1c64c4ed 324 result = try_open (name, "");
89cdebba 325 if (result == (FILE *) NULL)
1c64c4ed 326 {
89cdebba 327 /* Try now prefixes. */
1c64c4ed 328 for (search = search_head;
89cdebba 329 search != (search_dirs_type *) NULL;
1c64c4ed
NC
330 search = search->next)
331 {
89cdebba 332 sprintf (buffer, "%s%s%s", search->name, slash, name);
4de2d33d 333
1c64c4ed
NC
334 result = try_open (buffer, extend);
335 if (result)
336 break;
337 }
252b5132 338 }
4de2d33d 339
252b5132
RH
340 return result;
341}
342
343void
344ldfile_open_command_file (name)
345 const char *name;
346{
347 FILE *ldlex_input_stack;
1c64c4ed 348 ldlex_input_stack = ldfile_find_command_file (name, "");
252b5132 349
89cdebba 350 if (ldlex_input_stack == (FILE *) NULL)
1c64c4ed
NC
351 {
352 bfd_set_error (bfd_error_system_call);
353 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
354 }
4de2d33d 355
1c64c4ed 356 lex_push_file (ldlex_input_stack, name);
4de2d33d 357
252b5132
RH
358 ldfile_input_filename = name;
359 lineno = 1;
b7a26f91 360
b9a8de1e 361 saved_script_handle = ldlex_input_stack;
252b5132
RH
362}
363
252b5132 364#ifdef GNU960
1c64c4ed
NC
365static char *
366gnu960_map_archname (name)
367 char *name;
252b5132
RH
368{
369 struct tabentry { char *cmd_switch; char *arch; };
1c64c4ed
NC
370 static struct tabentry arch_tab[] =
371 {
252b5132
RH
372 "", "",
373 "KA", "ka",
374 "KB", "kb",
375 "KC", "mc", /* Synonym for MC */
376 "MC", "mc",
377 "CA", "ca",
378 "SA", "ka", /* Functionally equivalent to KA */
379 "SB", "kb", /* Functionally equivalent to KB */
380 NULL, ""
381 };
382 struct tabentry *tp;
252b5132 383
1c64c4ed
NC
384 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
385 {
386 if (! strcmp (name,tp->cmd_switch))
387 break;
252b5132 388 }
252b5132 389
1c64c4ed 390 if (tp->cmd_switch == NULL)
89cdebba 391 einfo (_("%P%F: unknown architecture: %s\n"), name);
4de2d33d 392
252b5132
RH
393 return tp->arch;
394}
395
252b5132 396void
1c64c4ed
NC
397ldfile_add_arch (name)
398 char *name;
252b5132
RH
399{
400 search_arch_type *new =
89cdebba 401 (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type)));
252b5132 402
1c64c4ed
NC
403 if (*name != '\0')
404 {
405 if (ldfile_output_machine_name[0] != '\0')
406 {
407 einfo (_("%P%F: target architecture respecified\n"));
408 return;
409 }
4de2d33d 410
1c64c4ed 411 ldfile_output_machine_name = name;
252b5132 412 }
252b5132 413
89cdebba 414 new->next = (search_arch_type *) NULL;
1c64c4ed 415 new->name = gnu960_map_archname (name);
252b5132
RH
416 *search_arch_tail_ptr = new;
417 search_arch_tail_ptr = &new->next;
252b5132
RH
418}
419
89cdebba 420#else /* not GNU960 */
252b5132 421
252b5132
RH
422void
423ldfile_add_arch (in_name)
4da711b1 424 const char *in_name;
252b5132 425{
d1b2b2dc 426 char *name = xstrdup (in_name);
252b5132
RH
427 search_arch_type *new =
428 (search_arch_type *) xmalloc (sizeof (search_arch_type));
429
430 ldfile_output_machine_name = in_name;
431
432 new->name = name;
89cdebba 433 new->next = (search_arch_type *) NULL;
252b5132
RH
434 while (*name)
435 {
3882b010 436 *name = TOLOWER (*name);
252b5132
RH
437 name++;
438 }
439 *search_arch_tail_ptr = new;
440 search_arch_tail_ptr = &new->next;
441
442}
443#endif
444
89cdebba
KH
445/* Set the output architecture. */
446
252b5132
RH
447void
448ldfile_set_output_arch (string)
4da711b1 449 const char *string;
252b5132 450{
1c64c4ed
NC
451 const bfd_arch_info_type *arch = bfd_scan_arch (string);
452
453 if (arch)
454 {
455 ldfile_output_architecture = arch->arch;
456 ldfile_output_machine = arch->mach;
457 ldfile_output_machine_name = arch->printable_name;
458 }
459 else
460 {
461 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
462 }
252b5132 463}
This page took 0.155639 seconds and 4 git commands to generate.