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