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