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

binutils/2.18/ld/ldlang.c

    1: /* Linker command language support.
    2:    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
    3:    2001, 2002, 2003, 2004, 2005, 2006, 2007
    4:    Free Software Foundation, Inc.
    5: 
    6:    This file is part of the GNU Binutils.
    7: 
    8:    This program is free software; you can redistribute it and/or modify
    9:    it under the terms of the GNU General Public License as published by
   10:    the Free Software Foundation; either version 3 of the License, or
   11:    (at your option) any later version.
   12: 
   13:    This program is distributed in the hope that it will be useful,
   14:    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16:    GNU General Public License for more details.
   17: 
   18:    You should have received a copy of the GNU General Public License
   19:    along with this program; if not, write to the Free Software
   20:    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   21:    MA 02110-1301, USA.  */
   22: 
   23: #include "sysdep.h"
   24: #include "bfd.h"
   25: #include "libiberty.h"
   26: #include "safe-ctype.h"
   27: #include "obstack.h"
   28: #include "bfdlink.h"
   29: 
   30: #include "ld.h"
   31: #include "ldmain.h"
   32: #include "ldexp.h"
   33: #include "ldlang.h"
   34: #include <ldgram.h>
   35: #include "ldlex.h"
   36: #include "ldmisc.h"
   37: #include "ldctor.h"
   38: #include "ldfile.h"
   39: #include "ldemul.h"
   40: #include "fnmatch.h"
   41: #include "demangle.h"
   42: #include "hashtab.h"
   43: 
   44: #ifndef offsetof
   45: #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
   46: #endif
   47: 
   48: /* Locals variables.  */
   49: static struct obstack stat_obstack;
   50: static struct obstack map_obstack;
   51: 
   52: #define obstack_chunk_alloc xmalloc
   53: #define obstack_chunk_free free
   54: static const char *startup_file;
   55: static bfd_boolean placed_commons = FALSE;
   56: static bfd_boolean stripped_excluded_sections = FALSE;
   57: static lang_output_section_statement_type *default_common_section;
   58: static bfd_boolean map_option_f;
   59: static bfd_vma print_dot;
   60: static lang_input_statement_type *first_file;
   61: static const char *current_target;
   62: static const char *output_target;
   63: static lang_statement_list_type statement_list;
   64: static struct bfd_hash_table lang_definedness_table;
   65: 
   66: /* Forward declarations.  */
   67: static void exp_init_os (etree_type *);
   68: static void init_map_userdata (bfd *, asection *, void *);
   69: static lang_input_statement_type *lookup_name (const char *);
   70: static struct bfd_hash_entry *lang_definedness_newfunc
   71:  (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
   72: static void insert_undefined (const char *);
   73: static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
   74: static void print_statement (lang_statement_union_type *,
   75:                              lang_output_section_statement_type *);
   76: static void print_statement_list (lang_statement_union_type *,
   77:                                   lang_output_section_statement_type *);
   78: static void print_statements (void);
   79: static void print_input_section (asection *);
   80: static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
   81: static void lang_record_phdrs (void);
   82: static void lang_do_version_exports_section (void);
   83: static void lang_finalize_version_expr_head
   84:   (struct bfd_elf_version_expr_head *);
   85: 
   86: /* Exported variables.  */
   87: lang_output_section_statement_type *abs_output_section;
   88: lang_statement_list_type lang_output_section_statement;
   89: lang_statement_list_type *stat_ptr = &statement_list;
   90: lang_statement_list_type file_chain = { NULL, NULL };
   91: lang_statement_list_type input_file_chain;
   92: struct bfd_sym_chain entry_symbol = { NULL, NULL };
   93: static const char *entry_symbol_default = "start";
   94: const char *entry_section = ".text";
   95: bfd_boolean entry_from_cmdline;
   96: bfd_boolean lang_has_input_file = FALSE;
   97: bfd_boolean had_output_filename = FALSE;
   98: bfd_boolean lang_float_flag = FALSE;
   99: bfd_boolean delete_output_file_on_failure = FALSE;
  100: struct lang_phdr *lang_phdr_list;
  101: struct lang_nocrossrefs *nocrossref_list;
  102: static struct unique_sections *unique_section_list;
  103: static bfd_boolean ldlang_sysrooted_script = FALSE;
  104: 
  105:  /* Functions that traverse the linker script and might evaluate
  106:     DEFINED() need to increment this.  */
  107: int lang_statement_iteration = 0;
  108: 
  109: etree_type *base; /* Relocation base - or null */
  110: 
  111: /* Return TRUE if the PATTERN argument is a wildcard pattern.
  112:    Although backslashes are treated specially if a pattern contains
  113:    wildcards, we do not consider the mere presence of a backslash to
  114:    be enough to cause the pattern to be treated as a wildcard.
  115:    That lets us handle DOS filenames more naturally.  */
  116: #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
  117: 
  118: #define new_stat(x, y) \
  119:   (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
  120: 
  121: #define outside_section_address(q) \
  122:   ((q)->output_offset + (q)->output_section->vma)
  123: 
  124: #define outside_symbol_address(q) \
  125:   ((q)->value + outside_section_address (q->section))
  126: 
  127: #define SECTION_NAME_MAP_LENGTH (16)
  128: 
  129: void *
  130: stat_alloc (size_t size)
  131: {
  132:   return obstack_alloc (&stat_obstack, size);
  133: }
  134: 
  135: bfd_boolean
  136: unique_section_p (const asection *sec)
  137: {
  138:   struct unique_sections *unam;
  139:   const char *secnam;
  140: 
  141:   if (link_info.relocatable
  142:       && sec->owner != NULL
  143:       && bfd_is_group_section (sec->owner, sec))
  144:     return TRUE;
  145: 
  146:   secnam = sec->name;
  147:   for (unam = unique_section_list; unam; unam = unam->next)
  148:     if (wildcardp (unam->name)
  149:         ? fnmatch (unam->name, secnam, 0) == 0
  150:         : strcmp (unam->name, secnam) == 0)
  151:       {
  152:         return TRUE;
  153:       }
  154: 
  155:   return FALSE;
  156: }
  157: 
  158: /* Generic traversal routines for finding matching sections.  */
  159: 
  160: /* Try processing a section against a wildcard.  This just calls
  161:    the callback unless the filename exclusion list is present
  162:    and excludes the file.  It's hardly ever present so this
  163:    function is very fast.  */
  164: 
  165: static void
  166: walk_wild_consider_section (lang_wild_statement_type *ptr,
  167:                             lang_input_statement_type *file,
  168:                             asection *s,
  169:                             struct wildcard_list *sec,
  170:                             callback_t callback,
  171:                             void *data)
  172: {
  173:   bfd_boolean skip = FALSE;
  174:   struct name_list *list_tmp;
  175: 
  176:   /* Don't process sections from files which were
  177:      excluded.  */
  178:   for (list_tmp = sec->spec.exclude_name_list;
  179:        list_tmp;
  180:        list_tmp = list_tmp->next)
  181:     {
  182:       bfd_boolean is_wildcard = wildcardp (list_tmp->name);
  183:       if (is_wildcard)
  184:         skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
  185:       else
  186:         skip = strcmp (list_tmp->name, file->filename) == 0;
  187: 
  188:       /* If this file is part of an archive, and the archive is
  189:          excluded, exclude this file.  */
  190:       if (! skip && file->the_bfd != NULL
  191:           && file->the_bfd->my_archive != NULL
  192:           && file->the_bfd->my_archive->filename != NULL)
  193:         {
  194:           if (is_wildcard)
  195:             skip = fnmatch (list_tmp->name,
  196:                             file->the_bfd->my_archive->filename,
  197:                             0) == 0;
  198:           else
  199:             skip = strcmp (list_tmp->name,
  200:                            file->the_bfd->my_archive->filename) == 0;
  201:         }
  202: 
  203:       if (skip)
  204:         break;
  205:     }
  206: 
  207:   if (!skip)
  208:     (*callback) (ptr, sec, s, file, data);
  209: }
  210: 
  211: /* Lowest common denominator routine that can handle everything correctly,
  212:    but slowly.  */
  213: 
  214: static void
  215: walk_wild_section_general (lang_wild_statement_type *ptr,
  216:                            lang_input_statement_type *file,
  217:                            callback_t callback,
  218:                            void *data)
  219: {
  220:   asection *s;
  221:   struct wildcard_list *sec;
  222: 
  223:   for (s = file->the_bfd->sections; s != NULL; s = s->next)
  224:     {
  225:       sec = ptr->section_list;
  226:       if (sec == NULL)
  227:         (*callback) (ptr, sec, s, file, data);
  228: 
  229:       while (sec != NULL)
  230:         {
  231:           bfd_boolean skip = FALSE;
  232: 
  233:           if (sec->spec.name != NULL)
  234:             {
  235:               const char *sname = bfd_get_section_name (file->the_bfd, s);
  236: 
  237:               if (wildcardp (sec->spec.name))
  238:                 skip = fnmatch (sec->spec.name, sname, 0) != 0;
  239:               else
  240:                 skip = strcmp (sec->spec.name, sname) != 0;
  241:             }
  242: 
  243:           if (!skip)
  244:             walk_wild_consider_section (ptr, file, s, sec, callback, data);
  245: 
  246:           sec = sec->next;
  247:         }
  248:     }
  249: }
  250: 
  251: /* Routines to find a single section given its name.  If there's more
  252:    than one section with that name, we report that.  */
  253: 
  254: typedef struct
  255: {
  256:   asection *found_section;
  257:   bfd_boolean multiple_sections_found;
  258: } section_iterator_callback_data;
  259: 
  260: static bfd_boolean
  261: section_iterator_callback (bfd *bfd ATTRIBUTE_UNUSED, asection *s, void *data)
  262: {
  263:   section_iterator_callback_data *d = data;
  264: 
  265:   if (d->found_section != NULL)
  266:     {
  267:       d->multiple_sections_found = TRUE;
  268:       return TRUE;
  269:     }
  270: 
  271:   d->found_section = s;
  272:   return FALSE;
  273: }
  274: 
  275: static asection *
  276: find_section (lang_input_statement_type *file,
  277:               struct wildcard_list *sec,
  278:               bfd_boolean *multiple_sections_found)
  279: {
  280:   section_iterator_callback_data cb_data = { NULL, FALSE };
  281: 
  282:   bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
  283:                               section_iterator_callback, &cb_data);
  284:   *multiple_sections_found = cb_data.multiple_sections_found;
  285:   return cb_data.found_section;
  286: }
  287: 
  288: /* Code for handling simple wildcards without going through fnmatch,
  289:    which can be expensive because of charset translations etc.  */
  290: 
  291: /* A simple wild is a literal string followed by a single '*',
  292:    where the literal part is at least 4 characters long.  */
  293: 
  294: static bfd_boolean
  295: is_simple_wild (const char *name)
  296: {
  297:   size_t len = strcspn (name, "*?[");
  298:   return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
  299: }
  300: 
  301: static bfd_boolean
  302: match_simple_wild (const char *pattern, const char *name)
  303: {
  304:   /* The first four characters of the pattern are guaranteed valid
  305:      non-wildcard characters.  So we can go faster.  */
  306:   if (pattern[0] != name[0] || pattern[1] != name[1]
  307:       || pattern[2] != name[2] || pattern[3] != name[3])
  308:     return FALSE;
  309: 
  310:   pattern += 4;
  311:   name += 4;
  312:   while (*pattern != '*')
  313:     if (*name++ != *pattern++)
  314:       return FALSE;
  315: 
  316:   return TRUE;
  317: }
  318: 
  319: /* Compare sections ASEC and BSEC according to SORT.  */
  320: 
  321: static int
  322: compare_section (sort_type sort, asection *asec, asection *bsec)
  323: {
  324:   int ret;
  325: 
  326:   switch (sort)
  327:     {
  328:     default:
  329:       abort ();
  330: 
  331:     case by_alignment_name:
  332:       ret = (bfd_section_alignment (bsec->owner, bsec)
  333:              - bfd_section_alignment (asec->owner, asec));
  334:       if (ret)
  335:         break;
  336:       /* Fall through.  */
  337: 
  338:     case by_name:
  339:       ret = strcmp (bfd_get_section_name (asec->owner, asec),
  340:                     bfd_get_section_name (bsec->owner, bsec));
  341:       break;
  342: 
  343:     case by_name_alignment:
  344:       ret = strcmp (bfd_get_section_name (asec->owner, asec),
  345:                     bfd_get_section_name (bsec->owner, bsec));
  346:       if (ret)
  347:         break;
  348:       /* Fall through.  */
  349: 
  350:     case by_alignment:
  351:       ret = (bfd_section_alignment (bsec->owner, bsec)
  352:              - bfd_section_alignment (asec->owner, asec));
  353:       break;
  354:     }
  355: 
  356:   return ret;
  357: }
  358: 
  359: /* Build a Binary Search Tree to sort sections, unlike insertion sort
  360:    used in wild_sort(). BST is considerably faster if the number of
  361:    of sections are large.  */
  362: 
  363: static lang_section_bst_type **
  364: wild_sort_fast (lang_wild_statement_type *wild,
  365:                 struct wildcard_list *sec,
  366:                 lang_input_statement_type *file ATTRIBUTE_UNUSED,
  367:                 asection *section)
  368: {
  369:   lang_section_bst_type **tree;
  370: 
  371:   tree = &wild->tree;
  372:   if (!wild->filenames_sorted
  373:       && (sec == NULL || sec->spec.sorted == none))
  374:     {
  375:       /* Append at the right end of tree.  */
  376:       while (*tree)
  377:         tree = &((*tree)->right);
  378:       return tree;
  379:     }
  380: 
  381:   while (*tree)
  382:     {
  383:       /* Find the correct node to append this section.  */
  384:       if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
  385:         tree = &((*tree)->left);
  386:       else
  387:         tree = &((*tree)->right);
  388:     }
  389: 
  390:   return tree;
  391: }
  392: 
  393: /* Use wild_sort_fast to build a BST to sort sections.  */
  394: 
  395: static void
  396: output_section_callback_fast (lang_wild_statement_type *ptr,
  397:                               struct wildcard_list *sec,
  398:                               asection *section,
  399:                               lang_input_statement_type *file,
  400:                               void *output ATTRIBUTE_UNUSED)
  401: {
  402:   lang_section_bst_type *node;
  403:   lang_section_bst_type **tree;
  404: 
  405:   if (unique_section_p (section))
  406:     return;
  407: 
  408:   node = xmalloc (sizeof (lang_section_bst_type));
  409:   node->left = 0;
  410:   node->right = 0;
  411:   node->section = section;
  412: 
  413:   tree = wild_sort_fast (ptr, sec, file, section);
  414:   if (tree != NULL)
  415:     *tree = node;
  416: }
  417: 
  418: /* Convert a sorted sections' BST back to list form.  */
  419: 
  420: static void
  421: output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
  422:                                       lang_section_bst_type *tree,
  423:                                       void *output)
  424: {
  425:   if (tree->left)
  426:     output_section_callback_tree_to_list (ptr, tree->left, output);
  427: 
  428:   lang_add_section (&ptr->children, tree->section,
  429:                     (lang_output_section_statement_type *) output);
  430: 
  431:   if (tree->right)
  432:     output_section_callback_tree_to_list (ptr, tree->right, output);
  433: 
  434:   free (tree);
  435: }
  436: 
  437: /* Specialized, optimized routines for handling different kinds of
  438:    wildcards */
  439: 
  440: static void
  441: walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
  442:                                 lang_input_statement_type *file,
  443:                                 callback_t callback,
  444:                                 void *data)
  445: {
  446:   /* We can just do a hash lookup for the section with the right name.
  447:      But if that lookup discovers more than one section with the name
  448:      (should be rare), we fall back to the general algorithm because
  449:      we would otherwise have to sort the sections to make sure they
  450:      get processed in the bfd's order.  */
  451:   bfd_boolean multiple_sections_found;
  452:   struct wildcard_list *sec0 = ptr->handler_data[0];
  453:   asection *s0 = find_section (file, sec0, &multiple_sections_found);
  454: 
  455:   if (multiple_sections_found)
  456:     walk_wild_section_general (ptr, file, callback, data);
  457:   else if (s0)
  458:     walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
  459: }
  460: 
  461: static void
  462: walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
  463:                                 lang_input_statement_type *file,
  464:                                 callback_t callback,
  465:                                 void *data)
  466: {
  467:   asection *s;
  468:   struct wildcard_list *wildsec0 = ptr->handler_data[0];
  469: 
  470:   for (s = file->the_bfd->sections; s != NULL; s = s->next)
  471:     {
  472:       const char *sname = bfd_get_section_name (file->the_bfd, s);
  473:       bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
  474: 
  475:       if (!skip)
  476:         walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
  477:     }
  478: }
  479: 
  480: static void
  481: walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
  482:                                 lang_input_statement_type *file,
  483:                                 callback_t callback,
  484:                                 void *data)
  485: {
  486:   asection *s;
  487:   struct wildcard_list *sec0 = ptr->handler_data[0];
  488:   struct wildcard_list *wildsec1 = ptr->handler_data[1];
  489:   bfd_boolean multiple_sections_found;
  490:   asection *s0 = find_section (file, sec0, &multiple_sections_found);
  491: 
  492:   if (multiple_sections_found)
  493:     {
  494:       walk_wild_section_general (ptr, file, callback, data);
  495:       return;
  496:     }
  497: 
  498:   /* Note that if the section was not found, s0 is NULL and
  499:      we'll simply never succeed the s == s0 test below.  */
  500:   for (s = file->the_bfd->sections; s != NULL; s = s->next)
  501:     {
  502:       /* Recall that in this code path, a section cannot satisfy more
  503:          than one spec, so if s == s0 then it cannot match
  504:          wildspec1.  */
  505:       if (s == s0)
  506:         walk_wild_consider_section (ptr, file, s, sec0, callback, data);
  507:       else
  508:         {
  509:           const char *sname = bfd_get_section_name (file->the_bfd, s);
  510:           bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
  511: 
  512:           if (!skip)
  513:             walk_wild_consider_section (ptr, file, s, wildsec1, callback,
  514:                                         data);
  515:         }
  516:     }
  517: }
  518: 
  519: static void
  520: walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
  521:                                 lang_input_statement_type *file,
  522:                                 callback_t callback,
  523:                                 void *data)
  524: {
  525:   asection *s;
  526:   struct wildcard_list *sec0 = ptr->handler_data[0];
  527:   struct wildcard_list *wildsec1 = ptr->handler_data[1];
  528:   struct wildcard_list *wildsec2 = ptr->handler_data[2];
  529:   bfd_boolean multiple_sections_found;
  530:   asection *s0 = find_section (file, sec0, &multiple_sections_found);
  531: 
  532:   if (multiple_sections_found)
  533:     {
  534:       walk_wild_section_general (ptr, file, callback, data);
  535:       return;
  536:     }
  537: 
  538:   for (s = file->the_bfd->sections; s != NULL; s = s->next)
  539:     {
  540:       if (s == s0)
  541:         walk_wild_consider_section (ptr, file, s, sec0, callback, data);
  542:       else
  543:         {
  544:           const char *sname = bfd_get_section_name (file->the_bfd, s);
  545:           bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
  546: 
  547:           if (!skip)
  548:             walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
  549:           else
  550:             {
  551:               skip = !match_simple_wild (wildsec2->spec.name, sname);
  552:               if (!skip)
  553:                 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
  554:                                             data);
  555:             }
  556:         }
  557:     }
  558: }
  559: 
  560: static void
  561: walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
  562:                                 lang_input_statement_type *file,
  563:                                 callback_t callback,
  564:                                 void *data)
  565: {
  566:   asection *s;
  567:   struct wildcard_list *sec0 = ptr->handler_data[0];
  568:   struct wildcard_list *sec1 = ptr->handler_data[1];
  569:   struct wildcard_list *wildsec2 = ptr->handler_data[2];
  570:   struct wildcard_list *wildsec3 = ptr->handler_data[3];