* config/mn10300/mn10300.mt (TM_FILE): Delete.
[deliverable/binutils-gdb.git] / ld / ldfile.c
CommitLineData
a9998805 1/* Linker file opening and searching.
a2b64bed 2 Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001
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"
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"
d1b2b2dc 36#include "libiberty.h"
252b5132
RH
37
38#include <ctype.h>
39
40const char *ldfile_input_filename;
41boolean ldfile_assumed_script = false;
42const char *ldfile_output_machine_name = "";
43unsigned long ldfile_output_machine;
44enum bfd_architecture ldfile_output_architecture;
45search_dirs_type *search_head;
46
47#ifndef MPW
48#ifdef VMS
49char *slash = "";
50#else
51#if defined (_WIN32) && ! defined (__CYGWIN32__)
52char *slash = "\\";
53#else
54char *slash = "/";
55#endif
56#endif
57#else /* MPW */
1c64c4ed 58/* The MPW path char is a colon. */
252b5132
RH
59char *slash = ":";
60#endif /* MPW */
61
62/* LOCAL */
63
64static search_dirs_type **search_tail_ptr = &search_head;
65
89cdebba 66typedef struct search_arch {
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;
89cdebba 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;
78f85fd7 235 boolean found = false;
252b5132
RH
236
237 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
238 for (arch = search_arch_head;
239 arch != (search_arch_type *) NULL;
240 arch = arch->next)
241 {
78f85fd7
L
242 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
243 if (found)
244 break;
252b5132 245#ifdef VMS
78f85fd7
L
246 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
247 if (found)
248 break;
252b5132 249#endif
78f85fd7
L
250 found = ldemul_find_potential_libraries (arch->name, entry);
251 if (found)
252 break;
252b5132 253 }
4de2d33d 254
78f85fd7
L
255 /* If we have found the file, we don't need to search directories
256 again. */
257 if (found)
258 entry->search_dirs_flag = false;
259 else
260 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
252b5132
RH
261 }
262}
263
264/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
265
266static FILE *
267try_open (name, exten)
268 const char *name;
269 const char *exten;
270{
271 FILE *result;
272 char buff[1000];
273
274 result = fopen (name, "r");
4de2d33d 275
252b5132
RH
276 if (trace_file_tries)
277 {
278 if (result == NULL)
279 info_msg (_("cannot find script file %s\n"), name);
280 else
281 info_msg (_("opened script file %s\n"), name);
282 }
283
284 if (result != NULL)
285 return result;
286
287 if (*exten)
288 {
289 sprintf (buff, "%s%s", name, exten);
290 result = fopen (buff, "r");
4de2d33d 291
252b5132
RH
292 if (trace_file_tries)
293 {
294 if (result == NULL)
295 info_msg (_("cannot find script file %s\n"), buff);
296 else
297 info_msg (_("opened script file %s\n"), buff);
298 }
299 }
300
301 return result;
302}
303
304/* Try to open NAME; if that fails, look for it in any directories
305 specified with -L, without and with EXTEND apppended. */
306
307FILE *
308ldfile_find_command_file (name, extend)
309 const char *name;
310 const char *extend;
311{
312 search_dirs_type *search;
313 FILE *result;
314 char buffer[1000];
315
89cdebba 316 /* First try raw name. */
1c64c4ed 317 result = try_open (name, "");
89cdebba 318 if (result == (FILE *) NULL)
1c64c4ed 319 {
89cdebba 320 /* Try now prefixes. */
1c64c4ed 321 for (search = search_head;
89cdebba 322 search != (search_dirs_type *) NULL;
1c64c4ed
NC
323 search = search->next)
324 {
89cdebba 325 sprintf (buffer, "%s%s%s", search->name, slash, name);
4de2d33d 326
1c64c4ed
NC
327 result = try_open (buffer, extend);
328 if (result)
329 break;
330 }
252b5132 331 }
4de2d33d 332
252b5132
RH
333 return result;
334}
335
336void
337ldfile_open_command_file (name)
338 const char *name;
339{
340 FILE *ldlex_input_stack;
1c64c4ed 341 ldlex_input_stack = ldfile_find_command_file (name, "");
252b5132 342
89cdebba 343 if (ldlex_input_stack == (FILE *) NULL)
1c64c4ed
NC
344 {
345 bfd_set_error (bfd_error_system_call);
346 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
347 }
4de2d33d 348
1c64c4ed 349 lex_push_file (ldlex_input_stack, name);
4de2d33d 350
252b5132
RH
351 ldfile_input_filename = name;
352 lineno = 1;
353 had_script = true;
354}
355
252b5132 356#ifdef GNU960
1c64c4ed
NC
357static char *
358gnu960_map_archname (name)
359 char *name;
252b5132
RH
360{
361 struct tabentry { char *cmd_switch; char *arch; };
1c64c4ed
NC
362 static struct tabentry arch_tab[] =
363 {
252b5132
RH
364 "", "",
365 "KA", "ka",
366 "KB", "kb",
367 "KC", "mc", /* Synonym for MC */
368 "MC", "mc",
369 "CA", "ca",
370 "SA", "ka", /* Functionally equivalent to KA */
371 "SB", "kb", /* Functionally equivalent to KB */
372 NULL, ""
373 };
374 struct tabentry *tp;
252b5132 375
1c64c4ed
NC
376 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
377 {
378 if (! strcmp (name,tp->cmd_switch))
379 break;
252b5132 380 }
252b5132 381
1c64c4ed 382 if (tp->cmd_switch == NULL)
89cdebba 383 einfo (_("%P%F: unknown architecture: %s\n"), name);
4de2d33d 384
252b5132
RH
385 return tp->arch;
386}
387
252b5132 388void
1c64c4ed
NC
389ldfile_add_arch (name)
390 char *name;
252b5132
RH
391{
392 search_arch_type *new =
89cdebba 393 (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type)));
252b5132 394
1c64c4ed
NC
395 if (*name != '\0')
396 {
397 if (ldfile_output_machine_name[0] != '\0')
398 {
399 einfo (_("%P%F: target architecture respecified\n"));
400 return;
401 }
4de2d33d 402
1c64c4ed 403 ldfile_output_machine_name = name;
252b5132 404 }
252b5132 405
89cdebba 406 new->next = (search_arch_type *) NULL;
1c64c4ed 407 new->name = gnu960_map_archname (name);
252b5132
RH
408 *search_arch_tail_ptr = new;
409 search_arch_tail_ptr = &new->next;
252b5132
RH
410}
411
89cdebba 412#else /* not GNU960 */
252b5132 413
252b5132
RH
414void
415ldfile_add_arch (in_name)
89cdebba 416 CONST char *in_name;
252b5132 417{
d1b2b2dc 418 char *name = xstrdup (in_name);
252b5132
RH
419 search_arch_type *new =
420 (search_arch_type *) xmalloc (sizeof (search_arch_type));
421
422 ldfile_output_machine_name = in_name;
423
424 new->name = name;
89cdebba 425 new->next = (search_arch_type *) NULL;
252b5132
RH
426 while (*name)
427 {
428 if (isupper ((unsigned char) *name))
429 *name = tolower ((unsigned char) *name);
430 name++;
431 }
432 *search_arch_tail_ptr = new;
433 search_arch_tail_ptr = &new->next;
434
435}
436#endif
437
89cdebba
KH
438/* Set the output architecture. */
439
252b5132
RH
440void
441ldfile_set_output_arch (string)
442 CONST char *string;
443{
1c64c4ed
NC
444 const bfd_arch_info_type *arch = bfd_scan_arch (string);
445
446 if (arch)
447 {
448 ldfile_output_architecture = arch->arch;
449 ldfile_output_machine = arch->mach;
450 ldfile_output_machine_name = arch->printable_name;
451 }
452 else
453 {
454 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
455 }
252b5132 456}
This page took 0.103394 seconds and 4 git commands to generate.