ldmain.c revision 78828
165557Sjasone/* Main program of GNU linker. 265557Sjasone Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 365557Sjasone Free Software Foundation, Inc. 465557Sjasone Written by Steve Chamberlain steve@cygnus.com 565557Sjasone 665557SjasoneThis file is part of GLD, the Gnu Linker. 765557Sjasone 865557SjasoneGLD is free software; you can redistribute it and/or modify 965557Sjasoneit under the terms of the GNU General Public License as published by 1065557Sjasonethe Free Software Foundation; either version 2, or (at your option) 1165557Sjasoneany later version. 1265557Sjasone 1365557SjasoneGLD is distributed in the hope that it will be useful, 1465557Sjasonebut WITHOUT ANY WARRANTY; without even the implied warranty of 1565557SjasoneMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1665557SjasoneGNU General Public License for more details. 1765557Sjasone 1865557SjasoneYou should have received a copy of the GNU General Public License 1965557Sjasonealong with GLD; see the file COPYING. If not, write to the Free 2065557SjasoneSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA 2165557Sjasone02111-1307, USA. */ 2265557Sjasone 2365557Sjasone#include "bfd.h" 2465557Sjasone#include "sysdep.h" 2565557Sjasone#include <stdio.h> 2665557Sjasone#include <ctype.h> 2765557Sjasone#include "libiberty.h" 2865557Sjasone#include "progress.h" 2967352Sjhb#include "bfdlink.h" 3065557Sjasone#include "filenames.h" 3165557Sjasone 3265557Sjasone#include "ld.h" 33122514Sjhb#include "ldmain.h" 34122514Sjhb#include "ldmisc.h" 35122514Sjhb#include "ldwrite.h" 36122514Sjhb#include "ldgram.h" 37122514Sjhb#include "ldexp.h" 38122514Sjhb#include "ldlang.h" 39122514Sjhb#include "ldlex.h" 40122514Sjhb#include "ldfile.h" 41122514Sjhb#include "ldemul.h" 42122514Sjhb#include "ldctor.h" 43122514Sjhb 44122514Sjhb/* Somewhere above, sys/stat.h got included . . . . */ 45122514Sjhb#if !defined(S_ISDIR) && defined(S_IFDIR) 46122514Sjhb#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 47122514Sjhb#endif 48122514Sjhb 49169666Sjeff#include <string.h> 50169666Sjeff 51169666Sjeff#ifdef HAVE_SBRK 52169666Sjeff#ifdef NEED_DECLARATION_SBRK 53169666Sjeffextern PTR sbrk (); 54169666Sjeff#endif 55169666Sjeff#endif 56169666Sjeff 5772200Sbmilekicstatic char *get_emulation PARAMS ((int, char **)); 5872200Sbmilekicstatic void set_scripts_dir PARAMS ((void)); 59116182Sobrien 60116182Sobrien/* EXPORTS */ 61116182Sobrien 62154937Sjhbchar *default_target; 63154937Sjhbconst char *output_filename = "a.out"; 64170640Sjeff 65154937Sjhb/* Name this program was invoked by. */ 6665557Sjasonechar *program_name; 6793609Sdes 68234280Smarius/* The file that we're creating. */ 6967352Sjhbbfd *output_bfd = 0; 7093609Sdes 7176166Smarkm/* Set by -G argument, for MIPS ECOFF target. */ 7274912Sjhbint g_switch_value = 8; 7365557Sjasone 74122514Sjhb/* Nonzero means print names of input files as processed. */ 75131259Sjhbboolean trace_files; 76131259Sjhb 77122514Sjhb/* Nonzero means same, but note open failures, too. */ 7865557Sjasoneboolean trace_file_tries; 79169666Sjeff 80169666Sjeff/* Nonzero means version number was printed, so exit successfully 81154937Sjhb instead of complaining if no input files are given. */ 82154937Sjhbboolean version_printed; 83161337Sjhb 84161337Sjhb/* Nonzero means link in every member of an archive. */ 85154937Sjhbboolean whole_archive; 86154937Sjhb 8765557Sjasone/* True if we should demangle symbol names. */ 88122514Sjhbboolean demangling; 89122514Sjhb 90122514Sjhbargs_type command_line; 91122514Sjhb 9271352Sjasoneld_config_type config; 93122514Sjhb 94122514Sjhbstatic void remove_output PARAMS ((void)); 95122514Sjhbstatic boolean check_for_scripts_dir PARAMS ((char *dir)); 96122514Sjhbstatic boolean add_archive_element PARAMS ((struct bfd_link_info *, bfd *, 97122514Sjhb const char *)); 9871352Sjasonestatic boolean multiple_definition PARAMS ((struct bfd_link_info *, 9971352Sjasone const char *, 100122514Sjhb bfd *, asection *, bfd_vma, 101122514Sjhb bfd *, asection *, bfd_vma)); 102122514Sjhbstatic boolean multiple_common PARAMS ((struct bfd_link_info *, 103122514Sjhb const char *, bfd *, 104126317Sjhb enum bfd_link_hash_type, bfd_vma, 105126317Sjhb bfd *, enum bfd_link_hash_type, 106126317Sjhb bfd_vma)); 107122514Sjhbstatic boolean add_to_set PARAMS ((struct bfd_link_info *, 108154937Sjhb struct bfd_link_hash_entry *, 109154937Sjhb bfd_reloc_code_real_type, 110154937Sjhb bfd *, asection *, bfd_vma)); 111126884Sjhbstatic boolean constructor_callback PARAMS ((struct bfd_link_info *, 112122514Sjhb boolean constructor, 113122514Sjhb const char *name, 114122514Sjhb bfd *, asection *, bfd_vma)); 115122514Sjhbstatic boolean warning_callback PARAMS ((struct bfd_link_info *, 116122514Sjhb const char *, const char *, bfd *, 117122514Sjhb asection *, bfd_vma)); 11871352Sjasonestatic void warning_find_reloc PARAMS ((bfd *, asection *, PTR)); 119122514Sjhbstatic boolean undefined_symbol PARAMS ((struct bfd_link_info *, 120170295Sjeff const char *, bfd *, 121154937Sjhb asection *, bfd_vma, boolean)); 122154937Sjhbstatic boolean reloc_overflow PARAMS ((struct bfd_link_info *, const char *, 123122514Sjhb const char *, bfd_vma, 124122514Sjhb bfd *, asection *, bfd_vma)); 125122514Sjhbstatic boolean reloc_dangerous PARAMS ((struct bfd_link_info *, const char *, 126122514Sjhb bfd *, asection *, bfd_vma)); 127122590Sjhbstatic boolean unattached_reloc PARAMS ((struct bfd_link_info *, 12874912Sjhb const char *, bfd *, asection *, 129122514Sjhb bfd_vma)); 130122514Sjhbstatic boolean notice PARAMS ((struct bfd_link_info *, const char *, 131122514Sjhb bfd *, asection *, bfd_vma)); 132122514Sjhb 133131259Sjhbstatic struct bfd_link_callbacks link_callbacks = { 134131259Sjhb add_archive_element, 135131259Sjhb multiple_definition, 136131259Sjhb multiple_common, 13774912Sjhb add_to_set, 13871352Sjasone constructor_callback, 139131259Sjhb warning_callback, 140131259Sjhb undefined_symbol, 141227309Sed reloc_overflow, 142227309Sed reloc_dangerous, 143227309Sed unattached_reloc, 144131259Sjhb notice 145131259Sjhb}; 146131259Sjhb 147131259Sjhbstruct bfd_link_info link_info; 148122514Sjhb 149122514Sjhbstatic void 150169666Sjeffremove_output () 15193702Sjhb{ 15293702Sjhb if (output_filename) 15372200Sbmilekic { 15472200Sbmilekic if (output_bfd && output_bfd->iostream) 155122514Sjhb fclose ((FILE *) (output_bfd->iostream)); 156131263Sjhb if (delete_output_file_on_failure) 157131263Sjhb unlink (output_filename); 158131263Sjhb } 159139453Sjhb} 160139453Sjhb 161139453Sjhbint 162154937Sjhbmain (argc, argv) 163122514Sjhb int argc; 164169666Sjeff char **argv; 165169666Sjeff{ 166169666Sjeff char *emulation; 167169666Sjeff long start_time = get_run_time (); 168170295Sjeff 16967352Sjhb#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) 170122514Sjhb setlocale (LC_MESSAGES, ""); 171122514Sjhb#endif 172122514Sjhb bindtextdomain (PACKAGE, LOCALEDIR); 173122514Sjhb textdomain (PACKAGE); 174122514Sjhb 17567352Sjhb program_name = argv[0]; 17683366Sjulian xmalloc_set_program_name (program_name); 17767352Sjhb 178122514Sjhb START_PROGRESS (program_name, 0); 179122514Sjhb 18067352Sjhb bfd_init (); 181170295Sjeff 182122514Sjhb bfd_set_error_program_name (program_name); 183122514Sjhb 184176078Sjeff xatexit (remove_output); 185170295Sjeff 186170295Sjeff /* Set the default BFD target based on the configured target. Doing 187170295Sjeff this permits the linker to be configured for a particular target, 188170295Sjeff and linked against a shared BFD library which was configured for 189170295Sjeff a different target. The macro TARGET is defined by Makefile. */ 190170295Sjeff if (! bfd_set_default_target (TARGET)) 191170295Sjeff { 19267352Sjhb einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET); 193122514Sjhb xexit (1); 19467352Sjhb } 19583366Sjulian 19667352Sjhb /* Initialize the data about options. */ 197154937Sjhb trace_files = trace_file_tries = version_printed = false; 198154937Sjhb whole_archive = false; 19967352Sjhb config.build_constructors = true; 200170295Sjeff config.dynamic_link = false; 20167352Sjhb config.has_shared = false; 20267352Sjhb config.split_by_reloc = (unsigned) -1; 20372200Sbmilekic config.split_by_file = (bfd_size_type) -1; 204170295Sjeff command_line.force_common_definition = false; 205170295Sjeff command_line.interpreter = NULL; 20699072Sjulian command_line.rpath = NULL; 20783366Sjulian command_line.warn_mismatch = true; 208122514Sjhb command_line.check_section_addresses = true; 209122514Sjhb 210157275Sjhb /* We initialize DEMANGLING based on the environment variable 211157275Sjhb COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the 212157275Sjhb output of the linker, unless COLLECT_NO_DEMANGLE is set in the 213157275Sjhb environment. Acting the same way here lets us provide the same 214157275Sjhb interface by default. */ 215122514Sjhb demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL; 216157275Sjhb 217157275Sjhb link_info.callbacks = &link_callbacks; 218157275Sjhb link_info.relocateable = false; 219157275Sjhb link_info.emitrelocations = false; 220234190Sjhb link_info.shared = false; 221157275Sjhb link_info.symbolic = false; 222157275Sjhb link_info.static_link = false; 223122514Sjhb link_info.traditional_format = false; 224122514Sjhb link_info.optimize = false; 225122514Sjhb link_info.no_undefined = false; 226122514Sjhb link_info.allow_shlib_undefined = false; 227122514Sjhb link_info.strip = strip_none; 228170295Sjeff link_info.discard = discard_none; 229170295Sjeff link_info.keep_memory = true; 23067352Sjhb link_info.input_bfds = NULL; 231170295Sjeff link_info.create_object_symbols_section = NULL; 23269376Sjhb link_info.hash = NULL; 23369376Sjhb link_info.keep_hash = NULL; 234139453Sjhb link_info.notice_all = false; 23567352Sjhb link_info.notice_hash = NULL; 236139453Sjhb link_info.wrap_hash = NULL; 237139453Sjhb link_info.mpc860c0 = 0; 238139453Sjhb /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init 239139453Sjhb and _fini symbols. We are compatible. */ 240139453Sjhb link_info.init_function = "_init"; 241139453Sjhb link_info.fini_function = "_fini"; 242139453Sjhb link_info.new_dtags = false; 243139453Sjhb link_info.flags = (bfd_vma) 0; 244170295Sjeff link_info.flags_1 = (bfd_vma) 0; 24567352Sjhb 24667352Sjhb ldfile_add_arch (""); 24772376Sjake 24873912Sjhb config.make_executable = true; 24967352Sjhb force_make_executable = false; 25083366Sjulian config.magic_demand_paged = true; 25173912Sjhb config.text_read_only = true; 25273912Sjhb 25383366Sjulian emulation = get_emulation (argc, argv); 25473912Sjhb ldemul_choose_mode (emulation); 25573912Sjhb default_target = ldemul_choose_target (); 25673912Sjhb lang_init (); 257122514Sjhb ldemul_before_parse (); 25867352Sjhb lang_has_input_file = false; 259104387Sjhb parse_args (argc, argv); 260139453Sjhb 261173600Sjulian ldemul_set_symbols (); 262122514Sjhb 26367352Sjhb if (link_info.relocateable) 26467352Sjhb { 265122514Sjhb if (command_line.gc_sections) 26667352Sjhb einfo ("%P%F: --gc-sections and -r may not be used together\n"); 267122514Sjhb if (link_info.mpc860c0) 268122514Sjhb einfo (_("%P%F: -r and --mpc860c0 may not be used together\n")); 269176078Sjeff else if (command_line.relax) 270139453Sjhb einfo (_("%P%F: --relax and -r may not be used together\n")); 271139453Sjhb if (link_info.shared) 272170295Sjeff einfo (_("%P%F: -r and -shared may not be used together\n")); 273122590Sjhb } 274122590Sjhb 275170295Sjeff /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols). I 276139453Sjhb don't see how else this can be handled, since in this case we 277139453Sjhb must preserve all externally visible symbols. */ 278122590Sjhb if (link_info.relocateable && link_info.strip == strip_all) 279139453Sjhb { 280139453Sjhb link_info.strip = strip_debugger; 281139453Sjhb if (link_info.discard == discard_none) 282139453Sjhb link_info.discard = discard_all; 283139453Sjhb } 284139453Sjhb 285139453Sjhb /* This essentially adds another -L directory so this must be done after 286139453Sjhb the -L's in argv have been processed. */ 287154937Sjhb set_scripts_dir (); 28872200Sbmilekic 289170295Sjeff if (had_script == false) 290139453Sjhb { 29167352Sjhb /* Read the emulation's appropriate default script. */ 292139453Sjhb int isfile; 293139453Sjhb char *s = ldemul_get_script (&isfile); 294139453Sjhb 295170295Sjeff if (isfile) 296139453Sjhb ldfile_open_command_file (s); 297139453Sjhb else 298139453Sjhb { 299139453Sjhb if (trace_file_tries) 300139453Sjhb { 301139453Sjhb info_msg (_("using internal linker script:\n")); 302139453Sjhb info_msg ("==================================================\n"); 303139453Sjhb info_msg (s); 304139453Sjhb info_msg ("\n==================================================\n"); 305139453Sjhb } 306139453Sjhb lex_string = s; 307139453Sjhb lex_redirect (s); 308139453Sjhb } 309139453Sjhb parser_input = input_script; 310176078Sjeff yyparse (); 311139453Sjhb lex_string = NULL; 312139453Sjhb } 313139453Sjhb 314139453Sjhb lang_final (); 315139453Sjhb 31667352Sjhb if (lang_has_input_file == false) 31783366Sjulian { 318139453Sjhb if (version_printed) 31967352Sjhb xexit (0); 320154937Sjhb einfo (_("%P%F: no input files\n")); 321154937Sjhb } 322122514Sjhb 323154937Sjhb if (trace_files) 324154937Sjhb { 32583366Sjulian info_msg (_("%P: mode %s\n"), emulation); 326139453Sjhb } 32767352Sjhb 32867352Sjhb ldemul_after_parse (); 32972200Sbmilekic 330139453Sjhb if (config.map_filename) 331154937Sjhb { 332139453Sjhb if (strcmp (config.map_filename, "-") == 0) 333139453Sjhb { 334122514Sjhb config.map_file = stdout; 335139453Sjhb } 336139453Sjhb else 337139453Sjhb { 338139453Sjhb config.map_file = fopen (config.map_filename, FOPEN_WT); 339139453Sjhb if (config.map_file == (FILE *) NULL) 340139453Sjhb { 341139453Sjhb bfd_set_error (bfd_error_system_call); 342139453Sjhb einfo (_("%P%F: cannot open map file %s: %E\n"), 343139453Sjhb config.map_filename); 34467352Sjhb } 345139453Sjhb } 34667352Sjhb } 34767352Sjhb 34871352Sjasone lang_process (); 349122514Sjhb 350122514Sjhb /* Print error messages for any missing symbols, for any warning 351122514Sjhb symbols, and possibly multiple definitions. */ 35293609Sdes 353122514Sjhb if (! link_info.relocateable) 354122514Sjhb { 35593667Sdes /* Look for a text section and switch the readonly attribute in it. */ 356122514Sjhb asection *found = bfd_get_section_by_name (output_bfd, ".text"); 35793667Sdes 358122514Sjhb if (found != (asection *) NULL) 359122514Sjhb { 360122514Sjhb if (config.text_read_only) 361122514Sjhb found->flags |= SEC_READONLY; 362131263Sjhb else 363131263Sjhb found->flags &= ~SEC_READONLY; 364154482Sjhb } 365131263Sjhb } 366131263Sjhb 367131263Sjhb if (link_info.relocateable) 368131259Sjhb output_bfd->flags &= ~EXEC_P; 369131263Sjhb else 370131263Sjhb output_bfd->flags |= EXEC_P; 371131263Sjhb 372131263Sjhb ldwrite (); 373131263Sjhb 374131263Sjhb if (config.map_file != NULL) 375131263Sjhb lang_map (); 376131263Sjhb if (command_line.cref) 377131259Sjhb output_cref (config.map_file != NULL ? config.map_file : stdout); 378131259Sjhb if (nocrossref_list != NULL) 379131259Sjhb check_nocrossrefs (); 380131259Sjhb 381131259Sjhb /* Even if we're producing relocateable output, some non-fatal errors should 382131259Sjhb be reported in the exit status. (What non-fatal errors, if any, do we 383131259Sjhb want to ignore for relocateable output?) */ 384131259Sjhb 385131259Sjhb if (config.make_executable == false && force_make_executable == false) 386131259Sjhb { 387122514Sjhb if (trace_files == true) 38893667Sdes { 389131263Sjhb einfo (_("%P: link errors found, deleting executable `%s'\n"), 390131263Sjhb output_filename); 391131263Sjhb } 39293667Sdes 393122514Sjhb /* The file will be removed by remove_output. */ 394122514Sjhb 39593609Sdes xexit (1); 39693609Sdes } 397169666Sjeff else 398182879Sjhb { 399169666Sjeff if (! bfd_close (output_bfd)) 400182879Sjhb einfo (_("%F%B: final close failed: %E\n"), output_bfd); 401169666Sjeff 402182879Sjhb /* If the --force-exe-suffix is enabled, and we're making an 403169666Sjeff executable file and it doesn't end in .exe, copy it to one 404182879Sjhb which does. */ 405122514Sjhb if (! link_info.relocateable && command_line.force_exe_suffix) 40693609Sdes { 407122514Sjhb int len = strlen (output_filename); 40893609Sdes if (len < 4 40993609Sdes || (strcasecmp (output_filename + len - 4, ".exe") != 0 410139453Sjhb && strcasecmp (output_filename + len - 4, ".dll") != 0)) 411139453Sjhb { 412139453Sjhb FILE *src; 413139453Sjhb FILE *dst; 414139453Sjhb const int bsize = 4096; 415139453Sjhb char *buf = xmalloc (bsize); 416139453Sjhb int l; 417139453Sjhb char *dst_name = xmalloc (len + 5); 418139453Sjhb strcpy (dst_name, output_filename); 419139453Sjhb strcat (dst_name, ".exe"); 420139453Sjhb src = fopen (output_filename, FOPEN_RB); 421139453Sjhb dst = fopen (dst_name, FOPEN_WB); 422139453Sjhb 423139453Sjhb if (!src) 424139453Sjhb einfo (_("%X%P: unable to open for source of copy `%s'\n"), output_filename); 425176078Sjeff if (!dst) 426170295Sjeff einfo (_("%X%P: unable to open for destination of copy `%s'\n"), dst_name); 427139453Sjhb while ((l = fread (buf, 1, bsize, src)) > 0) 428139453Sjhb { 429170295Sjeff int done = fwrite (buf, 1, l, dst); 430139453Sjhb if (done != l) 431139453Sjhb { 432139453Sjhb einfo (_("%P: Error writing file `%s'\n"), dst_name); 433139453Sjhb } 434139453Sjhb } 435139453Sjhb fclose (src); 436139453Sjhb if (fclose (dst) == EOF) 437154937Sjhb { 438154937Sjhb einfo (_("%P: Error closing file `%s'\n"), dst_name); 439154937Sjhb } 440154937Sjhb free (dst_name); 441139453Sjhb free (buf); 442170295Sjeff } 443139453Sjhb } 444139453Sjhb } 445139453Sjhb 446122514Sjhb END_PROGRESS (program_name); 44774900Sjhb 448122514Sjhb if (config.stats) 449122514Sjhb { 45074900Sjhb#ifdef HAVE_SBRK 45174900Sjhb char *lim = (char *) sbrk (0); 452122514Sjhb#endif 453154937Sjhb long run_time = get_run_time () - start_time; 454154937Sjhb 455154937Sjhb fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"), 456154937Sjhb program_name, run_time / 1000000, run_time % 1000000); 457154937Sjhb#ifdef HAVE_SBRK 458154937Sjhb fprintf (stderr, _("%s: data size %ld\n"), program_name, 459122514Sjhb (long) (lim - (char *) &environ)); 460122514Sjhb#endif 461122514Sjhb } 46274900Sjhb 46374900Sjhb /* Prevent remove_output from doing anything, after a successful link. */ 464169666Sjeff output_filename = NULL; 465122514Sjhb 466169666Sjeff xexit (0); 467122514Sjhb return 0; 468169666Sjeff} 469169666Sjeff 47074900Sjhb/* We need to find any explicitly given emulation in order to initialize the 471122514Sjhb state that's needed by the lex&yacc argument parser (parse_args). */ 47274900Sjhb 473169666Sjeffstatic char * 474169666Sjeffget_emulation (argc, argv) 475169666Sjeff int argc; 476169666Sjeff char **argv; 477169666Sjeff{ 478169666Sjeff char *emulation; 479169666Sjeff int i; 480169666Sjeff 481169666Sjeff emulation = getenv (EMULATION_ENVIRON); 482169666Sjeff if (emulation == NULL) 483169666Sjeff emulation = DEFAULT_EMULATION; 484169666Sjeff 485169666Sjeff for (i = 1; i < argc; i++) 486169666Sjeff { 487169666Sjeff if (!strncmp (argv[i], "-m", 2)) 488169666Sjeff { 489169666Sjeff if (argv[i][2] == '\0') 490154937Sjhb { 491154937Sjhb /* -m EMUL */ 492122514Sjhb if (i < argc - 1) 493122514Sjhb { 494170295Sjeff emulation = argv[i + 1]; 495169666Sjeff i++; 49674900Sjhb } 49774900Sjhb else 498170295Sjeff { 499170295Sjeff einfo (_("%P%F: missing argument to -m\n")); 500170295Sjeff } 501170295Sjeff } 502170295Sjeff else if (strcmp (argv[i], "-mips1") == 0 503170295Sjeff || strcmp (argv[i], "-mips2") == 0 504170295Sjeff || strcmp (argv[i], "-mips3") == 0 505170295Sjeff || strcmp (argv[i], "-mips4") == 0) 506170295Sjeff { 507122514Sjhb /* FIXME: The arguments -mips1, -mips2 and -mips3 are 508169666Sjeff passed to the linker by some MIPS compilers. They 509169666Sjeff generally tell the linker to use a slightly different 510169666Sjeff library path. Perhaps someday these should be 511169666Sjeff implemented as emulations; until then, we just ignore 512169666Sjeff the arguments and hope that nobody ever creates 513169666Sjeff emulations named ips1, ips2 or ips3. */ 514169666Sjeff } 515169666Sjeff else if (strcmp (argv[i], "-m486") == 0) 516169666Sjeff { 517169666Sjeff /* FIXME: The argument -m486 is passed to the linker on 518122514Sjhb some Linux systems. Hope that nobody creates an 519122514Sjhb emulation named 486. */ 52074900Sjhb } 521122514Sjhb else 52274900Sjhb { 52374900Sjhb /* -mEMUL */ 524169666Sjeff emulation = &argv[i][2]; 52574900Sjhb } 52674900Sjhb } 52774900Sjhb } 528136445Sjhb 529136445Sjhb return emulation; 530136445Sjhb} 531170295Sjeff 532136445Sjhb/* If directory DIR contains an "ldscripts" subdirectory, 533136445Sjhb add DIR to the library search path and return true, 534136445Sjhb else return false. */ 535136445Sjhb 536136445Sjhbstatic boolean 537136445Sjhbcheck_for_scripts_dir (dir) 538136445Sjhb char *dir; 539170295Sjeff{ 540170295Sjeff size_t dirlen; 541170295Sjeff char *buf; 542170295Sjeff struct stat s; 543170295Sjeff boolean res; 544170295Sjeff 545170295Sjeff dirlen = strlen (dir); 546170295Sjeff /* sizeof counts the terminating NUL. */ 547170295Sjeff buf = (char *) xmalloc (dirlen + sizeof ("/ldscripts")); 548170295Sjeff sprintf (buf, "%s/ldscripts", dir); 549170295Sjeff 550170295Sjeff res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode); 551170295Sjeff free (buf); 552170295Sjeff if (res) 553170295Sjeff ldfile_add_library_path (dir, false); 554170295Sjeff return res; 555170295Sjeff} 556170295Sjeff 557170295Sjeff/* Set the default directory for finding script files. 558170295Sjeff Libraries will be searched for here too, but that's ok. 559170295Sjeff We look for the "ldscripts" directory in: 560170295Sjeff 561170295Sjeff SCRIPTDIR (passed from Makefile) 562170295Sjeff the dir where this program is (for using it from the build tree) 563170295Sjeff the dir where this program is/../lib (for installing the tool suite elsewhere) */ 564170295Sjeff 565170295Sjeffstatic void 566170295Sjeffset_scripts_dir () 567170295Sjeff{ 568170295Sjeff char *end, *dir; 569170295Sjeff size_t dirlen; 570170295Sjeff 571170295Sjeff if (check_for_scripts_dir (SCRIPTDIR)) 572170295Sjeff /* We've been installed normally. */ 573170295Sjeff return; 574170295Sjeff 575170295Sjeff /* Look for "ldscripts" in the dir where our binary is. */ 576170295Sjeff end = strrchr (program_name, '/'); 577170295Sjeff#ifdef HAVE_DOS_BASED_FILE_SYSTEM 578136445Sjhb { 579122514Sjhb /* We could have \foo\bar, or /foo\bar. */ 580136445Sjhb char *bslash = strrchr (program_name, '\\'); 581136445Sjhb if (end == NULL || (bslash != NULL && bslash > end)) 58271352Sjasone end = bslash; 583122514Sjhb } 584122514Sjhb#endif 58571352Sjasone 586122514Sjhb if (end == NULL) 587122514Sjhb { 58871352Sjasone /* Don't look for ldscripts in the current directory. There is 589122514Sjhb too much potential for confusion. */ 590136445Sjhb return; 591122514Sjhb } 592170295Sjeff 593170295Sjeff dirlen = end - program_name; 594122514Sjhb /* Make a copy of program_name in dir. 595170295Sjeff Leave room for later "/../lib". */ 596122514Sjhb dir = (char *) xmalloc (dirlen + 8); 59771352Sjasone strncpy (dir, program_name, dirlen); 59871352Sjasone dir[dirlen] = '\0'; 59971352Sjasone 600122514Sjhb if (check_for_scripts_dir (dir)) 60171352Sjasone /* Don't free dir. */ 60272200Sbmilekic return; 603170295Sjeff 60471352Sjasone /* Look for "ldscripts" in <the dir where our binary is>/../lib. */ 605122514Sjhb strcpy (dir + dirlen, "/../lib"); 60671352Sjasone if (check_for_scripts_dir (dir)) 607122514Sjhb return; 608122514Sjhb 60972200Sbmilekic /* Well, we tried. */ 61072200Sbmilekic free (dir); 61172200Sbmilekic} 612154937Sjhb 613154937Sjhbvoid 614154937Sjhbadd_ysym (name) 615154937Sjhb const char *name; 616154937Sjhb{ 617154937Sjhb if (link_info.notice_hash == (struct bfd_hash_table *) NULL) 618154937Sjhb { 619154937Sjhb link_info.notice_hash = ((struct bfd_hash_table *) 620154937Sjhb xmalloc (sizeof (struct bfd_hash_table))); 621154937Sjhb if (! bfd_hash_table_init_n (link_info.notice_hash, 622154937Sjhb bfd_hash_newfunc, 623154937Sjhb 61)) 624154937Sjhb einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 625154937Sjhb } 626154937Sjhb 627154937Sjhb if (bfd_hash_lookup (link_info.notice_hash, name, true, true) 628122514Sjhb == (struct bfd_hash_entry *) NULL) 629122514Sjhb einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); 63072200Sbmilekic} 63172200Sbmilekic 632170295Sjeff/* Record a symbol to be wrapped, from the --wrap option. */ 63372200Sbmilekic 634170295Sjeffvoid 635122514Sjhbadd_wrap (name) 63672200Sbmilekic const char *name; 637170295Sjeff{ 638170295Sjeff if (link_info.wrap_hash == NULL) 63972200Sbmilekic { 640122514Sjhb link_info.wrap_hash = ((struct bfd_hash_table *) 641122514Sjhb xmalloc (sizeof (struct bfd_hash_table))); 642122514Sjhb if (! bfd_hash_table_init_n (link_info.wrap_hash, 643122514Sjhb bfd_hash_newfunc, 64472200Sbmilekic 61)) 645154937Sjhb einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 646122514Sjhb } 647122514Sjhb if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL) 648176078Sjeff einfo (_("%P%F: bfd_hash_lookup failed: %E\n")); 64972200Sbmilekic} 650122514Sjhb 651122514Sjhb/* Handle the -retain-symbols-file option. */ 652122514Sjhb 653170295Sjeffvoid 654122514Sjhbadd_keepsyms_file (filename) 655139453Sjhb const char *filename; 656170295Sjeff{ 657170295Sjeff FILE *file; 658170295Sjeff char *buf; 659170295Sjeff size_t bufsize; 66067352Sjhb int c; 66167352Sjhb 66272200Sbmilekic if (link_info.strip == strip_some) 663136445Sjhb einfo (_("%X%P: error: duplicate retain-symbols-file\n")); 664136445Sjhb 665136445Sjhb file = fopen (filename, "r"); 666136445Sjhb if (file == (FILE *) NULL) 66772200Sbmilekic { 66867352Sjhb bfd_set_error (bfd_error_system_call); 669170295Sjeff einfo ("%X%P: %s: %E\n", filename); 67067352Sjhb return; 671122514Sjhb } 67283366Sjulian 673170295Sjeff link_info.keep_hash = ((struct bfd_hash_table *) 67467352Sjhb xmalloc (sizeof (struct bfd_hash_table))); 67583366Sjulian if (! bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc)) 676170295Sjeff einfo (_("%P%F: bfd_hash_table_init failed: %E\n")); 677154937Sjhb 678154937Sjhb bufsize = 100; 679154937Sjhb buf = (char *) xmalloc (bufsize); 68072200Sbmilekic 681136445Sjhb c = getc (file); 682136445Sjhb while (c != EOF) 683136445Sjhb { 684136445Sjhb while (isspace (c)) 685136445Sjhb c = getc (file); 686170295Sjeff 687218272Sjhb if (c != EOF) 688170295Sjeff { 689131259Sjhb size_t len = 0; 690131259Sjhb 691131259Sjhb while (! isspace (c) && c != EOF) 692131259Sjhb { 693131259Sjhb buf[len] = c; 694131259Sjhb ++len; 695131259Sjhb if (len >= bufsize) 696131259Sjhb { 697122514Sjhb bufsize *= 2; 698122514Sjhb buf = xrealloc (buf, bufsize); 699122514Sjhb } 700154937Sjhb c = getc (file); 701154937Sjhb } 702154937Sjhb 703154937Sjhb buf[len] = '\0'; 704122514Sjhb 705122514Sjhb if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) 706170295Sjeff == (struct bfd_hash_entry *) NULL) 707122514Sjhb einfo (_("%P%F: bfd_hash_lookup for insertion failed: %E\n")); 708154937Sjhb } 709122514Sjhb } 710122514Sjhb 711122514Sjhb if (link_info.strip != strip_none) 712154937Sjhb einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n")); 713122514Sjhb 714122514Sjhb link_info.strip = strip_some; 715122514Sjhb} 716122514Sjhb 717122514Sjhb/* Callbacks from the BFD linker routines. */ 718122514Sjhb 719154937Sjhb/* This is called when BFD has decided to include an archive member in 720154937Sjhb a link. */ 721122514Sjhb 722122514Sjhbstatic boolean 723122514Sjhbadd_archive_element (info, abfd, name) 72472200Sbmilekic struct bfd_link_info *info ATTRIBUTE_UNUSED; 725170295Sjeff bfd *abfd; 726170295Sjeff const char *name; 727122514Sjhb{ 72872200Sbmilekic lang_input_statement_type *input; 729122514Sjhb 730170295Sjeff input = ((lang_input_statement_type *) 731154937Sjhb xmalloc (sizeof (lang_input_statement_type))); 732122514Sjhb input->filename = abfd->filename; 733122514Sjhb input->local_sym_name = abfd->filename; 734201879Sattilio input->the_bfd = abfd; 735122514Sjhb input->asymbols = NULL; 736170295Sjeff input->next = NULL; 737122514Sjhb input->just_syms_flag = false; 73872200Sbmilekic input->loaded = false; 739122514Sjhb input->search_dirs_flag = false; 740139453Sjhb 741139453Sjhb /* FIXME: The following fields are not set: header.next, 74272200Sbmilekic header.type, closed, passive_position, symbol_count, 743176078Sjeff next_real_file, is_archive, target, real. This bit of code is 744178272Sjeff from the old decode_library_subfile function. I don't know 74572200Sbmilekic whether any of those fields matters. */ 746122514Sjhb 747139453Sjhb ldlang_add_file (input); 748139453Sjhb 749170295Sjeff if (config.map_file != (FILE *) NULL) 75067352Sjhb { 75167352Sjhb static boolean header_printed; 75272200Sbmilekic struct bfd_link_hash_entry *h; 753122514Sjhb bfd *from; 754122514Sjhb int len; 75572200Sbmilekic 756122514Sjhb h = bfd_link_hash_lookup (link_info.hash, name, false, false, true); 757154937Sjhb 75871352Sjasone if (h == NULL) 759122514Sjhb from = NULL; 760122514Sjhb else 761122514Sjhb { 76280748Sjhb switch (h->type) 763122514Sjhb { 764170295Sjeff default: 765122514Sjhb from = NULL; 766176017Sjeff break; 767154937Sjhb 76871352Sjasone case bfd_link_hash_defined: 769122514Sjhb case bfd_link_hash_defweak: 770122514Sjhb from = h->u.def.section->owner; 771122514Sjhb break; 772122514Sjhb 773154937Sjhb case bfd_link_hash_undefined: 774122514Sjhb case bfd_link_hash_undefweak: 775122514Sjhb from = h->u.undef.abfd; 776154937Sjhb break; 777122514Sjhb 778122514Sjhb case bfd_link_hash_common: 77967352Sjhb from = h->u.c.p->section->owner; 78082304Sbmilekic break; 781122514Sjhb } 782122514Sjhb } 783122514Sjhb 784105782Sdes if (! header_printed) 785154937Sjhb { 786154937Sjhb char buf[100]; 787131259Sjhb 788170295Sjeff sprintf (buf, "%-29s %s\n\n", _("Archive member included"), 789170295Sjeff _("because of file (symbol)")); 790122514Sjhb minfo ("%s", buf); 791131259Sjhb header_printed = true; 792131259Sjhb } 793131259Sjhb 794131259Sjhb if (bfd_my_archive (abfd) == NULL) 795122514Sjhb { 796123363Sjhb minfo ("%s", bfd_get_filename (abfd)); 797122514Sjhb len = strlen (bfd_get_filename (abfd)); 798122514Sjhb } 799122514Sjhb else 800122514Sjhb { 80167352Sjhb minfo ("%s(%s)", bfd_get_filename (bfd_my_archive (abfd)), 802122514Sjhb bfd_get_filename (abfd)); 80372200Sbmilekic len = (strlen (bfd_get_filename (bfd_my_archive (abfd))) 804122514Sjhb + strlen (bfd_get_filename (abfd)) 805122514Sjhb + 2); 80693672Sarr } 80793672Sarr 808154937Sjhb if (len >= 29) 80993672Sarr { 810122514Sjhb print_nl (); 811122514Sjhb len = 0; 812122514Sjhb } 81393672Sarr while (len < 30) 814122514Sjhb { 815170295Sjeff print_space (); 816122514Sjhb ++len; 817176017Sjeff } 818170295Sjeff 819170295Sjeff if (from != NULL) 820170295Sjeff minfo ("%B ", from); 821170295Sjeff if (h != NULL) 822122514Sjhb minfo ("(%T)\n", h->root.string); 823122514Sjhb else 824154937Sjhb minfo ("(%s)\n", name); 825122514Sjhb } 826122514Sjhb 827122514Sjhb if (trace_files || trace_file_tries) 828122514Sjhb info_msg ("%I\n", input); 829122514Sjhb 830154937Sjhb return true; 831122514Sjhb} 832122514Sjhb 833122514Sjhb/* This is called when BFD has discovered a symbol which is defined 834122514Sjhb multiple times. */ 835154937Sjhb 836122514Sjhbstatic boolean 837122514Sjhbmultiple_definition (info, name, obfd, osec, oval, nbfd, nsec, nval) 838122514Sjhb struct bfd_link_info *info ATTRIBUTE_UNUSED; 839122514Sjhb const char *name; 840122514Sjhb bfd *obfd; 841131259Sjhb asection *osec; 842131259Sjhb bfd_vma oval; 843131259Sjhb bfd *nbfd; 844122514Sjhb asection *nsec; 845122514Sjhb bfd_vma nval; 846123363Sjhb{ 847122514Sjhb /* If either section has the output_section field set to 848122514Sjhb bfd_abs_section_ptr, it means that the section is being 849122514Sjhb discarded, and this is not really a multiple definition at all. 85093672Sarr FIXME: It would be cleaner to somehow ignore symbols defined in 851122590Sjhb sections which are being discarded. */ 85293672Sarr if ((osec->output_section != NULL 853122514Sjhb && ! bfd_is_abs_section (osec) 854122514Sjhb && bfd_is_abs_section (osec->output_section)) 855122514Sjhb || (nsec->output_section != NULL 856105782Sdes && ! bfd_is_abs_section (nsec) 85767352Sjhb && bfd_is_abs_section (nsec->output_section))) 858154937Sjhb return true; 85967352Sjhb 860122514Sjhb einfo (_("%X%C: multiple definition of `%T'\n"), 861170295Sjeff nbfd, nsec, nval, name); 862122514Sjhb if (obfd != (bfd *) NULL) 863139453Sjhb einfo (_("%D: first defined here\n"), obfd, osec, oval); 86472200Sbmilekic 865122514Sjhb if (command_line.relax) 866170295Sjeff { 867176017Sjeff einfo (_("%P: Disabling relaxation: it will not work with multiple definitions\n")); 868122514Sjhb command_line.relax = 0; 86972200Sbmilekic } 870122514Sjhb 871122514Sjhb return true; 872122514Sjhb} 873122514Sjhb 874122514Sjhb/* This is called when there is a definition of a common symbol, or 875122514Sjhb when a common symbol is found for a symbol that is already defined, 876122514Sjhb or when two common symbols are found. We only do something if 877154937Sjhb -warn-common was used. */ 878154937Sjhb 879122514Sjhbstatic boolean 88069429Sjhbmultiple_common (info, name, obfd, otype, osize, nbfd, ntype, nsize) 881122514Sjhb struct bfd_link_info *info ATTRIBUTE_UNUSED; 882170295Sjeff const char *name; 883170295Sjeff bfd *obfd; 884170295Sjeff enum bfd_link_hash_type otype; 885170295Sjeff bfd_vma osize; 886170295Sjeff bfd *nbfd; 887170295Sjeff enum bfd_link_hash_type ntype; 888170295Sjeff bfd_vma nsize; 889170295Sjeff{ 890170295Sjeff if (! config.warn_common) 891122514Sjhb return true; 892122514Sjhb 893122514Sjhb if (ntype == bfd_link_hash_defined 894154937Sjhb || ntype == bfd_link_hash_defweak 895154937Sjhb || ntype == bfd_link_hash_indirect) 896122514Sjhb { 897154937Sjhb ASSERT (otype == bfd_link_hash_common); 898154937Sjhb einfo (_("%B: warning: definition of `%T' overriding common\n"), 899154937Sjhb nbfd, name); 900154937Sjhb if (obfd != NULL) 901170295Sjeff einfo (_("%B: warning: common is here\n"), obfd); 902170295Sjeff } 903122514Sjhb else if (otype == bfd_link_hash_defined 904122514Sjhb || otype == bfd_link_hash_defweak 905122514Sjhb || otype == bfd_link_hash_indirect) 906122514Sjhb { 907139453Sjhb ASSERT (ntype == bfd_link_hash_common); 908170295Sjeff einfo (_("%B: warning: common of `%T' overridden by definition\n"), 909122514Sjhb nbfd, name); 910122514Sjhb if (obfd != NULL) 911122514Sjhb einfo (_("%B: warning: defined here\n"), obfd); 912123364Sjhb } 913123364Sjhb else 914122514Sjhb { 915122514Sjhb ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common); 916122514Sjhb if (osize > nsize) 917122514Sjhb { 918122514Sjhb einfo (_("%B: warning: common of `%T' overridden by larger common\n"), 919170295Sjeff nbfd, name); 920176078Sjeff if (obfd != NULL) 921122514Sjhb einfo (_("%B: warning: larger common is here\n"), obfd); 922170295Sjeff } 923170295Sjeff else if (nsize > osize) 924170295Sjeff { 925170295Sjeff einfo (_("%B: warning: common of `%T' overriding smaller common\n"), 926170295Sjeff nbfd, name); 927201879Sattilio if (obfd != NULL) 928154937Sjhb einfo (_("%B: warning: smaller common is here\n"), obfd); 929170295Sjeff } 930154937Sjhb else 931170295Sjeff { 932170295Sjeff einfo (_("%B: warning: multiple common of `%T'\n"), nbfd, name); 933122514Sjhb if (obfd != NULL) 934170295Sjeff einfo (_("%B: warning: previous common is here\n"), obfd); 93567352Sjhb } 93667352Sjhb } 93772200Sbmilekic 938157844Sjhb return true; 939157844Sjhb} 940157844Sjhb 941157844Sjhb/* This is called when BFD has discovered a set element. H is the 942157844Sjhb entry in the linker hash table for the set. SECTION and VALUE 943157844Sjhb represent a value which should be added to the set. */ 944157844Sjhb 945157844Sjhbstatic boolean 946157844Sjhbadd_to_set (info, h, reloc, abfd, section, value) 947157844Sjhb struct bfd_link_info *info ATTRIBUTE_UNUSED; 948170295Sjeff struct bfd_link_hash_entry *h; 949157844Sjhb bfd_reloc_code_real_type reloc; 950157844Sjhb bfd *abfd; 951157844Sjhb asection *section; 952157844Sjhb bfd_vma value; 953157844Sjhb{ 954157844Sjhb if (config.warn_constructors) 955157844Sjhb einfo (_("%P: warning: global constructor %s used\n"), 956157844Sjhb h->root.string); 957157844Sjhb 958157844Sjhb if (! config.build_constructors) 959157844Sjhb return true; 960157844Sjhb 961157844Sjhb ldctor_add_set_entry (h, reloc, (const char *) NULL, section, value); 962157844Sjhb 963157844Sjhb if (h->type == bfd_link_hash_new) 964157844Sjhb { 965157844Sjhb h->type = bfd_link_hash_undefined; 966157844Sjhb h->u.undef.abfd = abfd; 967157844Sjhb /* We don't call bfd_link_add_undef to add this to the list of 968157844Sjhb undefined symbols because we are going to define it 969157844Sjhb ourselves. */ 970157844Sjhb } 971157844Sjhb 972170295Sjeff return true; 973170295Sjeff} 974157844Sjhb 975157844Sjhb/* This is called when BFD has discovered a constructor. This is only 976157844Sjhb called for some object file formats--those which do not handle 977157844Sjhb constructors in some more clever fashion. This is similar to 978157844Sjhb adding an element to a set, but less general. */ 979157844Sjhb 980157844Sjhbstatic boolean 981157844Sjhbconstructor_callback (info, constructor, name, abfd, section, value) 982170295Sjeff struct bfd_link_info *info; 983157844Sjhb boolean constructor; 984157844Sjhb const char *name; 985157844Sjhb bfd *abfd; 986122514Sjhb asection *section; 98772200Sbmilekic bfd_vma value; 988122514Sjhb{ 989154937Sjhb char *s; 99067352Sjhb struct bfd_link_hash_entry *h; 991122514Sjhb char set_name[1 + sizeof "__CTOR_LIST__"]; 99267352Sjhb 993122514Sjhb if (config.warn_constructors) 994154937Sjhb einfo (_("%P: warning: global constructor %s used\n"), name); 995170295Sjeff 996122514Sjhb if (! config.build_constructors) 997154937Sjhb return true; 99871320Sjasone 999154937Sjhb /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a 1000157844Sjhb useful error message. */ 1001157844Sjhb if (bfd_reloc_type_lookup (output_bfd, BFD_RELOC_CTOR) == NULL 1002157844Sjhb && (link_info.relocateable 1003157844Sjhb || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL)) 1004157844Sjhb einfo (_("%P%F: BFD backend error: BFD_RELOC_CTOR unsupported\n")); 1005157844Sjhb 1006157844Sjhb s = set_name; 1007157844Sjhb if (bfd_get_symbol_leading_char (abfd) != '\0') 1008157844Sjhb *s++ = bfd_get_symbol_leading_char (abfd); 1009157844Sjhb if (constructor) 1010170295Sjeff strcpy (s, "__CTOR_LIST__"); 1011157844Sjhb else 1012157844Sjhb strcpy (s, "__DTOR_LIST__"); 1013157844Sjhb 1014157844Sjhb h = bfd_link_hash_lookup (info->hash, set_name, true, true, true); 1015154937Sjhb if (h == (struct bfd_link_hash_entry *) NULL) 1016154937Sjhb einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); 1017154937Sjhb if (h->type == bfd_link_hash_new) 1018154937Sjhb { 1019154937Sjhb h->type = bfd_link_hash_undefined; 1020154937Sjhb h->u.undef.abfd = abfd; 1021157952Sjhb /* We don't call bfd_link_add_undef to add this to the list of 1022173600Sjulian undefined symbols because we are going to define it 1023154937Sjhb ourselves. */ 1024154937Sjhb } 1025154937Sjhb 1026154937Sjhb ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value); 1027154937Sjhb return true; 1028154937Sjhb} 1029154937Sjhb 1030154937Sjhb/* A structure used by warning_callback to pass information through 1031154937Sjhb bfd_map_over_sections. */ 1032154937Sjhb 1033154937Sjhbstruct warning_callback_info { 1034154937Sjhb boolean found; 1035154937Sjhb const char *warning; 1036154937Sjhb const char *symbol; 1037154937Sjhb asymbol **asymbols; 1038154937Sjhb}; 1039154937Sjhb 1040154937Sjhb/* This is called when there is a reference to a warning symbol. */ 1041154937Sjhb 1042154937Sjhbstatic boolean 1043154937Sjhbwarning_callback (info, warning, symbol, abfd, section, address) 1044154937Sjhb struct bfd_link_info *info ATTRIBUTE_UNUSED; 1045154937Sjhb const char *warning; 1046154937Sjhb const char *symbol; 1047154937Sjhb bfd *abfd; 1048154937Sjhb asection *section; 1049154937Sjhb bfd_vma address; 1050154937Sjhb{ 1051154937Sjhb /* This is a hack to support warn_multiple_gp. FIXME: This should 1052154937Sjhb have a cleaner interface, but what? */ 1053154937Sjhb if (! config.warn_multiple_gp 1054154937Sjhb && strcmp (warning, "using multiple gp values") == 0) 1055154937Sjhb return true; 1056154937Sjhb 1057154937Sjhb if (section != NULL) 1058154937Sjhb einfo ("%C: %s\n", abfd, section, address, warning); 1059154937Sjhb else if (abfd == NULL) 1060154937Sjhb einfo ("%P: %s\n", warning); 1061154937Sjhb else if (symbol == NULL) 1062154937Sjhb einfo ("%B: %s\n", abfd, warning); 1063154937Sjhb else 1064154937Sjhb { 1065154937Sjhb lang_input_statement_type *entry; 1066154937Sjhb asymbol **asymbols; 1067154937Sjhb struct warning_callback_info info; 1068154937Sjhb 1069154937Sjhb /* Look through the relocs to see if we can find a plausible 1070154937Sjhb address. */ 1071154937Sjhb 1072154937Sjhb entry = (lang_input_statement_type *) abfd->usrdata; 1073154937Sjhb if (entry != NULL && entry->asymbols != NULL) 1074154937Sjhb asymbols = entry->asymbols; 1075154937Sjhb else 1076154937Sjhb { 1077154937Sjhb long symsize; 1078154937Sjhb long symbol_count; 1079154937Sjhb 1080154937Sjhb symsize = bfd_get_symtab_upper_bound (abfd); 1081154937Sjhb if (symsize < 0) 1082154937Sjhb einfo (_("%B%F: could not read symbols: %E\n"), abfd); 1083154937Sjhb asymbols = (asymbol **) xmalloc (symsize); 1084154937Sjhb symbol_count = bfd_canonicalize_symtab (abfd, asymbols); 1085154937Sjhb if (symbol_count < 0) 1086158031Sjhb einfo (_("%B%F: could not read symbols: %E\n"), abfd); 1087161324Sjhb if (entry != NULL) 1088161324Sjhb { 1089161324Sjhb entry->asymbols = asymbols; 1090161324Sjhb entry->symbol_count = symbol_count; 1091158031Sjhb } 1092161324Sjhb } 1093158031Sjhb 1094158031Sjhb info.found = false; 1095158031Sjhb info.warning = warning; 1096158031Sjhb info.symbol = symbol; 1097158031Sjhb info.asymbols = asymbols; 1098158031Sjhb bfd_map_over_sections (abfd, warning_find_reloc, (PTR) &info); 1099158031Sjhb 1100158031Sjhb if (! info.found) 1101158031Sjhb einfo ("%B: %s\n", abfd, warning); 1102160313Sjhb 1103158031Sjhb if (entry == NULL) 1104158031Sjhb free (asymbols); 1105173600Sjulian } 1106158031Sjhb 1107158031Sjhb return true; 1108158031Sjhb} 1109158031Sjhb 1110158031Sjhb/* This is called by warning_callback for each section. It checks the 1111158031Sjhb relocs of the section to see if it can find a reference to the 1112158031Sjhb symbol which triggered the warning. If it can, it uses the reloc 1113158031Sjhb to give an error message with a file and line number. */ 1114158031Sjhb 1115158031Sjhbstatic void 1116158031Sjhbwarning_find_reloc (abfd, sec, iarg) 1117158031Sjhb bfd *abfd; 1118158031Sjhb asection *sec; 1119158031Sjhb PTR iarg; 1120158031Sjhb{ 1121158031Sjhb struct warning_callback_info *info = (struct warning_callback_info *) iarg; 1122158031Sjhb long relsize; 1123158031Sjhb arelent **relpp; 1124158031Sjhb long relcount; 1125158031Sjhb arelent **p, **pend; 1126158031Sjhb 1127158031Sjhb if (info->found) 1128158031Sjhb return; 1129158031Sjhb 1130158031Sjhb relsize = bfd_get_reloc_upper_bound (abfd, sec); 1131158031Sjhb if (relsize < 0) 1132158031Sjhb einfo (_("%B%F: could not read relocs: %E\n"), abfd); 1133158031Sjhb if (relsize == 0) 1134158031Sjhb return; 1135158031Sjhb 1136158031Sjhb relpp = (arelent **) xmalloc (relsize); 1137158031Sjhb relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols); 1138158031Sjhb if (relcount < 0) 1139158031Sjhb einfo (_("%B%F: could not read relocs: %E\n"), abfd); 1140161324Sjhb 1141158031Sjhb p = relpp; 1142158031Sjhb pend = p + relcount; 1143158031Sjhb for (; p < pend && *p != NULL; p++) 1144158031Sjhb { 1145158031Sjhb arelent *q = *p; 1146158031Sjhb 1147158031Sjhb if (q->sym_ptr_ptr != NULL 1148158031Sjhb && *q->sym_ptr_ptr != NULL 1149158031Sjhb && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0) 1150161324Sjhb { 1151158031Sjhb /* We found a reloc for the symbol we are looking for. */ 1152158031Sjhb einfo ("%C: %s\n", abfd, sec, q->address, info->warning); 1153183054Ssam info->found = true; 1154158031Sjhb break; 1155158031Sjhb } 1156158031Sjhb } 1157158031Sjhb 1158158031Sjhb free (relpp); 1159158031Sjhb} 1160166073Sdelphij 1161158031Sjhb/* This is called when an undefined symbol is found. */ 1162158031Sjhb 1163158031Sjhbstatic boolean 1164161324Sjhbundefined_symbol (info, name, abfd, section, address, fatal) 1165158031Sjhb struct bfd_link_info *info ATTRIBUTE_UNUSED; 1166160313Sjhb const char *name; 1167160313Sjhb bfd *abfd; 1168158031Sjhb asection *section; 1169158031Sjhb bfd_vma address; 1170158031Sjhb boolean fatal ATTRIBUTE_UNUSED; 1171183054Ssam{ 1172158031Sjhb static char *error_name; 1173161337Sjhb static unsigned int error_count; 1174161337Sjhb 1175161337Sjhb#define MAX_ERRORS_IN_A_ROW 5 1176161337Sjhb 1177161337Sjhb if (config.warn_once) 1178161337Sjhb { 1179161337Sjhb static struct bfd_hash_table *hash; 1180161337Sjhb 1181161337Sjhb /* Only warn once about a particular undefined symbol. */ 1182161337Sjhb 1183161337Sjhb if (hash == NULL) 1184161337Sjhb { 1185161337Sjhb hash = ((struct bfd_hash_table *) 1186161337Sjhb xmalloc (sizeof (struct bfd_hash_table))); 1187161337Sjhb if (! bfd_hash_table_init (hash, bfd_hash_newfunc)) 1188161337Sjhb einfo (_("%F%P: bfd_hash_table_init failed: %E\n")); 1189173600Sjulian } 1190161337Sjhb 1191161337Sjhb if (bfd_hash_lookup (hash, name, false, false) != NULL) 1192161337Sjhb return true; 1193161337Sjhb 1194161337Sjhb if (bfd_hash_lookup (hash, name, true, true) == NULL) 1195161337Sjhb einfo (_("%F%P: bfd_hash_lookup failed: %E\n")); 1196161337Sjhb } 1197161337Sjhb 1198161337Sjhb /* We never print more than a reasonable number of errors in a row 1199161337Sjhb for a single symbol. */ 1200161337Sjhb if (error_name != (char *) NULL 1201161337Sjhb && strcmp (name, error_name) == 0) 1202161337Sjhb ++error_count; 1203161337Sjhb else 1204161337Sjhb { 1205161337Sjhb error_count = 0; 1206161337Sjhb if (error_name != (char *) NULL) 1207161337Sjhb free (error_name); 1208161337Sjhb error_name = xstrdup (name); 1209161337Sjhb } 1210161337Sjhb 1211161337Sjhb if (section != NULL) 1212161337Sjhb { 1213161337Sjhb if (error_count < MAX_ERRORS_IN_A_ROW) 1214161337Sjhb { 1215161337Sjhb einfo (_("%C: undefined reference to `%T'\n"), 1216161337Sjhb abfd, section, address, name); 1217161337Sjhb if (fatal) 1218161337Sjhb einfo ("%X"); 1219161337Sjhb } 1220161337Sjhb else if (error_count == MAX_ERRORS_IN_A_ROW) 1221161337Sjhb einfo (_("%D: more undefined references to `%T' follow\n"), 1222161337Sjhb abfd, section, address, name); 1223161337Sjhb } 1224161337Sjhb else 1225161337Sjhb { 1226161337Sjhb if (error_count < MAX_ERRORS_IN_A_ROW) 1227161337Sjhb { 1228161337Sjhb einfo (_("%B: undefined reference to `%T'\n"), 1229161337Sjhb abfd, name); 1230161337Sjhb if (fatal) 1231161337Sjhb einfo ("%X"); 1232161337Sjhb } 1233161337Sjhb else if (error_count == MAX_ERRORS_IN_A_ROW) 1234161337Sjhb einfo (_("%B: more undefined references to `%T' follow\n"), 1235161337Sjhb abfd, name); 1236161337Sjhb } 1237161337Sjhb 1238158031Sjhb return true; 1239158031Sjhb} 1240158031Sjhb 1241158031Sjhb/* This is called when a reloc overflows. */ 1242158031Sjhb 1243158031Sjhbstatic boolean 1244158031Sjhbreloc_overflow (info, name, reloc_name, addend, abfd, section, address) 1245158031Sjhb struct bfd_link_info *info ATTRIBUTE_UNUSED; 1246160313Sjhb const char *name; 1247160313Sjhb const char *reloc_name; 1248158031Sjhb bfd_vma addend; 1249158031Sjhb bfd *abfd; 1250158031Sjhb asection *section; 1251158031Sjhb bfd_vma address; 1252158031Sjhb{ 1253158031Sjhb if (abfd == (bfd *) NULL) 1254158031Sjhb einfo (_("%P%X: generated")); 1255158031Sjhb else 1256158031Sjhb einfo ("%X%C:", abfd, section, address); 1257158031Sjhb einfo (_(" relocation truncated to fit: %s %T"), reloc_name, name); 1258158031Sjhb if (addend != 0) 1259158031Sjhb einfo ("+%v", addend); 1260158031Sjhb einfo ("\n"); 1261158031Sjhb return true; 1262158031Sjhb} 1263160313Sjhb 1264160313Sjhb/* This is called when a dangerous relocation is made. */ 1265158031Sjhb 1266158031Sjhbstatic boolean 1267158031Sjhbreloc_dangerous (info, message, abfd, section, address) 1268158031Sjhb struct bfd_link_info *info ATTRIBUTE_UNUSED; 1269158031Sjhb const char *message; 1270158031Sjhb bfd *abfd; 1271158031Sjhb asection *section; 1272158031Sjhb bfd_vma address; 1273158031Sjhb{ 1274158031Sjhb if (abfd == (bfd *) NULL) 1275158031Sjhb einfo (_("%P%X: generated")); 1276158031Sjhb else 1277158031Sjhb einfo ("%X%C:", abfd, section, address); 1278161324Sjhb einfo (_("dangerous relocation: %s\n"), message); 1279158031Sjhb return true; 1280158031Sjhb} 1281158031Sjhb 1282158031Sjhb/* This is called when a reloc is being generated attached to a symbol 1283158031Sjhb that is not being output. */ 1284158031Sjhb 1285158031Sjhbstatic boolean 1286158031Sjhbunattached_reloc (info, name, abfd, section, address) 1287158031Sjhb struct bfd_link_info *info ATTRIBUTE_UNUSED; 1288158031Sjhb const char *name; 1289158031Sjhb bfd *abfd; 1290158031Sjhb asection *section; 1291158031Sjhb bfd_vma address; 1292158031Sjhb{ 1293158031Sjhb if (abfd == (bfd *) NULL) 1294158031Sjhb einfo (_("%P%X: generated")); 1295158031Sjhb else 1296158031Sjhb einfo ("%X%C:", abfd, section, address); 1297158031Sjhb einfo (_(" reloc refers to symbol `%T' which is not being output\n"), name); 1298158031Sjhb return true; 1299154937Sjhb} 1300 1301/* This is called if link_info.notice_all is set, or when a symbol in 1302 link_info.notice_hash is found. Symbols are put in notice_hash 1303 using the -y option. */ 1304 1305static boolean 1306notice (info, name, abfd, section, value) 1307 struct bfd_link_info *info; 1308 const char *name; 1309 bfd *abfd; 1310 asection *section; 1311 bfd_vma value; 1312{ 1313 if (! info->notice_all 1314 || (info->notice_hash != NULL 1315 && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL)) 1316 { 1317 if (bfd_is_und_section (section)) 1318 einfo ("%B: reference to %s\n", abfd, name); 1319 else 1320 einfo ("%B: definition of %s\n", abfd, name); 1321 } 1322 1323 if (command_line.cref || nocrossref_list != NULL) 1324 add_cref (name, abfd, section, value); 1325 1326 return true; 1327} 1328