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