* configure.in: Permit --enable-shared to specify a list of
[deliverable/binutils-gdb.git] / ld / ldmain.c
CommitLineData
8594f568 1/* Main program of GNU linker.
7d6439d9 2 Copyright (C) 1991, 92, 93, 94, 1995 Free Software Foundation, Inc.
ce4d59e2 3 Written by Steve Chamberlain steve@cygnus.com
e47bfa63 4
2fa0b342
DHW
5This file is part of GLD, the Gnu Linker.
6
7GLD is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
ce4d59e2 9the Free Software Foundation; either version 2, or (at your option)
2fa0b342
DHW
10any later version.
11
12GLD is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GLD; see the file COPYING. If not, write to
943fbd5b 19the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
2fa0b342 20
2fa0b342 21
2fa0b342 22#include "bfd.h"
f177a611 23#include "sysdep.h"
b9395be3 24#include <stdio.h>
7d6439d9 25#include <ctype.h>
d4e5e3c3 26#include "libiberty.h"
a735edad 27#include "progress.h"
b9395be3 28#include "bfdlink.h"
2fa0b342 29
2fa0b342
DHW
30#include "ld.h"
31#include "ldmain.h"
32#include "ldmisc.h"
33#include "ldwrite.h"
34#include "ldgram.h"
fcf276c4 35#include "ldexp.h"
2fa0b342 36#include "ldlang.h"
8c514453 37#include "ldemul.h"
2fa0b342
DHW
38#include "ldlex.h"
39#include "ldfile.h"
c611e285 40#include "ldctor.h"
9d1fe8a4 41
922018a1 42/* Somewhere above, sys/stat.h got included . . . . */
d723cd17
DM
43#if !defined(S_ISDIR) && defined(S_IFDIR)
44#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
45#endif
46
47#include <string.h>
48
fcf276c4
ILT
49static char *get_emulation PARAMS ((int, char **));
50static void set_scripts_dir PARAMS ((void));
e47bfa63 51
2fa0b342
DHW
52/* EXPORTS */
53
54char *default_target;
fcf276c4 55const char *output_filename = "a.out";
e47bfa63 56
2fa0b342
DHW
57/* Name this program was invoked by. */
58char *program_name;
59
60/* The file that we're creating */
b6316534 61bfd *output_bfd = 0;
2fa0b342 62
173a0c3d
DM
63/* Set by -G argument, for MIPS ECOFF target. */
64int g_switch_value = 8;
65
2fa0b342
DHW
66/* Nonzero means print names of input files as processed. */
67boolean trace_files;
68
bbd2521f
DM
69/* Nonzero means same, but note open failures, too. */
70boolean trace_file_tries;
71
7b40f2b1
DM
72/* Nonzero means version number was printed, so exit successfully
73 instead of complaining if no input files are given. */
74boolean version_printed;
75
f2f55b16
ILT
76/* Nonzero means link in every member of an archive. */
77boolean whole_archive;
78
2fa0b342 79args_type command_line;
173a0c3d 80
2fa0b342 81ld_config_type config;
9f629407
ILT
82
83static boolean check_for_scripts_dir PARAMS ((char *dir));
b9395be3
ILT
84static boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *,
85 const char *));
86static boolean multiple_definition PARAMS ((struct bfd_link_info *,
87 const char *,
88 bfd *, asection *, bfd_vma,
89 bfd *, asection *, bfd_vma));
90static boolean multiple_common PARAMS ((struct bfd_link_info *,
91 const char *, bfd *,
92 enum bfd_link_hash_type, bfd_vma,
93 bfd *, enum bfd_link_hash_type,
94 bfd_vma));
95static boolean add_to_set PARAMS ((struct bfd_link_info *,
96 struct bfd_link_hash_entry *,
2a9fa50c 97 bfd_reloc_code_real_type,
b9395be3
ILT
98 bfd *, asection *, bfd_vma));
99static boolean constructor_callback PARAMS ((struct bfd_link_info *,
100 boolean constructor,
b9395be3
ILT
101 const char *name,
102 bfd *, asection *, bfd_vma));
103static boolean warning_callback PARAMS ((struct bfd_link_info *,
7d6439d9
ILT
104 const char *, const char *, bfd *,
105 asection *, bfd_vma));
106static void warning_find_reloc PARAMS ((bfd *, asection *, PTR));
b9395be3
ILT
107static boolean undefined_symbol PARAMS ((struct bfd_link_info *,
108 const char *, bfd *,
109 asection *, bfd_vma));
5dad4c97
ILT
110static boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *,
111 const char *, bfd_vma,
112 bfd *, asection *, bfd_vma));
b9395be3
ILT
113static boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *,
114 bfd *, asection *, bfd_vma));
115static boolean unattached_reloc PARAMS ((struct bfd_link_info *,
116 const char *, bfd *, asection *,
117 bfd_vma));
118static boolean notice_ysym PARAMS ((struct bfd_link_info *, const char *,
119 bfd *, asection *, bfd_vma));
120
121static struct bfd_link_callbacks link_callbacks =
122{
123 add_archive_element,
124 multiple_definition,
125 multiple_common,
126 add_to_set,
127 constructor_callback,
128 warning_callback,
129 undefined_symbol,
130 reloc_overflow,
131 reloc_dangerous,
132 unattached_reloc,
133 notice_ysym
134};
135
136struct bfd_link_info link_info;
173a0c3d 137\f
0b2f8d2e
DM
138static void
139remove_output ()
140{
141 if (output_filename)
142 {
143 if (output_bfd && output_bfd->iostream)
144 fclose((FILE *)(output_bfd->iostream));
145 if (delete_output_file_on_failure)
146 unlink (output_filename);
147 }
148}
149
9f629407 150int
2fa0b342 151main (argc, argv)
2fa0b342 152 int argc;
9f629407 153 char **argv;
2fa0b342
DHW
154{
155 char *emulation;
8594f568 156 long start_time = get_run_time ();
e47bfa63 157
2fa0b342 158 program_name = argv[0];
5bcb7f28 159 xmalloc_set_program_name (program_name);
99fe4553 160
a735edad
ILT
161 START_PROGRESS (program_name, 0);
162
e47bfa63 163 bfd_init ();
bfbdc80f 164
28113e82
ILT
165 bfd_set_error_program_name (program_name);
166
5bcb7f28 167 xatexit (remove_output);
0b2f8d2e 168
2fa0b342 169 /* Initialize the data about options. */
7b40f2b1 170 trace_files = trace_file_tries = version_printed = false;
f2f55b16 171 whole_archive = false;
c96386c4 172 config.traditional_format = false;
b9395be3 173 config.build_constructors = true;
2a9fa50c 174 config.dynamic_link = false;
2fa0b342 175 command_line.force_common_definition = false;
7fb9ca5f 176 command_line.interpreter = NULL;
cc23cc69 177 command_line.rpath = NULL;
2fa0b342 178
b9395be3
ILT
179 link_info.callbacks = &link_callbacks;
180 link_info.relocateable = false;
64887de2 181 link_info.shared = false;
4551e108 182 link_info.symbolic = false;
7d6439d9 183 link_info.static_link = false;
b9395be3
ILT
184 link_info.strip = strip_none;
185 link_info.discard = discard_none;
186 link_info.lprefix_len = 1;
187 link_info.lprefix = "L";
188 link_info.keep_memory = true;
189 link_info.input_bfds = NULL;
190 link_info.create_object_symbols_section = NULL;
191 link_info.hash = NULL;
192 link_info.keep_hash = NULL;
193 link_info.notice_hash = NULL;
e3d73386 194 link_info.wrap_hash = NULL;
7d6439d9 195
e47bfa63 196 ldfile_add_arch ("");
a72f4e5f 197
2fa0b342
DHW
198 config.make_executable = true;
199 force_make_executable = false;
ce4d59e2
SC
200 config.magic_demand_paged = true;
201 config.text_read_only = true;
2fa0b342 202 config.make_executable = true;
1418c83b 203
d723cd17 204 emulation = get_emulation (argc, argv);
e47bfa63
SC
205 ldemul_choose_mode (emulation);
206 default_target = ldemul_choose_target ();
207 lang_init ();
208 ldemul_before_parse ();
2fa0b342 209 lang_has_input_file = false;
e47bfa63 210 parse_args (argc, argv);
f3739bc3 211
7d6439d9
ILT
212 ldemul_set_symbols ();
213
2a9fa50c
ILT
214 if (link_info.relocateable)
215 {
216 if (command_line.relax)
217 einfo ("%P%F: -relax and -r may not be used together\n");
64887de2
ILT
218 if (link_info.shared)
219 einfo ("%P%F: -r and -shared may not be used together\n");
2a9fa50c
ILT
220 }
221
8ed88239
ILT
222 /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I
223 don't see how else this can be handled, since in this case we
224 must preserve all externally visible symbols. */
225 if (link_info.relocateable && link_info.strip == strip_all)
226 {
227 link_info.strip = strip_debugger;
228 if (link_info.discard == discard_none)
229 link_info.discard = discard_all;
230 }
231
973e421e
ILT
232 /* This essentially adds another -L directory so this must be done after
233 the -L's in argv have been processed. */
234 set_scripts_dir ();
235
bbd2521f
DM
236 if (had_script == false)
237 {
238 /* Read the emulation's appropriate default script. */
2a28d8b0
DM
239 int isfile;
240 char *s = ldemul_get_script (&isfile);
241
242 if (isfile)
d4e5e3c3 243 ldfile_open_command_file (s);
2a28d8b0 244 else
22c41f00
ILT
245 {
246 if (trace_file_tries)
247 {
248 info_msg ("using internal linker script:\n");
249 info_msg ("==================================================\n");
250 info_msg (s);
251 info_msg ("\n==================================================\n");
252 }
7d6439d9 253 lex_string = s;
22c41f00
ILT
254 lex_redirect (s);
255 }
d4e5e3c3
DM
256 parser_input = input_script;
257 yyparse ();
7d6439d9 258 lex_string = NULL;
bbd2521f
DM
259 }
260
e47bfa63 261 lang_final ();
9d1fe8a4 262
e47bfa63
SC
263 if (lang_has_input_file == false)
264 {
7b40f2b1 265 if (version_printed)
0b2f8d2e 266 xexit (0);
973e421e 267 einfo ("%P%F: no input files\n");
870f54b2 268 }
2fa0b342 269
bbd2521f
DM
270 if (trace_files)
271 {
973e421e 272 info_msg ("%P: mode %s\n", emulation);
bbd2521f
DM
273 }
274
e47bfa63 275 ldemul_after_parse ();
870f54b2 276
9d1fe8a4 277
e47bfa63 278 if (config.map_filename)
870f54b2 279 {
e47bfa63 280 if (strcmp (config.map_filename, "-") == 0)
2e2bf962 281 {
e47bfa63 282 config.map_file = stdout;
2e2bf962 283 }
e47bfa63
SC
284 else
285 {
286 config.map_file = fopen (config.map_filename, FOPEN_WT);
287 if (config.map_file == (FILE *) NULL)
288 {
de71eb77 289 bfd_set_error (bfd_error_system_call);
bbd2521f 290 einfo ("%P%F: cannot open map file %s: %E\n",
e47bfa63
SC
291 config.map_filename);
292 }
293 }
294 }
870f54b2 295
2e2bf962 296
e47bfa63 297 lang_process ();
2fa0b342 298
2fa0b342
DHW
299 /* Print error messages for any missing symbols, for any warning
300 symbols, and possibly multiple definitions */
301
2fa0b342 302
e47bfa63
SC
303 if (config.text_read_only)
304 {
305 /* Look for a text section and mark the readonly attribute in it */
306 asection *found = bfd_get_section_by_name (output_bfd, ".text");
307
308 if (found != (asection *) NULL)
309 {
310 found->flags |= SEC_READONLY;
311 }
3e4c643d 312 }
2fa0b342 313
a735edad 314 if (link_info.relocateable)
f3739bc3 315 output_bfd->flags &= ~EXEC_P;
a72f4e5f 316 else
f3739bc3 317 output_bfd->flags |= EXEC_P;
b257477f 318
f3739bc3 319 ldwrite ();
b257477f 320
f3739bc3
SC
321 /* Even if we're producing relocateable output, some non-fatal errors should
322 be reported in the exit status. (What non-fatal errors, if any, do we
323 want to ignore for relocateable output?) */
a72f4e5f 324
f3739bc3
SC
325 if (config.make_executable == false && force_make_executable == false)
326 {
327 if (trace_files == true)
e47bfa63 328 {
973e421e 329 einfo ("%P: link errors found, deleting executable `%s'\n",
f3739bc3 330 output_filename);
e47bfa63 331 }
a72f4e5f 332
f3739bc3
SC
333 if (output_bfd->iostream)
334 fclose ((FILE *) (output_bfd->iostream));
a72f4e5f 335
f3739bc3 336 unlink (output_filename);
0b2f8d2e 337 xexit (1);
f3739bc3
SC
338 }
339 else
340 {
2a9fa50c
ILT
341 if (! bfd_close (output_bfd))
342 einfo ("%F%B: final close failed: %E\n", output_bfd);
f3739bc3 343 }
2fa0b342 344
a735edad
ILT
345 END_PROGRESS (program_name);
346
b9395be3
ILT
347 if (config.stats)
348 {
349 extern char **environ;
eac6290c 350#ifdef HAVE_SBRK
b9395be3 351 char *lim = (char *) sbrk (0);
de71eb77 352#endif
8594f568 353 long run_time = get_run_time () - start_time;
b9395be3 354
5bcb7f28 355 fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
8594f568 356 program_name, run_time / 1000000, run_time % 1000000);
eac6290c 357#ifdef HAVE_SBRK
b9395be3
ILT
358 fprintf (stderr, "%s: data size %ld\n", program_name,
359 (long) (lim - (char *) &environ));
de71eb77 360#endif
b9395be3
ILT
361 }
362
52a8ebfe
DM
363 /* Prevent remove_output from doing anything, after a successful link. */
364 output_filename = NULL;
365
0b2f8d2e 366 xexit (0);
9f629407 367 return 0;
d723cd17
DM
368}
369
370/* We need to find any explicitly given emulation in order to initialize the
371 state that's needed by the lex&yacc argument parser (parse_args). */
372
373static char *
374get_emulation (argc, argv)
375 int argc;
376 char **argv;
377{
378 char *emulation;
379 int i;
380
d723cd17
DM
381 emulation = (char *) getenv (EMULATION_ENVIRON);
382 if (emulation == NULL)
383 emulation = DEFAULT_EMULATION;
d723cd17
DM
384
385 for (i = 1; i < argc; i++)
386 {
387 if (!strncmp (argv[i], "-m", 2))
388 {
389 if (argv[i][2] == '\0')
390 {
391 /* -m EMUL */
392 if (i < argc - 1)
393 {
394 emulation = argv[i + 1];
395 i++;
396 }
397 else
398 {
973e421e 399 einfo("%P%F: missing argument to -m\n");
d723cd17
DM
400 }
401 }
973e421e
ILT
402 else if (strcmp (argv[i], "-mips1") == 0
403 || strcmp (argv[i], "-mips2") == 0
404 || strcmp (argv[i], "-mips3") == 0)
405 {
406 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
407 passed to the linker by some MIPS compilers. They
408 generally tell the linker to use a slightly different
409 library path. Perhaps someday these should be
410 implemented as emulations; until then, we just ignore
411 the arguments and hope that nobody ever creates
412 emulations named ips1, ips2 or ips3. */
413 }
cbbf9608
ILT
414 else if (strcmp (argv[i], "-m486") == 0)
415 {
416 /* FIXME: The argument -m486 is passed to the linker on
417 some Linux systems. Hope that nobody creates an
418 emulation named 486. */
419 }
d723cd17
DM
420 else
421 {
422 /* -mEMUL */
423 emulation = &argv[i][2];
424 }
425 }
426 }
427
428 return emulation;
429}
430
431/* If directory DIR contains an "ldscripts" subdirectory,
432 add DIR to the library search path and return true,
433 else return false. */
434
435static boolean
436check_for_scripts_dir (dir)
437 char *dir;
438{
439 size_t dirlen;
440 char *buf;
441 struct stat s;
442 boolean res;
443
444 dirlen = strlen (dir);
445 /* sizeof counts the terminating NUL. */
0b2f8d2e 446 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
d723cd17
DM
447 sprintf (buf, "%s/ldscripts", dir);
448
449 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
f4208462 450 free (buf);
d723cd17 451 if (res)
0cd82d00 452 ldfile_add_library_path (dir, false);
d723cd17
DM
453 return res;
454}
455
456/* Set the default directory for finding script files.
f4208462
DM
457 Libraries will be searched for here too, but that's ok.
458 We look for the "ldscripts" directory in:
459
f4208462 460 SCRIPTDIR (passed from Makefile)
bbd2521f
DM
461 the dir where this program is (for using it from the build tree)
462 the dir where this program is/../lib (for installing the tool suite elsewhere) */
d723cd17
DM
463
464static void
465set_scripts_dir ()
466{
f4208462
DM
467 char *end, *dir;
468 size_t dirlen;
469
d723cd17 470 if (check_for_scripts_dir (SCRIPTDIR))
f4208462 471 return; /* We've been installed normally. */
d723cd17
DM
472
473 /* Look for "ldscripts" in the dir where our binary is. */
474 end = strrchr (program_name, '/');
7d6439d9
ILT
475
476 if (end == NULL)
bbd2521f 477 {
7d6439d9
ILT
478 /* Don't look for ldscripts in the current directory. There is
479 too much potential for confusion. */
480 return;
bbd2521f 481 }
f4208462 482
7d6439d9
ILT
483 dirlen = end - program_name;
484 /* Make a copy of program_name in dir.
485 Leave room for later "/../lib". */
486 dir = (char *) xmalloc (dirlen + 8);
487 strncpy (dir, program_name, dirlen);
488 dir[dirlen] = '\0';
489
f4208462
DM
490 if (check_for_scripts_dir (dir))
491 return; /* Don't free dir. */
492
493 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
494 strcpy (dir + dirlen, "/../lib");
495 if (check_for_scripts_dir (dir))
496 return;
497
498 free (dir); /* Well, we tried. */
d723cd17 499}
2fa0b342 500
e47bfa63 501void
b9395be3
ILT
502add_ysym (name)
503 const char *name;
2fa0b342 504{
b9395be3
ILT
505 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
506 {
507 link_info.notice_hash = ((struct bfd_hash_table *)
0b2f8d2e 508 xmalloc (sizeof (struct bfd_hash_table)));
b9395be3
ILT
509 if (! bfd_hash_table_init_n (link_info.notice_hash,
510 bfd_hash_newfunc,
511 61))
512 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
513 }
514
515 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
516 == (struct bfd_hash_entry *) NULL)
517 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
2fa0b342 518}
e47bfa63 519
28113e82
ILT
520/* Record a symbol to be wrapped, from the --wrap option. */
521
522void
523add_wrap (name)
524 const char *name;
525{
526 if (link_info.wrap_hash == NULL)
527 {
528 link_info.wrap_hash = ((struct bfd_hash_table *)
529 xmalloc (sizeof (struct bfd_hash_table)));
530 if (! bfd_hash_table_init_n (link_info.wrap_hash,
531 bfd_hash_newfunc,
532 61))
533 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
534 }
535 if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
536 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
537}
538
b9395be3 539/* Handle the -retain-symbols-file option. */
2fa0b342 540
2fa0b342 541void
b9395be3
ILT
542add_keepsyms_file (filename)
543 const char *filename;
2fa0b342 544{
b9395be3
ILT
545 FILE *file;
546 char *buf;
547 size_t bufsize;
548 int c;
2fa0b342 549
b9395be3
ILT
550 if (link_info.strip == strip_some)
551 einfo ("%X%P: error: duplicate retain-symbols-file\n");
3e4c643d 552
b9395be3
ILT
553 file = fopen (filename, "r");
554 if (file == (FILE *) NULL)
e47bfa63 555 {
5bcb7f28 556 bfd_set_error (bfd_error_system_call);
7d6439d9 557 einfo ("%X%P: %s: %E\n", filename);
b9395be3 558 return;
be1627d3 559 }
be1627d3 560
b9395be3 561 link_info.keep_hash = ((struct bfd_hash_table *)
0b2f8d2e 562 xmalloc (sizeof (struct bfd_hash_table)));
b9395be3
ILT
563 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
564 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
2fa0b342 565
b9395be3 566 bufsize = 100;
0b2f8d2e 567 buf = (char *) xmalloc (bufsize);
c611e285 568
b9395be3
ILT
569 c = getc (file);
570 while (c != EOF)
29f33467 571 {
b9395be3
ILT
572 while (isspace (c))
573 c = getc (file);
e47bfa63 574
b9395be3 575 if (c != EOF)
29f33467 576 {
b9395be3 577 size_t len = 0;
8a045e50 578
b9395be3 579 while (! isspace (c) && c != EOF)
29f33467 580 {
b9395be3
ILT
581 buf[len] = c;
582 ++len;
583 if (len >= bufsize)
29f33467 584 {
b9395be3 585 bufsize *= 2;
0b2f8d2e 586 buf = xrealloc (buf, bufsize);
29f33467 587 }
b9395be3 588 c = getc (file);
29f33467 589 }
81016051 590
b9395be3
ILT
591 buf[len] = '\0';
592
593 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
594 == (struct bfd_hash_entry *) NULL)
7d6439d9 595 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E\n");
29f33467 596 }
e47bfa63 597 }
2fa0b342 598
b9395be3
ILT
599 if (link_info.strip != strip_none)
600 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
8a045e50 601
b9395be3
ILT
602 link_info.strip = strip_some;
603}
604\f
605/* Callbacks from the BFD linker routines. */
2fa0b342 606
b9395be3
ILT
607/* This is called when BFD has decided to include an archive member in
608 a link. */
2fa0b342 609
b9395be3
ILT
610/*ARGSUSED*/
611static boolean
612add_archive_element (info, abfd, name)
613 struct bfd_link_info *info;
614 bfd *abfd;
615 const char *name;
2fa0b342 616{
b9395be3
ILT
617 lang_input_statement_type *input;
618
619 input = ((lang_input_statement_type *)
7d6439d9 620 xmalloc (sizeof (lang_input_statement_type)));
b9395be3
ILT
621 input->filename = abfd->filename;
622 input->local_sym_name = abfd->filename;
623 input->the_bfd = abfd;
624 input->asymbols = NULL;
b9395be3
ILT
625 input->next = NULL;
626 input->just_syms_flag = false;
627 input->loaded = false;
210c52ac 628 input->search_dirs_flag = false;
b9395be3
ILT
629
630 /* FIXME: The following fields are not set: header.next,
210c52ac
ILT
631 header.type, closed, passive_position, symbol_count,
632 next_real_file, is_archive, target, real, common_section,
633 common_output_section, complained. This bit of code is from the
634 old decode_library_subfile function. I don't know whether any of
635 those fields matters. */
b9395be3
ILT
636
637 ldlang_add_file (input);
638
2a9fa50c
ILT
639 if (config.map_file != (FILE *) NULL)
640 minfo ("%s needed due to %T\n", abfd->filename, name);
b9395be3 641
5dad4c97
ILT
642 if (trace_files || trace_file_tries)
643 info_msg ("%I\n", input);
644
b9395be3
ILT
645 return true;
646}
2fa0b342 647
b9395be3
ILT
648/* This is called when BFD has discovered a symbol which is defined
649 multiple times. */
2fa0b342 650
b9395be3
ILT
651/*ARGSUSED*/
652static boolean
653multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
654 struct bfd_link_info *info;
655 const char *name;
656 bfd *obfd;
657 asection *osec;
658 bfd_vma oval;
659 bfd *nbfd;
660 asection *nsec;
661 bfd_vma nval;
662{
663 einfo ("%X%C: multiple definition of `%T'\n",
664 nbfd, nsec, nval, name);
665 if (obfd != (bfd *) NULL)
cc23cc69 666 einfo ("%D: first defined here\n", obfd, osec, oval);
b9395be3 667 return true;
2fa0b342
DHW
668}
669
b9395be3
ILT
670/* This is called when there is a definition of a common symbol, or
671 when a common symbol is found for a symbol that is already defined,
672 or when two common symbols are found. We only do something if
673 -warn-common was used. */
674
675/*ARGSUSED*/
676static boolean
677multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
678 struct bfd_link_info *info;
679 const char *name;
680 bfd *obfd;
681 enum bfd_link_hash_type otype;
682 bfd_vma osize;
683 bfd *nbfd;
684 enum bfd_link_hash_type ntype;
685 bfd_vma nsize;
99fe4553 686{
b9395be3
ILT
687 if (! config.warn_common)
688 return true;
99fe4553 689
8ed88239
ILT
690 if (ntype == bfd_link_hash_defined
691 || ntype == bfd_link_hash_defweak
692 || ntype == bfd_link_hash_indirect)
e47bfa63 693 {
b9395be3
ILT
694 ASSERT (otype == bfd_link_hash_common);
695 einfo ("%B: warning: definition of `%T' overriding common\n",
696 nbfd, name);
8ed88239
ILT
697 if (obfd != NULL)
698 einfo ("%B: warning: common is here\n", obfd);
e47bfa63 699 }
8ed88239
ILT
700 else if (otype == bfd_link_hash_defined
701 || otype == bfd_link_hash_defweak
702 || otype == bfd_link_hash_indirect)
2fa0b342 703 {
b9395be3
ILT
704 ASSERT (ntype == bfd_link_hash_common);
705 einfo ("%B: warning: common of `%T' overridden by definition\n",
706 nbfd, name);
8ed88239
ILT
707 if (obfd != NULL)
708 einfo ("%B: warning: defined here\n", obfd);
b9395be3
ILT
709 }
710 else
711 {
712 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
713 if (osize > nsize)
2fa0b342 714 {
b9395be3
ILT
715 einfo ("%B: warning: common of `%T' overridden by larger common\n",
716 nbfd, name);
8ed88239
ILT
717 if (obfd != NULL)
718 einfo ("%B: warning: larger common is here\n", obfd);
2fa0b342 719 }
b9395be3 720 else if (nsize > osize)
2fa0b342 721 {
b9395be3
ILT
722 einfo ("%B: warning: common of `%T' overriding smaller common\n",
723 nbfd, name);
8ed88239
ILT
724 if (obfd != NULL)
725 einfo ("%B: warning: smaller common is here\n", obfd);
2fa0b342 726 }
e47bfa63 727 else
2fa0b342 728 {
b9395be3 729 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
8ed88239
ILT
730 if (obfd != NULL)
731 einfo ("%B: warning: previous common is here\n", obfd);
2fa0b342
DHW
732 }
733 }
734
b9395be3 735 return true;
2fa0b342
DHW
736}
737
b9395be3
ILT
738/* This is called when BFD has discovered a set element. H is the
739 entry in the linker hash table for the set. SECTION and VALUE
740 represent a value which should be added to the set. */
2fa0b342 741
b9395be3
ILT
742/*ARGSUSED*/
743static boolean
2a9fa50c 744add_to_set (info, h, reloc, abfd, section, value)
b9395be3
ILT
745 struct bfd_link_info *info;
746 struct bfd_link_hash_entry *h;
2a9fa50c 747 bfd_reloc_code_real_type reloc;
b9395be3
ILT
748 bfd *abfd;
749 asection *section;
750 bfd_vma value;
2fa0b342 751{
7d6439d9
ILT
752 if (config.warn_constructors)
753 einfo ("%P: warning: global constructor %s used\n",
754 h->root.string);
755
2a9fa50c
ILT
756 if (! config.build_constructors)
757 return true;
758
4b7d2399 759 ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value);
2a9fa50c
ILT
760
761 if (h->type == bfd_link_hash_new)
762 {
763 h->type = bfd_link_hash_undefined;
764 h->u.undef.abfd = abfd;
765 /* We don't call bfd_link_add_undef to add this to the list of
766 undefined symbols because we are going to define it
767 ourselves. */
768 }
769
b9395be3
ILT
770 return true;
771}
1418c83b 772
b9395be3
ILT
773/* This is called when BFD has discovered a constructor. This is only
774 called for some object file formats--those which do not handle
775 constructors in some more clever fashion. This is similar to
776 adding an element to a set, but less general. */
2fa0b342 777
b9395be3 778static boolean
2a9fa50c 779constructor_callback (info, constructor, name, abfd, section, value)
b9395be3
ILT
780 struct bfd_link_info *info;
781 boolean constructor;
b9395be3
ILT
782 const char *name;
783 bfd *abfd;
784 asection *section;
785 bfd_vma value;
786{
b9395be3
ILT
787 char *s;
788 struct bfd_link_hash_entry *h;
7d6439d9
ILT
789 char set_name[1 + sizeof "__CTOR_LIST__"];
790
791 if (config.warn_constructors)
792 einfo ("%P: warning: global constructor %s used\n", name);
b9395be3
ILT
793
794 if (! config.build_constructors)
795 return true;
796
2a9fa50c
ILT
797 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
798 useful error message. */
4b7d2399
ILT
799 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL
800 && (link_info.relocateable
801 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
7d6439d9 802 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n");
2a9fa50c 803
b9395be3
ILT
804 s = set_name;
805 if (bfd_get_symbol_leading_char (abfd) != '\0')
806 *s++ = bfd_get_symbol_leading_char (abfd);
807 if (constructor)
808 strcpy (s, "__CTOR_LIST__");
29f33467 809 else
b9395be3 810 strcpy (s, "__DTOR_LIST__");
2fa0b342 811
2a9fa50c
ILT
812 if (config.map_file != (FILE *) NULL)
813 fprintf (config.map_file,
814 "Adding %s to constructor/destructor set %s\n", name, set_name);
e47bfa63 815
b9395be3
ILT
816 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
817 if (h == (struct bfd_link_hash_entry *) NULL)
7d6439d9 818 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
b9395be3 819 if (h->type == bfd_link_hash_new)
e47bfa63 820 {
b9395be3
ILT
821 h->type = bfd_link_hash_undefined;
822 h->u.undef.abfd = abfd;
823 /* We don't call bfd_link_add_undef to add this to the list of
824 undefined symbols because we are going to define it
825 ourselves. */
2fa0b342 826 }
2fa0b342 827
4b7d2399 828 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
b9395be3 829 return true;
2fa0b342
DHW
830}
831
7d6439d9
ILT
832/* A structure used by warning_callback to pass information through
833 bfd_map_over_sections. */
834
835struct warning_callback_info
836{
837 boolean found;
838 const char *warning;
839 const char *symbol;
840 asymbol **asymbols;
841};
842
b9395be3 843/* This is called when there is a reference to a warning symbol. */
2fa0b342 844
b9395be3
ILT
845/*ARGSUSED*/
846static boolean
7d6439d9 847warning_callback (info, warning, symbol, abfd, section, address)
b9395be3
ILT
848 struct bfd_link_info *info;
849 const char *warning;
7d6439d9
ILT
850 const char *symbol;
851 bfd *abfd;
852 asection *section;
853 bfd_vma address;
2fa0b342 854{
4b7d2399
ILT
855 /* This is a hack to support warn_multiple_gp. FIXME: This should
856 have a cleaner interface, but what? */
857 if (! config.warn_multiple_gp
858 && strcmp (warning, "using multiple gp values") == 0)
859 return true;
860
7d6439d9
ILT
861 if (section != NULL)
862 einfo ("%C: %s\n", abfd, section, address, warning);
863 else if (abfd == NULL)
864 einfo ("%P: %s\n", warning);
865 else if (symbol == NULL)
866 einfo ("%B: %s\n", abfd, warning);
867 else
868 {
869 lang_input_statement_type *entry;
870 asymbol **asymbols;
871 struct warning_callback_info info;
872
873 /* Look through the relocs to see if we can find a plausible
874 address. */
875
876 entry = (lang_input_statement_type *) abfd->usrdata;
877 if (entry != NULL && entry->asymbols != NULL)
878 asymbols = entry->asymbols;
879 else
880 {
881 long symsize;
882 long symbol_count;
883
884 symsize = bfd_get_symtab_upper_bound (abfd);
885 if (symsize < 0)
886 einfo ("%B%F: could not read symbols: %E\n", abfd);
887 asymbols = (asymbol **) xmalloc (symsize);
888 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
889 if (symbol_count < 0)
890 einfo ("%B%F: could not read symbols: %E\n", abfd);
891 if (entry != NULL)
892 {
893 entry->asymbols = asymbols;
894 entry->symbol_count = symbol_count;
895 }
896 }
897
898 info.found = false;
899 info.warning = warning;
900 info.symbol = symbol;
901 info.asymbols = asymbols;
902 bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info);
903
904 if (! info.found)
905 einfo ("%B: %s\n", abfd, warning);
906 }
907
b9395be3 908 return true;
2fa0b342
DHW
909}
910
7d6439d9
ILT
911/* This is called by warning_callback for each section. It checks the
912 relocs of the section to see if it can find a reference to the
913 symbol which triggered the warning. If it can, it uses the reloc
914 to give an error message with a file and line number. */
915
916static void
917warning_find_reloc (abfd, sec, iarg)
918 bfd *abfd;
919 asection *sec;
920 PTR iarg;
921{
922 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
923 long relsize;
924 arelent **relpp;
925 long relcount;
926 arelent **p, **pend;
927
928 if (info->found)
929 return;
930
931 relsize = bfd_get_reloc_upper_bound (abfd, sec);
932 if (relsize < 0)
933 einfo ("%B%F: could not read relocs: %E\n", abfd);
934 if (relsize == 0)
935 return;
936
937 relpp = (arelent **) xmalloc (relsize);
938 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
939 if (relcount < 0)
940 einfo ("%B%F: could not read relocs: %E\n", abfd);
941
942 p = relpp;
943 pend = p + relcount;
944 for (; p < pend && *p != NULL; p++)
945 {
946 arelent *q = *p;
947
948 if (q->sym_ptr_ptr != NULL
949 && *q->sym_ptr_ptr != NULL
950 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
951 {
952 /* We found a reloc for the symbol we are looking for. */
953 einfo ("%C: %s\n", abfd, sec, q->address, info->warning);
954 info->found = true;
955 break;
956 }
957 }
958
959 free (relpp);
960}
961
b9395be3 962/* This is called when an undefined symbol is found. */
2fa0b342 963
b9395be3
ILT
964/*ARGSUSED*/
965static boolean
966undefined_symbol (info, name, abfd, section, address)
967 struct bfd_link_info *info;
968 const char *name;
969 bfd *abfd;
970 asection *section;
971 bfd_vma address;
972{
973 static char *error_name;
974 static unsigned int error_count;
29f33467 975
b9395be3 976#define MAX_ERRORS_IN_A_ROW 5
8a045e50 977
809ee7e0
ILT
978 if (config.warn_once)
979 {
980 static struct bfd_hash_table *hash;
981
982 /* Only warn once about a particular undefined symbol. */
983
984 if (hash == NULL)
985 {
986 hash = ((struct bfd_hash_table *)
987 xmalloc (sizeof (struct bfd_hash_table)));
988 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
989 einfo ("%F%P: bfd_hash_table_init failed: %E\n");
990 }
991
992 if (bfd_hash_lookup (hash, name, false, false) != NULL)
993 return true;
994
995 if (bfd_hash_lookup (hash, name, true, true) == NULL)
996 einfo ("%F%P: bfd_hash_lookup failed: %E\n");
997 }
998
b9395be3
ILT
999 /* We never print more than a reasonable number of errors in a row
1000 for a single symbol. */
1001 if (error_name != (char *) NULL
1002 && strcmp (name, error_name) == 0)
1003 ++error_count;
1004 else
e47bfa63 1005 {
b9395be3
ILT
1006 error_count = 0;
1007 if (error_name != (char *) NULL)
1008 free (error_name);
1009 error_name = buystring (name);
1010 }
2fa0b342 1011
23244cd6
ILT
1012 if (section != NULL)
1013 {
1014 if (error_count < MAX_ERRORS_IN_A_ROW)
1015 einfo ("%X%C: undefined reference to `%T'\n",
1016 abfd, section, address, name);
1017 else if (error_count == MAX_ERRORS_IN_A_ROW)
1018 einfo ("%D: more undefined references to `%T' follow\n",
1019 abfd, section, address, name);
1020 }
1021 else
1022 {
1023 if (error_count < MAX_ERRORS_IN_A_ROW)
1024 einfo ("%X%B: undefined reference to `%T'\n",
1025 abfd, name);
1026 else if (error_count == MAX_ERRORS_IN_A_ROW)
1027 einfo ("%B: more undefined references to `%T' follow\n",
1028 abfd, name);
1029 }
2fa0b342 1030
b9395be3 1031 return true;
2fa0b342
DHW
1032}
1033
b9395be3 1034/* This is called when a reloc overflows. */
2fa0b342 1035
b9395be3 1036/*ARGSUSED*/
9f629407 1037static boolean
5dad4c97 1038reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
b9395be3 1039 struct bfd_link_info *info;
5dad4c97
ILT
1040 const char *name;
1041 const char *reloc_name;
1042 bfd_vma addend;
b9395be3
ILT
1043 bfd *abfd;
1044 asection *section;
1045 bfd_vma address;
2fa0b342 1046{
2a9fa50c
ILT
1047 if (abfd == (bfd *) NULL)
1048 einfo ("%P%X: generated");
1049 else
1050 einfo ("%X%C:", abfd, section, address);
1051 einfo (" relocation truncated to fit: %s %T", reloc_name, name);
5dad4c97
ILT
1052 if (addend != 0)
1053 einfo ("+%v", addend);
1054 einfo ("\n");
b9395be3
ILT
1055 return true;
1056}
2fa0b342 1057
b9395be3 1058/* This is called when a dangerous relocation is made. */
3e4c643d 1059
b9395be3
ILT
1060/*ARGSUSED*/
1061static boolean
1062reloc_dangerous (info, message, abfd, section, address)
1063 struct bfd_link_info *info;
1064 const char *message;
1065 bfd *abfd;
1066 asection *section;
1067 bfd_vma address;
1068{
2a9fa50c
ILT
1069 if (abfd == (bfd *) NULL)
1070 einfo ("%P%X: generated");
1071 else
1072 einfo ("%X%C:", abfd, section, address);
1073 einfo ("dangerous relocation: %s\n", message);
b9395be3
ILT
1074 return true;
1075}
973e421e 1076
b9395be3
ILT
1077/* This is called when a reloc is being generated attached to a symbol
1078 that is not being output. */
2fa0b342 1079
b9395be3
ILT
1080/*ARGSUSED*/
1081static boolean
1082unattached_reloc (info, name, abfd, section, address)
1083 struct bfd_link_info *info;
1084 const char *name;
1085 bfd *abfd;
1086 asection *section;
1087 bfd_vma address;
1088{
2a9fa50c
ILT
1089 if (abfd == (bfd *) NULL)
1090 einfo ("%P%X: generated");
1091 else
1092 einfo ("%X%C:", abfd, section, address);
1093 einfo (" reloc refers to symbol `%T' which is not being output\n", name);
b9395be3 1094 return true;
2fa0b342 1095}
8a045e50 1096
b9395be3
ILT
1097/* This is called when a symbol in notice_hash is found. Symbols are
1098 put in notice_hash using the -y option. */
1099
1100/*ARGSUSED*/
1101static boolean
1102notice_ysym (info, name, abfd, section, value)
1103 struct bfd_link_info *info;
1104 const char *name;
1105 bfd *abfd;
1106 asection *section;
1107 bfd_vma value;
8a045e50 1108{
b9395be3 1109 einfo ("%B: %s %s\n", abfd,
cc23cc69 1110 bfd_is_und_section (section) ? "reference to" : "definition of",
b9395be3
ILT
1111 name);
1112 return true;
8a045e50 1113}
This page took 0.376076 seconds and 4 git commands to generate.