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