]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ldfile.c
2000-10-09 Kazu Hirata <kazu@hxi.com>
[thirdparty/binutils-gdb.git] / ld / ldfile.c
CommitLineData
a9998805
ILT
1/* Linker file opening and searching.
2 Copyright (C) 1991, 92, 93, 94, 95, 98, 99, 2000
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"
27#include "ld.h"
28#include "ldmisc.h"
29#include "ldexp.h"
30#include "ldlang.h"
31#include "ldfile.h"
32#include "ldmain.h"
33#include "ldgram.h"
34#include "ldlex.h"
35#include "ldemul.h"
36
37#include <ctype.h>
38
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
4de2d33d 65typedef struct search_arch
252b5132 66{
4de2d33d 67 char *name;
252b5132
RH
68 struct search_arch *next;
69} search_arch_type;
70
71static search_arch_type *search_arch_head;
72static search_arch_type **search_arch_tail_ptr = &search_arch_head;
4de2d33d 73
252b5132
RH
74static FILE *try_open PARAMS ((const char *name, const char *exten));
75
76void
77ldfile_add_library_path (name, cmdline)
78 const char *name;
79 boolean cmdline;
80{
81 search_dirs_type *new;
82
83 new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
84 new->next = NULL;
85 new->name = name;
86 new->cmdline = cmdline;
87 *search_tail_ptr = new;
88 search_tail_ptr = &new->next;
89}
90
91/* Try to open a BFD for a lang_input_statement. */
92
93boolean
94ldfile_try_open_bfd (attempt, entry)
95 const char *attempt;
96 lang_input_statement_type *entry;
97{
98 entry->the_bfd = bfd_openr (attempt, entry->target);
99
100 if (trace_file_tries)
101 {
102 if (entry->the_bfd == NULL)
103 info_msg (_("attempt to open %s failed\n"), attempt);
104 else
105 info_msg (_("attempt to open %s succeeded\n"), attempt);
106 }
107
b90d1146 108 if (entry->the_bfd == NULL)
252b5132
RH
109 {
110 if (bfd_get_error () == bfd_error_invalid_target)
111 einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
112 return false;
113 }
b90d1146
ILT
114
115 /* If we are searching for this file, see if the architecture is
116 compatible with the output file. If it isn't, keep searching.
117 If we can't open the file as an object file, stop the search
118 here. */
119
120 if (entry->search_dirs_flag)
121 {
122 bfd *check;
123
124 if (bfd_check_format (entry->the_bfd, bfd_archive))
125 check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
126 else
127 check = entry->the_bfd;
128
a9998805 129 if (check != NULL)
b90d1146 130 {
a9998805
ILT
131 if (! bfd_check_format (check, bfd_object))
132 return true;
133 if (bfd_arch_get_compatible (check, output_bfd) == NULL)
134 {
1c64c4ed 135 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
a9998805
ILT
136 attempt, entry->local_sym_name);
137 bfd_close (entry->the_bfd);
138 entry->the_bfd = NULL;
139 return false;
140 }
b90d1146
ILT
141 }
142 }
143
144 return true;
252b5132
RH
145}
146
147/* Search for and open the file specified by ENTRY. If it is an
148 archive, use ARCH, LIB and SUFFIX to modify the file name. */
149
344a211f 150boolean
252b5132
RH
151ldfile_open_file_search (arch, entry, lib, suffix)
152 const char *arch;
153 lang_input_statement_type *entry;
154 const char *lib;
155 const char *suffix;
156{
157 search_dirs_type *search;
158
159 /* If this is not an archive, try to open it in the current
160 directory first. */
161 if (! entry->is_archive)
162 {
163 if (ldfile_try_open_bfd (entry->filename, entry))
164 return true;
165 }
166
167 for (search = search_head;
168 search != (search_dirs_type *)NULL;
4de2d33d 169 search = search->next)
252b5132
RH
170 {
171 char *string;
172
173 if (entry->dynamic && ! link_info.relocateable)
174 {
175 if (ldemul_open_dynamic_archive (arch, search, entry))
176 return true;
177 }
178
179 string = (char *) xmalloc (strlen (search->name)
180 + strlen (slash)
181 + strlen (lib)
182 + strlen (entry->filename)
183 + strlen (arch)
184 + strlen (suffix)
185 + 1);
186
187 if (entry->is_archive)
188 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
189 lib, entry->filename, arch, suffix);
190 else if (entry->filename[0] == '/' || entry->filename[0] == '.'
191#if defined (__MSDOS__) || defined (_WIN32)
4de2d33d
KH
192 || entry->filename[0] == '\\'
193 || (isalpha (entry->filename[0])
252b5132
RH
194 && entry->filename[1] == ':')
195#endif
196 )
197 strcpy (string, entry->filename);
198 else
199 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
200
201 if (ldfile_try_open_bfd (string, entry))
202 {
b90d1146
ILT
203 entry->filename = string;
204 return true;
252b5132
RH
205 }
206
207 free (string);
208 }
209
210 return false;
211}
212
213/* Open the input file specified by ENTRY. */
214
215void
216ldfile_open_file (entry)
217 lang_input_statement_type *entry;
218{
219 if (entry->the_bfd != NULL)
220 return;
221
222 if (! entry->search_dirs_flag)
223 {
224 if (ldfile_try_open_bfd (entry->filename, entry))
225 return;
226 if (strcmp (entry->filename, entry->local_sym_name) != 0)
227 einfo (_("%F%P: cannot open %s for %s: %E\n"),
228 entry->filename, entry->local_sym_name);
229 else
1c64c4ed 230 einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
252b5132
RH
231 }
232 else
233 {
234 search_arch_type *arch;
235
236 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
237 for (arch = search_arch_head;
238 arch != (search_arch_type *) NULL;
239 arch = arch->next)
240 {
241 if (ldfile_open_file_search (arch->name, entry, "lib", ".a"))
242 return;
243#ifdef VMS
244 if (ldfile_open_file_search (arch->name, entry, ":lib", ".a"))
245 return;
246#endif
344a211f
NC
247 if (ldemul_find_potential_libraries (arch->name, entry))
248 return;
252b5132 249 }
4de2d33d 250
252b5132
RH
251 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
252 }
253}
254
255/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
256
257static FILE *
258try_open (name, exten)
259 const char *name;
260 const char *exten;
261{
262 FILE *result;
263 char buff[1000];
264
265 result = fopen (name, "r");
4de2d33d 266
252b5132
RH
267 if (trace_file_tries)
268 {
269 if (result == NULL)
270 info_msg (_("cannot find script file %s\n"), name);
271 else
272 info_msg (_("opened script file %s\n"), name);
273 }
274
275 if (result != NULL)
276 return result;
277
278 if (*exten)
279 {
280 sprintf (buff, "%s%s", name, exten);
281 result = fopen (buff, "r");
4de2d33d 282
252b5132
RH
283 if (trace_file_tries)
284 {
285 if (result == NULL)
286 info_msg (_("cannot find script file %s\n"), buff);
287 else
288 info_msg (_("opened script file %s\n"), buff);
289 }
290 }
291
292 return result;
293}
294
295/* Try to open NAME; if that fails, look for it in any directories
296 specified with -L, without and with EXTEND apppended. */
297
298FILE *
299ldfile_find_command_file (name, extend)
300 const char *name;
301 const char *extend;
302{
303 search_dirs_type *search;
304 FILE *result;
305 char buffer[1000];
306
307 /* First try raw name */
1c64c4ed
NC
308 result = try_open (name, "");
309 if (result == (FILE *)NULL)
310 {
311 /* Try now prefixes */
312 for (search = search_head;
313 search != (search_dirs_type *)NULL;
314 search = search->next)
315 {
316 sprintf (buffer,"%s%s%s", search->name, slash, name);
4de2d33d 317
1c64c4ed
NC
318 result = try_open (buffer, extend);
319 if (result)
320 break;
321 }
252b5132 322 }
4de2d33d 323
252b5132
RH
324 return result;
325}
326
327void
328ldfile_open_command_file (name)
329 const char *name;
330{
331 FILE *ldlex_input_stack;
1c64c4ed 332 ldlex_input_stack = ldfile_find_command_file (name, "");
252b5132 333
1c64c4ed
NC
334 if (ldlex_input_stack == (FILE *)NULL)
335 {
336 bfd_set_error (bfd_error_system_call);
337 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
338 }
4de2d33d 339
1c64c4ed 340 lex_push_file (ldlex_input_stack, name);
4de2d33d 341
252b5132
RH
342 ldfile_input_filename = name;
343 lineno = 1;
344 had_script = true;
345}
346
252b5132 347#ifdef GNU960
1c64c4ed
NC
348static char *
349gnu960_map_archname (name)
350 char *name;
252b5132
RH
351{
352 struct tabentry { char *cmd_switch; char *arch; };
1c64c4ed
NC
353 static struct tabentry arch_tab[] =
354 {
252b5132
RH
355 "", "",
356 "KA", "ka",
357 "KB", "kb",
358 "KC", "mc", /* Synonym for MC */
359 "MC", "mc",
360 "CA", "ca",
361 "SA", "ka", /* Functionally equivalent to KA */
362 "SB", "kb", /* Functionally equivalent to KB */
363 NULL, ""
364 };
365 struct tabentry *tp;
252b5132 366
1c64c4ed
NC
367 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
368 {
369 if (! strcmp (name,tp->cmd_switch))
370 break;
252b5132 371 }
252b5132 372
1c64c4ed
NC
373 if (tp->cmd_switch == NULL)
374 einfo (_("%P%F: unknown architecture: %s\n"),name);
4de2d33d 375
252b5132
RH
376 return tp->arch;
377}
378
252b5132 379void
1c64c4ed
NC
380ldfile_add_arch (name)
381 char *name;
252b5132
RH
382{
383 search_arch_type *new =
1c64c4ed 384 (search_arch_type *)xmalloc ((bfd_size_type)(sizeof (search_arch_type)));
252b5132 385
1c64c4ed
NC
386 if (*name != '\0')
387 {
388 if (ldfile_output_machine_name[0] != '\0')
389 {
390 einfo (_("%P%F: target architecture respecified\n"));
391 return;
392 }
4de2d33d 393
1c64c4ed 394 ldfile_output_machine_name = name;
252b5132 395 }
252b5132
RH
396
397 new->next = (search_arch_type*)NULL;
1c64c4ed 398 new->name = gnu960_map_archname (name);
252b5132
RH
399 *search_arch_tail_ptr = new;
400 search_arch_tail_ptr = &new->next;
252b5132
RH
401}
402
403#else /* not GNU960 */
404
252b5132
RH
405void
406ldfile_add_arch (in_name)
407 CONST char * in_name;
408{
1c64c4ed 409 char *name = buystring (in_name);
252b5132
RH
410 search_arch_type *new =
411 (search_arch_type *) xmalloc (sizeof (search_arch_type));
412
413 ldfile_output_machine_name = in_name;
414
415 new->name = name;
416 new->next = (search_arch_type*)NULL;
417 while (*name)
418 {
419 if (isupper ((unsigned char) *name))
420 *name = tolower ((unsigned char) *name);
421 name++;
422 }
423 *search_arch_tail_ptr = new;
424 search_arch_tail_ptr = &new->next;
425
426}
427#endif
428
429/* Set the output architecture */
430void
431ldfile_set_output_arch (string)
432 CONST char *string;
433{
1c64c4ed
NC
434 const bfd_arch_info_type *arch = bfd_scan_arch (string);
435
436 if (arch)
437 {
438 ldfile_output_architecture = arch->arch;
439 ldfile_output_machine = arch->mach;
440 ldfile_output_machine_name = arch->printable_name;
441 }
442 else
443 {
444 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
445 }
252b5132 446}