Commit | Line | Data |
---|---|---|
a9998805 | 1 | /* Linker file opening and searching. |
6f2750fe | 2 | Copyright (C) 1991-2016 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 | |
0aa7f586 AM |
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; | |
0aa7f586 | 45 | search_dirs_type *search_head; |
252b5132 | 46 | |
252b5132 | 47 | #ifdef VMS |
0aa7f586 | 48 | static char *slash = ""; |
252b5132 | 49 | #else |
0aa7f586 AM |
50 | #if defined (_WIN32) && !defined (__CYGWIN32__) |
51 | static char *slash = "\\"; | |
252b5132 | 52 | #else |
0aa7f586 | 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 | ||
f6fe1ccd L |
145 | /* This is a linker input BFD. */ |
146 | entry->the_bfd->is_linker_input = 1; | |
147 | ||
ce875075 AM |
148 | #ifdef ENABLE_PLUGINS |
149 | if (entry->flags.lto_output) | |
150 | entry->the_bfd->lto_output = 1; | |
151 | #endif | |
152 | ||
b90d1146 ILT |
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 | |
66be1055 | 164 | if (entry->flags.search_dirs || !entry->flags.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 | { |
0aa7f586 | 175 | if (!bfd_check_format (check, bfd_object)) |
599917b8 JJ |
176 | { |
177 | if (check == entry->the_bfd | |
66be1055 | 178 | && entry->flags.search_dirs |
599917b8 | 179 | && bfd_get_error () == bfd_error_file_not_recognized |
0aa7f586 | 180 | && !ldemul_unrecognized_file (entry)) |
599917b8 JJ |
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 | |
66be1055 | 274 | if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0) |
6c0c5b1e AM |
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 | ||
66be1055 | 283 | if (entry->flags.search_dirs |
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. */ |
0aa7f586 AM |
287 | && !(bfd_get_flavour (check) == bfd_target_xcoff_flavour |
288 | && (bfd_get_flavour (link_info.output_bfd) | |
289 | == bfd_target_xcoff_flavour) | |
290 | && bfd_check_format (entry->the_bfd, bfd_archive))) | |
a9998805 | 291 | { |
fe7929ce AM |
292 | if (command_line.warn_search_mismatch) |
293 | einfo (_("%P: skipping incompatible %s " | |
294 | "when searching for %s\n"), | |
295 | attempt, entry->local_sym_name); | |
a9998805 ILT |
296 | bfd_close (entry->the_bfd); |
297 | entry->the_bfd = NULL; | |
b34976b6 | 298 | return FALSE; |
a9998805 | 299 | } |
b90d1146 ILT |
300 | } |
301 | } | |
5d3236ee DK |
302 | success: |
303 | #ifdef ENABLE_PLUGINS | |
304 | /* If plugins are active, they get first chance to claim | |
305 | any successfully-opened input file. We skip archives | |
306 | here; the plugin wants us to offer it the individual | |
307 | members when we enumerate them, not the whole file. We | |
308 | also ignore corefiles, because that's just weird. It is | |
309 | a needed side-effect of calling bfd_check_format with | |
310 | bfd_object that it sets the bfd's arch and mach, which | |
311 | will be needed when and if we want to bfd_create a new | |
312 | one using this one as a template. */ | |
1d5b29cf L |
313 | if (link_info.lto_plugin_active |
314 | && !no_more_claiming | |
315 | && bfd_check_format (entry->the_bfd, bfd_object)) | |
35a1e5f3 | 316 | plugin_maybe_claim (entry); |
5d3236ee | 317 | #endif /* ENABLE_PLUGINS */ |
b90d1146 | 318 | |
5d3236ee DK |
319 | /* It opened OK, the format checked out, and the plugins have had |
320 | their chance to claim it, so this is success. */ | |
b34976b6 | 321 | return TRUE; |
252b5132 RH |
322 | } |
323 | ||
324 | /* Search for and open the file specified by ENTRY. If it is an | |
325 | archive, use ARCH, LIB and SUFFIX to modify the file name. */ | |
326 | ||
b34976b6 | 327 | bfd_boolean |
1579bae1 AM |
328 | ldfile_open_file_search (const char *arch, |
329 | lang_input_statement_type *entry, | |
330 | const char *lib, | |
331 | const char *suffix) | |
252b5132 RH |
332 | { |
333 | search_dirs_type *search; | |
334 | ||
335 | /* If this is not an archive, try to open it in the current | |
336 | directory first. */ | |
0aa7f586 | 337 | if (!entry->flags.maybe_archive) |
252b5132 | 338 | { |
66be1055 | 339 | if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename)) |
e3f2db7f AO |
340 | { |
341 | char *name = concat (ld_sysroot, entry->filename, | |
342 | (const char *) NULL); | |
343 | if (ldfile_try_open_bfd (name, entry)) | |
344 | { | |
345 | entry->filename = name; | |
346 | return TRUE; | |
347 | } | |
348 | free (name); | |
349 | } | |
350 | else if (ldfile_try_open_bfd (entry->filename, entry)) | |
f4a23d42 | 351 | return TRUE; |
3fe38064 NC |
352 | |
353 | if (IS_ABSOLUTE_PATH (entry->filename)) | |
354 | return FALSE; | |
252b5132 RH |
355 | } |
356 | ||
1579bae1 | 357 | for (search = search_head; search != NULL; search = search->next) |
252b5132 RH |
358 | { |
359 | char *string; | |
360 | ||
0e1862bb | 361 | if (entry->flags.dynamic && !bfd_link_relocatable (&link_info)) |
252b5132 RH |
362 | { |
363 | if (ldemul_open_dynamic_archive (arch, search, entry)) | |
f4a23d42 | 364 | return TRUE; |
252b5132 RH |
365 | } |
366 | ||
d4ae5fb0 | 367 | if (entry->flags.maybe_archive && !entry->flags.full_name_provided) |
a26cc967 AM |
368 | string = concat (search->name, slash, lib, entry->filename, |
369 | arch, suffix, (const char *) NULL); | |
252b5132 | 370 | else |
a26cc967 AM |
371 | string = concat (search->name, slash, entry->filename, |
372 | (const char *) 0); | |
252b5132 RH |
373 | |
374 | if (ldfile_try_open_bfd (string, entry)) | |
375 | { | |
b90d1146 | 376 | entry->filename = string; |
b34976b6 | 377 | return TRUE; |
252b5132 RH |
378 | } |
379 | ||
380 | free (string); | |
381 | } | |
382 | ||
b34976b6 | 383 | return FALSE; |
252b5132 RH |
384 | } |
385 | ||
c4b78195 NC |
386 | /* Open the input file specified by ENTRY. |
387 | PR 4437: Do not stop on the first missing file, but | |
388 | continue processing other input files in case there | |
389 | are more errors to report. */ | |
252b5132 RH |
390 | |
391 | void | |
1579bae1 | 392 | ldfile_open_file (lang_input_statement_type *entry) |
252b5132 RH |
393 | { |
394 | if (entry->the_bfd != NULL) | |
395 | return; | |
396 | ||
0aa7f586 | 397 | if (!entry->flags.search_dirs) |
252b5132 RH |
398 | { |
399 | if (ldfile_try_open_bfd (entry->filename, entry)) | |
400 | return; | |
c4b78195 | 401 | |
42627821 | 402 | if (filename_cmp (entry->filename, entry->local_sym_name) != 0) |
c4b78195 | 403 | einfo (_("%P: cannot find %s (%s): %E\n"), |
252b5132 RH |
404 | entry->filename, entry->local_sym_name); |
405 | else | |
c4b78195 NC |
406 | einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name); |
407 | ||
66be1055 AM |
408 | entry->flags.missing_file = TRUE; |
409 | input_flags.missing_file = TRUE; | |
252b5132 RH |
410 | } |
411 | else | |
412 | { | |
413 | search_arch_type *arch; | |
b34976b6 | 414 | bfd_boolean found = FALSE; |
252b5132 RH |
415 | |
416 | /* Try to open <filename><suffix> or lib<filename><suffix>.a */ | |
1579bae1 | 417 | for (arch = search_arch_head; arch != NULL; arch = arch->next) |
252b5132 | 418 | { |
78f85fd7 L |
419 | found = ldfile_open_file_search (arch->name, entry, "lib", ".a"); |
420 | if (found) | |
421 | break; | |
252b5132 | 422 | #ifdef VMS |
78f85fd7 L |
423 | found = ldfile_open_file_search (arch->name, entry, ":lib", ".a"); |
424 | if (found) | |
425 | break; | |
252b5132 | 426 | #endif |
78f85fd7 L |
427 | found = ldemul_find_potential_libraries (arch->name, entry); |
428 | if (found) | |
429 | break; | |
252b5132 | 430 | } |
4de2d33d | 431 | |
78f85fd7 L |
432 | /* If we have found the file, we don't need to search directories |
433 | again. */ | |
434 | if (found) | |
66be1055 | 435 | entry->flags.search_dirs = FALSE; |
c4b78195 NC |
436 | else |
437 | { | |
66be1055 | 438 | if (entry->flags.sysrooted |
3fe38064 NC |
439 | && ld_sysroot |
440 | && IS_ABSOLUTE_PATH (entry->local_sym_name)) | |
c4b78195 NC |
441 | einfo (_("%P: cannot find %s inside %s\n"), |
442 | entry->local_sym_name, ld_sysroot); | |
443 | else | |
444 | einfo (_("%P: cannot find %s\n"), entry->local_sym_name); | |
66be1055 AM |
445 | entry->flags.missing_file = TRUE; |
446 | input_flags.missing_file = TRUE; | |
c4b78195 | 447 | } |
252b5132 RH |
448 | } |
449 | } | |
450 | ||
f4a23d42 | 451 | /* Try to open NAME. */ |
252b5132 RH |
452 | |
453 | static FILE * | |
f4a23d42 | 454 | try_open (const char *name, bfd_boolean *sysrooted) |
252b5132 RH |
455 | { |
456 | FILE *result; | |
252b5132 RH |
457 | |
458 | result = fopen (name, "r"); | |
4de2d33d | 459 | |
f4a23d42 AM |
460 | if (result != NULL) |
461 | *sysrooted = is_sysrooted_pathname (name); | |
462 | ||
cd6f1cf3 | 463 | if (verbose) |
252b5132 RH |
464 | { |
465 | if (result == NULL) | |
466 | info_msg (_("cannot find script file %s\n"), name); | |
467 | else | |
468 | info_msg (_("opened script file %s\n"), name); | |
469 | } | |
470 | ||
252b5132 RH |
471 | return result; |
472 | } | |
473 | ||
a8caa245 AM |
474 | /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */ |
475 | ||
476 | static bfd_boolean | |
477 | check_for_scripts_dir (char *dir) | |
478 | { | |
479 | char *buf; | |
480 | struct stat s; | |
481 | bfd_boolean res; | |
482 | ||
483 | buf = concat (dir, "/ldscripts", (const char *) NULL); | |
484 | res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); | |
485 | free (buf); | |
486 | return res; | |
487 | } | |
488 | ||
489 | /* Return the default directory for finding script files. | |
490 | We look for the "ldscripts" directory in: | |
491 | ||
492 | SCRIPTDIR (passed from Makefile) | |
493 | (adjusted according to the current location of the binary) | |
c38b10fa | 494 | the dir where this program is (for using it from the build tree). */ |
a8caa245 AM |
495 | |
496 | static char * | |
497 | find_scripts_dir (void) | |
498 | { | |
c38b10fa | 499 | char *dir; |
a8caa245 AM |
500 | |
501 | dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR); | |
502 | if (dir) | |
503 | { | |
504 | if (check_for_scripts_dir (dir)) | |
505 | return dir; | |
506 | free (dir); | |
507 | } | |
508 | ||
509 | dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR); | |
510 | if (dir) | |
511 | { | |
512 | if (check_for_scripts_dir (dir)) | |
513 | return dir; | |
514 | free (dir); | |
515 | } | |
516 | ||
a8caa245 | 517 | /* Look for "ldscripts" in the dir where our binary is. */ |
c38b10fa AM |
518 | dir = make_relative_prefix (program_name, ".", "."); |
519 | if (dir) | |
520 | { | |
521 | if (check_for_scripts_dir (dir)) | |
522 | return dir; | |
523 | free (dir); | |
524 | } | |
a8caa245 | 525 | |
a8caa245 AM |
526 | return NULL; |
527 | } | |
528 | ||
8c3e16f4 L |
529 | /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for |
530 | it in directories specified with -L, then in the default script | |
f4a23d42 AM |
531 | directory. If DEFAULT_ONLY is true, the search is restricted to |
532 | the default script location. */ | |
252b5132 | 533 | |
279e75dc | 534 | static FILE * |
f4a23d42 AM |
535 | ldfile_find_command_file (const char *name, |
536 | bfd_boolean default_only, | |
537 | bfd_boolean *sysrooted) | |
252b5132 RH |
538 | { |
539 | search_dirs_type *search; | |
32252ac1 | 540 | FILE *result = NULL; |
f4a23d42 | 541 | char *path; |
a8caa245 | 542 | static search_dirs_type *script_search; |
252b5132 | 543 | |
8c3e16f4 L |
544 | if (!default_only) |
545 | { | |
546 | /* First try raw name. */ | |
f4a23d42 | 547 | result = try_open (name, sysrooted); |
8c3e16f4 L |
548 | if (result != NULL) |
549 | return result; | |
550 | } | |
a8caa245 AM |
551 | |
552 | if (!script_search) | |
1c64c4ed | 553 | { |
a8caa245 AM |
554 | char *script_dir = find_scripts_dir (); |
555 | if (script_dir) | |
1c64c4ed | 556 | { |
a8caa245 AM |
557 | search_dirs_type **save_tail_ptr = search_tail_ptr; |
558 | search_tail_ptr = &script_search; | |
559 | ldfile_add_library_path (script_dir, TRUE); | |
560 | search_tail_ptr = save_tail_ptr; | |
1c64c4ed | 561 | } |
a8caa245 AM |
562 | } |
563 | ||
7d24f02c KH |
564 | /* Temporarily append script_search to the path list so that the |
565 | paths specified with -L will be searched first. */ | |
566 | *search_tail_ptr = script_search; | |
567 | ||
a8caa245 | 568 | /* Try now prefixes. */ |
7d24f02c KH |
569 | for (search = default_only ? script_search : search_head; |
570 | search != NULL; | |
571 | search = search->next) | |
a8caa245 | 572 | { |
f4a23d42 AM |
573 | path = concat (search->name, slash, name, (const char *) NULL); |
574 | result = try_open (path, sysrooted); | |
575 | free (path); | |
a8caa245 AM |
576 | if (result) |
577 | break; | |
252b5132 | 578 | } |
4de2d33d | 579 | |
7d24f02c KH |
580 | /* Restore the original path list. */ |
581 | *search_tail_ptr = NULL; | |
582 | ||
252b5132 RH |
583 | return result; |
584 | } | |
585 | ||
7d24f02c KH |
586 | /* Open command file NAME. */ |
587 | ||
588 | static void | |
589 | ldfile_open_command_file_1 (const char *name, bfd_boolean default_only) | |
252b5132 RH |
590 | { |
591 | FILE *ldlex_input_stack; | |
f4a23d42 AM |
592 | bfd_boolean sysrooted; |
593 | ||
594 | ldlex_input_stack = ldfile_find_command_file (name, default_only, &sysrooted); | |
252b5132 | 595 | |
1579bae1 | 596 | if (ldlex_input_stack == NULL) |
1c64c4ed NC |
597 | { |
598 | bfd_set_error (bfd_error_system_call); | |
599 | einfo (_("%P%F: cannot open linker script file %s: %E\n"), name); | |
3ab6909a | 600 | return; |
1c64c4ed | 601 | } |
4de2d33d | 602 | |
f4a23d42 | 603 | lex_push_file (ldlex_input_stack, name, sysrooted); |
4de2d33d | 604 | |
252b5132 | 605 | lineno = 1; |
b7a26f91 | 606 | |
b9a8de1e | 607 | saved_script_handle = ldlex_input_stack; |
252b5132 RH |
608 | } |
609 | ||
7d24f02c KH |
610 | /* Open command file NAME in the current directory, -L directories, |
611 | the default script location, in that order. */ | |
612 | ||
613 | void | |
614 | ldfile_open_command_file (const char *name) | |
615 | { | |
616 | ldfile_open_command_file_1 (name, FALSE); | |
617 | } | |
618 | ||
619 | /* Open command file NAME at the default script location. */ | |
620 | ||
621 | void | |
622 | ldfile_open_default_command_file (const char *name) | |
623 | { | |
624 | ldfile_open_command_file_1 (name, TRUE); | |
625 | } | |
626 | ||
252b5132 | 627 | void |
1579bae1 | 628 | ldfile_add_arch (const char *in_name) |
252b5132 | 629 | { |
d1b2b2dc | 630 | char *name = xstrdup (in_name); |
0aa7f586 AM |
631 | search_arch_type *new_arch |
632 | = (search_arch_type *) xmalloc (sizeof (search_arch_type)); | |
252b5132 RH |
633 | |
634 | ldfile_output_machine_name = in_name; | |
635 | ||
d3ce72d0 NC |
636 | new_arch->name = name; |
637 | new_arch->next = NULL; | |
252b5132 RH |
638 | while (*name) |
639 | { | |
3882b010 | 640 | *name = TOLOWER (*name); |
252b5132 RH |
641 | name++; |
642 | } | |
d3ce72d0 NC |
643 | *search_arch_tail_ptr = new_arch; |
644 | search_arch_tail_ptr = &new_arch->next; | |
252b5132 RH |
645 | |
646 | } | |
252b5132 | 647 | |
89cdebba KH |
648 | /* Set the output architecture. */ |
649 | ||
252b5132 | 650 | void |
5e2f1575 | 651 | ldfile_set_output_arch (const char *string, enum bfd_architecture defarch) |
252b5132 | 652 | { |
1c64c4ed NC |
653 | const bfd_arch_info_type *arch = bfd_scan_arch (string); |
654 | ||
655 | if (arch) | |
656 | { | |
657 | ldfile_output_architecture = arch->arch; | |
658 | ldfile_output_machine = arch->mach; | |
659 | ldfile_output_machine_name = arch->printable_name; | |
660 | } | |
5e2f1575 AM |
661 | else if (defarch != bfd_arch_unknown) |
662 | ldfile_output_architecture = defarch; | |
1c64c4ed | 663 | else |
5e2f1575 | 664 | einfo (_("%P%F: cannot represent machine `%s'\n"), string); |
252b5132 | 665 | } |