ldmain.c 39 KB

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