source: vendor/binutils/current/ld/ldmain.c

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

binutils v2.14 - offical sources.

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