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