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