Fix v850 test failures
[deliverable/binutils-gdb.git] / ld / ldmain.c
CommitLineData
252b5132 1/* Main program of GNU linker.
5af11cab 2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000
252b5132
RH
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain steve@cygnus.com
5
6This file is part of GLD, the Gnu Linker.
7
8GLD is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GLD is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GLD; see the file COPYING. If not, write to the Free
20Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2102111-1307, USA. */
22
23#include "bfd.h"
24#include "sysdep.h"
25#include <stdio.h>
26#include <ctype.h>
27#include "libiberty.h"
28#include "progress.h"
29#include "bfdlink.h"
5af11cab 30#include "filenames.h"
252b5132
RH
31
32#include "ld.h"
33#include "ldmain.h"
34#include "ldmisc.h"
35#include "ldwrite.h"
36#include "ldgram.h"
37#include "ldexp.h"
38#include "ldlang.h"
252b5132
RH
39#include "ldlex.h"
40#include "ldfile.h"
b71e2778 41#include "ldemul.h"
252b5132
RH
42#include "ldctor.h"
43
6d5e62f8 44/* Somewhere above, sys/stat.h got included . . . . */
252b5132
RH
45#if !defined(S_ISDIR) && defined(S_IFDIR)
46#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
47#endif
48
49#include <string.h>
50
51#ifdef HAVE_SBRK
52#ifdef NEED_DECLARATION_SBRK
53extern PTR sbrk ();
54#endif
55#endif
56
57static char *get_emulation PARAMS ((int, char **));
58static void set_scripts_dir PARAMS ((void));
59
60/* EXPORTS */
61
62char *default_target;
63const char *output_filename = "a.out";
64
65/* Name this program was invoked by. */
66char *program_name;
67
6d5e62f8 68/* The file that we're creating. */
252b5132
RH
69bfd *output_bfd = 0;
70
71/* Set by -G argument, for MIPS ECOFF target. */
72int g_switch_value = 8;
73
74/* Nonzero means print names of input files as processed. */
75boolean trace_files;
76
77/* Nonzero means same, but note open failures, too. */
78boolean trace_file_tries;
79
80/* Nonzero means version number was printed, so exit successfully
81 instead of complaining if no input files are given. */
82boolean version_printed;
83
84/* Nonzero means link in every member of an archive. */
85boolean whole_archive;
86
87/* True if we should demangle symbol names. */
88boolean demangling;
89
90args_type command_line;
91
92ld_config_type config;
93
94static void remove_output PARAMS ((void));
95static boolean check_for_scripts_dir PARAMS ((char *dir));
96static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
97 const char *));
98static boolean multiple_definition PARAMS ((struct bfd_link_info *,
99 const char *,
100 bfd *, asection *, bfd_vma,
101 bfd *, asection *, bfd_vma));
102static boolean multiple_common PARAMS ((struct bfd_link_info *,
103 const char *, bfd *,
104 enum bfd_link_hash_type, bfd_vma,
105 bfd *, enum bfd_link_hash_type,
106 bfd_vma));
107static boolean add_to_set PARAMS ((struct bfd_link_info *,
108 struct bfd_link_hash_entry *,
109 bfd_reloc_code_real_type,
110 bfd *, asection *, bfd_vma));
111static boolean constructor_callback PARAMS ((struct bfd_link_info *,
112 boolean constructor,
113 const char *name,
114 bfd *, asection *, bfd_vma));
115static boolean warning_callback PARAMS ((struct bfd_link_info *,
116 const char *, const char *, bfd *,
117 asection *, bfd_vma));
118static void warning_find_reloc PARAMS ((bfd *, asection *, PTR));
119static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
120 const char *, bfd *,
b6f29aaa 121 asection *, bfd_vma, boolean));
252b5132
RH
122static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
123 const char *, bfd_vma,
124 bfd *, asection *, bfd_vma));
125static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
126 bfd *, asection *, bfd_vma));
127static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
128 const char *, bfd *, asection *,
129 bfd_vma));
130static boolean notice PARAMS ((struct bfd_link_info *, const char *,
131 bfd *, asection *, bfd_vma));
132
6d5e62f8 133static struct bfd_link_callbacks link_callbacks = {
252b5132
RH
134 add_archive_element,
135 multiple_definition,
136 multiple_common,
137 add_to_set,
138 constructor_callback,
139 warning_callback,
140 undefined_symbol,
141 reloc_overflow,
142 reloc_dangerous,
143 unattached_reloc,
144 notice
145};
146
147struct bfd_link_info link_info;
148\f
149static void
150remove_output ()
151{
6d5e62f8 152 if (output_filename)
252b5132
RH
153 {
154 if (output_bfd && output_bfd->iostream)
6d5e62f8 155 fclose ((FILE *) (output_bfd->iostream));
252b5132
RH
156 if (delete_output_file_on_failure)
157 unlink (output_filename);
158 }
159}
160
161int
162main (argc, argv)
163 int argc;
164 char **argv;
165{
166 char *emulation;
167 long start_time = get_run_time ();
168
169#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
170 setlocale (LC_MESSAGES, "");
171#endif
172 bindtextdomain (PACKAGE, LOCALEDIR);
173 textdomain (PACKAGE);
174
175 program_name = argv[0];
176 xmalloc_set_program_name (program_name);
177
178 START_PROGRESS (program_name, 0);
179
180 bfd_init ();
181
182 bfd_set_error_program_name (program_name);
183
184 xatexit (remove_output);
185
186 /* Set the default BFD target based on the configured target. Doing
187 this permits the linker to be configured for a particular target,
188 and linked against a shared BFD library which was configured for
189 a different target. The macro TARGET is defined by Makefile. */
190 if (! bfd_set_default_target (TARGET))
191 {
192 einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
193 xexit (1);
194 }
195
196 /* Initialize the data about options. */
197 trace_files = trace_file_tries = version_printed = false;
198 whole_archive = false;
199 config.build_constructors = true;
200 config.dynamic_link = false;
201 config.has_shared = false;
a854a4a7
AM
202 config.split_by_reloc = (unsigned) -1;
203 config.split_by_file = (bfd_size_type) -1;
252b5132
RH
204 command_line.force_common_definition = false;
205 command_line.interpreter = NULL;
206 command_line.rpath = NULL;
207 command_line.warn_mismatch = true;
208 command_line.check_section_addresses = true;
209
210 /* We initialize DEMANGLING based on the environment variable
211 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
212 output of the linker, unless COLLECT_NO_DEMANGLE is set in the
213 environment. Acting the same way here lets us provide the same
214 interface by default. */
215 demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
216
217 link_info.callbacks = &link_callbacks;
218 link_info.relocateable = false;
a712da20 219 link_info.emitrelocations = false;
252b5132
RH
220 link_info.shared = false;
221 link_info.symbolic = false;
222 link_info.static_link = false;
223 link_info.traditional_format = false;
224 link_info.optimize = false;
225 link_info.no_undefined = false;
226 link_info.strip = strip_none;
227 link_info.discard = discard_none;
228 link_info.keep_memory = true;
229 link_info.input_bfds = NULL;
230 link_info.create_object_symbols_section = NULL;
231 link_info.hash = NULL;
232 link_info.keep_hash = NULL;
233 link_info.notice_all = false;
234 link_info.notice_hash = NULL;
235 link_info.wrap_hash = NULL;
236 link_info.mpc860c0 = 0;
3dbf70a2
MM
237 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
238 and _fini symbols. We are compatible. */
239 link_info.init_function = "_init";
240 link_info.fini_function = "_fini";
6c1439be 241 link_info.new_dtags = false;
e0ee487b
L
242 link_info.flags = (bfd_vma) 0;
243 link_info.flags_1 = (bfd_vma) 0;
3dbf70a2 244
252b5132
RH
245 ldfile_add_arch ("");
246
247 config.make_executable = true;
248 force_make_executable = false;
249 config.magic_demand_paged = true;
250 config.text_read_only = true;
252b5132
RH
251
252 emulation = get_emulation (argc, argv);
253 ldemul_choose_mode (emulation);
254 default_target = ldemul_choose_target ();
255 lang_init ();
256 ldemul_before_parse ();
257 lang_has_input_file = false;
258 parse_args (argc, argv);
259
260 ldemul_set_symbols ();
261
262 if (link_info.relocateable)
263 {
264 if (command_line.gc_sections)
265 einfo ("%P%F: --gc-sections and -r may not be used together\n");
266 if (link_info.mpc860c0)
267 einfo (_("%P%F: -r and --mpc860c0 may not be used together\n"));
268 else if (command_line.relax)
269 einfo (_("%P%F: --relax and -r may not be used together\n"));
270 if (link_info.shared)
271 einfo (_("%P%F: -r and -shared may not be used together\n"));
272 }
273
274 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
275 don't see how else this can be handled, since in this case we
276 must preserve all externally visible symbols. */
277 if (link_info.relocateable && link_info.strip == strip_all)
278 {
279 link_info.strip = strip_debugger;
280 if (link_info.discard == discard_none)
281 link_info.discard = discard_all;
282 }
283
284 /* This essentially adds another -L directory so this must be done after
285 the -L's in argv have been processed. */
286 set_scripts_dir ();
287
288 if (had_script == false)
289 {
290 /* Read the emulation's appropriate default script. */
291 int isfile;
292 char *s = ldemul_get_script (&isfile);
293
294 if (isfile)
295 ldfile_open_command_file (s);
296 else
297 {
298 if (trace_file_tries)
299 {
300 info_msg (_("using internal linker script:\n"));
301 info_msg ("==================================================\n");
302 info_msg (s);
303 info_msg ("\n==================================================\n");
304 }
305 lex_string = s;
306 lex_redirect (s);
307 }
308 parser_input = input_script;
309 yyparse ();
310 lex_string = NULL;
311 }
312
313 lang_final ();
314
315 if (lang_has_input_file == false)
316 {
317 if (version_printed)
318 xexit (0);
319 einfo (_("%P%F: no input files\n"));
320 }
321
322 if (trace_files)
323 {
324 info_msg (_("%P: mode %s\n"), emulation);
325 }
326
327 ldemul_after_parse ();
328
252b5132
RH
329 if (config.map_filename)
330 {
331 if (strcmp (config.map_filename, "-") == 0)
332 {
333 config.map_file = stdout;
334 }
335 else
336 {
337 config.map_file = fopen (config.map_filename, FOPEN_WT);
338 if (config.map_file == (FILE *) NULL)
339 {
340 bfd_set_error (bfd_error_system_call);
341 einfo (_("%P%F: cannot open map file %s: %E\n"),
342 config.map_filename);
343 }
344 }
345 }
346
252b5132
RH
347 lang_process ();
348
349 /* Print error messages for any missing symbols, for any warning
6d5e62f8 350 symbols, and possibly multiple definitions. */
252b5132 351
c7c54483
AM
352 if (! link_info.relocateable)
353 {
354 /* Look for a text section and switch the readonly attribute in it. */
6d5e62f8
KH
355 asection *found = bfd_get_section_by_name (output_bfd, ".text");
356
c7c54483
AM
357 if (found != (asection *) NULL)
358 {
359 if (config.text_read_only)
360 found->flags |= SEC_READONLY;
361 else
362 found->flags &= ~SEC_READONLY;
363 }
364 }
252b5132
RH
365
366 if (link_info.relocateable)
367 output_bfd->flags &= ~EXEC_P;
368 else
369 output_bfd->flags |= EXEC_P;
370
371 ldwrite ();
372
373 if (config.map_file != NULL)
374 lang_map ();
375 if (command_line.cref)
376 output_cref (config.map_file != NULL ? config.map_file : stdout);
377 if (nocrossref_list != NULL)
378 check_nocrossrefs ();
379
380 /* Even if we're producing relocateable output, some non-fatal errors should
381 be reported in the exit status. (What non-fatal errors, if any, do we
382 want to ignore for relocateable output?) */
383
384 if (config.make_executable == false && force_make_executable == false)
385 {
386 if (trace_files == true)
387 {
388 einfo (_("%P: link errors found, deleting executable `%s'\n"),
389 output_filename);
390 }
391
392 /* The file will be removed by remove_output. */
393
394 xexit (1);
395 }
396 else
397 {
398 if (! bfd_close (output_bfd))
399 einfo (_("%F%B: final close failed: %E\n"), output_bfd);
400
401 /* If the --force-exe-suffix is enabled, and we're making an
6d5e62f8
KH
402 executable file and it doesn't end in .exe, copy it to one
403 which does. */
252b5132
RH
404 if (! link_info.relocateable && command_line.force_exe_suffix)
405 {
406 int len = strlen (output_filename);
6d5e62f8 407 if (len < 4
252b5132
RH
408 || (strcasecmp (output_filename + len - 4, ".exe") != 0
409 && strcasecmp (output_filename + len - 4, ".dll") != 0))
410 {
411 FILE *src;
412 FILE *dst;
413 const int bsize = 4096;
414 char *buf = xmalloc (bsize);
415 int l;
416 char *dst_name = xmalloc (len + 5);
417 strcpy (dst_name, output_filename);
418 strcat (dst_name, ".exe");
419 src = fopen (output_filename, FOPEN_RB);
420 dst = fopen (dst_name, FOPEN_WB);
421
422 if (!src)
423 einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename);
424 if (!dst)
425 einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name);
426 while ((l = fread (buf, 1, bsize, src)) > 0)
427 {
428 int done = fwrite (buf, 1, l, dst);
429 if (done != l)
430 {
431 einfo (_("%P: Error writing file `%s'\n"), dst_name);
432 }
433 }
434 fclose (src);
435 if (fclose (dst) == EOF)
436 {
437 einfo (_("%P: Error closing file `%s'\n"), dst_name);
438 }
439 free (dst_name);
440 free (buf);
441 }
442 }
443 }
444
445 END_PROGRESS (program_name);
446
447 if (config.stats)
448 {
449#ifdef HAVE_SBRK
450 char *lim = (char *) sbrk (0);
451#endif
452 long run_time = get_run_time () - start_time;
453
454 fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
455 program_name, run_time / 1000000, run_time % 1000000);
456#ifdef HAVE_SBRK
457 fprintf (stderr, _("%s: data size %ld\n"), program_name,
458 (long) (lim - (char *) &environ));
459#endif
460 }
461
462 /* Prevent remove_output from doing anything, after a successful link. */
463 output_filename = NULL;
464
465 xexit (0);
466 return 0;
467}
468
469/* We need to find any explicitly given emulation in order to initialize the
470 state that's needed by the lex&yacc argument parser (parse_args). */
471
472static char *
473get_emulation (argc, argv)
474 int argc;
475 char **argv;
476{
477 char *emulation;
478 int i;
479
480 emulation = getenv (EMULATION_ENVIRON);
481 if (emulation == NULL)
482 emulation = DEFAULT_EMULATION;
483
484 for (i = 1; i < argc; i++)
485 {
486 if (!strncmp (argv[i], "-m", 2))
487 {
488 if (argv[i][2] == '\0')
489 {
490 /* -m EMUL */
491 if (i < argc - 1)
492 {
493 emulation = argv[i + 1];
494 i++;
495 }
496 else
497 {
6d5e62f8 498 einfo (_("%P%F: missing argument to -m\n"));
252b5132
RH
499 }
500 }
501 else if (strcmp (argv[i], "-mips1") == 0
502 || strcmp (argv[i], "-mips2") == 0
503 || strcmp (argv[i], "-mips3") == 0
504 || strcmp (argv[i], "-mips4") == 0)
505 {
506 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
507 passed to the linker by some MIPS compilers. They
508 generally tell the linker to use a slightly different
509 library path. Perhaps someday these should be
510 implemented as emulations; until then, we just ignore
511 the arguments and hope that nobody ever creates
512 emulations named ips1, ips2 or ips3. */
513 }
514 else if (strcmp (argv[i], "-m486") == 0)
515 {
516 /* FIXME: The argument -m486 is passed to the linker on
517 some Linux systems. Hope that nobody creates an
518 emulation named 486. */
519 }
520 else
521 {
522 /* -mEMUL */
523 emulation = &argv[i][2];
524 }
525 }
526 }
527
528 return emulation;
529}
530
531/* If directory DIR contains an "ldscripts" subdirectory,
532 add DIR to the library search path and return true,
533 else return false. */
534
535static boolean
536check_for_scripts_dir (dir)
537 char *dir;
538{
539 size_t dirlen;
540 char *buf;
541 struct stat s;
542 boolean res;
543
544 dirlen = strlen (dir);
545 /* sizeof counts the terminating NUL. */
6d5e62f8 546 buf = (char *) xmalloc (dirlen + sizeof ("/ldscripts"));
252b5132
RH
547 sprintf (buf, "%s/ldscripts", dir);
548
549 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
550 free (buf);
551 if (res)
552 ldfile_add_library_path (dir, false);
553 return res;
554}
555
556/* Set the default directory for finding script files.
557 Libraries will be searched for here too, but that's ok.
558 We look for the "ldscripts" directory in:
559
560 SCRIPTDIR (passed from Makefile)
561 the dir where this program is (for using it from the build tree)
562 the dir where this program is/../lib (for installing the tool suite elsewhere) */
563
564static void
565set_scripts_dir ()
566{
567 char *end, *dir;
568 size_t dirlen;
569
570 if (check_for_scripts_dir (SCRIPTDIR))
6d5e62f8
KH
571 /* We've been installed normally. */
572 return;
252b5132
RH
573
574 /* Look for "ldscripts" in the dir where our binary is. */
575 end = strrchr (program_name, '/');
5af11cab
AM
576#ifdef HAVE_DOS_BASED_FILE_SYSTEM
577 {
578 /* We could have \foo\bar, or /foo\bar. */
579 char *bslash = strrchr (program_name, '\\');
2ab47eed 580 if (end == NULL || (bslash != NULL && bslash > end))
5af11cab
AM
581 end = bslash;
582 }
583#endif
252b5132
RH
584
585 if (end == NULL)
586 {
587 /* Don't look for ldscripts in the current directory. There is
588 too much potential for confusion. */
589 return;
590 }
591
592 dirlen = end - program_name;
593 /* Make a copy of program_name in dir.
594 Leave room for later "/../lib". */
595 dir = (char *) xmalloc (dirlen + 8);
596 strncpy (dir, program_name, dirlen);
597 dir[dirlen] = '\0';
598
599 if (check_for_scripts_dir (dir))
6d5e62f8
KH
600 /* Don't free dir. */
601 return;
252b5132
RH
602
603 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
604 strcpy (dir + dirlen, "/../lib");
605 if (check_for_scripts_dir (dir))
606 return;
607
6d5e62f8
KH
608 /* Well, we tried. */
609 free (dir);
252b5132
RH
610}
611
612void
613add_ysym (name)
614 const char *name;
615{
616 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
617 {
618 link_info.notice_hash = ((struct bfd_hash_table *)
619 xmalloc (sizeof (struct bfd_hash_table)));
620 if (! bfd_hash_table_init_n (link_info.notice_hash,
621 bfd_hash_newfunc,
622 61))
623 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
6d5e62f8 624 }
252b5132
RH
625
626 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
627 == (struct bfd_hash_entry *) NULL)
628 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
629}
630
631/* Record a symbol to be wrapped, from the --wrap option. */
632
633void
634add_wrap (name)
635 const char *name;
636{
637 if (link_info.wrap_hash == NULL)
638 {
639 link_info.wrap_hash = ((struct bfd_hash_table *)
640 xmalloc (sizeof (struct bfd_hash_table)));
641 if (! bfd_hash_table_init_n (link_info.wrap_hash,
642 bfd_hash_newfunc,
643 61))
644 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
645 }
646 if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
647 einfo (_("%P%F: bfd_hash_lookup failed: %E\n"));
648}
649
650/* Handle the -retain-symbols-file option. */
651
652void
653add_keepsyms_file (filename)
654 const char *filename;
655{
656 FILE *file;
657 char *buf;
658 size_t bufsize;
659 int c;
660
661 if (link_info.strip == strip_some)
662 einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
663
664 file = fopen (filename, "r");
665 if (file == (FILE *) NULL)
666 {
667 bfd_set_error (bfd_error_system_call);
668 einfo ("%X%P: %s: %E\n", filename);
669 return;
670 }
671
672 link_info.keep_hash = ((struct bfd_hash_table *)
673 xmalloc (sizeof (struct bfd_hash_table)));
674 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
675 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
676
677 bufsize = 100;
678 buf = (char *) xmalloc (bufsize);
679
680 c = getc (file);
681 while (c != EOF)
682 {
683 while (isspace (c))
684 c = getc (file);
685
686 if (c != EOF)
687 {
688 size_t len = 0;
689
690 while (! isspace (c) && c != EOF)
691 {
692 buf[len] = c;
693 ++len;
694 if (len >= bufsize)
695 {
696 bufsize *= 2;
697 buf = xrealloc (buf, bufsize);
698 }
699 c = getc (file);
700 }
701
702 buf[len] = '\0';
703
704 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
705 == (struct bfd_hash_entry *) NULL)
706 einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n"));
707 }
708 }
709
710 if (link_info.strip != strip_none)
711 einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
712
713 link_info.strip = strip_some;
714}
715\f
716/* Callbacks from the BFD linker routines. */
717
718/* This is called when BFD has decided to include an archive member in
719 a link. */
720
252b5132
RH
721static boolean
722add_archive_element (info, abfd, name)
87f2a346 723 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
724 bfd *abfd;
725 const char *name;
726{
727 lang_input_statement_type *input;
728
729 input = ((lang_input_statement_type *)
730 xmalloc (sizeof (lang_input_statement_type)));
731 input->filename = abfd->filename;
732 input->local_sym_name = abfd->filename;
733 input->the_bfd = abfd;
734 input->asymbols = NULL;
735 input->next = NULL;
736 input->just_syms_flag = false;
737 input->loaded = false;
738 input->search_dirs_flag = false;
739
740 /* FIXME: The following fields are not set: header.next,
741 header.type, closed, passive_position, symbol_count,
742 next_real_file, is_archive, target, real. This bit of code is
743 from the old decode_library_subfile function. I don't know
744 whether any of those fields matters. */
745
746 ldlang_add_file (input);
747
748 if (config.map_file != (FILE *) NULL)
749 {
750 static boolean header_printed;
751 struct bfd_link_hash_entry *h;
752 bfd *from;
753 int len;
754
755 h = bfd_link_hash_lookup (link_info.hash, name, false, false, true);
756
757 if (h == NULL)
758 from = NULL;
759 else
760 {
761 switch (h->type)
762 {
763 default:
764 from = NULL;
765 break;
766
767 case bfd_link_hash_defined:
768 case bfd_link_hash_defweak:
769 from = h->u.def.section->owner;
770 break;
771
772 case bfd_link_hash_undefined:
773 case bfd_link_hash_undefweak:
774 from = h->u.undef.abfd;
775 break;
776
777 case bfd_link_hash_common:
778 from = h->u.c.p->section->owner;
779 break;
780 }
781 }
782
783 if (! header_printed)
784 {
785 char buf[100];
786
787 sprintf (buf, "%-29s %s\n\n", _("Archive member included"),
788 _("because of file (symbol)"));
789 minfo ("%s", buf);
790 header_printed = true;
791 }
792
793 if (bfd_my_archive (abfd) == NULL)
794 {
795 minfo ("%s", bfd_get_filename (abfd));
796 len = strlen (bfd_get_filename (abfd));
797 }
798 else
799 {
800 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)),
801 bfd_get_filename (abfd));
802 len = (strlen (bfd_get_filename (bfd_my_archive (abfd)))
803 + strlen (bfd_get_filename (abfd))
804 + 2);
805 }
806
807 if (len >= 29)
808 {
809 print_nl ();
810 len = 0;
811 }
812 while (len < 30)
813 {
814 print_space ();
815 ++len;
816 }
817
818 if (from != NULL)
819 minfo ("%B ", from);
820 if (h != NULL)
821 minfo ("(%T)\n", h->root.string);
822 else
823 minfo ("(%s)\n", name);
824 }
825
826 if (trace_files || trace_file_tries)
827 info_msg ("%I\n", input);
828
829 return true;
830}
831
832/* This is called when BFD has discovered a symbol which is defined
833 multiple times. */
834
252b5132
RH
835static boolean
836multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
87f2a346 837 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
838 const char *name;
839 bfd *obfd;
840 asection *osec;
841 bfd_vma oval;
842 bfd *nbfd;
843 asection *nsec;
844 bfd_vma nval;
845{
846 /* If either section has the output_section field set to
847 bfd_abs_section_ptr, it means that the section is being
848 discarded, and this is not really a multiple definition at all.
849 FIXME: It would be cleaner to somehow ignore symbols defined in
850 sections which are being discarded. */
851 if ((osec->output_section != NULL
852 && ! bfd_is_abs_section (osec)
853 && bfd_is_abs_section (osec->output_section))
854 || (nsec->output_section != NULL
855 && ! bfd_is_abs_section (nsec)
856 && bfd_is_abs_section (nsec->output_section)))
857 return true;
858
859 einfo (_("%X%C: multiple definition of `%T'\n"),
860 nbfd, nsec, nval, name);
861 if (obfd != (bfd *) NULL)
862 einfo (_("%D: first defined here\n"), obfd, osec, oval);
9b14b192
NC
863
864 if (command_line.relax)
865 {
866 einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n"));
867 command_line.relax = 0;
868 }
6d5e62f8 869
252b5132
RH
870 return true;
871}
872
873/* This is called when there is a definition of a common symbol, or
874 when a common symbol is found for a symbol that is already defined,
875 or when two common symbols are found. We only do something if
876 -warn-common was used. */
877
252b5132
RH
878static boolean
879multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
87f2a346 880 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
881 const char *name;
882 bfd *obfd;
883 enum bfd_link_hash_type otype;
884 bfd_vma osize;
885 bfd *nbfd;
886 enum bfd_link_hash_type ntype;
887 bfd_vma nsize;
888{
889 if (! config.warn_common)
890 return true;
891
892 if (ntype == bfd_link_hash_defined
893 || ntype == bfd_link_hash_defweak
894 || ntype == bfd_link_hash_indirect)
895 {
896 ASSERT (otype == bfd_link_hash_common);
897 einfo (_("%B: warning: definition of `%T' overriding common\n"),
898 nbfd, name);
899 if (obfd != NULL)
900 einfo (_("%B: warning: common is here\n"), obfd);
901 }
902 else if (otype == bfd_link_hash_defined
903 || otype == bfd_link_hash_defweak
904 || otype == bfd_link_hash_indirect)
905 {
906 ASSERT (ntype == bfd_link_hash_common);
907 einfo (_("%B: warning: common of `%T' overridden by definition\n"),
908 nbfd, name);
909 if (obfd != NULL)
910 einfo (_("%B: warning: defined here\n"), obfd);
911 }
912 else
913 {
914 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
915 if (osize > nsize)
916 {
917 einfo (_("%B: warning: common of `%T' overridden by larger common\n"),
918 nbfd, name);
919 if (obfd != NULL)
920 einfo (_("%B: warning: larger common is here\n"), obfd);
921 }
922 else if (nsize > osize)
923 {
924 einfo (_("%B: warning: common of `%T' overriding smaller common\n"),
925 nbfd, name);
926 if (obfd != NULL)
927 einfo (_("%B: warning: smaller common is here\n"), obfd);
928 }
929 else
930 {
931 einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name);
932 if (obfd != NULL)
933 einfo (_("%B: warning: previous common is here\n"), obfd);
934 }
935 }
936
937 return true;
938}
939
940/* This is called when BFD has discovered a set element. H is the
941 entry in the linker hash table for the set. SECTION and VALUE
942 represent a value which should be added to the set. */
943
252b5132
RH
944static boolean
945add_to_set (info, h, reloc, abfd, section, value)
87f2a346 946 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
947 struct bfd_link_hash_entry *h;
948 bfd_reloc_code_real_type reloc;
949 bfd *abfd;
950 asection *section;
951 bfd_vma value;
952{
953 if (config.warn_constructors)
954 einfo (_("%P: warning: global constructor %s used\n"),
955 h->root.string);
956
957 if (! config.build_constructors)
958 return true;
959
960 ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value);
961
962 if (h->type == bfd_link_hash_new)
963 {
964 h->type = bfd_link_hash_undefined;
965 h->u.undef.abfd = abfd;
966 /* We don't call bfd_link_add_undef to add this to the list of
967 undefined symbols because we are going to define it
968 ourselves. */
969 }
970
971 return true;
972}
973
974/* This is called when BFD has discovered a constructor. This is only
975 called for some object file formats--those which do not handle
976 constructors in some more clever fashion. This is similar to
977 adding an element to a set, but less general. */
978
979static boolean
980constructor_callback (info, constructor, name, abfd, section, value)
981 struct bfd_link_info *info;
982 boolean constructor;
983 const char *name;
984 bfd *abfd;
985 asection *section;
986 bfd_vma value;
987{
988 char *s;
989 struct bfd_link_hash_entry *h;
990 char set_name[1 + sizeof "__CTOR_LIST__"];
991
992 if (config.warn_constructors)
993 einfo (_("%P: warning: global constructor %s used\n"), name);
994
995 if (! config.build_constructors)
996 return true;
997
998 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
999 useful error message. */
1000 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL
1001 && (link_info.relocateable
1002 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
1003 einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
1004
1005 s = set_name;
1006 if (bfd_get_symbol_leading_char (abfd) != '\0')
1007 *s++ = bfd_get_symbol_leading_char (abfd);
1008 if (constructor)
1009 strcpy (s, "__CTOR_LIST__");
1010 else
1011 strcpy (s, "__DTOR_LIST__");
1012
1013 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
1014 if (h == (struct bfd_link_hash_entry *) NULL)
1015 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1016 if (h->type == bfd_link_hash_new)
1017 {
1018 h->type = bfd_link_hash_undefined;
1019 h->u.undef.abfd = abfd;
1020 /* We don't call bfd_link_add_undef to add this to the list of
1021 undefined symbols because we are going to define it
1022 ourselves. */
1023 }
1024
1025 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
1026 return true;
1027}
1028
1029/* A structure used by warning_callback to pass information through
1030 bfd_map_over_sections. */
1031
89cdebba 1032struct warning_callback_info {
252b5132
RH
1033 boolean found;
1034 const char *warning;
1035 const char *symbol;
1036 asymbol **asymbols;
1037};
1038
1039/* This is called when there is a reference to a warning symbol. */
1040
252b5132
RH
1041static boolean
1042warning_callback (info, warning, symbol, abfd, section, address)
87f2a346 1043 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
1044 const char *warning;
1045 const char *symbol;
1046 bfd *abfd;
1047 asection *section;
1048 bfd_vma address;
1049{
1050 /* This is a hack to support warn_multiple_gp. FIXME: This should
1051 have a cleaner interface, but what? */
1052 if (! config.warn_multiple_gp
1053 && strcmp (warning, "using multiple gp values") == 0)
1054 return true;
1055
1056 if (section != NULL)
1057 einfo ("%C: %s\n", abfd, section, address, warning);
1058 else if (abfd == NULL)
1059 einfo ("%P: %s\n", warning);
1060 else if (symbol == NULL)
1061 einfo ("%B: %s\n", abfd, warning);
1062 else
1063 {
1064 lang_input_statement_type *entry;
1065 asymbol **asymbols;
1066 struct warning_callback_info info;
1067
1068 /* Look through the relocs to see if we can find a plausible
1069 address. */
1070
1071 entry = (lang_input_statement_type *) abfd->usrdata;
1072 if (entry != NULL && entry->asymbols != NULL)
1073 asymbols = entry->asymbols;
1074 else
1075 {
1076 long symsize;
1077 long symbol_count;
1078
1079 symsize = bfd_get_symtab_upper_bound (abfd);
1080 if (symsize < 0)
1081 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1082 asymbols = (asymbol **) xmalloc (symsize);
1083 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
1084 if (symbol_count < 0)
1085 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
1086 if (entry != NULL)
1087 {
1088 entry->asymbols = asymbols;
1089 entry->symbol_count = symbol_count;
1090 }
1091 }
1092
1093 info.found = false;
1094 info.warning = warning;
1095 info.symbol = symbol;
1096 info.asymbols = asymbols;
1097 bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info);
1098
1099 if (! info.found)
1100 einfo ("%B: %s\n", abfd, warning);
1101
1102 if (entry == NULL)
1103 free (asymbols);
1104 }
1105
1106 return true;
1107}
1108
1109/* This is called by warning_callback for each section. It checks the
1110 relocs of the section to see if it can find a reference to the
1111 symbol which triggered the warning. If it can, it uses the reloc
1112 to give an error message with a file and line number. */
1113
1114static void
1115warning_find_reloc (abfd, sec, iarg)
1116 bfd *abfd;
1117 asection *sec;
1118 PTR iarg;
1119{
1120 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
1121 long relsize;
1122 arelent **relpp;
1123 long relcount;
1124 arelent **p, **pend;
1125
1126 if (info->found)
1127 return;
1128
1129 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1130 if (relsize < 0)
1131 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1132 if (relsize == 0)
1133 return;
1134
1135 relpp = (arelent **) xmalloc (relsize);
1136 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1137 if (relcount < 0)
1138 einfo (_("%B%F: could not read relocs: %E\n"), abfd);
1139
1140 p = relpp;
1141 pend = p + relcount;
1142 for (; p < pend && *p != NULL; p++)
1143 {
1144 arelent *q = *p;
1145
1146 if (q->sym_ptr_ptr != NULL
1147 && *q->sym_ptr_ptr != NULL
1148 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1149 {
1150 /* We found a reloc for the symbol we are looking for. */
1151 einfo ("%C: %s\n", abfd, sec, q->address, info->warning);
1152 info->found = true;
1153 break;
1154 }
1155 }
1156
1157 free (relpp);
1158}
1159
1160/* This is called when an undefined symbol is found. */
1161
252b5132 1162static boolean
b6f29aaa 1163undefined_symbol (info, name, abfd, section, address, fatal)
87f2a346 1164 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
1165 const char *name;
1166 bfd *abfd;
1167 asection *section;
1168 bfd_vma address;
a712da20 1169 boolean fatal ATTRIBUTE_UNUSED;
252b5132
RH
1170{
1171 static char *error_name;
1172 static unsigned int error_count;
1173
1174#define MAX_ERRORS_IN_A_ROW 5
1175
1176 if (config.warn_once)
1177 {
1178 static struct bfd_hash_table *hash;
1179
1180 /* Only warn once about a particular undefined symbol. */
1181
1182 if (hash == NULL)
1183 {
1184 hash = ((struct bfd_hash_table *)
1185 xmalloc (sizeof (struct bfd_hash_table)));
1186 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
1187 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
1188 }
1189
1190 if (bfd_hash_lookup (hash, name, false, false) != NULL)
1191 return true;
1192
1193 if (bfd_hash_lookup (hash, name, true, true) == NULL)
1194 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
1195 }
1196
1197 /* We never print more than a reasonable number of errors in a row
1198 for a single symbol. */
1199 if (error_name != (char *) NULL
1200 && strcmp (name, error_name) == 0)
1201 ++error_count;
1202 else
1203 {
1204 error_count = 0;
1205 if (error_name != (char *) NULL)
1206 free (error_name);
1207 error_name = buystring (name);
1208 }
1209
1210 if (section != NULL)
1211 {
1212 if (error_count < MAX_ERRORS_IN_A_ROW)
b6f29aaa
L
1213 {
1214 einfo (_("%C: undefined reference to `%T'\n"),
1215 abfd, section, address, name);
1216 if (fatal)
1217 einfo ("%X");
1218 }
252b5132
RH
1219 else if (error_count == MAX_ERRORS_IN_A_ROW)
1220 einfo (_("%D: more undefined references to `%T' follow\n"),
1221 abfd, section, address, name);
1222 }
1223 else
1224 {
1225 if (error_count < MAX_ERRORS_IN_A_ROW)
b6f29aaa
L
1226 {
1227 einfo (_("%B: undefined reference to `%T'\n"),
1228 abfd, name);
1229 if (fatal)
1230 einfo ("%X");
1231 }
252b5132
RH
1232 else if (error_count == MAX_ERRORS_IN_A_ROW)
1233 einfo (_("%B: more undefined references to `%T' follow\n"),
1234 abfd, name);
1235 }
1236
1237 return true;
1238}
1239
1240/* This is called when a reloc overflows. */
1241
252b5132
RH
1242static boolean
1243reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
87f2a346 1244 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
1245 const char *name;
1246 const char *reloc_name;
1247 bfd_vma addend;
1248 bfd *abfd;
1249 asection *section;
1250 bfd_vma address;
1251{
1252 if (abfd == (bfd *) NULL)
1253 einfo (_("%P%X: generated"));
1254 else
1255 einfo ("%X%C:", abfd, section, address);
1256 einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name);
1257 if (addend != 0)
1258 einfo ("+%v", addend);
1259 einfo ("\n");
1260 return true;
1261}
1262
1263/* This is called when a dangerous relocation is made. */
1264
252b5132
RH
1265static boolean
1266reloc_dangerous (info, message, abfd, section, address)
87f2a346 1267 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
1268 const char *message;
1269 bfd *abfd;
1270 asection *section;
1271 bfd_vma address;
1272{
1273 if (abfd == (bfd *) NULL)
1274 einfo (_("%P%X: generated"));
1275 else
1276 einfo ("%X%C:", abfd, section, address);
1277 einfo (_("dangerous relocation: %s\n"), message);
1278 return true;
1279}
1280
1281/* This is called when a reloc is being generated attached to a symbol
1282 that is not being output. */
1283
252b5132
RH
1284static boolean
1285unattached_reloc (info, name, abfd, section, address)
87f2a346 1286 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
1287 const char *name;
1288 bfd *abfd;
1289 asection *section;
1290 bfd_vma address;
1291{
1292 if (abfd == (bfd *) NULL)
1293 einfo (_("%P%X: generated"));
1294 else
1295 einfo ("%X%C:", abfd, section, address);
1296 einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name);
1297 return true;
1298}
1299
1300/* This is called if link_info.notice_all is set, or when a symbol in
1301 link_info.notice_hash is found. Symbols are put in notice_hash
1302 using the -y option. */
1303
1304static boolean
1305notice (info, name, abfd, section, value)
1306 struct bfd_link_info *info;
1307 const char *name;
1308 bfd *abfd;
1309 asection *section;
1310 bfd_vma value;
1311{
1312 if (! info->notice_all
1313 || (info->notice_hash != NULL
1314 && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL))
1315 {
1316 if (bfd_is_und_section (section))
1317 einfo ("%B: reference to %s\n", abfd, name);
1318 else
1319 einfo ("%B: definition of %s\n", abfd, name);
1320 }
1321
1322 if (command_line.cref || nocrossref_list != NULL)
1323 add_cref (name, abfd, section, value);
1324
1325 return true;
1326}
This page took 0.105701 seconds and 4 git commands to generate.