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