]>
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 | ||
b90d1146 ILT |
145 | /* If we are searching for this file, see if the architecture is |
146 | compatible with the output file. If it isn't, keep searching. | |
147 | If we can't open the file as an object file, stop the search | |
6c0c5b1e | 148 | here. If we are statically linking, ensure that we don't link |
5d3236ee DK |
149 | a dynamic object. |
150 | ||
151 | In the code below, it's OK to exit early if the check fails, | |
152 | closing the checked BFD and returning FALSE, but if the BFD | |
153 | checks out compatible, do not exit early returning TRUE, or | |
154 | the plugins will not get a chance to claim the file. */ | |
b90d1146 | 155 | |
66be1055 | 156 | if (entry->flags.search_dirs || !entry->flags.dynamic) |
b90d1146 ILT |
157 | { |
158 | bfd *check; | |
159 | ||
160 | if (bfd_check_format (entry->the_bfd, bfd_archive)) | |
161 | check = bfd_openr_next_archived_file (entry->the_bfd, NULL); | |
162 | else | |
163 | check = entry->the_bfd; | |
164 | ||
a9998805 | 165 | if (check != NULL) |
b90d1146 | 166 | { |
a9998805 | 167 | if (! bfd_check_format (check, bfd_object)) |
599917b8 JJ |
168 | { |
169 | if (check == entry->the_bfd | |
66be1055 | 170 | && entry->flags.search_dirs |
599917b8 JJ |
171 | && bfd_get_error () == bfd_error_file_not_recognized |
172 | && ! ldemul_unrecognized_file (entry)) | |
173 | { | |
174 | int token, skip = 0; | |
175 | char *arg, *arg1, *arg2, *arg3; | |
176 | extern FILE *yyin; | |
177 | ||
178 | /* Try to interpret the file as a linker script. */ | |
179 | ldfile_open_command_file (attempt); | |
b34976b6 AM |
180 | |
181 | ldfile_assumed_script = TRUE; | |
599917b8 JJ |
182 | parser_input = input_selected; |
183 | ldlex_both (); | |
184 | token = INPUT_SCRIPT; | |
185 | while (token != 0) | |
186 | { | |
187 | switch (token) | |
188 | { | |
189 | case OUTPUT_FORMAT: | |
190 | if ((token = yylex ()) != '(') | |
191 | continue; | |
192 | if ((token = yylex ()) != NAME) | |
193 | continue; | |
194 | arg1 = yylval.name; | |
195 | arg2 = NULL; | |
196 | arg3 = NULL; | |
197 | token = yylex (); | |
198 | if (token == ',') | |
199 | { | |
200 | if ((token = yylex ()) != NAME) | |
201 | { | |
202 | free (arg1); | |
203 | continue; | |
204 | } | |
205 | arg2 = yylval.name; | |
206 | if ((token = yylex ()) != ',' | |
207 | || (token = yylex ()) != NAME) | |
208 | { | |
209 | free (arg1); | |
210 | free (arg2); | |
211 | continue; | |
212 | } | |
213 | arg3 = yylval.name; | |
214 | token = yylex (); | |
215 | } | |
216 | if (token == ')') | |
217 | { | |
218 | switch (command_line.endian) | |
219 | { | |
220 | default: | |
221 | case ENDIAN_UNSET: | |
222 | arg = arg1; break; | |
223 | case ENDIAN_BIG: | |
224 | arg = arg2 ? arg2 : arg1; break; | |
225 | case ENDIAN_LITTLE: | |
226 | arg = arg3 ? arg3 : arg1; break; | |
227 | } | |
228 | if (strcmp (arg, lang_get_output_target ()) != 0) | |
229 | skip = 1; | |
230 | } | |
231 | free (arg1); | |
232 | if (arg2) free (arg2); | |
233 | if (arg3) free (arg3); | |
234 | break; | |
235 | case NAME: | |
236 | case LNAME: | |
237 | case VERS_IDENTIFIER: | |
238 | case VERS_TAG: | |
239 | free (yylval.name); | |
240 | break; | |
241 | case INT: | |
242 | if (yylval.bigint.str) | |
243 | free (yylval.bigint.str); | |
244 | break; | |
5e2f1575 | 245 | } |
599917b8 JJ |
246 | token = yylex (); |
247 | } | |
4f39e302 | 248 | ldlex_popstate (); |
b34976b6 | 249 | ldfile_assumed_script = FALSE; |
599917b8 JJ |
250 | fclose (yyin); |
251 | yyin = NULL; | |
252 | if (skip) | |
253 | { | |
fe7929ce AM |
254 | if (command_line.warn_search_mismatch) |
255 | einfo (_("%P: skipping incompatible %s " | |
256 | "when searching for %s\n"), | |
257 | attempt, entry->local_sym_name); | |
599917b8 JJ |
258 | bfd_close (entry->the_bfd); |
259 | entry->the_bfd = NULL; | |
b34976b6 | 260 | return FALSE; |
599917b8 JJ |
261 | } |
262 | } | |
5d3236ee | 263 | goto success; |
599917b8 | 264 | } |
f1f0d9ab | 265 | |
66be1055 | 266 | if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0) |
6c0c5b1e AM |
267 | { |
268 | einfo (_("%F%P: attempted static link of dynamic object `%s'\n"), | |
269 | attempt); | |
270 | bfd_close (entry->the_bfd); | |
271 | entry->the_bfd = NULL; | |
272 | return FALSE; | |
273 | } | |
274 | ||
66be1055 | 275 | if (entry->flags.search_dirs |
f13a99db | 276 | && !bfd_arch_get_compatible (check, link_info.output_bfd, |
6c0c5b1e | 277 | command_line.accept_unknown_input_arch) |
312b768e | 278 | /* XCOFF archives can have 32 and 64 bit objects. */ |
f1f0d9ab | 279 | && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour |
f13a99db | 280 | && bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour |
f1f0d9ab | 281 | && bfd_check_format (entry->the_bfd, bfd_archive))) |
a9998805 | 282 | { |
fe7929ce AM |
283 | if (command_line.warn_search_mismatch) |
284 | einfo (_("%P: skipping incompatible %s " | |
285 | "when searching for %s\n"), | |
286 | attempt, entry->local_sym_name); | |
a9998805 ILT |
287 | bfd_close (entry->the_bfd); |
288 | entry->the_bfd = NULL; | |
b34976b6 | 289 | return FALSE; |
a9998805 | 290 | } |
b90d1146 ILT |
291 | } |
292 | } | |
5d3236ee DK |
293 | success: |
294 | #ifdef ENABLE_PLUGINS | |
295 | /* If plugins are active, they get first chance to claim | |
296 | any successfully-opened input file. We skip archives | |
297 | here; the plugin wants us to offer it the individual | |
298 | members when we enumerate them, not the whole file. We | |
299 | also ignore corefiles, because that's just weird. It is | |
300 | a needed side-effect of calling bfd_check_format with | |
301 | bfd_object that it sets the bfd's arch and mach, which | |
302 | will be needed when and if we want to bfd_create a new | |
303 | one using this one as a template. */ | |
d44ad554 | 304 | if (bfd_check_format (entry->the_bfd, bfd_object) |
9e2278f5 AM |
305 | && plugin_active_plugins_p () |
306 | && !no_more_claiming) | |
5d3236ee DK |
307 | { |
308 | int fd = open (attempt, O_RDONLY | O_BINARY); | |
309 | if (fd >= 0) | |
310 | { | |
311 | struct ld_plugin_input_file file; | |
5d3236ee DK |
312 | |
313 | file.name = attempt; | |
314 | file.offset = 0; | |
315 | file.filesize = lseek (fd, 0, SEEK_END); | |
316 | file.fd = fd; | |
02d00247 | 317 | plugin_maybe_claim (&file, entry); |
5d3236ee DK |
318 | } |
319 | } | |
320 | #endif /* ENABLE_PLUGINS */ | |
b90d1146 | 321 | |
5d3236ee DK |
322 | /* It opened OK, the format checked out, and the plugins have had |
323 | their chance to claim it, so this is success. */ | |
b34976b6 | 324 | return TRUE; |
252b5132 RH |
325 | } |
326 | ||
327 | /* Search for and open the file specified by ENTRY. If it is an | |
328 | archive, use ARCH, LIB and SUFFIX to modify the file name. */ | |
329 | ||
b34976b6 | 330 | bfd_boolean |
1579bae1 AM |
331 | ldfile_open_file_search (const char *arch, |
332 | lang_input_statement_type *entry, | |
333 | const char *lib, | |
334 | const char *suffix) | |
252b5132 RH |
335 | { |
336 | search_dirs_type *search; | |
337 | ||
338 | /* If this is not an archive, try to open it in the current | |
339 | directory first. */ | |
66be1055 | 340 | if (! entry->flags.maybe_archive) |
252b5132 | 341 | { |
66be1055 | 342 | if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename)) |
e3f2db7f AO |
343 | { |
344 | char *name = concat (ld_sysroot, entry->filename, | |
345 | (const char *) NULL); | |
346 | if (ldfile_try_open_bfd (name, entry)) | |
347 | { | |
348 | entry->filename = name; | |
349 | return TRUE; | |
350 | } | |
351 | free (name); | |
352 | } | |
353 | else if (ldfile_try_open_bfd (entry->filename, entry)) | |
f4a23d42 | 354 | return TRUE; |
3fe38064 NC |
355 | |
356 | if (IS_ABSOLUTE_PATH (entry->filename)) | |
357 | return FALSE; | |
252b5132 RH |
358 | } |
359 | ||
1579bae1 | 360 | for (search = search_head; search != NULL; search = search->next) |
252b5132 RH |
361 | { |
362 | char *string; | |
363 | ||
66be1055 | 364 | if (entry->flags.dynamic && ! link_info.relocatable) |
252b5132 RH |
365 | { |
366 | if (ldemul_open_dynamic_archive (arch, search, entry)) | |
f4a23d42 | 367 | return TRUE; |
252b5132 RH |
368 | } |
369 | ||
d4ae5fb0 | 370 | if (entry->flags.maybe_archive && !entry->flags.full_name_provided) |
a26cc967 AM |
371 | string = concat (search->name, slash, lib, entry->filename, |
372 | arch, suffix, (const char *) NULL); | |
252b5132 | 373 | else |
a26cc967 AM |
374 | string = concat (search->name, slash, entry->filename, |
375 | (const char *) 0); | |
252b5132 RH |
376 | |
377 | if (ldfile_try_open_bfd (string, entry)) | |
378 | { | |
b90d1146 | 379 | entry->filename = string; |
b34976b6 | 380 | return TRUE; |
252b5132 RH |
381 | } |
382 | ||
383 | free (string); | |
384 | } | |
385 | ||
b34976b6 | 386 | return FALSE; |
252b5132 RH |
387 | } |
388 | ||
c4b78195 NC |
389 | /* Open the input file specified by ENTRY. |
390 | PR 4437: Do not stop on the first missing file, but | |
391 | continue processing other input files in case there | |
392 | are more errors to report. */ | |
252b5132 RH |
393 | |
394 | void | |
1579bae1 | 395 | ldfile_open_file (lang_input_statement_type *entry) |
252b5132 RH |
396 | { |
397 | if (entry->the_bfd != NULL) | |
398 | return; | |
399 | ||
66be1055 | 400 | if (! entry->flags.search_dirs) |
252b5132 RH |
401 | { |
402 | if (ldfile_try_open_bfd (entry->filename, entry)) | |
403 | return; | |
c4b78195 | 404 | |
42627821 | 405 | if (filename_cmp (entry->filename, entry->local_sym_name) != 0) |
c4b78195 | 406 | einfo (_("%P: cannot find %s (%s): %E\n"), |
252b5132 RH |
407 | entry->filename, entry->local_sym_name); |
408 | else | |
c4b78195 NC |
409 | einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name); |
410 | ||
66be1055 AM |
411 | entry->flags.missing_file = TRUE; |
412 | input_flags.missing_file = TRUE; | |
252b5132 RH |
413 | } |
414 | else | |
415 | { | |
416 | search_arch_type *arch; | |
b34976b6 | 417 | bfd_boolean found = FALSE; |
252b5132 RH |
418 | |
419 | /* Try to open <filename><suffix> or lib<filename><suffix>.a */ | |
1579bae1 | 420 | for (arch = search_arch_head; arch != NULL; arch = arch->next) |
252b5132 | 421 | { |
78f85fd7 L |
422 | found = ldfile_open_file_search (arch->name, entry, "lib", ".a"); |
423 | if (found) | |
424 | break; | |
252b5132 | 425 | #ifdef VMS |
78f85fd7 L |
426 | found = ldfile_open_file_search (arch->name, entry, ":lib", ".a"); |
427 | if (found) | |
428 | break; | |
252b5132 | 429 | #endif |
78f85fd7 L |
430 | found = ldemul_find_potential_libraries (arch->name, entry); |
431 | if (found) | |
432 | break; | |
252b5132 | 433 | } |
4de2d33d | 434 | |
78f85fd7 L |
435 | /* If we have found the file, we don't need to search directories |
436 | again. */ | |
437 | if (found) | |
66be1055 | 438 | entry->flags.search_dirs = FALSE; |
c4b78195 NC |
439 | else |
440 | { | |
66be1055 | 441 | if (entry->flags.sysrooted |
3fe38064 NC |
442 | && ld_sysroot |
443 | && IS_ABSOLUTE_PATH (entry->local_sym_name)) | |
c4b78195 NC |
444 | einfo (_("%P: cannot find %s inside %s\n"), |
445 | entry->local_sym_name, ld_sysroot); | |
446 | else | |
447 | einfo (_("%P: cannot find %s\n"), entry->local_sym_name); | |
66be1055 AM |
448 | entry->flags.missing_file = TRUE; |
449 | input_flags.missing_file = TRUE; | |
c4b78195 | 450 | } |
252b5132 RH |
451 | } |
452 | } | |
453 | ||
f4a23d42 | 454 | /* Try to open NAME. */ |
252b5132 RH |
455 | |
456 | static FILE * | |
f4a23d42 | 457 | try_open (const char *name, bfd_boolean *sysrooted) |
252b5132 RH |
458 | { |
459 | FILE *result; | |
252b5132 RH |
460 | |
461 | result = fopen (name, "r"); | |
4de2d33d | 462 | |
f4a23d42 AM |
463 | if (result != NULL) |
464 | *sysrooted = is_sysrooted_pathname (name); | |
465 | ||
cd6f1cf3 | 466 | if (verbose) |
252b5132 RH |
467 | { |
468 | if (result == NULL) | |
469 | info_msg (_("cannot find script file %s\n"), name); | |
470 | else | |
471 | info_msg (_("opened script file %s\n"), name); | |
472 | } | |
473 | ||
252b5132 RH |
474 | return result; |
475 | } | |
476 | ||
a8caa245 AM |
477 | /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */ |
478 | ||
479 | static bfd_boolean | |
480 | check_for_scripts_dir (char *dir) | |
481 | { | |
482 | char *buf; | |
483 | struct stat s; | |
484 | bfd_boolean res; | |
485 | ||
486 | buf = concat (dir, "/ldscripts", (const char *) NULL); | |
487 | res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); | |
488 | free (buf); | |
489 | return res; | |
490 | } | |
491 | ||
492 | /* Return the default directory for finding script files. | |
493 | We look for the "ldscripts" directory in: | |
494 | ||
495 | SCRIPTDIR (passed from Makefile) | |
496 | (adjusted according to the current location of the binary) | |
c38b10fa | 497 | the dir where this program is (for using it from the build tree). */ |
a8caa245 AM |
498 | |
499 | static char * | |
500 | find_scripts_dir (void) | |
501 | { | |
c38b10fa | 502 | char *dir; |
a8caa245 AM |
503 | |
504 | dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR); | |
505 | if (dir) | |
506 | { | |
507 | if (check_for_scripts_dir (dir)) | |
508 | return dir; | |
509 | free (dir); | |
510 | } | |
511 | ||
512 | dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR); | |
513 | if (dir) | |
514 | { | |
515 | if (check_for_scripts_dir (dir)) | |
516 | return dir; | |
517 | free (dir); | |
518 | } | |
519 | ||
a8caa245 | 520 | /* Look for "ldscripts" in the dir where our binary is. */ |
c38b10fa AM |
521 | dir = make_relative_prefix (program_name, ".", "."); |
522 | if (dir) | |
523 | { | |
524 | if (check_for_scripts_dir (dir)) | |
525 | return dir; | |
526 | free (dir); | |
527 | } | |
a8caa245 | 528 | |
a8caa245 AM |
529 | return NULL; |
530 | } | |
531 | ||
8c3e16f4 L |
532 | /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for |
533 | it in directories specified with -L, then in the default script | |
f4a23d42 AM |
534 | directory. If DEFAULT_ONLY is true, the search is restricted to |
535 | the default script location. */ | |
252b5132 | 536 | |
279e75dc | 537 | static FILE * |
f4a23d42 AM |
538 | ldfile_find_command_file (const char *name, |
539 | bfd_boolean default_only, | |
540 | bfd_boolean *sysrooted) | |
252b5132 RH |
541 | { |
542 | search_dirs_type *search; | |
32252ac1 | 543 | FILE *result = NULL; |
f4a23d42 | 544 | char *path; |
a8caa245 | 545 | static search_dirs_type *script_search; |
252b5132 | 546 | |
8c3e16f4 L |
547 | if (!default_only) |
548 | { | |
549 | /* First try raw name. */ | |
f4a23d42 | 550 | result = try_open (name, sysrooted); |
8c3e16f4 L |
551 | if (result != NULL) |
552 | return result; | |
553 | } | |
a8caa245 AM |
554 | |
555 | if (!script_search) | |
1c64c4ed | 556 | { |
a8caa245 AM |
557 | char *script_dir = find_scripts_dir (); |
558 | if (script_dir) | |
1c64c4ed | 559 | { |
a8caa245 AM |
560 | search_dirs_type **save_tail_ptr = search_tail_ptr; |
561 | search_tail_ptr = &script_search; | |
562 | ldfile_add_library_path (script_dir, TRUE); | |
563 | search_tail_ptr = save_tail_ptr; | |
1c64c4ed | 564 | } |
a8caa245 AM |
565 | } |
566 | ||
7d24f02c KH |
567 | /* Temporarily append script_search to the path list so that the |
568 | paths specified with -L will be searched first. */ | |
569 | *search_tail_ptr = script_search; | |
570 | ||
a8caa245 | 571 | /* Try now prefixes. */ |
7d24f02c KH |
572 | for (search = default_only ? script_search : search_head; |
573 | search != NULL; | |
574 | search = search->next) | |
a8caa245 | 575 | { |
f4a23d42 AM |
576 | path = concat (search->name, slash, name, (const char *) NULL); |
577 | result = try_open (path, sysrooted); | |
578 | free (path); | |
a8caa245 AM |
579 | if (result) |
580 | break; | |
252b5132 | 581 | } |
4de2d33d | 582 | |
7d24f02c KH |
583 | /* Restore the original path list. */ |
584 | *search_tail_ptr = NULL; | |
585 | ||
252b5132 RH |
586 | return result; |
587 | } | |
588 | ||
7d24f02c KH |
589 | /* Open command file NAME. */ |
590 | ||
591 | static void | |
592 | ldfile_open_command_file_1 (const char *name, bfd_boolean default_only) | |
252b5132 RH |
593 | { |
594 | FILE *ldlex_input_stack; | |
f4a23d42 AM |
595 | bfd_boolean sysrooted; |
596 | ||
597 | ldlex_input_stack = ldfile_find_command_file (name, default_only, &sysrooted); | |
252b5132 | 598 | |
1579bae1 | 599 | if (ldlex_input_stack == NULL) |
1c64c4ed NC |
600 | { |
601 | bfd_set_error (bfd_error_system_call); | |
602 | einfo (_("%P%F: cannot open linker script file %s: %E\n"), name); | |
3ab6909a | 603 | return; |
1c64c4ed | 604 | } |
4de2d33d | 605 | |
f4a23d42 | 606 | lex_push_file (ldlex_input_stack, name, sysrooted); |
4de2d33d | 607 | |
252b5132 | 608 | lineno = 1; |
b7a26f91 | 609 | |
b9a8de1e | 610 | saved_script_handle = ldlex_input_stack; |
252b5132 RH |
611 | } |
612 | ||
7d24f02c KH |
613 | /* Open command file NAME in the current directory, -L directories, |
614 | the default script location, in that order. */ | |
615 | ||
616 | void | |
617 | ldfile_open_command_file (const char *name) | |
618 | { | |
619 | ldfile_open_command_file_1 (name, FALSE); | |
620 | } | |
621 | ||
622 | /* Open command file NAME at the default script location. */ | |
623 | ||
624 | void | |
625 | ldfile_open_default_command_file (const char *name) | |
626 | { | |
627 | ldfile_open_command_file_1 (name, TRUE); | |
628 | } | |
629 | ||
252b5132 | 630 | void |
1579bae1 | 631 | ldfile_add_arch (const char *in_name) |
252b5132 | 632 | { |
d1b2b2dc | 633 | char *name = xstrdup (in_name); |
d3ce72d0 NC |
634 | search_arch_type *new_arch = (search_arch_type *) |
635 | xmalloc (sizeof (search_arch_type)); | |
252b5132 RH |
636 | |
637 | ldfile_output_machine_name = in_name; | |
638 | ||
d3ce72d0 NC |
639 | new_arch->name = name; |
640 | new_arch->next = NULL; | |
252b5132 RH |
641 | while (*name) |
642 | { | |
3882b010 | 643 | *name = TOLOWER (*name); |
252b5132 RH |
644 | name++; |
645 | } | |
d3ce72d0 NC |
646 | *search_arch_tail_ptr = new_arch; |
647 | search_arch_tail_ptr = &new_arch->next; | |
252b5132 RH |
648 | |
649 | } | |
252b5132 | 650 | |
89cdebba KH |
651 | /* Set the output architecture. */ |
652 | ||
252b5132 | 653 | void |
5e2f1575 | 654 | ldfile_set_output_arch (const char *string, enum bfd_architecture defarch) |
252b5132 | 655 | { |
1c64c4ed NC |
656 | const bfd_arch_info_type *arch = bfd_scan_arch (string); |
657 | ||
658 | if (arch) | |
659 | { | |
660 | ldfile_output_architecture = arch->arch; | |
661 | ldfile_output_machine = arch->mach; | |
662 | ldfile_output_machine_name = arch->printable_name; | |
663 | } | |
5e2f1575 AM |
664 | else if (defarch != bfd_arch_unknown) |
665 | ldfile_output_architecture = defarch; | |
1c64c4ed | 666 | else |
5e2f1575 | 667 | einfo (_("%P%F: cannot represent machine `%s'\n"), string); |
252b5132 | 668 | } |