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