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