(linenum→info "unix/slp.c:2238")

binutils/2.18/ld/ldmain.c

    1: /* Main program of GNU linker.
    2:    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
    3:    2002, 2003, 2004, 2005, 2006, 2007
    4:    Free Software Foundation, Inc.
    5:    Written by Steve Chamberlain steve@cygnus.com
    6: 
    7:    This file is part of the GNU Binutils.
    8: 
    9:    This program is free software; you can redistribute it and/or modify
   10:    it under the terms of the GNU General Public License as published by
   11:    the Free Software Foundation; either version 3 of the License, or
   12:    (at your option) any later version.
   13: 
   14:    This program is distributed in the hope that it will be useful,
   15:    but WITHOUT ANY WARRANTY; without even the implied warranty of
   16:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17:    GNU General Public License for more details.
   18: 
   19:    You should have received a copy of the GNU General Public License
   20:    along with this program; if not, write to the Free Software
   21:    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   22:    MA 02110-1301, USA.  */
   23: 
   24: #include "sysdep.h"
   25: #include "bfd.h"
   26: #include "safe-ctype.h"
   27: #include "libiberty.h"
   28: #include "progress.h"
   29: #include "bfdlink.h"
   30: #include "filenames.h"
   31: 
   32: #include "ld.h"
   33: #include "ldmain.h"
   34: #include "ldmisc.h"
   35: #include "ldwrite.h"
   36: #include "ldexp.h"
   37: #include "ldlang.h"
   38: #include <ldgram.h>
   39: #include "ldlex.h"
   40: #include "ldfile.h"
   41: #include "ldemul.h"
   42: #include "ldctor.h"
   43: 
   44: /* Somewhere above, sys/stat.h got included.  */
   45: #if !defined(S_ISDIR) && defined(S_IFDIR)
   46: #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
   47: #endif
   48: 
   49: #include <string.h>
   50: 
   51: #ifdef HAVE_SBRK
   52: #if !HAVE_DECL_SBRK
   53: extern void *sbrk ();
   54: #endif
   55: #endif
   56: 
   57: #ifndef TARGET_SYSTEM_ROOT
   58: #define TARGET_SYSTEM_ROOT ""
   59: #endif
   60: 
   61: /* EXPORTS */
   62: 
   63: char *default_target;
   64: const char *output_filename = "a.out";
   65: 
   66: /* Name this program was invoked by.  */
   67: char *program_name;
   68: 
   69: /* The prefix for system library directories.  */
   70: const char *ld_sysroot;
   71: 
   72: /* The canonical representation of ld_sysroot.  */
   73: char * ld_canon_sysroot;
   74: int ld_canon_sysroot_len;
   75: 
   76: /* The file that we're creating.  */
   77: bfd *output_bfd = 0;
   78: 
   79: /* Set by -G argument, for MIPS ECOFF target.  */
   80: int g_switch_value = 8;
   81: 
   82: /* Nonzero means print names of input files as processed.  */
   83: bfd_boolean trace_files;
   84: 
   85: /* Nonzero means same, but note open failures, too.  */
   86: bfd_boolean trace_file_tries;
   87: 
   88: /* Nonzero means version number was printed, so exit successfully
   89:    instead of complaining if no input files are given.  */
   90: bfd_boolean version_printed;
   91: 
   92: /* Nonzero means link in every member of an archive.  */
   93: bfd_boolean whole_archive;
   94: 
   95: /* Nonzero means create DT_NEEDED entries only if a dynamic library
   96:    actually satisfies some reference in a regular object.  */
   97: bfd_boolean as_needed;
   98: 
   99: /* Nonzero means never create DT_NEEDED entries for dynamic libraries
  100:    in DT_NEEDED tags.  */
  101: bfd_boolean add_needed = TRUE;
  102: 
  103: /* TRUE if we should demangle symbol names.  */
  104: bfd_boolean demangling;
  105: 
  106: args_type command_line;
  107: 
  108: ld_config_type config;
  109: 
  110: sort_type sort_section;
  111: 
  112: static const char *get_sysroot
  113:   (int, char **);
  114: static char *get_emulation
  115:   (int, char **);
  116: static void set_scripts_dir
  117:   (void);
  118: static bfd_boolean add_archive_element
  119:   (struct bfd_link_info *, bfd *, const char *);
  120: static bfd_boolean multiple_definition
  121:   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
  122:    bfd *, asection *, bfd_vma);
  123: static bfd_boolean multiple_common
  124:   (struct bfd_link_info *, const char *, bfd *, enum bfd_link_hash_type,
  125:    bfd_vma, bfd *, enum bfd_link_hash_type, bfd_vma);
  126: static bfd_boolean add_to_set
  127:   (struct bfd_link_info *, struct bfd_link_hash_entry *,
  128:    bfd_reloc_code_real_type, bfd *, asection *, bfd_vma);
  129: static bfd_boolean constructor_callback
  130:   (struct bfd_link_info *, bfd_boolean, const char *, bfd *,
  131:    asection *, bfd_vma);
  132: static bfd_boolean warning_callback
  133:   (struct bfd_link_info *, const char *, const char *, bfd *,
  134:    asection *, bfd_vma);
  135: static void warning_find_reloc
  136:   (bfd *, asection *, void *);
  137: static bfd_boolean undefined_symbol
  138:   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma,
  139:    bfd_boolean);
  140: static bfd_boolean reloc_overflow
  141:   (struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
  142:    const char *, bfd_vma, bfd *, asection *, bfd_vma);
  143: static bfd_boolean reloc_dangerous
  144:   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
  145: static bfd_boolean unattached_reloc
  146:   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
  147: static bfd_boolean notice
  148:   (struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma);
  149: 
  150: static struct bfd_link_callbacks link_callbacks =
  151: {
  152:   add_archive_element,
  153:   multiple_definition,
  154:   multiple_common,
  155:   add_to_set,
  156:   constructor_callback,
  157:   warning_callback,
  158:   undefined_symbol,
  159:   reloc_overflow,
  160:   reloc_dangerous,
  161:   unattached_reloc,
  162:   notice,
  163:   einfo,
  164:   info_msg,
  165:   minfo,
  166:   ldlang_override_segment_assignment
  167: };
  168: 
  169: struct bfd_link_info link_info;
  170: ^L
  171: static void
  172: remove_output (void)
  173: {
  174:   if (output_filename)
  175:     {
  176:       if (output_bfd)
  177:         bfd_cache_close (output_bfd);
  178:       if (delete_output_file_on_failure)
  179:         unlink_if_ordinary (output_filename);
  180:     }
  181: }
  182: 
  183: int
  184: main (int argc, char **argv)
  185: {
  186:   char *emulation;
  187:   long start_time = get_run_time ();
  188: 
  189: #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  190:   setlocale (LC_MESSAGES, "");
  191: #endif
  192: #if defined (HAVE_SETLOCALE)
  193:   setlocale (LC_CTYPE, "");
  194: #endif
  195:   bindtextdomain (PACKAGE, LOCALEDIR);
  196:   textdomain (PACKAGE);
  197: 
  198:   program_name = argv[0];
  199:   xmalloc_set_program_name (program_name);
  200: 
  201:   START_PROGRESS (program_name, 0);
  202: 
  203:   expandargv (&argc, &argv);
  204: 
  205:   bfd_init ();
  206: 
  207:   bfd_set_error_program_name (program_name);
  208: 
  209:   xatexit (remove_output);
  210: 
  211:   /* Set up the sysroot directory.  */
  212:   ld_sysroot = get_sysroot (argc, argv);
  213:   if (*ld_sysroot)
  214:     {
  215:       if (*TARGET_SYSTEM_ROOT == 0)
  216:         {
  217:           einfo ("%P%F: this linker was not configured to use sysroots\n");
  218:           ld_sysroot = "";
  219:         }
  220:       else
  221:         ld_canon_sysroot = lrealpath (ld_sysroot);
  222:     }
  223:   if (ld_canon_sysroot)
  224:     ld_canon_sysroot_len = strlen (ld_canon_sysroot);
  225:   else
  226:     ld_canon_sysroot_len = -1;
  227: 
  228:   /* Set the default BFD target based on the configured target.  Doing
  229:      this permits the linker to be configured for a particular target,
  230:      and linked against a shared BFD library which was configured for
  231:      a different target.  The macro TARGET is defined by Makefile.  */
  232:   if (! bfd_set_default_target (TARGET))
  233:     {
  234:       einfo (_("%X%P: can't set BFD default target to `%s': %E\n"), TARGET);
  235:       xexit (1);
  236:     }
  237: 
  238: #if YYDEBUG
  239:   {
  240:     extern int yydebug;
  241:     yydebug = 1;
  242:   }
  243: #endif
  244: 
  245:   config.build_constructors = TRUE;
  246:   config.rpath_separator = ':';
  247:   config.split_by_reloc = (unsigned) -1;
  248:   config.split_by_file = (bfd_size_type) -1;
  249:   config.make_executable = TRUE;
  250:   config.magic_demand_paged = TRUE;
  251:   config.text_read_only = TRUE;
  252: 
  253:   command_line.warn_mismatch = TRUE;
  254:   command_line.warn_search_mismatch = TRUE;
  255:   command_line.check_section_addresses = TRUE;
  256: 
  257:   /* We initialize DEMANGLING based on the environment variable
  258:      COLLECT_NO_DEMANGLE.  The gcc collect2 program will demangle the
  259:      output of the linker, unless COLLECT_NO_DEMANGLE is set in the
  260:      environment.  Acting the same way here lets us provide the same
  261:      interface by default.  */
  262:   demangling = getenv ("COLLECT_NO_DEMANGLE") == NULL;
  263: 
  264:   link_info.allow_undefined_version = TRUE;
  265:   link_info.keep_memory = TRUE;
  266:   link_info.combreloc = TRUE;
  267:   link_info.strip_discarded = TRUE;
  268:   link_info.emit_hash = TRUE;
  269:   link_info.callbacks = &link_callbacks;
  270:   link_info.input_bfds_tail = &link_info.input_bfds;
  271:   /* SVR4 linkers seem to set DT_INIT and DT_FINI based on magic _init
  272:      and _fini symbols.  We are compatible.  */
  273:   link_info.init_function = "_init";
  274:   link_info.fini_function = "_fini";
  275:   link_info.relax_pass = 1;
  276:   link_info.pei386_auto_import = -1;
  277:   link_info.spare_dynamic_tags = 5;
  278: 
  279:   ldfile_add_arch ("");
  280:   emulation = get_emulation (argc, argv);
  281:   ldemul_choose_mode (emulation);
  282:   default_target = ldemul_choose_target (argc, argv);
  283:   lang_init ();
  284:   ldemul_before_parse ();
  285:   lang_has_input_file = FALSE;
  286:   parse_args (argc, argv);
  287: 
  288:   if (config.hash_table_size != 0)
  289:     bfd_hash_set_default_size (config.hash_table_size);
  290: 
  291:   ldemul_set_symbols ();
  292: 
  293:   if (link_info.relocatable)
  294:     {
  295:       if (link_info.gc_sections)
  296:         einfo ("%P%F: --gc-sections and -r may not be used together\n");
  297:       else if (command_line.relax)
  298:         einfo (_("%P%F: --relax and -r may not be used together\n"));
  299:       if (link_info.shared)
  300:         einfo (_("%P%F: -r and -shared may not be used together\n"));
  301:     }
  302: 
  303:   /* We may have -Bsymbolic, -Bsymbolic-functions, --dynamic-list-data,
  304:      --dynamic-list-cpp-new, --dynamic-list-cpp-typeinfo and
  305:      --dynamic-list FILE.  -Bsymbolic and -Bsymbolic-functions are
  306:      for shared libraries.  -Bsymbolic overrides all others and vice
  307:      versa.  */
  308:   switch (command_line.symbolic)
  309:     {
  310:     case symbolic_unset:
  311:       break;
  312:     case symbolic:
  313:       /* -Bsymbolic is for shared library only.  */
  314:       if (link_info.shared)
  315:         {
  316:           link_info.symbolic = TRUE;
  317:           /* Should we free the unused memory?  */
  318:           link_info.dynamic_list = NULL;
  319:           command_line.dynamic_list = dynamic_list_unset;
  320:         }
  321:       break;
  322:     case symbolic_functions:
  323:       /* -Bsymbolic-functions is for shared library only.  */
  324:       if (link_info.shared)
  325:         command_line.dynamic_list = dynamic_list_data;
  326:       break;
  327:     }
  328: 
  329:   switch (command_line.dynamic_list)
  330:     {
  331:     case dynamic_list_unset:
  332:       break;
  333:     case dynamic_list_data:
  334:       link_info.dynamic_data = TRUE;
  335:     case dynamic_list:
  336:       link_info.dynamic = TRUE;
  337:       break;
  338:     }
  339: 
  340:   if (! link_info.shared)
  341:     {
  342:       if (command_line.filter_shlib)
  343:         einfo (_("%P%F: -F may not be used without -shared\n"));
  344:       if (command_line.auxiliary_filters)
  345:         einfo (_("%P%F: -f may not be used without -shared\n"));
  346:     }
  347: 
  348:   if (! link_info.shared || link_info.pie)
  349:     link_info.executable = TRUE;
  350: 
  351:   /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols).  I
  352:      don't see how else this can be handled, since in this case we
  353:      must preserve all externally visible symbols.  */
  354:   if (link_info.relocatable && link_info.strip == strip_all)
  355:     {
  356:       link_info.strip = strip_debugger;
  357:       if (link_info.discard == discard_sec_merge)
  358:         link_info.discard = discard_all;
  359:     }
  360: 
  361:   /* This essentially adds another -L directory so this must be done after
  362:      the -L's in argv have been processed.  */
  363:   set_scripts_dir ();
  364: 
  365:   /* If we have not already opened and parsed a linker script,
  366:      try the default script from command line first.  */
  367:   if (saved_script_handle == NULL
  368:       && command_line.default_script != NULL)
  369:     {
  370:       ldfile_open_command_file (command_line.default_script);
  371:       parser_input = input_script;
  372:       yyparse ();
  373:     }
  374: 
  375:   /* If we have not already opened and parsed a linker script
  376:      read the emulation's appropriate default script.  */
  377:   if (saved_script_handle == NULL)
  378:     {
  379:       int isfile;
  380:       char *s = ldemul_get_script (&isfile);
  381: 
  382:       if (isfile)
  383:         ldfile_open_command_file (s);
  384:       else
  385:         {
  386:           lex_string = s;
  387:           lex_redirect (s);
  388:         }
  389:       parser_input = input_script;
  390:       yyparse ();
  391:       lex_string = NULL;
  392:     }
  393: 
  394:   if (trace_file_tries)
  395:     {
  396:       if (saved_script_handle)
  397:         info_msg (_("using external linker script:"));
  398:       else
  399:         info_msg (_("using internal linker script:"));
  400:       info_msg ("\n==================================================\n");
  401: 
  402:       if (saved_script_handle)
  403:         {
  404:           static const int ld_bufsz = 8193;
  405:           size_t n;
  406:           char *buf = xmalloc (ld_bufsz);
  407: 
  408:           rewind (saved_script_handle);
  409:           while ((n = fread (buf, 1, ld_bufsz - 1, saved_script_handle)) > 0)
  410:             {
  411:               buf[n] = 0;
  412:               info_msg (buf);
  413:             }
  414:           rewind (saved_script_handle);
  415:           free (buf);
  416:         }
  417:       else
  418:         {
  419:           int isfile;
  420: 
  421:           info_msg (ldemul_get_script (&isfile));
  422:         }
  423: 
  424:       info_msg ("\n==================================================\n");
  425:     }
  426: 
  427:   lang_final ();
  428: 
  429:   if (!lang_has_input_file)
  430:     {
  431:       if (version_printed)
  432:         xexit (0);
  433:       einfo (_("%P%F: no input files\n"));
  434:     }
  435: 
  436:   if (trace_files)
  437:     info_msg (_("%P: mode %s\n"), emulation);
  438: 
  439:   ldemul_after_parse ();
  440: 
  441:   if (config.map_filename)
  442:     {
  443:       if (strcmp (config.map_filename, "-") == 0)
  444:         {
  445:           config.map_file = stdout;
  446:         }
  447:       else
  448:         {
  449:           config.map_file = fopen (config.map_filename, FOPEN_WT);
  450:           if (config.map_file == (FILE *) NULL)
  451:             {
  452:               bfd_set_error (bfd_error_system_call);
  453:               einfo (_("%P%F: cannot open map file %s: %E\n"),
  454:                      config.map_filename);
  455:             }
  456:         }
  457:     }
  458: 
  459:   lang_process ();
  460: 
  461:   /* Print error messages for any missing symbols, for any warning
  462:      symbols, and possibly multiple definitions.  */
  463:   if (link_info.relocatable)
  464:     output_bfd->flags &= ~EXEC_P;
  465:   else
  466:     output_bfd->flags |= EXEC_P;
  467: 
  468:   ldwrite ();
  469: 
  470:   if (config.map_file != NULL)
  471:     lang_map ();
  472:   if (command_line.cref)
  473:     output_cref (config.map_file != NULL ? config.map_file : stdout);
  474:   if (nocrossref_list != NULL)
  475:     check_nocrossrefs ();
  476: 
  477:   lang_finish ();
  478: 
  479:   /* Even if we're producing relocatable output, some non-fatal errors should
  480:      be reported in the exit status.  (What non-fatal errors, if any, do we
  481:      want to ignore for relocatable output?)  */
  482:   if (!config.make_executable && !force_make_executable)
  483:     {
  484:       if (trace_files)
  485:         einfo (_("%P: link errors found, deleting executable `%s'\n"),
  486:                output_filename);
  487: 
  488:       /* The file will be removed by remove_output.  */
  489:       xexit (1);
  490:     }
  491:   else
  492:     {
  493:       if (! bfd_close (output_bfd))
  494:         einfo (_("%F%B: final close failed: %E\n"), output_bfd);
  495: 
  496:       /* If the --force-exe-suffix is enabled, and we're making an
  497:          executable file and it doesn't end in .exe, copy it to one
  498:          which does.  */
  499:       if (! link_info.relocatable && command_line.force_exe_suffix)
  500:         {
  501:           int len = strlen (output_filename);
  502: 
  503:           if (len < 4
  504:               || (strcasecmp (output_filename + len - 4, ".exe") != 0
  505:                   && strcasecmp (output_filename + len - 4, ".dll") != 0))
  506:             {
  507:               FILE *src;
  508:               FILE *dst;
  509:               const int bsize = 4096;
  510:               char *buf = xmalloc (bsize);
  511:               int l;
  512:               char *dst_name = xmalloc (len + 5);
  513: 
  514:               strcpy (dst_name, output_filename);
  515:               strcat (dst_name, ".exe");
  516:               src = fopen (output_filename, FOPEN_RB);
  517:               dst = fopen (dst_name, FOPEN_WB);
  518: 
  519:               if (!src)
  520:                 einfo (_("%X%P: unable to open for source of copy `%s'\n"),
  521:                        output_filename);
  522:               if (!dst)
  523:                 einfo (_("%X%P: unable to open for destination of copy `%s'\n"),
  524:                        dst_name);
  525:               while ((l = fread (buf, 1, bsize, src)) > 0)
  526:                 {
  527:                   int done = fwrite (buf, 1, l, dst);
  528: 
  529:                   if (done != l)
  530:                     einfo (_("%P: Error writing file `%s'\n"), dst_name);
  531:                 }
  532: 
  533:               fclose (src);
  534:               if (fclose (dst) == EOF)
  535:                 einfo (_("%P: Error closing file `%s'\n"), dst_name);
  536:               free (dst_name);
  537:               free (buf);
  538:             }
  539:         }
  540:     }
  541: 
  542:   END_PROGRESS (program_name);
  543: 
  544:   if (config.stats)
  545:     {
  546: #ifdef HAVE_SBRK
  547:       char *lim = sbrk (0);
  548: #endif
  549:       long run_time = get_run_time () - start_time;
  550: 
  551:       fprintf (stderr, _("%s: total time in link: %ld.%06ld\n"),
  552:                program_name, run_time / 1000000, run_time % 1000000);
  553: #ifdef HAVE_SBRK
  554:       fprintf (stderr, _("%s: data size %ld\n"), program_name,
  555:                (long) (lim - (char *) &environ));
  556: #endif
  557:     }
  558: 
  559:   /* Prevent remove_output from doing anything, after a successful link.  */
  560:   output_filename = NULL;
  561: 
  562:   xexit (0);
  563:   return 0;
  564: }
  565: 
  566: /* If the configured sysroot is relocatable, try relocating it based on
  567:    default prefix FROM.  Return the relocated directory if it exists,
  568:    otherwise return null.  */
  569: 
  570: static char *
  571: get_relative_sysroot (const char *from ATTRIBUTE_UNUSED)
  572: {
  573: #ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
  574:   char *path;
  575:   struct stat s;
  576: 
  577:   path = make_relative_prefix (program_name, from, TARGET_SYSTEM_ROOT);
  578:   if (path)
  579:     {
  580:       if (stat (path, &s) == 0 && S_ISDIR (s.st_mode))
  581:         return path;
  582:       free (path);
  583:     }
  584: #endif
  585:   return 0;
  586: }
  587: 
  588: /* Return the sysroot directory.  Return "" if no sysroot is being used.  */
  589: 
  590: static const char *
  591: get_sysroot (int argc, char **argv)
  592: {
  593:   int i;
  594:   const char *path;
  595: 
  596:   for (i = 1; i < argc; i++)
  597:     if (CONST_STRNEQ (argv[i], "--sysroot="))
  598:       return argv[i] + strlen ("--sysroot=");
  599: 
  600:   path = get_relative_sysroot (BINDIR);
  601:   if (path)
  602:     return path;
  603: 
  604:   path = get_relative_sysroot (TOOLBINDIR);
  605:   if (path)
  606:     return path;
  607: