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