]>
Commit | Line | Data |
---|---|---|
a9998805 | 1 | /* Linker file opening and searching. |
5e2f1575 | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002, |
c4b78195 | 3 | 2003, 2004, 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
252b5132 | 4 | |
f96b4a7b | 5 | This file is part of the GNU Binutils. |
252b5132 | 6 | |
f96b4a7b | 7 | This program is free software; you can redistribute it and/or modify |
3fe38064 | 8 | it under the terms of the GNU General Public License as published by |
f96b4a7b NC |
9 | the Free Software Foundation; either version 3 of the License, or |
10 | (at your option) any later version. | |
252b5132 | 11 | |
f96b4a7b | 12 | This program is distributed in the hope that it will be useful, |
3fe38064 NC |
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. | |
252b5132 | 16 | |
3fe38064 | 17 | You should have received a copy of the GNU General Public License |
f96b4a7b NC |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
20 | MA 02110-1301, USA. */ | |
252b5132 | 21 | |
252b5132 | 22 | #include "sysdep.h" |
3db64b00 | 23 | #include "bfd.h" |
252b5132 | 24 | #include "bfdlink.h" |
3882b010 | 25 | #include "safe-ctype.h" |
252b5132 RH |
26 | #include "ld.h" |
27 | #include "ldmisc.h" | |
28 | #include "ldexp.h" | |
29 | #include "ldlang.h" | |
30 | #include "ldfile.h" | |
31 | #include "ldmain.h" | |
df2a7313 | 32 | #include <ldgram.h> |
252b5132 RH |
33 | #include "ldlex.h" |
34 | #include "ldemul.h" | |
d1b2b2dc | 35 | #include "libiberty.h" |
3fe38064 | 36 | #include "filenames.h" |
5d3236ee DK |
37 | #ifdef ENABLE_PLUGINS |
38 | #include "plugin-api.h" | |
39 | #include "plugin.h" | |
40 | #endif /* ENABLE_PLUGINS */ | |
252b5132 | 41 | |
3fe38064 NC |
42 | const char * ldfile_input_filename; |
43 | bfd_boolean ldfile_assumed_script = FALSE; | |
44 | const char * ldfile_output_machine_name = ""; | |
252b5132 RH |
45 | unsigned long ldfile_output_machine; |
46 | enum bfd_architecture ldfile_output_architecture; | |
3fe38064 | 47 | search_dirs_type * search_head; |
252b5132 | 48 | |
252b5132 | 49 | #ifdef VMS |
279e75dc | 50 | static char * slash = ""; |
252b5132 RH |
51 | #else |
52 | #if defined (_WIN32) && ! defined (__CYGWIN32__) | |
279e75dc | 53 | static char * slash = "\\"; |
252b5132 | 54 | #else |
279e75dc | 55 | static char * slash = "/"; |
252b5132 RH |
56 | #endif |
57 | #endif | |
252b5132 | 58 | |
3fe38064 NC |
59 | typedef struct search_arch |
60 | { | |
4de2d33d | 61 | char *name; |
252b5132 RH |
62 | struct search_arch *next; |
63 | } search_arch_type; | |
64 | ||
3fe38064 | 65 | static search_dirs_type **search_tail_ptr = &search_head; |
252b5132 RH |
66 | static search_arch_type *search_arch_head; |
67 | static search_arch_type **search_arch_tail_ptr = &search_arch_head; | |
4de2d33d | 68 | |
3fe38064 NC |
69 | /* Test whether a pathname, after canonicalization, is the same or a |
70 | sub-directory of the sysroot directory. */ | |
71 | ||
72 | static bfd_boolean | |
1579bae1 | 73 | is_sysrooted_pathname (const char *name, bfd_boolean notsame) |
3fe38064 NC |
74 | { |
75 | char * realname = ld_canon_sysroot ? lrealpath (name) : NULL; | |
76 | int len; | |
77 | bfd_boolean result; | |
78 | ||
79 | if (! realname) | |
80 | return FALSE; | |
5e2f1575 | 81 | |
3fe38064 NC |
82 | len = strlen (realname); |
83 | ||
84 | if (((! notsame && len == ld_canon_sysroot_len) | |
85 | || (len >= ld_canon_sysroot_len | |
86 | && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len]) | |
87 | && (realname[ld_canon_sysroot_len] = '\0') == '\0')) | |
88 | && FILENAME_CMP (ld_canon_sysroot, realname) == 0) | |
89 | result = TRUE; | |
90 | else | |
91 | result = FALSE; | |
92 | ||
93 | if (realname) | |
94 | free (realname); | |
95 | ||
96 | return result; | |
97 | } | |
252b5132 | 98 | |
5ed6aba4 NC |
99 | /* Adds NAME to the library search path. |
100 | Makes a copy of NAME using xmalloc(). */ | |
101 | ||
252b5132 | 102 | void |
1579bae1 | 103 | ldfile_add_library_path (const char *name, bfd_boolean cmdline) |
252b5132 | 104 | { |
d3ce72d0 | 105 | search_dirs_type *new_dirs; |
252b5132 | 106 | |
361b220e CD |
107 | if (!cmdline && config.only_cmd_line_lib_dirs) |
108 | return; | |
109 | ||
d3ce72d0 NC |
110 | new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type)); |
111 | new_dirs->next = NULL; | |
112 | new_dirs->cmdline = cmdline; | |
113 | *search_tail_ptr = new_dirs; | |
114 | search_tail_ptr = &new_dirs->next; | |
9c8ebd6a DJ |
115 | |
116 | /* If a directory is marked as honoring sysroot, prepend the sysroot path | |
117 | now. */ | |
5ed6aba4 | 118 | if (name[0] == '=') |
e3f2db7f | 119 | { |
d3ce72d0 NC |
120 | new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL); |
121 | new_dirs->sysrooted = TRUE; | |
e3f2db7f AO |
122 | } |
123 | else | |
5ed6aba4 | 124 | { |
d3ce72d0 NC |
125 | new_dirs->name = xstrdup (name); |
126 | new_dirs->sysrooted = is_sysrooted_pathname (name, FALSE); | |
5ed6aba4 | 127 | } |
252b5132 RH |
128 | } |
129 | ||
130 | /* Try to open a BFD for a lang_input_statement. */ | |
131 | ||
b34976b6 | 132 | bfd_boolean |
1579bae1 AM |
133 | ldfile_try_open_bfd (const char *attempt, |
134 | lang_input_statement_type *entry) | |
252b5132 RH |
135 | { |
136 | entry->the_bfd = bfd_openr (attempt, entry->target); | |
137 | ||
138 | if (trace_file_tries) | |
139 | { | |
140 | if (entry->the_bfd == NULL) | |
141 | info_msg (_("attempt to open %s failed\n"), attempt); | |
142 | else | |
143 | info_msg (_("attempt to open %s succeeded\n"), attempt); | |
144 | } | |
145 | ||
b90d1146 | 146 | if (entry->the_bfd == NULL) |
252b5132 RH |
147 | { |
148 | if (bfd_get_error () == bfd_error_invalid_target) | |
149 | einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target); | |
b34976b6 | 150 | return FALSE; |
252b5132 | 151 | } |
b90d1146 ILT |
152 | |
153 | /* If we are searching for this file, see if the architecture is | |
154 | compatible with the output file. If it isn't, keep searching. | |
155 | If we can't open the file as an object file, stop the search | |
6c0c5b1e | 156 | here. If we are statically linking, ensure that we don't link |
5d3236ee DK |
157 | a dynamic object. |
158 | ||
159 | In the code below, it's OK to exit early if the check fails, | |
160 | closing the checked BFD and returning FALSE, but if the BFD | |
161 | checks out compatible, do not exit early returning TRUE, or | |
162 | the plugins will not get a chance to claim the file. */ | |
b90d1146 | 163 | |
6c0c5b1e | 164 | if (entry->search_dirs_flag || !entry->dynamic) |
b90d1146 ILT |
165 | { |
166 | bfd *check; | |
167 | ||
168 | if (bfd_check_format (entry->the_bfd, bfd_archive)) | |
169 | check = bfd_openr_next_archived_file (entry->the_bfd, NULL); | |
170 | else | |
171 | check = entry->the_bfd; | |
172 | ||
a9998805 | 173 | if (check != NULL) |
b90d1146 | 174 | { |
a9998805 | 175 | if (! bfd_check_format (check, bfd_object)) |
599917b8 JJ |
176 | { |
177 | if (check == entry->the_bfd | |
6c0c5b1e | 178 | && entry->search_dirs_flag |
599917b8 JJ |
179 | && bfd_get_error () == bfd_error_file_not_recognized |
180 | && ! ldemul_unrecognized_file (entry)) | |
181 | { | |
182 | int token, skip = 0; | |
183 | char *arg, *arg1, *arg2, *arg3; | |
184 | extern FILE *yyin; | |
185 | ||
186 | /* Try to interpret the file as a linker script. */ | |
187 | ldfile_open_command_file (attempt); | |
b34976b6 AM |
188 | |
189 | ldfile_assumed_script = TRUE; | |
599917b8 JJ |
190 | parser_input = input_selected; |
191 | ldlex_both (); | |
192 | token = INPUT_SCRIPT; | |
193 | while (token != 0) | |
194 | { | |
195 | switch (token) | |
196 | { | |
197 | case OUTPUT_FORMAT: | |
198 | if ((token = yylex ()) != '(') | |
199 | continue; | |
200 | if ((token = yylex ()) != NAME) | |
201 | continue; | |
202 | arg1 = yylval.name; | |
203 | arg2 = NULL; | |
204 | arg3 = NULL; | |
205 | token = yylex (); | |
206 | if (token == ',') | |
207 | { | |
208 | if ((token = yylex ()) != NAME) | |
209 | { | |
210 | free (arg1); | |
211 | continue; | |
212 | } | |
213 | arg2 = yylval.name; | |
214 | if ((token = yylex ()) != ',' | |
215 | || (token = yylex ()) != NAME) | |
216 | { | |
217 | free (arg1); | |
218 | free (arg2); | |
219 | continue; | |
220 | } | |
221 | arg3 = yylval.name; | |
222 | token = yylex (); | |
223 | } | |
224 | if (token == ')') | |
225 | { | |
226 | switch (command_line.endian) | |
227 | { | |
228 | default: | |
229 | case ENDIAN_UNSET: | |
230 | arg = arg1; break; | |
231 | case ENDIAN_BIG: | |
232 | arg = arg2 ? arg2 : arg1; break; | |
233 | case ENDIAN_LITTLE: | |
234 | arg = arg3 ? arg3 : arg1; break; | |
235 | } | |
236 | if (strcmp (arg, lang_get_output_target ()) != 0) | |
237 | skip = 1; | |
238 | } | |
239 | free (arg1); | |
240 | if (arg2) free (arg2); | |
241 | if (arg3) free (arg3); | |
242 | break; | |
243 | case NAME: | |
244 | case LNAME: | |
245 | case VERS_IDENTIFIER: | |
246 | case VERS_TAG: | |
247 | free (yylval.name); | |
248 | break; | |
249 | case INT: | |
250 | if (yylval.bigint.str) | |
251 | free (yylval.bigint.str); | |
252 | break; | |
5e2f1575 | 253 | } |
599917b8 JJ |
254 | token = yylex (); |
255 | } | |
4f39e302 | 256 | ldlex_popstate (); |
b34976b6 | 257 | ldfile_assumed_script = FALSE; |
599917b8 JJ |
258 | fclose (yyin); |
259 | yyin = NULL; | |
260 | if (skip) | |
261 | { | |
fe7929ce AM |
262 | if (command_line.warn_search_mismatch) |
263 | einfo (_("%P: skipping incompatible %s " | |
264 | "when searching for %s\n"), | |
265 | attempt, entry->local_sym_name); | |
599917b8 JJ |
266 | bfd_close (entry->the_bfd); |
267 | entry->the_bfd = NULL; | |
b34976b6 | 268 | return FALSE; |
599917b8 JJ |
269 | } |
270 | } | |
5d3236ee | 271 | goto success; |
599917b8 | 272 | } |
f1f0d9ab | 273 | |
6c0c5b1e AM |
274 | if (!entry->dynamic && (entry->the_bfd->flags & DYNAMIC) != 0) |
275 | { | |
276 | einfo (_("%F%P: attempted static link of dynamic object `%s'\n"), | |
277 | attempt); | |
278 | bfd_close (entry->the_bfd); | |
279 | entry->the_bfd = NULL; | |
280 | return FALSE; | |
281 | } | |
282 | ||
283 | if (entry->search_dirs_flag | |
f13a99db | 284 | && !bfd_arch_get_compatible (check, link_info.output_bfd, |
6c0c5b1e | 285 | command_line.accept_unknown_input_arch) |
312b768e | 286 | /* XCOFF archives can have 32 and 64 bit objects. */ |
f1f0d9ab | 287 | && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour |
f13a99db | 288 | && bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour |
f1f0d9ab | 289 | && bfd_check_format (entry->the_bfd, bfd_archive))) |
a9998805 | 290 | { |
fe7929ce AM |
291 | if (command_line.warn_search_mismatch) |
292 | einfo (_("%P: skipping incompatible %s " | |
293 | "when searching for %s\n"), | |
294 | attempt, entry->local_sym_name); | |
a9998805 ILT |
295 | bfd_close (entry->the_bfd); |
296 | entry->the_bfd = NULL; | |
b34976b6 | 297 | return FALSE; |
a9998805 | 298 | } |
b90d1146 ILT |
299 | } |
300 | } | |
5d3236ee DK |
301 | success: |
302 | #ifdef ENABLE_PLUGINS | |
303 | /* If plugins are active, they get first chance to claim | |
304 | any successfully-opened input file. We skip archives | |
305 | here; the plugin wants us to offer it the individual | |
306 | members when we enumerate them, not the whole file. We | |
307 | also ignore corefiles, because that's just weird. It is | |
308 | a needed side-effect of calling bfd_check_format with | |
309 | bfd_object that it sets the bfd's arch and mach, which | |
310 | will be needed when and if we want to bfd_create a new | |
311 | one using this one as a template. */ | |
312 | if (bfd_check_format (entry->the_bfd, bfd_object)) | |
313 | { | |
314 | int fd = open (attempt, O_RDONLY | O_BINARY); | |
315 | if (fd >= 0) | |
316 | { | |
317 | struct ld_plugin_input_file file; | |
318 | int claimed = 0; | |
319 | ||
320 | file.name = attempt; | |
321 | file.offset = 0; | |
322 | file.filesize = lseek (fd, 0, SEEK_END); | |
323 | file.fd = fd; | |
324 | /* We create a dummy BFD, initially empty, to house | |
325 | whatever symbols the plugin may want to add. */ | |
326 | file.handle = plugin_get_ir_dummy_bfd (attempt, entry->the_bfd); | |
327 | if (plugin_call_claim_file (&file, &claimed)) | |
328 | einfo (_("%P%F: %s: plugin reported error claiming file\n"), | |
329 | plugin_error_plugin ()); | |
330 | if (claimed) | |
331 | { | |
332 | /* Discard the real file's BFD and substitute the dummy one. */ | |
333 | bfd_close (entry->the_bfd); | |
334 | entry->the_bfd = file.handle; | |
335 | entry->claimed = TRUE; | |
336 | bfd_make_readable (entry->the_bfd); | |
337 | } | |
338 | else | |
339 | { | |
340 | /* If plugin didn't claim the file, we don't need the fd or the | |
341 | dummy bfd. Can't avoid speculatively creating it, alas. */ | |
342 | bfd_close_all_done (file.handle); | |
343 | close (fd); | |
344 | entry->claimed = FALSE; | |
345 | } | |
346 | } | |
347 | } | |
348 | #endif /* ENABLE_PLUGINS */ | |
b90d1146 | 349 | |
5d3236ee DK |
350 | /* It opened OK, the format checked out, and the plugins have had |
351 | their chance to claim it, so this is success. */ | |
b34976b6 | 352 | return TRUE; |
252b5132 RH |
353 | } |
354 | ||
355 | /* Search for and open the file specified by ENTRY. If it is an | |
356 | archive, use ARCH, LIB and SUFFIX to modify the file name. */ | |
357 | ||
b34976b6 | 358 | bfd_boolean |
1579bae1 AM |
359 | ldfile_open_file_search (const char *arch, |
360 | lang_input_statement_type *entry, | |
361 | const char *lib, | |
362 | const char *suffix) | |
252b5132 RH |
363 | { |
364 | search_dirs_type *search; | |
365 | ||
366 | /* If this is not an archive, try to open it in the current | |
367 | directory first. */ | |
368 | if (! entry->is_archive) | |
369 | { | |
3fe38064 | 370 | if (entry->sysrooted && IS_ABSOLUTE_PATH (entry->filename)) |
e3f2db7f AO |
371 | { |
372 | char *name = concat (ld_sysroot, entry->filename, | |
373 | (const char *) NULL); | |
374 | if (ldfile_try_open_bfd (name, entry)) | |
375 | { | |
376 | entry->filename = name; | |
377 | return TRUE; | |
378 | } | |
379 | free (name); | |
380 | } | |
381 | else if (ldfile_try_open_bfd (entry->filename, entry)) | |
382 | { | |
3fe38064 NC |
383 | entry->sysrooted = IS_ABSOLUTE_PATH (entry->filename) |
384 | && is_sysrooted_pathname (entry->filename, TRUE); | |
e3f2db7f AO |
385 | return TRUE; |
386 | } | |
3fe38064 NC |
387 | |
388 | if (IS_ABSOLUTE_PATH (entry->filename)) | |
389 | return FALSE; | |
252b5132 RH |
390 | } |
391 | ||
1579bae1 | 392 | for (search = search_head; search != NULL; search = search->next) |
252b5132 RH |
393 | { |
394 | char *string; | |
395 | ||
1049f94e | 396 | if (entry->dynamic && ! link_info.relocatable) |
252b5132 RH |
397 | { |
398 | if (ldemul_open_dynamic_archive (arch, search, entry)) | |
e3f2db7f AO |
399 | { |
400 | entry->sysrooted = search->sysrooted; | |
401 | return TRUE; | |
402 | } | |
252b5132 RH |
403 | } |
404 | ||
252b5132 | 405 | if (entry->is_archive) |
a26cc967 AM |
406 | string = concat (search->name, slash, lib, entry->filename, |
407 | arch, suffix, (const char *) NULL); | |
252b5132 | 408 | else |
a26cc967 AM |
409 | string = concat (search->name, slash, entry->filename, |
410 | (const char *) 0); | |
252b5132 RH |
411 | |
412 | if (ldfile_try_open_bfd (string, entry)) | |
413 | { | |
b90d1146 | 414 | entry->filename = string; |
e3f2db7f | 415 | entry->sysrooted = search->sysrooted; |
b34976b6 | 416 | return TRUE; |
252b5132 RH |
417 | } |
418 | ||
419 | free (string); | |
420 | } | |
421 | ||
b34976b6 | 422 | return FALSE; |
252b5132 RH |
423 | } |
424 | ||
c4b78195 NC |
425 | /* Open the input file specified by ENTRY. |
426 | PR 4437: Do not stop on the first missing file, but | |
427 | continue processing other input files in case there | |
428 | are more errors to report. */ | |
252b5132 RH |
429 | |
430 | void | |
1579bae1 | 431 | ldfile_open_file (lang_input_statement_type *entry) |
252b5132 RH |
432 | { |
433 | if (entry->the_bfd != NULL) | |
434 | return; | |
435 | ||
436 | if (! entry->search_dirs_flag) | |
437 | { | |
438 | if (ldfile_try_open_bfd (entry->filename, entry)) | |
439 | return; | |
c4b78195 | 440 | |
252b5132 | 441 | if (strcmp (entry->filename, entry->local_sym_name) != 0) |
c4b78195 | 442 | einfo (_("%P: cannot find %s (%s): %E\n"), |
252b5132 RH |
443 | entry->filename, entry->local_sym_name); |
444 | else | |
c4b78195 NC |
445 | einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name); |
446 | ||
447 | entry->missing_file = TRUE; | |
448 | missing_file = TRUE; | |
252b5132 RH |
449 | } |
450 | else | |
451 | { | |
452 | search_arch_type *arch; | |
b34976b6 | 453 | bfd_boolean found = FALSE; |
252b5132 RH |
454 | |
455 | /* Try to open <filename><suffix> or lib<filename><suffix>.a */ | |
1579bae1 | 456 | for (arch = search_arch_head; arch != NULL; arch = arch->next) |
252b5132 | 457 | { |
78f85fd7 L |
458 | found = ldfile_open_file_search (arch->name, entry, "lib", ".a"); |
459 | if (found) | |
460 | break; | |
252b5132 | 461 | #ifdef VMS |
78f85fd7 L |
462 | found = ldfile_open_file_search (arch->name, entry, ":lib", ".a"); |
463 | if (found) | |
464 | break; | |
252b5132 | 465 | #endif |
78f85fd7 L |
466 | found = ldemul_find_potential_libraries (arch->name, entry); |
467 | if (found) | |
468 | break; | |
252b5132 | 469 | } |
4de2d33d | 470 | |
78f85fd7 L |
471 | /* If we have found the file, we don't need to search directories |
472 | again. */ | |
473 | if (found) | |
b34976b6 | 474 | entry->search_dirs_flag = FALSE; |
c4b78195 NC |
475 | else |
476 | { | |
477 | if (entry->sysrooted | |
3fe38064 NC |
478 | && ld_sysroot |
479 | && IS_ABSOLUTE_PATH (entry->local_sym_name)) | |
c4b78195 NC |
480 | einfo (_("%P: cannot find %s inside %s\n"), |
481 | entry->local_sym_name, ld_sysroot); | |
482 | else | |
483 | einfo (_("%P: cannot find %s\n"), entry->local_sym_name); | |
484 | entry->missing_file = TRUE; | |
485 | missing_file = TRUE; | |
486 | } | |
252b5132 RH |
487 | } |
488 | } | |
489 | ||
490 | /* Try to open NAME; if that fails, try NAME with EXTEN appended to it. */ | |
491 | ||
492 | static FILE * | |
1579bae1 | 493 | try_open (const char *name, const char *exten) |
252b5132 RH |
494 | { |
495 | FILE *result; | |
252b5132 RH |
496 | |
497 | result = fopen (name, "r"); | |
4de2d33d | 498 | |
252b5132 RH |
499 | if (trace_file_tries) |
500 | { | |
501 | if (result == NULL) | |
502 | info_msg (_("cannot find script file %s\n"), name); | |
503 | else | |
504 | info_msg (_("opened script file %s\n"), name); | |
505 | } | |
506 | ||
507 | if (result != NULL) | |
508 | return result; | |
509 | ||
510 | if (*exten) | |
511 | { | |
a26cc967 AM |
512 | char *buff; |
513 | ||
514 | buff = concat (name, exten, (const char *) NULL); | |
252b5132 | 515 | result = fopen (buff, "r"); |
4de2d33d | 516 | |
252b5132 RH |
517 | if (trace_file_tries) |
518 | { | |
519 | if (result == NULL) | |
520 | info_msg (_("cannot find script file %s\n"), buff); | |
521 | else | |
522 | info_msg (_("opened script file %s\n"), buff); | |
523 | } | |
a26cc967 | 524 | free (buff); |
252b5132 RH |
525 | } |
526 | ||
527 | return result; | |
528 | } | |
529 | ||
a8caa245 AM |
530 | /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */ |
531 | ||
532 | static bfd_boolean | |
533 | check_for_scripts_dir (char *dir) | |
534 | { | |
535 | char *buf; | |
536 | struct stat s; | |
537 | bfd_boolean res; | |
538 | ||
539 | buf = concat (dir, "/ldscripts", (const char *) NULL); | |
540 | res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); | |
541 | free (buf); | |
542 | return res; | |
543 | } | |
544 | ||
545 | /* Return the default directory for finding script files. | |
546 | We look for the "ldscripts" directory in: | |
547 | ||
548 | SCRIPTDIR (passed from Makefile) | |
549 | (adjusted according to the current location of the binary) | |
c38b10fa | 550 | the dir where this program is (for using it from the build tree). */ |
a8caa245 AM |
551 | |
552 | static char * | |
553 | find_scripts_dir (void) | |
554 | { | |
c38b10fa | 555 | char *dir; |
a8caa245 AM |
556 | |
557 | dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR); | |
558 | if (dir) | |
559 | { | |
560 | if (check_for_scripts_dir (dir)) | |
561 | return dir; | |
562 | free (dir); | |
563 | } | |
564 | ||
565 | dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR); | |
566 | if (dir) | |
567 | { | |
568 | if (check_for_scripts_dir (dir)) | |
569 | return dir; | |
570 | free (dir); | |
571 | } | |
572 | ||
a8caa245 | 573 | /* Look for "ldscripts" in the dir where our binary is. */ |
c38b10fa AM |
574 | dir = make_relative_prefix (program_name, ".", "."); |
575 | if (dir) | |
576 | { | |
577 | if (check_for_scripts_dir (dir)) | |
578 | return dir; | |
579 | free (dir); | |
580 | } | |
a8caa245 | 581 | |
a8caa245 AM |
582 | return NULL; |
583 | } | |
584 | ||
8c3e16f4 L |
585 | /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for |
586 | it in directories specified with -L, then in the default script | |
587 | directory, without and with EXTEND appended. If DEFAULT_ONLY is | |
588 | true, the search is restricted to the default script location. */ | |
252b5132 | 589 | |
279e75dc | 590 | static FILE * |
7d24f02c KH |
591 | ldfile_find_command_file (const char *name, const char *extend, |
592 | bfd_boolean default_only) | |
252b5132 RH |
593 | { |
594 | search_dirs_type *search; | |
32252ac1 | 595 | FILE *result = NULL; |
a8caa245 AM |
596 | char *buffer; |
597 | static search_dirs_type *script_search; | |
252b5132 | 598 | |
8c3e16f4 L |
599 | if (!default_only) |
600 | { | |
601 | /* First try raw name. */ | |
602 | result = try_open (name, ""); | |
603 | if (result != NULL) | |
604 | return result; | |
605 | } | |
a8caa245 AM |
606 | |
607 | if (!script_search) | |
1c64c4ed | 608 | { |
a8caa245 AM |
609 | char *script_dir = find_scripts_dir (); |
610 | if (script_dir) | |
1c64c4ed | 611 | { |
a8caa245 AM |
612 | search_dirs_type **save_tail_ptr = search_tail_ptr; |
613 | search_tail_ptr = &script_search; | |
614 | ldfile_add_library_path (script_dir, TRUE); | |
615 | search_tail_ptr = save_tail_ptr; | |
1c64c4ed | 616 | } |
a8caa245 AM |
617 | } |
618 | ||
7d24f02c KH |
619 | /* Temporarily append script_search to the path list so that the |
620 | paths specified with -L will be searched first. */ | |
621 | *search_tail_ptr = script_search; | |
622 | ||
a8caa245 | 623 | /* Try now prefixes. */ |
7d24f02c KH |
624 | for (search = default_only ? script_search : search_head; |
625 | search != NULL; | |
626 | search = search->next) | |
a8caa245 | 627 | { |
a8caa245 AM |
628 | buffer = concat (search->name, slash, name, (const char *) NULL); |
629 | result = try_open (buffer, extend); | |
630 | free (buffer); | |
631 | if (result) | |
632 | break; | |
252b5132 | 633 | } |
4de2d33d | 634 | |
7d24f02c KH |
635 | /* Restore the original path list. */ |
636 | *search_tail_ptr = NULL; | |
637 | ||
252b5132 RH |
638 | return result; |
639 | } | |
640 | ||
7d24f02c KH |
641 | /* Open command file NAME. */ |
642 | ||
643 | static void | |
644 | ldfile_open_command_file_1 (const char *name, bfd_boolean default_only) | |
252b5132 RH |
645 | { |
646 | FILE *ldlex_input_stack; | |
7d24f02c | 647 | ldlex_input_stack = ldfile_find_command_file (name, "", default_only); |
252b5132 | 648 | |
1579bae1 | 649 | if (ldlex_input_stack == NULL) |
1c64c4ed NC |
650 | { |
651 | bfd_set_error (bfd_error_system_call); | |
652 | einfo (_("%P%F: cannot open linker script file %s: %E\n"), name); | |
653 | } | |
4de2d33d | 654 | |
1c64c4ed | 655 | lex_push_file (ldlex_input_stack, name); |
4de2d33d | 656 | |
252b5132 RH |
657 | ldfile_input_filename = name; |
658 | lineno = 1; | |
b7a26f91 | 659 | |
b9a8de1e | 660 | saved_script_handle = ldlex_input_stack; |
252b5132 RH |
661 | } |
662 | ||
7d24f02c KH |
663 | /* Open command file NAME in the current directory, -L directories, |
664 | the default script location, in that order. */ | |
665 | ||
666 | void | |
667 | ldfile_open_command_file (const char *name) | |
668 | { | |
669 | ldfile_open_command_file_1 (name, FALSE); | |
670 | } | |
671 | ||
672 | /* Open command file NAME at the default script location. */ | |
673 | ||
674 | void | |
675 | ldfile_open_default_command_file (const char *name) | |
676 | { | |
677 | ldfile_open_command_file_1 (name, TRUE); | |
678 | } | |
679 | ||
252b5132 | 680 | void |
1579bae1 | 681 | ldfile_add_arch (const char *in_name) |
252b5132 | 682 | { |
d1b2b2dc | 683 | char *name = xstrdup (in_name); |
d3ce72d0 NC |
684 | search_arch_type *new_arch = (search_arch_type *) |
685 | xmalloc (sizeof (search_arch_type)); | |
252b5132 RH |
686 | |
687 | ldfile_output_machine_name = in_name; | |
688 | ||
d3ce72d0 NC |
689 | new_arch->name = name; |
690 | new_arch->next = NULL; | |
252b5132 RH |
691 | while (*name) |
692 | { | |
3882b010 | 693 | *name = TOLOWER (*name); |
252b5132 RH |
694 | name++; |
695 | } | |
d3ce72d0 NC |
696 | *search_arch_tail_ptr = new_arch; |
697 | search_arch_tail_ptr = &new_arch->next; | |
252b5132 RH |
698 | |
699 | } | |
252b5132 | 700 | |
89cdebba KH |
701 | /* Set the output architecture. */ |
702 | ||
252b5132 | 703 | void |
5e2f1575 | 704 | ldfile_set_output_arch (const char *string, enum bfd_architecture defarch) |
252b5132 | 705 | { |
1c64c4ed NC |
706 | const bfd_arch_info_type *arch = bfd_scan_arch (string); |
707 | ||
708 | if (arch) | |
709 | { | |
710 | ldfile_output_architecture = arch->arch; | |
711 | ldfile_output_machine = arch->mach; | |
712 | ldfile_output_machine_name = arch->printable_name; | |
713 | } | |
5e2f1575 AM |
714 | else if (defarch != bfd_arch_unknown) |
715 | ldfile_output_architecture = defarch; | |
1c64c4ed | 716 | else |
5e2f1575 | 717 | einfo (_("%P%F: cannot represent machine `%s'\n"), string); |
252b5132 | 718 | } |