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