ldmain.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. /* Main program of GNU linker.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. Written by Steve Chamberlain steve@cygnus.com
  4. This file is part of the GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "safe-ctype.h"
  20. #include "libiberty.h"
  21. #include "progress.h"
  22. #include "bfdlink.h"
  23. #include "ctf-api.h"
  24. #include "filenames.h"
  25. #include "elf/common.h"
  26. #include "ld.h"
  27. #include "ldmain.h"
  28. #include "ldmisc.h"
  29. #include "ldwrite.h"
  30. #include "ldexp.h"
  31. #include "ldlang.h"
  32. #include <ldgram.h>
  33. #include "ldlex.h"
  34. #include "ldfile.h"
  35. #include "ldemul.h"
  36. #include "ldctor.h"
  37. #if BFD_SUPPORTS_PLUGINS
  38. #include "plugin.h"
  39. #include "plugin-api.h"
  40. #endif /* BFD_SUPPORTS_PLUGINS */
  41. /* Somewhere above, sys/stat.h got included. */
  42. #if !defined(S_ISDIR) && defined(S_IFDIR)
  43. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  44. #endif
  45. #include <string.h>
  46. #ifndef TARGET_SYSTEM_ROOT
  47. #define TARGET_SYSTEM_ROOT ""
  48. #endif
  49. /* EXPORTS */
  50. FILE *saved_script_handle = NULL;
  51. FILE *previous_script_handle = NULL;
  52. bool force_make_executable = false;
  53. char *default_target;
  54. const char *output_filename = "a.out";
  55. /* Name this program was invoked by. */
  56. char *program_name;
  57. /* The prefix for system library directories. */
  58. const char *ld_sysroot;
  59. /* The canonical representation of ld_sysroot. */
  60. char *ld_canon_sysroot;
  61. int ld_canon_sysroot_len;
  62. /* Set by -G argument, for targets like MIPS ELF. */
  63. int g_switch_value = 8;
  64. /* Nonzero means print names of input files as processed. */
  65. unsigned int trace_files;
  66. /* Nonzero means report actions taken by the linker, and describe the linker script in use. */
  67. bool verbose;
  68. /* Nonzero means version number was printed, so exit successfully
  69. instead of complaining if no input files are given. */
  70. bool version_printed;
  71. /* TRUE if we should demangle symbol names. */
  72. bool demangling;
  73. args_type command_line;
  74. ld_config_type config;
  75. sort_type sort_section;
  76. static const char *get_sysroot
  77. (int, char **);
  78. static char *get_emulation
  79. (int, char **);
  80. static bool add_archive_element
  81. (struct bfd_link_info *, bfd *, const char *, bfd **);
  82. static void multiple_definition
  83. (struct bfd_link_info *, struct bfd_link_hash_entry *,
  84. bfd *, asection *, bfd_vma);
  85. static void multiple_common
  86. (struct bfd_link_info *, struct bfd_link_hash_entry *,
  87. bfd *, enum bfd_link_hash_type, bfd_vma);
  88. static void add_to_set
  89. (struct bfd_link_info *, struct bfd_link_hash_entry *,
  90. bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
  91. static void constructor_callback
  92. (struct bfd_link_info *, bool, const char *, bfd *,
  93. asection *, bfd_vma);
  94. static void warning_callback
  95. (struct bfd_link_info *, const char *, const char *, bfd *,
  96. asection *, bfd_vma);
  97. static void warning_find_reloc
  98. (bfd *, asection *, void *);
  99. static void undefined_symbol
  100. (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
  101. bool);
  102. static void reloc_overflow
  103. (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
  104. const char *, bfd_vma, bfd *, asection *, bfd_vma);
  105. static void reloc_dangerous
  106. (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
  107. static void unattached_reloc
  108. (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
  109. static bool notice
  110. (struct bfd_link_info *, struct bfd_link_hash_entry *,
  111. struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword);
  112. static struct bfd_link_callbacks link_callbacks =
  113. {
  114. add_archive_element,
  115. multiple_definition,
  116. multiple_common,
  117. add_to_set,
  118. constructor_callback,
  119. warning_callback,
  120. undefined_symbol,
  121. reloc_overflow,
  122. reloc_dangerous,
  123. unattached_reloc,
  124. notice,
  125. einfo,
  126. info_msg,
  127. minfo,
  128. ldlang_override_segment_assignment,
  129. ldlang_ctf_acquire_strings,
  130. NULL,
  131. ldlang_ctf_new_dynsym,
  132. ldlang_write_ctf_late
  133. };
  134. static bfd_assert_handler_type default_bfd_assert_handler;
  135. static bfd_error_handler_type default_bfd_error_handler;
  136. struct bfd_link_info link_info;
  137. struct dependency_file
  138. {
  139. struct dependency_file *next;
  140. char *name;
  141. };
  142. static struct dependency_file *dependency_files, *dependency_files_tail;
  143. void
  144. track_dependency_files (const char *filename)
  145. {
  146. struct dependency_file *dep
  147. = (struct dependency_file *) xmalloc (sizeof (*dep));
  148. dep->name = xstrdup (filename);
  149. dep->next = NULL;
  150. if (dependency_files == NULL)
  151. dependency_files = dep;
  152. else
  153. dependency_files_tail->next = dep;
  154. dependency_files_tail = dep;
  155. }
  156. static void
  157. write_dependency_file (void)
  158. {
  159. FILE *out;
  160. struct dependency_file *dep;
  161. out = fopen (config.dependency_file, FOPEN_WT);
  162. if (out == NULL)
  163. {
  164. einfo (_("%F%P: cannot open dependency file %s: %E\n"),
  165. config.dependency_file);
  166. }
  167. fprintf (out, "%s:", output_filename);
  168. for (dep = dependency_files; dep != NULL; dep = dep->next)
  169. fprintf (out, " \\\n %s", dep->name);
  170. fprintf (out, "\n");
  171. for (dep = dependency_files; dep != NULL; dep = dep->next)
  172. fprintf (out, "\n%s:\n", dep->name);
  173. fclose (out);
  174. }
  175. static void
  176. ld_cleanup (void)
  177. {
  178. bfd_cache_close_all ();
  179. #if BFD_SUPPORTS_PLUGINS
  180. plugin_call_cleanup ();
  181. #endif
  182. if (output_filename && delete_output_file_on_failure)
  183. unlink_if_ordinary (output_filename);
  184. }
  185. /* Hook to notice BFD assertions. */
  186. static void
  187. ld_bfd_assert_handler (const char *fmt, const char *bfdver,
  188. const char *file, int line)
  189. {
  190. config.make_executable = false;
  191. (*default_bfd_assert_handler) (fmt, bfdver, file, line);
  192. }
  193. /* Hook the bfd error/warning handler for --fatal-warnings. */
  194. static void
  195. ld_bfd_error_handler (const char *fmt, va_list ap)
  196. {
  197. if (config.fatal_warnings)
  198. config.make_executable = false;
  199. (*default_bfd_error_handler) (fmt, ap);
  200. }
  201. int
  202. main (int argc, char **argv)
  203. {
  204. char *emulation;
  205. long start_time = get_run_time ();
  206. #ifdef HAVE_LC_MESSAGES
  207. setlocale (LC_MESSAGES, "");
  208. #endif
  209. setlocale (LC_CTYPE, "");
  210. bindtextdomain (PACKAGE, LOCALEDIR);
  211. textdomain (PACKAGE);
  212. program_name = argv[0];
  213. xmalloc_set_program_name (program_name);
  214. START_PROGRESS (program_name, 0);
  215. expandargv (&argc, &argv);
  216. if (bfd_init () != BFD_INIT_MAGIC)
  217. einfo (_("%F%P: fatal error: libbfd ABI mismatch\n"));
  218. bfd_set_error_program_name (program_name);
  219. /* We want to notice and fail on those nasty BFD assertions which are
  220. likely to signal incorrect output being generated but otherwise may
  221. leave no trace. */
  222. default_bfd_assert_handler = bfd_set_assert_handler (ld_bfd_assert_handler);
  223. /* Also hook the bfd error/warning handler for --fatal-warnings. */
  224. default_bfd_error_handler = bfd_set_error_handler (ld_bfd_error_handler);
  225. xatexit (ld_cleanup);
  226. /* Set up the sysroot directory. */
  227. ld_sysroot = get_sysroot (argc, argv);
  228. if (*ld_sysroot)
  229. ld_canon_sysroot = lrealpath (ld_sysroot);
  230. if (ld_canon_sysroot)
  231. {
  232. ld_canon_sysroot_len = strlen (ld_canon_sysroot);
  233. /* is_sysrooted_pathname() relies on no trailing dirsep. */
  234. if (ld_canon_sysroot_len > 0
  235. && IS_DIR_SEPARATOR (ld_canon_sysroot [ld_canon_sysroot_len - 1]))
  236. ld_canon_sysroot [--ld_canon_sysroot_len] = '\0';
  237. }
  238. else
  239. ld_canon_sysroot_len = -1;
  240. /* Set the default BFD target based on the configured target. Doing
  241. this permits the linker to be configured for a particular target,
  242. and linked against a shared BFD library which was configured for
  243. a different target. The macro TARGET is defined by Makefile. */
  244. if (!bfd_set_default_target (TARGET))
  245. {
  246. einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
  247. xexit (1);
  248. }
  249. #if YYDEBUG
  250. {
  251. extern int yydebug;
  252. yydebug = 1;
  253. }
  254. #endif
  255. config.build_constructors = true;
  256. config.rpath_separator = ':';
  257. config.split_by_reloc = (unsigned) -1;
  258. config.split_by_file = (bfd_size_type) -1;
  259. config.make_executable = true;
  260. config.magic_demand_paged = true;
  261. config.text_read_only = true;
  262. config.print_map_discarded = true;
  263. link_info.disable_target_specific_optimizations = -1;
  264. command_line.warn_mismatch = true;
  265. command_line.warn_search_mismatch = true;
  266. command_line.check_section_addresses = -1;
  267. /* We initialize DEMANGLING based on the environment variable
  268. COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
  269. output of the linker, unless COLLECT_NO_DEMANGLE is set in the
  270. environment. Acting the same way here lets us provide the same
  271. interface by default. */
  272. demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
  273. link_info.allow_undefined_version = true;
  274. link_info.keep_memory = true;
  275. link_info.max_cache_size = (bfd_size_type) -1;
  276. link_info.combreloc = true;
  277. link_info.strip_discarded = true;
  278. link_info.prohibit_multiple_definition_absolute = false;
  279. link_info.textrel_check = DEFAULT_LD_TEXTREL_CHECK;
  280. link_info.emit_hash = DEFAULT_EMIT_SYSV_HASH;
  281. link_info.emit_gnu_hash = DEFAULT_EMIT_GNU_HASH;
  282. link_info.callbacks = &link_callbacks;
  283. link_info.input_bfds_tail = &link_info.input_bfds;
  284. /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
  285. and _fini symbols. We are compatible. */
  286. link_info.init_function = "_init";
  287. link_info.fini_function = "_fini";
  288. link_info.relax_pass = 1;
  289. link_info.extern_protected_data = -1;
  290. link_info.dynamic_undefined_weak = -1;
  291. link_info.indirect_extern_access = -1;
  292. link_info.pei386_auto_import = -1;
  293. link_info.spare_dynamic_tags = 5;
  294. link_info.path_separator = ':';
  295. #ifdef DEFAULT_FLAG_COMPRESS_DEBUG
  296. link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
  297. #endif
  298. #ifdef DEFAULT_NEW_DTAGS
  299. link_info.new_dtags = DEFAULT_NEW_DTAGS;
  300. #endif
  301. link_info.start_stop_gc = false;
  302. link_info.start_stop_visibility = STV_PROTECTED;
  303. ldfile_add_arch ("");
  304. emulation = get_emulation (argc, argv);
  305. ldemul_choose_mode (emulation);
  306. default_target = ldemul_choose_target (argc, argv);
  307. lang_init ();
  308. ldexp_init ();
  309. ldemul_before_parse ();
  310. lang_has_input_file = false;
  311. parse_args (argc, argv);
  312. if (config.hash_table_size != 0)
  313. bfd_hash_set_default_size (config.hash_table_size);
  314. #if BFD_SUPPORTS_PLUGINS
  315. /* Now all the plugin arguments have been gathered, we can load them. */
  316. plugin_load_plugins ();
  317. #endif /* BFD_SUPPORTS_PLUGINS */
  318. ldemul_set_symbols ();
  319. /* If we have not already opened and parsed a linker script,
  320. try the default script from command line first. */
  321. if (saved_script_handle == NULL
  322. && command_line.default_script != NULL)
  323. {
  324. ldfile_open_script_file (command_line.default_script);
  325. parser_input = input_script;
  326. yyparse ();
  327. }
  328. /* If we have not already opened and parsed a linker script
  329. read the emulation's appropriate default script. */
  330. if (saved_script_handle == NULL)
  331. {
  332. int isfile;
  333. char *s = ldemul_get_script (&isfile);
  334. if (isfile)
  335. ldfile_open_default_command_file (s);
  336. else
  337. {
  338. lex_string = s;
  339. lex_redirect (s, _("built in linker script"), 1);
  340. }
  341. parser_input = input_script;
  342. yyparse ();
  343. lex_string = NULL;
  344. }
  345. if (verbose)
  346. {
  347. if (saved_script_handle)
  348. info_msg (_("using external linker script:"));
  349. else
  350. info_msg (_("using internal linker script:"));
  351. info_msg ("\n==================================================\n");
  352. if (saved_script_handle)
  353. {
  354. static const int ld_bufsz = 8193;
  355. size_t n;
  356. char *buf = (char *) xmalloc (ld_bufsz);
  357. rewind (saved_script_handle);
  358. while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
  359. {
  360. buf[n] = 0;
  361. info_msg ("%s", buf);
  362. }
  363. rewind (saved_script_handle);
  364. free (buf);
  365. }
  366. else
  367. {
  368. int isfile;
  369. info_msg (ldemul_get_script (&isfile));
  370. }
  371. info_msg ("\n==================================================\n");
  372. }
  373. if (command_line.force_group_allocation
  374. || !bfd_link_relocatable (&link_info))
  375. link_info.resolve_section_groups = true;
  376. else
  377. link_info.resolve_section_groups = false;
  378. if (command_line.print_output_format)
  379. info_msg ("%s\n", lang_get_output_target ());
  380. lang_final ();
  381. /* If the only command line argument has been -v or --version or --verbose
  382. then ignore any input files provided by linker scripts and exit now.
  383. We do not want to create an output file when the linker is just invoked
  384. to provide version information. */
  385. if (argc == 2 && version_printed)
  386. xexit (0);
  387. if (link_info.inhibit_common_definition && !bfd_link_dll (&link_info))
  388. einfo (_("%F%P: --no-define-common may not be used without -shared\n"));
  389. if (!lang_has_input_file)
  390. {
  391. if (version_printed || command_line.print_output_format)
  392. xexit (0);
  393. einfo (_("%F%P: no input files\n"));
  394. }
  395. if (verbose)
  396. info_msg (_("%P: mode %s\n"), emulation);
  397. ldemul_after_parse ();
  398. if (config.map_filename)
  399. {
  400. if (strcmp (config.map_filename, "-") == 0)
  401. {
  402. config.map_file = stdout;
  403. }
  404. else
  405. {
  406. config.map_file = fopen (config.map_filename, FOPEN_WT);
  407. if (config.map_file == (FILE *) NULL)
  408. {
  409. bfd_set_error (bfd_error_system_call);
  410. einfo (_("%F%P: cannot open map file %s: %E\n"),
  411. config.map_filename);
  412. }
  413. }
  414. link_info.has_map_file = true;
  415. }
  416. lang_process ();
  417. /* Print error messages for any missing symbols, for any warning
  418. symbols, and possibly multiple definitions. */
  419. if (bfd_link_relocatable (&link_info))
  420. link_info.output_bfd->flags &= ~EXEC_P;
  421. else
  422. link_info.output_bfd->flags |= EXEC_P;
  423. if ((link_info.compress_debug & COMPRESS_DEBUG))
  424. {
  425. link_info.output_bfd->flags |= BFD_COMPRESS;
  426. if (link_info.compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
  427. link_info.output_bfd->flags |= BFD_COMPRESS_GABI;
  428. }
  429. ldwrite ();
  430. if (config.map_file != NULL)
  431. lang_map ();
  432. if (command_line.cref)
  433. output_cref (config.map_file != NULL ? config.map_file : stdout);
  434. if (nocrossref_list != NULL)
  435. check_nocrossrefs ();
  436. if (command_line.print_memory_usage)
  437. lang_print_memory_usage ();
  438. #if 0
  439. {
  440. struct bfd_link_hash_entry *h;
  441. h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1);
  442. fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
  443. }
  444. #endif
  445. ldexp_finish ();
  446. lang_finish ();
  447. if (config.dependency_file != NULL)
  448. write_dependency_file ();
  449. /* Even if we're producing relocatable output, some non-fatal errors should
  450. be reported in the exit status. (What non-fatal errors, if any, do we
  451. want to ignore for relocatable output?) */
  452. if (!config.make_executable && !force_make_executable)
  453. {
  454. if (verbose)
  455. einfo (_("%P: link errors found, deleting executable `%s'\n"),
  456. output_filename);
  457. /* The file will be removed by ld_cleanup. */
  458. xexit (1);
  459. }
  460. else
  461. {
  462. if (!bfd_close (link_info.output_bfd))
  463. einfo (_("%F%P: %pB: final close failed: %E\n"), link_info.output_bfd);
  464. /* If the --force-exe-suffix is enabled, and we're making an
  465. executable file and it doesn't end in .exe, copy it to one
  466. which does. */
  467. if (!bfd_link_relocatable (&link_info)
  468. && command_line.force_exe_suffix)
  469. {
  470. int len = strlen (output_filename);
  471. if (len < 4
  472. || (strcasecmp (output_filename + len - 4, ".exe") != 0
  473. && strcasecmp (output_filename + len - 4, ".dll") != 0))
  474. {
  475. FILE *src;
  476. FILE *dst;
  477. const int bsize = 4096;
  478. char *buf = (char *) xmalloc (bsize);
  479. int l;
  480. char *dst_name = (char *) xmalloc (len + 5);
  481. strcpy (dst_name, output_filename);
  482. strcat (dst_name, ".exe");
  483. src = fopen (output_filename, FOPEN_RB);
  484. dst = fopen (dst_name, FOPEN_WB);
  485. if (!src)
  486. einfo (_("%F%P: unable to open for source of copy `%s'\n"),
  487. output_filename);
  488. if (!dst)
  489. einfo (_("%F%P: unable to open for destination of copy `%s'\n"),
  490. dst_name);
  491. while ((l = fread (buf, 1, bsize, src)) > 0)
  492. {
  493. int done = fwrite (buf, 1, l, dst);
  494. if (done != l)
  495. einfo (_("%P: error writing file `%s'\n"), dst_name);
  496. }
  497. fclose (src);
  498. if (fclose (dst) == EOF)
  499. einfo (_("%P: error closing file `%s'\n"), dst_name);
  500. free (dst_name);
  501. free (buf);
  502. }
  503. }
  504. }
  505. END_PROGRESS (program_name);
  506. if (config.stats)
  507. {
  508. long run_time = get_run_time () - start_time;
  509. fflush (stdout);
  510. fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
  511. program_name, run_time / 1000000, run_time % 1000000);
  512. fflush (stderr);
  513. }
  514. /* Prevent ld_cleanup from doing anything, after a successful link. */
  515. output_filename = NULL;
  516. xexit (0);
  517. return 0;
  518. }
  519. /* If the configured sysroot is relocatable, try relocating it based on
  520. default prefix FROM. Return the relocated directory if it exists,
  521. otherwise return null. */
  522. static char *
  523. get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
  524. {
  525. #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
  526. char *path;
  527. struct stat s;
  528. path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
  529. if (path)
  530. {
  531. if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
  532. return path;
  533. free (path);
  534. }
  535. #endif
  536. return 0;
  537. }
  538. /* Return the sysroot directory. Return "" if no sysroot is being used. */
  539. static const char *
  540. get_sysroot (int argc, char **argv)
  541. {
  542. int i;
  543. const char *path = NULL;
  544. for (i = 1; i < argc; i++)
  545. if (startswith (argv[i], "--sysroot="))
  546. path = argv[i] + strlen ("--sysroot=");
  547. if (!path)
  548. path = get_relative_sysroot (BINDIR);
  549. if (!path)
  550. path = get_relative_sysroot (TOOLBINDIR);
  551. if (!path)
  552. path = TARGET_SYSTEM_ROOT;
  553. if (IS_DIR_SEPARATOR (*path) && path[1] == 0)
  554. path = "";
  555. return path;
  556. }
  557. /* We need to find any explicitly given emulation in order to initialize the
  558. state that's needed by the lex&yacc argument parser (parse_args). */
  559. static char *
  560. get_emulation (int argc, char **argv)
  561. {
  562. char *emulation;
  563. int i;
  564. emulation = getenv (EMULATION_ENVIRON);
  565. if (emulation == NULL)
  566. emulation = DEFAULT_EMULATION;
  567. for (i = 1; i < argc; i++)
  568. {
  569. if (startswith (argv[i], "-m"))
  570. {
  571. if (argv[i][2] == '\0')
  572. {
  573. /* -m EMUL */
  574. if (i < argc - 1)
  575. {
  576. emulation = argv[i + 1];
  577. i++;
  578. }
  579. else
  580. einfo (_("%F%P: missing argument to -m\n"));
  581. }
  582. else if (strcmp (argv[i], "-mips1") == 0
  583. || strcmp (argv[i], "-mips2") == 0
  584. || strcmp (argv[i], "-mips3") == 0
  585. || strcmp (argv[i], "-mips4") == 0
  586. || strcmp (argv[i], "-mips5") == 0
  587. || strcmp (argv[i], "-mips32") == 0
  588. || strcmp (argv[i], "-mips32r2") == 0
  589. || strcmp (argv[i], "-mips32r3") == 0
  590. || strcmp (argv[i], "-mips32r5") == 0
  591. || strcmp (argv[i], "-mips32r6") == 0
  592. || strcmp (argv[i], "-mips64") == 0
  593. || strcmp (argv[i], "-mips64r2") == 0
  594. || strcmp (argv[i], "-mips64r3") == 0
  595. || strcmp (argv[i], "-mips64r5") == 0
  596. || strcmp (argv[i], "-mips64r6") == 0)
  597. {
  598. /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
  599. passed to the linker by some MIPS compilers. They
  600. generally tell the linker to use a slightly different
  601. library path. Perhaps someday these should be
  602. implemented as emulations; until then, we just ignore
  603. the arguments and hope that nobody ever creates
  604. emulations named ips1, ips2 or ips3. */
  605. }
  606. else if (strcmp (argv[i], "-m486") == 0)
  607. {
  608. /* FIXME: The argument -m486 is passed to the linker on
  609. some Linux systems. Hope that nobody creates an
  610. emulation named 486. */
  611. }
  612. else
  613. {
  614. /* -mEMUL */
  615. emulation = &argv[i][2];
  616. }
  617. }
  618. }
  619. return emulation;
  620. }
  621. void
  622. add_ysym (const char *name)
  623. {
  624. if (link_info.notice_hash == NULL)
  625. {
  626. link_info.notice_hash
  627. = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
  628. if (!bfd_hash_table_init_n (link_info.notice_hash,
  629. bfd_hash_newfunc,
  630. sizeof (struct bfd_hash_entry),
  631. 61))
  632. einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
  633. }
  634. if (bfd_hash_lookup (link_info.notice_hash, name, true, true) == NULL)
  635. einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
  636. }
  637. void
  638. add_ignoresym (struct bfd_link_info *info, const char *name)
  639. {
  640. if (info->ignore_hash == NULL)
  641. {
  642. info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table));
  643. if (!bfd_hash_table_init_n (info->ignore_hash,
  644. bfd_hash_newfunc,
  645. sizeof (struct bfd_hash_entry),
  646. 61))
  647. einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
  648. }
  649. if (bfd_hash_lookup (info->ignore_hash, name, true, true) == NULL)
  650. einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
  651. }
  652. /* Record a symbol to be wrapped, from the --wrap option. */
  653. void
  654. add_wrap (const char *name)
  655. {
  656. if (link_info.wrap_hash == NULL)
  657. {
  658. link_info.wrap_hash
  659. = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
  660. if (!bfd_hash_table_init_n (link_info.wrap_hash,
  661. bfd_hash_newfunc,
  662. sizeof (struct bfd_hash_entry),
  663. 61))
  664. einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
  665. }
  666. if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
  667. einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
  668. }
  669. /* Handle the -retain-symbols-file option. */
  670. void
  671. add_keepsyms_file (const char *filename)
  672. {
  673. FILE *file;
  674. char *buf;
  675. size_t bufsize;
  676. int c;
  677. if (link_info.strip == strip_some)
  678. einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
  679. file = fopen (filename, "r");
  680. if (file == NULL)
  681. {
  682. bfd_set_error (bfd_error_system_call);
  683. einfo ("%X%P: %s: %E\n", filename);
  684. return;
  685. }
  686. link_info.keep_hash = (struct bfd_hash_table *)
  687. xmalloc (sizeof (struct bfd_hash_table));
  688. if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
  689. sizeof (struct bfd_hash_entry)))
  690. einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
  691. bufsize = 100;
  692. buf = (char *) xmalloc (bufsize);
  693. c = getc (file);
  694. while (c != EOF)
  695. {
  696. while (ISSPACE (c))
  697. c = getc (file);
  698. if (c != EOF)
  699. {
  700. size_t len = 0;
  701. while (!ISSPACE (c) && c != EOF)
  702. {
  703. buf[len] = c;
  704. ++len;
  705. if (len >= bufsize)
  706. {
  707. bufsize *= 2;
  708. buf = (char *) xrealloc (buf, bufsize);
  709. }
  710. c = getc (file);
  711. }
  712. buf[len] = '\0';
  713. if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) == NULL)
  714. einfo (_("%F%P: bfd_hash_lookup for insertion failed: %E\n"));
  715. }
  716. }
  717. if (link_info.strip != strip_none)
  718. einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
  719. free (buf);
  720. link_info.strip = strip_some;
  721. fclose (file);
  722. }
  723. /* Callbacks from the BFD linker routines. */
  724. /* This is called when BFD has decided to include an archive member in
  725. a link. */
  726. static bool
  727. add_archive_element (struct bfd_link_info *info,
  728. bfd *abfd,
  729. const char *name,
  730. bfd **subsbfd ATTRIBUTE_UNUSED)
  731. {
  732. lang_input_statement_type *input;
  733. lang_input_statement_type *parent;
  734. lang_input_statement_type orig_input;
  735. input = (lang_input_statement_type *)
  736. xcalloc (1, sizeof (lang_input_statement_type));
  737. input->header.type = lang_input_statement_enum;
  738. input->filename = bfd_get_filename (abfd);
  739. input->local_sym_name = bfd_get_filename (abfd);
  740. input->the_bfd = abfd;
  741. /* Save the original data for trace files/tries below, as plugins
  742. (if enabled) may possibly alter it to point to a replacement
  743. BFD, but we still want to output the original BFD filename. */
  744. orig_input = *input;
  745. #if BFD_SUPPORTS_PLUGINS
  746. if (link_info.lto_plugin_active)
  747. {
  748. /* We must offer this archive member to the plugins to claim. */
  749. plugin_maybe_claim (input);
  750. if (input->flags.claimed)
  751. {
  752. if (no_more_claiming)
  753. {
  754. /* Don't claim new IR symbols after all IR symbols have
  755. been claimed. */
  756. if (verbose)
  757. info_msg ("%pI: no new IR symbols to claim\n",
  758. &orig_input);
  759. input->flags.claimed = 0;
  760. return false;
  761. }
  762. input->flags.claim_archive = true;
  763. *subsbfd = input->the_bfd;
  764. }
  765. }
  766. #endif /* BFD_SUPPORTS_PLUGINS */
  767. if (link_info.input_bfds_tail == &input->the_bfd->link.next
  768. || input->the_bfd->link.next != NULL)
  769. {
  770. /* We have already loaded this element, and are attempting to
  771. load it again. This can happen when the archive map doesn't
  772. match actual symbols defined by the element. */
  773. free (input);
  774. bfd_set_error (bfd_error_malformed_archive);
  775. return false;
  776. }
  777. /* Set the file_chain pointer of archives to the last element loaded
  778. from the archive. See ldlang.c:find_rescan_insertion. */
  779. parent = bfd_usrdata (abfd->my_archive);
  780. if (parent != NULL && !parent->flags.reload)
  781. parent->next = input;
  782. ldlang_add_file (input);
  783. if (config.map_file != NULL)
  784. {
  785. static bool header_printed;
  786. struct bfd_link_hash_entry *h;
  787. bfd *from;
  788. int len;
  789. h = bfd_link_hash_lookup (info->hash, name, false, false, true);
  790. if (h == NULL
  791. && info->pei386_auto_import
  792. && startswith (name, "__imp_"))
  793. h = bfd_link_hash_lookup (info->hash, name + 6, false, false, true);
  794. if (h == NULL)
  795. from = NULL;
  796. else
  797. {
  798. switch (h->type)
  799. {
  800. default:
  801. from = NULL;
  802. break;
  803. case bfd_link_hash_defined:
  804. case bfd_link_hash_defweak:
  805. from = h->u.def.section->owner;
  806. break;
  807. case bfd_link_hash_undefined:
  808. case bfd_link_hash_undefweak:
  809. from = h->u.undef.abfd;
  810. break;
  811. case bfd_link_hash_common:
  812. from = h->u.c.p->section->owner;
  813. break;
  814. }
  815. }
  816. if (!header_printed)
  817. {
  818. minfo (_("Archive member included to satisfy reference by file (symbol)\n\n"));
  819. header_printed = true;
  820. }
  821. if (abfd->my_archive == NULL
  822. || bfd_is_thin_archive (abfd->my_archive))
  823. {
  824. minfo ("%s", bfd_get_filename (abfd));
  825. len = strlen (bfd_get_filename (abfd));
  826. }
  827. else
  828. {
  829. minfo ("%s(%s)", bfd_get_filename (abfd->my_archive),
  830. bfd_get_filename (abfd));
  831. len = (strlen (bfd_get_filename (abfd->my_archive))
  832. + strlen (bfd_get_filename (abfd))
  833. + 2);
  834. }
  835. if (len >= 29)
  836. {
  837. print_nl ();
  838. len = 0;
  839. }
  840. while (len < 30)
  841. {
  842. print_space ();
  843. ++len;
  844. }
  845. if (from != NULL)
  846. minfo ("%pB ", from);
  847. if (h != NULL)
  848. minfo ("(%pT)\n", h->root.string);
  849. else
  850. minfo ("(%s)\n", name);
  851. }
  852. if (verbose
  853. || trace_files > 1
  854. || (trace_files && bfd_is_thin_archive (orig_input.the_bfd->my_archive)))
  855. info_msg ("%pI\n", &orig_input);
  856. return true;
  857. }
  858. /* This is called when BFD has discovered a symbol which is defined
  859. multiple times. */
  860. static void
  861. multiple_definition (struct bfd_link_info *info,
  862. struct bfd_link_hash_entry *h,
  863. bfd *nbfd,
  864. asection *nsec,
  865. bfd_vma nval)
  866. {
  867. const char *name;
  868. bfd *obfd;
  869. asection *osec;
  870. bfd_vma oval;
  871. if (info->allow_multiple_definition)
  872. return;
  873. switch (h->type)
  874. {
  875. case bfd_link_hash_defined:
  876. osec = h->u.def.section;
  877. oval = h->u.def.value;
  878. obfd = h->u.def.section->owner;
  879. break;
  880. case bfd_link_hash_indirect:
  881. osec = bfd_ind_section_ptr;
  882. oval = 0;
  883. obfd = NULL;
  884. break;
  885. default:
  886. abort ();
  887. }
  888. /* Ignore a redefinition of an absolute symbol to the
  889. same value; it's harmless. */
  890. if (h->type == bfd_link_hash_defined
  891. && bfd_is_abs_section (osec)
  892. && bfd_is_abs_section (nsec)
  893. && nval == oval)
  894. return;
  895. /* If either section has the output_section field set to
  896. bfd_abs_section_ptr, it means that the section is being
  897. discarded, and this is not really a multiple definition at all.
  898. FIXME: It would be cleaner to somehow ignore symbols defined in
  899. sections which are being discarded. */
  900. if (!info->prohibit_multiple_definition_absolute
  901. && ((osec->output_section != NULL
  902. && ! bfd_is_abs_section (osec)
  903. && bfd_is_abs_section (osec->output_section))
  904. || (nsec->output_section != NULL
  905. && !bfd_is_abs_section (nsec)
  906. && bfd_is_abs_section (nsec->output_section))))
  907. return;
  908. name = h->root.string;
  909. if (nbfd == NULL)
  910. {
  911. nbfd = obfd;
  912. nsec = osec;
  913. nval = oval;
  914. obfd = NULL;
  915. }
  916. if (info->warn_multiple_definition)
  917. einfo (_("%P: %C: warning: multiple definition of `%pT'"),
  918. nbfd, nsec, nval, name);
  919. else
  920. einfo (_("%X%P: %C: multiple definition of `%pT'"),
  921. nbfd, nsec, nval, name);
  922. if (obfd != NULL)
  923. einfo (_("; %D: first defined here"), obfd, osec, oval);
  924. einfo ("\n");
  925. if (RELAXATION_ENABLED_BY_USER)
  926. {
  927. einfo (_("%P: disabling relaxation; it will not work with multiple definitions\n"));
  928. DISABLE_RELAXATION;
  929. }
  930. }
  931. /* This is called when there is a definition of a common symbol, or
  932. when a common symbol is found for a symbol that is already defined,
  933. or when two common symbols are found. We only do something if
  934. -warn-common was used. */
  935. static void
  936. multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  937. struct bfd_link_hash_entry *h,
  938. bfd *nbfd,
  939. enum bfd_link_hash_type ntype,
  940. bfd_vma nsize)
  941. {
  942. const char *name;
  943. bfd *obfd;
  944. enum bfd_link_hash_type otype;
  945. bfd_vma osize;
  946. if (!config.warn_common)
  947. return;
  948. name = h->root.string;
  949. otype = h->type;
  950. if (otype == bfd_link_hash_common)
  951. {
  952. obfd = h->u.c.p->section->owner;
  953. osize = h->u.c.size;
  954. }
  955. else if (otype == bfd_link_hash_defined
  956. || otype == bfd_link_hash_defweak)
  957. {
  958. obfd = h->u.def.section->owner;
  959. osize = 0;
  960. }
  961. else
  962. {
  963. /* FIXME: It would nice if we could report the BFD which defined
  964. an indirect symbol, but we don't have anywhere to store the
  965. information. */
  966. obfd = NULL;
  967. osize = 0;
  968. }
  969. if (ntype == bfd_link_hash_defined
  970. || ntype == bfd_link_hash_defweak
  971. || ntype == bfd_link_hash_indirect)
  972. {
  973. ASSERT (otype == bfd_link_hash_common);
  974. if (obfd != NULL)
  975. einfo (_("%P: %pB: warning: definition of `%pT' overriding common"
  976. " from %pB\n"),
  977. nbfd, name, obfd);
  978. else
  979. einfo (_("%P: %pB: warning: definition of `%pT' overriding common\n"),
  980. nbfd, name);
  981. }
  982. else if (otype == bfd_link_hash_defined
  983. || otype == bfd_link_hash_defweak
  984. || otype == bfd_link_hash_indirect)
  985. {
  986. ASSERT (ntype == bfd_link_hash_common);
  987. if (obfd != NULL)
  988. einfo (_("%P: %pB: warning: common of `%pT' overridden by definition"
  989. " from %pB\n"),
  990. nbfd, name, obfd);
  991. else
  992. einfo (_("%P: %pB: warning: common of `%pT' overridden by definition\n"),
  993. nbfd, name);
  994. }
  995. else
  996. {
  997. ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
  998. if (osize > nsize)
  999. {
  1000. if (obfd != NULL)
  1001. einfo (_("%P: %pB: warning: common of `%pT' overridden"
  1002. " by larger common from %pB\n"),
  1003. nbfd, name, obfd);
  1004. else
  1005. einfo (_("%P: %pB: warning: common of `%pT' overridden"
  1006. " by larger common\n"),
  1007. nbfd, name);
  1008. }
  1009. else if (nsize > osize)
  1010. {
  1011. if (obfd != NULL)
  1012. einfo (_("%P: %pB: warning: common of `%pT' overriding"
  1013. " smaller common from %pB\n"),
  1014. nbfd, name, obfd);
  1015. else
  1016. einfo (_("%P: %pB: warning: common of `%pT' overriding"
  1017. " smaller common\n"),
  1018. nbfd, name);
  1019. }
  1020. else
  1021. {
  1022. if (obfd != NULL)
  1023. einfo (_("%P: %pB and %pB: warning: multiple common of `%pT'\n"),
  1024. nbfd, obfd, name);
  1025. else
  1026. einfo (_("%P: %pB: warning: multiple common of `%pT'\n"),
  1027. nbfd, name);
  1028. }
  1029. }
  1030. }
  1031. /* This is called when BFD has discovered a set element. H is the
  1032. entry in the linker hash table for the set. SECTION and VALUE
  1033. represent a value which should be added to the set. */
  1034. static void
  1035. add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1036. struct bfd_link_hash_entry *h,
  1037. bfd_reloc_code_real_type reloc,
  1038. bfd *abfd,
  1039. asection *section,
  1040. bfd_vma value)
  1041. {
  1042. if (config.warn_constructors)
  1043. einfo (_("%P: warning: global constructor %s used\n"),
  1044. h->root.string);
  1045. if (!config.build_constructors)
  1046. return;
  1047. ldctor_add_set_entry (h, reloc, NULL, section, value);
  1048. if (h->type == bfd_link_hash_new)
  1049. {
  1050. h->type = bfd_link_hash_undefined;
  1051. h->u.undef.abfd = abfd;
  1052. /* We don't call bfd_link_add_undef to add this to the list of
  1053. undefined symbols because we are going to define it
  1054. ourselves. */
  1055. }
  1056. }
  1057. /* This is called when BFD has discovered a constructor. This is only
  1058. called for some object file formats--those which do not handle
  1059. constructors in some more clever fashion. This is similar to
  1060. adding an element to a set, but less general. */
  1061. static void
  1062. constructor_callback (struct bfd_link_info *info,
  1063. bool constructor,
  1064. const char *name,
  1065. bfd *abfd,
  1066. asection *section,
  1067. bfd_vma value)
  1068. {
  1069. char *s;
  1070. struct bfd_link_hash_entry *h;
  1071. char set_name[1 + sizeof "__CTOR_LIST__"];
  1072. if (config.warn_constructors)
  1073. einfo (_("%P: warning: global constructor %s used\n"), name);
  1074. if (!config.build_constructors)
  1075. return;
  1076. /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
  1077. useful error message. */
  1078. if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
  1079. && (bfd_link_relocatable (info)
  1080. || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
  1081. einfo (_("%F%P: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
  1082. s = set_name;
  1083. if (bfd_get_symbol_leading_char (abfd) != '\0')
  1084. *s++ = bfd_get_symbol_leading_char (abfd);
  1085. if (constructor)
  1086. strcpy (s, "__CTOR_LIST__");
  1087. else
  1088. strcpy (s, "__DTOR_LIST__");
  1089. h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
  1090. if (h == (struct bfd_link_hash_entry *) NULL)
  1091. einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
  1092. if (h->type == bfd_link_hash_new)
  1093. {
  1094. h->type = bfd_link_hash_undefined;
  1095. h->u.undef.abfd = abfd;
  1096. /* We don't call bfd_link_add_undef to add this to the list of
  1097. undefined symbols because we are going to define it
  1098. ourselves. */
  1099. }
  1100. ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
  1101. }
  1102. /* A structure used by warning_callback to pass information through
  1103. bfd_map_over_sections. */
  1104. struct warning_callback_info
  1105. {
  1106. bool found;
  1107. const char *warning;
  1108. const char *symbol;
  1109. asymbol **asymbols;
  1110. };
  1111. /* Look through the relocs to see if we can find a plausible address
  1112. for SYMBOL in ABFD. Return TRUE if found. Otherwise return FALSE. */
  1113. static bool
  1114. symbol_warning (const char *warning, const char *symbol, bfd *abfd)
  1115. {
  1116. struct warning_callback_info cinfo;
  1117. if (!bfd_generic_link_read_symbols (abfd))
  1118. einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
  1119. cinfo.found = false;
  1120. cinfo.warning = warning;
  1121. cinfo.symbol = symbol;
  1122. cinfo.asymbols = bfd_get_outsymbols (abfd);
  1123. bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
  1124. return cinfo.found;
  1125. }
  1126. /* This is called when there is a reference to a warning symbol. */
  1127. static void
  1128. warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1129. const char *warning,
  1130. const char *symbol,
  1131. bfd *abfd,
  1132. asection *section,
  1133. bfd_vma address)
  1134. {
  1135. /* This is a hack to support warn_multiple_gp. FIXME: This should
  1136. have a cleaner interface, but what? */
  1137. if (!config.warn_multiple_gp
  1138. && strcmp (warning, "using multiple gp values") == 0)
  1139. return;
  1140. if (section != NULL)
  1141. einfo ("%P: %C: %s%s\n", abfd, section, address, _("warning: "), warning);
  1142. else if (abfd == NULL)
  1143. einfo ("%P: %s%s\n", _("warning: "), warning);
  1144. else if (symbol == NULL)
  1145. einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
  1146. else if (!symbol_warning (warning, symbol, abfd))
  1147. {
  1148. bfd *b;
  1149. /* Search all input files for a reference to SYMBOL. */
  1150. for (b = info->input_bfds; b; b = b->link.next)
  1151. if (b != abfd && symbol_warning (warning, symbol, b))
  1152. return;
  1153. einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
  1154. }
  1155. }
  1156. /* This is called by warning_callback for each section. It checks the
  1157. relocs of the section to see if it can find a reference to the
  1158. symbol which triggered the warning. If it can, it uses the reloc
  1159. to give an error message with a file and line number. */
  1160. static void
  1161. warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
  1162. {
  1163. struct warning_callback_info *info = (struct warning_callback_info *) iarg;
  1164. long relsize;
  1165. arelent **relpp;
  1166. long relcount;
  1167. arelent **p, **pend;
  1168. if (info->found)
  1169. return;
  1170. relsize = bfd_get_reloc_upper_bound (abfd, sec);
  1171. if (relsize < 0)
  1172. einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
  1173. if (relsize == 0)
  1174. return;
  1175. relpp = (arelent **) xmalloc (relsize);
  1176. relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
  1177. if (relcount < 0)
  1178. einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
  1179. p = relpp;
  1180. pend = p + relcount;
  1181. for (; p < pend && *p != NULL; p++)
  1182. {
  1183. arelent *q = *p;
  1184. if (q->sym_ptr_ptr != NULL
  1185. && *q->sym_ptr_ptr != NULL
  1186. && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
  1187. {
  1188. /* We found a reloc for the symbol we are looking for. */
  1189. einfo ("%P: %C: %s%s\n", abfd, sec, q->address, _("warning: "),
  1190. info->warning);
  1191. info->found = true;
  1192. break;
  1193. }
  1194. }
  1195. free (relpp);
  1196. }
  1197. #if SUPPORT_ERROR_HANDLING_SCRIPT
  1198. char * error_handling_script = NULL;
  1199. #endif
  1200. /* This is called when an undefined symbol is found. */
  1201. static void
  1202. undefined_symbol (struct bfd_link_info *info,
  1203. const char *name,
  1204. bfd *abfd,
  1205. asection *section,
  1206. bfd_vma address,
  1207. bool error)
  1208. {
  1209. static char *error_name;
  1210. static unsigned int error_count;
  1211. #define MAX_ERRORS_IN_A_ROW 5
  1212. if (info->ignore_hash != NULL
  1213. && bfd_hash_lookup (info->ignore_hash, name, false, false) != NULL)
  1214. return;
  1215. if (config.warn_once)
  1216. {
  1217. /* Only warn once about a particular undefined symbol. */
  1218. add_ignoresym (info, name);
  1219. }
  1220. /* We never print more than a reasonable number of errors in a row
  1221. for a single symbol. */
  1222. if (error_name != NULL
  1223. && strcmp (name, error_name) == 0)
  1224. ++error_count;
  1225. else
  1226. {
  1227. error_count = 0;
  1228. free (error_name);
  1229. error_name = xstrdup (name);
  1230. }
  1231. #if SUPPORT_ERROR_HANDLING_SCRIPT
  1232. if (error_handling_script != NULL
  1233. && error_count < MAX_ERRORS_IN_A_ROW)
  1234. {
  1235. char * argv[4];
  1236. const char * res;
  1237. int status, err;
  1238. argv[0] = error_handling_script;
  1239. argv[1] = "undefined-symbol";
  1240. argv[2] = (char *) name;
  1241. argv[3] = NULL;
  1242. if (verbose)
  1243. einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
  1244. argv[0], argv[1], argv[2]);
  1245. res = pex_one (PEX_SEARCH, error_handling_script, argv,
  1246. N_("error handling script"),
  1247. NULL /* Send stdout to random, temp file. */,
  1248. NULL /* Write to stderr. */,
  1249. &status, &err);
  1250. if (res != NULL)
  1251. {
  1252. einfo (_("%P: Failed to run error handling script '%s', reason: "),
  1253. error_handling_script);
  1254. /* FIXME: We assume here that errrno == err. */
  1255. perror (res);
  1256. }
  1257. /* We ignore the return status of the script and
  1258. carry on to issue the normal error message. */
  1259. }
  1260. #endif /* SUPPORT_ERROR_HANDLING_SCRIPT */
  1261. if (section != NULL)
  1262. {
  1263. if (error_count < MAX_ERRORS_IN_A_ROW)
  1264. {
  1265. if (error)
  1266. einfo (_("%X%P: %C: undefined reference to `%pT'\n"),
  1267. abfd, section, address, name);
  1268. else
  1269. einfo (_("%P: %C: warning: undefined reference to `%pT'\n"),
  1270. abfd, section, address, name);
  1271. }
  1272. else if (error_count == MAX_ERRORS_IN_A_ROW)
  1273. {
  1274. if (error)
  1275. einfo (_("%X%P: %D: more undefined references to `%pT' follow\n"),
  1276. abfd, section, address, name);
  1277. else
  1278. einfo (_("%P: %D: warning: more undefined references to `%pT' follow\n"),
  1279. abfd, section, address, name);
  1280. }
  1281. else if (error)
  1282. einfo ("%X");
  1283. }
  1284. else
  1285. {
  1286. if (error_count < MAX_ERRORS_IN_A_ROW)
  1287. {
  1288. if (error)
  1289. einfo (_("%X%P: %pB: undefined reference to `%pT'\n"),
  1290. abfd, name);
  1291. else
  1292. einfo (_("%P: %pB: warning: undefined reference to `%pT'\n"),
  1293. abfd, name);
  1294. }
  1295. else if (error_count == MAX_ERRORS_IN_A_ROW)
  1296. {
  1297. if (error)
  1298. einfo (_("%X%P: %pB: more undefined references to `%pT' follow\n"),
  1299. abfd, name);
  1300. else
  1301. einfo (_("%P: %pB: warning: more undefined references to `%pT' follow\n"),
  1302. abfd, name);
  1303. }
  1304. else if (error)
  1305. einfo ("%X");
  1306. }
  1307. }
  1308. /* Counter to limit the number of relocation overflow error messages
  1309. to print. Errors are printed as it is decremented. When it's
  1310. called and the counter is zero, a final message is printed
  1311. indicating more relocations were omitted. When it gets to -1, no
  1312. such errors are printed. If it's initially set to a value less
  1313. than -1, all such errors will be printed (--verbose does this). */
  1314. int overflow_cutoff_limit = 10;
  1315. /* This is called when a reloc overflows. */
  1316. static void
  1317. reloc_overflow (struct bfd_link_info *info,
  1318. struct bfd_link_hash_entry *entry,
  1319. const char *name,
  1320. const char *reloc_name,
  1321. bfd_vma addend,
  1322. bfd *abfd,
  1323. asection *section,
  1324. bfd_vma address)
  1325. {
  1326. if (overflow_cutoff_limit == -1)
  1327. return;
  1328. einfo ("%X%H:", abfd, section, address);
  1329. if (overflow_cutoff_limit >= 0
  1330. && overflow_cutoff_limit-- == 0)
  1331. {
  1332. einfo (_(" additional relocation overflows omitted from the output\n"));
  1333. return;
  1334. }
  1335. if (entry)
  1336. {
  1337. while (entry->type == bfd_link_hash_indirect
  1338. || entry->type == bfd_link_hash_warning)
  1339. entry = entry->u.i.link;
  1340. switch (entry->type)
  1341. {
  1342. case bfd_link_hash_undefined:
  1343. case bfd_link_hash_undefweak:
  1344. einfo (_(" relocation truncated to fit: "
  1345. "%s against undefined symbol `%pT'"),
  1346. reloc_name, entry->root.string);
  1347. break;
  1348. case bfd_link_hash_defined:
  1349. case bfd_link_hash_defweak:
  1350. einfo (_(" relocation truncated to fit: "
  1351. "%s against symbol `%pT' defined in %pA section in %pB"),
  1352. reloc_name, entry->root.string,
  1353. entry->u.def.section,
  1354. entry->u.def.section == bfd_abs_section_ptr
  1355. ? info->output_bfd : entry->u.def.section->owner);
  1356. break;
  1357. default:
  1358. abort ();
  1359. break;
  1360. }
  1361. }
  1362. else
  1363. einfo (_(" relocation truncated to fit: %s against `%pT'"),
  1364. reloc_name, name);
  1365. if (addend != 0)
  1366. einfo ("+%v", addend);
  1367. einfo ("\n");
  1368. }
  1369. /* This is called when a dangerous relocation is made. */
  1370. static void
  1371. reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1372. const char *message,
  1373. bfd *abfd,
  1374. asection *section,
  1375. bfd_vma address)
  1376. {
  1377. einfo (_("%X%H: dangerous relocation: %s\n"),
  1378. abfd, section, address, message);
  1379. }
  1380. /* This is called when a reloc is being generated attached to a symbol
  1381. that is not being output. */
  1382. static void
  1383. unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1384. const char *name,
  1385. bfd *abfd,
  1386. asection *section,
  1387. bfd_vma address)
  1388. {
  1389. einfo (_("%X%H: reloc refers to symbol `%pT' which is not being output\n"),
  1390. abfd, section, address, name);
  1391. }
  1392. /* This is called if link_info.notice_all is set, or when a symbol in
  1393. link_info.notice_hash is found. Symbols are put in notice_hash
  1394. using the -y option, while notice_all is set if the --cref option
  1395. has been supplied, or if there are any NOCROSSREFS sections in the
  1396. linker script; and if plugins are active, since they need to monitor
  1397. all references from non-IR files. */
  1398. static bool
  1399. notice (struct bfd_link_info *info,
  1400. struct bfd_link_hash_entry *h,
  1401. struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED,
  1402. bfd *abfd,
  1403. asection *section,
  1404. bfd_vma value,
  1405. flagword flags ATTRIBUTE_UNUSED)
  1406. {
  1407. const char *name;
  1408. if (h == NULL)
  1409. {
  1410. if (command_line.cref || nocrossref_list != NULL)
  1411. return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value);
  1412. return true;
  1413. }
  1414. name = h->root.string;
  1415. if (info->notice_hash != NULL
  1416. && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL)
  1417. {
  1418. if (bfd_is_und_section (section))
  1419. einfo (_("%P: %pB: reference to %s\n"), abfd, name);
  1420. else
  1421. einfo (_("%P: %pB: definition of %s\n"), abfd, name);
  1422. }
  1423. if (command_line.cref || nocrossref_list != NULL)
  1424. add_cref (name, abfd, section, value);
  1425. return true;
  1426. }