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