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