Commit | Line | Data |
---|---|---|
a9998805 | 1 | /* Linker file opening and searching. |
b3adc24a | 2 | Copyright (C) 1991-2020 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" |
1ff6de03 | 24 | #include "ctf-api.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 | |
0aa7f586 AM |
42 | bfd_boolean ldfile_assumed_script = FALSE; |
43 | const char *ldfile_output_machine_name = ""; | |
252b5132 RH |
44 | unsigned long ldfile_output_machine; |
45 | enum bfd_architecture ldfile_output_architecture; | |
0aa7f586 | 46 | search_dirs_type *search_head; |
252b5132 | 47 | |
252b5132 | 48 | #ifdef VMS |
0aa7f586 | 49 | static char *slash = ""; |
252b5132 | 50 | #else |
0aa7f586 AM |
51 | #if defined (_WIN32) && !defined (__CYGWIN32__) |
52 | static char *slash = "\\"; | |
252b5132 | 53 | #else |
0aa7f586 | 54 | static char *slash = "/"; |
252b5132 RH |
55 | #endif |
56 | #endif | |
252b5132 | 57 | |
3fe38064 NC |
58 | typedef struct search_arch |
59 | { | |
4de2d33d | 60 | char *name; |
252b5132 RH |
61 | struct search_arch *next; |
62 | } search_arch_type; | |
63 | ||
3fe38064 | 64 | static search_dirs_type **search_tail_ptr = &search_head; |
252b5132 RH |
65 | static search_arch_type *search_arch_head; |
66 | static search_arch_type **search_arch_tail_ptr = &search_arch_head; | |
4de2d33d | 67 | |
3fe38064 NC |
68 | /* Test whether a pathname, after canonicalization, is the same or a |
69 | sub-directory of the sysroot directory. */ | |
70 | ||
71 | static bfd_boolean | |
f4a23d42 | 72 | is_sysrooted_pathname (const char *name) |
3fe38064 | 73 | { |
66be1055 | 74 | char *realname; |
3fe38064 NC |
75 | int len; |
76 | bfd_boolean result; | |
77 | ||
66be1055 | 78 | if (ld_canon_sysroot == NULL) |
3fe38064 | 79 | return FALSE; |
5e2f1575 | 80 | |
66be1055 | 81 | realname = lrealpath (name); |
3fe38064 | 82 | len = strlen (realname); |
66be1055 | 83 | result = FALSE; |
f4a23d42 AM |
84 | if (len > ld_canon_sysroot_len |
85 | && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len])) | |
66be1055 | 86 | { |
66be1055 | 87 | realname[ld_canon_sysroot_len] = '\0'; |
f4a23d42 | 88 | result = FILENAME_CMP (ld_canon_sysroot, realname) == 0; |
66be1055 | 89 | } |
3fe38064 | 90 | |
66be1055 | 91 | free (realname); |
3fe38064 NC |
92 | return result; |
93 | } | |
252b5132 | 94 | |
5ed6aba4 NC |
95 | /* Adds NAME to the library search path. |
96 | Makes a copy of NAME using xmalloc(). */ | |
97 | ||
252b5132 | 98 | void |
1579bae1 | 99 | ldfile_add_library_path (const char *name, bfd_boolean cmdline) |
252b5132 | 100 | { |
d3ce72d0 | 101 | search_dirs_type *new_dirs; |
252b5132 | 102 | |
361b220e CD |
103 | if (!cmdline && config.only_cmd_line_lib_dirs) |
104 | return; | |
105 | ||
d3ce72d0 NC |
106 | new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type)); |
107 | new_dirs->next = NULL; | |
108 | new_dirs->cmdline = cmdline; | |
109 | *search_tail_ptr = new_dirs; | |
110 | search_tail_ptr = &new_dirs->next; | |
9c8ebd6a DJ |
111 | |
112 | /* If a directory is marked as honoring sysroot, prepend the sysroot path | |
113 | now. */ | |
5ed6aba4 | 114 | if (name[0] == '=') |
f4a23d42 | 115 | new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL); |
3aa2d05a NC |
116 | else if (CONST_STRNEQ (name, "$SYSROOT")) |
117 | new_dirs->name = concat (ld_sysroot, name + strlen ("$SYSROOT"), (const char *) NULL); | |
e3f2db7f | 118 | else |
f4a23d42 | 119 | new_dirs->name = xstrdup (name); |
252b5132 RH |
120 | } |
121 | ||
122 | /* Try to open a BFD for a lang_input_statement. */ | |
123 | ||
b34976b6 | 124 | bfd_boolean |
1579bae1 AM |
125 | ldfile_try_open_bfd (const char *attempt, |
126 | lang_input_statement_type *entry) | |
252b5132 RH |
127 | { |
128 | entry->the_bfd = bfd_openr (attempt, entry->target); | |
129 | ||
cd6f1cf3 | 130 | if (verbose) |
252b5132 RH |
131 | { |
132 | if (entry->the_bfd == NULL) | |
133 | info_msg (_("attempt to open %s failed\n"), attempt); | |
134 | else | |
135 | info_msg (_("attempt to open %s succeeded\n"), attempt); | |
136 | } | |
137 | ||
b90d1146 | 138 | if (entry->the_bfd == NULL) |
252b5132 RH |
139 | { |
140 | if (bfd_get_error () == bfd_error_invalid_target) | |
141 | einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target); | |
b34976b6 | 142 | return FALSE; |
252b5132 | 143 | } |
b90d1146 | 144 | |
4a114e3e L |
145 | /* Linker needs to decompress sections. */ |
146 | entry->the_bfd->flags |= BFD_DECOMPRESS; | |
147 | ||
f6fe1ccd L |
148 | /* This is a linker input BFD. */ |
149 | entry->the_bfd->is_linker_input = 1; | |
150 | ||
ce875075 AM |
151 | #ifdef ENABLE_PLUGINS |
152 | if (entry->flags.lto_output) | |
153 | entry->the_bfd->lto_output = 1; | |
154 | #endif | |
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 | |
66be1055 | 167 | if (entry->flags.search_dirs || !entry->flags.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 | { |
0aa7f586 | 178 | if (!bfd_check_format (check, bfd_object)) |
599917b8 JJ |
179 | { |
180 | if (check == entry->the_bfd | |
66be1055 | 181 | && entry->flags.search_dirs |
599917b8 | 182 | && bfd_get_error () == bfd_error_file_not_recognized |
0aa7f586 | 183 | && !ldemul_unrecognized_file (entry)) |
599917b8 JJ |
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. */ | |
82d7a6f4 | 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); | |
5e2ab612 AM |
243 | free (arg2); |
244 | free (arg3); | |
599917b8 JJ |
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: | |
5e2ab612 | 253 | free (yylval.bigint.str); |
599917b8 | 254 | break; |
5e2f1575 | 255 | } |
599917b8 JJ |
256 | token = yylex (); |
257 | } | |
4f39e302 | 258 | ldlex_popstate (); |
b34976b6 | 259 | ldfile_assumed_script = FALSE; |
599917b8 JJ |
260 | fclose (yyin); |
261 | yyin = NULL; | |
262 | if (skip) | |
263 | { | |
fe7929ce AM |
264 | if (command_line.warn_search_mismatch) |
265 | einfo (_("%P: skipping incompatible %s " | |
266 | "when searching for %s\n"), | |
267 | attempt, entry->local_sym_name); | |
599917b8 JJ |
268 | bfd_close (entry->the_bfd); |
269 | entry->the_bfd = NULL; | |
b34976b6 | 270 | return FALSE; |
599917b8 JJ |
271 | } |
272 | } | |
5d3236ee | 273 | goto success; |
599917b8 | 274 | } |
f1f0d9ab | 275 | |
66be1055 | 276 | if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0) |
6c0c5b1e AM |
277 | { |
278 | einfo (_("%F%P: attempted static link of dynamic object `%s'\n"), | |
279 | attempt); | |
280 | bfd_close (entry->the_bfd); | |
281 | entry->the_bfd = NULL; | |
282 | return FALSE; | |
283 | } | |
284 | ||
66be1055 | 285 | if (entry->flags.search_dirs |
f13a99db | 286 | && !bfd_arch_get_compatible (check, link_info.output_bfd, |
6c0c5b1e | 287 | command_line.accept_unknown_input_arch) |
312b768e | 288 | /* XCOFF archives can have 32 and 64 bit objects. */ |
0aa7f586 AM |
289 | && !(bfd_get_flavour (check) == bfd_target_xcoff_flavour |
290 | && (bfd_get_flavour (link_info.output_bfd) | |
291 | == bfd_target_xcoff_flavour) | |
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 | } | |
dc1e8a47 | 304 | success: |
5d3236ee DK |
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. */ | |
1d5b29cf L |
315 | if (link_info.lto_plugin_active |
316 | && !no_more_claiming | |
317 | && bfd_check_format (entry->the_bfd, bfd_object)) | |
35a1e5f3 | 318 | plugin_maybe_claim (entry); |
5d3236ee | 319 | #endif /* ENABLE_PLUGINS */ |
b90d1146 | 320 | |
5d3236ee DK |
321 | /* It opened OK, the format checked out, and the plugins have had |
322 | their chance to claim it, so this is success. */ | |
b34976b6 | 323 | return TRUE; |
252b5132 RH |
324 | } |
325 | ||
326 | /* Search for and open the file specified by ENTRY. If it is an | |
327 | archive, use ARCH, LIB and SUFFIX to modify the file name. */ | |
328 | ||
b34976b6 | 329 | bfd_boolean |
1579bae1 AM |
330 | ldfile_open_file_search (const char *arch, |
331 | lang_input_statement_type *entry, | |
332 | const char *lib, | |
333 | const char *suffix) | |
252b5132 RH |
334 | { |
335 | search_dirs_type *search; | |
336 | ||
337 | /* If this is not an archive, try to open it in the current | |
338 | directory first. */ | |
0aa7f586 | 339 | if (!entry->flags.maybe_archive) |
252b5132 | 340 | { |
66be1055 | 341 | if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename)) |
e3f2db7f AO |
342 | { |
343 | char *name = concat (ld_sysroot, entry->filename, | |
344 | (const char *) NULL); | |
345 | if (ldfile_try_open_bfd (name, entry)) | |
346 | { | |
347 | entry->filename = name; | |
348 | return TRUE; | |
349 | } | |
350 | free (name); | |
351 | } | |
352 | else if (ldfile_try_open_bfd (entry->filename, entry)) | |
f4a23d42 | 353 | return TRUE; |
3fe38064 NC |
354 | |
355 | if (IS_ABSOLUTE_PATH (entry->filename)) | |
356 | return FALSE; | |
252b5132 RH |
357 | } |
358 | ||
1579bae1 | 359 | for (search = search_head; search != NULL; search = search->next) |
252b5132 RH |
360 | { |
361 | char *string; | |
362 | ||
0e1862bb | 363 | if (entry->flags.dynamic && !bfd_link_relocatable (&link_info)) |
252b5132 RH |
364 | { |
365 | if (ldemul_open_dynamic_archive (arch, search, entry)) | |
f4a23d42 | 366 | return TRUE; |
252b5132 RH |
367 | } |
368 | ||
d4ae5fb0 | 369 | if (entry->flags.maybe_archive && !entry->flags.full_name_provided) |
a26cc967 AM |
370 | string = concat (search->name, slash, lib, entry->filename, |
371 | arch, suffix, (const char *) NULL); | |
252b5132 | 372 | else |
a26cc967 AM |
373 | string = concat (search->name, slash, entry->filename, |
374 | (const char *) 0); | |
252b5132 RH |
375 | |
376 | if (ldfile_try_open_bfd (string, entry)) | |
377 | { | |
b90d1146 | 378 | entry->filename = string; |
b34976b6 | 379 | return TRUE; |
252b5132 RH |
380 | } |
381 | ||
382 | free (string); | |
383 | } | |
384 | ||
b34976b6 | 385 | return FALSE; |
252b5132 RH |
386 | } |
387 | ||
c4b78195 NC |
388 | /* Open the input file specified by ENTRY. |
389 | PR 4437: Do not stop on the first missing file, but | |
390 | continue processing other input files in case there | |
391 | are more errors to report. */ | |
252b5132 RH |
392 | |
393 | void | |
1579bae1 | 394 | ldfile_open_file (lang_input_statement_type *entry) |
252b5132 RH |
395 | { |
396 | if (entry->the_bfd != NULL) | |
397 | return; | |
398 | ||
0aa7f586 | 399 | if (!entry->flags.search_dirs) |
252b5132 RH |
400 | { |
401 | if (ldfile_try_open_bfd (entry->filename, entry)) | |
402 | return; | |
c4b78195 | 403 | |
42627821 | 404 | if (filename_cmp (entry->filename, entry->local_sym_name) != 0) |
c4b78195 | 405 | einfo (_("%P: cannot find %s (%s): %E\n"), |
252b5132 RH |
406 | entry->filename, entry->local_sym_name); |
407 | else | |
c4b78195 NC |
408 | einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name); |
409 | ||
66be1055 AM |
410 | entry->flags.missing_file = TRUE; |
411 | input_flags.missing_file = TRUE; | |
252b5132 RH |
412 | } |
413 | else | |
414 | { | |
415 | search_arch_type *arch; | |
b34976b6 | 416 | bfd_boolean found = FALSE; |
252b5132 | 417 | |
16171946 FS |
418 | /* If extra_search_path is set, entry->filename is a relative path. |
419 | Search the directory of the current linker script before searching | |
420 | other paths. */ | |
421 | if (entry->extra_search_path) | |
422 | { | |
423 | char *path = concat (entry->extra_search_path, slash, entry->filename, | |
424 | (const char *)0); | |
425 | if (ldfile_try_open_bfd (path, entry)) | |
426 | { | |
427 | entry->filename = path; | |
428 | entry->flags.search_dirs = FALSE; | |
429 | return; | |
430 | } | |
431 | ||
432 | free (path); | |
433 | } | |
434 | ||
435 | /* Try to open <filename><suffix> or lib<filename><suffix>.a. */ | |
1579bae1 | 436 | for (arch = search_arch_head; arch != NULL; arch = arch->next) |
252b5132 | 437 | { |
78f85fd7 L |
438 | found = ldfile_open_file_search (arch->name, entry, "lib", ".a"); |
439 | if (found) | |
440 | break; | |
252b5132 | 441 | #ifdef VMS |
78f85fd7 L |
442 | found = ldfile_open_file_search (arch->name, entry, ":lib", ".a"); |
443 | if (found) | |
444 | break; | |
252b5132 | 445 | #endif |
78f85fd7 L |
446 | found = ldemul_find_potential_libraries (arch->name, entry); |
447 | if (found) | |
448 | break; | |
252b5132 | 449 | } |
4de2d33d | 450 | |
78f85fd7 L |
451 | /* If we have found the file, we don't need to search directories |
452 | again. */ | |
453 | if (found) | |
66be1055 | 454 | entry->flags.search_dirs = FALSE; |
c4b78195 NC |
455 | else |
456 | { | |
66be1055 | 457 | if (entry->flags.sysrooted |
3fe38064 NC |
458 | && ld_sysroot |
459 | && IS_ABSOLUTE_PATH (entry->local_sym_name)) | |
c4b78195 NC |
460 | einfo (_("%P: cannot find %s inside %s\n"), |
461 | entry->local_sym_name, ld_sysroot); | |
462 | else | |
463 | einfo (_("%P: cannot find %s\n"), entry->local_sym_name); | |
77bf7b53 NC |
464 | |
465 | /* PR 25747: Be kind to users who forgot to add the | |
466 | "lib" prefix to their library when it was created. */ | |
467 | for (arch = search_arch_head; arch != NULL; arch = arch->next) | |
468 | { | |
469 | if (ldfile_open_file_search (arch->name, entry, "", ".a")) | |
470 | { | |
471 | const char * base = lbasename (entry->filename); | |
472 | ||
4f5bd885 | 473 | einfo (_("%P: note to link with %s use -l:%s or rename it to lib%s\n"), |
77bf7b53 NC |
474 | entry->filename, base, base); |
475 | bfd_close (entry->the_bfd); | |
476 | entry->the_bfd = NULL; | |
477 | break; | |
478 | } | |
479 | } | |
66be1055 AM |
480 | entry->flags.missing_file = TRUE; |
481 | input_flags.missing_file = TRUE; | |
c4b78195 | 482 | } |
252b5132 RH |
483 | } |
484 | } | |
485 | ||
f4a23d42 | 486 | /* Try to open NAME. */ |
252b5132 RH |
487 | |
488 | static FILE * | |
f4a23d42 | 489 | try_open (const char *name, bfd_boolean *sysrooted) |
252b5132 RH |
490 | { |
491 | FILE *result; | |
252b5132 RH |
492 | |
493 | result = fopen (name, "r"); | |
4de2d33d | 494 | |
f4a23d42 AM |
495 | if (result != NULL) |
496 | *sysrooted = is_sysrooted_pathname (name); | |
497 | ||
cd6f1cf3 | 498 | if (verbose) |
252b5132 RH |
499 | { |
500 | if (result == NULL) | |
501 | info_msg (_("cannot find script file %s\n"), name); | |
502 | else | |
503 | info_msg (_("opened script file %s\n"), name); | |
504 | } | |
505 | ||
252b5132 RH |
506 | return result; |
507 | } | |
508 | ||
a8caa245 AM |
509 | /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */ |
510 | ||
511 | static bfd_boolean | |
512 | check_for_scripts_dir (char *dir) | |
513 | { | |
514 | char *buf; | |
515 | struct stat s; | |
516 | bfd_boolean res; | |
517 | ||
518 | buf = concat (dir, "/ldscripts", (const char *) NULL); | |
519 | res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); | |
520 | free (buf); | |
521 | return res; | |
522 | } | |
523 | ||
524 | /* Return the default directory for finding script files. | |
525 | We look for the "ldscripts" directory in: | |
526 | ||
527 | SCRIPTDIR (passed from Makefile) | |
528 | (adjusted according to the current location of the binary) | |
c38b10fa | 529 | the dir where this program is (for using it from the build tree). */ |
a8caa245 AM |
530 | |
531 | static char * | |
532 | find_scripts_dir (void) | |
533 | { | |
c38b10fa | 534 | char *dir; |
a8caa245 AM |
535 | |
536 | dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR); | |
537 | if (dir) | |
538 | { | |
539 | if (check_for_scripts_dir (dir)) | |
540 | return dir; | |
541 | free (dir); | |
542 | } | |
543 | ||
544 | dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR); | |
545 | if (dir) | |
546 | { | |
547 | if (check_for_scripts_dir (dir)) | |
548 | return dir; | |
549 | free (dir); | |
550 | } | |
551 | ||
a8caa245 | 552 | /* Look for "ldscripts" in the dir where our binary is. */ |
c38b10fa AM |
553 | dir = make_relative_prefix (program_name, ".", "."); |
554 | if (dir) | |
555 | { | |
556 | if (check_for_scripts_dir (dir)) | |
557 | return dir; | |
558 | free (dir); | |
559 | } | |
a8caa245 | 560 | |
a8caa245 AM |
561 | return NULL; |
562 | } | |
563 | ||
8c3e16f4 L |
564 | /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for |
565 | it in directories specified with -L, then in the default script | |
f4a23d42 AM |
566 | directory. If DEFAULT_ONLY is true, the search is restricted to |
567 | the default script location. */ | |
252b5132 | 568 | |
279e75dc | 569 | static FILE * |
f4a23d42 AM |
570 | ldfile_find_command_file (const char *name, |
571 | bfd_boolean default_only, | |
572 | bfd_boolean *sysrooted) | |
252b5132 RH |
573 | { |
574 | search_dirs_type *search; | |
32252ac1 | 575 | FILE *result = NULL; |
f4a23d42 | 576 | char *path; |
a8caa245 | 577 | static search_dirs_type *script_search; |
252b5132 | 578 | |
8c3e16f4 L |
579 | if (!default_only) |
580 | { | |
581 | /* First try raw name. */ | |
f4a23d42 | 582 | result = try_open (name, sysrooted); |
8c3e16f4 L |
583 | if (result != NULL) |
584 | return result; | |
585 | } | |
a8caa245 AM |
586 | |
587 | if (!script_search) | |
1c64c4ed | 588 | { |
a8caa245 AM |
589 | char *script_dir = find_scripts_dir (); |
590 | if (script_dir) | |
1c64c4ed | 591 | { |
a8caa245 AM |
592 | search_dirs_type **save_tail_ptr = search_tail_ptr; |
593 | search_tail_ptr = &script_search; | |
594 | ldfile_add_library_path (script_dir, TRUE); | |
595 | search_tail_ptr = save_tail_ptr; | |
1c64c4ed | 596 | } |
a8caa245 AM |
597 | } |
598 | ||
7d24f02c KH |
599 | /* Temporarily append script_search to the path list so that the |
600 | paths specified with -L will be searched first. */ | |
601 | *search_tail_ptr = script_search; | |
602 | ||
a8caa245 | 603 | /* Try now prefixes. */ |
7d24f02c KH |
604 | for (search = default_only ? script_search : search_head; |
605 | search != NULL; | |
606 | search = search->next) | |
a8caa245 | 607 | { |
f4a23d42 AM |
608 | path = concat (search->name, slash, name, (const char *) NULL); |
609 | result = try_open (path, sysrooted); | |
610 | free (path); | |
a8caa245 AM |
611 | if (result) |
612 | break; | |
252b5132 | 613 | } |
4de2d33d | 614 | |
7d24f02c KH |
615 | /* Restore the original path list. */ |
616 | *search_tail_ptr = NULL; | |
617 | ||
252b5132 RH |
618 | return result; |
619 | } | |
620 | ||
82d7a6f4 AM |
621 | enum script_open_style { |
622 | script_nonT, | |
623 | script_T, | |
624 | script_defaultT | |
625 | }; | |
626 | ||
627 | struct script_name_list | |
628 | { | |
629 | struct script_name_list *next; | |
630 | enum script_open_style open_how; | |
631 | char name[1]; | |
632 | }; | |
633 | ||
7d24f02c KH |
634 | /* Open command file NAME. */ |
635 | ||
636 | static void | |
82d7a6f4 | 637 | ldfile_open_command_file_1 (const char *name, enum script_open_style open_how) |
252b5132 RH |
638 | { |
639 | FILE *ldlex_input_stack; | |
f4a23d42 | 640 | bfd_boolean sysrooted; |
82d7a6f4 AM |
641 | static struct script_name_list *processed_scripts = NULL; |
642 | struct script_name_list *script; | |
643 | size_t len; | |
f4a23d42 | 644 | |
82d7a6f4 AM |
645 | /* PR 24576: Catch the case where the user has accidentally included |
646 | the same linker script twice. */ | |
647 | for (script = processed_scripts; script != NULL; script = script->next) | |
6ec6968b | 648 | { |
82d7a6f4 AM |
649 | if ((open_how != script_nonT || script->open_how != script_nonT) |
650 | && strcmp (name, script->name) == 0) | |
6ec6968b | 651 | { |
82d7a6f4 AM |
652 | einfo (_("%F%P: error: linker script file '%s'" |
653 | " appears multiple times\n"), name); | |
654 | return; | |
6ec6968b | 655 | } |
6ec6968b NC |
656 | } |
657 | ||
82d7a6f4 AM |
658 | /* FIXME: This memory is never freed, but that should not really matter. |
659 | It will be released when the linker exits, and it is unlikely to ever | |
660 | be more than a few tens of bytes. */ | |
661 | len = strlen (name); | |
662 | script = xmalloc (sizeof (*script) + len); | |
663 | script->next = processed_scripts; | |
664 | script->open_how = open_how; | |
665 | memcpy (script->name, name, len + 1); | |
666 | processed_scripts = script; | |
667 | ||
668 | ldlex_input_stack = ldfile_find_command_file (name, | |
669 | open_how == script_defaultT, | |
670 | &sysrooted); | |
1579bae1 | 671 | if (ldlex_input_stack == NULL) |
1c64c4ed NC |
672 | { |
673 | bfd_set_error (bfd_error_system_call); | |
df5f2391 | 674 | einfo (_("%F%P: cannot open linker script file %s: %E\n"), name); |
3ab6909a | 675 | return; |
1c64c4ed | 676 | } |
4de2d33d | 677 | |
f4a23d42 | 678 | lex_push_file (ldlex_input_stack, name, sysrooted); |
4de2d33d | 679 | |
252b5132 | 680 | lineno = 1; |
b7a26f91 | 681 | |
b9a8de1e | 682 | saved_script_handle = ldlex_input_stack; |
252b5132 RH |
683 | } |
684 | ||
7d24f02c KH |
685 | /* Open command file NAME in the current directory, -L directories, |
686 | the default script location, in that order. */ | |
687 | ||
688 | void | |
689 | ldfile_open_command_file (const char *name) | |
690 | { | |
82d7a6f4 | 691 | ldfile_open_command_file_1 (name, script_nonT); |
6ec6968b NC |
692 | } |
693 | ||
694 | void | |
695 | ldfile_open_script_file (const char *name) | |
696 | { | |
82d7a6f4 | 697 | ldfile_open_command_file_1 (name, script_T); |
7d24f02c KH |
698 | } |
699 | ||
700 | /* Open command file NAME at the default script location. */ | |
701 | ||
702 | void | |
703 | ldfile_open_default_command_file (const char *name) | |
704 | { | |
82d7a6f4 | 705 | ldfile_open_command_file_1 (name, script_defaultT); |
7d24f02c KH |
706 | } |
707 | ||
252b5132 | 708 | void |
1579bae1 | 709 | ldfile_add_arch (const char *in_name) |
252b5132 | 710 | { |
d1b2b2dc | 711 | char *name = xstrdup (in_name); |
0aa7f586 AM |
712 | search_arch_type *new_arch |
713 | = (search_arch_type *) xmalloc (sizeof (search_arch_type)); | |
252b5132 RH |
714 | |
715 | ldfile_output_machine_name = in_name; | |
716 | ||
d3ce72d0 NC |
717 | new_arch->name = name; |
718 | new_arch->next = NULL; | |
252b5132 RH |
719 | while (*name) |
720 | { | |
3882b010 | 721 | *name = TOLOWER (*name); |
252b5132 RH |
722 | name++; |
723 | } | |
d3ce72d0 NC |
724 | *search_arch_tail_ptr = new_arch; |
725 | search_arch_tail_ptr = &new_arch->next; | |
252b5132 RH |
726 | |
727 | } | |
252b5132 | 728 | |
89cdebba KH |
729 | /* Set the output architecture. */ |
730 | ||
252b5132 | 731 | void |
5e2f1575 | 732 | ldfile_set_output_arch (const char *string, enum bfd_architecture defarch) |
252b5132 | 733 | { |
1c64c4ed NC |
734 | const bfd_arch_info_type *arch = bfd_scan_arch (string); |
735 | ||
736 | if (arch) | |
737 | { | |
738 | ldfile_output_architecture = arch->arch; | |
739 | ldfile_output_machine = arch->mach; | |
740 | ldfile_output_machine_name = arch->printable_name; | |
741 | } | |
5e2f1575 AM |
742 | else if (defarch != bfd_arch_unknown) |
743 | ldfile_output_architecture = defarch; | |
1c64c4ed | 744 | else |
df5f2391 | 745 | einfo (_("%F%P: cannot represent machine `%s'\n"), string); |
252b5132 | 746 | } |