source: branches/libc-0.6/src/binutils/ld/ldmain.c

Last change on this file was 618, checked in by bird, 22 years ago

Joined the port of 2.11.2 with 2.14.

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