ldfile.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /* Linker file opening and searching.
  2. Copyright (C) 1991-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "bfdlink.h"
  19. #include "safe-ctype.h"
  20. #include "ld.h"
  21. #include "ldmisc.h"
  22. #include "ldexp.h"
  23. #include "ldlang.h"
  24. #include "ldfile.h"
  25. #include "ldmain.h"
  26. #include <ldgram.h>
  27. #include "ldlex.h"
  28. #include "ldemul.h"
  29. #include "libiberty.h"
  30. #include "filenames.h"
  31. #ifdef ENABLE_PLUGINS
  32. #include "plugin-api.h"
  33. #include "plugin.h"
  34. #endif /* ENABLE_PLUGINS */
  35. bfd_boolean ldfile_assumed_script = FALSE;
  36. const char * ldfile_output_machine_name = "";
  37. unsigned long ldfile_output_machine;
  38. enum bfd_architecture ldfile_output_architecture;
  39. search_dirs_type * search_head;
  40. #ifdef VMS
  41. static char * slash = "";
  42. #else
  43. #if defined (_WIN32) && ! defined (__CYGWIN32__)
  44. static char * slash = "\\";
  45. #else
  46. static char * slash = "/";
  47. #endif
  48. #endif
  49. typedef struct search_arch
  50. {
  51. char *name;
  52. struct search_arch *next;
  53. } search_arch_type;
  54. static search_dirs_type **search_tail_ptr = &search_head;
  55. static search_arch_type *search_arch_head;
  56. static search_arch_type **search_arch_tail_ptr = &search_arch_head;
  57. /* Test whether a pathname, after canonicalization, is the same or a
  58. sub-directory of the sysroot directory. */
  59. static bfd_boolean
  60. is_sysrooted_pathname (const char *name)
  61. {
  62. char *realname;
  63. int len;
  64. bfd_boolean result;
  65. if (ld_canon_sysroot == NULL)
  66. return FALSE;
  67. realname = lrealpath (name);
  68. len = strlen (realname);
  69. result = FALSE;
  70. if (len > ld_canon_sysroot_len
  71. && IS_DIR_SEPARATOR (realname[ld_canon_sysroot_len]))
  72. {
  73. realname[ld_canon_sysroot_len] = '\0';
  74. result = FILENAME_CMP (ld_canon_sysroot, realname) == 0;
  75. }
  76. free (realname);
  77. return result;
  78. }
  79. /* Adds NAME to the library search path.
  80. Makes a copy of NAME using xmalloc(). */
  81. void
  82. ldfile_add_library_path (const char *name, bfd_boolean cmdline)
  83. {
  84. search_dirs_type *new_dirs;
  85. if (!cmdline && config.only_cmd_line_lib_dirs)
  86. return;
  87. new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
  88. new_dirs->next = NULL;
  89. new_dirs->cmdline = cmdline;
  90. *search_tail_ptr = new_dirs;
  91. search_tail_ptr = &new_dirs->next;
  92. /* If a directory is marked as honoring sysroot, prepend the sysroot path
  93. now. */
  94. if (name[0] == '=')
  95. new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL);
  96. else
  97. new_dirs->name = xstrdup (name);
  98. }
  99. /* Try to open a BFD for a lang_input_statement. */
  100. bfd_boolean
  101. ldfile_try_open_bfd (const char *attempt,
  102. lang_input_statement_type *entry)
  103. {
  104. entry->the_bfd = bfd_openr (attempt, entry->target);
  105. if (verbose)
  106. {
  107. if (entry->the_bfd == NULL)
  108. info_msg (_("attempt to open %s failed\n"), attempt);
  109. else
  110. info_msg (_("attempt to open %s succeeded\n"), attempt);
  111. }
  112. if (entry->the_bfd == NULL)
  113. {
  114. if (bfd_get_error () == bfd_error_invalid_target)
  115. einfo (_("%F%P: invalid BFD target `%s'\n"), entry->target);
  116. return FALSE;
  117. }
  118. /* Linker needs to decompress sections. */
  119. entry->the_bfd->flags |= BFD_DECOMPRESS;
  120. /* This is a linker input BFD. */
  121. entry->the_bfd->is_linker_input = 1;
  122. #ifdef ENABLE_PLUGINS
  123. if (entry->flags.lto_output)
  124. entry->the_bfd->lto_output = 1;
  125. #endif
  126. /* If we are searching for this file, see if the architecture is
  127. compatible with the output file. If it isn't, keep searching.
  128. If we can't open the file as an object file, stop the search
  129. here. If we are statically linking, ensure that we don't link
  130. a dynamic object.
  131. In the code below, it's OK to exit early if the check fails,
  132. closing the checked BFD and returning FALSE, but if the BFD
  133. checks out compatible, do not exit early returning TRUE, or
  134. the plugins will not get a chance to claim the file. */
  135. if (entry->flags.search_dirs || !entry->flags.dynamic)
  136. {
  137. bfd *check;
  138. if (bfd_check_format (entry->the_bfd, bfd_archive))
  139. check = bfd_openr_next_archived_file (entry->the_bfd, NULL);
  140. else
  141. check = entry->the_bfd;
  142. if (check != NULL)
  143. {
  144. if (! bfd_check_format (check, bfd_object))
  145. {
  146. if (check == entry->the_bfd
  147. && entry->flags.search_dirs
  148. && bfd_get_error () == bfd_error_file_not_recognized
  149. && ! ldemul_unrecognized_file (entry))
  150. {
  151. int token, skip = 0;
  152. char *arg, *arg1, *arg2, *arg3;
  153. extern FILE *yyin;
  154. /* Try to interpret the file as a linker script. */
  155. ldfile_open_command_file (attempt);
  156. ldfile_assumed_script = TRUE;
  157. parser_input = input_selected;
  158. ldlex_both ();
  159. token = INPUT_SCRIPT;
  160. while (token != 0)
  161. {
  162. switch (token)
  163. {
  164. case OUTPUT_FORMAT:
  165. if ((token = yylex ()) != '(')
  166. continue;
  167. if ((token = yylex ()) != NAME)
  168. continue;
  169. arg1 = yylval.name;
  170. arg2 = NULL;
  171. arg3 = NULL;
  172. token = yylex ();
  173. if (token == ',')
  174. {
  175. if ((token = yylex ()) != NAME)
  176. {
  177. free (arg1);
  178. continue;
  179. }
  180. arg2 = yylval.name;
  181. if ((token = yylex ()) != ','
  182. || (token = yylex ()) != NAME)
  183. {
  184. free (arg1);
  185. free (arg2);
  186. continue;
  187. }
  188. arg3 = yylval.name;
  189. token = yylex ();
  190. }
  191. if (token == ')')
  192. {
  193. switch (command_line.endian)
  194. {
  195. default:
  196. case ENDIAN_UNSET:
  197. arg = arg1; break;
  198. case ENDIAN_BIG:
  199. arg = arg2 ? arg2 : arg1; break;
  200. case ENDIAN_LITTLE:
  201. arg = arg3 ? arg3 : arg1; break;
  202. }
  203. if (strcmp (arg, lang_get_output_target ()) != 0)
  204. skip = 1;
  205. }
  206. free (arg1);
  207. if (arg2) free (arg2);
  208. if (arg3) free (arg3);
  209. break;
  210. case NAME:
  211. case LNAME:
  212. case VERS_IDENTIFIER:
  213. case VERS_TAG:
  214. free (yylval.name);
  215. break;
  216. case INT:
  217. if (yylval.bigint.str)
  218. free (yylval.bigint.str);
  219. break;
  220. }
  221. token = yylex ();
  222. }
  223. ldlex_popstate ();
  224. ldfile_assumed_script = FALSE;
  225. fclose (yyin);
  226. yyin = NULL;
  227. if (skip)
  228. {
  229. if (command_line.warn_search_mismatch)
  230. einfo (_("%P: skipping incompatible %s "
  231. "when searching for %s\n"),
  232. attempt, entry->local_sym_name);
  233. bfd_close (entry->the_bfd);
  234. entry->the_bfd = NULL;
  235. return FALSE;
  236. }
  237. }
  238. goto success;
  239. }
  240. if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0)
  241. {
  242. einfo (_("%F%P: attempted static link of dynamic object `%s'\n"),
  243. attempt);
  244. bfd_close (entry->the_bfd);
  245. entry->the_bfd = NULL;
  246. return FALSE;
  247. }
  248. if (entry->flags.search_dirs
  249. && !bfd_arch_get_compatible (check, link_info.output_bfd,
  250. command_line.accept_unknown_input_arch)
  251. /* XCOFF archives can have 32 and 64 bit objects. */
  252. && ! (bfd_get_flavour (check) == bfd_target_xcoff_flavour
  253. && bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour
  254. && bfd_check_format (entry->the_bfd, bfd_archive)))
  255. {
  256. if (command_line.warn_search_mismatch)
  257. einfo (_("%P: skipping incompatible %s "
  258. "when searching for %s\n"),
  259. attempt, entry->local_sym_name);
  260. bfd_close (entry->the_bfd);
  261. entry->the_bfd = NULL;
  262. return FALSE;
  263. }
  264. }
  265. }
  266. success:
  267. #ifdef ENABLE_PLUGINS
  268. /* If plugins are active, they get first chance to claim
  269. any successfully-opened input file. We skip archives
  270. here; the plugin wants us to offer it the individual
  271. members when we enumerate them, not the whole file. We
  272. also ignore corefiles, because that's just weird. It is
  273. a needed side-effect of calling bfd_check_format with
  274. bfd_object that it sets the bfd's arch and mach, which
  275. will be needed when and if we want to bfd_create a new
  276. one using this one as a template. */
  277. if (link_info.lto_plugin_active
  278. && !no_more_claiming
  279. && bfd_check_format (entry->the_bfd, bfd_object))
  280. plugin_maybe_claim (entry);
  281. #endif /* ENABLE_PLUGINS */
  282. /* It opened OK, the format checked out, and the plugins have had
  283. their chance to claim it, so this is success. */
  284. return TRUE;
  285. }
  286. /* Search for and open the file specified by ENTRY. If it is an
  287. archive, use ARCH, LIB and SUFFIX to modify the file name. */
  288. bfd_boolean
  289. ldfile_open_file_search (const char *arch,
  290. lang_input_statement_type *entry,
  291. const char *lib,
  292. const char *suffix)
  293. {
  294. search_dirs_type *search;
  295. /* If this is not an archive, try to open it in the current
  296. directory first. */
  297. if (! entry->flags.maybe_archive)
  298. {
  299. if (entry->flags.sysrooted && IS_ABSOLUTE_PATH (entry->filename))
  300. {
  301. char *name = concat (ld_sysroot, entry->filename,
  302. (const char *) NULL);
  303. if (ldfile_try_open_bfd (name, entry))
  304. {
  305. entry->filename = name;
  306. return TRUE;
  307. }
  308. free (name);
  309. }
  310. else if (ldfile_try_open_bfd (entry->filename, entry))
  311. return TRUE;
  312. if (IS_ABSOLUTE_PATH (entry->filename))
  313. return FALSE;
  314. }
  315. for (search = search_head; search != NULL; search = search->next)
  316. {
  317. char *string;
  318. if (entry->flags.dynamic && !bfd_link_relocatable (&link_info))
  319. {
  320. if (ldemul_open_dynamic_archive (arch, search, entry))
  321. return TRUE;
  322. }
  323. if (entry->flags.maybe_archive && !entry->flags.full_name_provided)
  324. string = concat (search->name, slash, lib, entry->filename,
  325. arch, suffix, (const char *) NULL);
  326. else
  327. string = concat (search->name, slash, entry->filename,
  328. (const char *) 0);
  329. if (ldfile_try_open_bfd (string, entry))
  330. {
  331. entry->filename = string;
  332. return TRUE;
  333. }
  334. free (string);
  335. }
  336. return FALSE;
  337. }
  338. /* Open the input file specified by ENTRY.
  339. PR 4437: Do not stop on the first missing file, but
  340. continue processing other input files in case there
  341. are more errors to report. */
  342. void
  343. ldfile_open_file (lang_input_statement_type *entry)
  344. {
  345. if (entry->the_bfd != NULL)
  346. return;
  347. if (! entry->flags.search_dirs)
  348. {
  349. if (ldfile_try_open_bfd (entry->filename, entry))
  350. return;
  351. if (filename_cmp (entry->filename, entry->local_sym_name) != 0)
  352. einfo (_("%P: cannot find %s (%s): %E\n"),
  353. entry->filename, entry->local_sym_name);
  354. else
  355. einfo (_("%P: cannot find %s: %E\n"), entry->local_sym_name);
  356. entry->flags.missing_file = TRUE;
  357. input_flags.missing_file = TRUE;
  358. }
  359. else
  360. {
  361. search_arch_type *arch;
  362. bfd_boolean found = FALSE;
  363. /* Try to open <filename><suffix> or lib<filename><suffix>.a */
  364. for (arch = search_arch_head; arch != NULL; arch = arch->next)
  365. {
  366. found = ldfile_open_file_search (arch->name, entry, "lib", ".a");
  367. if (found)
  368. break;
  369. #ifdef VMS
  370. found = ldfile_open_file_search (arch->name, entry, ":lib", ".a");
  371. if (found)
  372. break;
  373. #endif
  374. found = ldemul_find_potential_libraries (arch->name, entry);
  375. if (found)
  376. break;
  377. }
  378. /* If we have found the file, we don't need to search directories
  379. again. */
  380. if (found)
  381. entry->flags.search_dirs = FALSE;
  382. else
  383. {
  384. if (entry->flags.sysrooted
  385. && ld_sysroot
  386. && IS_ABSOLUTE_PATH (entry->local_sym_name))
  387. einfo (_("%P: cannot find %s inside %s\n"),
  388. entry->local_sym_name, ld_sysroot);
  389. else
  390. einfo (_("%P: cannot find %s\n"), entry->local_sym_name);
  391. entry->flags.missing_file = TRUE;
  392. input_flags.missing_file = TRUE;
  393. }
  394. }
  395. }
  396. /* Try to open NAME. */
  397. static FILE *
  398. try_open (const char *name, bfd_boolean *sysrooted)
  399. {
  400. FILE *result;
  401. result = fopen (name, "r");
  402. if (result != NULL)
  403. *sysrooted = is_sysrooted_pathname (name);
  404. if (verbose)
  405. {
  406. if (result == NULL)
  407. info_msg (_("cannot find script file %s\n"), name);
  408. else
  409. info_msg (_("opened script file %s\n"), name);
  410. }
  411. return result;
  412. }
  413. /* Return TRUE iff directory DIR contains an "ldscripts" subdirectory. */
  414. static bfd_boolean
  415. check_for_scripts_dir (char *dir)
  416. {
  417. char *buf;
  418. struct stat s;
  419. bfd_boolean res;
  420. buf = concat (dir, "/ldscripts", (const char *) NULL);
  421. res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
  422. free (buf);
  423. return res;
  424. }
  425. /* Return the default directory for finding script files.
  426. We look for the "ldscripts" directory in:
  427. SCRIPTDIR (passed from Makefile)
  428. (adjusted according to the current location of the binary)
  429. the dir where this program is (for using it from the build tree). */
  430. static char *
  431. find_scripts_dir (void)
  432. {
  433. char *dir;
  434. dir = make_relative_prefix (program_name, BINDIR, SCRIPTDIR);
  435. if (dir)
  436. {
  437. if (check_for_scripts_dir (dir))
  438. return dir;
  439. free (dir);
  440. }
  441. dir = make_relative_prefix (program_name, TOOLBINDIR, SCRIPTDIR);
  442. if (dir)
  443. {
  444. if (check_for_scripts_dir (dir))
  445. return dir;
  446. free (dir);
  447. }
  448. /* Look for "ldscripts" in the dir where our binary is. */
  449. dir = make_relative_prefix (program_name, ".", ".");
  450. if (dir)
  451. {
  452. if (check_for_scripts_dir (dir))
  453. return dir;
  454. free (dir);
  455. }
  456. return NULL;
  457. }
  458. /* If DEFAULT_ONLY is false, try to open NAME; if that fails, look for
  459. it in directories specified with -L, then in the default script
  460. directory. If DEFAULT_ONLY is true, the search is restricted to
  461. the default script location. */
  462. static FILE *
  463. ldfile_find_command_file (const char *name,
  464. bfd_boolean default_only,
  465. bfd_boolean *sysrooted)
  466. {
  467. search_dirs_type *search;
  468. FILE *result = NULL;
  469. char *path;
  470. static search_dirs_type *script_search;
  471. if (!default_only)
  472. {
  473. /* First try raw name. */
  474. result = try_open (name, sysrooted);
  475. if (result != NULL)
  476. return result;
  477. }
  478. if (!script_search)
  479. {
  480. char *script_dir = find_scripts_dir ();
  481. if (script_dir)
  482. {
  483. search_dirs_type **save_tail_ptr = search_tail_ptr;
  484. search_tail_ptr = &script_search;
  485. ldfile_add_library_path (script_dir, TRUE);
  486. search_tail_ptr = save_tail_ptr;
  487. }
  488. }
  489. /* Temporarily append script_search to the path list so that the
  490. paths specified with -L will be searched first. */
  491. *search_tail_ptr = script_search;
  492. /* Try now prefixes. */
  493. for (search = default_only ? script_search : search_head;
  494. search != NULL;
  495. search = search->next)
  496. {
  497. path = concat (search->name, slash, name, (const char *) NULL);
  498. result = try_open (path, sysrooted);
  499. free (path);
  500. if (result)
  501. break;
  502. }
  503. /* Restore the original path list. */
  504. *search_tail_ptr = NULL;
  505. return result;
  506. }
  507. /* Open command file NAME. */
  508. static void
  509. ldfile_open_command_file_1 (const char *name, bfd_boolean default_only)
  510. {
  511. FILE *ldlex_input_stack;
  512. bfd_boolean sysrooted;
  513. ldlex_input_stack = ldfile_find_command_file (name, default_only, &sysrooted);
  514. if (ldlex_input_stack == NULL)
  515. {
  516. bfd_set_error (bfd_error_system_call);
  517. einfo (_("%P%F: cannot open linker script file %s: %E\n"), name);
  518. return;
  519. }
  520. lex_push_file (ldlex_input_stack, name, sysrooted);
  521. lineno = 1;
  522. saved_script_handle = ldlex_input_stack;
  523. }
  524. /* Open command file NAME in the current directory, -L directories,
  525. the default script location, in that order. */
  526. void
  527. ldfile_open_command_file (const char *name)
  528. {
  529. ldfile_open_command_file_1 (name, FALSE);
  530. }
  531. /* Open command file NAME at the default script location. */
  532. void
  533. ldfile_open_default_command_file (const char *name)
  534. {
  535. ldfile_open_command_file_1 (name, TRUE);
  536. }
  537. void
  538. ldfile_add_arch (const char *in_name)
  539. {
  540. char *name = xstrdup (in_name);
  541. search_arch_type *new_arch = (search_arch_type *)
  542. xmalloc (sizeof (search_arch_type));
  543. ldfile_output_machine_name = in_name;
  544. new_arch->name = name;
  545. new_arch->next = NULL;
  546. while (*name)
  547. {
  548. *name = TOLOWER (*name);
  549. name++;
  550. }
  551. *search_arch_tail_ptr = new_arch;
  552. search_arch_tail_ptr = &new_arch->next;
  553. }
  554. /* Set the output architecture. */
  555. void
  556. ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
  557. {
  558. const bfd_arch_info_type *arch = bfd_scan_arch (string);
  559. if (arch)
  560. {
  561. ldfile_output_architecture = arch->arch;
  562. ldfile_output_machine = arch->mach;
  563. ldfile_output_machine_name = arch->printable_name;
  564. }
  565. else if (defarch != bfd_arch_unknown)
  566. ldfile_output_architecture = defarch;
  567. else
  568. einfo (_("%P%F: cannot represent machine `%s'\n"), string);
  569. }