Home | History | Annotate | Line # | Download | only in ld
ldmain.c revision 1.10.2.1
      1       1.1     skrll /* Main program of GNU linker.
      2  1.10.2.1  perseant    Copyright (C) 1991-2024 Free Software Foundation, Inc.
      3       1.1     skrll    Written by Steve Chamberlain steve (at) cygnus.com
      4       1.1     skrll 
      5       1.1     skrll    This file is part of the GNU Binutils.
      6       1.1     skrll 
      7       1.1     skrll    This program is free software; you can redistribute it and/or modify
      8       1.1     skrll    it under the terms of the GNU General Public License as published by
      9       1.1     skrll    the Free Software Foundation; either version 3 of the License, or
     10       1.1     skrll    (at your option) any later version.
     11       1.1     skrll 
     12       1.1     skrll    This program is distributed in the hope that it will be useful,
     13       1.1     skrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14       1.1     skrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15       1.1     skrll    GNU General Public License for more details.
     16       1.1     skrll 
     17       1.1     skrll    You should have received a copy of the GNU General Public License
     18       1.1     skrll    along with this program; if not, write to the Free Software
     19       1.1     skrll    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20       1.1     skrll    MA 02110-1301, USA.  */
     21       1.1     skrll 
     22       1.1     skrll #include "sysdep.h"
     23       1.1     skrll #include "bfd.h"
     24       1.1     skrll #include "safe-ctype.h"
     25       1.1     skrll #include "libiberty.h"
     26       1.1     skrll #include "bfdlink.h"
     27       1.9  christos #include "ctf-api.h"
     28       1.1     skrll #include "filenames.h"
     29      1.10  christos #include "elf/common.h"
     30       1.1     skrll 
     31       1.1     skrll #include "ld.h"
     32       1.1     skrll #include "ldmain.h"
     33       1.1     skrll #include "ldmisc.h"
     34       1.1     skrll #include "ldwrite.h"
     35       1.1     skrll #include "ldexp.h"
     36       1.1     skrll #include "ldlang.h"
     37       1.1     skrll #include <ldgram.h>
     38       1.1     skrll #include "ldlex.h"
     39       1.1     skrll #include "ldfile.h"
     40       1.1     skrll #include "ldemul.h"
     41       1.1     skrll #include "ldctor.h"
     42      1.10  christos #if BFD_SUPPORTS_PLUGINS
     43       1.3  christos #include "plugin.h"
     44       1.3  christos #include "plugin-api.h"
     45      1.10  christos #endif /* BFD_SUPPORTS_PLUGINS */
     46       1.1     skrll 
     47       1.1     skrll /* Somewhere above, sys/stat.h got included.  */
     48       1.1     skrll #if !defined(S_ISDIR) && defined(S_IFDIR)
     49       1.1     skrll #define	S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
     50       1.1     skrll #endif
     51       1.1     skrll 
     52       1.1     skrll #include <string.h>
     53       1.1     skrll 
     54       1.1     skrll #ifndef TARGET_SYSTEM_ROOT
     55       1.1     skrll #define TARGET_SYSTEM_ROOT ""
     56       1.1     skrll #endif
     57       1.1     skrll 
     58       1.1     skrll /* EXPORTS */
     59       1.1     skrll 
     60       1.1     skrll FILE *saved_script_handle = NULL;
     61       1.1     skrll FILE *previous_script_handle = NULL;
     62      1.10  christos bool force_make_executable = false;
     63       1.1     skrll 
     64       1.1     skrll char *default_target;
     65       1.1     skrll const char *output_filename = "a.out";
     66       1.1     skrll 
     67       1.1     skrll /* Name this program was invoked by.  */
     68       1.1     skrll char *program_name;
     69       1.1     skrll 
     70       1.1     skrll /* The prefix for system library directories.  */
     71       1.1     skrll const char *ld_sysroot;
     72       1.1     skrll 
     73       1.1     skrll /* The canonical representation of ld_sysroot.  */
     74       1.6  christos char *ld_canon_sysroot;
     75       1.1     skrll int ld_canon_sysroot_len;
     76       1.1     skrll 
     77       1.5  christos /* Set by -G argument, for targets like MIPS ELF.  */
     78       1.1     skrll int g_switch_value = 8;
     79       1.1     skrll 
     80       1.1     skrll /* Nonzero means print names of input files as processed.  */
     81       1.9  christos unsigned int trace_files;
     82       1.1     skrll 
     83       1.4  christos /* Nonzero means report actions taken by the linker, and describe the linker script in use.  */
     84      1.10  christos bool verbose;
     85       1.1     skrll 
     86       1.1     skrll /* Nonzero means version number was printed, so exit successfully
     87       1.1     skrll    instead of complaining if no input files are given.  */
     88      1.10  christos bool version_printed;
     89       1.1     skrll 
     90       1.1     skrll /* TRUE if we should demangle symbol names.  */
     91      1.10  christos bool demangling;
     92       1.1     skrll 
     93       1.1     skrll args_type command_line;
     94       1.1     skrll 
     95       1.1     skrll ld_config_type config;
     96       1.1     skrll 
     97       1.1     skrll sort_type sort_section;
     98       1.1     skrll 
     99       1.1     skrll static const char *get_sysroot
    100       1.1     skrll   (int, char **);
    101       1.1     skrll static char *get_emulation
    102       1.1     skrll   (int, char **);
    103      1.10  christos static bool add_archive_element
    104       1.3  christos   (struct bfd_link_info *, bfd *, const char *, bfd **);
    105       1.6  christos static void multiple_definition
    106       1.3  christos   (struct bfd_link_info *, struct bfd_link_hash_entry *,
    107       1.1     skrll    bfd *, asection *, bfd_vma);
    108       1.6  christos static void multiple_common
    109       1.3  christos   (struct bfd_link_info *, struct bfd_link_hash_entry *,
    110       1.3  christos    bfd *, enum bfd_link_hash_type, bfd_vma);
    111       1.6  christos static void add_to_set
    112       1.1     skrll   (struct bfd_link_info *, struct bfd_link_hash_entry *,
    113       1.1     skrll    bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
    114       1.6  christos static void constructor_callback
    115      1.10  christos   (struct bfd_link_info *, bool, const char *, bfd *,
    116       1.1     skrll    asection *, bfd_vma);
    117       1.6  christos static void warning_callback
    118       1.1     skrll   (struct bfd_link_info *, const char *, const char *, bfd *,
    119       1.1     skrll    asection *, bfd_vma);
    120       1.1     skrll static void warning_find_reloc
    121       1.1     skrll   (bfd *, asection *, void *);
    122       1.6  christos static void undefined_symbol
    123       1.1     skrll   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
    124      1.10  christos    bool);
    125       1.6  christos static void reloc_overflow
    126       1.1     skrll   (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
    127       1.1     skrll    const char *, bfd_vma, bfd *, asection *, bfd_vma);
    128       1.6  christos static void reloc_dangerous
    129       1.1     skrll   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
    130       1.6  christos static void unattached_reloc
    131       1.1     skrll   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
    132      1.10  christos static bool notice
    133       1.3  christos   (struct bfd_link_info *, struct bfd_link_hash_entry *,
    134       1.5  christos    struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword);
    135       1.1     skrll 
    136       1.1     skrll static struct bfd_link_callbacks link_callbacks =
    137       1.1     skrll {
    138       1.1     skrll   add_archive_element,
    139       1.1     skrll   multiple_definition,
    140       1.1     skrll   multiple_common,
    141       1.1     skrll   add_to_set,
    142       1.1     skrll   constructor_callback,
    143       1.1     skrll   warning_callback,
    144       1.1     skrll   undefined_symbol,
    145       1.1     skrll   reloc_overflow,
    146       1.1     skrll   reloc_dangerous,
    147       1.1     skrll   unattached_reloc,
    148       1.1     skrll   notice,
    149       1.1     skrll   einfo,
    150       1.1     skrll   info_msg,
    151       1.1     skrll   minfo,
    152       1.9  christos   ldlang_override_segment_assignment,
    153      1.10  christos   ldlang_ctf_acquire_strings,
    154      1.10  christos   NULL,
    155      1.10  christos   ldlang_ctf_new_dynsym,
    156       1.9  christos   ldlang_write_ctf_late
    157       1.1     skrll };
    158       1.1     skrll 
    159       1.4  christos static bfd_assert_handler_type default_bfd_assert_handler;
    160       1.7  christos static bfd_error_handler_type default_bfd_error_handler;
    161       1.4  christos 
    162       1.1     skrll struct bfd_link_info link_info;
    163       1.1     skrll 
    164      1.10  christos struct dependency_file
    166      1.10  christos {
    167      1.10  christos   struct dependency_file *next;
    168      1.10  christos   char *name;
    169      1.10  christos };
    170      1.10  christos 
    171      1.10  christos static struct dependency_file *dependency_files, *dependency_files_tail;
    172      1.10  christos 
    173      1.10  christos void
    174      1.10  christos track_dependency_files (const char *filename)
    175      1.10  christos {
    176      1.10  christos   struct dependency_file *dep
    177      1.10  christos     = (struct dependency_file *) xmalloc (sizeof (*dep));
    178      1.10  christos   dep->name = xstrdup (filename);
    179      1.10  christos   dep->next = NULL;
    180      1.10  christos   if (dependency_files == NULL)
    181      1.10  christos     dependency_files = dep;
    182      1.10  christos   else
    183      1.10  christos     dependency_files_tail->next = dep;
    184      1.10  christos   dependency_files_tail = dep;
    185      1.10  christos }
    186      1.10  christos 
    187      1.10  christos static void
    188      1.10  christos write_dependency_file (void)
    189      1.10  christos {
    190      1.10  christos   FILE *out;
    191      1.10  christos   struct dependency_file *dep;
    192      1.10  christos 
    193      1.10  christos   out = fopen (config.dependency_file, FOPEN_WT);
    194      1.10  christos   if (out == NULL)
    195      1.10  christos     {
    196      1.10  christos       einfo (_("%F%P: cannot open dependency file %s: %E\n"),
    197      1.10  christos 	     config.dependency_file);
    198      1.10  christos     }
    199      1.10  christos 
    200      1.10  christos   fprintf (out, "%s:", output_filename);
    201      1.10  christos 
    202      1.10  christos   for (dep = dependency_files; dep != NULL; dep = dep->next)
    203      1.10  christos     fprintf (out, " \\\n  %s", dep->name);
    204      1.10  christos 
    205      1.10  christos   fprintf (out, "\n");
    206      1.10  christos   for (dep = dependency_files; dep != NULL; dep = dep->next)
    207      1.10  christos     fprintf (out, "\n%s:\n", dep->name);
    208      1.10  christos 
    209      1.10  christos   fclose (out);
    210      1.10  christos }
    211       1.1     skrll 
    212       1.3  christos static void
    214  1.10.2.1  perseant ld_cleanup (void)
    215  1.10.2.1  perseant {
    216  1.10.2.1  perseant   bfd *ibfd, *inext;
    217  1.10.2.1  perseant   if (link_info.output_bfd)
    218  1.10.2.1  perseant     bfd_close_all_done (link_info.output_bfd);
    219  1.10.2.1  perseant   for (ibfd = link_info.input_bfds; ibfd; ibfd = inext)
    220  1.10.2.1  perseant     {
    221  1.10.2.1  perseant       inext = ibfd->link.next;
    222      1.10  christos       bfd_close_all_done (ibfd);
    223       1.3  christos     }
    224       1.3  christos #if BFD_SUPPORTS_PLUGINS
    225       1.3  christos   plugin_call_cleanup ();
    226       1.3  christos #endif
    227       1.1     skrll   if (output_filename && delete_output_file_on_failure)
    228       1.1     skrll     unlink_if_ordinary (output_filename);
    229       1.7  christos }
    230       1.4  christos 
    231       1.4  christos /* Hook to notice BFD assertions.  */
    232       1.4  christos 
    233       1.4  christos static void
    234       1.4  christos ld_bfd_assert_handler (const char *fmt, const char *bfdver,
    235      1.10  christos 		       const char *file, int line)
    236       1.4  christos {
    237       1.7  christos   config.make_executable = false;
    238       1.7  christos   (*default_bfd_assert_handler) (fmt, bfdver, file, line);
    239       1.7  christos }
    240       1.7  christos 
    241       1.7  christos /* Hook the bfd error/warning handler for --fatal-warnings.  */
    242       1.7  christos 
    243       1.7  christos static void
    244       1.7  christos ld_bfd_error_handler (const char *fmt, va_list ap)
    245      1.10  christos {
    246       1.7  christos   if (config.fatal_warnings)
    247       1.4  christos     config.make_executable = false;
    248       1.4  christos   (*default_bfd_error_handler) (fmt, ap);
    249       1.1     skrll }
    250       1.1     skrll 
    251       1.1     skrll int
    252       1.1     skrll main (int argc, char **argv)
    253       1.1     skrll {
    254       1.1     skrll   char *emulation;
    255      1.10  christos   long start_time = get_run_time ();
    256       1.1     skrll 
    257       1.1     skrll #ifdef HAVE_LC_MESSAGES
    258       1.1     skrll   setlocale (LC_MESSAGES, "");
    259       1.1     skrll #endif
    260       1.1     skrll   setlocale (LC_CTYPE, "");
    261       1.1     skrll   bindtextdomain (PACKAGE, LOCALEDIR);
    262       1.1     skrll   textdomain (PACKAGE);
    263       1.1     skrll 
    264       1.1     skrll   program_name = argv[0];
    265       1.1     skrll   xmalloc_set_program_name (program_name);
    266       1.1     skrll 
    267       1.9  christos   expandargv (&argc, &argv);
    268       1.9  christos 
    269       1.1     skrll   if (bfd_init () != BFD_INIT_MAGIC)
    270       1.1     skrll     einfo (_("%F%P: fatal error: libbfd ABI mismatch\n"));
    271       1.1     skrll 
    272       1.4  christos   bfd_set_error_program_name (program_name);
    273       1.4  christos 
    274       1.4  christos   /* We want to notice and fail on those nasty BFD assertions which are
    275       1.4  christos      likely to signal incorrect output being generated but otherwise may
    276       1.4  christos      leave no trace.  */
    277       1.7  christos   default_bfd_assert_handler = bfd_set_assert_handler (ld_bfd_assert_handler);
    278       1.7  christos 
    279       1.7  christos   /* Also hook the bfd error/warning handler for --fatal-warnings.  */
    280       1.3  christos   default_bfd_error_handler = bfd_set_error_handler (ld_bfd_error_handler);
    281       1.1     skrll 
    282       1.1     skrll   xatexit (ld_cleanup);
    283       1.1     skrll 
    284       1.1     skrll   /* Set up the sysroot directory.  */
    285       1.5  christos   ld_sysroot = get_sysroot (argc, argv);
    286       1.1     skrll   if (*ld_sysroot)
    287       1.8  christos     ld_canon_sysroot = lrealpath (ld_sysroot);
    288       1.8  christos   if (ld_canon_sysroot)
    289       1.8  christos     {
    290       1.8  christos       ld_canon_sysroot_len = strlen (ld_canon_sysroot);
    291       1.8  christos 
    292       1.8  christos       /* is_sysrooted_pathname() relies on no trailing dirsep.  */
    293      1.10  christos       if (ld_canon_sysroot_len > 0
    294       1.8  christos 	  && IS_DIR_SEPARATOR (ld_canon_sysroot [ld_canon_sysroot_len - 1]))
    295       1.1     skrll 	ld_canon_sysroot [--ld_canon_sysroot_len] = '\0';
    296       1.1     skrll     }
    297       1.1     skrll   else
    298       1.1     skrll     ld_canon_sysroot_len = -1;
    299       1.1     skrll 
    300       1.1     skrll   /* Set the default BFD target based on the configured target.  Doing
    301       1.1     skrll      this permits the linker to be configured for a particular target,
    302       1.6  christos      and linked against a shared BFD library which was configured for
    303       1.1     skrll      a different target.  The macro TARGET is defined by Makefile.  */
    304       1.1     skrll   if (!bfd_set_default_target (TARGET))
    305       1.1     skrll     {
    306       1.1     skrll       einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
    307       1.1     skrll       xexit (1);
    308       1.1     skrll     }
    309       1.1     skrll 
    310       1.1     skrll #if YYDEBUG
    311       1.1     skrll   {
    312       1.1     skrll     extern int yydebug;
    313       1.1     skrll     yydebug = 1;
    314       1.1     skrll   }
    315      1.10  christos #endif
    316       1.1     skrll 
    317       1.1     skrll   config.build_constructors = true;
    318       1.1     skrll   config.rpath_separator = ':';
    319      1.10  christos   config.split_by_reloc = (unsigned) -1;
    320      1.10  christos   config.split_by_file = (bfd_size_type) -1;
    321      1.10  christos   config.make_executable = true;
    322      1.10  christos   config.magic_demand_paged = true;
    323       1.5  christos   config.text_read_only = true;
    324       1.1     skrll   config.print_map_discarded = true;
    325      1.10  christos   link_info.disable_target_specific_optimizations = -1;
    326      1.10  christos 
    327       1.3  christos   command_line.warn_mismatch = true;
    328       1.1     skrll   command_line.warn_search_mismatch = true;
    329       1.1     skrll   command_line.check_section_addresses = -1;
    330       1.1     skrll 
    331       1.1     skrll   /* We initialize DEMANGLING based on the environment variable
    332       1.1     skrll      COLLECT_NO_DEMANGLE.  The gcc collect2 program will demangle the
    333       1.1     skrll      output of the linker, unless COLLECT_NO_DEMANGLE is set in the
    334       1.1     skrll      environment.  Acting the same way here lets us provide the same
    335       1.1     skrll      interface by default.  */
    336      1.10  christos   demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
    337      1.10  christos 
    338      1.10  christos   link_info.allow_undefined_version = true;
    339      1.10  christos   link_info.keep_memory = true;
    340      1.10  christos   link_info.max_cache_size = (bfd_size_type) -1;
    341      1.10  christos   link_info.combreloc = true;
    342      1.10  christos   link_info.strip_discarded = true;
    343       1.7  christos   link_info.prohibit_multiple_definition_absolute = false;
    344       1.7  christos   link_info.textrel_check = DEFAULT_LD_TEXTREL_CHECK;
    345       1.1     skrll   link_info.emit_hash = DEFAULT_EMIT_SYSV_HASH;
    346       1.1     skrll   link_info.emit_gnu_hash = DEFAULT_EMIT_GNU_HASH;
    347       1.1     skrll   link_info.callbacks = &link_callbacks;
    348       1.1     skrll   link_info.input_bfds_tail = &link_info.input_bfds;
    349       1.1     skrll   /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
    350       1.1     skrll      and _fini symbols.  We are compatible.  */
    351       1.1     skrll   link_info.init_function = "_init";
    352       1.5  christos   link_info.fini_function = "_fini";
    353       1.6  christos   link_info.relax_pass = 1;
    354      1.10  christos   link_info.extern_protected_data = -1;
    355       1.1     skrll   link_info.dynamic_undefined_weak = -1;
    356       1.1     skrll   link_info.indirect_extern_access = -1;
    357       1.1     skrll   link_info.pei386_auto_import = -1;
    358       1.5  christos   link_info.spare_dynamic_tags = 5;
    359  1.10.2.1  perseant   link_info.path_separator = ':';
    360       1.5  christos #ifdef DEFAULT_FLAG_COMPRESS_DEBUG
    361       1.7  christos   config.compress_debug = DEFAULT_COMPRESSED_DEBUG_ALGORITHM;
    362       1.7  christos #endif
    363       1.7  christos #ifdef DEFAULT_NEW_DTAGS
    364      1.10  christos   link_info.new_dtags = DEFAULT_NEW_DTAGS;
    365      1.10  christos #endif
    366       1.1     skrll   link_info.start_stop_gc = false;
    367       1.1     skrll   link_info.start_stop_visibility = STV_PROTECTED;
    368       1.1     skrll 
    369       1.1     skrll   ldfile_add_arch ("");
    370       1.1     skrll   emulation = get_emulation (argc, argv);
    371       1.1     skrll   ldemul_choose_mode (emulation);
    372       1.5  christos   default_target = ldemul_choose_target (argc, argv);
    373       1.1     skrll   lang_init ();
    374      1.10  christos   ldexp_init ();
    375       1.1     skrll   ldemul_before_parse ();
    376       1.1     skrll   lang_has_input_file = false;
    377       1.1     skrll   parse_args (argc, argv);
    378       1.1     skrll 
    379       1.1     skrll   if (config.hash_table_size != 0)
    380      1.10  christos     bfd_hash_set_default_size (config.hash_table_size);
    381       1.4  christos 
    382       1.4  christos #if BFD_SUPPORTS_PLUGINS
    383      1.10  christos   /* Now all the plugin arguments have been gathered, we can load them.  */
    384       1.4  christos   plugin_load_plugins ();
    385       1.1     skrll #endif /* BFD_SUPPORTS_PLUGINS */
    386       1.1     skrll 
    387       1.1     skrll   ldemul_set_symbols ();
    388       1.1     skrll 
    389       1.1     skrll   /* If we have not already opened and parsed a linker script,
    390       1.1     skrll      try the default script from command line first.  */
    391       1.1     skrll   if (saved_script_handle == NULL
    392       1.9  christos       && command_line.default_script != NULL)
    393       1.1     skrll     {
    394       1.1     skrll       ldfile_open_script_file (command_line.default_script);
    395       1.1     skrll       parser_input = input_script;
    396       1.1     skrll       yyparse ();
    397       1.1     skrll     }
    398       1.1     skrll 
    399       1.1     skrll   /* If we have not already opened and parsed a linker script
    400       1.1     skrll      read the emulation's appropriate default script.  */
    401       1.1     skrll   if (saved_script_handle == NULL)
    402       1.1     skrll     {
    403       1.1     skrll       int isfile;
    404       1.1     skrll       char *s = ldemul_get_script (&isfile);
    405       1.3  christos 
    406       1.1     skrll       if (isfile)
    407       1.1     skrll 	ldfile_open_default_command_file (s);
    408       1.1     skrll       else
    409       1.4  christos 	{
    410       1.1     skrll 	  lex_string = s;
    411       1.1     skrll 	  lex_redirect (s, _("built in linker script"), 1);
    412       1.1     skrll 	}
    413       1.1     skrll       parser_input = input_script;
    414       1.1     skrll       yyparse ();
    415       1.1     skrll       lex_string = NULL;
    416       1.4  christos     }
    417       1.1     skrll 
    418       1.1     skrll   if (verbose)
    419       1.1     skrll     {
    420       1.1     skrll       if (saved_script_handle)
    421       1.1     skrll 	info_msg (_("using external linker script:"));
    422       1.1     skrll       else
    423       1.1     skrll 	info_msg (_("using internal linker script:"));
    424       1.1     skrll       info_msg ("\n==================================================\n");
    425       1.1     skrll 
    426       1.1     skrll       if (saved_script_handle)
    427       1.1     skrll 	{
    428       1.3  christos 	  static const int ld_bufsz = 8193;
    429       1.1     skrll 	  size_t n;
    430       1.1     skrll 	  char *buf = (char *) xmalloc (ld_bufsz);
    431       1.1     skrll 
    432       1.1     skrll 	  rewind (saved_script_handle);
    433       1.1     skrll 	  while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
    434       1.7  christos 	    {
    435       1.1     skrll 	      buf[n] = 0;
    436       1.1     skrll 	      info_msg ("%s", buf);
    437       1.1     skrll 	    }
    438       1.1     skrll 	  rewind (saved_script_handle);
    439       1.1     skrll 	  free (buf);
    440       1.1     skrll 	}
    441       1.1     skrll       else
    442       1.1     skrll 	{
    443       1.1     skrll 	  int isfile;
    444       1.1     skrll 
    445       1.1     skrll 	  info_msg (ldemul_get_script (&isfile));
    446       1.1     skrll 	}
    447       1.1     skrll 
    448       1.1     skrll       info_msg ("\n==================================================\n");
    449       1.7  christos     }
    450       1.7  christos 
    451      1.10  christos   if (command_line.force_group_allocation
    452       1.7  christos       || !bfd_link_relocatable (&link_info))
    453      1.10  christos     link_info.resolve_section_groups = true;
    454       1.7  christos   else
    455       1.4  christos     link_info.resolve_section_groups = false;
    456       1.4  christos 
    457       1.4  christos   if (command_line.print_output_format)
    458       1.1     skrll     info_msg ("%s\n", lang_get_output_target ());
    459       1.1     skrll 
    460       1.5  christos   lang_final ();
    461       1.5  christos 
    462       1.5  christos   /* If the only command line argument has been -v or --version or --verbose
    463       1.5  christos      then ignore any input files provided by linker scripts and exit now.
    464       1.5  christos      We do not want to create an output file when the linker is just invoked
    465       1.5  christos      to provide version information.  */
    466       1.5  christos   if (argc == 2 && version_printed)
    467       1.7  christos     xexit (0);
    468       1.8  christos 
    469       1.7  christos   if (link_info.inhibit_common_definition && !bfd_link_dll (&link_info))
    470       1.1     skrll     einfo (_("%F%P: --no-define-common may not be used without -shared\n"));
    471       1.1     skrll 
    472       1.4  christos   if (!lang_has_input_file)
    473       1.1     skrll     {
    474       1.8  christos       if (version_printed || command_line.print_output_format)
    475       1.1     skrll 	xexit (0);
    476       1.1     skrll       einfo (_("%F%P: no input files\n"));
    477       1.9  christos     }
    478       1.1     skrll 
    479       1.1     skrll   if (verbose)
    480       1.1     skrll     info_msg (_("%P: mode %s\n"), emulation);
    481       1.1     skrll 
    482       1.1     skrll   ldemul_after_parse ();
    483       1.1     skrll 
    484       1.1     skrll   if (config.map_filename)
    485       1.1     skrll     {
    486       1.1     skrll       if (strcmp (config.map_filename, "-") == 0)
    487       1.1     skrll 	{
    488       1.1     skrll 	  config.map_file = stdout;
    489       1.1     skrll 	}
    490       1.1     skrll       else
    491       1.1     skrll 	{
    492       1.1     skrll 	  config.map_file = fopen (config.map_filename, FOPEN_WT);
    493       1.1     skrll 	  if (config.map_file == (FILE *) NULL)
    494       1.8  christos 	    {
    495       1.1     skrll 	      bfd_set_error (bfd_error_system_call);
    496       1.1     skrll 	      einfo (_("%F%P: cannot open map file %s: %E\n"),
    497       1.1     skrll 		     config.map_filename);
    498      1.10  christos 	    }
    499       1.1     skrll 	}
    500       1.1     skrll       link_info.has_map_file = true;
    501       1.1     skrll     }
    502       1.1     skrll 
    503       1.1     skrll   lang_process ();
    504       1.1     skrll 
    505       1.5  christos   /* Print error messages for any missing symbols, for any warning
    506       1.1     skrll      symbols, and possibly multiple definitions.  */
    507       1.1     skrll   if (bfd_link_relocatable (&link_info))
    508       1.1     skrll     link_info.output_bfd->flags &= ~EXEC_P;
    509       1.1     skrll   else
    510  1.10.2.1  perseant     link_info.output_bfd->flags |= EXEC_P;
    511  1.10.2.1  perseant 
    512       1.5  christos   flagword flags = 0;
    513  1.10.2.1  perseant   switch (config.compress_debug)
    514  1.10.2.1  perseant     {
    515  1.10.2.1  perseant     case COMPRESS_DEBUG_GNU_ZLIB:
    516  1.10.2.1  perseant       flags = BFD_COMPRESS;
    517  1.10.2.1  perseant       break;
    518  1.10.2.1  perseant     case COMPRESS_DEBUG_GABI_ZLIB:
    519  1.10.2.1  perseant       flags = BFD_COMPRESS | BFD_COMPRESS_GABI;
    520  1.10.2.1  perseant       break;
    521  1.10.2.1  perseant     case COMPRESS_DEBUG_ZSTD:
    522  1.10.2.1  perseant       flags = BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
    523  1.10.2.1  perseant       break;
    524       1.5  christos     default:
    525  1.10.2.1  perseant       break;
    526  1.10.2.1  perseant     }
    527       1.5  christos   link_info.output_bfd->flags
    528       1.1     skrll     |= flags & bfd_applicable_file_flags (link_info.output_bfd);
    529       1.1     skrll 
    530       1.1     skrll   ldwrite ();
    531       1.1     skrll 
    532       1.1     skrll   if (config.map_file != NULL)
    533       1.1     skrll     lang_map ();
    534       1.1     skrll   if (command_line.cref)
    535       1.1     skrll     output_cref (config.map_file != NULL ? config.map_file : stdout);
    536       1.5  christos   if (nocrossref_list != NULL)
    537       1.5  christos     check_nocrossrefs ();
    538       1.5  christos   if (command_line.print_memory_usage)
    539       1.5  christos     lang_print_memory_usage ();
    540       1.6  christos #if 0
    541       1.1     skrll   {
    542       1.5  christos     struct bfd_link_hash_entry *h;
    543       1.5  christos 
    544       1.5  christos     h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1);
    545       1.5  christos     fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
    546       1.5  christos   }
    547       1.1     skrll #endif
    548       1.1     skrll   ldexp_finish ();
    549      1.10  christos   lang_finish ();
    550      1.10  christos 
    551      1.10  christos   if (config.dependency_file != NULL)
    552       1.1     skrll     write_dependency_file ();
    553       1.1     skrll 
    554       1.1     skrll   /* Even if we're producing relocatable output, some non-fatal errors should
    555       1.1     skrll      be reported in the exit status.  (What non-fatal errors, if any, do we
    556       1.1     skrll      want to ignore for relocatable output?)  */
    557       1.9  christos   if (!config.make_executable && !force_make_executable)
    558       1.1     skrll     {
    559       1.1     skrll       if (verbose)
    560       1.1     skrll 	einfo (_("%P: link errors found, deleting executable `%s'\n"),
    561       1.4  christos 	       output_filename);
    562       1.1     skrll 
    563       1.1     skrll       /* The file will be removed by ld_cleanup.  */
    564       1.1     skrll       xexit (1);
    565       1.1     skrll     }
    566  1.10.2.1  perseant   else
    567  1.10.2.1  perseant     {
    568  1.10.2.1  perseant       bfd *obfd = link_info.output_bfd;
    569  1.10.2.1  perseant       link_info.output_bfd = NULL;
    570       1.1     skrll       if (!bfd_close (obfd))
    571       1.1     skrll 	einfo (_("%F%P: %s: final close failed: %E\n"), output_filename);
    572       1.1     skrll 
    573       1.1     skrll       /* If the --force-exe-suffix is enabled, and we're making an
    574       1.5  christos 	 executable file and it doesn't end in .exe, copy it to one
    575       1.5  christos 	 which does.  */
    576       1.1     skrll       if (!bfd_link_relocatable (&link_info)
    577       1.1     skrll 	  && command_line.force_exe_suffix)
    578       1.1     skrll 	{
    579       1.1     skrll 	  int len = strlen (output_filename);
    580       1.1     skrll 
    581       1.1     skrll 	  if (len < 4
    582       1.1     skrll 	      || (strcasecmp (output_filename + len - 4, ".exe") != 0
    583       1.1     skrll 		  && strcasecmp (output_filename + len - 4, ".dll") != 0))
    584       1.1     skrll 	    {
    585       1.1     skrll 	      FILE *src;
    586       1.3  christos 	      FILE *dst;
    587       1.1     skrll 	      const int bsize = 4096;
    588       1.3  christos 	      char *buf = (char *) xmalloc (bsize);
    589       1.1     skrll 	      int l;
    590       1.1     skrll 	      char *dst_name = (char *) xmalloc (len + 5);
    591       1.1     skrll 
    592       1.1     skrll 	      strcpy (dst_name, output_filename);
    593       1.1     skrll 	      strcat (dst_name, ".exe");
    594       1.1     skrll 	      src = fopen (output_filename, FOPEN_RB);
    595       1.1     skrll 	      dst = fopen (dst_name, FOPEN_WB);
    596       1.8  christos 
    597       1.1     skrll 	      if (!src)
    598       1.1     skrll 		einfo (_("%F%P: unable to open for source of copy `%s'\n"),
    599       1.8  christos 		       output_filename);
    600       1.1     skrll 	      if (!dst)
    601       1.1     skrll 		einfo (_("%F%P: unable to open for destination of copy `%s'\n"),
    602       1.1     skrll 		       dst_name);
    603       1.1     skrll 	      while ((l = fread (buf, 1, bsize, src)) > 0)
    604       1.1     skrll 		{
    605       1.1     skrll 		  int done = fwrite (buf, 1, l, dst);
    606       1.8  christos 
    607       1.1     skrll 		  if (done != l)
    608       1.1     skrll 		    einfo (_("%P: error writing file `%s'\n"), dst_name);
    609       1.1     skrll 		}
    610       1.1     skrll 
    611       1.8  christos 	      fclose (src);
    612       1.1     skrll 	      if (fclose (dst) == EOF)
    613       1.1     skrll 		einfo (_("%P: error closing file `%s'\n"), dst_name);
    614       1.1     skrll 	      free (dst_name);
    615       1.1     skrll 	      free (buf);
    616       1.1     skrll 	    }
    617       1.1     skrll 	}
    618       1.1     skrll     }
    619       1.1     skrll 
    620       1.1     skrll   if (config.stats)
    621       1.1     skrll     {
    622       1.3  christos       long run_time = get_run_time () - start_time;
    623       1.1     skrll 
    624       1.1     skrll       fflush (stdout);
    625       1.3  christos       fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
    626       1.1     skrll 	       program_name, run_time / 1000000, run_time % 1000000);
    627       1.1     skrll       fflush (stderr);
    628  1.10.2.1  perseant     }
    629       1.1     skrll 
    630       1.1     skrll   /* Prevent ld_cleanup from deleting the output file.  */
    631       1.1     skrll   output_filename = NULL;
    632       1.1     skrll 
    633       1.1     skrll   xexit (0);
    634       1.1     skrll   return 0;
    635       1.1     skrll }
    636       1.1     skrll 
    637       1.1     skrll /* If the configured sysroot is relocatable, try relocating it based on
    638       1.1     skrll    default prefix FROM.  Return the relocated directory if it exists,
    639       1.1     skrll    otherwise return null.  */
    640       1.1     skrll 
    641       1.1     skrll static char *
    642       1.1     skrll get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
    643       1.1     skrll {
    644       1.1     skrll #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
    645       1.1     skrll   char *path;
    646       1.1     skrll   struct stat s;
    647       1.1     skrll 
    648       1.1     skrll   path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
    649       1.1     skrll   if (path)
    650       1.1     skrll     {
    651       1.1     skrll       if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
    652       1.1     skrll 	return path;
    653       1.1     skrll       free (path);
    654       1.1     skrll     }
    655       1.1     skrll #endif
    656       1.1     skrll   return 0;
    657       1.1     skrll }
    658       1.1     skrll 
    659       1.1     skrll /* Return the sysroot directory.  Return "" if no sysroot is being used.  */
    660       1.1     skrll 
    661       1.1     skrll static const char *
    662       1.1     skrll get_sysroot (int argc, char **argv)
    663       1.9  christos {
    664       1.1     skrll   int i;
    665       1.1     skrll   const char *path = NULL;
    666      1.10  christos 
    667       1.9  christos   for (i = 1; i < argc; i++)
    668       1.9  christos     if (startswith (argv[i], "--sysroot="))
    669       1.9  christos       path = argv[i] + strlen ("--sysroot=");
    670       1.9  christos 
    671       1.9  christos   if (!path)
    672       1.9  christos     path = get_relative_sysroot (BINDIR);
    673       1.9  christos 
    674       1.1     skrll   if (!path)
    675       1.9  christos     path = get_relative_sysroot (TOOLBINDIR);
    676       1.9  christos 
    677       1.1     skrll   if (!path)
    678       1.9  christos     path = TARGET_SYSTEM_ROOT;
    679       1.9  christos 
    680       1.1     skrll   if (IS_DIR_SEPARATOR (*path) && path[1] == 0)
    681       1.9  christos     path = "";
    682       1.1     skrll 
    683       1.1     skrll   return path;
    684       1.1     skrll }
    685       1.1     skrll 
    686       1.1     skrll /* We need to find any explicitly given emulation in order to initialize the
    687       1.1     skrll    state that's needed by the lex&yacc argument parser (parse_args).  */
    688       1.1     skrll 
    689       1.1     skrll static char *
    690       1.1     skrll get_emulation (int argc, char **argv)
    691       1.1     skrll {
    692       1.1     skrll   char *emulation;
    693       1.1     skrll   int i;
    694       1.1     skrll 
    695       1.1     skrll   emulation = getenv (EMULATION_ENVIRON);
    696       1.1     skrll   if (emulation == NULL)
    697       1.1     skrll     emulation = DEFAULT_EMULATION;
    698       1.1     skrll 
    699      1.10  christos   for (i = 1; i < argc; i++)
    700       1.1     skrll     {
    701       1.1     skrll       if (startswith (argv[i], "-m"))
    702       1.1     skrll 	{
    703       1.1     skrll 	  if (argv[i][2] == '\0')
    704       1.1     skrll 	    {
    705       1.1     skrll 	      /* -m EMUL */
    706       1.1     skrll 	      if (i < argc - 1)
    707       1.1     skrll 		{
    708       1.1     skrll 		  emulation = argv[i + 1];
    709       1.1     skrll 		  i++;
    710       1.8  christos 		}
    711       1.1     skrll 	      else
    712       1.1     skrll 		einfo (_("%F%P: missing argument to -m\n"));
    713       1.1     skrll 	    }
    714       1.1     skrll 	  else if (strcmp (argv[i], "-mips1") == 0
    715       1.1     skrll 		   || strcmp (argv[i], "-mips2") == 0
    716       1.1     skrll 		   || strcmp (argv[i], "-mips3") == 0
    717       1.1     skrll 		   || strcmp (argv[i], "-mips4") == 0
    718       1.1     skrll 		   || strcmp (argv[i], "-mips5") == 0
    719      1.10  christos 		   || strcmp (argv[i], "-mips32") == 0
    720      1.10  christos 		   || strcmp (argv[i], "-mips32r2") == 0
    721       1.5  christos 		   || strcmp (argv[i], "-mips32r3") == 0
    722       1.1     skrll 		   || strcmp (argv[i], "-mips32r5") == 0
    723       1.5  christos 		   || strcmp (argv[i], "-mips32r6") == 0
    724      1.10  christos 		   || strcmp (argv[i], "-mips64") == 0
    725      1.10  christos 		   || strcmp (argv[i], "-mips64r2") == 0
    726       1.5  christos 		   || strcmp (argv[i], "-mips64r3") == 0
    727       1.1     skrll 		   || strcmp (argv[i], "-mips64r5") == 0
    728       1.1     skrll 		   || strcmp (argv[i], "-mips64r6") == 0)
    729       1.1     skrll 	    {
    730       1.1     skrll 	      /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
    731       1.1     skrll 		 passed to the linker by some MIPS compilers.  They
    732       1.1     skrll 		 generally tell the linker to use a slightly different
    733       1.1     skrll 		 library path.  Perhaps someday these should be
    734       1.1     skrll 		 implemented as emulations; until then, we just ignore
    735       1.1     skrll 		 the arguments and hope that nobody ever creates
    736       1.1     skrll 		 emulations named ips1, ips2 or ips3.  */
    737       1.1     skrll 	    }
    738       1.1     skrll 	  else if (strcmp (argv[i], "-m486") == 0)
    739       1.1     skrll 	    {
    740       1.1     skrll 	      /* FIXME: The argument -m486 is passed to the linker on
    741       1.1     skrll 		 some Linux systems.  Hope that nobody creates an
    742       1.1     skrll 		 emulation named 486.  */
    743       1.1     skrll 	    }
    744       1.1     skrll 	  else
    745       1.1     skrll 	    {
    746       1.1     skrll 	      /* -mEMUL */
    747       1.1     skrll 	      emulation = &argv[i][2];
    748       1.1     skrll 	    }
    749       1.1     skrll 	}
    750       1.1     skrll     }
    751       1.1     skrll 
    752       1.1     skrll   return emulation;
    753       1.1     skrll }
    754       1.1     skrll 
    755       1.1     skrll void
    756       1.1     skrll add_ysym (const char *name)
    757       1.1     skrll {
    758       1.6  christos   if (link_info.notice_hash == NULL)
    759       1.6  christos     {
    760       1.1     skrll       link_info.notice_hash
    761       1.1     skrll 	= (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
    762       1.1     skrll       if (!bfd_hash_table_init_n (link_info.notice_hash,
    763       1.1     skrll 				  bfd_hash_newfunc,
    764       1.8  christos 				  sizeof (struct bfd_hash_entry),
    765       1.1     skrll 				  61))
    766       1.1     skrll 	einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
    767      1.10  christos     }
    768       1.8  christos 
    769       1.1     skrll   if (bfd_hash_lookup (link_info.notice_hash, name, true, true) == NULL)
    770       1.1     skrll     einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
    771       1.2     skrll }
    772       1.5  christos 
    773       1.2     skrll void
    774       1.5  christos add_ignoresym (struct bfd_link_info *info, const char *name)
    775       1.2     skrll {
    776       1.5  christos   if (info->ignore_hash == NULL)
    777       1.6  christos     {
    778       1.6  christos       info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table));
    779       1.6  christos       if (!bfd_hash_table_init_n (info->ignore_hash,
    780       1.6  christos 				  bfd_hash_newfunc,
    781       1.8  christos 				  sizeof (struct bfd_hash_entry),
    782       1.2     skrll 				  61))
    783       1.2     skrll 	einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
    784      1.10  christos     }
    785       1.8  christos 
    786       1.2     skrll   if (bfd_hash_lookup (info->ignore_hash, name, true, true) == NULL)
    787       1.2     skrll     einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
    788       1.1     skrll }
    789       1.1     skrll 
    790       1.1     skrll /* Record a symbol to be wrapped, from the --wrap option.  */
    791       1.1     skrll 
    792       1.1     skrll void
    793       1.1     skrll add_wrap (const char *name)
    794       1.1     skrll {
    795       1.6  christos   if (link_info.wrap_hash == NULL)
    796       1.6  christos     {
    797       1.1     skrll       link_info.wrap_hash
    798       1.1     skrll 	= (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
    799       1.1     skrll       if (!bfd_hash_table_init_n (link_info.wrap_hash,
    800       1.1     skrll 				  bfd_hash_newfunc,
    801       1.8  christos 				  sizeof (struct bfd_hash_entry),
    802       1.1     skrll 				  61))
    803       1.1     skrll 	einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
    804      1.10  christos     }
    805       1.8  christos 
    806       1.1     skrll   if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
    807       1.1     skrll     einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
    808       1.1     skrll }
    809       1.1     skrll 
    810       1.1     skrll /* Handle the -retain-symbols-file option.  */
    811       1.1     skrll 
    812       1.1     skrll void
    813       1.1     skrll add_keepsyms_file (const char *filename)
    814       1.1     skrll {
    815       1.1     skrll   FILE *file;
    816       1.1     skrll   char *buf;
    817       1.1     skrll   size_t bufsize;
    818       1.1     skrll   int c;
    819       1.1     skrll 
    820       1.1     skrll   if (link_info.strip == strip_some)
    821       1.1     skrll     einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
    822       1.1     skrll 
    823       1.1     skrll   file = fopen (filename, "r");
    824       1.1     skrll   if (file == NULL)
    825       1.1     skrll     {
    826       1.1     skrll       bfd_set_error (bfd_error_system_call);
    827       1.1     skrll       einfo ("%X%P: %s: %E\n", filename);
    828       1.1     skrll       return;
    829       1.3  christos     }
    830       1.3  christos 
    831       1.1     skrll   link_info.keep_hash = (struct bfd_hash_table *)
    832       1.1     skrll       xmalloc (sizeof (struct bfd_hash_table));
    833       1.8  christos   if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
    834       1.1     skrll 			    sizeof (struct bfd_hash_entry)))
    835       1.1     skrll     einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
    836       1.3  christos 
    837       1.1     skrll   bufsize = 100;
    838       1.1     skrll   buf = (char *) xmalloc (bufsize);
    839       1.1     skrll 
    840       1.1     skrll   c = getc (file);
    841       1.1     skrll   while (c != EOF)
    842       1.1     skrll     {
    843       1.1     skrll       while (ISSPACE (c))
    844       1.1     skrll 	c = getc (file);
    845       1.1     skrll 
    846       1.1     skrll       if (c != EOF)
    847       1.1     skrll 	{
    848       1.6  christos 	  size_t len = 0;
    849       1.1     skrll 
    850       1.1     skrll 	  while (!ISSPACE (c) && c != EOF)
    851       1.1     skrll 	    {
    852       1.1     skrll 	      buf[len] = c;
    853       1.1     skrll 	      ++len;
    854       1.1     skrll 	      if (len >= bufsize)
    855       1.3  christos 		{
    856       1.1     skrll 		  bufsize *= 2;
    857       1.1     skrll 		  buf = (char *) xrealloc (buf, bufsize);
    858       1.1     skrll 		}
    859       1.1     skrll 	      c = getc (file);
    860       1.1     skrll 	    }
    861       1.1     skrll 
    862      1.10  christos 	  buf[len] = '\0';
    863       1.8  christos 
    864       1.1     skrll 	  if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) == NULL)
    865       1.1     skrll 	    einfo (_("%F%P: bfd_hash_lookup for insertion failed: %E\n"));
    866       1.1     skrll 	}
    867       1.1     skrll     }
    868       1.1     skrll 
    869       1.1     skrll   if (link_info.strip != strip_none)
    870       1.1     skrll     einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
    871       1.1     skrll 
    872       1.5  christos   free (buf);
    873       1.1     skrll   link_info.strip = strip_some;
    874       1.1     skrll   fclose (file);
    875       1.1     skrll }
    876       1.1     skrll 
    877       1.1     skrll /* Callbacks from the BFD linker routines.  */
    879       1.1     skrll 
    880      1.10  christos /* This is called when BFD has decided to include an archive member in
    881       1.1     skrll    a link.  */
    882       1.1     skrll 
    883       1.3  christos static bool
    884       1.3  christos add_archive_element (struct bfd_link_info *info,
    885       1.1     skrll 		     bfd *abfd,
    886       1.1     skrll 		     const char *name,
    887       1.8  christos 		     bfd **subsbfd ATTRIBUTE_UNUSED)
    888       1.3  christos {
    889       1.1     skrll   lang_input_statement_type *input;
    890       1.3  christos   lang_input_statement_type *parent;
    891       1.3  christos   lang_input_statement_type orig_input;
    892       1.7  christos 
    893      1.10  christos   input = (lang_input_statement_type *)
    894      1.10  christos       xcalloc (1, sizeof (lang_input_statement_type));
    895       1.1     skrll   input->header.type = lang_input_statement_enum;
    896       1.1     skrll   input->filename = bfd_get_filename (abfd);
    897       1.3  christos   input->local_sym_name = bfd_get_filename (abfd);
    898       1.3  christos   input->the_bfd = abfd;
    899       1.3  christos 
    900       1.3  christos   /* Save the original data for trace files/tries below, as plugins
    901      1.10  christos      (if enabled) may possibly alter it to point to a replacement
    902       1.6  christos      BFD, but we still want to output the original BFD filename.  */
    903       1.3  christos   orig_input = *input;
    904       1.3  christos #if BFD_SUPPORTS_PLUGINS
    905       1.5  christos   if (link_info.lto_plugin_active)
    906       1.5  christos     {
    907       1.5  christos       /* We must offer this archive member to the plugins to claim.  */
    908       1.6  christos       plugin_maybe_claim (input);
    909       1.6  christos       if (input->flags.claimed)
    910       1.6  christos 	{
    911       1.6  christos 	  if (no_more_claiming)
    912       1.9  christos 	    {
    913      1.10  christos 	      /* Don't claim new IR symbols after all IR symbols have
    914       1.6  christos 		 been claimed.  */
    915       1.6  christos 	      if (verbose)
    916      1.10  christos 		info_msg ("%pI: no new IR symbols to claim\n",
    917       1.6  christos 			  &orig_input);
    918      1.10  christos 	      input->flags.claimed = 0;
    919       1.5  christos 	      return false;
    920       1.3  christos 	    }
    921       1.3  christos 	  input->flags.claim_archive = true;
    922      1.10  christos 	  *subsbfd = input->the_bfd;
    923      1.10  christos 	}
    924      1.10  christos     }
    925      1.10  christos #endif /* BFD_SUPPORTS_PLUGINS */
    926      1.10  christos 
    927      1.10  christos   if (link_info.input_bfds_tail == &input->the_bfd->link.next
    928      1.10  christos       || input->the_bfd->link.next != NULL)
    929      1.10  christos     {
    930      1.10  christos       /* We have already loaded this element, and are attempting to
    931      1.10  christos 	 load it again.  This can happen when the archive map doesn't
    932      1.10  christos 	 match actual symbols defined by the element.  */
    933      1.10  christos       free (input);
    934      1.10  christos       bfd_set_error (bfd_error_malformed_archive);
    935      1.10  christos       return false;
    936      1.10  christos     }
    937      1.10  christos 
    938      1.10  christos   /* Set the file_chain pointer of archives to the last element loaded
    939      1.10  christos      from the archive.  See ldlang.c:find_rescan_insertion.  */
    940       1.3  christos   parent = bfd_usrdata (abfd->my_archive);
    941       1.1     skrll   if (parent != NULL && !parent->flags.reload)
    942       1.1     skrll     parent->next = input;
    943       1.1     skrll 
    944       1.1     skrll   ldlang_add_file (input);
    945      1.10  christos 
    946       1.1     skrll   if (config.map_file != NULL)
    947       1.1     skrll     {
    948       1.1     skrll       static bool header_printed;
    949       1.1     skrll       struct bfd_link_hash_entry *h;
    950      1.10  christos       bfd *from;
    951       1.9  christos       int len;
    952       1.9  christos 
    953      1.10  christos       h = bfd_link_hash_lookup (info->hash, name, false, false, true);
    954      1.10  christos       if (h == NULL
    955       1.1     skrll 	  && info->pei386_auto_import
    956       1.1     skrll 	  && startswith (name, "__imp_"))
    957       1.1     skrll 	h = bfd_link_hash_lookup (info->hash, name + 6, false, false, true);
    958       1.1     skrll 
    959       1.1     skrll       if (h == NULL)
    960       1.1     skrll 	from = NULL;
    961       1.1     skrll       else
    962       1.1     skrll 	{
    963       1.1     skrll 	  switch (h->type)
    964       1.1     skrll 	    {
    965       1.1     skrll 	    default:
    966       1.1     skrll 	      from = NULL;
    967       1.1     skrll 	      break;
    968       1.1     skrll 
    969       1.1     skrll 	    case bfd_link_hash_defined:
    970       1.1     skrll 	    case bfd_link_hash_defweak:
    971       1.1     skrll 	      from = h->u.def.section->owner;
    972       1.1     skrll 	      break;
    973       1.1     skrll 
    974       1.1     skrll 	    case bfd_link_hash_undefined:
    975       1.1     skrll 	    case bfd_link_hash_undefweak:
    976       1.1     skrll 	      from = h->u.undef.abfd;
    977       1.1     skrll 	      break;
    978       1.1     skrll 
    979       1.1     skrll 	    case bfd_link_hash_common:
    980       1.1     skrll 	      from = h->u.c.p->section->owner;
    981       1.1     skrll 	      break;
    982       1.6  christos 	    }
    983       1.1     skrll 	}
    984       1.7  christos 
    985      1.10  christos       if (!header_printed)
    986       1.1     skrll 	{
    987       1.1     skrll 	  minfo (_("Archive member included to satisfy reference by file (symbol)\n\n"));
    988       1.6  christos 	  header_printed = true;
    989       1.6  christos 	}
    990       1.1     skrll 
    991       1.1     skrll       if (abfd->my_archive == NULL
    992       1.1     skrll 	  || bfd_is_thin_archive (abfd->my_archive))
    993       1.1     skrll 	{
    994       1.1     skrll 	  minfo ("%s", bfd_get_filename (abfd));
    995       1.1     skrll 	  len = strlen (bfd_get_filename (abfd));
    996       1.6  christos 	}
    997       1.1     skrll       else
    998       1.6  christos 	{
    999       1.1     skrll 	  minfo ("%s(%s)", bfd_get_filename (abfd->my_archive),
   1000       1.1     skrll 		 bfd_get_filename (abfd));
   1001       1.1     skrll 	  len = (strlen (bfd_get_filename (abfd->my_archive))
   1002       1.1     skrll 		 + strlen (bfd_get_filename (abfd))
   1003       1.1     skrll 		 + 2);
   1004       1.1     skrll 	}
   1005       1.1     skrll 
   1006       1.1     skrll       if (len >= 29)
   1007       1.1     skrll 	{
   1008  1.10.2.1  perseant 	  print_nl ();
   1009       1.1     skrll 	  len = 0;
   1010       1.1     skrll 	}
   1011       1.8  christos       print_spaces (30 - len);
   1012       1.1     skrll 
   1013       1.8  christos       if (from != NULL)
   1014       1.1     skrll 	minfo ("%pB ", from);
   1015       1.1     skrll       if (h != NULL)
   1016       1.1     skrll 	minfo ("(%pT)\n", h->root.string);
   1017       1.1     skrll       else
   1018       1.9  christos 	minfo ("(%s)\n", name);
   1019       1.9  christos     }
   1020       1.9  christos 
   1021       1.8  christos   if (verbose
   1022      1.10  christos       || trace_files > 1
   1023       1.1     skrll       || (trace_files && bfd_is_thin_archive (orig_input.the_bfd->my_archive)))
   1024       1.1     skrll     info_msg ("%pI\n", &orig_input);
   1025       1.1     skrll   return true;
   1026       1.1     skrll }
   1027       1.1     skrll 
   1028       1.6  christos /* This is called when BFD has discovered a symbol which is defined
   1029       1.3  christos    multiple times.  */
   1030       1.3  christos 
   1031       1.1     skrll static void
   1032       1.1     skrll multiple_definition (struct bfd_link_info *info,
   1033       1.1     skrll 		     struct bfd_link_hash_entry *h,
   1034       1.1     skrll 		     bfd *nbfd,
   1035       1.3  christos 		     asection *nsec,
   1036       1.3  christos 		     bfd_vma nval)
   1037       1.3  christos {
   1038       1.3  christos   const char *name;
   1039       1.3  christos   bfd *obfd;
   1040       1.3  christos   asection *osec;
   1041       1.6  christos   bfd_vma oval;
   1042       1.3  christos 
   1043       1.3  christos   if (info->allow_multiple_definition)
   1044       1.3  christos     return;
   1045       1.3  christos 
   1046       1.3  christos   switch (h->type)
   1047       1.3  christos     {
   1048       1.3  christos     case bfd_link_hash_defined:
   1049       1.3  christos       osec = h->u.def.section;
   1050       1.3  christos       oval = h->u.def.value;
   1051       1.3  christos       obfd = h->u.def.section->owner;
   1052       1.3  christos       break;
   1053       1.3  christos     case bfd_link_hash_indirect:
   1054       1.3  christos       osec = bfd_ind_section_ptr;
   1055       1.3  christos       oval = 0;
   1056       1.3  christos       obfd = NULL;
   1057       1.3  christos       break;
   1058       1.3  christos     default:
   1059       1.3  christos       abort ();
   1060       1.3  christos     }
   1061       1.3  christos 
   1062       1.3  christos   /* Ignore a redefinition of an absolute symbol to the
   1063       1.3  christos      same value; it's harmless.  */
   1064       1.3  christos   if (h->type == bfd_link_hash_defined
   1065       1.6  christos       && bfd_is_abs_section (osec)
   1066       1.3  christos       && bfd_is_abs_section (nsec)
   1067       1.1     skrll       && nval == oval)
   1068       1.1     skrll     return;
   1069       1.1     skrll 
   1070       1.1     skrll   /* If either section has the output_section field set to
   1071       1.1     skrll      bfd_abs_section_ptr, it means that the section is being
   1072       1.8  christos      discarded, and this is not really a multiple definition at all.
   1073       1.8  christos      FIXME: It would be cleaner to somehow ignore symbols defined in
   1074       1.8  christos      sections which are being discarded.  */
   1075       1.8  christos   if (!info->prohibit_multiple_definition_absolute
   1076       1.8  christos       && ((osec->output_section != NULL
   1077       1.8  christos 	   && ! bfd_is_abs_section (osec)
   1078       1.8  christos 	   && bfd_is_abs_section (osec->output_section))
   1079       1.6  christos 	  || (nsec->output_section != NULL
   1080       1.1     skrll 	      && !bfd_is_abs_section (nsec)
   1081       1.3  christos 	      && bfd_is_abs_section (nsec->output_section))))
   1082       1.3  christos     return;
   1083       1.3  christos 
   1084       1.3  christos   name = h->root.string;
   1085       1.3  christos   if (nbfd == NULL)
   1086       1.3  christos     {
   1087       1.3  christos       nbfd = obfd;
   1088       1.3  christos       nsec = osec;
   1089      1.10  christos       nval = oval;
   1090      1.10  christos       obfd = NULL;
   1091      1.10  christos     }
   1092      1.10  christos   if (info->warn_multiple_definition)
   1093      1.10  christos     einfo (_("%P: %C: warning: multiple definition of `%pT'"),
   1094      1.10  christos 	   nbfd, nsec, nval, name);
   1095       1.1     skrll   else
   1096       1.8  christos     einfo (_("%X%P: %C: multiple definition of `%pT'"),
   1097       1.8  christos 	   nbfd, nsec, nval, name);
   1098       1.1     skrll   if (obfd != NULL)
   1099       1.5  christos     einfo (_("; %D: first defined here"), obfd, osec, oval);
   1100       1.1     skrll   einfo ("\n");
   1101       1.8  christos 
   1102       1.5  christos   if (RELAXATION_ENABLED_BY_USER)
   1103       1.1     skrll     {
   1104       1.1     skrll       einfo (_("%P: disabling relaxation; it will not work with multiple definitions\n"));
   1105       1.1     skrll       DISABLE_RELAXATION;
   1106       1.1     skrll     }
   1107       1.1     skrll }
   1108       1.1     skrll 
   1109       1.1     skrll /* This is called when there is a definition of a common symbol, or
   1110       1.1     skrll    when a common symbol is found for a symbol that is already defined,
   1111       1.6  christos    or when two common symbols are found.  We only do something if
   1112       1.1     skrll    -warn-common was used.  */
   1113       1.3  christos 
   1114       1.1     skrll static void
   1115       1.1     skrll multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1116       1.1     skrll 		 struct bfd_link_hash_entry *h,
   1117       1.1     skrll 		 bfd *nbfd,
   1118       1.3  christos 		 enum bfd_link_hash_type ntype,
   1119       1.3  christos 		 bfd_vma nsize)
   1120       1.3  christos {
   1121       1.3  christos   const char *name;
   1122       1.3  christos   bfd *obfd;
   1123       1.3  christos   enum bfd_link_hash_type otype;
   1124       1.6  christos   bfd_vma osize;
   1125       1.1     skrll 
   1126       1.3  christos   if (!config.warn_common)
   1127       1.3  christos     return;
   1128       1.3  christos 
   1129       1.3  christos   name = h->root.string;
   1130       1.3  christos   otype = h->type;
   1131       1.3  christos   if (otype == bfd_link_hash_common)
   1132       1.3  christos     {
   1133       1.3  christos       obfd = h->u.c.p->section->owner;
   1134       1.3  christos       osize = h->u.c.size;
   1135       1.3  christos     }
   1136       1.3  christos   else if (otype == bfd_link_hash_defined
   1137       1.3  christos 	   || otype == bfd_link_hash_defweak)
   1138       1.3  christos     {
   1139       1.3  christos       obfd = h->u.def.section->owner;
   1140       1.3  christos       osize = 0;
   1141       1.3  christos     }
   1142       1.3  christos   else
   1143       1.3  christos     {
   1144       1.3  christos       /* FIXME: It would nice if we could report the BFD which defined
   1145       1.3  christos 	 an indirect symbol, but we don't have anywhere to store the
   1146       1.3  christos 	 information.  */
   1147       1.3  christos       obfd = NULL;
   1148       1.1     skrll       osize = 0;
   1149       1.1     skrll     }
   1150       1.1     skrll 
   1151       1.1     skrll   if (ntype == bfd_link_hash_defined
   1152       1.1     skrll       || ntype == bfd_link_hash_defweak
   1153       1.1     skrll       || ntype == bfd_link_hash_indirect)
   1154       1.8  christos     {
   1155       1.8  christos       ASSERT (otype == bfd_link_hash_common);
   1156       1.8  christos       if (obfd != NULL)
   1157       1.8  christos 	einfo (_("%P: %pB: warning: definition of `%pT' overriding common"
   1158       1.8  christos 		 " from %pB\n"),
   1159       1.8  christos 	       nbfd, name, obfd);
   1160       1.1     skrll       else
   1161       1.1     skrll 	einfo (_("%P: %pB: warning: definition of `%pT' overriding common\n"),
   1162       1.1     skrll 	       nbfd, name);
   1163       1.1     skrll     }
   1164       1.1     skrll   else if (otype == bfd_link_hash_defined
   1165       1.1     skrll 	   || otype == bfd_link_hash_defweak
   1166       1.1     skrll 	   || otype == bfd_link_hash_indirect)
   1167       1.8  christos     {
   1168       1.8  christos       ASSERT (ntype == bfd_link_hash_common);
   1169       1.8  christos       if (obfd != NULL)
   1170       1.8  christos 	einfo (_("%P: %pB: warning: common of `%pT' overridden by definition"
   1171       1.8  christos 		 " from %pB\n"),
   1172       1.8  christos 	       nbfd, name, obfd);
   1173       1.1     skrll       else
   1174       1.1     skrll 	einfo (_("%P: %pB: warning: common of `%pT' overridden by definition\n"),
   1175       1.1     skrll 	       nbfd, name);
   1176       1.1     skrll     }
   1177       1.1     skrll   else
   1178       1.1     skrll     {
   1179       1.1     skrll       ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
   1180       1.8  christos       if (osize > nsize)
   1181       1.8  christos 	{
   1182       1.8  christos 	  if (obfd != NULL)
   1183       1.8  christos 	    einfo (_("%P: %pB: warning: common of `%pT' overridden"
   1184       1.8  christos 		     " by larger common from %pB\n"),
   1185       1.8  christos 		   nbfd, name, obfd);
   1186       1.8  christos 	  else
   1187       1.1     skrll 	    einfo (_("%P: %pB: warning: common of `%pT' overridden"
   1188       1.1     skrll 		     " by larger common\n"),
   1189       1.1     skrll 		   nbfd, name);
   1190       1.1     skrll 	}
   1191       1.8  christos       else if (nsize > osize)
   1192       1.8  christos 	{
   1193       1.8  christos 	  if (obfd != NULL)
   1194       1.8  christos 	    einfo (_("%P: %pB: warning: common of `%pT' overriding"
   1195       1.8  christos 		     " smaller common from %pB\n"),
   1196       1.8  christos 		   nbfd, name, obfd);
   1197       1.8  christos 	  else
   1198       1.1     skrll 	    einfo (_("%P: %pB: warning: common of `%pT' overriding"
   1199       1.1     skrll 		     " smaller common\n"),
   1200       1.1     skrll 		   nbfd, name);
   1201       1.1     skrll 	}
   1202       1.8  christos       else
   1203       1.8  christos 	{
   1204       1.8  christos 	  if (obfd != NULL)
   1205       1.8  christos 	    einfo (_("%P: %pB and %pB: warning: multiple common of `%pT'\n"),
   1206       1.8  christos 		   nbfd, obfd, name);
   1207       1.1     skrll 	  else
   1208       1.1     skrll 	    einfo (_("%P: %pB: warning: multiple common of `%pT'\n"),
   1209       1.1     skrll 		   nbfd, name);
   1210       1.1     skrll 	}
   1211       1.1     skrll     }
   1212       1.1     skrll }
   1213       1.1     skrll 
   1214       1.1     skrll /* This is called when BFD has discovered a set element.  H is the
   1215       1.6  christos    entry in the linker hash table for the set.  SECTION and VALUE
   1216       1.1     skrll    represent a value which should be added to the set.  */
   1217       1.1     skrll 
   1218       1.1     skrll static void
   1219       1.1     skrll add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1220       1.1     skrll 	    struct bfd_link_hash_entry *h,
   1221       1.1     skrll 	    bfd_reloc_code_real_type reloc,
   1222       1.1     skrll 	    bfd *abfd,
   1223       1.1     skrll 	    asection *section,
   1224       1.1     skrll 	    bfd_vma value)
   1225       1.1     skrll {
   1226       1.1     skrll   if (config.warn_constructors)
   1227       1.6  christos     einfo (_("%P: warning: global constructor %s used\n"),
   1228       1.6  christos 	   h->root.string);
   1229       1.1     skrll 
   1230       1.1     skrll   if (!config.build_constructors)
   1231       1.1     skrll     return;
   1232       1.1     skrll 
   1233       1.1     skrll   ldctor_add_set_entry (h, reloc, NULL, section, value);
   1234       1.1     skrll 
   1235       1.1     skrll   if (h->type == bfd_link_hash_new)
   1236       1.1     skrll     {
   1237       1.1     skrll       h->type = bfd_link_hash_undefined;
   1238       1.1     skrll       h->u.undef.abfd = abfd;
   1239       1.1     skrll       /* We don't call bfd_link_add_undef to add this to the list of
   1240       1.1     skrll 	 undefined symbols because we are going to define it
   1241       1.1     skrll 	 ourselves.  */
   1242       1.1     skrll     }
   1243       1.1     skrll }
   1244       1.1     skrll 
   1245       1.1     skrll /* This is called when BFD has discovered a constructor.  This is only
   1246       1.1     skrll    called for some object file formats--those which do not handle
   1247       1.6  christos    constructors in some more clever fashion.  This is similar to
   1248       1.1     skrll    adding an element to a set, but less general.  */
   1249      1.10  christos 
   1250       1.1     skrll static void
   1251       1.1     skrll constructor_callback (struct bfd_link_info *info,
   1252       1.1     skrll 		      bool constructor,
   1253       1.1     skrll 		      const char *name,
   1254       1.1     skrll 		      bfd *abfd,
   1255       1.1     skrll 		      asection *section,
   1256       1.1     skrll 		      bfd_vma value)
   1257       1.1     skrll {
   1258       1.1     skrll   char *s;
   1259       1.1     skrll   struct bfd_link_hash_entry *h;
   1260       1.1     skrll   char set_name[1 + sizeof "__CTOR_LIST__"];
   1261       1.1     skrll 
   1262       1.6  christos   if (config.warn_constructors)
   1263       1.6  christos     einfo (_("%P: warning: global constructor %s used\n"), name);
   1264       1.1     skrll 
   1265       1.1     skrll   if (!config.build_constructors)
   1266       1.1     skrll     return;
   1267       1.5  christos 
   1268       1.5  christos   /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
   1269       1.1     skrll      useful error message.  */
   1270       1.8  christos   if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
   1271       1.1     skrll       && (bfd_link_relocatable (info)
   1272       1.1     skrll 	  || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
   1273       1.1     skrll     einfo (_("%F%P: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
   1274       1.1     skrll 
   1275       1.1     skrll   s = set_name;
   1276       1.1     skrll   if (bfd_get_symbol_leading_char (abfd) != '\0')
   1277       1.1     skrll     *s++ = bfd_get_symbol_leading_char (abfd);
   1278       1.1     skrll   if (constructor)
   1279       1.1     skrll     strcpy (s, "__CTOR_LIST__");
   1280      1.10  christos   else
   1281       1.1     skrll     strcpy (s, "__DTOR_LIST__");
   1282       1.8  christos 
   1283       1.1     skrll   h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
   1284       1.1     skrll   if (h == (struct bfd_link_hash_entry *) NULL)
   1285       1.1     skrll     einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
   1286       1.1     skrll   if (h->type == bfd_link_hash_new)
   1287       1.1     skrll     {
   1288       1.1     skrll       h->type = bfd_link_hash_undefined;
   1289       1.1     skrll       h->u.undef.abfd = abfd;
   1290       1.1     skrll       /* We don't call bfd_link_add_undef to add this to the list of
   1291       1.1     skrll 	 undefined symbols because we are going to define it
   1292       1.1     skrll 	 ourselves.  */
   1293       1.1     skrll     }
   1294       1.1     skrll 
   1295       1.1     skrll   ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
   1296       1.1     skrll }
   1297       1.1     skrll 
   1298       1.1     skrll /* A structure used by warning_callback to pass information through
   1299       1.1     skrll    bfd_map_over_sections.  */
   1300      1.10  christos 
   1301       1.1     skrll struct warning_callback_info
   1302       1.1     skrll {
   1303       1.1     skrll   bool found;
   1304       1.1     skrll   const char *warning;
   1305       1.1     skrll   const char *symbol;
   1306       1.5  christos   asymbol **asymbols;
   1307       1.5  christos };
   1308       1.5  christos 
   1309      1.10  christos /* Look through the relocs to see if we can find a plausible address
   1310       1.5  christos    for SYMBOL in ABFD.  Return TRUE if found.  Otherwise return FALSE.  */
   1311       1.5  christos 
   1312       1.5  christos static bool
   1313       1.5  christos symbol_warning (const char *warning, const char *symbol, bfd *abfd)
   1314       1.5  christos {
   1315       1.8  christos   struct warning_callback_info cinfo;
   1316       1.5  christos 
   1317      1.10  christos   if (!bfd_generic_link_read_symbols (abfd))
   1318       1.5  christos     einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
   1319       1.5  christos 
   1320       1.5  christos   cinfo.found = false;
   1321       1.5  christos   cinfo.warning = warning;
   1322       1.5  christos   cinfo.symbol = symbol;
   1323       1.5  christos   cinfo.asymbols = bfd_get_outsymbols (abfd);
   1324       1.5  christos   bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
   1325       1.1     skrll   return cinfo.found;
   1326       1.1     skrll }
   1327       1.6  christos 
   1328       1.1     skrll /* This is called when there is a reference to a warning symbol.  */
   1329       1.1     skrll 
   1330       1.1     skrll static void
   1331       1.1     skrll warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1332       1.1     skrll 		  const char *warning,
   1333       1.1     skrll 		  const char *symbol,
   1334       1.1     skrll 		  bfd *abfd,
   1335       1.1     skrll 		  asection *section,
   1336       1.1     skrll 		  bfd_vma address)
   1337       1.6  christos {
   1338       1.1     skrll   /* This is a hack to support warn_multiple_gp.  FIXME: This should
   1339       1.6  christos      have a cleaner interface, but what?  */
   1340       1.1     skrll   if (!config.warn_multiple_gp
   1341       1.1     skrll       && strcmp (warning, "using multiple gp values") == 0)
   1342       1.8  christos     return;
   1343       1.1     skrll 
   1344       1.1     skrll   if (section != NULL)
   1345       1.1     skrll     einfo ("%P: %C: %s%s\n", abfd, section, address, _("warning: "), warning);
   1346       1.8  christos   else if (abfd == NULL)
   1347       1.6  christos     einfo ("%P: %s%s\n", _("warning: "), warning);
   1348       1.1     skrll   else if (symbol == NULL)
   1349       1.5  christos     einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
   1350       1.5  christos   else if (!symbol_warning (warning, symbol, abfd))
   1351       1.5  christos     {
   1352       1.5  christos       bfd *b;
   1353       1.6  christos       /* Search all input files for a reference to SYMBOL.  */
   1354       1.8  christos       for (b = info->input_bfds; b; b = b->link.next)
   1355       1.1     skrll 	if (b != abfd && symbol_warning (warning, symbol, b))
   1356       1.1     skrll 	  return;
   1357       1.1     skrll       einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
   1358       1.1     skrll     }
   1359       1.1     skrll }
   1360       1.1     skrll 
   1361       1.1     skrll /* This is called by warning_callback for each section.  It checks the
   1362       1.1     skrll    relocs of the section to see if it can find a reference to the
   1363       1.1     skrll    symbol which triggered the warning.  If it can, it uses the reloc
   1364       1.1     skrll    to give an error message with a file and line number.  */
   1365       1.1     skrll 
   1366       1.3  christos static void
   1367       1.1     skrll warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
   1368       1.1     skrll {
   1369       1.1     skrll   struct warning_callback_info *info = (struct warning_callback_info *) iarg;
   1370       1.1     skrll   long relsize;
   1371       1.1     skrll   arelent **relpp;
   1372       1.1     skrll   long relcount;
   1373       1.1     skrll   arelent **p, **pend;
   1374       1.1     skrll 
   1375       1.1     skrll   if (info->found)
   1376       1.1     skrll     return;
   1377       1.8  christos 
   1378       1.1     skrll   relsize = bfd_get_reloc_upper_bound (abfd, sec);
   1379       1.1     skrll   if (relsize < 0)
   1380       1.1     skrll     einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
   1381       1.3  christos   if (relsize == 0)
   1382       1.1     skrll     return;
   1383       1.1     skrll 
   1384       1.8  christos   relpp = (arelent **) xmalloc (relsize);
   1385       1.1     skrll   relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
   1386       1.1     skrll   if (relcount < 0)
   1387       1.1     skrll     einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
   1388       1.1     skrll 
   1389       1.1     skrll   p = relpp;
   1390       1.1     skrll   pend = p + relcount;
   1391       1.1     skrll   for (; p < pend && *p != NULL; p++)
   1392       1.1     skrll     {
   1393       1.1     skrll       arelent *q = *p;
   1394       1.1     skrll 
   1395       1.1     skrll       if (q->sym_ptr_ptr != NULL
   1396       1.1     skrll 	  && *q->sym_ptr_ptr != NULL
   1397  1.10.2.1  perseant 	  && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
   1398       1.1     skrll 	{
   1399      1.10  christos 	  /* We found a reloc for the symbol we are looking for.  */
   1400       1.1     skrll 	  einfo ("%P: %H: %s%s\n", abfd, sec, q->address, _("warning: "),
   1401       1.1     skrll 		 info->warning);
   1402       1.1     skrll 	  info->found = true;
   1403       1.1     skrll 	  break;
   1404       1.1     skrll 	}
   1405       1.1     skrll     }
   1406       1.1     skrll 
   1407      1.10  christos   free (relpp);
   1408      1.10  christos }
   1409      1.10  christos 
   1410      1.10  christos #if SUPPORT_ERROR_HANDLING_SCRIPT
   1411       1.1     skrll char * error_handling_script = NULL;
   1412       1.1     skrll #endif
   1413       1.6  christos 
   1414       1.5  christos /* This is called when an undefined symbol is found.  */
   1415       1.1     skrll 
   1416       1.1     skrll static void
   1417       1.1     skrll undefined_symbol (struct bfd_link_info *info,
   1418       1.1     skrll 		  const char *name,
   1419      1.10  christos 		  bfd *abfd,
   1420       1.1     skrll 		  asection *section,
   1421       1.1     skrll 		  bfd_vma address,
   1422       1.1     skrll 		  bool error)
   1423       1.1     skrll {
   1424       1.1     skrll   static char *error_name;
   1425       1.1     skrll   static unsigned int error_count;
   1426       1.2     skrll 
   1427      1.10  christos #define MAX_ERRORS_IN_A_ROW 5
   1428       1.6  christos 
   1429       1.2     skrll   if (info->ignore_hash != NULL
   1430       1.1     skrll       && bfd_hash_lookup (info->ignore_hash, name, false, false) != NULL)
   1431       1.1     skrll     return;
   1432       1.1     skrll 
   1433       1.5  christos   if (config.warn_once)
   1434       1.1     skrll     {
   1435       1.1     skrll       /* Only warn once about a particular undefined symbol.  */
   1436       1.1     skrll       add_ignoresym (info, name);
   1437       1.1     skrll     }
   1438       1.1     skrll 
   1439       1.1     skrll   /* We never print more than a reasonable number of errors in a row
   1440       1.1     skrll      for a single symbol.  */
   1441       1.1     skrll   if (error_name != NULL
   1442       1.1     skrll       && strcmp (name, error_name) == 0)
   1443       1.1     skrll     ++error_count;
   1444      1.10  christos   else
   1445       1.1     skrll     {
   1446       1.1     skrll       error_count = 0;
   1447       1.1     skrll       free (error_name);
   1448      1.10  christos       error_name = xstrdup (name);
   1449      1.10  christos     }
   1450      1.10  christos 
   1451      1.10  christos #if SUPPORT_ERROR_HANDLING_SCRIPT
   1452      1.10  christos   if (error_handling_script != NULL
   1453      1.10  christos       && error_count < MAX_ERRORS_IN_A_ROW)
   1454      1.10  christos     {
   1455      1.10  christos       char *        argv[4];
   1456      1.10  christos       const char *  res;
   1457      1.10  christos       int           status, err;
   1458      1.10  christos 
   1459      1.10  christos       argv[0] = error_handling_script;
   1460      1.10  christos       argv[1] = "undefined-symbol";
   1461      1.10  christos       argv[2] = (char *) name;
   1462      1.10  christos       argv[3] = NULL;
   1463      1.10  christos 
   1464      1.10  christos       if (verbose)
   1465      1.10  christos 	einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
   1466      1.10  christos 	       argv[0], argv[1], argv[2]);
   1467      1.10  christos 
   1468      1.10  christos       res = pex_one (PEX_SEARCH, error_handling_script, argv,
   1469      1.10  christos 		     N_("error handling script"),
   1470      1.10  christos 		     NULL /* Send stdout to random, temp file.  */,
   1471      1.10  christos 		     NULL /* Write to stderr.  */,
   1472      1.10  christos 		     &status, &err);
   1473      1.10  christos       if (res != NULL)
   1474      1.10  christos 	{
   1475      1.10  christos 	  einfo (_("%P: Failed to run error handling script '%s', reason: "),
   1476      1.10  christos 		 error_handling_script);
   1477      1.10  christos 	  /* FIXME: We assume here that errrno == err.  */
   1478      1.10  christos 	  perror (res);
   1479      1.10  christos 	}
   1480      1.10  christos       /* We ignore the return status of the script and
   1481      1.10  christos 	 carry on to issue the normal error message.  */
   1482       1.1     skrll     }
   1483       1.1     skrll #endif /* SUPPORT_ERROR_HANDLING_SCRIPT */
   1484       1.1     skrll 
   1485       1.1     skrll   if (section != NULL)
   1486       1.1     skrll     {
   1487  1.10.2.1  perseant       if (error_count < MAX_ERRORS_IN_A_ROW)
   1488       1.1     skrll 	{
   1489       1.1     skrll 	  if (error)
   1490  1.10.2.1  perseant 	    einfo (_("%X%P: %H: undefined reference to `%pT'\n"),
   1491       1.1     skrll 		   abfd, section, address, name);
   1492       1.1     skrll 	  else
   1493       1.1     skrll 	    einfo (_("%P: %H: warning: undefined reference to `%pT'\n"),
   1494       1.1     skrll 		   abfd, section, address, name);
   1495       1.1     skrll 	}
   1496       1.8  christos       else if (error_count == MAX_ERRORS_IN_A_ROW)
   1497       1.1     skrll 	{
   1498       1.1     skrll 	  if (error)
   1499       1.8  christos 	    einfo (_("%X%P: %D: more undefined references to `%pT' follow\n"),
   1500       1.1     skrll 		   abfd, section, address, name);
   1501       1.1     skrll 	  else
   1502       1.1     skrll 	    einfo (_("%P: %D: warning: more undefined references to `%pT' follow\n"),
   1503       1.1     skrll 		   abfd, section, address, name);
   1504       1.1     skrll 	}
   1505       1.1     skrll       else if (error)
   1506       1.1     skrll 	einfo ("%X");
   1507       1.1     skrll     }
   1508       1.1     skrll   else
   1509       1.1     skrll     {
   1510       1.8  christos       if (error_count < MAX_ERRORS_IN_A_ROW)
   1511       1.1     skrll 	{
   1512       1.1     skrll 	  if (error)
   1513       1.8  christos 	    einfo (_("%X%P: %pB: undefined reference to `%pT'\n"),
   1514       1.1     skrll 		   abfd, name);
   1515       1.1     skrll 	  else
   1516       1.1     skrll 	    einfo (_("%P: %pB: warning: undefined reference to `%pT'\n"),
   1517       1.1     skrll 		   abfd, name);
   1518       1.1     skrll 	}
   1519       1.8  christos       else if (error_count == MAX_ERRORS_IN_A_ROW)
   1520       1.1     skrll 	{
   1521       1.1     skrll 	  if (error)
   1522       1.8  christos 	    einfo (_("%X%P: %pB: more undefined references to `%pT' follow\n"),
   1523       1.1     skrll 		   abfd, name);
   1524       1.1     skrll 	  else
   1525       1.1     skrll 	    einfo (_("%P: %pB: warning: more undefined references to `%pT' follow\n"),
   1526       1.1     skrll 		   abfd, name);
   1527       1.1     skrll 	}
   1528       1.1     skrll       else if (error)
   1529       1.1     skrll 	einfo ("%X");
   1530       1.1     skrll     }
   1531       1.1     skrll }
   1532       1.1     skrll 
   1533       1.1     skrll /* Counter to limit the number of relocation overflow error messages
   1534       1.1     skrll    to print.  Errors are printed as it is decremented.  When it's
   1535       1.1     skrll    called and the counter is zero, a final message is printed
   1536       1.1     skrll    indicating more relocations were omitted.  When it gets to -1, no
   1537       1.1     skrll    such errors are printed.  If it's initially set to a value less
   1538       1.1     skrll    than -1, all such errors will be printed (--verbose does this).  */
   1539       1.1     skrll 
   1540       1.1     skrll int overflow_cutoff_limit = 10;
   1541       1.6  christos 
   1542       1.5  christos /* This is called when a reloc overflows.  */
   1543       1.1     skrll 
   1544       1.1     skrll static void
   1545       1.1     skrll reloc_overflow (struct bfd_link_info *info,
   1546       1.1     skrll 		struct bfd_link_hash_entry *entry,
   1547       1.1     skrll 		const char *name,
   1548       1.1     skrll 		const char *reloc_name,
   1549       1.1     skrll 		bfd_vma addend,
   1550       1.1     skrll 		bfd *abfd,
   1551       1.1     skrll 		asection *section,
   1552       1.6  christos 		bfd_vma address)
   1553       1.1     skrll {
   1554       1.9  christos   if (overflow_cutoff_limit == -1)
   1555       1.1     skrll     return;
   1556       1.1     skrll 
   1557       1.1     skrll   einfo ("%X%H:", abfd, section, address);
   1558       1.1     skrll 
   1559       1.1     skrll   if (overflow_cutoff_limit >= 0
   1560       1.6  christos       && overflow_cutoff_limit-- == 0)
   1561       1.1     skrll     {
   1562       1.1     skrll       einfo (_(" additional relocation overflows omitted from the output\n"));
   1563       1.1     skrll       return;
   1564       1.1     skrll     }
   1565       1.1     skrll 
   1566       1.1     skrll   if (entry)
   1567       1.1     skrll     {
   1568       1.1     skrll       while (entry->type == bfd_link_hash_indirect
   1569       1.1     skrll 	     || entry->type == bfd_link_hash_warning)
   1570       1.1     skrll 	entry = entry->u.i.link;
   1571       1.1     skrll       switch (entry->type)
   1572       1.6  christos 	{
   1573       1.8  christos 	case bfd_link_hash_undefined:
   1574       1.1     skrll 	case bfd_link_hash_undefweak:
   1575       1.1     skrll 	  einfo (_(" relocation truncated to fit: "
   1576       1.1     skrll 		   "%s against undefined symbol `%pT'"),
   1577       1.1     skrll 		 reloc_name, entry->root.string);
   1578       1.6  christos 	  break;
   1579       1.8  christos 	case bfd_link_hash_defined:
   1580       1.1     skrll 	case bfd_link_hash_defweak:
   1581       1.1     skrll 	  einfo (_(" relocation truncated to fit: "
   1582       1.1     skrll 		   "%s against symbol `%pT' defined in %pA section in %pB"),
   1583       1.5  christos 		 reloc_name, entry->root.string,
   1584       1.1     skrll 		 entry->u.def.section,
   1585       1.1     skrll 		 entry->u.def.section == bfd_abs_section_ptr
   1586       1.1     skrll 		 ? info->output_bfd : entry->u.def.section->owner);
   1587       1.1     skrll 	  break;
   1588       1.1     skrll 	default:
   1589       1.1     skrll 	  abort ();
   1590       1.1     skrll 	  break;
   1591       1.8  christos 	}
   1592       1.1     skrll     }
   1593       1.1     skrll   else
   1594       1.1     skrll     einfo (_(" relocation truncated to fit: %s against `%pT'"),
   1595       1.1     skrll 	   reloc_name, name);
   1596       1.1     skrll   if (addend != 0)
   1597       1.1     skrll     einfo ("+%v", addend);
   1598       1.1     skrll   einfo ("\n");
   1599       1.1     skrll }
   1600       1.6  christos 
   1601       1.1     skrll /* This is called when a dangerous relocation is made.  */
   1602       1.1     skrll 
   1603       1.1     skrll static void
   1604       1.1     skrll reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1605       1.1     skrll 		 const char *message,
   1606       1.1     skrll 		 bfd *abfd,
   1607       1.9  christos 		 asection *section,
   1608       1.1     skrll 		 bfd_vma address)
   1609       1.1     skrll {
   1610       1.1     skrll   einfo (_("%X%H: dangerous relocation: %s\n"),
   1611       1.1     skrll 	 abfd, section, address, message);
   1612       1.1     skrll }
   1613       1.1     skrll 
   1614       1.6  christos /* This is called when a reloc is being generated attached to a symbol
   1615       1.1     skrll    that is not being output.  */
   1616       1.1     skrll 
   1617       1.1     skrll static void
   1618       1.1     skrll unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1619       1.1     skrll 		  const char *name,
   1620       1.1     skrll 		  bfd *abfd,
   1621       1.9  christos 		  asection *section,
   1622       1.1     skrll 		  bfd_vma address)
   1623       1.1     skrll {
   1624       1.1     skrll   einfo (_("%X%H: reloc refers to symbol `%pT' which is not being output\n"),
   1625       1.1     skrll 	 abfd, section, address, name);
   1626       1.1     skrll }
   1627       1.3  christos 
   1628       1.3  christos /* This is called if link_info.notice_all is set, or when a symbol in
   1629       1.3  christos    link_info.notice_hash is found.  Symbols are put in notice_hash
   1630       1.3  christos    using the -y option, while notice_all is set if the --cref option
   1631       1.1     skrll    has been supplied, or if there are any NOCROSSREFS sections in the
   1632      1.10  christos    linker script; and if plugins are active, since they need to monitor
   1633       1.1     skrll    all references from non-IR files.  */
   1634       1.3  christos 
   1635       1.5  christos static bool
   1636       1.1     skrll notice (struct bfd_link_info *info,
   1637       1.1     skrll 	struct bfd_link_hash_entry *h,
   1638       1.3  christos 	struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED,
   1639       1.5  christos 	bfd *abfd,
   1640       1.1     skrll 	asection *section,
   1641       1.3  christos 	bfd_vma value,
   1642       1.3  christos 	flagword flags ATTRIBUTE_UNUSED)
   1643       1.3  christos {
   1644       1.1     skrll   const char *name;
   1645       1.1     skrll 
   1646       1.3  christos   if (h == NULL)
   1647      1.10  christos     {
   1648       1.1     skrll       if (command_line.cref || nocrossref_list != NULL)
   1649       1.1     skrll 	return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value);
   1650       1.3  christos       return true;
   1651       1.3  christos     }
   1652      1.10  christos 
   1653       1.1     skrll   name = h->root.string;
   1654       1.1     skrll   if (info->notice_hash != NULL
   1655       1.8  christos       && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL)
   1656       1.1     skrll     {
   1657       1.8  christos       if (bfd_is_und_section (section))
   1658       1.1     skrll 	einfo (_("%P: %pB: reference to %s\n"), abfd, name);
   1659       1.1     skrll       else
   1660       1.1     skrll 	einfo (_("%P: %pB: definition of %s\n"), abfd, name);
   1661       1.1     skrll     }
   1662       1.1     skrll 
   1663      1.10  christos   if (command_line.cref || nocrossref_list != NULL)
   1664       1.1     skrll     add_cref (name, abfd, section, value);
   1665                     
   1666                       return true;
   1667                     }
   1668