Home | History | Annotate | Line # | Download | only in ld
      1   1.1     skrll /* Main program of GNU linker.
      2  1.13  christos    Copyright (C) 1991-2026 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.12  christos #include "bfdver.h"
     25   1.1     skrll #include "safe-ctype.h"
     26   1.1     skrll #include "libiberty.h"
     27   1.1     skrll #include "bfdlink.h"
     28   1.9  christos #include "ctf-api.h"
     29   1.1     skrll #include "filenames.h"
     30  1.10  christos #include "elf/common.h"
     31   1.1     skrll 
     32   1.1     skrll #include "ld.h"
     33   1.1     skrll #include "ldmain.h"
     34   1.1     skrll #include "ldmisc.h"
     35   1.1     skrll #include "ldwrite.h"
     36   1.1     skrll #include "ldexp.h"
     37   1.1     skrll #include "ldlang.h"
     38   1.1     skrll #include <ldgram.h>
     39   1.1     skrll #include "ldlex.h"
     40   1.1     skrll #include "ldfile.h"
     41   1.1     skrll #include "ldemul.h"
     42   1.1     skrll #include "ldctor.h"
     43   1.3  christos #include "plugin.h"
     44   1.1     skrll 
     45   1.1     skrll /* Somewhere above, sys/stat.h got included.  */
     46   1.1     skrll #if !defined(S_ISDIR) && defined(S_IFDIR)
     47   1.1     skrll #define	S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
     48   1.1     skrll #endif
     49   1.1     skrll 
     50   1.1     skrll #include <string.h>
     51   1.1     skrll 
     52  1.12  christos #if defined (HAVE_GETRUSAGE)
     53  1.12  christos #include <sys/resource.h>
     54  1.12  christos #endif
     55  1.12  christos 
     56  1.13  christos #if defined (HAVE_MALLINFO2) || defined (HAVE_MALLINFO)
     57  1.13  christos #include <malloc.h>
     58  1.13  christos #endif
     59  1.13  christos 
     60   1.1     skrll #ifndef TARGET_SYSTEM_ROOT
     61   1.1     skrll #define TARGET_SYSTEM_ROOT ""
     62   1.1     skrll #endif
     63   1.1     skrll 
     64   1.1     skrll /* EXPORTS */
     65   1.1     skrll 
     66   1.1     skrll FILE *saved_script_handle = NULL;
     67   1.1     skrll FILE *previous_script_handle = NULL;
     68  1.10  christos bool force_make_executable = false;
     69   1.1     skrll 
     70   1.1     skrll char *default_target;
     71   1.1     skrll const char *output_filename = "a.out";
     72   1.1     skrll 
     73   1.1     skrll /* Name this program was invoked by.  */
     74  1.13  christos const char *program_name;
     75   1.1     skrll 
     76   1.1     skrll /* The prefix for system library directories.  */
     77   1.1     skrll const char *ld_sysroot;
     78   1.1     skrll 
     79   1.1     skrll /* The canonical representation of ld_sysroot.  */
     80   1.6  christos char *ld_canon_sysroot;
     81   1.1     skrll int ld_canon_sysroot_len;
     82   1.1     skrll 
     83   1.5  christos /* Set by -G argument, for targets like MIPS ELF.  */
     84   1.1     skrll int g_switch_value = 8;
     85   1.1     skrll 
     86   1.1     skrll /* Nonzero means print names of input files as processed.  */
     87   1.9  christos unsigned int trace_files;
     88   1.1     skrll 
     89   1.4  christos /* Nonzero means report actions taken by the linker, and describe the linker script in use.  */
     90  1.10  christos bool verbose;
     91   1.1     skrll 
     92   1.1     skrll /* Nonzero means version number was printed, so exit successfully
     93   1.1     skrll    instead of complaining if no input files are given.  */
     94  1.10  christos bool version_printed;
     95   1.1     skrll 
     96   1.1     skrll /* TRUE if we should demangle symbol names.  */
     97  1.10  christos bool demangling;
     98   1.1     skrll 
     99  1.12  christos bool in_section_ordering;
    100  1.12  christos 
    101   1.1     skrll args_type command_line;
    102   1.1     skrll 
    103   1.1     skrll ld_config_type config;
    104   1.1     skrll 
    105   1.1     skrll sort_type sort_section;
    106   1.1     skrll 
    107   1.1     skrll static const char *get_sysroot
    108   1.1     skrll   (int, char **);
    109   1.1     skrll static char *get_emulation
    110   1.1     skrll   (int, char **);
    111  1.10  christos static bool add_archive_element
    112   1.3  christos   (struct bfd_link_info *, bfd *, const char *, bfd **);
    113   1.6  christos static void multiple_definition
    114   1.3  christos   (struct bfd_link_info *, struct bfd_link_hash_entry *,
    115   1.1     skrll    bfd *, asection *, bfd_vma);
    116   1.6  christos static void multiple_common
    117   1.3  christos   (struct bfd_link_info *, struct bfd_link_hash_entry *,
    118   1.3  christos    bfd *, enum bfd_link_hash_type, bfd_vma);
    119   1.6  christos static void add_to_set
    120   1.1     skrll   (struct bfd_link_info *, struct bfd_link_hash_entry *,
    121   1.1     skrll    bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
    122   1.6  christos static void constructor_callback
    123  1.10  christos   (struct bfd_link_info *, bool, const char *, bfd *,
    124   1.1     skrll    asection *, bfd_vma);
    125   1.6  christos static void warning_callback
    126   1.1     skrll   (struct bfd_link_info *, const char *, const char *, bfd *,
    127   1.1     skrll    asection *, bfd_vma);
    128   1.1     skrll static void warning_find_reloc
    129   1.1     skrll   (bfd *, asection *, void *);
    130   1.6  christos static void undefined_symbol
    131   1.1     skrll   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
    132  1.10  christos    bool);
    133   1.6  christos static void reloc_overflow
    134   1.1     skrll   (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
    135   1.1     skrll    const char *, bfd_vma, bfd *, asection *, bfd_vma);
    136   1.6  christos static void reloc_dangerous
    137   1.1     skrll   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
    138   1.6  christos static void unattached_reloc
    139   1.1     skrll   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
    140  1.10  christos static bool notice
    141   1.3  christos   (struct bfd_link_info *, struct bfd_link_hash_entry *,
    142   1.5  christos    struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword);
    143   1.1     skrll 
    144   1.1     skrll static struct bfd_link_callbacks link_callbacks =
    145   1.1     skrll {
    146   1.1     skrll   add_archive_element,
    147   1.1     skrll   multiple_definition,
    148   1.1     skrll   multiple_common,
    149   1.1     skrll   add_to_set,
    150   1.1     skrll   constructor_callback,
    151   1.1     skrll   warning_callback,
    152   1.1     skrll   undefined_symbol,
    153   1.1     skrll   reloc_overflow,
    154   1.1     skrll   reloc_dangerous,
    155   1.1     skrll   unattached_reloc,
    156   1.1     skrll   notice,
    157  1.12  christos   fatal,
    158   1.1     skrll   einfo,
    159   1.1     skrll   info_msg,
    160   1.1     skrll   minfo,
    161   1.9  christos   ldlang_override_segment_assignment,
    162  1.13  christos   ldlang_nearby_section,
    163  1.10  christos   ldlang_ctf_acquire_strings,
    164  1.10  christos   NULL,
    165  1.10  christos   ldlang_ctf_new_dynsym,
    166   1.9  christos   ldlang_write_ctf_late
    167   1.1     skrll };
    168   1.1     skrll 
    169   1.4  christos static bfd_assert_handler_type default_bfd_assert_handler;
    170   1.7  christos static bfd_error_handler_type default_bfd_error_handler;
    171   1.4  christos 
    172   1.1     skrll struct bfd_link_info link_info;
    173   1.1     skrll 
    174  1.10  christos struct dependency_file
    176  1.10  christos {
    177  1.10  christos   struct dependency_file *next;
    178  1.10  christos   char *name;
    179  1.10  christos };
    180  1.10  christos 
    181  1.10  christos static struct dependency_file *dependency_files, *dependency_files_tail;
    182  1.10  christos 
    183  1.10  christos void
    184  1.10  christos track_dependency_files (const char *filename)
    185  1.10  christos {
    186  1.10  christos   struct dependency_file *dep
    187  1.10  christos     = (struct dependency_file *) xmalloc (sizeof (*dep));
    188  1.10  christos   dep->name = xstrdup (filename);
    189  1.10  christos   dep->next = NULL;
    190  1.10  christos   if (dependency_files == NULL)
    191  1.10  christos     dependency_files = dep;
    192  1.10  christos   else
    193  1.10  christos     dependency_files_tail->next = dep;
    194  1.10  christos   dependency_files_tail = dep;
    195  1.10  christos }
    196  1.10  christos 
    197  1.10  christos static void
    198  1.10  christos write_dependency_file (void)
    199  1.10  christos {
    200  1.10  christos   FILE *out;
    201  1.10  christos   struct dependency_file *dep;
    202  1.10  christos 
    203  1.10  christos   out = fopen (config.dependency_file, FOPEN_WT);
    204  1.10  christos   if (out == NULL)
    205  1.12  christos     {
    206  1.12  christos       bfd_set_error (bfd_error_system_call);
    207  1.10  christos       fatal (_("%P: cannot open dependency file %s: %E\n"),
    208  1.10  christos 	     config.dependency_file);
    209  1.10  christos     }
    210  1.10  christos 
    211  1.10  christos   fprintf (out, "%s:", output_filename);
    212  1.10  christos 
    213  1.10  christos   for (dep = dependency_files; dep != NULL; dep = dep->next)
    214  1.10  christos     fprintf (out, " \\\n  %s", dep->name);
    215  1.10  christos 
    216  1.10  christos   fprintf (out, "\n");
    217  1.10  christos   for (dep = dependency_files; dep != NULL; dep = dep->next)
    218  1.10  christos     fprintf (out, "\n%s:\n", dep->name);
    219  1.10  christos 
    220  1.10  christos   fclose (out);
    221  1.10  christos }
    222   1.1     skrll 
    223   1.3  christos static void
    225  1.11  christos ld_cleanup (void)
    226  1.11  christos {
    227  1.11  christos   bfd *ibfd, *inext;
    228  1.11  christos   if (link_info.output_bfd)
    229  1.11  christos     bfd_close_all_done (link_info.output_bfd);
    230  1.11  christos   for (ibfd = link_info.input_bfds; ibfd; ibfd = inext)
    231  1.11  christos     {
    232  1.11  christos       inext = ibfd->link.next;
    233  1.12  christos       bfd_close_all_done (ibfd);
    234  1.12  christos     }
    235  1.12  christos   /* Note - we do not call ld_plugin_start (PHASE_PLUGINS) here as this
    236  1.12  christos      function is only called when the linker is exiting - ie after any
    237   1.3  christos      stats may have been reported, and potentially in the middle of a
    238   1.3  christos      phase where we have already started recording plugin stats.  */
    239   1.3  christos   plugin_call_cleanup ();
    240   1.1     skrll   if (output_filename && delete_output_file_on_failure)
    241   1.1     skrll     unlink_if_ordinary (output_filename);
    242   1.7  christos }
    243   1.4  christos 
    244   1.4  christos /* Hook to notice BFD assertions.  */
    245   1.4  christos 
    246   1.4  christos static void
    247   1.4  christos ld_bfd_assert_handler (const char *fmt, const char *bfdver,
    248  1.10  christos 		       const char *file, int line)
    249   1.4  christos {
    250   1.7  christos   config.make_executable = false;
    251   1.7  christos   (*default_bfd_assert_handler) (fmt, bfdver, file, line);
    252   1.7  christos }
    253   1.7  christos 
    254   1.7  christos /* Hook the bfd error/warning handler for --fatal-warnings.  */
    255   1.7  christos 
    256   1.7  christos static void
    257   1.7  christos ld_bfd_error_handler (const char *fmt, va_list ap)
    258  1.10  christos {
    259   1.7  christos   if (config.fatal_warnings)
    260   1.4  christos     config.make_executable = false;
    261   1.4  christos   (*default_bfd_error_handler) (fmt, ap);
    262  1.12  christos }
    263  1.12  christos 
    264  1.12  christos static void
    265  1.12  christos display_external_script (void)
    266  1.12  christos {
    267  1.12  christos   if (saved_script_handle == NULL)
    268  1.12  christos     return;
    269  1.12  christos 
    270  1.12  christos   static const int ld_bufsz = 8193;
    271  1.12  christos   size_t n;
    272  1.12  christos   char *buf = (char *) xmalloc (ld_bufsz);
    273  1.12  christos 
    274  1.12  christos   rewind (saved_script_handle);
    275  1.12  christos   while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
    276  1.12  christos     {
    277  1.12  christos       buf[n] = 0;
    278  1.12  christos       info_msg ("%s", buf);
    279  1.12  christos     }
    280  1.12  christos   rewind (saved_script_handle);
    281  1.12  christos   free (buf);
    282  1.12  christos }
    283  1.12  christos 
    284  1.12  christos struct ld_phase_data
    285  1.12  christos {
    286  1.12  christos   const char *    name;
    287  1.12  christos 
    288  1.12  christos   unsigned long   start;
    289  1.12  christos   unsigned long   duration;
    290  1.12  christos 
    291  1.12  christos   bool            started;
    292  1.12  christos   bool            broken;
    293  1.12  christos 
    294  1.12  christos #if defined (HAVE_GETRUSAGE)
    295  1.12  christos   struct rusage   begin;
    296  1.13  christos   struct rusage   use;
    297  1.13  christos #endif
    298  1.13  christos 
    299  1.13  christos #if defined (HAVE_MALLINFO2) || defined (HAVE_MALLINFO)
    300  1.13  christos   size_t          begin_blks;
    301  1.12  christos   size_t          used_blks;
    302  1.12  christos #endif
    303  1.12  christos };
    304  1.12  christos 
    305  1.12  christos static struct ld_phase_data phase_data [NUM_PHASES] =
    306  1.12  christos {
    307  1.12  christos   [PHASE_ALL]     = { .name = "ALL" },
    308  1.12  christos   [PHASE_CTF]     = { .name = "ctf processing" },
    309  1.12  christos   [PHASE_MERGE]   = { .name = "string merge" },
    310  1.12  christos   [PHASE_PARSE]   = { .name = "parsing" },
    311  1.12  christos   [PHASE_PLUGINS] = { .name = "plugins" },
    312  1.13  christos   [PHASE_PROCESS] = { .name = "processing files" },
    313  1.12  christos   [PHASE_WRITE]   = { .name = "write" },
    314  1.12  christos   [PHASE_DEBUG]   = { .name = "debug" }
    315  1.12  christos };
    316  1.13  christos 
    317  1.13  christos void
    318  1.13  christos ld_set_phase_name (ld_phase phase, const char * name)
    319  1.13  christos {
    320  1.13  christos   phase_data[phase].name = name ? name : "<unnamed>";
    321  1.13  christos }
    322  1.12  christos 
    323  1.12  christos void
    324  1.12  christos ld_start_phase (ld_phase phase)
    325  1.12  christos {
    326  1.12  christos   struct ld_phase_data * pd = phase_data + phase;
    327  1.12  christos 
    328  1.12  christos   /* We record data even if config.stats_file is NULL.  This allows
    329  1.12  christos      us to record data about phases that start before the command line
    330  1.12  christos      arguments have been parsed.  ie PHASE_ALL and PHASE_PARSE.  */
    331  1.12  christos 
    332  1.12  christos   /* Do not overwrite the fields if we have already started recording.  */
    333  1.12  christos   if (pd->started)
    334  1.12  christos     {
    335  1.12  christos       /* Since we do not queue phase starts and stops, if a phase is started
    336  1.12  christos 	 multiple times there is a likelyhood that it will be stopped multiple
    337  1.12  christos 	 times as well.  This is problematic as we will only record the data
    338  1.12  christos 	 for the first time the phase stops and ignore all of the other stops.
    339  1.12  christos 
    340  1.12  christos 	 So let the user know.  Ideally real users will never actually see
    341  1.12  christos 	 this message, and instead only developers who are adding new phase
    342  1.12  christos 	 tracking code will ever encounter it.  */
    343  1.12  christos       einfo ("%P: --stats: phase %s started twice - data may be unreliable\n",
    344  1.12  christos 	     pd->name);
    345  1.12  christos       return;
    346  1.12  christos     }
    347  1.12  christos 
    348  1.12  christos   /* It is OK if other phases are also active at this point.
    349  1.12  christos      It just means that the phases overlap or that one phase is a sub-task
    350  1.12  christos      of another.  Since we record resources on a per-phase basis, this
    351  1.12  christos      should not matter.  */
    352  1.12  christos 
    353  1.12  christos   pd->started = true;
    354  1.12  christos   pd->start = get_run_time ();
    355  1.12  christos 
    356  1.12  christos #if defined (HAVE_GETRUSAGE)
    357  1.12  christos   /* Record the resource usage at the start of the phase.  */
    358  1.12  christos   struct rusage usage;
    359  1.12  christos 
    360  1.12  christos   if (getrusage (RUSAGE_SELF, & usage) != 0)
    361  1.12  christos     /* FIXME: Complain ?  */
    362  1.12  christos     return;
    363  1.12  christos 
    364  1.13  christos   memcpy (& pd->begin, & usage, sizeof usage);
    365  1.13  christos #endif
    366  1.13  christos 
    367  1.13  christos #if defined (HAVE_MALLINFO2)
    368  1.13  christos   struct mallinfo2 mi2 = mallinfo2 ();
    369  1.13  christos   pd->begin_blks = mi2.uordblks;
    370  1.13  christos #elif defined (HAVE_MALLINFO)
    371  1.13  christos   struct mallinfo mi = mallinfo ();
    372  1.12  christos   pd->begin_blks = mi.uordblks;
    373  1.12  christos #endif
    374  1.12  christos }
    375  1.12  christos 
    376  1.12  christos void
    377  1.12  christos ld_stop_phase (ld_phase phase)
    378  1.12  christos {
    379  1.12  christos   struct ld_phase_data * pd = phase_data + phase;
    380  1.12  christos 
    381  1.13  christos   if (!pd->started)
    382  1.13  christos     {
    383  1.13  christos       /* It does not matter if the debug phase has not been started.  */
    384  1.13  christos       if (phase != PHASE_DEBUG)
    385  1.13  christos 	{
    386  1.13  christos 	  /* We set the broken flag to indicate that the data
    387  1.13  christos 	     recorded for this phase is inconsistent.  */
    388  1.13  christos 	  pd->broken = true;
    389  1.12  christos 	  return;
    390  1.12  christos 	}
    391  1.12  christos     }
    392  1.12  christos 
    393  1.12  christos   pd->duration += get_run_time () - pd->start;
    394  1.12  christos   pd->started = false;
    395  1.12  christos 
    396  1.12  christos #if defined (HAVE_GETRUSAGE)
    397  1.12  christos   struct rusage usage;
    398  1.12  christos 
    399  1.12  christos   if (getrusage (RUSAGE_SELF, & usage) != 0)
    400  1.12  christos     /* FIXME: Complain ?  */
    401  1.12  christos     return;
    402  1.12  christos 
    403  1.12  christos   if (phase == PHASE_ALL)
    404  1.12  christos     memcpy (& pd->use, & usage, sizeof usage);
    405  1.12  christos   else
    406  1.12  christos     {
    407  1.12  christos       struct timeval t;
    408  1.12  christos 
    409  1.12  christos       /* For sub-phases we record the increase in specific fields.  */
    410  1.12  christos       /* FIXME: Most rusage{} fields appear to be irrelevent to when considering
    411  1.12  christos 	 linker resource usage.  Currently we record maxrss and user and system
    412  1.12  christos 	 cpu times.  Are there any other fields that might be useful ?  */
    413  1.12  christos 
    414  1.12  christos #ifndef timeradd /* Macros copied from <sys/time.h>.  */
    415  1.12  christos #define timeradd(a, b, result)					\
    416  1.12  christos       do							\
    417  1.12  christos 	{							\
    418  1.12  christos 	  (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;		\
    419  1.12  christos 	  (result)->tv_usec = (a)->tv_usec + (b)->tv_usec;	\
    420  1.12  christos 	  if ((result)->tv_usec >= 1000000)			\
    421  1.12  christos 	    {							\
    422  1.12  christos 	      ++(result)->tv_sec;				\
    423  1.12  christos 	      (result)->tv_usec -= 1000000;			\
    424  1.12  christos 	    }							\
    425  1.12  christos 	}							\
    426  1.12  christos       while (0)
    427  1.12  christos #endif
    428  1.12  christos 
    429  1.12  christos #ifndef timersub
    430  1.12  christos #define timersub(a, b, result)					\
    431  1.12  christos       do							\
    432  1.12  christos 	{							\
    433  1.12  christos 	  (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;		\
    434  1.12  christos 	  (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;	\
    435  1.12  christos 	  if ((result)->tv_usec < 0)				\
    436  1.12  christos 	    {							\
    437  1.12  christos 	      --(result)->tv_sec;				\
    438  1.12  christos 	      (result)->tv_usec += 1000000;			\
    439  1.12  christos 	    }							\
    440  1.12  christos 	}							\
    441  1.12  christos       while (0)
    442  1.12  christos #endif
    443  1.12  christos 
    444  1.12  christos       timersub (& usage.ru_utime, & pd->begin.ru_utime, & t);
    445  1.12  christos       timeradd (& pd->use.ru_utime, &t, & pd->use.ru_utime);
    446  1.12  christos 
    447  1.12  christos       timersub (& usage.ru_stime, & pd->begin.ru_stime, & t);
    448  1.12  christos       timeradd (& pd->use.ru_stime, &t, & pd->use.ru_stime);
    449  1.12  christos 
    450  1.12  christos       if (pd->begin.ru_maxrss < usage.ru_maxrss)
    451  1.12  christos 	pd->use.ru_maxrss += usage.ru_maxrss - pd->begin.ru_maxrss;
    452  1.13  christos     }
    453  1.13  christos #endif
    454  1.13  christos 
    455  1.13  christos #if defined (HAVE_MALLINFO2)
    456  1.13  christos   /* FIXME: How do we know if mallinfo2() has failed ?  */
    457  1.13  christos   struct mallinfo2 mi2 = mallinfo2 ();
    458  1.13  christos   pd->used_blks += mi2.uordblks - pd->begin_blks;
    459  1.13  christos #elif defined (HAVE_MALLINFO)
    460  1.13  christos   struct mallinfo mi = mallinfo ();
    461  1.13  christos   pd->used_blks += mi.uordblks - pd->begin_blks;
    462  1.13  christos #endif
    463  1.13  christos 
    464  1.13  christos   if (phase == PHASE_DEBUG)
    465  1.13  christos     {
    466  1.13  christos       /* FIXME: Should we report other resources as well ?  */
    467  1.13  christos       /* FIXME: Can we integrate this code with report_phases() ?  */
    468  1.13  christos 
    469  1.13  christos       fprintf (stderr, "stats: %s: cpu time: %ld ", pd->name, pd->duration);
    470  1.13  christos #if defined (HAVE_GETRUSAGE)
    471  1.13  christos       fprintf (stderr, "rss: %ld ", pd->use.ru_maxrss);
    472  1.13  christos #endif
    473  1.13  christos #if defined (HAVE_MALLINFO2) || defined (HAVE_MALLINFO)
    474  1.13  christos       fprintf (stderr, "memory: %ld", (long) pd->used_blks);
    475  1.13  christos #endif
    476  1.13  christos       fprintf (stderr, "\n");
    477  1.13  christos 
    478  1.13  christos       /* Reset the counters to zero.  */
    479  1.12  christos       memset (((char *) pd) + sizeof (pd->name), 0, (sizeof (* pd)) - sizeof (pd->name));
    480  1.12  christos     }
    481  1.12  christos }
    482  1.12  christos 
    483  1.12  christos static void
    484  1.12  christos report_phases (FILE * file, time_t * start, char ** argv)
    485  1.12  christos {
    486  1.12  christos   unsigned long i;
    487  1.12  christos 
    488  1.12  christos   if (file == NULL)
    489  1.12  christos     return;
    490  1.12  christos 
    491  1.12  christos   /* We might be writing to stdout, so make sure
    492  1.12  christos      that we do not have any pending error output.  */
    493  1.12  christos   fflush (stderr);
    494  1.12  christos 
    495  1.12  christos   /* We do not translate "Stats" as we provide this as a key
    496  1.12  christos      word that can be searched for by grep and the like.  */
    497  1.12  christos #define STATS_PREFIX "Stats: "
    498  1.12  christos 
    499  1.12  christos   fprintf (file, STATS_PREFIX "linker version: %s\n", BFD_VERSION_STRING);
    500  1.12  christos 
    501  1.12  christos   /* No \n at the end of the string as ctime() provides its own.  */
    502  1.12  christos   fprintf (file, STATS_PREFIX "linker started: %s", ctime (start));
    503  1.12  christos 
    504  1.12  christos   /* We include the linker command line arguments since
    505  1.12  christos      they can be hard to track down by other means.  */
    506  1.12  christos   if (argv != NULL)
    507  1.12  christos     {
    508  1.12  christos       fprintf (file, STATS_PREFIX "args: ");
    509  1.12  christos       for (i = 0; argv[i] != NULL; i++)
    510  1.12  christos 	fprintf (file, "%s ", argv[i]);
    511  1.12  christos       fprintf (file, "\n\n");  /* Blank line to separate the args from the stats.  */
    512  1.12  christos     }
    513  1.12  christos 
    514  1.12  christos   /* All of this song and dance with the column_info struct and printf
    515  1.12  christos      formatting is so that we can have a nicely formated table with regular
    516  1.12  christos      column spacing, whilst allowing for the column headers to be translated,
    517  1.12  christos      and coping nicely with extra long strings or numbers.  */
    518  1.12  christos   struct column_info
    519  1.12  christos   {
    520  1.12  christos     const char * header;
    521  1.12  christos     const char * sub_header;
    522  1.12  christos     int          width;
    523  1.12  christos     int          pad;
    524  1.12  christos   } columns[] =
    525  1.12  christos #define COLUMNS_FIELD(HEADER,SUBHEADER) \
    526  1.12  christos     { .header = N_( HEADER ), .sub_header = N_( SUBHEADER ) },
    527  1.12  christos   {
    528  1.12  christos     COLUMNS_FIELD ("phase", "name")
    529  1.12  christos     COLUMNS_FIELD ("cpu time", "(microsec)")
    530  1.12  christos #if defined (HAVE_GETRUSAGE)
    531  1.13  christos     /* Note: keep these columns in sync with the
    532  1.12  christos        information recorded in ld_stop_phase().  */
    533  1.12  christos     COLUMNS_FIELD ("rss", "(KiB)")
    534  1.12  christos     COLUMNS_FIELD ("user time", "(seconds)")
    535  1.13  christos     COLUMNS_FIELD ("system time", "(seconds)")
    536  1.13  christos #endif
    537  1.13  christos #if defined (HAVE_MALLINFO2) || defined (HAVE_MALLINFO)
    538  1.12  christos     COLUMNS_FIELD ("memory", "(KiB)")
    539  1.12  christos #endif
    540  1.12  christos   };
    541  1.12  christos 
    542  1.12  christos #ifndef max
    543  1.12  christos #define max(A,B) ((A) < (B) ? (B) : (A))
    544  1.12  christos #endif
    545  1.12  christos 
    546  1.13  christos   size_t maxwidth = 1;
    547  1.13  christos   for (i = 0; i < NUM_PHASES; i++)
    548  1.13  christos     {
    549  1.13  christos       struct ld_phase_data * pd = phase_data + i;
    550  1.13  christos 
    551  1.13  christos       if (pd->name != NULL)
    552  1.12  christos 	maxwidth = max (maxwidth, strlen (pd->name));
    553  1.12  christos     }
    554  1.12  christos 
    555  1.12  christos   fprintf (file, "%s", STATS_PREFIX);
    556  1.12  christos 
    557  1.12  christos   for (i = 0; i < ARRAY_SIZE (columns); i++)
    558  1.12  christos     {
    559  1.12  christos       int padding;
    560  1.12  christos 
    561  1.12  christos       if (i == 0)
    562  1.12  christos 	columns[i].width = fprintf (file, "%-*s", (int) maxwidth, columns[i].header);
    563  1.12  christos       else
    564  1.12  christos 	columns[i].width = fprintf (file, "%s", columns[i].header);
    565  1.12  christos       padding = columns[i].width % 8;
    566  1.12  christos       if (padding < 4)
    567  1.12  christos 	padding = 4;
    568  1.12  christos       columns[i].pad = fprintf (file, "%*c", padding, ' ');
    569  1.12  christos     }
    570  1.12  christos 
    571  1.12  christos   fprintf (file, "\n");
    572  1.12  christos 
    573  1.12  christos   int bias = 0;
    574  1.12  christos #define COLUMN_ENTRY(VAL, FORMAT, N)					\
    575  1.12  christos   do									\
    576  1.12  christos     {									\
    577  1.12  christos       int l;								\
    578  1.12  christos       									\
    579  1.12  christos       if (N == 0) 							\
    580  1.12  christos 	l = fprintf (file, "%-*" FORMAT, columns[N].width, VAL);	\
    581  1.12  christos       else								\
    582  1.12  christos 	l = fprintf (file, "%*" FORMAT, columns[N].width - bias, VAL);	\
    583  1.12  christos       bias = 0;								\
    584  1.12  christos       if (l < columns[N].width)						\
    585  1.12  christos 	l = columns[N].pad;						\
    586  1.12  christos       else if (l < columns[N].width + columns[N].pad)			\
    587  1.12  christos 	l = columns[N].pad - (l - columns[N].width);			\
    588  1.12  christos       else								\
    589  1.12  christos 	{								\
    590  1.12  christos 	  bias = l - (columns[N].width + columns[N].pad);		\
    591  1.12  christos 	  l = 0;							\
    592  1.12  christos 	}								\
    593  1.12  christos       if (l)								\
    594  1.12  christos 	fprintf (file, "%*c", l, ' ');					\
    595  1.12  christos     }									\
    596  1.12  christos   while (0)
    597  1.12  christos 
    598  1.12  christos   fprintf (file, "%s", STATS_PREFIX);
    599  1.12  christos 
    600  1.12  christos   for (i = 0; i < ARRAY_SIZE (columns); i++)
    601  1.12  christos     COLUMN_ENTRY (columns[i].sub_header, "s", i);
    602  1.12  christos 
    603  1.12  christos   fprintf (file, "\n");
    604  1.12  christos 
    605  1.12  christos   for (i = 0; i < NUM_PHASES; i++)
    606  1.12  christos     {
    607  1.12  christos       struct ld_phase_data * pd = phase_data + i;
    608  1.12  christos       /* This should not be needed...  */
    609  1.13  christos       const char * name = pd->name ? pd->name : "<unnamed>";
    610  1.13  christos 
    611  1.13  christos       if (i == PHASE_DEBUG)
    612  1.12  christos 	continue;
    613  1.12  christos 
    614  1.12  christos       if (pd->broken)
    615  1.12  christos 	{
    616  1.12  christos 	  fprintf (file, "%s %s: %s",
    617  1.12  christos 		   STATS_PREFIX, name, _("WARNING: Data is unreliable!\n"));
    618  1.12  christos 	  continue;
    619  1.12  christos 	}
    620  1.12  christos 
    621  1.13  christos       fprintf (file, "%s", STATS_PREFIX);
    622  1.12  christos 
    623  1.12  christos       /* Care must be taken to keep the numbers below in sync with
    624  1.12  christos 	 entries in the columns_info array.
    625  1.12  christos 	 FIXME: There ought to be a better way to do this...  */
    626  1.12  christos       COLUMN_ENTRY (name, "s", 0);
    627  1.12  christos       COLUMN_ENTRY (pd->duration, "ld", 1);
    628  1.12  christos #if defined (HAVE_GETRUSAGE)
    629  1.12  christos       COLUMN_ENTRY (pd->use.ru_maxrss, "ld", 2);
    630  1.12  christos       COLUMN_ENTRY ((int64_t) pd->use.ru_utime.tv_sec, PRId64, 3);
    631  1.13  christos       COLUMN_ENTRY ((int64_t) pd->use.ru_stime.tv_sec, PRId64, 4);
    632  1.13  christos #endif
    633  1.13  christos #if defined (HAVE_MALLINFO2) || defined (HAVE_MALLINFO)
    634  1.12  christos       COLUMN_ENTRY ((int64_t) pd->used_blks / 1024, PRId64, 5);
    635  1.12  christos #endif
    636  1.12  christos       fprintf (file, "\n");
    637  1.12  christos     }
    638  1.12  christos 
    639  1.12  christos   fflush (file);
    640  1.13  christos }
    641  1.13  christos 
    642  1.13  christos static void
    643  1.13  christos set_program_name (const char *argv0)
    644  1.13  christos {
    645  1.13  christos   program_name = argv0;
    646  1.13  christos 
    647  1.13  christos #if defined (__linux__) && _POSIX_VERSION >= 200112L
    648  1.13  christos   char name[PATH_MAX];
    649  1.13  christos   ssize_t len = readlink ("/proc/self/exe", name, ARRAY_SIZE (name));
    650  1.13  christos   if (len > 0 && (size_t)len < ARRAY_SIZE (name))
    651  1.13  christos     program_name = xmemdup (name, len, len + 1);
    652  1.13  christos #endif
    653   1.1     skrll }
    654   1.1     skrll 
    655   1.1     skrll int
    656   1.1     skrll main (int argc, char **argv)
    657   1.1     skrll {
    658  1.12  christos   char *emulation;
    659   1.1     skrll   long start_time = get_run_time ();
    660  1.10  christos   time_t start_seconds = time (NULL);
    661   1.1     skrll 
    662   1.1     skrll #ifdef HAVE_LC_MESSAGES
    663   1.1     skrll   setlocale (LC_MESSAGES, "");
    664   1.1     skrll #endif
    665   1.1     skrll   setlocale (LC_CTYPE, "");
    666   1.1     skrll   bindtextdomain (PACKAGE, LOCALEDIR);
    667  1.13  christos   textdomain (PACKAGE);
    668   1.1     skrll 
    669   1.1     skrll   set_program_name (argv[0]);
    670  1.12  christos   xmalloc_set_program_name (program_name);
    671  1.12  christos 
    672  1.12  christos   /* Check the LD_STATS environment variable before parsing the command line
    673  1.12  christos      so that the --stats option, if used, can override the environment variable.  */
    674  1.12  christos   char * stats_filename;
    675  1.12  christos   if ((stats_filename = getenv ("LD_STATS")) != NULL)
    676  1.12  christos     {
    677  1.12  christos       if (ISPRINT (stats_filename[0]))
    678  1.12  christos 	config.stats_filename = stats_filename;
    679  1.12  christos       else
    680  1.12  christos 	config.stats_filename = "-";
    681  1.12  christos       config.stats = true;
    682  1.12  christos     }
    683  1.12  christos 
    684  1.12  christos   ld_start_phase (PHASE_ALL);
    685   1.1     skrll   ld_start_phase (PHASE_PARSE);
    686  1.12  christos 
    687   1.1     skrll   expandargv (&argc, &argv);
    688   1.9  christos   char ** saved_argv = dupargv (argv);
    689  1.12  christos 
    690   1.1     skrll   if (bfd_init () != BFD_INIT_MAGIC)
    691   1.1     skrll     fatal (_("%P: fatal error: libbfd ABI mismatch\n"));
    692   1.1     skrll 
    693   1.4  christos   bfd_set_error_program_name (program_name);
    694   1.4  christos 
    695   1.4  christos   /* We want to notice and fail on those nasty BFD assertions which are
    696   1.4  christos      likely to signal incorrect output being generated but otherwise may
    697   1.4  christos      leave no trace.  */
    698   1.7  christos   default_bfd_assert_handler = bfd_set_assert_handler (ld_bfd_assert_handler);
    699   1.7  christos 
    700   1.7  christos   /* Also hook the bfd error/warning handler for --fatal-warnings.  */
    701   1.3  christos   default_bfd_error_handler = bfd_set_error_handler (ld_bfd_error_handler);
    702   1.1     skrll 
    703  1.12  christos   xatexit (ld_cleanup);
    704  1.12  christos 
    705  1.12  christos   /* Remove temporary object-only files.  */
    706   1.1     skrll   xatexit (cmdline_remove_object_only_files);
    707   1.1     skrll 
    708   1.1     skrll   /* Set up the sysroot directory.  */
    709   1.5  christos   ld_sysroot = get_sysroot (argc, argv);
    710   1.1     skrll   if (*ld_sysroot)
    711   1.8  christos     ld_canon_sysroot = lrealpath (ld_sysroot);
    712   1.8  christos   if (ld_canon_sysroot)
    713   1.8  christos     {
    714   1.8  christos       ld_canon_sysroot_len = strlen (ld_canon_sysroot);
    715   1.8  christos 
    716   1.8  christos       /* is_sysrooted_pathname() relies on no trailing dirsep.  */
    717  1.10  christos       if (ld_canon_sysroot_len > 0
    718   1.8  christos 	  && IS_DIR_SEPARATOR (ld_canon_sysroot [ld_canon_sysroot_len - 1]))
    719   1.1     skrll 	ld_canon_sysroot [--ld_canon_sysroot_len] = '\0';
    720   1.1     skrll     }
    721   1.1     skrll   else
    722   1.1     skrll     ld_canon_sysroot_len = -1;
    723   1.1     skrll 
    724   1.1     skrll   /* Set the default BFD target based on the configured target.  Doing
    725   1.1     skrll      this permits the linker to be configured for a particular target,
    726   1.6  christos      and linked against a shared BFD library which was configured for
    727   1.1     skrll      a different target.  The macro TARGET is defined by Makefile.  */
    728   1.1     skrll   if (!bfd_set_default_target (TARGET))
    729   1.1     skrll     {
    730   1.1     skrll       einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
    731   1.1     skrll       xexit (1);
    732   1.1     skrll     }
    733   1.1     skrll 
    734   1.1     skrll #if YYDEBUG
    735   1.1     skrll   {
    736   1.1     skrll     extern int yydebug;
    737   1.1     skrll     yydebug = 1;
    738   1.1     skrll   }
    739  1.10  christos #endif
    740   1.1     skrll 
    741   1.1     skrll   config.build_constructors = true;
    742   1.1     skrll   config.rpath_separator = ':';
    743  1.10  christos   config.split_by_reloc = (unsigned) -1;
    744  1.10  christos   config.split_by_file = (bfd_size_type) -1;
    745  1.10  christos   config.make_executable = true;
    746  1.10  christos   config.magic_demand_paged = true;
    747   1.5  christos   config.text_read_only = true;
    748   1.1     skrll   config.print_map_discarded = true;
    749  1.10  christos   link_info.disable_target_specific_optimizations = -1;
    750  1.10  christos 
    751   1.3  christos   command_line.warn_mismatch = true;
    752   1.1     skrll   command_line.warn_search_mismatch = true;
    753   1.1     skrll   command_line.check_section_addresses = -1;
    754   1.1     skrll 
    755   1.1     skrll   /* We initialize DEMANGLING based on the environment variable
    756   1.1     skrll      COLLECT_NO_DEMANGLE.  The gcc collect2 program will demangle the
    757   1.1     skrll      output of the linker, unless COLLECT_NO_DEMANGLE is set in the
    758   1.1     skrll      environment.  Acting the same way here lets us provide the same
    759   1.1     skrll      interface by default.  */
    760  1.10  christos   demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
    761  1.10  christos 
    762  1.10  christos   link_info.allow_undefined_version = true;
    763  1.10  christos   link_info.keep_memory = true;
    764  1.10  christos   link_info.max_cache_size = (bfd_size_type) -1;
    765  1.10  christos   link_info.combreloc = true;
    766  1.10  christos   link_info.strip_discarded = true;
    767   1.7  christos   link_info.prohibit_multiple_definition_absolute = false;
    768   1.7  christos   link_info.textrel_check = DEFAULT_LD_TEXTREL_CHECK;
    769   1.1     skrll   link_info.emit_hash = DEFAULT_EMIT_SYSV_HASH;
    770   1.1     skrll   link_info.emit_gnu_hash = DEFAULT_EMIT_GNU_HASH;
    771   1.1     skrll   link_info.callbacks = &link_callbacks;
    772   1.1     skrll   link_info.input_bfds_tail = &link_info.input_bfds;
    773   1.1     skrll   /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
    774   1.1     skrll      and _fini symbols.  We are compatible.  */
    775   1.1     skrll   link_info.init_function = "_init";
    776   1.5  christos   link_info.fini_function = "_fini";
    777   1.6  christos   link_info.relax_pass = 1;
    778  1.10  christos   link_info.extern_protected_data = -1;
    779   1.1     skrll   link_info.dynamic_undefined_weak = -1;
    780   1.1     skrll   link_info.indirect_extern_access = -1;
    781   1.1     skrll   link_info.pei386_auto_import = -1;
    782   1.5  christos   link_info.spare_dynamic_tags = 5;
    783  1.11  christos   link_info.path_separator = ':';
    784   1.5  christos #ifdef DEFAULT_FLAG_COMPRESS_DEBUG
    785   1.7  christos   config.compress_debug = DEFAULT_COMPRESSED_DEBUG_ALGORITHM;
    786   1.7  christos #endif
    787   1.7  christos #ifdef DEFAULT_NEW_DTAGS
    788  1.10  christos   link_info.new_dtags = DEFAULT_NEW_DTAGS;
    789  1.10  christos #endif
    790   1.1     skrll   link_info.start_stop_gc = false;
    791   1.1     skrll   link_info.start_stop_visibility = STV_PROTECTED;
    792   1.1     skrll 
    793   1.1     skrll   ldfile_add_arch ("");
    794   1.1     skrll   emulation = get_emulation (argc, argv);
    795  1.12  christos   ldemul_choose_mode (emulation);
    796  1.12  christos   default_target = ldemul_choose_target (argc, argv);
    797   1.1     skrll   lang_init (false);
    798  1.10  christos   ldexp_init (false);
    799   1.1     skrll   ldemul_before_parse ();
    800   1.1     skrll   lang_has_input_file = false;
    801   1.1     skrll   parse_args (argc, argv);
    802   1.1     skrll 
    803   1.1     skrll   if (config.hash_table_size != 0)
    804  1.12  christos     bfd_hash_set_default_size (config.hash_table_size);
    805  1.12  christos 
    806  1.12  christos   ld_stop_phase (PHASE_PARSE);
    807   1.4  christos 
    808   1.4  christos   ld_start_phase (PHASE_PLUGINS);
    809  1.12  christos   /* Now all the plugin arguments have been gathered, we can load them.  */
    810   1.4  christos   plugin_load_plugins ();
    811  1.12  christos   ld_stop_phase (PHASE_PLUGINS);
    812  1.12  christos 
    813   1.1     skrll   ld_start_phase (PHASE_PARSE);
    814   1.1     skrll 
    815   1.1     skrll   ldemul_set_symbols ();
    816   1.1     skrll 
    817   1.1     skrll   /* If we have not already opened and parsed a linker script,
    818   1.1     skrll      try the default script from command line first.  */
    819   1.1     skrll   if (saved_script_handle == NULL
    820   1.9  christos       && command_line.default_script != NULL)
    821   1.1     skrll     {
    822   1.1     skrll       ldfile_open_script_file (command_line.default_script);
    823   1.1     skrll       parser_input = input_script;
    824   1.1     skrll       yyparse ();
    825   1.1     skrll     }
    826   1.1     skrll 
    827   1.1     skrll   /* If we have not already opened and parsed a linker script
    828   1.1     skrll      read the emulation's appropriate default script.  */
    829   1.1     skrll   if (saved_script_handle == NULL)
    830   1.1     skrll     {
    831   1.1     skrll       int isfile;
    832   1.1     skrll       char *s = ldemul_get_script (&isfile);
    833   1.3  christos 
    834   1.1     skrll       if (isfile)
    835   1.1     skrll 	ldfile_open_default_command_file (s);
    836   1.1     skrll       else
    837   1.4  christos 	{
    838   1.1     skrll 	  lex_string = s;
    839   1.1     skrll 	  lex_redirect (s, _("built in linker script"), 1);
    840   1.1     skrll 	}
    841   1.1     skrll       parser_input = input_script;
    842   1.1     skrll       yyparse ();
    843   1.1     skrll       lex_string = NULL;
    844   1.4  christos     }
    845   1.1     skrll 
    846   1.1     skrll   if (verbose)
    847  1.12  christos     {
    848   1.1     skrll       if (saved_script_handle)
    849   1.1     skrll 	info_msg (_("using external linker script: %s"), processed_scripts->name);
    850   1.1     skrll       else
    851   1.1     skrll 	info_msg (_("using internal linker script:"));
    852   1.1     skrll       info_msg ("\n==================================================\n");
    853  1.12  christos 
    854   1.1     skrll       if (saved_script_handle)
    855   1.1     skrll 	display_external_script ();
    856   1.1     skrll       else
    857   1.1     skrll 	{
    858   1.1     skrll 	  int isfile;
    859   1.1     skrll 
    860   1.1     skrll 	  info_msg (ldemul_get_script (&isfile));
    861   1.1     skrll 	}
    862   1.1     skrll 
    863   1.1     skrll       info_msg ("\n==================================================\n");
    864  1.12  christos     }
    865  1.12  christos 
    866  1.12  christos   if (command_line.section_ordering_file)
    867  1.12  christos     {
    868  1.12  christos       FILE *hold_script_handle;
    869  1.12  christos 
    870  1.12  christos       hold_script_handle = saved_script_handle;
    871  1.12  christos       ldfile_open_command_file (command_line.section_ordering_file);
    872  1.12  christos       if (verbose)
    873  1.12  christos 	display_external_script ();
    874  1.12  christos       saved_script_handle = hold_script_handle;
    875  1.12  christos       in_section_ordering = true;
    876  1.12  christos       parser_input = input_section_ordering_script;
    877  1.12  christos       yyparse ();
    878  1.12  christos       in_section_ordering = false;
    879  1.12  christos 
    880   1.7  christos     }
    881   1.7  christos 
    882  1.10  christos   if (command_line.force_group_allocation
    883   1.7  christos       || !bfd_link_relocatable (&link_info))
    884  1.10  christos     link_info.resolve_section_groups = true;
    885   1.7  christos   else
    886   1.4  christos     link_info.resolve_section_groups = false;
    887   1.4  christos 
    888   1.4  christos   if (command_line.print_output_format)
    889   1.1     skrll     info_msg ("%s\n", lang_get_output_target ());
    890   1.1     skrll 
    891   1.5  christos   lang_final ();
    892   1.5  christos 
    893   1.5  christos   /* If the only command line argument has been -v or --version or --verbose
    894   1.5  christos      then ignore any input files provided by linker scripts and exit now.
    895   1.5  christos      We do not want to create an output file when the linker is just invoked
    896   1.5  christos      to provide version information.  */
    897   1.5  christos   if (argc == 2 && version_printed)
    898   1.7  christos     xexit (0);
    899  1.12  christos 
    900   1.7  christos   if (link_info.inhibit_common_definition && !bfd_link_dll (&link_info))
    901   1.1     skrll     fatal (_("%P: --no-define-common may not be used without -shared\n"));
    902   1.1     skrll 
    903   1.4  christos   if (!lang_has_input_file)
    904   1.1     skrll     {
    905  1.12  christos       if (version_printed || command_line.print_output_format)
    906  1.12  christos 	xexit (0);
    907   1.1     skrll       output_unknown_cmdline_warnings ();
    908   1.1     skrll       fatal (_("%P: no input files\n"));
    909   1.9  christos     }
    910   1.1     skrll 
    911   1.1     skrll   if (verbose)
    912   1.1     skrll     info_msg (_("%P: mode %s\n"), emulation);
    913   1.1     skrll 
    914  1.12  christos   ldemul_after_parse ();
    915  1.12  christos 
    916   1.1     skrll   output_unknown_cmdline_warnings ();
    917   1.1     skrll 
    918   1.1     skrll   if (config.map_filename)
    919   1.1     skrll     {
    920   1.1     skrll       if (strcmp (config.map_filename, "-") == 0)
    921   1.1     skrll 	{
    922   1.1     skrll 	  config.map_file = stdout;
    923   1.1     skrll 	}
    924   1.1     skrll       else
    925   1.1     skrll 	{
    926   1.1     skrll 	  config.map_file = fopen (config.map_filename, FOPEN_WT);
    927   1.1     skrll 	  if (config.map_file == (FILE *) NULL)
    928  1.12  christos 	    {
    929   1.1     skrll 	      bfd_set_error (bfd_error_system_call);
    930   1.1     skrll 	      einfo (_("%P: cannot open map file %s: %E\n"),
    931   1.1     skrll 		     config.map_filename);
    932  1.10  christos 	    }
    933   1.1     skrll 	}
    934   1.1     skrll       link_info.has_map_file = true;
    935  1.12  christos     }
    936  1.12  christos 
    937  1.12  christos   if (config.stats_filename != NULL)
    938  1.12  christos     {
    939  1.12  christos       if (config.map_filename != NULL
    940  1.12  christos 	  && strcmp (config.stats_filename, config.map_filename) == 0)
    941  1.12  christos 	config.stats_file = NULL;
    942  1.12  christos       else if (strcmp (config.stats_filename, "-") == 0)
    943  1.12  christos 	config.stats_file = stdout;
    944  1.12  christos       else
    945  1.12  christos 	{
    946  1.12  christos 	  if (config.stats_filename[0] == '+')
    947  1.12  christos 	    config.stats_file = fopen (config.stats_filename + 1, "a");
    948  1.12  christos 	  else
    949  1.12  christos 	    config.stats_file = fopen (config.stats_filename, "w");
    950  1.12  christos 
    951  1.12  christos 	  if (config.stats_file == NULL)
    952  1.12  christos 	    einfo ("%P: Warning: failed to open resource record file: %s\n",
    953  1.12  christos 		   config.stats_filename);
    954  1.12  christos 	}
    955  1.12  christos     }
    956  1.12  christos 
    957  1.12  christos   ld_stop_phase (PHASE_PARSE);
    958   1.1     skrll 
    959  1.12  christos   ld_start_phase (PHASE_PROCESS);
    960   1.1     skrll   lang_process ();
    961   1.1     skrll   ld_stop_phase (PHASE_PROCESS);
    962   1.1     skrll 
    963   1.5  christos   /* Print error messages for any missing symbols, for any warning
    964   1.1     skrll      symbols, and possibly multiple definitions.  */
    965   1.1     skrll   if (bfd_link_relocatable (&link_info))
    966   1.1     skrll     link_info.output_bfd->flags &= ~EXEC_P;
    967   1.1     skrll   else
    968  1.11  christos     link_info.output_bfd->flags |= EXEC_P;
    969  1.11  christos 
    970   1.5  christos   flagword flags = 0;
    971  1.11  christos   switch (config.compress_debug)
    972  1.11  christos     {
    973  1.11  christos     case COMPRESS_DEBUG_GNU_ZLIB:
    974  1.11  christos       flags = BFD_COMPRESS;
    975  1.11  christos       break;
    976  1.11  christos     case COMPRESS_DEBUG_GABI_ZLIB:
    977  1.11  christos       flags = BFD_COMPRESS | BFD_COMPRESS_GABI;
    978  1.11  christos       break;
    979  1.11  christos     case COMPRESS_DEBUG_ZSTD:
    980  1.11  christos       flags = BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
    981  1.11  christos       break;
    982   1.5  christos     default:
    983  1.11  christos       break;
    984  1.11  christos     }
    985   1.5  christos   link_info.output_bfd->flags
    986  1.12  christos     |= flags & bfd_applicable_file_flags (link_info.output_bfd);
    987  1.12  christos 
    988   1.1     skrll 
    989  1.12  christos   ld_start_phase (PHASE_WRITE);
    990  1.12  christos   ldwrite ();
    991   1.1     skrll   ld_stop_phase (PHASE_WRITE);
    992   1.1     skrll 
    993   1.1     skrll 
    994   1.1     skrll   if (config.map_file != NULL)
    995   1.1     skrll     lang_map ();
    996   1.1     skrll   if (command_line.cref)
    997   1.1     skrll     output_cref (config.map_file != NULL ? config.map_file : stdout);
    998   1.5  christos   if (nocrossref_list != NULL)
    999   1.5  christos     check_nocrossrefs ();
   1000   1.5  christos   if (command_line.print_memory_usage)
   1001   1.5  christos     lang_print_memory_usage ();
   1002   1.6  christos #if 0
   1003   1.1     skrll   {
   1004   1.5  christos     struct bfd_link_hash_entry *h;
   1005   1.5  christos 
   1006   1.5  christos     h = bfd_link_hash_lookup (link_info.hash, "__image_base__", 0,0,1);
   1007   1.5  christos     fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
   1008  1.12  christos   }
   1009   1.1     skrll #endif
   1010   1.1     skrll   ldexp_finish (false);
   1011  1.10  christos   lang_finish ();
   1012  1.10  christos 
   1013  1.10  christos   if (config.dependency_file != NULL)
   1014   1.1     skrll     write_dependency_file ();
   1015   1.1     skrll 
   1016   1.1     skrll   /* Even if we're producing relocatable output, some non-fatal errors should
   1017   1.1     skrll      be reported in the exit status.  (What non-fatal errors, if any, do we
   1018   1.1     skrll      want to ignore for relocatable output?)  */
   1019   1.9  christos   if (!config.make_executable && !force_make_executable)
   1020   1.1     skrll     {
   1021   1.1     skrll       if (verbose)
   1022   1.1     skrll 	einfo (_("%P: link errors found, deleting executable `%s'\n"),
   1023   1.4  christos 	       output_filename);
   1024   1.1     skrll 
   1025   1.1     skrll       /* The file will be removed by ld_cleanup.  */
   1026   1.1     skrll       xexit (1);
   1027   1.1     skrll     }
   1028  1.11  christos   else
   1029  1.11  christos     {
   1030  1.11  christos       bfd *obfd = link_info.output_bfd;
   1031  1.12  christos       link_info.output_bfd = NULL;
   1032  1.12  christos       if (!bfd_close (obfd))
   1033  1.12  christos 	fatal (_("%P: %s: final close failed: %E\n"), output_filename);
   1034   1.1     skrll 
   1035   1.1     skrll       link_info.output_bfd = NULL;
   1036   1.1     skrll 
   1037   1.1     skrll       /* If the --force-exe-suffix is enabled, and we're making an
   1038   1.5  christos 	 executable file and it doesn't end in .exe, copy it to one
   1039   1.5  christos 	 which does.  */
   1040   1.1     skrll       if (!bfd_link_relocatable (&link_info)
   1041   1.1     skrll 	  && command_line.force_exe_suffix)
   1042   1.1     skrll 	{
   1043   1.1     skrll 	  int len = strlen (output_filename);
   1044   1.1     skrll 
   1045   1.1     skrll 	  if (len < 4
   1046   1.1     skrll 	      || (strcasecmp (output_filename + len - 4, ".exe") != 0
   1047   1.1     skrll 		  && strcasecmp (output_filename + len - 4, ".dll") != 0))
   1048   1.1     skrll 	    {
   1049   1.1     skrll 	      FILE *src;
   1050   1.3  christos 	      FILE *dst;
   1051   1.1     skrll 	      const int bsize = 4096;
   1052   1.3  christos 	      char *buf = (char *) xmalloc (bsize);
   1053   1.1     skrll 	      int l;
   1054   1.1     skrll 	      char *dst_name = (char *) xmalloc (len + 5);
   1055   1.1     skrll 
   1056   1.1     skrll 	      strcpy (dst_name, output_filename);
   1057   1.1     skrll 	      strcat (dst_name, ".exe");
   1058   1.1     skrll 	      src = fopen (output_filename, FOPEN_RB);
   1059   1.1     skrll 	      dst = fopen (dst_name, FOPEN_WB);
   1060  1.12  christos 
   1061   1.1     skrll 	      if (!src)
   1062   1.1     skrll 		fatal (_("%P: unable to open for source of copy `%s'\n"),
   1063  1.12  christos 		       output_filename);
   1064   1.1     skrll 	      if (!dst)
   1065   1.1     skrll 		fatal (_("%P: unable to open for destination of copy `%s'\n"),
   1066   1.1     skrll 		       dst_name);
   1067   1.1     skrll 	      while ((l = fread (buf, 1, bsize, src)) > 0)
   1068   1.1     skrll 		{
   1069   1.1     skrll 		  int done = fwrite (buf, 1, l, dst);
   1070   1.8  christos 
   1071   1.1     skrll 		  if (done != l)
   1072   1.1     skrll 		    einfo (_("%P: error writing file `%s'\n"), dst_name);
   1073   1.1     skrll 		}
   1074   1.1     skrll 
   1075   1.8  christos 	      fclose (src);
   1076   1.1     skrll 	      if (fclose (dst) == EOF)
   1077   1.1     skrll 		einfo (_("%P: error closing file `%s'\n"), dst_name);
   1078   1.1     skrll 	      free (dst_name);
   1079   1.1     skrll 	      free (buf);
   1080   1.1     skrll 	    }
   1081   1.1     skrll 	}
   1082  1.12  christos     }
   1083  1.12  christos 
   1084  1.12  christos   if (config.emit_gnu_object_only)
   1085  1.12  christos     cmdline_emit_object_only_section ();
   1086  1.12  christos 
   1087   1.1     skrll   ld_stop_phase (PHASE_ALL);
   1088   1.1     skrll 
   1089  1.12  christos   if (config.stats)
   1090  1.12  christos     {
   1091  1.12  christos       report_phases (config.map_file, & start_seconds, saved_argv);
   1092  1.12  christos 
   1093  1.12  christos       if (config.stats_filename)
   1094  1.12  christos 	{
   1095  1.12  christos 	  report_phases (config.stats_file, & start_seconds, saved_argv);
   1096  1.12  christos 
   1097  1.12  christos 	  if (config.stats_file != stdout && config.stats_file != stderr)
   1098  1.12  christos 	    {
   1099  1.12  christos 	      fclose (config.stats_file);
   1100  1.12  christos 	      config.stats_file = NULL;
   1101  1.12  christos 	    }
   1102  1.12  christos 	}
   1103  1.12  christos       else /* This is for backwards compatibility.  */
   1104   1.1     skrll 	{
   1105  1.12  christos 	  long run_time = get_run_time () - start_time;
   1106  1.12  christos 
   1107  1.12  christos 	  fflush (stdout);
   1108  1.12  christos 	  fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
   1109  1.12  christos 		   program_name, run_time / 1000000, run_time % 1000000);
   1110   1.1     skrll 	  fflush (stderr);
   1111   1.1     skrll 	}
   1112  1.11  christos     }
   1113   1.1     skrll 
   1114   1.1     skrll   /* Prevent ld_cleanup from deleting the output file.  */
   1115  1.12  christos   output_filename = NULL;
   1116  1.12  christos 
   1117   1.1     skrll   freeargv (saved_argv);
   1118   1.1     skrll 
   1119   1.1     skrll   xexit (0);
   1120   1.1     skrll   return 0;
   1121   1.1     skrll }
   1122   1.1     skrll 
   1123   1.1     skrll /* If the configured sysroot is relocatable, try relocating it based on
   1124   1.1     skrll    default prefix FROM.  Return the relocated directory if it exists,
   1125   1.1     skrll    otherwise return null.  */
   1126   1.1     skrll 
   1127   1.1     skrll static char *
   1128   1.1     skrll get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
   1129   1.1     skrll {
   1130   1.1     skrll #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
   1131   1.1     skrll   char *path;
   1132   1.1     skrll   struct stat s;
   1133   1.1     skrll 
   1134   1.1     skrll   path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
   1135   1.1     skrll   if (path)
   1136   1.1     skrll     {
   1137   1.1     skrll       if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
   1138   1.1     skrll 	return path;
   1139   1.1     skrll       free (path);
   1140   1.1     skrll     }
   1141   1.1     skrll #endif
   1142   1.1     skrll   return 0;
   1143   1.1     skrll }
   1144   1.1     skrll 
   1145   1.1     skrll /* Return the sysroot directory.  Return "" if no sysroot is being used.  */
   1146   1.1     skrll 
   1147   1.1     skrll static const char *
   1148   1.1     skrll get_sysroot (int argc, char **argv)
   1149   1.9  christos {
   1150   1.1     skrll   int i;
   1151   1.1     skrll   const char *path = NULL;
   1152  1.10  christos 
   1153   1.9  christos   for (i = 1; i < argc; i++)
   1154   1.9  christos     if (startswith (argv[i], "--sysroot="))
   1155   1.9  christos       path = argv[i] + strlen ("--sysroot=");
   1156   1.9  christos 
   1157   1.9  christos   if (!path)
   1158   1.9  christos     path = get_relative_sysroot (BINDIR);
   1159   1.9  christos 
   1160   1.1     skrll   if (!path)
   1161   1.9  christos     path = get_relative_sysroot (TOOLBINDIR);
   1162   1.9  christos 
   1163   1.1     skrll   if (!path)
   1164   1.9  christos     path = TARGET_SYSTEM_ROOT;
   1165   1.9  christos 
   1166   1.1     skrll   if (IS_DIR_SEPARATOR (*path) && path[1] == 0)
   1167   1.9  christos     path = "";
   1168   1.1     skrll 
   1169   1.1     skrll   return path;
   1170   1.1     skrll }
   1171   1.1     skrll 
   1172   1.1     skrll /* We need to find any explicitly given emulation in order to initialize the
   1173   1.1     skrll    state that's needed by the lex&yacc argument parser (parse_args).  */
   1174   1.1     skrll 
   1175   1.1     skrll static char *
   1176   1.1     skrll get_emulation (int argc, char **argv)
   1177   1.1     skrll {
   1178   1.1     skrll   char *emulation;
   1179   1.1     skrll   int i;
   1180   1.1     skrll 
   1181   1.1     skrll   emulation = getenv (EMULATION_ENVIRON);
   1182   1.1     skrll   if (emulation == NULL)
   1183   1.1     skrll     emulation = DEFAULT_EMULATION;
   1184   1.1     skrll 
   1185  1.10  christos   for (i = 1; i < argc; i++)
   1186   1.1     skrll     {
   1187   1.1     skrll       if (startswith (argv[i], "-m"))
   1188   1.1     skrll 	{
   1189   1.1     skrll 	  if (argv[i][2] == '\0')
   1190   1.1     skrll 	    {
   1191   1.1     skrll 	      /* -m EMUL */
   1192   1.1     skrll 	      if (i < argc - 1)
   1193   1.1     skrll 		{
   1194   1.1     skrll 		  emulation = argv[i + 1];
   1195   1.1     skrll 		  i++;
   1196  1.12  christos 		}
   1197   1.1     skrll 	      else
   1198   1.1     skrll 		fatal (_("%P: missing argument to -m\n"));
   1199   1.1     skrll 	    }
   1200   1.1     skrll 	  else if (strcmp (argv[i], "-mips1") == 0
   1201   1.1     skrll 		   || strcmp (argv[i], "-mips2") == 0
   1202   1.1     skrll 		   || strcmp (argv[i], "-mips3") == 0
   1203   1.1     skrll 		   || strcmp (argv[i], "-mips4") == 0
   1204   1.1     skrll 		   || strcmp (argv[i], "-mips5") == 0
   1205  1.10  christos 		   || strcmp (argv[i], "-mips32") == 0
   1206  1.10  christos 		   || strcmp (argv[i], "-mips32r2") == 0
   1207   1.5  christos 		   || strcmp (argv[i], "-mips32r3") == 0
   1208   1.1     skrll 		   || strcmp (argv[i], "-mips32r5") == 0
   1209   1.5  christos 		   || strcmp (argv[i], "-mips32r6") == 0
   1210  1.10  christos 		   || strcmp (argv[i], "-mips64") == 0
   1211  1.10  christos 		   || strcmp (argv[i], "-mips64r2") == 0
   1212   1.5  christos 		   || strcmp (argv[i], "-mips64r3") == 0
   1213   1.1     skrll 		   || strcmp (argv[i], "-mips64r5") == 0
   1214   1.1     skrll 		   || strcmp (argv[i], "-mips64r6") == 0)
   1215   1.1     skrll 	    {
   1216   1.1     skrll 	      /* FIXME: The arguments -mips1, -mips2, -mips3, etc. are
   1217   1.1     skrll 		 passed to the linker by some MIPS compilers.  They
   1218   1.1     skrll 		 generally tell the linker to use a slightly different
   1219   1.1     skrll 		 library path.  Perhaps someday these should be
   1220   1.1     skrll 		 implemented as emulations; until then, we just ignore
   1221   1.1     skrll 		 the arguments and hope that nobody ever creates
   1222   1.1     skrll 		 emulations named ips1, ips2 or ips3.  */
   1223   1.1     skrll 	    }
   1224   1.1     skrll 	  else if (strcmp (argv[i], "-m486") == 0)
   1225   1.1     skrll 	    {
   1226   1.1     skrll 	      /* FIXME: The argument -m486 is passed to the linker on
   1227   1.1     skrll 		 some Linux systems.  Hope that nobody creates an
   1228   1.1     skrll 		 emulation named 486.  */
   1229   1.1     skrll 	    }
   1230   1.1     skrll 	  else
   1231   1.1     skrll 	    {
   1232   1.1     skrll 	      /* -mEMUL */
   1233   1.1     skrll 	      emulation = &argv[i][2];
   1234   1.1     skrll 	    }
   1235   1.1     skrll 	}
   1236   1.1     skrll     }
   1237   1.1     skrll 
   1238   1.1     skrll   return emulation;
   1239   1.1     skrll }
   1240   1.1     skrll 
   1241   1.1     skrll void
   1242   1.1     skrll add_ysym (const char *name)
   1243   1.1     skrll {
   1244   1.6  christos   if (link_info.notice_hash == NULL)
   1245   1.6  christos     {
   1246   1.1     skrll       link_info.notice_hash
   1247   1.1     skrll 	= (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
   1248   1.1     skrll       if (!bfd_hash_table_init_n (link_info.notice_hash,
   1249   1.1     skrll 				  bfd_hash_newfunc,
   1250  1.12  christos 				  sizeof (struct bfd_hash_entry),
   1251   1.1     skrll 				  61))
   1252   1.1     skrll 	fatal (_("%P: bfd_hash_table_init failed: %E\n"));
   1253  1.10  christos     }
   1254  1.12  christos 
   1255   1.1     skrll   if (bfd_hash_lookup (link_info.notice_hash, name, true, true) == NULL)
   1256   1.1     skrll     fatal (_("%P: bfd_hash_lookup failed: %E\n"));
   1257   1.2     skrll }
   1258   1.5  christos 
   1259   1.2     skrll void
   1260   1.5  christos add_ignoresym (struct bfd_link_info *info, const char *name)
   1261   1.2     skrll {
   1262   1.5  christos   if (info->ignore_hash == NULL)
   1263   1.6  christos     {
   1264   1.6  christos       info->ignore_hash = xmalloc (sizeof (struct bfd_hash_table));
   1265   1.6  christos       if (!bfd_hash_table_init_n (info->ignore_hash,
   1266   1.6  christos 				  bfd_hash_newfunc,
   1267  1.12  christos 				  sizeof (struct bfd_hash_entry),
   1268   1.2     skrll 				  61))
   1269   1.2     skrll 	fatal (_("%P: bfd_hash_table_init failed: %E\n"));
   1270  1.10  christos     }
   1271  1.12  christos 
   1272   1.2     skrll   if (bfd_hash_lookup (info->ignore_hash, name, true, true) == NULL)
   1273   1.2     skrll     fatal (_("%P: bfd_hash_lookup failed: %E\n"));
   1274   1.1     skrll }
   1275   1.1     skrll 
   1276   1.1     skrll /* Record a symbol to be wrapped, from the --wrap option.  */
   1277   1.1     skrll 
   1278   1.1     skrll void
   1279   1.1     skrll add_wrap (const char *name)
   1280   1.1     skrll {
   1281   1.6  christos   if (link_info.wrap_hash == NULL)
   1282   1.6  christos     {
   1283   1.1     skrll       link_info.wrap_hash
   1284   1.1     skrll 	= (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
   1285   1.1     skrll       if (!bfd_hash_table_init_n (link_info.wrap_hash,
   1286   1.1     skrll 				  bfd_hash_newfunc,
   1287  1.12  christos 				  sizeof (struct bfd_hash_entry),
   1288   1.1     skrll 				  61))
   1289   1.1     skrll 	fatal (_("%P: bfd_hash_table_init failed: %E\n"));
   1290  1.10  christos     }
   1291  1.12  christos 
   1292   1.1     skrll   if (bfd_hash_lookup (link_info.wrap_hash, name, true, true) == NULL)
   1293   1.1     skrll     fatal (_("%P: bfd_hash_lookup failed: %E\n"));
   1294   1.1     skrll }
   1295   1.1     skrll 
   1296   1.1     skrll /* Handle the -retain-symbols-file option.  */
   1297   1.1     skrll 
   1298   1.1     skrll void
   1299   1.1     skrll add_keepsyms_file (const char *filename)
   1300   1.1     skrll {
   1301   1.1     skrll   FILE *file;
   1302   1.1     skrll   char *buf;
   1303   1.1     skrll   size_t bufsize;
   1304   1.1     skrll   int c;
   1305   1.1     skrll 
   1306   1.1     skrll   if (link_info.strip == strip_some)
   1307   1.1     skrll     einfo (_("%X%P: error: duplicate retain-symbols-file\n"));
   1308   1.1     skrll 
   1309   1.1     skrll   file = fopen (filename, "r");
   1310   1.1     skrll   if (file == NULL)
   1311   1.1     skrll     {
   1312   1.1     skrll       bfd_set_error (bfd_error_system_call);
   1313   1.1     skrll       einfo ("%X%P: %s: %E\n", filename);
   1314   1.1     skrll       return;
   1315   1.3  christos     }
   1316   1.3  christos 
   1317   1.1     skrll   link_info.keep_hash = (struct bfd_hash_table *)
   1318   1.1     skrll       xmalloc (sizeof (struct bfd_hash_table));
   1319  1.12  christos   if (!bfd_hash_table_init (link_info.keep_hash, bfd_hash_newfunc,
   1320   1.1     skrll 			    sizeof (struct bfd_hash_entry)))
   1321   1.1     skrll     fatal (_("%P: bfd_hash_table_init failed: %E\n"));
   1322   1.3  christos 
   1323   1.1     skrll   bufsize = 100;
   1324   1.1     skrll   buf = (char *) xmalloc (bufsize);
   1325   1.1     skrll 
   1326   1.1     skrll   c = getc (file);
   1327   1.1     skrll   while (c != EOF)
   1328   1.1     skrll     {
   1329   1.1     skrll       while (ISSPACE (c))
   1330   1.1     skrll 	c = getc (file);
   1331   1.1     skrll 
   1332   1.1     skrll       if (c != EOF)
   1333   1.1     skrll 	{
   1334   1.6  christos 	  size_t len = 0;
   1335   1.1     skrll 
   1336   1.1     skrll 	  while (!ISSPACE (c) && c != EOF)
   1337   1.1     skrll 	    {
   1338   1.1     skrll 	      buf[len] = c;
   1339   1.1     skrll 	      ++len;
   1340   1.1     skrll 	      if (len >= bufsize)
   1341   1.3  christos 		{
   1342   1.1     skrll 		  bufsize *= 2;
   1343   1.1     skrll 		  buf = (char *) xrealloc (buf, bufsize);
   1344   1.1     skrll 		}
   1345   1.1     skrll 	      c = getc (file);
   1346   1.1     skrll 	    }
   1347   1.1     skrll 
   1348  1.10  christos 	  buf[len] = '\0';
   1349  1.12  christos 
   1350   1.1     skrll 	  if (bfd_hash_lookup (link_info.keep_hash, buf, true, true) == NULL)
   1351   1.1     skrll 	    fatal (_("%P: bfd_hash_lookup for insertion failed: %E\n"));
   1352   1.1     skrll 	}
   1353   1.1     skrll     }
   1354   1.1     skrll 
   1355   1.1     skrll   if (link_info.strip != strip_none)
   1356   1.1     skrll     einfo (_("%P: `-retain-symbols-file' overrides `-s' and `-S'\n"));
   1357   1.1     skrll 
   1358   1.5  christos   free (buf);
   1359   1.1     skrll   link_info.strip = strip_some;
   1360   1.1     skrll   fclose (file);
   1361   1.1     skrll }
   1362   1.1     skrll 
   1363   1.1     skrll /* Callbacks from the BFD linker routines.  */
   1365   1.1     skrll 
   1366  1.10  christos /* This is called when BFD has decided to include an archive member in
   1367   1.1     skrll    a link.  */
   1368   1.1     skrll 
   1369   1.3  christos static bool
   1370   1.3  christos add_archive_element (struct bfd_link_info *info,
   1371   1.1     skrll 		     bfd *abfd,
   1372   1.1     skrll 		     const char *name,
   1373   1.8  christos 		     bfd **subsbfd ATTRIBUTE_UNUSED)
   1374   1.3  christos {
   1375   1.1     skrll   lang_input_statement_type *input;
   1376   1.3  christos   lang_input_statement_type *parent;
   1377   1.3  christos   lang_input_statement_type orig_input;
   1378   1.7  christos 
   1379  1.10  christos   input = (lang_input_statement_type *)
   1380  1.10  christos       xcalloc (1, sizeof (lang_input_statement_type));
   1381   1.1     skrll   input->header.type = lang_input_statement_enum;
   1382   1.1     skrll   input->filename = bfd_get_filename (abfd);
   1383   1.3  christos   input->local_sym_name = bfd_get_filename (abfd);
   1384   1.3  christos   input->the_bfd = abfd;
   1385   1.3  christos 
   1386   1.3  christos   /* Save the original data for trace files/tries below, as plugins
   1387  1.12  christos      (if enabled) may possibly alter it to point to a replacement
   1388  1.12  christos      BFD, but we still want to output the original BFD filename.  */
   1389  1.12  christos   orig_input = *input;
   1390  1.12  christos   /* Don't claim a fat IR object if no IR object should be claimed.  */
   1391   1.3  christos   if (link_info.lto_plugin_active
   1392  1.12  christos       && (!no_more_claiming
   1393   1.3  christos 	  || bfd_get_lto_type (abfd) != lto_fat_ir_object))
   1394   1.5  christos     {
   1395  1.12  christos       ld_start_phase (PHASE_PLUGINS);
   1396  1.12  christos       /* We must offer this archive member to the plugins to claim.  */
   1397   1.5  christos       plugin_maybe_claim (input);
   1398   1.5  christos       ld_stop_phase (PHASE_PLUGINS);
   1399   1.6  christos 
   1400   1.6  christos       if (input->flags.claimed)
   1401   1.6  christos 	{
   1402   1.6  christos 	  if (no_more_claiming)
   1403   1.9  christos 	    {
   1404  1.10  christos 	      /* Don't claim new IR symbols after all IR symbols have
   1405   1.6  christos 		 been claimed.  */
   1406   1.6  christos 	      if (verbose)
   1407  1.10  christos 		info_msg ("%pI: no new IR symbols to claim\n",
   1408   1.6  christos 			  &orig_input);
   1409  1.10  christos 	      input->flags.claimed = 0;
   1410   1.5  christos 	      return false;
   1411   1.3  christos 	    }
   1412   1.3  christos 	  input->flags.claim_archive = true;
   1413  1.12  christos 	  *subsbfd = input->the_bfd;
   1414  1.12  christos 	}
   1415  1.10  christos     }
   1416  1.10  christos   else
   1417  1.10  christos     cmdline_check_object_only_section (input->the_bfd, false);
   1418  1.10  christos 
   1419  1.10  christos   if (link_info.input_bfds_tail == &input->the_bfd->link.next
   1420  1.10  christos       || input->the_bfd->link.next != NULL)
   1421  1.10  christos     {
   1422  1.10  christos       /* We have already loaded this element, and are attempting to
   1423  1.10  christos 	 load it again.  This can happen when the archive map doesn't
   1424  1.10  christos 	 match actual symbols defined by the element.  */
   1425  1.10  christos       free (input);
   1426  1.10  christos       bfd_set_error (bfd_error_malformed_archive);
   1427  1.10  christos       return false;
   1428  1.10  christos     }
   1429  1.10  christos 
   1430  1.10  christos   /* Set the file_chain pointer of archives to the last element loaded
   1431  1.10  christos      from the archive.  See ldlang.c:find_rescan_insertion.  */
   1432   1.3  christos   parent = bfd_usrdata (abfd->my_archive);
   1433   1.1     skrll   if (parent != NULL && !parent->flags.reload)
   1434   1.1     skrll     parent->next = input;
   1435   1.1     skrll 
   1436   1.1     skrll   ldlang_add_file (input);
   1437  1.10  christos 
   1438   1.1     skrll   if (config.map_file != NULL)
   1439   1.1     skrll     {
   1440   1.1     skrll       static bool header_printed;
   1441   1.1     skrll       struct bfd_link_hash_entry *h;
   1442  1.10  christos       bfd *from;
   1443   1.9  christos       int len;
   1444   1.9  christos 
   1445  1.10  christos       h = bfd_link_hash_lookup (info->hash, name, false, false, true);
   1446  1.10  christos       if (h == NULL
   1447   1.1     skrll 	  && info->pei386_auto_import
   1448   1.1     skrll 	  && startswith (name, "__imp_"))
   1449   1.1     skrll 	h = bfd_link_hash_lookup (info->hash, name + 6, false, false, true);
   1450   1.1     skrll 
   1451   1.1     skrll       if (h == NULL)
   1452   1.1     skrll 	from = NULL;
   1453   1.1     skrll       else
   1454   1.1     skrll 	{
   1455   1.1     skrll 	  switch (h->type)
   1456   1.1     skrll 	    {
   1457   1.1     skrll 	    default:
   1458   1.1     skrll 	      from = NULL;
   1459   1.1     skrll 	      break;
   1460   1.1     skrll 
   1461   1.1     skrll 	    case bfd_link_hash_defined:
   1462   1.1     skrll 	    case bfd_link_hash_defweak:
   1463   1.1     skrll 	      from = h->u.def.section->owner;
   1464   1.1     skrll 	      break;
   1465   1.1     skrll 
   1466   1.1     skrll 	    case bfd_link_hash_undefined:
   1467   1.1     skrll 	    case bfd_link_hash_undefweak:
   1468   1.1     skrll 	      from = h->u.undef.abfd;
   1469   1.1     skrll 	      break;
   1470   1.1     skrll 
   1471   1.1     skrll 	    case bfd_link_hash_common:
   1472   1.1     skrll 	      from = h->u.c.p->section->owner;
   1473   1.1     skrll 	      break;
   1474   1.6  christos 	    }
   1475   1.1     skrll 	}
   1476   1.7  christos 
   1477  1.10  christos       if (!header_printed)
   1478   1.1     skrll 	{
   1479   1.1     skrll 	  minfo (_("Archive member included to satisfy reference by file (symbol)\n\n"));
   1480   1.6  christos 	  header_printed = true;
   1481   1.6  christos 	}
   1482   1.1     skrll 
   1483   1.1     skrll       if (abfd->my_archive == NULL
   1484   1.1     skrll 	  || bfd_is_thin_archive (abfd->my_archive))
   1485   1.1     skrll 	{
   1486   1.1     skrll 	  minfo ("%s", bfd_get_filename (abfd));
   1487   1.1     skrll 	  len = strlen (bfd_get_filename (abfd));
   1488   1.6  christos 	}
   1489   1.1     skrll       else
   1490   1.6  christos 	{
   1491   1.1     skrll 	  minfo ("%s(%s)", bfd_get_filename (abfd->my_archive),
   1492   1.1     skrll 		 bfd_get_filename (abfd));
   1493   1.1     skrll 	  len = (strlen (bfd_get_filename (abfd->my_archive))
   1494   1.1     skrll 		 + strlen (bfd_get_filename (abfd))
   1495   1.1     skrll 		 + 2);
   1496   1.1     skrll 	}
   1497   1.1     skrll 
   1498   1.1     skrll       if (len >= 29)
   1499   1.1     skrll 	{
   1500  1.11  christos 	  print_nl ();
   1501   1.1     skrll 	  len = 0;
   1502   1.1     skrll 	}
   1503   1.8  christos       print_spaces (30 - len);
   1504   1.1     skrll 
   1505   1.8  christos       if (from != NULL)
   1506   1.1     skrll 	minfo ("%pB ", from);
   1507   1.1     skrll       if (h != NULL)
   1508   1.1     skrll 	minfo ("(%pT)\n", h->root.string);
   1509   1.1     skrll       else
   1510   1.9  christos 	minfo ("(%s)\n", name);
   1511   1.9  christos     }
   1512   1.9  christos 
   1513   1.8  christos   if (verbose
   1514  1.10  christos       || trace_files > 1
   1515   1.1     skrll       || (trace_files && bfd_is_thin_archive (orig_input.the_bfd->my_archive)))
   1516   1.1     skrll     info_msg ("%pI\n", &orig_input);
   1517   1.1     skrll   return true;
   1518   1.1     skrll }
   1519   1.1     skrll 
   1520   1.6  christos /* This is called when BFD has discovered a symbol which is defined
   1521   1.3  christos    multiple times.  */
   1522   1.3  christos 
   1523   1.1     skrll static void
   1524   1.1     skrll multiple_definition (struct bfd_link_info *info,
   1525   1.1     skrll 		     struct bfd_link_hash_entry *h,
   1526   1.1     skrll 		     bfd *nbfd,
   1527   1.3  christos 		     asection *nsec,
   1528   1.3  christos 		     bfd_vma nval)
   1529   1.3  christos {
   1530   1.3  christos   const char *name;
   1531   1.3  christos   bfd *obfd;
   1532   1.3  christos   asection *osec;
   1533   1.6  christos   bfd_vma oval;
   1534   1.3  christos 
   1535   1.3  christos   if (info->allow_multiple_definition)
   1536   1.3  christos     return;
   1537   1.3  christos 
   1538   1.3  christos   switch (h->type)
   1539   1.3  christos     {
   1540   1.3  christos     case bfd_link_hash_defined:
   1541   1.3  christos       osec = h->u.def.section;
   1542   1.3  christos       oval = h->u.def.value;
   1543   1.3  christos       obfd = h->u.def.section->owner;
   1544   1.3  christos       break;
   1545   1.3  christos     case bfd_link_hash_indirect:
   1546   1.3  christos       osec = bfd_ind_section_ptr;
   1547   1.3  christos       oval = 0;
   1548   1.3  christos       obfd = NULL;
   1549   1.3  christos       break;
   1550   1.3  christos     default:
   1551   1.3  christos       abort ();
   1552   1.3  christos     }
   1553   1.3  christos 
   1554   1.3  christos   /* Ignore a redefinition of an absolute symbol to the
   1555   1.3  christos      same value; it's harmless.  */
   1556   1.3  christos   if (h->type == bfd_link_hash_defined
   1557   1.6  christos       && bfd_is_abs_section (osec)
   1558   1.3  christos       && bfd_is_abs_section (nsec)
   1559   1.1     skrll       && nval == oval)
   1560   1.1     skrll     return;
   1561   1.1     skrll 
   1562   1.1     skrll   /* If either section has the output_section field set to
   1563   1.1     skrll      bfd_abs_section_ptr, it means that the section is being
   1564   1.8  christos      discarded, and this is not really a multiple definition at all.
   1565   1.8  christos      FIXME: It would be cleaner to somehow ignore symbols defined in
   1566   1.8  christos      sections which are being discarded.  */
   1567   1.8  christos   if (!info->prohibit_multiple_definition_absolute
   1568   1.8  christos       && ((osec->output_section != NULL
   1569   1.8  christos 	   && ! bfd_is_abs_section (osec)
   1570   1.8  christos 	   && bfd_is_abs_section (osec->output_section))
   1571   1.6  christos 	  || (nsec->output_section != NULL
   1572   1.1     skrll 	      && !bfd_is_abs_section (nsec)
   1573   1.3  christos 	      && bfd_is_abs_section (nsec->output_section))))
   1574   1.3  christos     return;
   1575   1.3  christos 
   1576   1.3  christos   name = h->root.string;
   1577   1.3  christos   if (nbfd == NULL)
   1578   1.3  christos     {
   1579   1.3  christos       nbfd = obfd;
   1580   1.3  christos       nsec = osec;
   1581  1.10  christos       nval = oval;
   1582  1.10  christos       obfd = NULL;
   1583  1.10  christos     }
   1584  1.10  christos   if (info->warn_multiple_definition)
   1585  1.10  christos     einfo (_("%P: %C: warning: multiple definition of `%pT'"),
   1586  1.10  christos 	   nbfd, nsec, nval, name);
   1587   1.1     skrll   else
   1588   1.8  christos     einfo (_("%X%P: %C: multiple definition of `%pT'"),
   1589   1.8  christos 	   nbfd, nsec, nval, name);
   1590   1.1     skrll   if (obfd != NULL)
   1591   1.5  christos     einfo (_("; %D: first defined here"), obfd, osec, oval);
   1592   1.1     skrll   einfo ("\n");
   1593   1.8  christos 
   1594   1.5  christos   if (RELAXATION_ENABLED_BY_USER)
   1595   1.1     skrll     {
   1596   1.1     skrll       einfo (_("%P: disabling relaxation; it will not work with multiple definitions\n"));
   1597   1.1     skrll       DISABLE_RELAXATION;
   1598   1.1     skrll     }
   1599   1.1     skrll }
   1600   1.1     skrll 
   1601   1.1     skrll /* This is called when there is a definition of a common symbol, or
   1602   1.1     skrll    when a common symbol is found for a symbol that is already defined,
   1603   1.6  christos    or when two common symbols are found.  We only do something if
   1604   1.1     skrll    -warn-common was used.  */
   1605   1.3  christos 
   1606   1.1     skrll static void
   1607   1.1     skrll multiple_common (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1608   1.1     skrll 		 struct bfd_link_hash_entry *h,
   1609   1.1     skrll 		 bfd *nbfd,
   1610   1.3  christos 		 enum bfd_link_hash_type ntype,
   1611   1.3  christos 		 bfd_vma nsize)
   1612   1.3  christos {
   1613   1.3  christos   const char *name;
   1614   1.3  christos   bfd *obfd;
   1615   1.3  christos   enum bfd_link_hash_type otype;
   1616   1.6  christos   bfd_vma osize;
   1617   1.1     skrll 
   1618   1.3  christos   if (!config.warn_common)
   1619   1.3  christos     return;
   1620   1.3  christos 
   1621   1.3  christos   name = h->root.string;
   1622   1.3  christos   otype = h->type;
   1623   1.3  christos   if (otype == bfd_link_hash_common)
   1624   1.3  christos     {
   1625   1.3  christos       obfd = h->u.c.p->section->owner;
   1626   1.3  christos       osize = h->u.c.size;
   1627   1.3  christos     }
   1628   1.3  christos   else if (otype == bfd_link_hash_defined
   1629   1.3  christos 	   || otype == bfd_link_hash_defweak)
   1630   1.3  christos     {
   1631   1.3  christos       obfd = h->u.def.section->owner;
   1632   1.3  christos       osize = 0;
   1633   1.3  christos     }
   1634   1.3  christos   else
   1635   1.3  christos     {
   1636   1.3  christos       /* FIXME: It would nice if we could report the BFD which defined
   1637   1.3  christos 	 an indirect symbol, but we don't have anywhere to store the
   1638   1.3  christos 	 information.  */
   1639   1.3  christos       obfd = NULL;
   1640   1.1     skrll       osize = 0;
   1641   1.1     skrll     }
   1642   1.1     skrll 
   1643   1.1     skrll   if (ntype == bfd_link_hash_defined
   1644   1.1     skrll       || ntype == bfd_link_hash_defweak
   1645   1.1     skrll       || ntype == bfd_link_hash_indirect)
   1646   1.8  christos     {
   1647   1.8  christos       ASSERT (otype == bfd_link_hash_common);
   1648   1.8  christos       if (obfd != NULL)
   1649   1.8  christos 	einfo (_("%P: %pB: warning: definition of `%pT' overriding common"
   1650   1.8  christos 		 " from %pB\n"),
   1651   1.8  christos 	       nbfd, name, obfd);
   1652   1.1     skrll       else
   1653   1.1     skrll 	einfo (_("%P: %pB: warning: definition of `%pT' overriding common\n"),
   1654   1.1     skrll 	       nbfd, name);
   1655   1.1     skrll     }
   1656   1.1     skrll   else if (otype == bfd_link_hash_defined
   1657   1.1     skrll 	   || otype == bfd_link_hash_defweak
   1658   1.1     skrll 	   || otype == bfd_link_hash_indirect)
   1659   1.8  christos     {
   1660   1.8  christos       ASSERT (ntype == bfd_link_hash_common);
   1661   1.8  christos       if (obfd != NULL)
   1662   1.8  christos 	einfo (_("%P: %pB: warning: common of `%pT' overridden by definition"
   1663   1.8  christos 		 " from %pB\n"),
   1664   1.8  christos 	       nbfd, name, obfd);
   1665   1.1     skrll       else
   1666   1.1     skrll 	einfo (_("%P: %pB: warning: common of `%pT' overridden by definition\n"),
   1667   1.1     skrll 	       nbfd, name);
   1668   1.1     skrll     }
   1669   1.1     skrll   else
   1670   1.1     skrll     {
   1671   1.1     skrll       ASSERT (otype == bfd_link_hash_common && ntype == bfd_link_hash_common);
   1672   1.8  christos       if (osize > nsize)
   1673   1.8  christos 	{
   1674   1.8  christos 	  if (obfd != NULL)
   1675   1.8  christos 	    einfo (_("%P: %pB: warning: common of `%pT' overridden"
   1676   1.8  christos 		     " by larger common from %pB\n"),
   1677   1.8  christos 		   nbfd, name, obfd);
   1678   1.8  christos 	  else
   1679   1.1     skrll 	    einfo (_("%P: %pB: warning: common of `%pT' overridden"
   1680   1.1     skrll 		     " by larger common\n"),
   1681   1.1     skrll 		   nbfd, name);
   1682   1.1     skrll 	}
   1683   1.8  christos       else if (nsize > osize)
   1684   1.8  christos 	{
   1685   1.8  christos 	  if (obfd != NULL)
   1686   1.8  christos 	    einfo (_("%P: %pB: warning: common of `%pT' overriding"
   1687   1.8  christos 		     " smaller common from %pB\n"),
   1688   1.8  christos 		   nbfd, name, obfd);
   1689   1.8  christos 	  else
   1690   1.1     skrll 	    einfo (_("%P: %pB: warning: common of `%pT' overriding"
   1691   1.1     skrll 		     " smaller common\n"),
   1692   1.1     skrll 		   nbfd, name);
   1693   1.1     skrll 	}
   1694   1.8  christos       else
   1695   1.8  christos 	{
   1696   1.8  christos 	  if (obfd != NULL)
   1697   1.8  christos 	    einfo (_("%P: %pB and %pB: warning: multiple common of `%pT'\n"),
   1698   1.8  christos 		   nbfd, obfd, name);
   1699   1.1     skrll 	  else
   1700   1.1     skrll 	    einfo (_("%P: %pB: warning: multiple common of `%pT'\n"),
   1701   1.1     skrll 		   nbfd, name);
   1702   1.1     skrll 	}
   1703   1.1     skrll     }
   1704   1.1     skrll }
   1705   1.1     skrll 
   1706   1.1     skrll /* This is called when BFD has discovered a set element.  H is the
   1707   1.6  christos    entry in the linker hash table for the set.  SECTION and VALUE
   1708   1.1     skrll    represent a value which should be added to the set.  */
   1709   1.1     skrll 
   1710   1.1     skrll static void
   1711   1.1     skrll add_to_set (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1712   1.1     skrll 	    struct bfd_link_hash_entry *h,
   1713   1.1     skrll 	    bfd_reloc_code_real_type reloc,
   1714   1.1     skrll 	    bfd *abfd,
   1715   1.1     skrll 	    asection *section,
   1716   1.1     skrll 	    bfd_vma value)
   1717   1.1     skrll {
   1718   1.1     skrll   if (config.warn_constructors)
   1719   1.6  christos     einfo (_("%P: warning: global constructor %s used\n"),
   1720   1.6  christos 	   h->root.string);
   1721   1.1     skrll 
   1722   1.1     skrll   if (!config.build_constructors)
   1723   1.1     skrll     return;
   1724   1.1     skrll 
   1725   1.1     skrll   ldctor_add_set_entry (h, reloc, NULL, section, value);
   1726   1.1     skrll 
   1727   1.1     skrll   if (h->type == bfd_link_hash_new)
   1728   1.1     skrll     {
   1729   1.1     skrll       h->type = bfd_link_hash_undefined;
   1730   1.1     skrll       h->u.undef.abfd = abfd;
   1731   1.1     skrll       /* We don't call bfd_link_add_undef to add this to the list of
   1732   1.1     skrll 	 undefined symbols because we are going to define it
   1733   1.1     skrll 	 ourselves.  */
   1734   1.1     skrll     }
   1735   1.1     skrll }
   1736   1.1     skrll 
   1737   1.1     skrll /* This is called when BFD has discovered a constructor.  This is only
   1738   1.1     skrll    called for some object file formats--those which do not handle
   1739   1.6  christos    constructors in some more clever fashion.  This is similar to
   1740   1.1     skrll    adding an element to a set, but less general.  */
   1741  1.10  christos 
   1742   1.1     skrll static void
   1743   1.1     skrll constructor_callback (struct bfd_link_info *info,
   1744   1.1     skrll 		      bool constructor,
   1745   1.1     skrll 		      const char *name,
   1746   1.1     skrll 		      bfd *abfd,
   1747   1.1     skrll 		      asection *section,
   1748   1.1     skrll 		      bfd_vma value)
   1749   1.1     skrll {
   1750   1.1     skrll   char *s;
   1751   1.1     skrll   struct bfd_link_hash_entry *h;
   1752   1.1     skrll   char set_name[1 + sizeof "__CTOR_LIST__"];
   1753   1.1     skrll 
   1754   1.6  christos   if (config.warn_constructors)
   1755   1.6  christos     einfo (_("%P: warning: global constructor %s used\n"), name);
   1756   1.1     skrll 
   1757   1.1     skrll   if (!config.build_constructors)
   1758   1.1     skrll     return;
   1759   1.5  christos 
   1760   1.5  christos   /* Ensure that BFD_RELOC_CTOR exists now, so that we can give a
   1761   1.1     skrll      useful error message.  */
   1762  1.12  christos   if (bfd_reloc_type_lookup (info->output_bfd, BFD_RELOC_CTOR) == NULL
   1763   1.1     skrll       && (bfd_link_relocatable (info)
   1764   1.1     skrll 	  || bfd_reloc_type_lookup (abfd, BFD_RELOC_CTOR) == NULL))
   1765   1.1     skrll     fatal (_("%P: BFD backend error: BFD_RELOC_CTOR unsupported\n"));
   1766   1.1     skrll 
   1767   1.1     skrll   s = set_name;
   1768   1.1     skrll   if (bfd_get_symbol_leading_char (abfd) != '\0')
   1769   1.1     skrll     *s++ = bfd_get_symbol_leading_char (abfd);
   1770   1.1     skrll   if (constructor)
   1771   1.1     skrll     strcpy (s, "__CTOR_LIST__");
   1772  1.10  christos   else
   1773   1.1     skrll     strcpy (s, "__DTOR_LIST__");
   1774  1.12  christos 
   1775   1.1     skrll   h = bfd_link_hash_lookup (info->hash, set_name, true, true, true);
   1776   1.1     skrll   if (h == (struct bfd_link_hash_entry *) NULL)
   1777   1.1     skrll     fatal (_("%P: bfd_link_hash_lookup failed: %E\n"));
   1778   1.1     skrll   if (h->type == bfd_link_hash_new)
   1779   1.1     skrll     {
   1780   1.1     skrll       h->type = bfd_link_hash_undefined;
   1781   1.1     skrll       h->u.undef.abfd = abfd;
   1782   1.1     skrll       /* We don't call bfd_link_add_undef to add this to the list of
   1783   1.1     skrll 	 undefined symbols because we are going to define it
   1784   1.1     skrll 	 ourselves.  */
   1785   1.1     skrll     }
   1786   1.1     skrll 
   1787   1.1     skrll   ldctor_add_set_entry (h, BFD_RELOC_CTOR, name, section, value);
   1788   1.1     skrll }
   1789   1.1     skrll 
   1790   1.1     skrll /* A structure used by warning_callback to pass information through
   1791   1.1     skrll    bfd_map_over_sections.  */
   1792  1.10  christos 
   1793   1.1     skrll struct warning_callback_info
   1794   1.1     skrll {
   1795   1.1     skrll   bool found;
   1796   1.1     skrll   const char *warning;
   1797   1.1     skrll   const char *symbol;
   1798   1.5  christos   asymbol **asymbols;
   1799   1.5  christos };
   1800   1.5  christos 
   1801  1.10  christos /* Look through the relocs to see if we can find a plausible address
   1802   1.5  christos    for SYMBOL in ABFD.  Return TRUE if found.  Otherwise return FALSE.  */
   1803   1.5  christos 
   1804   1.5  christos static bool
   1805   1.5  christos symbol_warning (const char *warning, const char *symbol, bfd *abfd)
   1806   1.5  christos {
   1807  1.12  christos   struct warning_callback_info cinfo;
   1808   1.5  christos 
   1809  1.10  christos   if (!bfd_generic_link_read_symbols (abfd))
   1810   1.5  christos     fatal (_("%P: %pB: could not read symbols: %E\n"), abfd);
   1811   1.5  christos 
   1812   1.5  christos   cinfo.found = false;
   1813   1.5  christos   cinfo.warning = warning;
   1814   1.5  christos   cinfo.symbol = symbol;
   1815   1.5  christos   cinfo.asymbols = bfd_get_outsymbols (abfd);
   1816   1.5  christos   bfd_map_over_sections (abfd, warning_find_reloc, &cinfo);
   1817   1.1     skrll   return cinfo.found;
   1818   1.1     skrll }
   1819   1.6  christos 
   1820   1.1     skrll /* This is called when there is a reference to a warning symbol.  */
   1821   1.1     skrll 
   1822   1.1     skrll static void
   1823   1.1     skrll warning_callback (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1824   1.1     skrll 		  const char *warning,
   1825   1.1     skrll 		  const char *symbol,
   1826   1.1     skrll 		  bfd *abfd,
   1827   1.1     skrll 		  asection *section,
   1828   1.1     skrll 		  bfd_vma address)
   1829   1.6  christos {
   1830   1.1     skrll   /* This is a hack to support warn_multiple_gp.  FIXME: This should
   1831   1.6  christos      have a cleaner interface, but what?  */
   1832   1.1     skrll   if (!config.warn_multiple_gp
   1833   1.1     skrll       && strcmp (warning, "using multiple gp values") == 0)
   1834   1.8  christos     return;
   1835   1.1     skrll 
   1836   1.1     skrll   if (section != NULL)
   1837   1.1     skrll     einfo ("%P: %C: %s%s\n", abfd, section, address, _("warning: "), warning);
   1838   1.8  christos   else if (abfd == NULL)
   1839   1.6  christos     einfo ("%P: %s%s\n", _("warning: "), warning);
   1840   1.1     skrll   else if (symbol == NULL)
   1841   1.5  christos     einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
   1842   1.5  christos   else if (!symbol_warning (warning, symbol, abfd))
   1843   1.5  christos     {
   1844   1.5  christos       bfd *b;
   1845   1.6  christos       /* Search all input files for a reference to SYMBOL.  */
   1846   1.8  christos       for (b = info->input_bfds; b; b = b->link.next)
   1847   1.1     skrll 	if (b != abfd && symbol_warning (warning, symbol, b))
   1848   1.1     skrll 	  return;
   1849   1.1     skrll       einfo ("%P: %pB: %s%s\n", abfd, _("warning: "), warning);
   1850   1.1     skrll     }
   1851   1.1     skrll }
   1852   1.1     skrll 
   1853   1.1     skrll /* This is called by warning_callback for each section.  It checks the
   1854   1.1     skrll    relocs of the section to see if it can find a reference to the
   1855   1.1     skrll    symbol which triggered the warning.  If it can, it uses the reloc
   1856   1.1     skrll    to give an error message with a file and line number.  */
   1857   1.1     skrll 
   1858   1.3  christos static void
   1859   1.1     skrll warning_find_reloc (bfd *abfd, asection *sec, void *iarg)
   1860   1.1     skrll {
   1861   1.1     skrll   struct warning_callback_info *info = (struct warning_callback_info *) iarg;
   1862   1.1     skrll   long relsize;
   1863   1.1     skrll   arelent **relpp;
   1864   1.1     skrll   long relcount;
   1865   1.1     skrll   arelent **p, **pend;
   1866   1.1     skrll 
   1867   1.1     skrll   if (info->found)
   1868   1.1     skrll     return;
   1869  1.12  christos 
   1870   1.1     skrll   relsize = bfd_get_reloc_upper_bound (abfd, sec);
   1871   1.1     skrll   if (relsize < 0)
   1872   1.1     skrll     fatal (_("%P: %pB: could not read relocs: %E\n"), abfd);
   1873   1.3  christos   if (relsize == 0)
   1874   1.1     skrll     return;
   1875   1.1     skrll 
   1876  1.12  christos   relpp = (arelent **) xmalloc (relsize);
   1877   1.1     skrll   relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
   1878   1.1     skrll   if (relcount < 0)
   1879   1.1     skrll     fatal (_("%P: %pB: could not read relocs: %E\n"), abfd);
   1880   1.1     skrll 
   1881   1.1     skrll   p = relpp;
   1882   1.1     skrll   pend = p + relcount;
   1883   1.1     skrll   for (; p < pend && *p != NULL; p++)
   1884   1.1     skrll     {
   1885   1.1     skrll       arelent *q = *p;
   1886   1.1     skrll 
   1887   1.1     skrll       if (q->sym_ptr_ptr != NULL
   1888   1.1     skrll 	  && *q->sym_ptr_ptr != NULL
   1889  1.11  christos 	  && strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), info->symbol) == 0)
   1890   1.1     skrll 	{
   1891  1.10  christos 	  /* We found a reloc for the symbol we are looking for.  */
   1892   1.1     skrll 	  einfo ("%P: %H: %s%s\n", abfd, sec, q->address, _("warning: "),
   1893   1.1     skrll 		 info->warning);
   1894   1.1     skrll 	  info->found = true;
   1895   1.1     skrll 	  break;
   1896   1.1     skrll 	}
   1897   1.1     skrll     }
   1898   1.1     skrll 
   1899  1.10  christos   free (relpp);
   1900  1.10  christos }
   1901  1.10  christos 
   1902  1.10  christos #if SUPPORT_ERROR_HANDLING_SCRIPT
   1903   1.1     skrll char * error_handling_script = NULL;
   1904   1.1     skrll #endif
   1905   1.6  christos 
   1906   1.5  christos /* This is called when an undefined symbol is found.  */
   1907   1.1     skrll 
   1908   1.1     skrll static void
   1909   1.1     skrll undefined_symbol (struct bfd_link_info *info,
   1910   1.1     skrll 		  const char *name,
   1911  1.10  christos 		  bfd *abfd,
   1912   1.1     skrll 		  asection *section,
   1913   1.1     skrll 		  bfd_vma address,
   1914   1.1     skrll 		  bool error)
   1915   1.1     skrll {
   1916   1.1     skrll   static char *error_name;
   1917   1.1     skrll   static unsigned int error_count;
   1918   1.2     skrll 
   1919  1.10  christos #define MAX_ERRORS_IN_A_ROW 5
   1920   1.6  christos 
   1921   1.2     skrll   if (info->ignore_hash != NULL
   1922   1.1     skrll       && bfd_hash_lookup (info->ignore_hash, name, false, false) != NULL)
   1923   1.1     skrll     return;
   1924   1.1     skrll 
   1925   1.5  christos   if (config.warn_once)
   1926   1.1     skrll     {
   1927   1.1     skrll       /* Only warn once about a particular undefined symbol.  */
   1928   1.1     skrll       add_ignoresym (info, name);
   1929   1.1     skrll     }
   1930   1.1     skrll 
   1931   1.1     skrll   /* We never print more than a reasonable number of errors in a row
   1932   1.1     skrll      for a single symbol.  */
   1933   1.1     skrll   if (error_name != NULL
   1934   1.1     skrll       && strcmp (name, error_name) == 0)
   1935   1.1     skrll     ++error_count;
   1936  1.10  christos   else
   1937   1.1     skrll     {
   1938   1.1     skrll       error_count = 0;
   1939   1.1     skrll       free (error_name);
   1940  1.10  christos       error_name = xstrdup (name);
   1941  1.10  christos     }
   1942  1.10  christos 
   1943  1.10  christos #if SUPPORT_ERROR_HANDLING_SCRIPT
   1944  1.10  christos   if (error_handling_script != NULL
   1945  1.10  christos       && error_count < MAX_ERRORS_IN_A_ROW)
   1946  1.10  christos     {
   1947  1.10  christos       char *        argv[4];
   1948  1.10  christos       const char *  res;
   1949  1.10  christos       int           status, err;
   1950  1.10  christos 
   1951  1.10  christos       argv[0] = error_handling_script;
   1952  1.10  christos       argv[1] = "undefined-symbol";
   1953  1.10  christos       argv[2] = (char *) name;
   1954  1.10  christos       argv[3] = NULL;
   1955  1.10  christos 
   1956  1.10  christos       if (verbose)
   1957  1.10  christos 	einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
   1958  1.10  christos 	       argv[0], argv[1], argv[2]);
   1959  1.10  christos 
   1960  1.10  christos       res = pex_one (PEX_SEARCH, error_handling_script, argv,
   1961  1.10  christos 		     N_("error handling script"),
   1962  1.10  christos 		     NULL /* Send stdout to random, temp file.  */,
   1963  1.10  christos 		     NULL /* Write to stderr.  */,
   1964  1.10  christos 		     &status, &err);
   1965  1.10  christos       if (res != NULL)
   1966  1.10  christos 	{
   1967  1.10  christos 	  einfo (_("%P: Failed to run error handling script '%s', reason: "),
   1968  1.10  christos 		 error_handling_script);
   1969  1.10  christos 	  /* FIXME: We assume here that errrno == err.  */
   1970  1.10  christos 	  perror (res);
   1971  1.10  christos 	}
   1972  1.10  christos       /* We ignore the return status of the script and
   1973  1.10  christos 	 carry on to issue the normal error message.  */
   1974   1.1     skrll     }
   1975   1.1     skrll #endif /* SUPPORT_ERROR_HANDLING_SCRIPT */
   1976   1.1     skrll 
   1977   1.1     skrll   if (section != NULL)
   1978   1.1     skrll     {
   1979  1.11  christos       if (error_count < MAX_ERRORS_IN_A_ROW)
   1980   1.1     skrll 	{
   1981   1.1     skrll 	  if (error)
   1982  1.11  christos 	    einfo (_("%X%P: %H: undefined reference to `%pT'\n"),
   1983   1.1     skrll 		   abfd, section, address, name);
   1984   1.1     skrll 	  else
   1985   1.1     skrll 	    einfo (_("%P: %H: warning: undefined reference to `%pT'\n"),
   1986   1.1     skrll 		   abfd, section, address, name);
   1987   1.1     skrll 	}
   1988   1.8  christos       else if (error_count == MAX_ERRORS_IN_A_ROW)
   1989   1.1     skrll 	{
   1990   1.1     skrll 	  if (error)
   1991   1.8  christos 	    einfo (_("%X%P: %D: more undefined references to `%pT' follow\n"),
   1992   1.1     skrll 		   abfd, section, address, name);
   1993   1.1     skrll 	  else
   1994   1.1     skrll 	    einfo (_("%P: %D: warning: more undefined references to `%pT' follow\n"),
   1995   1.1     skrll 		   abfd, section, address, name);
   1996   1.1     skrll 	}
   1997   1.1     skrll       else if (error)
   1998   1.1     skrll 	einfo ("%X");
   1999   1.1     skrll     }
   2000   1.1     skrll   else
   2001   1.1     skrll     {
   2002   1.8  christos       if (error_count < MAX_ERRORS_IN_A_ROW)
   2003   1.1     skrll 	{
   2004   1.1     skrll 	  if (error)
   2005   1.8  christos 	    einfo (_("%X%P: %pB: undefined reference to `%pT'\n"),
   2006   1.1     skrll 		   abfd, name);
   2007   1.1     skrll 	  else
   2008   1.1     skrll 	    einfo (_("%P: %pB: warning: undefined reference to `%pT'\n"),
   2009   1.1     skrll 		   abfd, name);
   2010   1.1     skrll 	}
   2011   1.8  christos       else if (error_count == MAX_ERRORS_IN_A_ROW)
   2012   1.1     skrll 	{
   2013   1.1     skrll 	  if (error)
   2014   1.8  christos 	    einfo (_("%X%P: %pB: more undefined references to `%pT' follow\n"),
   2015   1.1     skrll 		   abfd, name);
   2016   1.1     skrll 	  else
   2017   1.1     skrll 	    einfo (_("%P: %pB: warning: more undefined references to `%pT' follow\n"),
   2018   1.1     skrll 		   abfd, name);
   2019   1.1     skrll 	}
   2020   1.1     skrll       else if (error)
   2021   1.1     skrll 	einfo ("%X");
   2022   1.1     skrll     }
   2023   1.1     skrll }
   2024   1.1     skrll 
   2025   1.1     skrll /* Counter to limit the number of relocation overflow error messages
   2026   1.1     skrll    to print.  Errors are printed as it is decremented.  When it's
   2027   1.1     skrll    called and the counter is zero, a final message is printed
   2028   1.1     skrll    indicating more relocations were omitted.  When it gets to -1, no
   2029   1.1     skrll    such errors are printed.  If it's initially set to a value less
   2030   1.1     skrll    than -1, all such errors will be printed (--verbose does this).  */
   2031   1.1     skrll 
   2032   1.1     skrll int overflow_cutoff_limit = 10;
   2033   1.6  christos 
   2034   1.5  christos /* This is called when a reloc overflows.  */
   2035   1.1     skrll 
   2036   1.1     skrll static void
   2037   1.1     skrll reloc_overflow (struct bfd_link_info *info,
   2038   1.1     skrll 		struct bfd_link_hash_entry *entry,
   2039   1.1     skrll 		const char *name,
   2040   1.1     skrll 		const char *reloc_name,
   2041   1.1     skrll 		bfd_vma addend,
   2042   1.1     skrll 		bfd *abfd,
   2043   1.1     skrll 		asection *section,
   2044   1.6  christos 		bfd_vma address)
   2045   1.1     skrll {
   2046   1.9  christos   if (overflow_cutoff_limit == -1)
   2047   1.1     skrll     return;
   2048   1.1     skrll 
   2049   1.1     skrll   einfo ("%X%H:", abfd, section, address);
   2050   1.1     skrll 
   2051   1.1     skrll   if (overflow_cutoff_limit >= 0
   2052   1.6  christos       && overflow_cutoff_limit-- == 0)
   2053   1.1     skrll     {
   2054   1.1     skrll       einfo (_(" additional relocation overflows omitted from the output\n"));
   2055   1.1     skrll       return;
   2056   1.1     skrll     }
   2057   1.1     skrll 
   2058   1.1     skrll   if (entry)
   2059   1.1     skrll     {
   2060   1.1     skrll       while (entry->type == bfd_link_hash_indirect
   2061   1.1     skrll 	     || entry->type == bfd_link_hash_warning)
   2062   1.1     skrll 	entry = entry->u.i.link;
   2063   1.1     skrll       switch (entry->type)
   2064   1.6  christos 	{
   2065   1.8  christos 	case bfd_link_hash_undefined:
   2066   1.1     skrll 	case bfd_link_hash_undefweak:
   2067   1.1     skrll 	  einfo (_(" relocation truncated to fit: "
   2068   1.1     skrll 		   "%s against undefined symbol `%pT'"),
   2069   1.1     skrll 		 reloc_name, entry->root.string);
   2070   1.6  christos 	  break;
   2071   1.8  christos 	case bfd_link_hash_defined:
   2072   1.1     skrll 	case bfd_link_hash_defweak:
   2073   1.1     skrll 	  einfo (_(" relocation truncated to fit: "
   2074   1.1     skrll 		   "%s against symbol `%pT' defined in %pA section in %pB"),
   2075   1.5  christos 		 reloc_name, entry->root.string,
   2076   1.1     skrll 		 entry->u.def.section,
   2077   1.1     skrll 		 entry->u.def.section == bfd_abs_section_ptr
   2078   1.1     skrll 		 ? info->output_bfd : entry->u.def.section->owner);
   2079   1.1     skrll 	  break;
   2080   1.1     skrll 	default:
   2081   1.1     skrll 	  abort ();
   2082   1.1     skrll 	  break;
   2083   1.8  christos 	}
   2084   1.1     skrll     }
   2085   1.1     skrll   else
   2086   1.1     skrll     einfo (_(" relocation truncated to fit: %s against `%pT'"),
   2087   1.1     skrll 	   reloc_name, name);
   2088   1.1     skrll   if (addend != 0)
   2089   1.1     skrll     einfo ("+%v", addend);
   2090   1.1     skrll   einfo ("\n");
   2091   1.1     skrll }
   2092   1.6  christos 
   2093   1.1     skrll /* This is called when a dangerous relocation is made.  */
   2094   1.1     skrll 
   2095   1.1     skrll static void
   2096   1.1     skrll reloc_dangerous (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   2097   1.1     skrll 		 const char *message,
   2098   1.1     skrll 		 bfd *abfd,
   2099   1.9  christos 		 asection *section,
   2100   1.1     skrll 		 bfd_vma address)
   2101   1.1     skrll {
   2102   1.1     skrll   einfo (_("%X%H: dangerous relocation: %s\n"),
   2103   1.1     skrll 	 abfd, section, address, message);
   2104   1.1     skrll }
   2105   1.1     skrll 
   2106   1.6  christos /* This is called when a reloc is being generated attached to a symbol
   2107   1.1     skrll    that is not being output.  */
   2108   1.1     skrll 
   2109   1.1     skrll static void
   2110   1.1     skrll unattached_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   2111   1.1     skrll 		  const char *name,
   2112   1.1     skrll 		  bfd *abfd,
   2113   1.9  christos 		  asection *section,
   2114   1.1     skrll 		  bfd_vma address)
   2115   1.1     skrll {
   2116   1.1     skrll   einfo (_("%X%H: reloc refers to symbol `%pT' which is not being output\n"),
   2117   1.1     skrll 	 abfd, section, address, name);
   2118   1.1     skrll }
   2119   1.3  christos 
   2120   1.3  christos /* This is called if link_info.notice_all is set, or when a symbol in
   2121   1.3  christos    link_info.notice_hash is found.  Symbols are put in notice_hash
   2122   1.3  christos    using the -y option, while notice_all is set if the --cref option
   2123   1.1     skrll    has been supplied, or if there are any NOCROSSREFS sections in the
   2124  1.10  christos    linker script; and if plugins are active, since they need to monitor
   2125   1.1     skrll    all references from non-IR files.  */
   2126   1.3  christos 
   2127   1.5  christos static bool
   2128   1.1     skrll notice (struct bfd_link_info *info,
   2129   1.1     skrll 	struct bfd_link_hash_entry *h,
   2130   1.3  christos 	struct bfd_link_hash_entry *inh ATTRIBUTE_UNUSED,
   2131   1.5  christos 	bfd *abfd,
   2132   1.1     skrll 	asection *section,
   2133   1.3  christos 	bfd_vma value,
   2134   1.3  christos 	flagword flags ATTRIBUTE_UNUSED)
   2135   1.3  christos {
   2136   1.1     skrll   const char *name;
   2137   1.1     skrll 
   2138   1.3  christos   if (h == NULL)
   2139  1.10  christos     {
   2140   1.1     skrll       if (command_line.cref || nocrossref_list != NULL)
   2141   1.1     skrll 	return handle_asneeded_cref (abfd, (enum notice_asneeded_action) value);
   2142   1.3  christos       return true;
   2143   1.3  christos     }
   2144  1.10  christos 
   2145   1.1     skrll   name = h->root.string;
   2146   1.1     skrll   if (info->notice_hash != NULL
   2147   1.8  christos       && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL)
   2148   1.1     skrll     {
   2149   1.8  christos       if (bfd_is_und_section (section))
   2150   1.1     skrll 	einfo (_("%P: %pB: reference to %s\n"), abfd, name);
   2151   1.1     skrll       else
   2152   1.1     skrll 	einfo (_("%P: %pB: definition of %s\n"), abfd, name);
   2153   1.1     skrll     }
   2154   1.1     skrll 
   2155  1.10  christos   if (command_line.cref || nocrossref_list != NULL)
   2156   1.1     skrll     add_cref (name, abfd, section, value);
   2157                 
   2158                   return true;
   2159                 }
   2160