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