Add linux.mh and xm-linux.h.
[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;
b9395be3 172 config.build_constructors = true;
2a9fa50c 173 config.dynamic_link = false;
2fa0b342 174 command_line.force_common_definition = false;
7fb9ca5f 175 command_line.interpreter = NULL;
cc23cc69 176 command_line.rpath = NULL;
2fa0b342 177
b9395be3
ILT
178 link_info.callbacks = &link_callbacks;
179 link_info.relocateable = false;
64887de2 180 link_info.shared = false;
4551e108 181 link_info.symbolic = false;
7d6439d9 182 link_info.static_link = false;
6799c638 183 link_info.traditional_format = 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
0cacbcbe
ILT
321 if (config.map_file != NULL)
322 lang_map ();
323
f3739bc3
SC
324 /* Even if we're producing relocateable output, some non-fatal errors should
325 be reported in the exit status. (What non-fatal errors, if any, do we
326 want to ignore for relocateable output?) */
a72f4e5f 327
f3739bc3
SC
328 if (config.make_executable == false && force_make_executable == false)
329 {
330 if (trace_files == true)
e47bfa63 331 {
973e421e 332 einfo ("%P: link errors found, deleting executable `%s'\n",
f3739bc3 333 output_filename);
e47bfa63 334 }
a72f4e5f 335
f3739bc3
SC
336 if (output_bfd->iostream)
337 fclose ((FILE *) (output_bfd->iostream));
a72f4e5f 338
f3739bc3 339 unlink (output_filename);
0b2f8d2e 340 xexit (1);
f3739bc3
SC
341 }
342 else
343 {
2a9fa50c
ILT
344 if (! bfd_close (output_bfd))
345 einfo ("%F%B: final close failed: %E\n", output_bfd);
f3739bc3 346 }
2fa0b342 347
a735edad
ILT
348 END_PROGRESS (program_name);
349
b9395be3
ILT
350 if (config.stats)
351 {
352 extern char **environ;
eac6290c 353#ifdef HAVE_SBRK
b9395be3 354 char *lim = (char *) sbrk (0);
de71eb77 355#endif
8594f568 356 long run_time = get_run_time () - start_time;
b9395be3 357
5bcb7f28 358 fprintf (stderr, "%s: total time in link: %ld.%06ld\n",
8594f568 359 program_name, run_time / 1000000, run_time % 1000000);
eac6290c 360#ifdef HAVE_SBRK
b9395be3
ILT
361 fprintf (stderr, "%s: data size %ld\n", program_name,
362 (long) (lim - (char *) &environ));
de71eb77 363#endif
b9395be3
ILT
364 }
365
52a8ebfe
DM
366 /* Prevent remove_output from doing anything, after a successful link. */
367 output_filename = NULL;
368
0b2f8d2e 369 xexit (0);
9f629407 370 return 0;
d723cd17
DM
371}
372
373/* We need to find any explicitly given emulation in order to initialize the
374 state that's needed by the lex&yacc argument parser (parse_args). */
375
376static char *
377get_emulation (argc, argv)
378 int argc;
379 char **argv;
380{
381 char *emulation;
382 int i;
383
d723cd17
DM
384 emulation = (char *) getenv (EMULATION_ENVIRON);
385 if (emulation == NULL)
386 emulation = DEFAULT_EMULATION;
d723cd17
DM
387
388 for (i = 1; i < argc; i++)
389 {
390 if (!strncmp (argv[i], "-m", 2))
391 {
392 if (argv[i][2] == '\0')
393 {
394 /* -m EMUL */
395 if (i < argc - 1)
396 {
397 emulation = argv[i + 1];
398 i++;
399 }
400 else
401 {
973e421e 402 einfo("%P%F: missing argument to -m\n");
d723cd17
DM
403 }
404 }
973e421e
ILT
405 else if (strcmp (argv[i], "-mips1") == 0
406 || strcmp (argv[i], "-mips2") == 0
407 || strcmp (argv[i], "-mips3") == 0)
408 {
409 /* FIXME: The arguments -mips1, -mips2 and -mips3 are
410 passed to the linker by some MIPS compilers. They
411 generally tell the linker to use a slightly different
412 library path. Perhaps someday these should be
413 implemented as emulations; until then, we just ignore
414 the arguments and hope that nobody ever creates
415 emulations named ips1, ips2 or ips3. */
416 }
cbbf9608
ILT
417 else if (strcmp (argv[i], "-m486") == 0)
418 {
419 /* FIXME: The argument -m486 is passed to the linker on
420 some Linux systems. Hope that nobody creates an
421 emulation named 486. */
422 }
d723cd17
DM
423 else
424 {
425 /* -mEMUL */
426 emulation = &argv[i][2];
427 }
428 }
429 }
430
431 return emulation;
432}
433
434/* If directory DIR contains an "ldscripts" subdirectory,
435 add DIR to the library search path and return true,
436 else return false. */
437
438static boolean
439check_for_scripts_dir (dir)
440 char *dir;
441{
442 size_t dirlen;
443 char *buf;
444 struct stat s;
445 boolean res;
446
447 dirlen = strlen (dir);
448 /* sizeof counts the terminating NUL. */
0b2f8d2e 449 buf = (char *) xmalloc (dirlen + sizeof("/ldscripts"));
d723cd17
DM
450 sprintf (buf, "%s/ldscripts", dir);
451
452 res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
f4208462 453 free (buf);
d723cd17 454 if (res)
0cd82d00 455 ldfile_add_library_path (dir, false);
d723cd17
DM
456 return res;
457}
458
459/* Set the default directory for finding script files.
f4208462
DM
460 Libraries will be searched for here too, but that's ok.
461 We look for the "ldscripts" directory in:
462
f4208462 463 SCRIPTDIR (passed from Makefile)
bbd2521f
DM
464 the dir where this program is (for using it from the build tree)
465 the dir where this program is/../lib (for installing the tool suite elsewhere) */
d723cd17
DM
466
467static void
468set_scripts_dir ()
469{
f4208462
DM
470 char *end, *dir;
471 size_t dirlen;
472
d723cd17 473 if (check_for_scripts_dir (SCRIPTDIR))
f4208462 474 return; /* We've been installed normally. */
d723cd17
DM
475
476 /* Look for "ldscripts" in the dir where our binary is. */
477 end = strrchr (program_name, '/');
7d6439d9
ILT
478
479 if (end == NULL)
bbd2521f 480 {
7d6439d9
ILT
481 /* Don't look for ldscripts in the current directory. There is
482 too much potential for confusion. */
483 return;
bbd2521f 484 }
f4208462 485
7d6439d9
ILT
486 dirlen = end - program_name;
487 /* Make a copy of program_name in dir.
488 Leave room for later "/../lib". */
489 dir = (char *) xmalloc (dirlen + 8);
490 strncpy (dir, program_name, dirlen);
491 dir[dirlen] = '\0';
492
f4208462
DM
493 if (check_for_scripts_dir (dir))
494 return; /* Don't free dir. */
495
496 /* Look for "ldscripts" in <the dir where our binary is>/../lib. */
497 strcpy (dir + dirlen, "/../lib");
498 if (check_for_scripts_dir (dir))
499 return;
500
501 free (dir); /* Well, we tried. */
d723cd17 502}
2fa0b342 503
e47bfa63 504void
b9395be3
ILT
505add_ysym (name)
506 const char *name;
2fa0b342 507{
b9395be3
ILT
508 if (link_info.notice_hash == (struct bfd_hash_table *) NULL)
509 {
510 link_info.notice_hash = ((struct bfd_hash_table *)
0b2f8d2e 511 xmalloc (sizeof (struct bfd_hash_table)));
b9395be3
ILT
512 if (! bfd_hash_table_init_n (link_info.notice_hash,
513 bfd_hash_newfunc,
514 61))
515 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
516 }
517
518 if (bfd_hash_lookup (link_info.notice_hash, name, true, true)
519 == (struct bfd_hash_entry *) NULL)
520 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
2fa0b342 521}
e47bfa63 522
28113e82
ILT
523/* Record a symbol to be wrapped, from the --wrap option. */
524
525void
526add_wrap (name)
527 const char *name;
528{
529 if (link_info.wrap_hash == NULL)
530 {
531 link_info.wrap_hash = ((struct bfd_hash_table *)
532 xmalloc (sizeof (struct bfd_hash_table)));
533 if (! bfd_hash_table_init_n (link_info.wrap_hash,
534 bfd_hash_newfunc,
535 61))
536 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
537 }
538 if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
539 einfo ("%P%F: bfd_hash_lookup failed: %E\n");
540}
541
b9395be3 542/* Handle the -retain-symbols-file option. */
2fa0b342 543
2fa0b342 544void
b9395be3
ILT
545add_keepsyms_file (filename)
546 const char *filename;
2fa0b342 547{
b9395be3
ILT
548 FILE *file;
549 char *buf;
550 size_t bufsize;
551 int c;
2fa0b342 552
b9395be3
ILT
553 if (link_info.strip == strip_some)
554 einfo ("%X%P: error: duplicate retain-symbols-file\n");
3e4c643d 555
b9395be3
ILT
556 file = fopen (filename, "r");
557 if (file == (FILE *) NULL)
e47bfa63 558 {
5bcb7f28 559 bfd_set_error (bfd_error_system_call);
7d6439d9 560 einfo ("%X%P: %s: %E\n", filename);
b9395be3 561 return;
be1627d3 562 }
be1627d3 563
b9395be3 564 link_info.keep_hash = ((struct bfd_hash_table *)
0b2f8d2e 565 xmalloc (sizeof (struct bfd_hash_table)));
b9395be3
ILT
566 if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc))
567 einfo ("%P%F: bfd_hash_table_init failed: %E\n");
2fa0b342 568
b9395be3 569 bufsize = 100;
0b2f8d2e 570 buf = (char *) xmalloc (bufsize);
c611e285 571
b9395be3
ILT
572 c = getc (file);
573 while (c != EOF)
29f33467 574 {
b9395be3
ILT
575 while (isspace (c))
576 c = getc (file);
e47bfa63 577
b9395be3 578 if (c != EOF)
29f33467 579 {
b9395be3 580 size_t len = 0;
8a045e50 581
b9395be3 582 while (! isspace (c) && c != EOF)
29f33467 583 {
b9395be3
ILT
584 buf[len] = c;
585 ++len;
586 if (len >= bufsize)
29f33467 587 {
b9395be3 588 bufsize *= 2;
0b2f8d2e 589 buf = xrealloc (buf, bufsize);
29f33467 590 }
b9395be3 591 c = getc (file);
29f33467 592 }
81016051 593
b9395be3
ILT
594 buf[len] = '\0';
595
596 if (bfd_hash_lookup (link_info.keep_hash, buf, true, true)
597 == (struct bfd_hash_entry *) NULL)
7d6439d9 598 einfo ("%P%F: bfd_hash_lookup for insertion failed: %E\n");
29f33467 599 }
e47bfa63 600 }
2fa0b342 601
b9395be3
ILT
602 if (link_info.strip != strip_none)
603 einfo ("%P: `-retain-symbols-file' overrides `-s' and `-S'\n");
8a045e50 604
b9395be3
ILT
605 link_info.strip = strip_some;
606}
607\f
608/* Callbacks from the BFD linker routines. */
2fa0b342 609
b9395be3
ILT
610/* This is called when BFD has decided to include an archive member in
611 a link. */
2fa0b342 612
b9395be3
ILT
613/*ARGSUSED*/
614static boolean
615add_archive_element (info, abfd, name)
616 struct bfd_link_info *info;
617 bfd *abfd;
618 const char *name;
2fa0b342 619{
b9395be3
ILT
620 lang_input_statement_type *input;
621
622 input = ((lang_input_statement_type *)
7d6439d9 623 xmalloc (sizeof (lang_input_statement_type)));
b9395be3
ILT
624 input->filename = abfd->filename;
625 input->local_sym_name = abfd->filename;
626 input->the_bfd = abfd;
627 input->asymbols = NULL;
b9395be3
ILT
628 input->next = NULL;
629 input->just_syms_flag = false;
630 input->loaded = false;
210c52ac 631 input->search_dirs_flag = false;
b9395be3
ILT
632
633 /* FIXME: The following fields are not set: header.next,
210c52ac
ILT
634 header.type, closed, passive_position, symbol_count,
635 next_real_file, is_archive, target, real, common_section,
636 common_output_section, complained. This bit of code is from the
637 old decode_library_subfile function. I don't know whether any of
638 those fields matters. */
b9395be3
ILT
639
640 ldlang_add_file (input);
641
2a9fa50c 642 if (config.map_file != (FILE *) NULL)
efa6c497
ILT
643 {
644 static boolean header_printed;
645 struct bfd_link_hash_entry *h;
646 bfd *from;
647 int len;
648
649 h = bfd_link_hash_lookup (link_info.hash, name, false, false, true);
650
651 if (h == NULL)
652 from = NULL;
653 else
654 {
655 switch (h->type)
656 {
657 default:
658 from = NULL;
659 break;
660
661 case bfd_link_hash_defined:
662 case bfd_link_hash_defweak:
663 from = h->u.def.section->owner;
664 break;
665
666 case bfd_link_hash_undefined:
667 case bfd_link_hash_undefweak:
668 from = h->u.undef.abfd;
669 break;
670
671 case bfd_link_hash_common:
672 from = h->u.c.p->section->owner;
673 break;
674 }
675 }
676
677 if (! header_printed)
678 {
679 char buf[100];
680
681 sprintf (buf, "%-29s %s\n\n", "Archive member included",
682 "because of file (symbol)");
683 minfo ("%s", buf);
684 header_printed = true;
685 }
686
687 if (bfd_my_archive (abfd) == NULL)
688 {
689 minfo ("%s", bfd_get_filename (abfd));
690 len = strlen (bfd_get_filename (abfd));
691 }
692 else
693 {
694 minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)),
695 bfd_get_filename (abfd));
696 len = (strlen (bfd_get_filename (bfd_my_archive (abfd)))
697 + strlen (bfd_get_filename (abfd))
698 + 2);
699 }
700
701 if (len >= 29)
702 {
703 print_nl ();
704 len = 0;
705 }
706 while (len < 30)
707 {
708 print_space ();
709 ++len;
710 }
711
712 if (from != NULL)
713 minfo ("%B ", from);
714 if (h != NULL)
715 minfo ("(%T)\n", h->root.string);
716 else
717 minfo ("(%s)\n", name);
718 }
b9395be3 719
5dad4c97
ILT
720 if (trace_files || trace_file_tries)
721 info_msg ("%I\n", input);
722
b9395be3
ILT
723 return true;
724}
2fa0b342 725
b9395be3
ILT
726/* This is called when BFD has discovered a symbol which is defined
727 multiple times. */
2fa0b342 728
b9395be3
ILT
729/*ARGSUSED*/
730static boolean
731multiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval)
732 struct bfd_link_info *info;
733 const char *name;
734 bfd *obfd;
735 asection *osec;
736 bfd_vma oval;
737 bfd *nbfd;
738 asection *nsec;
739 bfd_vma nval;
740{
6799c638
ILT
741 /* If either section has the output_section field set to
742 bfd_abs_section_ptr, it means that the section is being
743 discarded, and this is not really a multiple definition at all.
744 FIXME: It would be cleaner to somehow ignore symbols defined in
745 sections which are being discarded. */
746 if ((osec->output_section != NULL
747 && bfd_is_abs_section (osec->output_section))
748 || (nsec->output_section != NULL
749 && bfd_is_abs_section (nsec->output_section)))
750 return true;
751
b9395be3
ILT
752 einfo ("%X%C: multiple definition of `%T'\n",
753 nbfd, nsec, nval, name);
754 if (obfd != (bfd *) NULL)
cc23cc69 755 einfo ("%D: first defined here\n", obfd, osec, oval);
b9395be3 756 return true;
2fa0b342
DHW
757}
758
b9395be3
ILT
759/* This is called when there is a definition of a common symbol, or
760 when a common symbol is found for a symbol that is already defined,
761 or when two common symbols are found. We only do something if
762 -warn-common was used. */
763
764/*ARGSUSED*/
765static boolean
766multiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize)
767 struct bfd_link_info *info;
768 const char *name;
769 bfd *obfd;
770 enum bfd_link_hash_type otype;
771 bfd_vma osize;
772 bfd *nbfd;
773 enum bfd_link_hash_type ntype;
774 bfd_vma nsize;
99fe4553 775{
b9395be3
ILT
776 if (! config.warn_common)
777 return true;
99fe4553 778
8ed88239
ILT
779 if (ntype == bfd_link_hash_defined
780 || ntype == bfd_link_hash_defweak
781 || ntype == bfd_link_hash_indirect)
e47bfa63 782 {
b9395be3
ILT
783 ASSERT (otype == bfd_link_hash_common);
784 einfo ("%B: warning: definition of `%T' overriding common\n",
785 nbfd, name);
8ed88239
ILT
786 if (obfd != NULL)
787 einfo ("%B: warning: common is here\n", obfd);
e47bfa63 788 }
8ed88239
ILT
789 else if (otype == bfd_link_hash_defined
790 || otype == bfd_link_hash_defweak
791 || otype == bfd_link_hash_indirect)
2fa0b342 792 {
b9395be3
ILT
793 ASSERT (ntype == bfd_link_hash_common);
794 einfo ("%B: warning: common of `%T' overridden by definition\n",
795 nbfd, name);
8ed88239
ILT
796 if (obfd != NULL)
797 einfo ("%B: warning: defined here\n", obfd);
b9395be3
ILT
798 }
799 else
800 {
801 ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
802 if (osize > nsize)
2fa0b342 803 {
b9395be3
ILT
804 einfo ("%B: warning: common of `%T' overridden by larger common\n",
805 nbfd, name);
8ed88239
ILT
806 if (obfd != NULL)
807 einfo ("%B: warning: larger common is here\n", obfd);
2fa0b342 808 }
b9395be3 809 else if (nsize > osize)
2fa0b342 810 {
b9395be3
ILT
811 einfo ("%B: warning: common of `%T' overriding smaller common\n",
812 nbfd, name);
8ed88239
ILT
813 if (obfd != NULL)
814 einfo ("%B: warning: smaller common is here\n", obfd);
2fa0b342 815 }
e47bfa63 816 else
2fa0b342 817 {
b9395be3 818 einfo ("%B: warning: multiple common of `%T'\n", nbfd, name);
8ed88239
ILT
819 if (obfd != NULL)
820 einfo ("%B: warning: previous common is here\n", obfd);
2fa0b342
DHW
821 }
822 }
823
b9395be3 824 return true;
2fa0b342
DHW
825}
826
b9395be3
ILT
827/* This is called when BFD has discovered a set element. H is the
828 entry in the linker hash table for the set. SECTION and VALUE
829 represent a value which should be added to the set. */
2fa0b342 830
b9395be3
ILT
831/*ARGSUSED*/
832static boolean
2a9fa50c 833add_to_set (info, h, reloc, abfd, section, value)
b9395be3
ILT
834 struct bfd_link_info *info;
835 struct bfd_link_hash_entry *h;
2a9fa50c 836 bfd_reloc_code_real_type reloc;
b9395be3
ILT
837 bfd *abfd;
838 asection *section;
839 bfd_vma value;
2fa0b342 840{
7d6439d9
ILT
841 if (config.warn_constructors)
842 einfo ("%P: warning: global constructor %s used\n",
843 h->root.string);
844
2a9fa50c
ILT
845 if (! config.build_constructors)
846 return true;
847
4b7d2399 848 ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value);
2a9fa50c
ILT
849
850 if (h->type == bfd_link_hash_new)
851 {
852 h->type = bfd_link_hash_undefined;
853 h->u.undef.abfd = abfd;
854 /* We don't call bfd_link_add_undef to add this to the list of
855 undefined symbols because we are going to define it
856 ourselves. */
857 }
858
b9395be3
ILT
859 return true;
860}
1418c83b 861
b9395be3
ILT
862/* This is called when BFD has discovered a constructor. This is only
863 called for some object file formats--those which do not handle
864 constructors in some more clever fashion. This is similar to
865 adding an element to a set, but less general. */
2fa0b342 866
b9395be3 867static boolean
2a9fa50c 868constructor_callback (info, constructor, name, abfd, section, value)
b9395be3
ILT
869 struct bfd_link_info *info;
870 boolean constructor;
b9395be3
ILT
871 const char *name;
872 bfd *abfd;
873 asection *section;
874 bfd_vma value;
875{
b9395be3
ILT
876 char *s;
877 struct bfd_link_hash_entry *h;
7d6439d9
ILT
878 char set_name[1 + sizeof "__CTOR_LIST__"];
879
880 if (config.warn_constructors)
881 einfo ("%P: warning: global constructor %s used\n", name);
b9395be3
ILT
882
883 if (! config.build_constructors)
884 return true;
885
2a9fa50c
ILT
886 /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
887 useful error message. */
4b7d2399
ILT
888 if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL
889 && (link_info.relocateable
890 || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
7d6439d9 891 einfo ("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n");
2a9fa50c 892
b9395be3
ILT
893 s = set_name;
894 if (bfd_get_symbol_leading_char (abfd) != '\0')
895 *s++ = bfd_get_symbol_leading_char (abfd);
896 if (constructor)
897 strcpy (s, "__CTOR_LIST__");
29f33467 898 else
b9395be3 899 strcpy (s, "__DTOR_LIST__");
2fa0b342 900
b9395be3
ILT
901 h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
902 if (h == (struct bfd_link_hash_entry *) NULL)
7d6439d9 903 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
b9395be3 904 if (h->type == bfd_link_hash_new)
e47bfa63 905 {
b9395be3
ILT
906 h->type = bfd_link_hash_undefined;
907 h->u.undef.abfd = abfd;
908 /* We don't call bfd_link_add_undef to add this to the list of
909 undefined symbols because we are going to define it
910 ourselves. */
2fa0b342 911 }
2fa0b342 912
4b7d2399 913 ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
b9395be3 914 return true;
2fa0b342
DHW
915}
916
7d6439d9
ILT
917/* A structure used by warning_callback to pass information through
918 bfd_map_over_sections. */
919
920struct warning_callback_info
921{
922 boolean found;
923 const char *warning;
924 const char *symbol;
925 asymbol **asymbols;
926};
927
b9395be3 928/* This is called when there is a reference to a warning symbol. */
2fa0b342 929
b9395be3
ILT
930/*ARGSUSED*/
931static boolean
7d6439d9 932warning_callback (info, warning, symbol, abfd, section, address)
b9395be3
ILT
933 struct bfd_link_info *info;
934 const char *warning;
7d6439d9
ILT
935 const char *symbol;
936 bfd *abfd;
937 asection *section;
938 bfd_vma address;
2fa0b342 939{
4b7d2399
ILT
940 /* This is a hack to support warn_multiple_gp. FIXME: This should
941 have a cleaner interface, but what? */
942 if (! config.warn_multiple_gp
943 && strcmp (warning, "using multiple gp values") == 0)
944 return true;
945
7d6439d9
ILT
946 if (section != NULL)
947 einfo ("%C: %s\n", abfd, section, address, warning);
948 else if (abfd == NULL)
949 einfo ("%P: %s\n", warning);
950 else if (symbol == NULL)
951 einfo ("%B: %s\n", abfd, warning);
952 else
953 {
954 lang_input_statement_type *entry;
955 asymbol **asymbols;
956 struct warning_callback_info info;
957
958 /* Look through the relocs to see if we can find a plausible
959 address. */
960
961 entry = (lang_input_statement_type *) abfd->usrdata;
962 if (entry != NULL && entry->asymbols != NULL)
963 asymbols = entry->asymbols;
964 else
965 {
966 long symsize;
967 long symbol_count;
968
969 symsize = bfd_get_symtab_upper_bound (abfd);
970 if (symsize < 0)
971 einfo ("%B%F: could not read symbols: %E\n", abfd);
972 asymbols = (asymbol **) xmalloc (symsize);
973 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
974 if (symbol_count < 0)
975 einfo ("%B%F: could not read symbols: %E\n", abfd);
976 if (entry != NULL)
977 {
978 entry->asymbols = asymbols;
979 entry->symbol_count = symbol_count;
980 }
981 }
982
983 info.found = false;
984 info.warning = warning;
985 info.symbol = symbol;
986 info.asymbols = asymbols;
987 bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info);
988
989 if (! info.found)
990 einfo ("%B: %s\n", abfd, warning);
991 }
992
b9395be3 993 return true;
2fa0b342
DHW
994}
995
7d6439d9
ILT
996/* This is called by warning_callback for each section. It checks the
997 relocs of the section to see if it can find a reference to the
998 symbol which triggered the warning. If it can, it uses the reloc
999 to give an error message with a file and line number. */
1000
1001static void
1002warning_find_reloc (abfd, sec, iarg)
1003 bfd *abfd;
1004 asection *sec;
1005 PTR iarg;
1006{
1007 struct warning_callback_info *info = (struct warning_callback_info *) iarg;
1008 long relsize;
1009 arelent **relpp;
1010 long relcount;
1011 arelent **p, **pend;
1012
1013 if (info->found)
1014 return;
1015
1016 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1017 if (relsize < 0)
1018 einfo ("%B%F: could not read relocs: %E\n", abfd);
1019 if (relsize == 0)
1020 return;
1021
1022 relpp = (arelent **) xmalloc (relsize);
1023 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
1024 if (relcount < 0)
1025 einfo ("%B%F: could not read relocs: %E\n", abfd);
1026
1027 p = relpp;
1028 pend = p + relcount;
1029 for (; p < pend && *p != NULL; p++)
1030 {
1031 arelent *q = *p;
1032
1033 if (q->sym_ptr_ptr != NULL
1034 && *q->sym_ptr_ptr != NULL
1035 && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
1036 {
1037 /* We found a reloc for the symbol we are looking for. */
1038 einfo ("%C: %s\n", abfd, sec, q->address, info->warning);
1039 info->found = true;
1040 break;
1041 }
1042 }
1043
1044 free (relpp);
1045}
1046
b9395be3 1047/* This is called when an undefined symbol is found. */
2fa0b342 1048
b9395be3
ILT
1049/*ARGSUSED*/
1050static boolean
1051undefined_symbol (info, name, abfd, section, address)
1052 struct bfd_link_info *info;
1053 const char *name;
1054 bfd *abfd;
1055 asection *section;
1056 bfd_vma address;
1057{
1058 static char *error_name;
1059 static unsigned int error_count;
29f33467 1060
b9395be3 1061#define MAX_ERRORS_IN_A_ROW 5
8a045e50 1062
809ee7e0
ILT
1063 if (config.warn_once)
1064 {
1065 static struct bfd_hash_table *hash;
1066
1067 /* Only warn once about a particular undefined symbol. */
1068
1069 if (hash == NULL)
1070 {
1071 hash = ((struct bfd_hash_table *)
1072 xmalloc (sizeof (struct bfd_hash_table)));
1073 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
1074 einfo ("%F%P: bfd_hash_table_init failed: %E\n");
1075 }
1076
1077 if (bfd_hash_lookup (hash, name, false, false) != NULL)
1078 return true;
1079
1080 if (bfd_hash_lookup (hash, name, true, true) == NULL)
1081 einfo ("%F%P: bfd_hash_lookup failed: %E\n");
1082 }
1083
b9395be3
ILT
1084 /* We never print more than a reasonable number of errors in a row
1085 for a single symbol. */
1086 if (error_name != (char *) NULL
1087 && strcmp (name, error_name) == 0)
1088 ++error_count;
1089 else
e47bfa63 1090 {
b9395be3
ILT
1091 error_count = 0;
1092 if (error_name != (char *) NULL)
1093 free (error_name);
1094 error_name = buystring (name);
1095 }
2fa0b342 1096
23244cd6
ILT
1097 if (section != NULL)
1098 {
1099 if (error_count < MAX_ERRORS_IN_A_ROW)
1100 einfo ("%X%C: undefined reference to `%T'\n",
1101 abfd, section, address, name);
1102 else if (error_count == MAX_ERRORS_IN_A_ROW)
1103 einfo ("%D: more undefined references to `%T' follow\n",
1104 abfd, section, address, name);
1105 }
1106 else
1107 {
1108 if (error_count < MAX_ERRORS_IN_A_ROW)
1109 einfo ("%X%B: undefined reference to `%T'\n",
1110 abfd, name);
1111 else if (error_count == MAX_ERRORS_IN_A_ROW)
1112 einfo ("%B: more undefined references to `%T' follow\n",
1113 abfd, name);
1114 }
2fa0b342 1115
b9395be3 1116 return true;
2fa0b342
DHW
1117}
1118
b9395be3 1119/* This is called when a reloc overflows. */
2fa0b342 1120
b9395be3 1121/*ARGSUSED*/
9f629407 1122static boolean
5dad4c97 1123reloc_overflow (info, name, reloc_name, addend, abfd, section, address)
b9395be3 1124 struct bfd_link_info *info;
5dad4c97
ILT
1125 const char *name;
1126 const char *reloc_name;
1127 bfd_vma addend;
b9395be3
ILT
1128 bfd *abfd;
1129 asection *section;
1130 bfd_vma address;
2fa0b342 1131{
2a9fa50c
ILT
1132 if (abfd == (bfd *) NULL)
1133 einfo ("%P%X: generated");
1134 else
1135 einfo ("%X%C:", abfd, section, address);
1136 einfo (" relocation truncated to fit: %s %T", reloc_name, name);
5dad4c97
ILT
1137 if (addend != 0)
1138 einfo ("+%v", addend);
1139 einfo ("\n");
b9395be3
ILT
1140 return true;
1141}
2fa0b342 1142
b9395be3 1143/* This is called when a dangerous relocation is made. */
3e4c643d 1144
b9395be3
ILT
1145/*ARGSUSED*/
1146static boolean
1147reloc_dangerous (info, message, abfd, section, address)
1148 struct bfd_link_info *info;
1149 const char *message;
1150 bfd *abfd;
1151 asection *section;
1152 bfd_vma address;
1153{
2a9fa50c
ILT
1154 if (abfd == (bfd *) NULL)
1155 einfo ("%P%X: generated");
1156 else
1157 einfo ("%X%C:", abfd, section, address);
1158 einfo ("dangerous relocation: %s\n", message);
b9395be3
ILT
1159 return true;
1160}
973e421e 1161
b9395be3
ILT
1162/* This is called when a reloc is being generated attached to a symbol
1163 that is not being output. */
2fa0b342 1164
b9395be3
ILT
1165/*ARGSUSED*/
1166static boolean
1167unattached_reloc (info, name, abfd, section, address)
1168 struct bfd_link_info *info;
1169 const char *name;
1170 bfd *abfd;
1171 asection *section;
1172 bfd_vma address;
1173{
2a9fa50c
ILT
1174 if (abfd == (bfd *) NULL)
1175 einfo ("%P%X: generated");
1176 else
1177 einfo ("%X%C:", abfd, section, address);
1178 einfo (" reloc refers to symbol `%T' which is not being output\n", name);
b9395be3 1179 return true;
2fa0b342 1180}
8a045e50 1181
b9395be3
ILT
1182/* This is called when a symbol in notice_hash is found. Symbols are
1183 put in notice_hash using the -y option. */
1184
1185/*ARGSUSED*/
1186static boolean
1187notice_ysym (info, name, abfd, section, value)
1188 struct bfd_link_info *info;
1189 const char *name;
1190 bfd *abfd;
1191 asection *section;
1192 bfd_vma value;
8a045e50 1193{
b9395be3 1194 einfo ("%B: %s %s\n", abfd,
cc23cc69 1195 bfd_is_und_section (section) ? "reference to" : "definition of",
b9395be3
ILT
1196 name);
1197 return true;
8a045e50 1198}
This page took 0.239011 seconds and 4 git commands to generate.