* gdb.base/printcmds.exp: Don't xfail the ptype command. This is
[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;
135 if (bfd_arch_get_compatible (check, output_bfd) == NULL)
136 {
1c64c4ed 137 einfo (_("%P: skipping incompatible %s when searching for %s\n"),
a9998805
ILT
138 attempt, entry->local_sym_name);
139 bfd_close (entry->the_bfd);
140 entry->the_bfd = NULL;
141 return false;
142 }
b90d1146
ILT
143 }
144 }
145
146 return true;
252b5132
RH
147}
148
149/* Search for and open the file specified by ENTRY. If it is an
150 archive, use ARCH, LIB and SUFFIX to modify the file name. */
151
344a211f 152boolean
252b5132
RH
153ldfile_open_file_search (arch, entry, lib, suffix)
154 const char *arch;
155 lang_input_statement_type *entry;
156 const char *lib;
157 const char *suffix;
158{
159 search_dirs_type *search;
160
161 /* If this is not an archive, try to open it in the current
162 directory first. */
163 if (! entry->is_archive)
164 {
165 if (ldfile_try_open_bfd (entry->filename, entry))
166 return true;
167 }
168
169 for (search = search_head;
89cdebba 170 search != (search_dirs_type *) NULL;
4de2d33d 171 search = search->next)
252b5132
RH
172 {
173 char *string;
174
175 if (entry->dynamic && ! link_info.relocateable)
176 {
177 if (ldemul_open_dynamic_archive (arch, search, entry))
178 return true;
179 }
180
181 string = (char *) xmalloc (strlen (search->name)
182 + strlen (slash)
183 + strlen (lib)
184 + strlen (entry->filename)
185 + strlen (arch)
186 + strlen (suffix)
187 + 1);
188
189 if (entry->is_archive)
190 sprintf (string, "%s%s%s%s%s%s", search->name, slash,
191 lib, entry->filename, arch, suffix);
192 else if (entry->filename[0] == '/' || entry->filename[0] == '.'
193#if defined (__MSDOS__) || defined (_WIN32)
4de2d33d 194 || entry->filename[0] == '\\'
3882b010 195 || (ISALPHA (entry->filename[0])
252b5132
RH
196 && entry->filename[1] == ':')
197#endif
198 )
199 strcpy (string, entry->filename);
200 else
201 sprintf (string, "%s%s%s", search->name, slash, entry->filename);
202
203 if (ldfile_try_open_bfd (string, entry))
204 {
b90d1146
ILT
205 entry->filename = string;
206 return true;
252b5132
RH
207 }
208
209 free (string);
210 }
211
212 return false;
213}
214
215/* Open the input file specified by ENTRY. */
216
217void
218ldfile_open_file (entry)
219 lang_input_statement_type *entry;
220{
221 if (entry->the_bfd != NULL)
222 return;
223
224 if (! entry->search_dirs_flag)
225 {
226 if (ldfile_try_open_bfd (entry->filename, entry))
227 return;
228 if (strcmp (entry->filename, entry->local_sym_name) != 0)
229 einfo (_("%F%P: cannot open %s for %s: %E\n"),
230 entry->filename, entry->local_sym_name);
231 else
1c64c4ed 232 einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
252b5132
RH
233 }
234 else
235 {
236 search_arch_type *arch;
78f85fd7 237 boolean found = false;
252b5132
RH
238
239 /* Try to open <filename><suffix> or lib<filename><suffix>.a */
240 for (arch = search_arch_head;
241 arch != (search_arch_type *) NULL;
242 arch = arch->next)
243 {
78f85fd7
L
244 found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
245 if (found)
246 break;
252b5132 247#ifdef VMS
78f85fd7
L
248 found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
249 if (found)
250 break;
252b5132 251#endif
78f85fd7
L
252 found = ldemul_find_potential_libraries (arch->name, entry);
253 if (found)
254 break;
252b5132 255 }
4de2d33d 256
78f85fd7
L
257 /* If we have found the file, we don't need to search directories
258 again. */
259 if (found)
260 entry->search_dirs_flag = false;
261 else
262 einfo (_("%F%P: cannot find %s\n"), entry->local_sym_name);
252b5132
RH
263 }
264}
265
266/* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */
267
268static FILE *
269try_open (name, exten)
270 const char *name;
271 const char *exten;
272{
273 FILE *result;
274 char buff[1000];
275
276 result = fopen (name, "r");
4de2d33d 277
252b5132
RH
278 if (trace_file_tries)
279 {
280 if (result == NULL)
281 info_msg (_("cannot find script file %s\n"), name);
282 else
283 info_msg (_("opened script file %s\n"), name);
284 }
285
286 if (result != NULL)
287 return result;
288
289 if (*exten)
290 {
291 sprintf (buff, "%s%s", name, exten);
292 result = fopen (buff, "r");
4de2d33d 293
252b5132
RH
294 if (trace_file_tries)
295 {
296 if (result == NULL)
297 info_msg (_("cannot find script file %s\n"), buff);
298 else
299 info_msg (_("opened script file %s\n"), buff);
300 }
301 }
302
303 return result;
304}
305
306/* Try to open NAME; if that fails, look for it in any directories
307 specified with -L, without and with EXTEND apppended. */
308
309FILE *
310ldfile_find_command_file (name, extend)
311 const char *name;
312 const char *extend;
313{
314 search_dirs_type *search;
315 FILE *result;
316 char buffer[1000];
317
89cdebba 318 /* First try raw name. */
1c64c4ed 319 result = try_open (name, "");
89cdebba 320 if (result == (FILE *) NULL)
1c64c4ed 321 {
89cdebba 322 /* Try now prefixes. */
1c64c4ed 323 for (search = search_head;
89cdebba 324 search != (search_dirs_type *) NULL;
1c64c4ed
NC
325 search = search->next)
326 {
89cdebba 327 sprintf (buffer, "%s%s%s", search->name, slash, name);
4de2d33d 328
1c64c4ed
NC
329 result = try_open (buffer, extend);
330 if (result)
331 break;
332 }
252b5132 333 }
4de2d33d 334
252b5132
RH
335 return result;
336}
337
338void
339ldfile_open_command_file (name)
340 const char *name;
341{
342 FILE *ldlex_input_stack;
1c64c4ed 343 ldlex_input_stack = ldfile_find_command_file (name, "");
252b5132 344
89cdebba 345 if (ldlex_input_stack == (FILE *) NULL)
1c64c4ed
NC
346 {
347 bfd_set_error (bfd_error_system_call);
348 einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
349 }
4de2d33d 350
1c64c4ed 351 lex_push_file (ldlex_input_stack, name);
4de2d33d 352
252b5132
RH
353 ldfile_input_filename = name;
354 lineno = 1;
b7a26f91 355
b9a8de1e 356 saved_script_handle = ldlex_input_stack;
252b5132
RH
357}
358
252b5132 359#ifdef GNU960
1c64c4ed
NC
360static char *
361gnu960_map_archname (name)
362 char *name;
252b5132
RH
363{
364 struct tabentry { char *cmd_switch; char *arch; };
1c64c4ed
NC
365 static struct tabentry arch_tab[] =
366 {
252b5132
RH
367 "", "",
368 "KA", "ka",
369 "KB", "kb",
370 "KC", "mc", /* Synonym for MC */
371 "MC", "mc",
372 "CA", "ca",
373 "SA", "ka", /* Functionally equivalent to KA */
374 "SB", "kb", /* Functionally equivalent to KB */
375 NULL, ""
376 };
377 struct tabentry *tp;
252b5132 378
1c64c4ed
NC
379 for (tp = arch_tab; tp->cmd_switch != NULL; tp++)
380 {
381 if (! strcmp (name,tp->cmd_switch))
382 break;
252b5132 383 }
252b5132 384
1c64c4ed 385 if (tp->cmd_switch == NULL)
89cdebba 386 einfo (_("%P%F: unknown architecture: %s\n"), name);
4de2d33d 387
252b5132
RH
388 return tp->arch;
389}
390
252b5132 391void
1c64c4ed
NC
392ldfile_add_arch (name)
393 char *name;
252b5132
RH
394{
395 search_arch_type *new =
89cdebba 396 (search_arch_type *) xmalloc ((bfd_size_type) (sizeof (search_arch_type)));
252b5132 397
1c64c4ed
NC
398 if (*name != '\0')
399 {
400 if (ldfile_output_machine_name[0] != '\0')
401 {
402 einfo (_("%P%F: target architecture respecified\n"));
403 return;
404 }
4de2d33d 405
1c64c4ed 406 ldfile_output_machine_name = name;
252b5132 407 }
252b5132 408
89cdebba 409 new->next = (search_arch_type *) NULL;
1c64c4ed 410 new->name = gnu960_map_archname (name);
252b5132
RH
411 *search_arch_tail_ptr = new;
412 search_arch_tail_ptr = &new->next;
252b5132
RH
413}
414
89cdebba 415#else /* not GNU960 */
252b5132 416
252b5132
RH
417void
418ldfile_add_arch (in_name)
89cdebba 419 CONST char *in_name;
252b5132 420{
d1b2b2dc 421 char *name = xstrdup (in_name);
252b5132
RH
422 search_arch_type *new =
423 (search_arch_type *) xmalloc (sizeof (search_arch_type));
424
425 ldfile_output_machine_name = in_name;
426
427 new->name = name;
89cdebba 428 new->next = (search_arch_type *) NULL;
252b5132
RH
429 while (*name)
430 {
3882b010 431 *name = TOLOWER (*name);
252b5132
RH
432 name++;
433 }
434 *search_arch_tail_ptr = new;
435 search_arch_tail_ptr = &new->next;
436
437}
438#endif
439
89cdebba
KH
440/* Set the output architecture. */
441
252b5132
RH
442void
443ldfile_set_output_arch (string)
444 CONST char *string;
445{
1c64c4ed
NC
446 const bfd_arch_info_type *arch = bfd_scan_arch (string);
447
448 if (arch)
449 {
450 ldfile_output_architecture = arch->arch;
451 ldfile_output_machine = arch->mach;
452 ldfile_output_machine_name = arch->printable_name;
453 }
454 else
455 {
456 einfo (_("%P%F: cannot represent machine `%s'\n"), string);
457 }
252b5132 458}
This page took 0.176543 seconds and 4 git commands to generate.